2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
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>
38 #include <asm/byteorder.h>
39 #include <jffs2/jffs2.h>
42 #include "legacy-mtd-utils.h"
44 #if defined(CONFIG_CMD_MTDPARTS)
46 /* partition handling routines */
47 int mtdparts_init(void);
48 int find_dev_and_part(const char *id, struct mtd_device **dev,
49 u8 *part_num, struct part_info **part);
52 #define MAX_NUM_PAGES 64
54 static int nand_biterror(struct mtd_info *mtd, ulong off, int bit)
59 u_char *datbuf[MAX_NUM_PAGES]; /* Data and OOB */
61 int pages_per_blk = mtd->erasesize / mtd->writesize;
62 struct erase_info einfo;
64 if (pages_per_blk > MAX_NUM_PAGES) {
65 printf("Too many pages in one erase block\n");
69 if (bit < 0 || bit > 7) {
70 printf("bit position 0 to 7 is allowed\n");
75 memset(datbuf, 0, sizeof(datbuf));
76 for (page = 0; page < pages_per_blk ; page++) {
77 datbuf[page] = malloc(mtd->writesize + mtd->oobsize);
79 printf("No memory for page buffer\n");
85 /* Align to erase block boundary */
86 block_off = off & (~(mtd->erasesize - 1));
88 /* Read out memory as first step */
89 for (page = 0; page < pages_per_blk ; page++) {
90 struct mtd_oob_ops ops;
91 loff_t addr = (loff_t)block_off;
93 memset(&ops, 0, sizeof(ops));
94 ops.datbuf = datbuf[page];
95 ops.oobbuf = datbuf[page] + mtd->writesize;
96 ops.len = mtd->writesize;
97 ops.ooblen = mtd->oobsize;
98 ops.mode = MTD_OPS_RAW;
99 ret = mtd_read_oob(mtd, addr, &ops);
101 printf("Error (%d) reading page %08lx\n",
106 block_off += mtd->writesize;
109 /* Erase the block */
110 memset(&einfo, 0, sizeof(einfo));
112 /* Align to erase block boundary */
113 einfo.addr = (loff_t)(off & (~(mtd->erasesize - 1)));
114 einfo.len = mtd->erasesize;
115 ret = mtd_erase(mtd, &einfo);
117 printf("Error (%d) nand_erase_nand page %08llx\n",
123 /* Twist a bit in data part */
124 block_off = off & (mtd->erasesize - 1);
125 data = datbuf[block_off / mtd->writesize][block_off % mtd->writesize];
127 datbuf[block_off / mtd->writesize][block_off % mtd->writesize] = data;
129 printf("Flip data at 0x%lx with xor 0x%02x (bit=%d) to value=0x%02x\n",
130 off, (1 << bit), bit, data);
132 /* Write back twisted data and unmodified OOB */
133 /* Align to erase block boundary */
134 block_off = off & (~(mtd->erasesize - 1));
135 for (page = 0; page < pages_per_blk; page++) {
136 struct mtd_oob_ops ops;
137 loff_t addr = (loff_t)block_off;
139 memset(&ops, 0, sizeof(ops));
140 ops.datbuf = datbuf[page];
141 ops.oobbuf = datbuf[page] + mtd->writesize;
142 ops.len = mtd->writesize;
143 ops.ooblen = mtd->oobsize;
144 ops.mode = MTD_OPS_RAW;
145 ret = mtd_write_oob(mtd, addr, &ops);
147 printf("Error (%d) write page %08lx\n", ret, block_off);
151 block_off += mtd->writesize;
155 for (page = 0; page < pages_per_blk ; page++) {
162 static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
166 u_char *datbuf, *oobbuf, *p;
171 off = last + mtd->writesize;
175 datbuf = memalign(ARCH_DMA_MINALIGN, mtd->writesize);
177 puts("No memory for page buffer\n");
181 oobbuf = memalign(ARCH_DMA_MINALIGN, mtd->oobsize);
183 puts("No memory for page buffer\n");
187 off &= ~(mtd->writesize - 1);
188 loff_t addr = (loff_t) off;
189 struct mtd_oob_ops ops;
190 memset(&ops, 0, sizeof(ops));
193 ops.len = mtd->writesize;
194 ops.ooblen = mtd->oobsize;
195 ops.mode = MTD_OPS_RAW;
196 i = mtd_read_oob(mtd, addr, &ops);
198 printf("Error (%d) reading page %08lx\n", i, off);
202 printf("Page %08lx dump:\n", off);
205 i = mtd->writesize >> 4;
209 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
210 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
211 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
212 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
219 i = mtd->oobsize >> 3;
222 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
223 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
235 /* ------------------------------------------------------------------------- */
237 static int set_dev(int dev)
239 struct mtd_info *mtd = get_nand_dev_by_index(dev);
244 if (nand_curr_device == dev)
247 printf("Device %d: %s", dev, mtd->name);
248 puts("... is now current device\n");
249 nand_curr_device = dev;
251 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
252 board_nand_select_device(mtd_to_nand(mtd), dev);
258 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
259 static void print_status(ulong start, ulong end, ulong erasesize, int status)
262 * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
263 * not the same as others. Instead of bit 1 being lock, it is
264 * #lock_tight. To make the driver support either format, ignore bit 1
265 * and use only bit 0 and bit 2.
267 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
270 (end - start) / erasesize,
271 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
272 (!(status & NAND_LOCK_STATUS_UNLOCK) ? "LOCK " : ""),
273 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
276 static void do_nand_status(struct mtd_info *mtd)
278 ulong block_start = 0;
280 int last_status = -1;
282 struct nand_chip *nand_chip = mtd_to_nand(mtd);
283 /* check the WP bit */
284 nand_chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
285 printf("device is %swrite protected\n",
286 (nand_chip->read_byte(mtd) & 0x80 ?
289 for (off = 0; off < mtd->size; off += mtd->erasesize) {
290 int s = nand_get_lock_status(mtd, off);
292 /* print message only if status has changed */
293 if (s != last_status && off != 0) {
294 print_status(block_start, off, mtd->erasesize,
300 /* Print the last block info */
301 print_status(block_start, off, mtd->erasesize, last_status);
305 #ifdef CONFIG_ENV_OFFSET_OOB
306 unsigned long nand_env_oob_offset;
308 int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
311 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
312 struct mtd_info *mtd = get_nand_dev_by_index(0);
315 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !mtd) {
316 puts("no devices available\n");
322 if (!strcmp(cmd, "get")) {
323 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
327 printf("0x%08lx\n", nand_env_oob_offset);
328 } else if (!strcmp(cmd, "set")) {
331 struct mtd_oob_ops ops;
337 mtd = get_nand_dev_by_index(idx);
338 /* We don't care about size, or maxsize. */
339 if (mtd_arg_off(argv[2], &idx, &addr, &maxsize, &maxsize,
340 MTD_DEV_TYPE_NAND, mtd->size)) {
341 puts("Offset or partition name expected\n");
345 puts("Offset or partition name expected\n");
350 puts("Partition not on first NAND device\n");
354 if (mtd->oobavail < ENV_OFFSET_SIZE) {
355 printf("Insufficient available OOB bytes:\n"
356 "%d OOB bytes available but %d required for "
358 mtd->oobavail, ENV_OFFSET_SIZE);
362 if ((addr & (mtd->erasesize - 1)) != 0) {
363 printf("Environment offset must be block-aligned\n");
368 ops.mode = MTD_OOB_AUTO;
370 ops.ooblen = ENV_OFFSET_SIZE;
371 ops.oobbuf = (void *) oob_buf;
373 oob_buf[0] = ENV_OOB_MARKER;
374 oob_buf[1] = addr / mtd->erasesize;
376 ret = mtd->write_oob(mtd, ENV_OFFSET_SIZE, &ops);
378 printf("Error writing OOB block 0\n");
382 ret = get_nand_env_oob(mtd, &nand_env_oob_offset);
384 printf("Error reading env offset in OOB\n");
388 if (addr != nand_env_oob_offset) {
389 printf("Verification of env offset in OOB failed: "
390 "0x%08llx expected but got 0x%08lx\n",
391 (unsigned long long)addr, nand_env_oob_offset);
401 return CMD_RET_USAGE;
406 static void nand_print_and_set_info(int idx)
408 struct mtd_info *mtd;
409 struct nand_chip *chip;
411 mtd = get_nand_dev_by_index(idx);
415 chip = mtd_to_nand(mtd);
416 printf("Device %d: ", idx);
417 if (chip->numchips > 1)
418 printf("%dx ", chip->numchips);
419 printf("%s, sector size %u KiB\n",
420 mtd->name, mtd->erasesize >> 10);
421 printf(" Page size %8d b\n", mtd->writesize);
422 printf(" OOB size %8d b\n", mtd->oobsize);
423 printf(" Erase size %8d b\n", mtd->erasesize);
424 printf(" ecc strength %8d bits\n", mtd->ecc_strength);
425 printf(" ecc step size %8d b\n", mtd->ecc_step_size);
426 printf(" subpagesize %8d b\n", chip->subpagesize);
427 printf(" options 0x%08x\n", chip->options);
428 printf(" bbt options 0x%08x\n", chip->bbt_options);
430 /* Set geometry info */
431 env_set_hex("nand_writesize", mtd->writesize);
432 env_set_hex("nand_oobsize", mtd->oobsize);
433 env_set_hex("nand_erasesize", mtd->erasesize);
436 static int raw_access(struct mtd_info *mtd, void *buf, loff_t off,
437 ulong count, int read, int no_verify)
443 mtd_oob_ops_t ops = {
445 .oobbuf = buf + mtd->writesize,
446 .len = mtd->writesize,
447 .ooblen = mtd->oobsize,
452 ret = mtd_read_oob(mtd, off, &ops);
454 ret = mtd_write_oob(mtd, off, &ops);
455 if (!ret && !no_verify)
456 ret = nand_verify_page_oob(mtd, &ops, off);
460 printf("%s: error at offset %llx, ret %d\n",
461 __func__, (long long)off, ret);
465 buf += mtd->writesize + mtd->oobsize;
466 off += mtd->writesize;
472 /* Adjust a chip/partition size down for bad blocks so we don't
473 * read/write past the end of a chip/partition by accident.
475 static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
477 /* We grab the nand info object here fresh because this is usually
478 * called after arg_off_size() which can change the value of dev.
480 struct mtd_info *mtd = get_nand_dev_by_index(dev);
481 loff_t maxoffset = offset + *size;
484 /* count badblocks in NAND from offset to offset + size */
485 for (; offset < maxoffset; offset += mtd->erasesize) {
486 if (nand_block_isbad(mtd, offset))
489 /* adjust size if any bad blocks found */
491 *size -= badblocks * mtd->erasesize;
492 printf("size adjusted to 0x%llx (%d bad blocks)\n",
493 (unsigned long long)*size, badblocks);
497 static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
502 loff_t off, size, maxsize;
504 struct mtd_info *mtd;
505 #ifdef CONFIG_SYS_NAND_QUIET
506 int quiet = CONFIG_SYS_NAND_QUIET;
510 const char *quiet_str = env_get("quiet");
511 int dev = nand_curr_device;
512 int repeat = flag & CMD_FLAG_REPEAT;
514 /* at least two arguments please */
519 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
523 /* Only "dump" is repeatable. */
524 if (repeat && strcmp(cmd, "dump"))
527 if (strcmp(cmd, "info") == 0) {
530 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
531 nand_print_and_set_info(i);
535 if (strcmp(cmd, "device") == 0) {
538 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
539 puts("no devices available\n");
541 nand_print_and_set_info(dev);
545 dev = (int)dectoul(argv[2], NULL);
551 #ifdef CONFIG_ENV_OFFSET_OOB
552 /* this command operates only on the first nand device */
553 if (strcmp(cmd, "env.oob") == 0)
554 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
557 /* The following commands operate on the current device, unless
558 * overridden by a partition specifier. Note that if somehow the
559 * current device is invalid, it will have to be changed to a valid
560 * one before these commands can run, even if a partition specifier
561 * for another device is to be used.
563 mtd = get_nand_dev_by_index(dev);
565 puts("\nno devices available\n");
569 if (strcmp(cmd, "bad") == 0) {
570 printf("\nDevice %d bad blocks:\n", dev);
571 for (off = 0; off < mtd->size; off += mtd->erasesize) {
572 ret = nand_block_isbad(mtd, off);
574 printf(" 0x%08llx%s\n", (unsigned long long)off,
575 ret == 2 ? "\t (bbt reserved)" : "");
583 * nand erase [clean] [off size]
585 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
586 nand_erase_options_t opts;
587 /* "clean" at index 2 means request to write cleanmarker */
588 int clean = argc > 2 && !strcmp("clean", argv[2]);
589 int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
590 int o = (clean || scrub_yes) ? 3 : 2;
591 int scrub = !strncmp(cmd, "scrub", 5);
594 const char *scrub_warn =
596 "scrub option will erase all factory set bad blocks!\n"
598 "There is no reliable way to recover them.\n"
600 "Use this command only for testing purposes if you\n"
602 "are sure of what you are doing!\n"
603 "\nReally scrub this NAND flash? <y/N>\n";
606 if (!strcmp(&cmd[5], ".spread")) {
608 } else if (!strcmp(&cmd[5], ".part")) {
610 } else if (!strcmp(&cmd[5], ".chip")) {
618 * Don't allow missing arguments to cause full chip/partition
619 * erases -- easy to do accidentally, e.g. with a misspelled
622 if (argc != o + args)
625 printf("\nNAND %s: ", cmd);
626 /* skip first two or three arguments, look for offset and size */
627 if (mtd_arg_off_size(argc - o, argv + o, &dev, &off, &size,
628 &maxsize, MTD_DEV_TYPE_NAND,
635 mtd = get_nand_dev_by_index(dev);
637 memset(&opts, 0, sizeof(opts));
642 opts.spread = spread;
649 if (confirm_yesno()) {
652 puts("scrub aborted\n");
657 ret = nand_erase_opts(mtd, &opts);
658 printf("%s\n", ret ? "ERROR" : "OK");
660 return ret == 0 ? 0 : 1;
663 if (strncmp(cmd, "dump", 4) == 0) {
667 off = (int)hextoul(argv[2], NULL);
668 ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
670 return ret == 0 ? 1 : 0;
673 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
684 addr = (ulong)hextoul(argv[2], NULL);
686 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
687 printf("\nNAND %s: ", read ? "read" : "write");
689 s = strchr(cmd, '.');
691 if (s && !strncmp(s, ".raw", 4)) {
694 if (!strcmp(s, ".raw.noverify"))
697 if (mtd_arg_off(argv[3], &dev, &off, &size, &maxsize,
705 mtd = get_nand_dev_by_index(dev);
707 if (argc > 4 && !str2long(argv[4], &pagecount)) {
708 printf("'%s' is not a number\n", argv[4]);
712 if (pagecount * mtd->writesize > size) {
713 puts("Size exceeds partition or device limit\n");
717 rwsize = pagecount * (mtd->writesize + mtd->oobsize);
719 if (mtd_arg_off_size(argc - 3, argv + 3, &dev, &off,
728 /* size is unspecified */
730 adjust_size_for_badblocks(&size, off, dev);
734 mtd = get_nand_dev_by_index(dev);
735 buf = map_sysmem(addr, maxsize);
737 if (!s || !strcmp(s, ".jffs2") ||
738 !strcmp(s, ".e") || !strcmp(s, ".i")) {
740 ret = nand_read_skip_bad(mtd, off, &rwsize,
743 ret = nand_write_skip_bad(mtd, off, &rwsize,
746 #ifdef CONFIG_CMD_NAND_TRIMFFS
747 } else if (!strcmp(s, ".trimffs")) {
749 printf("Unknown nand command suffix '%s'\n", s);
753 ret = nand_write_skip_bad(mtd, off, &rwsize, NULL,
755 WITH_DROP_FFS | WITH_WR_VERIFY);
757 } else if (!strcmp(s, ".oob")) {
758 /* out-of-band data */
759 mtd_oob_ops_t ops = {
766 ret = mtd_read_oob(mtd, off, &ops);
768 ret = mtd_write_oob(mtd, off, &ops);
770 ret = raw_access(mtd, buf, off, pagecount, read,
773 printf("Unknown nand command suffix '%s'.\n", s);
779 printf(" %zu bytes %s: %s\n", rwsize,
780 read ? "read" : "written", ret ? "ERROR" : "OK");
782 return ret == 0 ? 0 : 1;
785 #ifdef CONFIG_CMD_NAND_TORTURE
786 if (strcmp(cmd, "torture") == 0) {
788 unsigned int failed = 0, passed = 0;
793 if (!str2off(argv[2], &off)) {
794 puts("Offset is not a valid number\n");
798 size = mtd->erasesize;
800 if (!str2off(argv[3], &size)) {
801 puts("Size is not a valid number\n");
807 if (endoff > mtd->size) {
808 puts("Arguments beyond end of NAND\n");
812 off = round_down(off, mtd->erasesize);
813 endoff = round_up(endoff, mtd->erasesize);
815 printf("\nNAND torture: device %d offset 0x%llx size 0x%llx (block size 0x%x)\n",
816 dev, off, size, mtd->erasesize);
817 while (off < endoff) {
818 ret = nand_torture(mtd, off);
821 printf(" block at 0x%llx failed\n", off);
825 off += mtd->erasesize;
827 printf(" Passed: %u, failed: %u\n", passed, failed);
832 if (strcmp(cmd, "markbad") == 0) {
840 addr = hextoul(*argv, NULL);
842 if (mtd_block_markbad(mtd, addr)) {
843 printf("block 0x%08lx NOT marked "
844 "as bad! ERROR %d\n",
848 printf("block 0x%08lx successfully "
858 if (strcmp(cmd, "biterr") == 0) {
864 off = (int)simple_strtoul(argv[2], NULL, 16);
865 bit = (int)simple_strtoul(argv[3], NULL, 10);
866 ret = nand_biterror(mtd, off, bit);
870 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
871 if (strcmp(cmd, "lock") == 0) {
875 if (!strcmp("tight", argv[2]))
877 if (!strcmp("status", argv[2]))
883 if (!nand_lock(mtd, tight)) {
884 puts("NAND flash successfully locked\n");
886 puts("Error locking NAND flash\n");
893 if (strncmp(cmd, "unlock", 5) == 0) {
896 s = strchr(cmd, '.');
898 if (s && !strcmp(s, ".allexcept"))
901 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &off, &size,
902 &maxsize, MTD_DEV_TYPE_NAND,
909 mtd = get_nand_dev_by_index(dev);
911 if (!nand_unlock(mtd, off, size, allexcept)) {
912 puts("NAND flash successfully unlocked\n");
914 puts("Error unlocking NAND flash, "
915 "write and erase will probably fail\n");
923 return CMD_RET_USAGE;
926 U_BOOT_LONGHELP(nand,
927 "info - show available NAND devices\n"
928 "nand device [dev] - show or set current device\n"
929 "nand read - addr off|partition size\n"
930 "nand write - addr off|partition size\n"
931 " read/write 'size' bytes starting at offset 'off'\n"
932 " to/from memory address 'addr', skipping bad blocks.\n"
933 "nand read.raw - addr off|partition [count]\n"
934 "nand write.raw[.noverify] - addr off|partition [count]\n"
935 " Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
936 #ifdef CONFIG_CMD_NAND_TRIMFFS
937 "nand write.trimffs - addr off|partition size\n"
938 " write 'size' bytes starting at offset 'off' from memory address\n"
939 " 'addr', skipping bad blocks and dropping any pages at the end\n"
940 " of eraseblocks that contain only 0xFF\n"
942 "nand erase[.spread] [clean] off size - erase 'size' bytes "
943 "from offset 'off'\n"
944 " With '.spread', erase enough for given file size, otherwise,\n"
945 " 'size' includes skipped bad blocks.\n"
946 "nand erase.part [clean] partition - erase entire mtd partition'\n"
947 "nand erase.chip [clean] - erase entire chip'\n"
948 "nand bad - show bad blocks\n"
949 "nand dump[.oob] off - dump page\n"
950 #ifdef CONFIG_CMD_NAND_TORTURE
951 "nand torture off - torture one block at offset\n"
952 "nand torture off [size] - torture blocks from off to off+size\n"
954 "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
955 " really clean NAND erasing bad blocks (UNSAFE)\n"
956 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
957 "nand biterr off bit - make a bit error at offset and bit position (UNSAFE)"
958 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
960 "nand lock [tight] [status]\n"
961 " bring nand to lock state or display locked pages\n"
962 "nand unlock[.allexcept] [offset] [size] - unlock section"
964 #ifdef CONFIG_ENV_OFFSET_OOB
966 "nand env.oob - environment offset in OOB of block 0 of"
968 "nand env.oob set off|partition - set enviromnent offset\n"
969 "nand env.oob get - get environment offset"
974 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
975 "NAND sub-system", nand_help_text
978 static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
979 ulong offset, ulong addr, char *cmd)
984 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
985 struct legacy_img_hdr *hdr;
987 #if defined(CONFIG_FIT)
988 const void *fit_hdr = NULL;
991 s = strchr(cmd, '.');
993 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
994 printf("Unknown nand load suffix '%s'\n", s);
995 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
999 printf("\nLoading from %s, offset 0x%lx\n", mtd->name, offset);
1001 cnt = mtd->writesize;
1002 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
1005 puts("** Read error\n");
1006 bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
1009 bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
1011 switch (genimg_get_format ((void *)addr)) {
1012 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
1013 case IMAGE_FORMAT_LEGACY:
1014 hdr = (struct legacy_img_hdr *)addr;
1016 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
1017 image_print_contents (hdr);
1019 cnt = image_get_image_size (hdr);
1022 #if defined(CONFIG_FIT)
1023 case IMAGE_FORMAT_FIT:
1024 fit_hdr = (const void *)addr;
1025 puts ("Fit image detected...\n");
1027 cnt = fit_get_size (fit_hdr);
1031 bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
1032 puts ("** Unknown image type\n");
1035 bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
1037 r = nand_read_skip_bad(mtd, offset, &cnt, NULL, mtd->size,
1040 puts("** Read error\n");
1041 bootstage_error(BOOTSTAGE_ID_NAND_READ);
1044 bootstage_mark(BOOTSTAGE_ID_NAND_READ);
1046 #if defined(CONFIG_FIT)
1047 /* This cannot be done earlier, we need complete FIT image in RAM first */
1048 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1049 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1050 bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
1051 puts ("** Bad FIT image format\n");
1054 bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
1055 fit_print_contents (fit_hdr);
1059 /* Loading ok, update default load address */
1061 image_load_addr = addr;
1063 return bootm_maybe_autostart(cmdtp, cmd);
1066 static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
1069 char *boot_device = NULL;
1071 ulong addr, offset = 0;
1072 struct mtd_info *mtd;
1073 #if defined(CONFIG_CMD_MTDPARTS)
1074 struct mtd_device *dev;
1075 struct part_info *part;
1079 char *p = (argc == 2) ? argv[1] : argv[2];
1080 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
1081 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
1082 if (dev->id->type != MTD_DEV_TYPE_NAND) {
1083 puts("Not a NAND device\n");
1089 addr = hextoul(argv[1], NULL);
1091 addr = CONFIG_SYS_LOAD_ADDR;
1093 mtd = get_nand_dev_by_index(dev->id->num);
1094 return nand_load_image(cmdtp, mtd, part->offset,
1100 bootstage_mark(BOOTSTAGE_ID_NAND_PART);
1103 addr = CONFIG_SYS_LOAD_ADDR;
1104 boot_device = env_get("bootdevice");
1107 addr = hextoul(argv[1], NULL);
1108 boot_device = env_get("bootdevice");
1111 addr = hextoul(argv[1], NULL);
1112 boot_device = argv[2];
1115 addr = hextoul(argv[1], NULL);
1116 boot_device = argv[2];
1117 offset = hextoul(argv[3], NULL);
1120 #if defined(CONFIG_CMD_MTDPARTS)
1123 bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
1124 return CMD_RET_USAGE;
1126 bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
1129 puts("\n** No boot device **\n");
1130 bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1133 bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
1135 idx = hextoul(boot_device, NULL);
1137 mtd = get_nand_dev_by_index(idx);
1139 printf("\n** Device %d not available\n", idx);
1140 bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
1143 bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
1145 return nand_load_image(cmdtp, mtd, offset, addr, argv[0]);
1148 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
1149 "boot from NAND device",
1150 "[partition] | [[[loadAddr] dev] offset]"