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 (CONFIG_COMMANDS & CFG_CMD_JFFS2)
99 #include <cramfs/cramfs_fs.h>
101 #if (CONFIG_COMMANDS & CFG_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 */
108 #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
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 (CONFIG_COMMANDS & CFG_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) && (CONFIG_COMMANDS & CFG_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 (CONFIG_COMMANDS & CFG_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) && (CONFIG_COMMANDS & CFG_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
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 u8 type, dnum, pnum;
1315 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1322 if (id_parse(p, &p, &type, &dnum) != 0)
1325 if ((*p++ != ',') || (*p == '\0')) {
1326 printf("no partition number specified\n");
1329 pnum = simple_strtoul(p, (char **)&p, 0);
1331 printf("unexpected trailing character '%c'\n", *p);
1335 if ((*dev = device_find(type, dnum)) == NULL) {
1336 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1340 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1341 printf("no such partition\n");
1352 * Find and delete partition. For partition id format see find_dev_and_part().
1354 * @param id string describing device and partition
1355 * @return 0 on success, 1 otherwise
1357 static int delete_partition(const char *id)
1360 struct mtd_device *dev;
1361 struct part_info *part;
1363 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1365 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1366 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1367 part->name, part->size, part->offset);
1369 if (part_del(dev, part) != 0)
1372 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1373 printf("generated mtdparts too long, reseting to null\n");
1379 printf("partition %s not found\n", id);
1384 * Accept character string describing mtd partitions and call device_parse()
1385 * for each entry. Add created devices to the global devices list.
1387 * @param mtdparts string specifing mtd partitions
1388 * @return 0 on success, 1 otherwise
1390 static int parse_mtdparts(const char *const mtdparts)
1392 const char *p = mtdparts;
1393 struct mtd_device *dev;
1396 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1398 /* delete all devices and partitions */
1399 if (devices_init() != 0) {
1400 printf("could not initialise device list\n");
1404 /* re-read 'mtdparts' variable, devices_init may be updating env */
1405 p = getenv("mtdparts");
1407 if (strncmp(p, "mtdparts=", 9) != 0) {
1408 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1413 while (p && (*p != '\0')) {
1415 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1418 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1419 dev->id->num, dev->id->mtd_id);
1421 /* check if parsed device is already on the list */
1422 if (device_find(dev->id->type, dev->id->num) != NULL) {
1423 printf("device %s%d redefined, please correct mtdparts variable\n",
1424 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1428 list_add_tail(&dev->link, &devices);
1432 device_delall(&devices);
1440 * Parse provided string describing mtdids mapping (see file header for mtdids
1441 * variable format). Allocate memory for each entry and add all found entries
1442 * to the global mtdids list.
1444 * @param ids mapping string
1445 * @return 0 on success, 1 otherwise
1447 static int parse_mtdids(const char *const ids)
1449 const char *p = ids;
1453 struct list_head *entry, *n;
1454 struct mtdids *id_tmp;
1459 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1461 /* clean global mtdids list */
1462 list_for_each_safe(entry, n, &mtdids) {
1463 id_tmp = list_entry(entry, struct mtdids, link);
1464 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1469 INIT_LIST_HEAD(&mtdids);
1471 while(p && (*p != '\0')) {
1474 /* parse 'nor'|'nand'<dev-num> */
1475 if (id_parse(p, &p, &type, &num) != 0)
1479 printf("mtdids: incorrect <dev-num>\n");
1484 /* check if requested device exists */
1485 if (device_validate(type, num, &size) != 0)
1488 /* locate <mtd-id> */
1490 if ((p = strchr(mtd_id, ',')) != NULL) {
1491 mtd_id_len = p - mtd_id + 1;
1494 mtd_id_len = strlen(mtd_id) + 1;
1496 if (mtd_id_len == 0) {
1497 printf("mtdids: no <mtd-id> identifier\n");
1501 /* check if this id is already on the list */
1502 int double_entry = 0;
1503 list_for_each(entry, &mtdids) {
1504 id_tmp = list_entry(entry, struct mtdids, link);
1505 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1511 printf("device id %s%d redefined, please correct mtdids variable\n",
1512 MTD_DEV_TYPE(type), num);
1516 /* allocate mtdids structure */
1517 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1518 printf("out of memory\n");
1521 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1525 id->mtd_id = (char *)(id + 1);
1526 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1527 id->mtd_id[mtd_id_len - 1] = '\0';
1528 INIT_LIST_HEAD(&id->link);
1530 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1531 MTD_DEV_TYPE(id->type), id->num,
1532 id->size, id->mtd_id);
1534 list_add_tail(&id->link, &mtdids);
1538 /* clean mtdids list and free allocated memory */
1539 list_for_each_safe(entry, n, &mtdids) {
1540 id_tmp = list_entry(entry, struct mtdids, link);
1551 * Parse and initialize global mtdids mapping and create global
1552 * device/partition list.
1554 * @return 0 on success, 1 otherwise
1556 int mtdparts_init(void)
1558 static int initialized = 0;
1559 const char *ids, *parts;
1560 const char *current_partition;
1562 char tmp_ep[PARTITION_MAXLEN];
1564 DEBUGF("\n---mtdparts_init---\n");
1566 INIT_LIST_HEAD(&mtdids);
1567 INIT_LIST_HEAD(&devices);
1568 memset(last_ids, 0, MTDIDS_MAXLEN);
1569 memset(last_parts, 0, MTDPARTS_MAXLEN);
1570 memset(last_partition, 0, PARTITION_MAXLEN);
1575 ids = getenv("mtdids");
1576 parts = getenv("mtdparts");
1577 current_partition = getenv("partition");
1579 /* save it for later parsing, cannot rely on current partition pointer
1580 * as 'partition' variable may be updated during init */
1582 if (current_partition)
1583 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1585 DEBUGF("last_ids : %s\n", last_ids);
1586 DEBUGF("env_ids : %s\n", ids);
1587 DEBUGF("last_parts: %s\n", last_parts);
1588 DEBUGF("env_parts : %s\n\n", parts);
1590 DEBUGF("last_partition : %s\n", last_partition);
1591 DEBUGF("env_partition : %s\n", current_partition);
1593 /* if mtdids varible is empty try to use defaults */
1595 if (mtdids_default) {
1596 DEBUGF("mtdids variable not defined, using default\n");
1597 ids = mtdids_default;
1598 setenv("mtdids", (char *)ids);
1600 printf("mtdids not defined, no default present\n");
1604 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1605 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1609 /* do no try to use defaults when mtdparts variable is not defined,
1610 * just check the length */
1612 printf("mtdparts variable not set, see 'help mtdparts'\n");
1614 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1615 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1619 /* check if we have already parsed those mtdids */
1620 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1625 if (parse_mtdids(ids) != 0) {
1630 /* ok it's good, save new ids */
1631 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1634 /* parse partitions if either mtdparts or mtdids were updated */
1635 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1636 if (parse_mtdparts(parts) != 0)
1639 if (list_empty(&devices)) {
1640 printf("mtdparts_init: no valid partitions\n");
1644 /* ok it's good, save new parts */
1645 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1647 /* reset first partition from first dev from the list as current */
1648 current_dev = list_entry(devices.next, struct mtd_device, link);
1649 current_partnum = 0;
1652 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1653 MTD_DEV_TYPE(current_dev->id->type),
1654 current_dev->id->num, current_partnum);
1657 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1658 if (!parts && (last_parts[0] != '\0'))
1659 return devices_init();
1661 /* do not process current partition if mtdparts variable is null */
1665 /* is current partition set in environment? if so, use it */
1666 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1667 struct part_info *p;
1668 struct mtd_device *cdev;
1671 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1673 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1675 current_partnum = pnum;
1678 } else if (getenv("partition") == NULL) {
1679 DEBUGF("no partition variable set, setting...\n");
1685 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1687 * 'Static' version of command line mtdparts_init() routine. Single partition on
1688 * a single device configuration.
1692 * Parse and initialize global mtdids mapping and create global
1693 * device/partition list.
1695 * @return 0 on success, 1 otherwise
1697 int mtdparts_init(void)
1699 static int initialized = 0;
1703 DEBUGF("\n---mtdparts_init---\n");
1706 struct part_info *part;
1709 current_dev = (struct mtd_device *)
1710 malloc(sizeof(struct mtd_device) +
1711 sizeof(struct part_info) +
1712 sizeof(struct mtdids));
1714 printf("out of memory\n");
1717 memset(current_dev, 0, sizeof(struct mtd_device) +
1718 sizeof(struct part_info) + sizeof(struct mtdids));
1720 id = (struct mtdids *)(current_dev + 1);
1721 part = (struct part_info *)(id + 1);
1724 id->mtd_id = "single part";
1726 #if defined(CONFIG_JFFS2_DEV)
1727 dev_name = CONFIG_JFFS2_DEV;
1732 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1733 (device_validate(id->type, id->num, &size) != 0)) {
1734 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1739 INIT_LIST_HEAD(&id->link);
1741 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1742 id->type, id->num, id->size, id->mtd_id);
1745 part->name = "static";
1746 part->auto_name = 0;
1748 #if defined(CONFIG_JFFS2_PART_SIZE)
1749 part->size = CONFIG_JFFS2_PART_SIZE;
1751 part->size = SIZE_REMAINING;
1754 #if defined(CONFIG_JFFS2_PART_OFFSET)
1755 part->offset = CONFIG_JFFS2_PART_OFFSET;
1757 part->offset = 0x00000000;
1760 part->dev = current_dev;
1761 INIT_LIST_HEAD(&part->link);
1763 /* recalculate size if needed */
1764 if (part->size == SIZE_REMAINING)
1765 part->size = id->size - part->offset;
1767 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1768 part->name, part->size, part->offset);
1771 current_dev->id = id;
1772 INIT_LIST_HEAD(¤t_dev->link);
1773 current_dev->num_parts = 1;
1774 INIT_LIST_HEAD(¤t_dev->parts);
1775 list_add(&part->link, ¤t_dev->parts);
1780 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1783 * Return pointer to the partition of a requested number from a requested
1786 * @param dev device that is to be searched for a partition
1787 * @param part_num requested partition number
1788 * @return pointer to the part_info, NULL otherwise
1790 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1792 struct list_head *entry;
1793 struct part_info *part;
1799 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1800 part_num, MTD_DEV_TYPE(dev->id->type),
1801 dev->id->num, dev->id->mtd_id);
1803 if (part_num >= dev->num_parts) {
1804 printf("invalid partition number %d for device %s%d (%s)\n",
1805 part_num, MTD_DEV_TYPE(dev->id->type),
1806 dev->id->num, dev->id->mtd_id);
1810 /* locate partition number, return it */
1812 list_for_each(entry, &dev->parts) {
1813 part = list_entry(entry, struct part_info, link);
1815 if (part_num == num++) {
1823 /***************************************************/
1824 /* U-boot commands */
1825 /***************************************************/
1828 * Routine implementing fsload u-boot command. This routine tries to load
1829 * a requested file from jffs2/cramfs filesystem on a current partition.
1831 * @param cmdtp command internal data
1832 * @param flag command flag
1833 * @param argc number of arguments supplied to the command
1834 * @param argv arguments list
1835 * @return 0 on success, 1 otherwise
1837 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1842 struct part_info *part;
1843 ulong offset = load_addr;
1845 /* pre-set Boot file name */
1846 if ((filename = getenv("bootfile")) == NULL) {
1847 filename = "uImage";
1854 offset = simple_strtoul(argv[1], NULL, 16);
1859 /* make sure we are in sync with env variables */
1860 if (mtdparts_init() !=0)
1863 if ((part = jffs2_part_info(current_dev, current_partnum))){
1865 /* check partition type for cramfs */
1866 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1867 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1869 if (cramfs_check(part)) {
1870 size = cramfs_load ((char *) offset, part, filename);
1872 /* if this is not cramfs assume jffs2 */
1873 size = jffs2_1pass_load((char *)offset, part, filename);
1878 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1879 fsname, size, offset);
1880 sprintf(buf, "%x", size);
1881 setenv("filesize", buf);
1883 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
1892 * Routine implementing u-boot ls command which lists content of a given
1893 * directory on a current partition.
1895 * @param cmdtp command internal data
1896 * @param flag command flag
1897 * @param argc number of arguments supplied to the command
1898 * @param argv arguments list
1899 * @return 0 on success, 1 otherwise
1901 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1903 char *filename = "/";
1905 struct part_info *part;
1910 /* make sure we are in sync with env variables */
1911 if (mtdparts_init() !=0)
1914 if ((part = jffs2_part_info(current_dev, current_partnum))){
1916 /* check partition type for cramfs */
1917 if (cramfs_check(part)) {
1918 ret = cramfs_ls (part, filename);
1920 /* if this is not cramfs assume jffs2 */
1921 ret = jffs2_1pass_ls(part, filename);
1930 * Routine implementing u-boot fsinfo command. This routine prints out
1931 * miscellaneous filesystem informations/statistics.
1933 * @param cmdtp command internal data
1934 * @param flag command flag
1935 * @param argc number of arguments supplied to the command
1936 * @param argv arguments list
1937 * @return 0 on success, 1 otherwise
1939 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1941 struct part_info *part;
1945 /* make sure we are in sync with env variables */
1946 if (mtdparts_init() !=0)
1949 if ((part = jffs2_part_info(current_dev, current_partnum))){
1951 /* check partition type for cramfs */
1952 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
1953 printf("### filesystem type is %s\n", fsname);
1955 if (cramfs_check(part)) {
1956 ret = cramfs_info (part);
1958 /* if this is not cramfs assume jffs2 */
1959 ret = jffs2_1pass_info(part);
1967 /* command line only */
1968 #ifdef CONFIG_JFFS2_CMDLINE
1970 * Routine implementing u-boot chpart command. Sets new current partition based
1971 * on the user supplied partition id. For partition id format see find_dev_and_part().
1973 * @param cmdtp command internal data
1974 * @param flag command flag
1975 * @param argc number of arguments supplied to the command
1976 * @param argv arguments list
1977 * @return 0 on success, 1 otherwise
1979 int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1981 /* command line only */
1982 struct mtd_device *dev;
1983 struct part_info *part;
1986 if (mtdparts_init() !=0)
1990 printf("no partition id specified\n");
1994 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
1998 current_partnum = pnum;
2001 printf("partition changed to %s%d,%d\n",
2002 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2008 * Routine implementing u-boot mtdparts command. Initialize/update default global
2009 * partition list and process user partition request (list, add, del).
2011 * @param cmdtp command internal data
2012 * @param flag command flag
2013 * @param argc number of arguments supplied to the command
2014 * @param argv arguments list
2015 * @return 0 on success, 1 otherwise
2017 int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2020 if (strcmp(argv[1], "default") == 0) {
2021 setenv("mtdids", (char *)mtdids_default);
2022 setenv("mtdparts", (char *)mtdparts_default);
2023 setenv("partition", NULL);
2027 } else if (strcmp(argv[1], "delall") == 0) {
2028 /* this may be the first run, initialize lists if needed */
2031 setenv("mtdparts", NULL);
2033 /* devices_init() calls current_save() */
2034 return devices_init();
2038 /* make sure we are in sync with env variables */
2039 if (mtdparts_init() != 0)
2047 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2048 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
2049 #define PART_ADD_DESC_MAXLEN 64
2050 char tmpbuf[PART_ADD_DESC_MAXLEN];
2052 struct mtd_device *dev;
2053 struct mtd_device *dev_tmp;
2055 struct part_info *p;
2057 if (id_parse(argv[2], NULL, &type, &num) != 0)
2060 if ((id = id_find(type, num)) == NULL) {
2061 printf("no such device %s defined in mtdids variable\n", argv[2]);
2065 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2066 len += strlen(argv[3]); /* size@offset */
2067 len += strlen(argv[4]) + 2; /* '(' name ')' */
2068 if (argv[5] && (strlen(argv[5]) == 2))
2069 len += 2; /* 'ro' */
2071 if (len >= PART_ADD_DESC_MAXLEN) {
2072 printf("too long partition description\n");
2075 sprintf(tmpbuf, "%s:%s(%s)%s",
2076 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
2077 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2079 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2082 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2083 dev->id->num, dev->id->mtd_id);
2085 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2088 /* merge new partition with existing ones*/
2089 p = list_entry(dev->parts.next, struct part_info, link);
2090 if (part_add(dev_tmp, p) != 0) {
2096 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2097 printf("generated mtdparts too long, reseting to null\n");
2104 /* mtdparts del part-id */
2105 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2106 DEBUGF("del: part-id = %s\n", argv[2]);
2108 return delete_partition(argv[2]);
2111 printf ("Usage:\n%s\n", cmdtp->usage);
2114 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2116 /***************************************************/
2118 fsload, 3, 0, do_jffs2_fsload,
2119 "fsload\t- load binary file from a filesystem image\n",
2120 "[ off ] [ filename ]\n"
2121 " - load binary file from flash bank\n"
2122 " with offset 'off'\n"
2125 ls, 2, 1, do_jffs2_ls,
2126 "ls\t- list files in a directory (default /)\n",
2128 " - list files in a directory.\n"
2132 fsinfo, 1, 1, do_jffs2_fsinfo,
2133 "fsinfo\t- print information about filesystems\n",
2134 " - print information about filesystems\n"
2137 #ifdef CONFIG_JFFS2_CMDLINE
2139 chpart, 2, 0, do_jffs2_chpart,
2140 "chpart\t- change active partition\n",
2142 " - change active partition (e.g. part-id = nand0,1)\n"
2146 mtdparts, 6, 0, do_jffs2_mtdparts,
2147 "mtdparts- define flash/nand partitions\n",
2149 " - list partition table\n"
2151 " - delete all partitions\n"
2152 "mtdparts del part-id\n"
2153 " - delete partition (e.g. part-id = nand0,1)\n"
2154 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2155 " - add partition\n"
2156 "mtdparts default\n"
2157 " - reset partition table to defaults\n\n"
2159 "this command uses three environment variables:\n\n"
2160 "'partition' - keeps current partition identifier\n\n"
2161 "partition := <part-id>\n"
2162 "<part-id> := <dev-id>,part_num\n\n"
2163 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2164 "mtdids=<idmap>[,<idmap>,...]\n\n"
2165 "<idmap> := <dev-id>=<mtd-id>\n"
2166 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2167 "<dev-num> := mtd device number, 0...\n"
2168 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2169 "'mtdparts' - partition list\n\n"
2170 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2171 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2172 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2173 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2174 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2175 "<offset> := partition start offset within the device\n"
2176 "<name> := '(' NAME ')'\n"
2177 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2179 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2181 /***************************************************/
2183 #endif /* CFG_CMD_JFFS2 */