oobtest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 (err || ops.oobretlen != use_len) {
  175. pr_err("error: readoob failed at %#llx\n",
  176. (long long)addr);
  177. errcnt += 1;
  178. return err ? err : -1;
  179. }
  180. bitflips = memcmpshow(addr, readbuf,
  181. writebuf + (use_len_max * i) + use_offset,
  182. use_len);
  183. if (bitflips > bitflip_limit) {
  184. pr_err("error: verify failed at %#llx\n",
  185. (long long)addr);
  186. errcnt += 1;
  187. if (errcnt > 1000) {
  188. pr_err("error: too many errors\n");
  189. return -1;
  190. }
  191. } else if (bitflips) {
  192. pr_info("ignoring error as within bitflip_limit\n");
  193. }
  194. if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
  195. int k;
  196. ops.mode = MTD_OPS_AUTO_OOB;
  197. ops.len = 0;
  198. ops.retlen = 0;
  199. ops.ooblen = mtd->ecclayout->oobavail;
  200. ops.oobretlen = 0;
  201. ops.ooboffs = 0;
  202. ops.datbuf = NULL;
  203. ops.oobbuf = readbuf;
  204. err = mtd_read_oob(mtd, addr, &ops);
  205. if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
  206. pr_err("error: readoob failed at %#llx\n",
  207. (long long)addr);
  208. errcnt += 1;
  209. return err ? err : -1;
  210. }
  211. bitflips = memcmpshowoffset(addr, use_offset,
  212. readbuf + use_offset,
  213. writebuf + (use_len_max * i) + use_offset,
  214. use_len);
  215. /* verify pre-offset area for 0xff */
  216. bitflips += memffshow(addr, 0, readbuf, use_offset);
  217. /* verify post-(use_offset + use_len) area for 0xff */
  218. k = use_offset + use_len;
  219. bitflips += memffshow(addr, k, readbuf + k,
  220. mtd->ecclayout->oobavail - k);
  221. if (bitflips > bitflip_limit) {
  222. pr_err("error: verify failed at %#llx\n",
  223. (long long)addr);
  224. errcnt += 1;
  225. if (errcnt > 1000) {
  226. pr_err("error: too many errors\n");
  227. return -1;
  228. }
  229. } else if (bitflips) {
  230. pr_info("ignoring errors as within bitflip limit\n");
  231. }
  232. }
  233. if (vary_offset)
  234. do_vary_offset();
  235. }
  236. return err;
  237. }
  238. static int verify_eraseblock_in_one_go(int ebnum)
  239. {
  240. struct mtd_oob_ops ops;
  241. int err = 0;
  242. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  243. size_t len = mtd->ecclayout->oobavail * pgcnt;
  244. size_t oobavail = mtd->ecclayout->oobavail;
  245. size_t bitflips;
  246. int i;
  247. prandom_bytes_state(&rnd_state, writebuf, len);
  248. ops.mode = MTD_OPS_AUTO_OOB;
  249. ops.len = 0;
  250. ops.retlen = 0;
  251. ops.ooblen = len;
  252. ops.oobretlen = 0;
  253. ops.ooboffs = 0;
  254. ops.datbuf = NULL;
  255. ops.oobbuf = readbuf;
  256. /* read entire block's OOB at one go */
  257. err = mtd_read_oob(mtd, addr, &ops);
  258. if (err || ops.oobretlen != len) {
  259. pr_err("error: readoob failed at %#llx\n",
  260. (long long)addr);
  261. errcnt += 1;
  262. return err ? err : -1;
  263. }
  264. /* verify one page OOB at a time for bitflip per page limit check */
  265. for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
  266. bitflips = memcmpshow(addr, readbuf + (i * oobavail),
  267. writebuf + (i * oobavail), oobavail);
  268. if (bitflips > bitflip_limit) {
  269. pr_err("error: verify failed at %#llx\n",
  270. (long long)addr);
  271. errcnt += 1;
  272. if (errcnt > 1000) {
  273. pr_err("error: too many errors\n");
  274. return -1;
  275. }
  276. } else if (bitflips) {
  277. pr_info("ignoring error as within bitflip_limit\n");
  278. }
  279. }
  280. return err;
  281. }
  282. static int verify_all_eraseblocks(void)
  283. {
  284. int err;
  285. unsigned int i;
  286. pr_info("verifying all eraseblocks\n");
  287. for (i = 0; i < ebcnt; ++i) {
  288. if (bbt[i])
  289. continue;
  290. err = verify_eraseblock(i);
  291. if (err)
  292. return err;
  293. if (i % 256 == 0)
  294. pr_info("verified up to eraseblock %u\n", i);
  295. err = mtdtest_relax();
  296. if (err)
  297. return err;
  298. }
  299. pr_info("verified %u eraseblocks\n", i);
  300. return 0;
  301. }
  302. static int __init mtd_oobtest_init(void)
  303. {
  304. int err = 0;
  305. unsigned int i;
  306. uint64_t tmp;
  307. struct mtd_oob_ops ops;
  308. loff_t addr = 0, addr0;
  309. printk(KERN_INFO "\n");
  310. printk(KERN_INFO "=================================================\n");
  311. if (dev < 0) {
  312. pr_info("Please specify a valid mtd-device via module parameter\n");
  313. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  314. return -EINVAL;
  315. }
  316. pr_info("MTD device: %d\n", dev);
  317. mtd = get_mtd_device(NULL, dev);
  318. if (IS_ERR(mtd)) {
  319. err = PTR_ERR(mtd);
  320. pr_err("error: cannot get MTD device\n");
  321. return err;
  322. }
  323. if (!mtd_type_is_nand(mtd)) {
  324. pr_info("this test requires NAND flash\n");
  325. goto out;
  326. }
  327. tmp = mtd->size;
  328. do_div(tmp, mtd->erasesize);
  329. ebcnt = tmp;
  330. pgcnt = mtd->erasesize / mtd->writesize;
  331. pr_info("MTD device size %llu, eraseblock size %u, "
  332. "page size %u, count of eraseblocks %u, pages per "
  333. "eraseblock %u, OOB size %u\n",
  334. (unsigned long long)mtd->size, mtd->erasesize,
  335. mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
  336. err = -ENOMEM;
  337. readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  338. if (!readbuf)
  339. goto out;
  340. writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  341. if (!writebuf)
  342. goto out;
  343. bbt = kzalloc(ebcnt, GFP_KERNEL);
  344. if (!bbt)
  345. goto out;
  346. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  347. if (err)
  348. goto out;
  349. use_offset = 0;
  350. use_len = mtd->ecclayout->oobavail;
  351. use_len_max = mtd->ecclayout->oobavail;
  352. vary_offset = 0;
  353. /* First test: write all OOB, read it back and verify */
  354. pr_info("test 1 of 5\n");
  355. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  356. if (err)
  357. goto out;
  358. prandom_seed_state(&rnd_state, 1);
  359. err = write_whole_device();
  360. if (err)
  361. goto out;
  362. prandom_seed_state(&rnd_state, 1);
  363. err = verify_all_eraseblocks();
  364. if (err)
  365. goto out;
  366. /*
  367. * Second test: write all OOB, a block at a time, read it back and
  368. * verify.
  369. */
  370. pr_info("test 2 of 5\n");
  371. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  372. if (err)
  373. goto out;
  374. prandom_seed_state(&rnd_state, 3);
  375. err = write_whole_device();
  376. if (err)
  377. goto out;
  378. /* Check all eraseblocks */
  379. prandom_seed_state(&rnd_state, 3);
  380. pr_info("verifying all eraseblocks\n");
  381. for (i = 0; i < ebcnt; ++i) {
  382. if (bbt[i])
  383. continue;
  384. err = verify_eraseblock_in_one_go(i);
  385. if (err)
  386. goto out;
  387. if (i % 256 == 0)
  388. pr_info("verified up to eraseblock %u\n", i);
  389. err = mtdtest_relax();
  390. if (err)
  391. goto out;
  392. }
  393. pr_info("verified %u eraseblocks\n", i);
  394. /*
  395. * Third test: write OOB at varying offsets and lengths, read it back
  396. * and verify.
  397. */
  398. pr_info("test 3 of 5\n");
  399. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  400. if (err)
  401. goto out;
  402. /* Write all eraseblocks */
  403. use_offset = 0;
  404. use_len = mtd->ecclayout->oobavail;
  405. use_len_max = mtd->ecclayout->oobavail;
  406. vary_offset = 1;
  407. prandom_seed_state(&rnd_state, 5);
  408. err = write_whole_device();
  409. if (err)
  410. goto out;
  411. /* Check all eraseblocks */
  412. use_offset = 0;
  413. use_len = mtd->ecclayout->oobavail;
  414. use_len_max = mtd->ecclayout->oobavail;
  415. vary_offset = 1;
  416. prandom_seed_state(&rnd_state, 5);
  417. err = verify_all_eraseblocks();
  418. if (err)
  419. goto out;
  420. use_offset = 0;
  421. use_len = mtd->ecclayout->oobavail;
  422. use_len_max = mtd->ecclayout->oobavail;
  423. vary_offset = 0;
  424. /* Fourth test: try to write off end of device */
  425. pr_info("test 4 of 5\n");
  426. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  427. if (err)
  428. goto out;
  429. addr0 = 0;
  430. for (i = 0; i < ebcnt && bbt[i]; ++i)
  431. addr0 += mtd->erasesize;
  432. /* Attempt to write off end of OOB */
  433. ops.mode = MTD_OPS_AUTO_OOB;
  434. ops.len = 0;
  435. ops.retlen = 0;
  436. ops.ooblen = 1;
  437. ops.oobretlen = 0;
  438. ops.ooboffs = mtd->ecclayout->oobavail;
  439. ops.datbuf = NULL;
  440. ops.oobbuf = writebuf;
  441. pr_info("attempting to start write past end of OOB\n");
  442. pr_info("an error is expected...\n");
  443. err = mtd_write_oob(mtd, addr0, &ops);
  444. if (err) {
  445. pr_info("error occurred as expected\n");
  446. err = 0;
  447. } else {
  448. pr_err("error: can write past end of OOB\n");
  449. errcnt += 1;
  450. }
  451. /* Attempt to read off end of OOB */
  452. ops.mode = MTD_OPS_AUTO_OOB;
  453. ops.len = 0;
  454. ops.retlen = 0;
  455. ops.ooblen = 1;
  456. ops.oobretlen = 0;
  457. ops.ooboffs = mtd->ecclayout->oobavail;
  458. ops.datbuf = NULL;
  459. ops.oobbuf = readbuf;
  460. pr_info("attempting to start read past end of OOB\n");
  461. pr_info("an error is expected...\n");
  462. err = mtd_read_oob(mtd, addr0, &ops);
  463. if (err) {
  464. pr_info("error occurred as expected\n");
  465. err = 0;
  466. } else {
  467. pr_err("error: can read past end of OOB\n");
  468. errcnt += 1;
  469. }
  470. if (bbt[ebcnt - 1])
  471. pr_info("skipping end of device tests because last "
  472. "block is bad\n");
  473. else {
  474. /* Attempt to write off end of device */
  475. ops.mode = MTD_OPS_AUTO_OOB;
  476. ops.len = 0;
  477. ops.retlen = 0;
  478. ops.ooblen = mtd->ecclayout->oobavail + 1;
  479. ops.oobretlen = 0;
  480. ops.ooboffs = 0;
  481. ops.datbuf = NULL;
  482. ops.oobbuf = writebuf;
  483. pr_info("attempting to write past end of device\n");
  484. pr_info("an error is expected...\n");
  485. err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
  486. if (err) {
  487. pr_info("error occurred as expected\n");
  488. err = 0;
  489. } else {
  490. pr_err("error: wrote past end of device\n");
  491. errcnt += 1;
  492. }
  493. /* Attempt to read off end of device */
  494. ops.mode = MTD_OPS_AUTO_OOB;
  495. ops.len = 0;
  496. ops.retlen = 0;
  497. ops.ooblen = mtd->ecclayout->oobavail + 1;
  498. ops.oobretlen = 0;
  499. ops.ooboffs = 0;
  500. ops.datbuf = NULL;
  501. ops.oobbuf = readbuf;
  502. pr_info("attempting to read past end of device\n");
  503. pr_info("an error is expected...\n");
  504. err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
  505. if (err) {
  506. pr_info("error occurred as expected\n");
  507. err = 0;
  508. } else {
  509. pr_err("error: read past end of device\n");
  510. errcnt += 1;
  511. }
  512. err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
  513. if (err)
  514. goto out;
  515. /* Attempt to write off end of device */
  516. ops.mode = MTD_OPS_AUTO_OOB;
  517. ops.len = 0;
  518. ops.retlen = 0;
  519. ops.ooblen = mtd->ecclayout->oobavail;
  520. ops.oobretlen = 0;
  521. ops.ooboffs = 1;
  522. ops.datbuf = NULL;
  523. ops.oobbuf = writebuf;
  524. pr_info("attempting to write past end of device\n");
  525. pr_info("an error is expected...\n");
  526. err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
  527. if (err) {
  528. pr_info("error occurred as expected\n");
  529. err = 0;
  530. } else {
  531. pr_err("error: wrote past end of device\n");
  532. errcnt += 1;
  533. }
  534. /* Attempt to read off end of device */
  535. ops.mode = MTD_OPS_AUTO_OOB;
  536. ops.len = 0;
  537. ops.retlen = 0;
  538. ops.ooblen = mtd->ecclayout->oobavail;
  539. ops.oobretlen = 0;
  540. ops.ooboffs = 1;
  541. ops.datbuf = NULL;
  542. ops.oobbuf = readbuf;
  543. pr_info("attempting to read past end of device\n");
  544. pr_info("an error is expected...\n");
  545. err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
  546. if (err) {
  547. pr_info("error occurred as expected\n");
  548. err = 0;
  549. } else {
  550. pr_err("error: read past end of device\n");
  551. errcnt += 1;
  552. }
  553. }
  554. /* Fifth test: write / read across block boundaries */
  555. pr_info("test 5 of 5\n");
  556. /* Erase all eraseblocks */
  557. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  558. if (err)
  559. goto out;
  560. /* Write all eraseblocks */
  561. prandom_seed_state(&rnd_state, 11);
  562. pr_info("writing OOBs of whole device\n");
  563. for (i = 0; i < ebcnt - 1; ++i) {
  564. int cnt = 2;
  565. int pg;
  566. size_t sz = mtd->ecclayout->oobavail;
  567. if (bbt[i] || bbt[i + 1])
  568. continue;
  569. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  570. prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
  571. for (pg = 0; pg < cnt; ++pg) {
  572. ops.mode = MTD_OPS_AUTO_OOB;
  573. ops.len = 0;
  574. ops.retlen = 0;
  575. ops.ooblen = sz;
  576. ops.oobretlen = 0;
  577. ops.ooboffs = 0;
  578. ops.datbuf = NULL;
  579. ops.oobbuf = writebuf + pg * sz;
  580. err = mtd_write_oob(mtd, addr, &ops);
  581. if (err)
  582. goto out;
  583. if (i % 256 == 0)
  584. pr_info("written up to eraseblock %u\n", i);
  585. err = mtdtest_relax();
  586. if (err)
  587. goto out;
  588. addr += mtd->writesize;
  589. }
  590. }
  591. pr_info("written %u eraseblocks\n", i);
  592. /* Check all eraseblocks */
  593. prandom_seed_state(&rnd_state, 11);
  594. pr_info("verifying all eraseblocks\n");
  595. for (i = 0; i < ebcnt - 1; ++i) {
  596. if (bbt[i] || bbt[i + 1])
  597. continue;
  598. prandom_bytes_state(&rnd_state, writebuf,
  599. mtd->ecclayout->oobavail * 2);
  600. addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
  601. ops.mode = MTD_OPS_AUTO_OOB;
  602. ops.len = 0;
  603. ops.retlen = 0;
  604. ops.ooblen = mtd->ecclayout->oobavail * 2;
  605. ops.oobretlen = 0;
  606. ops.ooboffs = 0;
  607. ops.datbuf = NULL;
  608. ops.oobbuf = readbuf;
  609. err = mtd_read_oob(mtd, addr, &ops);
  610. if (err)
  611. goto out;
  612. if (memcmpshow(addr, readbuf, writebuf,
  613. mtd->ecclayout->oobavail * 2)) {
  614. pr_err("error: verify failed at %#llx\n",
  615. (long long)addr);
  616. errcnt += 1;
  617. if (errcnt > 1000) {
  618. pr_err("error: too many errors\n");
  619. goto out;
  620. }
  621. }
  622. if (i % 256 == 0)
  623. pr_info("verified up to eraseblock %u\n", i);
  624. err = mtdtest_relax();
  625. if (err)
  626. goto out;
  627. }
  628. pr_info("verified %u eraseblocks\n", i);
  629. pr_info("finished with %d errors\n", errcnt);
  630. out:
  631. kfree(bbt);
  632. kfree(writebuf);
  633. kfree(readbuf);
  634. put_mtd_device(mtd);
  635. if (err)
  636. pr_info("error %d occurred\n", err);
  637. printk(KERN_INFO "=================================================\n");
  638. return err;
  639. }
  640. module_init(mtd_oobtest_init);
  641. static void __exit mtd_oobtest_exit(void)
  642. {
  643. return;
  644. }
  645. module_exit(mtd_oobtest_exit);
  646. MODULE_DESCRIPTION("Out-of-band test module");
  647. MODULE_AUTHOR("Adrian Hunter");
  648. MODULE_LICENSE("GPL");