oobtest.c 17 KB

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