cmd_nand.c 19 KB

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