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 #if defined(CONFIG_CMD_CRAMFS)
171 extern int cramfs_check (struct part_info
*info
);
172 extern int cramfs_load (char *loadoffset
, struct part_info
*info
, char *filename
);
173 extern int cramfs_ls (struct part_info
*info
, char *filename
);
174 extern int cramfs_info (struct part_info
*info
);
176 /* defining empty macros for function names is ugly but avoids ifdef clutter
177 * all over the code */
178 #define cramfs_check(x) (0)
179 #define cramfs_load(x,y,z) (-1)
180 #define cramfs_ls(x,y) (0)
181 #define cramfs_info(x) (0)
184 static struct part_info
* jffs2_part_info(struct mtd_device
*dev
, unsigned int part_num
);
186 /* command line only routines */
187 #ifdef CONFIG_JFFS2_CMDLINE
189 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
);
190 static int device_del(struct mtd_device
*dev
);
193 * Parses a string into a number. The number stored at ptr is
194 * potentially suffixed with K (for kilobytes, or 1024 bytes),
195 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
196 * 1073741824). If the number is suffixed with K, M, or G, then
197 * the return value is the number multiplied by one kilobyte, one
198 * megabyte, or one gigabyte, respectively.
200 * @param ptr where parse begins
201 * @param retptr output pointer to next char after parse completes (output)
202 * @return resulting unsigned int
204 static unsigned long memsize_parse (const char *const ptr
, const char **retptr
)
206 unsigned long ret
= simple_strtoul(ptr
, (char **)retptr
, 0);
227 * Format string describing supplied size. This routine does the opposite job
228 * to memsize_parse(). Size in bytes is converted to string and if possible
229 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
231 * Note, that this routine does not check for buffer overflow, it's the caller
232 * who must assure enough space.
234 * @param buf output buffer
235 * @param size size to be converted to string
237 static void memsize_format(char *buf
, u32 size
)
239 #define SIZE_GB ((u32)1024*1024*1024)
240 #define SIZE_MB ((u32)1024*1024)
241 #define SIZE_KB ((u32)1024)
243 if ((size
% SIZE_GB
) == 0)
244 sprintf(buf
, "%lug", size
/SIZE_GB
);
245 else if ((size
% SIZE_MB
) == 0)
246 sprintf(buf
, "%lum", size
/SIZE_MB
);
247 else if (size
% SIZE_KB
== 0)
248 sprintf(buf
, "%luk", size
/SIZE_KB
);
250 sprintf(buf
, "%lu", size
);
254 * This routine does global indexing of all partitions. Resulting index for
255 * current partition is saved in 'mtddevnum'. Current partition name in
258 static void index_partitions(void)
262 struct part_info
*part
;
263 struct list_head
*dentry
;
264 struct mtd_device
*dev
;
266 DEBUGF("--- index partitions ---\n");
270 list_for_each(dentry
, &devices
) {
271 dev
= list_entry(dentry
, struct mtd_device
, link
);
272 if (dev
== current_dev
) {
273 mtddevnum
+= current_partnum
;
274 sprintf(buf
, "%d", mtddevnum
);
275 setenv("mtddevnum", buf
);
278 mtddevnum
+= dev
->num_parts
;
281 part
= jffs2_part_info(current_dev
, current_partnum
);
282 setenv("mtddevname", part
->name
);
284 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum
, part
->name
);
286 setenv("mtddevnum", NULL
);
287 setenv("mtddevname", NULL
);
289 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
294 * Save current device and partition in environment variable 'partition'.
296 static void current_save(void)
300 DEBUGF("--- current_save ---\n");
303 sprintf(buf
, "%s%d,%d", MTD_DEV_TYPE(current_dev
->id
->type
),
304 current_dev
->id
->num
, current_partnum
);
306 setenv("partition", buf
);
307 strncpy(last_partition
, buf
, 16);
309 DEBUGF("=> partition %s\n", buf
);
311 setenv("partition", NULL
);
312 last_partition
[0] = '\0';
314 DEBUGF("=> partition NULL\n");
320 * Performs sanity check for supplied NOR flash partition. Table of existing
321 * NOR flash devices is searched and partition device is located. Alignment
322 * with the granularity of NOR flash sectors is verified.
324 * @param id of the parent device
325 * @param part partition to validate
326 * @return 0 if partition is valid, 1 otherwise
328 static int part_validate_nor(struct mtdids
*id
, struct part_info
*part
)
330 #if defined(CONFIG_CMD_FLASH)
331 /* info for FLASH chips */
332 extern flash_info_t flash_info
[];
338 flash
= &flash_info
[id
->num
];
341 for (i
= 0; i
< flash
->sector_count
; i
++) {
342 if ((flash
->start
[i
] - flash
->start
[0]) == part
->offset
) {
347 if (offset_aligned
== 0) {
348 printf("%s%d: partition (%s) start offset alignment incorrect\n",
349 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
353 end_offset
= part
->offset
+ part
->size
;
354 for (i
= 0; i
< flash
->sector_count
; i
++) {
355 if ((flash
->start
[i
] - flash
->start
[0]) == end_offset
)
359 if (flash
->size
== end_offset
)
362 printf("%s%d: partition (%s) size alignment incorrect\n",
363 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
369 * Performs sanity check for supplied NAND flash partition. Table of existing
370 * NAND flash devices is searched and partition device is located. Alignment
371 * with the granularity of nand erasesize is verified.
373 * @param id of the parent device
374 * @param part partition to validate
375 * @return 0 if partition is valid, 1 otherwise
377 static int part_validate_nand(struct mtdids
*id
, struct part_info
*part
)
379 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
380 /* info for NAND chips */
383 nand
= &nand_info
[id
->num
];
385 if ((unsigned long)(part
->offset
) % nand
->erasesize
) {
386 printf("%s%d: partition (%s) start offset alignment incorrect\n",
387 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
391 if (part
->size
% nand
->erasesize
) {
392 printf("%s%d: partition (%s) size alignment incorrect\n",
393 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
404 * Performs sanity check for supplied partition. Offset and size are verified
405 * to be within valid range. Partition type is checked and either
406 * parts_validate_nor() or parts_validate_nand() is called with the argument
409 * @param id of the parent device
410 * @param part partition to validate
411 * @return 0 if partition is valid, 1 otherwise
413 static int part_validate(struct mtdids
*id
, struct part_info
*part
)
415 if (part
->size
== SIZE_REMAINING
)
416 part
->size
= id
->size
- part
->offset
;
418 if (part
->offset
> id
->size
) {
419 printf("%s: offset %08lx beyond flash size %08lx\n",
420 id
->mtd_id
, part
->offset
, id
->size
);
424 if ((part
->offset
+ part
->size
) <= part
->offset
) {
425 printf("%s%d: partition (%s) size too big\n",
426 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
430 if (part
->offset
+ part
->size
> id
->size
) {
431 printf("%s: partitioning exceeds flash size\n", id
->mtd_id
);
435 if (id
->type
== MTD_DEV_TYPE_NAND
)
436 return part_validate_nand(id
, part
);
437 else if (id
->type
== MTD_DEV_TYPE_NOR
)
438 return part_validate_nor(id
, part
);
440 DEBUGF("part_validate: invalid dev type\n");
446 * Delete selected partition from the partion list of the specified device.
448 * @param dev device to delete partition from
449 * @param part partition to delete
450 * @return 0 on success, 1 otherwise
452 static int part_del(struct mtd_device
*dev
, struct part_info
*part
)
454 u8 current_save_needed
= 0;
456 /* if there is only one partition, remove whole device */
457 if (dev
->num_parts
== 1)
458 return device_del(dev
);
460 /* otherwise just delete this partition */
462 if (dev
== current_dev
) {
463 /* we are modyfing partitions for the current device,
465 struct part_info
*curr_pi
;
466 curr_pi
= jffs2_part_info(current_dev
, current_partnum
);
469 if (curr_pi
== part
) {
470 printf("current partition deleted, resetting current to 0\n");
472 } else if (part
->offset
<= curr_pi
->offset
) {
475 current_save_needed
= 1;
479 #ifdef CFG_NAND_LEGACY
480 jffs2_free_cache(part
);
482 list_del(&part
->link
);
486 if (current_save_needed
> 0)
495 * Delete all partitions from parts head list, free memory.
497 * @param head list of partitions to delete
499 static void part_delall(struct list_head
*head
)
501 struct list_head
*entry
, *n
;
502 struct part_info
*part_tmp
;
504 /* clean tmp_list and free allocated memory */
505 list_for_each_safe(entry
, n
, head
) {
506 part_tmp
= list_entry(entry
, struct part_info
, link
);
508 #ifdef CFG_NAND_LEGACY
509 jffs2_free_cache(part_tmp
);
517 * Add new partition to the supplied partition list. Make sure partitions are
518 * sorted by offset in ascending order.
520 * @param head list this partition is to be added to
521 * @param new partition to be added
523 static int part_sort_add(struct mtd_device
*dev
, struct part_info
*part
)
525 struct list_head
*entry
;
526 struct part_info
*new_pi
, *curr_pi
;
528 /* link partition to parrent dev */
531 if (list_empty(&dev
->parts
)) {
532 DEBUGF("part_sort_add: list empty\n");
533 list_add(&part
->link
, &dev
->parts
);
539 new_pi
= list_entry(&part
->link
, struct part_info
, link
);
541 /* get current partition info if we are updating current device */
543 if (dev
== current_dev
)
544 curr_pi
= jffs2_part_info(current_dev
, current_partnum
);
546 list_for_each(entry
, &dev
->parts
) {
547 struct part_info
*pi
;
549 pi
= list_entry(entry
, struct part_info
, link
);
551 /* be compliant with kernel cmdline, allow only one partition at offset zero */
552 if ((new_pi
->offset
== pi
->offset
) && (pi
->offset
== 0)) {
553 printf("cannot add second partition at offset 0\n");
557 if (new_pi
->offset
<= pi
->offset
) {
558 list_add_tail(&part
->link
, entry
);
561 if (curr_pi
&& (pi
->offset
<= curr_pi
->offset
)) {
562 /* we are modyfing partitions for the current
563 * device, update current */
573 list_add_tail(&part
->link
, &dev
->parts
);
580 * Add provided partition to the partition list of a given device.
582 * @param dev device to which partition is added
583 * @param part partition to be added
584 * @return 0 on success, 1 otherwise
586 static int part_add(struct mtd_device
*dev
, struct part_info
*part
)
588 /* verify alignment and size */
589 if (part_validate(dev
->id
, part
) != 0)
592 /* partition is ok, add it to the list */
593 if (part_sort_add(dev
, part
) != 0)
600 * Parse one partition definition, allocate memory and return pointer to this
601 * location in retpart.
603 * @param partdef pointer to the partition definition string i.e. <part-def>
604 * @param ret output pointer to next char after parse completes (output)
605 * @param retpart pointer to the allocated partition (output)
606 * @return 0 on success, 1 otherwise
608 static int part_parse(const char *const partdef
, const char **ret
, struct part_info
**retpart
)
610 struct part_info
*part
;
612 unsigned long offset
;
615 unsigned int mask_flags
;
622 /* fetch the partition size */
624 /* assign all remaining space to this partition */
625 DEBUGF("'-': remaining size assigned\n");
626 size
= SIZE_REMAINING
;
629 size
= memsize_parse(p
, &p
);
630 if (size
< MIN_PART_SIZE
) {
631 printf("partition size too small (%lx)\n", size
);
636 /* check for offset */
637 offset
= OFFSET_NOT_SPECIFIED
;
640 offset
= memsize_parse(p
, &p
);
643 /* now look for the name */
646 if ((p
= strchr(name
, ')')) == NULL
) {
647 printf("no closing ) found in partition name\n");
650 name_len
= p
- name
+ 1;
651 if ((name_len
- 1) == 0) {
652 printf("empty partition name\n");
657 /* 0x00000000@0x00000000 */
662 /* test for options */
664 if (strncmp(p
, "ro", 2) == 0) {
665 mask_flags
|= MTD_WRITEABLE_CMD
;
669 /* check for next partition definition */
671 if (size
== SIZE_REMAINING
) {
673 printf("no partitions allowed after a fill-up partition\n");
677 } else if ((*p
== ';') || (*p
== '\0')) {
680 printf("unexpected character '%c' at the end of partition\n", *p
);
685 /* allocate memory */
686 part
= (struct part_info
*)malloc(sizeof(struct part_info
) + name_len
);
688 printf("out of memory\n");
691 memset(part
, 0, sizeof(struct part_info
) + name_len
);
693 part
->offset
= offset
;
694 part
->mask_flags
= mask_flags
;
695 part
->name
= (char *)(part
+ 1);
698 /* copy user provided name */
699 strncpy(part
->name
, name
, name_len
- 1);
702 /* auto generated name in form of size@offset */
703 sprintf(part
->name
, "0x%08lx@0x%08lx", size
, offset
);
707 part
->name
[name_len
- 1] = '\0';
708 INIT_LIST_HEAD(&part
->link
);
710 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
711 part
->name
, part
->size
,
712 part
->offset
, part
->mask_flags
);
717 #endif/* #ifdef CONFIG_JFFS2_CMDLINE */
720 * Check device number to be within valid range for given device type.
722 * @param dev device to validate
723 * @return 0 if device is valid, 1 otherwise
725 static int device_validate(u8 type
, u8 num
, u32
*size
)
727 if (type
== MTD_DEV_TYPE_NOR
) {
728 #if defined(CONFIG_CMD_FLASH)
729 if (num
< CFG_MAX_FLASH_BANKS
) {
730 extern flash_info_t flash_info
[];
731 *size
= flash_info
[num
].size
;
736 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
737 MTD_DEV_TYPE(type
), num
, CFG_MAX_FLASH_BANKS
- 1);
739 printf("support for FLASH devices not present\n");
741 } else if (type
== MTD_DEV_TYPE_NAND
) {
742 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
743 if (num
< CFG_MAX_NAND_DEVICE
) {
744 #ifndef CFG_NAND_LEGACY
745 *size
= nand_info
[num
].size
;
747 extern struct nand_chip nand_dev_desc
[CFG_MAX_NAND_DEVICE
];
748 *size
= nand_dev_desc
[num
].totlen
;
753 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
754 MTD_DEV_TYPE(type
), num
, CFG_MAX_NAND_DEVICE
- 1);
756 printf("support for NAND devices not present\n");
763 #ifdef CONFIG_JFFS2_CMDLINE
765 * Delete all mtd devices from a supplied devices list, free memory allocated for
766 * each device and delete all device partitions.
768 * @return 0 on success, 1 otherwise
770 static int device_delall(struct list_head
*head
)
772 struct list_head
*entry
, *n
;
773 struct mtd_device
*dev_tmp
;
775 /* clean devices list */
776 list_for_each_safe(entry
, n
, head
) {
777 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
779 part_delall(&dev_tmp
->parts
);
782 INIT_LIST_HEAD(&devices
);
788 * If provided device exists it's partitions are deleted, device is removed
789 * from device list and device memory is freed.
791 * @param dev device to be deleted
792 * @return 0 on success, 1 otherwise
794 static int device_del(struct mtd_device
*dev
)
796 part_delall(&dev
->parts
);
797 list_del(&dev
->link
);
800 if (dev
== current_dev
) {
801 /* we just deleted current device */
802 if (list_empty(&devices
)) {
805 /* reset first partition from first dev from the
806 * devices list as current */
807 current_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
819 * Search global device list and return pointer to the device of type and num
822 * @param type device type
823 * @param num device number
824 * @return NULL if requested device does not exist
826 static struct mtd_device
* device_find(u8 type
, u8 num
)
828 struct list_head
*entry
;
829 struct mtd_device
*dev_tmp
;
831 list_for_each(entry
, &devices
) {
832 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
834 if ((dev_tmp
->id
->type
== type
) && (dev_tmp
->id
->num
== num
))
842 * Add specified device to the global device list.
844 * @param dev device to be added
846 static void device_add(struct mtd_device
*dev
)
848 u8 current_save_needed
= 0;
850 if (list_empty(&devices
)) {
853 current_save_needed
= 1;
856 list_add_tail(&dev
->link
, &devices
);
858 if (current_save_needed
> 0)
865 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
866 * return pointer to the device structure.
868 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
869 * @param ret output pointer to next char after parse completes (output)
870 * @param retdev pointer to the allocated device (output)
871 * @return 0 on success, 1 otherwise
873 static int device_parse(const char *const mtd_dev
, const char **ret
, struct mtd_device
**retdev
)
875 struct mtd_device
*dev
;
876 struct part_info
*part
;
879 unsigned int mtd_id_len
;
880 const char *p
, *pend
;
882 struct list_head
*entry
, *n
;
891 DEBUGF("===device_parse===\n");
895 if (!(p
= strchr(mtd_id
, ':'))) {
896 printf("no <mtd-id> identifier\n");
899 mtd_id_len
= p
- mtd_id
+ 1;
902 /* verify if we have a valid device specified */
903 if ((id
= id_find_by_mtd_id(mtd_id
, mtd_id_len
- 1)) == NULL
) {
904 printf("invalid mtd device '%.*s'\n", mtd_id_len
- 1, mtd_id
);
908 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
909 id
->type
, MTD_DEV_TYPE(id
->type
),
910 id
->num
, id
->mtd_id
);
911 pend
= strchr(p
, ';');
912 DEBUGF("parsing partitions %.*s\n", (pend
? pend
- p
: strlen(p
)), p
);
915 /* parse partitions */
919 if ((dev
= device_find(id
->type
, id
->num
)) != NULL
) {
920 /* if device already exists start at the end of the last partition */
921 part
= list_entry(dev
->parts
.prev
, struct part_info
, link
);
922 offset
= part
->offset
+ part
->size
;
925 while (p
&& (*p
!= '\0') && (*p
!= ';')) {
927 if ((part_parse(p
, &p
, &part
) != 0) || (!part
))
930 /* calculate offset when not specified */
931 if (part
->offset
== OFFSET_NOT_SPECIFIED
)
932 part
->offset
= offset
;
934 offset
= part
->offset
;
936 /* verify alignment and size */
937 if (part_validate(id
, part
) != 0)
940 offset
+= part
->size
;
942 /* partition is ok, add it to the list */
943 list_add_tail(&part
->link
, &tmp_list
);
948 part_delall(&tmp_list
);
952 if (num_parts
== 0) {
953 printf("no partitions for device %s%d (%s)\n",
954 MTD_DEV_TYPE(id
->type
), id
->num
, id
->mtd_id
);
958 DEBUGF("\ntotal partitions: %d\n", num_parts
);
960 /* check for next device presence */
964 } else if (*p
== '\0') {
967 printf("unexpected character '%c' at the end of device\n", *p
);
973 /* allocate memory for mtd_device structure */
974 if ((dev
= (struct mtd_device
*)malloc(sizeof(struct mtd_device
))) == NULL
) {
975 printf("out of memory\n");
978 memset(dev
, 0, sizeof(struct mtd_device
));
980 dev
->num_parts
= 0; /* part_sort_add increments num_parts */
981 INIT_LIST_HEAD(&dev
->parts
);
982 INIT_LIST_HEAD(&dev
->link
);
984 /* move partitions from tmp_list to dev->parts */
985 list_for_each_safe(entry
, n
, &tmp_list
) {
986 part
= list_entry(entry
, struct part_info
, link
);
988 if (part_sort_add(dev
, part
) != 0) {
1001 * Initialize global device list.
1003 * @return 0 on success, 1 otherwise
1005 static int devices_init(void)
1007 last_parts
[0] = '\0';
1011 return device_delall(&devices
);
1015 * Search global mtdids list and find id of requested type and number.
1017 * @return pointer to the id if it exists, NULL otherwise
1019 static struct mtdids
* id_find(u8 type
, u8 num
)
1021 struct list_head
*entry
;
1024 list_for_each(entry
, &mtdids
) {
1025 id
= list_entry(entry
, struct mtdids
, link
);
1027 if ((id
->type
== type
) && (id
->num
== num
))
1035 * Search global mtdids list and find id of a requested mtd_id.
1037 * Note: first argument is not null terminated.
1039 * @param mtd_id string containing requested mtd_id
1040 * @param mtd_id_len length of supplied mtd_id
1041 * @return pointer to the id if it exists, NULL otherwise
1043 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
)
1045 struct list_head
*entry
;
1048 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1049 mtd_id_len
, mtd_id
, mtd_id_len
);
1051 list_for_each(entry
, &mtdids
) {
1052 id
= list_entry(entry
, struct mtdids
, link
);
1054 DEBUGF("entry: '%s' (len = %d)\n",
1055 id
->mtd_id
, strlen(id
->mtd_id
));
1057 if (mtd_id_len
!= strlen(id
->mtd_id
))
1059 if (strncmp(id
->mtd_id
, mtd_id
, mtd_id_len
) == 0)
1065 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1068 * Parse device id string <dev-id> := 'nand'|'nor'<dev-num>, return device
1071 * @param id string describing device id
1072 * @param ret_id output pointer to next char after parse completes (output)
1073 * @param dev_type parsed device type (output)
1074 * @param dev_num parsed device number (output)
1075 * @return 0 on success, 1 otherwise
1077 int id_parse(const char *id
, const char **ret_id
, u8
*dev_type
, u8
*dev_num
)
1082 if (strncmp(p
, "nand", 4) == 0) {
1083 *dev_type
= MTD_DEV_TYPE_NAND
;
1085 } else if (strncmp(p
, "nor", 3) == 0) {
1086 *dev_type
= MTD_DEV_TYPE_NOR
;
1089 printf("incorrect device type in %s\n", id
);
1094 printf("incorrect device number in %s\n", id
);
1098 *dev_num
= simple_strtoul(p
, (char **)&p
, 0);
1104 #ifdef CONFIG_JFFS2_CMDLINE
1106 * Process all devices and generate corresponding mtdparts string describing
1107 * all partitions on all devices.
1109 * @param buf output buffer holding generated mtdparts string (output)
1110 * @param buflen buffer size
1111 * @return 0 on success, 1 otherwise
1113 static int generate_mtdparts(char *buf
, u32 buflen
)
1115 struct list_head
*pentry
, *dentry
;
1116 struct mtd_device
*dev
;
1117 struct part_info
*part
, *prev_part
;
1120 u32 size
, offset
, len
, part_cnt
;
1121 u32 maxlen
= buflen
- 1;
1123 DEBUGF("--- generate_mtdparts ---\n");
1125 if (list_empty(&devices
)) {
1130 sprintf(p
, "mtdparts=");
1133 list_for_each(dentry
, &devices
) {
1134 dev
= list_entry(dentry
, struct mtd_device
, link
);
1137 len
= strlen(dev
->id
->mtd_id
) + 1;
1140 memcpy(p
, dev
->id
->mtd_id
, len
- 1);
1145 /* format partitions */
1148 list_for_each(pentry
, &dev
->parts
) {
1149 part
= list_entry(pentry
, struct part_info
, link
);
1151 offset
= part
->offset
;
1154 /* partition size */
1155 memsize_format(tmpbuf
, size
);
1156 len
= strlen(tmpbuf
);
1159 memcpy(p
, tmpbuf
, len
);
1164 /* add offset only when there is a gap between
1166 if ((!prev_part
&& (offset
!= 0)) ||
1167 (prev_part
&& ((prev_part
->offset
+ prev_part
->size
) != part
->offset
))) {
1169 memsize_format(tmpbuf
, offset
);
1170 len
= strlen(tmpbuf
) + 1;
1174 memcpy(p
, tmpbuf
, len
- 1);
1179 /* copy name only if user supplied */
1180 if(!part
->auto_name
) {
1181 len
= strlen(part
->name
) + 2;
1186 memcpy(p
, part
->name
, len
- 2);
1193 if (part
->mask_flags
&& MTD_WRITEABLE_CMD
) {
1202 /* print ',' separator if there are other partitions
1204 if (dev
->num_parts
> part_cnt
) {
1212 /* print ';' separator if there are other devices following */
1213 if (dentry
->next
!= &devices
) {
1221 /* we still have at least one char left, as we decremented maxlen at
1228 last_parts
[0] = '\0';
1233 * Call generate_mtdparts to process all devices and generate corresponding
1234 * mtdparts string, save it in mtdparts environment variable.
1236 * @param buf output buffer holding generated mtdparts string (output)
1237 * @param buflen buffer size
1238 * @return 0 on success, 1 otherwise
1240 static int generate_mtdparts_save(char *buf
, u32 buflen
)
1244 ret
= generate_mtdparts(buf
, buflen
);
1246 if ((buf
[0] != '\0') && (ret
== 0))
1247 setenv("mtdparts", buf
);
1249 setenv("mtdparts", NULL
);
1255 * Format and print out a partition list for each device from global device
1258 static void list_partitions(void)
1260 struct list_head
*dentry
, *pentry
;
1261 struct part_info
*part
;
1262 struct mtd_device
*dev
;
1265 DEBUGF("\n---list_partitions---\n");
1266 list_for_each(dentry
, &devices
) {
1267 dev
= list_entry(dentry
, struct mtd_device
, link
);
1268 printf("\ndevice %s%d <%s>, # parts = %d\n",
1269 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1270 dev
->id
->mtd_id
, dev
->num_parts
);
1271 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
1273 /* list partitions for given device */
1275 list_for_each(pentry
, &dev
->parts
) {
1276 part
= list_entry(pentry
, struct part_info
, link
);
1277 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1278 part_num
, part
->name
, part
->size
,
1279 part
->offset
, part
->mask_flags
);
1284 if (list_empty(&devices
))
1285 printf("no partitions defined\n");
1287 /* current_dev is not NULL only when we have non empty device list */
1289 part
= jffs2_part_info(current_dev
, current_partnum
);
1291 printf("\nactive partition: %s%d,%d - (%s) 0x%08lx @ 0x%08lx\n",
1292 MTD_DEV_TYPE(current_dev
->id
->type
),
1293 current_dev
->id
->num
, current_partnum
,
1294 part
->name
, part
->size
, part
->offset
);
1296 printf("could not get current partition info\n\n");
1300 printf("\ndefaults:\n");
1301 printf("mtdids : %s\n", mtdids_default
);
1302 printf("mtdparts: %s\n", mtdparts_default
);
1306 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1307 * corresponding device and verify partition number.
1309 * @param id string describing device and partition or partition name
1310 * @param dev pointer to the requested device (output)
1311 * @param part_num verified partition number (output)
1312 * @param part pointer to requested partition (output)
1313 * @return 0 on success, 1 otherwise
1315 int find_dev_and_part(const char *id
, struct mtd_device
**dev
,
1316 u8
*part_num
, struct part_info
**part
)
1318 struct list_head
*dentry
, *pentry
;
1319 u8 type
, dnum
, pnum
;
1322 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id
);
1324 list_for_each(dentry
, &devices
) {
1326 *dev
= list_entry(dentry
, struct mtd_device
, link
);
1327 list_for_each(pentry
, &(*dev
)->parts
) {
1328 *part
= list_entry(pentry
, struct part_info
, link
);
1329 if (strcmp((*part
)->name
, id
) == 0)
1340 if (id_parse(p
, &p
, &type
, &dnum
) != 0)
1343 if ((*p
++ != ',') || (*p
== '\0')) {
1344 printf("no partition number specified\n");
1347 pnum
= simple_strtoul(p
, (char **)&p
, 0);
1349 printf("unexpected trailing character '%c'\n", *p
);
1353 if ((*dev
= device_find(type
, dnum
)) == NULL
) {
1354 printf("no such device %s%d\n", MTD_DEV_TYPE(type
), dnum
);
1358 if ((*part
= jffs2_part_info(*dev
, pnum
)) == NULL
) {
1359 printf("no such partition\n");
1370 * Find and delete partition. For partition id format see find_dev_and_part().
1372 * @param id string describing device and partition
1373 * @return 0 on success, 1 otherwise
1375 static int delete_partition(const char *id
)
1378 struct mtd_device
*dev
;
1379 struct part_info
*part
;
1381 if (find_dev_and_part(id
, &dev
, &pnum
, &part
) == 0) {
1383 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1384 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
,
1385 part
->name
, part
->size
, part
->offset
);
1387 if (part_del(dev
, part
) != 0)
1390 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
1391 printf("generated mtdparts too long, reseting to null\n");
1397 printf("partition %s not found\n", id
);
1402 * Accept character string describing mtd partitions and call device_parse()
1403 * for each entry. Add created devices to the global devices list.
1405 * @param mtdparts string specifing mtd partitions
1406 * @return 0 on success, 1 otherwise
1408 static int parse_mtdparts(const char *const mtdparts
)
1410 const char *p
= mtdparts
;
1411 struct mtd_device
*dev
;
1414 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p
);
1416 /* delete all devices and partitions */
1417 if (devices_init() != 0) {
1418 printf("could not initialise device list\n");
1422 /* re-read 'mtdparts' variable, devices_init may be updating env */
1423 p
= getenv("mtdparts");
1425 if (strncmp(p
, "mtdparts=", 9) != 0) {
1426 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1431 while (p
&& (*p
!= '\0')) {
1433 if ((device_parse(p
, &p
, &dev
) != 0) || (!dev
))
1436 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
1437 dev
->id
->num
, dev
->id
->mtd_id
);
1439 /* check if parsed device is already on the list */
1440 if (device_find(dev
->id
->type
, dev
->id
->num
) != NULL
) {
1441 printf("device %s%d redefined, please correct mtdparts variable\n",
1442 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
1446 list_add_tail(&dev
->link
, &devices
);
1450 device_delall(&devices
);
1458 * Parse provided string describing mtdids mapping (see file header for mtdids
1459 * variable format). Allocate memory for each entry and add all found entries
1460 * to the global mtdids list.
1462 * @param ids mapping string
1463 * @return 0 on success, 1 otherwise
1465 static int parse_mtdids(const char *const ids
)
1467 const char *p
= ids
;
1471 struct list_head
*entry
, *n
;
1472 struct mtdids
*id_tmp
;
1477 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids
);
1479 /* clean global mtdids list */
1480 list_for_each_safe(entry
, n
, &mtdids
) {
1481 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1482 DEBUGF("mtdids del: %d %d\n", id_tmp
->type
, id_tmp
->num
);
1487 INIT_LIST_HEAD(&mtdids
);
1489 while(p
&& (*p
!= '\0')) {
1492 /* parse 'nor'|'nand'<dev-num> */
1493 if (id_parse(p
, &p
, &type
, &num
) != 0)
1497 printf("mtdids: incorrect <dev-num>\n");
1502 /* check if requested device exists */
1503 if (device_validate(type
, num
, &size
) != 0)
1506 /* locate <mtd-id> */
1508 if ((p
= strchr(mtd_id
, ',')) != NULL
) {
1509 mtd_id_len
= p
- mtd_id
+ 1;
1512 mtd_id_len
= strlen(mtd_id
) + 1;
1514 if (mtd_id_len
== 0) {
1515 printf("mtdids: no <mtd-id> identifier\n");
1519 /* check if this id is already on the list */
1520 int double_entry
= 0;
1521 list_for_each(entry
, &mtdids
) {
1522 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1523 if ((id_tmp
->type
== type
) && (id_tmp
->num
== num
)) {
1529 printf("device id %s%d redefined, please correct mtdids variable\n",
1530 MTD_DEV_TYPE(type
), num
);
1534 /* allocate mtdids structure */
1535 if (!(id
= (struct mtdids
*)malloc(sizeof(struct mtdids
) + mtd_id_len
))) {
1536 printf("out of memory\n");
1539 memset(id
, 0, sizeof(struct mtdids
) + mtd_id_len
);
1543 id
->mtd_id
= (char *)(id
+ 1);
1544 strncpy(id
->mtd_id
, mtd_id
, mtd_id_len
- 1);
1545 id
->mtd_id
[mtd_id_len
- 1] = '\0';
1546 INIT_LIST_HEAD(&id
->link
);
1548 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1549 MTD_DEV_TYPE(id
->type
), id
->num
,
1550 id
->size
, id
->mtd_id
);
1552 list_add_tail(&id
->link
, &mtdids
);
1556 /* clean mtdids list and free allocated memory */
1557 list_for_each_safe(entry
, n
, &mtdids
) {
1558 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1569 * Parse and initialize global mtdids mapping and create global
1570 * device/partition list.
1572 * @return 0 on success, 1 otherwise
1574 int mtdparts_init(void)
1576 static int initialized
= 0;
1577 const char *ids
, *parts
;
1578 const char *current_partition
;
1580 char tmp_ep
[PARTITION_MAXLEN
];
1582 DEBUGF("\n---mtdparts_init---\n");
1584 INIT_LIST_HEAD(&mtdids
);
1585 INIT_LIST_HEAD(&devices
);
1586 memset(last_ids
, 0, MTDIDS_MAXLEN
);
1587 memset(last_parts
, 0, MTDPARTS_MAXLEN
);
1588 memset(last_partition
, 0, PARTITION_MAXLEN
);
1593 ids
= getenv("mtdids");
1594 parts
= getenv("mtdparts");
1595 current_partition
= getenv("partition");
1597 /* save it for later parsing, cannot rely on current partition pointer
1598 * as 'partition' variable may be updated during init */
1600 if (current_partition
)
1601 strncpy(tmp_ep
, current_partition
, PARTITION_MAXLEN
);
1603 DEBUGF("last_ids : %s\n", last_ids
);
1604 DEBUGF("env_ids : %s\n", ids
);
1605 DEBUGF("last_parts: %s\n", last_parts
);
1606 DEBUGF("env_parts : %s\n\n", parts
);
1608 DEBUGF("last_partition : %s\n", last_partition
);
1609 DEBUGF("env_partition : %s\n", current_partition
);
1611 /* if mtdids varible is empty try to use defaults */
1613 if (mtdids_default
) {
1614 DEBUGF("mtdids variable not defined, using default\n");
1615 ids
= mtdids_default
;
1616 setenv("mtdids", (char *)ids
);
1618 printf("mtdids not defined, no default present\n");
1622 if (strlen(ids
) > MTDIDS_MAXLEN
- 1) {
1623 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN
);
1627 /* do no try to use defaults when mtdparts variable is not defined,
1628 * just check the length */
1630 printf("mtdparts variable not set, see 'help mtdparts'\n");
1632 if (parts
&& (strlen(parts
) > MTDPARTS_MAXLEN
- 1)) {
1633 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN
);
1637 /* check if we have already parsed those mtdids */
1638 if ((last_ids
[0] != '\0') && (strcmp(last_ids
, ids
) == 0)) {
1643 if (parse_mtdids(ids
) != 0) {
1648 /* ok it's good, save new ids */
1649 strncpy(last_ids
, ids
, MTDIDS_MAXLEN
);
1652 /* parse partitions if either mtdparts or mtdids were updated */
1653 if (parts
&& ((last_parts
[0] == '\0') || ((strcmp(last_parts
, parts
) != 0)) || ids_changed
)) {
1654 if (parse_mtdparts(parts
) != 0)
1657 if (list_empty(&devices
)) {
1658 printf("mtdparts_init: no valid partitions\n");
1662 /* ok it's good, save new parts */
1663 strncpy(last_parts
, parts
, MTDPARTS_MAXLEN
);
1665 /* reset first partition from first dev from the list as current */
1666 current_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
1667 current_partnum
= 0;
1670 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1671 MTD_DEV_TYPE(current_dev
->id
->type
),
1672 current_dev
->id
->num
, current_partnum
);
1675 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1676 if (!parts
&& (last_parts
[0] != '\0'))
1677 return devices_init();
1679 /* do not process current partition if mtdparts variable is null */
1683 /* is current partition set in environment? if so, use it */
1684 if ((tmp_ep
[0] != '\0') && (strcmp(tmp_ep
, last_partition
) != 0)) {
1685 struct part_info
*p
;
1686 struct mtd_device
*cdev
;
1689 DEBUGF("--- getting current partition: %s\n", tmp_ep
);
1691 if (find_dev_and_part(tmp_ep
, &cdev
, &pnum
, &p
) == 0) {
1693 current_partnum
= pnum
;
1696 } else if (getenv("partition") == NULL
) {
1697 DEBUGF("no partition variable set, setting...\n");
1703 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1705 * 'Static' version of command line mtdparts_init() routine. Single partition on
1706 * a single device configuration.
1710 * Parse and initialize global mtdids mapping and create global
1711 * device/partition list.
1713 * @return 0 on success, 1 otherwise
1715 int mtdparts_init(void)
1717 static int initialized
= 0;
1721 DEBUGF("\n---mtdparts_init---\n");
1724 struct part_info
*part
;
1727 current_dev
= (struct mtd_device
*)
1728 malloc(sizeof(struct mtd_device
) +
1729 sizeof(struct part_info
) +
1730 sizeof(struct mtdids
));
1732 printf("out of memory\n");
1735 memset(current_dev
, 0, sizeof(struct mtd_device
) +
1736 sizeof(struct part_info
) + sizeof(struct mtdids
));
1738 id
= (struct mtdids
*)(current_dev
+ 1);
1739 part
= (struct part_info
*)(id
+ 1);
1742 id
->mtd_id
= "single part";
1744 #if defined(CONFIG_JFFS2_DEV)
1745 dev_name
= CONFIG_JFFS2_DEV
;
1750 if ((id_parse(dev_name
, NULL
, &id
->type
, &id
->num
) != 0) ||
1751 (device_validate(id
->type
, id
->num
, &size
) != 0)) {
1752 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id
->type
), id
->num
);
1757 INIT_LIST_HEAD(&id
->link
);
1759 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1760 id
->type
, id
->num
, id
->size
, id
->mtd_id
);
1763 part
->name
= "static";
1764 part
->auto_name
= 0;
1766 #if defined(CONFIG_JFFS2_PART_SIZE)
1767 part
->size
= CONFIG_JFFS2_PART_SIZE
;
1769 part
->size
= SIZE_REMAINING
;
1772 #if defined(CONFIG_JFFS2_PART_OFFSET)
1773 part
->offset
= CONFIG_JFFS2_PART_OFFSET
;
1775 part
->offset
= 0x00000000;
1778 part
->dev
= current_dev
;
1779 INIT_LIST_HEAD(&part
->link
);
1781 /* recalculate size if needed */
1782 if (part
->size
== SIZE_REMAINING
)
1783 part
->size
= id
->size
- part
->offset
;
1785 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1786 part
->name
, part
->size
, part
->offset
);
1789 current_dev
->id
= id
;
1790 INIT_LIST_HEAD(¤t_dev
->link
);
1791 current_dev
->num_parts
= 1;
1792 INIT_LIST_HEAD(¤t_dev
->parts
);
1793 list_add(&part
->link
, ¤t_dev
->parts
);
1798 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1801 * Return pointer to the partition of a requested number from a requested
1804 * @param dev device that is to be searched for a partition
1805 * @param part_num requested partition number
1806 * @return pointer to the part_info, NULL otherwise
1808 static struct part_info
* jffs2_part_info(struct mtd_device
*dev
, unsigned int part_num
)
1810 struct list_head
*entry
;
1811 struct part_info
*part
;
1817 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1818 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1819 dev
->id
->num
, dev
->id
->mtd_id
);
1821 if (part_num
>= dev
->num_parts
) {
1822 printf("invalid partition number %d for device %s%d (%s)\n",
1823 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1824 dev
->id
->num
, dev
->id
->mtd_id
);
1828 /* locate partition number, return it */
1830 list_for_each(entry
, &dev
->parts
) {
1831 part
= list_entry(entry
, struct part_info
, link
);
1833 if (part_num
== num
++) {
1841 /* Return the 'net size' of the partition (i.e. excluding any bad blocks) */
1842 unsigned int nand_net_part_size(struct part_info
*part
)
1844 struct mtd_info
*mtd
;
1846 unsigned int bb_delta
= 0;
1848 if (!part
|| !part
->dev
|| !part
->dev
->id
||
1849 part
->dev
->id
->num
>= CFG_MAX_NAND_DEVICE
)
1852 mtd
= &nand_info
[part
->dev
->id
->num
];
1854 for (offs
= part
->offset
; offs
< part
->offset
+ part
->size
;
1855 offs
+= mtd
->erasesize
) {
1856 if (nand_isbad_bbt(mtd
, offs
, 0))
1857 bb_delta
+= mtd
->erasesize
;
1860 return part
->size
- bb_delta
;
1864 /***************************************************/
1865 /* U-boot commands */
1866 /***************************************************/
1869 * Routine implementing fsload u-boot command. This routine tries to load
1870 * a requested file from jffs2/cramfs filesystem on a current partition.
1872 * @param cmdtp command internal data
1873 * @param flag command flag
1874 * @param argc number of arguments supplied to the command
1875 * @param argv arguments list
1876 * @return 0 on success, 1 otherwise
1878 int do_jffs2_fsload(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1883 struct part_info
*part
;
1884 ulong offset
= load_addr
;
1886 /* pre-set Boot file name */
1887 if ((filename
= getenv("bootfile")) == NULL
) {
1888 filename
= "uImage";
1895 offset
= simple_strtoul(argv
[1], NULL
, 16);
1900 /* make sure we are in sync with env variables */
1901 if (mtdparts_init() !=0)
1904 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1906 /* check partition type for cramfs */
1907 fsname
= (cramfs_check(part
) ? "CRAMFS" : "JFFS2");
1908 printf("### %s loading '%s' to 0x%lx\n", fsname
, filename
, offset
);
1910 if (cramfs_check(part
)) {
1911 size
= cramfs_load ((char *) offset
, part
, filename
);
1913 /* if this is not cramfs assume jffs2 */
1914 size
= jffs2_1pass_load((char *)offset
, part
, filename
);
1919 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1920 fsname
, size
, offset
);
1921 sprintf(buf
, "%x", size
);
1922 setenv("filesize", buf
);
1924 printf("### %s LOAD ERROR<%x> for %s!\n", fsname
, size
, filename
);
1933 * Routine implementing u-boot ls command which lists content of a given
1934 * directory on a current partition.
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
1942 int do_jffs2_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1944 char *filename
= "/";
1946 struct part_info
*part
;
1951 /* make sure we are in sync with env variables */
1952 if (mtdparts_init() !=0)
1955 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1957 /* check partition type for cramfs */
1958 if (cramfs_check(part
)) {
1959 ret
= cramfs_ls (part
, filename
);
1961 /* if this is not cramfs assume jffs2 */
1962 ret
= jffs2_1pass_ls(part
, filename
);
1971 * Routine implementing u-boot fsinfo command. This routine prints out
1972 * miscellaneous filesystem informations/statistics.
1974 * @param cmdtp command internal data
1975 * @param flag command flag
1976 * @param argc number of arguments supplied to the command
1977 * @param argv arguments list
1978 * @return 0 on success, 1 otherwise
1980 int do_jffs2_fsinfo(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1982 struct part_info
*part
;
1986 /* make sure we are in sync with env variables */
1987 if (mtdparts_init() !=0)
1990 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1992 /* check partition type for cramfs */
1993 fsname
= (cramfs_check(part
) ? "CRAMFS" : "JFFS2");
1994 printf("### filesystem type is %s\n", fsname
);
1996 if (cramfs_check(part
)) {
1997 ret
= cramfs_info (part
);
1999 /* if this is not cramfs assume jffs2 */
2000 ret
= jffs2_1pass_info(part
);
2008 /* command line only */
2009 #ifdef CONFIG_JFFS2_CMDLINE
2011 * Routine implementing u-boot chpart command. Sets new current partition based
2012 * on the user supplied partition id. For partition id format see find_dev_and_part().
2014 * @param cmdtp command internal data
2015 * @param flag command flag
2016 * @param argc number of arguments supplied to the command
2017 * @param argv arguments list
2018 * @return 0 on success, 1 otherwise
2020 int do_jffs2_chpart(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
2022 /* command line only */
2023 struct mtd_device
*dev
;
2024 struct part_info
*part
;
2027 if (mtdparts_init() !=0)
2031 printf("no partition id specified\n");
2035 if (find_dev_and_part(argv
[1], &dev
, &pnum
, &part
) != 0)
2039 current_partnum
= pnum
;
2042 printf("partition changed to %s%d,%d\n",
2043 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
);
2049 * Routine implementing u-boot mtdparts command. Initialize/update default global
2050 * partition list and process user partition request (list, add, del).
2052 * @param cmdtp command internal data
2053 * @param flag command flag
2054 * @param argc number of arguments supplied to the command
2055 * @param argv arguments list
2056 * @return 0 on success, 1 otherwise
2058 int do_jffs2_mtdparts(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
2061 if (strcmp(argv
[1], "default") == 0) {
2062 setenv("mtdids", (char *)mtdids_default
);
2063 setenv("mtdparts", (char *)mtdparts_default
);
2064 setenv("partition", NULL
);
2068 } else if (strcmp(argv
[1], "delall") == 0) {
2069 /* this may be the first run, initialize lists if needed */
2072 setenv("mtdparts", NULL
);
2074 /* devices_init() calls current_save() */
2075 return devices_init();
2079 /* make sure we are in sync with env variables */
2080 if (mtdparts_init() != 0)
2088 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2089 if (((argc
== 5) || (argc
== 6)) && (strcmp(argv
[1], "add") == 0)) {
2090 #define PART_ADD_DESC_MAXLEN 64
2091 char tmpbuf
[PART_ADD_DESC_MAXLEN
];
2093 struct mtd_device
*dev
;
2094 struct mtd_device
*dev_tmp
;
2096 struct part_info
*p
;
2098 if (id_parse(argv
[2], NULL
, &type
, &num
) != 0)
2101 if ((id
= id_find(type
, num
)) == NULL
) {
2102 printf("no such device %s defined in mtdids variable\n", argv
[2]);
2106 len
= strlen(id
->mtd_id
) + 1; /* 'mtd_id:' */
2107 len
+= strlen(argv
[3]); /* size@offset */
2108 len
+= strlen(argv
[4]) + 2; /* '(' name ')' */
2109 if (argv
[5] && (strlen(argv
[5]) == 2))
2110 len
+= 2; /* 'ro' */
2112 if (len
>= PART_ADD_DESC_MAXLEN
) {
2113 printf("too long partition description\n");
2116 sprintf(tmpbuf
, "%s:%s(%s)%s",
2117 id
->mtd_id
, argv
[3], argv
[4], argv
[5] ? argv
[5] : "");
2118 DEBUGF("add tmpbuf: %s\n", tmpbuf
);
2120 if ((device_parse(tmpbuf
, NULL
, &dev
) != 0) || (!dev
))
2123 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
2124 dev
->id
->num
, dev
->id
->mtd_id
);
2126 if ((dev_tmp
= device_find(dev
->id
->type
, dev
->id
->num
)) == NULL
) {
2129 /* merge new partition with existing ones*/
2130 p
= list_entry(dev
->parts
.next
, struct part_info
, link
);
2131 if (part_add(dev_tmp
, p
) != 0) {
2137 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
2138 printf("generated mtdparts too long, reseting to null\n");
2145 /* mtdparts del part-id */
2146 if ((argc
== 3) && (strcmp(argv
[1], "del") == 0)) {
2147 DEBUGF("del: part-id = %s\n", argv
[2]);
2149 return delete_partition(argv
[2]);
2152 printf ("Usage:\n%s\n", cmdtp
->usage
);
2156 #if defined(CONFIG_NAND_DYNPART)
2157 extern int nand_create_mtd_dynpart(struct mtd_info
*mtd
);
2159 int do_dynpart(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
2161 extern void dfu_update_strings(void);
2164 int i
= simple_strtoul(argv
[1], NULL
, 0);
2165 if (i
>= CFG_MAX_NAND_DEVICE
)
2168 nand_create_mtd_dynpart(&nand_info
[0]);
2170 #ifdef CONFIG_USBD_DFU
2171 dfu_update_strings();
2176 #endif /* CONFIG_NAND_DYNPART */
2179 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2181 /***************************************************/
2183 fsload
, 3, 0, do_jffs2_fsload
,
2184 "fsload\t- load binary file from a filesystem image\n",
2185 "[ off ] [ filename ]\n"
2186 " - load binary file from flash bank\n"
2187 " with offset 'off'\n"
2190 ls
, 2, 1, do_jffs2_ls
,
2191 "ls\t- list files in a directory (default /)\n",
2193 " - list files in a directory.\n"
2197 fsinfo
, 1, 1, do_jffs2_fsinfo
,
2198 "fsinfo\t- print information about filesystems\n",
2199 " - print information about filesystems\n"
2202 #ifdef CONFIG_JFFS2_CMDLINE
2204 chpart
, 2, 0, do_jffs2_chpart
,
2205 "chpart\t- change active partition\n",
2207 " - change active partition (e.g. part-id = nand0,1)\n"
2211 mtdparts
, 6, 0, do_jffs2_mtdparts
,
2212 "mtdparts- define flash/nand partitions\n",
2214 " - list partition table\n"
2216 " - delete all partitions\n"
2217 "mtdparts del part-id\n"
2218 " - delete partition (e.g. part-id = nand0,1)\n"
2219 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2220 " - add partition\n"
2221 "mtdparts default\n"
2222 " - reset partition table to defaults\n\n"
2224 "this command uses three environment variables:\n\n"
2225 "'partition' - keeps current partition identifier\n\n"
2226 "partition := <part-id>\n"
2227 "<part-id> := <dev-id>,part_num\n\n"
2228 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2229 "mtdids=<idmap>[,<idmap>,...]\n\n"
2230 "<idmap> := <dev-id>=<mtd-id>\n"
2231 "<dev-id> := 'nand'|'nor'<dev-num>\n"
2232 "<dev-num> := mtd device number, 0...\n"
2233 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2234 "'mtdparts' - partition list\n\n"
2235 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2236 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2237 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2238 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2239 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2240 "<offset> := partition start offset within the device\n"
2241 "<name> := '(' NAME ')'\n"
2242 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2245 #if defined(CONFIG_NAND_DYNPART)
2247 dynpart
, 1, 1, do_dynpart
,
2248 "dynpart\t- dynamically calculate partition table based on BBT\n",
2250 " - sets 'mtdparts' according to BBT\n");
2251 #endif /* CONFIG_NAND_DYNPART */
2253 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2255 /***************************************************/