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
18 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
19 * Copyright 2002 SYSGO Real-Time Solutions GmbH
21 * See file CREDITS for list of people who contributed to this
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2 of
27 * the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
41 * Three environment variables are used by the parsing routines:
43 * 'partition' - keeps current partition identifier
45 * partition := <part-id>
46 * <part-id> := <dev-id>,part_num
49 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
51 * mtdids=<idmap>[,<idmap>,...]
53 * <idmap> := <dev-id>=<mtd-id>
54 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
55 * <dev-num> := mtd device number, 0...
56 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
59 * 'mtdparts' - partition list
61 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
63 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
64 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
65 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
66 * <size> := standard linux memsize OR '-' to denote all remaining space
67 * <offset> := partition start offset within the device
68 * <name> := '(' NAME ')'
69 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
72 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
73 * - if the above variables are not set defaults for a given target are used
77 * 1 NOR Flash, with 1 single writable partition:
78 * mtdids=nor0=edb7312-nor
79 * mtdparts=mtdparts=edb7312-nor:-
81 * 1 NOR Flash with 2 partitions, 1 NAND with one
82 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
83 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
90 #include <jffs2/load_kernel.h>
91 #include <linux/list.h>
92 #include <linux/ctype.h>
93 #include <linux/err.h>
94 #include <linux/mtd/mtd.h>
96 #if defined(CONFIG_CMD_NAND)
97 #include <linux/mtd/nand.h>
101 #if defined(CONFIG_CMD_ONENAND)
102 #include <linux/mtd/onenand.h>
103 #include <onenand_uboot.h>
106 /* special size referring to all the remaining space in a partition */
107 #define SIZE_REMAINING 0xFFFFFFFF
109 /* special offset value, it is used when not provided by user
111 * this value is used temporarily during parsing, later such offests
112 * are recalculated */
113 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
115 /* minimum partition size */
116 #define MIN_PART_SIZE 4096
118 /* this flag needs to be set in part_info struct mask_flags
119 * field for read-only partitions */
120 #define MTD_WRITEABLE_CMD 1
122 /* default values for mtdids and mtdparts variables */
123 #if defined(MTDIDS_DEFAULT)
124 static const char *const mtdids_default = MTDIDS_DEFAULT;
126 static const char *const mtdids_default = NULL;
129 #if defined(MTDPARTS_DEFAULT)
130 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
132 static const char *const mtdparts_default = NULL;
135 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
136 #define MTDIDS_MAXLEN 128
137 #define MTDPARTS_MAXLEN 512
138 #define PARTITION_MAXLEN 16
139 static char last_ids[MTDIDS_MAXLEN];
140 static char last_parts[MTDPARTS_MAXLEN];
141 static char last_partition[PARTITION_MAXLEN];
143 /* low level jffs2 cache cleaning routine */
144 extern void jffs2_free_cache(struct part_info *part);
146 /* mtdids mapping list, filled by parse_ids() */
147 struct list_head mtdids;
149 /* device/partition list, parse_cmdline() parses into here */
150 struct list_head devices;
152 /* current active device and partition number */
153 struct mtd_device *current_mtd_dev = NULL;
154 u8 current_mtd_partnum = 0;
156 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
158 /* command line only routines */
159 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
160 static int device_del(struct mtd_device *dev);
163 * Parses a string into a number. The number stored at ptr is
164 * potentially suffixed with K (for kilobytes, or 1024 bytes),
165 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
166 * 1073741824). If the number is suffixed with K, M, or G, then
167 * the return value is the number multiplied by one kilobyte, one
168 * megabyte, or one gigabyte, respectively.
170 * @param ptr where parse begins
171 * @param retptr output pointer to next char after parse completes (output)
172 * @return resulting unsigned int
174 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
176 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
197 * Format string describing supplied size. This routine does the opposite job
198 * to memsize_parse(). Size in bytes is converted to string and if possible
199 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
201 * Note, that this routine does not check for buffer overflow, it's the caller
202 * who must assure enough space.
204 * @param buf output buffer
205 * @param size size to be converted to string
207 static void memsize_format(char *buf, u32 size)
209 #define SIZE_GB ((u32)1024*1024*1024)
210 #define SIZE_MB ((u32)1024*1024)
211 #define SIZE_KB ((u32)1024)
213 if ((size % SIZE_GB) == 0)
214 sprintf(buf, "%ug", size/SIZE_GB);
215 else if ((size % SIZE_MB) == 0)
216 sprintf(buf, "%um", size/SIZE_MB);
217 else if (size % SIZE_KB == 0)
218 sprintf(buf, "%uk", size/SIZE_KB);
220 sprintf(buf, "%u", size);
224 * This routine does global indexing of all partitions. Resulting index for
225 * current partition is saved in 'mtddevnum'. Current partition name in
228 static void index_partitions(void)
232 struct part_info *part;
233 struct list_head *dentry;
234 struct mtd_device *dev;
236 debug("--- index partitions ---\n");
238 if (current_mtd_dev) {
240 list_for_each(dentry, &devices) {
241 dev = list_entry(dentry, struct mtd_device, link);
242 if (dev == current_mtd_dev) {
243 mtddevnum += current_mtd_partnum;
244 sprintf(buf, "%d", mtddevnum);
245 setenv("mtddevnum", buf);
248 mtddevnum += dev->num_parts;
251 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
252 setenv("mtddevname", part->name);
254 debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
256 setenv("mtddevnum", NULL);
257 setenv("mtddevname", NULL);
259 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
264 * Save current device and partition in environment variable 'partition'.
266 static void current_save(void)
270 debug("--- current_save ---\n");
272 if (current_mtd_dev) {
273 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
274 current_mtd_dev->id->num, current_mtd_partnum);
276 setenv("partition", buf);
277 strncpy(last_partition, buf, 16);
279 debug("=> partition %s\n", buf);
281 setenv("partition", NULL);
282 last_partition[0] = '\0';
284 debug("=> partition NULL\n");
290 * Performs sanity check for supplied flash partition.
291 * Table of existing MTD flash devices is searched and partition device
292 * is located. Alignment with the granularity of nand erasesize is verified.
294 * @param id of the parent device
295 * @param part partition to validate
296 * @return 0 if partition is valid, 1 otherwise
298 static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
300 struct mtd_info *mtd;
305 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(id->type), id->num);
306 mtd = get_mtd_device_nm(mtd_dev);
308 printf("Partition %s not found on device %s!\n", part->name, mtd_dev);
312 part->sector_size = mtd->erasesize;
314 if (!mtd->numeraseregions) {
316 * Only one eraseregion (NAND, OneNAND or uniform NOR),
317 * checking for alignment is easy here
319 if ((unsigned long)part->offset % mtd->erasesize) {
320 printf("%s%d: partition (%s) start offset"
321 "alignment incorrect\n",
322 MTD_DEV_TYPE(id->type), id->num, part->name);
326 if (part->size % mtd->erasesize) {
327 printf("%s%d: partition (%s) size alignment incorrect\n",
328 MTD_DEV_TYPE(id->type), id->num, part->name);
333 * Multiple eraseregions (non-uniform NOR),
334 * checking for alignment is more complex here
337 /* Check start alignment */
338 for (i = 0; i < mtd->numeraseregions; i++) {
339 start = mtd->eraseregions[i].offset;
340 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
341 if (part->offset == start)
343 start += mtd->eraseregions[i].erasesize;
347 printf("%s%d: partition (%s) start offset alignment incorrect\n",
348 MTD_DEV_TYPE(id->type), id->num, part->name);
353 /* Check end/size alignment */
354 for (i = 0; i < mtd->numeraseregions; i++) {
355 start = mtd->eraseregions[i].offset;
356 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
357 if ((part->offset + part->size) == start)
359 start += mtd->eraseregions[i].erasesize;
362 /* Check last sector alignment */
363 if ((part->offset + part->size) == start)
366 printf("%s%d: partition (%s) size alignment incorrect\n",
367 MTD_DEV_TYPE(id->type), id->num, part->name);
379 * Performs sanity check for supplied partition. Offset and size are verified
380 * to be within valid range. Partition type is checked and either
381 * parts_validate_nor() or parts_validate_nand() is called with the argument
384 * @param id of the parent device
385 * @param part partition to validate
386 * @return 0 if partition is valid, 1 otherwise
388 static int part_validate(struct mtdids *id, struct part_info *part)
390 if (part->size == SIZE_REMAINING)
391 part->size = id->size - part->offset;
393 if (part->offset > id->size) {
394 printf("%s: offset %08x beyond flash size %08x\n",
395 id->mtd_id, part->offset, id->size);
399 if ((part->offset + part->size) <= part->offset) {
400 printf("%s%d: partition (%s) size too big\n",
401 MTD_DEV_TYPE(id->type), id->num, part->name);
405 if (part->offset + part->size > id->size) {
406 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
411 * Now we need to check if the partition starts and ends on
412 * sector (eraseblock) regions
414 return part_validate_eraseblock(id, part);
418 * Delete selected partition from the partion list of the specified device.
420 * @param dev device to delete partition from
421 * @param part partition to delete
422 * @return 0 on success, 1 otherwise
424 static int part_del(struct mtd_device *dev, struct part_info *part)
426 u8 current_save_needed = 0;
428 /* if there is only one partition, remove whole device */
429 if (dev->num_parts == 1)
430 return device_del(dev);
432 /* otherwise just delete this partition */
434 if (dev == current_mtd_dev) {
435 /* we are modyfing partitions for the current device,
437 struct part_info *curr_pi;
438 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
441 if (curr_pi == part) {
442 printf("current partition deleted, resetting current to 0\n");
443 current_mtd_partnum = 0;
444 } else if (part->offset <= curr_pi->offset) {
445 current_mtd_partnum--;
447 current_save_needed = 1;
451 list_del(&part->link);
455 if (current_save_needed > 0)
464 * Delete all partitions from parts head list, free memory.
466 * @param head list of partitions to delete
468 static void part_delall(struct list_head *head)
470 struct list_head *entry, *n;
471 struct part_info *part_tmp;
473 /* clean tmp_list and free allocated memory */
474 list_for_each_safe(entry, n, head) {
475 part_tmp = list_entry(entry, struct part_info, link);
483 * Add new partition to the supplied partition list. Make sure partitions are
484 * sorted by offset in ascending order.
486 * @param head list this partition is to be added to
487 * @param new partition to be added
489 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
491 struct list_head *entry;
492 struct part_info *new_pi, *curr_pi;
494 /* link partition to parrent dev */
497 if (list_empty(&dev->parts)) {
498 debug("part_sort_add: list empty\n");
499 list_add(&part->link, &dev->parts);
505 new_pi = list_entry(&part->link, struct part_info, link);
507 /* get current partition info if we are updating current device */
509 if (dev == current_mtd_dev)
510 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
512 list_for_each(entry, &dev->parts) {
513 struct part_info *pi;
515 pi = list_entry(entry, struct part_info, link);
517 /* be compliant with kernel cmdline, allow only one partition at offset zero */
518 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
519 printf("cannot add second partition at offset 0\n");
523 if (new_pi->offset <= pi->offset) {
524 list_add_tail(&part->link, entry);
527 if (curr_pi && (pi->offset <= curr_pi->offset)) {
528 /* we are modyfing partitions for the current
529 * device, update current */
530 current_mtd_partnum++;
539 list_add_tail(&part->link, &dev->parts);
546 * Add provided partition to the partition list of a given device.
548 * @param dev device to which partition is added
549 * @param part partition to be added
550 * @return 0 on success, 1 otherwise
552 static int part_add(struct mtd_device *dev, struct part_info *part)
554 /* verify alignment and size */
555 if (part_validate(dev->id, part) != 0)
558 /* partition is ok, add it to the list */
559 if (part_sort_add(dev, part) != 0)
566 * Parse one partition definition, allocate memory and return pointer to this
567 * location in retpart.
569 * @param partdef pointer to the partition definition string i.e. <part-def>
570 * @param ret output pointer to next char after parse completes (output)
571 * @param retpart pointer to the allocated partition (output)
572 * @return 0 on success, 1 otherwise
574 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
576 struct part_info *part;
578 unsigned long offset;
581 unsigned int mask_flags;
588 /* fetch the partition size */
590 /* assign all remaining space to this partition */
591 debug("'-': remaining size assigned\n");
592 size = SIZE_REMAINING;
595 size = memsize_parse(p, &p);
596 if (size < MIN_PART_SIZE) {
597 printf("partition size too small (%lx)\n", size);
602 /* check for offset */
603 offset = OFFSET_NOT_SPECIFIED;
606 offset = memsize_parse(p, &p);
609 /* now look for the name */
612 if ((p = strchr(name, ')')) == NULL) {
613 printf("no closing ) found in partition name\n");
616 name_len = p - name + 1;
617 if ((name_len - 1) == 0) {
618 printf("empty partition name\n");
623 /* 0x00000000@0x00000000 */
628 /* test for options */
630 if (strncmp(p, "ro", 2) == 0) {
631 mask_flags |= MTD_WRITEABLE_CMD;
635 /* check for next partition definition */
637 if (size == SIZE_REMAINING) {
639 printf("no partitions allowed after a fill-up partition\n");
643 } else if ((*p == ';') || (*p == '\0')) {
646 printf("unexpected character '%c' at the end of partition\n", *p);
651 /* allocate memory */
652 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
654 printf("out of memory\n");
657 memset(part, 0, sizeof(struct part_info) + name_len);
659 part->offset = offset;
660 part->mask_flags = mask_flags;
661 part->name = (char *)(part + 1);
664 /* copy user provided name */
665 strncpy(part->name, name, name_len - 1);
668 /* auto generated name in form of size@offset */
669 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
673 part->name[name_len - 1] = '\0';
674 INIT_LIST_HEAD(&part->link);
676 debug("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
677 part->name, part->size,
678 part->offset, part->mask_flags);
685 * Check device number to be within valid range for given device type.
687 * @param dev device to validate
688 * @return 0 if device is valid, 1 otherwise
690 int mtd_device_validate(u8 type, u8 num, u32 *size)
692 struct mtd_info *mtd;
695 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
696 mtd = get_mtd_device_nm(mtd_dev);
698 printf("Device %s not found!\n", mtd_dev);
708 * Delete all mtd devices from a supplied devices list, free memory allocated for
709 * each device and delete all device partitions.
711 * @return 0 on success, 1 otherwise
713 static int device_delall(struct list_head *head)
715 struct list_head *entry, *n;
716 struct mtd_device *dev_tmp;
718 /* clean devices list */
719 list_for_each_safe(entry, n, head) {
720 dev_tmp = list_entry(entry, struct mtd_device, link);
722 part_delall(&dev_tmp->parts);
725 INIT_LIST_HEAD(&devices);
731 * If provided device exists it's partitions are deleted, device is removed
732 * from device list and device memory is freed.
734 * @param dev device to be deleted
735 * @return 0 on success, 1 otherwise
737 static int device_del(struct mtd_device *dev)
739 part_delall(&dev->parts);
740 list_del(&dev->link);
743 if (dev == current_mtd_dev) {
744 /* we just deleted current device */
745 if (list_empty(&devices)) {
746 current_mtd_dev = NULL;
748 /* reset first partition from first dev from the
749 * devices list as current */
750 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
751 current_mtd_partnum = 0;
762 * Search global device list and return pointer to the device of type and num
765 * @param type device type
766 * @param num device number
767 * @return NULL if requested device does not exist
769 struct mtd_device *device_find(u8 type, u8 num)
771 struct list_head *entry;
772 struct mtd_device *dev_tmp;
774 list_for_each(entry, &devices) {
775 dev_tmp = list_entry(entry, struct mtd_device, link);
777 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
785 * Add specified device to the global device list.
787 * @param dev device to be added
789 static void device_add(struct mtd_device *dev)
791 u8 current_save_needed = 0;
793 if (list_empty(&devices)) {
794 current_mtd_dev = dev;
795 current_mtd_partnum = 0;
796 current_save_needed = 1;
799 list_add_tail(&dev->link, &devices);
801 if (current_save_needed > 0)
808 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
809 * return pointer to the device structure.
811 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
812 * @param ret output pointer to next char after parse completes (output)
813 * @param retdev pointer to the allocated device (output)
814 * @return 0 on success, 1 otherwise
816 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
818 struct mtd_device *dev;
819 struct part_info *part;
822 unsigned int mtd_id_len;
823 const char *p, *pend;
825 struct list_head *entry, *n;
830 debug("===device_parse===\n");
839 mtd_id = p = mtd_dev;
840 if (!(p = strchr(mtd_id, ':'))) {
841 printf("no <mtd-id> identifier\n");
844 mtd_id_len = p - mtd_id + 1;
847 /* verify if we have a valid device specified */
848 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
849 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
853 debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
854 id->type, MTD_DEV_TYPE(id->type),
855 id->num, id->mtd_id);
856 pend = strchr(p, ';');
857 debug("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
860 /* parse partitions */
864 if ((dev = device_find(id->type, id->num)) != NULL) {
865 /* if device already exists start at the end of the last partition */
866 part = list_entry(dev->parts.prev, struct part_info, link);
867 offset = part->offset + part->size;
870 while (p && (*p != '\0') && (*p != ';')) {
872 if ((part_parse(p, &p, &part) != 0) || (!part))
875 /* calculate offset when not specified */
876 if (part->offset == OFFSET_NOT_SPECIFIED)
877 part->offset = offset;
879 offset = part->offset;
881 /* verify alignment and size */
882 if (part_validate(id, part) != 0)
885 offset += part->size;
887 /* partition is ok, add it to the list */
888 list_add_tail(&part->link, &tmp_list);
893 part_delall(&tmp_list);
897 if (num_parts == 0) {
898 printf("no partitions for device %s%d (%s)\n",
899 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
903 debug("\ntotal partitions: %d\n", num_parts);
905 /* check for next device presence */
910 } else if (*p == '\0') {
914 printf("unexpected character '%c' at the end of device\n", *p);
921 /* allocate memory for mtd_device structure */
922 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
923 printf("out of memory\n");
926 memset(dev, 0, sizeof(struct mtd_device));
928 dev->num_parts = 0; /* part_sort_add increments num_parts */
929 INIT_LIST_HEAD(&dev->parts);
930 INIT_LIST_HEAD(&dev->link);
932 /* move partitions from tmp_list to dev->parts */
933 list_for_each_safe(entry, n, &tmp_list) {
934 part = list_entry(entry, struct part_info, link);
936 if (part_sort_add(dev, part) != 0) {
949 * Initialize global device list.
951 * @return 0 on success, 1 otherwise
953 static int mtd_devices_init(void)
955 last_parts[0] = '\0';
956 current_mtd_dev = NULL;
959 return device_delall(&devices);
963 * Search global mtdids list and find id of requested type and number.
965 * @return pointer to the id if it exists, NULL otherwise
967 static struct mtdids* id_find(u8 type, u8 num)
969 struct list_head *entry;
972 list_for_each(entry, &mtdids) {
973 id = list_entry(entry, struct mtdids, link);
975 if ((id->type == type) && (id->num == num))
983 * Search global mtdids list and find id of a requested mtd_id.
985 * Note: first argument is not null terminated.
987 * @param mtd_id string containing requested mtd_id
988 * @param mtd_id_len length of supplied mtd_id
989 * @return pointer to the id if it exists, NULL otherwise
991 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
993 struct list_head *entry;
996 debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
997 mtd_id_len, mtd_id, mtd_id_len);
999 list_for_each(entry, &mtdids) {
1000 id = list_entry(entry, struct mtdids, link);
1002 debug("entry: '%s' (len = %d)\n",
1003 id->mtd_id, strlen(id->mtd_id));
1005 if (mtd_id_len != strlen(id->mtd_id))
1007 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1015 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1016 * return device type and number.
1018 * @param id string describing device id
1019 * @param ret_id output pointer to next char after parse completes (output)
1020 * @param dev_type parsed device type (output)
1021 * @param dev_num parsed device number (output)
1022 * @return 0 on success, 1 otherwise
1024 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1029 if (strncmp(p, "nand", 4) == 0) {
1030 *dev_type = MTD_DEV_TYPE_NAND;
1032 } else if (strncmp(p, "nor", 3) == 0) {
1033 *dev_type = MTD_DEV_TYPE_NOR;
1035 } else if (strncmp(p, "onenand", 7) == 0) {
1036 *dev_type = MTD_DEV_TYPE_ONENAND;
1039 printf("incorrect device type in %s\n", id);
1044 printf("incorrect device number in %s\n", id);
1048 *dev_num = simple_strtoul(p, (char **)&p, 0);
1055 * Process all devices and generate corresponding mtdparts string describing
1056 * all partitions on all devices.
1058 * @param buf output buffer holding generated mtdparts string (output)
1059 * @param buflen buffer size
1060 * @return 0 on success, 1 otherwise
1062 static int generate_mtdparts(char *buf, u32 buflen)
1064 struct list_head *pentry, *dentry;
1065 struct mtd_device *dev;
1066 struct part_info *part, *prev_part;
1069 u32 size, offset, len, part_cnt;
1070 u32 maxlen = buflen - 1;
1072 debug("--- generate_mtdparts ---\n");
1074 if (list_empty(&devices)) {
1079 sprintf(p, "mtdparts=");
1082 list_for_each(dentry, &devices) {
1083 dev = list_entry(dentry, struct mtd_device, link);
1086 len = strlen(dev->id->mtd_id) + 1;
1089 memcpy(p, dev->id->mtd_id, len - 1);
1094 /* format partitions */
1097 list_for_each(pentry, &dev->parts) {
1098 part = list_entry(pentry, struct part_info, link);
1100 offset = part->offset;
1103 /* partition size */
1104 memsize_format(tmpbuf, size);
1105 len = strlen(tmpbuf);
1108 memcpy(p, tmpbuf, len);
1113 /* add offset only when there is a gap between
1115 if ((!prev_part && (offset != 0)) ||
1116 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1118 memsize_format(tmpbuf, offset);
1119 len = strlen(tmpbuf) + 1;
1123 memcpy(p, tmpbuf, len - 1);
1128 /* copy name only if user supplied */
1129 if(!part->auto_name) {
1130 len = strlen(part->name) + 2;
1135 memcpy(p, part->name, len - 2);
1142 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1151 /* print ',' separator if there are other partitions
1153 if (dev->num_parts > part_cnt) {
1161 /* print ';' separator if there are other devices following */
1162 if (dentry->next != &devices) {
1170 /* we still have at least one char left, as we decremented maxlen at
1177 last_parts[0] = '\0';
1182 * Call generate_mtdparts to process all devices and generate corresponding
1183 * mtdparts string, save it in mtdparts environment variable.
1185 * @param buf output buffer holding generated mtdparts string (output)
1186 * @param buflen buffer size
1187 * @return 0 on success, 1 otherwise
1189 static int generate_mtdparts_save(char *buf, u32 buflen)
1193 ret = generate_mtdparts(buf, buflen);
1195 if ((buf[0] != '\0') && (ret == 0))
1196 setenv("mtdparts", buf);
1198 setenv("mtdparts", NULL);
1204 * Format and print out a partition list for each device from global device
1207 static void list_partitions(void)
1209 struct list_head *dentry, *pentry;
1210 struct part_info *part;
1211 struct mtd_device *dev;
1214 debug("\n---list_partitions---\n");
1215 list_for_each(dentry, &devices) {
1216 dev = list_entry(dentry, struct mtd_device, link);
1217 printf("\ndevice %s%d <%s>, # parts = %d\n",
1218 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1219 dev->id->mtd_id, dev->num_parts);
1220 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
1222 /* list partitions for given device */
1224 list_for_each(pentry, &dev->parts) {
1225 part = list_entry(pentry, struct part_info, link);
1226 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1227 part_num, part->name, part->size,
1228 part->offset, part->mask_flags);
1233 if (list_empty(&devices))
1234 printf("no partitions defined\n");
1236 /* current_mtd_dev is not NULL only when we have non empty device list */
1237 if (current_mtd_dev) {
1238 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
1240 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
1241 MTD_DEV_TYPE(current_mtd_dev->id->type),
1242 current_mtd_dev->id->num, current_mtd_partnum,
1243 part->name, part->size, part->offset);
1245 printf("could not get current partition info\n\n");
1249 printf("\ndefaults:\n");
1250 printf("mtdids : %s\n",
1251 mtdids_default ? mtdids_default : "none");
1253 * Using printf() here results in printbuffer overflow
1254 * if default mtdparts string is greater than console
1255 * printbuffer. Use puts() to prevent system crashes.
1258 puts(mtdparts_default ? mtdparts_default : "none");
1263 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1264 * corresponding device and verify partition number.
1266 * @param id string describing device and partition or partition name
1267 * @param dev pointer to the requested device (output)
1268 * @param part_num verified partition number (output)
1269 * @param part pointer to requested partition (output)
1270 * @return 0 on success, 1 otherwise
1272 int find_dev_and_part(const char *id, struct mtd_device **dev,
1273 u8 *part_num, struct part_info **part)
1275 struct list_head *dentry, *pentry;
1276 u8 type, dnum, pnum;
1279 debug("--- find_dev_and_part ---\nid = %s\n", id);
1281 list_for_each(dentry, &devices) {
1283 *dev = list_entry(dentry, struct mtd_device, link);
1284 list_for_each(pentry, &(*dev)->parts) {
1285 *part = list_entry(pentry, struct part_info, link);
1286 if (strcmp((*part)->name, id) == 0)
1297 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1300 if ((*p++ != ',') || (*p == '\0')) {
1301 printf("no partition number specified\n");
1304 pnum = simple_strtoul(p, (char **)&p, 0);
1306 printf("unexpected trailing character '%c'\n", *p);
1310 if ((*dev = device_find(type, dnum)) == NULL) {
1311 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1315 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1316 printf("no such partition\n");
1327 * Find and delete partition. For partition id format see find_dev_and_part().
1329 * @param id string describing device and partition
1330 * @return 0 on success, 1 otherwise
1332 static int delete_partition(const char *id)
1335 struct mtd_device *dev;
1336 struct part_info *part;
1338 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1340 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08x@0x%08x\n",
1341 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1342 part->name, part->size, part->offset);
1344 if (part_del(dev, part) != 0)
1347 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1348 printf("generated mtdparts too long, reseting to null\n");
1354 printf("partition %s not found\n", id);
1359 * Accept character string describing mtd partitions and call device_parse()
1360 * for each entry. Add created devices to the global devices list.
1362 * @param mtdparts string specifing mtd partitions
1363 * @return 0 on success, 1 otherwise
1365 static int parse_mtdparts(const char *const mtdparts)
1367 const char *p = mtdparts;
1368 struct mtd_device *dev;
1371 debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1373 /* delete all devices and partitions */
1374 if (mtd_devices_init() != 0) {
1375 printf("could not initialise device list\n");
1379 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1380 p = getenv("mtdparts");
1382 if (strncmp(p, "mtdparts=", 9) != 0) {
1383 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1388 while (p && (*p != '\0')) {
1390 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1393 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1394 dev->id->num, dev->id->mtd_id);
1396 /* check if parsed device is already on the list */
1397 if (device_find(dev->id->type, dev->id->num) != NULL) {
1398 printf("device %s%d redefined, please correct mtdparts variable\n",
1399 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1403 list_add_tail(&dev->link, &devices);
1407 device_delall(&devices);
1415 * Parse provided string describing mtdids mapping (see file header for mtdids
1416 * variable format). Allocate memory for each entry and add all found entries
1417 * to the global mtdids list.
1419 * @param ids mapping string
1420 * @return 0 on success, 1 otherwise
1422 static int parse_mtdids(const char *const ids)
1424 const char *p = ids;
1428 struct list_head *entry, *n;
1429 struct mtdids *id_tmp;
1434 debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1436 /* clean global mtdids list */
1437 list_for_each_safe(entry, n, &mtdids) {
1438 id_tmp = list_entry(entry, struct mtdids, link);
1439 debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1444 INIT_LIST_HEAD(&mtdids);
1446 while(p && (*p != '\0')) {
1449 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1450 if (mtd_id_parse(p, &p, &type, &num) != 0)
1454 printf("mtdids: incorrect <dev-num>\n");
1459 /* check if requested device exists */
1460 if (mtd_device_validate(type, num, &size) != 0)
1463 /* locate <mtd-id> */
1465 if ((p = strchr(mtd_id, ',')) != NULL) {
1466 mtd_id_len = p - mtd_id + 1;
1469 mtd_id_len = strlen(mtd_id) + 1;
1471 if (mtd_id_len == 0) {
1472 printf("mtdids: no <mtd-id> identifier\n");
1476 /* check if this id is already on the list */
1477 int double_entry = 0;
1478 list_for_each(entry, &mtdids) {
1479 id_tmp = list_entry(entry, struct mtdids, link);
1480 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1486 printf("device id %s%d redefined, please correct mtdids variable\n",
1487 MTD_DEV_TYPE(type), num);
1491 /* allocate mtdids structure */
1492 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1493 printf("out of memory\n");
1496 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1500 id->mtd_id = (char *)(id + 1);
1501 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1502 id->mtd_id[mtd_id_len - 1] = '\0';
1503 INIT_LIST_HEAD(&id->link);
1505 debug("+ id %s%d\t%16d bytes\t%s\n",
1506 MTD_DEV_TYPE(id->type), id->num,
1507 id->size, id->mtd_id);
1509 list_add_tail(&id->link, &mtdids);
1513 /* clean mtdids list and free allocated memory */
1514 list_for_each_safe(entry, n, &mtdids) {
1515 id_tmp = list_entry(entry, struct mtdids, link);
1526 * Parse and initialize global mtdids mapping and create global
1527 * device/partition list.
1529 * @return 0 on success, 1 otherwise
1531 int mtdparts_init(void)
1533 static int initialized = 0;
1534 const char *ids, *parts;
1535 const char *current_partition;
1537 char tmp_ep[PARTITION_MAXLEN];
1539 debug("\n---mtdparts_init---\n");
1541 INIT_LIST_HEAD(&mtdids);
1542 INIT_LIST_HEAD(&devices);
1543 memset(last_ids, 0, MTDIDS_MAXLEN);
1544 memset(last_parts, 0, MTDPARTS_MAXLEN);
1545 memset(last_partition, 0, PARTITION_MAXLEN);
1550 ids = getenv("mtdids");
1551 parts = getenv("mtdparts");
1552 current_partition = getenv("partition");
1554 /* save it for later parsing, cannot rely on current partition pointer
1555 * as 'partition' variable may be updated during init */
1557 if (current_partition)
1558 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1560 debug("last_ids : %s\n", last_ids);
1561 debug("env_ids : %s\n", ids);
1562 debug("last_parts: %s\n", last_parts);
1563 debug("env_parts : %s\n\n", parts);
1565 debug("last_partition : %s\n", last_partition);
1566 debug("env_partition : %s\n", current_partition);
1568 /* if mtdids varible is empty try to use defaults */
1570 if (mtdids_default) {
1571 debug("mtdids variable not defined, using default\n");
1572 ids = mtdids_default;
1573 setenv("mtdids", (char *)ids);
1575 printf("mtdids not defined, no default present\n");
1579 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1580 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1584 /* do no try to use defaults when mtdparts variable is not defined,
1585 * just check the length */
1587 printf("mtdparts variable not set, see 'help mtdparts'\n");
1589 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1590 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1594 /* check if we have already parsed those mtdids */
1595 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1600 if (parse_mtdids(ids) != 0) {
1605 /* ok it's good, save new ids */
1606 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1609 /* parse partitions if either mtdparts or mtdids were updated */
1610 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1611 if (parse_mtdparts(parts) != 0)
1614 if (list_empty(&devices)) {
1615 printf("mtdparts_init: no valid partitions\n");
1619 /* ok it's good, save new parts */
1620 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1622 /* reset first partition from first dev from the list as current */
1623 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1624 current_mtd_partnum = 0;
1627 debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
1628 MTD_DEV_TYPE(current_mtd_dev->id->type),
1629 current_mtd_dev->id->num, current_mtd_partnum);
1632 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1633 if (!parts && (last_parts[0] != '\0'))
1634 return mtd_devices_init();
1636 /* do not process current partition if mtdparts variable is null */
1640 /* is current partition set in environment? if so, use it */
1641 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1642 struct part_info *p;
1643 struct mtd_device *cdev;
1646 debug("--- getting current partition: %s\n", tmp_ep);
1648 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1649 current_mtd_dev = cdev;
1650 current_mtd_partnum = pnum;
1653 } else if (getenv("partition") == NULL) {
1654 debug("no partition variable set, setting...\n");
1662 * Return pointer to the partition of a requested number from a requested
1665 * @param dev device that is to be searched for a partition
1666 * @param part_num requested partition number
1667 * @return pointer to the part_info, NULL otherwise
1669 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1671 struct list_head *entry;
1672 struct part_info *part;
1678 debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1679 part_num, MTD_DEV_TYPE(dev->id->type),
1680 dev->id->num, dev->id->mtd_id);
1682 if (part_num >= dev->num_parts) {
1683 printf("invalid partition number %d for device %s%d (%s)\n",
1684 part_num, MTD_DEV_TYPE(dev->id->type),
1685 dev->id->num, dev->id->mtd_id);
1689 /* locate partition number, return it */
1691 list_for_each(entry, &dev->parts) {
1692 part = list_entry(entry, struct part_info, link);
1694 if (part_num == num++) {
1702 /***************************************************/
1703 /* U-boot commands */
1704 /***************************************************/
1705 /* command line only */
1707 * Routine implementing u-boot chpart command. Sets new current partition based
1708 * on the user supplied partition id. For partition id format see find_dev_and_part().
1710 * @param cmdtp command internal data
1711 * @param flag command flag
1712 * @param argc number of arguments supplied to the command
1713 * @param argv arguments list
1714 * @return 0 on success, 1 otherwise
1716 int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1718 /* command line only */
1719 struct mtd_device *dev;
1720 struct part_info *part;
1723 if (mtdparts_init() !=0)
1727 printf("no partition id specified\n");
1731 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1734 current_mtd_dev = dev;
1735 current_mtd_partnum = pnum;
1738 printf("partition changed to %s%d,%d\n",
1739 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1745 * Routine implementing u-boot mtdparts command. Initialize/update default global
1746 * partition list and process user partition request (list, add, del).
1748 * @param cmdtp command internal data
1749 * @param flag command flag
1750 * @param argc number of arguments supplied to the command
1751 * @param argv arguments list
1752 * @return 0 on success, 1 otherwise
1754 int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1757 if (strcmp(argv[1], "default") == 0) {
1758 setenv("mtdids", (char *)mtdids_default);
1759 setenv("mtdparts", (char *)mtdparts_default);
1760 setenv("partition", NULL);
1764 } else if (strcmp(argv[1], "delall") == 0) {
1765 /* this may be the first run, initialize lists if needed */
1768 setenv("mtdparts", NULL);
1770 /* mtd_devices_init() calls current_save() */
1771 return mtd_devices_init();
1775 /* make sure we are in sync with env variables */
1776 if (mtdparts_init() != 0)
1784 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
1785 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
1786 #define PART_ADD_DESC_MAXLEN 64
1787 char tmpbuf[PART_ADD_DESC_MAXLEN];
1789 struct mtd_device *dev;
1790 struct mtd_device *dev_tmp;
1792 struct part_info *p;
1794 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
1797 if ((id = id_find(type, num)) == NULL) {
1798 printf("no such device %s defined in mtdids variable\n", argv[2]);
1802 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
1803 len += strlen(argv[3]); /* size@offset */
1804 len += strlen(argv[4]) + 2; /* '(' name ')' */
1805 if (argv[5] && (strlen(argv[5]) == 2))
1806 len += 2; /* 'ro' */
1808 if (len >= PART_ADD_DESC_MAXLEN) {
1809 printf("too long partition description\n");
1812 sprintf(tmpbuf, "%s:%s(%s)%s",
1813 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
1814 debug("add tmpbuf: %s\n", tmpbuf);
1816 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
1819 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1820 dev->id->num, dev->id->mtd_id);
1822 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
1825 /* merge new partition with existing ones*/
1826 p = list_entry(dev->parts.next, struct part_info, link);
1827 if (part_add(dev_tmp, p) != 0) {
1833 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1834 printf("generated mtdparts too long, reseting to null\n");
1841 /* mtdparts del part-id */
1842 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
1843 debug("del: part-id = %s\n", argv[2]);
1845 return delete_partition(argv[2]);
1848 return cmd_usage(cmdtp);
1851 /***************************************************/
1853 chpart, 2, 0, do_chpart,
1854 "change active partition",
1856 " - change active partition (e.g. part-id = nand0,1)"
1860 mtdparts, 6, 0, do_mtdparts,
1861 "define flash/nand partitions",
1863 " - list partition table\n"
1865 " - delete all partitions\n"
1866 "mtdparts del part-id\n"
1867 " - delete partition (e.g. part-id = nand0,1)\n"
1868 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
1869 " - add partition\n"
1870 "mtdparts default\n"
1871 " - reset partition table to defaults\n\n"
1873 "this command uses three environment variables:\n\n"
1874 "'partition' - keeps current partition identifier\n\n"
1875 "partition := <part-id>\n"
1876 "<part-id> := <dev-id>,part_num\n\n"
1877 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
1878 "mtdids=<idmap>[,<idmap>,...]\n\n"
1879 "<idmap> := <dev-id>=<mtd-id>\n"
1880 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
1881 "<dev-num> := mtd device number, 0...\n"
1882 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
1883 "'mtdparts' - partition list\n\n"
1884 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
1885 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
1886 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
1887 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
1888 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
1889 "<offset> := partition start offset within the device\n"
1890 "<name> := '(' NAME ')'\n"
1891 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)"
1893 /***************************************************/