nand_util.c 22 KB

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