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'<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)
88 * JFFS2/CRAMFS support
93 #include <jffs2/jffs2.h>
94 #include <linux/list.h>
95 #include <linux/ctype.h>
97 #if defined(CONFIG_CMD_JFFS2)
99 #include <cramfs/cramfs_fs.h>
101 #if defined(CONFIG_CMD_NAND)
102 #ifdef CFG_NAND_LEGACY
103 #include <linux/mtd/nand_legacy.h>
104 #else /* !CFG_NAND_LEGACY */
105 #include <linux/mtd/nand.h>
107 #endif /* !CFG_NAND_LEGACY */
109 /* enable/disable debugging messages */
114 # define DEBUGF(fmt, args...) printf(fmt ,##args)
116 # define DEBUGF(fmt, args...)
119 /* special size referring to all the remaining space in a partition */
120 #define SIZE_REMAINING 0xFFFFFFFF
122 /* special offset value, it is used when not provided by user
124 * this value is used temporarily during parsing, later such offests
125 * are recalculated */
126 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
128 /* minimum partition size */
129 #define MIN_PART_SIZE 4096
131 /* this flag needs to be set in part_info struct mask_flags
132 * field for read-only partitions */
133 #define MTD_WRITEABLE_CMD 1
135 #ifdef CONFIG_JFFS2_CMDLINE
136 /* default values for mtdids and mtdparts variables */
137 #if defined(MTDIDS_DEFAULT)
138 static const char *const mtdids_default = MTDIDS_DEFAULT;
140 #warning "MTDIDS_DEFAULT not defined!"
141 static const char *const mtdids_default = NULL;
144 #if defined(MTDPARTS_DEFAULT)
145 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
147 #warning "MTDPARTS_DEFAULT not defined!"
148 static const char *const mtdparts_default = NULL;
151 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
152 #define MTDIDS_MAXLEN 128
153 #define MTDPARTS_MAXLEN 512
154 #define PARTITION_MAXLEN 16
155 static char last_ids[MTDIDS_MAXLEN];
156 static char last_parts[MTDPARTS_MAXLEN];
157 static char last_partition[PARTITION_MAXLEN];
159 /* low level jffs2 cache cleaning routine */
160 extern void jffs2_free_cache(struct part_info *part);
162 /* mtdids mapping list, filled by parse_ids() */
163 struct list_head mtdids;
165 /* device/partition list, parse_cmdline() parses into here */
166 struct list_head devices;
167 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
169 /* current active device and partition number */
170 static struct mtd_device *current_dev = NULL;
171 static u8 current_partnum = 0;
173 extern int cramfs_check (struct part_info *info);
174 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
175 extern int cramfs_ls (struct part_info *info, char *filename);
176 extern int cramfs_info (struct part_info *info);
178 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
180 /* command line only routines */
181 #ifdef CONFIG_JFFS2_CMDLINE
183 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
184 static int device_del(struct mtd_device *dev);
187 * Parses a string into a number. The number stored at ptr is
188 * potentially suffixed with K (for kilobytes, or 1024 bytes),
189 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
190 * 1073741824). If the number is suffixed with K, M, or G, then
191 * the return value is the number multiplied by one kilobyte, one
192 * megabyte, or one gigabyte, respectively.
194 * @param ptr where parse begins
195 * @param retptr output pointer to next char after parse completes (output)
196 * @return resulting unsigned int
198 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
200 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
221 * Format string describing supplied size. This routine does the opposite job
222 * to memsize_parse(). Size in bytes is converted to string and if possible
223 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
225 * Note, that this routine does not check for buffer overflow, it's the caller
226 * who must assure enough space.
228 * @param buf output buffer
229 * @param size size to be converted to string
231 static void memsize_format(char *buf, u32 size)
233 #define SIZE_GB ((u32)1024*1024*1024)
234 #define SIZE_MB ((u32)1024*1024)
235 #define SIZE_KB ((u32)1024)
237 if ((size % SIZE_GB) == 0)
238 sprintf(buf, "%lug", size/SIZE_GB);
239 else if ((size % SIZE_MB) == 0)
240 sprintf(buf, "%lum", size/SIZE_MB);
241 else if (size % SIZE_KB == 0)
242 sprintf(buf, "%luk", size/SIZE_KB);
244 sprintf(buf, "%lu", size);
248 * This routine does global indexing of all partitions. Resulting index for
249 * current partition is saved in 'mtddevnum'. Current partition name in
252 static void index_partitions(void)
256 struct part_info *part;
257 struct list_head *dentry;
258 struct mtd_device *dev;
260 DEBUGF("--- index partitions ---\n");
264 list_for_each(dentry, &devices) {
265 dev = list_entry(dentry, struct mtd_device, link);
266 if (dev == current_dev) {
267 mtddevnum += current_partnum;
268 sprintf(buf, "%d", mtddevnum);
269 setenv("mtddevnum", buf);
272 mtddevnum += dev->num_parts;
275 part = jffs2_part_info(current_dev, current_partnum);
276 setenv("mtddevname", part->name);
278 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
280 setenv("mtddevnum", NULL);
281 setenv("mtddevname", NULL);
283 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
288 * Save current device and partition in environment variable 'partition'.
290 static void current_save(void)
294 DEBUGF("--- current_save ---\n");
297 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
298 current_dev->id->num, current_partnum);
300 setenv("partition", buf);
301 strncpy(last_partition, buf, 16);
303 DEBUGF("=> partition %s\n", buf);
305 setenv("partition", NULL);
306 last_partition[0] = '\0';
308 DEBUGF("=> partition NULL\n");
314 * Performs sanity check for supplied NOR flash partition. Table of existing
315 * NOR flash devices is searched and partition device is located. Alignment
316 * with the granularity of NOR flash sectors is verified.
318 * @param id of the parent device
319 * @param part partition to validate
320 * @return 0 if partition is valid, 1 otherwise
322 static int part_validate_nor(struct mtdids *id, struct part_info *part)
324 #if defined(CONFIG_CMD_FLASH)
325 /* info for FLASH chips */
326 extern flash_info_t flash_info[];
332 flash = &flash_info[id->num];
335 for (i = 0; i < flash->sector_count; i++) {
336 if ((flash->start[i] - flash->start[0]) == part->offset) {
341 if (offset_aligned == 0) {
342 printf("%s%d: partition (%s) start offset alignment incorrect\n",
343 MTD_DEV_TYPE(id->type), id->num, part->name);
347 end_offset = part->offset + part->size;
348 for (i = 0; i < flash->sector_count; i++) {
349 if ((flash->start[i] - flash->start[0]) == end_offset)
353 if (flash->size == end_offset)
356 printf("%s%d: partition (%s) size alignment incorrect\n",
357 MTD_DEV_TYPE(id->type), id->num, part->name);
363 * Performs sanity check for supplied NAND flash partition. Table of existing
364 * NAND flash devices is searched and partition device is located. Alignment
365 * with the granularity of nand erasesize is verified.
367 * @param id of the parent device
368 * @param part partition to validate
369 * @return 0 if partition is valid, 1 otherwise
371 static int part_validate_nand(struct mtdids *id, struct part_info *part)
373 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
374 /* info for NAND chips */
377 nand = &nand_info[id->num];
379 if ((unsigned long)(part->offset) % nand->erasesize) {
380 printf("%s%d: partition (%s) start offset alignment incorrect\n",
381 MTD_DEV_TYPE(id->type), id->num, part->name);
385 if (part->size % nand->erasesize) {
386 printf("%s%d: partition (%s) size alignment incorrect\n",
387 MTD_DEV_TYPE(id->type), id->num, part->name);
398 * Performs sanity check for supplied partition. Offset and size are verified
399 * to be within valid range. Partition type is checked and either
400 * parts_validate_nor() or parts_validate_nand() is called with the argument
403 * @param id of the parent device
404 * @param part partition to validate
405 * @return 0 if partition is valid, 1 otherwise
407 static int part_validate(struct mtdids *id, struct part_info *part)
409 if (part->size == SIZE_REMAINING)
410 part->size = id->size - part->offset;
412 if (part->offset > id->size) {
413 printf("%s: offset %08lx beyond flash size %08lx\n",
414 id->mtd_id, part->offset, id->size);
418 if ((part->offset + part->size) <= part->offset) {
419 printf("%s%d: partition (%s) size too big\n",
420 MTD_DEV_TYPE(id->type), id->num, part->name);
424 if (part->offset + part->size > id->size) {
425 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
429 if (id->type == MTD_DEV_TYPE_NAND)
430 return part_validate_nand(id, part);
431 else if (id->type == MTD_DEV_TYPE_NOR)
432 return part_validate_nor(id, part);
434 DEBUGF("part_validate: invalid dev type\n");
440 * Delete selected partition from the partion list of the specified device.
442 * @param dev device to delete partition from
443 * @param part partition to delete
444 * @return 0 on success, 1 otherwise
446 static int part_del(struct mtd_device *dev, struct part_info *part)
448 u8 current_save_needed = 0;
450 /* if there is only one partition, remove whole device */
451 if (dev->num_parts == 1)
452 return device_del(dev);
454 /* otherwise just delete this partition */
456 if (dev == current_dev) {
457 /* we are modyfing partitions for the current device,
459 struct part_info *curr_pi;
460 curr_pi = jffs2_part_info(current_dev, current_partnum);
463 if (curr_pi == part) {
464 printf("current partition deleted, resetting current to 0\n");
466 } else if (part->offset <= curr_pi->offset) {
469 current_save_needed = 1;
473 #ifdef CFG_NAND_LEGACY
474 jffs2_free_cache(part);
476 list_del(&part->link);
480 if (current_save_needed > 0)
489 * Delete all partitions from parts head list, free memory.
491 * @param head list of partitions to delete
493 static void part_delall(struct list_head *head)
495 struct list_head *entry, *n;
496 struct part_info *part_tmp;
498 /* clean tmp_list and free allocated memory */
499 list_for_each_safe(entry, n, head) {
500 part_tmp = list_entry(entry, struct part_info, link);
502 #ifdef CFG_NAND_LEGACY
503 jffs2_free_cache(part_tmp);
511 * Add new partition to the supplied partition list. Make sure partitions are
512 * sorted by offset in ascending order.
514 * @param head list this partition is to be added to
515 * @param new partition to be added
517 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
519 struct list_head *entry;
520 struct part_info *new_pi, *curr_pi;
522 /* link partition to parrent dev */
525 if (list_empty(&dev->parts)) {
526 DEBUGF("part_sort_add: list empty\n");
527 list_add(&part->link, &dev->parts);
533 new_pi = list_entry(&part->link, struct part_info, link);
535 /* get current partition info if we are updating current device */
537 if (dev == current_dev)
538 curr_pi = jffs2_part_info(current_dev, current_partnum);
540 list_for_each(entry, &dev->parts) {
541 struct part_info *pi;
543 pi = list_entry(entry, struct part_info, link);
545 /* be compliant with kernel cmdline, allow only one partition at offset zero */
546 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
547 printf("cannot add second partition at offset 0\n");
551 if (new_pi->offset <= pi->offset) {
552 list_add_tail(&part->link, entry);
555 if (curr_pi && (pi->offset <= curr_pi->offset)) {
556 /* we are modyfing partitions for the current
557 * device, update current */
567 list_add_tail(&part->link, &dev->parts);
574 * Add provided partition to the partition list of a given device.
576 * @param dev device to which partition is added
577 * @param part partition to be added
578 * @return 0 on success, 1 otherwise
580 static int part_add(struct mtd_device *dev, struct part_info *part)
582 /* verify alignment and size */
583 if (part_validate(dev->id, part) != 0)
586 /* partition is ok, add it to the list */
587 if (part_sort_add(dev, part) != 0)
594 * Parse one partition definition, allocate memory and return pointer to this
595 * location in retpart.
597 * @param partdef pointer to the partition definition string i.e. <part-def>
598 * @param ret output pointer to next char after parse completes (output)
599 * @param retpart pointer to the allocated partition (output)
600 * @return 0 on success, 1 otherwise
602 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
604 struct part_info *part;
606 unsigned long offset;
609 unsigned int mask_flags;
616 /* fetch the partition size */
618 /* assign all remaining space to this partition */
619 DEBUGF("'-': remaining size assigned\n");
620 size = SIZE_REMAINING;
623 size = memsize_parse(p, &p);
624 if (size < MIN_PART_SIZE) {
625 printf("partition size too small (%lx)\n", size);
630 /* check for offset */
631 offset = OFFSET_NOT_SPECIFIED;
634 offset = memsize_parse(p, &p);
637 /* now look for the name */
640 if ((p = strchr(name, ')')) == NULL) {
641 printf("no closing ) found in partition name\n");
644 name_len = p - name + 1;
645 if ((name_len - 1) == 0) {
646 printf("empty partition name\n");
651 /* 0x00000000@0x00000000 */
656 /* test for options */
658 if (strncmp(p, "ro", 2) == 0) {
659 mask_flags |= MTD_WRITEABLE_CMD;
663 /* check for next partition definition */
665 if (size == SIZE_REMAINING) {
667 printf("no partitions allowed after a fill-up partition\n");
671 } else if ((*p == ';') || (*p == '\0')) {
674 printf("unexpected character '%c' at the end of partition\n", *p);
679 /* allocate memory */
680 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
682 printf("out of memory\n");
685 memset(part, 0, sizeof(struct part_info) + name_len);
687 part->offset = offset;
688 part->mask_flags = mask_flags;
689 part->name = (char *)(part + 1);
692 /* copy user provided name */
693 strncpy(part->name, name, name_len - 1);
696 /* auto generated name in form of size@offset */
697 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
701 part->name[name_len - 1] = '\0';
702 INIT_LIST_HEAD(&part->link);
704 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
705 part->name, part->size,
706 part->offset, part->mask_flags);
711 #endif/* #ifdef CONFIG_JFFS2_CMDLINE */
714 * Check device number to be within valid range for given device type.
716 * @param dev device to validate
717 * @return 0 if device is valid, 1 otherwise
719 static int device_validate(u8 type, u8 num, u32 *size)
721 if (type == MTD_DEV_TYPE_NOR) {
722 #if defined(CONFIG_CMD_FLASH)
723 if (num < CFG_MAX_FLASH_BANKS) {
724 extern flash_info_t flash_info[];
725 *size = flash_info[num].size;
730 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
731 MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
733 printf("support for FLASH devices not present\n");
735 } else if (type == MTD_DEV_TYPE_NAND) {
736 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
737 if (num < CFG_MAX_NAND_DEVICE) {
738 #ifndef CFG_NAND_LEGACY
739 *size = nand_info[num].size;
741 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
742 *size = nand_dev_desc[num].totlen;
747 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
748 MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
750 printf("support for NAND devices not present\n");
757 #ifdef CONFIG_JFFS2_CMDLINE
759 * Delete all mtd devices from a supplied devices list, free memory allocated for
760 * each device and delete all device partitions.
762 * @return 0 on success, 1 otherwise
764 static int device_delall(struct list_head *head)
766 struct list_head *entry, *n;
767 struct mtd_device *dev_tmp;
769 /* clean devices list */
770 list_for_each_safe(entry, n, head) {
771 dev_tmp = list_entry(entry, struct mtd_device, link);
773 part_delall(&dev_tmp->parts);
776 INIT_LIST_HEAD(&devices);
782 * If provided device exists it's partitions are deleted, device is removed
783 * from device list and device memory is freed.
785 * @param dev device to be deleted
786 * @return 0 on success, 1 otherwise
788 static int device_del(struct mtd_device *dev)
790 part_delall(&dev->parts);
791 list_del(&dev->link);
794 if (dev == current_dev) {
795 /* we just deleted current device */
796 if (list_empty(&devices)) {
799 /* reset first partition from first dev from the
800 * devices list as current */
801 current_dev = list_entry(devices.next, struct mtd_device, link);
813 * Search global device list and return pointer to the device of type and num
816 * @param type device type
817 * @param num device number
818 * @return NULL if requested device does not exist
820 static struct mtd_device* device_find(u8 type, u8 num)
822 struct list_head *entry;
823 struct mtd_device *dev_tmp;
825 list_for_each(entry, &devices) {
826 dev_tmp = list_entry(entry, struct mtd_device, link);
828 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
836 * Add specified device to the global device list.
838 * @param dev device to be added
840 static void device_add(struct mtd_device *dev)
842 u8 current_save_needed = 0;
844 if (list_empty(&devices)) {
847 current_save_needed = 1;
850 list_add_tail(&dev->link, &devices);
852 if (current_save_needed > 0)
859 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
860 * return pointer to the device structure.
862 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
863 * @param ret output pointer to next char after parse completes (output)
864 * @param retdev pointer to the allocated device (output)
865 * @return 0 on success, 1 otherwise
867 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
869 struct mtd_device *dev;
870 struct part_info *part;
873 unsigned int mtd_id_len;
874 const char *p, *pend;
876 struct list_head *entry, *n;
885 DEBUGF("===device_parse===\n");
889 if (!(p = strchr(mtd_id, ':'))) {
890 printf("no <mtd-id> identifier\n");
893 mtd_id_len = p - mtd_id + 1;
896 /* verify if we have a valid device specified */
897 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
898 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
902 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
903 id->type, MTD_DEV_TYPE(id->type),
904 id->num, id->mtd_id);
905 pend = strchr(p, ';');
906 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
909 /* parse partitions */
913 if ((dev = device_find(id->type, id->num)) != NULL) {
914 /* if device already exists start at the end of the last partition */
915 part = list_entry(dev->parts.prev, struct part_info, link);
916 offset = part->offset + part->size;
919 while (p && (*p != '\0') && (*p != ';')) {
921 if ((part_parse(p, &p, &part) != 0) || (!part))
924 /* calculate offset when not specified */
925 if (part->offset == OFFSET_NOT_SPECIFIED)
926 part->offset = offset;
928 offset = part->offset;
930 /* verify alignment and size */
931 if (part_validate(id, part) != 0)
934 offset += part->size;
936 /* partition is ok, add it to the list */
937 list_add_tail(&part->link, &tmp_list);
942 part_delall(&tmp_list);
946 if (num_parts == 0) {
947 printf("no partitions for device %s%d (%s)\n",
948 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
952 DEBUGF("\ntotal partitions: %d\n", num_parts);
954 /* check for next device presence */
958 } else if (*p == '\0') {
961 printf("unexpected character '%c' at the end of device\n", *p);
967 /* allocate memory for mtd_device structure */
968 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
969 printf("out of memory\n");
972 memset(dev, 0, sizeof(struct mtd_device));
974 dev->num_parts = 0; /* part_sort_add increments num_parts */
975 INIT_LIST_HEAD(&dev->parts);
976 INIT_LIST_HEAD(&dev->link);
978 /* move partitions from tmp_list to dev->parts */
979 list_for_each_safe(entry, n, &tmp_list) {
980 part = list_entry(entry, struct part_info, link);
982 if (part_sort_add(dev, part) != 0) {
995 * Initialize global device list.
997 * @return 0 on success, 1 otherwise
999 static int devices_init(void)
1001 last_parts[0] = '\0';
1005 return device_delall(&devices);
1009 * Search global mtdids list and find id of requested type and number.
1011 * @return pointer to the id if it exists, NULL otherwise
1013 static struct mtdids* id_find(u8 type, u8 num)
1015 struct list_head *entry;
1018 list_for_each(entry, &mtdids) {
1019 id = list_entry(entry, struct mtdids, link);
1021 if ((id->type == type) && (id->num == num))
1029 * Search global mtdids list and find id of a requested mtd_id.
1031 * Note: first argument is not null terminated.
1033 * @param mtd_id string containing requested mtd_id
1034 * @param mtd_id_len length of supplied mtd_id
1035 * @return pointer to the id if it exists, NULL otherwise
1037 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1039 struct list_head *entry;
1042 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1043 mtd_id_len, mtd_id, mtd_id_len);
1045 list_for_each(entry, &mtdids) {
1046 id = list_entry(entry, struct mtdids, link);
1048 DEBUGF("entry: '%s' (len = %d)\n",
1049 id->mtd_id, strlen(id->mtd_id));
1051 if (mtd_id_len != strlen(id->mtd_id))
1053 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1059 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1062 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1065 * @param id string describing device id
1066 * @param ret_id output pointer to next char after parse completes (output)
1067 * @param dev_type parsed device type (output)
1068 * @param dev_num parsed device number (output)
1069 * @return 0 on success, 1 otherwise
1071 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1076 if (strncmp(p, "nand", 4) == 0) {
1077 *dev_type = MTD_DEV_TYPE_NAND;
1079 } else if (strncmp(p, "nor", 3) == 0) {
1080 *dev_type = MTD_DEV_TYPE_NOR;
1083 printf("incorrect device type in %s\n", id);
1088 printf("incorrect device number in %s\n", id);
1092 *dev_num = simple_strtoul(p, (char **)&p, 0);
1098 #ifdef CONFIG_JFFS2_CMDLINE
1100 * Process all devices and generate corresponding mtdparts string describing
1101 * all partitions on all devices.
1103 * @param buf output buffer holding generated mtdparts string (output)
1104 * @param buflen buffer size
1105 * @return 0 on success, 1 otherwise
1107 static int generate_mtdparts(char *buf, u32 buflen)
1109 struct list_head *pentry, *dentry;
1110 struct mtd_device *dev;
1111 struct part_info *part, *prev_part;
1114 u32 size, offset, len, part_cnt;
1115 u32 maxlen = buflen - 1;
1117 DEBUGF("--- generate_mtdparts ---\n");
1119 if (list_empty(&devices)) {
1124 sprintf(p, "mtdparts=");
1127 list_for_each(dentry, &devices) {
1128 dev = list_entry(dentry, struct mtd_device, link);
1131 len = strlen(dev->id->mtd_id) + 1;
1134 memcpy(p, dev->id->mtd_id, len - 1);
1139 /* format partitions */
1142 list_for_each(pentry, &dev->parts) {
1143 part = list_entry(pentry, struct part_info, link);
1145 offset = part->offset;
1148 /* partition size */
1149 memsize_format(tmpbuf, size);
1150 len = strlen(tmpbuf);
1153 memcpy(p, tmpbuf, len);
1158 /* add offset only when there is a gap between
1160 if ((!prev_part && (offset != 0)) ||
1161 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1163 memsize_format(tmpbuf, offset);
1164 len = strlen(tmpbuf) + 1;
1168 memcpy(p, tmpbuf, len - 1);
1173 /* copy name only if user supplied */
1174 if(!part->auto_name) {
1175 len = strlen(part->name) + 2;
1180 memcpy(p, part->name, len - 2);
1187 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1196 /* print ',' separator if there are other partitions
1198 if (dev->num_parts > part_cnt) {
1206 /* print ';' separator if there are other devices following */
1207 if (dentry->next != &devices) {
1215 /* we still have at least one char left, as we decremented maxlen at
1222 last_parts[0] = '\0';
1227 * Call generate_mtdparts to process all devices and generate corresponding
1228 * mtdparts string, save it in mtdparts environment variable.
1230 * @param buf output buffer holding generated mtdparts string (output)
1231 * @param buflen buffer size
1232 * @return 0 on success, 1 otherwise
1234 static int generate_mtdparts_save(char *buf, u32 buflen)
1238 ret = generate_mtdparts(buf, buflen);
1240 if ((buf[0] != '\0') && (ret == 0))
1241 setenv("mtdparts", buf);
1243 setenv("mtdparts", NULL);
1249 * Format and print out a partition list for each device from global device
1252 static void list_partitions(void)
1254 struct list_head *dentry, *pentry;
1255 struct part_info *part;
1256 struct mtd_device *dev;
1259 DEBUGF("\n---list_partitions---\n");
1260 list_for_each(dentry, &devices) {
1261 dev = list_entry(dentry, struct mtd_device, link);
1262 printf("\ndevice %s%d <%s>, # parts = %d\n",
1263 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1264 dev->id->mtd_id, dev->num_parts);
1265 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
1267 /* list partitions for given device */
1269 list_for_each(pentry, &dev->parts) {
1270 part = list_entry(pentry, struct part_info, link);
1271 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1272 part_num, part->name, part->size,
1273 part->offset, part->mask_flags);
1278 if (list_empty(&devices))
1279 printf("no partitions defined\n");
1281 /* current_dev is not NULL only when we have non empty device list */
1283 part = jffs2_part_info(current_dev, current_partnum);
1285 printf("\nactive partition: %s%d,%d - (%s) 0x%08lx @ 0x%08lx\n",
1286 MTD_DEV_TYPE(current_dev->id->type),
1287 current_dev->id->num, current_partnum,
1288 part->name, part->size, part->offset);
1290 printf("could not get current partition info\n\n");
1294 printf("\ndefaults:\n");
1295 printf("mtdids : %s\n", mtdids_default);
1296 printf("mtdparts: %s\n", mtdparts_default);
1300 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1301 * corresponding device and verify partition number.
1303 * @param id string describing device and partition or partition name
1304 * @param dev pointer to the requested device (output)
1305 * @param part_num verified partition number (output)
1306 * @param part pointer to requested partition (output)
1307 * @return 0 on success, 1 otherwise
1309 int find_dev_and_part(const char *id, struct mtd_device **dev,
1310 u8 *part_num, struct part_info **part)
1312 struct list_head *dentry, *pentry;
1313 u8 type, dnum, pnum;
1316 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1318 list_for_each(dentry, &devices) {
1320 *dev = list_entry(dentry, struct mtd_device, link);
1321 list_for_each(pentry, &(*dev)->parts) {
1322 *part = list_entry(pentry, struct part_info, link);
1323 if (strcmp((*part)->name, id) == 0)
1334 if (id_parse(p, &p, &type, &dnum) != 0)
1337 if ((*p++ != ',') || (*p == '\0')) {
1338 printf("no partition number specified\n");
1341 pnum = simple_strtoul(p, (char **)&p, 0);
1343 printf("unexpected trailing character '%c'\n", *p);
1347 if ((*dev = device_find(type, dnum)) == NULL) {
1348 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1352 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1353 printf("no such partition\n");
1364 * Find and delete partition. For partition id format see find_dev_and_part().
1366 * @param id string describing device and partition
1367 * @return 0 on success, 1 otherwise
1369 static int delete_partition(const char *id)
1372 struct mtd_device *dev;
1373 struct part_info *part;
1375 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1377 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1378 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1379 part->name, part->size, part->offset);
1381 if (part_del(dev, part) != 0)
1384 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1385 printf("generated mtdparts too long, reseting to null\n");
1391 printf("partition %s not found\n", id);
1396 * Accept character string describing mtd partitions and call device_parse()
1397 * for each entry. Add created devices to the global devices list.
1399 * @param mtdparts string specifing mtd partitions
1400 * @return 0 on success, 1 otherwise
1402 static int parse_mtdparts(const char *const mtdparts)
1404 const char *p = mtdparts;
1405 struct mtd_device *dev;
1408 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1410 /* delete all devices and partitions */
1411 if (devices_init() != 0) {
1412 printf("could not initialise device list\n");
1416 /* re-read 'mtdparts' variable, devices_init may be updating env */
1417 p = getenv("mtdparts");
1419 if (strncmp(p, "mtdparts=", 9) != 0) {
1420 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1425 while (p && (*p != '\0')) {
1427 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1430 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1431 dev->id->num, dev->id->mtd_id);
1433 /* check if parsed device is already on the list */
1434 if (device_find(dev->id->type, dev->id->num) != NULL) {
1435 printf("device %s%d redefined, please correct mtdparts variable\n",
1436 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1440 list_add_tail(&dev->link, &devices);
1444 device_delall(&devices);
1452 * Parse provided string describing mtdids mapping (see file header for mtdids
1453 * variable format). Allocate memory for each entry and add all found entries
1454 * to the global mtdids list.
1456 * @param ids mapping string
1457 * @return 0 on success, 1 otherwise
1459 static int parse_mtdids(const char *const ids)
1461 const char *p = ids;
1465 struct list_head *entry, *n;
1466 struct mtdids *id_tmp;
1471 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1473 /* clean global mtdids list */
1474 list_for_each_safe(entry, n, &mtdids) {
1475 id_tmp = list_entry(entry, struct mtdids, link);
1476 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1481 INIT_LIST_HEAD(&mtdids);
1483 while(p && (*p != '\0')) {
1486 /* parse 'nor'|'nand'<dev-num> */
1487 if (id_parse(p, &p, &type, &num) != 0)
1491 printf("mtdids: incorrect <dev-num>\n");
1496 /* check if requested device exists */
1497 if (device_validate(type, num, &size) != 0)
1500 /* locate <mtd-id> */
1502 if ((p = strchr(mtd_id, ',')) != NULL) {
1503 mtd_id_len = p - mtd_id + 1;
1506 mtd_id_len = strlen(mtd_id) + 1;
1508 if (mtd_id_len == 0) {
1509 printf("mtdids: no <mtd-id> identifier\n");
1513 /* check if this id is already on the list */
1514 int double_entry = 0;
1515 list_for_each(entry, &mtdids) {
1516 id_tmp = list_entry(entry, struct mtdids, link);
1517 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1523 printf("device id %s%d redefined, please correct mtdids variable\n",
1524 MTD_DEV_TYPE(type), num);
1528 /* allocate mtdids structure */
1529 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1530 printf("out of memory\n");
1533 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1537 id->mtd_id = (char *)(id + 1);
1538 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1539 id->mtd_id[mtd_id_len - 1] = '\0';
1540 INIT_LIST_HEAD(&id->link);
1542 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1543 MTD_DEV_TYPE(id->type), id->num,
1544 id->size, id->mtd_id);
1546 list_add_tail(&id->link, &mtdids);
1550 /* clean mtdids list and free allocated memory */
1551 list_for_each_safe(entry, n, &mtdids) {
1552 id_tmp = list_entry(entry, struct mtdids, link);
1563 * Parse and initialize global mtdids mapping and create global
1564 * device/partition list.
1566 * @return 0 on success, 1 otherwise
1568 int mtdparts_init(void)
1570 static int initialized = 0;
1571 const char *ids, *parts;
1572 const char *current_partition;
1574 char tmp_ep[PARTITION_MAXLEN];
1576 DEBUGF("\n---mtdparts_init---\n");
1578 INIT_LIST_HEAD(&mtdids);
1579 INIT_LIST_HEAD(&devices);
1580 memset(last_ids, 0, MTDIDS_MAXLEN);
1581 memset(last_parts, 0, MTDPARTS_MAXLEN);
1582 memset(last_partition, 0, PARTITION_MAXLEN);
1587 ids = getenv("mtdids");
1588 parts = getenv("mtdparts");
1589 current_partition = getenv("partition");
1591 /* save it for later parsing, cannot rely on current partition pointer
1592 * as 'partition' variable may be updated during init */
1594 if (current_partition)
1595 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1597 DEBUGF("last_ids : %s\n", last_ids);
1598 DEBUGF("env_ids : %s\n", ids);
1599 DEBUGF("last_parts: %s\n", last_parts);
1600 DEBUGF("env_parts : %s\n\n", parts);
1602 DEBUGF("last_partition : %s\n", last_partition);
1603 DEBUGF("env_partition : %s\n", current_partition);
1605 /* if mtdids varible is empty try to use defaults */
1607 if (mtdids_default) {
1608 DEBUGF("mtdids variable not defined, using default\n");
1609 ids = mtdids_default;
1610 setenv("mtdids", (char *)ids);
1612 printf("mtdids not defined, no default present\n");
1616 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1617 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1621 /* do no try to use defaults when mtdparts variable is not defined,
1622 * just check the length */
1624 printf("mtdparts variable not set, see 'help mtdparts'\n");
1626 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1627 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1631 /* check if we have already parsed those mtdids */
1632 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1637 if (parse_mtdids(ids) != 0) {
1642 /* ok it's good, save new ids */
1643 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1646 /* parse partitions if either mtdparts or mtdids were updated */
1647 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1648 if (parse_mtdparts(parts) != 0)
1651 if (list_empty(&devices)) {
1652 printf("mtdparts_init: no valid partitions\n");
1656 /* ok it's good, save new parts */
1657 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1659 /* reset first partition from first dev from the list as current */
1660 current_dev = list_entry(devices.next, struct mtd_device, link);
1661 current_partnum = 0;
1664 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1665 MTD_DEV_TYPE(current_dev->id->type),
1666 current_dev->id->num, current_partnum);
1669 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1670 if (!parts && (last_parts[0] != '\0'))
1671 return devices_init();
1673 /* do not process current partition if mtdparts variable is null */
1677 /* is current partition set in environment? if so, use it */
1678 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1679 struct part_info *p;
1680 struct mtd_device *cdev;
1683 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1685 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1687 current_partnum = pnum;
1690 } else if (getenv("partition") == NULL) {
1691 DEBUGF("no partition variable set, setting...\n");
1697 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1699 * 'Static' version of command line mtdparts_init() routine. Single partition on
1700 * a single device configuration.
1704 * Parse and initialize global mtdids mapping and create global
1705 * device/partition list.
1707 * @return 0 on success, 1 otherwise
1709 int mtdparts_init(void)
1711 static int initialized = 0;
1715 DEBUGF("\n---mtdparts_init---\n");
1718 struct part_info *part;
1721 current_dev = (struct mtd_device *)
1722 malloc(sizeof(struct mtd_device) +
1723 sizeof(struct part_info) +
1724 sizeof(struct mtdids));
1726 printf("out of memory\n");
1729 memset(current_dev, 0, sizeof(struct mtd_device) +
1730 sizeof(struct part_info) + sizeof(struct mtdids));
1732 id = (struct mtdids *)(current_dev + 1);
1733 part = (struct part_info *)(id + 1);
1736 id->mtd_id = "single part";
1738 #if defined(CONFIG_JFFS2_DEV)
1739 dev_name = CONFIG_JFFS2_DEV;
1744 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1745 (device_validate(id->type, id->num, &size) != 0)) {
1746 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1751 INIT_LIST_HEAD(&id->link);
1753 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1754 id->type, id->num, id->size, id->mtd_id);
1757 part->name = "static";
1758 part->auto_name = 0;
1760 #if defined(CONFIG_JFFS2_PART_SIZE)
1761 part->size = CONFIG_JFFS2_PART_SIZE;
1763 part->size = SIZE_REMAINING;
1766 #if defined(CONFIG_JFFS2_PART_OFFSET)
1767 part->offset = CONFIG_JFFS2_PART_OFFSET;
1769 part->offset = 0x00000000;
1772 part->dev = current_dev;
1773 INIT_LIST_HEAD(&part->link);
1775 /* recalculate size if needed */
1776 if (part->size == SIZE_REMAINING)
1777 part->size = id->size - part->offset;
1779 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1780 part->name, part->size, part->offset);
1783 current_dev->id = id;
1784 INIT_LIST_HEAD(¤t_dev->link);
1785 current_dev->num_parts = 1;
1786 INIT_LIST_HEAD(¤t_dev->parts);
1787 list_add(&part->link, ¤t_dev->parts);
1792 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1795 * Return pointer to the partition of a requested number from a requested
1798 * @param dev device that is to be searched for a partition
1799 * @param part_num requested partition number
1800 * @return pointer to the part_info, NULL otherwise
1802 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1804 struct list_head *entry;
1805 struct part_info *part;
1811 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1812 part_num, MTD_DEV_TYPE(dev->id->type),
1813 dev->id->num, dev->id->mtd_id);
1815 if (part_num >= dev->num_parts) {
1816 printf("invalid partition number %d for device %s%d (%s)\n",
1817 part_num, MTD_DEV_TYPE(dev->id->type),
1818 dev->id->num, dev->id->mtd_id);
1822 /* locate partition number, return it */
1824 list_for_each(entry, &dev->parts) {
1825 part = list_entry(entry, struct part_info, link);
1827 if (part_num == num++) {
1835 /***************************************************/
1836 /* U-boot commands */
1837 /***************************************************/
1840 * Routine implementing fsload u-boot command. This routine tries to load
1841 * a requested file from jffs2/cramfs filesystem on a current partition.
1843 * @param cmdtp command internal data
1844 * @param flag command flag
1845 * @param argc number of arguments supplied to the command
1846 * @param argv arguments list
1847 * @return 0 on success, 1 otherwise
1849 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1854 struct part_info *part;
1855 ulong offset = load_addr;
1857 /* pre-set Boot file name */
1858 if ((filename = getenv("bootfile")) == NULL) {
1859 filename = "uImage";
1866 offset = simple_strtoul(argv[1], NULL, 16);
1871 /* make sure we are in sync with env variables */
1872 if (mtdparts_init() !=0)
1875 if ((part = jffs2_part_info(current_dev, current_partnum))){
1877 /* check partition type for cramfs */
1878 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1879 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1881 if (cramfs_check(part)) {
1882 size = cramfs_load ((char *) offset, part, filename);
1884 /* if this is not cramfs assume jffs2 */
1885 size = jffs2_1pass_load((char *)offset, part, filename);
1890 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1891 fsname, size, offset);
1892 sprintf(buf, "%x", size);
1893 setenv("filesize", buf);
1895 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
1904 * Routine implementing u-boot ls command which lists content of a given
1905 * directory on a current partition.
1907 * @param cmdtp command internal data
1908 * @param flag command flag
1909 * @param argc number of arguments supplied to the command
1910 * @param argv arguments list
1911 * @return 0 on success, 1 otherwise
1913 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1915 char *filename = "/";
1917 struct part_info *part;
1922 /* make sure we are in sync with env variables */
1923 if (mtdparts_init() !=0)
1926 if ((part = jffs2_part_info(current_dev, current_partnum))){
1928 /* check partition type for cramfs */
1929 if (cramfs_check(part)) {
1930 ret = cramfs_ls (part, filename);
1932 /* if this is not cramfs assume jffs2 */
1933 ret = jffs2_1pass_ls(part, filename);
1942 * Routine implementing u-boot fsinfo command. This routine prints out
1943 * miscellaneous filesystem informations/statistics.
1945 * @param cmdtp command internal data
1946 * @param flag command flag
1947 * @param argc number of arguments supplied to the command
1948 * @param argv arguments list
1949 * @return 0 on success, 1 otherwise
1951 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1953 struct part_info *part;
1957 /* make sure we are in sync with env variables */
1958 if (mtdparts_init() !=0)
1961 if ((part = jffs2_part_info(current_dev, current_partnum))){
1963 /* check partition type for cramfs */
1964 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1965 printf("### filesystem type is %s\n", fsname);
1967 if (cramfs_check(part)) {
1968 ret = cramfs_info (part);
1970 /* if this is not cramfs assume jffs2 */
1971 ret = jffs2_1pass_info(part);
1979 /* command line only */
1980 #ifdef CONFIG_JFFS2_CMDLINE
1982 * Routine implementing u-boot chpart command. Sets new current partition based
1983 * on the user supplied partition id. For partition id format see find_dev_and_part().
1985 * @param cmdtp command internal data
1986 * @param flag command flag
1987 * @param argc number of arguments supplied to the command
1988 * @param argv arguments list
1989 * @return 0 on success, 1 otherwise
1991 int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1993 /* command line only */
1994 struct mtd_device *dev;
1995 struct part_info *part;
1998 if (mtdparts_init() !=0)
2002 printf("no partition id specified\n");
2006 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2010 current_partnum = pnum;
2013 printf("partition changed to %s%d,%d\n",
2014 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2020 * Routine implementing u-boot mtdparts command. Initialize/update default global
2021 * partition list and process user partition request (list, add, del).
2023 * @param cmdtp command internal data
2024 * @param flag command flag
2025 * @param argc number of arguments supplied to the command
2026 * @param argv arguments list
2027 * @return 0 on success, 1 otherwise
2029 int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2032 if (strcmp(argv[1], "default") == 0) {
2033 setenv("mtdids", (char *)mtdids_default);
2034 setenv("mtdparts", (char *)mtdparts_default);
2035 setenv("partition", NULL);
2039 } else if (strcmp(argv[1], "delall") == 0) {
2040 /* this may be the first run, initialize lists if needed */
2043 setenv("mtdparts", NULL);
2045 /* devices_init() calls current_save() */
2046 return devices_init();
2050 /* make sure we are in sync with env variables */
2051 if (mtdparts_init() != 0)
2059 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2060 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
2061 #define PART_ADD_DESC_MAXLEN 64
2062 char tmpbuf[PART_ADD_DESC_MAXLEN];
2064 struct mtd_device *dev;
2065 struct mtd_device *dev_tmp;
2067 struct part_info *p;
2069 if (id_parse(argv[2], NULL, &type, &num) != 0)
2072 if ((id = id_find(type, num)) == NULL) {
2073 printf("no such device %s defined in mtdids variable\n", argv[2]);
2077 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2078 len += strlen(argv[3]); /* size@offset */
2079 len += strlen(argv[4]) + 2; /* '(' name ')' */
2080 if (argv[5] && (strlen(argv[5]) == 2))
2081 len += 2; /* 'ro' */
2083 if (len >= PART_ADD_DESC_MAXLEN) {
2084 printf("too long partition description\n");
2087 sprintf(tmpbuf, "%s:%s(%s)%s",
2088 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
2089 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2091 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2094 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2095 dev->id->num, dev->id->mtd_id);
2097 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2100 /* merge new partition with existing ones*/
2101 p = list_entry(dev->parts.next, struct part_info, link);
2102 if (part_add(dev_tmp, p) != 0) {
2108 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2109 printf("generated mtdparts too long, reseting to null\n");
2116 /* mtdparts del part-id */
2117 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2118 DEBUGF("del: part-id = %s\n", argv[2]);
2120 return delete_partition(argv[2]);
2123 printf ("Usage:\n%s\n", cmdtp->usage);
2126 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2128 /***************************************************/
2130 fsload, 3, 0, do_jffs2_fsload,
2131 "fsload\t- load binary file from a filesystem image\n",
2132 "[ off ] [ filename ]\n"
2133 " - load binary file from flash bank\n"
2134 " with offset 'off'\n"
2137 ls, 2, 1, do_jffs2_ls,
2138 "ls\t- list files in a directory (default /)\n",
2140 " - list files in a directory.\n"
2144 fsinfo, 1, 1, do_jffs2_fsinfo,
2145 "fsinfo\t- print information about filesystems\n",
2146 " - print information about filesystems\n"
2149 #ifdef CONFIG_JFFS2_CMDLINE
2151 chpart, 2, 0, do_jffs2_chpart,
2152 "chpart\t- change active partition\n",
2154 " - change active partition (e.g. part-id = nand0,1)\n"
2158 mtdparts, 6, 0, do_jffs2_mtdparts,
2159 "mtdparts- define flash/nand partitions\n",
2161 " - list partition table\n"
2163 " - delete all partitions\n"
2164 "mtdparts del part-id\n"
2165 " - delete partition (e.g. part-id = nand0,1)\n"
2166 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2167 " - add partition\n"
2168 "mtdparts default\n"
2169 " - reset partition table to defaults\n\n"
2171 "this command uses three environment variables:\n\n"
2172 "'partition' - keeps current partition identifier\n\n"
2173 "partition := <part-id>\n"
2174 "<part-id> := <dev-id>,part_num\n\n"
2175 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2176 "mtdids=<idmap>[,<idmap>,...]\n\n"
2177 "<idmap> := <dev-id>=<mtd-id>\n"
2178 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2179 "<dev-num> := mtd device number, 0...\n"
2180 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2181 "'mtdparts' - partition list\n\n"
2182 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2183 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2184 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2185 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2186 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2187 "<offset> := partition start offset within the device\n"
2188 "<name> := '(' NAME ')'\n"
2189 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2191 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2193 /***************************************************/