]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_jffs2.c
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'|'onenand'<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 CONFIG_NAND_LEGACY
100 #include <linux/mtd/nand_legacy.h>
101 #else /* !CONFIG_NAND_LEGACY */
102 #include <linux/mtd/nand.h>
104 #endif /* !CONFIG_NAND_LEGACY */
107 #if defined(CONFIG_CMD_ONENAND)
108 #include <linux/mtd/mtd.h>
109 #include <linux/mtd/onenand.h>
110 #include <onenand_uboot.h>
113 /* enable/disable debugging messages */
118 # define DEBUGF(fmt, args...) printf(fmt ,##args)
120 # define DEBUGF(fmt, args...)
123 /* special size referring to all the remaining space in a partition */
124 #define SIZE_REMAINING 0xFFFFFFFF
126 /* special offset value, it is used when not provided by user
128 * this value is used temporarily during parsing, later such offests
129 * are recalculated */
130 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
132 /* minimum partition size */
133 #define MIN_PART_SIZE 4096
135 /* this flag needs to be set in part_info struct mask_flags
136 * field for read-only partitions */
137 #define MTD_WRITEABLE_CMD 1
139 #ifdef CONFIG_JFFS2_CMDLINE
140 /* default values for mtdids and mtdparts variables */
141 #if defined(MTDIDS_DEFAULT)
142 static const char *const mtdids_default
= MTDIDS_DEFAULT
;
144 #warning "MTDIDS_DEFAULT not defined!"
145 static const char *const mtdids_default
= NULL
;
148 #if defined(MTDPARTS_DEFAULT)
149 static const char *const mtdparts_default
= MTDPARTS_DEFAULT
;
151 #warning "MTDPARTS_DEFAULT not defined!"
152 static const char *const mtdparts_default
= NULL
;
155 /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
156 #define MTDIDS_MAXLEN 128
157 #define MTDPARTS_MAXLEN 512
158 #define PARTITION_MAXLEN 16
159 static char last_ids
[MTDIDS_MAXLEN
];
160 static char last_parts
[MTDPARTS_MAXLEN
];
161 static char last_partition
[PARTITION_MAXLEN
];
163 /* low level jffs2 cache cleaning routine */
164 extern void jffs2_free_cache(struct part_info
*part
);
166 /* mtdids mapping list, filled by parse_ids() */
167 struct list_head mtdids
;
169 /* device/partition list, parse_cmdline() parses into here */
170 struct list_head devices
;
171 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
173 /* current active device and partition number */
174 static struct mtd_device
*current_dev
= NULL
;
175 static u8 current_partnum
= 0;
177 #if defined(CONFIG_CMD_CRAMFS)
178 extern int cramfs_check (struct part_info
*info
);
179 extern int cramfs_load (char *loadoffset
, struct part_info
*info
, char *filename
);
180 extern int cramfs_ls (struct part_info
*info
, char *filename
);
181 extern int cramfs_info (struct part_info
*info
);
183 /* defining empty macros for function names is ugly but avoids ifdef clutter
184 * all over the code */
185 #define cramfs_check(x) (0)
186 #define cramfs_load(x,y,z) (-1)
187 #define cramfs_ls(x,y) (0)
188 #define cramfs_info(x) (0)
191 static struct part_info
* jffs2_part_info(struct mtd_device
*dev
, unsigned int part_num
);
193 /* command line only routines */
194 #ifdef CONFIG_JFFS2_CMDLINE
196 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
);
197 static int device_del(struct mtd_device
*dev
);
200 * Parses a string into a number. The number stored at ptr is
201 * potentially suffixed with K (for kilobytes, or 1024 bytes),
202 * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
203 * 1073741824). If the number is suffixed with K, M, or G, then
204 * the return value is the number multiplied by one kilobyte, one
205 * megabyte, or one gigabyte, respectively.
207 * @param ptr where parse begins
208 * @param retptr output pointer to next char after parse completes (output)
209 * @return resulting unsigned int
211 static unsigned long memsize_parse (const char *const ptr
, const char **retptr
)
213 unsigned long ret
= simple_strtoul(ptr
, (char **)retptr
, 0);
234 * Format string describing supplied size. This routine does the opposite job
235 * to memsize_parse(). Size in bytes is converted to string and if possible
236 * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
238 * Note, that this routine does not check for buffer overflow, it's the caller
239 * who must assure enough space.
241 * @param buf output buffer
242 * @param size size to be converted to string
244 static void memsize_format(char *buf
, u32 size
)
246 #define SIZE_GB ((u32)1024*1024*1024)
247 #define SIZE_MB ((u32)1024*1024)
248 #define SIZE_KB ((u32)1024)
250 if ((size
% SIZE_GB
) == 0)
251 sprintf(buf
, "%ug", size
/SIZE_GB
);
252 else if ((size
% SIZE_MB
) == 0)
253 sprintf(buf
, "%um", size
/SIZE_MB
);
254 else if (size
% SIZE_KB
== 0)
255 sprintf(buf
, "%uk", size
/SIZE_KB
);
257 sprintf(buf
, "%u", size
);
261 * This routine does global indexing of all partitions. Resulting index for
262 * current partition is saved in 'mtddevnum'. Current partition name in
265 static void index_partitions(void)
269 struct part_info
*part
;
270 struct list_head
*dentry
;
271 struct mtd_device
*dev
;
273 DEBUGF("--- index partitions ---\n");
277 list_for_each(dentry
, &devices
) {
278 dev
= list_entry(dentry
, struct mtd_device
, link
);
279 if (dev
== current_dev
) {
280 mtddevnum
+= current_partnum
;
281 sprintf(buf
, "%d", mtddevnum
);
282 setenv("mtddevnum", buf
);
285 mtddevnum
+= dev
->num_parts
;
288 part
= jffs2_part_info(current_dev
, current_partnum
);
289 setenv("mtddevname", part
->name
);
291 DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum
, part
->name
);
293 setenv("mtddevnum", NULL
);
294 setenv("mtddevname", NULL
);
296 DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
301 * Save current device and partition in environment variable 'partition'.
303 static void current_save(void)
307 DEBUGF("--- current_save ---\n");
310 sprintf(buf
, "%s%d,%d", MTD_DEV_TYPE(current_dev
->id
->type
),
311 current_dev
->id
->num
, current_partnum
);
313 setenv("partition", buf
);
314 strncpy(last_partition
, buf
, 16);
316 DEBUGF("=> partition %s\n", buf
);
318 setenv("partition", NULL
);
319 last_partition
[0] = '\0';
321 DEBUGF("=> partition NULL\n");
327 * Performs sanity check for supplied NOR flash partition. Table of existing
328 * NOR flash devices is searched and partition device is located. Alignment
329 * with the granularity of NOR flash sectors is verified.
331 * @param id of the parent device
332 * @param part partition to validate
333 * @return 0 if partition is valid, 1 otherwise
335 static int part_validate_nor(struct mtdids
*id
, struct part_info
*part
)
337 #if defined(CONFIG_CMD_FLASH)
338 /* info for FLASH chips */
339 extern flash_info_t flash_info
[];
345 flash
= &flash_info
[id
->num
];
348 for (i
= 0; i
< flash
->sector_count
; i
++) {
349 if ((flash
->start
[i
] - flash
->start
[0]) == part
->offset
) {
354 if (offset_aligned
== 0) {
355 printf("%s%d: partition (%s) start offset alignment incorrect\n",
356 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
360 end_offset
= part
->offset
+ part
->size
;
361 for (i
= 0; i
< flash
->sector_count
; i
++) {
362 if ((flash
->start
[i
] - flash
->start
[0]) == end_offset
)
366 if (flash
->size
== end_offset
)
369 printf("%s%d: partition (%s) size alignment incorrect\n",
370 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
376 * Performs sanity check for supplied NAND flash partition. Table of existing
377 * NAND flash devices is searched and partition device is located. Alignment
378 * with the granularity of nand erasesize is verified.
380 * @param id of the parent device
381 * @param part partition to validate
382 * @return 0 if partition is valid, 1 otherwise
384 static int part_validate_nand(struct mtdids
*id
, struct part_info
*part
)
386 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
387 /* info for NAND chips */
390 nand
= &nand_info
[id
->num
];
392 if ((unsigned long)(part
->offset
) % nand
->erasesize
) {
393 printf("%s%d: partition (%s) start offset alignment incorrect\n",
394 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
398 if (part
->size
% nand
->erasesize
) {
399 printf("%s%d: partition (%s) size alignment incorrect\n",
400 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
411 * Performs sanity check for supplied OneNAND flash partition.
412 * Table of existing OneNAND flash devices is searched and partition device
413 * is located. Alignment with the granularity of nand erasesize is verified.
415 * @param id of the parent device
416 * @param part partition to validate
417 * @return 0 if partition is valid, 1 otherwise
419 static int part_validate_onenand(struct mtdids
*id
, struct part_info
*part
)
421 #if defined(CONFIG_CMD_ONENAND)
422 /* info for OneNAND chips */
423 struct mtd_info
*mtd
;
427 if ((unsigned long)(part
->offset
) % mtd
->erasesize
) {
428 printf("%s%d: partition (%s) start offset"
429 "alignment incorrect\n",
430 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
434 if (part
->size
% mtd
->erasesize
) {
435 printf("%s%d: partition (%s) size alignment incorrect\n",
436 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
448 * Performs sanity check for supplied partition. Offset and size are verified
449 * to be within valid range. Partition type is checked and either
450 * parts_validate_nor() or parts_validate_nand() is called with the argument
453 * @param id of the parent device
454 * @param part partition to validate
455 * @return 0 if partition is valid, 1 otherwise
457 static int part_validate(struct mtdids
*id
, struct part_info
*part
)
459 if (part
->size
== SIZE_REMAINING
)
460 part
->size
= id
->size
- part
->offset
;
462 if (part
->offset
> id
->size
) {
463 printf("%s: offset %08x beyond flash size %08x\n",
464 id
->mtd_id
, part
->offset
, id
->size
);
468 if ((part
->offset
+ part
->size
) <= part
->offset
) {
469 printf("%s%d: partition (%s) size too big\n",
470 MTD_DEV_TYPE(id
->type
), id
->num
, part
->name
);
474 if (part
->offset
+ part
->size
> id
->size
) {
475 printf("%s: partitioning exceeds flash size\n", id
->mtd_id
);
479 if (id
->type
== MTD_DEV_TYPE_NAND
)
480 return part_validate_nand(id
, part
);
481 else if (id
->type
== MTD_DEV_TYPE_NOR
)
482 return part_validate_nor(id
, part
);
483 else if (id
->type
== MTD_DEV_TYPE_ONENAND
)
484 return part_validate_onenand(id
, part
);
486 DEBUGF("part_validate: invalid dev type\n");
492 * Delete selected partition from the partion list of the specified device.
494 * @param dev device to delete partition from
495 * @param part partition to delete
496 * @return 0 on success, 1 otherwise
498 static int part_del(struct mtd_device
*dev
, struct part_info
*part
)
500 u8 current_save_needed
= 0;
502 /* if there is only one partition, remove whole device */
503 if (dev
->num_parts
== 1)
504 return device_del(dev
);
506 /* otherwise just delete this partition */
508 if (dev
== current_dev
) {
509 /* we are modyfing partitions for the current device,
511 struct part_info
*curr_pi
;
512 curr_pi
= jffs2_part_info(current_dev
, current_partnum
);
515 if (curr_pi
== part
) {
516 printf("current partition deleted, resetting current to 0\n");
518 } else if (part
->offset
<= curr_pi
->offset
) {
521 current_save_needed
= 1;
525 #ifdef CONFIG_NAND_LEGACY
526 jffs2_free_cache(part
);
528 list_del(&part
->link
);
532 if (current_save_needed
> 0)
541 * Delete all partitions from parts head list, free memory.
543 * @param head list of partitions to delete
545 static void part_delall(struct list_head
*head
)
547 struct list_head
*entry
, *n
;
548 struct part_info
*part_tmp
;
550 /* clean tmp_list and free allocated memory */
551 list_for_each_safe(entry
, n
, head
) {
552 part_tmp
= list_entry(entry
, struct part_info
, link
);
554 #ifdef CONFIG_NAND_LEGACY
555 jffs2_free_cache(part_tmp
);
563 * Add new partition to the supplied partition list. Make sure partitions are
564 * sorted by offset in ascending order.
566 * @param head list this partition is to be added to
567 * @param new partition to be added
569 static int part_sort_add(struct mtd_device
*dev
, struct part_info
*part
)
571 struct list_head
*entry
;
572 struct part_info
*new_pi
, *curr_pi
;
574 /* link partition to parrent dev */
577 if (list_empty(&dev
->parts
)) {
578 DEBUGF("part_sort_add: list empty\n");
579 list_add(&part
->link
, &dev
->parts
);
585 new_pi
= list_entry(&part
->link
, struct part_info
, link
);
587 /* get current partition info if we are updating current device */
589 if (dev
== current_dev
)
590 curr_pi
= jffs2_part_info(current_dev
, current_partnum
);
592 list_for_each(entry
, &dev
->parts
) {
593 struct part_info
*pi
;
595 pi
= list_entry(entry
, struct part_info
, link
);
597 /* be compliant with kernel cmdline, allow only one partition at offset zero */
598 if ((new_pi
->offset
== pi
->offset
) && (pi
->offset
== 0)) {
599 printf("cannot add second partition at offset 0\n");
603 if (new_pi
->offset
<= pi
->offset
) {
604 list_add_tail(&part
->link
, entry
);
607 if (curr_pi
&& (pi
->offset
<= curr_pi
->offset
)) {
608 /* we are modyfing partitions for the current
609 * device, update current */
619 list_add_tail(&part
->link
, &dev
->parts
);
626 * Add provided partition to the partition list of a given device.
628 * @param dev device to which partition is added
629 * @param part partition to be added
630 * @return 0 on success, 1 otherwise
632 static int part_add(struct mtd_device
*dev
, struct part_info
*part
)
634 /* verify alignment and size */
635 if (part_validate(dev
->id
, part
) != 0)
638 /* partition is ok, add it to the list */
639 if (part_sort_add(dev
, part
) != 0)
646 * Parse one partition definition, allocate memory and return pointer to this
647 * location in retpart.
649 * @param partdef pointer to the partition definition string i.e. <part-def>
650 * @param ret output pointer to next char after parse completes (output)
651 * @param retpart pointer to the allocated partition (output)
652 * @return 0 on success, 1 otherwise
654 static int part_parse(const char *const partdef
, const char **ret
, struct part_info
**retpart
)
656 struct part_info
*part
;
658 unsigned long offset
;
661 unsigned int mask_flags
;
668 /* fetch the partition size */
670 /* assign all remaining space to this partition */
671 DEBUGF("'-': remaining size assigned\n");
672 size
= SIZE_REMAINING
;
675 size
= memsize_parse(p
, &p
);
676 if (size
< MIN_PART_SIZE
) {
677 printf("partition size too small (%lx)\n", size
);
682 /* check for offset */
683 offset
= OFFSET_NOT_SPECIFIED
;
686 offset
= memsize_parse(p
, &p
);
689 /* now look for the name */
692 if ((p
= strchr(name
, ')')) == NULL
) {
693 printf("no closing ) found in partition name\n");
696 name_len
= p
- name
+ 1;
697 if ((name_len
- 1) == 0) {
698 printf("empty partition name\n");
703 /* 0x00000000@0x00000000 */
708 /* test for options */
710 if (strncmp(p
, "ro", 2) == 0) {
711 mask_flags
|= MTD_WRITEABLE_CMD
;
715 /* check for next partition definition */
717 if (size
== SIZE_REMAINING
) {
719 printf("no partitions allowed after a fill-up partition\n");
723 } else if ((*p
== ';') || (*p
== '\0')) {
726 printf("unexpected character '%c' at the end of partition\n", *p
);
731 /* allocate memory */
732 part
= (struct part_info
*)malloc(sizeof(struct part_info
) + name_len
);
734 printf("out of memory\n");
737 memset(part
, 0, sizeof(struct part_info
) + name_len
);
739 part
->offset
= offset
;
740 part
->mask_flags
= mask_flags
;
741 part
->name
= (char *)(part
+ 1);
744 /* copy user provided name */
745 strncpy(part
->name
, name
, name_len
- 1);
748 /* auto generated name in form of size@offset */
749 sprintf(part
->name
, "0x%08lx@0x%08lx", size
, offset
);
753 part
->name
[name_len
- 1] = '\0';
754 INIT_LIST_HEAD(&part
->link
);
756 DEBUGF("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
757 part
->name
, part
->size
,
758 part
->offset
, part
->mask_flags
);
763 #endif/* #ifdef CONFIG_JFFS2_CMDLINE */
766 * Check device number to be within valid range for given device type.
768 * @param dev device to validate
769 * @return 0 if device is valid, 1 otherwise
771 static int device_validate(u8 type
, u8 num
, u32
*size
)
773 if (type
== MTD_DEV_TYPE_NOR
) {
774 #if defined(CONFIG_CMD_FLASH)
775 if (num
< CFG_MAX_FLASH_BANKS
) {
776 extern flash_info_t flash_info
[];
777 *size
= flash_info
[num
].size
;
782 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
783 MTD_DEV_TYPE(type
), num
, CFG_MAX_FLASH_BANKS
- 1);
785 printf("support for FLASH devices not present\n");
787 } else if (type
== MTD_DEV_TYPE_NAND
) {
788 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
789 if (num
< CFG_MAX_NAND_DEVICE
) {
790 #ifndef CONFIG_NAND_LEGACY
791 *size
= nand_info
[num
].size
;
793 extern struct nand_chip nand_dev_desc
[CFG_MAX_NAND_DEVICE
];
794 *size
= nand_dev_desc
[num
].totlen
;
799 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
800 MTD_DEV_TYPE(type
), num
, CFG_MAX_NAND_DEVICE
- 1);
802 printf("support for NAND devices not present\n");
804 } else if (type
== MTD_DEV_TYPE_ONENAND
) {
805 #if defined(CONFIG_CMD_ONENAND)
806 *size
= onenand_mtd
.size
;
809 printf("support for OneNAND devices not present\n");
812 printf("Unknown defice type %d\n", type
);
817 #ifdef CONFIG_JFFS2_CMDLINE
819 * Delete all mtd devices from a supplied devices list, free memory allocated for
820 * each device and delete all device partitions.
822 * @return 0 on success, 1 otherwise
824 static int device_delall(struct list_head
*head
)
826 struct list_head
*entry
, *n
;
827 struct mtd_device
*dev_tmp
;
829 /* clean devices list */
830 list_for_each_safe(entry
, n
, head
) {
831 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
833 part_delall(&dev_tmp
->parts
);
836 INIT_LIST_HEAD(&devices
);
842 * If provided device exists it's partitions are deleted, device is removed
843 * from device list and device memory is freed.
845 * @param dev device to be deleted
846 * @return 0 on success, 1 otherwise
848 static int device_del(struct mtd_device
*dev
)
850 part_delall(&dev
->parts
);
851 list_del(&dev
->link
);
854 if (dev
== current_dev
) {
855 /* we just deleted current device */
856 if (list_empty(&devices
)) {
859 /* reset first partition from first dev from the
860 * devices list as current */
861 current_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
873 * Search global device list and return pointer to the device of type and num
876 * @param type device type
877 * @param num device number
878 * @return NULL if requested device does not exist
880 static struct mtd_device
* device_find(u8 type
, u8 num
)
882 struct list_head
*entry
;
883 struct mtd_device
*dev_tmp
;
885 list_for_each(entry
, &devices
) {
886 dev_tmp
= list_entry(entry
, struct mtd_device
, link
);
888 if ((dev_tmp
->id
->type
== type
) && (dev_tmp
->id
->num
== num
))
896 * Add specified device to the global device list.
898 * @param dev device to be added
900 static void device_add(struct mtd_device
*dev
)
902 u8 current_save_needed
= 0;
904 if (list_empty(&devices
)) {
907 current_save_needed
= 1;
910 list_add_tail(&dev
->link
, &devices
);
912 if (current_save_needed
> 0)
919 * Parse device type, name and mtd-id. If syntax is ok allocate memory and
920 * return pointer to the device structure.
922 * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
923 * @param ret output pointer to next char after parse completes (output)
924 * @param retdev pointer to the allocated device (output)
925 * @return 0 on success, 1 otherwise
927 static int device_parse(const char *const mtd_dev
, const char **ret
, struct mtd_device
**retdev
)
929 struct mtd_device
*dev
;
930 struct part_info
*part
;
933 unsigned int mtd_id_len
;
934 const char *p
, *pend
;
936 struct list_head
*entry
, *n
;
945 DEBUGF("===device_parse===\n");
949 if (!(p
= strchr(mtd_id
, ':'))) {
950 printf("no <mtd-id> identifier\n");
953 mtd_id_len
= p
- mtd_id
+ 1;
956 /* verify if we have a valid device specified */
957 if ((id
= id_find_by_mtd_id(mtd_id
, mtd_id_len
- 1)) == NULL
) {
958 printf("invalid mtd device '%.*s'\n", mtd_id_len
- 1, mtd_id
);
962 DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
963 id
->type
, MTD_DEV_TYPE(id
->type
),
964 id
->num
, id
->mtd_id
);
965 pend
= strchr(p
, ';');
966 DEBUGF("parsing partitions %.*s\n", (pend
? pend
- p
: strlen(p
)), p
);
969 /* parse partitions */
973 if ((dev
= device_find(id
->type
, id
->num
)) != NULL
) {
974 /* if device already exists start at the end of the last partition */
975 part
= list_entry(dev
->parts
.prev
, struct part_info
, link
);
976 offset
= part
->offset
+ part
->size
;
979 while (p
&& (*p
!= '\0') && (*p
!= ';')) {
981 if ((part_parse(p
, &p
, &part
) != 0) || (!part
))
984 /* calculate offset when not specified */
985 if (part
->offset
== OFFSET_NOT_SPECIFIED
)
986 part
->offset
= offset
;
988 offset
= part
->offset
;
990 /* verify alignment and size */
991 if (part_validate(id
, part
) != 0)
994 offset
+= part
->size
;
996 /* partition is ok, add it to the list */
997 list_add_tail(&part
->link
, &tmp_list
);
1002 part_delall(&tmp_list
);
1006 if (num_parts
== 0) {
1007 printf("no partitions for device %s%d (%s)\n",
1008 MTD_DEV_TYPE(id
->type
), id
->num
, id
->mtd_id
);
1012 DEBUGF("\ntotal partitions: %d\n", num_parts
);
1014 /* check for next device presence */
1018 } else if (*p
== '\0') {
1021 printf("unexpected character '%c' at the end of device\n", *p
);
1027 /* allocate memory for mtd_device structure */
1028 if ((dev
= (struct mtd_device
*)malloc(sizeof(struct mtd_device
))) == NULL
) {
1029 printf("out of memory\n");
1032 memset(dev
, 0, sizeof(struct mtd_device
));
1034 dev
->num_parts
= 0; /* part_sort_add increments num_parts */
1035 INIT_LIST_HEAD(&dev
->parts
);
1036 INIT_LIST_HEAD(&dev
->link
);
1038 /* move partitions from tmp_list to dev->parts */
1039 list_for_each_safe(entry
, n
, &tmp_list
) {
1040 part
= list_entry(entry
, struct part_info
, link
);
1042 if (part_sort_add(dev
, part
) != 0) {
1055 * Initialize global device list.
1057 * @return 0 on success, 1 otherwise
1059 static int devices_init(void)
1061 last_parts
[0] = '\0';
1065 return device_delall(&devices
);
1069 * Search global mtdids list and find id of requested type and number.
1071 * @return pointer to the id if it exists, NULL otherwise
1073 static struct mtdids
* id_find(u8 type
, u8 num
)
1075 struct list_head
*entry
;
1078 list_for_each(entry
, &mtdids
) {
1079 id
= list_entry(entry
, struct mtdids
, link
);
1081 if ((id
->type
== type
) && (id
->num
== num
))
1089 * Search global mtdids list and find id of a requested mtd_id.
1091 * Note: first argument is not null terminated.
1093 * @param mtd_id string containing requested mtd_id
1094 * @param mtd_id_len length of supplied mtd_id
1095 * @return pointer to the id if it exists, NULL otherwise
1097 static struct mtdids
* id_find_by_mtd_id(const char *mtd_id
, unsigned int mtd_id_len
)
1099 struct list_head
*entry
;
1102 DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
1103 mtd_id_len
, mtd_id
, mtd_id_len
);
1105 list_for_each(entry
, &mtdids
) {
1106 id
= list_entry(entry
, struct mtdids
, link
);
1108 DEBUGF("entry: '%s' (len = %d)\n",
1109 id
->mtd_id
, strlen(id
->mtd_id
));
1111 if (mtd_id_len
!= strlen(id
->mtd_id
))
1113 if (strncmp(id
->mtd_id
, mtd_id
, mtd_id_len
) == 0)
1119 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1122 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
1123 * return device type and number.
1125 * @param id string describing device id
1126 * @param ret_id output pointer to next char after parse completes (output)
1127 * @param dev_type parsed device type (output)
1128 * @param dev_num parsed device number (output)
1129 * @return 0 on success, 1 otherwise
1131 int id_parse(const char *id
, const char **ret_id
, u8
*dev_type
, u8
*dev_num
)
1136 if (strncmp(p
, "nand", 4) == 0) {
1137 *dev_type
= MTD_DEV_TYPE_NAND
;
1139 } else if (strncmp(p
, "nor", 3) == 0) {
1140 *dev_type
= MTD_DEV_TYPE_NOR
;
1142 } else if (strncmp(p
, "onenand", 7) == 0) {
1143 *dev_type
= MTD_DEV_TYPE_ONENAND
;
1146 printf("incorrect device type in %s\n", id
);
1151 printf("incorrect device number in %s\n", id
);
1155 *dev_num
= simple_strtoul(p
, (char **)&p
, 0);
1161 #ifdef CONFIG_JFFS2_CMDLINE
1163 * Process all devices and generate corresponding mtdparts string describing
1164 * all partitions on all devices.
1166 * @param buf output buffer holding generated mtdparts string (output)
1167 * @param buflen buffer size
1168 * @return 0 on success, 1 otherwise
1170 static int generate_mtdparts(char *buf
, u32 buflen
)
1172 struct list_head
*pentry
, *dentry
;
1173 struct mtd_device
*dev
;
1174 struct part_info
*part
, *prev_part
;
1177 u32 size
, offset
, len
, part_cnt
;
1178 u32 maxlen
= buflen
- 1;
1180 DEBUGF("--- generate_mtdparts ---\n");
1182 if (list_empty(&devices
)) {
1187 sprintf(p
, "mtdparts=");
1190 list_for_each(dentry
, &devices
) {
1191 dev
= list_entry(dentry
, struct mtd_device
, link
);
1194 len
= strlen(dev
->id
->mtd_id
) + 1;
1197 memcpy(p
, dev
->id
->mtd_id
, len
- 1);
1202 /* format partitions */
1205 list_for_each(pentry
, &dev
->parts
) {
1206 part
= list_entry(pentry
, struct part_info
, link
);
1208 offset
= part
->offset
;
1211 /* partition size */
1212 memsize_format(tmpbuf
, size
);
1213 len
= strlen(tmpbuf
);
1216 memcpy(p
, tmpbuf
, len
);
1221 /* add offset only when there is a gap between
1223 if ((!prev_part
&& (offset
!= 0)) ||
1224 (prev_part
&& ((prev_part
->offset
+ prev_part
->size
) != part
->offset
))) {
1226 memsize_format(tmpbuf
, offset
);
1227 len
= strlen(tmpbuf
) + 1;
1231 memcpy(p
, tmpbuf
, len
- 1);
1236 /* copy name only if user supplied */
1237 if(!part
->auto_name
) {
1238 len
= strlen(part
->name
) + 2;
1243 memcpy(p
, part
->name
, len
- 2);
1250 if (part
->mask_flags
&& MTD_WRITEABLE_CMD
) {
1259 /* print ',' separator if there are other partitions
1261 if (dev
->num_parts
> part_cnt
) {
1269 /* print ';' separator if there are other devices following */
1270 if (dentry
->next
!= &devices
) {
1278 /* we still have at least one char left, as we decremented maxlen at
1285 last_parts
[0] = '\0';
1290 * Call generate_mtdparts to process all devices and generate corresponding
1291 * mtdparts string, save it in mtdparts environment variable.
1293 * @param buf output buffer holding generated mtdparts string (output)
1294 * @param buflen buffer size
1295 * @return 0 on success, 1 otherwise
1297 static int generate_mtdparts_save(char *buf
, u32 buflen
)
1301 ret
= generate_mtdparts(buf
, buflen
);
1303 if ((buf
[0] != '\0') && (ret
== 0))
1304 setenv("mtdparts", buf
);
1306 setenv("mtdparts", NULL
);
1312 * Format and print out a partition list for each device from global device
1315 static void list_partitions(void)
1317 struct list_head
*dentry
, *pentry
;
1318 struct part_info
*part
;
1319 struct mtd_device
*dev
;
1322 DEBUGF("\n---list_partitions---\n");
1323 list_for_each(dentry
, &devices
) {
1324 dev
= list_entry(dentry
, struct mtd_device
, link
);
1325 printf("\ndevice %s%d <%s>, # parts = %d\n",
1326 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
,
1327 dev
->id
->mtd_id
, dev
->num_parts
);
1328 printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
1330 /* list partitions for given device */
1332 list_for_each(pentry
, &dev
->parts
) {
1333 part
= list_entry(pentry
, struct part_info
, link
);
1334 printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
1335 part_num
, part
->name
, part
->size
,
1336 part
->offset
, part
->mask_flags
);
1341 if (list_empty(&devices
))
1342 printf("no partitions defined\n");
1344 /* current_dev is not NULL only when we have non empty device list */
1346 part
= jffs2_part_info(current_dev
, current_partnum
);
1348 printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
1349 MTD_DEV_TYPE(current_dev
->id
->type
),
1350 current_dev
->id
->num
, current_partnum
,
1351 part
->name
, part
->size
, part
->offset
);
1353 printf("could not get current partition info\n\n");
1357 printf("\ndefaults:\n");
1358 printf("mtdids : %s\n", mtdids_default
);
1359 printf("mtdparts: %s\n", mtdparts_default
);
1363 * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
1364 * corresponding device and verify partition number.
1366 * @param id string describing device and partition or partition name
1367 * @param dev pointer to the requested device (output)
1368 * @param part_num verified partition number (output)
1369 * @param part pointer to requested partition (output)
1370 * @return 0 on success, 1 otherwise
1372 int find_dev_and_part(const char *id
, struct mtd_device
**dev
,
1373 u8
*part_num
, struct part_info
**part
)
1375 struct list_head
*dentry
, *pentry
;
1376 u8 type
, dnum
, pnum
;
1379 DEBUGF("--- find_dev_and_part ---\nid = %s\n", id
);
1381 list_for_each(dentry
, &devices
) {
1383 *dev
= list_entry(dentry
, struct mtd_device
, link
);
1384 list_for_each(pentry
, &(*dev
)->parts
) {
1385 *part
= list_entry(pentry
, struct part_info
, link
);
1386 if (strcmp((*part
)->name
, id
) == 0)
1397 if (id_parse(p
, &p
, &type
, &dnum
) != 0)
1400 if ((*p
++ != ',') || (*p
== '\0')) {
1401 printf("no partition number specified\n");
1404 pnum
= simple_strtoul(p
, (char **)&p
, 0);
1406 printf("unexpected trailing character '%c'\n", *p
);
1410 if ((*dev
= device_find(type
, dnum
)) == NULL
) {
1411 printf("no such device %s%d\n", MTD_DEV_TYPE(type
), dnum
);
1415 if ((*part
= jffs2_part_info(*dev
, pnum
)) == NULL
) {
1416 printf("no such partition\n");
1427 * Find and delete partition. For partition id format see find_dev_and_part().
1429 * @param id string describing device and partition
1430 * @return 0 on success, 1 otherwise
1432 static int delete_partition(const char *id
)
1435 struct mtd_device
*dev
;
1436 struct part_info
*part
;
1438 if (find_dev_and_part(id
, &dev
, &pnum
, &part
) == 0) {
1440 DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
1441 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
,
1442 part
->name
, part
->size
, part
->offset
);
1444 if (part_del(dev
, part
) != 0)
1447 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
1448 printf("generated mtdparts too long, reseting to null\n");
1454 printf("partition %s not found\n", id
);
1459 * Accept character string describing mtd partitions and call device_parse()
1460 * for each entry. Add created devices to the global devices list.
1462 * @param mtdparts string specifing mtd partitions
1463 * @return 0 on success, 1 otherwise
1465 static int parse_mtdparts(const char *const mtdparts
)
1467 const char *p
= mtdparts
;
1468 struct mtd_device
*dev
;
1471 DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p
);
1473 /* delete all devices and partitions */
1474 if (devices_init() != 0) {
1475 printf("could not initialise device list\n");
1479 /* re-read 'mtdparts' variable, devices_init may be updating env */
1480 p
= getenv("mtdparts");
1482 if (strncmp(p
, "mtdparts=", 9) != 0) {
1483 printf("mtdparts variable doesn't start with 'mtdparts='\n");
1488 while (p
&& (*p
!= '\0')) {
1490 if ((device_parse(p
, &p
, &dev
) != 0) || (!dev
))
1493 DEBUGF("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
1494 dev
->id
->num
, dev
->id
->mtd_id
);
1496 /* check if parsed device is already on the list */
1497 if (device_find(dev
->id
->type
, dev
->id
->num
) != NULL
) {
1498 printf("device %s%d redefined, please correct mtdparts variable\n",
1499 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
);
1503 list_add_tail(&dev
->link
, &devices
);
1507 device_delall(&devices
);
1515 * Parse provided string describing mtdids mapping (see file header for mtdids
1516 * variable format). Allocate memory for each entry and add all found entries
1517 * to the global mtdids list.
1519 * @param ids mapping string
1520 * @return 0 on success, 1 otherwise
1522 static int parse_mtdids(const char *const ids
)
1524 const char *p
= ids
;
1528 struct list_head
*entry
, *n
;
1529 struct mtdids
*id_tmp
;
1534 DEBUGF("\n---parse_mtdids---\nmtdids = %s\n\n", ids
);
1536 /* clean global mtdids list */
1537 list_for_each_safe(entry
, n
, &mtdids
) {
1538 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1539 DEBUGF("mtdids del: %d %d\n", id_tmp
->type
, id_tmp
->num
);
1544 INIT_LIST_HEAD(&mtdids
);
1546 while(p
&& (*p
!= '\0')) {
1549 /* parse 'nor'|'nand'|'onenand'<dev-num> */
1550 if (id_parse(p
, &p
, &type
, &num
) != 0)
1554 printf("mtdids: incorrect <dev-num>\n");
1559 /* check if requested device exists */
1560 if (device_validate(type
, num
, &size
) != 0)
1563 /* locate <mtd-id> */
1565 if ((p
= strchr(mtd_id
, ',')) != NULL
) {
1566 mtd_id_len
= p
- mtd_id
+ 1;
1569 mtd_id_len
= strlen(mtd_id
) + 1;
1571 if (mtd_id_len
== 0) {
1572 printf("mtdids: no <mtd-id> identifier\n");
1576 /* check if this id is already on the list */
1577 int double_entry
= 0;
1578 list_for_each(entry
, &mtdids
) {
1579 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1580 if ((id_tmp
->type
== type
) && (id_tmp
->num
== num
)) {
1586 printf("device id %s%d redefined, please correct mtdids variable\n",
1587 MTD_DEV_TYPE(type
), num
);
1591 /* allocate mtdids structure */
1592 if (!(id
= (struct mtdids
*)malloc(sizeof(struct mtdids
) + mtd_id_len
))) {
1593 printf("out of memory\n");
1596 memset(id
, 0, sizeof(struct mtdids
) + mtd_id_len
);
1600 id
->mtd_id
= (char *)(id
+ 1);
1601 strncpy(id
->mtd_id
, mtd_id
, mtd_id_len
- 1);
1602 id
->mtd_id
[mtd_id_len
- 1] = '\0';
1603 INIT_LIST_HEAD(&id
->link
);
1605 DEBUGF("+ id %s%d\t%16d bytes\t%s\n",
1606 MTD_DEV_TYPE(id
->type
), id
->num
,
1607 id
->size
, id
->mtd_id
);
1609 list_add_tail(&id
->link
, &mtdids
);
1613 /* clean mtdids list and free allocated memory */
1614 list_for_each_safe(entry
, n
, &mtdids
) {
1615 id_tmp
= list_entry(entry
, struct mtdids
, link
);
1626 * Parse and initialize global mtdids mapping and create global
1627 * device/partition list.
1629 * @return 0 on success, 1 otherwise
1631 int mtdparts_init(void)
1633 static int initialized
= 0;
1634 const char *ids
, *parts
;
1635 const char *current_partition
;
1637 char tmp_ep
[PARTITION_MAXLEN
];
1639 DEBUGF("\n---mtdparts_init---\n");
1641 INIT_LIST_HEAD(&mtdids
);
1642 INIT_LIST_HEAD(&devices
);
1643 memset(last_ids
, 0, MTDIDS_MAXLEN
);
1644 memset(last_parts
, 0, MTDPARTS_MAXLEN
);
1645 memset(last_partition
, 0, PARTITION_MAXLEN
);
1650 ids
= getenv("mtdids");
1651 parts
= getenv("mtdparts");
1652 current_partition
= getenv("partition");
1654 /* save it for later parsing, cannot rely on current partition pointer
1655 * as 'partition' variable may be updated during init */
1657 if (current_partition
)
1658 strncpy(tmp_ep
, current_partition
, PARTITION_MAXLEN
);
1660 DEBUGF("last_ids : %s\n", last_ids
);
1661 DEBUGF("env_ids : %s\n", ids
);
1662 DEBUGF("last_parts: %s\n", last_parts
);
1663 DEBUGF("env_parts : %s\n\n", parts
);
1665 DEBUGF("last_partition : %s\n", last_partition
);
1666 DEBUGF("env_partition : %s\n", current_partition
);
1668 /* if mtdids varible is empty try to use defaults */
1670 if (mtdids_default
) {
1671 DEBUGF("mtdids variable not defined, using default\n");
1672 ids
= mtdids_default
;
1673 setenv("mtdids", (char *)ids
);
1675 printf("mtdids not defined, no default present\n");
1679 if (strlen(ids
) > MTDIDS_MAXLEN
- 1) {
1680 printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN
);
1684 /* do no try to use defaults when mtdparts variable is not defined,
1685 * just check the length */
1687 printf("mtdparts variable not set, see 'help mtdparts'\n");
1689 if (parts
&& (strlen(parts
) > MTDPARTS_MAXLEN
- 1)) {
1690 printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN
);
1694 /* check if we have already parsed those mtdids */
1695 if ((last_ids
[0] != '\0') && (strcmp(last_ids
, ids
) == 0)) {
1700 if (parse_mtdids(ids
) != 0) {
1705 /* ok it's good, save new ids */
1706 strncpy(last_ids
, ids
, MTDIDS_MAXLEN
);
1709 /* parse partitions if either mtdparts or mtdids were updated */
1710 if (parts
&& ((last_parts
[0] == '\0') || ((strcmp(last_parts
, parts
) != 0)) || ids_changed
)) {
1711 if (parse_mtdparts(parts
) != 0)
1714 if (list_empty(&devices
)) {
1715 printf("mtdparts_init: no valid partitions\n");
1719 /* ok it's good, save new parts */
1720 strncpy(last_parts
, parts
, MTDPARTS_MAXLEN
);
1722 /* reset first partition from first dev from the list as current */
1723 current_dev
= list_entry(devices
.next
, struct mtd_device
, link
);
1724 current_partnum
= 0;
1727 DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
1728 MTD_DEV_TYPE(current_dev
->id
->type
),
1729 current_dev
->id
->num
, current_partnum
);
1732 /* mtdparts variable was reset to NULL, delete all devices/partitions */
1733 if (!parts
&& (last_parts
[0] != '\0'))
1734 return devices_init();
1736 /* do not process current partition if mtdparts variable is null */
1740 /* is current partition set in environment? if so, use it */
1741 if ((tmp_ep
[0] != '\0') && (strcmp(tmp_ep
, last_partition
) != 0)) {
1742 struct part_info
*p
;
1743 struct mtd_device
*cdev
;
1746 DEBUGF("--- getting current partition: %s\n", tmp_ep
);
1748 if (find_dev_and_part(tmp_ep
, &cdev
, &pnum
, &p
) == 0) {
1750 current_partnum
= pnum
;
1753 } else if (getenv("partition") == NULL
) {
1754 DEBUGF("no partition variable set, setting...\n");
1760 #else /* #ifdef CONFIG_JFFS2_CMDLINE */
1762 * 'Static' version of command line mtdparts_init() routine. Single partition on
1763 * a single device configuration.
1767 * Parse and initialize global mtdids mapping and create global
1768 * device/partition list.
1770 * @return 0 on success, 1 otherwise
1772 int mtdparts_init(void)
1774 static int initialized
= 0;
1778 DEBUGF("\n---mtdparts_init---\n");
1781 struct part_info
*part
;
1784 current_dev
= (struct mtd_device
*)
1785 malloc(sizeof(struct mtd_device
) +
1786 sizeof(struct part_info
) +
1787 sizeof(struct mtdids
));
1789 printf("out of memory\n");
1792 memset(current_dev
, 0, sizeof(struct mtd_device
) +
1793 sizeof(struct part_info
) + sizeof(struct mtdids
));
1795 id
= (struct mtdids
*)(current_dev
+ 1);
1796 part
= (struct part_info
*)(id
+ 1);
1799 id
->mtd_id
= "single part";
1801 #if defined(CONFIG_JFFS2_DEV)
1802 dev_name
= CONFIG_JFFS2_DEV
;
1807 if ((id_parse(dev_name
, NULL
, &id
->type
, &id
->num
) != 0) ||
1808 (device_validate(id
->type
, id
->num
, &size
) != 0)) {
1809 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id
->type
), id
->num
);
1814 INIT_LIST_HEAD(&id
->link
);
1816 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
1817 id
->type
, id
->num
, id
->size
, id
->mtd_id
);
1820 part
->name
= "static";
1821 part
->auto_name
= 0;
1823 #if defined(CONFIG_JFFS2_PART_SIZE)
1824 part
->size
= CONFIG_JFFS2_PART_SIZE
;
1826 part
->size
= SIZE_REMAINING
;
1829 #if defined(CONFIG_JFFS2_PART_OFFSET)
1830 part
->offset
= CONFIG_JFFS2_PART_OFFSET
;
1832 part
->offset
= 0x00000000;
1835 part
->dev
= current_dev
;
1836 INIT_LIST_HEAD(&part
->link
);
1838 /* recalculate size if needed */
1839 if (part
->size
== SIZE_REMAINING
)
1840 part
->size
= id
->size
- part
->offset
;
1842 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
1843 part
->name
, part
->size
, part
->offset
);
1846 current_dev
->id
= id
;
1847 INIT_LIST_HEAD(¤t_dev
->link
);
1848 current_dev
->num_parts
= 1;
1849 INIT_LIST_HEAD(¤t_dev
->parts
);
1850 list_add(&part
->link
, ¤t_dev
->parts
);
1855 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
1858 * Return pointer to the partition of a requested number from a requested
1861 * @param dev device that is to be searched for a partition
1862 * @param part_num requested partition number
1863 * @return pointer to the part_info, NULL otherwise
1865 static struct part_info
* jffs2_part_info(struct mtd_device
*dev
, unsigned int part_num
)
1867 struct list_head
*entry
;
1868 struct part_info
*part
;
1874 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
1875 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1876 dev
->id
->num
, dev
->id
->mtd_id
);
1878 if (part_num
>= dev
->num_parts
) {
1879 printf("invalid partition number %d for device %s%d (%s)\n",
1880 part_num
, MTD_DEV_TYPE(dev
->id
->type
),
1881 dev
->id
->num
, dev
->id
->mtd_id
);
1885 /* locate partition number, return it */
1887 list_for_each(entry
, &dev
->parts
) {
1888 part
= list_entry(entry
, struct part_info
, link
);
1890 if (part_num
== num
++) {
1898 /***************************************************/
1899 /* U-boot commands */
1900 /***************************************************/
1903 * Routine implementing fsload u-boot command. This routine tries to load
1904 * a requested file from jffs2/cramfs filesystem on a current partition.
1906 * @param cmdtp command internal data
1907 * @param flag command flag
1908 * @param argc number of arguments supplied to the command
1909 * @param argv arguments list
1910 * @return 0 on success, 1 otherwise
1912 int do_jffs2_fsload(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1917 struct part_info
*part
;
1918 ulong offset
= load_addr
;
1920 /* pre-set Boot file name */
1921 if ((filename
= getenv("bootfile")) == NULL
) {
1922 filename
= "uImage";
1929 offset
= simple_strtoul(argv
[1], NULL
, 16);
1934 /* make sure we are in sync with env variables */
1935 if (mtdparts_init() !=0)
1938 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1940 /* check partition type for cramfs */
1941 fsname
= (cramfs_check(part
) ? "CRAMFS" : "JFFS2");
1942 printf("### %s loading '%s' to 0x%lx\n", fsname
, filename
, offset
);
1944 if (cramfs_check(part
)) {
1945 size
= cramfs_load ((char *) offset
, part
, filename
);
1947 /* if this is not cramfs assume jffs2 */
1948 size
= jffs2_1pass_load((char *)offset
, part
, filename
);
1953 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
1954 fsname
, size
, offset
);
1955 sprintf(buf
, "%x", size
);
1956 setenv("filesize", buf
);
1958 printf("### %s LOAD ERROR<%x> for %s!\n", fsname
, size
, filename
);
1967 * Routine implementing u-boot ls command which lists content of a given
1968 * directory on a current partition.
1970 * @param cmdtp command internal data
1971 * @param flag command flag
1972 * @param argc number of arguments supplied to the command
1973 * @param argv arguments list
1974 * @return 0 on success, 1 otherwise
1976 int do_jffs2_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
1978 char *filename
= "/";
1980 struct part_info
*part
;
1985 /* make sure we are in sync with env variables */
1986 if (mtdparts_init() !=0)
1989 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
1991 /* check partition type for cramfs */
1992 if (cramfs_check(part
)) {
1993 ret
= cramfs_ls (part
, filename
);
1995 /* if this is not cramfs assume jffs2 */
1996 ret
= jffs2_1pass_ls(part
, filename
);
2005 * Routine implementing u-boot fsinfo command. This routine prints out
2006 * miscellaneous filesystem informations/statistics.
2008 * @param cmdtp command internal data
2009 * @param flag command flag
2010 * @param argc number of arguments supplied to the command
2011 * @param argv arguments list
2012 * @return 0 on success, 1 otherwise
2014 int do_jffs2_fsinfo(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
2016 struct part_info
*part
;
2020 /* make sure we are in sync with env variables */
2021 if (mtdparts_init() !=0)
2024 if ((part
= jffs2_part_info(current_dev
, current_partnum
))){
2026 /* check partition type for cramfs */
2027 fsname
= (cramfs_check(part
) ? "CRAMFS" : "JFFS2");
2028 printf("### filesystem type is %s\n", fsname
);
2030 if (cramfs_check(part
)) {
2031 ret
= cramfs_info (part
);
2033 /* if this is not cramfs assume jffs2 */
2034 ret
= jffs2_1pass_info(part
);
2042 /* command line only */
2043 #ifdef CONFIG_JFFS2_CMDLINE
2045 * Routine implementing u-boot chpart command. Sets new current partition based
2046 * on the user supplied partition id. For partition id format see find_dev_and_part().
2048 * @param cmdtp command internal data
2049 * @param flag command flag
2050 * @param argc number of arguments supplied to the command
2051 * @param argv arguments list
2052 * @return 0 on success, 1 otherwise
2054 int do_jffs2_chpart(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
2056 /* command line only */
2057 struct mtd_device
*dev
;
2058 struct part_info
*part
;
2061 if (mtdparts_init() !=0)
2065 printf("no partition id specified\n");
2069 if (find_dev_and_part(argv
[1], &dev
, &pnum
, &part
) != 0)
2073 current_partnum
= pnum
;
2076 printf("partition changed to %s%d,%d\n",
2077 MTD_DEV_TYPE(dev
->id
->type
), dev
->id
->num
, pnum
);
2083 * Routine implementing u-boot mtdparts command. Initialize/update default global
2084 * partition list and process user partition request (list, add, del).
2086 * @param cmdtp command internal data
2087 * @param flag command flag
2088 * @param argc number of arguments supplied to the command
2089 * @param argv arguments list
2090 * @return 0 on success, 1 otherwise
2092 int do_jffs2_mtdparts(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
2095 if (strcmp(argv
[1], "default") == 0) {
2096 setenv("mtdids", (char *)mtdids_default
);
2097 setenv("mtdparts", (char *)mtdparts_default
);
2098 setenv("partition", NULL
);
2102 } else if (strcmp(argv
[1], "delall") == 0) {
2103 /* this may be the first run, initialize lists if needed */
2106 setenv("mtdparts", NULL
);
2108 /* devices_init() calls current_save() */
2109 return devices_init();
2113 /* make sure we are in sync with env variables */
2114 if (mtdparts_init() != 0)
2122 /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
2123 if (((argc
== 5) || (argc
== 6)) && (strcmp(argv
[1], "add") == 0)) {
2124 #define PART_ADD_DESC_MAXLEN 64
2125 char tmpbuf
[PART_ADD_DESC_MAXLEN
];
2127 struct mtd_device
*dev
;
2128 struct mtd_device
*dev_tmp
;
2130 struct part_info
*p
;
2132 if (id_parse(argv
[2], NULL
, &type
, &num
) != 0)
2135 if ((id
= id_find(type
, num
)) == NULL
) {
2136 printf("no such device %s defined in mtdids variable\n", argv
[2]);
2140 len
= strlen(id
->mtd_id
) + 1; /* 'mtd_id:' */
2141 len
+= strlen(argv
[3]); /* size@offset */
2142 len
+= strlen(argv
[4]) + 2; /* '(' name ')' */
2143 if (argv
[5] && (strlen(argv
[5]) == 2))
2144 len
+= 2; /* 'ro' */
2146 if (len
>= PART_ADD_DESC_MAXLEN
) {
2147 printf("too long partition description\n");
2150 sprintf(tmpbuf
, "%s:%s(%s)%s",
2151 id
->mtd_id
, argv
[3], argv
[4], argv
[5] ? argv
[5] : "");
2152 DEBUGF("add tmpbuf: %s\n", tmpbuf
);
2154 if ((device_parse(tmpbuf
, NULL
, &dev
) != 0) || (!dev
))
2157 DEBUGF("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev
->id
->type
),
2158 dev
->id
->num
, dev
->id
->mtd_id
);
2160 if ((dev_tmp
= device_find(dev
->id
->type
, dev
->id
->num
)) == NULL
) {
2163 /* merge new partition with existing ones*/
2164 p
= list_entry(dev
->parts
.next
, struct part_info
, link
);
2165 if (part_add(dev_tmp
, p
) != 0) {
2171 if (generate_mtdparts_save(last_parts
, MTDPARTS_MAXLEN
) != 0) {
2172 printf("generated mtdparts too long, reseting to null\n");
2179 /* mtdparts del part-id */
2180 if ((argc
== 3) && (strcmp(argv
[1], "del") == 0)) {
2181 DEBUGF("del: part-id = %s\n", argv
[2]);
2183 return delete_partition(argv
[2]);
2186 printf ("Usage:\n%s\n", cmdtp
->usage
);
2189 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2191 /***************************************************/
2193 fsload
, 3, 0, do_jffs2_fsload
,
2194 "fsload\t- load binary file from a filesystem image\n",
2195 "[ off ] [ filename ]\n"
2196 " - load binary file from flash bank\n"
2197 " with offset 'off'\n"
2200 ls
, 2, 1, do_jffs2_ls
,
2201 "ls\t- list files in a directory (default /)\n",
2203 " - list files in a directory.\n"
2207 fsinfo
, 1, 1, do_jffs2_fsinfo
,
2208 "fsinfo\t- print information about filesystems\n",
2209 " - print information about filesystems\n"
2212 #ifdef CONFIG_JFFS2_CMDLINE
2214 chpart
, 2, 0, do_jffs2_chpart
,
2215 "chpart\t- change active partition\n",
2217 " - change active partition (e.g. part-id = nand0,1)\n"
2221 mtdparts
, 6, 0, do_jffs2_mtdparts
,
2222 "mtdparts- define flash/nand partitions\n",
2224 " - list partition table\n"
2226 " - delete all partitions\n"
2227 "mtdparts del part-id\n"
2228 " - delete partition (e.g. part-id = nand0,1)\n"
2229 "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
2230 " - add partition\n"
2231 "mtdparts default\n"
2232 " - reset partition table to defaults\n\n"
2234 "this command uses three environment variables:\n\n"
2235 "'partition' - keeps current partition identifier\n\n"
2236 "partition := <part-id>\n"
2237 "<part-id> := <dev-id>,part_num\n\n"
2238 "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
2239 "mtdids=<idmap>[,<idmap>,...]\n\n"
2240 "<idmap> := <dev-id>=<mtd-id>\n"
2241 "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
2242 "<dev-num> := mtd device number, 0...\n"
2243 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
2244 "'mtdparts' - partition list\n\n"
2245 "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
2246 "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
2247 "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
2248 "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
2249 "<size> := standard linux memsize OR '-' to denote all remaining space\n"
2250 "<offset> := partition start offset within the device\n"
2251 "<name> := '(' NAME ')'\n"
2252 "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)\n"
2254 #endif /* #ifdef CONFIG_JFFS2_CMDLINE */
2256 /***************************************************/