1 /* 2 * (C) Copyright 2002 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * (C) Copyright 2002 6 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de> 7 * 8 * (C) Copyright 2003 9 * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de> 10 * 11 * (C) Copyright 2005 12 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 13 * 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 16 * kernel tree. 17 * 18 * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $ 19 * Copyright 2002 SYSGO Real-Time Solutions GmbH 20 * 21 * SPDX-License-Identifier: GPL-2.0+ 22 */ 23 24 /* 25 * Three environment variables are used by the parsing routines: 26 * 27 * 'partition' - keeps current partition identifier 28 * 29 * partition := <part-id> 30 * <part-id> := <dev-id>,part_num 31 * 32 * 33 * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping 34 * 35 * mtdids=<idmap>[,<idmap>,...] 36 * 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) 41 * 42 * 43 * 'mtdparts' - partition list 44 * 45 * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...] 46 * 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) 54 * 55 * Notes: 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 58 * 59 * Examples: 60 * 61 * 1 NOR Flash, with 1 single writable partition: 62 * mtdids=nor0=edb7312-nor 63 * mtdparts=mtdparts=edb7312-nor:- 64 * 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) 68 * 69 */ 70 71 /* 72 * JFFS2/CRAMFS support 73 */ 74 #include <common.h> 75 #include <command.h> 76 #include <malloc.h> 77 #include <jffs2/jffs2.h> 78 #include <linux/list.h> 79 #include <linux/ctype.h> 80 #include <cramfs/cramfs_fs.h> 81 82 #if defined(CONFIG_CMD_NAND) 83 #include <linux/mtd/rawnand.h> 84 #include <nand.h> 85 #endif 86 87 #if defined(CONFIG_CMD_ONENAND) 88 #include <linux/mtd/mtd.h> 89 #include <linux/mtd/onenand.h> 90 #include <onenand_uboot.h> 91 #endif 92 93 /* enable/disable debugging messages */ 94 #define DEBUG_JFFS 95 #undef DEBUG_JFFS 96 97 #ifdef DEBUG_JFFS 98 # define DEBUGF(fmt, args...) printf(fmt ,##args) 99 #else 100 # define DEBUGF(fmt, args...) 101 #endif 102 103 /* special size referring to all the remaining space in a partition */ 104 #define SIZE_REMAINING 0xFFFFFFFF 105 106 /* special offset value, it is used when not provided by user 107 * 108 * this value is used temporarily during parsing, later such offests 109 * are recalculated */ 110 #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF 111 112 /* minimum partition size */ 113 #define MIN_PART_SIZE 4096 114 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 118 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; 124 #else 125 /* Use local ones */ 126 struct mtd_device *current_mtd_dev = NULL; 127 u8 current_mtd_partnum = 0; 128 #endif 129 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); 135 #else 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) 142 #endif 143 144 #ifndef CONFIG_CMD_MTDPARTS 145 /** 146 * Check device number to be within valid range for given device type. 147 * 148 * @param dev device to validate 149 * @return 0 if device is valid, 1 otherwise 150 */ 151 static int mtd_device_validate(u8 type, u8 num, u32 *size) 152 { 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; 158 159 return 0; 160 } 161 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); 164 #else 165 struct mtd_info *mtd = get_nand_dev_by_index(num); 166 if (mtd) { 167 *size = mtd->size; 168 return 0; 169 } 170 171 printf("no such Nor device: %s%d (valid range 0 ... %d)\n", 172 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_FLASH_BANKS - 1); 173 #endif 174 } else if (type == MTD_DEV_TYPE_NAND) { 175 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) 176 struct mtd_info *mtd = get_nand_dev_by_index(num); 177 if (mtd) { 178 *size = mtd->size; 179 return 0; 180 } 181 182 printf("no such NAND device: %s%d (valid range 0 ... %d)\n", 183 MTD_DEV_TYPE(type), num, CONFIG_SYS_MAX_NAND_DEVICE - 1); 184 #else 185 printf("support for NAND devices not present\n"); 186 #endif 187 } else if (type == MTD_DEV_TYPE_ONENAND) { 188 #if defined(CONFIG_CMD_ONENAND) 189 *size = onenand_mtd.size; 190 return 0; 191 #else 192 printf("support for OneNAND devices not present\n"); 193 #endif 194 } else 195 printf("Unknown defice type %d\n", type); 196 197 return 1; 198 } 199 200 /** 201 * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>, 202 * return device type and number. 203 * 204 * @param id string describing device id 205 * @param ret_id output pointer to next char after parse completes (output) 206 * @param dev_type parsed device type (output) 207 * @param dev_num parsed device number (output) 208 * @return 0 on success, 1 otherwise 209 */ 210 static int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num) 211 { 212 const char *p = id; 213 214 *dev_type = 0; 215 if (strncmp(p, "nand", 4) == 0) { 216 *dev_type = MTD_DEV_TYPE_NAND; 217 p += 4; 218 } else if (strncmp(p, "nor", 3) == 0) { 219 *dev_type = MTD_DEV_TYPE_NOR; 220 p += 3; 221 } else if (strncmp(p, "onenand", 7) == 0) { 222 *dev_type = MTD_DEV_TYPE_ONENAND; 223 p += 7; 224 } else { 225 printf("incorrect device type in %s\n", id); 226 return 1; 227 } 228 229 if (!isdigit(*p)) { 230 printf("incorrect device number in %s\n", id); 231 return 1; 232 } 233 234 *dev_num = simple_strtoul(p, (char **)&p, 0); 235 if (ret_id) 236 *ret_id = p; 237 return 0; 238 } 239 240 /* 241 * 'Static' version of command line mtdparts_init() routine. Single partition on 242 * a single device configuration. 243 */ 244 245 /** 246 * Calculate sector size. 247 * 248 * @return sector size 249 */ 250 static inline u32 get_part_sector_size_nand(struct mtdids *id) 251 { 252 #if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND) 253 struct mtd_info *mtd; 254 255 mtd = get_nand_dev_by_index(id->num); 256 257 return mtd->erasesize; 258 #else 259 BUG(); 260 return 0; 261 #endif 262 } 263 264 static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part) 265 { 266 #if defined(CONFIG_CMD_FLASH) 267 extern flash_info_t flash_info[]; 268 269 u32 end_phys, start_phys, sector_size = 0, size = 0; 270 int i; 271 flash_info_t *flash; 272 273 flash = &flash_info[id->num]; 274 275 start_phys = flash->start[0] + part->offset; 276 end_phys = start_phys + part->size - 1; 277 278 for (i = 0; i < flash->sector_count; i++) { 279 if (flash->start[i] >= end_phys) 280 break; 281 282 if (flash->start[i] >= start_phys) { 283 if (i == flash->sector_count - 1) { 284 size = flash->start[0] + flash->size - flash->start[i]; 285 } else { 286 size = flash->start[i+1] - flash->start[i]; 287 } 288 289 if (sector_size < size) 290 sector_size = size; 291 } 292 } 293 294 return sector_size; 295 #else 296 struct mtd_info *mtd; 297 298 mtd = get_nand_dev_by_index(id->num); 299 300 return mtd->erasesize; 301 #endif 302 } 303 304 static inline u32 get_part_sector_size_onenand(void) 305 { 306 #if defined(CONFIG_CMD_ONENAND) 307 struct mtd_info *mtd; 308 309 mtd = &onenand_mtd; 310 311 return mtd->erasesize; 312 #else 313 BUG(); 314 return 0; 315 #endif 316 } 317 318 static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part) 319 { 320 if (id->type == MTD_DEV_TYPE_NAND) 321 return get_part_sector_size_nand(id); 322 else if (id->type == MTD_DEV_TYPE_NOR) 323 return get_part_sector_size_nor(id, part); 324 else if (id->type == MTD_DEV_TYPE_ONENAND) 325 return get_part_sector_size_onenand(); 326 else 327 DEBUGF("Error: Unknown device type.\n"); 328 329 return 0; 330 } 331 332 /** 333 * Parse and initialize global mtdids mapping and create global 334 * device/partition list. 335 * 336 * 'Static' version of command line mtdparts_init() routine. Single partition on 337 * a single device configuration. 338 * 339 * @return 0 on success, 1 otherwise 340 */ 341 int mtdparts_init(void) 342 { 343 static int initialized = 0; 344 u32 size; 345 char *dev_name; 346 347 DEBUGF("\n---mtdparts_init---\n"); 348 if (!initialized) { 349 struct mtdids *id; 350 struct part_info *part; 351 352 initialized = 1; 353 current_mtd_dev = (struct mtd_device *) 354 malloc(sizeof(struct mtd_device) + 355 sizeof(struct part_info) + 356 sizeof(struct mtdids)); 357 if (!current_mtd_dev) { 358 printf("out of memory\n"); 359 return 1; 360 } 361 memset(current_mtd_dev, 0, sizeof(struct mtd_device) + 362 sizeof(struct part_info) + sizeof(struct mtdids)); 363 364 id = (struct mtdids *)(current_mtd_dev + 1); 365 part = (struct part_info *)(id + 1); 366 367 /* id */ 368 id->mtd_id = "single part"; 369 370 #if defined(CONFIG_JFFS2_DEV) 371 dev_name = CONFIG_JFFS2_DEV; 372 #else 373 dev_name = "nor0"; 374 #endif 375 376 if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) || 377 (mtd_device_validate(id->type, id->num, &size) != 0)) { 378 printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num); 379 free(current_mtd_dev); 380 return 1; 381 } 382 id->size = size; 383 INIT_LIST_HEAD(&id->link); 384 385 DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n", 386 id->type, id->num, id->size, id->mtd_id); 387 388 /* partition */ 389 part->name = "static"; 390 part->auto_name = 0; 391 392 #if defined(CONFIG_JFFS2_PART_SIZE) 393 part->size = CONFIG_JFFS2_PART_SIZE; 394 #else 395 part->size = SIZE_REMAINING; 396 #endif 397 398 #if defined(CONFIG_JFFS2_PART_OFFSET) 399 part->offset = CONFIG_JFFS2_PART_OFFSET; 400 #else 401 part->offset = 0x00000000; 402 #endif 403 404 part->dev = current_mtd_dev; 405 INIT_LIST_HEAD(&part->link); 406 407 /* recalculate size if needed */ 408 if (part->size == SIZE_REMAINING) 409 part->size = id->size - part->offset; 410 411 part->sector_size = get_part_sector_size(id, part); 412 413 DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n", 414 part->name, part->size, part->offset); 415 416 /* device */ 417 current_mtd_dev->id = id; 418 INIT_LIST_HEAD(¤t_mtd_dev->link); 419 current_mtd_dev->num_parts = 1; 420 INIT_LIST_HEAD(¤t_mtd_dev->parts); 421 list_add(&part->link, ¤t_mtd_dev->parts); 422 } 423 424 return 0; 425 } 426 #endif /* #ifndef CONFIG_CMD_MTDPARTS */ 427 428 /** 429 * Return pointer to the partition of a requested number from a requested 430 * device. 431 * 432 * @param dev device that is to be searched for a partition 433 * @param part_num requested partition number 434 * @return pointer to the part_info, NULL otherwise 435 */ 436 static struct part_info* jffs2_part_info(struct mtd_device *dev, unsigned int part_num) 437 { 438 struct list_head *entry; 439 struct part_info *part; 440 int num; 441 442 if (!dev) 443 return NULL; 444 445 DEBUGF("\n--- jffs2_part_info: partition number %d for device %s%d (%s)\n", 446 part_num, MTD_DEV_TYPE(dev->id->type), 447 dev->id->num, dev->id->mtd_id); 448 449 if (part_num >= dev->num_parts) { 450 printf("invalid partition number %d for device %s%d (%s)\n", 451 part_num, MTD_DEV_TYPE(dev->id->type), 452 dev->id->num, dev->id->mtd_id); 453 return NULL; 454 } 455 456 /* locate partition number, return it */ 457 num = 0; 458 list_for_each(entry, &dev->parts) { 459 part = list_entry(entry, struct part_info, link); 460 461 if (part_num == num++) { 462 return part; 463 } 464 } 465 466 return NULL; 467 } 468 469 /***************************************************/ 470 /* U-Boot commands */ 471 /***************************************************/ 472 473 /** 474 * Routine implementing fsload u-boot command. This routine tries to load 475 * a requested file from jffs2/cramfs filesystem on a current partition. 476 * 477 * @param cmdtp command internal data 478 * @param flag command flag 479 * @param argc number of arguments supplied to the command 480 * @param argv arguments list 481 * @return 0 on success, 1 otherwise 482 */ 483 int do_jffs2_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 484 { 485 char *fsname; 486 char *filename; 487 int size; 488 struct part_info *part; 489 ulong offset = load_addr; 490 491 /* pre-set Boot file name */ 492 filename = env_get("bootfile"); 493 if (!filename) 494 filename = "uImage"; 495 496 if (argc == 2) { 497 filename = argv[1]; 498 } 499 if (argc == 3) { 500 offset = simple_strtoul(argv[1], NULL, 16); 501 load_addr = offset; 502 filename = argv[2]; 503 } 504 505 /* make sure we are in sync with env variables */ 506 if (mtdparts_init() !=0) 507 return 1; 508 509 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){ 510 511 /* check partition type for cramfs */ 512 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2"); 513 printf("### %s loading '%s' to 0x%lx\n", fsname, filename, offset); 514 515 if (cramfs_check(part)) { 516 size = cramfs_load ((char *) offset, part, filename); 517 } else { 518 /* if this is not cramfs assume jffs2 */ 519 size = jffs2_1pass_load((char *)offset, part, filename); 520 } 521 522 if (size > 0) { 523 printf("### %s load complete: %d bytes loaded to 0x%lx\n", 524 fsname, size, offset); 525 env_set_hex("filesize", size); 526 } else { 527 printf("### %s LOAD ERROR<%x> for %s!\n", fsname, size, filename); 528 } 529 530 return !(size > 0); 531 } 532 return 1; 533 } 534 535 /** 536 * Routine implementing u-boot ls command which lists content of a given 537 * directory on a current partition. 538 * 539 * @param cmdtp command internal data 540 * @param flag command flag 541 * @param argc number of arguments supplied to the command 542 * @param argv arguments list 543 * @return 0 on success, 1 otherwise 544 */ 545 int do_jffs2_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 546 { 547 char *filename = "/"; 548 int ret; 549 struct part_info *part; 550 551 if (argc == 2) 552 filename = argv[1]; 553 554 /* make sure we are in sync with env variables */ 555 if (mtdparts_init() !=0) 556 return 1; 557 558 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){ 559 560 /* check partition type for cramfs */ 561 if (cramfs_check(part)) { 562 ret = cramfs_ls (part, filename); 563 } else { 564 /* if this is not cramfs assume jffs2 */ 565 ret = jffs2_1pass_ls(part, filename); 566 } 567 568 return ret ? 0 : 1; 569 } 570 return 1; 571 } 572 573 /** 574 * Routine implementing u-boot fsinfo command. This routine prints out 575 * miscellaneous filesystem informations/statistics. 576 * 577 * @param cmdtp command internal data 578 * @param flag command flag 579 * @param argc number of arguments supplied to the command 580 * @param argv arguments list 581 * @return 0 on success, 1 otherwise 582 */ 583 int do_jffs2_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 584 { 585 struct part_info *part; 586 char *fsname; 587 int ret; 588 589 /* make sure we are in sync with env variables */ 590 if (mtdparts_init() !=0) 591 return 1; 592 593 if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){ 594 595 /* check partition type for cramfs */ 596 fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2"); 597 printf("### filesystem type is %s\n", fsname); 598 599 if (cramfs_check(part)) { 600 ret = cramfs_info (part); 601 } else { 602 /* if this is not cramfs assume jffs2 */ 603 ret = jffs2_1pass_info(part); 604 } 605 606 return ret ? 0 : 1; 607 } 608 return 1; 609 } 610 611 /***************************************************/ 612 U_BOOT_CMD( 613 fsload, 3, 0, do_jffs2_fsload, 614 "load binary file from a filesystem image", 615 "[ off ] [ filename ]\n" 616 " - load binary file from flash bank\n" 617 " with offset 'off'" 618 ); 619 U_BOOT_CMD( 620 fsls, 2, 1, do_jffs2_ls, 621 "list files in a directory (default /)", 622 "[ directory ]" 623 ); 624 625 U_BOOT_CMD( 626 fsinfo, 1, 1, do_jffs2_fsinfo, 627 "print information about filesystems", 628 "" 629 ); 630 /***************************************************/ 631