]>
git.ipfire.org Git - people/ms/u-boot.git/blob - cmd/mtdparts.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
9 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
12 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 * Added support for reading flash partition table from environment.
15 * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
19 * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
21 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
22 * Copyright 2002 SYSGO Real-Time Solutions GmbH
24 * SPDX-License-Identifier: GPL-2.0+
28 * Three environment variables are used by the parsing routines:
30 * 'partition' - keeps current partition identifier
32 * partition := <part-id>
33 * <part-id> := <dev-id>,part_num
36 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
38 * mtdids=<idmap>[,<idmap>,...]
40 * <idmap> := <dev-id>=<mtd-id>
41 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
42 * <dev-num> := mtd device number, 0...
43 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
46 * 'mtdparts' - partition list
48 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
50 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
51 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
52 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
53 * <size> := standard linux memsize OR '-' to denote all remaining space
54 * <offset> := partition start offset within the device
55 * <name> := '(' NAME ')'
56 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
59 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
60 * - if the above variables are not set defaults for a given target are used
64 * 1 NOR Flash, with 1 single writable partition:
65 * mtdids=nor0=edb7312-nor
66 * mtdparts=mtdparts=edb7312-nor:-
68 * 1 NOR Flash with 2 partitions, 1 NAND with one
69 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
70 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
77 #include <jffs2/load_kernel.h>
78 #include <linux/list.h>
79 #include <linux/ctype.h>
80 #include <linux/err.h>
81 #include <linux/mtd/mtd.h>
83 #if defined(CONFIG_CMD_NAND)
84 #include <linux/mtd/nand.h>
88 #if defined(CONFIG_CMD_ONENAND)
89 #include <linux/mtd/onenand.h>
90 #include <onenand_uboot.h>
93 DECLARE_GLOBAL_DATA_PTR
;
95 /* special size referring to all the remaining space in a partition */
96 #define SIZE_REMAINING (~0llu)
98 /* special offset value, it is used when not provided by user
100 * this value is used temporarily during parsing, later such offests
101 * are recalculated */
102 #define OFFSET_NOT_SPECIFIED (~0llu)
104 /* minimum partition size */
105 #define MIN_PART_SIZE 4096
107 /* this flag needs to be set in part_info struct mask_flags
108 * field for read-only partitions */
109 #define MTD_WRITEABLE_CMD 1
111 /* default values for mtdids and mtdparts variables */
112 #if !defined(MTDIDS_DEFAULT)
113 #define MTDIDS_DEFAULT NULL
115 #if !defined(MTDPARTS_DEFAULT)
116 #define MTDPARTS_DEFAULT NULL
118 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
119 extern void board_mtdparts_default(const char **mtdids
, const char **mtdparts
);
121 static const char *mtdids_default
= MTDIDS_DEFAULT
;
122 static const char *mtdparts_default
= MTDPARTS_DEFAULT
;
124 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
125 #define MTDIDS_MAXLEN 128
126 #define MTDPARTS_MAXLEN 512
127 #define PARTITION_MAXLEN 16
128 static char last_ids
[MTDIDS_MAXLEN
];
129 static char last_parts
[MTDPARTS_MAXLEN
];
130 static char last_partition
[PARTITION_MAXLEN
];
132 /* low level jffs2 cache cleaning routine */
133 extern void jffs2_free_cache(struct part_info
*part
);
135 /* mtdids mapping list, filled by parse_ids() */
136 static struct list_head mtdids
;
138 /* device/partition list, parse_cmdline() parses into here */
139 static struct list_head devices
;
141 /* current active device and partition number */
142 struct mtd_device
*current_mtd_dev
= NULL
;
143 u8 current_mtd_partnum
= 0;
147 static struct part_info
* mtd_part_info(struct mtd_device
*dev
, unsigned int part_num
);
149 /* command line only routines */
150 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
);
151 static int device_del(struct mtd_device
*dev
);
154 * Parses a string into a number. The number stored at ptr is
155 * potentially suffixed with K (for kilobytes, or 1024 bytes),
156 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
157 * 1073741824). If the number is suffixed with K, M, or G, then
158 * the return value is the number multiplied by one kilobyte, one
159 * megabyte, or one gigabyte, respectively.
161 * @param ptr where parse begins
162 * @param retptr output pointer to next char after parse completes (output)
163 * @return resulting unsigned int
165 static u64
memsize_parse (const char *const ptr
, const char **retptr
)
167 u64 ret
= simple_strtoull(ptr
, (char **)retptr
, 0);
188 * Format string describing supplied size. This routine does the opposite job
189 * to memsize_parse(). Size in bytes is converted to string and if possible
190 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
192 * Note, that this routine does not check for buffer overflow, it's the caller
193 * who must assure enough space.
195 * @param buf output buffer
196 * @param size size to be converted to string
198 static void memsize_format(char *buf
, u64 size
)
200 #define SIZE_GB ((u32)1024*1024*1024)
201 #define SIZE_MB ((u32)1024*1024)
202 #define SIZE_KB ((u32)1024)
204 if ((size
% SIZE_GB
) == 0)
205 sprintf(buf
, "%llug", size
/SIZE_GB
);
206 else if ((size
% SIZE_MB
) == 0)
207 sprintf(buf
, "%llum", size
/SIZE_MB
);
208 else if (size
% SIZE_KB
== 0)
209 sprintf(buf
, "%lluk", size
/SIZE_KB
);
211 sprintf(buf
, "%llu", size
);
215 * This routine does global indexing of all partitions. Resulting index for
216 * current partition is saved in 'mtddevnum'. Current partition name in
219 static void index_partitions(void)
222 struct part_info
*part
;
223 struct list_head
*dentry
;
224 struct mtd_device
*dev
;
226 debug("--- index partitions ---\n");
228 if (current_mtd_dev
) {
230 list_for_each(dentry
, &devices
) {
231 dev
= list_entry(dentry
, struct mtd_device
, link
);
232 if (dev
== current_mtd_dev
) {
233 mtddevnum
+= current_mtd_partnum
;
234 setenv_ulong("mtddevnum", mtddevnum
);
237 mtddevnum
+= dev
->num_parts
;
240 part
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
241 setenv("mtddevname", part
->name
);
243 debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum
, part
->name
);
245 setenv("mtddevnum", NULL
);
246 setenv("mtddevname", NULL
);
248 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
253 * Save current device and partition in environment variable 'partition'.
255 static void current_save(void)
259 debug("--- current_save ---\n");
261 if (current_mtd_dev
) {
262 sprintf(buf
, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev
->id
->type
),
263 current_mtd_dev
->id
->num
, current_mtd_partnum
);
265 setenv("partition", buf
);
266 strncpy(last_partition
, buf
, 16);
268 debug("=> partition %s\n", buf
);
270 setenv("partition", NULL
);
271 last_partition
[0] = '\0';
273 debug("=> partition NULL\n");
280 * Produce a mtd_info given a type and num.
282 * @param type mtd type
283 * @param num mtd number
284 * @param mtd a pointer to an mtd_info instance (output)
285 * @return 0 if device is valid, 1 otherwise
287 static int get_mtd_info(u8 type
, u8 num
, struct mtd_info
**mtd
)
291 sprintf(mtd_dev
, "%s%d", MTD_DEV_TYPE(type
), num
);
292 *mtd
= get_mtd_device_nm(mtd_dev
);
294 printf("Device %s not found!\n", mtd_dev
);
297 put_mtd_device(*mtd
);
303 * Performs sanity check for supplied flash partition.
304 * Table of existing MTD flash devices is searched and partition device
305 * is located. Alignment with the granularity of nand erasesize is verified.
307 * @param id of the parent device
308 * @param part partition to validate
309 * @return 0 if partition is valid, 1 otherwise
311 static int part_validate_eraseblock(struct mtdids
*id
, struct part_info
*part
)
313 struct mtd_info
*mtd
= NULL
;
318 if (get_mtd_info(id
->type
, id
->num
, &mtd
))
321 part
->sector_size
= mtd
->erasesize
;
323 if (!mtd
->numeraseregions
) {
325 * Only one eraseregion (NAND, OneNAND or uniform NOR),
326 * checking for alignment is easy here
328 offset
= part
->offset
;
329 if (do_div(offset
, mtd
->erasesize
)) {
330 printf("%s%d: partition (%s) start offset"
331 "alignment incorrect\n",
332 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
337 if (do_div(size
, mtd
->erasesize
)) {
338 printf("%s%d: partition (%s) size alignment incorrect\n",
339 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
344 * Multiple eraseregions (non-uniform NOR),
345 * checking for alignment is more complex here
348 /* Check start alignment */
349 for (i
= 0; i
< mtd
->numeraseregions
; i
++) {
350 start
= mtd
->eraseregions
[i
].offset
;
351 for (j
= 0; j
< mtd
->eraseregions
[i
].numblocks
; j
++) {
352 if (part
->offset
== start
)
354 start
+= mtd
->eraseregions
[i
].erasesize
;
358 printf("%s%d: partition (%s) start offset alignment incorrect\n",
359 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
364 /* Check end/size alignment */
365 for (i
= 0; i
< mtd
->numeraseregions
; i
++) {
366 start
= mtd
->eraseregions
[i
].offset
;
367 for (j
= 0; j
< mtd
->eraseregions
[i
].numblocks
; j
++) {
368 if ((part
->offset
+ part
->size
) == start
)
370 start
+= mtd
->eraseregions
[i
].erasesize
;
373 /* Check last sector alignment */
374 if ((part
->offset
+ part
->size
) == start
)
377 printf("%s%d: partition (%s) size alignment incorrect\n",
378 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
390 * Performs sanity check for supplied partition. Offset and size are
391 * verified to be within valid range. Partition type is checked and
392 * part_validate_eraseblock() is called with the argument of part.
394 * @param id of the parent device
395 * @param part partition to validate
396 * @return 0 if partition is valid, 1 otherwise
398 static int part_validate(struct mtdids
*id
, struct part_info
*part
)
400 if (part
->size
== SIZE_REMAINING
)
401 part
->size
= id
->size
- part
->offset
;
403 if (part
->offset
> id
->size
) {
404 printf("%s: offset %08llx beyond flash size %08llx\n",
405 id
->mtd_id
, part
->offset
, id
->size
);
409 if ((part
->offset
+ part
->size
) <= part
->offset
) {
410 printf("%s%d: partition (%s) size too big\n",
411 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
415 if (part
->offset
+ part
->size
> id
->size
) {
416 printf("%s: partitioning exceeds flash size\n", id
->mtd_id
);
421 * Now we need to check if the partition starts and ends on
422 * sector (eraseblock) regions
424 return part_validate_eraseblock(id
, part
);
428 * Delete selected partition from the partition list of the specified device.
430 * @param dev device to delete partition from
431 * @param part partition to delete
432 * @return 0 on success, 1 otherwise
434 static int part_del(struct mtd_device
*dev
, struct part_info
*part
)
436 u8 current_save_needed
= 0;
438 /* if there is only one partition, remove whole device */
439 if (dev
->num_parts
== 1)
440 return device_del(dev
);
442 /* otherwise just delete this partition */
444 if (dev
== current_mtd_dev
) {
445 /* we are modyfing partitions for the current device,
447 struct part_info
*curr_pi
;
448 curr_pi
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
451 if (curr_pi
== part
) {
452 printf("current partition deleted, resetting current to 0\n");
453 current_mtd_partnum
= 0;
454 } else if (part
->offset
<= curr_pi
->offset
) {
455 current_mtd_partnum
--;
457 current_save_needed
= 1;
461 list_del(&part
->link
);
465 if (current_save_needed
> 0)
474 * Delete all partitions from parts head list, free memory.
476 * @param head list of partitions to delete
478 static void part_delall(struct list_head
*head
)
480 struct list_head
*entry
, *n
;
481 struct part_info
*part_tmp
;
483 /* clean tmp_list and free allocated memory */
484 list_for_each_safe(entry
, n
, head
) {
485 part_tmp
= list_entry(entry
, struct part_info
, link
);
493 * Add new partition to the supplied partition list. Make sure partitions are
494 * sorted by offset in ascending order.
496 * @param head list this partition is to be added to
497 * @param new partition to be added
499 static int part_sort_add(struct mtd_device
*dev
, struct part_info
*part
)
501 struct list_head
*entry
;
502 struct part_info
*new_pi
, *curr_pi
;
504 /* link partition to parrent dev */
507 if (list_empty(&dev
->parts
)) {
508 debug("part_sort_add: list empty\n");
509 list_add(&part
->link
, &dev
->parts
);
515 new_pi
= list_entry(&part
->link
, struct part_info
, link
);
517 /* get current partition info if we are updating current device */
519 if (dev
== current_mtd_dev
)
520 curr_pi
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
522 list_for_each(entry
, &dev
->parts
) {
523 struct part_info
*pi
;
525 pi
= list_entry(entry
, struct part_info
, link
);
527 /* be compliant with kernel cmdline, allow only one partition at offset zero */
528 if ((new_pi
->offset
== pi
->offset
) && (pi
->offset
== 0)) {
529 printf("cannot add second partition at offset 0\n");
533 if (new_pi
->offset
<= pi
->offset
) {
534 list_add_tail(&part
->link
, entry
);
537 if (curr_pi
&& (pi
->offset
<= curr_pi
->offset
)) {
538 /* we are modyfing partitions for the current
539 * device, update current */
540 current_mtd_partnum
++;
549 list_add_tail(&part
->link
, &dev
->parts
);
556 * Add provided partition to the partition list of a given device.
558 * @param dev device to which partition is added
559 * @param part partition to be added
560 * @return 0 on success, 1 otherwise
562 static int part_add(struct mtd_device
*dev
, struct part_info
*part
)
564 /* verify alignment and size */
565 if (part_validate(dev
->id
, part
) != 0)
568 /* partition is ok, add it to the list */
569 if (part_sort_add(dev
, part
) != 0)
576 * Parse one partition definition, allocate memory and return pointer to this
577 * location in retpart.
579 * @param partdef pointer to the partition definition string i.e. <part-def>
580 * @param ret output pointer to next char after parse completes (output)
581 * @param retpart pointer to the allocated partition (output)
582 * @return 0 on success, 1 otherwise
584 static int part_parse(const char *const partdef
, const char **ret
, struct part_info
**retpart
)
586 struct part_info
*part
;
591 unsigned int mask_flags
;
598 /* fetch the partition size */
600 /* assign all remaining space to this partition */
601 debug("'-': remaining size assigned\n");
602 size
= SIZE_REMAINING
;
605 size
= memsize_parse(p
, &p
);
606 if (size
< MIN_PART_SIZE
) {
607 printf("partition size too small (%llx)\n", size
);
612 /* check for offset */
613 offset
= OFFSET_NOT_SPECIFIED
;
616 offset
= memsize_parse(p
, &p
);
619 /* now look for the name */
622 if ((p
= strchr(name
, ')')) == NULL
) {
623 printf("no closing ) found in partition name\n");
626 name_len
= p
- name
+ 1;
627 if ((name_len
- 1) == 0) {
628 printf("empty partition name\n");
633 /* 0x00000000@0x00000000 */
638 /* test for options */
640 if (strncmp(p
, "ro", 2) == 0) {
641 mask_flags
|= MTD_WRITEABLE_CMD
;
645 /* check for next partition definition */
647 if (size
== SIZE_REMAINING
) {
649 printf("no partitions allowed after a fill-up partition\n");
653 } else if ((*p
== ';') || (*p
== '\0')) {
656 printf("unexpected character '%c' at the end of partition\n", *p
);
661 /* allocate memory */
662 part
= (struct part_info
*)malloc(sizeof(struct part_info
) + name_len
);
664 printf("out of memory\n");
667 memset(part
, 0, sizeof(struct part_info
) + name_len
);
669 part
->offset
= offset
;
670 part
->mask_flags
= mask_flags
;
671 part
->name
= (char *)(part
+ 1);
674 /* copy user provided name */
675 strncpy(part
->name
, name
, name_len
- 1);
678 /* auto generated name in form of size@offset */
679 sprintf(part
->name
, "0x%08llx@0x%08llx", size
, offset
);
683 part
->name
[name_len
- 1] = '\0';
684 INIT_LIST_HEAD(&part
->link
);
686 debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d\n",
687 part
->name
, part
->size
,
688 part
->offset
, part
->mask_flags
);
695 * Check device number to be within valid range for given device type.
697 * @param type mtd type
698 * @param num mtd number
699 * @param size a pointer to the size of the mtd device (output)
700 * @return 0 if device is valid, 1 otherwise
702 static int mtd_device_validate(u8 type
, u8 num
, u64
*size
)
704 struct mtd_info
*mtd
= NULL
;
706 if (get_mtd_info(type
, num
, &mtd
))
715 * Delete all mtd devices from a supplied devices list, free memory allocated for
716 * each device and delete all device partitions.
718 * @return 0 on success, 1 otherwise
720 static int device_delall(struct list_head
*head
)
722 struct list_head
*entry
, *n
;
723 struct mtd_device
*dev_tmp
;
725 /* clean devices list */
726 list_for_each_safe(entry
, n
, head
) {
727 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
729 part_delall(&dev_tmp
->parts
);
732 INIT_LIST_HEAD(&devices
);
738 * If provided device exists it's partitions are deleted, device is removed
739 * from device list and device memory is freed.
741 * @param dev device to be deleted
742 * @return 0 on success, 1 otherwise
744 static int device_del(struct mtd_device
*dev
)
746 part_delall(&dev
->parts
);
747 list_del(&dev
->link
);
750 if (dev
== current_mtd_dev
) {
751 /* we just deleted current device */
752 if (list_empty(&devices
)) {
753 current_mtd_dev
= NULL
;
755 /* reset first partition from first dev from the
756 * devices list as current */
757 current_mtd_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
758 current_mtd_partnum
= 0;
769 * Search global device list and return pointer to the device of type and num
772 * @param type device type
773 * @param num device number
774 * @return NULL if requested device does not exist
776 struct mtd_device
*device_find(u8 type
, u8 num
)
778 struct list_head
*entry
;
779 struct mtd_device
*dev_tmp
;
781 list_for_each(entry
, &devices
) {
782 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
784 if ((dev_tmp
->id
->type
== type
) && (dev_tmp
->id
->num
== num
))
792 * Add specified device to the global device list.
794 * @param dev device to be added
796 static void device_add(struct mtd_device
*dev
)
798 u8 current_save_needed
= 0;
800 if (list_empty(&devices
)) {
801 current_mtd_dev
= dev
;
802 current_mtd_partnum
= 0;
803 current_save_needed
= 1;
806 list_add_tail(&dev
->link
, &devices
);
808 if (current_save_needed
> 0)
815 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
816 * return pointer to the device structure.
818 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
819 * @param ret output pointer to next char after parse completes (output)
820 * @param retdev pointer to the allocated device (output)
821 * @return 0 on success, 1 otherwise
823 static int device_parse(const char *const mtd_dev
, const char **ret
, struct mtd_device
**retdev
)
825 struct mtd_device
*dev
;
826 struct part_info
*part
;
829 unsigned int mtd_id_len
;
833 struct list_head
*entry
, *n
;
838 debug("===device_parse===\n");
847 mtd_id
= p
= mtd_dev
;
848 if (!(p
= strchr(mtd_id
, ':'))) {
849 printf("no <mtd-id> identifier\n");
852 mtd_id_len
= p
- mtd_id
+ 1;
855 /* verify if we have a valid device specified */
856 if ((id
= id_find_by_mtd_id(mtd_id
, mtd_id_len
- 1)) == NULL
) {
857 printf("invalid mtd device '%.*s'\n", mtd_id_len
- 1, mtd_id
);
862 pend
= strchr(p
, ';');
864 debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
865 id
->type
, MTD_DEV_TYPE(id
->type
),
866 id
->num
, id
->mtd_id
);
867 debug("parsing partitions %.*s\n", (int)(pend
? pend
- p
: strlen(p
)), p
);
870 /* parse partitions */
874 if ((dev
= device_find(id
->type
, id
->num
)) != NULL
) {
875 /* if device already exists start at the end of the last partition */
876 part
= list_entry(dev
->parts
.prev
, struct part_info
, link
);
877 offset
= part
->offset
+ part
->size
;
880 while (p
&& (*p
!= '\0') && (*p
!= ';')) {
882 if ((part_parse(p
, &p
, &part
) != 0) || (!part
))
885 /* calculate offset when not specified */
886 if (part
->offset
== OFFSET_NOT_SPECIFIED
)
887 part
->offset
= offset
;
889 offset
= part
->offset
;
891 /* verify alignment and size */
892 if (part_validate(id
, part
) != 0)
895 offset
+= part
->size
;
897 /* partition is ok, add it to the list */
898 list_add_tail(&part
->link
, &tmp_list
);
903 part_delall(&tmp_list
);
907 if (num_parts
== 0) {
908 printf("no partitions for device %s%d (%s)\n",
909 MTD_DEV_TYPE(id
->type
), id
->num
, id
->mtd_id
);
913 debug("\ntotal partitions: %d\n", num_parts
);
915 /* check for next device presence */
920 } else if (*p
== '\0') {
924 printf("unexpected character '%c' at the end of device\n", *p
);
931 /* allocate memory for mtd_device structure */
932 if ((dev
= (struct mtd_device
*)malloc(sizeof(struct mtd_device
))) == NULL
) {
933 printf("out of memory\n");
936 memset(dev
, 0, sizeof(struct mtd_device
));
938 dev
->num_parts
= 0; /* part_sort_add increments num_parts */
939 INIT_LIST_HEAD(&dev
->parts
);
940 INIT_LIST_HEAD(&dev
->link
);
942 /* move partitions from tmp_list to dev->parts */
943 list_for_each_safe(entry
, n
, &tmp_list
) {
944 part
= list_entry(entry
, struct part_info
, link
);
946 if (part_sort_add(dev
, part
) != 0) {
959 * Initialize global device list.
961 * @return 0 on success, 1 otherwise
963 static int mtd_devices_init(void)
965 last_parts
[0] = '\0';
966 current_mtd_dev
= NULL
;
969 return device_delall(&devices
);
973 * Search global mtdids list and find id of requested type and number.
975 * @return pointer to the id if it exists, NULL otherwise
977 static struct mtdids
* id_find(u8 type
, u8 num
)
979 struct list_head
*entry
;
982 list_for_each(entry
, &mtdids
) {
983 id
= list_entry(entry
, struct mtdids
, link
);
985 if ((id
->type
== type
) && (id
->num
== num
))
993 * Search global mtdids list and find id of a requested mtd_id.
995 * Note: first argument is not null terminated.
997 * @param mtd_id string containing requested mtd_id
998 * @param mtd_id_len length of supplied mtd_id
999 * @return pointer to the id if it exists, NULL otherwise
1001 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
)
1003 struct list_head
*entry
;
1006 debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1007 mtd_id_len
, mtd_id
, mtd_id_len
);
1009 list_for_each(entry
, &mtdids
) {
1010 id
= list_entry(entry
, struct mtdids
, link
);
1012 debug("entry: '%s' (len = %zu)\n",
1013 id
->mtd_id
, strlen(id
->mtd_id
));
1015 if (mtd_id_len
!= strlen(id
->mtd_id
))
1017 if (strncmp(id
->mtd_id
, mtd_id
, mtd_id_len
) == 0)
1025 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1026 * return device type and number.
1028 * @param id string describing device id
1029 * @param ret_id output pointer to next char after parse completes (output)
1030 * @param dev_type parsed device type (output)
1031 * @param dev_num parsed device number (output)
1032 * @return 0 on success, 1 otherwise
1034 int mtd_id_parse(const char *id
, const char **ret_id
, u8
*dev_type
,
1040 if (strncmp(p
, "nand", 4) == 0) {
1041 *dev_type
= MTD_DEV_TYPE_NAND
;
1043 } else if (strncmp(p
, "nor", 3) == 0) {
1044 *dev_type
= MTD_DEV_TYPE_NOR
;
1046 } else if (strncmp(p
, "onenand", 7) == 0) {
1047 *dev_type
= MTD_DEV_TYPE_ONENAND
;
1050 printf("incorrect device type in %s\n", id
);
1055 printf("incorrect device number in %s\n", id
);
1059 *dev_num
= simple_strtoul(p
, (char **)&p
, 0);
1066 * Process all devices and generate corresponding mtdparts string describing
1067 * all partitions on all devices.
1069 * @param buf output buffer holding generated mtdparts string (output)
1070 * @param buflen buffer size
1071 * @return 0 on success, 1 otherwise
1073 static int generate_mtdparts(char *buf
, u32 buflen
)
1075 struct list_head
*pentry
, *dentry
;
1076 struct mtd_device
*dev
;
1077 struct part_info
*part
, *prev_part
;
1082 u32 maxlen
= buflen
- 1;
1084 debug("--- generate_mtdparts ---\n");
1086 if (list_empty(&devices
)) {
1091 strcpy(p
, "mtdparts=");
1094 list_for_each(dentry
, &devices
) {
1095 dev
= list_entry(dentry
, struct mtd_device
, link
);
1098 len
= strlen(dev
->id
->mtd_id
) + 1;
1101 memcpy(p
, dev
->id
->mtd_id
, len
- 1);
1106 /* format partitions */
1109 list_for_each(pentry
, &dev
->parts
) {
1110 part
= list_entry(pentry
, struct part_info
, link
);
1112 offset
= part
->offset
;
1115 /* partition size */
1116 memsize_format(tmpbuf
, size
);
1117 len
= strlen(tmpbuf
);
1120 memcpy(p
, tmpbuf
, len
);
1125 /* add offset only when there is a gap between
1127 if ((!prev_part
&& (offset
!= 0)) ||
1128 (prev_part
&& ((prev_part
->offset
+ prev_part
->size
) != part
->offset
))) {
1130 memsize_format(tmpbuf
, offset
);
1131 len
= strlen(tmpbuf
) + 1;
1135 memcpy(p
, tmpbuf
, len
- 1);
1140 /* copy name only if user supplied */
1141 if(!part
->auto_name
) {
1142 len
= strlen(part
->name
) + 2;
1147 memcpy(p
, part
->name
, len
- 2);
1154 if (part
->mask_flags
&& MTD_WRITEABLE_CMD
) {
1163 /* print ',' separator if there are other partitions
1165 if (dev
->num_parts
> part_cnt
) {
1173 /* print ';' separator if there are other devices following */
1174 if (dentry
->next
!= &devices
) {
1182 /* we still have at least one char left, as we decremented maxlen at
1189 last_parts
[0] = '\0';
1194 * Call generate_mtdparts to process all devices and generate corresponding
1195 * mtdparts string, save it in mtdparts environment variable.
1197 * @param buf output buffer holding generated mtdparts string (output)
1198 * @param buflen buffer size
1199 * @return 0 on success, 1 otherwise
1201 static int generate_mtdparts_save(char *buf
, u32 buflen
)
1205 ret
= generate_mtdparts(buf
, buflen
);
1207 if ((buf
[0] != '\0') && (ret
== 0))
1208 setenv("mtdparts", buf
);
1210 setenv("mtdparts", NULL
);
1215 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1217 * Get the net size (w/o bad blocks) of the given partition.
1219 * @param mtd the mtd info
1220 * @param part the partition
1221 * @return the calculated net size of this partition
1223 static uint64_t net_part_size(struct mtd_info
*mtd
, struct part_info
*part
)
1225 uint64_t i
, net_size
= 0;
1227 if (!mtd
->block_isbad
)
1230 for (i
= 0; i
< part
->size
; i
+= mtd
->erasesize
) {
1231 if (!mtd
->block_isbad(mtd
, part
->offset
+ i
))
1232 net_size
+= mtd
->erasesize
;
1239 static void print_partition_table(void)
1241 struct list_head
*dentry
, *pentry
;
1242 struct part_info
*part
;
1243 struct mtd_device
*dev
;
1246 list_for_each(dentry
, &devices
) {
1247 dev
= list_entry(dentry
, struct mtd_device
, link
);
1248 /* list partitions for given device */
1250 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1251 struct mtd_info
*mtd
;
1253 if (get_mtd_info(dev
->id
->type
, dev
->id
->num
, &mtd
))
1256 printf("\ndevice %s%d <%s>, # parts = %d\n",
1257 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1258 dev
->id
->mtd_id
, dev
->num_parts
);
1259 printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
1261 list_for_each(pentry
, &dev
->parts
) {
1265 part
= list_entry(pentry
, struct part_info
, link
);
1266 net_size
= net_part_size(mtd
, part
);
1267 size_note
= part
->size
== net_size
? " " : " (!)";
1268 printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
1269 part_num
, part
->name
, part
->size
,
1270 net_size
, size_note
, part
->offset
,
1272 #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1273 printf("\ndevice %s%d <%s>, # parts = %d\n",
1274 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1275 dev
->id
->mtd_id
, dev
->num_parts
);
1276 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
1278 list_for_each(pentry
, &dev
->parts
) {
1279 part
= list_entry(pentry
, struct part_info
, link
);
1280 printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
1281 part_num
, part
->name
, part
->size
,
1282 part
->offset
, part
->mask_flags
);
1283 #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1288 if (list_empty(&devices
))
1289 printf("no partitions defined\n");
1293 * Format and print out a partition list for each device from global device
1296 static void list_partitions(void)
1298 struct part_info
*part
;
1300 debug("\n---list_partitions---\n");
1301 print_partition_table();
1303 /* current_mtd_dev is not NULL only when we have non empty device list */
1304 if (current_mtd_dev
) {
1305 part
= mtd_part_info(current_mtd_dev
, current_mtd_partnum
);
1307 printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n",
1308 MTD_DEV_TYPE(current_mtd_dev
->id
->type
),
1309 current_mtd_dev
->id
->num
, current_mtd_partnum
,
1310 part
->name
, part
->size
, part
->offset
);
1312 printf("could not get current partition info\n\n");
1316 printf("\ndefaults:\n");
1317 printf("mtdids : %s\n",
1318 mtdids_default
? mtdids_default
: "none");
1320 * Using printf() here results in printbuffer overflow
1321 * if default mtdparts string is greater than console
1322 * printbuffer. Use puts() to prevent system crashes.
1325 puts(mtdparts_default
? mtdparts_default
: "none");
1330 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1331 * corresponding device and verify partition number.
1333 * @param id string describing device and partition or partition name
1334 * @param dev pointer to the requested device (output)
1335 * @param part_num verified partition number (output)
1336 * @param part pointer to requested partition (output)
1337 * @return 0 on success, 1 otherwise
1339 int find_dev_and_part(const char *id
, struct mtd_device
**dev
,
1340 u8
*part_num
, struct part_info
**part
)
1342 struct list_head
*dentry
, *pentry
;
1343 u8 type
, dnum
, pnum
;
1346 debug("--- find_dev_and_part ---\nid = %s\n", id
);
1348 list_for_each(dentry
, &devices
) {
1350 *dev
= list_entry(dentry
, struct mtd_device
, link
);
1351 list_for_each(pentry
, &(*dev
)->parts
) {
1352 *part
= list_entry(pentry
, struct part_info
, link
);
1353 if (strcmp((*part
)->name
, id
) == 0)
1364 if (mtd_id_parse(p
, &p
, &type
, &dnum
) != 0)
1367 if ((*p
++ != ',') || (*p
== '\0')) {
1368 printf("no partition number specified\n");
1371 pnum
= simple_strtoul(p
, (char **)&p
, 0);
1373 printf("unexpected trailing character '%c'\n", *p
);
1377 if ((*dev
= device_find(type
, dnum
)) == NULL
) {
1378 printf("no such device %s%d\n", MTD_DEV_TYPE(type
), dnum
);
1382 if ((*part
= mtd_part_info(*dev
, pnum
)) == NULL
) {
1383 printf("no such partition\n");
1394 * Find and delete partition. For partition id format see find_dev_and_part().
1396 * @param id string describing device and partition
1397 * @return 0 on success, 1 otherwise
1399 static int delete_partition(const char *id
)
1402 struct mtd_device
*dev
;
1403 struct part_info
*part
;
1405 if (find_dev_and_part(id
, &dev
, &pnum
, &part
) == 0) {
1407 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n",
1408 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
,
1409 part
->name
, part
->size
, part
->offset
);
1411 if (part_del(dev
, part
) != 0)
1414 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
1415 printf("generated mtdparts too long, resetting to null\n");
1421 printf("partition %s not found\n", id
);
1425 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1427 * Increase the size of the given partition so that it's net size is at least
1428 * as large as the size member and such that the next partition would start on a
1429 * good block if it were adjacent to this partition.
1431 * @param mtd the mtd device
1432 * @param part the partition
1433 * @param next_offset pointer to the offset of the next partition after this
1434 * partition's size has been modified (output)
1436 static void spread_partition(struct mtd_info
*mtd
, struct part_info
*part
,
1437 uint64_t *next_offset
)
1439 uint64_t net_size
, padding_size
= 0;
1442 mtd_get_len_incl_bad(mtd
, part
->offset
, part
->size
, &net_size
,
1446 * Absorb bad blocks immediately following this
1447 * partition also into the partition, such that
1448 * the next partition starts with a good block.
1451 mtd_get_len_incl_bad(mtd
, part
->offset
+ net_size
,
1452 mtd
->erasesize
, &padding_size
, &truncated
);
1456 padding_size
-= mtd
->erasesize
;
1460 printf("truncated partition %s to %lld bytes\n", part
->name
,
1461 (uint64_t) net_size
+ padding_size
);
1464 part
->size
= net_size
+ padding_size
;
1465 *next_offset
= part
->offset
+ part
->size
;
1469 * Adjust all of the partition sizes, such that all partitions are at least
1470 * as big as their mtdparts environment variable sizes and they each start
1473 * @return 0 on success, 1 otherwise
1475 static int spread_partitions(void)
1477 struct list_head
*dentry
, *pentry
;
1478 struct mtd_device
*dev
;
1479 struct part_info
*part
;
1480 struct mtd_info
*mtd
;
1484 list_for_each(dentry
, &devices
) {
1485 dev
= list_entry(dentry
, struct mtd_device
, link
);
1487 if (get_mtd_info(dev
->id
->type
, dev
->id
->num
, &mtd
))
1492 list_for_each(pentry
, &dev
->parts
) {
1493 part
= list_entry(pentry
, struct part_info
, link
);
1495 debug("spread_partitions: device = %s%d, partition %d ="
1496 " (%s) 0x%08llx@0x%08llx\n",
1497 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1498 part_num
, part
->name
, part
->size
,
1501 if (cur_offs
> part
->offset
)
1502 part
->offset
= cur_offs
;
1504 spread_partition(mtd
, part
, &cur_offs
);
1512 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
1513 printf("generated mtdparts too long, resetting to null\n");
1518 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1521 * The mtdparts variable tends to be long. If we need to access it
1522 * before the env is relocated, then we need to use our own stack
1523 * buffer. gd->env_buf will be too small.
1525 * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
1526 * @return mtdparts variable string, NULL if not found
1528 static const char *getenv_mtdparts(char *buf
)
1530 if (gd
->flags
& GD_FLG_ENV_READY
)
1531 return getenv("mtdparts");
1532 if (getenv_f("mtdparts", buf
, MTDPARTS_MAXLEN
) != -1)
1538 * Accept character string describing mtd partitions and call device_parse()
1539 * for each entry. Add created devices to the global devices list.
1541 * @param mtdparts string specifing mtd partitions
1542 * @return 0 on success, 1 otherwise
1544 static int parse_mtdparts(const char *const mtdparts
)
1547 struct mtd_device
*dev
;
1549 char tmp_parts
[MTDPARTS_MAXLEN
];
1551 debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p
);
1553 /* delete all devices and partitions */
1554 if (mtd_devices_init() != 0) {
1555 printf("could not initialise device list\n");
1559 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1560 p
= getenv_mtdparts(tmp_parts
);
1564 if (strncmp(p
, "mtdparts=", 9) != 0) {
1565 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1570 while (*p
!= '\0') {
1572 if ((device_parse(p
, &p
, &dev
) != 0) || (!dev
))
1575 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
1576 dev
->id
->num
, dev
->id
->mtd_id
);
1578 /* check if parsed device is already on the list */
1579 if (device_find(dev
->id
->type
, dev
->id
->num
) != NULL
) {
1580 printf("device %s%d redefined, please correct mtdparts variable\n",
1581 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
1585 list_add_tail(&dev
->link
, &devices
);
1589 device_delall(&devices
);
1595 * Parse provided string describing mtdids mapping (see file header for mtdids
1596 * variable format). Allocate memory for each entry and add all found entries
1597 * to the global mtdids list.
1599 * @param ids mapping string
1600 * @return 0 on success, 1 otherwise
1602 static int parse_mtdids(const char *const ids
)
1604 const char *p
= ids
;
1608 struct list_head
*entry
, *n
;
1609 struct mtdids
*id_tmp
;
1614 debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids
);
1616 /* clean global mtdids list */
1617 list_for_each_safe(entry
, n
, &mtdids
) {
1618 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1619 debug("mtdids del: %d %d\n", id_tmp
->type
, id_tmp
->num
);
1624 INIT_LIST_HEAD(&mtdids
);
1626 while(p
&& (*p
!= '\0')) {
1629 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1630 if (mtd_id_parse(p
, &p
, &type
, &num
) != 0)
1634 printf("mtdids: incorrect <dev-num>\n");
1639 /* check if requested device exists */
1640 if (mtd_device_validate(type
, num
, &size
) != 0)
1643 /* locate <mtd-id> */
1645 if ((p
= strchr(mtd_id
, ',')) != NULL
) {
1646 mtd_id_len
= p
- mtd_id
+ 1;
1649 mtd_id_len
= strlen(mtd_id
) + 1;
1651 if (mtd_id_len
== 0) {
1652 printf("mtdids: no <mtd-id> identifier\n");
1656 /* check if this id is already on the list */
1657 int double_entry
= 0;
1658 list_for_each(entry
, &mtdids
) {
1659 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1660 if ((id_tmp
->type
== type
) && (id_tmp
->num
== num
)) {
1666 printf("device id %s%d redefined, please correct mtdids variable\n",
1667 MTD_DEV_TYPE(type
), num
);
1671 /* allocate mtdids structure */
1672 if (!(id
= (struct mtdids
*)malloc(sizeof(struct mtdids
) + mtd_id_len
))) {
1673 printf("out of memory\n");
1676 memset(id
, 0, sizeof(struct mtdids
) + mtd_id_len
);
1680 id
->mtd_id
= (char *)(id
+ 1);
1681 strncpy(id
->mtd_id
, mtd_id
, mtd_id_len
- 1);
1682 id
->mtd_id
[mtd_id_len
- 1] = '\0';
1683 INIT_LIST_HEAD(&id
->link
);
1685 debug("+ id %s%d\t%16lld bytes\t%s\n",
1686 MTD_DEV_TYPE(id
->type
), id
->num
,
1687 id
->size
, id
->mtd_id
);
1689 list_add_tail(&id
->link
, &mtdids
);
1693 /* clean mtdids list and free allocated memory */
1694 list_for_each_safe(entry
, n
, &mtdids
) {
1695 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1707 * Parse and initialize global mtdids mapping and create global
1708 * device/partition list.
1710 * @return 0 on success, 1 otherwise
1712 int mtdparts_init(void)
1714 static int initialized
= 0;
1715 const char *ids
, *parts
;
1716 const char *current_partition
;
1718 char tmp_ep
[PARTITION_MAXLEN
];
1719 char tmp_parts
[MTDPARTS_MAXLEN
];
1721 debug("\n---mtdparts_init---\n");
1723 INIT_LIST_HEAD(&mtdids
);
1724 INIT_LIST_HEAD(&devices
);
1725 memset(last_ids
, 0, MTDIDS_MAXLEN
);
1726 memset(last_parts
, 0, MTDPARTS_MAXLEN
);
1727 memset(last_partition
, 0, PARTITION_MAXLEN
);
1728 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
1729 board_mtdparts_default(&mtdids_default
, &mtdparts_default
);
1736 ids
= getenv("mtdids");
1737 parts
= getenv_mtdparts(tmp_parts
);
1738 current_partition
= getenv("partition");
1740 /* save it for later parsing, cannot rely on current partition pointer
1741 * as 'partition' variable may be updated during init */
1743 if (current_partition
)
1744 strncpy(tmp_ep
, current_partition
, PARTITION_MAXLEN
);
1746 debug("last_ids : %s\n", last_ids
);
1747 debug("env_ids : %s\n", ids
);
1748 debug("last_parts: %s\n", last_parts
);
1749 debug("env_parts : %s\n\n", parts
);
1751 debug("last_partition : %s\n", last_partition
);
1752 debug("env_partition : %s\n", current_partition
);
1754 /* if mtdids variable is empty try to use defaults */
1756 if (mtdids_default
) {
1757 debug("mtdids variable not defined, using default\n");
1758 ids
= mtdids_default
;
1759 setenv("mtdids", (char *)ids
);
1761 printf("mtdids not defined, no default present\n");
1765 if (strlen(ids
) > MTDIDS_MAXLEN
- 1) {
1766 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN
);
1770 /* use defaults when mtdparts variable is not defined
1771 * once mtdparts is saved environment, drop use_defaults flag */
1773 if (mtdparts_default
&& use_defaults
) {
1774 parts
= mtdparts_default
;
1775 if (setenv("mtdparts", (char *)parts
) == 0)
1778 printf("mtdparts variable not set, see 'help mtdparts'\n");
1781 if (parts
&& (strlen(parts
) > MTDPARTS_MAXLEN
- 1)) {
1782 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN
);
1786 /* check if we have already parsed those mtdids */
1787 if ((last_ids
[0] != '\0') && (strcmp(last_ids
, ids
) == 0)) {
1792 if (parse_mtdids(ids
) != 0) {
1797 /* ok it's good, save new ids */
1798 strncpy(last_ids
, ids
, MTDIDS_MAXLEN
);
1801 /* parse partitions if either mtdparts or mtdids were updated */
1802 if (parts
&& ((last_parts
[0] == '\0') || ((strcmp(last_parts
, parts
) != 0)) || ids_changed
)) {
1803 if (parse_mtdparts(parts
) != 0)
1806 if (list_empty(&devices
)) {
1807 printf("mtdparts_init: no valid partitions\n");
1811 /* ok it's good, save new parts */
1812 strncpy(last_parts
, parts
, MTDPARTS_MAXLEN
);
1814 /* reset first partition from first dev from the list as current */
1815 current_mtd_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
1816 current_mtd_partnum
= 0;
1819 debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
1820 MTD_DEV_TYPE(current_mtd_dev
->id
->type
),
1821 current_mtd_dev
->id
->num
, current_mtd_partnum
);
1824 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1825 if (!parts
&& (last_parts
[0] != '\0'))
1826 return mtd_devices_init();
1828 /* do not process current partition if mtdparts variable is null */
1832 /* is current partition set in environment? if so, use it */
1833 if ((tmp_ep
[0] != '\0') && (strcmp(tmp_ep
, last_partition
) != 0)) {
1834 struct part_info
*p
;
1835 struct mtd_device
*cdev
;
1838 debug("--- getting current partition: %s\n", tmp_ep
);
1840 if (find_dev_and_part(tmp_ep
, &cdev
, &pnum
, &p
) == 0) {
1841 current_mtd_dev
= cdev
;
1842 current_mtd_partnum
= pnum
;
1845 } else if (getenv("partition") == NULL
) {
1846 debug("no partition variable set, setting...\n");
1854 * Return pointer to the partition of a requested number from a requested
1857 * @param dev device that is to be searched for a partition
1858 * @param part_num requested partition number
1859 * @return pointer to the part_info, NULL otherwise
1861 static struct part_info
* mtd_part_info(struct mtd_device
*dev
, unsigned int part_num
)
1863 struct list_head
*entry
;
1864 struct part_info
*part
;
1870 debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1871 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1872 dev
->id
->num
, dev
->id
->mtd_id
);
1874 if (part_num
>= dev
->num_parts
) {
1875 printf("invalid partition number %d for device %s%d (%s)\n",
1876 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1877 dev
->id
->num
, dev
->id
->mtd_id
);
1881 /* locate partition number, return it */
1883 list_for_each(entry
, &dev
->parts
) {
1884 part
= list_entry(entry
, struct part_info
, link
);
1886 if (part_num
== num
++) {
1894 /***************************************************/
1895 /* U-Boot commands */
1896 /***************************************************/
1897 /* command line only */
1899 * Routine implementing u-boot chpart command. Sets new current partition based
1900 * on the user supplied partition id. For partition id format see find_dev_and_part().
1902 * @param cmdtp command internal data
1903 * @param flag command flag
1904 * @param argc number of arguments supplied to the command
1905 * @param argv arguments list
1906 * @return 0 on success, 1 otherwise
1908 static int do_chpart(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
1910 /* command line only */
1911 struct mtd_device
*dev
;
1912 struct part_info
*part
;
1915 if (mtdparts_init() !=0)
1919 printf("no partition id specified\n");
1923 if (find_dev_and_part(argv
[1], &dev
, &pnum
, &part
) != 0)
1926 current_mtd_dev
= dev
;
1927 current_mtd_partnum
= pnum
;
1930 printf("partition changed to %s%d,%d\n",
1931 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
);
1937 * Routine implementing u-boot mtdparts command. Initialize/update default global
1938 * partition list and process user partition request (list, add, del).
1940 * @param cmdtp command internal data
1941 * @param flag command flag
1942 * @param argc number of arguments supplied to the command
1943 * @param argv arguments list
1944 * @return 0 on success, 1 otherwise
1946 static int do_mtdparts(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
1947 char * const argv
[])
1950 if (strcmp(argv
[1], "default") == 0) {
1951 setenv("mtdids", NULL
);
1952 setenv("mtdparts", NULL
);
1953 setenv("partition", NULL
);
1958 } else if (strcmp(argv
[1], "delall") == 0) {
1959 /* this may be the first run, initialize lists if needed */
1962 setenv("mtdparts", NULL
);
1964 /* mtd_devices_init() calls current_save() */
1965 return mtd_devices_init();
1969 /* make sure we are in sync with env variables */
1970 if (mtdparts_init() != 0)
1978 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
1979 if (((argc
== 5) || (argc
== 6)) && (strncmp(argv
[1], "add", 3) == 0)) {
1980 #define PART_ADD_DESC_MAXLEN 64
1981 char tmpbuf
[PART_ADD_DESC_MAXLEN
];
1982 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1983 struct mtd_info
*mtd
;
1984 uint64_t next_offset
;
1987 struct mtd_device
*dev
;
1988 struct mtd_device
*dev_tmp
;
1990 struct part_info
*p
;
1992 if (mtd_id_parse(argv
[2], NULL
, &type
, &num
) != 0)
1995 if ((id
= id_find(type
, num
)) == NULL
) {
1996 printf("no such device %s defined in mtdids variable\n", argv
[2]);
2000 len
= strlen(id
->mtd_id
) + 1; /* 'mtd_id:' */
2001 len
+= strlen(argv
[3]); /* size@offset */
2002 len
+= strlen(argv
[4]) + 2; /* '(' name ')' */
2003 if (argv
[5] && (strlen(argv
[5]) == 2))
2004 len
+= 2; /* 'ro' */
2006 if (len
>= PART_ADD_DESC_MAXLEN
) {
2007 printf("too long partition description\n");
2010 sprintf(tmpbuf
, "%s:%s(%s)%s",
2011 id
->mtd_id
, argv
[3], argv
[4], argv
[5] ? argv
[5] : "");
2012 debug("add tmpbuf: %s\n", tmpbuf
);
2014 if ((device_parse(tmpbuf
, NULL
, &dev
) != 0) || (!dev
))
2017 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
2018 dev
->id
->num
, dev
->id
->mtd_id
);
2020 p
= list_entry(dev
->parts
.next
, struct part_info
, link
);
2022 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2023 if (get_mtd_info(dev
->id
->type
, dev
->id
->num
, &mtd
))
2026 if (!strcmp(&argv
[1][3], ".spread")) {
2027 spread_partition(mtd
, p
, &next_offset
);
2028 debug("increased %s to %llu bytes\n", p
->name
, p
->size
);
2032 dev_tmp
= device_find(dev
->id
->type
, dev
->id
->num
);
2033 if (dev_tmp
== NULL
) {
2035 } else if (part_add(dev_tmp
, p
) != 0) {
2036 /* merge new partition with existing ones*/
2041 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
2042 printf("generated mtdparts too long, resetting to null\n");
2049 /* mtdparts del part-id */
2050 if ((argc
== 3) && (strcmp(argv
[1], "del") == 0)) {
2051 debug("del: part-id = %s\n", argv
[2]);
2053 return delete_partition(argv
[2]);
2056 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2057 if ((argc
== 2) && (strcmp(argv
[1], "spread") == 0))
2058 return spread_partitions();
2059 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2061 return CMD_RET_USAGE
;
2064 /***************************************************/
2066 chpart
, 2, 0, do_chpart
,
2067 "change active partition",
2069 " - change active partition (e.g. part-id = nand0,1)"
2072 #ifdef CONFIG_SYS_LONGHELP
2073 static char mtdparts_help_text
[] =
2075 " - list partition table\n"
2077 " - delete all partitions\n"
2078 "mtdparts del part-id\n"
2079 " - delete partition (e.g. part-id = nand0,1)\n"
2080 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2081 " - add partition\n"
2082 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2083 "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2084 " - add partition, padding size by skipping bad blocks\n"
2086 "mtdparts default\n"
2087 " - reset partition table to defaults\n"
2088 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2090 " - adjust the sizes of the partitions so they are\n"
2091 " at least as big as the mtdparts variable specifies\n"
2092 " and they each start on a good block\n\n"
2095 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2097 "this command uses three environment variables:\n\n"
2098 "'partition' - keeps current partition identifier\n\n"
2099 "partition := <part-id>\n"
2100 "<part-id> := <dev-id>,part_num\n\n"
2101 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2102 "mtdids=<idmap>[,<idmap>,...]\n\n"
2103 "<idmap> := <dev-id>=<mtd-id>\n"
2104 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
2105 "<dev-num> := mtd device number, 0...\n"
2106 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2107 "'mtdparts' - partition list\n\n"
2108 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2109 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2110 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2111 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2112 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2113 "<offset> := partition start offset within the device\n"
2114 "<name> := '(' NAME ')'\n"
2115 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)";
2119 mtdparts
, 6, 0, do_mtdparts
,
2120 "define flash/nand partitions", mtdparts_help_text
2122 /***************************************************/