Tizen 2.1 base
[kernel/u-boot.git] / common / cmd_mtdparts.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  * (C) Copyright 2008
19  * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
20  *
21  *   $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
22  *   Copyright 2002 SYSGO Real-Time Solutions GmbH
23  *
24  * See file CREDITS for list of people who contributed to this
25  * project.
26  *
27  * This program is free software; you can redistribute it and/or
28  * modify it under the terms of the GNU General Public License as
29  * published by the Free Software Foundation; either version 2 of
30  * the License, or (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program; if not, write to the Free Software
39  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
40  * MA 02111-1307 USA
41  */
42
43 /*
44  * Three environment variables are used by the parsing routines:
45  *
46  * 'partition' - keeps current partition identifier
47  *
48  * partition  := <part-id>
49  * <part-id>  := <dev-id>,part_num
50  *
51  *
52  * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
53  *
54  * mtdids=<idmap>[,<idmap>,...]
55  *
56  * <idmap>    := <dev-id>=<mtd-id>
57  * <dev-id>   := 'nand'|'nor'|'onenand'<dev-num>
58  * <dev-num>  := mtd device number, 0...
59  * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
60  *
61  *
62  * 'mtdparts' - partition list
63  *
64  * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
65  *
66  * <mtd-def>  := <mtd-id>:<part-def>[,<part-def>...]
67  * <mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)
68  * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
69  * <size>     := standard linux memsize OR '-' to denote all remaining space
70  * <offset>   := partition start offset within the device
71  * <name>     := '(' NAME ')'
72  * <ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)
73  *
74  * Notes:
75  * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
76  * - if the above variables are not set defaults for a given target are used
77  *
78  * Examples:
79  *
80  * 1 NOR Flash, with 1 single writable partition:
81  * mtdids=nor0=edb7312-nor
82  * mtdparts=mtdparts=edb7312-nor:-
83  *
84  * 1 NOR Flash with 2 partitions, 1 NAND with one
85  * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
86  * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
87  *
88  */
89
90 #include <common.h>
91 #include <command.h>
92 #include <malloc.h>
93 #include <jffs2/load_kernel.h>
94 #include <linux/list.h>
95 #include <linux/ctype.h>
96 #include <linux/err.h>
97 #include <linux/mtd/mtd.h>
98
99 #if defined(CONFIG_CMD_NAND)
100 #include <linux/mtd/nand.h>
101 #include <nand.h>
102 #endif
103
104 #if defined(CONFIG_CMD_ONENAND)
105 #include <linux/mtd/onenand.h>
106 #include <onenand_uboot.h>
107 #endif
108
109 /* special size referring to all the remaining space in a partition */
110 #define SIZE_REMAINING          0xFFFFFFFF
111
112 /* special offset value, it is used when not provided by user
113  *
114  * this value is used temporarily during parsing, later such offests
115  * are recalculated */
116 #define OFFSET_NOT_SPECIFIED    0xFFFFFFFF
117
118 /* minimum partition size */
119 #define MIN_PART_SIZE           4096
120
121 /* this flag needs to be set in part_info struct mask_flags
122  * field for read-only partitions */
123 #define MTD_WRITEABLE_CMD               1
124
125 /* default values for mtdids and mtdparts variables */
126 #if defined(MTDIDS_DEFAULT)
127 static const char *const mtdids_default = MTDIDS_DEFAULT;
128 #else
129 static const char *const mtdids_default = NULL;
130 #endif
131
132 #if defined(MTDPARTS_DEFAULT)
133 static const char *const mtdparts_default = MTDPARTS_DEFAULT;
134 #else
135 static const char *const mtdparts_default = NULL;
136 #endif
137
138 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
139 #define MTDIDS_MAXLEN           128
140 #define MTDPARTS_MAXLEN         512
141 #define PARTITION_MAXLEN        16
142 static char last_ids[MTDIDS_MAXLEN];
143 static char last_parts[MTDPARTS_MAXLEN];
144 static char last_partition[PARTITION_MAXLEN];
145
146 /* low level jffs2 cache cleaning routine */
147 extern void jffs2_free_cache(struct part_info *part);
148
149 /* mtdids mapping list, filled by parse_ids() */
150 struct list_head mtdids;
151
152 /* device/partition list, parse_cmdline() parses into here */
153 struct list_head devices;
154
155 /* current active device and partition number */
156 struct mtd_device *current_mtd_dev = NULL;
157 u8 current_mtd_partnum = 0;
158
159 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
160
161 /* command line only routines */
162 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
163 static int device_del(struct mtd_device *dev);
164
165 /**
166  * Parses a string into a number.  The number stored at ptr is
167  * potentially suffixed with K (for kilobytes, or 1024 bytes),
168  * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
169  * 1073741824).  If the number is suffixed with K, M, or G, then
170  * the return value is the number multiplied by one kilobyte, one
171  * megabyte, or one gigabyte, respectively.
172  *
173  * @param ptr where parse begins
174  * @param retptr output pointer to next char after parse completes (output)
175  * @return resulting unsigned int
176  */
177 static unsigned long memsize_parse (const char *const ptr, const char **retptr)
178 {
179         unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
180
181         switch (**retptr) {
182                 case 'G':
183                 case 'g':
184                         ret <<= 10;
185                 case 'M':
186                 case 'm':
187                         ret <<= 10;
188                 case 'K':
189                 case 'k':
190                         ret <<= 10;
191                         (*retptr)++;
192                 default:
193                         break;
194         }
195
196         return ret;
197 }
198
199 #ifndef CONFIG_CMD_MTDPARTS_LITE
200 /**
201  * Format string describing supplied size. This routine does the opposite job
202  * to memsize_parse(). Size in bytes is converted to string and if possible
203  * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
204  *
205  * Note, that this routine does not check for buffer overflow, it's the caller
206  * who must assure enough space.
207  *
208  * @param buf output buffer
209  * @param size size to be converted to string
210  */
211 static void memsize_format(char *buf, u32 size)
212 {
213 #define SIZE_GB ((u32)1024*1024*1024)
214 #define SIZE_MB ((u32)1024*1024)
215 #define SIZE_KB ((u32)1024)
216
217         if ((size % SIZE_GB) == 0)
218                 sprintf(buf, "%ug", size/SIZE_GB);
219         else if ((size % SIZE_MB) == 0)
220                 sprintf(buf, "%um", size/SIZE_MB);
221         else if (size % SIZE_KB == 0)
222                 sprintf(buf, "%uk", size/SIZE_KB);
223         else
224                 sprintf(buf, "%u", size);
225 }
226 #endif
227
228 /**
229  * This routine does global indexing of all partitions. Resulting index for
230  * current partition is saved in 'mtddevnum'. Current partition name in
231  * 'mtddevname'.
232  */
233 static void index_partitions(void)
234 {
235         char buf[16];
236         u16 mtddevnum;
237         struct part_info *part;
238         struct list_head *dentry;
239         struct mtd_device *dev;
240
241         debug("--- index partitions ---\n");
242
243         if (current_mtd_dev) {
244                 mtddevnum = 0;
245                 list_for_each(dentry, &devices) {
246                         dev = list_entry(dentry, struct mtd_device, link);
247                         if (dev == current_mtd_dev) {
248                                 mtddevnum += current_mtd_partnum;
249                                 sprintf(buf, "%d", mtddevnum);
250                                 setenv("mtddevnum", buf);
251                                 break;
252                         }
253                         mtddevnum += dev->num_parts;
254                 }
255
256                 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
257                 setenv("mtddevname", part->name);
258
259                 debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
260         } else {
261                 setenv("mtddevnum", NULL);
262                 setenv("mtddevname", NULL);
263
264                 debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
265         }
266 }
267
268 /**
269  * Save current device and partition in environment variable 'partition'.
270  */
271 static void current_save(void)
272 {
273         char buf[16];
274
275         debug("--- current_save ---\n");
276
277         if (current_mtd_dev) {
278                 sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
279                                         current_mtd_dev->id->num, current_mtd_partnum);
280
281                 setenv("partition", buf);
282                 strncpy(last_partition, buf, 16);
283
284                 debug("=> partition %s\n", buf);
285         } else {
286                 setenv("partition", NULL);
287                 last_partition[0] = '\0';
288
289                 debug("=> partition NULL\n");
290         }
291         index_partitions();
292 }
293
294
295 /**
296  * Produce a mtd_info given a type and num.
297  *
298  * @param type mtd type
299  * @param num mtd number
300  * @param mtd a pointer to an mtd_info instance (output)
301  * @return 0 if device is valid, 1 otherwise
302  */
303 static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
304 {
305         char mtd_dev[16];
306
307         sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
308         *mtd = get_mtd_device_nm(mtd_dev);
309         if (IS_ERR(*mtd)) {
310                 printf("Device %s not found!\n", mtd_dev);
311                 return 1;
312         }
313
314         return 0;
315 }
316
317 /**
318  * Performs sanity check for supplied flash partition.
319  * Table of existing MTD flash devices is searched and partition device
320  * is located. Alignment with the granularity of nand erasesize is verified.
321  *
322  * @param id of the parent device
323  * @param part partition to validate
324  * @return 0 if partition is valid, 1 otherwise
325  */
326 static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
327 {
328         struct mtd_info *mtd = NULL;
329         int i, j;
330         ulong start;
331
332         if (get_mtd_info(id->type, id->num, &mtd))
333                 return 1;
334
335         part->sector_size = mtd->erasesize;
336
337         if (!mtd->numeraseregions) {
338                 /*
339                  * Only one eraseregion (NAND, OneNAND or uniform NOR),
340                  * checking for alignment is easy here
341                  */
342                 if ((unsigned long)part->offset % mtd->erasesize) {
343                         printf("%s%d: partition (%s) start offset"
344                                "alignment incorrect\n",
345                                MTD_DEV_TYPE(id->type), id->num, part->name);
346                         return 1;
347                 }
348
349                 if (part->size % mtd->erasesize) {
350                         printf("%s%d: partition (%s) size alignment incorrect\n",
351                                MTD_DEV_TYPE(id->type), id->num, part->name);
352                         return 1;
353                 }
354         } else {
355                 /*
356                  * Multiple eraseregions (non-uniform NOR),
357                  * checking for alignment is more complex here
358                  */
359
360                 /* Check start alignment */
361                 for (i = 0; i < mtd->numeraseregions; i++) {
362                         start = mtd->eraseregions[i].offset;
363                         for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
364                                 if (part->offset == start)
365                                         goto start_ok;
366                                 start += mtd->eraseregions[i].erasesize;
367                         }
368                 }
369
370                 printf("%s%d: partition (%s) start offset alignment incorrect\n",
371                        MTD_DEV_TYPE(id->type), id->num, part->name);
372                 return 1;
373
374         start_ok:
375
376                 /* Check end/size alignment */
377                 for (i = 0; i < mtd->numeraseregions; i++) {
378                         start = mtd->eraseregions[i].offset;
379                         for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
380                                 if ((part->offset + part->size) == start)
381                                         goto end_ok;
382                                 start += mtd->eraseregions[i].erasesize;
383                         }
384                 }
385                 /* Check last sector alignment */
386                 if ((part->offset + part->size) == start)
387                         goto end_ok;
388
389                 printf("%s%d: partition (%s) size alignment incorrect\n",
390                        MTD_DEV_TYPE(id->type), id->num, part->name);
391                 return 1;
392
393         end_ok:
394                 return 0;
395         }
396
397         return 0;
398 }
399
400
401 /**
402  * Performs sanity check for supplied partition. Offset and size are verified
403  * to be within valid range. Partition type is checked and either
404  * parts_validate_nor() or parts_validate_nand() is called with the argument
405  * of part.
406  *
407  * @param id of the parent device
408  * @param part partition to validate
409  * @return 0 if partition is valid, 1 otherwise
410  */
411 static int part_validate(struct mtdids *id, struct part_info *part)
412 {
413         if (part->size == SIZE_REMAINING)
414                 part->size = id->size - part->offset;
415
416         if (part->offset > id->size) {
417                 printf("%s: offset %08x beyond flash size %08x\n",
418                                 id->mtd_id, part->offset, id->size);
419                 return 1;
420         }
421
422         if ((part->offset + part->size) <= part->offset) {
423                 printf("%s%d: partition (%s) size too big\n",
424                                 MTD_DEV_TYPE(id->type), id->num, part->name);
425                 return 1;
426         }
427
428         if (part->offset + part->size > id->size) {
429                 printf("%s: partitioning exceeds flash size\n", id->mtd_id);
430                 return 1;
431         }
432
433         /*
434          * Now we need to check if the partition starts and ends on
435          * sector (eraseblock) regions
436          */
437         return part_validate_eraseblock(id, part);
438 }
439
440 #ifndef CONFIG_CMD_MTDPARTS_LITE
441 /**
442  * Delete selected partition from the partion list of the specified device.
443  *
444  * @param dev device to delete partition from
445  * @param part partition to delete
446  * @return 0 on success, 1 otherwise
447  */
448 static int part_del(struct mtd_device *dev, struct part_info *part)
449 {
450         u8 current_save_needed = 0;
451
452         /* if there is only one partition, remove whole device */
453         if (dev->num_parts == 1)
454                 return device_del(dev);
455
456         /* otherwise just delete this partition */
457
458         if (dev == current_mtd_dev) {
459                 /* we are modyfing partitions for the current device,
460                  * update current */
461                 struct part_info *curr_pi;
462                 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
463
464                 if (curr_pi) {
465                         if (curr_pi == part) {
466                                 printf("current partition deleted, resetting current to 0\n");
467                                 current_mtd_partnum = 0;
468                         } else if (part->offset <= curr_pi->offset) {
469                                 current_mtd_partnum--;
470                         }
471                         current_save_needed = 1;
472                 }
473         }
474
475         list_del(&part->link);
476         free(part);
477         dev->num_parts--;
478
479         if (current_save_needed > 0)
480                 current_save();
481         else
482                 index_partitions();
483
484         return 0;
485 }
486 #endif
487
488 /**
489  * Delete all partitions from parts head list, free memory.
490  *
491  * @param head list of partitions to delete
492  */
493 static void part_delall(struct list_head *head)
494 {
495         struct list_head *entry, *n;
496         struct part_info *part_tmp;
497
498         /* clean tmp_list and free allocated memory */
499         list_for_each_safe(entry, n, head) {
500                 part_tmp = list_entry(entry, struct part_info, link);
501
502                 list_del(entry);
503                 free(part_tmp);
504         }
505 }
506
507 /**
508  * Add new partition to the supplied partition list. Make sure partitions are
509  * sorted by offset in ascending order.
510  *
511  * @param head list this partition is to be added to
512  * @param new partition to be added
513  */
514 static int part_sort_add(struct mtd_device *dev, struct part_info *part)
515 {
516         struct list_head *entry;
517         struct part_info *new_pi, *curr_pi;
518
519         /* link partition to parrent dev */
520         part->dev = dev;
521
522         if (list_empty(&dev->parts)) {
523                 debug("part_sort_add: list empty\n");
524                 list_add(&part->link, &dev->parts);
525                 dev->num_parts++;
526                 index_partitions();
527                 return 0;
528         }
529
530         new_pi = list_entry(&part->link, struct part_info, link);
531
532         /* get current partition info if we are updating current device */
533         curr_pi = NULL;
534         if (dev == current_mtd_dev)
535                 curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
536
537         list_for_each(entry, &dev->parts) {
538                 struct part_info *pi;
539
540                 pi = list_entry(entry, struct part_info, link);
541
542                 /* be compliant with kernel cmdline, allow only one partition at offset zero */
543                 if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
544                         printf("cannot add second partition at offset 0\n");
545                         return 1;
546                 }
547
548                 if (new_pi->offset <= pi->offset) {
549                         list_add_tail(&part->link, entry);
550                         dev->num_parts++;
551
552                         if (curr_pi && (pi->offset <= curr_pi->offset)) {
553                                 /* we are modyfing partitions for the current
554                                  * device, update current */
555                                 current_mtd_partnum++;
556                                 current_save();
557                         } else {
558                                 index_partitions();
559                         }
560                         return 0;
561                 }
562         }
563
564         list_add_tail(&part->link, &dev->parts);
565         dev->num_parts++;
566         index_partitions();
567         return 0;
568 }
569
570 #ifndef CONFIG_CMD_MTDPARTS_LITE
571 /**
572  * Add provided partition to the partition list of a given device.
573  *
574  * @param dev device to which partition is added
575  * @param part partition to be added
576  * @return 0 on success, 1 otherwise
577  */
578 static int part_add(struct mtd_device *dev, struct part_info *part)
579 {
580         /* verify alignment and size */
581         if (part_validate(dev->id, part) != 0)
582                 return 1;
583
584         /* partition is ok, add it to the list */
585         if (part_sort_add(dev, part) != 0)
586                 return 1;
587
588         return 0;
589 }
590 #endif
591
592 /**
593  * Parse one partition definition, allocate memory and return pointer to this
594  * location in retpart.
595  *
596  * @param partdef pointer to the partition definition string i.e. <part-def>
597  * @param ret output pointer to next char after parse completes (output)
598  * @param retpart pointer to the allocated partition (output)
599  * @return 0 on success, 1 otherwise
600  */
601 static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
602 {
603         struct part_info *part;
604         unsigned long size;
605         unsigned long offset;
606         const char *name;
607         int name_len;
608         unsigned int mask_flags;
609         const char *p;
610
611         p = partdef;
612         *retpart = NULL;
613         *ret = NULL;
614
615         /* fetch the partition size */
616         if (*p == '-') {
617                 /* assign all remaining space to this partition */
618                 debug("'-': remaining size assigned\n");
619                 size = SIZE_REMAINING;
620                 p++;
621         } else {
622                 size = memsize_parse(p, &p);
623                 if (size < MIN_PART_SIZE) {
624                         printf("partition size too small (%lx)\n", size);
625                         return 1;
626                 }
627         }
628
629         /* check for offset */
630         offset = OFFSET_NOT_SPECIFIED;
631         if (*p == '@') {
632                 p++;
633                 offset = memsize_parse(p, &p);
634         }
635
636         /* now look for the name */
637         if (*p == '(') {
638                 name = ++p;
639                 if ((p = strchr(name, ')')) == NULL) {
640                         printf("no closing ) found in partition name\n");
641                         return 1;
642                 }
643                 name_len = p - name + 1;
644                 if ((name_len - 1) == 0) {
645                         printf("empty partition name\n");
646                         return 1;
647                 }
648                 p++;
649         } else {
650                 /* 0x00000000@0x00000000 */
651                 name_len = 22;
652                 name = NULL;
653         }
654
655         /* test for options */
656         mask_flags = 0;
657         if (strncmp(p, "ro", 2) == 0) {
658                 mask_flags |= MTD_WRITEABLE_CMD;
659                 p += 2;
660         }
661
662         /* check for next partition definition */
663         if (*p == ',') {
664                 if (size == SIZE_REMAINING) {
665                         *ret = NULL;
666                         printf("no partitions allowed after a fill-up partition\n");
667                         return 1;
668                 }
669                 *ret = ++p;
670         } else if ((*p == ';') || (*p == '\0')) {
671                 *ret = p;
672         } else {
673                 printf("unexpected character '%c' at the end of partition\n", *p);
674                 *ret = NULL;
675                 return 1;
676         }
677
678         /*  allocate memory */
679         part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
680         if (!part) {
681                 printf("out of memory\n");
682                 return 1;
683         }
684         memset(part, 0, sizeof(struct part_info) + name_len);
685         part->size = size;
686         part->offset = offset;
687         part->mask_flags = mask_flags;
688         part->name = (char *)(part + 1);
689
690         if (name) {
691                 /* copy user provided name */
692                 strncpy(part->name, name, name_len - 1);
693                 part->auto_name = 0;
694         } else {
695                 /* auto generated name in form of size@offset */
696                 sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
697                 part->auto_name = 1;
698         }
699
700         part->name[name_len - 1] = '\0';
701         INIT_LIST_HEAD(&part->link);
702
703         debug("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
704                         part->name, part->size,
705                         part->offset, part->mask_flags);
706
707         *retpart = part;
708         return 0;
709 }
710
711 /**
712  * Check device number to be within valid range for given device type.
713  *
714  * @param type mtd type
715  * @param num mtd number
716  * @param size a pointer to the size of the mtd device (output)
717  * @return 0 if device is valid, 1 otherwise
718  */
719 int mtd_device_validate(u8 type, u8 num, u32 *size)
720 {
721         struct mtd_info *mtd = NULL;
722
723         if (get_mtd_info(type, num, &mtd))
724                 return 1;
725
726         *size = mtd->size;
727
728         return 0;
729 }
730
731 /**
732  * Delete all mtd devices from a supplied devices list, free memory allocated for
733  * each device and delete all device partitions.
734  *
735  * @return 0 on success, 1 otherwise
736  */
737 static int device_delall(struct list_head *head)
738 {
739         struct list_head *entry, *n;
740         struct mtd_device *dev_tmp;
741
742         /* clean devices list */
743         list_for_each_safe(entry, n, head) {
744                 dev_tmp = list_entry(entry, struct mtd_device, link);
745                 list_del(entry);
746                 part_delall(&dev_tmp->parts);
747                 free(dev_tmp);
748         }
749         INIT_LIST_HEAD(&devices);
750
751         return 0;
752 }
753
754 /**
755  * If provided device exists it's partitions are deleted, device is removed
756  * from device list and device memory is freed.
757  *
758  * @param dev device to be deleted
759  * @return 0 on success, 1 otherwise
760  */
761 static int device_del(struct mtd_device *dev)
762 {
763         part_delall(&dev->parts);
764         list_del(&dev->link);
765         free(dev);
766
767         if (dev == current_mtd_dev) {
768                 /* we just deleted current device */
769                 if (list_empty(&devices)) {
770                         current_mtd_dev = NULL;
771                 } else {
772                         /* reset first partition from first dev from the
773                          * devices list as current */
774                         current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
775                         current_mtd_partnum = 0;
776                 }
777                 current_save();
778                 return 0;
779         }
780
781         index_partitions();
782         return 0;
783 }
784
785 /**
786  * Search global device list and return pointer to the device of type and num
787  * specified.
788  *
789  * @param type device type
790  * @param num device number
791  * @return NULL if requested device does not exist
792  */
793 struct mtd_device *device_find(u8 type, u8 num)
794 {
795         struct list_head *entry;
796         struct mtd_device *dev_tmp;
797
798         list_for_each(entry, &devices) {
799                 dev_tmp = list_entry(entry, struct mtd_device, link);
800
801                 if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
802                         return dev_tmp;
803         }
804
805         return NULL;
806 }
807
808 #ifndef CONFIG_CMD_MTDPARTS_LITE
809 /**
810  * Add specified device to the global device list.
811  *
812  * @param dev device to be added
813  */
814 static void device_add(struct mtd_device *dev)
815 {
816         u8 current_save_needed = 0;
817
818         if (list_empty(&devices)) {
819                 current_mtd_dev = dev;
820                 current_mtd_partnum = 0;
821                 current_save_needed = 1;
822         }
823
824         list_add_tail(&dev->link, &devices);
825
826         if (current_save_needed > 0)
827                 current_save();
828         else
829                 index_partitions();
830 }
831 #endif
832
833 /**
834  * Parse device type, name and mtd-id. If syntax is ok allocate memory and
835  * return pointer to the device structure.
836  *
837  * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
838  * @param ret output pointer to next char after parse completes (output)
839  * @param retdev pointer to the allocated device (output)
840  * @return 0 on success, 1 otherwise
841  */
842 static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
843 {
844         struct mtd_device *dev;
845         struct part_info *part;
846         struct mtdids *id;
847         const char *mtd_id;
848         unsigned int mtd_id_len;
849         const char *p, *pend;
850         LIST_HEAD(tmp_list);
851         struct list_head *entry, *n;
852         u16 num_parts;
853         u32 offset;
854         int err = 1;
855
856         debug("===device_parse===\n");
857
858         assert(retdev);
859         *retdev = NULL;
860
861         if (ret)
862                 *ret = NULL;
863
864         /* fetch <mtd-id> */
865         mtd_id = p = mtd_dev;
866         if (!(p = strchr(mtd_id, ':'))) {
867                 printf("no <mtd-id> identifier\n");
868                 return 1;
869         }
870         mtd_id_len = p - mtd_id + 1;
871         p++;
872
873         /* verify if we have a valid device specified */
874         if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
875                 printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
876                 return 1;
877         }
878
879         debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
880                         id->type, MTD_DEV_TYPE(id->type),
881                         id->num, id->mtd_id);
882         pend = strchr(p, ';');
883         debug("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
884
885
886         /* parse partitions */
887         num_parts = 0;
888
889         offset = 0;
890         if ((dev = device_find(id->type, id->num)) != NULL) {
891                 /* if device already exists start at the end of the last partition */
892                 part = list_entry(dev->parts.prev, struct part_info, link);
893                 offset = part->offset + part->size;
894         }
895
896         while (p && (*p != '\0') && (*p != ';')) {
897                 err = 1;
898                 if ((part_parse(p, &p, &part) != 0) || (!part))
899                         break;
900
901                 /* calculate offset when not specified */
902                 if (part->offset == OFFSET_NOT_SPECIFIED)
903                         part->offset = offset;
904                 else
905                         offset = part->offset;
906
907                 /* verify alignment and size */
908                 if (part_validate(id, part) != 0)
909                         break;
910
911                 offset += part->size;
912
913                 /* partition is ok, add it to the list */
914                 list_add_tail(&part->link, &tmp_list);
915                 num_parts++;
916                 err = 0;
917         }
918         if (err == 1) {
919                 part_delall(&tmp_list);
920                 return 1;
921         }
922
923         if (num_parts == 0) {
924                 printf("no partitions for device %s%d (%s)\n",
925                                 MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
926                 return 1;
927         }
928
929         debug("\ntotal partitions: %d\n", num_parts);
930
931         /* check for next device presence */
932         if (p) {
933                 if (*p == ';') {
934                         if (ret)
935                                 *ret = ++p;
936                 } else if (*p == '\0') {
937                         if (ret)
938                                 *ret = p;
939                 } else {
940                         printf("unexpected character '%c' at the end of device\n", *p);
941                         if (ret)
942                                 *ret = NULL;
943                         return 1;
944                 }
945         }
946
947         /* allocate memory for mtd_device structure */
948         if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
949                 printf("out of memory\n");
950                 return 1;
951         }
952         memset(dev, 0, sizeof(struct mtd_device));
953         dev->id = id;
954         dev->num_parts = 0; /* part_sort_add increments num_parts */
955         INIT_LIST_HEAD(&dev->parts);
956         INIT_LIST_HEAD(&dev->link);
957
958         /* move partitions from tmp_list to dev->parts */
959         list_for_each_safe(entry, n, &tmp_list) {
960                 part = list_entry(entry, struct part_info, link);
961                 list_del(entry);
962                 if (part_sort_add(dev, part) != 0) {
963                         device_del(dev);
964                         return 1;
965                 }
966         }
967
968         *retdev = dev;
969
970         debug("===\n\n");
971         return 0;
972 }
973
974 /**
975  * Initialize global device list.
976  *
977  * @return 0 on success, 1 otherwise
978  */
979 static int mtd_devices_init(void)
980 {
981         last_parts[0] = '\0';
982         current_mtd_dev = NULL;
983         current_save();
984
985         return device_delall(&devices);
986 }
987
988 #ifndef CONFIG_CMD_MTDPARTS_LITE
989 /*
990  * Search global mtdids list and find id of requested type and number.
991  *
992  * @return pointer to the id if it exists, NULL otherwise
993  */
994 static struct mtdids* id_find(u8 type, u8 num)
995 {
996         struct list_head *entry;
997         struct mtdids *id;
998
999         list_for_each(entry, &mtdids) {
1000                 id = list_entry(entry, struct mtdids, link);
1001
1002                 if ((id->type == type) && (id->num == num))
1003                         return id;
1004         }
1005
1006         return NULL;
1007 }
1008 #endif
1009
1010 /**
1011  * Search global mtdids list and find id of a requested mtd_id.
1012  *
1013  * Note: first argument is not null terminated.
1014  *
1015  * @param mtd_id string containing requested mtd_id
1016  * @param mtd_id_len length of supplied mtd_id
1017  * @return pointer to the id if it exists, NULL otherwise
1018  */
1019 static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
1020 {
1021         struct list_head *entry;
1022         struct mtdids *id;
1023
1024         debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1025                         mtd_id_len, mtd_id, mtd_id_len);
1026
1027         list_for_each(entry, &mtdids) {
1028                 id = list_entry(entry, struct mtdids, link);
1029
1030                 debug("entry: '%s' (len = %d)\n",
1031                                 id->mtd_id, strlen(id->mtd_id));
1032
1033                 if (mtd_id_len != strlen(id->mtd_id))
1034                         continue;
1035                 if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
1036                         return id;
1037         }
1038
1039         return NULL;
1040 }
1041
1042 /**
1043  * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1044  * return device type and number.
1045  *
1046  * @param id string describing device id
1047  * @param ret_id output pointer to next char after parse completes (output)
1048  * @param dev_type parsed device type (output)
1049  * @param dev_num parsed device number (output)
1050  * @return 0 on success, 1 otherwise
1051  */
1052 int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
1053 {
1054         const char *p = id;
1055
1056         *dev_type = 0;
1057         if (strncmp(p, "nand", 4) == 0) {
1058                 *dev_type = MTD_DEV_TYPE_NAND;
1059                 p += 4;
1060         } else if (strncmp(p, "nor", 3) == 0) {
1061                 *dev_type = MTD_DEV_TYPE_NOR;
1062                 p += 3;
1063         } else if (strncmp(p, "onenand", 7) == 0) {
1064                 *dev_type = MTD_DEV_TYPE_ONENAND;
1065                 p += 7;
1066         } else {
1067                 printf("incorrect device type in %s\n", id);
1068                 return 1;
1069         }
1070
1071         if (!isdigit(*p)) {
1072                 printf("incorrect device number in %s\n", id);
1073                 return 1;
1074         }
1075
1076         *dev_num = simple_strtoul(p, (char **)&p, 0);
1077         if (ret_id)
1078                 *ret_id = p;
1079         return 0;
1080 }
1081
1082 #ifndef CONFIG_CMD_MTDPARTS_LITE
1083 /**
1084  * Process all devices and generate corresponding mtdparts string describing
1085  * all partitions on all devices.
1086  *
1087  * @param buf output buffer holding generated mtdparts string (output)
1088  * @param buflen buffer size
1089  * @return 0 on success, 1 otherwise
1090  */
1091 static int generate_mtdparts(char *buf, u32 buflen)
1092 {
1093         struct list_head *pentry, *dentry;
1094         struct mtd_device *dev;
1095         struct part_info *part, *prev_part;
1096         char *p = buf;
1097         char tmpbuf[32];
1098         u32 size, offset, len, part_cnt;
1099         u32 maxlen = buflen - 1;
1100
1101         debug("--- generate_mtdparts ---\n");
1102
1103         if (list_empty(&devices)) {
1104                 buf[0] = '\0';
1105                 return 0;
1106         }
1107
1108         sprintf(p, "mtdparts=");
1109         p += 9;
1110
1111         list_for_each(dentry, &devices) {
1112                 dev = list_entry(dentry, struct mtd_device, link);
1113
1114                 /* copy mtd_id */
1115                 len = strlen(dev->id->mtd_id) + 1;
1116                 if (len > maxlen)
1117                         goto cleanup;
1118                 memcpy(p, dev->id->mtd_id, len - 1);
1119                 p += len - 1;
1120                 *(p++) = ':';
1121                 maxlen -= len;
1122
1123                 /* format partitions */
1124                 prev_part = NULL;
1125                 part_cnt = 0;
1126                 list_for_each(pentry, &dev->parts) {
1127                         part = list_entry(pentry, struct part_info, link);
1128                         size = part->size;
1129                         offset = part->offset;
1130                         part_cnt++;
1131
1132                         /* partition size */
1133                         memsize_format(tmpbuf, size);
1134                         len = strlen(tmpbuf);
1135                         if (len > maxlen)
1136                                 goto cleanup;
1137                         memcpy(p, tmpbuf, len);
1138                         p += len;
1139                         maxlen -= len;
1140
1141
1142                         /* add offset only when there is a gap between
1143                          * partitions */
1144                         if ((!prev_part && (offset != 0)) ||
1145                                         (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
1146
1147                                 memsize_format(tmpbuf, offset);
1148                                 len = strlen(tmpbuf) + 1;
1149                                 if (len > maxlen)
1150                                         goto cleanup;
1151                                 *(p++) = '@';
1152                                 memcpy(p, tmpbuf, len - 1);
1153                                 p += len - 1;
1154                                 maxlen -= len;
1155                         }
1156
1157                         /* copy name only if user supplied */
1158                         if(!part->auto_name) {
1159                                 len = strlen(part->name) + 2;
1160                                 if (len > maxlen)
1161                                         goto cleanup;
1162
1163                                 *(p++) = '(';
1164                                 memcpy(p, part->name, len - 2);
1165                                 p += len - 2;
1166                                 *(p++) = ')';
1167                                 maxlen -= len;
1168                         }
1169
1170                         /* ro mask flag */
1171                         if (part->mask_flags && MTD_WRITEABLE_CMD) {
1172                                 len = 2;
1173                                 if (len > maxlen)
1174                                         goto cleanup;
1175                                 *(p++) = 'r';
1176                                 *(p++) = 'o';
1177                                 maxlen -= 2;
1178                         }
1179
1180                         /* print ',' separator if there are other partitions
1181                          * following */
1182                         if (dev->num_parts > part_cnt) {
1183                                 if (1 > maxlen)
1184                                         goto cleanup;
1185                                 *(p++) = ',';
1186                                 maxlen--;
1187                         }
1188                         prev_part = part;
1189                 }
1190                 /* print ';' separator if there are other devices following */
1191                 if (dentry->next != &devices) {
1192                         if (1 > maxlen)
1193                                 goto cleanup;
1194                         *(p++) = ';';
1195                         maxlen--;
1196                 }
1197         }
1198
1199         /* we still have at least one char left, as we decremented maxlen at
1200          * the begining */
1201         *p = '\0';
1202
1203         return 0;
1204
1205 cleanup:
1206         last_parts[0] = '\0';
1207         return 1;
1208 }
1209
1210 /**
1211  * Call generate_mtdparts to process all devices and generate corresponding
1212  * mtdparts string, save it in mtdparts environment variable.
1213  *
1214  * @param buf output buffer holding generated mtdparts string (output)
1215  * @param buflen buffer size
1216  * @return 0 on success, 1 otherwise
1217  */
1218 static int generate_mtdparts_save(char *buf, u32 buflen)
1219 {
1220         int ret;
1221
1222         ret = generate_mtdparts(buf, buflen);
1223
1224         if ((buf[0] != '\0') && (ret == 0))
1225                 setenv("mtdparts", buf);
1226         else
1227                 setenv("mtdparts", NULL);
1228
1229         return ret;
1230 }
1231 #endif
1232
1233 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1234 /**
1235  * Get the net size (w/o bad blocks) of the given partition.
1236  *
1237  * @param mtd the mtd info
1238  * @param part the partition
1239  * @return the calculated net size of this partition
1240  */
1241 static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
1242 {
1243         uint64_t i, net_size = 0;
1244
1245         if (!mtd->block_isbad)
1246                 return part->size;
1247
1248         for (i = 0; i < part->size; i += mtd->erasesize) {
1249                 if (!mtd->block_isbad(mtd, part->offset + i))
1250                         net_size += mtd->erasesize;
1251         }
1252
1253         return net_size;
1254 }
1255 #endif
1256
1257 static void print_partition_table(void)
1258 {
1259         struct list_head *dentry, *pentry;
1260         struct part_info *part;
1261         struct mtd_device *dev;
1262         int part_num;
1263
1264         list_for_each(dentry, &devices) {
1265                 dev = list_entry(dentry, struct mtd_device, link);
1266                 /* list partitions for given device */
1267                 part_num = 0;
1268 #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
1269                 struct mtd_info *mtd;
1270
1271                 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1272                         return;
1273
1274                 printf("\ndevice %s%d <%s>, # parts = %d\n",
1275                                 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1276                                 dev->id->mtd_id, dev->num_parts);
1277                 printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
1278
1279                 list_for_each(pentry, &dev->parts) {
1280                         u32 net_size;
1281                         char *size_note;
1282
1283                         part = list_entry(pentry, struct part_info, link);
1284                         net_size = net_part_size(mtd, part);
1285                         size_note = part->size == net_size ? " " : " (!)";
1286                         printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
1287                                         part_num, part->name, part->size,
1288                                         net_size, size_note, part->offset,
1289                                         part->mask_flags);
1290 #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1291                 printf("\ndevice %s%d <%s>, # parts = %d\n",
1292                                 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1293                                 dev->id->mtd_id, dev->num_parts);
1294                 printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
1295
1296                 list_for_each(pentry, &dev->parts) {
1297                         part = list_entry(pentry, struct part_info, link);
1298                         printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1299                                         part_num, part->name, part->size,
1300                                         part->offset, part->mask_flags);
1301 #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
1302                         part_num++;
1303                 }
1304         }
1305
1306         if (list_empty(&devices))
1307                 printf("no partitions defined\n");
1308 }
1309
1310 /**
1311  * Format and print out a partition list for each device from global device
1312  * list.
1313  */
1314 static void list_partitions(void)
1315 {
1316         struct part_info *part;
1317
1318         debug("\n---list_partitions---\n");
1319         print_partition_table();
1320
1321         /* current_mtd_dev is not NULL only when we have non empty device list */
1322         if (current_mtd_dev) {
1323                 part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
1324                 if (part) {
1325                         printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
1326                                         MTD_DEV_TYPE(current_mtd_dev->id->type),
1327                                         current_mtd_dev->id->num, current_mtd_partnum,
1328                                         part->name, part->size, part->offset);
1329                 } else {
1330                         printf("could not get current partition info\n\n");
1331                 }
1332         }
1333
1334         printf("\ndefaults:\n");
1335         printf("mtdids  : %s\n",
1336                 mtdids_default ? mtdids_default : "none");
1337         /*
1338          * Using printf() here results in printbuffer overflow
1339          * if default mtdparts string is greater than console
1340          * printbuffer. Use puts() to prevent system crashes.
1341          */
1342         puts("mtdparts: ");
1343         puts(mtdparts_default ? mtdparts_default : "none");
1344         puts("\n");
1345 }
1346
1347 /**
1348  * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1349  * corresponding device and verify partition number.
1350  *
1351  * @param id string describing device and partition or partition name
1352  * @param dev pointer to the requested device (output)
1353  * @param part_num verified partition number (output)
1354  * @param part pointer to requested partition (output)
1355  * @return 0 on success, 1 otherwise
1356  */
1357 int find_dev_and_part(const char *id, struct mtd_device **dev,
1358                 u8 *part_num, struct part_info **part)
1359 {
1360         struct list_head *dentry, *pentry;
1361         u8 type, dnum, pnum;
1362         const char *p;
1363
1364         debug("--- find_dev_and_part ---\nid = %s\n", id);
1365
1366         list_for_each(dentry, &devices) {
1367                 *part_num = 0;
1368                 *dev = list_entry(dentry, struct mtd_device, link);
1369                 list_for_each(pentry, &(*dev)->parts) {
1370                         *part = list_entry(pentry, struct part_info, link);
1371                         if (strcmp((*part)->name, id) == 0)
1372                                 return 0;
1373                         (*part_num)++;
1374                 }
1375         }
1376
1377         p = id;
1378         *dev = NULL;
1379         *part = NULL;
1380         *part_num = 0;
1381
1382         if (mtd_id_parse(p, &p, &type, &dnum) != 0)
1383                 return 1;
1384
1385         if ((*p++ != ',') || (*p == '\0')) {
1386                 printf("no partition number specified\n");
1387                 return 1;
1388         }
1389         pnum = simple_strtoul(p, (char **)&p, 0);
1390         if (*p != '\0') {
1391                 printf("unexpected trailing character '%c'\n", *p);
1392                 return 1;
1393         }
1394
1395         if ((*dev = device_find(type, dnum)) == NULL) {
1396                 printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
1397                 return 1;
1398         }
1399
1400         if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
1401                 printf("no such partition\n");
1402                 *dev = NULL;
1403                 return 1;
1404         }
1405
1406         *part_num = pnum;
1407
1408         return 0;
1409 }
1410
1411 #ifndef CONFIG_CMD_MTDPARTS_LITE
1412 /**
1413  * Find and delete partition. For partition id format see find_dev_and_part().
1414  *
1415  * @param id string describing device and partition
1416  * @return 0 on success, 1 otherwise
1417  */
1418 static int delete_partition(const char *id)
1419 {
1420         u8 pnum;
1421         struct mtd_device *dev;
1422         struct part_info *part;
1423
1424         if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
1425
1426                 debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08x@0x%08x\n",
1427                                 MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
1428                                 part->name, part->size, part->offset);
1429
1430                 if (part_del(dev, part) != 0)
1431                         return 1;
1432
1433                 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1434                         printf("generated mtdparts too long, reseting to null\n");
1435                         return 1;
1436                 }
1437                 return 0;
1438         }
1439
1440         printf("partition %s not found\n", id);
1441         return 1;
1442 }
1443 #endif
1444
1445 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1446 /**
1447  * Increase the size of the given partition so that it's net size is at least
1448  * as large as the size member and such that the next partition would start on a
1449  * good block if it were adjacent to this partition.
1450  *
1451  * @param mtd the mtd device
1452  * @param part the partition
1453  * @param next_offset pointer to the offset of the next partition after this
1454  *                    partition's size has been modified (output)
1455  */
1456 static void spread_partition(struct mtd_info *mtd, struct part_info *part,
1457                              uint64_t *next_offset)
1458 {
1459         uint64_t net_size, padding_size = 0;
1460         int truncated;
1461
1462         mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
1463                              &truncated);
1464
1465         /*
1466          * Absorb bad blocks immediately following this
1467          * partition also into the partition, such that
1468          * the next partition starts with a good block.
1469          */
1470         if (!truncated) {
1471                 mtd_get_len_incl_bad(mtd, part->offset + net_size,
1472                                      mtd->erasesize, &padding_size, &truncated);
1473                 if (truncated)
1474                         padding_size = 0;
1475                 else
1476                         padding_size -= mtd->erasesize;
1477         }
1478
1479         if (truncated) {
1480                 printf("truncated partition %s to %lld bytes\n", part->name,
1481                        (uint64_t) net_size + padding_size);
1482         }
1483
1484         part->size = net_size + padding_size;
1485         *next_offset = part->offset + part->size;
1486 }
1487
1488 /**
1489  * Adjust all of the partition sizes, such that all partitions are at least
1490  * as big as their mtdparts environment variable sizes and they each start
1491  * on a good block.
1492  *
1493  * @return 0 on success, 1 otherwise
1494  */
1495 static int spread_partitions(void)
1496 {
1497         struct list_head *dentry, *pentry;
1498         struct mtd_device *dev;
1499         struct part_info *part;
1500         struct mtd_info *mtd;
1501         int part_num;
1502         uint64_t cur_offs;
1503
1504         list_for_each(dentry, &devices) {
1505                 dev = list_entry(dentry, struct mtd_device, link);
1506
1507                 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1508                         return 1;
1509
1510                 part_num = 0;
1511                 cur_offs = 0;
1512                 list_for_each(pentry, &dev->parts) {
1513                         part = list_entry(pentry, struct part_info, link);
1514
1515                         debug("spread_partitions: device = %s%d, partition %d ="
1516                                 " (%s) 0x%08x@0x%08x\n",
1517                                 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1518                                 part_num, part->name, part->size,
1519                                 part->offset);
1520
1521                         if (cur_offs > part->offset)
1522                                 part->offset = cur_offs;
1523
1524                         spread_partition(mtd, part, &cur_offs);
1525
1526                         part_num++;
1527                 }
1528         }
1529
1530         index_partitions();
1531
1532         if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1533                 printf("generated mtdparts too long, reseting to null\n");
1534                 return 1;
1535         }
1536         return 0;
1537 }
1538 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1539
1540 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
1541 /**
1542  * Increase the size of the given partition so that it's net size is at least
1543  * as large as the size member and such that the next partition would start on a
1544  * good block if it were adjacent to this partition.
1545  *
1546  * @param mtd the mtd device
1547  * @param part the partition
1548  * @param next_offset pointer to the offset of the next partition after this
1549  *                    partition's size has been modified (output)
1550  */
1551 static void spread_partition(struct mtd_info *mtd, struct part_info *part,
1552                              uint64_t *next_offset)
1553 {
1554         uint64_t net_size, padding_size = 0;
1555         int truncated;
1556
1557         mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
1558                              &truncated);
1559
1560         /*
1561          * Absorb bad blocks immediately following this
1562          * partition also into the partition, such that
1563          * the next partition starts with a good block.
1564          */
1565         if (!truncated) {
1566                 mtd_get_len_incl_bad(mtd, part->offset + net_size,
1567                                      mtd->erasesize, &padding_size, &truncated);
1568                 if (truncated)
1569                         padding_size = 0;
1570                 else
1571                         padding_size -= mtd->erasesize;
1572         }
1573
1574         if (truncated) {
1575                 printf("truncated partition %s to %lld bytes\n", part->name,
1576                        (uint64_t) net_size + padding_size);
1577         }
1578
1579         part->size = net_size + padding_size;
1580         *next_offset = part->offset + part->size;
1581 }
1582
1583 /**
1584  * Adjust all of the partition sizes, such that all partitions are at least
1585  * as big as their mtdparts environment variable sizes and they each start
1586  * on a good block.
1587  *
1588  * @return 0 on success, 1 otherwise
1589  */
1590 static int spread_partitions(void)
1591 {
1592         struct list_head *dentry, *pentry;
1593         struct mtd_device *dev;
1594         struct part_info *part;
1595         struct mtd_info *mtd;
1596         int part_num;
1597         uint64_t cur_offs;
1598
1599         list_for_each(dentry, &devices) {
1600                 dev = list_entry(dentry, struct mtd_device, link);
1601
1602                 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
1603                         return 1;
1604
1605                 part_num = 0;
1606                 cur_offs = 0;
1607                 list_for_each(pentry, &dev->parts) {
1608                         part = list_entry(pentry, struct part_info, link);
1609
1610                         debug("spread_partitions: device = %s%d, partition %d ="
1611                                 " (%s) 0x%08x@0x%08x\n",
1612                                 MTD_DEV_TYPE(dev->id->type), dev->id->num,
1613                                 part_num, part->name, part->size,
1614                                 part->offset);
1615
1616                         if (cur_offs > part->offset)
1617                                 part->offset = cur_offs;
1618
1619                         spread_partition(mtd, part, &cur_offs);
1620
1621                         part_num++;
1622                 }
1623         }
1624
1625         index_partitions();
1626
1627         if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
1628                 printf("generated mtdparts too long, reseting to null\n");
1629                 return 1;
1630         }
1631         return 0;
1632 }
1633 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
1634
1635 /**
1636  * Accept character string describing mtd partitions and call device_parse()
1637  * for each entry. Add created devices to the global devices list.
1638  *
1639  * @param mtdparts string specifing mtd partitions
1640  * @return 0 on success, 1 otherwise
1641  */
1642 static int parse_mtdparts(const char *const mtdparts)
1643 {
1644         const char *p = mtdparts;
1645         struct mtd_device *dev;
1646         int err = 1;
1647
1648         debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
1649
1650         /* delete all devices and partitions */
1651         if (mtd_devices_init() != 0) {
1652                 printf("could not initialise device list\n");
1653                 return err;
1654         }
1655
1656         /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
1657         p = getenv("mtdparts");
1658
1659         if (strncmp(p, "mtdparts=", 9) != 0) {
1660                 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1661                 return err;
1662         }
1663         p += 9;
1664
1665         while (p && (*p != '\0')) {
1666                 err = 1;
1667                 if ((device_parse(p, &p, &dev) != 0) || (!dev))
1668                         break;
1669
1670                 debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
1671                                 dev->id->num, dev->id->mtd_id);
1672
1673                 /* check if parsed device is already on the list */
1674                 if (device_find(dev->id->type, dev->id->num) != NULL) {
1675                         printf("device %s%d redefined, please correct mtdparts variable\n",
1676                                         MTD_DEV_TYPE(dev->id->type), dev->id->num);
1677                         break;
1678                 }
1679
1680                 list_add_tail(&dev->link, &devices);
1681                 err = 0;
1682         }
1683         if (err == 1) {
1684                 device_delall(&devices);
1685                 return 1;
1686         }
1687
1688         return 0;
1689 }
1690
1691 /**
1692  * Parse provided string describing mtdids mapping (see file header for mtdids
1693  * variable format). Allocate memory for each entry and add all found entries
1694  * to the global mtdids list.
1695  *
1696  * @param ids mapping string
1697  * @return 0 on success, 1 otherwise
1698  */
1699 static int parse_mtdids(const char *const ids)
1700 {
1701         const char *p = ids;
1702         const char *mtd_id;
1703         int mtd_id_len;
1704         struct mtdids *id;
1705         struct list_head *entry, *n;
1706         struct mtdids *id_tmp;
1707         u8 type, num;
1708         u32 size;
1709         int ret = 1;
1710
1711         debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
1712
1713         /* clean global mtdids list */
1714         list_for_each_safe(entry, n, &mtdids) {
1715                 id_tmp = list_entry(entry, struct mtdids, link);
1716                 debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
1717                 list_del(entry);
1718                 free(id_tmp);
1719         }
1720         last_ids[0] = '\0';
1721         INIT_LIST_HEAD(&mtdids);
1722
1723         while(p && (*p != '\0')) {
1724
1725                 ret = 1;
1726                 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1727                 if (mtd_id_parse(p, &p, &type, &num) != 0)
1728                         break;
1729
1730                 if (*p != '=') {
1731                         printf("mtdids: incorrect <dev-num>\n");
1732                         break;
1733                 }
1734                 p++;
1735
1736                 /* check if requested device exists */
1737                 if (mtd_device_validate(type, num, &size) != 0)
1738                         return 1;
1739
1740                 /* locate <mtd-id> */
1741                 mtd_id = p;
1742                 if ((p = strchr(mtd_id, ',')) != NULL) {
1743                         mtd_id_len = p - mtd_id + 1;
1744                         p++;
1745                 } else {
1746                         mtd_id_len = strlen(mtd_id) + 1;
1747                 }
1748                 if (mtd_id_len == 0) {
1749                         printf("mtdids: no <mtd-id> identifier\n");
1750                         break;
1751                 }
1752
1753                 /* check if this id is already on the list */
1754                 int double_entry = 0;
1755                 list_for_each(entry, &mtdids) {
1756                         id_tmp = list_entry(entry, struct mtdids, link);
1757                         if ((id_tmp->type == type) && (id_tmp->num == num)) {
1758                                 double_entry = 1;
1759                                 break;
1760                         }
1761                 }
1762                 if (double_entry) {
1763                         printf("device id %s%d redefined, please correct mtdids variable\n",
1764                                         MTD_DEV_TYPE(type), num);
1765                         break;
1766                 }
1767
1768                 /* allocate mtdids structure */
1769                 if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
1770                         printf("out of memory\n");
1771                         break;
1772                 }
1773                 memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
1774                 id->num = num;
1775                 id->type = type;
1776                 id->size = size;
1777                 id->mtd_id = (char *)(id + 1);
1778                 strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
1779                 id->mtd_id[mtd_id_len - 1] = '\0';
1780                 INIT_LIST_HEAD(&id->link);
1781
1782                 debug("+ id %s%d\t%16d bytes\t%s\n",
1783                                 MTD_DEV_TYPE(id->type), id->num,
1784                                 id->size, id->mtd_id);
1785
1786                 list_add_tail(&id->link, &mtdids);
1787                 ret = 0;
1788         }
1789         if (ret == 1) {
1790                 /* clean mtdids list and free allocated memory */
1791                 list_for_each_safe(entry, n, &mtdids) {
1792                         id_tmp = list_entry(entry, struct mtdids, link);
1793                         list_del(entry);
1794                         free(id_tmp);
1795                 }
1796                 return 1;
1797         }
1798
1799         return 0;
1800 }
1801
1802 /**
1803  * Parse and initialize global mtdids mapping and create global
1804  * device/partition list.
1805  *
1806  * @return 0 on success, 1 otherwise
1807  */
1808 int mtdparts_init(void)
1809 {
1810         static int initialized = 0;
1811         const char *ids, *parts;
1812         const char *current_partition;
1813         int ids_changed;
1814         char tmp_ep[PARTITION_MAXLEN+1];
1815
1816         debug("\n---mtdparts_init---\n");
1817         if (!initialized) {
1818                 INIT_LIST_HEAD(&mtdids);
1819                 INIT_LIST_HEAD(&devices);
1820                 memset(last_ids, 0, MTDIDS_MAXLEN);
1821                 memset(last_parts, 0, MTDPARTS_MAXLEN);
1822                 memset(last_partition, 0, PARTITION_MAXLEN);
1823                 initialized = 1;
1824         }
1825
1826         /* get variables */
1827         ids = getenv("mtdids");
1828         parts = getenv("mtdparts");
1829         current_partition = getenv("partition");
1830
1831         /* save it for later parsing, cannot rely on current partition pointer
1832          * as 'partition' variable may be updated during init */
1833         tmp_ep[0] = '\0';
1834         if (current_partition)
1835                 strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
1836
1837         debug("last_ids  : %s\n", last_ids);
1838         debug("env_ids   : %s\n", ids);
1839         debug("last_parts: %s\n", last_parts);
1840         debug("env_parts : %s\n\n", parts);
1841
1842         debug("last_partition : %s\n", last_partition);
1843         debug("env_partition  : %s\n", current_partition);
1844
1845         /* if mtdids varible is empty try to use defaults */
1846         if (!ids) {
1847                 if (mtdids_default) {
1848                         debug("mtdids variable not defined, using default\n");
1849                         ids = mtdids_default;
1850                         setenv("mtdids", (char *)ids);
1851                 } else {
1852                         printf("mtdids not defined, no default present\n");
1853                         return 1;
1854                 }
1855         }
1856         if (strlen(ids) > MTDIDS_MAXLEN - 1) {
1857                 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
1858                 return 1;
1859         }
1860
1861         /* do no try to use defaults when mtdparts variable is not defined,
1862          * just check the length */
1863         if (!parts)
1864                 printf("mtdparts variable not set, see 'help mtdparts'\n");
1865
1866         if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
1867                 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
1868                 return 1;
1869         }
1870
1871         /* check if we have already parsed those mtdids */
1872         if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
1873                 ids_changed = 0;
1874         } else {
1875                 ids_changed = 1;
1876
1877                 if (parse_mtdids(ids) != 0) {
1878                         mtd_devices_init();
1879                         return 1;
1880                 }
1881
1882                 /* ok it's good, save new ids */
1883                 strncpy(last_ids, ids, MTDIDS_MAXLEN);
1884         }
1885
1886         /* parse partitions if either mtdparts or mtdids were updated */
1887         if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
1888                 if (parse_mtdparts(parts) != 0)
1889                         return 1;
1890
1891                 if (list_empty(&devices)) {
1892                         printf("mtdparts_init: no valid partitions\n");
1893                         return 1;
1894                 }
1895
1896                 /* ok it's good, save new parts */
1897                 strncpy(last_parts, parts, MTDPARTS_MAXLEN);
1898
1899                 /* reset first partition from first dev from the list as current */
1900                 current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
1901                 current_mtd_partnum = 0;
1902                 current_save();
1903
1904                 debug("mtdparts_init: current_mtd_dev  = %s%d, current_mtd_partnum = %d\n",
1905                                 MTD_DEV_TYPE(current_mtd_dev->id->type),
1906                                 current_mtd_dev->id->num, current_mtd_partnum);
1907         }
1908
1909         /* mtdparts variable was reset to NULL, delete all devices/partitions */
1910         if (!parts && (last_parts[0] != '\0'))
1911                 return mtd_devices_init();
1912
1913         /* do not process current partition if mtdparts variable is null */
1914         if (!parts)
1915                 return 0;
1916
1917         /* is current partition set in environment? if so, use it */
1918         if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
1919                 struct part_info *p;
1920                 struct mtd_device *cdev;
1921                 u8 pnum;
1922
1923                 debug("--- getting current partition: %s\n", tmp_ep);
1924
1925                 if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
1926                         current_mtd_dev = cdev;
1927                         current_mtd_partnum = pnum;
1928                         current_save();
1929                 }
1930         } else if (getenv("partition") == NULL) {
1931                 debug("no partition variable set, setting...\n");
1932                 current_save();
1933         }
1934
1935         return 0;
1936 }
1937
1938 /**
1939  * Return pointer to the partition of a requested number from a requested
1940  * device.
1941  *
1942  * @param dev device that is to be searched for a partition
1943  * @param part_num requested partition number
1944  * @return pointer to the part_info, NULL otherwise
1945  */
1946 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
1947 {
1948         struct list_head *entry;
1949         struct part_info *part;
1950         int num;
1951
1952         if (!dev)
1953                 return NULL;
1954
1955         debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
1956                         part_num, MTD_DEV_TYPE(dev->id->type),
1957                         dev->id->num, dev->id->mtd_id);
1958
1959         if (part_num >= dev->num_parts) {
1960                 printf("invalid partition number %d for device %s%d (%s)\n",
1961                                 part_num, MTD_DEV_TYPE(dev->id->type),
1962                                 dev->id->num, dev->id->mtd_id);
1963                 return NULL;
1964         }
1965
1966         /* locate partition number, return it */
1967         num = 0;
1968         list_for_each(entry, &dev->parts) {
1969                 part = list_entry(entry, struct part_info, link);
1970
1971                 if (part_num == num++) {
1972                         return part;
1973                 }
1974         }
1975
1976         return NULL;
1977 }
1978
1979 /***************************************************/
1980 /* U-boot commands                                 */
1981 /***************************************************/
1982 /* command line only */
1983 #ifndef CONFIG_CMD_MTDPARTS_LITE
1984 /**
1985  * Routine implementing u-boot chpart command. Sets new current partition based
1986  * on the user supplied partition id. For partition id format see find_dev_and_part().
1987  *
1988  * @param cmdtp command internal data
1989  * @param flag command flag
1990  * @param argc number of arguments supplied to the command
1991  * @param argv arguments list
1992  * @return 0 on success, 1 otherwise
1993  */
1994 int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1995 {
1996 /* command line only */
1997         struct mtd_device *dev;
1998         struct part_info *part;
1999         u8 pnum;
2000
2001         if (mtdparts_init() !=0)
2002                 return 1;
2003
2004         if (argc < 2) {
2005                 printf("no partition id specified\n");
2006                 return 1;
2007         }
2008
2009         if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
2010                 return 1;
2011
2012         current_mtd_dev = dev;
2013         current_mtd_partnum = pnum;
2014         current_save();
2015
2016         printf("partition changed to %s%d,%d\n",
2017                         MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
2018
2019         return 0;
2020 }
2021 #endif
2022
2023 /**
2024  * Routine implementing u-boot mtdparts command. Initialize/update default global
2025  * partition list and process user partition request (list, add, del).
2026  *
2027  * @param cmdtp command internal data
2028  * @param flag command flag
2029  * @param argc number of arguments supplied to the command
2030  * @param argv arguments list
2031  * @return 0 on success, 1 otherwise
2032  */
2033 int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
2034 {
2035 #ifndef CONFIG_CMD_MTDPARTS_LITE
2036         if (argc == 2) {
2037                 if (strcmp(argv[1], "default") == 0) {
2038                         setenv("mtdids", (char *)mtdids_default);
2039                         setenv("mtdparts", (char *)mtdparts_default);
2040                         setenv("partition", NULL);
2041
2042                         mtdparts_init();
2043                         return 0;
2044                 } else if (strcmp(argv[1], "delall") == 0) {
2045                         /* this may be the first run, initialize lists if needed */
2046                         mtdparts_init();
2047
2048                         setenv("mtdparts", NULL);
2049
2050                         /* mtd_devices_init() calls current_save() */
2051                         return mtd_devices_init();
2052                 }
2053         }
2054 #endif
2055
2056         /* make sure we are in sync with env variables */
2057         if (mtdparts_init() != 0)
2058                 return 1;
2059
2060         if (argc == 1) {
2061                 list_partitions();
2062                 return 0;
2063         }
2064
2065 #ifndef CONFIG_CMD_MTDPARTS_LITE
2066         /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2067         if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
2068 #define PART_ADD_DESC_MAXLEN 64
2069                 char tmpbuf[PART_ADD_DESC_MAXLEN];
2070 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2071                 struct mtd_info *mtd;
2072                 uint64_t next_offset;
2073 #endif
2074                 u8 type, num, len;
2075                 struct mtd_device *dev;
2076                 struct mtd_device *dev_tmp;
2077                 struct mtdids *id;
2078                 struct part_info *p;
2079
2080                 if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
2081                         return 1;
2082
2083                 if ((id = id_find(type, num)) == NULL) {
2084                         printf("no such device %s defined in mtdids variable\n", argv[2]);
2085                         return 1;
2086                 }
2087
2088                 len = strlen(id->mtd_id) + 1;   /* 'mtd_id:' */
2089                 len += strlen(argv[3]);         /* size@offset */
2090                 len += strlen(argv[4]) + 2;     /* '(' name ')' */
2091                 if (argv[5] && (strlen(argv[5]) == 2))
2092                         len += 2;               /* 'ro' */
2093
2094                 if (len >= PART_ADD_DESC_MAXLEN) {
2095                         printf("too long partition description\n");
2096                         return 1;
2097                 }
2098                 sprintf(tmpbuf, "%s:%s(%s)%s",
2099                                 id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
2100                 debug("add tmpbuf: %s\n", tmpbuf);
2101
2102                 if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
2103                         return 1;
2104
2105                 debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
2106                                 dev->id->num, dev->id->mtd_id);
2107
2108                 p = list_entry(dev->parts.next, struct part_info, link);
2109
2110 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2111                 if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
2112                         return 1;
2113
2114                 if (!strcmp(&argv[1][3], ".spread")) {
2115                         spread_partition(mtd, p, &next_offset);
2116                         debug("increased %s to %d bytes\n", p->name, p->size);
2117                 }
2118 #endif
2119
2120                 dev_tmp = device_find(dev->id->type, dev->id->num);
2121                 if (dev_tmp == NULL) {
2122                         device_add(dev);
2123                 } else if (part_add(dev_tmp, p) != 0) {
2124                         /* merge new partition with existing ones*/
2125                         device_del(dev);
2126                         return 1;
2127                 }
2128
2129                 if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
2130                         printf("generated mtdparts too long, reseting to null\n");
2131                         return 1;
2132                 }
2133
2134                 return 0;
2135         }
2136
2137         /* mtdparts del part-id */
2138         if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
2139                 debug("del: part-id = %s\n", argv[2]);
2140
2141                 return delete_partition(argv[2]);
2142         }
2143 #endif
2144
2145 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2146         if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
2147                 return spread_partitions();
2148 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2149
2150 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2151         if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
2152                 return spread_partitions();
2153 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2154
2155         return cmd_usage(cmdtp);
2156 }
2157
2158 /***************************************************/
2159 #ifndef CONFIG_CMD_MTDPARTS_LITE
2160 U_BOOT_CMD(
2161         chpart, 2,      0,      do_chpart,
2162         "change active partition",
2163         "part-id\n"
2164         "    - change active partition (e.g. part-id = nand0,1)"
2165 );
2166
2167 U_BOOT_CMD(
2168         mtdparts,       6,      0,      do_mtdparts,
2169         "define flash/nand partitions",
2170         "\n"
2171         "    - list partition table\n"
2172         "mtdparts delall\n"
2173         "    - delete all partitions\n"
2174         "mtdparts del part-id\n"
2175         "    - delete partition (e.g. part-id = nand0,1)\n"
2176         "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2177         "    - add partition\n"
2178 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2179         "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2180         "    - add partition, padding size by skipping bad blocks\n"
2181 #endif
2182         "mtdparts default\n"
2183         "    - reset partition table to defaults\n"
2184 #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
2185         "mtdparts spread\n"
2186         "    - adjust the sizes of the partitions so they are\n"
2187         "      at least as big as the mtdparts variable specifies\n"
2188         "      and they each start on a good block\n\n"
2189 #else
2190         "\n"
2191 #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
2192         "-----\n\n"
2193         "this command uses three environment variables:\n\n"
2194         "'partition' - keeps current partition identifier\n\n"
2195         "partition  := <part-id>\n"
2196         "<part-id>  := <dev-id>,part_num\n\n"
2197         "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2198         "mtdids=<idmap>[,<idmap>,...]\n\n"
2199         "<idmap>    := <dev-id>=<mtd-id>\n"
2200         "<dev-id>   := 'nand'|'nor'|'onenand'<dev-num>\n"
2201         "<dev-num>  := mtd device number, 0...\n"
2202         "<mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2203         "'mtdparts' - partition list\n\n"
2204         "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2205         "<mtd-def>  := <mtd-id>:<part-def>[,<part-def>...]\n"
2206         "<mtd-id>   := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2207         "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2208         "<size>     := standard linux memsize OR '-' to denote all remaining space\n"
2209         "<offset>   := partition start offset within the device\n"
2210         "<name>     := '(' NAME ')'\n"
2211         "<ro-flag>  := when set to 'ro' makes partition read-only (not used, passed to kernel)"
2212 );
2213 #else
2214 U_BOOT_CMD(
2215         mtdparts,       1,      0,      do_mtdparts,
2216         "define flash/nand partitions",
2217         "\n"
2218         "    - list partition table\n"
2219 );
2220 #endif
2221 /***************************************************/