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 * See file CREDITS for list of people who contributed to this
27 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License as
29 * published by the Free Software Foundation; either version 2 of
30 * the License, or (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
44 * Three environment variables are used by the parsing routines:
46 * 'partition' - keeps current partition identifier
48 * partition := <part-id>
49 * <part-id> := <dev-id>,part_num
52 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
54 * mtdids=<idmap>[,<idmap>,...]
56 * <idmap> := <dev-id>=<mtd-id>
57 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
58 * <dev-num> := mtd device number, 0...
59 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
62 * 'mtdparts' - partition list
64 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
66 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
67 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
68 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
69 * <size> := standard linux memsize OR '-' to denote all remaining space
70 * <offset> := partition start offset within the device
71 * <name> := '(' NAME ')'
72 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
75 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
76 * - if the above variables are not set defaults for a given target are used
80 * 1 NOR Flash, with 1 single writable partition:
81 * mtdids=nor0=edb7312-nor
82 * mtdparts=mtdparts=edb7312-nor:-
84 * 1 NOR Flash with 2 partitions, 1 NAND with one
85 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
86 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
93 #include <jffs2/load_kernel.h>
94 #include <linux/list.h>
95 #include <linux/ctype.h>
96 #include <linux/err.h>
97 #include <linux/mtd/mtd.h>
99 #if defined(CONFIG_CMD_NAND)
100 #include <linux/mtd/nand.h>
104 #if defined(CONFIG_CMD_ONENAND)
105 #include <linux/mtd/onenand.h>
106 #include <onenand_uboot.h>
109 /* special size referring to all the remaining space in a partition */
110 #define SIZE_REMAINING 0xFFFFFFFF
112 /* special offset value, it is used when not provided by user
114 * this value is used temporarily during parsing, later such offests
115 * are recalculated */
116 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
118 /* minimum partition size */
119 #define MIN_PART_SIZE 4096
121 /* this flag needs to be set in part_info struct mask_flags
122 * field for read-only partitions */
123 #define MTD_WRITEABLE_CMD 1
125 /* default values for mtdids and mtdparts variables */
126 #if defined(MTDIDS_DEFAULT)
127 static const char *const mtdids_default = MTDIDS_DEFAULT;
129 static const char *const mtdids_default = NULL;
132 #if defined(MTDPARTS_DEFAULT)
133 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
135 static const char *const mtdparts_default = NULL;
138 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
139 #define MTDIDS_MAXLEN 128
140 #define MTDPARTS_MAXLEN 512
141 #define PARTITION_MAXLEN 16
142 static char last_ids[MTDIDS_MAXLEN];
143 static char last_parts[MTDPARTS_MAXLEN];
144 static char last_partition[PARTITION_MAXLEN];
146 /* low level jffs2 cache cleaning routine */
147 extern void jffs2_free_cache(struct part_info *part);
149 /* mtdids mapping list, filled by parse_ids() */
150 struct list_head mtdids;
152 /* device/partition list, parse_cmdline() parses into here */
153 struct list_head devices;
155 /* current active device and partition number */
156 struct mtd_device *current_mtd_dev = NULL;
157 u8 current_mtd_partnum = 0;
159 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
161 /* command line only routines */
162 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
163 static int device_del(struct mtd_device *dev);
166 * Parses a string into a number. The number stored at ptr is
167 * potentially suffixed with K (for kilobytes, or 1024 bytes),
168 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
169 * 1073741824). If the number is suffixed with K, M, or G, then
170 * the return value is the number multiplied by one kilobyte, one
171 * megabyte, or one gigabyte, respectively.
173 * @param ptr where parse begins
174 * @param retptr output pointer to next char after parse completes (output)
175 * @return resulting unsigned int
177 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
179 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
199 #ifndef CONFIG_CMD_MTDPARTS_LITE
201 * Format string describing supplied size. This routine does the opposite job
202 * to memsize_parse(). Size in bytes is converted to string and if possible
203 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
205 * Note, that this routine does not check for buffer overflow, it's the caller
206 * who must assure enough space.
208 * @param buf output buffer
209 * @param size size to be converted to string
211 static void memsize_format(char *buf, u32 size)
213 #define SIZE_GB ((u32)1024*1024*1024)
214 #define SIZE_MB ((u32)1024*1024)
215 #define SIZE_KB ((u32)1024)
217 if ((size % SIZE_GB) == 0)
218 sprintf(buf, "%ug", size/SIZE_GB);
219 else if ((size % SIZE_MB) == 0)
220 sprintf(buf, "%um", size/SIZE_MB);
221 else if (size % SIZE_KB == 0)
222 sprintf(buf, "%uk", size/SIZE_KB);
224 sprintf(buf, "%u", size);
229 * This routine does global indexing of all partitions. Resulting index for
230 * current partition is saved in 'mtddevnum'. Current partition name in
233 static void index_partitions(void)
237 struct part_info *part;
238 struct list_head *dentry;
239 struct mtd_device *dev;
241 debug("--- index partitions ---\n");
243 if (current_mtd_dev) {
245 list_for_each(dentry, &devices) {
246 dev = list_entry(dentry, struct mtd_device, link);
247 if (dev == current_mtd_dev) {
248 mtddevnum += current_mtd_partnum;
249 sprintf(buf, "%d", mtddevnum);
250 setenv("mtddevnum", buf);
253 mtddevnum += dev->num_parts;
256 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
257 setenv("mtddevname", part->name);
259 debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
261 setenv("mtddevnum", NULL);
262 setenv("mtddevname", NULL);
264 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
269 * Save current device and partition in environment variable 'partition'.
271 static void current_save(void)
275 debug("--- current_save ---\n");
277 if (current_mtd_dev) {
278 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
279 current_mtd_dev->id->num, current_mtd_partnum);
281 setenv("partition", buf);
282 strncpy(last_partition, buf, 16);
284 debug("=> partition %s\n", buf);
286 setenv("partition", NULL);
287 last_partition[0] = '\0';
289 debug("=> partition NULL\n");
296 * Produce a mtd_info given a type and num.
298 * @param type mtd type
299 * @param num mtd number
300 * @param mtd a pointer to an mtd_info instance (output)
301 * @return 0 if device is valid, 1 otherwise
303 static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
307 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
308 *mtd = get_mtd_device_nm(mtd_dev);
310 printf("Device %s not found!\n", mtd_dev);
318 * Performs sanity check for supplied flash partition.
319 * Table of existing MTD flash devices is searched and partition device
320 * is located. Alignment with the granularity of nand erasesize is verified.
322 * @param id of the parent device
323 * @param part partition to validate
324 * @return 0 if partition is valid, 1 otherwise
326 static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
328 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 if ((unsigned long)part->offset % mtd->erasesize) {
343 printf("%s%d: partition (%s) start offset"
344 "alignment incorrect\n",
345 MTD_DEV_TYPE(id->type), id->num, part->name);
349 if (part->size % mtd->erasesize) {
350 printf("%s%d: partition (%s) size alignment incorrect\n",
351 MTD_DEV_TYPE(id->type), id->num, part->name);
356 * Multiple eraseregions (non-uniform NOR),
357 * checking for alignment is more complex here
360 /* Check start alignment */
361 for (i = 0; i < mtd->numeraseregions; i++) {
362 start = mtd->eraseregions[i].offset;
363 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
364 if (part->offset == start)
366 start += mtd->eraseregions[i].erasesize;
370 printf("%s%d: partition (%s) start offset alignment incorrect\n",
371 MTD_DEV_TYPE(id->type), id->num, part->name);
376 /* Check end/size alignment */
377 for (i = 0; i < mtd->numeraseregions; i++) {
378 start = mtd->eraseregions[i].offset;
379 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
380 if ((part->offset + part->size) == start)
382 start += mtd->eraseregions[i].erasesize;
385 /* Check last sector alignment */
386 if ((part->offset + part->size) == start)
389 printf("%s%d: partition (%s) size alignment incorrect\n",
390 MTD_DEV_TYPE(id->type), id->num, part->name);
402 * Performs sanity check for supplied partition. Offset and size are verified
403 * to be within valid range. Partition type is checked and either
404 * parts_validate_nor() or parts_validate_nand() is called with the argument
407 * @param id of the parent device
408 * @param part partition to validate
409 * @return 0 if partition is valid, 1 otherwise
411 static int part_validate(struct mtdids *id, struct part_info *part)
413 if (part->size == SIZE_REMAINING)
414 part->size = id->size - part->offset;
416 if (part->offset > id->size) {
417 printf("%s: offset %08x beyond flash size %08x\n",
418 id->mtd_id, part->offset, id->size);
422 if ((part->offset + part->size) <= part->offset) {
423 printf("%s%d: partition (%s) size too big\n",
424 MTD_DEV_TYPE(id->type), id->num, part->name);
428 if (part->offset + part->size > id->size) {
429 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
434 * Now we need to check if the partition starts and ends on
435 * sector (eraseblock) regions
437 return part_validate_eraseblock(id, part);
440 #ifndef CONFIG_CMD_MTDPARTS_LITE
442 * Delete selected partition from the partion 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)
489 * Delete all partitions from parts head list, free memory.
491 * @param head list of partitions to delete
493 static void part_delall(struct list_head *head)
495 struct list_head *entry, *n;
496 struct part_info *part_tmp;
498 /* clean tmp_list and free allocated memory */
499 list_for_each_safe(entry, n, head) {
500 part_tmp = list_entry(entry, struct part_info, link);
508 * Add new partition to the supplied partition list. Make sure partitions are
509 * sorted by offset in ascending order.
511 * @param head list this partition is to be added to
512 * @param new partition to be added
514 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
516 struct list_head *entry;
517 struct part_info *new_pi, *curr_pi;
519 /* link partition to parrent dev */
522 if (list_empty(&dev->parts)) {
523 debug("part_sort_add: list empty\n");
524 list_add(&part->link, &dev->parts);
530 new_pi = list_entry(&part->link, struct part_info, link);
532 /* get current partition info if we are updating current device */
534 if (dev == current_mtd_dev)
535 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
537 list_for_each(entry, &dev->parts) {
538 struct part_info *pi;
540 pi = list_entry(entry, struct part_info, link);
542 /* be compliant with kernel cmdline, allow only one partition at offset zero */
543 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
544 printf("cannot add second partition at offset 0\n");
548 if (new_pi->offset <= pi->offset) {
549 list_add_tail(&part->link, entry);
552 if (curr_pi && (pi->offset <= curr_pi->offset)) {
553 /* we are modyfing partitions for the current
554 * device, update current */
555 current_mtd_partnum++;
564 list_add_tail(&part->link, &dev->parts);
570 #ifndef CONFIG_CMD_MTDPARTS_LITE
572 * Add provided partition to the partition list of a given device.
574 * @param dev device to which partition is added
575 * @param part partition to be added
576 * @return 0 on success, 1 otherwise
578 static int part_add(struct mtd_device *dev, struct part_info *part)
580 /* verify alignment and size */
581 if (part_validate(dev->id, part) != 0)
584 /* partition is ok, add it to the list */
585 if (part_sort_add(dev, part) != 0)
593 * Parse one partition definition, allocate memory and return pointer to this
594 * location in retpart.
596 * @param partdef pointer to the partition definition string i.e. <part-def>
597 * @param ret output pointer to next char after parse completes (output)
598 * @param retpart pointer to the allocated partition (output)
599 * @return 0 on success, 1 otherwise
601 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
603 struct part_info *part;
605 unsigned long offset;
608 unsigned int mask_flags;
615 /* fetch the partition size */
617 /* assign all remaining space to this partition */
618 debug("'-': remaining size assigned\n");
619 size = SIZE_REMAINING;
622 size = memsize_parse(p, &p);
623 if (size < MIN_PART_SIZE) {
624 printf("partition size too small (%lx)\n", size);
629 /* check for offset */
630 offset = OFFSET_NOT_SPECIFIED;
633 offset = memsize_parse(p, &p);
636 /* now look for the name */
639 if ((p = strchr(name, ')')) == NULL) {
640 printf("no closing ) found in partition name\n");
643 name_len = p - name + 1;
644 if ((name_len - 1) == 0) {
645 printf("empty partition name\n");
650 /* 0x00000000@0x00000000 */
655 /* test for options */
657 if (strncmp(p, "ro", 2) == 0) {
658 mask_flags |= MTD_WRITEABLE_CMD;
662 /* check for next partition definition */
664 if (size == SIZE_REMAINING) {
666 printf("no partitions allowed after a fill-up partition\n");
670 } else if ((*p == ';') || (*p == '\0')) {
673 printf("unexpected character '%c' at the end of partition\n", *p);
678 /* allocate memory */
679 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
681 printf("out of memory\n");
684 memset(part, 0, sizeof(struct part_info) + name_len);
686 part->offset = offset;
687 part->mask_flags = mask_flags;
688 part->name = (char *)(part + 1);
691 /* copy user provided name */
692 strncpy(part->name, name, name_len - 1);
695 /* auto generated name in form of size@offset */
696 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
700 part->name[name_len - 1] = '\0';
701 INIT_LIST_HEAD(&part->link);
703 debug("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
704 part->name, part->size,
705 part->offset, part->mask_flags);
712 * Check device number to be within valid range for given device type.
714 * @param type mtd type
715 * @param num mtd number
716 * @param size a pointer to the size of the mtd device (output)
717 * @return 0 if device is valid, 1 otherwise
719 int mtd_device_validate(u8 type, u8 num, u32 *size)
721 struct mtd_info *mtd = NULL;
723 if (get_mtd_info(type, num, &mtd))
732 * Delete all mtd devices from a supplied devices list, free memory allocated for
733 * each device and delete all device partitions.
735 * @return 0 on success, 1 otherwise
737 static int device_delall(struct list_head *head)
739 struct list_head *entry, *n;
740 struct mtd_device *dev_tmp;
742 /* clean devices list */
743 list_for_each_safe(entry, n, head) {
744 dev_tmp = list_entry(entry, struct mtd_device, link);
746 part_delall(&dev_tmp->parts);
749 INIT_LIST_HEAD(&devices);
755 * If provided device exists it's partitions are deleted, device is removed
756 * from device list and device memory is freed.
758 * @param dev device to be deleted
759 * @return 0 on success, 1 otherwise
761 static int device_del(struct mtd_device *dev)
763 part_delall(&dev->parts);
764 list_del(&dev->link);
767 if (dev == current_mtd_dev) {
768 /* we just deleted current device */
769 if (list_empty(&devices)) {
770 current_mtd_dev = NULL;
772 /* reset first partition from first dev from the
773 * devices list as current */
774 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
775 current_mtd_partnum = 0;
786 * Search global device list and return pointer to the device of type and num
789 * @param type device type
790 * @param num device number
791 * @return NULL if requested device does not exist
793 struct mtd_device *device_find(u8 type, u8 num)
795 struct list_head *entry;
796 struct mtd_device *dev_tmp;
798 list_for_each(entry, &devices) {
799 dev_tmp = list_entry(entry, struct mtd_device, link);
801 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
808 #ifndef CONFIG_CMD_MTDPARTS_LITE
810 * Add specified device to the global device list.
812 * @param dev device to be added
814 static void device_add(struct mtd_device *dev)
816 u8 current_save_needed = 0;
818 if (list_empty(&devices)) {
819 current_mtd_dev = dev;
820 current_mtd_partnum = 0;
821 current_save_needed = 1;
824 list_add_tail(&dev->link, &devices);
826 if (current_save_needed > 0)
834 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
835 * return pointer to the device structure.
837 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
838 * @param ret output pointer to next char after parse completes (output)
839 * @param retdev pointer to the allocated device (output)
840 * @return 0 on success, 1 otherwise
842 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
844 struct mtd_device *dev;
845 struct part_info *part;
848 unsigned int mtd_id_len;
849 const char *p, *pend;
851 struct list_head *entry, *n;
856 debug("===device_parse===\n");
865 mtd_id = p = mtd_dev;
866 if (!(p = strchr(mtd_id, ':'))) {
867 printf("no <mtd-id> identifier\n");
870 mtd_id_len = p - mtd_id + 1;
873 /* verify if we have a valid device specified */
874 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
875 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
879 debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
880 id->type, MTD_DEV_TYPE(id->type),
881 id->num, id->mtd_id);
882 pend = strchr(p, ';');
883 debug("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
886 /* parse partitions */
890 if ((dev = device_find(id->type, id->num)) != NULL) {
891 /* if device already exists start at the end of the last partition */
892 part = list_entry(dev->parts.prev, struct part_info, link);
893 offset = part->offset + part->size;
896 while (p && (*p != '\0') && (*p != ';')) {
898 if ((part_parse(p, &p, &part) != 0) || (!part))
901 /* calculate offset when not specified */
902 if (part->offset == OFFSET_NOT_SPECIFIED)
903 part->offset = offset;
905 offset = part->offset;
907 /* verify alignment and size */
908 if (part_validate(id, part) != 0)
911 offset += part->size;
913 /* partition is ok, add it to the list */
914 list_add_tail(&part->link, &tmp_list);
919 part_delall(&tmp_list);
923 if (num_parts == 0) {
924 printf("no partitions for device %s%d (%s)\n",
925 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
929 debug("\ntotal partitions: %d\n", num_parts);
931 /* check for next device presence */
936 } else if (*p == '\0') {
940 printf("unexpected character '%c' at the end of device\n", *p);
947 /* allocate memory for mtd_device structure */
948 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
949 printf("out of memory\n");
952 memset(dev, 0, sizeof(struct mtd_device));
954 dev->num_parts = 0; /* part_sort_add increments num_parts */
955 INIT_LIST_HEAD(&dev->parts);
956 INIT_LIST_HEAD(&dev->link);
958 /* move partitions from tmp_list to dev->parts */
959 list_for_each_safe(entry, n, &tmp_list) {
960 part = list_entry(entry, struct part_info, link);
962 if (part_sort_add(dev, part) != 0) {
975 * Initialize global device list.
977 * @return 0 on success, 1 otherwise
979 static int mtd_devices_init(void)
981 last_parts[0] = '\0';
982 current_mtd_dev = NULL;
985 return device_delall(&devices);
988 #ifndef CONFIG_CMD_MTDPARTS_LITE
990 * Search global mtdids list and find id of requested type and number.
992 * @return pointer to the id if it exists, NULL otherwise
994 static struct mtdids* id_find(u8 type, u8 num)
996 struct list_head *entry;
999 list_for_each(entry, &mtdids) {
1000 id = list_entry(entry, struct mtdids, link);
1002 if ((id->type == type) && (id->num == num))
1011 * Search global mtdids list and find id of a requested mtd_id.
1013 * Note: first argument is not null terminated.
1015 * @param mtd_id string containing requested mtd_id
1016 * @param mtd_id_len length of supplied mtd_id
1017 * @return pointer to the id if it exists, NULL otherwise
1019 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1021 struct list_head *entry;
1024 debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1025 mtd_id_len, mtd_id, mtd_id_len);
1027 list_for_each(entry, &mtdids) {
1028 id = list_entry(entry, struct mtdids, link);
1030 debug("entry: '%s' (len = %d)\n",
1031 id->mtd_id, strlen(id->mtd_id));
1033 if (mtd_id_len != strlen(id->mtd_id))
1035 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1043 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1044 * return device type and number.
1046 * @param id string describing device id
1047 * @param ret_id output pointer to next char after parse completes (output)
1048 * @param dev_type parsed device type (output)
1049 * @param dev_num parsed device number (output)
1050 * @return 0 on success, 1 otherwise
1052 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1057 if (strncmp(p, "nand", 4) == 0) {
1058 *dev_type = MTD_DEV_TYPE_NAND;
1060 } else if (strncmp(p, "nor", 3) == 0) {
1061 *dev_type = MTD_DEV_TYPE_NOR;
1063 } else if (strncmp(p, "onenand", 7) == 0) {
1064 *dev_type = MTD_DEV_TYPE_ONENAND;
1067 printf("incorrect device type in %s\n", id);
1072 printf("incorrect device number in %s\n", id);
1076 *dev_num = simple_strtoul(p, (char **)&p, 0);
1082 #ifndef CONFIG_CMD_MTDPARTS_LITE
1084 * Process all devices and generate corresponding mtdparts string describing
1085 * all partitions on all devices.
1087 * @param buf output buffer holding generated mtdparts string (output)
1088 * @param buflen buffer size
1089 * @return 0 on success, 1 otherwise
1091 static int generate_mtdparts(char *buf, u32 buflen)
1093 struct list_head *pentry, *dentry;
1094 struct mtd_device *dev;
1095 struct part_info *part, *prev_part;
1098 u32 size, offset, len, part_cnt;
1099 u32 maxlen = buflen - 1;
1101 debug("--- generate_mtdparts ---\n");
1103 if (list_empty(&devices)) {
1108 sprintf(p, "mtdparts=");
1111 list_for_each(dentry, &devices) {
1112 dev = list_entry(dentry, struct mtd_device, link);
1115 len = strlen(dev->id->mtd_id) + 1;
1118 memcpy(p, dev->id->mtd_id, len - 1);
1123 /* format partitions */
1126 list_for_each(pentry, &dev->parts) {
1127 part = list_entry(pentry, struct part_info, link);
1129 offset = part->offset;
1132 /* partition size */
1133 memsize_format(tmpbuf, size);
1134 len = strlen(tmpbuf);
1137 memcpy(p, tmpbuf, len);
1142 /* add offset only when there is a gap between
1144 if ((!prev_part && (offset != 0)) ||
1145 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1147 memsize_format(tmpbuf, offset);
1148 len = strlen(tmpbuf) + 1;
1152 memcpy(p, tmpbuf, len - 1);
1157 /* copy name only if user supplied */
1158 if(!part->auto_name) {
1159 len = strlen(part->name) + 2;
1164 memcpy(p, part->name, len - 2);
1171 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1180 /* print ',' separator if there are other partitions
1182 if (dev->num_parts > part_cnt) {
1190 /* print ';' separator if there are other devices following */
1191 if (dentry->next != &devices) {
1199 /* we still have at least one char left, as we decremented maxlen at
1206 last_parts[0] = '\0';
1211 * Call generate_mtdparts to process all devices and generate corresponding
1212 * mtdparts string, save it in mtdparts environment variable.
1214 * @param buf output buffer holding generated mtdparts string (output)
1215 * @param buflen buffer size
1216 * @return 0 on success, 1 otherwise
1218 static int generate_mtdparts_save(char *buf, u32 buflen)
1222 ret = generate_mtdparts(buf, buflen);
1224 if ((buf[0] != '\0') && (ret == 0))
1225 setenv("mtdparts", buf);
1227 setenv("mtdparts", NULL);
1233 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1235 * Get the net size (w/o bad blocks) of the given partition.
1237 * @param mtd the mtd info
1238 * @param part the partition
1239 * @return the calculated net size of this partition
1241 static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
1243 uint64_t i, net_size = 0;
1245 if (!mtd->block_isbad)
1248 for (i = 0; i < part->size; i += mtd->erasesize) {
1249 if (!mtd->block_isbad(mtd, part->offset + i))
1250 net_size += mtd->erasesize;
1257 static void print_partition_table(void)
1259 struct list_head *dentry, *pentry;
1260 struct part_info *part;
1261 struct mtd_device *dev;
1264 list_for_each(dentry, &devices) {
1265 dev = list_entry(dentry, struct mtd_device, link);
1266 /* list partitions for given device */
1268 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1269 struct mtd_info *mtd;
1271 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1274 printf("\ndevice %s%d <%s>, # parts = %d\n",
1275 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1276 dev->id->mtd_id, dev->num_parts);
1277 printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
1279 list_for_each(pentry, &dev->parts) {
1283 part = list_entry(pentry, struct part_info, link);
1284 net_size = net_part_size(mtd, part);
1285 size_note = part->size == net_size ? " " : " (!)";
1286 printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
1287 part_num, part->name, part->size,
1288 net_size, size_note, part->offset,
1290 #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1291 printf("\ndevice %s%d <%s>, # parts = %d\n",
1292 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1293 dev->id->mtd_id, dev->num_parts);
1294 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
1296 list_for_each(pentry, &dev->parts) {
1297 part = list_entry(pentry, struct part_info, link);
1298 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1299 part_num, part->name, part->size,
1300 part->offset, part->mask_flags);
1301 #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1306 if (list_empty(&devices))
1307 printf("no partitions defined\n");
1311 * Format and print out a partition list for each device from global device
1314 static void list_partitions(void)
1316 struct part_info *part;
1318 debug("\n---list_partitions---\n");
1319 print_partition_table();
1321 /* current_mtd_dev is not NULL only when we have non empty device list */
1322 if (current_mtd_dev) {
1323 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
1325 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
1326 MTD_DEV_TYPE(current_mtd_dev->id->type),
1327 current_mtd_dev->id->num, current_mtd_partnum,
1328 part->name, part->size, part->offset);
1330 printf("could not get current partition info\n\n");
1334 printf("\ndefaults:\n");
1335 printf("mtdids : %s\n",
1336 mtdids_default ? mtdids_default : "none");
1338 * Using printf() here results in printbuffer overflow
1339 * if default mtdparts string is greater than console
1340 * printbuffer. Use puts() to prevent system crashes.
1343 puts(mtdparts_default ? mtdparts_default : "none");
1348 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1349 * corresponding device and verify partition number.
1351 * @param id string describing device and partition or partition name
1352 * @param dev pointer to the requested device (output)
1353 * @param part_num verified partition number (output)
1354 * @param part pointer to requested partition (output)
1355 * @return 0 on success, 1 otherwise
1357 int find_dev_and_part(const char *id, struct mtd_device **dev,
1358 u8 *part_num, struct part_info **part)
1360 struct list_head *dentry, *pentry;
1361 u8 type, dnum, pnum;
1364 debug("--- find_dev_and_part ---\nid = %s\n", id);
1366 list_for_each(dentry, &devices) {
1368 *dev = list_entry(dentry, struct mtd_device, link);
1369 list_for_each(pentry, &(*dev)->parts) {
1370 *part = list_entry(pentry, struct part_info, link);
1371 if (strcmp((*part)->name, id) == 0)
1382 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1385 if ((*p++ != ',') || (*p == '\0')) {
1386 printf("no partition number specified\n");
1389 pnum = simple_strtoul(p, (char **)&p, 0);
1391 printf("unexpected trailing character '%c'\n", *p);
1395 if ((*dev = device_find(type, dnum)) == NULL) {
1396 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1400 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1401 printf("no such partition\n");
1411 #ifndef CONFIG_CMD_MTDPARTS_LITE
1413 * Find and delete partition. For partition id format see find_dev_and_part().
1415 * @param id string describing device and partition
1416 * @return 0 on success, 1 otherwise
1418 static int delete_partition(const char *id)
1421 struct mtd_device *dev;
1422 struct part_info *part;
1424 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1426 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08x@0x%08x\n",
1427 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1428 part->name, part->size, part->offset);
1430 if (part_del(dev, part) != 0)
1433 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1434 printf("generated mtdparts too long, reseting to null\n");
1440 printf("partition %s not found\n", id);
1445 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1447 * Increase the size of the given partition so that it's net size is at least
1448 * as large as the size member and such that the next partition would start on a
1449 * good block if it were adjacent to this partition.
1451 * @param mtd the mtd device
1452 * @param part the partition
1453 * @param next_offset pointer to the offset of the next partition after this
1454 * partition's size has been modified (output)
1456 static void spread_partition(struct mtd_info *mtd, struct part_info *part,
1457 uint64_t *next_offset)
1459 uint64_t net_size, padding_size = 0;
1462 mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
1466 * Absorb bad blocks immediately following this
1467 * partition also into the partition, such that
1468 * the next partition starts with a good block.
1471 mtd_get_len_incl_bad(mtd, part->offset + net_size,
1472 mtd->erasesize, &padding_size, &truncated);
1476 padding_size -= mtd->erasesize;
1480 printf("truncated partition %s to %lld bytes\n", part->name,
1481 (uint64_t) net_size + padding_size);
1484 part->size = net_size + padding_size;
1485 *next_offset = part->offset + part->size;
1489 * Adjust all of the partition sizes, such that all partitions are at least
1490 * as big as their mtdparts environment variable sizes and they each start
1493 * @return 0 on success, 1 otherwise
1495 static int spread_partitions(void)
1497 struct list_head *dentry, *pentry;
1498 struct mtd_device *dev;
1499 struct part_info *part;
1500 struct mtd_info *mtd;
1504 list_for_each(dentry, &devices) {
1505 dev = list_entry(dentry, struct mtd_device, link);
1507 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1512 list_for_each(pentry, &dev->parts) {
1513 part = list_entry(pentry, struct part_info, link);
1515 debug("spread_partitions: device = %s%d, partition %d ="
1516 " (%s) 0x%08x@0x%08x\n",
1517 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1518 part_num, part->name, part->size,
1521 if (cur_offs > part->offset)
1522 part->offset = cur_offs;
1524 spread_partition(mtd, part, &cur_offs);
1532 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1533 printf("generated mtdparts too long, reseting to null\n");
1538 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1540 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1542 * Increase the size of the given partition so that it's net size is at least
1543 * as large as the size member and such that the next partition would start on a
1544 * good block if it were adjacent to this partition.
1546 * @param mtd the mtd device
1547 * @param part the partition
1548 * @param next_offset pointer to the offset of the next partition after this
1549 * partition's size has been modified (output)
1551 static void spread_partition(struct mtd_info *mtd, struct part_info *part,
1552 uint64_t *next_offset)
1554 uint64_t net_size, padding_size = 0;
1557 mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
1561 * Absorb bad blocks immediately following this
1562 * partition also into the partition, such that
1563 * the next partition starts with a good block.
1566 mtd_get_len_incl_bad(mtd, part->offset + net_size,
1567 mtd->erasesize, &padding_size, &truncated);
1571 padding_size -= mtd->erasesize;
1575 printf("truncated partition %s to %lld bytes\n", part->name,
1576 (uint64_t) net_size + padding_size);
1579 part->size = net_size + padding_size;
1580 *next_offset = part->offset + part->size;
1584 * Adjust all of the partition sizes, such that all partitions are at least
1585 * as big as their mtdparts environment variable sizes and they each start
1588 * @return 0 on success, 1 otherwise
1590 static int spread_partitions(void)
1592 struct list_head *dentry, *pentry;
1593 struct mtd_device *dev;
1594 struct part_info *part;
1595 struct mtd_info *mtd;
1599 list_for_each(dentry, &devices) {
1600 dev = list_entry(dentry, struct mtd_device, link);
1602 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1607 list_for_each(pentry, &dev->parts) {
1608 part = list_entry(pentry, struct part_info, link);
1610 debug("spread_partitions: device = %s%d, partition %d ="
1611 " (%s) 0x%08x@0x%08x\n",
1612 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1613 part_num, part->name, part->size,
1616 if (cur_offs > part->offset)
1617 part->offset = cur_offs;
1619 spread_partition(mtd, part, &cur_offs);
1627 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1628 printf("generated mtdparts too long, reseting to null\n");
1633 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1636 * Accept character string describing mtd partitions and call device_parse()
1637 * for each entry. Add created devices to the global devices list.
1639 * @param mtdparts string specifing mtd partitions
1640 * @return 0 on success, 1 otherwise
1642 static int parse_mtdparts(const char *const mtdparts)
1644 const char *p = mtdparts;
1645 struct mtd_device *dev;
1648 debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1650 /* delete all devices and partitions */
1651 if (mtd_devices_init() != 0) {
1652 printf("could not initialise device list\n");
1656 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1657 p = getenv("mtdparts");
1659 if (strncmp(p, "mtdparts=", 9) != 0) {
1660 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1665 while (p && (*p != '\0')) {
1667 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1670 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1671 dev->id->num, dev->id->mtd_id);
1673 /* check if parsed device is already on the list */
1674 if (device_find(dev->id->type, dev->id->num) != NULL) {
1675 printf("device %s%d redefined, please correct mtdparts variable\n",
1676 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1680 list_add_tail(&dev->link, &devices);
1684 device_delall(&devices);
1692 * Parse provided string describing mtdids mapping (see file header for mtdids
1693 * variable format). Allocate memory for each entry and add all found entries
1694 * to the global mtdids list.
1696 * @param ids mapping string
1697 * @return 0 on success, 1 otherwise
1699 static int parse_mtdids(const char *const ids)
1701 const char *p = ids;
1705 struct list_head *entry, *n;
1706 struct mtdids *id_tmp;
1711 debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1713 /* clean global mtdids list */
1714 list_for_each_safe(entry, n, &mtdids) {
1715 id_tmp = list_entry(entry, struct mtdids, link);
1716 debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1721 INIT_LIST_HEAD(&mtdids);
1723 while(p && (*p != '\0')) {
1726 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1727 if (mtd_id_parse(p, &p, &type, &num) != 0)
1731 printf("mtdids: incorrect <dev-num>\n");
1736 /* check if requested device exists */
1737 if (mtd_device_validate(type, num, &size) != 0)
1740 /* locate <mtd-id> */
1742 if ((p = strchr(mtd_id, ',')) != NULL) {
1743 mtd_id_len = p - mtd_id + 1;
1746 mtd_id_len = strlen(mtd_id) + 1;
1748 if (mtd_id_len == 0) {
1749 printf("mtdids: no <mtd-id> identifier\n");
1753 /* check if this id is already on the list */
1754 int double_entry = 0;
1755 list_for_each(entry, &mtdids) {
1756 id_tmp = list_entry(entry, struct mtdids, link);
1757 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1763 printf("device id %s%d redefined, please correct mtdids variable\n",
1764 MTD_DEV_TYPE(type), num);
1768 /* allocate mtdids structure */
1769 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1770 printf("out of memory\n");
1773 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1777 id->mtd_id = (char *)(id + 1);
1778 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1779 id->mtd_id[mtd_id_len - 1] = '\0';
1780 INIT_LIST_HEAD(&id->link);
1782 debug("+ id %s%d\t%16d bytes\t%s\n",
1783 MTD_DEV_TYPE(id->type), id->num,
1784 id->size, id->mtd_id);
1786 list_add_tail(&id->link, &mtdids);
1790 /* clean mtdids list and free allocated memory */
1791 list_for_each_safe(entry, n, &mtdids) {
1792 id_tmp = list_entry(entry, struct mtdids, link);
1803 * Parse and initialize global mtdids mapping and create global
1804 * device/partition list.
1806 * @return 0 on success, 1 otherwise
1808 int mtdparts_init(void)
1810 static int initialized = 0;
1811 const char *ids, *parts;
1812 const char *current_partition;
1814 char tmp_ep[PARTITION_MAXLEN+1];
1816 debug("\n---mtdparts_init---\n");
1818 INIT_LIST_HEAD(&mtdids);
1819 INIT_LIST_HEAD(&devices);
1820 memset(last_ids, 0, MTDIDS_MAXLEN);
1821 memset(last_parts, 0, MTDPARTS_MAXLEN);
1822 memset(last_partition, 0, PARTITION_MAXLEN);
1827 ids = getenv("mtdids");
1828 parts = getenv("mtdparts");
1829 current_partition = getenv("partition");
1831 /* save it for later parsing, cannot rely on current partition pointer
1832 * as 'partition' variable may be updated during init */
1834 if (current_partition)
1835 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1837 debug("last_ids : %s\n", last_ids);
1838 debug("env_ids : %s\n", ids);
1839 debug("last_parts: %s\n", last_parts);
1840 debug("env_parts : %s\n\n", parts);
1842 debug("last_partition : %s\n", last_partition);
1843 debug("env_partition : %s\n", current_partition);
1845 /* if mtdids varible is empty try to use defaults */
1847 if (mtdids_default) {
1848 debug("mtdids variable not defined, using default\n");
1849 ids = mtdids_default;
1850 setenv("mtdids", (char *)ids);
1852 printf("mtdids not defined, no default present\n");
1856 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1857 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1861 /* do no try to use defaults when mtdparts variable is not defined,
1862 * just check the length */
1864 printf("mtdparts variable not set, see 'help mtdparts'\n");
1866 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1867 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1871 /* check if we have already parsed those mtdids */
1872 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1877 if (parse_mtdids(ids) != 0) {
1882 /* ok it's good, save new ids */
1883 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1886 /* parse partitions if either mtdparts or mtdids were updated */
1887 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1888 if (parse_mtdparts(parts) != 0)
1891 if (list_empty(&devices)) {
1892 printf("mtdparts_init: no valid partitions\n");
1896 /* ok it's good, save new parts */
1897 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1899 /* reset first partition from first dev from the list as current */
1900 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1901 current_mtd_partnum = 0;
1904 debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
1905 MTD_DEV_TYPE(current_mtd_dev->id->type),
1906 current_mtd_dev->id->num, current_mtd_partnum);
1909 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1910 if (!parts && (last_parts[0] != '\0'))
1911 return mtd_devices_init();
1913 /* do not process current partition if mtdparts variable is null */
1917 /* is current partition set in environment? if so, use it */
1918 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1919 struct part_info *p;
1920 struct mtd_device *cdev;
1923 debug("--- getting current partition: %s\n", tmp_ep);
1925 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1926 current_mtd_dev = cdev;
1927 current_mtd_partnum = pnum;
1930 } else if (getenv("partition") == NULL) {
1931 debug("no partition variable set, setting...\n");
1939 * Return pointer to the partition of a requested number from a requested
1942 * @param dev device that is to be searched for a partition
1943 * @param part_num requested partition number
1944 * @return pointer to the part_info, NULL otherwise
1946 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1948 struct list_head *entry;
1949 struct part_info *part;
1955 debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1956 part_num, MTD_DEV_TYPE(dev->id->type),
1957 dev->id->num, dev->id->mtd_id);
1959 if (part_num >= dev->num_parts) {
1960 printf("invalid partition number %d for device %s%d (%s)\n",
1961 part_num, MTD_DEV_TYPE(dev->id->type),
1962 dev->id->num, dev->id->mtd_id);
1966 /* locate partition number, return it */
1968 list_for_each(entry, &dev->parts) {
1969 part = list_entry(entry, struct part_info, link);
1971 if (part_num == num++) {
1979 /***************************************************/
1980 /* U-boot commands */
1981 /***************************************************/
1982 /* command line only */
1983 #ifndef CONFIG_CMD_MTDPARTS_LITE
1985 * Routine implementing u-boot chpart command. Sets new current partition based
1986 * on the user supplied partition id. For partition id format see find_dev_and_part().
1988 * @param cmdtp command internal data
1989 * @param flag command flag
1990 * @param argc number of arguments supplied to the command
1991 * @param argv arguments list
1992 * @return 0 on success, 1 otherwise
1994 int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1996 /* command line only */
1997 struct mtd_device *dev;
1998 struct part_info *part;
2001 if (mtdparts_init() !=0)
2005 printf("no partition id specified\n");
2009 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2012 current_mtd_dev = dev;
2013 current_mtd_partnum = pnum;
2016 printf("partition changed to %s%d,%d\n",
2017 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2024 * Routine implementing u-boot mtdparts command. Initialize/update default global
2025 * partition list and process user partition request (list, add, del).
2027 * @param cmdtp command internal data
2028 * @param flag command flag
2029 * @param argc number of arguments supplied to the command
2030 * @param argv arguments list
2031 * @return 0 on success, 1 otherwise
2033 int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
2035 #ifndef CONFIG_CMD_MTDPARTS_LITE
2037 if (strcmp(argv[1], "default") == 0) {
2038 setenv("mtdids", (char *)mtdids_default);
2039 setenv("mtdparts", (char *)mtdparts_default);
2040 setenv("partition", NULL);
2044 } else if (strcmp(argv[1], "delall") == 0) {
2045 /* this may be the first run, initialize lists if needed */
2048 setenv("mtdparts", NULL);
2050 /* mtd_devices_init() calls current_save() */
2051 return mtd_devices_init();
2056 /* make sure we are in sync with env variables */
2057 if (mtdparts_init() != 0)
2065 #ifndef CONFIG_CMD_MTDPARTS_LITE
2066 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2067 if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
2068 #define PART_ADD_DESC_MAXLEN 64
2069 char tmpbuf[PART_ADD_DESC_MAXLEN];
2070 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2071 struct mtd_info *mtd;
2072 uint64_t next_offset;
2075 struct mtd_device *dev;
2076 struct mtd_device *dev_tmp;
2078 struct part_info *p;
2080 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
2083 if ((id = id_find(type, num)) == NULL) {
2084 printf("no such device %s defined in mtdids variable\n", argv[2]);
2088 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2089 len += strlen(argv[3]); /* size@offset */
2090 len += strlen(argv[4]) + 2; /* '(' name ')' */
2091 if (argv[5] && (strlen(argv[5]) == 2))
2092 len += 2; /* 'ro' */
2094 if (len >= PART_ADD_DESC_MAXLEN) {
2095 printf("too long partition description\n");
2098 sprintf(tmpbuf, "%s:%s(%s)%s",
2099 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
2100 debug("add tmpbuf: %s\n", tmpbuf);
2102 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2105 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2106 dev->id->num, dev->id->mtd_id);
2108 p = list_entry(dev->parts.next, struct part_info, link);
2110 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2111 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
2114 if (!strcmp(&argv[1][3], ".spread")) {
2115 spread_partition(mtd, p, &next_offset);
2116 debug("increased %s to %d bytes\n", p->name, p->size);
2120 dev_tmp = device_find(dev->id->type, dev->id->num);
2121 if (dev_tmp == NULL) {
2123 } else if (part_add(dev_tmp, p) != 0) {
2124 /* merge new partition with existing ones*/
2129 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2130 printf("generated mtdparts too long, reseting to null\n");
2137 /* mtdparts del part-id */
2138 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2139 debug("del: part-id = %s\n", argv[2]);
2141 return delete_partition(argv[2]);
2145 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2146 if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
2147 return spread_partitions();
2148 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2150 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2151 if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
2152 return spread_partitions();
2153 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2155 return cmd_usage(cmdtp);
2158 /***************************************************/
2159 #ifndef CONFIG_CMD_MTDPARTS_LITE
2161 chpart, 2, 0, do_chpart,
2162 "change active partition",
2164 " - change active partition (e.g. part-id = nand0,1)"
2168 mtdparts, 6, 0, do_mtdparts,
2169 "define flash/nand partitions",
2171 " - list partition table\n"
2173 " - delete all partitions\n"
2174 "mtdparts del part-id\n"
2175 " - delete partition (e.g. part-id = nand0,1)\n"
2176 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2177 " - add partition\n"
2178 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2179 "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2180 " - add partition, padding size by skipping bad blocks\n"
2182 "mtdparts default\n"
2183 " - reset partition table to defaults\n"
2184 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2186 " - adjust the sizes of the partitions so they are\n"
2187 " at least as big as the mtdparts variable specifies\n"
2188 " and they each start on a good block\n\n"
2191 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2193 "this command uses three environment variables:\n\n"
2194 "'partition' - keeps current partition identifier\n\n"
2195 "partition := <part-id>\n"
2196 "<part-id> := <dev-id>,part_num\n\n"
2197 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2198 "mtdids=<idmap>[,<idmap>,...]\n\n"
2199 "<idmap> := <dev-id>=<mtd-id>\n"
2200 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
2201 "<dev-num> := mtd device number, 0...\n"
2202 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2203 "'mtdparts' - partition list\n\n"
2204 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2205 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2206 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2207 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2208 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2209 "<offset> := partition start offset within the device\n"
2210 "<name> := '(' NAME ')'\n"
2211 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)"
2215 mtdparts, 1, 0, do_mtdparts,
2216 "define flash/nand partitions",
2218 " - list partition table\n"
2221 /***************************************************/