]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - cmd/mtdparts.c
1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
10 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
13 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 * Added support for reading flash partition table from environment.
16 * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
20 * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
22 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
23 * Copyright 2002 SYSGO Real-Time Solutions GmbH
27 * Three environment variables are used by the parsing routines:
29 * 'partition' - keeps current partition identifier
31 * partition := <part-id>
32 * <part-id> := <dev-id>,part_num
35 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
37 * mtdids=<idmap>[,<idmap>,...]
39 * <idmap> := <dev-id>=<mtd-id>
40 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
41 * <dev-num> := mtd device number, 0...
42 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
45 * 'mtdparts' - partition list
47 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
49 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
50 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
51 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
52 * <size> := standard linux memsize OR '-' to denote all remaining space
53 * <offset> := partition start offset within the device
54 * <name> := '(' NAME ')'
55 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
58 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
59 * - if the above variables are not set defaults for a given target are used
63 * 1 NOR Flash, with 1 single writable partition:
64 * mtdids=nor0=edb7312-nor
65 * mtdparts=mtdparts=edb7312-nor:-
67 * 1 NOR Flash with 2 partitions, 1 NAND with one
68 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
69 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
76 #include <jffs2/load_kernel.h>
77 #include <linux/list.h>
78 #include <linux/ctype.h>
79 #include <linux/err.h>
80 #include <linux/mtd/mtd.h>
82 #if defined(CONFIG_CMD_NAND)
83 #include <linux/mtd/rawnand.h>
87 #if defined(CONFIG_CMD_ONENAND)
88 #include <linux/mtd/onenand.h>
89 #include <onenand_uboot.h>
92 DECLARE_GLOBAL_DATA_PTR
;
94 /* special size referring to all the remaining space in a partition */
95 #define SIZE_REMAINING (~0llu)
97 /* special offset value, it is used when not provided by user
99 * this value is used temporarily during parsing, later such offests
100 * are recalculated */
101 #define OFFSET_NOT_SPECIFIED (~0llu)
103 /* minimum partition size */
104 #define MIN_PART_SIZE 4096
106 /* this flag needs to be set in part_info struct mask_flags
107 * field for read-only partitions */
108 #define MTD_WRITEABLE_CMD 1
110 /* default values for mtdids and mtdparts variables */
111 #if !defined(MTDIDS_DEFAULT)
112 #ifdef CONFIG_MTDIDS_DEFAULT
113 #define MTDIDS_DEFAULT CONFIG_MTDIDS_DEFAULT
115 #define MTDIDS_DEFAULT NULL
118 #if !defined(MTDPARTS_DEFAULT)
119 #ifdef CONFIG_MTDPARTS_DEFAULT
120 #define MTDPARTS_DEFAULT CONFIG_MTDPARTS_DEFAULT
122 #define MTDPARTS_DEFAULT NULL
125 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
126 extern void board_mtdparts_default(const char **mtdids
, const char **mtdparts
);
128 static const char *mtdids_default
= MTDIDS_DEFAULT
;
129 static const char *mtdparts_default
= MTDPARTS_DEFAULT
;
131 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
132 #define MTDIDS_MAXLEN 128
133 #define MTDPARTS_MAXLEN 512
134 #define PARTITION_MAXLEN 16
135 static char last_ids
[MTDIDS_MAXLEN
+ 1];
136 static char last_parts
[MTDPARTS_MAXLEN
+ 1];
137 static char last_partition
[PARTITION_MAXLEN
+ 1];
139 /* low level jffs2 cache cleaning routine */
140 extern void jffs2_free_cache(struct part_info
*part
);
142 /* mtdids mapping list, filled by parse_ids() */
143 static struct list_head mtdids
;
145 /* device/partition list, parse_cmdline() parses into here */
146 static struct list_head devices
;
148 /* current active device and partition number */
149 struct mtd_device
*current_mtd_dev
= NULL
;
150 u8 current_mtd_partnum
= 0;
154 static struct part_info
* mtd_part_info(struct mtd_device
*dev
, unsigned int part_num
);
156 /* command line only routines */
157 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
);
158 static int device_del(struct mtd_device
*dev
);
161 * Parses a string into a number. The number stored at ptr is
162 * potentially suffixed with K (for kilobytes, or 1024 bytes),
163 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
164 * 1073741824). If the number is suffixed with K, M, or G, then
165 * the return value is the number multiplied by one kilobyte, one
166 * megabyte, or one gigabyte, respectively.
168 * @param ptr where parse begins
169 * @param retptr output pointer to next char after parse completes (output)
170 * @return resulting unsigned int
172 static u64
memsize_parse (const char *const ptr
, const char **retptr
)
174 u64 ret
= simple_strtoull(ptr
, (char **)retptr
, 0);
195 * Format string describing supplied size. This routine does the opposite job
196 * to memsize_parse(). Size in bytes is converted to string and if possible
197 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
199 * Note, that this routine does not check for buffer overflow, it's the caller
200 * who must assure enough space.
202 * @param buf output buffer
203 * @param size size to be converted to string
205 static void memsize_format(char *buf
, u64 size
)
207 #define SIZE_GB ((u32)1024*1024*1024)
208 #define SIZE_MB ((u32)1024*1024)
209 #define SIZE_KB ((u32)1024)
211 if ((size
% SIZE_GB
) == 0)
212 sprintf(buf
, "%llug", size
/SIZE_GB
);
213 else if ((size
% SIZE_MB
) == 0)
214 sprintf(buf
, "%llum", size
/SIZE_MB
);
215 else if (size
% SIZE_KB
== 0)
216 sprintf(buf
, "%lluk", size
/SIZE_KB
);
218 sprintf(buf
, "%llu", size
);
222 * This routine does global indexing of all partitions. Resulting index for
223 * current partition is saved in 'mtddevnum'. Current partition name in
226 static void index_partitions(void)
229 struct part_info
*part
;
230 struct list_head
*dentry
;
231 struct mtd_device
*dev
;
233 debug("--- index partitions ---\n");
235 if (current_mtd_dev
) {
237 list_for_each(dentry
, &devices
) {
238 dev
= list_entry(dentry
, struct mtd_device
, link
);
239 if (dev
== current_mtd_dev
) {
240 mtddevnum
+= current_mtd_partnum
;
241 env_set_ulong("mtddevnum", mtddevnum
);
242 debug("=> mtddevnum %d,\n", mtddevnum
);
245 mtddevnum
+= dev
->num_parts
;
248 part
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
250 env_set("mtddevname", part
->name
);
252 debug("=> mtddevname %s\n", part
->name
);
254 env_set("mtddevname", NULL
);
256 debug("=> mtddevname NULL\n");
259 env_set("mtddevnum", NULL
);
260 env_set("mtddevname", NULL
);
262 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
267 * Save current device and partition in environment variable 'partition'.
269 static void current_save(void)
273 debug("--- current_save ---\n");
275 if (current_mtd_dev
) {
276 sprintf(buf
, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev
->id
->type
),
277 current_mtd_dev
->id
->num
, current_mtd_partnum
);
279 env_set("partition", buf
);
280 strncpy(last_partition
, buf
, 16);
282 debug("=> partition %s\n", buf
);
284 env_set("partition", NULL
);
285 last_partition
[0] = '\0';
287 debug("=> partition NULL\n");
294 * Produce a mtd_info given a type and num.
296 * @param type mtd type
297 * @param num mtd number
298 * @param mtd a pointer to an mtd_info instance (output)
299 * @return 0 if device is valid, 1 otherwise
301 static int get_mtd_info(u8 type
, u8 num
, struct mtd_info
**mtd
)
305 sprintf(mtd_dev
, "%s%d", MTD_DEV_TYPE(type
), num
);
306 *mtd
= get_mtd_device_nm(mtd_dev
);
308 printf("Device %s not found!\n", mtd_dev
);
311 put_mtd_device(*mtd
);
317 * Performs sanity check for supplied flash partition.
318 * Table of existing MTD flash devices is searched and partition device
319 * is located. Alignment with the granularity of nand erasesize is verified.
321 * @param id of the parent device
322 * @param part partition to validate
323 * @return 0 if partition is valid, 1 otherwise
325 static int part_validate_eraseblock(struct mtdids
*id
, struct part_info
*part
)
327 struct mtd_info
*mtd
= NULL
;
332 if (get_mtd_info(id
->type
, id
->num
, &mtd
))
335 part
->sector_size
= mtd
->erasesize
;
337 if (!mtd
->numeraseregions
) {
339 * Only one eraseregion (NAND, OneNAND or uniform NOR),
340 * checking for alignment is easy here
342 offset
= part
->offset
;
343 if (do_div(offset
, mtd
->erasesize
)) {
344 printf("%s%d: partition (%s) start offset"
345 "alignment incorrect\n",
346 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
351 if (do_div(size
, mtd
->erasesize
)) {
352 printf("%s%d: partition (%s) size alignment incorrect\n",
353 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
358 * Multiple eraseregions (non-uniform NOR),
359 * checking for alignment is more complex here
362 /* Check start alignment */
363 for (i
= 0; i
< mtd
->numeraseregions
; i
++) {
364 start
= mtd
->eraseregions
[i
].offset
;
365 for (j
= 0; j
< mtd
->eraseregions
[i
].numblocks
; j
++) {
366 if (part
->offset
== start
)
368 start
+= mtd
->eraseregions
[i
].erasesize
;
372 printf("%s%d: partition (%s) start offset alignment incorrect\n",
373 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
378 /* Check end/size alignment */
379 for (i
= 0; i
< mtd
->numeraseregions
; i
++) {
380 start
= mtd
->eraseregions
[i
].offset
;
381 for (j
= 0; j
< mtd
->eraseregions
[i
].numblocks
; j
++) {
382 if ((part
->offset
+ part
->size
) == start
)
384 start
+= mtd
->eraseregions
[i
].erasesize
;
387 /* Check last sector alignment */
388 if ((part
->offset
+ part
->size
) == start
)
391 printf("%s%d: partition (%s) size alignment incorrect\n",
392 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
404 * Performs sanity check for supplied partition. Offset and size are
405 * verified to be within valid range. Partition type is checked and
406 * part_validate_eraseblock() is called with the argument of part.
408 * @param id of the parent device
409 * @param part partition to validate
410 * @return 0 if partition is valid, 1 otherwise
412 static int part_validate(struct mtdids
*id
, struct part_info
*part
)
414 if (part
->size
== SIZE_REMAINING
)
415 part
->size
= id
->size
- part
->offset
;
417 if (part
->offset
> id
->size
) {
418 printf("%s: offset %08llx beyond flash size %08llx\n",
419 id
->mtd_id
, part
->offset
, id
->size
);
423 if ((part
->offset
+ part
->size
) <= part
->offset
) {
424 printf("%s%d: partition (%s) size too big\n",
425 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
429 if (part
->offset
+ part
->size
> id
->size
) {
430 printf("%s: partitioning exceeds flash size\n", id
->mtd_id
);
435 * Now we need to check if the partition starts and ends on
436 * sector (eraseblock) regions
438 return part_validate_eraseblock(id
, part
);
442 * Delete selected partition from the partition list of the specified device.
444 * @param dev device to delete partition from
445 * @param part partition to delete
446 * @return 0 on success, 1 otherwise
448 static int part_del(struct mtd_device
*dev
, struct part_info
*part
)
450 u8 current_save_needed
= 0;
452 /* if there is only one partition, remove whole device */
453 if (dev
->num_parts
== 1)
454 return device_del(dev
);
456 /* otherwise just delete this partition */
458 if (dev
== current_mtd_dev
) {
459 /* we are modyfing partitions for the current device,
461 struct part_info
*curr_pi
;
462 curr_pi
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
465 if (curr_pi
== part
) {
466 printf("current partition deleted, resetting current to 0\n");
467 current_mtd_partnum
= 0;
468 } else if (part
->offset
<= curr_pi
->offset
) {
469 current_mtd_partnum
--;
471 current_save_needed
= 1;
475 list_del(&part
->link
);
479 if (current_save_needed
> 0)
488 * Delete all partitions from parts head list, free memory.
490 * @param head list of partitions to delete
492 static void part_delall(struct list_head
*head
)
494 struct list_head
*entry
, *n
;
495 struct part_info
*part_tmp
;
497 /* clean tmp_list and free allocated memory */
498 list_for_each_safe(entry
, n
, head
) {
499 part_tmp
= list_entry(entry
, struct part_info
, link
);
507 * Add new partition to the supplied partition list. Make sure partitions are
508 * sorted by offset in ascending order.
510 * @param head list this partition is to be added to
511 * @param new partition to be added
513 static int part_sort_add(struct mtd_device
*dev
, struct part_info
*part
)
515 struct list_head
*entry
;
516 struct part_info
*new_pi
, *curr_pi
;
518 /* link partition to parrent dev */
521 if (list_empty(&dev
->parts
)) {
522 debug("part_sort_add: list empty\n");
523 list_add(&part
->link
, &dev
->parts
);
529 new_pi
= list_entry(&part
->link
, struct part_info
, link
);
531 /* get current partition info if we are updating current device */
533 if (dev
== current_mtd_dev
)
534 curr_pi
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
536 list_for_each(entry
, &dev
->parts
) {
537 struct part_info
*pi
;
539 pi
= list_entry(entry
, struct part_info
, link
);
541 /* be compliant with kernel cmdline, allow only one partition at offset zero */
542 if ((new_pi
->offset
== pi
->offset
) && (pi
->offset
== 0)) {
543 printf("cannot add second partition at offset 0\n");
547 if (new_pi
->offset
<= pi
->offset
) {
548 list_add_tail(&part
->link
, entry
);
551 if (curr_pi
&& (pi
->offset
<= curr_pi
->offset
)) {
552 /* we are modyfing partitions for the current
553 * device, update current */
554 current_mtd_partnum
++;
563 list_add_tail(&part
->link
, &dev
->parts
);
570 * Add provided partition to the partition list of a given device.
572 * @param dev device to which partition is added
573 * @param part partition to be added
574 * @return 0 on success, 1 otherwise
576 static int part_add(struct mtd_device
*dev
, struct part_info
*part
)
578 /* verify alignment and size */
579 if (part_validate(dev
->id
, part
) != 0)
582 /* partition is ok, add it to the list */
583 if (part_sort_add(dev
, part
) != 0)
590 * Parse one partition definition, allocate memory and return pointer to this
591 * location in retpart.
593 * @param partdef pointer to the partition definition string i.e. <part-def>
594 * @param ret output pointer to next char after parse completes (output)
595 * @param retpart pointer to the allocated partition (output)
596 * @return 0 on success, 1 otherwise
598 static int part_parse(const char *const partdef
, const char **ret
, struct part_info
**retpart
)
600 struct part_info
*part
;
605 unsigned int mask_flags
;
612 /* fetch the partition size */
614 /* assign all remaining space to this partition */
615 debug("'-': remaining size assigned\n");
616 size
= SIZE_REMAINING
;
619 size
= memsize_parse(p
, &p
);
620 if (size
< MIN_PART_SIZE
) {
621 printf("partition size too small (%llx)\n", size
);
626 /* check for offset */
627 offset
= OFFSET_NOT_SPECIFIED
;
630 offset
= memsize_parse(p
, &p
);
633 /* now look for the name */
636 if ((p
= strchr(name
, ')')) == NULL
) {
637 printf("no closing ) found in partition name\n");
640 name_len
= p
- name
+ 1;
641 if ((name_len
- 1) == 0) {
642 printf("empty partition name\n");
647 /* 0x00000000@0x00000000 */
652 /* test for options */
654 if (strncmp(p
, "ro", 2) == 0) {
655 mask_flags
|= MTD_WRITEABLE_CMD
;
659 /* check for next partition definition */
661 if (size
== SIZE_REMAINING
) {
663 printf("no partitions allowed after a fill-up partition\n");
667 } else if ((*p
== ';') || (*p
== '\0')) {
670 printf("unexpected character '%c' at the end of partition\n", *p
);
675 /* allocate memory */
676 part
= (struct part_info
*)malloc(sizeof(struct part_info
) + name_len
);
678 printf("out of memory\n");
681 memset(part
, 0, sizeof(struct part_info
) + name_len
);
683 part
->offset
= offset
;
684 part
->mask_flags
= mask_flags
;
685 part
->name
= (char *)(part
+ 1);
688 /* copy user provided name */
689 strncpy(part
->name
, name
, name_len
- 1);
692 /* auto generated name in form of size@offset */
693 snprintf(part
->name
, name_len
, "0x%08llx@0x%08llx", size
, offset
);
697 part
->name
[name_len
- 1] = '\0';
698 INIT_LIST_HEAD(&part
->link
);
700 debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d\n",
701 part
->name
, part
->size
,
702 part
->offset
, part
->mask_flags
);
709 * Check device number to be within valid range for given device type.
711 * @param type mtd type
712 * @param num mtd number
713 * @param size a pointer to the size of the mtd device (output)
714 * @return 0 if device is valid, 1 otherwise
716 static int mtd_device_validate(u8 type
, u8 num
, u64
*size
)
718 struct mtd_info
*mtd
= NULL
;
720 if (get_mtd_info(type
, num
, &mtd
))
729 * Delete all mtd devices from a supplied devices list, free memory allocated for
730 * each device and delete all device partitions.
732 * @return 0 on success, 1 otherwise
734 static int device_delall(struct list_head
*head
)
736 struct list_head
*entry
, *n
;
737 struct mtd_device
*dev_tmp
;
739 /* clean devices list */
740 list_for_each_safe(entry
, n
, head
) {
741 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
743 part_delall(&dev_tmp
->parts
);
746 INIT_LIST_HEAD(&devices
);
752 * If provided device exists it's partitions are deleted, device is removed
753 * from device list and device memory is freed.
755 * @param dev device to be deleted
756 * @return 0 on success, 1 otherwise
758 static int device_del(struct mtd_device
*dev
)
760 part_delall(&dev
->parts
);
761 list_del(&dev
->link
);
764 if (dev
== current_mtd_dev
) {
765 /* we just deleted current device */
766 if (list_empty(&devices
)) {
767 current_mtd_dev
= NULL
;
769 /* reset first partition from first dev from the
770 * devices list as current */
771 current_mtd_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
772 current_mtd_partnum
= 0;
783 * Search global device list and return pointer to the device of type and num
786 * @param type device type
787 * @param num device number
788 * @return NULL if requested device does not exist
790 struct mtd_device
*device_find(u8 type
, u8 num
)
792 struct list_head
*entry
;
793 struct mtd_device
*dev_tmp
;
795 list_for_each(entry
, &devices
) {
796 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
798 if ((dev_tmp
->id
->type
== type
) && (dev_tmp
->id
->num
== num
))
806 * Add specified device to the global device list.
808 * @param dev device to be added
810 static void device_add(struct mtd_device
*dev
)
812 u8 current_save_needed
= 0;
814 if (list_empty(&devices
)) {
815 current_mtd_dev
= dev
;
816 current_mtd_partnum
= 0;
817 current_save_needed
= 1;
820 list_add_tail(&dev
->link
, &devices
);
822 if (current_save_needed
> 0)
829 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
830 * return pointer to the device structure.
832 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
833 * @param ret output pointer to next char after parse completes (output)
834 * @param retdev pointer to the allocated device (output)
835 * @return 0 on success, 1 otherwise
837 static int device_parse(const char *const mtd_dev
, const char **ret
, struct mtd_device
**retdev
)
839 struct mtd_device
*dev
;
840 struct part_info
*part
;
843 unsigned int mtd_id_len
;
847 struct list_head
*entry
, *n
;
852 debug("===device_parse===\n");
861 mtd_id
= p
= mtd_dev
;
862 if (!(p
= strchr(mtd_id
, ':'))) {
863 printf("no <mtd-id> identifier\n");
866 mtd_id_len
= p
- mtd_id
+ 1;
869 /* verify if we have a valid device specified */
870 if ((id
= id_find_by_mtd_id(mtd_id
, mtd_id_len
- 1)) == NULL
) {
871 printf("invalid mtd device '%.*s'\n", mtd_id_len
- 1, mtd_id
);
875 pend
= strchr(p
, ';');
876 debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
877 id
->type
, MTD_DEV_TYPE(id
->type
),
878 id
->num
, id
->mtd_id
);
879 debug("parsing partitions %.*s\n", (int)(pend
? pend
- p
: strlen(p
)), p
);
881 /* parse partitions */
885 if ((dev
= device_find(id
->type
, id
->num
)) != NULL
) {
886 /* if device already exists start at the end of the last partition */
887 part
= list_entry(dev
->parts
.prev
, struct part_info
, link
);
888 offset
= part
->offset
+ part
->size
;
891 while (p
&& (*p
!= '\0') && (*p
!= ';')) {
893 if ((part_parse(p
, &p
, &part
) != 0) || (!part
))
896 /* calculate offset when not specified */
897 if (part
->offset
== OFFSET_NOT_SPECIFIED
)
898 part
->offset
= offset
;
900 offset
= part
->offset
;
902 /* verify alignment and size */
903 if (part_validate(id
, part
) != 0)
906 offset
+= part
->size
;
908 /* partition is ok, add it to the list */
909 list_add_tail(&part
->link
, &tmp_list
);
914 part_delall(&tmp_list
);
918 debug("\ntotal partitions: %d\n", num_parts
);
920 /* check for next device presence */
925 } else if (*p
== '\0') {
929 printf("unexpected character '%c' at the end of device\n", *p
);
936 /* allocate memory for mtd_device structure */
937 if ((dev
= (struct mtd_device
*)malloc(sizeof(struct mtd_device
))) == NULL
) {
938 printf("out of memory\n");
941 memset(dev
, 0, sizeof(struct mtd_device
));
943 dev
->num_parts
= 0; /* part_sort_add increments num_parts */
944 INIT_LIST_HEAD(&dev
->parts
);
945 INIT_LIST_HEAD(&dev
->link
);
947 /* move partitions from tmp_list to dev->parts */
948 list_for_each_safe(entry
, n
, &tmp_list
) {
949 part
= list_entry(entry
, struct part_info
, link
);
951 if (part_sort_add(dev
, part
) != 0) {
964 * Initialize global device list.
966 * @return 0 on success, 1 otherwise
968 static int mtd_devices_init(void)
970 last_parts
[0] = '\0';
971 current_mtd_dev
= NULL
;
974 return device_delall(&devices
);
978 * Search global mtdids list and find id of requested type and number.
980 * @return pointer to the id if it exists, NULL otherwise
982 static struct mtdids
* id_find(u8 type
, u8 num
)
984 struct list_head
*entry
;
987 list_for_each(entry
, &mtdids
) {
988 id
= list_entry(entry
, struct mtdids
, link
);
990 if ((id
->type
== type
) && (id
->num
== num
))
998 * Search global mtdids list and find id of a requested mtd_id.
1000 * Note: first argument is not null terminated.
1002 * @param mtd_id string containing requested mtd_id
1003 * @param mtd_id_len length of supplied mtd_id
1004 * @return pointer to the id if it exists, NULL otherwise
1006 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
)
1008 struct list_head
*entry
;
1011 debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1012 mtd_id_len
, mtd_id
, mtd_id_len
);
1014 list_for_each(entry
, &mtdids
) {
1015 id
= list_entry(entry
, struct mtdids
, link
);
1017 debug("entry: '%s' (len = %zu)\n",
1018 id
->mtd_id
, strlen(id
->mtd_id
));
1020 if (mtd_id_len
!= strlen(id
->mtd_id
))
1022 if (strncmp(id
->mtd_id
, mtd_id
, mtd_id_len
) == 0)
1030 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1031 * return device type and number.
1033 * @param id string describing device id
1034 * @param ret_id output pointer to next char after parse completes (output)
1035 * @param dev_type parsed device type (output)
1036 * @param dev_num parsed device number (output)
1037 * @return 0 on success, 1 otherwise
1039 int mtd_id_parse(const char *id
, const char **ret_id
, u8
*dev_type
,
1045 if (strncmp(p
, "nand", 4) == 0) {
1046 *dev_type
= MTD_DEV_TYPE_NAND
;
1048 } else if (strncmp(p
, "nor", 3) == 0) {
1049 *dev_type
= MTD_DEV_TYPE_NOR
;
1051 } else if (strncmp(p
, "onenand", 7) == 0) {
1052 *dev_type
= MTD_DEV_TYPE_ONENAND
;
1055 printf("incorrect device type in %s\n", id
);
1060 printf("incorrect device number in %s\n", id
);
1064 *dev_num
= simple_strtoul(p
, (char **)&p
, 0);
1071 * Process all devices and generate corresponding mtdparts string describing
1072 * all partitions on all devices.
1074 * @param buf output buffer holding generated mtdparts string (output)
1075 * @param buflen buffer size
1076 * @return 0 on success, 1 otherwise
1078 static int generate_mtdparts(char *buf
, u32 buflen
)
1080 struct list_head
*pentry
, *dentry
;
1081 struct mtd_device
*dev
;
1082 struct part_info
*part
, *prev_part
;
1087 u32 maxlen
= buflen
- 1;
1089 debug("--- generate_mtdparts ---\n");
1091 if (list_empty(&devices
)) {
1096 strcpy(p
, "mtdparts=");
1099 list_for_each(dentry
, &devices
) {
1100 dev
= list_entry(dentry
, struct mtd_device
, link
);
1103 len
= strlen(dev
->id
->mtd_id
) + 1;
1106 memcpy(p
, dev
->id
->mtd_id
, len
- 1);
1111 /* format partitions */
1114 list_for_each(pentry
, &dev
->parts
) {
1115 part
= list_entry(pentry
, struct part_info
, link
);
1117 offset
= part
->offset
;
1120 /* partition size */
1121 memsize_format(tmpbuf
, size
);
1122 len
= strlen(tmpbuf
);
1125 memcpy(p
, tmpbuf
, len
);
1130 /* add offset only when there is a gap between
1132 if ((!prev_part
&& (offset
!= 0)) ||
1133 (prev_part
&& ((prev_part
->offset
+ prev_part
->size
) != part
->offset
))) {
1135 memsize_format(tmpbuf
, offset
);
1136 len
= strlen(tmpbuf
) + 1;
1140 memcpy(p
, tmpbuf
, len
- 1);
1145 /* copy name only if user supplied */
1146 if(!part
->auto_name
) {
1147 len
= strlen(part
->name
) + 2;
1152 memcpy(p
, part
->name
, len
- 2);
1159 if (part
->mask_flags
&& MTD_WRITEABLE_CMD
) {
1168 /* print ',' separator if there are other partitions
1170 if (dev
->num_parts
> part_cnt
) {
1178 /* print ';' separator if there are other devices following */
1179 if (dentry
->next
!= &devices
) {
1187 /* we still have at least one char left, as we decremented maxlen at
1194 last_parts
[0] = '\0';
1199 * Call generate_mtdparts to process all devices and generate corresponding
1200 * mtdparts string, save it in mtdparts environment variable.
1202 * @param buf output buffer holding generated mtdparts string (output)
1203 * @param buflen buffer size
1204 * @return 0 on success, 1 otherwise
1206 static int generate_mtdparts_save(char *buf
, u32 buflen
)
1210 ret
= generate_mtdparts(buf
, buflen
);
1212 if ((buf
[0] != '\0') && (ret
== 0))
1213 env_set("mtdparts", buf
);
1215 env_set("mtdparts", NULL
);
1220 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1222 * Get the net size (w/o bad blocks) of the given partition.
1224 * @param mtd the mtd info
1225 * @param part the partition
1226 * @return the calculated net size of this partition
1228 static uint64_t net_part_size(struct mtd_info
*mtd
, struct part_info
*part
)
1230 uint64_t i
, net_size
= 0;
1232 if (!mtd
->block_isbad
)
1235 for (i
= 0; i
< part
->size
; i
+= mtd
->erasesize
) {
1236 if (!mtd
->block_isbad(mtd
, part
->offset
+ i
))
1237 net_size
+= mtd
->erasesize
;
1244 static void print_partition_table(void)
1246 struct list_head
*dentry
, *pentry
;
1247 struct part_info
*part
;
1248 struct mtd_device
*dev
;
1251 list_for_each(dentry
, &devices
) {
1252 dev
= list_entry(dentry
, struct mtd_device
, link
);
1253 /* list partitions for given device */
1255 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1256 struct mtd_info
*mtd
;
1258 if (get_mtd_info(dev
->id
->type
, dev
->id
->num
, &mtd
))
1261 printf("\ndevice %s%d <%s>, # parts = %d\n",
1262 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1263 dev
->id
->mtd_id
, dev
->num_parts
);
1264 printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
1266 list_for_each(pentry
, &dev
->parts
) {
1270 part
= list_entry(pentry
, struct part_info
, link
);
1271 net_size
= net_part_size(mtd
, part
);
1272 size_note
= part
->size
== net_size
? " " : " (!)";
1273 printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
1274 part_num
, part
->name
, part
->size
,
1275 net_size
, size_note
, part
->offset
,
1277 #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1278 printf("\ndevice %s%d <%s>, # parts = %d\n",
1279 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1280 dev
->id
->mtd_id
, dev
->num_parts
);
1281 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
1283 list_for_each(pentry
, &dev
->parts
) {
1284 part
= list_entry(pentry
, struct part_info
, link
);
1285 printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
1286 part_num
, part
->name
, part
->size
,
1287 part
->offset
, part
->mask_flags
);
1288 #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1293 if (list_empty(&devices
))
1294 printf("no partitions defined\n");
1298 * Format and print out a partition list for each device from global device
1301 static void list_partitions(void)
1303 struct part_info
*part
;
1305 debug("\n---list_partitions---\n");
1306 print_partition_table();
1308 /* current_mtd_dev is not NULL only when we have non empty device list */
1309 if (current_mtd_dev
) {
1310 part
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
1312 printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n",
1313 MTD_DEV_TYPE(current_mtd_dev
->id
->type
),
1314 current_mtd_dev
->id
->num
, current_mtd_partnum
,
1315 part
->name
, part
->size
, part
->offset
);
1317 printf("could not get current partition info\n\n");
1321 printf("\ndefaults:\n");
1322 printf("mtdids : %s\n",
1323 mtdids_default
? mtdids_default
: "none");
1325 * Using printf() here results in printbuffer overflow
1326 * if default mtdparts string is greater than console
1327 * printbuffer. Use puts() to prevent system crashes.
1330 puts(mtdparts_default
? mtdparts_default
: "none");
1335 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1336 * corresponding device and verify partition number.
1338 * @param id string describing device and partition or partition name
1339 * @param dev pointer to the requested device (output)
1340 * @param part_num verified partition number (output)
1341 * @param part pointer to requested partition (output)
1342 * @return 0 on success, 1 otherwise
1344 int find_dev_and_part(const char *id
, struct mtd_device
**dev
,
1345 u8
*part_num
, struct part_info
**part
)
1347 struct list_head
*dentry
, *pentry
;
1348 u8 type
, dnum
, pnum
;
1351 debug("--- find_dev_and_part ---\nid = %s\n", id
);
1353 list_for_each(dentry
, &devices
) {
1355 *dev
= list_entry(dentry
, struct mtd_device
, link
);
1356 list_for_each(pentry
, &(*dev
)->parts
) {
1357 *part
= list_entry(pentry
, struct part_info
, link
);
1358 if (strcmp((*part
)->name
, id
) == 0)
1369 if (mtd_id_parse(p
, &p
, &type
, &dnum
) != 0)
1372 if ((*p
++ != ',') || (*p
== '\0')) {
1373 printf("no partition number specified\n");
1376 pnum
= simple_strtoul(p
, (char **)&p
, 0);
1378 printf("unexpected trailing character '%c'\n", *p
);
1382 if ((*dev
= device_find(type
, dnum
)) == NULL
) {
1383 printf("no such device %s%d\n", MTD_DEV_TYPE(type
), dnum
);
1387 if ((*part
= mtd_part_info(*dev
, pnum
)) == NULL
) {
1388 printf("no such partition\n");
1399 * Find and delete partition. For partition id format see find_dev_and_part().
1401 * @param id string describing device and partition
1402 * @return 0 on success, 1 otherwise
1404 static int delete_partition(const char *id
)
1407 struct mtd_device
*dev
;
1408 struct part_info
*part
;
1410 if (find_dev_and_part(id
, &dev
, &pnum
, &part
) == 0) {
1412 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n",
1413 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
,
1414 part
->name
, part
->size
, part
->offset
);
1416 if (part_del(dev
, part
) != 0)
1419 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
1420 printf("generated mtdparts too long, resetting to null\n");
1426 printf("partition %s not found\n", id
);
1430 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1432 * Increase the size of the given partition so that it's net size is at least
1433 * as large as the size member and such that the next partition would start on a
1434 * good block if it were adjacent to this partition.
1436 * @param mtd the mtd device
1437 * @param part the partition
1438 * @param next_offset pointer to the offset of the next partition after this
1439 * partition's size has been modified (output)
1441 static void spread_partition(struct mtd_info
*mtd
, struct part_info
*part
,
1442 uint64_t *next_offset
)
1444 uint64_t net_size
, padding_size
= 0;
1447 mtd_get_len_incl_bad(mtd
, part
->offset
, part
->size
, &net_size
,
1451 * Absorb bad blocks immediately following this
1452 * partition also into the partition, such that
1453 * the next partition starts with a good block.
1456 mtd_get_len_incl_bad(mtd
, part
->offset
+ net_size
,
1457 mtd
->erasesize
, &padding_size
, &truncated
);
1461 padding_size
-= mtd
->erasesize
;
1465 printf("truncated partition %s to %lld bytes\n", part
->name
,
1466 (uint64_t) net_size
+ padding_size
);
1469 part
->size
= net_size
+ padding_size
;
1470 *next_offset
= part
->offset
+ part
->size
;
1474 * Adjust all of the partition sizes, such that all partitions are at least
1475 * as big as their mtdparts environment variable sizes and they each start
1478 * @return 0 on success, 1 otherwise
1480 static int spread_partitions(void)
1482 struct list_head
*dentry
, *pentry
;
1483 struct mtd_device
*dev
;
1484 struct part_info
*part
;
1485 struct mtd_info
*mtd
;
1489 list_for_each(dentry
, &devices
) {
1490 dev
= list_entry(dentry
, struct mtd_device
, link
);
1492 if (get_mtd_info(dev
->id
->type
, dev
->id
->num
, &mtd
))
1497 list_for_each(pentry
, &dev
->parts
) {
1498 part
= list_entry(pentry
, struct part_info
, link
);
1500 debug("spread_partitions: device = %s%d, partition %d ="
1501 " (%s) 0x%08llx@0x%08llx\n",
1502 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1503 part_num
, part
->name
, part
->size
,
1506 if (cur_offs
> part
->offset
)
1507 part
->offset
= cur_offs
;
1509 spread_partition(mtd
, part
, &cur_offs
);
1517 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
1518 printf("generated mtdparts too long, resetting to null\n");
1523 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1526 * The mtdparts variable tends to be long. If we need to access it
1527 * before the env is relocated, then we need to use our own stack
1528 * buffer. gd->env_buf will be too small.
1530 * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
1531 * @return mtdparts variable string, NULL if not found
1533 static const char *env_get_mtdparts(char *buf
)
1535 if (gd
->flags
& GD_FLG_ENV_READY
)
1536 return env_get("mtdparts");
1537 if (env_get_f("mtdparts", buf
, MTDPARTS_MAXLEN
) != -1)
1543 * Accept character string describing mtd partitions and call device_parse()
1544 * for each entry. Add created devices to the global devices list.
1546 * @param mtdparts string specifing mtd partitions
1547 * @return 0 on success, 1 otherwise
1549 static int parse_mtdparts(const char *const mtdparts
)
1552 struct mtd_device
*dev
;
1554 char tmp_parts
[MTDPARTS_MAXLEN
];
1556 debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", mtdparts
);
1558 /* delete all devices and partitions */
1559 if (mtd_devices_init() != 0) {
1560 printf("could not initialise device list\n");
1564 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1565 p
= env_get_mtdparts(tmp_parts
);
1569 if (strncmp(p
, "mtdparts=", 9) != 0) {
1570 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1575 while (*p
!= '\0') {
1577 if ((device_parse(p
, &p
, &dev
) != 0) || (!dev
))
1580 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
1581 dev
->id
->num
, dev
->id
->mtd_id
);
1583 /* check if parsed device is already on the list */
1584 if (device_find(dev
->id
->type
, dev
->id
->num
) != NULL
) {
1585 printf("device %s%d redefined, please correct mtdparts variable\n",
1586 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
1590 list_add_tail(&dev
->link
, &devices
);
1595 device_delall(&devices
);
1602 * Parse provided string describing mtdids mapping (see file header for mtdids
1603 * variable format). Allocate memory for each entry and add all found entries
1604 * to the global mtdids list.
1606 * @param ids mapping string
1607 * @return 0 on success, 1 otherwise
1609 static int parse_mtdids(const char *const ids
)
1611 const char *p
= ids
;
1615 struct list_head
*entry
, *n
;
1616 struct mtdids
*id_tmp
;
1621 debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids
);
1623 /* clean global mtdids list */
1624 list_for_each_safe(entry
, n
, &mtdids
) {
1625 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1626 debug("mtdids del: %d %d\n", id_tmp
->type
, id_tmp
->num
);
1631 INIT_LIST_HEAD(&mtdids
);
1633 while(p
&& (*p
!= '\0')) {
1636 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1637 if (mtd_id_parse(p
, &p
, &type
, &num
) != 0)
1641 printf("mtdids: incorrect <dev-num>\n");
1646 /* check if requested device exists */
1647 if (mtd_device_validate(type
, num
, &size
) != 0)
1650 /* locate <mtd-id> */
1652 if ((p
= strchr(mtd_id
, ',')) != NULL
) {
1653 mtd_id_len
= p
- mtd_id
+ 1;
1656 mtd_id_len
= strlen(mtd_id
) + 1;
1658 if (mtd_id_len
== 0) {
1659 printf("mtdids: no <mtd-id> identifier\n");
1663 /* check if this id is already on the list */
1664 int double_entry
= 0;
1665 list_for_each(entry
, &mtdids
) {
1666 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1667 if ((id_tmp
->type
== type
) && (id_tmp
->num
== num
)) {
1673 printf("device id %s%d redefined, please correct mtdids variable\n",
1674 MTD_DEV_TYPE(type
), num
);
1678 /* allocate mtdids structure */
1679 if (!(id
= (struct mtdids
*)malloc(sizeof(struct mtdids
) + mtd_id_len
))) {
1680 printf("out of memory\n");
1683 memset(id
, 0, sizeof(struct mtdids
) + mtd_id_len
);
1687 id
->mtd_id
= (char *)(id
+ 1);
1688 strncpy(id
->mtd_id
, mtd_id
, mtd_id_len
- 1);
1689 id
->mtd_id
[mtd_id_len
- 1] = '\0';
1690 INIT_LIST_HEAD(&id
->link
);
1692 debug("+ id %s%d\t%16lld bytes\t%s\n",
1693 MTD_DEV_TYPE(id
->type
), id
->num
,
1694 id
->size
, id
->mtd_id
);
1696 list_add_tail(&id
->link
, &mtdids
);
1700 /* clean mtdids list and free allocated memory */
1701 list_for_each_safe(entry
, n
, &mtdids
) {
1702 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1714 * Parse and initialize global mtdids mapping and create global
1715 * device/partition list.
1717 * @return 0 on success, 1 otherwise
1719 int mtdparts_init(void)
1721 static int initialized
= 0;
1722 const char *ids
, *parts
;
1723 const char *current_partition
;
1725 char tmp_ep
[PARTITION_MAXLEN
+ 1];
1726 char tmp_parts
[MTDPARTS_MAXLEN
];
1728 debug("\n---mtdparts_init---\n");
1730 INIT_LIST_HEAD(&mtdids
);
1731 INIT_LIST_HEAD(&devices
);
1732 memset(last_ids
, 0, sizeof(last_ids
));
1733 memset(last_parts
, 0, sizeof(last_parts
));
1734 memset(last_partition
, 0, sizeof(last_partition
));
1735 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
1736 board_mtdparts_default(&mtdids_default
, &mtdparts_default
);
1743 ids
= env_get("mtdids");
1744 parts
= env_get_mtdparts(tmp_parts
);
1745 current_partition
= env_get("partition");
1747 /* save it for later parsing, cannot rely on current partition pointer
1748 * as 'partition' variable may be updated during init */
1749 memset(tmp_parts
, 0, sizeof(tmp_parts
));
1750 memset(tmp_ep
, 0, sizeof(tmp_ep
));
1751 if (current_partition
)
1752 strncpy(tmp_ep
, current_partition
, PARTITION_MAXLEN
);
1754 debug("last_ids : %s\n", last_ids
);
1755 debug("env_ids : %s\n", ids
);
1756 debug("last_parts: %s\n", last_parts
);
1757 debug("env_parts : %s\n\n", parts
);
1759 debug("last_partition : %s\n", last_partition
);
1760 debug("env_partition : %s\n", current_partition
);
1762 /* if mtdids variable is empty try to use defaults */
1764 if (mtdids_default
) {
1765 debug("mtdids variable not defined, using default\n");
1766 ids
= mtdids_default
;
1767 env_set("mtdids", (char *)ids
);
1769 printf("mtdids not defined, no default present\n");
1773 if (strlen(ids
) > MTDIDS_MAXLEN
- 1) {
1774 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN
);
1778 /* use defaults when mtdparts variable is not defined
1779 * once mtdparts is saved environment, drop use_defaults flag */
1781 if (mtdparts_default
&& use_defaults
) {
1782 parts
= mtdparts_default
;
1783 if (env_set("mtdparts", (char *)parts
) == 0)
1786 printf("mtdparts variable not set, see 'help mtdparts'\n");
1789 if (parts
&& (strlen(parts
) > MTDPARTS_MAXLEN
- 1)) {
1790 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN
);
1794 /* check if we have already parsed those mtdids */
1795 if ((last_ids
[0] != '\0') && (strcmp(last_ids
, ids
) == 0)) {
1800 if (parse_mtdids(ids
) != 0) {
1805 /* ok it's good, save new ids */
1806 strncpy(last_ids
, ids
, MTDIDS_MAXLEN
);
1809 /* parse partitions if either mtdparts or mtdids were updated */
1810 if (parts
&& ((last_parts
[0] == '\0') || ((strcmp(last_parts
, parts
) != 0)) || ids_changed
)) {
1811 if (parse_mtdparts(parts
) != 0)
1814 if (list_empty(&devices
)) {
1815 printf("mtdparts_init: no valid partitions\n");
1819 /* ok it's good, save new parts */
1820 strncpy(last_parts
, parts
, MTDPARTS_MAXLEN
);
1822 /* reset first partition from first dev from the list as current */
1823 current_mtd_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
1824 current_mtd_partnum
= 0;
1827 debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
1828 MTD_DEV_TYPE(current_mtd_dev
->id
->type
),
1829 current_mtd_dev
->id
->num
, current_mtd_partnum
);
1832 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1833 if (!parts
&& (last_parts
[0] != '\0'))
1834 return mtd_devices_init();
1836 /* do not process current partition if mtdparts variable is null */
1840 /* is current partition set in environment? if so, use it */
1841 if ((tmp_ep
[0] != '\0') && (strcmp(tmp_ep
, last_partition
) != 0)) {
1842 struct part_info
*p
;
1843 struct mtd_device
*cdev
;
1846 debug("--- getting current partition: %s\n", tmp_ep
);
1848 if (find_dev_and_part(tmp_ep
, &cdev
, &pnum
, &p
) == 0) {
1849 current_mtd_dev
= cdev
;
1850 current_mtd_partnum
= pnum
;
1853 } else if (env_get("partition") == NULL
) {
1854 debug("no partition variable set, setting...\n");
1862 * Return pointer to the partition of a requested number from a requested
1865 * @param dev device that is to be searched for a partition
1866 * @param part_num requested partition number
1867 * @return pointer to the part_info, NULL otherwise
1869 static struct part_info
* mtd_part_info(struct mtd_device
*dev
, unsigned int part_num
)
1871 struct list_head
*entry
;
1872 struct part_info
*part
;
1878 debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1879 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1880 dev
->id
->num
, dev
->id
->mtd_id
);
1882 if (part_num
>= dev
->num_parts
) {
1883 printf("invalid partition number %d for device %s%d (%s)\n",
1884 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1885 dev
->id
->num
, dev
->id
->mtd_id
);
1889 /* locate partition number, return it */
1891 list_for_each(entry
, &dev
->parts
) {
1892 part
= list_entry(entry
, struct part_info
, link
);
1894 if (part_num
== num
++) {
1902 /***************************************************/
1903 /* U-Boot commands */
1904 /***************************************************/
1905 /* command line only */
1907 * Routine implementing u-boot chpart command. Sets new current partition based
1908 * on the user supplied partition id. For partition id format see find_dev_and_part().
1910 * @param cmdtp command internal data
1911 * @param flag command flag
1912 * @param argc number of arguments supplied to the command
1913 * @param argv arguments list
1914 * @return 0 on success, 1 otherwise
1916 static int do_chpart(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1918 /* command line only */
1919 struct mtd_device
*dev
;
1920 struct part_info
*part
;
1923 if (mtdparts_init() !=0)
1927 printf("no partition id specified\n");
1931 if (find_dev_and_part(argv
[1], &dev
, &pnum
, &part
) != 0)
1934 current_mtd_dev
= dev
;
1935 current_mtd_partnum
= pnum
;
1938 printf("partition changed to %s%d,%d\n",
1939 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
);
1945 * Routine implementing u-boot mtdparts command. Initialize/update default global
1946 * partition list and process user partition request (list, add, del).
1948 * @param cmdtp command internal data
1949 * @param flag command flag
1950 * @param argc number of arguments supplied to the command
1951 * @param argv arguments list
1952 * @return 0 on success, 1 otherwise
1954 static int do_mtdparts(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
1955 char * const argv
[])
1958 if (strcmp(argv
[1], "default") == 0) {
1959 env_set("mtdids", NULL
);
1960 env_set("mtdparts", NULL
);
1961 env_set("partition", NULL
);
1966 } else if (strcmp(argv
[1], "delall") == 0) {
1967 /* this may be the first run, initialize lists if needed */
1970 env_set("mtdparts", NULL
);
1972 /* mtd_devices_init() calls current_save() */
1973 return mtd_devices_init();
1977 /* make sure we are in sync with env variables */
1978 if (mtdparts_init() != 0)
1986 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
1987 if (((argc
== 5) || (argc
== 6)) && (strncmp(argv
[1], "add", 3) == 0)) {
1988 #define PART_ADD_DESC_MAXLEN 64
1989 char tmpbuf
[PART_ADD_DESC_MAXLEN
];
1990 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1991 struct mtd_info
*mtd
;
1992 uint64_t next_offset
;
1995 struct mtd_device
*dev
;
1996 struct mtd_device
*dev_tmp
;
1998 struct part_info
*p
;
2000 if (mtd_id_parse(argv
[2], NULL
, &type
, &num
) != 0)
2003 if ((id
= id_find(type
, num
)) == NULL
) {
2004 printf("no such device %s defined in mtdids variable\n", argv
[2]);
2008 len
= strlen(id
->mtd_id
) + 1; /* 'mtd_id:' */
2009 len
+= strlen(argv
[3]); /* size@offset */
2010 len
+= strlen(argv
[4]) + 2; /* '(' name ')' */
2011 if (argv
[5] && (strlen(argv
[5]) == 2))
2012 len
+= 2; /* 'ro' */
2014 if (len
>= PART_ADD_DESC_MAXLEN
) {
2015 printf("too long partition description\n");
2018 sprintf(tmpbuf
, "%s:%s(%s)%s",
2019 id
->mtd_id
, argv
[3], argv
[4], argv
[5] ? argv
[5] : "");
2020 debug("add tmpbuf: %s\n", tmpbuf
);
2022 if ((device_parse(tmpbuf
, NULL
, &dev
) != 0) || (!dev
))
2025 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
2026 dev
->id
->num
, dev
->id
->mtd_id
);
2028 p
= list_entry(dev
->parts
.next
, struct part_info
, link
);
2030 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2031 if (get_mtd_info(dev
->id
->type
, dev
->id
->num
, &mtd
))
2034 if (!strcmp(&argv
[1][3], ".spread")) {
2035 spread_partition(mtd
, p
, &next_offset
);
2036 debug("increased %s to %llu bytes\n", p
->name
, p
->size
);
2040 dev_tmp
= device_find(dev
->id
->type
, dev
->id
->num
);
2041 if (dev_tmp
== NULL
) {
2043 } else if (part_add(dev_tmp
, p
) != 0) {
2044 /* merge new partition with existing ones*/
2049 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
2050 printf("generated mtdparts too long, resetting to null\n");
2057 /* mtdparts del part-id */
2058 if ((argc
== 3) && (strcmp(argv
[1], "del") == 0)) {
2059 debug("del: part-id = %s\n", argv
[2]);
2061 return delete_partition(argv
[2]);
2064 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2065 if ((argc
== 2) && (strcmp(argv
[1], "spread") == 0))
2066 return spread_partitions();
2067 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2069 return CMD_RET_USAGE
;
2072 /***************************************************/
2074 chpart
, 2, 0, do_chpart
,
2075 "change active partition",
2077 " - change active partition (e.g. part-id = nand0,1)"
2080 #ifdef CONFIG_SYS_LONGHELP
2081 static char mtdparts_help_text
[] =
2083 " - list partition table\n"
2085 " - delete all partitions\n"
2086 "mtdparts del part-id\n"
2087 " - delete partition (e.g. part-id = nand0,1)\n"
2088 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2089 " - add partition\n"
2090 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2091 "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2092 " - add partition, padding size by skipping bad blocks\n"
2094 "mtdparts default\n"
2095 " - reset partition table to defaults\n"
2096 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2098 " - adjust the sizes of the partitions so they are\n"
2099 " at least as big as the mtdparts variable specifies\n"
2100 " and they each start on a good block\n\n"
2103 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2105 "this command uses three environment variables:\n\n"
2106 "'partition' - keeps current partition identifier\n\n"
2107 "partition := <part-id>\n"
2108 "<part-id> := <dev-id>,part_num\n\n"
2109 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2110 "mtdids=<idmap>[,<idmap>,...]\n\n"
2111 "<idmap> := <dev-id>=<mtd-id>\n"
2112 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
2113 "<dev-num> := mtd device number, 0...\n"
2114 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2115 "'mtdparts' - partition list\n\n"
2116 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2117 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2118 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2119 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2120 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2121 "<offset> := partition start offset within the device\n"
2122 "<name> := '(' NAME ')'\n"
2123 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)";
2127 mtdparts
, 6, 0, do_mtdparts
,
2128 "define flash/nand partitions", mtdparts_help_text
2130 /***************************************************/