nand_util.c 23 KB

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