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 * SPDX-License-Identifier: GPL-2.0+
25 * Three environment variables are used by the parsing routines:
27 * 'partition' - keeps current partition identifier
29 * partition := <part-id>
30 * <part-id> := <dev-id>,part_num
33 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
35 * mtdids=<idmap>[,<idmap>,...]
37 * <idmap> := <dev-id>=<mtd-id>
38 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
39 * <dev-num> := mtd device number, 0...
40 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
43 * 'mtdparts' - partition list
45 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
47 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
48 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
49 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
50 * <size> := standard linux memsize OR '-' to denote all remaining space
51 * <offset> := partition start offset within the device
52 * <name> := '(' NAME ')'
53 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
56 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
57 * - if the above variables are not set defaults for a given target are used
61 * 1 NOR Flash, with 1 single writable partition:
62 * mtdids=nor0=edb7312-nor
63 * mtdparts=mtdparts=edb7312-nor:-
65 * 1 NOR Flash with 2 partitions, 1 NAND with one
66 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
67 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
72 * JFFS2/CRAMFS support
77 #include <jffs2/jffs2.h>
78 #include <linux/list.h>
79 #include <linux/ctype.h>
80 #include <cramfs/cramfs_fs.h>
82 #if defined(CONFIG_CMD_NAND)
83 #include <linux/mtd/nand.h>
87 #if defined(CONFIG_CMD_ONENAND)
88 #include <linux/mtd/mtd.h>
89 #include <linux/mtd/onenand.h>
90 #include <onenand_uboot.h>
93 /* enable/disable debugging messages */
98 # define DEBUGF(fmt, args...) printf(fmt ,##args)
100 # define DEBUGF(fmt, args...)
103 /* special size referring to all the remaining space in a partition */
104 #define SIZE_REMAINING 0xFFFFFFFF
106 /* special offset value, it is used when not provided by user
108 * this value is used temporarily during parsing, later such offests
109 * are recalculated */
110 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
112 /* minimum partition size */
113 #define MIN_PART_SIZE 4096
115 /* this flag needs to be set in part_info struct mask_flags
116 * field for read-only partitions */
117 #define MTD_WRITEABLE_CMD 1
119 /* current active device and partition number */
120 #ifdef CONFIG_CMD_MTDPARTS
121 /* Use the ones declared in cmd_mtdparts.c */
122 extern struct mtd_device *current_mtd_dev;
123 extern u8 current_mtd_partnum;
126 struct mtd_device *current_mtd_dev = NULL;
127 u8 current_mtd_partnum = 0;
130 #if defined(CONFIG_CMD_CRAMFS)
131 extern int cramfs_check (struct part_info *info);
132 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
133 extern int cramfs_ls (struct part_info *info, char *filename);
134 extern int cramfs_info (struct part_info *info);
136 /* defining empty macros for function names is ugly but avoids ifdef clutter
137 * all over the code */
138 #define cramfs_check(x) (0)
139 #define cramfs_load(x,y,z) (-1)
140 #define cramfs_ls(x,y) (0)
141 #define cramfs_info(x) (0)
144 #ifndef CONFIG_CMD_MTDPARTS
146 * Check device number to be within valid range for given device type.
148 * @param dev device to validate
149 * @return 0 if device is valid, 1 otherwise
151 static int mtd_device_validate(u8 type, u8 num, u32 *size)
153 if (type == MTD_DEV_TYPE_NOR) {
154 #if defined(CONFIG_CMD_FLASH)
155 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
156 extern flash_info_t flash_info[];
157 *size = flash_info[num].size;
162 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
163 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
165 printf("support for FLASH devices not present\n");
167 } else if (type == MTD_DEV_TYPE_NAND) {
168 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
169 struct mtd_info *mtd = get_nand_dev_by_index(num);
175 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
176 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
178 printf("support for NAND devices not present\n");
180 } else if (type == MTD_DEV_TYPE_ONENAND) {
181 #if defined(CONFIG_CMD_ONENAND)
182 *size = onenand_mtd.size;
185 printf("support for OneNAND devices not present\n");
188 printf("Unknown defice type %d\n", type);
194 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
195 * return device type and number.
197 * @param id string describing device id
198 * @param ret_id output pointer to next char after parse completes (output)
199 * @param dev_type parsed device type (output)
200 * @param dev_num parsed device number (output)
201 * @return 0 on success, 1 otherwise
203 static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
208 if (strncmp(p, "nand", 4) == 0) {
209 *dev_type = MTD_DEV_TYPE_NAND;
211 } else if (strncmp(p, "nor", 3) == 0) {
212 *dev_type = MTD_DEV_TYPE_NOR;
214 } else if (strncmp(p, "onenand", 7) == 0) {
215 *dev_type = MTD_DEV_TYPE_ONENAND;
218 printf("incorrect device type in %s\n", id);
223 printf("incorrect device number in %s\n", id);
227 *dev_num = simple_strtoul(p, (char **)&p, 0);
234 * 'Static' version of command line mtdparts_init() routine. Single partition on
235 * a single device configuration.
239 * Calculate sector size.
241 * @return sector size
243 static inline u32 get_part_sector_size_nand(struct mtdids *id)
245 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
246 struct mtd_info *mtd;
248 mtd = get_nand_dev_by_index(id->num);
250 return mtd->erasesize;
257 static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
259 #if defined(CONFIG_CMD_FLASH)
260 extern flash_info_t flash_info[];
262 u32 end_phys, start_phys, sector_size = 0, size = 0;
266 flash = &flash_info[id->num];
268 start_phys = flash->start[0] + part->offset;
269 end_phys = start_phys + part->size - 1;
271 for (i = 0; i < flash->sector_count; i++) {
272 if (flash->start[i] >= end_phys)
275 if (flash->start[i] >= start_phys) {
276 if (i == flash->sector_count - 1) {
277 size = flash->start[0] + flash->size - flash->start[i];
279 size = flash->start[i+1] - flash->start[i];
282 if (sector_size < size)
294 static inline u32 get_part_sector_size_onenand(void)
296 #if defined(CONFIG_CMD_ONENAND)
297 struct mtd_info *mtd;
301 return mtd->erasesize;
308 static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
310 if (id->type == MTD_DEV_TYPE_NAND)
311 return get_part_sector_size_nand(id);
312 else if (id->type == MTD_DEV_TYPE_NOR)
313 return get_part_sector_size_nor(id, part);
314 else if (id->type == MTD_DEV_TYPE_ONENAND)
315 return get_part_sector_size_onenand();
317 DEBUGF("Error: Unknown device type.\n");
323 * Parse and initialize global mtdids mapping and create global
324 * device/partition list.
326 * 'Static' version of command line mtdparts_init() routine. Single partition on
327 * a single device configuration.
329 * @return 0 on success, 1 otherwise
331 int mtdparts_init(void)
333 static int initialized = 0;
337 DEBUGF("\n---mtdparts_init---\n");
340 struct part_info *part;
343 current_mtd_dev = (struct mtd_device *)
344 malloc(sizeof(struct mtd_device) +
345 sizeof(struct part_info) +
346 sizeof(struct mtdids));
347 if (!current_mtd_dev) {
348 printf("out of memory\n");
351 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
352 sizeof(struct part_info) + sizeof(struct mtdids));
354 id = (struct mtdids *)(current_mtd_dev + 1);
355 part = (struct part_info *)(id + 1);
358 id->mtd_id = "single part";
360 #if defined(CONFIG_JFFS2_DEV)
361 dev_name = CONFIG_JFFS2_DEV;
366 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
367 (mtd_device_validate(id->type, id->num, &size) != 0)) {
368 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
369 free(current_mtd_dev);
373 INIT_LIST_HEAD(&id->link);
375 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
376 id->type, id->num, id->size, id->mtd_id);
379 part->name = "static";
382 #if defined(CONFIG_JFFS2_PART_SIZE)
383 part->size = CONFIG_JFFS2_PART_SIZE;
385 part->size = SIZE_REMAINING;
388 #if defined(CONFIG_JFFS2_PART_OFFSET)
389 part->offset = CONFIG_JFFS2_PART_OFFSET;
391 part->offset = 0x00000000;
394 part->dev = current_mtd_dev;
395 INIT_LIST_HEAD(&part->link);
397 /* recalculate size if needed */
398 if (part->size == SIZE_REMAINING)
399 part->size = id->size - part->offset;
401 part->sector_size = get_part_sector_size(id, part);
403 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
404 part->name, part->size, part->offset);
407 current_mtd_dev->id = id;
408 INIT_LIST_HEAD(¤t_mtd_dev->link);
409 current_mtd_dev->num_parts = 1;
410 INIT_LIST_HEAD(¤t_mtd_dev->parts);
411 list_add(&part->link, ¤t_mtd_dev->parts);
416 #endif /* #ifndef CONFIG_CMD_MTDPARTS */
419 * Return pointer to the partition of a requested number from a requested
422 * @param dev device that is to be searched for a partition
423 * @param part_num requested partition number
424 * @return pointer to the part_info, NULL otherwise
426 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
428 struct list_head *entry;
429 struct part_info *part;
435 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
436 part_num, MTD_DEV_TYPE(dev->id->type),
437 dev->id->num, dev->id->mtd_id);
439 if (part_num >= dev->num_parts) {
440 printf("invalid partition number %d for device %s%d (%s)\n",
441 part_num, MTD_DEV_TYPE(dev->id->type),
442 dev->id->num, dev->id->mtd_id);
446 /* locate partition number, return it */
448 list_for_each(entry, &dev->parts) {
449 part = list_entry(entry, struct part_info, link);
451 if (part_num == num++) {
459 /***************************************************/
460 /* U-Boot commands */
461 /***************************************************/
464 * Routine implementing fsload u-boot command. This routine tries to load
465 * a requested file from jffs2/cramfs filesystem on a current partition.
467 * @param cmdtp command internal data
468 * @param flag command flag
469 * @param argc number of arguments supplied to the command
470 * @param argv arguments list
471 * @return 0 on success, 1 otherwise
473 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
478 struct part_info *part;
479 ulong offset = load_addr;
481 /* pre-set Boot file name */
482 filename = env_get("bootfile");
490 offset = simple_strtoul(argv[1], NULL, 16);
495 /* make sure we are in sync with env variables */
496 if (mtdparts_init() !=0)
499 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
501 /* check partition type for cramfs */
502 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
503 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
505 if (cramfs_check(part)) {
506 size = cramfs_load ((char *) offset, part, filename);
508 /* if this is not cramfs assume jffs2 */
509 size = jffs2_1pass_load((char *)offset, part, filename);
513 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
514 fsname, size, offset);
515 env_set_hex("filesize", size);
517 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
526 * Routine implementing u-boot ls command which lists content of a given
527 * directory on a current partition.
529 * @param cmdtp command internal data
530 * @param flag command flag
531 * @param argc number of arguments supplied to the command
532 * @param argv arguments list
533 * @return 0 on success, 1 otherwise
535 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
537 char *filename = "/";
539 struct part_info *part;
544 /* make sure we are in sync with env variables */
545 if (mtdparts_init() !=0)
548 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
550 /* check partition type for cramfs */
551 if (cramfs_check(part)) {
552 ret = cramfs_ls (part, filename);
554 /* if this is not cramfs assume jffs2 */
555 ret = jffs2_1pass_ls(part, filename);
564 * Routine implementing u-boot fsinfo command. This routine prints out
565 * miscellaneous filesystem informations/statistics.
567 * @param cmdtp command internal data
568 * @param flag command flag
569 * @param argc number of arguments supplied to the command
570 * @param argv arguments list
571 * @return 0 on success, 1 otherwise
573 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
575 struct part_info *part;
579 /* make sure we are in sync with env variables */
580 if (mtdparts_init() !=0)
583 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
585 /* check partition type for cramfs */
586 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
587 printf("### filesystem type is %s\n", fsname);
589 if (cramfs_check(part)) {
590 ret = cramfs_info (part);
592 /* if this is not cramfs assume jffs2 */
593 ret = jffs2_1pass_info(part);
601 /***************************************************/
603 fsload, 3, 0, do_jffs2_fsload,
604 "load binary file from a filesystem image",
605 "[ off ] [ filename ]\n"
606 " - load binary file from flash bank\n"
610 fsls, 2, 1, do_jffs2_ls,
611 "list files in a directory (default /)",
616 fsinfo, 1, 1, do_jffs2_fsinfo,
617 "print information about filesystems",
620 /***************************************************/