nand.c 24 KB

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