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 #ifdef CONFIG_NAND_LEGACY
98 #include <linux/mtd/nand_legacy.h>
99 #else /* !CONFIG_NAND_LEGACY */
100 #include <linux/mtd/nand.h>
102 #endif /* !CONFIG_NAND_LEGACY */
105 #if defined(CONFIG_CMD_ONENAND)
106 #include <linux/mtd/onenand.h>
107 #include <onenand_uboot.h>
110 /* enable/disable debugging messages */
111 #define DEBUG_MTDPARTS
112 #undef DEBUG_MTDPARTS
114 #ifdef DEBUG_MTDPARTS
115 # define DEBUGF(fmt, args...) printf(fmt ,##args)
117 # define DEBUGF(fmt, args...)
120 /* special size referring to all the remaining space in a partition */
121 #define SIZE_REMAINING 0xFFFFFFFF
123 /* special offset value, it is used when not provided by user
125 * this value is used temporarily during parsing, later such offests
126 * are recalculated */
127 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
129 /* minimum partition size */
130 #define MIN_PART_SIZE 4096
132 /* this flag needs to be set in part_info struct mask_flags
133 * field for read-only partitions */
134 #define MTD_WRITEABLE_CMD 1
136 /* default values for mtdids and mtdparts variables */
137 #if defined(MTDIDS_DEFAULT)
138 static const char *const mtdids_default = MTDIDS_DEFAULT;
140 static const char *const mtdids_default = NULL;
143 #if defined(MTDPARTS_DEFAULT)
144 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
146 static const char *const mtdparts_default = NULL;
149 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
150 #define MTDIDS_MAXLEN 128
151 #define MTDPARTS_MAXLEN 512
152 #define PARTITION_MAXLEN 16
153 static char last_ids[MTDIDS_MAXLEN];
154 static char last_parts[MTDPARTS_MAXLEN];
155 static char last_partition[PARTITION_MAXLEN];
157 /* low level jffs2 cache cleaning routine */
158 extern void jffs2_free_cache(struct part_info *part);
160 /* mtdids mapping list, filled by parse_ids() */
161 struct list_head mtdids;
163 /* device/partition list, parse_cmdline() parses into here */
164 struct list_head devices;
166 /* current active device and partition number */
167 struct mtd_device *current_mtd_dev = NULL;
168 u8 current_mtd_partnum = 0;
170 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
172 /* command line only routines */
173 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
174 static int device_del(struct mtd_device *dev);
177 * Parses a string into a number. The number stored at ptr is
178 * potentially suffixed with K (for kilobytes, or 1024 bytes),
179 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
180 * 1073741824). If the number is suffixed with K, M, or G, then
181 * the return value is the number multiplied by one kilobyte, one
182 * megabyte, or one gigabyte, respectively.
184 * @param ptr where parse begins
185 * @param retptr output pointer to next char after parse completes (output)
186 * @return resulting unsigned int
188 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
190 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
211 * Format string describing supplied size. This routine does the opposite job
212 * to memsize_parse(). Size in bytes is converted to string and if possible
213 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
215 * Note, that this routine does not check for buffer overflow, it's the caller
216 * who must assure enough space.
218 * @param buf output buffer
219 * @param size size to be converted to string
221 static void memsize_format(char *buf, u32 size)
223 #define SIZE_GB ((u32)1024*1024*1024)
224 #define SIZE_MB ((u32)1024*1024)
225 #define SIZE_KB ((u32)1024)
227 if ((size % SIZE_GB) == 0)
228 sprintf(buf, "%ug", size/SIZE_GB);
229 else if ((size % SIZE_MB) == 0)
230 sprintf(buf, "%um", size/SIZE_MB);
231 else if (size % SIZE_KB == 0)
232 sprintf(buf, "%uk", size/SIZE_KB);
234 sprintf(buf, "%u", size);
238 * This routine does global indexing of all partitions. Resulting index for
239 * current partition is saved in 'mtddevnum'. Current partition name in
242 static void index_partitions(void)
246 struct part_info *part;
247 struct list_head *dentry;
248 struct mtd_device *dev;
250 DEBUGF("--- index partitions ---\n");
252 if (current_mtd_dev) {
254 list_for_each(dentry, &devices) {
255 dev = list_entry(dentry, struct mtd_device, link);
256 if (dev == current_mtd_dev) {
257 mtddevnum += current_mtd_partnum;
258 sprintf(buf, "%d", mtddevnum);
259 setenv("mtddevnum", buf);
262 mtddevnum += dev->num_parts;
265 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
266 setenv("mtddevname", part->name);
268 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
270 setenv("mtddevnum", NULL);
271 setenv("mtddevname", NULL);
273 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
278 * Save current device and partition in environment variable 'partition'.
280 static void current_save(void)
284 DEBUGF("--- current_save ---\n");
286 if (current_mtd_dev) {
287 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
288 current_mtd_dev->id->num, current_mtd_partnum);
290 setenv("partition", buf);
291 strncpy(last_partition, buf, 16);
293 DEBUGF("=> partition %s\n", buf);
295 setenv("partition", NULL);
296 last_partition[0] = '\0';
298 DEBUGF("=> partition NULL\n");
304 * Performs sanity check for supplied flash partition.
305 * Table of existing MTD flash devices is searched and partition device
306 * is located. Alignment with the granularity of nand erasesize is verified.
308 * @param id of the parent device
309 * @param part partition to validate
310 * @return 0 if partition is valid, 1 otherwise
312 static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
314 struct mtd_info *mtd;
319 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(id->type), id->num);
320 mtd = get_mtd_device_nm(mtd_dev);
322 printf("Partition %s not found on device %s!\n", part->name, mtd_dev);
326 part->sector_size = mtd->erasesize;
328 if (!mtd->numeraseregions) {
330 * Only one eraseregion (NAND, OneNAND or uniform NOR),
331 * checking for alignment is easy here
333 if ((unsigned long)part->offset % mtd->erasesize) {
334 printf("%s%d: partition (%s) start offset"
335 "alignment incorrect\n",
336 MTD_DEV_TYPE(id->type), id->num, part->name);
340 if (part->size % mtd->erasesize) {
341 printf("%s%d: partition (%s) size alignment incorrect\n",
342 MTD_DEV_TYPE(id->type), id->num, part->name);
347 * Multiple eraseregions (non-uniform NOR),
348 * checking for alignment is more complex here
351 /* Check start alignment */
352 for (i = 0; i < mtd->numeraseregions; i++) {
353 start = mtd->eraseregions[i].offset;
354 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
355 if (part->offset == start)
357 start += mtd->eraseregions[i].erasesize;
361 printf("%s%d: partition (%s) start offset alignment incorrect\n",
362 MTD_DEV_TYPE(id->type), id->num, part->name);
367 /* Check end/size alignment */
368 for (i = 0; i < mtd->numeraseregions; i++) {
369 start = mtd->eraseregions[i].offset;
370 for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
371 if ((part->offset + part->size) == start)
373 start += mtd->eraseregions[i].erasesize;
376 /* Check last sector alignment */
377 if ((part->offset + part->size) == start)
380 printf("%s%d: partition (%s) size alignment incorrect\n",
381 MTD_DEV_TYPE(id->type), id->num, part->name);
393 * Performs sanity check for supplied partition. Offset and size are verified
394 * to be within valid range. Partition type is checked and either
395 * parts_validate_nor() or parts_validate_nand() is called with the argument
398 * @param id of the parent device
399 * @param part partition to validate
400 * @return 0 if partition is valid, 1 otherwise
402 static int part_validate(struct mtdids *id, struct part_info *part)
404 if (part->size == SIZE_REMAINING)
405 part->size = id->size - part->offset;
407 if (part->offset > id->size) {
408 printf("%s: offset %08x beyond flash size %08x\n",
409 id->mtd_id, part->offset, id->size);
413 if ((part->offset + part->size) <= part->offset) {
414 printf("%s%d: partition (%s) size too big\n",
415 MTD_DEV_TYPE(id->type), id->num, part->name);
419 if (part->offset + part->size > id->size) {
420 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
425 * Now we need to check if the partition starts and ends on
426 * sector (eraseblock) regions
428 return part_validate_eraseblock(id, part);
432 * Delete selected partition from the partion list of the specified device.
434 * @param dev device to delete partition from
435 * @param part partition to delete
436 * @return 0 on success, 1 otherwise
438 static int part_del(struct mtd_device *dev, struct part_info *part)
440 u8 current_save_needed = 0;
442 /* if there is only one partition, remove whole device */
443 if (dev->num_parts == 1)
444 return device_del(dev);
446 /* otherwise just delete this partition */
448 if (dev == current_mtd_dev) {
449 /* we are modyfing partitions for the current device,
451 struct part_info *curr_pi;
452 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
455 if (curr_pi == part) {
456 printf("current partition deleted, resetting current to 0\n");
457 current_mtd_partnum = 0;
458 } else if (part->offset <= curr_pi->offset) {
459 current_mtd_partnum--;
461 current_save_needed = 1;
465 #ifdef CONFIG_NAND_LEGACY
466 jffs2_free_cache(part);
468 list_del(&part->link);
472 if (current_save_needed > 0)
481 * Delete all partitions from parts head list, free memory.
483 * @param head list of partitions to delete
485 static void part_delall(struct list_head *head)
487 struct list_head *entry, *n;
488 struct part_info *part_tmp;
490 /* clean tmp_list and free allocated memory */
491 list_for_each_safe(entry, n, head) {
492 part_tmp = list_entry(entry, struct part_info, link);
494 #ifdef CONFIG_NAND_LEGACY
495 jffs2_free_cache(part_tmp);
503 * Add new partition to the supplied partition list. Make sure partitions are
504 * sorted by offset in ascending order.
506 * @param head list this partition is to be added to
507 * @param new partition to be added
509 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
511 struct list_head *entry;
512 struct part_info *new_pi, *curr_pi;
514 /* link partition to parrent dev */
517 if (list_empty(&dev->parts)) {
518 DEBUGF("part_sort_add: list empty\n");
519 list_add(&part->link, &dev->parts);
525 new_pi = list_entry(&part->link, struct part_info, link);
527 /* get current partition info if we are updating current device */
529 if (dev == current_mtd_dev)
530 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
532 list_for_each(entry, &dev->parts) {
533 struct part_info *pi;
535 pi = list_entry(entry, struct part_info, link);
537 /* be compliant with kernel cmdline, allow only one partition at offset zero */
538 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
539 printf("cannot add second partition at offset 0\n");
543 if (new_pi->offset <= pi->offset) {
544 list_add_tail(&part->link, entry);
547 if (curr_pi && (pi->offset <= curr_pi->offset)) {
548 /* we are modyfing partitions for the current
549 * device, update current */
550 current_mtd_partnum++;
559 list_add_tail(&part->link, &dev->parts);
566 * Add provided partition to the partition list of a given device.
568 * @param dev device to which partition is added
569 * @param part partition to be added
570 * @return 0 on success, 1 otherwise
572 static int part_add(struct mtd_device *dev, struct part_info *part)
574 /* verify alignment and size */
575 if (part_validate(dev->id, part) != 0)
578 /* partition is ok, add it to the list */
579 if (part_sort_add(dev, part) != 0)
586 * Parse one partition definition, allocate memory and return pointer to this
587 * location in retpart.
589 * @param partdef pointer to the partition definition string i.e. <part-def>
590 * @param ret output pointer to next char after parse completes (output)
591 * @param retpart pointer to the allocated partition (output)
592 * @return 0 on success, 1 otherwise
594 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
596 struct part_info *part;
598 unsigned long offset;
601 unsigned int mask_flags;
608 /* fetch the partition size */
610 /* assign all remaining space to this partition */
611 DEBUGF("'-': remaining size assigned\n");
612 size = SIZE_REMAINING;
615 size = memsize_parse(p, &p);
616 if (size < MIN_PART_SIZE) {
617 printf("partition size too small (%lx)\n", size);
622 /* check for offset */
623 offset = OFFSET_NOT_SPECIFIED;
626 offset = memsize_parse(p, &p);
629 /* now look for the name */
632 if ((p = strchr(name, ')')) == NULL) {
633 printf("no closing ) found in partition name\n");
636 name_len = p - name + 1;
637 if ((name_len - 1) == 0) {
638 printf("empty partition name\n");
643 /* 0x00000000@0x00000000 */
648 /* test for options */
650 if (strncmp(p, "ro", 2) == 0) {
651 mask_flags |= MTD_WRITEABLE_CMD;
655 /* check for next partition definition */
657 if (size == SIZE_REMAINING) {
659 printf("no partitions allowed after a fill-up partition\n");
663 } else if ((*p == ';') || (*p == '\0')) {
666 printf("unexpected character '%c' at the end of partition\n", *p);
671 /* allocate memory */
672 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
674 printf("out of memory\n");
677 memset(part, 0, sizeof(struct part_info) + name_len);
679 part->offset = offset;
680 part->mask_flags = mask_flags;
681 part->name = (char *)(part + 1);
684 /* copy user provided name */
685 strncpy(part->name, name, name_len - 1);
688 /* auto generated name in form of size@offset */
689 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
693 part->name[name_len - 1] = '\0';
694 INIT_LIST_HEAD(&part->link);
696 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
697 part->name, part->size,
698 part->offset, part->mask_flags);
705 * Check device number to be within valid range for given device type.
707 * @param dev device to validate
708 * @return 0 if device is valid, 1 otherwise
710 int mtd_device_validate(u8 type, u8 num, u32 *size)
712 struct mtd_info *mtd;
715 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
716 mtd = get_mtd_device_nm(mtd_dev);
718 printf("Device %s not found!\n", mtd_dev);
728 * Delete all mtd devices from a supplied devices list, free memory allocated for
729 * each device and delete all device partitions.
731 * @return 0 on success, 1 otherwise
733 static int device_delall(struct list_head *head)
735 struct list_head *entry, *n;
736 struct mtd_device *dev_tmp;
738 /* clean devices list */
739 list_for_each_safe(entry, n, head) {
740 dev_tmp = list_entry(entry, struct mtd_device, link);
742 part_delall(&dev_tmp->parts);
745 INIT_LIST_HEAD(&devices);
751 * If provided device exists it's partitions are deleted, device is removed
752 * from device list and device memory is freed.
754 * @param dev device to be deleted
755 * @return 0 on success, 1 otherwise
757 static int device_del(struct mtd_device *dev)
759 part_delall(&dev->parts);
760 list_del(&dev->link);
763 if (dev == current_mtd_dev) {
764 /* we just deleted current device */
765 if (list_empty(&devices)) {
766 current_mtd_dev = NULL;
768 /* reset first partition from first dev from the
769 * devices list as current */
770 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
771 current_mtd_partnum = 0;
782 * Search global device list and return pointer to the device of type and num
785 * @param type device type
786 * @param num device number
787 * @return NULL if requested device does not exist
789 static struct mtd_device* device_find(u8 type, u8 num)
791 struct list_head *entry;
792 struct mtd_device *dev_tmp;
794 list_for_each(entry, &devices) {
795 dev_tmp = list_entry(entry, struct mtd_device, link);
797 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
805 * Add specified device to the global device list.
807 * @param dev device to be added
809 static void device_add(struct mtd_device *dev)
811 u8 current_save_needed = 0;
813 if (list_empty(&devices)) {
814 current_mtd_dev = dev;
815 current_mtd_partnum = 0;
816 current_save_needed = 1;
819 list_add_tail(&dev->link, &devices);
821 if (current_save_needed > 0)
828 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
829 * return pointer to the device structure.
831 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
832 * @param ret output pointer to next char after parse completes (output)
833 * @param retdev pointer to the allocated device (output)
834 * @return 0 on success, 1 otherwise
836 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
838 struct mtd_device *dev;
839 struct part_info *part;
842 unsigned int mtd_id_len;
843 const char *p, *pend;
845 struct list_head *entry, *n;
854 DEBUGF("===device_parse===\n");
858 if (!(p = strchr(mtd_id, ':'))) {
859 printf("no <mtd-id> identifier\n");
862 mtd_id_len = p - mtd_id + 1;
865 /* verify if we have a valid device specified */
866 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
867 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
871 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
872 id->type, MTD_DEV_TYPE(id->type),
873 id->num, id->mtd_id);
874 pend = strchr(p, ';');
875 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
878 /* parse partitions */
882 if ((dev = device_find(id->type, id->num)) != NULL) {
883 /* if device already exists start at the end of the last partition */
884 part = list_entry(dev->parts.prev, struct part_info, link);
885 offset = part->offset + part->size;
888 while (p && (*p != '\0') && (*p != ';')) {
890 if ((part_parse(p, &p, &part) != 0) || (!part))
893 /* calculate offset when not specified */
894 if (part->offset == OFFSET_NOT_SPECIFIED)
895 part->offset = offset;
897 offset = part->offset;
899 /* verify alignment and size */
900 if (part_validate(id, part) != 0)
903 offset += part->size;
905 /* partition is ok, add it to the list */
906 list_add_tail(&part->link, &tmp_list);
911 part_delall(&tmp_list);
915 if (num_parts == 0) {
916 printf("no partitions for device %s%d (%s)\n",
917 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
921 DEBUGF("\ntotal partitions: %d\n", num_parts);
923 /* check for next device presence */
927 } else if (*p == '\0') {
930 printf("unexpected character '%c' at the end of device\n", *p);
936 /* allocate memory for mtd_device structure */
937 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
938 printf("out of memory\n");
941 memset(dev, 0, sizeof(struct mtd_device));
943 dev->num_parts = 0; /* part_sort_add increments num_parts */
944 INIT_LIST_HEAD(&dev->parts);
945 INIT_LIST_HEAD(&dev->link);
947 /* move partitions from tmp_list to dev->parts */
948 list_for_each_safe(entry, n, &tmp_list) {
949 part = list_entry(entry, struct part_info, link);
951 if (part_sort_add(dev, part) != 0) {
964 * Initialize global device list.
966 * @return 0 on success, 1 otherwise
968 static int mtd_devices_init(void)
970 last_parts[0] = '\0';
971 current_mtd_dev = NULL;
974 return device_delall(&devices);
978 * Search global mtdids list and find id of requested type and number.
980 * @return pointer to the id if it exists, NULL otherwise
982 static struct mtdids* id_find(u8 type, u8 num)
984 struct list_head *entry;
987 list_for_each(entry, &mtdids) {
988 id = list_entry(entry, struct mtdids, link);
990 if ((id->type == type) && (id->num == num))
998 * Search global mtdids list and find id of a requested mtd_id.
1000 * Note: first argument is not null terminated.
1002 * @param mtd_id string containing requested mtd_id
1003 * @param mtd_id_len length of supplied mtd_id
1004 * @return pointer to the id if it exists, NULL otherwise
1006 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1008 struct list_head *entry;
1011 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1012 mtd_id_len, mtd_id, mtd_id_len);
1014 list_for_each(entry, &mtdids) {
1015 id = list_entry(entry, struct mtdids, link);
1017 DEBUGF("entry: '%s' (len = %d)\n",
1018 id->mtd_id, strlen(id->mtd_id));
1020 if (mtd_id_len != strlen(id->mtd_id))
1022 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1030 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1031 * return device type and number.
1033 * @param id string describing device id
1034 * @param ret_id output pointer to next char after parse completes (output)
1035 * @param dev_type parsed device type (output)
1036 * @param dev_num parsed device number (output)
1037 * @return 0 on success, 1 otherwise
1039 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1044 if (strncmp(p, "nand", 4) == 0) {
1045 *dev_type = MTD_DEV_TYPE_NAND;
1047 } else if (strncmp(p, "nor", 3) == 0) {
1048 *dev_type = MTD_DEV_TYPE_NOR;
1050 } else if (strncmp(p, "onenand", 7) == 0) {
1051 *dev_type = MTD_DEV_TYPE_ONENAND;
1054 printf("incorrect device type in %s\n", id);
1059 printf("incorrect device number in %s\n", id);
1063 *dev_num = simple_strtoul(p, (char **)&p, 0);
1070 * Process all devices and generate corresponding mtdparts string describing
1071 * all partitions on all devices.
1073 * @param buf output buffer holding generated mtdparts string (output)
1074 * @param buflen buffer size
1075 * @return 0 on success, 1 otherwise
1077 static int generate_mtdparts(char *buf, u32 buflen)
1079 struct list_head *pentry, *dentry;
1080 struct mtd_device *dev;
1081 struct part_info *part, *prev_part;
1084 u32 size, offset, len, part_cnt;
1085 u32 maxlen = buflen - 1;
1087 DEBUGF("--- generate_mtdparts ---\n");
1089 if (list_empty(&devices)) {
1094 sprintf(p, "mtdparts=");
1097 list_for_each(dentry, &devices) {
1098 dev = list_entry(dentry, struct mtd_device, link);
1101 len = strlen(dev->id->mtd_id) + 1;
1104 memcpy(p, dev->id->mtd_id, len - 1);
1109 /* format partitions */
1112 list_for_each(pentry, &dev->parts) {
1113 part = list_entry(pentry, struct part_info, link);
1115 offset = part->offset;
1118 /* partition size */
1119 memsize_format(tmpbuf, size);
1120 len = strlen(tmpbuf);
1123 memcpy(p, tmpbuf, len);
1128 /* add offset only when there is a gap between
1130 if ((!prev_part && (offset != 0)) ||
1131 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1133 memsize_format(tmpbuf, offset);
1134 len = strlen(tmpbuf) + 1;
1138 memcpy(p, tmpbuf, len - 1);
1143 /* copy name only if user supplied */
1144 if(!part->auto_name) {
1145 len = strlen(part->name) + 2;
1150 memcpy(p, part->name, len - 2);
1157 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1166 /* print ',' separator if there are other partitions
1168 if (dev->num_parts > part_cnt) {
1176 /* print ';' separator if there are other devices following */
1177 if (dentry->next != &devices) {
1185 /* we still have at least one char left, as we decremented maxlen at
1192 last_parts[0] = '\0';
1197 * Call generate_mtdparts to process all devices and generate corresponding
1198 * mtdparts string, save it in mtdparts environment variable.
1200 * @param buf output buffer holding generated mtdparts string (output)
1201 * @param buflen buffer size
1202 * @return 0 on success, 1 otherwise
1204 static int generate_mtdparts_save(char *buf, u32 buflen)
1208 ret = generate_mtdparts(buf, buflen);
1210 if ((buf[0] != '\0') && (ret == 0))
1211 setenv("mtdparts", buf);
1213 setenv("mtdparts", NULL);
1219 * Format and print out a partition list for each device from global device
1222 static void list_partitions(void)
1224 struct list_head *dentry, *pentry;
1225 struct part_info *part;
1226 struct mtd_device *dev;
1229 DEBUGF("\n---list_partitions---\n");
1230 list_for_each(dentry, &devices) {
1231 dev = list_entry(dentry, struct mtd_device, link);
1232 printf("\ndevice %s%d <%s>, # parts = %d\n",
1233 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1234 dev->id->mtd_id, dev->num_parts);
1235 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
1237 /* list partitions for given device */
1239 list_for_each(pentry, &dev->parts) {
1240 part = list_entry(pentry, struct part_info, link);
1241 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1242 part_num, part->name, part->size,
1243 part->offset, part->mask_flags);
1248 if (list_empty(&devices))
1249 printf("no partitions defined\n");
1251 /* current_mtd_dev is not NULL only when we have non empty device list */
1252 if (current_mtd_dev) {
1253 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
1255 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
1256 MTD_DEV_TYPE(current_mtd_dev->id->type),
1257 current_mtd_dev->id->num, current_mtd_partnum,
1258 part->name, part->size, part->offset);
1260 printf("could not get current partition info\n\n");
1264 printf("\ndefaults:\n");
1265 printf("mtdids : %s\n",
1266 mtdids_default ? mtdids_default : "none");
1267 printf("mtdparts: %s\n",
1268 mtdparts_default ? mtdparts_default : "none");
1272 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1273 * corresponding device and verify partition number.
1275 * @param id string describing device and partition or partition name
1276 * @param dev pointer to the requested device (output)
1277 * @param part_num verified partition number (output)
1278 * @param part pointer to requested partition (output)
1279 * @return 0 on success, 1 otherwise
1281 int find_dev_and_part(const char *id, struct mtd_device **dev,
1282 u8 *part_num, struct part_info **part)
1284 struct list_head *dentry, *pentry;
1285 u8 type, dnum, pnum;
1288 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1290 list_for_each(dentry, &devices) {
1292 *dev = list_entry(dentry, struct mtd_device, link);
1293 list_for_each(pentry, &(*dev)->parts) {
1294 *part = list_entry(pentry, struct part_info, link);
1295 if (strcmp((*part)->name, id) == 0)
1306 if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1309 if ((*p++ != ',') || (*p == '\0')) {
1310 printf("no partition number specified\n");
1313 pnum = simple_strtoul(p, (char **)&p, 0);
1315 printf("unexpected trailing character '%c'\n", *p);
1319 if ((*dev = device_find(type, dnum)) == NULL) {
1320 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1324 if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1325 printf("no such partition\n");
1336 * Find and delete partition. For partition id format see find_dev_and_part().
1338 * @param id string describing device and partition
1339 * @return 0 on success, 1 otherwise
1341 static int delete_partition(const char *id)
1344 struct mtd_device *dev;
1345 struct part_info *part;
1347 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1349 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1350 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1351 part->name, part->size, part->offset);
1353 if (part_del(dev, part) != 0)
1356 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1357 printf("generated mtdparts too long, reseting to null\n");
1363 printf("partition %s not found\n", id);
1368 * Accept character string describing mtd partitions and call device_parse()
1369 * for each entry. Add created devices to the global devices list.
1371 * @param mtdparts string specifing mtd partitions
1372 * @return 0 on success, 1 otherwise
1374 static int parse_mtdparts(const char *const mtdparts)
1376 const char *p = mtdparts;
1377 struct mtd_device *dev;
1380 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1382 /* delete all devices and partitions */
1383 if (mtd_devices_init() != 0) {
1384 printf("could not initialise device list\n");
1388 /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1389 p = getenv("mtdparts");
1391 if (strncmp(p, "mtdparts=", 9) != 0) {
1392 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1397 while (p && (*p != '\0')) {
1399 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1402 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1403 dev->id->num, dev->id->mtd_id);
1405 /* check if parsed device is already on the list */
1406 if (device_find(dev->id->type, dev->id->num) != NULL) {
1407 printf("device %s%d redefined, please correct mtdparts variable\n",
1408 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1412 list_add_tail(&dev->link, &devices);
1416 device_delall(&devices);
1424 * Parse provided string describing mtdids mapping (see file header for mtdids
1425 * variable format). Allocate memory for each entry and add all found entries
1426 * to the global mtdids list.
1428 * @param ids mapping string
1429 * @return 0 on success, 1 otherwise
1431 static int parse_mtdids(const char *const ids)
1433 const char *p = ids;
1437 struct list_head *entry, *n;
1438 struct mtdids *id_tmp;
1443 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1445 /* clean global mtdids list */
1446 list_for_each_safe(entry, n, &mtdids) {
1447 id_tmp = list_entry(entry, struct mtdids, link);
1448 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1453 INIT_LIST_HEAD(&mtdids);
1455 while(p && (*p != '\0')) {
1458 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1459 if (mtd_id_parse(p, &p, &type, &num) != 0)
1463 printf("mtdids: incorrect <dev-num>\n");
1468 /* check if requested device exists */
1469 if (mtd_device_validate(type, num, &size) != 0)
1472 /* locate <mtd-id> */
1474 if ((p = strchr(mtd_id, ',')) != NULL) {
1475 mtd_id_len = p - mtd_id + 1;
1478 mtd_id_len = strlen(mtd_id) + 1;
1480 if (mtd_id_len == 0) {
1481 printf("mtdids: no <mtd-id> identifier\n");
1485 /* check if this id is already on the list */
1486 int double_entry = 0;
1487 list_for_each(entry, &mtdids) {
1488 id_tmp = list_entry(entry, struct mtdids, link);
1489 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1495 printf("device id %s%d redefined, please correct mtdids variable\n",
1496 MTD_DEV_TYPE(type), num);
1500 /* allocate mtdids structure */
1501 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1502 printf("out of memory\n");
1505 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1509 id->mtd_id = (char *)(id + 1);
1510 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1511 id->mtd_id[mtd_id_len - 1] = '\0';
1512 INIT_LIST_HEAD(&id->link);
1514 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1515 MTD_DEV_TYPE(id->type), id->num,
1516 id->size, id->mtd_id);
1518 list_add_tail(&id->link, &mtdids);
1522 /* clean mtdids list and free allocated memory */
1523 list_for_each_safe(entry, n, &mtdids) {
1524 id_tmp = list_entry(entry, struct mtdids, link);
1535 * Parse and initialize global mtdids mapping and create global
1536 * device/partition list.
1538 * @return 0 on success, 1 otherwise
1540 int mtdparts_init(void)
1542 static int initialized = 0;
1543 const char *ids, *parts;
1544 const char *current_partition;
1546 char tmp_ep[PARTITION_MAXLEN];
1548 DEBUGF("\n---mtdparts_init---\n");
1550 INIT_LIST_HEAD(&mtdids);
1551 INIT_LIST_HEAD(&devices);
1552 memset(last_ids, 0, MTDIDS_MAXLEN);
1553 memset(last_parts, 0, MTDPARTS_MAXLEN);
1554 memset(last_partition, 0, PARTITION_MAXLEN);
1559 ids = getenv("mtdids");
1560 parts = getenv("mtdparts");
1561 current_partition = getenv("partition");
1563 /* save it for later parsing, cannot rely on current partition pointer
1564 * as 'partition' variable may be updated during init */
1566 if (current_partition)
1567 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1569 DEBUGF("last_ids : %s\n", last_ids);
1570 DEBUGF("env_ids : %s\n", ids);
1571 DEBUGF("last_parts: %s\n", last_parts);
1572 DEBUGF("env_parts : %s\n\n", parts);
1574 DEBUGF("last_partition : %s\n", last_partition);
1575 DEBUGF("env_partition : %s\n", current_partition);
1577 /* if mtdids varible is empty try to use defaults */
1579 if (mtdids_default) {
1580 DEBUGF("mtdids variable not defined, using default\n");
1581 ids = mtdids_default;
1582 setenv("mtdids", (char *)ids);
1584 printf("mtdids not defined, no default present\n");
1588 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1589 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1593 /* do no try to use defaults when mtdparts variable is not defined,
1594 * just check the length */
1596 printf("mtdparts variable not set, see 'help mtdparts'\n");
1598 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1599 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1603 /* check if we have already parsed those mtdids */
1604 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1609 if (parse_mtdids(ids) != 0) {
1614 /* ok it's good, save new ids */
1615 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1618 /* parse partitions if either mtdparts or mtdids were updated */
1619 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1620 if (parse_mtdparts(parts) != 0)
1623 if (list_empty(&devices)) {
1624 printf("mtdparts_init: no valid partitions\n");
1628 /* ok it's good, save new parts */
1629 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1631 /* reset first partition from first dev from the list as current */
1632 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1633 current_mtd_partnum = 0;
1636 DEBUGF("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
1637 MTD_DEV_TYPE(current_mtd_dev->id->type),
1638 current_mtd_dev->id->num, current_mtd_partnum);
1641 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1642 if (!parts && (last_parts[0] != '\0'))
1643 return mtd_devices_init();
1645 /* do not process current partition if mtdparts variable is null */
1649 /* is current partition set in environment? if so, use it */
1650 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1651 struct part_info *p;
1652 struct mtd_device *cdev;
1655 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1657 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1658 current_mtd_dev = cdev;
1659 current_mtd_partnum = pnum;
1662 } else if (getenv("partition") == NULL) {
1663 DEBUGF("no partition variable set, setting...\n");
1671 * Return pointer to the partition of a requested number from a requested
1674 * @param dev device that is to be searched for a partition
1675 * @param part_num requested partition number
1676 * @return pointer to the part_info, NULL otherwise
1678 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1680 struct list_head *entry;
1681 struct part_info *part;
1687 DEBUGF("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1688 part_num, MTD_DEV_TYPE(dev->id->type),
1689 dev->id->num, dev->id->mtd_id);
1691 if (part_num >= dev->num_parts) {
1692 printf("invalid partition number %d for device %s%d (%s)\n",
1693 part_num, MTD_DEV_TYPE(dev->id->type),
1694 dev->id->num, dev->id->mtd_id);
1698 /* locate partition number, return it */
1700 list_for_each(entry, &dev->parts) {
1701 part = list_entry(entry, struct part_info, link);
1703 if (part_num == num++) {
1711 /***************************************************/
1712 /* U-boot commands */
1713 /***************************************************/
1714 /* command line only */
1716 * Routine implementing u-boot chpart command. Sets new current partition based
1717 * on the user supplied partition id. For partition id format see find_dev_and_part().
1719 * @param cmdtp command internal data
1720 * @param flag command flag
1721 * @param argc number of arguments supplied to the command
1722 * @param argv arguments list
1723 * @return 0 on success, 1 otherwise
1725 int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1727 /* command line only */
1728 struct mtd_device *dev;
1729 struct part_info *part;
1732 if (mtdparts_init() !=0)
1736 printf("no partition id specified\n");
1740 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1743 current_mtd_dev = dev;
1744 current_mtd_partnum = pnum;
1747 printf("partition changed to %s%d,%d\n",
1748 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1754 * Routine implementing u-boot mtdparts command. Initialize/update default global
1755 * partition list and process user partition request (list, add, del).
1757 * @param cmdtp command internal data
1758 * @param flag command flag
1759 * @param argc number of arguments supplied to the command
1760 * @param argv arguments list
1761 * @return 0 on success, 1 otherwise
1763 int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1766 if (strcmp(argv[1], "default") == 0) {
1767 setenv("mtdids", (char *)mtdids_default);
1768 setenv("mtdparts", (char *)mtdparts_default);
1769 setenv("partition", NULL);
1773 } else if (strcmp(argv[1], "delall") == 0) {
1774 /* this may be the first run, initialize lists if needed */
1777 setenv("mtdparts", NULL);
1779 /* mtd_devices_init() calls current_save() */
1780 return mtd_devices_init();
1784 /* make sure we are in sync with env variables */
1785 if (mtdparts_init() != 0)
1793 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
1794 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
1795 #define PART_ADD_DESC_MAXLEN 64
1796 char tmpbuf[PART_ADD_DESC_MAXLEN];
1798 struct mtd_device *dev;
1799 struct mtd_device *dev_tmp;
1801 struct part_info *p;
1803 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
1806 if ((id = id_find(type, num)) == NULL) {
1807 printf("no such device %s defined in mtdids variable\n", argv[2]);
1811 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
1812 len += strlen(argv[3]); /* size@offset */
1813 len += strlen(argv[4]) + 2; /* '(' name ')' */
1814 if (argv[5] && (strlen(argv[5]) == 2))
1815 len += 2; /* 'ro' */
1817 if (len >= PART_ADD_DESC_MAXLEN) {
1818 printf("too long partition description\n");
1821 sprintf(tmpbuf, "%s:%s(%s)%s",
1822 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
1823 DEBUGF("add tmpbuf: %s\n", tmpbuf);
1825 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
1828 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1829 dev->id->num, dev->id->mtd_id);
1831 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
1834 /* merge new partition with existing ones*/
1835 p = list_entry(dev->parts.next, struct part_info, link);
1836 if (part_add(dev_tmp, p) != 0) {
1842 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1843 printf("generated mtdparts too long, reseting to null\n");
1850 /* mtdparts del part-id */
1851 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
1852 DEBUGF("del: part-id = %s\n", argv[2]);
1854 return delete_partition(argv[2]);
1861 /***************************************************/
1863 chpart, 2, 0, do_chpart,
1864 "change active partition",
1866 " - change active partition (e.g. part-id = nand0,1)"
1870 mtdparts, 6, 0, do_mtdparts,
1871 "define flash/nand partitions",
1873 " - list partition table\n"
1875 " - delete all partitions\n"
1876 "mtdparts del part-id\n"
1877 " - delete partition (e.g. part-id = nand0,1)\n"
1878 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
1879 " - add partition\n"
1880 "mtdparts default\n"
1881 " - reset partition table to defaults\n\n"
1883 "this command uses three environment variables:\n\n"
1884 "'partition' - keeps current partition identifier\n\n"
1885 "partition := <part-id>\n"
1886 "<part-id> := <dev-id>,part_num\n\n"
1887 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
1888 "mtdids=<idmap>[,<idmap>,...]\n\n"
1889 "<idmap> := <dev-id>=<mtd-id>\n"
1890 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
1891 "<dev-num> := mtd device number, 0...\n"
1892 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
1893 "'mtdparts' - partition list\n\n"
1894 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
1895 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
1896 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
1897 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
1898 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
1899 "<offset> := partition start offset within the device\n"
1900 "<name> := '(' NAME ')'\n"
1901 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)"
1903 /***************************************************/