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/mtd/nand.h>
95 #include <linux/list.h>
96 #include <linux/ctype.h>
98 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2)
100 #include <cramfs/cramfs_fs.h>
102 /* enable/disable debugging messages */
107 # define DEBUGF(fmt, args...) printf(fmt ,##args)
109 # define DEBUGF(fmt, args...)
112 /* special size referring to all the remaining space in a partition */
113 #define SIZE_REMAINING 0xFFFFFFFF
115 /* special offset value, it is used when not provided by user
117 * this value is used temporarily during parsing, later such offests
118 * are recalculated */
119 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
121 /* minimum partition size */
122 #define MIN_PART_SIZE 4096
124 /* this flag needs to be set in part_info struct mask_flags
125 * field for read-only partitions */
126 #define MTD_WRITEABLE 1
128 #ifdef CONFIG_JFFS2_CMDLINE
129 /* default values for mtdids and mtdparts variables */
130 #if defined(MTDIDS_DEFAULT)
131 static const char *const mtdids_default = MTDIDS_DEFAULT;
133 #warning "MTDIDS_DEFAULT not defined!"
134 static const char *const mtdids_default = NULL;
137 #if defined(MTDPARTS_DEFAULT)
138 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
140 #warning "MTDPARTS_DEFAULT not defined!"
141 static const char *const mtdparts_default = NULL;
144 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
145 #define MTDIDS_MAXLEN 128
146 #define MTDPARTS_MAXLEN 512
147 #define PARTITION_MAXLEN 16
148 static char last_ids[MTDIDS_MAXLEN];
149 static char last_parts[MTDPARTS_MAXLEN];
150 static char last_partition[PARTITION_MAXLEN];
152 /* low level jffs2 cache cleaning routine */
153 extern void jffs2_free_cache(struct part_info *part);
155 /* mtdids mapping list, filled by parse_ids() */
156 struct list_head mtdids;
158 /* device/partition list, parse_cmdline() parses into here */
159 struct list_head devices;
160 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
162 /* current active device and partition number */
163 static struct mtd_device *current_dev = NULL;
164 static u8 current_partnum = 0;
166 extern int cramfs_check (struct part_info *info);
167 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
168 extern int cramfs_ls (struct part_info *info, char *filename);
169 extern int cramfs_info (struct part_info *info);
171 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
173 /* command line only routines */
174 #ifdef CONFIG_JFFS2_CMDLINE
176 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
177 static int device_del(struct mtd_device *dev);
180 * Parses a string into a number. The number stored at ptr is
181 * potentially suffixed with K (for kilobytes, or 1024 bytes),
182 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
183 * 1073741824). If the number is suffixed with K, M, or G, then
184 * the return value is the number multiplied by one kilobyte, one
185 * megabyte, or one gigabyte, respectively.
187 * @param ptr where parse begins
188 * @param retptr output pointer to next char after parse completes (output)
189 * @return resulting unsigned int
191 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
193 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
214 * Format string describing supplied size. This routine does the opposite job
215 * to memsize_parse(). Size in bytes is converted to string and if possible
216 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
218 * Note, that this routine does not check for buffer overflow, it's the caller
219 * who must assure enough space.
221 * @param buf output buffer
222 * @param size size to be converted to string
224 static void memsize_format(char *buf, u32 size)
226 #define SIZE_GB ((u32)1024*1024*1024)
227 #define SIZE_MB ((u32)1024*1024)
228 #define SIZE_KB ((u32)1024)
230 if ((size % SIZE_GB) == 0)
231 sprintf(buf, "%lug", size/SIZE_GB);
232 else if ((size % SIZE_MB) == 0)
233 sprintf(buf, "%lum", size/SIZE_MB);
234 else if (size % SIZE_KB == 0)
235 sprintf(buf, "%luk", size/SIZE_KB);
237 sprintf(buf, "%lu", size);
241 * Save current device and partition in environment variable 'partition'.
243 static void current_save(void)
247 DEBUGF("--- current_save ---\n");
250 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
251 current_dev->id->num, current_partnum);
253 setenv("partition", buf);
254 strncpy(last_partition, buf, 16);
256 DEBUGF("=> partition %s\n", buf);
258 setenv("partition", NULL);
259 last_partition[0] = '\0';
261 DEBUGF("=> partition NULL\n");
266 * Performs sanity check for supplied NOR flash partition. Table of existing
267 * NOR flash devices is searched and partition device is located. Alignment
268 * with the granularity of NOR flash sectors is verified.
270 * @param id of the parent device
271 * @param part partition to validate
272 * @return 0 if partition is valid, 1 otherwise
274 static int part_validate_nor(struct mtdids *id, struct part_info *part)
276 #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
277 /* info for FLASH chips */
278 extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
284 flash = &flash_info[id->num];
287 for (i = 0; i < flash->sector_count; i++) {
288 if ((flash->start[i] - flash->start[0]) == part->offset) {
293 if (offset_aligned == 0) {
294 printf("%s%d: partition (%s) start offset alignment incorrect\n",
295 MTD_DEV_TYPE(id->type), id->num, part->name);
299 end_offset = part->offset + part->size;
300 for (i = 0; i < flash->sector_count; i++) {
301 if ((flash->start[i] - flash->start[0]) == end_offset)
305 if (flash->size == end_offset)
308 printf("%s%d: partition (%s) size alignment incorrect\n",
309 MTD_DEV_TYPE(id->type), id->num, part->name);
315 * Performs sanity check for supplied NAND flash partition. Table of existing
316 * NAND flash devices is searched and partition device is located. Alignment
317 * with the granularity of nand erasesize is verified.
319 * @param id of the parent device
320 * @param part partition to validate
321 * @return 0 if partition is valid, 1 otherwise
323 static int part_validate_nand(struct mtdids *id, struct part_info *part)
325 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
326 /* info for NAND chips */
327 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
328 struct nand_chip *nand;
330 nand = &nand_dev_desc[id->num];
332 if ((unsigned long)(part->offset) % nand->erasesize) {
333 printf("%s%d: partition (%s) start offset alignment incorrect\n",
334 MTD_DEV_TYPE(id->type), id->num, part->name);
338 if (part->size % nand->erasesize) {
339 printf("%s%d: partition (%s) size alignment incorrect\n",
340 MTD_DEV_TYPE(id->type), id->num, part->name);
351 * Performs sanity check for supplied partition. Offset and size are verified
352 * to be within valid range. Partition type is checked and either
353 * parts_validate_nor() or parts_validate_nand() is called with the argument
356 * @param id of the parent device
357 * @param part partition to validate
358 * @return 0 if partition is valid, 1 otherwise
360 static int part_validate(struct mtdids *id, struct part_info *part)
362 if (part->size == SIZE_REMAINING)
363 part->size = id->size - part->offset;
365 if (part->offset > id->size) {
366 printf("%s: offset %08lx beyond flash size %08lx\n",
367 id->mtd_id, part->offset, id->size);
371 if ((part->offset + part->size) <= part->offset) {
372 printf("%s%d: partition (%s) size too big\n",
373 MTD_DEV_TYPE(id->type), id->num, part->name);
377 if (part->offset + part->size > id->size) {
378 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
382 if (id->type == MTD_DEV_TYPE_NAND)
383 return part_validate_nand(id, part);
384 else if (id->type == MTD_DEV_TYPE_NOR)
385 return part_validate_nor(id, part);
387 DEBUGF("part_validate: invalid dev type\n");
393 * Delete selected partition from the partion list of the specified device.
395 * @param dev device to delete partition from
396 * @param part partition to delete
397 * @return 0 on success, 1 otherwise
399 static int part_del(struct mtd_device *dev, struct part_info *part)
401 /* if there is only one partition, remove whole device */
402 if (dev->num_parts == 1)
403 return device_del(dev);
405 /* otherwise just delete this partition */
407 if (dev == current_dev) {
408 /* we are modyfing partitions for the current device,
410 struct part_info *curr_pi;
411 curr_pi = jffs2_part_info(current_dev, current_partnum);
414 if (curr_pi == part) {
415 printf("current partition deleted, resetting current to 0\n");
418 } else if (part->offset <= curr_pi->offset) {
426 jffs2_free_cache(part);
427 list_del(&part->link);
435 * Delete all partitions from parts head list, free memory.
437 * @param head list of partitions to delete
439 static void part_delall(struct list_head *head)
441 struct list_head *entry, *n;
442 struct part_info *part_tmp;
444 /* clean tmp_list and free allocated memory */
445 list_for_each_safe(entry, n, head) {
446 part_tmp = list_entry(entry, struct part_info, link);
448 jffs2_free_cache(part_tmp);
455 * Add new partition to the supplied partition list. Make sure partitions are
456 * sorted by offset in ascending order.
458 * @param head list this partition is to be added to
459 * @param new partition to be added
461 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
463 struct list_head *entry;
464 struct part_info *new_pi, *curr_pi;
466 /* link partition to parrent dev */
469 if (list_empty(&dev->parts)) {
470 DEBUGF("part_sort_add: list empty\n");
471 list_add(&part->link, &dev->parts);
475 new_pi = list_entry(&part->link, struct part_info, link);
477 /* get current partition info if we are updating current device */
479 if (dev == current_dev)
480 curr_pi = jffs2_part_info(current_dev, current_partnum);
482 list_for_each(entry, &dev->parts) {
483 struct part_info *pi;
485 pi = list_entry(entry, struct part_info, link);
487 /* be compliant with kernel cmdline, allow only one partition at offset zero */
488 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
489 printf("cannot add second partition at offset 0\n");
493 if (new_pi->offset <= pi->offset) {
494 list_add_tail(&part->link, entry);
496 if (curr_pi && (pi->offset <= curr_pi->offset)) {
497 /* we are modyfing partitions for the current
498 * device, update current */
506 list_add_tail(&part->link, &dev->parts);
511 * Add provided partition to the partition list of a given device.
513 * @param dev device to which partition is added
514 * @param part partition to be added
515 * @return 0 on success, 1 otherwise
517 static int part_add(struct mtd_device *dev, struct part_info *part)
519 /* verify alignment and size */
520 if (part_validate(dev->id, part) != 0)
523 /* partition is ok, add it to the list */
524 if (part_sort_add(dev, part) != 0)
532 * Parse one partition definition, allocate memory and return pointer to this
533 * location in retpart.
535 * @param partdef pointer to the partition definition string i.e. <part-def>
536 * @param ret output pointer to next char after parse completes (output)
537 * @param retpart pointer to the allocated partition (output)
538 * @return 0 on success, 1 otherwise
540 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
542 struct part_info *part;
544 unsigned long offset;
547 unsigned int mask_flags;
554 /* fetch the partition size */
556 /* assign all remaining space to this partition */
557 DEBUGF("'-': remaining size assigned\n");
558 size = SIZE_REMAINING;
561 size = memsize_parse(p, &p);
562 if (size < MIN_PART_SIZE) {
563 printf("partition size too small (%lx)\n", size);
568 /* check for offset */
569 offset = OFFSET_NOT_SPECIFIED;
572 offset = memsize_parse(p, &p);
575 /* now look for the name */
578 if ((p = strchr(name, ')')) == NULL) {
579 printf("no closing ) found in partition name\n");
582 name_len = p - name + 1;
583 if ((name_len - 1) == 0) {
584 printf("empty partition name\n");
589 /* 0x00000000@0x00000000 */
594 /* test for options */
596 if (strncmp(p, "ro", 2) == 0) {
597 mask_flags |= MTD_WRITEABLE;
601 /* check for next partition definition */
603 if (size == SIZE_REMAINING) {
605 printf("no partitions allowed after a fill-up partition\n");
609 } else if ((*p == ';') || (*p == '\0')) {
612 printf("unexpected character '%c' at the end of partition\n", *p);
617 /* allocate memory */
618 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
620 printf("out of memory\n");
623 memset(part, 0, sizeof(struct part_info) + name_len);
625 part->offset = offset;
626 part->mask_flags = mask_flags;
627 part->name = (char *)(part + 1);
630 /* copy user provided name */
631 strncpy(part->name, name, name_len - 1);
634 /* auto generated name in form of size@offset */
635 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
639 part->name[name_len - 1] = '\0';
640 INIT_LIST_HEAD(&part->link);
642 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
643 part->name, part->size,
644 part->offset, part->mask_flags);
649 #endif/* #ifdef CONFIG_JFFS2_CMDLINE */
652 * Check device number to be within valid range for given device type.
654 * @param dev device to validate
655 * @return 0 if device is valid, 1 otherwise
657 static int device_validate(u8 type, u8 num, u32 *size)
659 if (type == MTD_DEV_TYPE_NOR) {
660 #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
661 if (num < CFG_MAX_FLASH_BANKS) {
662 extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
663 *size = flash_info[num].size;
667 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
668 MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
670 printf("support for FLASH devices not present\n");
672 } else if (type == MTD_DEV_TYPE_NAND) {
673 #if defined(CONFIG_JFFS2_NAND) && (CONFIG_COMMANDS & CFG_CMD_NAND)
674 if (num < CFG_MAX_NAND_DEVICE) {
675 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
676 *size = nand_dev_desc[num].totlen;
680 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
681 MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
683 printf("support for NAND devices not present\n");
690 #ifdef CONFIG_JFFS2_CMDLINE
692 * Delete all mtd devices from a supplied devices list, free memory allocated for
693 * each device and delete all device partitions.
695 * @return 0 on success, 1 otherwise
697 static int device_delall(struct list_head *head)
699 struct list_head *entry, *n;
700 struct mtd_device *dev_tmp;
702 /* clean devices list */
703 list_for_each_safe(entry, n, head) {
704 dev_tmp = list_entry(entry, struct mtd_device, link);
706 part_delall(&dev_tmp->parts);
709 INIT_LIST_HEAD(&devices);
715 * If provided device exists it's partitions are deleted, device is removed
716 * from device list and device memory is freed.
718 * @param dev device to be deleted
719 * @return 0 on success, 1 otherwise
721 static int device_del(struct mtd_device *dev)
723 part_delall(&dev->parts);
724 list_del(&dev->link);
727 if (dev == current_dev) {
728 /* we just deleted current device */
729 if (list_empty(&devices)) {
732 /* reset first partition from first dev from the
733 * devices list as current */
734 current_dev = list_entry(devices.next, struct mtd_device, link);
745 * Search global device list and return pointer to the device of type and num
748 * @param type device type
749 * @param num device number
750 * @return NULL if requested device does not exist
752 static struct mtd_device* device_find(u8 type, u8 num)
754 struct list_head *entry;
755 struct mtd_device *dev_tmp;
757 list_for_each(entry, &devices) {
758 dev_tmp = list_entry(entry, struct mtd_device, link);
760 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
768 * Add specified device to the global device list.
770 * @param dev device to be added
772 static void device_add(struct mtd_device *dev)
774 if (list_empty(&devices)) {
780 list_add_tail(&dev->link, &devices);
784 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
785 * return pointer to the device structure.
787 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
788 * @param ret output pointer to next char after parse completes (output)
789 * @param retdev pointer to the allocated device (output)
790 * @return 0 on success, 1 otherwise
792 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
794 struct mtd_device *dev;
795 struct part_info *part;
798 unsigned int mtd_id_len;
799 const char *p, *pend;
801 struct list_head *entry, *n;
810 DEBUGF("===device_parse===\n");
814 if (!(p = strchr(mtd_id, ':'))) {
815 printf("no <mtd-id> identifier\n");
818 mtd_id_len = p - mtd_id + 1;
821 /* verify if we have a valid device specified */
822 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
823 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
827 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
828 id->type, MTD_DEV_TYPE(id->type),
829 id->num, id->mtd_id);
830 pend = strchr(p, ';');
831 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
834 /* parse partitions */
838 if ((dev = device_find(id->type, id->num)) != NULL) {
839 /* if device already exists start at the end of the last partition */
840 part = list_entry(dev->parts.prev, struct part_info, link);
841 offset = part->offset + part->size;
844 while (p && (*p != '\0') && (*p != ';')) {
846 if ((part_parse(p, &p, &part) != 0) || (!part))
849 /* calculate offset when not specified */
850 if (part->offset == OFFSET_NOT_SPECIFIED)
851 part->offset = offset;
853 offset = part->offset;
855 /* verify alignment and size */
856 if (part_validate(id, part) != 0)
859 offset += part->size;
861 /* partition is ok, add it to the list */
862 list_add_tail(&part->link, &tmp_list);
867 part_delall(&tmp_list);
871 if (num_parts == 0) {
872 printf("no partitions for device %s%d (%s)\n",
873 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
877 DEBUGF("\ntotal partitions: %d\n", num_parts);
879 /* check for next device presence */
883 } else if (*p == '\0') {
886 printf("unexpected character '%c' at the end of device\n", *p);
892 /* allocate memory for mtd_device structure */
893 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
894 printf("out of memory\n");
897 memset(dev, 0, sizeof(struct mtd_device));
899 dev->num_parts = num_parts;
900 INIT_LIST_HEAD(&dev->parts);
901 INIT_LIST_HEAD(&dev->link);
903 /* move partitions from tmp_list to dev->parts */
904 list_for_each_safe(entry, n, &tmp_list) {
905 part = list_entry(entry, struct part_info, link);
907 if (part_sort_add(dev, part) != 0) {
920 * Initialize global device list.
922 * @return 0 on success, 1 otherwise
924 static int devices_init(void)
926 last_parts[0] = '\0';
930 return device_delall(&devices);
934 * Search global mtdids list and find id of requested type and number.
936 * @return pointer to the id if it exists, NULL otherwise
938 static struct mtdids* id_find(u8 type, u8 num)
940 struct list_head *entry;
943 list_for_each(entry, &mtdids) {
944 id = list_entry(entry, struct mtdids, link);
946 if ((id->type == type) && (id->num == num))
954 * Search global mtdids list and find id of a requested mtd_id.
956 * Note: first argument is not null terminated.
958 * @param mtd_id string containing requested mtd_id
959 * @param mtd_id_len length of supplied mtd_id
960 * @return pointer to the id if it exists, NULL otherwise
962 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
964 struct list_head *entry;
967 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
968 mtd_id_len, mtd_id, mtd_id_len);
970 list_for_each(entry, &mtdids) {
971 id = list_entry(entry, struct mtdids, link);
973 DEBUGF("entry: '%s' (len = %d)\n",
974 id->mtd_id, strlen(id->mtd_id));
976 if (mtd_id_len != strlen(id->mtd_id))
978 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
984 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
987 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
990 * @param id string describing device id
991 * @param ret_id output pointer to next char after parse completes (output)
992 * @param dev_type parsed device type (output)
993 * @param dev_num parsed device number (output)
994 * @return 0 on success, 1 otherwise
996 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1001 if (strncmp(p, "nand", 4) == 0) {
1002 *dev_type = MTD_DEV_TYPE_NAND;
1004 } else if (strncmp(p, "nor", 3) == 0) {
1005 *dev_type = MTD_DEV_TYPE_NOR;
1008 printf("incorrect device type in %s\n", id);
1013 printf("incorrect device number in %s\n", id);
1017 *dev_num = simple_strtoul(p, (char **)&p, 0);
1023 #ifdef CONFIG_JFFS2_CMDLINE
1025 * Process all devices and generate corresponding mtdparts string describing
1026 * all partitions on all devices.
1028 * @param buf output buffer holding generated mtdparts string (output)
1029 * @param buflen buffer size
1030 * @return 0 on success, 1 otherwise
1032 static int generate_mtdparts(char *buf, u32 buflen)
1034 struct list_head *pentry, *dentry;
1035 struct mtd_device *dev;
1036 struct part_info *part, *prev_part;
1039 u32 size, offset, len, part_cnt;
1040 u32 maxlen = buflen - 1;
1042 DEBUGF("--- generate_mtdparts ---\n");
1044 if (list_empty(&devices)) {
1049 sprintf(p, "mtdparts=");
1052 list_for_each(dentry, &devices) {
1053 dev = list_entry(dentry, struct mtd_device, link);
1056 len = strlen(dev->id->mtd_id) + 1;
1059 memcpy(p, dev->id->mtd_id, len - 1);
1064 /* format partitions */
1067 list_for_each(pentry, &dev->parts) {
1068 part = list_entry(pentry, struct part_info, link);
1070 offset = part->offset;
1073 /* partition size */
1074 memsize_format(tmpbuf, size);
1075 len = strlen(tmpbuf);
1078 memcpy(p, tmpbuf, len);
1083 /* add offset only when there is a gap between
1085 if ((!prev_part && (offset != 0)) ||
1086 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1088 memsize_format(tmpbuf, offset);
1089 len = strlen(tmpbuf) + 1;
1093 memcpy(p, tmpbuf, len - 1);
1098 /* copy name only if user supplied */
1099 if(!part->auto_name) {
1100 len = strlen(part->name) + 2;
1105 memcpy(p, part->name, len - 2);
1112 if (part->mask_flags && MTD_WRITEABLE) {
1121 /* print ',' separator if there are other partitions
1123 if (dev->num_parts > part_cnt) {
1131 /* print ';' separator if there are other devices following */
1132 if (dentry->next != &devices) {
1140 /* we still have at least one char left, as we decremented maxlen at
1147 last_parts[0] = '\0';
1152 * Call generate_mtdparts to process all devices and generate corresponding
1153 * mtdparts string, save it in mtdparts environment variable.
1155 * @param buf output buffer holding generated mtdparts string (output)
1156 * @param buflen buffer size
1157 * @return 0 on success, 1 otherwise
1159 static int generate_mtdparts_save(char *buf, u32 buflen)
1163 ret = generate_mtdparts(buf, buflen);
1165 if ((buf[0] != '\0') && (ret == 0))
1166 setenv("mtdparts", buf);
1168 setenv("mtdparts", NULL);
1174 * Format and print out a partition list for each device from global device
1177 static void list_partitions(void)
1179 struct list_head *dentry, *pentry;
1180 struct part_info *part;
1181 struct mtd_device *dev;
1184 DEBUGF("\n---list_partitions---\n");
1185 list_for_each(dentry, &devices) {
1186 dev = list_entry(dentry, struct mtd_device, link);
1187 printf("\ndevice %s%d <%s>, # parts = %d\n",
1188 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1189 dev->id->mtd_id, dev->num_parts);
1190 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
1192 /* list partitions for given device */
1194 list_for_each(pentry, &dev->parts) {
1195 part = list_entry(pentry, struct part_info, link);
1196 printf(" %d: %-22s\t0x%08x\t0x%08x\t%d\n",
1197 part_num, part->name, part->size,
1198 part->offset, part->mask_flags);
1203 if (list_empty(&devices))
1204 printf("no partitions defined\n");
1206 /* current_dev is not NULL only when we have non empty device list */
1208 part = jffs2_part_info(current_dev, current_partnum);
1210 printf("\nactive partition: %s%d,%d - (%s) 0x%08lx @ 0x%08lx\n",
1211 MTD_DEV_TYPE(current_dev->id->type),
1212 current_dev->id->num, current_partnum,
1213 part->name, part->size, part->offset);
1215 printf("could not get current partition info\n\n");
1219 printf("\ndefaults:\n");
1220 printf("mtdids : %s\n", mtdids_default);
1221 printf("mtdparts: %s\n", mtdparts_default);
1225 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1226 * corresponding device and verify partition number.
1228 * @param id string describing device and partition
1229 * @param dev pointer to the requested device (output)
1230 * @param part_num verified partition number (output)
1231 * @param part pointer to requested partition (output)
1232 * @return 0 on success, 1 otherwise
1234 int find_dev_and_part(const char *id, struct mtd_device **dev,
1235 u8 *part_num, struct part_info **part)
1237 u8 type, dnum, pnum;
1240 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1247 if (id_parse(p, &p, &type, &dnum) != 0)
1250 if ((*p++ != ',') || (*p == '\0')) {
1251 printf("no partition number specified\n");
1254 pnum = simple_strtoul(p, (char **)&p, 0);
1256 printf("unexpected trailing character '%c'\n", *p);
1260 if ((*dev = device_find(type, dnum)) == NULL) {
1261 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1265 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1266 printf("no such partition\n");
1277 * Find and delete partition. For partition id format see find_dev_and_part().
1279 * @param id string describing device and partition
1280 * @return 0 on success, 1 otherwise
1282 static int delete_partition(const char *id)
1285 struct mtd_device *dev;
1286 struct part_info *part;
1288 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1290 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1291 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1292 part->name, part->size, part->offset);
1294 if (part_del(dev, part) != 0)
1297 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1298 printf("generated mtdparts too long, reseting to null\n");
1304 printf("partition %s not found\n", id);
1309 * Accept character string describing mtd partitions and call device_parse()
1310 * for each entry. Add created devices to the global devices list.
1312 * @param mtdparts string specifing mtd partitions
1313 * @return 0 on success, 1 otherwise
1315 static int parse_mtdparts(const char *const mtdparts)
1317 const char *p = mtdparts;
1318 struct mtd_device *dev;
1321 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1323 /* delete all devices and partitions */
1324 if (devices_init() != 0) {
1325 printf("could not initialise device list\n");
1329 /* re-read 'mtdparts' variable, devices_init may be updating env */
1330 p = getenv("mtdparts");
1332 if (strncmp(p, "mtdparts=", 9) != 0) {
1333 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1338 while (p && (*p != '\0')) {
1340 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1343 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1344 dev->id->num, dev->id->mtd_id);
1346 /* check if parsed device is already on the list */
1347 if (device_find(dev->id->type, dev->id->num) != NULL) {
1348 printf("device %s%d redefined, please correct mtdparts variable\n",
1349 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1353 list_add_tail(&dev->link, &devices);
1357 device_delall(&devices);
1365 * Parse provided string describing mtdids mapping (see file header for mtdids
1366 * variable format). Allocate memory for each entry and add all found entries
1367 * to the global mtdids list.
1369 * @param ids mapping string
1370 * @return 0 on success, 1 otherwise
1372 static int parse_mtdids(const char *const ids)
1374 const char *p = ids;
1378 struct list_head *entry, *n;
1379 struct mtdids *id_tmp;
1384 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1386 /* clean global mtdids list */
1387 list_for_each_safe(entry, n, &mtdids) {
1388 id_tmp = list_entry(entry, struct mtdids, link);
1389 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1394 INIT_LIST_HEAD(&mtdids);
1396 while(p && (*p != '\0')) {
1399 /* parse 'nor'|'nand'<dev-num> */
1400 if (id_parse(p, &p, &type, &num) != 0)
1404 printf("mtdids: incorrect <dev-num>\n");
1409 /* check if requested device exists */
1410 if (device_validate(type, num, &size) != 0)
1413 /* locate <mtd-id> */
1415 if ((p = strchr(mtd_id, ',')) != NULL) {
1416 mtd_id_len = p - mtd_id + 1;
1419 mtd_id_len = strlen(mtd_id) + 1;
1421 if (mtd_id_len == 0) {
1422 printf("mtdids: no <mtd-id> identifier\n");
1426 /* check if this id is already on the list */
1427 int double_entry = 0;
1428 list_for_each(entry, &mtdids) {
1429 id_tmp = list_entry(entry, struct mtdids, link);
1430 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1436 printf("device id %s%d redefined, please correct mtdids variable\n",
1437 MTD_DEV_TYPE(type), num);
1441 /* allocate mtdids structure */
1442 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1443 printf("out of memory\n");
1446 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1450 id->mtd_id = (char *)(id + 1);
1451 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1452 id->mtd_id[mtd_id_len - 1] = '\0';
1453 INIT_LIST_HEAD(&id->link);
1455 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1456 MTD_DEV_TYPE(id->type), id->num,
1457 id->size, id->mtd_id);
1459 list_add_tail(&id->link, &mtdids);
1463 /* clean mtdids list and free allocated memory */
1464 list_for_each_safe(entry, n, &mtdids) {
1465 id_tmp = list_entry(entry, struct mtdids, link);
1476 * Parse and initialize global mtdids mapping and create global
1477 * device/partition list.
1479 * @return 0 on success, 1 otherwise
1481 int mtdparts_init(void)
1483 static int initialized = 0;
1484 const char *ids, *parts;
1485 const char *current_partition;
1487 char tmp_ep[PARTITION_MAXLEN];
1489 DEBUGF("\n---mtdparts_init---\n");
1491 INIT_LIST_HEAD(&mtdids);
1492 INIT_LIST_HEAD(&devices);
1493 memset(last_ids, 0, MTDIDS_MAXLEN);
1494 memset(last_parts, 0, MTDPARTS_MAXLEN);
1495 memset(last_partition, 0, PARTITION_MAXLEN);
1500 ids = getenv("mtdids");
1501 parts = getenv("mtdparts");
1502 current_partition = getenv("partition");
1504 /* save it for later parsing, cannot rely on current partition pointer
1505 * as 'partition' variable may be updated during init */
1507 if (current_partition)
1508 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1510 DEBUGF("last_ids : %s\n", last_ids);
1511 DEBUGF("env_ids : %s\n", ids);
1512 DEBUGF("last_parts: %s\n", last_parts);
1513 DEBUGF("env_parts : %s\n\n", parts);
1515 DEBUGF("last_partition : %s\n", last_partition);
1516 DEBUGF("env_partition : %s\n", current_partition);
1518 /* if mtdids varible is empty try to use defaults */
1520 if (mtdids_default) {
1521 DEBUGF("mtdids variable not defined, using default\n");
1522 ids = mtdids_default;
1523 setenv("mtdids", (char *)ids);
1525 printf("mtdids not defined, no default present\n");
1529 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1530 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1534 /* do no try to use defaults when mtdparts variable is not defined,
1535 * just check the length */
1537 printf("mtdparts variable not set, see 'help mtdparts'\n");
1539 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1540 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1544 /* check if we have already parsed those mtdids */
1545 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1550 if (parse_mtdids(ids) != 0) {
1551 device_delall(&devices);
1555 /* ok it's good, save new ids */
1556 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1559 /* parse partitions if either mtdparts or mtdids were updated */
1560 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1561 if (parse_mtdparts(parts) != 0)
1564 if (list_empty(&devices)) {
1565 printf("mtdparts_init: no valid partitions\n");
1569 /* ok it's good, save new parts */
1570 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1572 /* reset first partition from first dev from the list as current */
1573 current_dev = list_entry(devices.next, struct mtd_device, link);
1574 current_partnum = 0;
1577 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1578 MTD_DEV_TYPE(current_dev->id->type),
1579 current_dev->id->num, current_partnum);
1582 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1583 if (!parts && (last_parts[0] != '\0'))
1584 return devices_init();
1586 /* do not process current partition if mtdparts variable is null */
1590 /* is current partition set in environment? if so, use it */
1591 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1592 struct part_info *p;
1593 struct mtd_device *cdev;
1596 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1598 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1600 current_partnum = pnum;
1603 } else if (getenv("partition") == NULL) {
1604 DEBUGF("no partition variable set, setting...\n");
1610 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1612 * 'Static' version of command line mtdparts_init() routine. Single partition on
1613 * a single device configuration.
1617 * Parse and initialize global mtdids mapping and create global
1618 * device/partition list.
1620 * @return 0 on success, 1 otherwise
1622 int mtdparts_init(void)
1624 static int initialized = 0;
1628 DEBUGF("\n---mtdparts_init---\n");
1631 struct part_info *part;
1634 current_dev = (struct mtd_device *)
1635 malloc(sizeof(struct mtd_device) +
1636 sizeof(struct part_info) +
1637 sizeof(struct mtdids));
1639 printf("out of memory\n");
1642 memset(current_dev, 0, sizeof(struct mtd_device) +
1643 sizeof(struct part_info) + sizeof(struct mtdids));
1645 id = (struct mtdids *)(current_dev + 1);
1646 part = (struct part_info *)(id + 1);
1649 id->mtd_id = "single part";
1651 #if defined(CONFIG_JFFS2_DEV)
1652 dev_name = CONFIG_JFFS2_DEV;
1657 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1658 (device_validate(id->type, id->num, &size) != 0)) {
1659 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1664 INIT_LIST_HEAD(&id->link);
1666 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1667 id->type, id->num, id->size, id->mtd_id);
1670 part->name = "static";
1671 part->auto_name = 0;
1673 #if defined(CONFIG_JFFS2_PART_SIZE)
1674 part->size = CONFIG_JFFS2_PART_SIZE;
1676 part->size = SIZE_REMAINING;
1679 #if defined(CONFIG_JFFS2_PART_OFFSET)
1680 part->offset = CONFIG_JFFS2_PART_OFFSET;
1682 part->offset = 0x00000000;
1685 part->dev = current_dev;
1686 INIT_LIST_HEAD(&part->link);
1688 /* recalculate size if needed */
1689 if (part->size == SIZE_REMAINING)
1690 part->size = id->size - part->offset;
1692 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1693 part->name, part->size, part->offset);
1696 current_dev->id = id;
1697 INIT_LIST_HEAD(¤t_dev->link);
1698 current_dev->num_parts = 1;
1699 INIT_LIST_HEAD(¤t_dev->parts);
1700 list_add(&part->link, ¤t_dev->parts);
1705 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1708 * Return pointer to the partition of a requested number from a requested
1711 * @param dev device that is to be searched for a partition
1712 * @param part_num requested partition number
1713 * @return pointer to the part_info, NULL otherwise
1715 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1717 struct list_head *entry;
1718 struct part_info *part;
1724 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1725 part_num, MTD_DEV_TYPE(dev->id->type),
1726 dev->id->num, dev->id->mtd_id);
1728 if (part_num >= dev->num_parts) {
1729 printf("invalid partition number %d for device %s%d (%s)\n",
1730 part_num, MTD_DEV_TYPE(dev->id->type),
1731 dev->id->num, dev->id->mtd_id);
1735 /* locate partition number, return it */
1737 list_for_each(entry, &dev->parts) {
1738 part = list_entry(entry, struct part_info, link);
1740 if (part_num == num++) {
1748 /***************************************************/
1749 /* U-boot commands */
1750 /***************************************************/
1753 * Routine implementing fsload u-boot command. This routine tries to load
1754 * a requested file from jffs2/cramfs filesystem on a current partition.
1756 * @param cmdtp command internal data
1757 * @param flag command flag
1758 * @param argc number of arguments supplied to the command
1759 * @param argv arguments list
1760 * @return 0 on success, 1 otherwise
1762 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1767 struct part_info *part;
1768 ulong offset = load_addr;
1770 /* pre-set Boot file name */
1771 if ((filename = getenv("bootfile")) == NULL) {
1772 filename = "uImage";
1779 offset = simple_strtoul(argv[1], NULL, 16);
1784 /* make sure we are in sync with env variables */
1785 if (mtdparts_init() !=0)
1788 if ((part = jffs2_part_info(current_dev, current_partnum))){
1790 /* check partition type for cramfs */
1791 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1792 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1794 if (cramfs_check(part)) {
1795 size = cramfs_load ((char *) offset, part, filename);
1797 /* if this is not cramfs assume jffs2 */
1798 size = jffs2_1pass_load((char *)offset, part, filename);
1803 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1804 fsname, size, offset);
1805 sprintf(buf, "%x", size);
1806 setenv("filesize", buf);
1808 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
1817 * Routine implementing u-boot ls command which lists content of a given
1818 * directory on a current partition.
1820 * @param cmdtp command internal data
1821 * @param flag command flag
1822 * @param argc number of arguments supplied to the command
1823 * @param argv arguments list
1824 * @return 0 on success, 1 otherwise
1826 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1828 char *filename = "/";
1830 struct part_info *part;
1835 /* make sure we are in sync with env variables */
1836 if (mtdparts_init() !=0)
1839 if ((part = jffs2_part_info(current_dev, current_partnum))){
1841 /* check partition type for cramfs */
1842 if (cramfs_check(part)) {
1843 ret = cramfs_ls (part, filename);
1845 /* if this is not cramfs assume jffs2 */
1846 ret = jffs2_1pass_ls(part, filename);
1855 * Routine implementing u-boot fsinfo command. This routine prints out
1856 * miscellaneous filesystem informations/statistics.
1858 * @param cmdtp command internal data
1859 * @param flag command flag
1860 * @param argc number of arguments supplied to the command
1861 * @param argv arguments list
1862 * @return 0 on success, 1 otherwise
1864 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1866 struct part_info *part;
1870 /* make sure we are in sync with env variables */
1871 if (mtdparts_init() !=0)
1874 if ((part = jffs2_part_info(current_dev, current_partnum))){
1876 /* check partition type for cramfs */
1877 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1878 printf("### filesystem type is %s\n", fsname);
1880 if (cramfs_check(part)) {
1881 ret = cramfs_info (part);
1883 /* if this is not cramfs assume jffs2 */
1884 ret = jffs2_1pass_info(part);
1892 /* command line only */
1893 #ifdef CONFIG_JFFS2_CMDLINE
1895 * Routine implementing u-boot chpart command. Sets new current partition based
1896 * on the user supplied partition id. For partition id format see find_dev_and_part().
1898 * @param cmdtp command internal data
1899 * @param flag command flag
1900 * @param argc number of arguments supplied to the command
1901 * @param argv arguments list
1902 * @return 0 on success, 1 otherwise
1904 int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1906 /* command line only */
1907 struct mtd_device *dev;
1908 struct part_info *part;
1911 if (mtdparts_init() !=0)
1915 printf("no partition id specified\n");
1919 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1923 current_partnum = pnum;
1926 printf("partition changed to %s%d,%d\n",
1927 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
1933 * Routine implementing u-boot mtdparts command. Initialize/update default global
1934 * partition list and process user partition request (list, add, del).
1936 * @param cmdtp command internal data
1937 * @param flag command flag
1938 * @param argc number of arguments supplied to the command
1939 * @param argv arguments list
1940 * @return 0 on success, 1 otherwise
1942 int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1945 if (strcmp(argv[1], "default") == 0) {
1946 setenv("mtdids", (char *)mtdids_default);
1947 setenv("mtdparts", (char *)mtdparts_default);
1948 setenv("partition", NULL);
1952 } else if (strcmp(argv[1], "delall") == 0) {
1953 /* this may be the first run, initialize lists if needed */
1956 setenv("mtdparts", NULL);
1958 /* devices_init() calls current_save() */
1959 return devices_init();
1963 /* make sure we are in sync with env variables */
1964 if (mtdparts_init() != 0)
1972 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
1973 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
1974 #define PART_ADD_DESC_MAXLEN 64
1975 char tmpbuf[PART_ADD_DESC_MAXLEN];
1977 struct mtd_device *dev;
1978 struct mtd_device *dev_tmp;
1980 struct part_info *p;
1982 if (id_parse(argv[2], NULL, &type, &num) != 0)
1985 if ((id = id_find(type, num)) == NULL) {
1986 printf("no such device %s defined in mtdids variable\n", argv[2]);
1990 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
1991 len += strlen(argv[3]); /* size@offset */
1992 len += strlen(argv[4]) + 2; /* '(' name ')' */
1993 if (argv[5] && (strlen(argv[5]) == 2))
1994 len += 2; /* 'ro' */
1996 if (len >= PART_ADD_DESC_MAXLEN) {
1997 printf("too long partition description\n");
2000 sprintf(tmpbuf, "%s:%s(%s)%s",
2001 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
2002 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2004 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2007 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2008 dev->id->num, dev->id->mtd_id);
2010 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2013 /* merge new partition with existing ones*/
2014 p = list_entry(dev->parts.next, struct part_info, link);
2015 if (part_add(dev_tmp, p) != 0) {
2021 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2022 printf("generated mtdparts too long, reseting to null\n");
2029 /* mtdparts del part-id */
2030 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2031 DEBUGF("del: part-id = %s\n", argv[2]);
2033 return delete_partition(argv[2]);
2036 printf ("Usage:\n%s\n", cmdtp->usage);
2039 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2041 /***************************************************/
2043 fsload, 3, 0, do_jffs2_fsload,
2044 "fsload\t- load binary file from a filesystem image\n",
2045 "[ off ] [ filename ]\n"
2046 " - load binary file from flash bank\n"
2047 " with offset 'off'\n"
2050 ls, 2, 1, do_jffs2_ls,
2051 "ls\t- list files in a directory (default /)\n",
2053 " - list files in a directory.\n"
2057 fsinfo, 1, 1, do_jffs2_fsinfo,
2058 "fsinfo\t- print information about filesystems\n",
2059 " - print information about filesystems\n"
2062 #ifdef CONFIG_JFFS2_CMDLINE
2064 chpart, 2, 0, do_jffs2_chpart,
2065 "chpart\t- change active partition\n",
2067 " - change active partition (e.g. part-id = nand0,1)\n"
2071 mtdparts, 6, 0, do_jffs2_mtdparts,
2072 "mtdparts- define flash/nand partitions\n",
2074 " - list partition table\n"
2076 " - delete all partitions\n"
2077 "mtdparts del part-id\n"
2078 " - delete partition (e.g. part-id = nand0,1)\n"
2079 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2080 " - add partition\n"
2081 "mtdparts default\n"
2082 " - reset partition table to defaults\n\n"
2084 "this command uses three environment variables:\n\n"
2085 "'partition' - keeps current partition identifier\n\n"
2086 "partition := <part-id>\n"
2087 "<part-id> := <dev-id>,part_num\n\n"
2088 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2089 "mtdids=<idmap>[,<idmap>,...]\n\n"
2090 "<idmap> := <dev-id>=<mtd-id>\n"
2091 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2092 "<dev-num> := mtd device number, 0...\n"
2093 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2094 "'mtdparts' - partition list\n\n"
2095 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2096 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2097 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2098 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2099 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2100 "<offset> := partition start offset within the device\n"
2101 "<name> := '(' NAME ')'\n"
2102 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2104 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2106 /***************************************************/
2108 #endif /* CFG_CMD_JFFS2 */