nand_util.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * drivers/mtd/nand/nand_util.c
  3. *
  4. * Copyright (C) 2006 by Weiss-Electronic GmbH.
  5. * All rights reserved.
  6. *
  7. * @author: Guido Classen <clagix@gmail.com>
  8. * @descr: NAND Flash support
  9. * @references: borrowed heavily from Linux mtd-utils code:
  10. * flash_eraseall.c by Arcom Control System Ltd
  11. * nandwrite.c by Steven J. Hill (sjhill@realitydiluted.com)
  12. * and Thomas Gleixner (tglx@linutronix.de)
  13. *
  14. * Copyright (C) 2008 Nokia Corporation: drop_ffs() function by
  15. * Artem Bityutskiy <dedekind1@gmail.com> from mtd-utils
  16. *
  17. * Copyright 2010 Freescale Semiconductor
  18. *
  19. * SPDX-License-Identifier: GPL-2.0
  20. */
  21. #include <common.h>
  22. #include <command.h>
  23. #include <watchdog.h>
  24. #include <malloc.h>
  25. #include <div64.h>
  26. #include <asm/errno.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <nand.h>
  29. #include <jffs2/jffs2.h>
  30. typedef struct erase_info erase_info_t;
  31. typedef struct mtd_info mtd_info_t;
  32. /* support only for native endian JFFS2 */
  33. #define cpu_to_je16(x) (x)
  34. #define cpu_to_je32(x) (x)
  35. /**
  36. * nand_erase_opts: - erase NAND flash with support for various options
  37. * (jffs2 formatting)
  38. *
  39. * @param meminfo NAND device to erase
  40. * @param opts options, @see struct nand_erase_options
  41. * @return 0 in case of success
  42. *
  43. * This code is ported from flash_eraseall.c from Linux mtd utils by
  44. * Arcom Control System Ltd.
  45. */
  46. int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
  47. {
  48. struct jffs2_unknown_node cleanmarker;
  49. erase_info_t erase;
  50. unsigned long erase_length, erased_length; /* in blocks */
  51. int result;
  52. int percent_complete = -1;
  53. const char *mtd_device = meminfo->name;
  54. struct mtd_oob_ops oob_opts;
  55. struct nand_chip *chip = meminfo->priv;
  56. if ((opts->offset & (meminfo->erasesize - 1)) != 0) {
  57. printf("Attempt to erase non block-aligned data\n");
  58. return -1;
  59. }
  60. memset(&erase, 0, sizeof(erase));
  61. memset(&oob_opts, 0, sizeof(oob_opts));
  62. erase.mtd = meminfo;
  63. erase.len = meminfo->erasesize;
  64. erase.addr = opts->offset;
  65. erase_length = lldiv(opts->length + meminfo->erasesize - 1,
  66. meminfo->erasesize);
  67. cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  68. cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
  69. cleanmarker.totlen = cpu_to_je32(8);
  70. /* scrub option allows to erase badblock. To prevent internal
  71. * check from erase() method, set block check method to dummy
  72. * and disable bad block table while erasing.
  73. */
  74. if (opts->scrub) {
  75. erase.scrub = opts->scrub;
  76. /*
  77. * We don't need the bad block table anymore...
  78. * after scrub, there are no bad blocks left!
  79. */
  80. if (chip->bbt) {
  81. kfree(chip->bbt);
  82. }
  83. chip->bbt = NULL;
  84. }
  85. for (erased_length = 0;
  86. erased_length < erase_length;
  87. erase.addr += meminfo->erasesize) {
  88. WATCHDOG_RESET();
  89. if (opts->lim && (erase.addr >= (opts->offset + opts->lim))) {
  90. puts("Size of erase exceeds limit\n");
  91. return -EFBIG;
  92. }
  93. if (!opts->scrub) {
  94. int ret = mtd_block_isbad(meminfo, erase.addr);
  95. if (ret > 0) {
  96. if (!opts->quiet)
  97. printf("\rSkipping bad block at "
  98. "0x%08llx "
  99. " \n",
  100. erase.addr);
  101. if (!opts->spread)
  102. erased_length++;
  103. continue;
  104. } else if (ret < 0) {
  105. printf("\n%s: MTD get bad block failed: %d\n",
  106. mtd_device,
  107. ret);
  108. return -1;
  109. }
  110. }
  111. erased_length++;
  112. result = mtd_erase(meminfo, &erase);
  113. if (result != 0) {
  114. printf("\n%s: MTD Erase failure: %d\n",
  115. mtd_device, result);
  116. continue;
  117. }
  118. /* format for JFFS2 ? */
  119. if (opts->jffs2 && chip->ecc.layout->oobavail >= 8) {
  120. struct mtd_oob_ops ops;
  121. ops.ooblen = 8;
  122. ops.datbuf = NULL;
  123. ops.oobbuf = (uint8_t *)&cleanmarker;
  124. ops.ooboffs = 0;
  125. ops.mode = MTD_OPS_AUTO_OOB;
  126. result = mtd_write_oob(meminfo,
  127. erase.addr,
  128. &ops);
  129. if (result != 0) {
  130. printf("\n%s: MTD writeoob failure: %d\n",
  131. mtd_device, result);
  132. continue;
  133. }
  134. }
  135. if (!opts->quiet) {
  136. unsigned long long n = erased_length * 100ULL;
  137. int percent;
  138. do_div(n, erase_length);
  139. percent = (int)n;
  140. /* output progress message only at whole percent
  141. * steps to reduce the number of messages printed
  142. * on (slow) serial consoles
  143. */
  144. if (percent != percent_complete) {
  145. percent_complete = percent;
  146. printf("\rErasing at 0x%llx -- %3d%% complete.",
  147. erase.addr, percent);
  148. if (opts->jffs2 && result == 0)
  149. printf(" Cleanmarker written at 0x%llx.",
  150. erase.addr);
  151. }
  152. }
  153. }
  154. if (!opts->quiet)
  155. printf("\n");
  156. if (opts->scrub)
  157. chip->scan_bbt(meminfo);
  158. return 0;
  159. }
  160. #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
  161. #define NAND_CMD_LOCK_TIGHT 0x2c
  162. #define NAND_CMD_LOCK_STATUS 0x7a
  163. /******************************************************************************
  164. * Support for locking / unlocking operations of some NAND devices
  165. *****************************************************************************/
  166. /**
  167. * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
  168. * state
  169. *
  170. * @param mtd nand mtd instance
  171. * @param tight bring device in lock tight mode
  172. *
  173. * @return 0 on success, -1 in case of error
  174. *
  175. * The lock / lock-tight command only applies to the whole chip. To get some
  176. * parts of the chip lock and others unlocked use the following sequence:
  177. *
  178. * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
  179. * - Call nand_unlock() once for each consecutive area to be unlocked
  180. * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
  181. *
  182. * If the device is in lock-tight state software can't change the
  183. * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
  184. * calls will fail. It is only posible to leave lock-tight state by
  185. * an hardware signal (low pulse on _WP pin) or by power down.
  186. */
  187. int nand_lock(struct mtd_info *mtd, int tight)
  188. {
  189. int ret = 0;
  190. int status;
  191. struct nand_chip *chip = mtd->priv;
  192. /* select the NAND device */
  193. chip->select_chip(mtd, 0);
  194. /* check the Lock Tight Status */
  195. chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, 0);
  196. if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
  197. printf("nand_lock: Device is locked tight!\n");
  198. ret = -1;
  199. goto out;
  200. }
  201. chip->cmdfunc(mtd,
  202. (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
  203. -1, -1);
  204. /* call wait ready function */
  205. status = chip->waitfunc(mtd, chip);
  206. /* see if device thinks it succeeded */
  207. if (status & 0x01) {
  208. ret = -1;
  209. }
  210. out:
  211. /* de-select the NAND device */
  212. chip->select_chip(mtd, -1);
  213. return ret;
  214. }
  215. /**
  216. * nand_get_lock_status: - query current lock state from one page of NAND
  217. * flash
  218. *
  219. * @param mtd nand mtd instance
  220. * @param offset page address to query (must be page-aligned!)
  221. *
  222. * @return -1 in case of error
  223. * >0 lock status:
  224. * bitfield with the following combinations:
  225. * NAND_LOCK_STATUS_TIGHT: page in tight state
  226. * NAND_LOCK_STATUS_UNLOCK: page unlocked
  227. *
  228. */
  229. int nand_get_lock_status(struct mtd_info *mtd, loff_t offset)
  230. {
  231. int ret = 0;
  232. int chipnr;
  233. int page;
  234. struct nand_chip *chip = mtd->priv;
  235. /* select the NAND device */
  236. chipnr = (int)(offset >> chip->chip_shift);
  237. chip->select_chip(mtd, chipnr);
  238. if ((offset & (mtd->writesize - 1)) != 0) {
  239. printf("nand_get_lock_status: "
  240. "Start address must be beginning of "
  241. "nand page!\n");
  242. ret = -1;
  243. goto out;
  244. }
  245. /* check the Lock Status */
  246. page = (int)(offset >> chip->page_shift);
  247. chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
  248. ret = chip->read_byte(mtd) & (NAND_LOCK_STATUS_TIGHT
  249. | NAND_LOCK_STATUS_UNLOCK);
  250. out:
  251. /* de-select the NAND device */
  252. chip->select_chip(mtd, -1);
  253. return ret;
  254. }
  255. /**
  256. * nand_unlock: - Unlock area of NAND pages
  257. * only one consecutive area can be unlocked at one time!
  258. *
  259. * @param mtd nand mtd instance
  260. * @param start start byte address
  261. * @param length number of bytes to unlock (must be a multiple of
  262. * page size nand->writesize)
  263. * @param allexcept if set, unlock everything not selected
  264. *
  265. * @return 0 on success, -1 in case of error
  266. */
  267. int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
  268. int allexcept)
  269. {
  270. int ret = 0;
  271. int chipnr;
  272. int status;
  273. int page;
  274. struct nand_chip *chip = mtd->priv;
  275. debug("nand_unlock%s: start: %08llx, length: %zd!\n",
  276. allexcept ? " (allexcept)" : "", start, length);
  277. /* select the NAND device */
  278. chipnr = (int)(start >> chip->chip_shift);
  279. chip->select_chip(mtd, chipnr);
  280. /* check the WP bit */
  281. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  282. if (!(chip->read_byte(mtd) & NAND_STATUS_WP)) {
  283. printf("nand_unlock: Device is write protected!\n");
  284. ret = -1;
  285. goto out;
  286. }
  287. /* check the Lock Tight Status */
  288. page = (int)(start >> chip->page_shift);
  289. chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
  290. if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
  291. printf("nand_unlock: Device is locked tight!\n");
  292. ret = -1;
  293. goto out;
  294. }
  295. if ((start & (mtd->erasesize - 1)) != 0) {
  296. printf("nand_unlock: Start address must be beginning of "
  297. "nand block!\n");
  298. ret = -1;
  299. goto out;
  300. }
  301. if (length == 0 || (length & (mtd->erasesize - 1)) != 0) {
  302. printf("nand_unlock: Length must be a multiple of nand block "
  303. "size %08x!\n", mtd->erasesize);
  304. ret = -1;
  305. goto out;
  306. }
  307. /*
  308. * Set length so that the last address is set to the
  309. * starting address of the last block
  310. */
  311. length -= mtd->erasesize;
  312. /* submit address of first page to unlock */
  313. chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
  314. /* submit ADDRESS of LAST page to unlock */
  315. page += (int)(length >> chip->page_shift);
  316. /*
  317. * Page addresses for unlocking are supposed to be block-aligned.
  318. * At least some NAND chips use the low bit to indicate that the
  319. * page range should be inverted.
  320. */
  321. if (allexcept)
  322. page |= 1;
  323. chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, page & chip->pagemask);
  324. /* call wait ready function */
  325. status = chip->waitfunc(mtd, chip);
  326. /* see if device thinks it succeeded */
  327. if (status & 0x01) {
  328. /* there was an error */
  329. ret = -1;
  330. goto out;
  331. }
  332. out:
  333. /* de-select the NAND device */
  334. chip->select_chip(mtd, -1);
  335. return ret;
  336. }
  337. #endif
  338. /**
  339. * check_skip_len
  340. *
  341. * Check if there are any bad blocks, and whether length including bad
  342. * blocks fits into device
  343. *
  344. * @param nand NAND device
  345. * @param offset offset in flash
  346. * @param length image length
  347. * @param used length of flash needed for the requested length
  348. * @return 0 if the image fits and there are no bad blocks
  349. * 1 if the image fits, but there are bad blocks
  350. * -1 if the image does not fit
  351. */
  352. static int check_skip_len(nand_info_t *nand, loff_t offset, size_t length,
  353. size_t *used)
  354. {
  355. size_t len_excl_bad = 0;
  356. int ret = 0;
  357. while (len_excl_bad < length) {
  358. size_t block_len, block_off;
  359. loff_t block_start;
  360. if (offset >= nand->size)
  361. return -1;
  362. block_start = offset & ~(loff_t)(nand->erasesize - 1);
  363. block_off = offset & (nand->erasesize - 1);
  364. block_len = nand->erasesize - block_off;
  365. if (!nand_block_isbad(nand, block_start))
  366. len_excl_bad += block_len;
  367. else
  368. ret = 1;
  369. offset += block_len;
  370. *used += block_len;
  371. }
  372. /* If the length is not a multiple of block_len, adjust. */
  373. if (len_excl_bad > length)
  374. *used -= (len_excl_bad - length);
  375. return ret;
  376. }
  377. #ifdef CONFIG_CMD_NAND_TRIMFFS
  378. static size_t drop_ffs(const nand_info_t *nand, const u_char *buf,
  379. const size_t *len)
  380. {
  381. size_t l = *len;
  382. ssize_t i;
  383. for (i = l - 1; i >= 0; i--)
  384. if (buf[i] != 0xFF)
  385. break;
  386. /* The resulting length must be aligned to the minimum flash I/O size */
  387. l = i + 1;
  388. l = (l + nand->writesize - 1) / nand->writesize;
  389. l *= nand->writesize;
  390. /*
  391. * since the input length may be unaligned, prevent access past the end
  392. * of the buffer
  393. */
  394. return min(l, *len);
  395. }
  396. #endif
  397. /**
  398. * nand_write_skip_bad:
  399. *
  400. * Write image to NAND flash.
  401. * Blocks that are marked bad are skipped and the is written to the next
  402. * block instead as long as the image is short enough to fit even after
  403. * skipping the bad blocks. Due to bad blocks we may not be able to
  404. * perform the requested write. In the case where the write would
  405. * extend beyond the end of the NAND device, both length and actual (if
  406. * not NULL) are set to 0. In the case where the write would extend
  407. * beyond the limit we are passed, length is set to 0 and actual is set
  408. * to the required length.
  409. *
  410. * @param nand NAND device
  411. * @param offset offset in flash
  412. * @param length buffer length
  413. * @param actual set to size required to write length worth of
  414. * buffer or 0 on error, if not NULL
  415. * @param lim maximum size that actual may be in order to not
  416. * exceed the buffer
  417. * @param buffer buffer to read from
  418. * @param flags flags modifying the behaviour of the write to NAND
  419. * @return 0 in case of success
  420. */
  421. int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  422. size_t *actual, loff_t lim, u_char *buffer, int flags)
  423. {
  424. int rval = 0, blocksize;
  425. size_t left_to_write = *length;
  426. size_t used_for_write = 0;
  427. u_char *p_buffer = buffer;
  428. int need_skip;
  429. if (actual)
  430. *actual = 0;
  431. #ifdef CONFIG_CMD_NAND_YAFFS
  432. if (flags & WITH_YAFFS_OOB) {
  433. if (flags & ~WITH_YAFFS_OOB)
  434. return -EINVAL;
  435. int pages;
  436. pages = nand->erasesize / nand->writesize;
  437. blocksize = (pages * nand->oobsize) + nand->erasesize;
  438. if (*length % (nand->writesize + nand->oobsize)) {
  439. printf("Attempt to write incomplete page"
  440. " in yaffs mode\n");
  441. return -EINVAL;
  442. }
  443. } else
  444. #endif
  445. {
  446. blocksize = nand->erasesize;
  447. }
  448. /*
  449. * nand_write() handles unaligned, partial page writes.
  450. *
  451. * We allow length to be unaligned, for convenience in
  452. * using the $filesize variable.
  453. *
  454. * However, starting at an unaligned offset makes the
  455. * semantics of bad block skipping ambiguous (really,
  456. * you should only start a block skipping access at a
  457. * partition boundary). So don't try to handle that.
  458. */
  459. if ((offset & (nand->writesize - 1)) != 0) {
  460. printf("Attempt to write non page-aligned data\n");
  461. *length = 0;
  462. return -EINVAL;
  463. }
  464. need_skip = check_skip_len(nand, offset, *length, &used_for_write);
  465. if (actual)
  466. *actual = used_for_write;
  467. if (need_skip < 0) {
  468. printf("Attempt to write outside the flash area\n");
  469. *length = 0;
  470. return -EINVAL;
  471. }
  472. if (used_for_write > lim) {
  473. puts("Size of write exceeds partition or device limit\n");
  474. *length = 0;
  475. return -EFBIG;
  476. }
  477. if (!need_skip && !(flags & WITH_DROP_FFS)) {
  478. rval = nand_write(nand, offset, length, buffer);
  479. if (rval == 0)
  480. return 0;
  481. *length = 0;
  482. printf("NAND write to offset %llx failed %d\n",
  483. offset, rval);
  484. return rval;
  485. }
  486. while (left_to_write > 0) {
  487. size_t block_offset = offset & (nand->erasesize - 1);
  488. size_t write_size, truncated_write_size;
  489. WATCHDOG_RESET();
  490. if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
  491. printf("Skip bad block 0x%08llx\n",
  492. offset & ~(nand->erasesize - 1));
  493. offset += nand->erasesize - block_offset;
  494. continue;
  495. }
  496. if (left_to_write < (blocksize - block_offset))
  497. write_size = left_to_write;
  498. else
  499. write_size = blocksize - block_offset;
  500. #ifdef CONFIG_CMD_NAND_YAFFS
  501. if (flags & WITH_YAFFS_OOB) {
  502. int page, pages;
  503. size_t pagesize = nand->writesize;
  504. size_t pagesize_oob = pagesize + nand->oobsize;
  505. struct mtd_oob_ops ops;
  506. ops.len = pagesize;
  507. ops.ooblen = nand->oobsize;
  508. ops.mode = MTD_OPS_AUTO_OOB;
  509. ops.ooboffs = 0;
  510. pages = write_size / pagesize_oob;
  511. for (page = 0; page < pages; page++) {
  512. WATCHDOG_RESET();
  513. ops.datbuf = p_buffer;
  514. ops.oobbuf = ops.datbuf + pagesize;
  515. rval = mtd_write_oob(nand, offset, &ops);
  516. if (rval != 0)
  517. break;
  518. offset += pagesize;
  519. p_buffer += pagesize_oob;
  520. }
  521. }
  522. else
  523. #endif
  524. {
  525. truncated_write_size = write_size;
  526. #ifdef CONFIG_CMD_NAND_TRIMFFS
  527. if (flags & WITH_DROP_FFS)
  528. truncated_write_size = drop_ffs(nand, p_buffer,
  529. &write_size);
  530. #endif
  531. rval = nand_write(nand, offset, &truncated_write_size,
  532. p_buffer);
  533. offset += write_size;
  534. p_buffer += write_size;
  535. }
  536. if (rval != 0) {
  537. printf("NAND write to offset %llx failed %d\n",
  538. offset, rval);
  539. *length -= left_to_write;
  540. return rval;
  541. }
  542. left_to_write -= write_size;
  543. }
  544. return 0;
  545. }
  546. /**
  547. * nand_read_skip_bad:
  548. *
  549. * Read image from NAND flash.
  550. * Blocks that are marked bad are skipped and the next block is read
  551. * instead as long as the image is short enough to fit even after
  552. * skipping the bad blocks. Due to bad blocks we may not be able to
  553. * perform the requested read. In the case where the read would extend
  554. * beyond the end of the NAND device, both length and actual (if not
  555. * NULL) are set to 0. In the case where the read would extend beyond
  556. * the limit we are passed, length is set to 0 and actual is set to the
  557. * required length.
  558. *
  559. * @param nand NAND device
  560. * @param offset offset in flash
  561. * @param length buffer length, on return holds number of read bytes
  562. * @param actual set to size required to read length worth of buffer or 0
  563. * on error, if not NULL
  564. * @param lim maximum size that actual may be in order to not exceed the
  565. * buffer
  566. * @param buffer buffer to write to
  567. * @return 0 in case of success
  568. */
  569. int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
  570. size_t *actual, loff_t lim, u_char *buffer)
  571. {
  572. int rval;
  573. size_t left_to_read = *length;
  574. size_t used_for_read = 0;
  575. u_char *p_buffer = buffer;
  576. int need_skip;
  577. if ((offset & (nand->writesize - 1)) != 0) {
  578. printf("Attempt to read non page-aligned data\n");
  579. *length = 0;
  580. if (actual)
  581. *actual = 0;
  582. return -EINVAL;
  583. }
  584. need_skip = check_skip_len(nand, offset, *length, &used_for_read);
  585. if (actual)
  586. *actual = used_for_read;
  587. if (need_skip < 0) {
  588. printf("Attempt to read outside the flash area\n");
  589. *length = 0;
  590. return -EINVAL;
  591. }
  592. if (used_for_read > lim) {
  593. puts("Size of read exceeds partition or device limit\n");
  594. *length = 0;
  595. return -EFBIG;
  596. }
  597. if (!need_skip) {
  598. rval = nand_read(nand, offset, length, buffer);
  599. if (!rval || rval == -EUCLEAN)
  600. return 0;
  601. *length = 0;
  602. printf("NAND read from offset %llx failed %d\n",
  603. offset, rval);
  604. return rval;
  605. }
  606. while (left_to_read > 0) {
  607. size_t block_offset = offset & (nand->erasesize - 1);
  608. size_t read_length;
  609. WATCHDOG_RESET();
  610. if (nand_block_isbad(nand, offset & ~(nand->erasesize - 1))) {
  611. printf("Skipping bad block 0x%08llx\n",
  612. offset & ~(nand->erasesize - 1));
  613. offset += nand->erasesize - block_offset;
  614. continue;
  615. }
  616. if (left_to_read < (nand->erasesize - block_offset))
  617. read_length = left_to_read;
  618. else
  619. read_length = nand->erasesize - block_offset;
  620. rval = nand_read(nand, offset, &read_length, p_buffer);
  621. if (rval && rval != -EUCLEAN) {
  622. printf("NAND read from offset %llx failed %d\n",
  623. offset, rval);
  624. *length -= left_to_read;
  625. return rval;
  626. }
  627. left_to_read -= read_length;
  628. offset += read_length;
  629. p_buffer += read_length;
  630. }
  631. return 0;
  632. }
  633. #ifdef CONFIG_CMD_NAND_TORTURE
  634. /**
  635. * check_pattern:
  636. *
  637. * Check if buffer contains only a certain byte pattern.
  638. *
  639. * @param buf buffer to check
  640. * @param patt the pattern to check
  641. * @param size buffer size in bytes
  642. * @return 1 if there are only patt bytes in buf
  643. * 0 if something else was found
  644. */
  645. static int check_pattern(const u_char *buf, u_char patt, int size)
  646. {
  647. int i;
  648. for (i = 0; i < size; i++)
  649. if (buf[i] != patt)
  650. return 0;
  651. return 1;
  652. }
  653. /**
  654. * nand_torture:
  655. *
  656. * Torture a block of NAND flash.
  657. * This is useful to determine if a block that caused a write error is still
  658. * good or should be marked as bad.
  659. *
  660. * @param nand NAND device
  661. * @param offset offset in flash
  662. * @return 0 if the block is still good
  663. */
  664. int nand_torture(nand_info_t *nand, loff_t offset)
  665. {
  666. u_char patterns[] = {0xa5, 0x5a, 0x00};
  667. struct erase_info instr = {
  668. .mtd = nand,
  669. .addr = offset,
  670. .len = nand->erasesize,
  671. };
  672. size_t retlen;
  673. int err, ret = -1, i, patt_count;
  674. u_char *buf;
  675. if ((offset & (nand->erasesize - 1)) != 0) {
  676. puts("Attempt to torture a block at a non block-aligned offset\n");
  677. return -EINVAL;
  678. }
  679. if (offset + nand->erasesize > nand->size) {
  680. puts("Attempt to torture a block outside the flash area\n");
  681. return -EINVAL;
  682. }
  683. patt_count = ARRAY_SIZE(patterns);
  684. buf = malloc(nand->erasesize);
  685. if (buf == NULL) {
  686. puts("Out of memory for erase block buffer\n");
  687. return -ENOMEM;
  688. }
  689. for (i = 0; i < patt_count; i++) {
  690. err = nand->erase(nand, &instr);
  691. if (err) {
  692. printf("%s: erase() failed for block at 0x%llx: %d\n",
  693. nand->name, instr.addr, err);
  694. goto out;
  695. }
  696. /* Make sure the block contains only 0xff bytes */
  697. err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
  698. if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
  699. printf("%s: read() failed for block at 0x%llx: %d\n",
  700. nand->name, instr.addr, err);
  701. goto out;
  702. }
  703. err = check_pattern(buf, 0xff, nand->erasesize);
  704. if (!err) {
  705. printf("Erased block at 0x%llx, but a non-0xff byte was found\n",
  706. offset);
  707. ret = -EIO;
  708. goto out;
  709. }
  710. /* Write a pattern and check it */
  711. memset(buf, patterns[i], nand->erasesize);
  712. err = nand->write(nand, offset, nand->erasesize, &retlen, buf);
  713. if (err || retlen != nand->erasesize) {
  714. printf("%s: write() failed for block at 0x%llx: %d\n",
  715. nand->name, instr.addr, err);
  716. goto out;
  717. }
  718. err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
  719. if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
  720. printf("%s: read() failed for block at 0x%llx: %d\n",
  721. nand->name, instr.addr, err);
  722. goto out;
  723. }
  724. err = check_pattern(buf, patterns[i], nand->erasesize);
  725. if (!err) {
  726. printf("Pattern 0x%.2x checking failed for block at "
  727. "0x%llx\n", patterns[i], offset);
  728. ret = -EIO;
  729. goto out;
  730. }
  731. }
  732. ret = 0;
  733. out:
  734. free(buf);
  735. return ret;
  736. }
  737. #endif