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>
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
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.
21 * The function nand_biterror() in this file is inspired from
22 * mtd-utils/nand-utils/nandflipbits.c which was released under GPLv2
27 #include <bootstage.h>
29 #include <asm/cache.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/rawnand.h>
37 #include <asm/byteorder.h>
38 #include <jffs2/jffs2.h>
41 #include "legacy-mtd-utils.h"
43 #if defined(CONFIG_CMD_MTDPARTS)
45 /* partition handling routines */
46 int mtdparts_init(void);
47 int find_dev_and_part(const char *id, struct mtd_device **dev,
48 u8 *part_num, struct part_info **part);
51 #define MAX_NUM_PAGES 64
53 static int nand_biterror(struct mtd_info *mtd, ulong off, int bit)
58 u_char *datbuf[MAX_NUM_PAGES]; /* Data and OOB */
60 int pages_per_blk = mtd->erasesize / mtd->writesize;
61 struct erase_info einfo;
63 if (pages_per_blk > MAX_NUM_PAGES) {
64 printf("Too many pages in one erase block\n");
68 if (bit < 0 || bit > 7) {
69 printf("bit position 0 to 7 is allowed\n");
74 memset(datbuf, 0, sizeof(datbuf));
75 for (page = 0; page < pages_per_blk ; page++) {
76 datbuf[page] = malloc(mtd->writesize + mtd->oobsize);
78 printf("No memory for page buffer\n");
84 /* Align to erase block boundary */
85 block_off = off & (~(mtd->erasesize - 1));
87 /* Read out memory as first step */
88 for (page = 0; page < pages_per_blk ; page++) {
89 struct mtd_oob_ops ops;
90 loff_t addr = (loff_t)block_off;
92 memset(&ops, 0, sizeof(ops));
93 ops.datbuf = datbuf[page];
94 ops.oobbuf = datbuf[page] + mtd->writesize;
95 ops.len = mtd->writesize;
96 ops.ooblen = mtd->oobsize;
97 ops.mode = MTD_OPS_RAW;
98 ret = mtd_read_oob(mtd, addr, &ops);
100 printf("Error (%d) reading page %08lx\n",
105 block_off += mtd->writesize;
108 /* Erase the block */
109 memset(&einfo, 0, sizeof(einfo));
111 /* Align to erase block boundary */
112 einfo.addr = (loff_t)(off & (~(mtd->erasesize - 1)));
113 einfo.len = mtd->erasesize;
114 ret = mtd_erase(mtd, &einfo);
116 printf("Error (%d) nand_erase_nand page %08llx\n",
122 /* Twist a bit in data part */
123 block_off = off & (mtd->erasesize - 1);
124 data = datbuf[block_off / mtd->writesize][block_off % mtd->writesize];
126 datbuf[block_off / mtd->writesize][block_off % mtd->writesize] = data;
128 printf("Flip data at 0x%lx with xor 0x%02x (bit=%d) to value=0x%02x\n",
129 off, (1 << bit), bit, data);
131 /* Write back twisted data and unmodified OOB */
132 /* Align to erase block boundary */
133 block_off = off & (~(mtd->erasesize - 1));
134 for (page = 0; page < pages_per_blk; page++) {
135 struct mtd_oob_ops ops;
136 loff_t addr = (loff_t)block_off;
138 memset(&ops, 0, sizeof(ops));
139 ops.datbuf = datbuf[page];
140 ops.oobbuf = datbuf[page] + mtd->writesize;
141 ops.len = mtd->writesize;
142 ops.ooblen = mtd->oobsize;
143 ops.mode = MTD_OPS_RAW;
144 ret = mtd_write_oob(mtd, addr, &ops);
146 printf("Error (%d) write page %08lx\n", ret, block_off);
150 block_off += mtd->writesize;
154 for (page = 0; page < pages_per_blk ; page++) {
161 static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
165 u_char *datbuf, *oobbuf, *p;
170 off = last + mtd->writesize;
174 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
176 puts("No memory for page buffer\n");
180 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
182 puts("No memory for page buffer\n");
186 off &= ~(mtd->writesize - 1);
187 loff_t addr = (loff_t) off;
188 struct mtd_oob_ops ops;
189 memset(&ops, 0, sizeof(ops));
192 ops.len = mtd->writesize;
193 ops.ooblen = mtd->oobsize;
194 ops.mode = MTD_OPS_RAW;
195 i = mtd_read_oob(mtd, addr, &ops);
197 printf("Error (%d) reading page %08lx\n", i, off);
201 printf("Page %08lx dump:\n", off);
204 i = mtd->writesize >> 4;
208 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
209 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
210 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
211 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
218 i = mtd->oobsize >> 3;
221 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
222 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
234 /* ------------------------------------------------------------------------- */
236 static int set_dev(int dev)
238 struct mtd_info *mtd = get_nand_dev_by_index(dev);
243 if (nand_curr_device == dev)
246 printf("Device %d: %s", dev, mtd->name);
247 puts("... is now current device\n");
248 nand_curr_device = dev;
250 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
251 board_nand_select_device(mtd_to_nand(mtd), dev);
257 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
258 static void print_status(ulong start, ulong end, ulong erasesize, int status)
261 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
262 * not the same as others. Instead of bit 1 being lock, it is
263 * #lock_tight. To make the driver support either format, ignore bit 1
264 * and use only bit 0 and bit 2.
266 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
269 (end - start) / erasesize,
270 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
271 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
272 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
275 static void do_nand_status(struct mtd_info *mtd)
277 ulong block_start = 0;
279 int last_status = -1;
281 struct nand_chip *nand_chip = mtd_to_nand(mtd);
282 /* check the WP bit */
283 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
284 printf("device is %swrite protected\n",
285 (nand_chip->read_byte(mtd) & 0x80 ?
288 for (off = 0; off < mtd->size; off += mtd->erasesize) {
289 int s = nand_get_lock_status(mtd, off);
291 /* print message only if status has changed */
292 if (s != last_status && off != 0) {
293 print_status(block_start, off, mtd->erasesize,
299 /* Print the last block info */
300 print_status(block_start, off, mtd->erasesize, last_status);
304 #ifdef CONFIG_ENV_OFFSET_OOB
305 unsigned long nand_env_oob_offset;
307 int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
310 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
311 struct mtd_info *mtd = get_nand_dev_by_index(0);
314 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
315 puts("no devices available\n");
321 if (!strcmp(cmd, "get")) {
322 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
326 printf("0x%08lx\n", nand_env_oob_offset);
327 } else if (!strcmp(cmd, "set")) {
330 struct mtd_oob_ops ops;
336 mtd = get_nand_dev_by_index(idx);
337 /* We don't care about size, or maxsize. */
338 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
339 MTD_DEV_TYPE_NAND, mtd->size)) {
340 puts("Offset or partition name expected\n");
344 puts("Offset or partition name expected\n");
349 puts("Partition not on first NAND device\n");
353 if (mtd->oobavail < ENV_OFFSET_SIZE) {
354 printf("Insufficient available OOB bytes:\n"
355 "%d OOB bytes available but %d required for "
357 mtd->oobavail, ENV_OFFSET_SIZE);
361 if ((addr & (mtd->erasesize - 1)) != 0) {
362 printf("Environment offset must be block-aligned\n");
367 ops.mode = MTD_OOB_AUTO;
369 ops.ooblen = ENV_OFFSET_SIZE;
370 ops.oobbuf = (void *) oob_buf;
372 oob_buf[0] = ENV_OOB_MARKER;
373 oob_buf[1] = addr / mtd->erasesize;
375 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
377 printf("Error writing OOB block 0\n");
381 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
383 printf("Error reading env offset in OOB\n");
387 if (addr != nand_env_oob_offset) {
388 printf("Verification of env offset in OOB failed: "
389 "0x%08llx expected but got 0x%08lx\n",
390 (unsigned long long)addr, nand_env_oob_offset);
400 return CMD_RET_USAGE;
405 static void nand_print_and_set_info(int idx)
407 struct mtd_info *mtd;
408 struct nand_chip *chip;
410 mtd = get_nand_dev_by_index(idx);
414 chip = mtd_to_nand(mtd);
415 printf("Device %d: ", idx);
416 if (chip->numchips > 1)
417 printf("%dx ", chip->numchips);
418 printf("%s, sector size %u KiB\n",
419 mtd->name, mtd->erasesize >> 10);
420 printf(" Page size %8d b\n", mtd->writesize);
421 printf(" OOB size %8d b\n", mtd->oobsize);
422 printf(" Erase size %8d b\n", mtd->erasesize);
423 printf(" ecc strength %8d bits\n", mtd->ecc_strength);
424 printf(" ecc step size %8d b\n", mtd->ecc_step_size);
425 printf(" subpagesize %8d b\n", chip->subpagesize);
426 printf(" options 0x%08x\n", chip->options);
427 printf(" bbt options 0x%08x\n", chip->bbt_options);
429 /* Set geometry info */
430 env_set_hex("nand_writesize", mtd->writesize);
431 env_set_hex("nand_oobsize", mtd->oobsize);
432 env_set_hex("nand_erasesize", mtd->erasesize);
435 static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
436 ulong count, int read, int no_verify)
442 mtd_oob_ops_t ops = {
443 .datbuf = (u8 *)addr,
444 .oobbuf = ((u8 *)addr) + mtd->writesize,
445 .len = mtd->writesize,
446 .ooblen = mtd->oobsize,
451 ret = mtd_read_oob(mtd, off, &ops);
453 ret = mtd_write_oob(mtd, off, &ops);
454 if (!ret && !no_verify)
455 ret = nand_verify_page_oob(mtd, &ops, off);
459 printf("%s: error at offset %llx, ret %d\n",
460 __func__, (long long)off, ret);
464 addr += mtd->writesize + mtd->oobsize;
465 off += mtd->writesize;
471 /* Adjust a chip/partition size down for bad blocks so we don't
472 * read/write past the end of a chip/partition by accident.
474 static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
476 /* We grab the nand info object here fresh because this is usually
477 * called after arg_off_size() which can change the value of dev.
479 struct mtd_info *mtd = get_nand_dev_by_index(dev);
480 loff_t maxoffset = offset + *size;
483 /* count badblocks in NAND from offset to offset + size */
484 for (; offset < maxoffset; offset += mtd->erasesize) {
485 if (nand_block_isbad(mtd, offset))
488 /* adjust size if any bad blocks found */
490 *size -= badblocks * mtd->erasesize;
491 printf("size adjusted to 0x%llx (%d bad blocks)\n",
492 (unsigned long long)*size, badblocks);
496 static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
501 loff_t off, size, maxsize;
503 struct mtd_info *mtd;
504 #ifdef CONFIG_SYS_NAND_QUIET
505 int quiet = CONFIG_SYS_NAND_QUIET;
509 const char *quiet_str = env_get("quiet");
510 int dev = nand_curr_device;
511 int repeat = flag & CMD_FLAG_REPEAT;
513 /* at least two arguments please */
518 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
522 /* Only "dump" is repeatable. */
523 if (repeat && strcmp(cmd, "dump"))
526 if (strcmp(cmd, "info") == 0) {
529 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
530 nand_print_and_set_info(i);
534 if (strcmp(cmd, "device") == 0) {
537 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
538 puts("no devices available\n");
540 nand_print_and_set_info(dev);
544 dev = (int)dectoul(argv[2], NULL);
550 #ifdef CONFIG_ENV_OFFSET_OOB
551 /* this command operates only on the first nand device */
552 if (strcmp(cmd, "env.oob") == 0)
553 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
556 /* The following commands operate on the current device, unless
557 * overridden by a partition specifier. Note that if somehow the
558 * current device is invalid, it will have to be changed to a valid
559 * one before these commands can run, even if a partition specifier
560 * for another device is to be used.
562 mtd = get_nand_dev_by_index(dev);
564 puts("\nno devices available\n");
568 if (strcmp(cmd, "bad") == 0) {
569 printf("\nDevice %d bad blocks:\n", dev);
570 for (off = 0; off < mtd->size; off += mtd->erasesize) {
571 ret = nand_block_isbad(mtd, off);
573 printf(" 0x%08llx%s\n", (unsigned long long)off,
574 ret == 2 ? "\t (bbt reserved)" : "");
582 * nand erase [clean] [off size]
584 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
585 nand_erase_options_t opts;
586 /* "clean" at index 2 means request to write cleanmarker */
587 int clean = argc > 2 && !strcmp("clean", argv[2]);
588 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
589 int o = (clean || scrub_yes) ? 3 : 2;
590 int scrub = !strncmp(cmd, "scrub", 5);
593 const char *scrub_warn =
595 "scrub option will erase all factory set bad blocks!\n"
597 "There is no reliable way to recover them.\n"
599 "Use this command only for testing purposes if you\n"
601 "are sure of what you are doing!\n"
602 "\nReally scrub this NAND flash? <y/N>\n";
605 if (!strcmp(&cmd[5], ".spread")) {
607 } else if (!strcmp(&cmd[5], ".part")) {
609 } else if (!strcmp(&cmd[5], ".chip")) {
617 * Don't allow missing arguments to cause full chip/partition
618 * erases -- easy to do accidentally, e.g. with a misspelled
621 if (argc != o + args)
624 printf("\nNAND %s: ", cmd);
625 /* skip first two or three arguments, look for offset and size */
626 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
627 &maxsize, MTD_DEV_TYPE_NAND,
634 mtd = get_nand_dev_by_index(dev);
636 memset(&opts, 0, sizeof(opts));
641 opts.spread = spread;
648 if (confirm_yesno()) {
651 puts("scrub aborted\n");
656 ret = nand_erase_opts(mtd, &opts);
657 printf("%s\n", ret ? "ERROR" : "OK");
659 return ret == 0 ? 0 : 1;
662 if (strncmp(cmd, "dump", 4) == 0) {
666 off = (int)hextoul(argv[2], NULL);
667 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
669 return ret == 0 ? 1 : 0;
672 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
682 addr = (ulong)hextoul(argv[2], NULL);
684 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
685 printf("\nNAND %s: ", read ? "read" : "write");
687 s = strchr(cmd, '.');
689 if (s && !strncmp(s, ".raw", 4)) {
692 if (!strcmp(s, ".raw.noverify"))
695 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
703 mtd = get_nand_dev_by_index(dev);
705 if (argc > 4 && !str2long(argv[4], &pagecount)) {
706 printf("'%s' is not a number\n", argv[4]);
710 if (pagecount * mtd->writesize > size) {
711 puts("Size exceeds partition or device limit\n");
715 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
717 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
726 /* size is unspecified */
728 adjust_size_for_badblocks(&size, off, dev);
732 mtd = get_nand_dev_by_index(dev);
734 if (!s || !strcmp(s, ".jffs2") ||
735 !strcmp(s, ".e") || !strcmp(s, ".i")) {
737 ret = nand_read_skip_bad(mtd, off, &rwsize,
741 ret = nand_write_skip_bad(mtd, off, &rwsize,
745 #ifdef CONFIG_CMD_NAND_TRIMFFS
746 } else if (!strcmp(s, ".trimffs")) {
748 printf("Unknown nand command suffix '%s'\n", s);
751 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
752 maxsize, (u_char *)addr,
753 WITH_DROP_FFS | WITH_WR_VERIFY);
755 } else if (!strcmp(s, ".oob")) {
756 /* out-of-band data */
757 mtd_oob_ops_t ops = {
758 .oobbuf = (u8 *)addr,
764 ret = mtd_read_oob(mtd, off, &ops);
766 ret = mtd_write_oob(mtd, off, &ops);
768 ret = raw_access(mtd, addr, off, pagecount, read,
771 printf("Unknown nand command suffix '%s'.\n", s);
775 printf(" %zu bytes %s: %s\n", rwsize,
776 read ? "read" : "written", ret ? "ERROR" : "OK");
778 return ret == 0 ? 0 : 1;
781 #ifdef CONFIG_CMD_NAND_TORTURE
782 if (strcmp(cmd, "torture") == 0) {
784 unsigned int failed = 0, passed = 0;
789 if (!str2off(argv[2], &off)) {
790 puts("Offset is not a valid number\n");
794 size = mtd->erasesize;
796 if (!str2off(argv[3], &size)) {
797 puts("Size is not a valid number\n");
803 if (endoff > mtd->size) {
804 puts("Arguments beyond end of NAND\n");
808 off = round_down(off, mtd->erasesize);
809 endoff = round_up(endoff, mtd->erasesize);
811 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
812 dev, off, size, mtd->erasesize);
813 while (off < endoff) {
814 ret = nand_torture(mtd, off);
817 printf(" block at 0x%llx failed\n", off);
821 off += mtd->erasesize;
823 printf(" Passed: %u, failed: %u\n", passed, failed);
828 if (strcmp(cmd, "markbad") == 0) {
836 addr = hextoul(*argv, NULL);
838 if (mtd_block_markbad(mtd, addr)) {
839 printf("block 0x%08lx NOT marked "
840 "as bad! ERROR %d\n",
844 printf("block 0x%08lx successfully "
854 if (strcmp(cmd, "biterr") == 0) {
860 off = (int)simple_strtoul(argv[2], NULL, 16);
861 bit = (int)simple_strtoul(argv[3], NULL, 10);
862 ret = nand_biterror(mtd, off, bit);
866 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
867 if (strcmp(cmd, "lock") == 0) {
871 if (!strcmp("tight", argv[2]))
873 if (!strcmp("status", argv[2]))
879 if (!nand_lock(mtd, tight)) {
880 puts("NAND flash successfully locked\n");
882 puts("Error locking NAND flash\n");
889 if (strncmp(cmd, "unlock", 5) == 0) {
892 s = strchr(cmd, '.');
894 if (s && !strcmp(s, ".allexcept"))
897 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
898 &maxsize, MTD_DEV_TYPE_NAND,
905 mtd = get_nand_dev_by_index(dev);
907 if (!nand_unlock(mtd, off, size, allexcept)) {
908 puts("NAND flash successfully unlocked\n");
910 puts("Error unlocking NAND flash, "
911 "write and erase will probably fail\n");
919 return CMD_RET_USAGE;
922 #ifdef CONFIG_SYS_LONGHELP
923 static char nand_help_text[] =
924 "info - show available NAND devices\n"
925 "nand device [dev] - show or set current device\n"
926 "nand read - addr off|partition size\n"
927 "nand write - addr off|partition size\n"
928 " read/write 'size' bytes starting at offset 'off'\n"
929 " to/from memory address 'addr', skipping bad blocks.\n"
930 "nand read.raw - addr off|partition [count]\n"
931 "nand write.raw[.noverify] - addr off|partition [count]\n"
932 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
933 #ifdef CONFIG_CMD_NAND_TRIMFFS
934 "nand write.trimffs - addr off|partition size\n"
935 " write 'size' bytes starting at offset 'off' from memory address\n"
936 " 'addr', skipping bad blocks and dropping any pages at the end\n"
937 " of eraseblocks that contain only 0xFF\n"
939 "nand erase[.spread] [clean] off size - erase 'size' bytes "
940 "from offset 'off'\n"
941 " With '.spread', erase enough for given file size, otherwise,\n"
942 " 'size' includes skipped bad blocks.\n"
943 "nand erase.part [clean] partition - erase entire mtd partition'\n"
944 "nand erase.chip [clean] - erase entire chip'\n"
945 "nand bad - show bad blocks\n"
946 "nand dump[.oob] off - dump page\n"
947 #ifdef CONFIG_CMD_NAND_TORTURE
948 "nand torture off - torture one block at offset\n"
949 "nand torture off [size] - torture blocks from off to off+size\n"
951 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
952 " really clean NAND erasing bad blocks (UNSAFE)\n"
953 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
954 "nand biterr off bit - make a bit error at offset and bit position (UNSAFE)"
955 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
957 "nand lock [tight] [status]\n"
958 " bring nand to lock state or display locked pages\n"
959 "nand unlock[.allexcept] [offset] [size] - unlock section"
961 #ifdef CONFIG_ENV_OFFSET_OOB
963 "nand env.oob - environment offset in OOB of block 0 of"
965 "nand env.oob set off|partition - set enviromnent offset\n"
966 "nand env.oob get - get environment offset"
972 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
973 "NAND sub-system", nand_help_text
976 static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
977 ulong offset, ulong addr, char *cmd)
982 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
983 struct legacy_img_hdr *hdr;
985 #if defined(CONFIG_FIT)
986 const void *fit_hdr = NULL;
989 s = strchr(cmd, '.');
991 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
992 printf("Unknown nand load suffix '%s'\n", s);
993 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
997 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
999 cnt = mtd->writesize;
1000 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
1003 puts("** Read error\n");
1004 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
1007 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
1009 switch (genimg_get_format ((void *)addr)) {
1010 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
1011 case IMAGE_FORMAT_LEGACY:
1012 hdr = (struct legacy_img_hdr *)addr;
1014 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
1015 image_print_contents (hdr);
1017 cnt = image_get_image_size (hdr);
1020 #if defined(CONFIG_FIT)
1021 case IMAGE_FORMAT_FIT:
1022 fit_hdr = (const void *)addr;
1023 puts ("Fit image detected...\n");
1025 cnt = fit_get_size (fit_hdr);
1029 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
1030 puts ("** Unknown image type\n");
1033 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
1035 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
1038 puts("** Read error\n");
1039 bootstage_error(BOOTSTAGE_ID_NAND_READ);
1042 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
1044 #if defined(CONFIG_FIT)
1045 /* This cannot be done earlier, we need complete FIT image in RAM first */
1046 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1047 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1048 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
1049 puts ("** Bad FIT image format\n");
1052 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
1053 fit_print_contents (fit_hdr);
1057 /* Loading ok, update default load address */
1059 image_load_addr = addr;
1061 return bootm_maybe_autostart(cmdtp, cmd);
1064 static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
1067 char *boot_device = NULL;
1069 ulong addr, offset = 0;
1070 struct mtd_info *mtd;
1071 #if defined(CONFIG_CMD_MTDPARTS)
1072 struct mtd_device *dev;
1073 struct part_info *part;
1077 char *p = (argc == 2) ? argv[1] : argv[2];
1078 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
1079 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
1080 if (dev->id->type != MTD_DEV_TYPE_NAND) {
1081 puts("Not a NAND device\n");
1087 addr = hextoul(argv[1], NULL);
1089 addr = CONFIG_SYS_LOAD_ADDR;
1091 mtd = get_nand_dev_by_index(dev->id->num);
1092 return nand_load_image(cmdtp, mtd, part->offset,
1098 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
1101 addr = CONFIG_SYS_LOAD_ADDR;
1102 boot_device = env_get("bootdevice");
1105 addr = hextoul(argv[1], NULL);
1106 boot_device = env_get("bootdevice");
1109 addr = hextoul(argv[1], NULL);
1110 boot_device = argv[2];
1113 addr = hextoul(argv[1], NULL);
1114 boot_device = argv[2];
1115 offset = hextoul(argv[3], NULL);
1118 #if defined(CONFIG_CMD_MTDPARTS)
1121 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
1122 return CMD_RET_USAGE;
1124 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
1127 puts("\n** No boot device **\n");
1128 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1131 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1133 idx = hextoul(boot_device, NULL);
1135 mtd = get_nand_dev_by_index(idx);
1137 printf("\n** Device %d not available\n", idx);
1138 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
1141 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
1143 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
1146 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
1147 "boot from NAND device",
1148 "[partition] | [[[loadAddr] dev] offset]"