mtd_oobtest.c 18 KB

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