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/ROMFS support
93 #include <jffs2/jffs2.h>
94 #include <linux/list.h>
95 #include <linux/ctype.h>
97 #if defined(CONFIG_CMD_JFFS2)
99 #include <cramfs/cramfs_fs.h>
101 #if defined(CONFIG_CMD_NAND)
102 #ifdef CFG_NAND_LEGACY
103 #include <linux/mtd/nand_legacy.h>
104 #else /* !CFG_NAND_LEGACY */
105 #include <linux/mtd/nand.h>
107 #endif /* !CFG_NAND_LEGACY */
109 /* enable/disable debugging messages */
114 # define DEBUGF(fmt, args...) printf(fmt ,##args)
116 # define DEBUGF(fmt, args...)
119 /* special size referring to all the remaining space in a partition */
120 #define SIZE_REMAINING 0xFFFFFFFF
122 /* special offset value, it is used when not provided by user
124 * this value is used temporarily during parsing, later such offests
125 * are recalculated */
126 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
128 /* minimum partition size */
129 #define MIN_PART_SIZE 4096
131 /* this flag needs to be set in part_info struct mask_flags
132 * field for read-only partitions */
133 #define MTD_WRITEABLE_CMD 1
135 #ifdef CONFIG_JFFS2_CMDLINE
136 /* default values for mtdids and mtdparts variables */
137 #if defined(MTDIDS_DEFAULT)
138 static const char *const mtdids_default = MTDIDS_DEFAULT;
140 #warning "MTDIDS_DEFAULT not defined!"
141 static const char *const mtdids_default = NULL;
144 #if defined(MTDPARTS_DEFAULT)
145 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
147 #warning "MTDPARTS_DEFAULT not defined!"
148 static const char *const mtdparts_default = NULL;
151 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
152 #define MTDIDS_MAXLEN 128
153 #define MTDPARTS_MAXLEN 512
154 #define PARTITION_MAXLEN 16
155 static char last_ids[MTDIDS_MAXLEN];
156 static char last_parts[MTDPARTS_MAXLEN];
157 static char last_partition[PARTITION_MAXLEN];
159 /* low level jffs2 cache cleaning routine */
160 extern void jffs2_free_cache(struct part_info *part);
162 /* mtdids mapping list, filled by parse_ids() */
163 struct list_head mtdids;
165 /* device/partition list, parse_cmdline() parses into here */
166 struct list_head devices;
167 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
169 /* current active device and partition number */
170 static struct mtd_device *current_dev = NULL;
171 static u8 current_partnum = 0;
173 extern int cramfs_check (struct part_info *info);
174 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
175 extern int cramfs_ls (struct part_info *info, char *filename);
176 extern int cramfs_info (struct part_info *info);
178 extern int romfs_check (struct part_info *info);
179 extern int romfs_load (char *loadoffset, struct part_info *info, char *filename);
180 extern int romfs_ls (struct part_info *info, char *filename);
181 extern int romfs_info (struct part_info *info);
183 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
185 /* command line only routines */
186 #ifdef CONFIG_JFFS2_CMDLINE
188 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
189 static int device_del(struct mtd_device *dev);
192 * Parses a string into a number. The number stored at ptr is
193 * potentially suffixed with K (for kilobytes, or 1024 bytes),
194 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
195 * 1073741824). If the number is suffixed with K, M, or G, then
196 * the return value is the number multiplied by one kilobyte, one
197 * megabyte, or one gigabyte, respectively.
199 * @param ptr where parse begins
200 * @param retptr output pointer to next char after parse completes (output)
201 * @return resulting unsigned int
203 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
205 unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
226 * Format string describing supplied size. This routine does the opposite job
227 * to memsize_parse(). Size in bytes is converted to string and if possible
228 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
230 * Note, that this routine does not check for buffer overflow, it's the caller
231 * who must assure enough space.
233 * @param buf output buffer
234 * @param size size to be converted to string
236 static void memsize_format(char *buf, u32 size)
238 #define SIZE_GB ((u32)1024*1024*1024)
239 #define SIZE_MB ((u32)1024*1024)
240 #define SIZE_KB ((u32)1024)
242 if ((size % SIZE_GB) == 0)
243 sprintf(buf, "%lug", size/SIZE_GB);
244 else if ((size % SIZE_MB) == 0)
245 sprintf(buf, "%lum", size/SIZE_MB);
246 else if (size % SIZE_KB == 0)
247 sprintf(buf, "%luk", size/SIZE_KB);
249 sprintf(buf, "%lu", size);
253 * This routine does global indexing of all partitions. Resulting index for
254 * current partition is saved in 'mtddevnum'. Current partition name in
257 static void index_partitions(void)
261 struct part_info *part;
262 struct list_head *dentry;
263 struct mtd_device *dev;
265 DEBUGF("--- index partitions ---\n");
269 list_for_each(dentry, &devices) {
270 dev = list_entry(dentry, struct mtd_device, link);
271 if (dev == current_dev) {
272 mtddevnum += current_partnum;
273 sprintf(buf, "%d", mtddevnum);
274 setenv("mtddevnum", buf);
277 mtddevnum += dev->num_parts;
280 part = jffs2_part_info(current_dev, current_partnum);
281 setenv("mtddevname", part->name);
283 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
285 setenv("mtddevnum", NULL);
286 setenv("mtddevname", NULL);
288 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
293 * Save current device and partition in environment variable 'partition'.
295 static void current_save(void)
299 DEBUGF("--- current_save ---\n");
302 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
303 current_dev->id->num, current_partnum);
305 setenv("partition", buf);
306 strncpy(last_partition, buf, 16);
308 DEBUGF("=> partition %s\n", buf);
310 setenv("partition", NULL);
311 last_partition[0] = '\0';
313 DEBUGF("=> partition NULL\n");
319 * Performs sanity check for supplied NOR flash partition. Table of existing
320 * NOR flash devices is searched and partition device is located. Alignment
321 * with the granularity of NOR flash sectors is verified.
323 * @param id of the parent device
324 * @param part partition to validate
325 * @return 0 if partition is valid, 1 otherwise
327 static int part_validate_nor(struct mtdids *id, struct part_info *part)
329 #if defined(CONFIG_CMD_FLASH)
330 /* info for FLASH chips */
331 extern flash_info_t flash_info[];
337 flash = &flash_info[id->num];
340 for (i = 0; i < flash->sector_count; i++) {
341 if ((flash->start[i] - flash->start[0]) == part->offset) {
346 if (offset_aligned == 0) {
347 printf("%s%d: partition (%s) start offset alignment incorrect\n",
348 MTD_DEV_TYPE(id->type), id->num, part->name);
352 end_offset = part->offset + part->size;
353 for (i = 0; i < flash->sector_count; i++) {
354 if ((flash->start[i] - flash->start[0]) == end_offset)
358 if (flash->size == end_offset)
361 printf("%s%d: partition (%s) size alignment incorrect\n",
362 MTD_DEV_TYPE(id->type), id->num, part->name);
368 * Performs sanity check for supplied NAND flash partition. Table of existing
369 * NAND flash devices is searched and partition device is located. Alignment
370 * with the granularity of nand erasesize is verified.
372 * @param id of the parent device
373 * @param part partition to validate
374 * @return 0 if partition is valid, 1 otherwise
376 static int part_validate_nand(struct mtdids *id, struct part_info *part)
378 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
379 /* info for NAND chips */
382 nand = &nand_info[id->num];
384 if ((unsigned long)(part->offset) % nand->erasesize) {
385 printf("%s%d: partition (%s) start offset alignment incorrect\n",
386 MTD_DEV_TYPE(id->type), id->num, part->name);
390 if (part->size % nand->erasesize) {
391 printf("%s%d: partition (%s) size alignment incorrect\n",
392 MTD_DEV_TYPE(id->type), id->num, part->name);
403 * Performs sanity check for supplied partition. Offset and size are verified
404 * to be within valid range. Partition type is checked and either
405 * parts_validate_nor() or parts_validate_nand() is called with the argument
408 * @param id of the parent device
409 * @param part partition to validate
410 * @return 0 if partition is valid, 1 otherwise
412 static int part_validate(struct mtdids *id, struct part_info *part)
414 if (part->size == SIZE_REMAINING)
415 part->size = id->size - part->offset;
417 if (part->offset > id->size) {
418 printf("%s: offset %08lx beyond flash size %08lx\n",
419 id->mtd_id, part->offset, id->size);
423 if ((part->offset + part->size) <= part->offset) {
424 printf("%s%d: partition (%s) size too big\n",
425 MTD_DEV_TYPE(id->type), id->num, part->name);
429 if (part->offset + part->size > id->size) {
430 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
434 if (id->type == MTD_DEV_TYPE_NAND)
435 return part_validate_nand(id, part);
436 else if (id->type == MTD_DEV_TYPE_NOR)
437 return part_validate_nor(id, part);
439 DEBUGF("part_validate: invalid dev type\n");
445 * Delete selected partition from the partion list of the specified device.
447 * @param dev device to delete partition from
448 * @param part partition to delete
449 * @return 0 on success, 1 otherwise
451 static int part_del(struct mtd_device *dev, struct part_info *part)
453 u8 current_save_needed = 0;
455 /* if there is only one partition, remove whole device */
456 if (dev->num_parts == 1)
457 return device_del(dev);
459 /* otherwise just delete this partition */
461 if (dev == current_dev) {
462 /* we are modyfing partitions for the current device,
464 struct part_info *curr_pi;
465 curr_pi = jffs2_part_info(current_dev, current_partnum);
468 if (curr_pi == part) {
469 printf("current partition deleted, resetting current to 0\n");
471 } else if (part->offset <= curr_pi->offset) {
474 current_save_needed = 1;
478 #ifdef CFG_NAND_LEGACY
479 jffs2_free_cache(part);
481 list_del(&part->link);
485 if (current_save_needed > 0)
494 * Delete all partitions from parts head list, free memory.
496 * @param head list of partitions to delete
498 static void part_delall(struct list_head *head)
500 struct list_head *entry, *n;
501 struct part_info *part_tmp;
503 /* clean tmp_list and free allocated memory */
504 list_for_each_safe(entry, n, head) {
505 part_tmp = list_entry(entry, struct part_info, link);
507 #ifdef CFG_NAND_LEGACY
508 jffs2_free_cache(part_tmp);
516 * Add new partition to the supplied partition list. Make sure partitions are
517 * sorted by offset in ascending order.
519 * @param head list this partition is to be added to
520 * @param new partition to be added
522 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
524 struct list_head *entry;
525 struct part_info *new_pi, *curr_pi;
527 /* link partition to parrent dev */
530 if (list_empty(&dev->parts)) {
531 DEBUGF("part_sort_add: list empty\n");
532 list_add(&part->link, &dev->parts);
538 new_pi = list_entry(&part->link, struct part_info, link);
540 /* get current partition info if we are updating current device */
542 if (dev == current_dev)
543 curr_pi = jffs2_part_info(current_dev, current_partnum);
545 list_for_each(entry, &dev->parts) {
546 struct part_info *pi;
548 pi = list_entry(entry, struct part_info, link);
550 /* be compliant with kernel cmdline, allow only one partition at offset zero */
551 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
552 printf("cannot add second partition at offset 0\n");
556 if (new_pi->offset <= pi->offset) {
557 list_add_tail(&part->link, entry);
560 if (curr_pi && (pi->offset <= curr_pi->offset)) {
561 /* we are modyfing partitions for the current
562 * device, update current */
572 list_add_tail(&part->link, &dev->parts);
579 * Add provided partition to the partition list of a given device.
581 * @param dev device to which partition is added
582 * @param part partition to be added
583 * @return 0 on success, 1 otherwise
585 static int part_add(struct mtd_device *dev, struct part_info *part)
587 /* verify alignment and size */
588 if (part_validate(dev->id, part) != 0)
591 /* partition is ok, add it to the list */
592 if (part_sort_add(dev, part) != 0)
599 * Parse one partition definition, allocate memory and return pointer to this
600 * location in retpart.
602 * @param partdef pointer to the partition definition string i.e. <part-def>
603 * @param ret output pointer to next char after parse completes (output)
604 * @param retpart pointer to the allocated partition (output)
605 * @return 0 on success, 1 otherwise
607 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
609 struct part_info *part;
611 unsigned long offset;
614 unsigned int mask_flags;
621 /* fetch the partition size */
623 /* assign all remaining space to this partition */
624 DEBUGF("'-': remaining size assigned\n");
625 size = SIZE_REMAINING;
628 size = memsize_parse(p, &p);
629 if (size < MIN_PART_SIZE) {
630 printf("partition size too small (%lx)\n", size);
635 /* check for offset */
636 offset = OFFSET_NOT_SPECIFIED;
639 offset = memsize_parse(p, &p);
642 /* now look for the name */
645 if ((p = strchr(name, ')')) == NULL) {
646 printf("no closing ) found in partition name\n");
649 name_len = p - name + 1;
650 if ((name_len - 1) == 0) {
651 printf("empty partition name\n");
656 /* 0x00000000@0x00000000 */
661 /* test for options */
663 if (strncmp(p, "ro", 2) == 0) {
664 mask_flags |= MTD_WRITEABLE_CMD;
668 /* check for next partition definition */
670 if (size == SIZE_REMAINING) {
672 printf("no partitions allowed after a fill-up partition\n");
676 } else if ((*p == ';') || (*p == '\0')) {
679 printf("unexpected character '%c' at the end of partition\n", *p);
684 /* allocate memory */
685 part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
687 printf("out of memory\n");
690 memset(part, 0, sizeof(struct part_info) + name_len);
692 part->offset = offset;
693 part->mask_flags = mask_flags;
694 part->name = (char *)(part + 1);
697 /* copy user provided name */
698 strncpy(part->name, name, name_len - 1);
701 /* auto generated name in form of size@offset */
702 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
706 part->name[name_len - 1] = '\0';
707 INIT_LIST_HEAD(&part->link);
709 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
710 part->name, part->size,
711 part->offset, part->mask_flags);
716 #endif/* #ifdef CONFIG_JFFS2_CMDLINE */
719 * Check device number to be within valid range for given device type.
721 * @param dev device to validate
722 * @return 0 if device is valid, 1 otherwise
724 static int device_validate(u8 type, u8 num, u32 *size)
726 if (type == MTD_DEV_TYPE_NOR) {
727 #if defined(CONFIG_CMD_FLASH)
728 if (num < CFG_MAX_FLASH_BANKS) {
729 extern flash_info_t flash_info[];
730 *size = flash_info[num].size;
735 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
736 MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
738 printf("support for FLASH devices not present\n");
740 } else if (type == MTD_DEV_TYPE_NAND) {
741 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
742 if (num < CFG_MAX_NAND_DEVICE) {
743 #ifndef CFG_NAND_LEGACY
744 *size = nand_info[num].size;
746 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
747 *size = nand_dev_desc[num].totlen;
752 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
753 MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
755 printf("support for NAND devices not present\n");
762 #ifdef CONFIG_JFFS2_CMDLINE
764 * Delete all mtd devices from a supplied devices list, free memory allocated for
765 * each device and delete all device partitions.
767 * @return 0 on success, 1 otherwise
769 static int device_delall(struct list_head *head)
771 struct list_head *entry, *n;
772 struct mtd_device *dev_tmp;
774 /* clean devices list */
775 list_for_each_safe(entry, n, head) {
776 dev_tmp = list_entry(entry, struct mtd_device, link);
778 part_delall(&dev_tmp->parts);
781 INIT_LIST_HEAD(&devices);
787 * If provided device exists it's partitions are deleted, device is removed
788 * from device list and device memory is freed.
790 * @param dev device to be deleted
791 * @return 0 on success, 1 otherwise
793 static int device_del(struct mtd_device *dev)
795 part_delall(&dev->parts);
796 list_del(&dev->link);
799 if (dev == current_dev) {
800 /* we just deleted current device */
801 if (list_empty(&devices)) {
804 /* reset first partition from first dev from the
805 * devices list as current */
806 current_dev = list_entry(devices.next, struct mtd_device, link);
818 * Search global device list and return pointer to the device of type and num
821 * @param type device type
822 * @param num device number
823 * @return NULL if requested device does not exist
825 static struct mtd_device* device_find(u8 type, u8 num)
827 struct list_head *entry;
828 struct mtd_device *dev_tmp;
830 list_for_each(entry, &devices) {
831 dev_tmp = list_entry(entry, struct mtd_device, link);
833 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
841 * Add specified device to the global device list.
843 * @param dev device to be added
845 static void device_add(struct mtd_device *dev)
847 u8 current_save_needed = 0;
849 if (list_empty(&devices)) {
852 current_save_needed = 1;
855 list_add_tail(&dev->link, &devices);
857 if (current_save_needed > 0)
864 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
865 * return pointer to the device structure.
867 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
868 * @param ret output pointer to next char after parse completes (output)
869 * @param retdev pointer to the allocated device (output)
870 * @return 0 on success, 1 otherwise
872 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
874 struct mtd_device *dev;
875 struct part_info *part;
878 unsigned int mtd_id_len;
879 const char *p, *pend;
881 struct list_head *entry, *n;
890 DEBUGF("===device_parse===\n");
894 if (!(p = strchr(mtd_id, ':'))) {
895 printf("no <mtd-id> identifier\n");
898 mtd_id_len = p - mtd_id + 1;
901 /* verify if we have a valid device specified */
902 if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
903 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
907 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
908 id->type, MTD_DEV_TYPE(id->type),
909 id->num, id->mtd_id);
910 pend = strchr(p, ';');
911 DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
914 /* parse partitions */
918 if ((dev = device_find(id->type, id->num)) != NULL) {
919 /* if device already exists start at the end of the last partition */
920 part = list_entry(dev->parts.prev, struct part_info, link);
921 offset = part->offset + part->size;
924 while (p && (*p != '\0') && (*p != ';')) {
926 if ((part_parse(p, &p, &part) != 0) || (!part))
929 /* calculate offset when not specified */
930 if (part->offset == OFFSET_NOT_SPECIFIED)
931 part->offset = offset;
933 offset = part->offset;
935 /* verify alignment and size */
936 if (part_validate(id, part) != 0)
939 offset += part->size;
941 /* partition is ok, add it to the list */
942 list_add_tail(&part->link, &tmp_list);
947 part_delall(&tmp_list);
951 if (num_parts == 0) {
952 printf("no partitions for device %s%d (%s)\n",
953 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
957 DEBUGF("\ntotal partitions: %d\n", num_parts);
959 /* check for next device presence */
963 } else if (*p == '\0') {
966 printf("unexpected character '%c' at the end of device\n", *p);
972 /* allocate memory for mtd_device structure */
973 if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
974 printf("out of memory\n");
977 memset(dev, 0, sizeof(struct mtd_device));
979 dev->num_parts = 0; /* part_sort_add increments num_parts */
980 INIT_LIST_HEAD(&dev->parts);
981 INIT_LIST_HEAD(&dev->link);
983 /* move partitions from tmp_list to dev->parts */
984 list_for_each_safe(entry, n, &tmp_list) {
985 part = list_entry(entry, struct part_info, link);
987 if (part_sort_add(dev, part) != 0) {
1000 * Initialize global device list.
1002 * @return 0 on success, 1 otherwise
1004 static int devices_init(void)
1006 last_parts[0] = '\0';
1010 return device_delall(&devices);
1014 * Search global mtdids list and find id of requested type and number.
1016 * @return pointer to the id if it exists, NULL otherwise
1018 static struct mtdids* id_find(u8 type, u8 num)
1020 struct list_head *entry;
1023 list_for_each(entry, &mtdids) {
1024 id = list_entry(entry, struct mtdids, link);
1026 if ((id->type == type) && (id->num == num))
1034 * Search global mtdids list and find id of a requested mtd_id.
1036 * Note: first argument is not null terminated.
1038 * @param mtd_id string containing requested mtd_id
1039 * @param mtd_id_len length of supplied mtd_id
1040 * @return pointer to the id if it exists, NULL otherwise
1042 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1044 struct list_head *entry;
1047 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1048 mtd_id_len, mtd_id, mtd_id_len);
1050 list_for_each(entry, &mtdids) {
1051 id = list_entry(entry, struct mtdids, link);
1053 DEBUGF("entry: '%s' (len = %d)\n",
1054 id->mtd_id, strlen(id->mtd_id));
1056 if (mtd_id_len != strlen(id->mtd_id))
1058 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1064 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1067 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1070 * @param id string describing device id
1071 * @param ret_id output pointer to next char after parse completes (output)
1072 * @param dev_type parsed device type (output)
1073 * @param dev_num parsed device number (output)
1074 * @return 0 on success, 1 otherwise
1076 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1081 if (strncmp(p, "nand", 4) == 0) {
1082 *dev_type = MTD_DEV_TYPE_NAND;
1084 } else if (strncmp(p, "nor", 3) == 0) {
1085 *dev_type = MTD_DEV_TYPE_NOR;
1088 printf("incorrect device type in %s\n", id);
1093 printf("incorrect device number in %s\n", id);
1097 *dev_num = simple_strtoul(p, (char **)&p, 0);
1103 #ifdef CONFIG_JFFS2_CMDLINE
1105 * Process all devices and generate corresponding mtdparts string describing
1106 * all partitions on all devices.
1108 * @param buf output buffer holding generated mtdparts string (output)
1109 * @param buflen buffer size
1110 * @return 0 on success, 1 otherwise
1112 static int generate_mtdparts(char *buf, u32 buflen)
1114 struct list_head *pentry, *dentry;
1115 struct mtd_device *dev;
1116 struct part_info *part, *prev_part;
1119 u32 size, offset, len, part_cnt;
1120 u32 maxlen = buflen - 1;
1122 DEBUGF("--- generate_mtdparts ---\n");
1124 if (list_empty(&devices)) {
1129 sprintf(p, "mtdparts=");
1132 list_for_each(dentry, &devices) {
1133 dev = list_entry(dentry, struct mtd_device, link);
1136 len = strlen(dev->id->mtd_id) + 1;
1139 memcpy(p, dev->id->mtd_id, len - 1);
1144 /* format partitions */
1147 list_for_each(pentry, &dev->parts) {
1148 part = list_entry(pentry, struct part_info, link);
1150 offset = part->offset;
1153 /* partition size */
1154 memsize_format(tmpbuf, size);
1155 len = strlen(tmpbuf);
1158 memcpy(p, tmpbuf, len);
1163 /* add offset only when there is a gap between
1165 if ((!prev_part && (offset != 0)) ||
1166 (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1168 memsize_format(tmpbuf, offset);
1169 len = strlen(tmpbuf) + 1;
1173 memcpy(p, tmpbuf, len - 1);
1178 /* copy name only if user supplied */
1179 if(!part->auto_name) {
1180 len = strlen(part->name) + 2;
1185 memcpy(p, part->name, len - 2);
1192 if (part->mask_flags && MTD_WRITEABLE_CMD) {
1201 /* print ',' separator if there are other partitions
1203 if (dev->num_parts > part_cnt) {
1211 /* print ';' separator if there are other devices following */
1212 if (dentry->next != &devices) {
1220 /* we still have at least one char left, as we decremented maxlen at
1227 last_parts[0] = '\0';
1232 * Call generate_mtdparts to process all devices and generate corresponding
1233 * mtdparts string, save it in mtdparts environment variable.
1235 * @param buf output buffer holding generated mtdparts string (output)
1236 * @param buflen buffer size
1237 * @return 0 on success, 1 otherwise
1239 static int generate_mtdparts_save(char *buf, u32 buflen)
1243 ret = generate_mtdparts(buf, buflen);
1245 if ((buf[0] != '\0') && (ret == 0))
1246 setenv("mtdparts", buf);
1248 setenv("mtdparts", NULL);
1254 * Format and print out a partition list for each device from global device
1257 static void list_partitions(void)
1259 struct list_head *dentry, *pentry;
1260 struct part_info *part;
1261 struct mtd_device *dev;
1264 DEBUGF("\n---list_partitions---\n");
1265 list_for_each(dentry, &devices) {
1266 dev = list_entry(dentry, struct mtd_device, link);
1267 printf("\ndevice %s%d <%s>, # parts = %d\n",
1268 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1269 dev->id->mtd_id, dev->num_parts);
1270 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
1272 /* list partitions for given device */
1274 list_for_each(pentry, &dev->parts) {
1275 part = list_entry(pentry, struct part_info, link);
1276 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1277 part_num, part->name, part->size,
1278 part->offset, part->mask_flags);
1283 if (list_empty(&devices))
1284 printf("no partitions defined\n");
1286 /* current_dev is not NULL only when we have non empty device list */
1288 part = jffs2_part_info(current_dev, current_partnum);
1290 printf("\nactive partition: %s%d,%d - (%s) 0x%08lx @ 0x%08lx\n",
1291 MTD_DEV_TYPE(current_dev->id->type),
1292 current_dev->id->num, current_partnum,
1293 part->name, part->size, part->offset);
1295 printf("could not get current partition info\n\n");
1299 printf("\ndefaults:\n");
1300 printf("mtdids : %s\n", mtdids_default);
1301 printf("mtdparts: %s\n", mtdparts_default);
1305 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1306 * corresponding device and verify partition number.
1308 * @param id string describing device and partition or partition name
1309 * @param dev pointer to the requested device (output)
1310 * @param part_num verified partition number (output)
1311 * @param part pointer to requested partition (output)
1312 * @return 0 on success, 1 otherwise
1314 int find_dev_and_part(const char *id, struct mtd_device **dev,
1315 u8 *part_num, struct part_info **part)
1317 struct list_head *dentry, *pentry;
1318 u8 type, dnum, pnum;
1321 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1323 list_for_each(dentry, &devices) {
1325 *dev = list_entry(dentry, struct mtd_device, link);
1326 list_for_each(pentry, &(*dev)->parts) {
1327 *part = list_entry(pentry, struct part_info, link);
1328 if (strcmp((*part)->name, id) == 0)
1339 if (id_parse(p, &p, &type, &dnum) != 0)
1342 if ((*p++ != ',') || (*p == '\0')) {
1343 printf("no partition number specified\n");
1346 pnum = simple_strtoul(p, (char **)&p, 0);
1348 printf("unexpected trailing character '%c'\n", *p);
1352 if ((*dev = device_find(type, dnum)) == NULL) {
1353 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1357 if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1358 printf("no such partition\n");
1369 * Find and delete partition. For partition id format see find_dev_and_part().
1371 * @param id string describing device and partition
1372 * @return 0 on success, 1 otherwise
1374 static int delete_partition(const char *id)
1377 struct mtd_device *dev;
1378 struct part_info *part;
1380 if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1382 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1383 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1384 part->name, part->size, part->offset);
1386 if (part_del(dev, part) != 0)
1389 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1390 printf("generated mtdparts too long, reseting to null\n");
1396 printf("partition %s not found\n", id);
1401 * Accept character string describing mtd partitions and call device_parse()
1402 * for each entry. Add created devices to the global devices list.
1404 * @param mtdparts string specifing mtd partitions
1405 * @return 0 on success, 1 otherwise
1407 static int parse_mtdparts(const char *const mtdparts)
1409 const char *p = mtdparts;
1410 struct mtd_device *dev;
1413 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1415 /* delete all devices and partitions */
1416 if (devices_init() != 0) {
1417 printf("could not initialise device list\n");
1421 /* re-read 'mtdparts' variable, devices_init may be updating env */
1422 p = getenv("mtdparts");
1424 if (strncmp(p, "mtdparts=", 9) != 0) {
1425 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1430 while (p && (*p != '\0')) {
1432 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1435 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1436 dev->id->num, dev->id->mtd_id);
1438 /* check if parsed device is already on the list */
1439 if (device_find(dev->id->type, dev->id->num) != NULL) {
1440 printf("device %s%d redefined, please correct mtdparts variable\n",
1441 MTD_DEV_TYPE(dev->id->type), dev->id->num);
1445 list_add_tail(&dev->link, &devices);
1449 device_delall(&devices);
1457 * Parse provided string describing mtdids mapping (see file header for mtdids
1458 * variable format). Allocate memory for each entry and add all found entries
1459 * to the global mtdids list.
1461 * @param ids mapping string
1462 * @return 0 on success, 1 otherwise
1464 static int parse_mtdids(const char *const ids)
1466 const char *p = ids;
1470 struct list_head *entry, *n;
1471 struct mtdids *id_tmp;
1476 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1478 /* clean global mtdids list */
1479 list_for_each_safe(entry, n, &mtdids) {
1480 id_tmp = list_entry(entry, struct mtdids, link);
1481 DEBUGF("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1486 INIT_LIST_HEAD(&mtdids);
1488 while(p && (*p != '\0')) {
1491 /* parse 'nor'|'nand'<dev-num> */
1492 if (id_parse(p, &p, &type, &num) != 0)
1496 printf("mtdids: incorrect <dev-num>\n");
1501 /* check if requested device exists */
1502 if (device_validate(type, num, &size) != 0)
1505 /* locate <mtd-id> */
1507 if ((p = strchr(mtd_id, ',')) != NULL) {
1508 mtd_id_len = p - mtd_id + 1;
1511 mtd_id_len = strlen(mtd_id) + 1;
1513 if (mtd_id_len == 0) {
1514 printf("mtdids: no <mtd-id> identifier\n");
1518 /* check if this id is already on the list */
1519 int double_entry = 0;
1520 list_for_each(entry, &mtdids) {
1521 id_tmp = list_entry(entry, struct mtdids, link);
1522 if ((id_tmp->type == type) && (id_tmp->num == num)) {
1528 printf("device id %s%d redefined, please correct mtdids variable\n",
1529 MTD_DEV_TYPE(type), num);
1533 /* allocate mtdids structure */
1534 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1535 printf("out of memory\n");
1538 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1542 id->mtd_id = (char *)(id + 1);
1543 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1544 id->mtd_id[mtd_id_len - 1] = '\0';
1545 INIT_LIST_HEAD(&id->link);
1547 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1548 MTD_DEV_TYPE(id->type), id->num,
1549 id->size, id->mtd_id);
1551 list_add_tail(&id->link, &mtdids);
1555 /* clean mtdids list and free allocated memory */
1556 list_for_each_safe(entry, n, &mtdids) {
1557 id_tmp = list_entry(entry, struct mtdids, link);
1568 * Parse and initialize global mtdids mapping and create global
1569 * device/partition list.
1571 * @return 0 on success, 1 otherwise
1573 int mtdparts_init(void)
1575 static int initialized = 0;
1576 const char *ids, *parts;
1577 const char *current_partition;
1579 char tmp_ep[PARTITION_MAXLEN];
1581 DEBUGF("\n---mtdparts_init---\n");
1583 INIT_LIST_HEAD(&mtdids);
1584 INIT_LIST_HEAD(&devices);
1585 memset(last_ids, 0, MTDIDS_MAXLEN);
1586 memset(last_parts, 0, MTDPARTS_MAXLEN);
1587 memset(last_partition, 0, PARTITION_MAXLEN);
1592 ids = getenv("mtdids");
1593 parts = getenv("mtdparts");
1594 current_partition = getenv("partition");
1596 /* save it for later parsing, cannot rely on current partition pointer
1597 * as 'partition' variable may be updated during init */
1599 if (current_partition)
1600 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1602 DEBUGF("last_ids : %s\n", last_ids);
1603 DEBUGF("env_ids : %s\n", ids);
1604 DEBUGF("last_parts: %s\n", last_parts);
1605 DEBUGF("env_parts : %s\n\n", parts);
1607 DEBUGF("last_partition : %s\n", last_partition);
1608 DEBUGF("env_partition : %s\n", current_partition);
1610 /* if mtdids varible is empty try to use defaults */
1612 if (mtdids_default) {
1613 DEBUGF("mtdids variable not defined, using default\n");
1614 ids = mtdids_default;
1615 setenv("mtdids", (char *)ids);
1617 printf("mtdids not defined, no default present\n");
1621 if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1622 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1626 /* do no try to use defaults when mtdparts variable is not defined,
1627 * just check the length */
1629 printf("mtdparts variable not set, see 'help mtdparts'\n");
1631 if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1632 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1636 /* check if we have already parsed those mtdids */
1637 if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1642 if (parse_mtdids(ids) != 0) {
1647 /* ok it's good, save new ids */
1648 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1651 /* parse partitions if either mtdparts or mtdids were updated */
1652 if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1653 if (parse_mtdparts(parts) != 0)
1656 if (list_empty(&devices)) {
1657 printf("mtdparts_init: no valid partitions\n");
1661 /* ok it's good, save new parts */
1662 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1664 /* reset first partition from first dev from the list as current */
1665 current_dev = list_entry(devices.next, struct mtd_device, link);
1666 current_partnum = 0;
1669 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1670 MTD_DEV_TYPE(current_dev->id->type),
1671 current_dev->id->num, current_partnum);
1674 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1675 if (!parts && (last_parts[0] != '\0'))
1676 return devices_init();
1678 /* do not process current partition if mtdparts variable is null */
1682 /* is current partition set in environment? if so, use it */
1683 if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1684 struct part_info *p;
1685 struct mtd_device *cdev;
1688 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1690 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1692 current_partnum = pnum;
1695 } else if (getenv("partition") == NULL) {
1696 DEBUGF("no partition variable set, setting...\n");
1702 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1704 * 'Static' version of command line mtdparts_init() routine. Single partition on
1705 * a single device configuration.
1709 * Parse and initialize global mtdids mapping and create global
1710 * device/partition list.
1712 * @return 0 on success, 1 otherwise
1714 int mtdparts_init(void)
1716 static int initialized = 0;
1720 DEBUGF("\n---mtdparts_init---\n");
1723 struct part_info *part;
1726 current_dev = (struct mtd_device *)
1727 malloc(sizeof(struct mtd_device) +
1728 sizeof(struct part_info) +
1729 sizeof(struct mtdids));
1731 printf("out of memory\n");
1734 memset(current_dev, 0, sizeof(struct mtd_device) +
1735 sizeof(struct part_info) + sizeof(struct mtdids));
1737 id = (struct mtdids *)(current_dev + 1);
1738 part = (struct part_info *)(id + 1);
1741 id->mtd_id = "single part";
1743 #if defined(CONFIG_JFFS2_DEV)
1744 dev_name = CONFIG_JFFS2_DEV;
1749 if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
1750 (device_validate(id->type, id->num, &size) != 0)) {
1751 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
1756 INIT_LIST_HEAD(&id->link);
1758 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1759 id->type, id->num, id->size, id->mtd_id);
1762 part->name = "static";
1763 part->auto_name = 0;
1765 #if defined(CONFIG_JFFS2_PART_SIZE)
1766 part->size = CONFIG_JFFS2_PART_SIZE;
1768 part->size = SIZE_REMAINING;
1771 #if defined(CONFIG_JFFS2_PART_OFFSET)
1772 part->offset = CONFIG_JFFS2_PART_OFFSET;
1774 part->offset = 0x00000000;
1777 part->dev = current_dev;
1778 INIT_LIST_HEAD(&part->link);
1780 /* recalculate size if needed */
1781 if (part->size == SIZE_REMAINING)
1782 part->size = id->size - part->offset;
1784 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1785 part->name, part->size, part->offset);
1788 current_dev->id = id;
1789 INIT_LIST_HEAD(¤t_dev->link);
1790 current_dev->num_parts = 1;
1791 INIT_LIST_HEAD(¤t_dev->parts);
1792 list_add(&part->link, ¤t_dev->parts);
1797 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1800 * Return pointer to the partition of a requested number from a requested
1803 * @param dev device that is to be searched for a partition
1804 * @param part_num requested partition number
1805 * @return pointer to the part_info, NULL otherwise
1807 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1809 struct list_head *entry;
1810 struct part_info *part;
1816 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1817 part_num, MTD_DEV_TYPE(dev->id->type),
1818 dev->id->num, dev->id->mtd_id);
1820 if (part_num >= dev->num_parts) {
1821 printf("invalid partition number %d for device %s%d (%s)\n",
1822 part_num, MTD_DEV_TYPE(dev->id->type),
1823 dev->id->num, dev->id->mtd_id);
1827 /* locate partition number, return it */
1829 list_for_each(entry, &dev->parts) {
1830 part = list_entry(entry, struct part_info, link);
1832 if (part_num == num++) {
1840 /***************************************************/
1841 /* U-boot commands */
1842 /***************************************************/
1845 * Routine implementing fsload u-boot command. This routine tries to load
1846 * a requested file from jffs2/cramfs filesystem on a current partition.
1848 * @param cmdtp command internal data
1849 * @param flag command flag
1850 * @param argc number of arguments supplied to the command
1851 * @param argv arguments list
1852 * @return 0 on success, 1 otherwise
1854 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1859 struct part_info *part;
1860 ulong offset = load_addr;
1862 /* pre-set Boot file name */
1863 if ((filename = getenv("bootfile")) == NULL) {
1864 filename = "uImage";
1871 offset = simple_strtoul(argv[1], NULL, 16);
1876 /* make sure we are in sync with env variables */
1877 if (mtdparts_init() !=0)
1880 if ((part = jffs2_part_info(current_dev, current_partnum))){
1882 /* check partition type for JFFS2, cramfs, romfs */
1883 if (cramfs_check(part)) {
1885 } else if (romfs_check(part)) {
1890 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1892 if (cramfs_check(part)) {
1893 size = cramfs_load ((char *) offset, part, filename);
1894 } else if (romfs_check(part)){
1895 size = romfs_load ((char *) offset, part, filename);
1897 /* if this is not cramfs or romfs assume jffs2 */
1898 size = jffs2_1pass_load((char *)offset, part, filename);
1903 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1904 fsname, size, offset);
1905 sprintf(buf, "%x", size);
1906 setenv("filesize", buf);
1908 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
1917 * Routine implementing u-boot ls command which lists content of a given
1918 * directory on a current partition.
1920 * @param cmdtp command internal data
1921 * @param flag command flag
1922 * @param argc number of arguments supplied to the command
1923 * @param argv arguments list
1924 * @return 0 on success, 1 otherwise
1926 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1928 char *filename = "/";
1930 struct part_info *part;
1935 /* make sure we are in sync with env variables */
1936 if (mtdparts_init() !=0)
1939 if ((part = jffs2_part_info(current_dev, current_partnum))){
1941 /* check partition type for cramfs */
1942 if (cramfs_check(part)) {
1943 ret = cramfs_ls (part, filename);
1944 } else if (romfs_check(part)) {
1945 ret = romfs_ls (part, filename);
1947 /* if this is not cramfs or romfs assume jffs2 */
1948 ret = jffs2_1pass_ls(part, filename);
1957 * Routine implementing u-boot fsinfo command. This routine prints out
1958 * miscellaneous filesystem informations/statistics.
1960 * @param cmdtp command internal data
1961 * @param flag command flag
1962 * @param argc number of arguments supplied to the command
1963 * @param argv arguments list
1964 * @return 0 on success, 1 otherwise
1966 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1968 struct part_info *part;
1971 /* make sure we are in sync with env variables */
1972 if (mtdparts_init() !=0)
1975 if ((part = jffs2_part_info(current_dev, current_partnum))){
1977 /* check partition type for cramfs */
1978 puts("### filesystem type is ");
1980 if (cramfs_check(part)) {
1982 ret = cramfs_info (part);
1983 } else if (romfs_check(part)) {
1985 ret = romfs_info (part);
1987 /* if this is not cramfs or romfs assume jffs2 */
1989 ret = jffs2_1pass_info(part);
1997 /* command line only */
1998 #ifdef CONFIG_JFFS2_CMDLINE
2000 * Routine implementing u-boot chpart command. Sets new current partition based
2001 * on the user supplied partition id. For partition id format see find_dev_and_part().
2003 * @param cmdtp command internal data
2004 * @param flag command flag
2005 * @param argc number of arguments supplied to the command
2006 * @param argv arguments list
2007 * @return 0 on success, 1 otherwise
2009 int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2011 /* command line only */
2012 struct mtd_device *dev;
2013 struct part_info *part;
2016 if (mtdparts_init() !=0)
2020 printf("no partition id specified\n");
2024 if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2028 current_partnum = pnum;
2031 printf("partition changed to %s%d,%d\n",
2032 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2038 * Routine implementing u-boot mtdparts command. Initialize/update default global
2039 * partition list and process user partition request (list, add, del).
2041 * @param cmdtp command internal data
2042 * @param flag command flag
2043 * @param argc number of arguments supplied to the command
2044 * @param argv arguments list
2045 * @return 0 on success, 1 otherwise
2047 int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2050 if (strcmp(argv[1], "default") == 0) {
2051 setenv("mtdids", (char *)mtdids_default);
2052 setenv("mtdparts", (char *)mtdparts_default);
2053 setenv("partition", NULL);
2057 } else if (strcmp(argv[1], "delall") == 0) {
2058 /* this may be the first run, initialize lists if needed */
2061 setenv("mtdparts", NULL);
2063 /* devices_init() calls current_save() */
2064 return devices_init();
2068 /* make sure we are in sync with env variables */
2069 if (mtdparts_init() != 0)
2077 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2078 if (((argc == 5) || (argc == 6)) && (strcmp(argv[1], "add") == 0)) {
2079 #define PART_ADD_DESC_MAXLEN 64
2080 char tmpbuf[PART_ADD_DESC_MAXLEN];
2082 struct mtd_device *dev;
2083 struct mtd_device *dev_tmp;
2085 struct part_info *p;
2087 if (id_parse(argv[2], NULL, &type, &num) != 0)
2090 if ((id = id_find(type, num)) == NULL) {
2091 printf("no such device %s defined in mtdids variable\n", argv[2]);
2095 len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
2096 len += strlen(argv[3]); /* size@offset */
2097 len += strlen(argv[4]) + 2; /* '(' name ')' */
2098 if (argv[5] && (strlen(argv[5]) == 2))
2099 len += 2; /* 'ro' */
2101 if (len >= PART_ADD_DESC_MAXLEN) {
2102 printf("too long partition description\n");
2105 sprintf(tmpbuf, "%s:%s(%s)%s",
2106 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
2107 DEBUGF("add tmpbuf: %s\n", tmpbuf);
2109 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2112 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2113 dev->id->num, dev->id->mtd_id);
2115 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2118 /* merge new partition with existing ones*/
2119 p = list_entry(dev->parts.next, struct part_info, link);
2120 if (part_add(dev_tmp, p) != 0) {
2126 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2127 printf("generated mtdparts too long, reseting to null\n");
2134 /* mtdparts del part-id */
2135 if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2136 DEBUGF("del: part-id = %s\n", argv[2]);
2138 return delete_partition(argv[2]);
2141 printf ("Usage:\n%s\n", cmdtp->usage);
2144 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2146 /***************************************************/
2148 fsload, 3, 0, do_jffs2_fsload,
2149 "fsload\t- load binary file from a filesystem image\n",
2150 "[ off ] [ filename ]\n"
2151 " - load binary file from flash bank\n"
2152 " with offset 'off'\n"
2155 ls, 2, 1, do_jffs2_ls,
2156 "ls\t- list files in a directory (default /)\n",
2158 " - list files in a directory.\n"
2162 fsinfo, 1, 1, do_jffs2_fsinfo,
2163 "fsinfo\t- print information about filesystems\n",
2164 " - print information about filesystems\n"
2167 #ifdef CONFIG_JFFS2_CMDLINE
2169 chpart, 2, 0, do_jffs2_chpart,
2170 "chpart\t- change active partition\n",
2172 " - change active partition (e.g. part-id = nand0,1)\n"
2176 mtdparts, 6, 0, do_jffs2_mtdparts,
2177 "mtdparts- define flash/nand partitions\n",
2179 " - list partition table\n"
2181 " - delete all partitions\n"
2182 "mtdparts del part-id\n"
2183 " - delete partition (e.g. part-id = nand0,1)\n"
2184 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2185 " - add partition\n"
2186 "mtdparts default\n"
2187 " - reset partition table to defaults\n\n"
2189 "this command uses three environment variables:\n\n"
2190 "'partition' - keeps current partition identifier\n\n"
2191 "partition := <part-id>\n"
2192 "<part-id> := <dev-id>,part_num\n\n"
2193 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2194 "mtdids=<idmap>[,<idmap>,...]\n\n"
2195 "<idmap> := <dev-id>=<mtd-id>\n"
2196 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2197 "<dev-num> := mtd device number, 0...\n"
2198 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2199 "'mtdparts' - partition list\n\n"
2200 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2201 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2202 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2203 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2204 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2205 "<offset> := partition start offset within the device\n"
2206 "<name> := '(' NAME ')'\n"
2207 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2209 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2211 /***************************************************/