nand_util.c 22 KB

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