1 /* 2 * Driver for NAND support, Rick Bronson 3 * borrowed heavily from: 4 * (c) 1999 Machine Vision Holdings, Inc. 5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org> 6 * 7 * Ported 'dynenv' to 'nand env.oob' command 8 * (C) 2010 Nanometrics, Inc. 9 * 'dynenv' -- Dynamic environment offset in NAND OOB 10 * (C) Copyright 2006-2007 OpenMoko, Inc. 11 * Added 16-bit nand support 12 * (C) 2004 Texas Instruments 13 * 14 * Copyright 2010, 2012 Freescale Semiconductor 15 * The portions of this file whose copyright is held by Freescale and which 16 * are not considered a derived work of GPL v2-only code may be distributed 17 * and/or modified under the terms of the GNU General Public License as 18 * published by the Free Software Foundation; either version 2 of the 19 * License, or (at your option) any later version. 20 */ 21 22 #include <common.h> 23 #include <linux/mtd/mtd.h> 24 #include <command.h> 25 #include <console.h> 26 #include <watchdog.h> 27 #include <malloc.h> 28 #include <asm/byteorder.h> 29 #include <jffs2/jffs2.h> 30 #include <nand.h> 31 32 #if defined(CONFIG_CMD_MTDPARTS) 33 34 /* partition handling routines */ 35 int mtdparts_init(void); 36 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num); 37 int find_dev_and_part(const char *id, struct mtd_device **dev, 38 u8 *part_num, struct part_info **part); 39 #endif 40 41 static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob, 42 int repeat) 43 { 44 int i; 45 u_char *datbuf, *oobbuf, *p; 46 static loff_t last; 47 int ret = 0; 48 49 if (repeat) 50 off = last + mtd->writesize; 51 52 last = off; 53 54 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize); 55 if (!datbuf) { 56 puts("No memory for page buffer\n"); 57 return 1; 58 } 59 60 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize); 61 if (!oobbuf) { 62 puts("No memory for page buffer\n"); 63 ret = 1; 64 goto free_dat; 65 } 66 off &= ~(mtd->writesize - 1); 67 loff_t addr = (loff_t) off; 68 struct mtd_oob_ops ops; 69 memset(&ops, 0, sizeof(ops)); 70 ops.datbuf = datbuf; 71 ops.oobbuf = oobbuf; 72 ops.len = mtd->writesize; 73 ops.ooblen = mtd->oobsize; 74 ops.mode = MTD_OPS_RAW; 75 i = mtd_read_oob(mtd, addr, &ops); 76 if (i < 0) { 77 printf("Error (%d) reading page %08lx\n", i, off); 78 ret = 1; 79 goto free_all; 80 } 81 printf("Page %08lx dump:\n", off); 82 83 if (!only_oob) { 84 i = mtd->writesize >> 4; 85 p = datbuf; 86 87 while (i--) { 88 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x" 89 " %02x %02x %02x %02x %02x %02x %02x %02x\n", 90 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 91 p[8], p[9], p[10], p[11], p[12], p[13], p[14], 92 p[15]); 93 p += 16; 94 } 95 } 96 97 puts("OOB:\n"); 98 i = mtd->oobsize >> 3; 99 p = oobbuf; 100 while (i--) { 101 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n", 102 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 103 p += 8; 104 } 105 106 free_all: 107 free(oobbuf); 108 free_dat: 109 free(datbuf); 110 111 return ret; 112 } 113 114 /* ------------------------------------------------------------------------- */ 115 116 static int set_dev(int dev) 117 { 118 struct mtd_info *mtd = get_nand_dev_by_index(dev); 119 120 if (!mtd) 121 return -ENODEV; 122 123 if (nand_curr_device == dev) 124 return 0; 125 126 printf("Device %d: %s", dev, mtd->name); 127 puts("... is now current device\n"); 128 nand_curr_device = dev; 129 130 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE 131 board_nand_select_device(mtd_to_nand(mtd), dev); 132 #endif 133 134 return 0; 135 } 136 137 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK 138 static void print_status(ulong start, ulong end, ulong erasesize, int status) 139 { 140 /* 141 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is 142 * not the same as others. Instead of bit 1 being lock, it is 143 * #lock_tight. To make the driver support either format, ignore bit 1 144 * and use only bit 0 and bit 2. 145 */ 146 printf("%08lx - %08lx: %08lx blocks %s%s%s\n", 147 start, 148 end - 1, 149 (end - start) / erasesize, 150 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""), 151 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""), 152 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : "")); 153 } 154 155 static void do_nand_status(struct mtd_info *mtd) 156 { 157 ulong block_start = 0; 158 ulong off; 159 int last_status = -1; 160 161 struct nand_chip *nand_chip = mtd_to_nand(mtd); 162 /* check the WP bit */ 163 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); 164 printf("device is %swrite protected\n", 165 (nand_chip->read_byte(mtd) & 0x80 ? 166 "NOT " : "")); 167 168 for (off = 0; off < mtd->size; off += mtd->erasesize) { 169 int s = nand_get_lock_status(mtd, off); 170 171 /* print message only if status has changed */ 172 if (s != last_status && off != 0) { 173 print_status(block_start, off, mtd->erasesize, 174 last_status); 175 block_start = off; 176 } 177 last_status = s; 178 } 179 /* Print the last block info */ 180 print_status(block_start, off, mtd->erasesize, last_status); 181 } 182 #endif 183 184 #ifdef CONFIG_ENV_OFFSET_OOB 185 unsigned long nand_env_oob_offset; 186 187 int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[]) 188 { 189 int ret; 190 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)]; 191 struct mtd_info *mtd = get_nand_dev_by_index(0); 192 char *cmd = argv[1]; 193 194 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) { 195 puts("no devices available\n"); 196 return 1; 197 } 198 199 set_dev(0); 200 201 if (!strcmp(cmd, "get")) { 202 ret = get_nand_env_oob(mtd, &nand_env_oob_offset); 203 if (ret) 204 return 1; 205 206 printf("0x%08lx\n", nand_env_oob_offset); 207 } else if (!strcmp(cmd, "set")) { 208 loff_t addr; 209 loff_t maxsize; 210 struct mtd_oob_ops ops; 211 int idx = 0; 212 213 if (argc < 3) 214 goto usage; 215 216 mtd = get_nand_dev_by_index(idx); 217 /* We don't care about size, or maxsize. */ 218 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize, 219 MTD_DEV_TYPE_NAND, mtd->size)) { 220 puts("Offset or partition name expected\n"); 221 return 1; 222 } 223 if (set_dev(idx)) { 224 puts("Offset or partition name expected\n"); 225 return 1; 226 } 227 228 if (idx != 0) { 229 puts("Partition not on first NAND device\n"); 230 return 1; 231 } 232 233 if (mtd->oobavail < ENV_OFFSET_SIZE) { 234 printf("Insufficient available OOB bytes:\n" 235 "%d OOB bytes available but %d required for " 236 "env.oob support\n", 237 mtd->oobavail, ENV_OFFSET_SIZE); 238 return 1; 239 } 240 241 if ((addr & (mtd->erasesize - 1)) != 0) { 242 printf("Environment offset must be block-aligned\n"); 243 return 1; 244 } 245 246 ops.datbuf = NULL; 247 ops.mode = MTD_OOB_AUTO; 248 ops.ooboffs = 0; 249 ops.ooblen = ENV_OFFSET_SIZE; 250 ops.oobbuf = (void *) oob_buf; 251 252 oob_buf[0] = ENV_OOB_MARKER; 253 oob_buf[1] = addr / mtd->erasesize; 254 255 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops); 256 if (ret) { 257 printf("Error writing OOB block 0\n"); 258 return ret; 259 } 260 261 ret = get_nand_env_oob(mtd, &nand_env_oob_offset); 262 if (ret) { 263 printf("Error reading env offset in OOB\n"); 264 return ret; 265 } 266 267 if (addr != nand_env_oob_offset) { 268 printf("Verification of env offset in OOB failed: " 269 "0x%08llx expected but got 0x%08lx\n", 270 (unsigned long long)addr, nand_env_oob_offset); 271 return 1; 272 } 273 } else { 274 goto usage; 275 } 276 277 return ret; 278 279 usage: 280 return CMD_RET_USAGE; 281 } 282 283 #endif 284 285 static void nand_print_and_set_info(int idx) 286 { 287 struct mtd_info *mtd; 288 struct nand_chip *chip; 289 290 mtd = get_nand_dev_by_index(idx); 291 if (!mtd) 292 return; 293 294 chip = mtd_to_nand(mtd); 295 printf("Device %d: ", idx); 296 if (chip->numchips > 1) 297 printf("%dx ", chip->numchips); 298 printf("%s, sector size %u KiB\n", 299 mtd->name, mtd->erasesize >> 10); 300 printf(" Page size %8d b\n", mtd->writesize); 301 printf(" OOB size %8d b\n", mtd->oobsize); 302 printf(" Erase size %8d b\n", mtd->erasesize); 303 printf(" subpagesize %8d b\n", chip->subpagesize); 304 printf(" options 0x%08x\n", chip->options); 305 printf(" bbt options 0x%08x\n", chip->bbt_options); 306 307 /* Set geometry info */ 308 env_set_hex("nand_writesize", mtd->writesize); 309 env_set_hex("nand_oobsize", mtd->oobsize); 310 env_set_hex("nand_erasesize", mtd->erasesize); 311 } 312 313 static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off, 314 ulong count, int read, int no_verify) 315 { 316 int ret = 0; 317 318 while (count--) { 319 /* Raw access */ 320 mtd_oob_ops_t ops = { 321 .datbuf = (u8 *)addr, 322 .oobbuf = ((u8 *)addr) + mtd->writesize, 323 .len = mtd->writesize, 324 .ooblen = mtd->oobsize, 325 .mode = MTD_OPS_RAW 326 }; 327 328 if (read) { 329 ret = mtd_read_oob(mtd, off, &ops); 330 } else { 331 ret = mtd_write_oob(mtd, off, &ops); 332 if (!ret && !no_verify) 333 ret = nand_verify_page_oob(mtd, &ops, off); 334 } 335 336 if (ret) { 337 printf("%s: error at offset %llx, ret %d\n", 338 __func__, (long long)off, ret); 339 break; 340 } 341 342 addr += mtd->writesize + mtd->oobsize; 343 off += mtd->writesize; 344 } 345 346 return ret; 347 } 348 349 /* Adjust a chip/partition size down for bad blocks so we don't 350 * read/write past the end of a chip/partition by accident. 351 */ 352 static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev) 353 { 354 /* We grab the nand info object here fresh because this is usually 355 * called after arg_off_size() which can change the value of dev. 356 */ 357 struct mtd_info *mtd = get_nand_dev_by_index(dev); 358 loff_t maxoffset = offset + *size; 359 int badblocks = 0; 360 361 /* count badblocks in NAND from offset to offset + size */ 362 for (; offset < maxoffset; offset += mtd->erasesize) { 363 if (nand_block_isbad(mtd, offset)) 364 badblocks++; 365 } 366 /* adjust size if any bad blocks found */ 367 if (badblocks) { 368 *size -= badblocks * mtd->erasesize; 369 printf("size adjusted to 0x%llx (%d bad blocks)\n", 370 (unsigned long long)*size, badblocks); 371 } 372 } 373 374 static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 375 { 376 int i, ret = 0; 377 ulong addr; 378 loff_t off, size, maxsize; 379 char *cmd, *s; 380 struct mtd_info *mtd; 381 #ifdef CONFIG_SYS_NAND_QUIET 382 int quiet = CONFIG_SYS_NAND_QUIET; 383 #else 384 int quiet = 0; 385 #endif 386 const char *quiet_str = env_get("quiet"); 387 int dev = nand_curr_device; 388 int repeat = flag & CMD_FLAG_REPEAT; 389 390 /* at least two arguments please */ 391 if (argc < 2) 392 goto usage; 393 394 if (quiet_str) 395 quiet = simple_strtoul(quiet_str, NULL, 0) != 0; 396 397 cmd = argv[1]; 398 399 /* Only "dump" is repeatable. */ 400 if (repeat && strcmp(cmd, "dump")) 401 return 0; 402 403 if (strcmp(cmd, "info") == 0) { 404 405 putc('\n'); 406 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) 407 nand_print_and_set_info(i); 408 return 0; 409 } 410 411 if (strcmp(cmd, "device") == 0) { 412 if (argc < 3) { 413 putc('\n'); 414 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE) 415 puts("no devices available\n"); 416 else 417 nand_print_and_set_info(dev); 418 return 0; 419 } 420 421 dev = (int)simple_strtoul(argv[2], NULL, 10); 422 set_dev(dev); 423 424 return 0; 425 } 426 427 #ifdef CONFIG_ENV_OFFSET_OOB 428 /* this command operates only on the first nand device */ 429 if (strcmp(cmd, "env.oob") == 0) 430 return do_nand_env_oob(cmdtp, argc - 1, argv + 1); 431 #endif 432 433 /* The following commands operate on the current device, unless 434 * overridden by a partition specifier. Note that if somehow the 435 * current device is invalid, it will have to be changed to a valid 436 * one before these commands can run, even if a partition specifier 437 * for another device is to be used. 438 */ 439 mtd = get_nand_dev_by_index(dev); 440 if (!mtd) { 441 puts("\nno devices available\n"); 442 return 1; 443 } 444 445 if (strcmp(cmd, "bad") == 0) { 446 printf("\nDevice %d bad blocks:\n", dev); 447 for (off = 0; off < mtd->size; off += mtd->erasesize) 448 if (nand_block_isbad(mtd, off)) 449 printf(" %08llx\n", (unsigned long long)off); 450 return 0; 451 } 452 453 /* 454 * Syntax is: 455 * 0 1 2 3 4 456 * nand erase [clean] [off size] 457 */ 458 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) { 459 nand_erase_options_t opts; 460 /* "clean" at index 2 means request to write cleanmarker */ 461 int clean = argc > 2 && !strcmp("clean", argv[2]); 462 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]); 463 int o = (clean || scrub_yes) ? 3 : 2; 464 int scrub = !strncmp(cmd, "scrub", 5); 465 int spread = 0; 466 int args = 2; 467 const char *scrub_warn = 468 "Warning: " 469 "scrub option will erase all factory set bad blocks!\n" 470 " " 471 "There is no reliable way to recover them.\n" 472 " " 473 "Use this command only for testing purposes if you\n" 474 " " 475 "are sure of what you are doing!\n" 476 "\nReally scrub this NAND flash? <y/N>\n"; 477 478 if (cmd[5] != 0) { 479 if (!strcmp(&cmd[5], ".spread")) { 480 spread = 1; 481 } else if (!strcmp(&cmd[5], ".part")) { 482 args = 1; 483 } else if (!strcmp(&cmd[5], ".chip")) { 484 args = 0; 485 } else { 486 goto usage; 487 } 488 } 489 490 /* 491 * Don't allow missing arguments to cause full chip/partition 492 * erases -- easy to do accidentally, e.g. with a misspelled 493 * variable name. 494 */ 495 if (argc != o + args) 496 goto usage; 497 498 printf("\nNAND %s: ", cmd); 499 /* skip first two or three arguments, look for offset and size */ 500 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size, 501 &maxsize, MTD_DEV_TYPE_NAND, 502 mtd->size) != 0) 503 return 1; 504 505 if (set_dev(dev)) 506 return 1; 507 508 mtd = get_nand_dev_by_index(dev); 509 510 memset(&opts, 0, sizeof(opts)); 511 opts.offset = off; 512 opts.length = size; 513 opts.jffs2 = clean; 514 opts.quiet = quiet; 515 opts.spread = spread; 516 517 if (scrub) { 518 if (scrub_yes) { 519 opts.scrub = 1; 520 } else { 521 puts(scrub_warn); 522 if (confirm_yesno()) { 523 opts.scrub = 1; 524 } else { 525 puts("scrub aborted\n"); 526 return 1; 527 } 528 } 529 } 530 ret = nand_erase_opts(mtd, &opts); 531 printf("%s\n", ret ? "ERROR" : "OK"); 532 533 return ret == 0 ? 0 : 1; 534 } 535 536 if (strncmp(cmd, "dump", 4) == 0) { 537 if (argc < 3) 538 goto usage; 539 540 off = (int)simple_strtoul(argv[2], NULL, 16); 541 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat); 542 543 return ret == 0 ? 1 : 0; 544 } 545 546 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) { 547 size_t rwsize; 548 ulong pagecount = 1; 549 int read; 550 int raw = 0; 551 int no_verify = 0; 552 553 if (argc < 4) 554 goto usage; 555 556 addr = (ulong)simple_strtoul(argv[2], NULL, 16); 557 558 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */ 559 printf("\nNAND %s: ", read ? "read" : "write"); 560 561 s = strchr(cmd, '.'); 562 563 if (s && !strncmp(s, ".raw", 4)) { 564 raw = 1; 565 566 if (!strcmp(s, ".raw.noverify")) 567 no_verify = 1; 568 569 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize, 570 MTD_DEV_TYPE_NAND, 571 mtd->size)) 572 return 1; 573 574 if (set_dev(dev)) 575 return 1; 576 577 mtd = get_nand_dev_by_index(dev); 578 579 if (argc > 4 && !str2long(argv[4], &pagecount)) { 580 printf("'%s' is not a number\n", argv[4]); 581 return 1; 582 } 583 584 if (strncmp(cmd, "readbyte", 8) == 0 || strncmp(cmd, "writebyte", 9) == 0) { 585 if (pagecount % (mtd->writesize + mtd->oobsize)) { 586 printf("Count=%ld should be aligned with (writesize + oobsize)\n", pagecount); 587 return -1; 588 } 589 pagecount = pagecount / (mtd->writesize + mtd->oobsize); 590 } 591 592 if (pagecount * mtd->writesize > size) { 593 puts("Size exceeds partition or device limit\n"); 594 return -1; 595 } 596 597 rwsize = pagecount * (mtd->writesize + mtd->oobsize); 598 } else { 599 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off, 600 &size, &maxsize, 601 MTD_DEV_TYPE_NAND, 602 mtd->size) != 0) 603 return 1; 604 605 if (set_dev(dev)) 606 return 1; 607 608 /* size is unspecified */ 609 if (argc < 5) 610 adjust_size_for_badblocks(&size, off, dev); 611 rwsize = size; 612 } 613 614 mtd = get_nand_dev_by_index(dev); 615 616 if (!s || !strcmp(s, ".jffs2") || 617 !strcmp(s, ".e") || !strcmp(s, ".i")) { 618 if (read) 619 ret = nand_read_skip_bad(mtd, off, &rwsize, 620 NULL, maxsize, 621 (u_char *)addr); 622 else 623 ret = nand_write_skip_bad(mtd, off, &rwsize, 624 NULL, maxsize, 625 (u_char *)addr, 626 WITH_WR_VERIFY); 627 #ifdef CONFIG_CMD_NAND_TRIMFFS 628 } else if (!strcmp(s, ".trimffs")) { 629 if (read) { 630 printf("Unknown nand command suffix '%s'\n", s); 631 return 1; 632 } 633 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL, 634 maxsize, (u_char *)addr, 635 WITH_DROP_FFS | WITH_WR_VERIFY); 636 #endif 637 } else if (!strcmp(s, ".oob")) { 638 /* out-of-band data */ 639 mtd_oob_ops_t ops = { 640 .oobbuf = (u8 *)addr, 641 .ooblen = rwsize, 642 .mode = MTD_OPS_RAW 643 }; 644 645 if (read) 646 ret = mtd_read_oob(mtd, off, &ops); 647 else 648 ret = mtd_write_oob(mtd, off, &ops); 649 } else if (raw) { 650 ret = raw_access(mtd, addr, off, pagecount, read, 651 no_verify); 652 } else { 653 printf("Unknown nand command suffix '%s'.\n", s); 654 return 1; 655 } 656 657 printf(" %zu bytes %s: %s\n", rwsize, 658 read ? "read" : "written", ret ? "ERROR" : "OK"); 659 660 return ret == 0 ? 0 : 1; 661 } 662 663 #ifdef CONFIG_CMD_NAND_TORTURE 664 if (strcmp(cmd, "torture") == 0) { 665 loff_t endoff; 666 unsigned int failed = 0, passed = 0; 667 668 if (argc < 3) 669 goto usage; 670 671 if (!str2off(argv[2], &off)) { 672 puts("Offset is not a valid number\n"); 673 return 1; 674 } 675 676 size = mtd->erasesize; 677 if (argc > 3) { 678 if (!str2off(argv[3], &size)) { 679 puts("Size is not a valid number\n"); 680 return 1; 681 } 682 } 683 684 endoff = off + size; 685 if (endoff > mtd->size) { 686 puts("Arguments beyond end of NAND\n"); 687 return 1; 688 } 689 690 off = round_down(off, mtd->erasesize); 691 endoff = round_up(endoff, mtd->erasesize); 692 size = endoff - off; 693 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n", 694 dev, off, size, mtd->erasesize); 695 while (off < endoff) { 696 ret = nand_torture(mtd, off); 697 if (ret) { 698 failed++; 699 printf(" block at 0x%llx failed\n", off); 700 } else { 701 passed++; 702 } 703 off += mtd->erasesize; 704 } 705 printf(" Passed: %u, failed: %u\n", passed, failed); 706 return failed != 0; 707 } 708 #endif 709 710 if (strcmp(cmd, "markbad") == 0) { 711 argc -= 2; 712 argv += 2; 713 714 if (argc <= 0) 715 goto usage; 716 717 while (argc > 0) { 718 addr = simple_strtoul(*argv, NULL, 16); 719 720 if (mtd_block_markbad(mtd, addr)) { 721 printf("block 0x%08lx NOT marked " 722 "as bad! ERROR %d\n", 723 addr, ret); 724 ret = 1; 725 } else { 726 printf("block 0x%08lx successfully " 727 "marked as bad\n", 728 addr); 729 } 730 --argc; 731 ++argv; 732 } 733 return ret; 734 } 735 736 if (strcmp(cmd, "biterr") == 0) { 737 /* todo */ 738 return 1; 739 } 740 741 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK 742 if (strcmp(cmd, "lock") == 0) { 743 int tight = 0; 744 int status = 0; 745 if (argc == 3) { 746 if (!strcmp("tight", argv[2])) 747 tight = 1; 748 if (!strcmp("status", argv[2])) 749 status = 1; 750 } 751 if (status) { 752 do_nand_status(mtd); 753 } else { 754 if (!nand_lock(mtd, tight)) { 755 puts("NAND flash successfully locked\n"); 756 } else { 757 puts("Error locking NAND flash\n"); 758 return 1; 759 } 760 } 761 return 0; 762 } 763 764 if (strncmp(cmd, "unlock", 5) == 0) { 765 int allexcept = 0; 766 767 s = strchr(cmd, '.'); 768 769 if (s && !strcmp(s, ".allexcept")) 770 allexcept = 1; 771 772 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size, 773 &maxsize, MTD_DEV_TYPE_NAND, 774 mtd->size) < 0) 775 return 1; 776 777 if (set_dev(dev)) 778 return 1; 779 780 mtd = get_nand_dev_by_index(dev); 781 782 if (!nand_unlock(mtd, off, size, allexcept)) { 783 puts("NAND flash successfully unlocked\n"); 784 } else { 785 puts("Error unlocking NAND flash, " 786 "write and erase will probably fail\n"); 787 return 1; 788 } 789 return 0; 790 } 791 #endif 792 793 usage: 794 return CMD_RET_USAGE; 795 } 796 797 #ifdef CONFIG_SYS_LONGHELP 798 static char nand_help_text[] = 799 "info - show available NAND devices\n" 800 "nand device [dev] - show or set current device\n" 801 "nand read - addr off|partition size\n" 802 "nand write - addr off|partition size\n" 803 " read/write 'size' bytes starting at offset 'off'\n" 804 " to/from memory address 'addr', skipping bad blocks.\n" 805 "nand read.raw - addr off|partition [count]\n" 806 "nand write.raw[.noverify] - addr off|partition [count]\n" 807 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n" 808 #ifdef CONFIG_CMD_NAND_TRIMFFS 809 "nand write.trimffs - addr off|partition size\n" 810 " write 'size' bytes starting at offset 'off' from memory address\n" 811 " 'addr', skipping bad blocks and dropping any pages at the end\n" 812 " of eraseblocks that contain only 0xFF\n" 813 #endif 814 "nand erase[.spread] [clean] off size - erase 'size' bytes " 815 "from offset 'off'\n" 816 " With '.spread', erase enough for given file size, otherwise,\n" 817 " 'size' includes skipped bad blocks.\n" 818 "nand erase.part [clean] partition - erase entire mtd partition'\n" 819 "nand erase.chip [clean] - erase entire chip'\n" 820 "nand bad - show bad blocks\n" 821 "nand dump[.oob] off - dump page\n" 822 #ifdef CONFIG_CMD_NAND_TORTURE 823 "nand torture off - torture one block at offset\n" 824 "nand torture off [size] - torture blocks from off to off+size\n" 825 #endif 826 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n" 827 " really clean NAND erasing bad blocks (UNSAFE)\n" 828 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n" 829 "nand biterr off - make a bit error at offset (UNSAFE)" 830 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK 831 "\n" 832 "nand lock [tight] [status]\n" 833 " bring nand to lock state or display locked pages\n" 834 "nand unlock[.allexcept] [offset] [size] - unlock section" 835 #endif 836 #ifdef CONFIG_ENV_OFFSET_OOB 837 "\n" 838 "nand env.oob - environment offset in OOB of block 0 of" 839 " first device.\n" 840 "nand env.oob set off|partition - set enviromnent offset\n" 841 "nand env.oob get - get environment offset" 842 #endif 843 ""; 844 #endif 845 846 U_BOOT_CMD( 847 nand, CONFIG_SYS_MAXARGS, 1, do_nand, 848 "NAND sub-system", nand_help_text 849 ); 850 851 static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd, 852 ulong offset, ulong addr, char *cmd) 853 { 854 int r; 855 char *s; 856 size_t cnt; 857 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 858 image_header_t *hdr; 859 #endif 860 #if defined(CONFIG_FIT) 861 const void *fit_hdr = NULL; 862 #endif 863 864 s = strchr(cmd, '.'); 865 if (s != NULL && 866 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) { 867 printf("Unknown nand load suffix '%s'\n", s); 868 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); 869 return 1; 870 } 871 872 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset); 873 874 cnt = mtd->writesize; 875 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size, 876 (u_char *)addr); 877 if (r) { 878 puts("** Read error\n"); 879 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ); 880 return 1; 881 } 882 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ); 883 884 switch (genimg_get_format ((void *)addr)) { 885 #if defined(CONFIG_IMAGE_FORMAT_LEGACY) 886 case IMAGE_FORMAT_LEGACY: 887 hdr = (image_header_t *)addr; 888 889 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); 890 image_print_contents (hdr); 891 892 cnt = image_get_image_size (hdr); 893 break; 894 #endif 895 #if defined(CONFIG_FIT) 896 case IMAGE_FORMAT_FIT: 897 fit_hdr = (const void *)addr; 898 puts ("Fit image detected...\n"); 899 900 cnt = fit_get_size (fit_hdr); 901 break; 902 #endif 903 default: 904 bootstage_error(BOOTSTAGE_ID_NAND_TYPE); 905 puts ("** Unknown image type\n"); 906 return 1; 907 } 908 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE); 909 910 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size, 911 (u_char *)addr); 912 if (r) { 913 puts("** Read error\n"); 914 bootstage_error(BOOTSTAGE_ID_NAND_READ); 915 return 1; 916 } 917 bootstage_mark(BOOTSTAGE_ID_NAND_READ); 918 919 #if defined(CONFIG_FIT) 920 /* This cannot be done earlier, we need complete FIT image in RAM first */ 921 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) { 922 if (!fit_check_format (fit_hdr)) { 923 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ); 924 puts ("** Bad FIT image format\n"); 925 return 1; 926 } 927 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK); 928 fit_print_contents (fit_hdr); 929 } 930 #endif 931 932 /* Loading ok, update default load address */ 933 934 load_addr = addr; 935 936 return bootm_maybe_autostart(cmdtp, cmd); 937 } 938 939 static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc, 940 char * const argv[]) 941 { 942 char *boot_device = NULL; 943 int idx; 944 ulong addr, offset = 0; 945 struct mtd_info *mtd; 946 #if defined(CONFIG_CMD_MTDPARTS) 947 struct mtd_device *dev; 948 struct part_info *part; 949 u8 pnum; 950 951 if (argc >= 2) { 952 char *p = (argc == 2) ? argv[1] : argv[2]; 953 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) && 954 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) { 955 if (dev->id->type != MTD_DEV_TYPE_NAND) { 956 puts("Not a NAND device\n"); 957 return 1; 958 } 959 if (argc > 3) 960 goto usage; 961 if (argc == 3) 962 addr = simple_strtoul(argv[1], NULL, 16); 963 else 964 addr = CONFIG_SYS_LOAD_ADDR; 965 966 mtd = get_nand_dev_by_index(dev->id->num); 967 return nand_load_image(cmdtp, mtd, part->offset, 968 addr, argv[0]); 969 } 970 } 971 #endif 972 973 bootstage_mark(BOOTSTAGE_ID_NAND_PART); 974 switch (argc) { 975 case 1: 976 addr = CONFIG_SYS_LOAD_ADDR; 977 boot_device = env_get("bootdevice"); 978 break; 979 case 2: 980 addr = simple_strtoul(argv[1], NULL, 16); 981 boot_device = env_get("bootdevice"); 982 break; 983 case 3: 984 addr = simple_strtoul(argv[1], NULL, 16); 985 boot_device = argv[2]; 986 break; 987 case 4: 988 addr = simple_strtoul(argv[1], NULL, 16); 989 boot_device = argv[2]; 990 offset = simple_strtoul(argv[3], NULL, 16); 991 break; 992 default: 993 #if defined(CONFIG_CMD_MTDPARTS) 994 usage: 995 #endif 996 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX); 997 return CMD_RET_USAGE; 998 } 999 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX); 1000 1001 if (!boot_device) { 1002 puts("\n** No boot device **\n"); 1003 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE); 1004 return 1; 1005 } 1006 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE); 1007 1008 idx = simple_strtoul(boot_device, NULL, 16); 1009 1010 mtd = get_nand_dev_by_index(idx); 1011 if (!mtd) { 1012 printf("\n** Device %d not available\n", idx); 1013 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE); 1014 return 1; 1015 } 1016 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE); 1017 1018 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]); 1019 } 1020 1021 U_BOOT_CMD(nboot, 4, 1, do_nandboot, 1022 "boot from NAND device", 1023 "[partition] | [[[loadAddr] dev] offset]" 1024 ); 1025