]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_jffs2.c
Merge git://www.denx.de/git/u-boot
[karo-tx-uboot.git] / common / cmd_jffs2.c
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2002
6  * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
7  *
8  * (C) Copyright 2003
9  * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
10  *
11  * (C) Copyright 2005
12  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13  *
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
16  *   kernel tree.
17  *
18  *   $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
19  *   Copyright 2002 SYSGO Real-Time Solutions GmbH
20  *
21  * See file CREDITS for list of people who contributed to this
22  * project.
23  *
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.
28  *
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.
33  *
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,
37  * MA 02111-1307 USA
38  */
39
40 /*
41  * Three environment variables are used by the parsing routines:
42  *
43  * 'partition' - keeps current partition identifier
44  *
45  * partition  := <part-id>
46  * <part-id>  := <dev-id>,part_num
47  *
48  *
49  * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
50  *
51  * mtdids=<idmap>[,<idmap>,...]
52  *
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)
57  *
58  *
59  * 'mtdparts' - partition list
60  *
61  * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
62  *
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)
70  *
71  * Notes:
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
74  *
75  * Examples:
76  *
77  * 1 NOR Flash, with 1 single writable partition:
78  * mtdids=nor0=edb7312-nor
79  * mtdparts=mtdparts=edb7312-nor:-
80  *
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)
84  *
85  */
86
87 /*
88  * JFFS2/CRAMFS/ROMFS support
89  */
90 #include <common.h>
91 #include <command.h>
92 #include <malloc.h>
93 #include <jffs2/jffs2.h>
94 #include <linux/list.h>
95 #include <linux/ctype.h>
96
97 #if defined(CONFIG_CMD_JFFS2)
98
99 #include <cramfs/cramfs_fs.h>
100
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>
106 #include <nand.h>
107 #endif /* !CFG_NAND_LEGACY */
108 #endif
109 /* enable/disable debugging messages */
110 #define DEBUG_JFFS
111 #undef  DEBUG_JFFS
112
113 #ifdef  DEBUG_JFFS
114 # define DEBUGF(fmt, args...)   printf(fmt ,##args)
115 #else
116 # define DEBUGF(fmt, args...)
117 #endif
118
119 /* special size referring to all the remaining space in a partition */
120 #define SIZE_REMAINING          0xFFFFFFFF
121
122 /* special offset value, it is used when not provided by user
123  *
124  * this value is used temporarily during parsing, later such offests
125  * are recalculated */
126 #define OFFSET_NOT_SPECIFIED    0xFFFFFFFF
127
128 /* minimum partition size */
129 #define MIN_PART_SIZE           4096
130
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
134
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;
139 #else
140 #warning "MTDIDS_DEFAULT not defined!"
141 static const char *const mtdids_default = NULL;
142 #endif
143
144 #if defined(MTDPARTS_DEFAULT)
145 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
146 #else
147 #warning "MTDPARTS_DEFAULT not defined!"
148 static const char *const mtdparts_default = NULL;
149 #endif
150
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];
158
159 /* low level jffs2 cache cleaning routine */
160 extern void jffs2_free_cache(struct part_info *part);
161
162 /* mtdids mapping list, filled by parse_ids() */
163 struct list_head mtdids;
164
165 /* device/partition list, parse_cmdline() parses into here */
166 struct list_head devices;
167 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
168
169 /* current active device and partition number */
170 static struct mtd_device *current_dev = NULL;
171 static u8 current_partnum = 0;
172
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);
177
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);
182
183 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num);
184
185 /* command line only routines */
186 #ifdef CONFIG_JFFS2_CMDLINE
187
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);
190
191 /**
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.
198  *
199  * @param ptr where parse begins
200  * @param retptr output pointer to next char after parse completes (output)
201  * @return resulting unsigned int
202  */
203 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
204 {
205         unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
206
207         switch (**retptr) {
208                 case 'G':
209                 case 'g':
210                         ret <<= 10;
211                 case 'M':
212                 case 'm':
213                         ret <<= 10;
214                 case 'K':
215                 case 'k':
216                         ret <<= 10;
217                         (*retptr)++;
218                 default:
219                         break;
220         }
221
222         return ret;
223 }
224
225 /**
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.
229  *
230  * Note, that this routine does not check for buffer overflow, it's the caller
231  * who must assure enough space.
232  *
233  * @param buf output buffer
234  * @param size size to be converted to string
235  */
236 static void memsize_format(char *buf, u32 size)
237 {
238 #define SIZE_GB ((u32)1024*1024*1024)
239 #define SIZE_MB ((u32)1024*1024)
240 #define SIZE_KB ((u32)1024)
241
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);
248         else
249                 sprintf(buf, "%lu", size);
250 }
251
252 /**
253  * This routine does global indexing of all partitions. Resulting index for
254  * current partition is saved in 'mtddevnum'. Current partition name in
255  * 'mtddevname'.
256  */
257 static void index_partitions(void)
258 {
259         char buf[16];
260         u16 mtddevnum;
261         struct part_info *part;
262         struct list_head *dentry;
263         struct mtd_device *dev;
264
265         DEBUGF("--- index partitions ---\n");
266
267         if (current_dev) {
268                 mtddevnum = 0;
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);
275                                 break;
276                         }
277                         mtddevnum += dev->num_parts;
278                 }
279
280                 part = jffs2_part_info(current_dev, current_partnum);
281                 setenv("mtddevname", part->name);
282
283                 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
284         } else {
285                 setenv("mtddevnum", NULL);
286                 setenv("mtddevname", NULL);
287
288                 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
289         }
290 }
291
292 /**
293  * Save current device and partition in environment variable 'partition'.
294  */
295 static void current_save(void)
296 {
297         char buf[16];
298
299         DEBUGF("--- current_save ---\n");
300
301         if (current_dev) {
302                 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
303                                         current_dev->id->num, current_partnum);
304
305                 setenv("partition", buf);
306                 strncpy(last_partition, buf, 16);
307
308                 DEBUGF("=> partition %s\n", buf);
309         } else {
310                 setenv("partition", NULL);
311                 last_partition[0] = '\0';
312
313                 DEBUGF("=> partition NULL\n");
314         }
315         index_partitions();
316 }
317
318 /**
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.
322  *
323  * @param id of the parent device
324  * @param part partition to validate
325  * @return 0 if partition is valid, 1 otherwise
326  */
327 static int part_validate_nor(struct mtdids *id, struct part_info *part)
328 {
329 #if defined(CONFIG_CMD_FLASH)
330         /* info for FLASH chips */
331         extern flash_info_t flash_info[];
332         flash_info_t *flash;
333         int offset_aligned;
334         u32 end_offset;
335         int i;
336
337         flash = &flash_info[id->num];
338
339         offset_aligned = 0;
340         for (i = 0; i < flash->sector_count; i++) {
341                 if ((flash->start[i] - flash->start[0]) == part->offset) {
342                         offset_aligned = 1;
343                         break;
344                 }
345         }
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);
349                 return 1;
350         }
351
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)
355                         return 0;
356         }
357
358         if (flash->size == end_offset)
359                 return 0;
360
361         printf("%s%d: partition (%s) size alignment incorrect\n",
362                         MTD_DEV_TYPE(id->type), id->num, part->name);
363 #endif
364         return 1;
365 }
366
367 /**
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.
371  *
372  * @param id of the parent device
373  * @param part partition to validate
374  * @return 0 if partition is valid, 1 otherwise
375  */
376 static int part_validate_nand(struct mtdids *id, struct part_info *part)
377 {
378 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
379         /* info for NAND chips */
380         nand_info_t *nand;
381
382         nand = &nand_info[id->num];
383
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);
387                 return 1;
388         }
389
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);
393                 return 1;
394         }
395
396         return 0;
397 #else
398         return 1;
399 #endif
400 }
401
402 /**
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
406  * of part.
407  *
408  * @param id of the parent device
409  * @param part partition to validate
410  * @return 0 if partition is valid, 1 otherwise
411  */
412 static int part_validate(struct mtdids *id, struct part_info *part)
413 {
414         if (part->size == SIZE_REMAINING)
415                 part->size = id->size - part->offset;
416
417         if (part->offset > id->size) {
418                 printf("%s: offset %08lx beyond flash size %08lx\n",
419                                 id->mtd_id, part->offset, id->size);
420                 return 1;
421         }
422
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);
426                 return 1;
427         }
428
429         if (part->offset + part->size > id->size) {
430                 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
431                 return 1;
432         }
433
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);
438         else
439                 DEBUGF("part_validate: invalid dev type\n");
440
441         return 1;
442 }
443
444 /**
445  * Delete selected partition from the partion list of the specified device.
446  *
447  * @param dev device to delete partition from
448  * @param part partition to delete
449  * @return 0 on success, 1 otherwise
450  */
451 static int part_del(struct mtd_device *dev, struct part_info *part)
452 {
453         u8 current_save_needed = 0;
454
455         /* if there is only one partition, remove whole device */
456         if (dev->num_parts == 1)
457                 return device_del(dev);
458
459         /* otherwise just delete this partition */
460
461         if (dev == current_dev) {
462                 /* we are modyfing partitions for the current device,
463                  * update current */
464                 struct part_info *curr_pi;
465                 curr_pi = jffs2_part_info(current_dev, current_partnum);
466
467                 if (curr_pi) {
468                         if (curr_pi == part) {
469                                 printf("current partition deleted, resetting current to 0\n");
470                                 current_partnum = 0;
471                         } else if (part->offset <= curr_pi->offset) {
472                                 current_partnum--;
473                         }
474                         current_save_needed = 1;
475                 }
476         }
477
478 #ifdef CFG_NAND_LEGACY
479         jffs2_free_cache(part);
480 #endif
481         list_del(&part->link);
482         free(part);
483         dev->num_parts--;
484
485         if (current_save_needed > 0)
486                 current_save();
487         else
488                 index_partitions();
489
490         return 0;
491 }
492
493 /**
494  * Delete all partitions from parts head list, free memory.
495  *
496  * @param head list of partitions to delete
497  */
498 static void part_delall(struct list_head *head)
499 {
500         struct list_head *entry, *n;
501         struct part_info *part_tmp;
502
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);
506
507 #ifdef CFG_NAND_LEGACY
508                 jffs2_free_cache(part_tmp);
509 #endif
510                 list_del(entry);
511                 free(part_tmp);
512         }
513 }
514
515 /**
516  * Add new partition to the supplied partition list. Make sure partitions are
517  * sorted by offset in ascending order.
518  *
519  * @param head list this partition is to be added to
520  * @param new partition to be added
521  */
522 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
523 {
524         struct list_head *entry;
525         struct part_info *new_pi, *curr_pi;
526
527         /* link partition to parrent dev */
528         part->dev = dev;
529
530         if (list_empty(&dev->parts)) {
531                 DEBUGF("part_sort_add: list empty\n");
532                 list_add(&part->link, &dev->parts);
533                 dev->num_parts++;
534                 index_partitions();
535                 return 0;
536         }
537
538         new_pi = list_entry(&part->link, struct part_info, link);
539
540         /* get current partition info if we are updating current device */
541         curr_pi = NULL;
542         if (dev == current_dev)
543                 curr_pi = jffs2_part_info(current_dev, current_partnum);
544
545         list_for_each(entry, &dev->parts) {
546                 struct part_info *pi;
547
548                 pi = list_entry(entry, struct part_info, link);
549
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");
553                         return 1;
554                 }
555
556                 if (new_pi->offset <= pi->offset) {
557                         list_add_tail(&part->link, entry);
558                         dev->num_parts++;
559
560                         if (curr_pi && (pi->offset <= curr_pi->offset)) {
561                                 /* we are modyfing partitions for the current
562                                  * device, update current */
563                                 current_partnum++;
564                                 current_save();
565                         } else {
566                                 index_partitions();
567                         }
568                         return 0;
569                 }
570         }
571
572         list_add_tail(&part->link, &dev->parts);
573         dev->num_parts++;
574         index_partitions();
575         return 0;
576 }
577
578 /**
579  * Add provided partition to the partition list of a given device.
580  *
581  * @param dev device to which partition is added
582  * @param part partition to be added
583  * @return 0 on success, 1 otherwise
584  */
585 static int part_add(struct mtd_device *dev, struct part_info *part)
586 {
587         /* verify alignment and size */
588         if (part_validate(dev->id, part) != 0)
589                 return 1;
590
591         /* partition is ok, add it to the list */
592         if (part_sort_add(dev, part) != 0)
593                 return 1;
594
595         return 0;
596 }
597
598 /**
599  * Parse one partition definition, allocate memory and return pointer to this
600  * location in retpart.
601  *
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
606  */
607 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
608 {
609         struct part_info *part;
610         unsigned long size;
611         unsigned long offset;
612         const char *name;
613         int name_len;
614         unsigned int mask_flags;
615         const char *p;
616
617         p = partdef;
618         *retpart = NULL;
619         *ret = NULL;
620
621         /* fetch the partition size */
622         if (*p == '-') {
623                 /* assign all remaining space to this partition */
624                 DEBUGF("'-': remaining size assigned\n");
625                 size = SIZE_REMAINING;
626                 p++;
627         } else {
628                 size = memsize_parse(p, &p);
629                 if (size < MIN_PART_SIZE) {
630                         printf("partition size too small (%lx)\n", size);
631                         return 1;
632                 }
633         }
634
635         /* check for offset */
636         offset = OFFSET_NOT_SPECIFIED;
637         if (*p == '@') {
638                 p++;
639                 offset = memsize_parse(p, &p);
640         }
641
642         /* now look for the name */
643         if (*p == '(') {
644                 name = ++p;
645                 if ((p = strchr(name, ')')) == NULL) {
646                         printf("no closing ) found in partition name\n");
647                         return 1;
648                 }
649                 name_len = p - name + 1;
650                 if ((name_len - 1) == 0) {
651                         printf("empty partition name\n");
652                         return 1;
653                 }
654                 p++;
655         } else {
656                 /* 0x00000000@0x00000000 */
657                 name_len = 22;
658                 name = NULL;
659         }
660
661         /* test for options */
662         mask_flags = 0;
663         if (strncmp(p, "ro", 2) == 0) {
664                 mask_flags |= MTD_WRITEABLE_CMD;
665                 p += 2;
666         }
667
668         /* check for next partition definition */
669         if (*p == ',') {
670                 if (size == SIZE_REMAINING) {
671                         *ret = NULL;
672                         printf("no partitions allowed after a fill-up partition\n");
673                         return 1;
674                 }
675                 *ret = ++p;
676         } else if ((*p == ';') || (*p == '\0')) {
677                 *ret = p;
678         } else {
679                 printf("unexpected character '%c' at the end of partition\n", *p);
680                 *ret = NULL;
681                 return 1;
682         }
683
684         /*  allocate memory */
685         part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
686         if (!part) {
687                 printf("out of memory\n");
688                 return 1;
689         }
690         memset(part, 0, sizeof(struct part_info) + name_len);
691         part->size = size;
692         part->offset = offset;
693         part->mask_flags = mask_flags;
694         part->name = (char *)(part + 1);
695
696         if (name) {
697                 /* copy user provided name */
698                 strncpy(part->name, name, name_len - 1);
699                 part->auto_name = 0;
700         } else {
701                 /* auto generated name in form of size@offset */
702                 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
703                 part->auto_name = 1;
704         }
705
706         part->name[name_len - 1] = '\0';
707         INIT_LIST_HEAD(&part->link);
708
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);
712
713         *retpart = part;
714         return 0;
715 }
716 #endif/* #ifdef CONFIG_JFFS2_CMDLINE */
717
718 /**
719  * Check device number to be within valid range for given device type.
720  *
721  * @param dev device to validate
722  * @return 0 if device is valid, 1 otherwise
723  */
724 static int device_validate(u8 type, u8 num, u32 *size)
725 {
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;
731
732                         return 0;
733                 }
734
735                 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
736                                 MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
737 #else
738                 printf("support for FLASH devices not present\n");
739 #endif
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;
745 #else
746                         extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
747                         *size = nand_dev_desc[num].totlen;
748 #endif
749                         return 0;
750                 }
751
752                 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
753                                 MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
754 #else
755                 printf("support for NAND devices not present\n");
756 #endif
757         }
758
759         return 1;
760 }
761
762 #ifdef CONFIG_JFFS2_CMDLINE
763 /**
764  * Delete all mtd devices from a supplied devices list, free memory allocated for
765  * each device and delete all device partitions.
766  *
767  * @return 0 on success, 1 otherwise
768  */
769 static int device_delall(struct list_head *head)
770 {
771         struct list_head *entry, *n;
772         struct mtd_device *dev_tmp;
773
774         /* clean devices list */
775         list_for_each_safe(entry, n, head) {
776                 dev_tmp = list_entry(entry, struct mtd_device, link);
777                 list_del(entry);
778                 part_delall(&dev_tmp->parts);
779                 free(dev_tmp);
780         }
781         INIT_LIST_HEAD(&devices);
782
783         return 0;
784 }
785
786 /**
787  * If provided device exists it's partitions are deleted, device is removed
788  * from device list and device memory is freed.
789  *
790  * @param dev device to be deleted
791  * @return 0 on success, 1 otherwise
792  */
793 static int device_del(struct mtd_device *dev)
794 {
795         part_delall(&dev->parts);
796         list_del(&dev->link);
797         free(dev);
798
799         if (dev == current_dev) {
800                 /* we just deleted current device */
801                 if (list_empty(&devices)) {
802                         current_dev = NULL;
803                 } else {
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);
807                         current_partnum = 0;
808                 }
809                 current_save();
810                 return 0;
811         }
812
813         index_partitions();
814         return 0;
815 }
816
817 /**
818  * Search global device list and return pointer to the device of type and num
819  * specified.
820  *
821  * @param type device type
822  * @param num device number
823  * @return NULL if requested device does not exist
824  */
825 static struct mtd_device* device_find(u8 type, u8 num)
826 {
827         struct list_head *entry;
828         struct mtd_device *dev_tmp;
829
830         list_for_each(entry, &devices) {
831                 dev_tmp = list_entry(entry, struct mtd_device, link);
832
833                 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
834                         return dev_tmp;
835         }
836
837         return NULL;
838 }
839
840 /**
841  * Add specified device to the global device list.
842  *
843  * @param dev device to be added
844  */
845 static void device_add(struct mtd_device *dev)
846 {
847         u8 current_save_needed = 0;
848
849         if (list_empty(&devices)) {
850                 current_dev = dev;
851                 current_partnum = 0;
852                 current_save_needed = 1;
853         }
854
855         list_add_tail(&dev->link, &devices);
856
857         if (current_save_needed > 0)
858                 current_save();
859         else
860                 index_partitions();
861 }
862
863 /**
864  * Parse device type, name and mtd-id. If syntax is ok allocate memory and
865  * return pointer to the device structure.
866  *
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
871  */
872 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
873 {
874         struct mtd_device *dev;
875         struct part_info *part;
876         struct mtdids *id;
877         const char *mtd_id;
878         unsigned int mtd_id_len;
879         const char *p, *pend;
880         LIST_HEAD(tmp_list);
881         struct list_head *entry, *n;
882         u16 num_parts;
883         u32 offset;
884         int err = 1;
885
886         p = mtd_dev;
887         *retdev = NULL;
888         *ret = NULL;
889
890         DEBUGF("===device_parse===\n");
891
892         /* fetch <mtd-id> */
893         mtd_id = p;
894         if (!(p = strchr(mtd_id, ':'))) {
895                 printf("no <mtd-id> identifier\n");
896                 return 1;
897         }
898         mtd_id_len = p - mtd_id + 1;
899         p++;
900
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);
904                 return 1;
905         }
906
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);
912
913
914         /* parse partitions */
915         num_parts = 0;
916
917         offset = 0;
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;
922         }
923
924         while (p && (*p != '\0') && (*p != ';')) {
925                 err = 1;
926                 if ((part_parse(p, &p, &part) != 0) || (!part))
927                         break;
928
929                 /* calculate offset when not specified */
930                 if (part->offset == OFFSET_NOT_SPECIFIED)
931                         part->offset = offset;
932                 else
933                         offset = part->offset;
934
935                 /* verify alignment and size */
936                 if (part_validate(id, part) != 0)
937                         break;
938
939                 offset += part->size;
940
941                 /* partition is ok, add it to the list */
942                 list_add_tail(&part->link, &tmp_list);
943                 num_parts++;
944                 err = 0;
945         }
946         if (err == 1) {
947                 part_delall(&tmp_list);
948                 return 1;
949         }
950
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);
954                 return 1;
955         }
956
957         DEBUGF("\ntotal partitions: %d\n", num_parts);
958
959         /* check for next device presence */
960         if (p) {
961                 if (*p == ';') {
962                         *ret = ++p;
963                 } else if (*p == '\0') {
964                         *ret = p;
965                 } else {
966                         printf("unexpected character '%c' at the end of device\n", *p);
967                         *ret = NULL;
968                         return 1;
969                 }
970         }
971
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");
975                 return 1;
976         }
977         memset(dev, 0, sizeof(struct mtd_device));
978         dev->id = id;
979         dev->num_parts = 0; /* part_sort_add increments num_parts */
980         INIT_LIST_HEAD(&dev->parts);
981         INIT_LIST_HEAD(&dev->link);
982
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);
986                 list_del(entry);
987                 if (part_sort_add(dev, part) != 0) {
988                         device_del(dev);
989                         return 1;
990                 }
991         }
992
993         *retdev = dev;
994
995         DEBUGF("===\n\n");
996         return 0;
997 }
998
999 /**
1000  * Initialize global device list.
1001  *
1002  * @return 0 on success, 1 otherwise
1003  */
1004 static int devices_init(void)
1005 {
1006         last_parts[0] = '\0';
1007         current_dev = NULL;
1008         current_save();
1009
1010         return device_delall(&devices);
1011 }
1012
1013 /*
1014  * Search global mtdids list and find id of requested type and number.
1015  *
1016  * @return pointer to the id if it exists, NULL otherwise
1017  */
1018 static struct mtdids* id_find(u8 type, u8 num)
1019 {
1020         struct list_head *entry;
1021         struct mtdids *id;
1022
1023         list_for_each(entry, &mtdids) {
1024                 id = list_entry(entry, struct mtdids, link);
1025
1026                 if ((id->type == type) && (id->num == num))
1027                         return id;
1028         }
1029
1030         return NULL;
1031 }
1032
1033 /**
1034  * Search global mtdids list and find id of a requested mtd_id.
1035  *
1036  * Note: first argument is not null terminated.
1037  *
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
1041  */
1042 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1043 {
1044         struct list_head *entry;
1045         struct mtdids *id;
1046
1047         DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1048                         mtd_id_len, mtd_id, mtd_id_len);
1049
1050         list_for_each(entry, &mtdids) {
1051                 id = list_entry(entry, struct mtdids, link);
1052
1053                 DEBUGF("entry: '%s' (len = %d)\n",
1054                                 id->mtd_id, strlen(id->mtd_id));
1055
1056                 if (mtd_id_len != strlen(id->mtd_id))
1057                         continue;
1058                 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1059                         return id;
1060         }
1061
1062         return NULL;
1063 }
1064 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1065
1066 /**
1067  * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1068  * type and number.
1069  *
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
1075  */
1076 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1077 {
1078         const char *p = id;
1079
1080         *dev_type = 0;
1081         if (strncmp(p, "nand", 4) == 0) {
1082                 *dev_type = MTD_DEV_TYPE_NAND;
1083                 p += 4;
1084         } else if (strncmp(p, "nor", 3) == 0) {
1085                 *dev_type = MTD_DEV_TYPE_NOR;
1086                 p += 3;
1087         } else {
1088                 printf("incorrect device type in %s\n", id);
1089                 return 1;
1090         }
1091
1092         if (!isdigit(*p)) {
1093                 printf("incorrect device number in %s\n", id);
1094                 return 1;
1095         }
1096
1097         *dev_num = simple_strtoul(p, (char **)&p, 0);
1098         if (ret_id)
1099                 *ret_id = p;
1100         return 0;
1101 }
1102
1103 #ifdef CONFIG_JFFS2_CMDLINE
1104 /**
1105  * Process all devices and generate corresponding mtdparts string describing
1106  * all partitions on all devices.
1107  *
1108  * @param buf output buffer holding generated mtdparts string (output)
1109  * @param buflen buffer size
1110  * @return 0 on success, 1 otherwise
1111  */
1112 static int generate_mtdparts(char *buf, u32 buflen)
1113 {
1114         struct list_head *pentry, *dentry;
1115         struct mtd_device *dev;
1116         struct part_info *part, *prev_part;
1117         char *p = buf;
1118         char tmpbuf[32];
1119         u32 size, offset, len, part_cnt;
1120         u32 maxlen = buflen - 1;
1121
1122         DEBUGF("--- generate_mtdparts ---\n");
1123
1124         if (list_empty(&devices)) {
1125                 buf[0] = '\0';
1126                 return 0;
1127         }
1128
1129         sprintf(p, "mtdparts=");
1130         p += 9;
1131
1132         list_for_each(dentry, &devices) {
1133                 dev = list_entry(dentry, struct mtd_device, link);
1134
1135                 /* copy mtd_id */
1136                 len = strlen(dev->id->mtd_id) + 1;
1137                 if (len > maxlen)
1138                         goto cleanup;
1139                 memcpy(p, dev->id->mtd_id, len - 1);
1140                 p += len - 1;
1141                 *(p++) = ':';
1142                 maxlen -= len;
1143
1144                 /* format partitions */
1145                 prev_part = NULL;
1146                 part_cnt = 0;
1147                 list_for_each(pentry, &dev->parts) {
1148                         part = list_entry(pentry, struct part_info, link);
1149                         size = part->size;
1150                         offset = part->offset;
1151                         part_cnt++;
1152
1153                         /* partition size */
1154                         memsize_format(tmpbuf, size);
1155                         len = strlen(tmpbuf);
1156                         if (len > maxlen)
1157                                 goto cleanup;
1158                         memcpy(p, tmpbuf, len);
1159                         p += len;
1160                         maxlen -= len;
1161
1162
1163                         /* add offset only when there is a gap between
1164                          * partitions */
1165                         if ((!prev_part && (offset != 0)) ||
1166                                         (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1167
1168                                 memsize_format(tmpbuf, offset);
1169                                 len = strlen(tmpbuf) + 1;
1170                                 if (len > maxlen)
1171                                         goto cleanup;
1172                                 *(p++) = '@';
1173                                 memcpy(p, tmpbuf, len - 1);
1174                                 p += len - 1;
1175                                 maxlen -= len;
1176                         }
1177
1178                         /* copy name only if user supplied */
1179                         if(!part->auto_name) {
1180                                 len = strlen(part->name) + 2;
1181                                 if (len > maxlen)
1182                                         goto cleanup;
1183
1184                                 *(p++) = '(';
1185                                 memcpy(p, part->name, len - 2);
1186                                 p += len - 2;
1187                                 *(p++) = ')';
1188                                 maxlen -= len;
1189                         }
1190
1191                         /* ro mask flag */
1192                         if (part->mask_flags && MTD_WRITEABLE_CMD) {
1193                                 len = 2;
1194                                 if (len > maxlen)
1195                                         goto cleanup;
1196                                 *(p++) = 'r';
1197                                 *(p++) = 'o';
1198                                 maxlen -= 2;
1199                         }
1200
1201                         /* print ',' separator if there are other partitions
1202                          * following */
1203                         if (dev->num_parts > part_cnt) {
1204                                 if (1 > maxlen)
1205                                         goto cleanup;
1206                                 *(p++) = ',';
1207                                 maxlen--;
1208                         }
1209                         prev_part = part;
1210                 }
1211                 /* print ';' separator if there are other devices following */
1212                 if (dentry->next != &devices) {
1213                         if (1 > maxlen)
1214                                 goto cleanup;
1215                         *(p++) = ';';
1216                         maxlen--;
1217                 }
1218         }
1219
1220         /* we still have at least one char left, as we decremented maxlen at
1221          * the begining */
1222         *p = '\0';
1223
1224         return 0;
1225
1226 cleanup:
1227         last_parts[0] = '\0';
1228         return 1;
1229 }
1230
1231 /**
1232  * Call generate_mtdparts to process all devices and generate corresponding
1233  * mtdparts string, save it in mtdparts environment variable.
1234  *
1235  * @param buf output buffer holding generated mtdparts string (output)
1236  * @param buflen buffer size
1237  * @return 0 on success, 1 otherwise
1238  */
1239 static int generate_mtdparts_save(char *buf, u32 buflen)
1240 {
1241         int ret;
1242
1243         ret = generate_mtdparts(buf, buflen);
1244
1245         if ((buf[0] != '\0') && (ret == 0))
1246                 setenv("mtdparts", buf);
1247         else
1248                 setenv("mtdparts", NULL);
1249
1250         return ret;
1251 }
1252
1253 /**
1254  * Format and print out a partition list for each device from global device
1255  * list.
1256  */
1257 static void list_partitions(void)
1258 {
1259         struct list_head *dentry, *pentry;
1260         struct part_info *part;
1261         struct mtd_device *dev;
1262         int part_num;
1263
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");
1271
1272                 /* list partitions for given device */
1273                 part_num = 0;
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);
1279
1280                         part_num++;
1281                 }
1282         }
1283         if (list_empty(&devices))
1284                 printf("no partitions defined\n");
1285
1286         /* current_dev is not NULL only when we have non empty device list */
1287         if (current_dev) {
1288                 part = jffs2_part_info(current_dev, current_partnum);
1289                 if (part) {
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);
1294                 } else {
1295                         printf("could not get current partition info\n\n");
1296                 }
1297         }
1298
1299         printf("\ndefaults:\n");
1300         printf("mtdids  : %s\n", mtdids_default);
1301         printf("mtdparts: %s\n", mtdparts_default);
1302 }
1303
1304 /**
1305  * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1306  * corresponding device and verify partition number.
1307  *
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
1313  */
1314 int find_dev_and_part(const char *id, struct mtd_device **dev,
1315                 u8 *part_num, struct part_info **part)
1316 {
1317         struct list_head *dentry, *pentry;
1318         u8 type, dnum, pnum;
1319         const char *p;
1320
1321         DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
1322
1323         list_for_each(dentry, &devices) {
1324                 *part_num = 0;
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)
1329                                 return 0;
1330                         (*part_num)++;
1331                 }
1332         }
1333
1334         p = id;
1335         *dev = NULL;
1336         *part = NULL;
1337         *part_num = 0;
1338
1339         if (id_parse(p, &p, &type, &dnum) != 0)
1340                 return 1;
1341
1342         if ((*p++ != ',') || (*p == '\0')) {
1343                 printf("no partition number specified\n");
1344                 return 1;
1345         }
1346         pnum = simple_strtoul(p, (char **)&p, 0);
1347         if (*p != '\0') {
1348                 printf("unexpected trailing character '%c'\n", *p);
1349                 return 1;
1350         }
1351
1352         if ((*dev = device_find(type, dnum)) == NULL) {
1353                 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1354                 return 1;
1355         }
1356
1357         if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
1358                 printf("no such partition\n");
1359                 *dev = NULL;
1360                 return 1;
1361         }
1362
1363         *part_num = pnum;
1364
1365         return 0;
1366 }
1367
1368 /**
1369  * Find and delete partition. For partition id format see find_dev_and_part().
1370  *
1371  * @param id string describing device and partition
1372  * @return 0 on success, 1 otherwise
1373  */
1374 static int delete_partition(const char *id)
1375 {
1376         u8 pnum;
1377         struct mtd_device *dev;
1378         struct part_info *part;
1379
1380         if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1381
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);
1385
1386                 if (part_del(dev, part) != 0)
1387                         return 1;
1388
1389                 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1390                         printf("generated mtdparts too long, reseting to null\n");
1391                         return 1;
1392                 }
1393                 return 0;
1394         }
1395
1396         printf("partition %s not found\n", id);
1397         return 1;
1398 }
1399
1400 /**
1401  * Accept character string describing mtd partitions and call device_parse()
1402  * for each entry. Add created devices to the global devices list.
1403  *
1404  * @param mtdparts string specifing mtd partitions
1405  * @return 0 on success, 1 otherwise
1406  */
1407 static int parse_mtdparts(const char *const mtdparts)
1408 {
1409         const char *p = mtdparts;
1410         struct mtd_device *dev;
1411         int err = 1;
1412
1413         DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1414
1415         /* delete all devices and partitions */
1416         if (devices_init() != 0) {
1417                 printf("could not initialise device list\n");
1418                 return err;
1419         }
1420
1421         /* re-read 'mtdparts' variable, devices_init may be updating env */
1422         p = getenv("mtdparts");
1423
1424         if (strncmp(p, "mtdparts=", 9) != 0) {
1425                 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1426                 return err;
1427         }
1428         p += 9;
1429
1430         while (p && (*p != '\0')) {
1431                 err = 1;
1432                 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1433                         break;
1434
1435                 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1436                                 dev->id->num, dev->id->mtd_id);
1437
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);
1442                         break;
1443                 }
1444
1445                 list_add_tail(&dev->link, &devices);
1446                 err = 0;
1447         }
1448         if (err == 1) {
1449                 device_delall(&devices);
1450                 return 1;
1451         }
1452
1453         return 0;
1454 }
1455
1456 /**
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.
1460  *
1461  * @param ids mapping string
1462  * @return 0 on success, 1 otherwise
1463  */
1464 static int parse_mtdids(const char *const ids)
1465 {
1466         const char *p = ids;
1467         const char *mtd_id;
1468         int mtd_id_len;
1469         struct mtdids *id;
1470         struct list_head *entry, *n;
1471         struct mtdids *id_tmp;
1472         u8 type, num;
1473         u32 size;
1474         int ret = 1;
1475
1476         DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1477
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);
1482                 list_del(entry);
1483                 free(id_tmp);
1484         }
1485         last_ids[0] = '\0';
1486         INIT_LIST_HEAD(&mtdids);
1487
1488         while(p && (*p != '\0')) {
1489
1490                 ret = 1;
1491                 /* parse 'nor'|'nand'<dev-num> */
1492                 if (id_parse(p, &p, &type, &num) != 0)
1493                         break;
1494
1495                 if (*p != '=') {
1496                         printf("mtdids: incorrect <dev-num>\n");
1497                         break;
1498                 }
1499                 p++;
1500
1501                 /* check if requested device exists */
1502                 if (device_validate(type, num, &size) != 0)
1503                         return 1;
1504
1505                 /* locate <mtd-id> */
1506                 mtd_id = p;
1507                 if ((p = strchr(mtd_id, ',')) != NULL) {
1508                         mtd_id_len = p - mtd_id + 1;
1509                         p++;
1510                 } else {
1511                         mtd_id_len = strlen(mtd_id) + 1;
1512                 }
1513                 if (mtd_id_len == 0) {
1514                         printf("mtdids: no <mtd-id> identifier\n");
1515                         break;
1516                 }
1517
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)) {
1523                                 double_entry = 1;
1524                                 break;
1525                         }
1526                 }
1527                 if (double_entry) {
1528                         printf("device id %s%d redefined, please correct mtdids variable\n",
1529                                         MTD_DEV_TYPE(type), num);
1530                         break;
1531                 }
1532
1533                 /* allocate mtdids structure */
1534                 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1535                         printf("out of memory\n");
1536                         break;
1537                 }
1538                 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1539                 id->num = num;
1540                 id->type = type;
1541                 id->size = size;
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);
1546
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);
1550
1551                 list_add_tail(&id->link, &mtdids);
1552                 ret = 0;
1553         }
1554         if (ret == 1) {
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);
1558                         list_del(entry);
1559                         free(id_tmp);
1560                 }
1561                 return 1;
1562         }
1563
1564         return 0;
1565 }
1566
1567 /**
1568  * Parse and initialize global mtdids mapping and create global
1569  * device/partition list.
1570  *
1571  * @return 0 on success, 1 otherwise
1572  */
1573 int mtdparts_init(void)
1574 {
1575         static int initialized = 0;
1576         const char *ids, *parts;
1577         const char *current_partition;
1578         int ids_changed;
1579         char tmp_ep[PARTITION_MAXLEN];
1580
1581         DEBUGF("\n---mtdparts_init---\n");
1582         if (!initialized) {
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);
1588                 initialized = 1;
1589         }
1590
1591         /* get variables */
1592         ids = getenv("mtdids");
1593         parts = getenv("mtdparts");
1594         current_partition = getenv("partition");
1595
1596         /* save it for later parsing, cannot rely on current partition pointer
1597          * as 'partition' variable may be updated during init */
1598         tmp_ep[0] = '\0';
1599         if (current_partition)
1600                 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1601
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);
1606
1607         DEBUGF("last_partition : %s\n", last_partition);
1608         DEBUGF("env_partition  : %s\n", current_partition);
1609
1610         /* if mtdids varible is empty try to use defaults */
1611         if (!ids) {
1612                 if (mtdids_default) {
1613                         DEBUGF("mtdids variable not defined, using default\n");
1614                         ids = mtdids_default;
1615                         setenv("mtdids", (char *)ids);
1616                 } else {
1617                         printf("mtdids not defined, no default present\n");
1618                         return 1;
1619                 }
1620         }
1621         if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1622                 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1623                 return 1;
1624         }
1625
1626         /* do no try to use defaults when mtdparts variable is not defined,
1627          * just check the length */
1628         if (!parts)
1629                 printf("mtdparts variable not set, see 'help mtdparts'\n");
1630
1631         if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1632                 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1633                 return 1;
1634         }
1635
1636         /* check if we have already parsed those mtdids */
1637         if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1638                 ids_changed = 0;
1639         } else {
1640                 ids_changed = 1;
1641
1642                 if (parse_mtdids(ids) != 0) {
1643                         devices_init();
1644                         return 1;
1645                 }
1646
1647                 /* ok it's good, save new ids */
1648                 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1649         }
1650
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)
1654                         return 1;
1655
1656                 if (list_empty(&devices)) {
1657                         printf("mtdparts_init: no valid partitions\n");
1658                         return 1;
1659                 }
1660
1661                 /* ok it's good, save new parts */
1662                 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1663
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;
1667                 current_save();
1668
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);
1672         }
1673
1674         /* mtdparts variable was reset to NULL, delete all devices/partitions */
1675         if (!parts && (last_parts[0] != '\0'))
1676                 return devices_init();
1677
1678         /* do not process current partition if mtdparts variable is null */
1679         if (!parts)
1680                 return 0;
1681
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;
1686                 u8 pnum;
1687
1688                 DEBUGF("--- getting current partition: %s\n", tmp_ep);
1689
1690                 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1691                         current_dev = cdev;
1692                         current_partnum = pnum;
1693                         current_save();
1694                 }
1695         } else if (getenv("partition") == NULL) {
1696                 DEBUGF("no partition variable set, setting...\n");
1697                 current_save();
1698         }
1699
1700         return 0;
1701 }
1702 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1703 /*
1704  * 'Static' version of command line mtdparts_init() routine. Single partition on
1705  * a single device configuration.
1706  */
1707
1708 /**
1709  * Parse and initialize global mtdids mapping and create global
1710  * device/partition list.
1711  *
1712  * @return 0 on success, 1 otherwise
1713  */
1714 int mtdparts_init(void)
1715 {
1716         static int initialized = 0;
1717         u32 size;
1718         char *dev_name;
1719
1720         DEBUGF("\n---mtdparts_init---\n");
1721         if (!initialized) {
1722                 struct mtdids *id;
1723                 struct part_info *part;
1724
1725                 initialized = 1;
1726                 current_dev = (struct mtd_device *)
1727                         malloc(sizeof(struct mtd_device) +
1728                                         sizeof(struct part_info) +
1729                                         sizeof(struct mtdids));
1730                 if (!current_dev) {
1731                         printf("out of memory\n");
1732                         return 1;
1733                 }
1734                 memset(current_dev, 0, sizeof(struct mtd_device) +
1735                                         sizeof(struct part_info) + sizeof(struct mtdids));
1736
1737                 id = (struct mtdids *)(current_dev + 1);
1738                 part = (struct part_info *)(id + 1);
1739
1740                 /* id */
1741                 id->mtd_id = "single part";
1742
1743 #if defined(CONFIG_JFFS2_DEV)
1744                 dev_name = CONFIG_JFFS2_DEV;
1745 #else
1746                 dev_name = "nor0";
1747 #endif
1748
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);
1752                         free(current_dev);
1753                         return 1;
1754                 }
1755                 id->size = size;
1756                 INIT_LIST_HEAD(&id->link);
1757
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);
1760
1761                 /* partition */
1762                 part->name = "static";
1763                 part->auto_name = 0;
1764
1765 #if defined(CONFIG_JFFS2_PART_SIZE)
1766                 part->size = CONFIG_JFFS2_PART_SIZE;
1767 #else
1768                 part->size = SIZE_REMAINING;
1769 #endif
1770
1771 #if defined(CONFIG_JFFS2_PART_OFFSET)
1772                 part->offset = CONFIG_JFFS2_PART_OFFSET;
1773 #else
1774                 part->offset = 0x00000000;
1775 #endif
1776
1777                 part->dev = current_dev;
1778                 INIT_LIST_HEAD(&part->link);
1779
1780                 /* recalculate size if needed */
1781                 if (part->size == SIZE_REMAINING)
1782                         part->size = id->size - part->offset;
1783
1784                 DEBUGF("part  : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1785                                 part->name, part->size, part->offset);
1786
1787                 /* device */
1788                 current_dev->id = id;
1789                 INIT_LIST_HEAD(&current_dev->link);
1790                 current_dev->num_parts = 1;
1791                 INIT_LIST_HEAD(&current_dev->parts);
1792                 list_add(&part->link, &current_dev->parts);
1793         }
1794
1795         return 0;
1796 }
1797 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1798
1799 /**
1800  * Return pointer to the partition of a requested number from a requested
1801  * device.
1802  *
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
1806  */
1807 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
1808 {
1809         struct list_head *entry;
1810         struct part_info *part;
1811         int num;
1812
1813         if (!dev)
1814                 return NULL;
1815
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);
1819
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);
1824                 return NULL;
1825         }
1826
1827         /* locate partition number, return it */
1828         num = 0;
1829         list_for_each(entry, &dev->parts) {
1830                 part = list_entry(entry, struct part_info, link);
1831
1832                 if (part_num == num++) {
1833                         return part;
1834                 }
1835         }
1836
1837         return NULL;
1838 }
1839
1840 /***************************************************/
1841 /* U-boot commands                                 */
1842 /***************************************************/
1843
1844 /**
1845  * Routine implementing fsload u-boot command. This routine tries to load
1846  * a requested file from jffs2/cramfs filesystem on a current partition.
1847  *
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
1853  */
1854 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1855 {
1856         char *fsname;
1857         char *filename;
1858         int size;
1859         struct part_info *part;
1860         ulong offset = load_addr;
1861
1862         /* pre-set Boot file name */
1863         if ((filename = getenv("bootfile")) == NULL) {
1864                 filename = "uImage";
1865         }
1866
1867         if (argc == 2) {
1868                 filename = argv[1];
1869         }
1870         if (argc == 3) {
1871                 offset = simple_strtoul(argv[1], NULL, 16);
1872                 load_addr = offset;
1873                 filename = argv[2];
1874         }
1875
1876         /* make sure we are in sync with env variables */
1877         if (mtdparts_init() !=0)
1878                 return 1;
1879
1880         if ((part = jffs2_part_info(current_dev, current_partnum))){
1881
1882                 /* check partition type for JFFS2, cramfs, romfs */
1883                 if (cramfs_check(part)) {
1884                         fsname = "CRAMFS";
1885                 } else if (romfs_check(part)) {
1886                         fsname = "ROMFS";
1887                 } else {
1888                         fsname = "JFFS2";
1889                 }
1890                 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
1891
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);
1896                 } else {
1897                         /* if this is not cramfs or romfs assume jffs2 */
1898                         size = jffs2_1pass_load((char *)offset, part, filename);
1899                 }
1900
1901                 if (size > 0) {
1902                         char buf[10];
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);
1907                 } else {
1908                         printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
1909                 }
1910
1911                 return !(size > 0);
1912         }
1913         return 1;
1914 }
1915
1916 /**
1917  * Routine implementing u-boot ls command which lists content of a given
1918  * directory on a current partition.
1919  *
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
1925  */
1926 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1927 {
1928         char *filename = "/";
1929         int ret;
1930         struct part_info *part;
1931
1932         if (argc == 2)
1933                 filename = argv[1];
1934
1935         /* make sure we are in sync with env variables */
1936         if (mtdparts_init() !=0)
1937                 return 1;
1938
1939         if ((part = jffs2_part_info(current_dev, current_partnum))){
1940
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);
1946                 } else {
1947                         /* if this is not cramfs or romfs assume jffs2 */
1948                         ret = jffs2_1pass_ls(part, filename);
1949                 }
1950
1951                 return ret ? 0 : 1;
1952         }
1953         return 1;
1954 }
1955
1956 /**
1957  * Routine implementing u-boot fsinfo command. This routine prints out
1958  * miscellaneous filesystem informations/statistics.
1959  *
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
1965  */
1966 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1967 {
1968         struct part_info *part;
1969         int ret;
1970
1971         /* make sure we are in sync with env variables */
1972         if (mtdparts_init() !=0)
1973                 return 1;
1974
1975         if ((part = jffs2_part_info(current_dev, current_partnum))){
1976
1977                 /* check partition type for cramfs */
1978                 puts("### filesystem type is ");
1979
1980                 if (cramfs_check(part)) {
1981                         puts("CRAMFS\n");
1982                         ret = cramfs_info (part);
1983                 } else if (romfs_check(part)) {
1984                         puts("ROMFS\n");
1985                         ret = romfs_info (part);
1986                 } else {
1987                         /* if this is not cramfs or romfs assume jffs2 */
1988                         puts("JFFS2\n");
1989                         ret = jffs2_1pass_info(part);
1990                 }
1991
1992                 return ret ? 0 : 1;
1993         }
1994         return 1;
1995 }
1996
1997 /* command line only */
1998 #ifdef CONFIG_JFFS2_CMDLINE
1999 /**
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().
2002  *
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
2008  */
2009 int do_jffs2_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2010 {
2011 /* command line only */
2012         struct mtd_device *dev;
2013         struct part_info *part;
2014         u8 pnum;
2015
2016         if (mtdparts_init() !=0)
2017                 return 1;
2018
2019         if (argc < 2) {
2020                 printf("no partition id specified\n");
2021                 return 1;
2022         }
2023
2024         if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2025                 return 1;
2026
2027         current_dev = dev;
2028         current_partnum = pnum;
2029         current_save();
2030
2031         printf("partition changed to %s%d,%d\n",
2032                         MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2033
2034         return 0;
2035 }
2036
2037 /**
2038  * Routine implementing u-boot mtdparts command. Initialize/update default global
2039  * partition list and process user partition request (list, add, del).
2040  *
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
2046  */
2047 int do_jffs2_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
2048 {
2049         if (argc == 2) {
2050                 if (strcmp(argv[1], "default") == 0) {
2051                         setenv("mtdids", (char *)mtdids_default);
2052                         setenv("mtdparts", (char *)mtdparts_default);
2053                         setenv("partition", NULL);
2054
2055                         mtdparts_init();
2056                         return 0;
2057                 } else if (strcmp(argv[1], "delall") == 0) {
2058                         /* this may be the first run, initialize lists if needed */
2059                         mtdparts_init();
2060
2061                         setenv("mtdparts", NULL);
2062
2063                         /* devices_init() calls current_save() */
2064                         return devices_init();
2065                 }
2066         }
2067
2068         /* make sure we are in sync with env variables */
2069         if (mtdparts_init() != 0)
2070                 return 1;
2071
2072         if (argc == 1) {
2073                 list_partitions();
2074                 return 0;
2075         }
2076
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];
2081                 u8 type, num, len;
2082                 struct mtd_device *dev;
2083                 struct mtd_device *dev_tmp;
2084                 struct mtdids *id;
2085                 struct part_info *p;
2086
2087                 if (id_parse(argv[2], NULL, &type, &num) != 0)
2088                         return 1;
2089
2090                 if ((id = id_find(type, num)) == NULL) {
2091                         printf("no such device %s defined in mtdids variable\n", argv[2]);
2092                         return 1;
2093                 }
2094
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' */
2100
2101                 if (len >= PART_ADD_DESC_MAXLEN) {
2102                         printf("too long partition description\n");
2103                         return 1;
2104                 }
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);
2108
2109                 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2110                         return 1;
2111
2112                 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2113                                 dev->id->num, dev->id->mtd_id);
2114
2115                 if ((dev_tmp = device_find(dev->id->type, dev->id->num)) == NULL) {
2116                         device_add(dev);
2117                 } else {
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) {
2121                                 device_del(dev);
2122                                 return 1;
2123                         }
2124                 }
2125
2126                 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2127                         printf("generated mtdparts too long, reseting to null\n");
2128                         return 1;
2129                 }
2130
2131                 return 0;
2132         }
2133
2134         /* mtdparts del part-id */
2135         if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2136                 DEBUGF("del: part-id = %s\n", argv[2]);
2137
2138                 return delete_partition(argv[2]);
2139         }
2140
2141         printf ("Usage:\n%s\n", cmdtp->usage);
2142         return 1;
2143 }
2144 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2145
2146 /***************************************************/
2147 U_BOOT_CMD(
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"
2153 );
2154 U_BOOT_CMD(
2155         ls,     2,      1,      do_jffs2_ls,
2156         "ls\t- list files in a directory (default /)\n",
2157         "[ directory ]\n"
2158         "    - list files in a directory.\n"
2159 );
2160
2161 U_BOOT_CMD(
2162         fsinfo, 1,      1,      do_jffs2_fsinfo,
2163         "fsinfo\t- print information about filesystems\n",
2164         "    - print information about filesystems\n"
2165 );
2166
2167 #ifdef CONFIG_JFFS2_CMDLINE
2168 U_BOOT_CMD(
2169         chpart, 2,      0,      do_jffs2_chpart,
2170         "chpart\t- change active partition\n",
2171         "part-id\n"
2172         "    - change active partition (e.g. part-id = nand0,1)\n"
2173 );
2174
2175 U_BOOT_CMD(
2176         mtdparts,       6,      0,      do_jffs2_mtdparts,
2177         "mtdparts- define flash/nand partitions\n",
2178         "\n"
2179         "    - list partition table\n"
2180         "mtdparts delall\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"
2188         "-----\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"
2208 );
2209 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2210
2211 /***************************************************/
2212
2213 #endif