cmd_nand.c 21 KB

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