oobtest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. #include "mtd_test.h"
  32. static int dev = -EINVAL;
  33. static int bitflip_limit;
  34. module_param(dev, int, S_IRUGO);
  35. MODULE_PARM_DESC(dev, "MTD device number to use");
  36. module_param(bitflip_limit, int, S_IRUGO);
  37. MODULE_PARM_DESC(bitflip_limit, "Max. allowed bitflips per page");
  38. static struct mtd_info *mtd;
  39. static unsigned char *readbuf;
  40. static unsigned char *writebuf;
  41. static unsigned char *bbt;
  42. static int ebcnt;
  43. static int pgcnt;
  44. static int errcnt;
  45. static int use_offset;
  46. static int use_len;
  47. static int use_len_max;
  48. static int vary_offset;
  49. static struct rnd_state rnd_state;
  50. static void do_vary_offset(void)
  51. {
  52. use_len -= 1;
  53. if (use_len < 1) {
  54. use_offset += 1;
  55. if (use_offset >= use_len_max)
  56. use_offset = 0;
  57. use_len = use_len_max - use_offset;
  58. }
  59. }
  60. static int write_eraseblock(int ebnum)
  61. {
  62. int i;
  63. struct mtd_oob_ops ops;
  64. int err = 0;
  65. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  66. prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
  67. for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
  68. ops.mode = MTD_OPS_AUTO_OOB;
  69. ops.len = 0;
  70. ops.retlen = 0;
  71. ops.ooblen = use_len;
  72. ops.oobretlen = 0;
  73. ops.ooboffs = use_offset;
  74. ops.datbuf = NULL;
  75. ops.oobbuf = writebuf + (use_len_max * i) + use_offset;
  76. err = mtd_write_oob(mtd, addr, &ops);
  77. if (err || ops.oobretlen != use_len) {
  78. pr_err("error: writeoob failed at %#llx\n",
  79. (long long)addr);
  80. pr_err("error: use_len %d, use_offset %d\n",
  81. use_len, use_offset);
  82. errcnt += 1;
  83. return err ? err : -1;
  84. }
  85. if (vary_offset)
  86. do_vary_offset();
  87. }
  88. return err;
  89. }
  90. static int write_whole_device(void)
  91. {
  92. int err;
  93. unsigned int i;
  94. pr_info("writing OOBs of whole device\n");
  95. for (i = 0; i < ebcnt; ++i) {
  96. if (bbt[i])
  97. continue;
  98. err = write_eraseblock(i);
  99. if (err)
  100. return err;
  101. if (i % 256 == 0)
  102. pr_info("written up to eraseblock %u\n", i);
  103. err = mtdtest_relax();
  104. if (err)
  105. return err;
  106. }
  107. pr_info("written %u eraseblocks\n", i);
  108. return 0;
  109. }
  110. /*
  111. * Display the address, offset and data bytes at comparison failure.
  112. * Return number of bitflips encountered.
  113. */
  114. static size_t memcmpshowoffset(loff_t addr, loff_t offset, const void *cs,
  115. const void *ct, size_t count)
  116. {
  117. const unsigned char *su1, *su2;
  118. int res;
  119. size_t i = 0;
  120. size_t bitflips = 0;
  121. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--, i++) {
  122. res = *su1 ^ *su2;
  123. if (res) {
  124. pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0x%x diff 0x%x\n",
  125. (unsigned long)addr, (unsigned long)offset + i,
  126. *su1, *su2, res);
  127. bitflips += hweight8(res);
  128. }
  129. }
  130. return bitflips;
  131. }
  132. #define memcmpshow(addr, cs, ct, count) memcmpshowoffset((addr), 0, (cs), (ct),\
  133. (count))
  134. /*
  135. * Compare with 0xff and show the address, offset and data bytes at
  136. * comparison failure. Return number of bitflips encountered.
  137. */
  138. static size_t memffshow(loff_t addr, loff_t offset, const void *cs,
  139. size_t count)
  140. {
  141. const unsigned char *su1;
  142. int res;
  143. size_t i = 0;
  144. size_t bitflips = 0;
  145. for (su1 = cs; 0 < count; ++su1, count--, i++) {
  146. res = *su1 ^ 0xff;
  147. if (res) {
  148. pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
  149. (unsigned long)addr, (unsigned long)offset + i,
  150. *su1, res);
  151. bitflips += hweight8(res);
  152. }
  153. }
  154. return bitflips;
  155. }
  156. static int verify_eraseblock(int ebnum)
  157. {
  158. int i;
  159. struct mtd_oob_ops ops;
  160. int err = 0;
  161. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  162. size_t bitflips;
  163. prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
  164. for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
  165. ops.mode = MTD_OPS_AUTO_OOB;
  166. ops.len = 0;
  167. ops.retlen = 0;
  168. ops.ooblen = use_len;
  169. ops.oobretlen = 0;
  170. ops.ooboffs = use_offset;
  171. ops.datbuf = NULL;
  172. ops.oobbuf = readbuf;
  173. err = mtd_read_oob(mtd, addr, &ops);
  174. if (mtd_is_bitflip(err))
  175. err = 0;
  176. if (err || ops.oobretlen != use_len) {
  177. pr_err("error: readoob failed at %#llx\n",
  178. (long long)addr);
  179. errcnt += 1;
  180. return err ? err : -1;
  181. }
  182. bitflips = memcmpshow(addr, readbuf,
  183. writebuf + (use_len_max * i) + use_offset,
  184. use_len);
  185. if (bitflips > bitflip_limit) {
  186. pr_err("error: verify failed at %#llx\n",
  187. (long long)addr);
  188. errcnt += 1;
  189. if (errcnt > 1000) {
  190. pr_err("error: too many errors\n");
  191. return -1;
  192. }
  193. } else if (bitflips) {
  194. pr_info("ignoring error as within bitflip_limit\n");
  195. }
  196. if (use_offset != 0 || use_len < mtd->oobavail) {
  197. int k;
  198. ops.mode = MTD_OPS_AUTO_OOB;
  199. ops.len = 0;
  200. ops.retlen = 0;
  201. ops.ooblen = mtd->oobavail;
  202. ops.oobretlen = 0;
  203. ops.ooboffs = 0;
  204. ops.datbuf = NULL;
  205. ops.oobbuf = readbuf;
  206. err = mtd_read_oob(mtd, addr, &ops);
  207. if (mtd_is_bitflip(err))
  208. err = 0;
  209. if (err || ops.oobretlen != mtd->oobavail) {
  210. pr_err("error: readoob failed at %#llx\n",
  211. (long long)addr);
  212. errcnt += 1;
  213. return err ? err : -1;
  214. }
  215. bitflips = memcmpshowoffset(addr, use_offset,
  216. readbuf + use_offset,
  217. writebuf + (use_len_max * i) + use_offset,
  218. use_len);
  219. /* verify pre-offset area for 0xff */
  220. bitflips += memffshow(addr, 0, readbuf, use_offset);
  221. /* verify post-(use_offset + use_len) area for 0xff */
  222. k = use_offset + use_len;
  223. bitflips += memffshow(addr, k, readbuf + k,
  224. mtd->oobavail - k);
  225. if (bitflips > bitflip_limit) {
  226. pr_err("error: verify failed at %#llx\n",
  227. (long long)addr);
  228. errcnt += 1;
  229. if (errcnt > 1000) {
  230. pr_err("error: too many errors\n");
  231. return -1;
  232. }
  233. } else if (bitflips) {
  234. pr_info("ignoring errors as within bitflip limit\n");
  235. }
  236. }
  237. if (vary_offset)
  238. do_vary_offset();
  239. }
  240. return err;
  241. }
  242. static int verify_eraseblock_in_one_go(int ebnum)
  243. {
  244. struct mtd_oob_ops ops;
  245. int err = 0;
  246. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  247. size_t len = mtd->oobavail * pgcnt;
  248. size_t oobavail = mtd->oobavail;
  249. size_t bitflips;
  250. int i;
  251. prandom_bytes_state(&rnd_state, writebuf, len);
  252. ops.mode = MTD_OPS_AUTO_OOB;
  253. ops.len = 0;
  254. ops.retlen = 0;
  255. ops.ooblen = len;
  256. ops.oobretlen = 0;
  257. ops.ooboffs = 0;
  258. ops.datbuf = NULL;
  259. ops.oobbuf = readbuf;
  260. /* read entire block's OOB at one go */
  261. err = mtd_read_oob(mtd, addr, &ops);
  262. if (mtd_is_bitflip(err))
  263. err = 0;
  264. if (err || ops.oobretlen != len) {
  265. pr_err("error: readoob failed at %#llx\n",
  266. (long long)addr);
  267. errcnt += 1;
  268. return err ? err : -1;
  269. }
  270. /* verify one page OOB at a time for bitflip per page limit check */
  271. for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
  272. bitflips = memcmpshow(addr, readbuf + (i * oobavail),
  273. writebuf + (i * oobavail), oobavail);
  274. if (bitflips > bitflip_limit) {
  275. pr_err("error: verify failed at %#llx\n",
  276. (long long)addr);
  277. errcnt += 1;
  278. if (errcnt > 1000) {
  279. pr_err("error: too many errors\n");
  280. return -1;
  281. }
  282. } else if (bitflips) {
  283. pr_info("ignoring error as within bitflip_limit\n");
  284. }
  285. }
  286. return err;
  287. }
  288. static int verify_all_eraseblocks(void)
  289. {
  290. int err;
  291. unsigned int i;
  292. pr_info("verifying all eraseblocks\n");
  293. for (i = 0; i < ebcnt; ++i) {
  294. if (bbt[i])
  295. continue;
  296. err = verify_eraseblock(i);
  297. if (err)
  298. return err;
  299. if (i % 256 == 0)
  300. pr_info("verified up to eraseblock %u\n", i);
  301. err = mtdtest_relax();
  302. if (err)
  303. return err;
  304. }
  305. pr_info("verified %u eraseblocks\n", i);
  306. return 0;
  307. }
  308. static int __init mtd_oobtest_init(void)
  309. {
  310. int err = 0;
  311. unsigned int i;
  312. uint64_t tmp;
  313. struct mtd_oob_ops ops;
  314. loff_t addr = 0, addr0;
  315. printk(KERN_INFO "\n");
  316. printk(KERN_INFO "=================================================\n");
  317. if (dev < 0) {
  318. pr_info("Please specify a valid mtd-device via module parameter\n");
  319. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  320. return -EINVAL;
  321. }
  322. pr_info("MTD device: %d\n", dev);
  323. mtd = get_mtd_device(NULL, dev);
  324. if (IS_ERR(mtd)) {
  325. err = PTR_ERR(mtd);
  326. pr_err("error: cannot get MTD device\n");
  327. return err;
  328. }
  329. if (!mtd_type_is_nand(mtd)) {
  330. pr_info("this test requires NAND flash\n");
  331. goto out;
  332. }
  333. tmp = mtd->size;
  334. do_div(tmp, mtd->erasesize);
  335. ebcnt = tmp;
  336. pgcnt = mtd->erasesize / mtd->writesize;
  337. pr_info("MTD device size %llu, eraseblock size %u, "
  338. "page size %u, count of eraseblocks %u, pages per "
  339. "eraseblock %u, OOB size %u\n",
  340. (unsigned long long)mtd->size, mtd->erasesize,
  341. mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
  342. err = -ENOMEM;
  343. readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  344. if (!readbuf)
  345. goto out;
  346. writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  347. if (!writebuf)
  348. goto out;
  349. bbt = kzalloc(ebcnt, GFP_KERNEL);
  350. if (!bbt)
  351. goto out;
  352. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  353. if (err)
  354. goto out;
  355. use_offset = 0;
  356. use_len = mtd->oobavail;
  357. use_len_max = mtd->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 = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  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 = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  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. err = mtdtest_relax();
  396. if (err)
  397. goto out;
  398. }
  399. pr_info("verified %u eraseblocks\n", i);
  400. /*
  401. * Third test: write OOB at varying offsets and lengths, read it back
  402. * and verify.
  403. */
  404. pr_info("test 3 of 5\n");
  405. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  406. if (err)
  407. goto out;
  408. /* Write all eraseblocks */
  409. use_offset = 0;
  410. use_len = mtd->oobavail;
  411. use_len_max = mtd->oobavail;
  412. vary_offset = 1;
  413. prandom_seed_state(&rnd_state, 5);
  414. err = write_whole_device();
  415. if (err)
  416. goto out;
  417. /* Check all eraseblocks */
  418. use_offset = 0;
  419. use_len = mtd->oobavail;
  420. use_len_max = mtd->oobavail;
  421. vary_offset = 1;
  422. prandom_seed_state(&rnd_state, 5);
  423. err = verify_all_eraseblocks();
  424. if (err)
  425. goto out;
  426. use_offset = 0;
  427. use_len = mtd->oobavail;
  428. use_len_max = mtd->oobavail;
  429. vary_offset = 0;
  430. /* Fourth test: try to write off end of device */
  431. pr_info("test 4 of 5\n");
  432. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  433. if (err)
  434. goto out;
  435. addr0 = 0;
  436. for (i = 0; i < ebcnt && bbt[i]; ++i)
  437. addr0 += mtd->erasesize;
  438. /* Attempt to write off end of OOB */
  439. ops.mode = MTD_OPS_AUTO_OOB;
  440. ops.len = 0;
  441. ops.retlen = 0;
  442. ops.ooblen = 1;
  443. ops.oobretlen = 0;
  444. ops.ooboffs = mtd->oobavail;
  445. ops.datbuf = NULL;
  446. ops.oobbuf = writebuf;
  447. pr_info("attempting to start write past end of OOB\n");
  448. pr_info("an error is expected...\n");
  449. err = mtd_write_oob(mtd, addr0, &ops);
  450. if (err) {
  451. pr_info("error occurred as expected\n");
  452. err = 0;
  453. } else {
  454. pr_err("error: can write past end of OOB\n");
  455. errcnt += 1;
  456. }
  457. /* Attempt to read off end of OOB */
  458. ops.mode = MTD_OPS_AUTO_OOB;
  459. ops.len = 0;
  460. ops.retlen = 0;
  461. ops.ooblen = 1;
  462. ops.oobretlen = 0;
  463. ops.ooboffs = mtd->oobavail;
  464. ops.datbuf = NULL;
  465. ops.oobbuf = readbuf;
  466. pr_info("attempting to start read past end of OOB\n");
  467. pr_info("an error is expected...\n");
  468. err = mtd_read_oob(mtd, addr0, &ops);
  469. if (mtd_is_bitflip(err))
  470. err = 0;
  471. if (err) {
  472. pr_info("error occurred as expected\n");
  473. err = 0;
  474. } else {
  475. pr_err("error: can read past end of OOB\n");
  476. errcnt += 1;
  477. }
  478. if (bbt[ebcnt - 1])
  479. pr_info("skipping end of device tests because last "
  480. "block is bad\n");
  481. else {
  482. /* Attempt to write off end of device */
  483. ops.mode = MTD_OPS_AUTO_OOB;
  484. ops.len = 0;
  485. ops.retlen = 0;
  486. ops.ooblen = mtd->oobavail + 1;
  487. ops.oobretlen = 0;
  488. ops.ooboffs = 0;
  489. ops.datbuf = NULL;
  490. ops.oobbuf = writebuf;
  491. pr_info("attempting to write past end of device\n");
  492. pr_info("an error is expected...\n");
  493. err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
  494. if (err) {
  495. pr_info("error occurred as expected\n");
  496. err = 0;
  497. } else {
  498. pr_err("error: wrote past end of device\n");
  499. errcnt += 1;
  500. }
  501. /* Attempt to read off end of device */
  502. ops.mode = MTD_OPS_AUTO_OOB;
  503. ops.len = 0;
  504. ops.retlen = 0;
  505. ops.ooblen = mtd->oobavail + 1;
  506. ops.oobretlen = 0;
  507. ops.ooboffs = 0;
  508. ops.datbuf = NULL;
  509. ops.oobbuf = readbuf;
  510. pr_info("attempting to read past end of device\n");
  511. pr_info("an error is expected...\n");
  512. err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
  513. if (mtd_is_bitflip(err))
  514. err = 0;
  515. if (err) {
  516. pr_info("error occurred as expected\n");
  517. err = 0;
  518. } else {
  519. pr_err("error: read past end of device\n");
  520. errcnt += 1;
  521. }
  522. err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
  523. if (err)
  524. goto out;
  525. /* Attempt to write off end of device */
  526. ops.mode = MTD_OPS_AUTO_OOB;
  527. ops.len = 0;
  528. ops.retlen = 0;
  529. ops.ooblen = mtd->oobavail;
  530. ops.oobretlen = 0;
  531. ops.ooboffs = 1;
  532. ops.datbuf = NULL;
  533. ops.oobbuf = writebuf;
  534. pr_info("attempting to write past end of device\n");
  535. pr_info("an error is expected...\n");
  536. err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
  537. if (err) {
  538. pr_info("error occurred as expected\n");
  539. err = 0;
  540. } else {
  541. pr_err("error: wrote past end of device\n");
  542. errcnt += 1;
  543. }
  544. /* Attempt to read off end of device */
  545. ops.mode = MTD_OPS_AUTO_OOB;
  546. ops.len = 0;
  547. ops.retlen = 0;
  548. ops.ooblen = mtd->oobavail;
  549. ops.oobretlen = 0;
  550. ops.ooboffs = 1;
  551. ops.datbuf = NULL;
  552. ops.oobbuf = readbuf;
  553. pr_info("attempting to read past end of device\n");
  554. pr_info("an error is expected...\n");
  555. err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
  556. if (mtd_is_bitflip(err))
  557. err = 0;
  558. if (err) {
  559. pr_info("error occurred as expected\n");
  560. err = 0;
  561. } else {
  562. pr_err("error: read past end of device\n");
  563. errcnt += 1;
  564. }
  565. }
  566. /* Fifth test: write / read across block boundaries */
  567. pr_info("test 5 of 5\n");
  568. /* Erase all eraseblocks */
  569. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  570. if (err)
  571. goto out;
  572. /* Write all eraseblocks */
  573. prandom_seed_state(&rnd_state, 11);
  574. pr_info("writing OOBs of whole device\n");
  575. for (i = 0; i < ebcnt - 1; ++i) {
  576. int cnt = 2;
  577. int pg;
  578. size_t sz = mtd->oobavail;
  579. if (bbt[i] || bbt[i + 1])
  580. continue;
  581. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  582. prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
  583. for (pg = 0; pg < cnt; ++pg) {
  584. ops.mode = MTD_OPS_AUTO_OOB;
  585. ops.len = 0;
  586. ops.retlen = 0;
  587. ops.ooblen = sz;
  588. ops.oobretlen = 0;
  589. ops.ooboffs = 0;
  590. ops.datbuf = NULL;
  591. ops.oobbuf = writebuf + pg * sz;
  592. err = mtd_write_oob(mtd, addr, &ops);
  593. if (err)
  594. goto out;
  595. if (i % 256 == 0)
  596. pr_info("written up to eraseblock %u\n", i);
  597. err = mtdtest_relax();
  598. if (err)
  599. goto out;
  600. addr += mtd->writesize;
  601. }
  602. }
  603. pr_info("written %u eraseblocks\n", i);
  604. /* Check all eraseblocks */
  605. prandom_seed_state(&rnd_state, 11);
  606. pr_info("verifying all eraseblocks\n");
  607. for (i = 0; i < ebcnt - 1; ++i) {
  608. if (bbt[i] || bbt[i + 1])
  609. continue;
  610. prandom_bytes_state(&rnd_state, writebuf, mtd->oobavail * 2);
  611. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  612. ops.mode = MTD_OPS_AUTO_OOB;
  613. ops.len = 0;
  614. ops.retlen = 0;
  615. ops.ooblen = mtd->oobavail * 2;
  616. ops.oobretlen = 0;
  617. ops.ooboffs = 0;
  618. ops.datbuf = NULL;
  619. ops.oobbuf = readbuf;
  620. err = mtd_read_oob(mtd, addr, &ops);
  621. if (mtd_is_bitflip(err))
  622. err = 0;
  623. if (err)
  624. goto out;
  625. if (memcmpshow(addr, readbuf, writebuf,
  626. mtd->oobavail * 2)) {
  627. pr_err("error: verify failed at %#llx\n",
  628. (long long)addr);
  629. errcnt += 1;
  630. if (errcnt > 1000) {
  631. pr_err("error: too many errors\n");
  632. goto out;
  633. }
  634. }
  635. if (i % 256 == 0)
  636. pr_info("verified up to eraseblock %u\n", i);
  637. err = mtdtest_relax();
  638. if (err)
  639. goto out;
  640. }
  641. pr_info("verified %u eraseblocks\n", i);
  642. pr_info("finished with %d errors\n", errcnt);
  643. out:
  644. kfree(bbt);
  645. kfree(writebuf);
  646. kfree(readbuf);
  647. put_mtd_device(mtd);
  648. if (err)
  649. pr_info("error %d occurred\n", err);
  650. printk(KERN_INFO "=================================================\n");
  651. return err;
  652. }
  653. module_init(mtd_oobtest_init);
  654. static void __exit mtd_oobtest_exit(void)
  655. {
  656. return;
  657. }
  658. module_exit(mtd_oobtest_exit);
  659. MODULE_DESCRIPTION("Out-of-band test module");
  660. MODULE_AUTHOR("Adrian Hunter");
  661. MODULE_LICENSE("GPL");