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 #include <linux/mtd/nand.h>
103 #if defined(CONFIG_CMD_ONENAND)
104 #include <linux/mtd/mtd.h>
105 #include <linux/mtd/onenand.h>
106 #include <onenand_uboot.h>
109 /* enable/disable debugging messages */
114 # define DEBUGF(fmt, args...) printf(fmt ,##args)
116 # define DEBUGF(fmt, args...)
119 /* special size referring to all the remaining space in a partition */
120 #define SIZE_REMAINING 0xFFFFFFFF
122 /* special offset value, it is used when not provided by user
124 * this value is used temporarily during parsing, later such offests
125 * are recalculated */
126 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
128 /* minimum partition size */
129 #define MIN_PART_SIZE 4096
131 /* this flag needs to be set in part_info struct mask_flags
132 * field for read-only partitions */
133 #define MTD_WRITEABLE_CMD 1
135 /* current active device and partition number */
136 #ifdef CONFIG_CMD_MTDPARTS
137 /* Use the ones declared in cmd_mtdparts.c */
138 extern struct mtd_device *current_mtd_dev;
139 extern u8 current_mtd_partnum;
142 struct mtd_device *current_mtd_dev = NULL;
143 u8 current_mtd_partnum = 0;
146 #if defined(CONFIG_CMD_CRAMFS)
147 extern int cramfs_check (struct part_info *info);
148 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
149 extern int cramfs_ls (struct part_info *info, char *filename);
150 extern int cramfs_info (struct part_info *info);
152 /* defining empty macros for function names is ugly but avoids ifdef clutter
153 * all over the code */
154 #define cramfs_check(x) (0)
155 #define cramfs_load(x,y,z) (-1)
156 #define cramfs_ls(x,y) (0)
157 #define cramfs_info(x) (0)
160 #ifndef CONFIG_CMD_MTDPARTS
162 * Check device number to be within valid range for given device type.
164 * @param dev device to validate
165 * @return 0 if device is valid, 1 otherwise
167 static int mtd_device_validate(u8 type, u8 num, u32 *size)
169 if (type == MTD_DEV_TYPE_NOR) {
170 #if defined(CONFIG_CMD_FLASH)
171 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
172 extern flash_info_t flash_info[];
173 *size = flash_info[num].size;
178 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
179 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
181 printf("support for FLASH devices not present\n");
183 } else if (type == MTD_DEV_TYPE_NAND) {
184 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
185 if (num < CONFIG_SYS_MAX_NAND_DEVICE) {
186 *size = nand_info[num].size;
190 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
191 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
193 printf("support for NAND devices not present\n");
195 } else if (type == MTD_DEV_TYPE_ONENAND) {
196 #if defined(CONFIG_CMD_ONENAND)
197 *size = onenand_mtd.size;
200 printf("support for OneNAND devices not present\n");
203 printf("Unknown defice type %d\n", type);
209 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
210 * return device type and number.
212 * @param id string describing device id
213 * @param ret_id output pointer to next char after parse completes (output)
214 * @param dev_type parsed device type (output)
215 * @param dev_num parsed device number (output)
216 * @return 0 on success, 1 otherwise
218 static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
223 if (strncmp(p, "nand", 4) == 0) {
224 *dev_type = MTD_DEV_TYPE_NAND;
226 } else if (strncmp(p, "nor", 3) == 0) {
227 *dev_type = MTD_DEV_TYPE_NOR;
229 } else if (strncmp(p, "onenand", 7) == 0) {
230 *dev_type = MTD_DEV_TYPE_ONENAND;
233 printf("incorrect device type in %s\n", id);
238 printf("incorrect device number in %s\n", id);
242 *dev_num = simple_strtoul(p, (char **)&p, 0);
249 * 'Static' version of command line mtdparts_init() routine. Single partition on
250 * a single device configuration.
254 * Calculate sector size.
256 * @return sector size
258 static inline u32 get_part_sector_size_nand(struct mtdids *id)
260 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
263 nand = &nand_info[id->num];
265 return nand->erasesize;
272 static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
274 #if defined(CONFIG_CMD_FLASH)
275 extern flash_info_t flash_info[];
277 u32 end_phys, start_phys, sector_size = 0, size = 0;
281 flash = &flash_info[id->num];
283 start_phys = flash->start[0] + part->offset;
284 end_phys = start_phys + part->size;
286 for (i = 0; i < flash->sector_count; i++) {
287 if (flash->start[i] >= end_phys)
290 if (flash->start[i] >= start_phys) {
291 if (i == flash->sector_count - 1) {
292 size = flash->start[0] + flash->size - flash->start[i];
294 size = flash->start[i+1] - flash->start[i];
297 if (sector_size < size)
309 static inline u32 get_part_sector_size_onenand(void)
311 #if defined(CONFIG_CMD_ONENAND)
312 struct mtd_info *mtd;
316 return mtd->erasesize;
323 static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
325 if (id->type == MTD_DEV_TYPE_NAND)
326 return get_part_sector_size_nand(id);
327 else if (id->type == MTD_DEV_TYPE_NOR)
328 return get_part_sector_size_nor(id, part);
329 else if (id->type == MTD_DEV_TYPE_ONENAND)
330 return get_part_sector_size_onenand();
332 DEBUGF("Error: Unknown device type.\n");
338 * Parse and initialize global mtdids mapping and create global
339 * device/partition list.
341 * 'Static' version of command line mtdparts_init() routine. Single partition on
342 * a single device configuration.
344 * @return 0 on success, 1 otherwise
346 int mtdparts_init(void)
348 static int initialized = 0;
352 DEBUGF("\n---mtdparts_init---\n");
355 struct part_info *part;
358 current_mtd_dev = (struct mtd_device *)
359 malloc(sizeof(struct mtd_device) +
360 sizeof(struct part_info) +
361 sizeof(struct mtdids));
362 if (!current_mtd_dev) {
363 printf("out of memory\n");
366 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
367 sizeof(struct part_info) + sizeof(struct mtdids));
369 id = (struct mtdids *)(current_mtd_dev + 1);
370 part = (struct part_info *)(id + 1);
373 id->mtd_id = "single part";
375 #if defined(CONFIG_JFFS2_DEV)
376 dev_name = CONFIG_JFFS2_DEV;
381 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
382 (mtd_device_validate(id->type, id->num, &size) != 0)) {
383 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
384 free(current_mtd_dev);
388 INIT_LIST_HEAD(&id->link);
390 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
391 id->type, id->num, id->size, id->mtd_id);
394 part->name = "static";
397 #if defined(CONFIG_JFFS2_PART_SIZE)
398 part->size = CONFIG_JFFS2_PART_SIZE;
400 part->size = SIZE_REMAINING;
403 #if defined(CONFIG_JFFS2_PART_OFFSET)
404 part->offset = CONFIG_JFFS2_PART_OFFSET;
406 part->offset = 0x00000000;
409 part->dev = current_mtd_dev;
410 INIT_LIST_HEAD(&part->link);
412 /* recalculate size if needed */
413 if (part->size == SIZE_REMAINING)
414 part->size = id->size - part->offset;
416 part->sector_size = get_part_sector_size(id, part);
418 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
419 part->name, part->size, part->offset);
422 current_mtd_dev->id = id;
423 INIT_LIST_HEAD(¤t_mtd_dev->link);
424 current_mtd_dev->num_parts = 1;
425 INIT_LIST_HEAD(¤t_mtd_dev->parts);
426 list_add(&part->link, ¤t_mtd_dev->parts);
431 #endif /* #ifndef CONFIG_CMD_MTDPARTS */
434 * Return pointer to the partition of a requested number from a requested
437 * @param dev device that is to be searched for a partition
438 * @param part_num requested partition number
439 * @return pointer to the part_info, NULL otherwise
441 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
443 struct list_head *entry;
444 struct part_info *part;
450 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
451 part_num, MTD_DEV_TYPE(dev->id->type),
452 dev->id->num, dev->id->mtd_id);
454 if (part_num >= dev->num_parts) {
455 printf("invalid partition number %d for device %s%d (%s)\n",
456 part_num, MTD_DEV_TYPE(dev->id->type),
457 dev->id->num, dev->id->mtd_id);
461 /* locate partition number, return it */
463 list_for_each(entry, &dev->parts) {
464 part = list_entry(entry, struct part_info, link);
466 if (part_num == num++) {
474 /***************************************************/
475 /* U-boot commands */
476 /***************************************************/
479 * Routine implementing fsload u-boot command. This routine tries to load
480 * a requested file from jffs2/cramfs filesystem on a current partition.
482 * @param cmdtp command internal data
483 * @param flag command flag
484 * @param argc number of arguments supplied to the command
485 * @param argv arguments list
486 * @return 0 on success, 1 otherwise
488 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
493 struct part_info *part;
494 ulong offset = load_addr;
496 /* pre-set Boot file name */
497 if ((filename = getenv("bootfile")) == NULL) {
505 offset = simple_strtoul(argv[1], NULL, 16);
510 /* make sure we are in sync with env variables */
511 if (mtdparts_init() !=0)
514 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
516 /* check partition type for cramfs */
517 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
518 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
520 if (cramfs_check(part)) {
521 size = cramfs_load ((char *) offset, part, filename);
523 /* if this is not cramfs assume jffs2 */
524 size = jffs2_1pass_load((char *)offset, part, filename);
529 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
530 fsname, size, offset);
531 sprintf(buf, "%x", size);
532 setenv("filesize", buf);
534 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
543 * Routine implementing u-boot ls command which lists content of a given
544 * directory on a current partition.
546 * @param cmdtp command internal data
547 * @param flag command flag
548 * @param argc number of arguments supplied to the command
549 * @param argv arguments list
550 * @return 0 on success, 1 otherwise
552 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
554 char *filename = "/";
556 struct part_info *part;
561 /* make sure we are in sync with env variables */
562 if (mtdparts_init() !=0)
565 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
567 /* check partition type for cramfs */
568 if (cramfs_check(part)) {
569 ret = cramfs_ls (part, filename);
571 /* if this is not cramfs assume jffs2 */
572 ret = jffs2_1pass_ls(part, filename);
581 * Routine implementing u-boot fsinfo command. This routine prints out
582 * miscellaneous filesystem informations/statistics.
584 * @param cmdtp command internal data
585 * @param flag command flag
586 * @param argc number of arguments supplied to the command
587 * @param argv arguments list
588 * @return 0 on success, 1 otherwise
590 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
592 struct part_info *part;
596 /* make sure we are in sync with env variables */
597 if (mtdparts_init() !=0)
600 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
602 /* check partition type for cramfs */
603 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
604 printf("### filesystem type is %s\n", fsname);
606 if (cramfs_check(part)) {
607 ret = cramfs_info (part);
609 /* if this is not cramfs assume jffs2 */
610 ret = jffs2_1pass_info(part);
618 /***************************************************/
620 fsload, 3, 0, do_jffs2_fsload,
621 "load binary file from a filesystem image",
622 "[ off ] [ filename ]\n"
623 " - load binary file from flash bank\n"
627 ls, 2, 1, do_jffs2_ls,
628 "list files in a directory (default /)",
633 fsinfo, 1, 1, do_jffs2_fsinfo,
634 "print information about filesystems",
637 /***************************************************/