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
if (num < CONFIG_SYS_MAX_NAND_DEVICE) {
170
*size = nand_info[num].size;
174
printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
175
MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1);
177
printf("support for NAND devices not present\n");
179
} else if (type == MTD_DEV_TYPE_ONENAND) {
180
#if defined(CONFIG_CMD_ONENAND)
181
*size = onenand_mtd.size;
184
printf("support for OneNAND devices not present\n");
187
printf("Unknown defice type %d\n", type);
193
* Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
194
* return device type and number.
196
* @param id string describing device id
197
* @param ret_id output pointer to next char after parse completes (output)
198
* @param dev_type parsed device type (output)
199
* @param dev_num parsed device number (output)
200
* @return 0 on success, 1 otherwise
202
static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
207
if (strncmp(p, "nand", 4) == 0) {
208
*dev_type = MTD_DEV_TYPE_NAND;
210
} else if (strncmp(p, "nor", 3) == 0) {
211
*dev_type = MTD_DEV_TYPE_NOR;
213
} else if (strncmp(p, "onenand", 7) == 0) {
214
*dev_type = MTD_DEV_TYPE_ONENAND;
217
printf("incorrect device type in %s\n", id);
222
printf("incorrect device number in %s\n", id);
226
*dev_num = simple_strtoul(p, (char **)&p, 0);
233
* 'Static' version of command line mtdparts_init() routine. Single partition on
234
* a single device configuration.
238
* Calculate sector size.
240
* @return sector size
242
static inline u32 get_part_sector_size_nand(struct mtdids *id)
244
#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
247
nand = &nand_info[id->num];
249
return nand->erasesize;
256
static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
258
#if defined(CONFIG_CMD_FLASH)
259
extern flash_info_t flash_info[];
261
u32 end_phys, start_phys, sector_size = 0, size = 0;
265
flash = &flash_info[id->num];
267
start_phys = flash->start[0] + part->offset;
268
end_phys = start_phys + part->size - 1;
270
for (i = 0; i < flash->sector_count; i++) {
271
if (flash->start[i] >= end_phys)
274
if (flash->start[i] >= start_phys) {
275
if (i == flash->sector_count - 1) {
276
size = flash->start[0] + flash->size - flash->start[i];
278
size = flash->start[i+1] - flash->start[i];
281
if (sector_size < size)
293
static inline u32 get_part_sector_size_onenand(void)
295
#if defined(CONFIG_CMD_ONENAND)
296
struct mtd_info *mtd;
300
return mtd->erasesize;
307
static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
309
if (id->type == MTD_DEV_TYPE_NAND)
310
return get_part_sector_size_nand(id);
311
else if (id->type == MTD_DEV_TYPE_NOR)
312
return get_part_sector_size_nor(id, part);
313
else if (id->type == MTD_DEV_TYPE_ONENAND)
314
return get_part_sector_size_onenand();
316
DEBUGF("Error: Unknown device type.\n");
322
* Parse and initialize global mtdids mapping and create global
323
* device/partition list.
325
* 'Static' version of command line mtdparts_init() routine. Single partition on
326
* a single device configuration.
328
* @return 0 on success, 1 otherwise
330
int mtdparts_init(void)
332
static int initialized = 0;
336
DEBUGF("\n---mtdparts_init---\n");
339
struct part_info *part;
342
current_mtd_dev = (struct mtd_device *)
343
malloc(sizeof(struct mtd_device) +
344
sizeof(struct part_info) +
345
sizeof(struct mtdids));
346
if (!current_mtd_dev) {
347
printf("out of memory\n");
350
memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
351
sizeof(struct part_info) + sizeof(struct mtdids));
353
id = (struct mtdids *)(current_mtd_dev + 1);
354
part = (struct part_info *)(id + 1);
357
id->mtd_id = "single part";
359
#if defined(CONFIG_JFFS2_DEV)
360
dev_name = CONFIG_JFFS2_DEV;
365
if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
366
(mtd_device_validate(id->type, id->num, &size) != 0)) {
367
printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
368
free(current_mtd_dev);
372
INIT_LIST_HEAD(&id->link);
374
DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
375
id->type, id->num, id->size, id->mtd_id);
378
part->name = "static";
381
#if defined(CONFIG_JFFS2_PART_SIZE)
382
part->size = CONFIG_JFFS2_PART_SIZE;
384
part->size = SIZE_REMAINING;
387
#if defined(CONFIG_JFFS2_PART_OFFSET)
388
part->offset = CONFIG_JFFS2_PART_OFFSET;
390
part->offset = 0x00000000;
393
part->dev = current_mtd_dev;
394
INIT_LIST_HEAD(&part->link);
396
/* recalculate size if needed */
397
if (part->size == SIZE_REMAINING)
398
part->size = id->size - part->offset;
400
part->sector_size = get_part_sector_size(id, part);
402
DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
403
part->name, part->size, part->offset);
406
current_mtd_dev->id = id;
407
INIT_LIST_HEAD(¤t_mtd_dev->link);
408
current_mtd_dev->num_parts = 1;
409
INIT_LIST_HEAD(¤t_mtd_dev->parts);
410
list_add(&part->link, ¤t_mtd_dev->parts);
415
#endif /* #ifndef CONFIG_CMD_MTDPARTS */
418
* Return pointer to the partition of a requested number from a requested
421
* @param dev device that is to be searched for a partition
422
* @param part_num requested partition number
423
* @return pointer to the part_info, NULL otherwise
425
static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
427
struct list_head *entry;
428
struct part_info *part;
434
DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n",
435
part_num, MTD_DEV_TYPE(dev->id->type),
436
dev->id->num, dev->id->mtd_id);
438
if (part_num >= dev->num_parts) {
439
printf("invalid partition number %d for device %s%d (%s)\n",
440
part_num, MTD_DEV_TYPE(dev->id->type),
441
dev->id->num, dev->id->mtd_id);
445
/* locate partition number, return it */
447
list_for_each(entry, &dev->parts) {
448
part = list_entry(entry, struct part_info, link);
450
if (part_num == num++) {
458
/***************************************************/
459
/* U-boot commands */
460
/***************************************************/
463
* Routine implementing fsload u-boot command. This routine tries to load
464
* a requested file from jffs2/cramfs filesystem on a current partition.
466
* @param cmdtp command internal data
467
* @param flag command flag
468
* @param argc number of arguments supplied to the command
469
* @param argv arguments list
470
* @return 0 on success, 1 otherwise
472
int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
477
struct part_info *part;
478
ulong offset = load_addr;
480
/* pre-set Boot file name */
481
if ((filename = getenv("bootfile")) == NULL) {
489
offset = simple_strtoul(argv[1], NULL, 16);
494
/* make sure we are in sync with env variables */
495
if (mtdparts_init() !=0)
498
if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
500
/* check partition type for cramfs */
501
fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
502
printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset);
504
if (cramfs_check(part)) {
505
size = cramfs_load ((char *) offset, part, filename);
507
/* if this is not cramfs assume jffs2 */
508
size = jffs2_1pass_load((char *)offset, part, filename);
512
printf("### %s load complete: %d bytes loaded to 0x%lx\n",
513
fsname, size, offset);
514
setenv_hex("filesize", size);
516
printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename);
525
* Routine implementing u-boot ls command which lists content of a given
526
* directory on a current partition.
528
* @param cmdtp command internal data
529
* @param flag command flag
530
* @param argc number of arguments supplied to the command
531
* @param argv arguments list
532
* @return 0 on success, 1 otherwise
534
int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
536
char *filename = "/";
538
struct part_info *part;
543
/* make sure we are in sync with env variables */
544
if (mtdparts_init() !=0)
547
if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
549
/* check partition type for cramfs */
550
if (cramfs_check(part)) {
551
ret = cramfs_ls (part, filename);
553
/* if this is not cramfs assume jffs2 */
554
ret = jffs2_1pass_ls(part, filename);
563
* Routine implementing u-boot fsinfo command. This routine prints out
564
* miscellaneous filesystem informations/statistics.
566
* @param cmdtp command internal data
567
* @param flag command flag
568
* @param argc number of arguments supplied to the command
569
* @param argv arguments list
570
* @return 0 on success, 1 otherwise
572
int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
574
struct part_info *part;
578
/* make sure we are in sync with env variables */
579
if (mtdparts_init() !=0)
582
if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
584
/* check partition type for cramfs */
585
fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
586
printf("### filesystem type is %s\n", fsname);
588
if (cramfs_check(part)) {
589
ret = cramfs_info (part);
591
/* if this is not cramfs assume jffs2 */
592
ret = jffs2_1pass_info(part);
600
/***************************************************/
602
fsload, 3, 0, do_jffs2_fsload,
603
"load binary file from a filesystem image",
604
"[ off ] [ filename ]\n"
605
" - load binary file from flash bank\n"
609
ls, 2, 1, do_jffs2_ls,
610
"list files in a directory (default /)",
615
fsinfo, 1, 1, do_jffs2_fsinfo,
616
"print information about filesystems",
619
/***************************************************/