3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
9 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
12 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 * Added support for reading flash partition table from environment.
15 * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
18 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
19 * Copyright 2002 SYSGO Real-Time Solutions GmbH
21 * See file CREDITS for list of people who contributed to this
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2 of
27 * the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
41 * Three environment variables are used by the parsing routines:
43 * 'partition' - keeps current partition identifier
45 * partition := <part-id>
46 * <part-id> := <dev-id>,part_num
49 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
51 * mtdids=<idmap>[,<idmap>,...]
53 * <idmap> := <dev-id>=<mtd-id>
54 * <dev-id> := 'nand'|'nor'<dev-num>
55 * <dev-num> := mtd device number, 0...
56 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
59 * 'mtdparts' - partition list
61 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
63 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
64 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
65 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
66 * <size> := standard linux memsize OR '-' to denote all remaining space
67 * <offset> := partition start offset within the device
68 * <name> := '(' NAME ')'
69 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
72 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
73 * - if the above variables are not set defaults for a given target are used
77 * 1 NOR Flash, with 1 single writable partition:
78 * mtdids=nor0=edb7312-nor
79 * mtdparts=mtdparts=edb7312-nor:-
81 * 1 NOR Flash with 2 partitions, 1 NAND with one
82 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
83 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
88 * JFFS2/CRAMFS support
93 #include <jffs2/jffs2.h>
94 #include <linux/list.h>
95 #include <linux/ctype.h>
96 #include <cramfs/cramfs_fs.h>
98 #if defined(CONFIG_CMD_NAND)
99 #ifdef CFG_NAND_LEGACY
100 #include <linux/mtd/nand_legacy.h>
101 #else /* !CFG_NAND_LEGACY */
102 #include <linux/mtd/nand.h>
104 #endif /* !CFG_NAND_LEGACY */
106 /* enable/disable debugging messages */
111 # define DEBUGF(fmt, args...) printf(fmt ,##args)
113 # define DEBUGF(fmt, args...)
116 /* special size referring to all the remaining space in a partition */
117 #define SIZE_REMAINING 0xFFFFFFFF
119 /* special offset value, it is used when not provided by user
121 * this value is used temporarily during parsing, later such offests
122 * are recalculated */
123 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
125 /* minimum partition size */
126 #define MIN_PART_SIZE 4096
128 /* this flag needs to be set in part_info struct mask_flags
129 * field for read-only partitions */
130 #define MTD_WRITEABLE_CMD 1
132 #ifdef CONFIG_JFFS2_CMDLINE
133 /* default values for mtdids and mtdparts variables */
134 #if defined(MTDIDS_DEFAULT)
135 static const char *const mtdids_default
= MTDIDS_DEFAULT
;
137 #warning "MTDIDS_DEFAULT not defined!"
138 static const char *const mtdids_default
= NULL
;
141 #if defined(MTDPARTS_DEFAULT)
142 static const char *const mtdparts_default
= MTDPARTS_DEFAULT
;
144 #warning "MTDPARTS_DEFAULT not defined!"
145 static const char *const mtdparts_default
= NULL
;
148 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
149 #define MTDIDS_MAXLEN 128
150 #define MTDPARTS_MAXLEN 512
151 #define PARTITION_MAXLEN 16
152 static char last_ids
[MTDIDS_MAXLEN
];
153 static char last_parts
[MTDPARTS_MAXLEN
];
154 static char last_partition
[PARTITION_MAXLEN
];
156 /* low level jffs2 cache cleaning routine */
157 extern void jffs2_free_cache(struct part_info
*part
);
159 /* mtdids mapping list, filled by parse_ids() */
160 struct list_head mtdids
;
162 /* device/partition list, parse_cmdline() parses into here */
163 struct list_head devices
;
164 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
166 /* current active device and partition number */
167 static struct mtd_device
*current_dev
= NULL
;
168 static u8 current_partnum
= 0;
170 extern int cramfs_check (struct part_info
*info
);
171 extern int cramfs_load (char *loadoffset
, struct part_info
*info
, char *filename
);
172 extern int cramfs_ls (struct part_info
*info
, char *filename
);
173 extern int cramfs_info (struct part_info
*info
);
175 static struct part_info
* jffs2_part_info(struct mtd_device
*dev
, unsigned int part_num
);
177 /* command line only routines */
178 #ifdef CONFIG_JFFS2_CMDLINE
180 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
);
181 static int device_del(struct mtd_device
*dev
);
184 * Parses a string into a number. The number stored at ptr is
185 * potentially suffixed with K (for kilobytes, or 1024 bytes),
186 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
187 * 1073741824). If the number is suffixed with K, M, or G, then
188 * the return value is the number multiplied by one kilobyte, one
189 * megabyte, or one gigabyte, respectively.
191 * @param ptr where parse begins
192 * @param retptr output pointer to next char after parse completes (output)
193 * @return resulting unsigned int
195 static unsigned long memsize_parse (const char *const ptr
, const char **retptr
)
197 unsigned long ret
= simple_strtoul(ptr
, (char **)retptr
, 0);
218 * Format string describing supplied size. This routine does the opposite job
219 * to memsize_parse(). Size in bytes is converted to string and if possible
220 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
222 * Note, that this routine does not check for buffer overflow, it's the caller
223 * who must assure enough space.
225 * @param buf output buffer
226 * @param size size to be converted to string
228 static void memsize_format(char *buf
, u32 size
)
230 #define SIZE_GB ((u32)1024*1024*1024)
231 #define SIZE_MB ((u32)1024*1024)
232 #define SIZE_KB ((u32)1024)
234 if ((size
% SIZE_GB
) == 0)
235 sprintf(buf
, "%lug", size
/SIZE_GB
);
236 else if ((size
% SIZE_MB
) == 0)
237 sprintf(buf
, "%lum", size
/SIZE_MB
);
238 else if (size
% SIZE_KB
== 0)
239 sprintf(buf
, "%luk", size
/SIZE_KB
);
241 sprintf(buf
, "%lu", size
);
245 * This routine does global indexing of all partitions. Resulting index for
246 * current partition is saved in 'mtddevnum'. Current partition name in
249 static void index_partitions(void)
253 struct part_info
*part
;
254 struct list_head
*dentry
;
255 struct mtd_device
*dev
;
257 DEBUGF("--- index partitions ---\n");
261 list_for_each(dentry
, &devices
) {
262 dev
= list_entry(dentry
, struct mtd_device
, link
);
263 if (dev
== current_dev
) {
264 mtddevnum
+= current_partnum
;
265 sprintf(buf
, "%d", mtddevnum
);
266 setenv("mtddevnum", buf
);
269 mtddevnum
+= dev
->num_parts
;
272 part
= jffs2_part_info(current_dev
, current_partnum
);
273 setenv("mtddevname", part
->name
);
275 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum
, part
->name
);
277 setenv("mtddevnum", NULL
);
278 setenv("mtddevname", NULL
);
280 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
285 * Save current device and partition in environment variable 'partition'.
287 static void current_save(void)
291 DEBUGF("--- current_save ---\n");
294 sprintf(buf
, "%s%d,%d", MTD_DEV_TYPE(current_dev
->id
->type
),
295 current_dev
->id
->num
, current_partnum
);
297 setenv("partition", buf
);
298 strncpy(last_partition
, buf
, 16);
300 DEBUGF("=> partition %s\n", buf
);
302 setenv("partition", NULL
);
303 last_partition
[0] = '\0';
305 DEBUGF("=> partition NULL\n");
311 * Performs sanity check for supplied NOR flash partition. Table of existing
312 * NOR flash devices is searched and partition device is located. Alignment
313 * with the granularity of NOR flash sectors is verified.
315 * @param id of the parent device
316 * @param part partition to validate
317 * @return 0 if partition is valid, 1 otherwise
319 static int part_validate_nor(struct mtdids
*id
, struct part_info
*part
)
321 #if defined(CONFIG_CMD_FLASH)
322 /* info for FLASH chips */
323 extern flash_info_t flash_info
[];
329 flash
= &flash_info
[id
->num
];
332 for (i
= 0; i
< flash
->sector_count
; i
++) {
333 if ((flash
->start
[i
] - flash
->start
[0]) == part
->offset
) {
338 if (offset_aligned
== 0) {
339 printf("%s%d: partition (%s) start offset alignment incorrect\n",
340 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
344 end_offset
= part
->offset
+ part
->size
;
345 for (i
= 0; i
< flash
->sector_count
; i
++) {
346 if ((flash
->start
[i
] - flash
->start
[0]) == end_offset
)
350 if (flash
->size
== end_offset
)
353 printf("%s%d: partition (%s) size alignment incorrect\n",
354 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
360 * Performs sanity check for supplied NAND flash partition. Table of existing
361 * NAND flash devices is searched and partition device is located. Alignment
362 * with the granularity of nand erasesize is verified.
364 * @param id of the parent device
365 * @param part partition to validate
366 * @return 0 if partition is valid, 1 otherwise
368 static int part_validate_nand(struct mtdids
*id
, struct part_info
*part
)
370 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
371 /* info for NAND chips */
374 nand
= &nand_info
[id
->num
];
376 if ((unsigned long)(part
->offset
) % nand
->erasesize
) {
377 printf("%s%d: partition (%s) start offset alignment incorrect\n",
378 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
382 if (part
->size
% nand
->erasesize
) {
383 printf("%s%d: partition (%s) size alignment incorrect\n",
384 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
395 * Performs sanity check for supplied partition. Offset and size are verified
396 * to be within valid range. Partition type is checked and either
397 * parts_validate_nor() or parts_validate_nand() is called with the argument
400 * @param id of the parent device
401 * @param part partition to validate
402 * @return 0 if partition is valid, 1 otherwise
404 static int part_validate(struct mtdids
*id
, struct part_info
*part
)
406 if (part
->size
== SIZE_REMAINING
)
407 part
->size
= id
->size
- part
->offset
;
409 if (part
->offset
> id
->size
) {
410 printf("%s: offset %08lx beyond flash size %08lx\n",
411 id
->mtd_id
, part
->offset
, id
->size
);
415 if ((part
->offset
+ part
->size
) <= part
->offset
) {
416 printf("%s%d: partition (%s) size too big\n",
417 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
421 if (part
->offset
+ part
->size
> id
->size
) {
422 printf("%s: partitioning exceeds flash size\n", id
->mtd_id
);
426 if (id
->type
== MTD_DEV_TYPE_NAND
)
427 return part_validate_nand(id
, part
);
428 else if (id
->type
== MTD_DEV_TYPE_NOR
)
429 return part_validate_nor(id
, part
);
431 DEBUGF("part_validate: invalid dev type\n");
437 * Delete selected partition from the partion list of the specified device.
439 * @param dev device to delete partition from
440 * @param part partition to delete
441 * @return 0 on success, 1 otherwise
443 static int part_del(struct mtd_device
*dev
, struct part_info
*part
)
445 u8 current_save_needed
= 0;
447 /* if there is only one partition, remove whole device */
448 if (dev
->num_parts
== 1)
449 return device_del(dev
);
451 /* otherwise just delete this partition */
453 if (dev
== current_dev
) {
454 /* we are modyfing partitions for the current device,
456 struct part_info
*curr_pi
;
457 curr_pi
= jffs2_part_info(current_dev
, current_partnum
);
460 if (curr_pi
== part
) {
461 printf("current partition deleted, resetting current to 0\n");
463 } else if (part
->offset
<= curr_pi
->offset
) {
466 current_save_needed
= 1;
470 #ifdef CFG_NAND_LEGACY
471 jffs2_free_cache(part
);
473 list_del(&part
->link
);
477 if (current_save_needed
> 0)
486 * Delete all partitions from parts head list, free memory.
488 * @param head list of partitions to delete
490 static void part_delall(struct list_head
*head
)
492 struct list_head
*entry
, *n
;
493 struct part_info
*part_tmp
;
495 /* clean tmp_list and free allocated memory */
496 list_for_each_safe(entry
, n
, head
) {
497 part_tmp
= list_entry(entry
, struct part_info
, link
);
499 #ifdef CFG_NAND_LEGACY
500 jffs2_free_cache(part_tmp
);
508 * Add new partition to the supplied partition list. Make sure partitions are
509 * sorted by offset in ascending order.
511 * @param head list this partition is to be added to
512 * @param new partition to be added
514 static int part_sort_add(struct mtd_device
*dev
, struct part_info
*part
)
516 struct list_head
*entry
;
517 struct part_info
*new_pi
, *curr_pi
;
519 /* link partition to parrent dev */
522 if (list_empty(&dev
->parts
)) {
523 DEBUGF("part_sort_add: list empty\n");
524 list_add(&part
->link
, &dev
->parts
);
530 new_pi
= list_entry(&part
->link
, struct part_info
, link
);
532 /* get current partition info if we are updating current device */
534 if (dev
== current_dev
)
535 curr_pi
= jffs2_part_info(current_dev
, current_partnum
);
537 list_for_each(entry
, &dev
->parts
) {
538 struct part_info
*pi
;
540 pi
= list_entry(entry
, struct part_info
, link
);
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");
548 if (new_pi
->offset
<= pi
->offset
) {
549 list_add_tail(&part
->link
, entry
);
552 if (curr_pi
&& (pi
->offset
<= curr_pi
->offset
)) {
553 /* we are modyfing partitions for the current
554 * device, update current */
564 list_add_tail(&part
->link
, &dev
->parts
);
571 * Add provided partition to the partition list of a given device.
573 * @param dev device to which partition is added
574 * @param part partition to be added
575 * @return 0 on success, 1 otherwise
577 static int part_add(struct mtd_device
*dev
, struct part_info
*part
)
579 /* verify alignment and size */
580 if (part_validate(dev
->id
, part
) != 0)
583 /* partition is ok, add it to the list */
584 if (part_sort_add(dev
, part
) != 0)
591 * Parse one partition definition, allocate memory and return pointer to this
592 * location in retpart.
594 * @param partdef pointer to the partition definition string i.e. <part-def>
595 * @param ret output pointer to next char after parse completes (output)
596 * @param retpart pointer to the allocated partition (output)
597 * @return 0 on success, 1 otherwise
599 static int part_parse(const char *const partdef
, const char **ret
, struct part_info
**retpart
)
601 struct part_info
*part
;
603 unsigned long offset
;
606 unsigned int mask_flags
;
613 /* fetch the partition size */
615 /* assign all remaining space to this partition */
616 DEBUGF("'-': remaining size assigned\n");
617 size
= SIZE_REMAINING
;
620 size
= memsize_parse(p
, &p
);
621 if (size
< MIN_PART_SIZE
) {
622 printf("partition size too small (%lx)\n", size
);
627 /* check for offset */
628 offset
= OFFSET_NOT_SPECIFIED
;
631 offset
= memsize_parse(p
, &p
);
634 /* now look for the name */
637 if ((p
= strchr(name
, ')')) == NULL
) {
638 printf("no closing ) found in partition name\n");
641 name_len
= p
- name
+ 1;
642 if ((name_len
- 1) == 0) {
643 printf("empty partition name\n");
648 /* 0x00000000@0x00000000 */
653 /* test for options */
655 if (strncmp(p
, "ro", 2) == 0) {
656 mask_flags
|= MTD_WRITEABLE_CMD
;
660 /* check for next partition definition */
662 if (size
== SIZE_REMAINING
) {
664 printf("no partitions allowed after a fill-up partition\n");
668 } else if ((*p
== ';') || (*p
== '\0')) {
671 printf("unexpected character '%c' at the end of partition\n", *p
);
676 /* allocate memory */
677 part
= (struct part_info
*)malloc(sizeof(struct part_info
) + name_len
);
679 printf("out of memory\n");
682 memset(part
, 0, sizeof(struct part_info
) + name_len
);
684 part
->offset
= offset
;
685 part
->mask_flags
= mask_flags
;
686 part
->name
= (char *)(part
+ 1);
689 /* copy user provided name */
690 strncpy(part
->name
, name
, name_len
- 1);
693 /* auto generated name in form of size@offset */
694 sprintf(part
->name
, "0x%08lx@0x%08lx", size
, offset
);
698 part
->name
[name_len
- 1] = '\0';
699 INIT_LIST_HEAD(&part
->link
);
701 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
702 part
->name
, part
->size
,
703 part
->offset
, part
->mask_flags
);
708 #endif/* #ifdef CONFIG_JFFS2_CMDLINE */
711 * Check device number to be within valid range for given device type.
713 * @param dev device to validate
714 * @return 0 if device is valid, 1 otherwise
716 static int device_validate(u8 type
, u8 num
, u32
*size
)
718 if (type
== MTD_DEV_TYPE_NOR
) {
719 #if defined(CONFIG_CMD_FLASH)
720 if (num
< CFG_MAX_FLASH_BANKS
) {
721 extern flash_info_t flash_info
[];
722 *size
= flash_info
[num
].size
;
727 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
728 MTD_DEV_TYPE(type
), num
, CFG_MAX_FLASH_BANKS
- 1);
730 printf("support for FLASH devices not present\n");
732 } else if (type
== MTD_DEV_TYPE_NAND
) {
733 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
734 if (num
< CFG_MAX_NAND_DEVICE
) {
735 #ifndef CFG_NAND_LEGACY
736 *size
= nand_info
[num
].size
;
738 extern struct nand_chip nand_dev_desc
[CFG_MAX_NAND_DEVICE
];
739 *size
= nand_dev_desc
[num
].totlen
;
744 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
745 MTD_DEV_TYPE(type
), num
, CFG_MAX_NAND_DEVICE
- 1);
747 printf("support for NAND devices not present\n");
754 #ifdef CONFIG_JFFS2_CMDLINE
756 * Delete all mtd devices from a supplied devices list, free memory allocated for
757 * each device and delete all device partitions.
759 * @return 0 on success, 1 otherwise
761 static int device_delall(struct list_head
*head
)
763 struct list_head
*entry
, *n
;
764 struct mtd_device
*dev_tmp
;
766 /* clean devices list */
767 list_for_each_safe(entry
, n
, head
) {
768 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
770 part_delall(&dev_tmp
->parts
);
773 INIT_LIST_HEAD(&devices
);
779 * If provided device exists it's partitions are deleted, device is removed
780 * from device list and device memory is freed.
782 * @param dev device to be deleted
783 * @return 0 on success, 1 otherwise
785 static int device_del(struct mtd_device
*dev
)
787 part_delall(&dev
->parts
);
788 list_del(&dev
->link
);
791 if (dev
== current_dev
) {
792 /* we just deleted current device */
793 if (list_empty(&devices
)) {
796 /* reset first partition from first dev from the
797 * devices list as current */
798 current_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
810 * Search global device list and return pointer to the device of type and num
813 * @param type device type
814 * @param num device number
815 * @return NULL if requested device does not exist
817 static struct mtd_device
* device_find(u8 type
, u8 num
)
819 struct list_head
*entry
;
820 struct mtd_device
*dev_tmp
;
822 list_for_each(entry
, &devices
) {
823 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
825 if ((dev_tmp
->id
->type
== type
) && (dev_tmp
->id
->num
== num
))
833 * Add specified device to the global device list.
835 * @param dev device to be added
837 static void device_add(struct mtd_device
*dev
)
839 u8 current_save_needed
= 0;
841 if (list_empty(&devices
)) {
844 current_save_needed
= 1;
847 list_add_tail(&dev
->link
, &devices
);
849 if (current_save_needed
> 0)
856 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
857 * return pointer to the device structure.
859 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
860 * @param ret output pointer to next char after parse completes (output)
861 * @param retdev pointer to the allocated device (output)
862 * @return 0 on success, 1 otherwise
864 static int device_parse(const char *const mtd_dev
, const char **ret
, struct mtd_device
**retdev
)
866 struct mtd_device
*dev
;
867 struct part_info
*part
;
870 unsigned int mtd_id_len
;
871 const char *p
, *pend
;
873 struct list_head
*entry
, *n
;
882 DEBUGF("===device_parse===\n");
886 if (!(p
= strchr(mtd_id
, ':'))) {
887 printf("no <mtd-id> identifier\n");
890 mtd_id_len
= p
- mtd_id
+ 1;
893 /* verify if we have a valid device specified */
894 if ((id
= id_find_by_mtd_id(mtd_id
, mtd_id_len
- 1)) == NULL
) {
895 printf("invalid mtd device '%.*s'\n", mtd_id_len
- 1, mtd_id
);
899 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
900 id
->type
, MTD_DEV_TYPE(id
->type
),
901 id
->num
, id
->mtd_id
);
902 pend
= strchr(p
, ';');
903 DEBUGF("parsing partitions %.*s\n", (pend
? pend
- p
: strlen(p
)), p
);
906 /* parse partitions */
910 if ((dev
= device_find(id
->type
, id
->num
)) != NULL
) {
911 /* if device already exists start at the end of the last partition */
912 part
= list_entry(dev
->parts
.prev
, struct part_info
, link
);
913 offset
= part
->offset
+ part
->size
;
916 while (p
&& (*p
!= '\0') && (*p
!= ';')) {
918 if ((part_parse(p
, &p
, &part
) != 0) || (!part
))
921 /* calculate offset when not specified */
922 if (part
->offset
== OFFSET_NOT_SPECIFIED
)
923 part
->offset
= offset
;
925 offset
= part
->offset
;
927 /* verify alignment and size */
928 if (part_validate(id
, part
) != 0)
931 offset
+= part
->size
;
933 /* partition is ok, add it to the list */
934 list_add_tail(&part
->link
, &tmp_list
);
939 part_delall(&tmp_list
);
943 if (num_parts
== 0) {
944 printf("no partitions for device %s%d (%s)\n",
945 MTD_DEV_TYPE(id
->type
), id
->num
, id
->mtd_id
);
949 DEBUGF("\ntotal partitions: %d\n", num_parts
);
951 /* check for next device presence */
955 } else if (*p
== '\0') {
958 printf("unexpected character '%c' at the end of device\n", *p
);
964 /* allocate memory for mtd_device structure */
965 if ((dev
= (struct mtd_device
*)malloc(sizeof(struct mtd_device
))) == NULL
) {
966 printf("out of memory\n");
969 memset(dev
, 0, sizeof(struct mtd_device
));
971 dev
->num_parts
= 0; /* part_sort_add increments num_parts */
972 INIT_LIST_HEAD(&dev
->parts
);
973 INIT_LIST_HEAD(&dev
->link
);
975 /* move partitions from tmp_list to dev->parts */
976 list_for_each_safe(entry
, n
, &tmp_list
) {
977 part
= list_entry(entry
, struct part_info
, link
);
979 if (part_sort_add(dev
, part
) != 0) {
992 * Initialize global device list.
994 * @return 0 on success, 1 otherwise
996 static int devices_init(void)
998 last_parts
[0] = '\0';
1002 return device_delall(&devices
);
1006 * Search global mtdids list and find id of requested type and number.
1008 * @return pointer to the id if it exists, NULL otherwise
1010 static struct mtdids
* id_find(u8 type
, u8 num
)
1012 struct list_head
*entry
;
1015 list_for_each(entry
, &mtdids
) {
1016 id
= list_entry(entry
, struct mtdids
, link
);
1018 if ((id
->type
== type
) && (id
->num
== num
))
1026 * Search global mtdids list and find id of a requested mtd_id.
1028 * Note: first argument is not null terminated.
1030 * @param mtd_id string containing requested mtd_id
1031 * @param mtd_id_len length of supplied mtd_id
1032 * @return pointer to the id if it exists, NULL otherwise
1034 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
)
1036 struct list_head
*entry
;
1039 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1040 mtd_id_len
, mtd_id
, mtd_id_len
);
1042 list_for_each(entry
, &mtdids
) {
1043 id
= list_entry(entry
, struct mtdids
, link
);
1045 DEBUGF("entry: '%s' (len = %d)\n",
1046 id
->mtd_id
, strlen(id
->mtd_id
));
1048 if (mtd_id_len
!= strlen(id
->mtd_id
))
1050 if (strncmp(id
->mtd_id
, mtd_id
, mtd_id_len
) == 0)
1056 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1059 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1062 * @param id string describing device id
1063 * @param ret_id output pointer to next char after parse completes (output)
1064 * @param dev_type parsed device type (output)
1065 * @param dev_num parsed device number (output)
1066 * @return 0 on success, 1 otherwise
1068 int id_parse(const char *id
, const char **ret_id
, u8
*dev_type
, u8
*dev_num
)
1073 if (strncmp(p
, "nand", 4) == 0) {
1074 *dev_type
= MTD_DEV_TYPE_NAND
;
1076 } else if (strncmp(p
, "nor", 3) == 0) {
1077 *dev_type
= MTD_DEV_TYPE_NOR
;
1080 printf("incorrect device type in %s\n", id
);
1085 printf("incorrect device number in %s\n", id
);
1089 *dev_num
= simple_strtoul(p
, (char **)&p
, 0);
1095 #ifdef CONFIG_JFFS2_CMDLINE
1097 * Process all devices and generate corresponding mtdparts string describing
1098 * all partitions on all devices.
1100 * @param buf output buffer holding generated mtdparts string (output)
1101 * @param buflen buffer size
1102 * @return 0 on success, 1 otherwise
1104 static int generate_mtdparts(char *buf
, u32 buflen
)
1106 struct list_head
*pentry
, *dentry
;
1107 struct mtd_device
*dev
;
1108 struct part_info
*part
, *prev_part
;
1111 u32 size
, offset
, len
, part_cnt
;
1112 u32 maxlen
= buflen
- 1;
1114 DEBUGF("--- generate_mtdparts ---\n");
1116 if (list_empty(&devices
)) {
1121 sprintf(p
, "mtdparts=");
1124 list_for_each(dentry
, &devices
) {
1125 dev
= list_entry(dentry
, struct mtd_device
, link
);
1128 len
= strlen(dev
->id
->mtd_id
) + 1;
1131 memcpy(p
, dev
->id
->mtd_id
, len
- 1);
1136 /* format partitions */
1139 list_for_each(pentry
, &dev
->parts
) {
1140 part
= list_entry(pentry
, struct part_info
, link
);
1142 offset
= part
->offset
;
1145 /* partition size */
1146 memsize_format(tmpbuf
, size
);
1147 len
= strlen(tmpbuf
);
1150 memcpy(p
, tmpbuf
, len
);
1155 /* add offset only when there is a gap between
1157 if ((!prev_part
&& (offset
!= 0)) ||
1158 (prev_part
&& ((prev_part
->offset
+ prev_part
->size
) != part
->offset
))) {
1160 memsize_format(tmpbuf
, offset
);
1161 len
= strlen(tmpbuf
) + 1;
1165 memcpy(p
, tmpbuf
, len
- 1);
1170 /* copy name only if user supplied */
1171 if(!part
->auto_name
) {
1172 len
= strlen(part
->name
) + 2;
1177 memcpy(p
, part
->name
, len
- 2);
1184 if (part
->mask_flags
&& MTD_WRITEABLE_CMD
) {
1193 /* print ',' separator if there are other partitions
1195 if (dev
->num_parts
> part_cnt
) {
1203 /* print ';' separator if there are other devices following */
1204 if (dentry
->next
!= &devices
) {
1212 /* we still have at least one char left, as we decremented maxlen at
1219 last_parts
[0] = '\0';
1224 * Call generate_mtdparts to process all devices and generate corresponding
1225 * mtdparts string, save it in mtdparts environment variable.
1227 * @param buf output buffer holding generated mtdparts string (output)
1228 * @param buflen buffer size
1229 * @return 0 on success, 1 otherwise
1231 static int generate_mtdparts_save(char *buf
, u32 buflen
)
1235 ret
= generate_mtdparts(buf
, buflen
);
1237 if ((buf
[0] != '\0') && (ret
== 0))
1238 setenv("mtdparts", buf
);
1240 setenv("mtdparts", NULL
);
1246 * Format and print out a partition list for each device from global device
1249 static void list_partitions(void)
1251 struct list_head
*dentry
, *pentry
;
1252 struct part_info
*part
;
1253 struct mtd_device
*dev
;
1256 DEBUGF("\n---list_partitions---\n");
1257 list_for_each(dentry
, &devices
) {
1258 dev
= list_entry(dentry
, struct mtd_device
, link
);
1259 printf("\ndevice %s%d <%s>, # parts = %d\n",
1260 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1261 dev
->id
->mtd_id
, dev
->num_parts
);
1262 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
1264 /* list partitions for given device */
1266 list_for_each(pentry
, &dev
->parts
) {
1267 part
= list_entry(pentry
, struct part_info
, link
);
1268 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1269 part_num
, part
->name
, part
->size
,
1270 part
->offset
, part
->mask_flags
);
1275 if (list_empty(&devices
))
1276 printf("no partitions defined\n");
1278 /* current_dev is not NULL only when we have non empty device list */
1280 part
= jffs2_part_info(current_dev
, current_partnum
);
1282 printf("\nactive partition: %s%d,%d - (%s) 0x%08lx @ 0x%08lx\n",
1283 MTD_DEV_TYPE(current_dev
->id
->type
),
1284 current_dev
->id
->num
, current_partnum
,
1285 part
->name
, part
->size
, part
->offset
);
1287 printf("could not get current partition info\n\n");
1291 printf("\ndefaults:\n");
1292 printf("mtdids : %s\n", mtdids_default
);
1293 printf("mtdparts: %s\n", mtdparts_default
);
1297 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1298 * corresponding device and verify partition number.
1300 * @param id string describing device and partition or partition name
1301 * @param dev pointer to the requested device (output)
1302 * @param part_num verified partition number (output)
1303 * @param part pointer to requested partition (output)
1304 * @return 0 on success, 1 otherwise
1306 int find_dev_and_part(const char *id
, struct mtd_device
**dev
,
1307 u8
*part_num
, struct part_info
**part
)
1309 struct list_head
*dentry
, *pentry
;
1310 u8 type
, dnum
, pnum
;
1313 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id
);
1315 list_for_each(dentry
, &devices
) {
1317 *dev
= list_entry(dentry
, struct mtd_device
, link
);
1318 list_for_each(pentry
, &(*dev
)->parts
) {
1319 *part
= list_entry(pentry
, struct part_info
, link
);
1320 if (strcmp((*part
)->name
, id
) == 0)
1331 if (id_parse(p
, &p
, &type
, &dnum
) != 0)
1334 if ((*p
++ != ',') || (*p
== '\0')) {
1335 printf("no partition number specified\n");
1338 pnum
= simple_strtoul(p
, (char **)&p
, 0);
1340 printf("unexpected trailing character '%c'\n", *p
);
1344 if ((*dev
= device_find(type
, dnum
)) == NULL
) {
1345 printf("no such device %s%d\n", MTD_DEV_TYPE(type
), dnum
);
1349 if ((*part
= jffs2_part_info(*dev
, pnum
)) == NULL
) {
1350 printf("no such partition\n");
1361 * Find and delete partition. For partition id format see find_dev_and_part().
1363 * @param id string describing device and partition
1364 * @return 0 on success, 1 otherwise
1366 static int delete_partition(const char *id
)
1369 struct mtd_device
*dev
;
1370 struct part_info
*part
;
1372 if (find_dev_and_part(id
, &dev
, &pnum
, &part
) == 0) {
1374 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1375 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
,
1376 part
->name
, part
->size
, part
->offset
);
1378 if (part_del(dev
, part
) != 0)
1381 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
1382 printf("generated mtdparts too long, reseting to null\n");
1388 printf("partition %s not found\n", id
);
1393 * Accept character string describing mtd partitions and call device_parse()
1394 * for each entry. Add created devices to the global devices list.
1396 * @param mtdparts string specifing mtd partitions
1397 * @return 0 on success, 1 otherwise
1399 static int parse_mtdparts(const char *const mtdparts
)
1401 const char *p
= mtdparts
;
1402 struct mtd_device
*dev
;
1405 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p
);
1407 /* delete all devices and partitions */
1408 if (devices_init() != 0) {
1409 printf("could not initialise device list\n");
1413 /* re-read 'mtdparts' variable, devices_init may be updating env */
1414 p
= getenv("mtdparts");
1416 if (strncmp(p
, "mtdparts=", 9) != 0) {
1417 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1422 while (p
&& (*p
!= '\0')) {
1424 if ((device_parse(p
, &p
, &dev
) != 0) || (!dev
))
1427 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
1428 dev
->id
->num
, dev
->id
->mtd_id
);
1430 /* check if parsed device is already on the list */
1431 if (device_find(dev
->id
->type
, dev
->id
->num
) != NULL
) {
1432 printf("device %s%d redefined, please correct mtdparts variable\n",
1433 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
1437 list_add_tail(&dev
->link
, &devices
);
1441 device_delall(&devices
);
1449 * Parse provided string describing mtdids mapping (see file header for mtdids
1450 * variable format). Allocate memory for each entry and add all found entries
1451 * to the global mtdids list.
1453 * @param ids mapping string
1454 * @return 0 on success, 1 otherwise
1456 static int parse_mtdids(const char *const ids
)
1458 const char *p
= ids
;
1462 struct list_head
*entry
, *n
;
1463 struct mtdids
*id_tmp
;
1468 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids
);
1470 /* clean global mtdids list */
1471 list_for_each_safe(entry
, n
, &mtdids
) {
1472 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1473 DEBUGF("mtdids del: %d %d\n", id_tmp
->type
, id_tmp
->num
);
1478 INIT_LIST_HEAD(&mtdids
);
1480 while(p
&& (*p
!= '\0')) {
1483 /* parse 'nor'|'nand'<dev-num> */
1484 if (id_parse(p
, &p
, &type
, &num
) != 0)
1488 printf("mtdids: incorrect <dev-num>\n");
1493 /* check if requested device exists */
1494 if (device_validate(type
, num
, &size
) != 0)
1497 /* locate <mtd-id> */
1499 if ((p
= strchr(mtd_id
, ',')) != NULL
) {
1500 mtd_id_len
= p
- mtd_id
+ 1;
1503 mtd_id_len
= strlen(mtd_id
) + 1;
1505 if (mtd_id_len
== 0) {
1506 printf("mtdids: no <mtd-id> identifier\n");
1510 /* check if this id is already on the list */
1511 int double_entry
= 0;
1512 list_for_each(entry
, &mtdids
) {
1513 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1514 if ((id_tmp
->type
== type
) && (id_tmp
->num
== num
)) {
1520 printf("device id %s%d redefined, please correct mtdids variable\n",
1521 MTD_DEV_TYPE(type
), num
);
1525 /* allocate mtdids structure */
1526 if (!(id
= (struct mtdids
*)malloc(sizeof(struct mtdids
) + mtd_id_len
))) {
1527 printf("out of memory\n");
1530 memset(id
, 0, sizeof(struct mtdids
) + mtd_id_len
);
1534 id
->mtd_id
= (char *)(id
+ 1);
1535 strncpy(id
->mtd_id
, mtd_id
, mtd_id_len
- 1);
1536 id
->mtd_id
[mtd_id_len
- 1] = '\0';
1537 INIT_LIST_HEAD(&id
->link
);
1539 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1540 MTD_DEV_TYPE(id
->type
), id
->num
,
1541 id
->size
, id
->mtd_id
);
1543 list_add_tail(&id
->link
, &mtdids
);
1547 /* clean mtdids list and free allocated memory */
1548 list_for_each_safe(entry
, n
, &mtdids
) {
1549 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1560 * Parse and initialize global mtdids mapping and create global
1561 * device/partition list.
1563 * @return 0 on success, 1 otherwise
1565 int mtdparts_init(void)
1567 static int initialized
= 0;
1568 const char *ids
, *parts
;
1569 const char *current_partition
;
1571 char tmp_ep
[PARTITION_MAXLEN
];
1573 DEBUGF("\n---mtdparts_init---\n");
1575 INIT_LIST_HEAD(&mtdids
);
1576 INIT_LIST_HEAD(&devices
);
1577 memset(last_ids
, 0, MTDIDS_MAXLEN
);
1578 memset(last_parts
, 0, MTDPARTS_MAXLEN
);
1579 memset(last_partition
, 0, PARTITION_MAXLEN
);
1584 ids
= getenv("mtdids");
1585 parts
= getenv("mtdparts");
1586 current_partition
= getenv("partition");
1588 /* save it for later parsing, cannot rely on current partition pointer
1589 * as 'partition' variable may be updated during init */
1591 if (current_partition
)
1592 strncpy(tmp_ep
, current_partition
, PARTITION_MAXLEN
);
1594 DEBUGF("last_ids : %s\n", last_ids
);
1595 DEBUGF("env_ids : %s\n", ids
);
1596 DEBUGF("last_parts: %s\n", last_parts
);
1597 DEBUGF("env_parts : %s\n\n", parts
);
1599 DEBUGF("last_partition : %s\n", last_partition
);
1600 DEBUGF("env_partition : %s\n", current_partition
);
1602 /* if mtdids varible is empty try to use defaults */
1604 if (mtdids_default
) {
1605 DEBUGF("mtdids variable not defined, using default\n");
1606 ids
= mtdids_default
;
1607 setenv("mtdids", (char *)ids
);
1609 printf("mtdids not defined, no default present\n");
1613 if (strlen(ids
) > MTDIDS_MAXLEN
- 1) {
1614 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN
);
1618 /* do no try to use defaults when mtdparts variable is not defined,
1619 * just check the length */
1621 printf("mtdparts variable not set, see 'help mtdparts'\n");
1623 if (parts
&& (strlen(parts
) > MTDPARTS_MAXLEN
- 1)) {
1624 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN
);
1628 /* check if we have already parsed those mtdids */
1629 if ((last_ids
[0] != '\0') && (strcmp(last_ids
, ids
) == 0)) {
1634 if (parse_mtdids(ids
) != 0) {
1639 /* ok it's good, save new ids */
1640 strncpy(last_ids
, ids
, MTDIDS_MAXLEN
);
1643 /* parse partitions if either mtdparts or mtdids were updated */
1644 if (parts
&& ((last_parts
[0] == '\0') || ((strcmp(last_parts
, parts
) != 0)) || ids_changed
)) {
1645 if (parse_mtdparts(parts
) != 0)
1648 if (list_empty(&devices
)) {
1649 printf("mtdparts_init: no valid partitions\n");
1653 /* ok it's good, save new parts */
1654 strncpy(last_parts
, parts
, MTDPARTS_MAXLEN
);
1656 /* reset first partition from first dev from the list as current */
1657 current_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
1658 current_partnum
= 0;
1661 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1662 MTD_DEV_TYPE(current_dev
->id
->type
),
1663 current_dev
->id
->num
, current_partnum
);
1666 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1667 if (!parts
&& (last_parts
[0] != '\0'))
1668 return devices_init();
1670 /* do not process current partition if mtdparts variable is null */
1674 /* is current partition set in environment? if so, use it */
1675 if ((tmp_ep
[0] != '\0') && (strcmp(tmp_ep
, last_partition
) != 0)) {
1676 struct part_info
*p
;
1677 struct mtd_device
*cdev
;
1680 DEBUGF("--- getting current partition: %s\n", tmp_ep
);
1682 if (find_dev_and_part(tmp_ep
, &cdev
, &pnum
, &p
) == 0) {
1684 current_partnum
= pnum
;
1687 } else if (getenv("partition") == NULL
) {
1688 DEBUGF("no partition variable set, setting...\n");
1694 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1696 * 'Static' version of command line mtdparts_init() routine. Single partition on
1697 * a single device configuration.
1701 * Parse and initialize global mtdids mapping and create global
1702 * device/partition list.
1704 * @return 0 on success, 1 otherwise
1706 int mtdparts_init(void)
1708 static int initialized
= 0;
1712 DEBUGF("\n---mtdparts_init---\n");
1715 struct part_info
*part
;
1718 current_dev
= (struct mtd_device
*)
1719 malloc(sizeof(struct mtd_device
) +
1720 sizeof(struct part_info
) +
1721 sizeof(struct mtdids
));
1723 printf("out of memory\n");
1726 memset(current_dev
, 0, sizeof(struct mtd_device
) +
1727 sizeof(struct part_info
) + sizeof(struct mtdids
));
1729 id
= (struct mtdids
*)(current_dev
+ 1);
1730 part
= (struct part_info
*)(id
+ 1);
1733 id
->mtd_id
= "single part";
1735 #if defined(CONFIG_JFFS2_DEV)
1736 dev_name
= CONFIG_JFFS2_DEV
;
1741 if ((id_parse(dev_name
, NULL
, &id
->type
, &id
->num
) != 0) ||
1742 (device_validate(id
->type
, id
->num
, &size
) != 0)) {
1743 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id
->type
), id
->num
);
1748 INIT_LIST_HEAD(&id
->link
);
1750 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1751 id
->type
, id
->num
, id
->size
, id
->mtd_id
);
1754 part
->name
= "static";
1755 part
->auto_name
= 0;
1757 #if defined(CONFIG_JFFS2_PART_SIZE)
1758 part
->size
= CONFIG_JFFS2_PART_SIZE
;
1760 part
->size
= SIZE_REMAINING
;
1763 #if defined(CONFIG_JFFS2_PART_OFFSET)
1764 part
->offset
= CONFIG_JFFS2_PART_OFFSET
;
1766 part
->offset
= 0x00000000;
1769 part
->dev
= current_dev
;
1770 INIT_LIST_HEAD(&part
->link
);
1772 /* recalculate size if needed */
1773 if (part
->size
== SIZE_REMAINING
)
1774 part
->size
= id
->size
- part
->offset
;
1776 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1777 part
->name
, part
->size
, part
->offset
);
1780 current_dev
->id
= id
;
1781 INIT_LIST_HEAD(¤t_dev
->link
);
1782 current_dev
->num_parts
= 1;
1783 INIT_LIST_HEAD(¤t_dev
->parts
);
1784 list_add(&part
->link
, ¤t_dev
->parts
);
1789 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1792 * Return pointer to the partition of a requested number from a requested
1795 * @param dev device that is to be searched for a partition
1796 * @param part_num requested partition number
1797 * @return pointer to the part_info, NULL otherwise
1799 static struct part_info
* jffs2_part_info(struct mtd_device
*dev
, unsigned int part_num
)
1801 struct list_head
*entry
;
1802 struct part_info
*part
;
1808 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1809 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1810 dev
->id
->num
, dev
->id
->mtd_id
);
1812 if (part_num
>= dev
->num_parts
) {
1813 printf("invalid partition number %d for device %s%d (%s)\n",
1814 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1815 dev
->id
->num
, dev
->id
->mtd_id
);
1819 /* locate partition number, return it */
1821 list_for_each(entry
, &dev
->parts
) {
1822 part
= list_entry(entry
, struct part_info
, link
);
1824 if (part_num
== num
++) {
1832 /***************************************************/
1833 /* U-boot commands */
1834 /***************************************************/
1837 * Routine implementing fsload u-boot command. This routine tries to load
1838 * a requested file from jffs2/cramfs filesystem on a current partition.
1840 * @param cmdtp command internal data
1841 * @param flag command flag
1842 * @param argc number of arguments supplied to the command
1843 * @param argv arguments list
1844 * @return 0 on success, 1 otherwise
1846 int do_jffs2_fsload(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1851 struct part_info
*part
;
1852 ulong offset
= load_addr
;
1854 /* pre-set Boot file name */
1855 if ((filename
= getenv("bootfile")) == NULL
) {
1856 filename
= "uImage";
1863 offset
= simple_strtoul(argv
[1], NULL
, 16);
1868 /* make sure we are in sync with env variables */
1869 if (mtdparts_init() !=0)
1872 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1874 /* check partition type for cramfs */
1875 fsname
= (cramfs_check(part
) ? "CRAMFS" : "JFFS2");
1876 printf("### %s loading '%s' to 0x%lx\n", fsname
, filename
, offset
);
1878 if (cramfs_check(part
)) {
1879 size
= cramfs_load ((char *) offset
, part
, filename
);
1881 /* if this is not cramfs assume jffs2 */
1882 size
= jffs2_1pass_load((char *)offset
, part
, filename
);
1887 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1888 fsname
, size
, offset
);
1889 sprintf(buf
, "%x", size
);
1890 setenv("filesize", buf
);
1892 printf("### %s LOAD ERROR<%x> for %s!\n", fsname
, size
, filename
);
1901 * Routine implementing u-boot ls command which lists content of a given
1902 * directory on a current partition.
1904 * @param cmdtp command internal data
1905 * @param flag command flag
1906 * @param argc number of arguments supplied to the command
1907 * @param argv arguments list
1908 * @return 0 on success, 1 otherwise
1910 int do_jffs2_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1912 char *filename
= "/";
1914 struct part_info
*part
;
1919 /* make sure we are in sync with env variables */
1920 if (mtdparts_init() !=0)
1923 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1925 /* check partition type for cramfs */
1926 if (cramfs_check(part
)) {
1927 ret
= cramfs_ls (part
, filename
);
1929 /* if this is not cramfs assume jffs2 */
1930 ret
= jffs2_1pass_ls(part
, filename
);
1939 * Routine implementing u-boot fsinfo command. This routine prints out
1940 * miscellaneous filesystem informations/statistics.
1942 * @param cmdtp command internal data
1943 * @param flag command flag
1944 * @param argc number of arguments supplied to the command
1945 * @param argv arguments list
1946 * @return 0 on success, 1 otherwise
1948 int do_jffs2_fsinfo(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1950 struct part_info
*part
;
1954 /* make sure we are in sync with env variables */
1955 if (mtdparts_init() !=0)
1958 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1960 /* check partition type for cramfs */
1961 fsname
= (cramfs_check(part
) ? "CRAMFS" : "JFFS2");
1962 printf("### filesystem type is %s\n", fsname
);
1964 if (cramfs_check(part
)) {
1965 ret
= cramfs_info (part
);
1967 /* if this is not cramfs assume jffs2 */
1968 ret
= jffs2_1pass_info(part
);
1976 /* command line only */
1977 #ifdef CONFIG_JFFS2_CMDLINE
1979 * Routine implementing u-boot chpart command. Sets new current partition based
1980 * on the user supplied partition id. For partition id format see find_dev_and_part().
1982 * @param cmdtp command internal data
1983 * @param flag command flag
1984 * @param argc number of arguments supplied to the command
1985 * @param argv arguments list
1986 * @return 0 on success, 1 otherwise
1988 int do_jffs2_chpart(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1990 /* command line only */
1991 struct mtd_device
*dev
;
1992 struct part_info
*part
;
1995 if (mtdparts_init() !=0)
1999 printf("no partition id specified\n");
2003 if (find_dev_and_part(argv
[1], &dev
, &pnum
, &part
) != 0)
2007 current_partnum
= pnum
;
2010 printf("partition changed to %s%d,%d\n",
2011 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
);
2017 * Routine implementing u-boot mtdparts command. Initialize/update default global
2018 * partition list and process user partition request (list, add, del).
2020 * @param cmdtp command internal data
2021 * @param flag command flag
2022 * @param argc number of arguments supplied to the command
2023 * @param argv arguments list
2024 * @return 0 on success, 1 otherwise
2026 int do_jffs2_mtdparts(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
2029 if (strcmp(argv
[1], "default") == 0) {
2030 setenv("mtdids", (char *)mtdids_default
);
2031 setenv("mtdparts", (char *)mtdparts_default
);
2032 setenv("partition", NULL
);
2036 } else if (strcmp(argv
[1], "delall") == 0) {
2037 /* this may be the first run, initialize lists if needed */
2040 setenv("mtdparts", NULL
);
2042 /* devices_init() calls current_save() */
2043 return devices_init();
2047 /* make sure we are in sync with env variables */
2048 if (mtdparts_init() != 0)
2056 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2057 if (((argc
== 5) || (argc
== 6)) && (strcmp(argv
[1], "add") == 0)) {
2058 #define PART_ADD_DESC_MAXLEN 64
2059 char tmpbuf
[PART_ADD_DESC_MAXLEN
];
2061 struct mtd_device
*dev
;
2062 struct mtd_device
*dev_tmp
;
2064 struct part_info
*p
;
2066 if (id_parse(argv
[2], NULL
, &type
, &num
) != 0)
2069 if ((id
= id_find(type
, num
)) == NULL
) {
2070 printf("no such device %s defined in mtdids variable\n", argv
[2]);
2074 len
= strlen(id
->mtd_id
) + 1; /* 'mtd_id:' */
2075 len
+= strlen(argv
[3]); /* size@offset */
2076 len
+= strlen(argv
[4]) + 2; /* '(' name ')' */
2077 if (argv
[5] && (strlen(argv
[5]) == 2))
2078 len
+= 2; /* 'ro' */
2080 if (len
>= PART_ADD_DESC_MAXLEN
) {
2081 printf("too long partition description\n");
2084 sprintf(tmpbuf
, "%s:%s(%s)%s",
2085 id
->mtd_id
, argv
[3], argv
[4], argv
[5] ? argv
[5] : "");
2086 DEBUGF("add tmpbuf: %s\n", tmpbuf
);
2088 if ((device_parse(tmpbuf
, NULL
, &dev
) != 0) || (!dev
))
2091 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
2092 dev
->id
->num
, dev
->id
->mtd_id
);
2094 if ((dev_tmp
= device_find(dev
->id
->type
, dev
->id
->num
)) == NULL
) {
2097 /* merge new partition with existing ones*/
2098 p
= list_entry(dev
->parts
.next
, struct part_info
, link
);
2099 if (part_add(dev_tmp
, p
) != 0) {
2105 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
2106 printf("generated mtdparts too long, reseting to null\n");
2113 /* mtdparts del part-id */
2114 if ((argc
== 3) && (strcmp(argv
[1], "del") == 0)) {
2115 DEBUGF("del: part-id = %s\n", argv
[2]);
2117 return delete_partition(argv
[2]);
2120 printf ("Usage:\n%s\n", cmdtp
->usage
);
2123 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2125 /***************************************************/
2127 fsload
, 3, 0, do_jffs2_fsload
,
2128 "fsload\t- load binary file from a filesystem image\n",
2129 "[ off ] [ filename ]\n"
2130 " - load binary file from flash bank\n"
2131 " with offset 'off'\n"
2134 ls
, 2, 1, do_jffs2_ls
,
2135 "ls\t- list files in a directory (default /)\n",
2137 " - list files in a directory.\n"
2141 fsinfo
, 1, 1, do_jffs2_fsinfo
,
2142 "fsinfo\t- print information about filesystems\n",
2143 " - print information about filesystems\n"
2146 #ifdef CONFIG_JFFS2_CMDLINE
2148 chpart
, 2, 0, do_jffs2_chpart
,
2149 "chpart\t- change active partition\n",
2151 " - change active partition (e.g. part-id = nand0,1)\n"
2155 mtdparts
, 6, 0, do_jffs2_mtdparts
,
2156 "mtdparts- define flash/nand partitions\n",
2158 " - list partition table\n"
2160 " - delete all partitions\n"
2161 "mtdparts del part-id\n"
2162 " - delete partition (e.g. part-id = nand0,1)\n"
2163 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2164 " - add partition\n"
2165 "mtdparts default\n"
2166 " - reset partition table to defaults\n\n"
2168 "this command uses three environment variables:\n\n"
2169 "'partition' - keeps current partition identifier\n\n"
2170 "partition := <part-id>\n"
2171 "<part-id> := <dev-id>,part_num\n\n"
2172 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2173 "mtdids=<idmap>[,<idmap>,...]\n\n"
2174 "<idmap> := <dev-id>=<mtd-id>\n"
2175 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2176 "<dev-num> := mtd device number, 0...\n"
2177 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2178 "'mtdparts' - partition list\n\n"
2179 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2180 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2181 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2182 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2183 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2184 "<offset> := partition start offset within the device\n"
2185 "<name> := '(' NAME ')'\n"
2186 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2188 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2190 /***************************************************/