mtd_oobtest.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. * Copyright (C) 2006-2008 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; see the file COPYING. If not, write to the Free Software
  15. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Test OOB read and write on MTD device.
  18. *
  19. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <asm/div64.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/err.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/slab.h>
  29. #include <linux/sched.h>
  30. #include <linux/random.h>
  31. static int dev = -EINVAL;
  32. module_param(dev, int, S_IRUGO);
  33. MODULE_PARM_DESC(dev, "MTD device number to use");
  34. static struct mtd_info *mtd;
  35. static unsigned char *readbuf;
  36. static unsigned char *writebuf;
  37. static unsigned char *bbt;
  38. static int ebcnt;
  39. static int pgcnt;
  40. static int errcnt;
  41. static int use_offset;
  42. static int use_len;
  43. static int use_len_max;
  44. static int vary_offset;
  45. static struct rnd_state rnd_state;
  46. static int erase_eraseblock(int ebnum)
  47. {
  48. int err;
  49. struct erase_info ei;
  50. loff_t addr = ebnum * mtd->erasesize;
  51. memset(&ei, 0, sizeof(struct erase_info));
  52. ei.mtd = mtd;
  53. ei.addr = addr;
  54. ei.len = mtd->erasesize;
  55. err = mtd_erase(mtd, &ei);
  56. if (err) {
  57. pr_err("error %d while erasing EB %d\n", err, ebnum);
  58. return err;
  59. }
  60. if (ei.state == MTD_ERASE_FAILED) {
  61. pr_err("some erase error occurred at EB %d\n", ebnum);
  62. return -EIO;
  63. }
  64. return 0;
  65. }
  66. static int erase_whole_device(void)
  67. {
  68. int err;
  69. unsigned int i;
  70. pr_info("erasing whole device\n");
  71. for (i = 0; i < ebcnt; ++i) {
  72. if (bbt[i])
  73. continue;
  74. err = erase_eraseblock(i);
  75. if (err)
  76. return err;
  77. cond_resched();
  78. }
  79. pr_info("erased %u eraseblocks\n", i);
  80. return 0;
  81. }
  82. static void do_vary_offset(void)
  83. {
  84. use_len -= 1;
  85. if (use_len < 1) {
  86. use_offset += 1;
  87. if (use_offset >= use_len_max)
  88. use_offset = 0;
  89. use_len = use_len_max - use_offset;
  90. }
  91. }
  92. static int write_eraseblock(int ebnum)
  93. {
  94. int i;
  95. struct mtd_oob_ops ops;
  96. int err = 0;
  97. loff_t addr = ebnum * mtd->erasesize;
  98. for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
  99. prandom_bytes_state(&rnd_state, writebuf, use_len);
  100. ops.mode = MTD_OPS_AUTO_OOB;
  101. ops.len = 0;
  102. ops.retlen = 0;
  103. ops.ooblen = use_len;
  104. ops.oobretlen = 0;
  105. ops.ooboffs = use_offset;
  106. ops.datbuf = NULL;
  107. ops.oobbuf = writebuf;
  108. err = mtd_write_oob(mtd, addr, &ops);
  109. if (err || ops.oobretlen != use_len) {
  110. pr_err("error: writeoob failed at %#llx\n",
  111. (long long)addr);
  112. pr_err("error: use_len %d, use_offset %d\n",
  113. use_len, use_offset);
  114. errcnt += 1;
  115. return err ? err : -1;
  116. }
  117. if (vary_offset)
  118. do_vary_offset();
  119. }
  120. return err;
  121. }
  122. static int write_whole_device(void)
  123. {
  124. int err;
  125. unsigned int i;
  126. pr_info("writing OOBs of whole device\n");
  127. for (i = 0; i < ebcnt; ++i) {
  128. if (bbt[i])
  129. continue;
  130. err = write_eraseblock(i);
  131. if (err)
  132. return err;
  133. if (i % 256 == 0)
  134. pr_info("written up to eraseblock %u\n", i);
  135. cond_resched();
  136. }
  137. pr_info("written %u eraseblocks\n", i);
  138. return 0;
  139. }
  140. static int verify_eraseblock(int ebnum)
  141. {
  142. int i;
  143. struct mtd_oob_ops ops;
  144. int err = 0;
  145. loff_t addr = ebnum * mtd->erasesize;
  146. for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
  147. prandom_bytes_state(&rnd_state, writebuf, use_len);
  148. ops.mode = MTD_OPS_AUTO_OOB;
  149. ops.len = 0;
  150. ops.retlen = 0;
  151. ops.ooblen = use_len;
  152. ops.oobretlen = 0;
  153. ops.ooboffs = use_offset;
  154. ops.datbuf = NULL;
  155. ops.oobbuf = readbuf;
  156. err = mtd_read_oob(mtd, addr, &ops);
  157. if (err || ops.oobretlen != use_len) {
  158. pr_err("error: readoob failed at %#llx\n",
  159. (long long)addr);
  160. errcnt += 1;
  161. return err ? err : -1;
  162. }
  163. if (memcmp(readbuf, writebuf, use_len)) {
  164. pr_err("error: verify failed at %#llx\n",
  165. (long long)addr);
  166. errcnt += 1;
  167. if (errcnt > 1000) {
  168. pr_err("error: too many errors\n");
  169. return -1;
  170. }
  171. }
  172. if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
  173. int k;
  174. ops.mode = MTD_OPS_AUTO_OOB;
  175. ops.len = 0;
  176. ops.retlen = 0;
  177. ops.ooblen = mtd->ecclayout->oobavail;
  178. ops.oobretlen = 0;
  179. ops.ooboffs = 0;
  180. ops.datbuf = NULL;
  181. ops.oobbuf = readbuf;
  182. err = mtd_read_oob(mtd, addr, &ops);
  183. if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
  184. pr_err("error: readoob failed at %#llx\n",
  185. (long long)addr);
  186. errcnt += 1;
  187. return err ? err : -1;
  188. }
  189. if (memcmp(readbuf + use_offset, writebuf, use_len)) {
  190. pr_err("error: verify failed at %#llx\n",
  191. (long long)addr);
  192. errcnt += 1;
  193. if (errcnt > 1000) {
  194. pr_err("error: too many errors\n");
  195. return -1;
  196. }
  197. }
  198. for (k = 0; k < use_offset; ++k)
  199. if (readbuf[k] != 0xff) {
  200. pr_err("error: verify 0xff "
  201. "failed at %#llx\n",
  202. (long long)addr);
  203. errcnt += 1;
  204. if (errcnt > 1000) {
  205. pr_err("error: too "
  206. "many errors\n");
  207. return -1;
  208. }
  209. }
  210. for (k = use_offset + use_len;
  211. k < mtd->ecclayout->oobavail; ++k)
  212. if (readbuf[k] != 0xff) {
  213. pr_err("error: verify 0xff "
  214. "failed at %#llx\n",
  215. (long long)addr);
  216. errcnt += 1;
  217. if (errcnt > 1000) {
  218. pr_err("error: too "
  219. "many errors\n");
  220. return -1;
  221. }
  222. }
  223. }
  224. if (vary_offset)
  225. do_vary_offset();
  226. }
  227. return err;
  228. }
  229. static int verify_eraseblock_in_one_go(int ebnum)
  230. {
  231. struct mtd_oob_ops ops;
  232. int err = 0;
  233. loff_t addr = ebnum * mtd->erasesize;
  234. size_t len = mtd->ecclayout->oobavail * pgcnt;
  235. prandom_bytes_state(&rnd_state, writebuf, len);
  236. ops.mode = MTD_OPS_AUTO_OOB;
  237. ops.len = 0;
  238. ops.retlen = 0;
  239. ops.ooblen = len;
  240. ops.oobretlen = 0;
  241. ops.ooboffs = 0;
  242. ops.datbuf = NULL;
  243. ops.oobbuf = readbuf;
  244. err = mtd_read_oob(mtd, addr, &ops);
  245. if (err || ops.oobretlen != len) {
  246. pr_err("error: readoob failed at %#llx\n",
  247. (long long)addr);
  248. errcnt += 1;
  249. return err ? err : -1;
  250. }
  251. if (memcmp(readbuf, writebuf, len)) {
  252. pr_err("error: verify failed at %#llx\n",
  253. (long long)addr);
  254. errcnt += 1;
  255. if (errcnt > 1000) {
  256. pr_err("error: too many errors\n");
  257. return -1;
  258. }
  259. }
  260. return err;
  261. }
  262. static int verify_all_eraseblocks(void)
  263. {
  264. int err;
  265. unsigned int i;
  266. pr_info("verifying all eraseblocks\n");
  267. for (i = 0; i < ebcnt; ++i) {
  268. if (bbt[i])
  269. continue;
  270. err = verify_eraseblock(i);
  271. if (err)
  272. return err;
  273. if (i % 256 == 0)
  274. pr_info("verified up to eraseblock %u\n", i);
  275. cond_resched();
  276. }
  277. pr_info("verified %u eraseblocks\n", i);
  278. return 0;
  279. }
  280. static int is_block_bad(int ebnum)
  281. {
  282. int ret;
  283. loff_t addr = ebnum * mtd->erasesize;
  284. ret = mtd_block_isbad(mtd, addr);
  285. if (ret)
  286. pr_info("block %d is bad\n", ebnum);
  287. return ret;
  288. }
  289. static int scan_for_bad_eraseblocks(void)
  290. {
  291. int i, bad = 0;
  292. bbt = kmalloc(ebcnt, GFP_KERNEL);
  293. if (!bbt) {
  294. pr_err("error: cannot allocate memory\n");
  295. return -ENOMEM;
  296. }
  297. pr_info("scanning for bad eraseblocks\n");
  298. for (i = 0; i < ebcnt; ++i) {
  299. bbt[i] = is_block_bad(i) ? 1 : 0;
  300. if (bbt[i])
  301. bad += 1;
  302. cond_resched();
  303. }
  304. pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
  305. return 0;
  306. }
  307. static int __init mtd_oobtest_init(void)
  308. {
  309. int err = 0;
  310. unsigned int i;
  311. uint64_t tmp;
  312. struct mtd_oob_ops ops;
  313. loff_t addr = 0, addr0;
  314. printk(KERN_INFO "\n");
  315. printk(KERN_INFO "=================================================\n");
  316. if (dev < 0) {
  317. pr_info("Please specify a valid mtd-device via module parameter\n");
  318. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  319. return -EINVAL;
  320. }
  321. pr_info("MTD device: %d\n", dev);
  322. mtd = get_mtd_device(NULL, dev);
  323. if (IS_ERR(mtd)) {
  324. err = PTR_ERR(mtd);
  325. pr_err("error: cannot get MTD device\n");
  326. return err;
  327. }
  328. if (mtd->type != MTD_NANDFLASH) {
  329. pr_info("this test requires NAND flash\n");
  330. goto out;
  331. }
  332. tmp = mtd->size;
  333. do_div(tmp, mtd->erasesize);
  334. ebcnt = tmp;
  335. pgcnt = mtd->erasesize / mtd->writesize;
  336. pr_info("MTD device size %llu, eraseblock size %u, "
  337. "page size %u, count of eraseblocks %u, pages per "
  338. "eraseblock %u, OOB size %u\n",
  339. (unsigned long long)mtd->size, mtd->erasesize,
  340. mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
  341. err = -ENOMEM;
  342. readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  343. if (!readbuf) {
  344. pr_err("error: cannot allocate memory\n");
  345. goto out;
  346. }
  347. writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  348. if (!writebuf) {
  349. pr_err("error: cannot allocate memory\n");
  350. goto out;
  351. }
  352. err = scan_for_bad_eraseblocks();
  353. if (err)
  354. goto out;
  355. use_offset = 0;
  356. use_len = mtd->ecclayout->oobavail;
  357. use_len_max = mtd->ecclayout->oobavail;
  358. vary_offset = 0;
  359. /* First test: write all OOB, read it back and verify */
  360. pr_info("test 1 of 5\n");
  361. err = erase_whole_device();
  362. if (err)
  363. goto out;
  364. prandom_seed_state(&rnd_state, 1);
  365. err = write_whole_device();
  366. if (err)
  367. goto out;
  368. prandom_seed_state(&rnd_state, 1);
  369. err = verify_all_eraseblocks();
  370. if (err)
  371. goto out;
  372. /*
  373. * Second test: write all OOB, a block at a time, read it back and
  374. * verify.
  375. */
  376. pr_info("test 2 of 5\n");
  377. err = erase_whole_device();
  378. if (err)
  379. goto out;
  380. prandom_seed_state(&rnd_state, 3);
  381. err = write_whole_device();
  382. if (err)
  383. goto out;
  384. /* Check all eraseblocks */
  385. prandom_seed_state(&rnd_state, 3);
  386. pr_info("verifying all eraseblocks\n");
  387. for (i = 0; i < ebcnt; ++i) {
  388. if (bbt[i])
  389. continue;
  390. err = verify_eraseblock_in_one_go(i);
  391. if (err)
  392. goto out;
  393. if (i % 256 == 0)
  394. pr_info("verified up to eraseblock %u\n", i);
  395. cond_resched();
  396. }
  397. pr_info("verified %u eraseblocks\n", i);
  398. /*
  399. * Third test: write OOB at varying offsets and lengths, read it back
  400. * and verify.
  401. */
  402. pr_info("test 3 of 5\n");
  403. err = erase_whole_device();
  404. if (err)
  405. goto out;
  406. /* Write all eraseblocks */
  407. use_offset = 0;
  408. use_len = mtd->ecclayout->oobavail;
  409. use_len_max = mtd->ecclayout->oobavail;
  410. vary_offset = 1;
  411. prandom_seed_state(&rnd_state, 5);
  412. err = write_whole_device();
  413. if (err)
  414. goto out;
  415. /* Check all eraseblocks */
  416. use_offset = 0;
  417. use_len = mtd->ecclayout->oobavail;
  418. use_len_max = mtd->ecclayout->oobavail;
  419. vary_offset = 1;
  420. prandom_seed_state(&rnd_state, 5);
  421. err = verify_all_eraseblocks();
  422. if (err)
  423. goto out;
  424. use_offset = 0;
  425. use_len = mtd->ecclayout->oobavail;
  426. use_len_max = mtd->ecclayout->oobavail;
  427. vary_offset = 0;
  428. /* Fourth test: try to write off end of device */
  429. pr_info("test 4 of 5\n");
  430. err = erase_whole_device();
  431. if (err)
  432. goto out;
  433. addr0 = 0;
  434. for (i = 0; i < ebcnt && bbt[i]; ++i)
  435. addr0 += mtd->erasesize;
  436. /* Attempt to write off end of OOB */
  437. ops.mode = MTD_OPS_AUTO_OOB;
  438. ops.len = 0;
  439. ops.retlen = 0;
  440. ops.ooblen = 1;
  441. ops.oobretlen = 0;
  442. ops.ooboffs = mtd->ecclayout->oobavail;
  443. ops.datbuf = NULL;
  444. ops.oobbuf = writebuf;
  445. pr_info("attempting to start write past end of OOB\n");
  446. pr_info("an error is expected...\n");
  447. err = mtd_write_oob(mtd, addr0, &ops);
  448. if (err) {
  449. pr_info("error occurred as expected\n");
  450. err = 0;
  451. } else {
  452. pr_err("error: can write past end of OOB\n");
  453. errcnt += 1;
  454. }
  455. /* Attempt to read off end of OOB */
  456. ops.mode = MTD_OPS_AUTO_OOB;
  457. ops.len = 0;
  458. ops.retlen = 0;
  459. ops.ooblen = 1;
  460. ops.oobretlen = 0;
  461. ops.ooboffs = mtd->ecclayout->oobavail;
  462. ops.datbuf = NULL;
  463. ops.oobbuf = readbuf;
  464. pr_info("attempting to start read past end of OOB\n");
  465. pr_info("an error is expected...\n");
  466. err = mtd_read_oob(mtd, addr0, &ops);
  467. if (err) {
  468. pr_info("error occurred as expected\n");
  469. err = 0;
  470. } else {
  471. pr_err("error: can read past end of OOB\n");
  472. errcnt += 1;
  473. }
  474. if (bbt[ebcnt - 1])
  475. pr_info("skipping end of device tests because last "
  476. "block is bad\n");
  477. else {
  478. /* Attempt to write off end of device */
  479. ops.mode = MTD_OPS_AUTO_OOB;
  480. ops.len = 0;
  481. ops.retlen = 0;
  482. ops.ooblen = mtd->ecclayout->oobavail + 1;
  483. ops.oobretlen = 0;
  484. ops.ooboffs = 0;
  485. ops.datbuf = NULL;
  486. ops.oobbuf = writebuf;
  487. pr_info("attempting to write past end of device\n");
  488. pr_info("an error is expected...\n");
  489. err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
  490. if (err) {
  491. pr_info("error occurred as expected\n");
  492. err = 0;
  493. } else {
  494. pr_err("error: wrote past end of device\n");
  495. errcnt += 1;
  496. }
  497. /* Attempt to read off end of device */
  498. ops.mode = MTD_OPS_AUTO_OOB;
  499. ops.len = 0;
  500. ops.retlen = 0;
  501. ops.ooblen = mtd->ecclayout->oobavail + 1;
  502. ops.oobretlen = 0;
  503. ops.ooboffs = 0;
  504. ops.datbuf = NULL;
  505. ops.oobbuf = readbuf;
  506. pr_info("attempting to read past end of device\n");
  507. pr_info("an error is expected...\n");
  508. err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
  509. if (err) {
  510. pr_info("error occurred as expected\n");
  511. err = 0;
  512. } else {
  513. pr_err("error: read past end of device\n");
  514. errcnt += 1;
  515. }
  516. err = erase_eraseblock(ebcnt - 1);
  517. if (err)
  518. goto out;
  519. /* Attempt to write off end of device */
  520. ops.mode = MTD_OPS_AUTO_OOB;
  521. ops.len = 0;
  522. ops.retlen = 0;
  523. ops.ooblen = mtd->ecclayout->oobavail;
  524. ops.oobretlen = 0;
  525. ops.ooboffs = 1;
  526. ops.datbuf = NULL;
  527. ops.oobbuf = writebuf;
  528. pr_info("attempting to write past end of device\n");
  529. pr_info("an error is expected...\n");
  530. err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
  531. if (err) {
  532. pr_info("error occurred as expected\n");
  533. err = 0;
  534. } else {
  535. pr_err("error: wrote past end of device\n");
  536. errcnt += 1;
  537. }
  538. /* Attempt to read off end of device */
  539. ops.mode = MTD_OPS_AUTO_OOB;
  540. ops.len = 0;
  541. ops.retlen = 0;
  542. ops.ooblen = mtd->ecclayout->oobavail;
  543. ops.oobretlen = 0;
  544. ops.ooboffs = 1;
  545. ops.datbuf = NULL;
  546. ops.oobbuf = readbuf;
  547. pr_info("attempting to read past end of device\n");
  548. pr_info("an error is expected...\n");
  549. err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
  550. if (err) {
  551. pr_info("error occurred as expected\n");
  552. err = 0;
  553. } else {
  554. pr_err("error: read past end of device\n");
  555. errcnt += 1;
  556. }
  557. }
  558. /* Fifth test: write / read across block boundaries */
  559. pr_info("test 5 of 5\n");
  560. /* Erase all eraseblocks */
  561. err = erase_whole_device();
  562. if (err)
  563. goto out;
  564. /* Write all eraseblocks */
  565. prandom_seed_state(&rnd_state, 11);
  566. pr_info("writing OOBs of whole device\n");
  567. for (i = 0; i < ebcnt - 1; ++i) {
  568. int cnt = 2;
  569. int pg;
  570. size_t sz = mtd->ecclayout->oobavail;
  571. if (bbt[i] || bbt[i + 1])
  572. continue;
  573. addr = (i + 1) * mtd->erasesize - mtd->writesize;
  574. for (pg = 0; pg < cnt; ++pg) {
  575. prandom_bytes_state(&rnd_state, writebuf, sz);
  576. ops.mode = MTD_OPS_AUTO_OOB;
  577. ops.len = 0;
  578. ops.retlen = 0;
  579. ops.ooblen = sz;
  580. ops.oobretlen = 0;
  581. ops.ooboffs = 0;
  582. ops.datbuf = NULL;
  583. ops.oobbuf = writebuf;
  584. err = mtd_write_oob(mtd, addr, &ops);
  585. if (err)
  586. goto out;
  587. if (i % 256 == 0)
  588. pr_info("written up to eraseblock %u\n", i);
  589. cond_resched();
  590. addr += mtd->writesize;
  591. }
  592. }
  593. pr_info("written %u eraseblocks\n", i);
  594. /* Check all eraseblocks */
  595. prandom_seed_state(&rnd_state, 11);
  596. pr_info("verifying all eraseblocks\n");
  597. for (i = 0; i < ebcnt - 1; ++i) {
  598. if (bbt[i] || bbt[i + 1])
  599. continue;
  600. prandom_bytes_state(&rnd_state, writebuf,
  601. mtd->ecclayout->oobavail * 2);
  602. addr = (i + 1) * mtd->erasesize - mtd->writesize;
  603. ops.mode = MTD_OPS_AUTO_OOB;
  604. ops.len = 0;
  605. ops.retlen = 0;
  606. ops.ooblen = mtd->ecclayout->oobavail * 2;
  607. ops.oobretlen = 0;
  608. ops.ooboffs = 0;
  609. ops.datbuf = NULL;
  610. ops.oobbuf = readbuf;
  611. err = mtd_read_oob(mtd, addr, &ops);
  612. if (err)
  613. goto out;
  614. if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) {
  615. pr_err("error: verify failed at %#llx\n",
  616. (long long)addr);
  617. errcnt += 1;
  618. if (errcnt > 1000) {
  619. pr_err("error: too many errors\n");
  620. goto out;
  621. }
  622. }
  623. if (i % 256 == 0)
  624. pr_info("verified up to eraseblock %u\n", i);
  625. cond_resched();
  626. }
  627. pr_info("verified %u eraseblocks\n", i);
  628. pr_info("finished with %d errors\n", errcnt);
  629. out:
  630. kfree(bbt);
  631. kfree(writebuf);
  632. kfree(readbuf);
  633. put_mtd_device(mtd);
  634. if (err)
  635. pr_info("error %d occurred\n", err);
  636. printk(KERN_INFO "=================================================\n");
  637. return err;
  638. }
  639. module_init(mtd_oobtest_init);
  640. static void __exit mtd_oobtest_exit(void)
  641. {
  642. return;
  643. }
  644. module_exit(mtd_oobtest_exit);
  645. MODULE_DESCRIPTION("Out-of-band test module");
  646. MODULE_AUTHOR("Adrian Hunter");
  647. MODULE_LICENSE("GPL");