nand_util.c 24 KB

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