nand_util.c 22 KB

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