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