1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
10 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
13 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 * Added support for reading flash partition table from environment.
16 * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
19 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
20 * Copyright 2002 SYSGO Real-Time Solutions GmbH
24 * Three environment variables are used by the parsing routines:
26 * 'partition' - keeps current partition identifier
28 * partition := <part-id>
29 * <part-id> := <dev-id>,part_num
32 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
34 * mtdids=<idmap>[,<idmap>,...]
36 * <idmap> := <dev-id>=<mtd-id>
37 * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
38 * <dev-num> := mtd device number, 0...
39 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
42 * 'mtdparts' - partition list
44 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
46 * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
47 * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
48 * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
49 * <size> := standard linux memsize OR '-' to denote all remaining space
50 * <offset> := partition start offset within the device
51 * <name> := '(' NAME ')'
52 * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
55 * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
56 * - if the above variables are not set defaults for a given target are used
60 * 1 NOR Flash, with 1 single writable partition:
61 * mtdids=nor0=edb7312-nor
62 * mtdparts=mtdparts=edb7312-nor:-
64 * 1 NOR Flash with 2 partitions, 1 NAND with one
65 * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
66 * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
71 * JFFS2/CRAMFS support
78 #include <jffs2/jffs2.h>
79 #include <linux/list.h>
80 #include <linux/ctype.h>
81 #include <cramfs/cramfs_fs.h>
83 #if defined(CONFIG_CMD_NAND)
84 #include <linux/mtd/rawnand.h>
88 #if defined(CONFIG_CMD_ONENAND)
89 #include <linux/mtd/mtd.h>
90 #include <linux/mtd/onenand.h>
91 #include <onenand_uboot.h>
94 /* enable/disable debugging messages */
99 # define DEBUGF(fmt, args...) printf(fmt ,##args)
101 # define DEBUGF(fmt, args...)
104 /* special size referring to all the remaining space in a partition */
105 #define SIZE_REMAINING 0xFFFFFFFF
107 /* special offset value, it is used when not provided by user
109 * this value is used temporarily during parsing, later such offests
110 * are recalculated */
111 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
113 /* minimum partition size */
114 #define MIN_PART_SIZE 4096
116 /* this flag needs to be set in part_info struct mask_flags
117 * field for read-only partitions */
118 #define MTD_WRITEABLE_CMD 1
120 /* current active device and partition number */
121 #ifdef CONFIG_CMD_MTDPARTS
122 /* Use the ones declared in cmd_mtdparts.c */
123 extern struct mtd_device *current_mtd_dev;
124 extern u8 current_mtd_partnum;
127 struct mtd_device *current_mtd_dev = NULL;
128 u8 current_mtd_partnum = 0;
131 #if defined(CONFIG_CMD_CRAMFS)
132 extern int cramfs_check (struct part_info *info);
133 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
134 extern int cramfs_ls (struct part_info *info, char *filename);
135 extern int cramfs_info (struct part_info *info);
137 /* defining empty macros for function names is ugly but avoids ifdef clutter
138 * all over the code */
139 #define cramfs_check(x) (0)
140 #define cramfs_load(x,y,z) (-1)
141 #define cramfs_ls(x,y) (0)
142 #define cramfs_info(x) (0)
145 #ifndef CONFIG_CMD_MTDPARTS
147 * Check device number to be within valid range for given device type.
149 * @param dev device to validate
150 * @return 0 if device is valid, 1 otherwise
152 static int mtd_device_validate(u8 type, u8 num, u32 *size)
154 if (type == MTD_DEV_TYPE_NOR) {
155 #if defined(CONFIG_CMD_FLASH)
156 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
157 extern flash_info_t flash_info[];
158 *size = flash_info[num].size;
163 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
164 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
166 printf("support for FLASH devices not present\n");
168 } else if (type == MTD_DEV_TYPE_NAND) {
169 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
170 struct mtd_info *mtd = get_nand_dev_by_index(num);
176 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
177 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
179 printf("support for NAND devices not present\n");
181 } else if (type == MTD_DEV_TYPE_ONENAND) {
182 #if defined(CONFIG_CMD_ONENAND)
183 *size = onenand_mtd.size;
186 printf("support for OneNAND devices not present\n");
189 printf("Unknown defice type %d\n", type);
195 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
196 * return device type and number.
198 * @param id string describing device id
199 * @param ret_id output pointer to next char after parse completes (output)
200 * @param dev_type parsed device type (output)
201 * @param dev_num parsed device number (output)
202 * @return 0 on success, 1 otherwise
204 static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
209 if (strncmp(p, "nand", 4) == 0) {
210 *dev_type = MTD_DEV_TYPE_NAND;
212 } else if (strncmp(p, "nor", 3) == 0) {
213 *dev_type = MTD_DEV_TYPE_NOR;
215 } else if (strncmp(p, "onenand", 7) == 0) {
216 *dev_type = MTD_DEV_TYPE_ONENAND;
219 printf("incorrect device type in %s\n", id);
224 printf("incorrect device number in %s\n", id);
228 *dev_num = simple_strtoul(p, (char **)&p, 0);
235 * 'Static' version of command line mtdparts_init() routine. Single partition on
236 * a single device configuration.
240 * Calculate sector size.
242 * @return sector size
244 static inline u32 get_part_sector_size_nand(struct mtdids *id)
246 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
247 struct mtd_info *mtd;
249 mtd = get_nand_dev_by_index(id->num);
251 return mtd->erasesize;
258 static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
260 #if defined(CONFIG_CMD_FLASH)
261 extern flash_info_t flash_info[];
263 u32 end_phys, start_phys, sector_size = 0, size = 0;
267 flash = &flash_info[id->num];
269 start_phys = flash->start[0] + part->offset;
270 end_phys = start_phys + part->size - 1;
272 for (i = 0; i < flash->sector_count; i++) {
273 if (flash->start[i] >= end_phys)
276 if (flash->start[i] >= start_phys) {
277 if (i == flash->sector_count - 1) {
278 size = flash->start[0] + flash->size - flash->start[i];
280 size = flash->start[i+1] - flash->start[i];
283 if (sector_size < size)
295 static inline u32 get_part_sector_size_onenand(void)
297 #if defined(CONFIG_CMD_ONENAND)
298 struct mtd_info *mtd;
302 return mtd->erasesize;
309 static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
311 if (id->type == MTD_DEV_TYPE_NAND)
312 return get_part_sector_size_nand(id);
313 else if (id->type == MTD_DEV_TYPE_NOR)
314 return get_part_sector_size_nor(id, part);
315 else if (id->type == MTD_DEV_TYPE_ONENAND)
316 return get_part_sector_size_onenand();
318 DEBUGF("Error: Unknown device type.\n");
324 * Parse and initialize global mtdids mapping and create global
325 * device/partition list.
327 * 'Static' version of command line mtdparts_init() routine. Single partition on
328 * a single device configuration.
330 * @return 0 on success, 1 otherwise
332 int mtdparts_init(void)
334 static int initialized = 0;
338 DEBUGF("\n---mtdparts_init---\n");
341 struct part_info *part;
344 current_mtd_dev = (struct mtd_device *)
345 malloc(sizeof(struct mtd_device) +
346 sizeof(struct part_info) +
347 sizeof(struct mtdids));
348 if (!current_mtd_dev) {
349 printf("out of memory\n");
352 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
353 sizeof(struct part_info) + sizeof(struct mtdids));
355 id = (struct mtdids *)(current_mtd_dev + 1);
356 part = (struct part_info *)(id + 1);
359 id->mtd_id = "single part";
361 #if defined(CONFIG_JFFS2_DEV)
362 dev_name = CONFIG_JFFS2_DEV;
367 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
368 (mtd_device_validate(id->type, id->num, &size) != 0)) {
369 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
370 free(current_mtd_dev);
374 INIT_LIST_HEAD(&id->link);
376 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
377 id->type, id->num, id->size, id->mtd_id);
380 part->name = "static";
383 #if defined(CONFIG_JFFS2_PART_SIZE)
384 part->size = CONFIG_JFFS2_PART_SIZE;
386 part->size = SIZE_REMAINING;
389 #if defined(CONFIG_JFFS2_PART_OFFSET)
390 part->offset = CONFIG_JFFS2_PART_OFFSET;
392 part->offset = 0x00000000;
395 part->dev = current_mtd_dev;
396 INIT_LIST_HEAD(&part->link);
398 /* recalculate size if needed */
399 if (part->size == SIZE_REMAINING)
400 part->size = id->size - part->offset;
402 part->sector_size = get_part_sector_size(id, part);
404 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
405 part->name, part->size, part->offset);
408 current_mtd_dev->id = id;
409 INIT_LIST_HEAD(¤t_mtd_dev->link);
410 current_mtd_dev->num_parts = 1;
411 INIT_LIST_HEAD(¤t_mtd_dev->parts);
412 list_add(&part->link, ¤t_mtd_dev->parts);
417 #endif /* #ifndef CONFIG_CMD_MTDPARTS */
420 * Return pointer to the partition of a requested number from a requested
423 * @param dev device that is to be searched for a partition
424 * @param part_num requested partition number
425 * @return pointer to the part_info, NULL otherwise
427 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
429 struct list_head *entry;
430 struct part_info *part;
436 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
437 part_num, MTD_DEV_TYPE(dev->id->type),
438 dev->id->num, dev->id->mtd_id);
440 if (part_num >= dev->num_parts) {
441 printf("invalid partition number %d for device %s%d (%s)\n",
442 part_num, MTD_DEV_TYPE(dev->id->type),
443 dev->id->num, dev->id->mtd_id);
447 /* locate partition number, return it */
449 list_for_each(entry, &dev->parts) {
450 part = list_entry(entry, struct part_info, link);
452 if (part_num == num++) {
460 /***************************************************/
461 /* U-Boot commands */
462 /***************************************************/
465 * Routine implementing fsload u-boot command. This routine tries to load
466 * a requested file from jffs2/cramfs filesystem on a current partition.
468 * @param cmdtp command internal data
469 * @param flag command flag
470 * @param argc number of arguments supplied to the command
471 * @param argv arguments list
472 * @return 0 on success, 1 otherwise
474 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
479 struct part_info *part;
480 ulong offset = image_load_addr;
482 /* pre-set Boot file name */
483 filename = env_get("bootfile");
491 offset = simple_strtoul(argv[1], NULL, 16);
492 image_load_addr = offset;
496 /* make sure we are in sync with env variables */
497 if (mtdparts_init() !=0)
500 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
502 /* check partition type for cramfs */
503 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
504 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
506 if (cramfs_check(part)) {
507 size = cramfs_load ((char *) offset, part, filename);
509 /* if this is not cramfs assume jffs2 */
510 size = jffs2_1pass_load((char *)offset, part, filename);
514 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
515 fsname, size, offset);
516 env_set_hex("filesize", size);
518 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
527 * Routine implementing u-boot ls command which lists content of a given
528 * directory on a current partition.
530 * @param cmdtp command internal data
531 * @param flag command flag
532 * @param argc number of arguments supplied to the command
533 * @param argv arguments list
534 * @return 0 on success, 1 otherwise
536 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
538 char *filename = "/";
540 struct part_info *part;
545 /* make sure we are in sync with env variables */
546 if (mtdparts_init() !=0)
549 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
551 /* check partition type for cramfs */
552 if (cramfs_check(part)) {
553 ret = cramfs_ls (part, filename);
555 /* if this is not cramfs assume jffs2 */
556 ret = jffs2_1pass_ls(part, filename);
565 * Routine implementing u-boot fsinfo command. This routine prints out
566 * miscellaneous filesystem informations/statistics.
568 * @param cmdtp command internal data
569 * @param flag command flag
570 * @param argc number of arguments supplied to the command
571 * @param argv arguments list
572 * @return 0 on success, 1 otherwise
574 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
576 struct part_info *part;
580 /* make sure we are in sync with env variables */
581 if (mtdparts_init() !=0)
584 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
586 /* check partition type for cramfs */
587 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
588 printf("### filesystem type is %s\n", fsname);
590 if (cramfs_check(part)) {
591 ret = cramfs_info (part);
593 /* if this is not cramfs assume jffs2 */
594 ret = jffs2_1pass_info(part);
602 /***************************************************/
604 fsload, 3, 0, do_jffs2_fsload,
605 "load binary file from a filesystem image",
606 "[ off ] [ filename ]\n"
607 " - load binary file from flash bank\n"
611 fsls, 2, 1, do_jffs2_ls,
612 "list files in a directory (default /)",
617 fsinfo, 1, 1, do_jffs2_fsinfo,
618 "print information about filesystems",
621 /***************************************************/