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
76 #if defined(CONFIG_CMD_FLASH)
81 #include <jffs2/jffs2.h>
82 #include <linux/bug.h>
83 #include <linux/list.h>
84 #include <linux/ctype.h>
85 #include <cramfs/cramfs_fs.h>
87 #if defined(CONFIG_CMD_NAND)
88 #include <linux/mtd/rawnand.h>
92 #if defined(CONFIG_CMD_ONENAND)
93 #include <linux/mtd/mtd.h>
94 #include <linux/mtd/onenand.h>
95 #include <onenand_uboot.h>
98 /* enable/disable debugging messages */
103 # define DEBUGF(fmt, args...) printf(fmt ,##args)
105 # define DEBUGF(fmt, args...)
108 /* special size referring to all the remaining space in a partition */
109 #define SIZE_REMAINING 0xFFFFFFFF
111 /* special offset value, it is used when not provided by user
113 * this value is used temporarily during parsing, later such offests
114 * are recalculated */
115 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
117 /* minimum partition size */
118 #define MIN_PART_SIZE 4096
120 /* this flag needs to be set in part_info struct mask_flags
121 * field for read-only partitions */
122 #define MTD_WRITEABLE_CMD 1
124 /* current active device and partition number */
125 #ifdef CONFIG_CMD_MTDPARTS
126 /* Use the ones declared in cmd_mtdparts.c */
127 extern struct mtd_device *current_mtd_dev;
128 extern u8 current_mtd_partnum;
131 struct mtd_device *current_mtd_dev = NULL;
132 u8 current_mtd_partnum = 0;
135 #if defined(CONFIG_CMD_CRAMFS)
136 extern int cramfs_check (struct part_info *info);
137 extern int cramfs_load (char *loadoffset, struct part_info *info, char *filename);
138 extern int cramfs_ls (struct part_info *info, char *filename);
139 extern int cramfs_info (struct part_info *info);
141 /* defining empty macros for function names is ugly but avoids ifdef clutter
142 * all over the code */
143 #define cramfs_check(x) (0)
144 #define cramfs_load(x,y,z) (-1)
145 #define cramfs_ls(x,y) (0)
146 #define cramfs_info(x) (0)
149 #ifndef CONFIG_CMD_MTDPARTS
151 * Check device number to be within valid range for given device type.
153 * @param dev device to validate
154 * Return: 0 if device is valid, 1 otherwise
156 static int mtd_device_validate(u8 type, u8 num, u32 *size)
158 if (type == MTD_DEV_TYPE_NOR) {
159 #if defined(CONFIG_CMD_FLASH)
160 if (num < CONFIG_SYS_MAX_FLASH_BANKS) {
161 *size = flash_info[num].size;
166 printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
167 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1);
169 printf("support for FLASH devices not present\n");
171 } else if (type == MTD_DEV_TYPE_NAND) {
172 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
173 struct mtd_info *mtd = get_nand_dev_by_index(num);
179 printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
180 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
182 printf("support for NAND devices not present\n");
184 } else if (type == MTD_DEV_TYPE_ONENAND) {
185 #if defined(CONFIG_CMD_ONENAND)
186 *size = onenand_mtd.size;
189 printf("support for OneNAND devices not present\n");
192 printf("Unknown defice type %d\n", type);
198 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
199 * return device type and number.
201 * @param id string describing device id
202 * @param ret_id output pointer to next char after parse completes (output)
203 * @param dev_type parsed device type (output)
204 * @param dev_num parsed device number (output)
205 * Return: 0 on success, 1 otherwise
207 static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
212 if (strncmp(p, "nand", 4) == 0) {
213 *dev_type = MTD_DEV_TYPE_NAND;
215 } else if (strncmp(p, "nor", 3) == 0) {
216 *dev_type = MTD_DEV_TYPE_NOR;
218 } else if (strncmp(p, "onenand", 7) == 0) {
219 *dev_type = MTD_DEV_TYPE_ONENAND;
222 printf("incorrect device type in %s\n", id);
227 printf("incorrect device number in %s\n", id);
231 *dev_num = simple_strtoul(p, (char **)&p, 0);
238 * 'Static' version of command line mtdparts_init() routine. Single partition on
239 * a single device configuration.
243 * Calculate sector size.
245 * Return: sector size
247 static inline u32 get_part_sector_size_nand(struct mtdids *id)
249 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
250 struct mtd_info *mtd;
252 mtd = get_nand_dev_by_index(id->num);
254 return mtd->erasesize;
261 static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
263 #if defined(CONFIG_CMD_FLASH)
264 u32 end_phys, start_phys, sector_size = 0, size = 0;
268 flash = &flash_info[id->num];
270 start_phys = flash->start[0] + part->offset;
271 end_phys = start_phys + part->size - 1;
273 for (i = 0; i < flash->sector_count; i++) {
274 if (flash->start[i] >= end_phys)
277 if (flash->start[i] >= start_phys) {
278 if (i == flash->sector_count - 1) {
279 size = flash->start[0] + flash->size - flash->start[i];
281 size = flash->start[i+1] - flash->start[i];
284 if (sector_size < size)
296 static inline u32 get_part_sector_size_onenand(void)
298 #if defined(CONFIG_CMD_ONENAND)
299 struct mtd_info *mtd;
303 return mtd->erasesize;
310 static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
312 if (id->type == MTD_DEV_TYPE_NAND)
313 return get_part_sector_size_nand(id);
314 else if (id->type == MTD_DEV_TYPE_NOR)
315 return get_part_sector_size_nor(id, part);
316 else if (id->type == MTD_DEV_TYPE_ONENAND)
317 return get_part_sector_size_onenand();
319 DEBUGF("Error: Unknown device type.\n");
325 * Parse and initialize global mtdids mapping and create global
326 * device/partition list.
328 * 'Static' version of command line mtdparts_init() routine. Single partition on
329 * a single device configuration.
331 * Return: 0 on success, 1 otherwise
333 int mtdparts_init(void)
335 static int initialized = 0;
339 DEBUGF("\n---mtdparts_init---\n");
342 struct part_info *part;
345 current_mtd_dev = (struct mtd_device *)
346 malloc(sizeof(struct mtd_device) +
347 sizeof(struct part_info) +
348 sizeof(struct mtdids));
349 if (!current_mtd_dev) {
350 printf("out of memory\n");
353 memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
354 sizeof(struct part_info) + sizeof(struct mtdids));
356 id = (struct mtdids *)(current_mtd_dev + 1);
357 part = (struct part_info *)(id + 1);
360 id->mtd_id = "single part";
362 dev_name = CONFIG_JFFS2_DEV;
364 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
365 (mtd_device_validate(id->type, id->num, &size) != 0)) {
366 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
367 free(current_mtd_dev);
371 INIT_LIST_HEAD(&id->link);
373 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
374 id->type, id->num, id->size, id->mtd_id);
377 part->name = "static";
380 part->size = CONFIG_JFFS2_PART_SIZE;
382 part->offset = CONFIG_JFFS2_PART_OFFSET;
384 part->dev = current_mtd_dev;
385 INIT_LIST_HEAD(&part->link);
387 /* recalculate size if needed */
388 if (part->size == SIZE_REMAINING)
389 part->size = id->size - part->offset;
391 part->sector_size = get_part_sector_size(id, part);
393 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
394 part->name, part->size, part->offset);
397 current_mtd_dev->id = id;
398 INIT_LIST_HEAD(¤t_mtd_dev->link);
399 current_mtd_dev->num_parts = 1;
400 INIT_LIST_HEAD(¤t_mtd_dev->parts);
401 list_add(&part->link, ¤t_mtd_dev->parts);
406 #endif /* #ifndef CONFIG_CMD_MTDPARTS */
409 * Return pointer to the partition of a requested number from a requested
412 * @param dev device that is to be searched for a partition
413 * @param part_num requested partition number
414 * Return: pointer to the part_info, NULL otherwise
416 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
418 struct list_head *entry;
419 struct part_info *part;
425 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
426 part_num, MTD_DEV_TYPE(dev->id->type),
427 dev->id->num, dev->id->mtd_id);
429 if (part_num >= dev->num_parts) {
430 printf("invalid partition number %d for device %s%d (%s)\n",
431 part_num, MTD_DEV_TYPE(dev->id->type),
432 dev->id->num, dev->id->mtd_id);
436 /* locate partition number, return it */
438 list_for_each(entry, &dev->parts) {
439 part = list_entry(entry, struct part_info, link);
441 if (part_num == num++) {
449 /***************************************************/
450 /* U-Boot commands */
451 /***************************************************/
454 * Routine implementing fsload u-boot command. This routine tries to load
455 * a requested file from jffs2/cramfs filesystem on a current partition.
457 * @param cmdtp command internal data
458 * @param flag command flag
459 * @param argc number of arguments supplied to the command
460 * @param argv arguments list
461 * Return: 0 on success, 1 otherwise
463 int do_jffs2_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
469 struct part_info *part;
470 ulong offset = image_load_addr;
472 /* pre-set Boot file name */
473 filename = env_get("bootfile");
481 offset = hextoul(argv[1], NULL);
482 image_load_addr = offset;
486 /* make sure we are in sync with env variables */
487 if (mtdparts_init() !=0)
490 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
492 /* check partition type for cramfs */
493 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
494 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
496 if (cramfs_check(part)) {
497 size = cramfs_load ((char *) offset, part, filename);
499 /* if this is not cramfs assume jffs2 */
500 size = jffs2_1pass_load((char *)offset, part, filename);
504 printf("### %s load complete: %d bytes loaded to 0x%lx\n",
505 fsname, size, offset);
506 env_set_hex("filesize", size);
508 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
517 * Routine implementing u-boot ls command which lists content of a given
518 * directory on a current partition.
520 * @param cmdtp command internal data
521 * @param flag command flag
522 * @param argc number of arguments supplied to the command
523 * @param argv arguments list
524 * Return: 0 on success, 1 otherwise
526 int do_jffs2_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
528 char *filename = "/";
530 struct part_info *part;
535 /* make sure we are in sync with env variables */
536 if (mtdparts_init() !=0)
539 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
541 /* check partition type for cramfs */
542 if (cramfs_check(part)) {
543 ret = cramfs_ls (part, filename);
545 /* if this is not cramfs assume jffs2 */
546 ret = jffs2_1pass_ls(part, filename);
555 * Routine implementing u-boot fsinfo command. This routine prints out
556 * miscellaneous filesystem informations/statistics.
558 * @param cmdtp command internal data
559 * @param flag command flag
560 * @param argc number of arguments supplied to the command
561 * @param argv arguments list
562 * Return: 0 on success, 1 otherwise
564 int do_jffs2_fsinfo(struct cmd_tbl *cmdtp, int flag, int argc,
567 struct part_info *part;
571 /* make sure we are in sync with env variables */
572 if (mtdparts_init() !=0)
575 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
577 /* check partition type for cramfs */
578 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
579 printf("### filesystem type is %s\n", fsname);
581 if (cramfs_check(part)) {
582 ret = cramfs_info (part);
584 /* if this is not cramfs assume jffs2 */
585 ret = jffs2_1pass_info(part);
593 /***************************************************/
595 fsload, 3, 0, do_jffs2_fsload,
596 "load binary file from a filesystem image",
597 "[ off ] [ filename ]\n"
598 " - load binary file from flash bank\n"
602 fsls, 2, 1, do_jffs2_ls,
603 "list files in a directory (default /)",
608 fsinfo, 1, 1, do_jffs2_fsinfo,
609 "print information about filesystems",
612 /***************************************************/