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 /* enable/disable debugging messages */
107 #define DEBUG_MTDPARTS
108 #undef DEBUG_MTDPARTS
110 #ifdef DEBUG_MTDPARTS
111 # define DEBUGF(fmt, args...) printf(fmt ,##args)
113 # define DEBUGF(fmt, args...)
116 /* special size referring to all the remaining space in a partition */
117 #define SIZE_REMAINING 0xFFFFFFFF
119 /* special offset value, it is used when not provided by user
121 * this value is used temporarily during parsing, later such offests
122 * are recalculated */
123 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
125 /* minimum partition size */
126 #define MIN_PART_SIZE 4096
128 /* this flag needs to be set in part_info struct mask_flags
129 * field for read-only partitions */
130 #define MTD_WRITEABLE_CMD 1
132 /* default values for mtdids and mtdparts variables */
133 #if defined(MTDIDS_DEFAULT)
134 static const char *const mtdids_default = MTDIDS_DEFAULT;
136 static const char *const mtdids_default = NULL;
139 #if defined(MTDPARTS_DEFAULT)
140 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
142 static const char *const mtdparts_default = NULL;
145 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
146 #define MTDIDS_MAXLEN 128
147 #define MTDPARTS_MAXLEN 512
148 #define PARTITION_MAXLEN 16
149 static char last_ids[MTDIDS_MAXLEN];
150 static char last_parts[MTDPARTS_MAXLEN];
151 static char last_partition[PARTITION_MAXLEN];
153 /* low level jffs2 cache cleaning routine */
154 extern void jffs2_free_cache(struct part_info *part);
156 /* mtdids mapping list, filled by parse_ids() */
157 struct list_head mtdids;
159 /* device/partition list, parse_cmdline() parses into here */
160 struct list_head devices;
162 /* current active device and partition number */
163 struct mtd_device *current_mtd_dev = NULL;
164 u8 current_mtd_partnum = 0;
166 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
168 /* command line only routines */
169 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
170 static int device_del(struct mtd_device *dev);
173 * Parses a string into a number. The number stored at ptr is
174 * potentially suffixed with K (for kilobytes, or 1024 bytes),
175 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
176 * 1073741824). If the number is suffixed with K, M, or G, then
177 * the return value is the number multiplied by one kilobyte, one
178 * megabyte, or one gigabyte, respectively.
180 * @param ptr where parse begins
181 * @param retptr output pointer to next char after parse completes (output)
182 * @return resulting unsigned int
184 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
186 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
207 * Format string describing supplied size. This routine does the opposite job
208 * to memsize_parse(). Size in bytes is converted to string and if possible
209 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
211 * Note, that this routine does not check for buffer overflow, it's the caller
212 * who must assure enough space.
214 * @param buf output buffer
215 * @param size size to be converted to string
217 static void memsize_format(char *buf, u32 size)
219 #define SIZE_GB ((u32)1024*1024*1024)
220 #define SIZE_MB ((u32)1024*1024)
221 #define SIZE_KB ((u32)1024)
223 if ((size % SIZE_GB) == 0)
224 sprintf(buf, "%ug", size/SIZE_GB);
225 else if ((size % SIZE_MB) == 0)
226 sprintf(buf, "%um", size/SIZE_MB);
227 else if (size % SIZE_KB == 0)
228 sprintf(buf, "%uk", size/SIZE_KB);
230 sprintf(buf, "%u", size);
234 * This routine does global indexing of all partitions. Resulting index for
235 * current partition is saved in 'mtddevnum'. Current partition name in
238 static void index_partitions(void)
242 struct part_info *part;
243 struct list_head *dentry;
244 struct mtd_device *dev;
246 DEBUGF("--- index partitions ---\n");
248 if (current_mtd_dev) {
250 list_for_each(dentry, &devices) {
251 dev = list_entry(dentry, struct mtd_device, link);
252 if (dev == current_mtd_dev) {
253 mtddevnum += current_mtd_partnum;
254 sprintf(buf, "%d", mtddevnum);
255 setenv("mtddevnum", buf);
258 mtddevnum += dev->num_parts;
261 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
262 setenv("mtddevname", part->name);
264 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
266 setenv("mtddevnum", NULL);
267 setenv("mtddevname", NULL);
269 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
274 * Save current device and partition in environment variable 'partition'.
276 static void current_save(void)
280 DEBUGF("--- current_save ---\n");
282 if (current_mtd_dev) {
283 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
284 current_mtd_dev->id->num, current_mtd_partnum);
286 setenv("partition", buf);
287 strncpy(last_partition, buf, 16);
289 DEBUGF("=> partition %s\n", buf);
291 setenv("partition", NULL);
292 last_partition[0] = '\0';
294 DEBUGF("=> partition NULL\n");
300 * Performs sanity check for supplied flash partition.
301 * Table of existing MTD flash devices is searched and partition device
302 * is located. Alignment with the granularity of nand erasesize is verified.
304 * @param id of the parent device
305 * @param part partition to validate
306 * @return 0 if partition is valid, 1 otherwise
308 static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
310 struct mtd_info *mtd;
315 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(id->type), id->num);
316 mtd = get_mtd_device_nm(mtd_dev);
318 printf("Partition %s not found on device %s!\n", part->name, mtd_dev);
322 part->sector_size = mtd->erasesize;
324 if (!mtd->numeraseregions) {
326 * Only one eraseregion (NAND, OneNAND or uniform NOR),
327 * checking for alignment is easy here
329 if ((unsigned long)part->offset % mtd->erasesize) {
330 printf("%s%d: partition (%s) start offset"
331 "alignment incorrect\n",
332 MTD_DEV_TYPE(id->type), id->num, part->name);
336 if (part->size % mtd->erasesize) {
337 printf("%s%d: partition (%s) size alignment incorrect\n",
338 MTD_DEV_TYPE(id->type), id->num, part->name);
343 * Multiple eraseregions (non-uniform NOR),
344 * checking for alignment is more complex here
347 /* Check start alignment */
348 for (i = 0; i < mtd->numeraseregions; i++) {
349 start = mtd->eraseregions[i].offset;
350 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
351 if (part->offset == start)
353 start += mtd->eraseregions[i].erasesize;
357 printf("%s%d: partition (%s) start offset alignment incorrect\n",
358 MTD_DEV_TYPE(id->type), id->num, part->name);
363 /* Check end/size alignment */
364 for (i = 0; i < mtd->numeraseregions; i++) {
365 start = mtd->eraseregions[i].offset;
366 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
367 if ((part->offset + part->size) == start)
369 start += mtd->eraseregions[i].erasesize;
372 /* Check last sector alignment */
373 if ((part->offset + part->size) == start)
376 printf("%s%d: partition (%s) size alignment incorrect\n",
377 MTD_DEV_TYPE(id->type), id->num, part->name);
389 * Performs sanity check for supplied partition. Offset and size are verified
390 * to be within valid range. Partition type is checked and either
391 * parts_validate_nor() or parts_validate_nand() is called with the argument
394 * @param id of the parent device
395 * @param part partition to validate
396 * @return 0 if partition is valid, 1 otherwise
398 static int part_validate(struct mtdids *id, struct part_info *part)
400 if (part->size == SIZE_REMAINING)
401 part->size = id->size - part->offset;
403 if (part->offset > id->size) {
404 printf("%s: offset %08x beyond flash size %08x\n",
405 id->mtd_id, part->offset, id->size);
409 if ((part->offset + part->size) <= part->offset) {
410 printf("%s%d: partition (%s) size too big\n",
411 MTD_DEV_TYPE(id->type), id->num, part->name);
415 if (part->offset + part->size > id->size) {
416 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
421 * Now we need to check if the partition starts and ends on
422 * sector (eraseblock) regions
424 return part_validate_eraseblock(id, part);
428 * Delete selected partition from the partion list of the specified device.
430 * @param dev device to delete partition from
431 * @param part partition to delete
432 * @return 0 on success, 1 otherwise
434 static int part_del(struct mtd_device *dev, struct part_info *part)
436 u8 current_save_needed = 0;
438 /* if there is only one partition, remove whole device */
439 if (dev->num_parts == 1)
440 return device_del(dev);
442 /* otherwise just delete this partition */
444 if (dev == current_mtd_dev) {
445 /* we are modyfing partitions for the current device,
447 struct part_info *curr_pi;
448 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
451 if (curr_pi == part) {
452 printf("current partition deleted, resetting current to 0\n");
453 current_mtd_partnum = 0;
454 } else if (part->offset <= curr_pi->offset) {
455 current_mtd_partnum--;
457 current_save_needed = 1;
461 list_del(&part->link);
465 if (current_save_needed > 0)
474 * Delete all partitions from parts head list, free memory.
476 * @param head list of partitions to delete
478 static void part_delall(struct list_head *head)
480 struct list_head *entry, *n;
481 struct part_info *part_tmp;
483 /* clean tmp_list and free allocated memory */
484 list_for_each_safe(entry, n, head) {
485 part_tmp = list_entry(entry, struct part_info, link);
493 * Add new partition to the supplied partition list. Make sure partitions are
494 * sorted by offset in ascending order.
496 * @param head list this partition is to be added to
497 * @param new partition to be added
499 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
501 struct list_head *entry;
502 struct part_info *new_pi, *curr_pi;
504 /* link partition to parrent dev */
507 if (list_empty(&dev->parts)) {
508 DEBUGF("part_sort_add: list empty\n");
509 list_add(&part->link, &dev->parts);
515 new_pi = list_entry(&part->link, struct part_info, link);
517 /* get current partition info if we are updating current device */
519 if (dev == current_mtd_dev)
520 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
522 list_for_each(entry, &dev->parts) {
523 struct part_info *pi;
525 pi = list_entry(entry, struct part_info, link);
527 /* be compliant with kernel cmdline, allow only one partition at offset zero */
528 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
529 printf("cannot add second partition at offset 0\n");
533 if (new_pi->offset <= pi->offset) {
534 list_add_tail(&part->link, entry);
537 if (curr_pi && (pi->offset <= curr_pi->offset)) {
538 /* we are modyfing partitions for the current
539 * device, update current */
540 current_mtd_partnum++;
549 list_add_tail(&part->link, &dev->parts);
556 * Add provided partition to the partition list of a given device.
558 * @param dev device to which partition is added
559 * @param part partition to be added
560 * @return 0 on success, 1 otherwise
562 static int part_add(struct mtd_device *dev, struct part_info *part)
564 /* verify alignment and size */
565 if (part_validate(dev->id, part) != 0)
568 /* partition is ok, add it to the list */
569 if (part_sort_add(dev, part) != 0)
576 * Parse one partition definition, allocate memory and return pointer to this
577 * location in retpart.
579 * @param partdef pointer to the partition definition string i.e. <part-def>
580 * @param ret output pointer to next char after parse completes (output)
581 * @param retpart pointer to the allocated partition (output)
582 * @return 0 on success, 1 otherwise
584 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
586 struct part_info *part;
588 unsigned long offset;
591 unsigned int mask_flags;
598 /* fetch the partition size */
600 /* assign all remaining space to this partition */
601 DEBUGF("'-': remaining size assigned\n");
602 size = SIZE_REMAINING;
605 size = memsize_parse(p, &p);
606 if (size < MIN_PART_SIZE) {
607 printf("partition size too small (%lx)\n", size);
612 /* check for offset */
613 offset = OFFSET_NOT_SPECIFIED;
616 offset = memsize_parse(p, &p);
619 /* now look for the name */
622 if ((p = strchr(name, ')')) == NULL) {
623 printf("no closing ) found in partition name\n");
626 name_len = p - name + 1;
627 if ((name_len - 1) == 0) {
628 printf("empty partition name\n");
633 /* 0x00000000@0x00000000 */
638 /* test for options */
640 if (strncmp(p, "ro", 2) == 0) {
641 mask_flags |= MTD_WRITEABLE_CMD;
645 /* check for next partition definition */
647 if (size == SIZE_REMAINING) {
649 printf("no partitions allowed after a fill-up partition\n");
653 } else if ((*p == ';') || (*p == '\0')) {
656 printf("unexpected character '%c' at the end of partition\n", *p);
661 /* allocate memory */
662 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
664 printf("out of memory\n");
667 memset(part, 0, sizeof(struct part_info) + name_len);
669 part->offset = offset;
670 part->mask_flags = mask_flags;
671 part->name = (char *)(part + 1);
674 /* copy user provided name */
675 strncpy(part->name, name, name_len - 1);
678 /* auto generated name in form of size@offset */
679 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
683 part->name[name_len - 1] = '\0';
684 INIT_LIST_HEAD(&part->link);
686 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
687 part->name, part->size,
688 part->offset, part->mask_flags);
695 * Check device number to be within valid range for given device type.
697 * @param dev device to validate
698 * @return 0 if device is valid, 1 otherwise
700 int mtd_device_validate(u8 type, u8 num, u32 *size)
702 struct mtd_info *mtd;
705 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
706 mtd = get_mtd_device_nm(mtd_dev);
708 printf("Device %s not found!\n", mtd_dev);
718 * Delete all mtd devices from a supplied devices list, free memory allocated for
719 * each device and delete all device partitions.
721 * @return 0 on success, 1 otherwise
723 static int device_delall(struct list_head *head)
725 struct list_head *entry, *n;
726 struct mtd_device *dev_tmp;
728 /* clean devices list */
729 list_for_each_safe(entry, n, head) {
730 dev_tmp = list_entry(entry, struct mtd_device, link);
732 part_delall(&dev_tmp->parts);
735 INIT_LIST_HEAD(&devices);
741 * If provided device exists it's partitions are deleted, device is removed
742 * from device list and device memory is freed.
744 * @param dev device to be deleted
745 * @return 0 on success, 1 otherwise
747 static int device_del(struct mtd_device *dev)
749 part_delall(&dev->parts);
750 list_del(&dev->link);
753 if (dev == current_mtd_dev) {
754 /* we just deleted current device */
755 if (list_empty(&devices)) {
756 current_mtd_dev = NULL;
758 /* reset first partition from first dev from the
759 * devices list as current */
760 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
761 current_mtd_partnum = 0;
772 * Search global device list and return pointer to the device of type and num
775 * @param type device type
776 * @param num device number
777 * @return NULL if requested device does not exist
779 static struct mtd_device* device_find(u8 type, u8 num)
781 struct list_head *entry;
782 struct mtd_device *dev_tmp;
784 list_for_each(entry, &devices) {
785 dev_tmp = list_entry(entry, struct mtd_device, link);
787 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
795 * Add specified device to the global device list.
797 * @param dev device to be added
799 static void device_add(struct mtd_device *dev)
801 u8 current_save_needed = 0;
803 if (list_empty(&devices)) {
804 current_mtd_dev = dev;
805 current_mtd_partnum = 0;
806 current_save_needed = 1;
809 list_add_tail(&dev->link, &devices);
811 if (current_save_needed > 0)
818 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
819 * return pointer to the device structure.
821 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
822 * @param ret output pointer to next char after parse completes (output)
823 * @param retdev pointer to the allocated device (output)
824 * @return 0 on success, 1 otherwise
826 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
828 struct mtd_device *dev;
829 struct part_info *part;
832 unsigned int mtd_id_len;
833 const char *p, *pend;
835 struct list_head *entry, *n;
844 DEBUGF("===device_parse===\n");
848 if (!(p = strchr(mtd_id, ':'))) {
849 printf("no <mtd-id> identifier\n");
852 mtd_id_len = p - mtd_id + 1;
855 /* verify if we have a valid device specified */
856 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
857 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
861 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
862 id->type, MTD_DEV_TYPE(id->type),
863 id->num, id->mtd_id);
864 pend = strchr(p, ';');
865 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
868 /* parse partitions */
872 if ((dev = device_find(id->type, id->num)) != NULL) {
873 /* if device already exists start at the end of the last partition */
874 part = list_entry(dev->parts.prev, struct part_info, link);
875 offset = part->offset + part->size;
878 while (p && (*p != '\0') && (*p != ';')) {
880 if ((part_parse(p, &p, &part) != 0) || (!part))
883 /* calculate offset when not specified */
884 if (part->offset == OFFSET_NOT_SPECIFIED)
885 part->offset = offset;
887 offset = part->offset;
889 /* verify alignment and size */
890 if (part_validate(id, part) != 0)
893 offset += part->size;
895 /* partition is ok, add it to the list */
896 list_add_tail(&part->link, &tmp_list);
901 part_delall(&tmp_list);
905 if (num_parts == 0) {
906 printf("no partitions for device %s%d (%s)\n",
907 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
911 DEBUGF("\ntotal partitions: %d\n", num_parts);
913 /* check for next device presence */
917 } else if (*p == '\0') {
920 printf("unexpected character '%c' at the end of device\n", *p);
926 /* allocate memory for mtd_device structure */
927 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
928 printf("out of memory\n");
931 memset(dev, 0, sizeof(struct mtd_device));
933 dev->num_parts = 0; /* part_sort_add increments num_parts */
934 INIT_LIST_HEAD(&dev->parts);
935 INIT_LIST_HEAD(&dev->link);
937 /* move partitions from tmp_list to dev->parts */
938 list_for_each_safe(entry, n, &tmp_list) {
939 part = list_entry(entry, struct part_info, link);
941 if (part_sort_add(dev, part) != 0) {
954 * Initialize global device list.
956 * @return 0 on success, 1 otherwise
958 static int mtd_devices_init(void)
960 last_parts[0] = '\0';
961 current_mtd_dev = NULL;
964 return device_delall(&devices);
968 * Search global mtdids list and find id of requested type and number.
970 * @return pointer to the id if it exists, NULL otherwise
972 static struct mtdids* id_find(u8 type, u8 num)
974 struct list_head *entry;
977 list_for_each(entry, &mtdids) {
978 id = list_entry(entry, struct mtdids, link);
980 if ((id->type == type) && (id->num == num))
988 * Search global mtdids list and find id of a requested mtd_id.
990 * Note: first argument is not null terminated.
992 * @param mtd_id string containing requested mtd_id
993 * @param mtd_id_len length of supplied mtd_id
994 * @return pointer to the id if it exists, NULL otherwise
996 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
998 struct list_head *entry;
1001 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1002 mtd_id_len, mtd_id, mtd_id_len);
1004 list_for_each(entry, &mtdids) {
1005 id = list_entry(entry, struct mtdids, link);
1007 DEBUGF("entry: '%s' (len = %d)\n",
1008 id->mtd_id, strlen(id->mtd_id));
1010 if (mtd_id_len != strlen(id->mtd_id))
1012 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1020 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1021 * return device type and number.
1023 * @param id string describing device id
1024 * @param ret_id output pointer to next char after parse completes (output)
1025 * @param dev_type parsed device type (output)
1026 * @param dev_num parsed device number (output)
1027 * @return 0 on success, 1 otherwise
1029 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1034 if (strncmp(p, "nand", 4) == 0) {
1035 *dev_type = MTD_DEV_TYPE_NAND;
1037 } else if (strncmp(p, "nor", 3) == 0) {
1038 *dev_type = MTD_DEV_TYPE_NOR;
1040 } else if (strncmp(p, "onenand", 7) == 0) {
1041 *dev_type = MTD_DEV_TYPE_ONENAND;
1044 printf("incorrect device type in %s\n", id);
1049 printf("incorrect device number in %s\n", id);
1053 *dev_num = simple_strtoul(p, (char **)&p, 0);
1060 * Process all devices and generate corresponding mtdparts string describing
1061 * all partitions on all devices.
1063 * @param buf output buffer holding generated mtdparts string (output)
1064 * @param buflen buffer size
1065 * @return 0 on success, 1 otherwise
1067 static int generate_mtdparts(char *buf, u32 buflen)
1069 struct list_head *pentry, *dentry;
1070 struct mtd_device *dev;
1071 struct part_info *part, *prev_part;
1074 u32 size, offset, len, part_cnt;
1075 u32 maxlen = buflen - 1;
1077 DEBUGF("--- generate_mtdparts ---\n");
1079 if (list_empty(&devices)) {
1084 sprintf(p, "mtdparts=");
1087 list_for_each(dentry, &devices) {
1088 dev = list_entry(dentry, struct mtd_device, link);
1091 len = strlen(dev->id->mtd_id) + 1;
1094 memcpy(p, dev->id->mtd_id, len - 1);
1099 /* format partitions */
1102 list_for_each(pentry, &dev->parts) {
1103 part = list_entry(pentry, struct part_info, link);
1105 offset = part->offset;
1108 /* partition size */
1109 memsize_format(tmpbuf, size);
1110 len = strlen(tmpbuf);
1113 memcpy(p, tmpbuf, len);
1118 /* add offset only when there is a gap between
1120 if ((!prev_part && (offset != 0)) ||
1121 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1123 memsize_format(tmpbuf, offset);
1124 len = strlen(tmpbuf) + 1;
1128 memcpy(p, tmpbuf, len - 1);
1133 /* copy name only if user supplied */
1134 if(!part->auto_name) {
1135 len = strlen(part->name) + 2;
1140 memcpy(p, part->name, len - 2);
1147 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1156 /* print ',' separator if there are other partitions
1158 if (dev->num_parts > part_cnt) {
1166 /* print ';' separator if there are other devices following */
1167 if (dentry->next != &devices) {
1175 /* we still have at least one char left, as we decremented maxlen at
1182 last_parts[0] = '\0';
1187 * Call generate_mtdparts to process all devices and generate corresponding
1188 * mtdparts string, save it in mtdparts environment variable.
1190 * @param buf output buffer holding generated mtdparts string (output)
1191 * @param buflen buffer size
1192 * @return 0 on success, 1 otherwise
1194 static int generate_mtdparts_save(char *buf, u32 buflen)
1198 ret = generate_mtdparts(buf, buflen);
1200 if ((buf[0] != '\0') && (ret == 0))
1201 setenv("mtdparts", buf);
1203 setenv("mtdparts", NULL);
1209 * Format and print out a partition list for each device from global device
1212 static void list_partitions(void)
1214 struct list_head *dentry, *pentry;
1215 struct part_info *part;
1216 struct mtd_device *dev;
1219 DEBUGF("\n---list_partitions---\n");
1220 list_for_each(dentry, &devices) {
1221 dev = list_entry(dentry, struct mtd_device, link);
1222 printf("\ndevice %s%d <%s>, # parts = %d\n",
1223 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1224 dev->id->mtd_id, dev->num_parts);
1225 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
1227 /* list partitions for given device */
1229 list_for_each(pentry, &dev->parts) {
1230 part = list_entry(pentry, struct part_info, link);
1231 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1232 part_num, part->name, part->size,
1233 part->offset, part->mask_flags);
1238 if (list_empty(&devices))
1239 printf("no partitions defined\n");
1241 /* current_mtd_dev is not NULL only when we have non empty device list */
1242 if (current_mtd_dev) {
1243 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
1245 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
1246 MTD_DEV_TYPE(current_mtd_dev->id->type),
1247 current_mtd_dev->id->num, current_mtd_partnum,
1248 part->name, part->size, part->offset);
1250 printf("could not get current partition info\n\n");
1254 printf("\ndefaults:\n");
1255 printf("mtdids : %s\n",
1256 mtdids_default ? mtdids_default : "none");
1257 printf("mtdparts: %s\n",
1258 mtdparts_default ? mtdparts_default : "none");
1262 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1263 * corresponding device and verify partition number.
1265 * @param id string describing device and partition or partition name
1266 * @param dev pointer to the requested device (output)
1267 * @param part_num verified partition number (output)
1268 * @param part pointer to requested partition (output)
1269 * @return 0 on success, 1 otherwise
1271 int find_dev_and_part(const char *id, struct mtd_device **dev,
1272 u8 *part_num, struct part_info **part)
1274 struct list_head *dentry, *pentry;
1275 u8 type, dnum, pnum;
1278 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1280 list_for_each(dentry, &devices) {
1282 *dev = list_entry(dentry, struct mtd_device, link);
1283 list_for_each(pentry, &(*dev)->parts) {
1284 *part = list_entry(pentry, struct part_info, link);
1285 if (strcmp((*part)->name, id) == 0)
1296 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1299 if ((*p++ != ',') || (*p == '\0')) {
1300 printf("no partition number specified\n");
1303 pnum = simple_strtoul(p, (char **)&p, 0);
1305 printf("unexpected trailing character '%c'\n", *p);
1309 if ((*dev = device_find(type, dnum)) == NULL) {
1310 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1314 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1315 printf("no such partition\n");
1326 * Find and delete partition. For partition id format see find_dev_and_part().
1328 * @param id string describing device and partition
1329 * @return 0 on success, 1 otherwise
1331 static int delete_partition(const char *id)
1334 struct mtd_device *dev;
1335 struct part_info *part;
1337 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1339 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08x@0x%08x\n",
1340 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1341 part->name, part->size, part->offset);
1343 if (part_del(dev, part) != 0)
1346 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1347 printf("generated mtdparts too long, reseting to null\n");
1353 printf("partition %s not found\n", id);
1358 * Accept character string describing mtd partitions and call device_parse()
1359 * for each entry. Add created devices to the global devices list.
1361 * @param mtdparts string specifing mtd partitions
1362 * @return 0 on success, 1 otherwise
1364 static int parse_mtdparts(const char *const mtdparts)
1366 const char *p = mtdparts;
1367 struct mtd_device *dev;
1370 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1372 /* delete all devices and partitions */
1373 if (mtd_devices_init() != 0) {
1374 printf("could not initialise device list\n");
1378 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1379 p = getenv("mtdparts");
1381 if (strncmp(p, "mtdparts=", 9) != 0) {
1382 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1387 while (p && (*p != '\0')) {
1389 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1392 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1393 dev->id->num, dev->id->mtd_id);
1395 /* check if parsed device is already on the list */
1396 if (device_find(dev->id->type, dev->id->num) != NULL) {
1397 printf("device %s%d redefined, please correct mtdparts variable\n",
1398 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1402 list_add_tail(&dev->link, &devices);
1406 device_delall(&devices);
1414 * Parse provided string describing mtdids mapping (see file header for mtdids
1415 * variable format). Allocate memory for each entry and add all found entries
1416 * to the global mtdids list.
1418 * @param ids mapping string
1419 * @return 0 on success, 1 otherwise
1421 static int parse_mtdids(const char *const ids)
1423 const char *p = ids;
1427 struct list_head *entry, *n;
1428 struct mtdids *id_tmp;
1433 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1435 /* clean global mtdids list */
1436 list_for_each_safe(entry, n, &mtdids) {
1437 id_tmp = list_entry(entry, struct mtdids, link);
1438 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1443 INIT_LIST_HEAD(&mtdids);
1445 while(p && (*p != '\0')) {
1448 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1449 if (mtd_id_parse(p, &p, &type, &num) != 0)
1453 printf("mtdids: incorrect <dev-num>\n");
1458 /* check if requested device exists */
1459 if (mtd_device_validate(type, num, &size) != 0)
1462 /* locate <mtd-id> */
1464 if ((p = strchr(mtd_id, ',')) != NULL) {
1465 mtd_id_len = p - mtd_id + 1;
1468 mtd_id_len = strlen(mtd_id) + 1;
1470 if (mtd_id_len == 0) {
1471 printf("mtdids: no <mtd-id> identifier\n");
1475 /* check if this id is already on the list */
1476 int double_entry = 0;
1477 list_for_each(entry, &mtdids) {
1478 id_tmp = list_entry(entry, struct mtdids, link);
1479 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1485 printf("device id %s%d redefined, please correct mtdids variable\n",
1486 MTD_DEV_TYPE(type), num);
1490 /* allocate mtdids structure */
1491 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1492 printf("out of memory\n");
1495 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1499 id->mtd_id = (char *)(id + 1);
1500 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1501 id->mtd_id[mtd_id_len - 1] = '\0';
1502 INIT_LIST_HEAD(&id->link);
1504 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1505 MTD_DEV_TYPE(id->type), id->num,
1506 id->size, id->mtd_id);
1508 list_add_tail(&id->link, &mtdids);
1512 /* clean mtdids list and free allocated memory */
1513 list_for_each_safe(entry, n, &mtdids) {
1514 id_tmp = list_entry(entry, struct mtdids, link);
1525 * Parse and initialize global mtdids mapping and create global
1526 * device/partition list.
1528 * @return 0 on success, 1 otherwise
1530 int mtdparts_init(void)
1532 static int initialized = 0;
1533 const char *ids, *parts;
1534 const char *current_partition;
1536 char tmp_ep[PARTITION_MAXLEN];
1538 DEBUGF("\n---mtdparts_init---\n");
1540 INIT_LIST_HEAD(&mtdids);
1541 INIT_LIST_HEAD(&devices);
1542 memset(last_ids, 0, MTDIDS_MAXLEN);
1543 memset(last_parts, 0, MTDPARTS_MAXLEN);
1544 memset(last_partition, 0, PARTITION_MAXLEN);
1549 ids = getenv("mtdids");
1550 parts = getenv("mtdparts");
1551 current_partition = getenv("partition");
1553 /* save it for later parsing, cannot rely on current partition pointer
1554 * as 'partition' variable may be updated during init */
1556 if (current_partition)
1557 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1559 DEBUGF("last_ids : %s\n", last_ids);
1560 DEBUGF("env_ids : %s\n", ids);
1561 DEBUGF("last_parts: %s\n", last_parts);
1562 DEBUGF("env_parts : %s\n\n", parts);
1564 DEBUGF("last_partition : %s\n", last_partition);
1565 DEBUGF("env_partition : %s\n", current_partition);
1567 /* if mtdids varible is empty try to use defaults */
1569 if (mtdids_default) {
1570 DEBUGF("mtdids variable not defined, using default\n");
1571 ids = mtdids_default;
1572 setenv("mtdids", (char *)ids);
1574 printf("mtdids not defined, no default present\n");
1578 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1579 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1583 /* do no try to use defaults when mtdparts variable is not defined,
1584 * just check the length */
1586 printf("mtdparts variable not set, see 'help mtdparts'\n");
1588 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1589 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1593 /* check if we have already parsed those mtdids */
1594 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1599 if (parse_mtdids(ids) != 0) {
1604 /* ok it's good, save new ids */
1605 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1608 /* parse partitions if either mtdparts or mtdids were updated */
1609 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1610 if (parse_mtdparts(parts) != 0)
1613 if (list_empty(&devices)) {
1614 printf("mtdparts_init: no valid partitions\n");
1618 /* ok it's good, save new parts */
1619 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1621 /* reset first partition from first dev from the list as current */
1622 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1623 current_mtd_partnum = 0;
1626 DEBUGF("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
1627 MTD_DEV_TYPE(current_mtd_dev->id->type),
1628 current_mtd_dev->id->num, current_mtd_partnum);
1631 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1632 if (!parts && (last_parts[0] != '\0'))
1633 return mtd_devices_init();
1635 /* do not process current partition if mtdparts variable is null */
1639 /* is current partition set in environment? if so, use it */
1640 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1641 struct part_info *p;
1642 struct mtd_device *cdev;
1645 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1647 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1648 current_mtd_dev = cdev;
1649 current_mtd_partnum = pnum;
1652 } else if (getenv("partition") == NULL) {
1653 DEBUGF("no partition variable set, setting...\n");
1661 * Return pointer to the partition of a requested number from a requested
1664 * @param dev device that is to be searched for a partition
1665 * @param part_num requested partition number
1666 * @return pointer to the part_info, NULL otherwise
1668 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1670 struct list_head *entry;
1671 struct part_info *part;
1677 DEBUGF("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1678 part_num, MTD_DEV_TYPE(dev->id->type),
1679 dev->id->num, dev->id->mtd_id);
1681 if (part_num >= dev->num_parts) {
1682 printf("invalid partition number %d for device %s%d (%s)\n",
1683 part_num, MTD_DEV_TYPE(dev->id->type),
1684 dev->id->num, dev->id->mtd_id);
1688 /* locate partition number, return it */
1690 list_for_each(entry, &dev->parts) {
1691 part = list_entry(entry, struct part_info, link);
1693 if (part_num == num++) {
1701 /***************************************************/
1702 /* U-boot commands */
1703 /***************************************************/
1704 /* command line only */
1706 * Routine implementing u-boot chpart command. Sets new current partition based
1707 * on the user supplied partition id. For partition id format see find_dev_and_part().
1709 * @param cmdtp command internal data
1710 * @param flag command flag
1711 * @param argc number of arguments supplied to the command
1712 * @param argv arguments list
1713 * @return 0 on success, 1 otherwise
1715 int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1717 /* command line only */
1718 struct mtd_device *dev;
1719 struct part_info *part;
1722 if (mtdparts_init() !=0)
1726 printf("no partition id specified\n");
1730 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1733 current_mtd_dev = dev;
1734 current_mtd_partnum = pnum;
1737 printf("partition changed to %s%d,%d\n",
1738 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1744 * Routine implementing u-boot mtdparts command. Initialize/update default global
1745 * partition list and process user partition request (list, add, del).
1747 * @param cmdtp command internal data
1748 * @param flag command flag
1749 * @param argc number of arguments supplied to the command
1750 * @param argv arguments list
1751 * @return 0 on success, 1 otherwise
1753 int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1756 if (strcmp(argv[1], "default") == 0) {
1757 setenv("mtdids", (char *)mtdids_default);
1758 setenv("mtdparts", (char *)mtdparts_default);
1759 setenv("partition", NULL);
1763 } else if (strcmp(argv[1], "delall") == 0) {
1764 /* this may be the first run, initialize lists if needed */
1767 setenv("mtdparts", NULL);
1769 /* mtd_devices_init() calls current_save() */
1770 return mtd_devices_init();
1774 /* make sure we are in sync with env variables */
1775 if (mtdparts_init() != 0)
1783 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
1784 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
1785 #define PART_ADD_DESC_MAXLEN 64
1786 char tmpbuf[PART_ADD_DESC_MAXLEN];
1788 struct mtd_device *dev;
1789 struct mtd_device *dev_tmp;
1791 struct part_info *p;
1793 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
1796 if ((id = id_find(type, num)) == NULL) {
1797 printf("no such device %s defined in mtdids variable\n", argv[2]);
1801 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
1802 len += strlen(argv[3]); /* size@offset */
1803 len += strlen(argv[4]) + 2; /* '(' name ')' */
1804 if (argv[5] && (strlen(argv[5]) == 2))
1805 len += 2; /* 'ro' */
1807 if (len >= PART_ADD_DESC_MAXLEN) {
1808 printf("too long partition description\n");
1811 sprintf(tmpbuf, "%s:%s(%s)%s",
1812 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
1813 DEBUGF("add tmpbuf: %s\n", tmpbuf);
1815 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
1818 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1819 dev->id->num, dev->id->mtd_id);
1821 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
1824 /* merge new partition with existing ones*/
1825 p = list_entry(dev->parts.next, struct part_info, link);
1826 if (part_add(dev_tmp, p) != 0) {
1832 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1833 printf("generated mtdparts too long, reseting to null\n");
1840 /* mtdparts del part-id */
1841 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
1842 DEBUGF("del: part-id = %s\n", argv[2]);
1844 return delete_partition(argv[2]);
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 /***************************************************/