nand.c 24 KB

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