1 // SPDX-License-Identifier: GPL-2.0+
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
75 #if defined(CONFIG_CMD_FLASH)
80 #include <jffs2/jffs2.h>
81 #include <linux/bug.h>
82 #include <linux/list.h>
83 #include <linux/ctype.h>
84 #include <cramfs/cramfs_fs.h>
86 #if defined(CONFIG_CMD_NAND)
87 #include <linux/mtd/rawnand.h>
91 #if defined(CONFIG_CMD_ONENAND)
92 #include <linux/mtd/mtd.h>
93 #include <linux/mtd/onenand.h>
94 #include <onenand_uboot.h>
97 /* enable/disable debugging messages */
102 # define DEBUGF(fmt, args...) printf(fmt ,##args)
104 # define DEBUGF(fmt, args...)
107 /* special size referring to all the remaining space in a partition */
108 #define SIZE_REMAINING 0xFFFFFFFF
110 /* special offset value, it is used when not provided by user
112 * this value is used temporarily during parsing, later such offests
113 * are recalculated */
114 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
116 /* minimum partition size */
117 #define MIN_PART_SIZE 4096
119 /* this flag needs to be set in part_info struct mask_flags
120 * field for read-only partitions */
121 #define MTD_WRITEABLE_CMD 1
123 /* current active device and partition number */
124 #ifdef CONFIG_CMD_MTDPARTS
125 /* Use the ones declared in cmd_mtdparts.c */
126 extern struct mtd_device *current_mtd_dev;
127 extern u8 current_mtd_partnum;
130 struct mtd_device *current_mtd_dev = NULL;
131 u8 current_mtd_partnum = 0;
134 #if defined(CONFIG_CMD_CRAMFS)
135 extern int cramfs_check (struct part_info *info);
136 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
137 extern int cramfs_ls (struct part_info *info, char *filename);
138 extern int cramfs_info (struct part_info *info);
140 /* defining empty macros for function names is ugly but avoids ifdef clutter
141 * all over the code */
142 #define cramfs_check(x) (0)
143 #define cramfs_load(x,y,z) (-1)
144 #define cramfs_ls(x,y) (0)
145 #define cramfs_info(x) (0)
148 #ifndef CONFIG_CMD_MTDPARTS
150 * Check device number to be within valid range for given device type.
152 * @param dev device to validate
153 * Return: 0 if device is valid, 1 otherwise
155 static int mtd_device_validate(u8 type, u8 num, u32 *size)
157 if (type == MTD_DEV_TYPE_NOR) {
158 #if defined(CONFIG_CMD_FLASH)
159 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
160 *size = flash_info[num].size;
165 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
166 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
168 printf("support for FLASH devices not present\n");
170 } else if (type == MTD_DEV_TYPE_NAND) {
171 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
172 struct mtd_info *mtd = get_nand_dev_by_index(num);
178 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
179 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
181 printf("support for NAND devices not present\n");
183 } else if (type == MTD_DEV_TYPE_ONENAND) {
184 #if defined(CONFIG_CMD_ONENAND)
185 *size = onenand_mtd.size;
188 printf("support for OneNAND devices not present\n");
191 printf("Unknown defice type %d\n", type);
197 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
198 * return device type and number.
200 * @param id string describing device id
201 * @param ret_id output pointer to next char after parse completes (output)
202 * @param dev_type parsed device type (output)
203 * @param dev_num parsed device number (output)
204 * Return: 0 on success, 1 otherwise
206 static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
211 if (strncmp(p, "nand", 4) == 0) {
212 *dev_type = MTD_DEV_TYPE_NAND;
214 } else if (strncmp(p, "nor", 3) == 0) {
215 *dev_type = MTD_DEV_TYPE_NOR;
217 } else if (strncmp(p, "onenand", 7) == 0) {
218 *dev_type = MTD_DEV_TYPE_ONENAND;
221 printf("incorrect device type in %s\n", id);
226 printf("incorrect device number in %s\n", id);
230 *dev_num = simple_strtoul(p, (char **)&p, 0);
237 * 'Static' version of command line mtdparts_init() routine. Single partition on
238 * a single device configuration.
242 * Calculate sector size.
244 * Return: sector size
246 static inline u32 get_part_sector_size_nand(struct mtdids *id)
248 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
249 struct mtd_info *mtd;
251 mtd = get_nand_dev_by_index(id->num);
253 return mtd->erasesize;
260 static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
262 #if defined(CONFIG_CMD_FLASH)
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 dev_name = CONFIG_JFFS2_DEV;
363 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
364 (mtd_device_validate(id->type, id->num, &size) != 0)) {
365 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
366 free(current_mtd_dev);
370 INIT_LIST_HEAD(&id->link);
372 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
373 id->type, id->num, id->size, id->mtd_id);
376 part->name = "static";
379 part->size = CONFIG_JFFS2_PART_SIZE;
381 part->offset = CONFIG_JFFS2_PART_OFFSET;
383 part->dev = current_mtd_dev;
384 INIT_LIST_HEAD(&part->link);
386 /* recalculate size if needed */
387 if (part->size == SIZE_REMAINING)
388 part->size = id->size - part->offset;
390 part->sector_size = get_part_sector_size(id, part);
392 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
393 part->name, part->size, part->offset);
396 current_mtd_dev->id = id;
397 INIT_LIST_HEAD(¤t_mtd_dev->link);
398 current_mtd_dev->num_parts = 1;
399 INIT_LIST_HEAD(¤t_mtd_dev->parts);
400 list_add(&part->link, ¤t_mtd_dev->parts);
405 #endif /* #ifndef CONFIG_CMD_MTDPARTS */
408 * Return pointer to the partition of a requested number from a requested
411 * @param dev device that is to be searched for a partition
412 * @param part_num requested partition number
413 * Return: pointer to the part_info, NULL otherwise
415 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
417 struct list_head *entry;
418 struct part_info *part;
424 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
425 part_num, MTD_DEV_TYPE(dev->id->type),
426 dev->id->num, dev->id->mtd_id);
428 if (part_num >= dev->num_parts) {
429 printf("invalid partition number %d for device %s%d (%s)\n",
430 part_num, MTD_DEV_TYPE(dev->id->type),
431 dev->id->num, dev->id->mtd_id);
435 /* locate partition number, return it */
437 list_for_each(entry, &dev->parts) {
438 part = list_entry(entry, struct part_info, link);
440 if (part_num == num++) {
448 /***************************************************/
449 /* U-Boot commands */
450 /***************************************************/
453 * Routine implementing fsload u-boot command. This routine tries to load
454 * a requested file from jffs2/cramfs filesystem on a current partition.
456 * @param cmdtp command internal data
457 * @param flag command flag
458 * @param argc number of arguments supplied to the command
459 * @param argv arguments list
460 * Return: 0 on success, 1 otherwise
462 int do_jffs2_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
468 struct part_info *part;
469 ulong offset = image_load_addr;
471 /* pre-set Boot file name */
472 filename = env_get("bootfile");
480 offset = hextoul(argv[1], NULL);
481 image_load_addr = offset;
485 /* make sure we are in sync with env variables */
486 if (mtdparts_init() !=0)
489 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
491 /* check partition type for cramfs */
492 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
493 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
495 if (cramfs_check(part)) {
496 size = cramfs_load ((char *) offset, part, filename);
498 /* if this is not cramfs assume jffs2 */
499 size = jffs2_1pass_load((char *)offset, part, filename);
503 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
504 fsname, size, offset);
505 env_set_hex("filesize", size);
507 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
516 * Routine implementing u-boot ls command which lists content of a given
517 * directory on a current partition.
519 * @param cmdtp command internal data
520 * @param flag command flag
521 * @param argc number of arguments supplied to the command
522 * @param argv arguments list
523 * Return: 0 on success, 1 otherwise
525 int do_jffs2_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
527 char *filename = "/";
529 struct part_info *part;
534 /* make sure we are in sync with env variables */
535 if (mtdparts_init() !=0)
538 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
540 /* check partition type for cramfs */
541 if (cramfs_check(part)) {
542 ret = cramfs_ls (part, filename);
544 /* if this is not cramfs assume jffs2 */
545 ret = jffs2_1pass_ls(part, filename);
554 * Routine implementing u-boot fsinfo command. This routine prints out
555 * miscellaneous filesystem informations/statistics.
557 * @param cmdtp command internal data
558 * @param flag command flag
559 * @param argc number of arguments supplied to the command
560 * @param argv arguments list
561 * Return: 0 on success, 1 otherwise
563 int do_jffs2_fsinfo(struct cmd_tbl *cmdtp, int flag, int argc,
566 struct part_info *part;
570 /* make sure we are in sync with env variables */
571 if (mtdparts_init() !=0)
574 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
576 /* check partition type for cramfs */
577 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
578 printf("### filesystem type is %s\n", fsname);
580 if (cramfs_check(part)) {
581 ret = cramfs_info (part);
583 /* if this is not cramfs assume jffs2 */
584 ret = jffs2_1pass_info(part);
592 /***************************************************/
594 fsload, 3, 0, do_jffs2_fsload,
595 "load binary file from a filesystem image",
596 "[ off ] [ filename ]\n"
597 " - load binary file from flash bank\n"
601 fsls, 2, 1, do_jffs2_ls,
602 "list files in a directory (default /)",
607 fsinfo, 1, 1, do_jffs2_fsinfo,
608 "print information about filesystems",
611 /***************************************************/