2 * Copyright (C) 2006-2008 Nokia Corporation
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.
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
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.
17 * Test OOB read and write on MTD device.
19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <asm/div64.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/err.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/slab.h>
31 #include <linux/sched.h>
32 #include <linux/random.h>
36 static int dev = -EINVAL;
37 module_param(dev, int, S_IRUGO);
38 MODULE_PARM_DESC(dev, "MTD device number to use");
40 static struct mtd_info *mtd;
41 static unsigned char *readbuf;
42 static unsigned char *writebuf;
43 static unsigned char *bbt;
48 static int use_offset;
50 static int use_len_max;
51 static int vary_offset;
52 static struct rnd_state rnd_state;
54 static void do_vary_offset(void)
59 if (use_offset >= use_len_max)
61 use_len = use_len_max - use_offset;
65 static int write_eraseblock(int ebnum)
68 struct mtd_oob_ops ops;
70 loff_t addr = ebnum * mtd->erasesize;
72 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
73 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
74 ops.mode = MTD_OPS_AUTO_OOB;
79 ops.ooboffs = use_offset;
81 ops.oobbuf = writebuf + (use_len_max * i) + use_offset;
82 err = mtd_write_oob(mtd, addr, &ops);
83 if (err || ops.oobretlen != use_len) {
84 pr_err("error: writeoob failed at %#llx\n",
86 pr_err("error: use_len %d, use_offset %d\n",
89 return err ? err : -1;
98 static int write_whole_device(void)
103 pr_info("writing OOBs of whole device\n");
104 for (i = 0; i < ebcnt; ++i) {
107 err = write_eraseblock(i);
111 pr_info("written up to eraseblock %u\n", i);
114 pr_info("written %u eraseblocks\n", i);
118 static int verify_eraseblock(int ebnum)
121 struct mtd_oob_ops ops;
123 loff_t addr = ebnum * mtd->erasesize;
125 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
126 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
127 ops.mode = MTD_OPS_AUTO_OOB;
130 ops.ooblen = use_len;
132 ops.ooboffs = use_offset;
134 ops.oobbuf = readbuf;
135 err = mtd_read_oob(mtd, addr, &ops);
136 if (err || ops.oobretlen != use_len) {
137 pr_err("error: readoob failed at %#llx\n",
140 return err ? err : -1;
142 if (memcmp(readbuf, writebuf + (use_len_max * i) + use_offset,
144 pr_err("error: verify failed at %#llx\n",
148 pr_err("error: too many errors\n");
152 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
155 ops.mode = MTD_OPS_AUTO_OOB;
158 ops.ooblen = mtd->ecclayout->oobavail;
162 ops.oobbuf = readbuf;
163 err = mtd_read_oob(mtd, addr, &ops);
164 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
165 pr_err("error: readoob failed at %#llx\n",
168 return err ? err : -1;
170 if (memcmp(readbuf + use_offset,
171 writebuf + (use_len_max * i) + use_offset,
173 pr_err("error: verify failed at %#llx\n",
177 pr_err("error: too many errors\n");
181 for (k = 0; k < use_offset; ++k)
182 if (readbuf[k] != 0xff) {
183 pr_err("error: verify 0xff "
193 for (k = use_offset + use_len;
194 k < mtd->ecclayout->oobavail; ++k)
195 if (readbuf[k] != 0xff) {
196 pr_err("error: verify 0xff "
213 static int verify_eraseblock_in_one_go(int ebnum)
215 struct mtd_oob_ops ops;
217 loff_t addr = ebnum * mtd->erasesize;
218 size_t len = mtd->ecclayout->oobavail * pgcnt;
220 prandom_bytes_state(&rnd_state, writebuf, len);
221 ops.mode = MTD_OPS_AUTO_OOB;
228 ops.oobbuf = readbuf;
229 err = mtd_read_oob(mtd, addr, &ops);
230 if (err || ops.oobretlen != len) {
231 pr_err("error: readoob failed at %#llx\n",
234 return err ? err : -1;
236 if (memcmp(readbuf, writebuf, len)) {
237 pr_err("error: verify failed at %#llx\n",
241 pr_err("error: too many errors\n");
249 static int verify_all_eraseblocks(void)
254 pr_info("verifying all eraseblocks\n");
255 for (i = 0; i < ebcnt; ++i) {
258 err = verify_eraseblock(i);
262 pr_info("verified up to eraseblock %u\n", i);
265 pr_info("verified %u eraseblocks\n", i);
269 static int __init mtd_oobtest_init(void)
274 struct mtd_oob_ops ops;
275 loff_t addr = 0, addr0;
277 printk(KERN_INFO "\n");
278 printk(KERN_INFO "=================================================\n");
281 pr_info("Please specify a valid mtd-device via module parameter\n");
282 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
286 pr_info("MTD device: %d\n", dev);
288 mtd = get_mtd_device(NULL, dev);
291 pr_err("error: cannot get MTD device\n");
295 if (!mtd_type_is_nand(mtd)) {
296 pr_info("this test requires NAND flash\n");
301 do_div(tmp, mtd->erasesize);
303 pgcnt = mtd->erasesize / mtd->writesize;
305 pr_info("MTD device size %llu, eraseblock size %u, "
306 "page size %u, count of eraseblocks %u, pages per "
307 "eraseblock %u, OOB size %u\n",
308 (unsigned long long)mtd->size, mtd->erasesize,
309 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
312 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
315 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
318 bbt = kzalloc(ebcnt, GFP_KERNEL);
322 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
327 use_len = mtd->ecclayout->oobavail;
328 use_len_max = mtd->ecclayout->oobavail;
331 /* First test: write all OOB, read it back and verify */
332 pr_info("test 1 of 5\n");
334 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
338 prandom_seed_state(&rnd_state, 1);
339 err = write_whole_device();
343 prandom_seed_state(&rnd_state, 1);
344 err = verify_all_eraseblocks();
349 * Second test: write all OOB, a block at a time, read it back and
352 pr_info("test 2 of 5\n");
354 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
358 prandom_seed_state(&rnd_state, 3);
359 err = write_whole_device();
363 /* Check all eraseblocks */
364 prandom_seed_state(&rnd_state, 3);
365 pr_info("verifying all eraseblocks\n");
366 for (i = 0; i < ebcnt; ++i) {
369 err = verify_eraseblock_in_one_go(i);
373 pr_info("verified up to eraseblock %u\n", i);
376 pr_info("verified %u eraseblocks\n", i);
379 * Third test: write OOB at varying offsets and lengths, read it back
382 pr_info("test 3 of 5\n");
384 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
388 /* Write all eraseblocks */
390 use_len = mtd->ecclayout->oobavail;
391 use_len_max = mtd->ecclayout->oobavail;
393 prandom_seed_state(&rnd_state, 5);
395 err = write_whole_device();
399 /* Check all eraseblocks */
401 use_len = mtd->ecclayout->oobavail;
402 use_len_max = mtd->ecclayout->oobavail;
404 prandom_seed_state(&rnd_state, 5);
405 err = verify_all_eraseblocks();
410 use_len = mtd->ecclayout->oobavail;
411 use_len_max = mtd->ecclayout->oobavail;
414 /* Fourth test: try to write off end of device */
415 pr_info("test 4 of 5\n");
417 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
422 for (i = 0; i < ebcnt && bbt[i]; ++i)
423 addr0 += mtd->erasesize;
425 /* Attempt to write off end of OOB */
426 ops.mode = MTD_OPS_AUTO_OOB;
431 ops.ooboffs = mtd->ecclayout->oobavail;
433 ops.oobbuf = writebuf;
434 pr_info("attempting to start write past end of OOB\n");
435 pr_info("an error is expected...\n");
436 err = mtd_write_oob(mtd, addr0, &ops);
438 pr_info("error occurred as expected\n");
441 pr_err("error: can write past end of OOB\n");
445 /* Attempt to read off end of OOB */
446 ops.mode = MTD_OPS_AUTO_OOB;
451 ops.ooboffs = mtd->ecclayout->oobavail;
453 ops.oobbuf = readbuf;
454 pr_info("attempting to start read past end of OOB\n");
455 pr_info("an error is expected...\n");
456 err = mtd_read_oob(mtd, addr0, &ops);
458 pr_info("error occurred as expected\n");
461 pr_err("error: can read past end of OOB\n");
466 pr_info("skipping end of device tests because last "
469 /* Attempt to write off end of device */
470 ops.mode = MTD_OPS_AUTO_OOB;
473 ops.ooblen = mtd->ecclayout->oobavail + 1;
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);
482 pr_info("error occurred as expected\n");
485 pr_err("error: wrote past end of device\n");
489 /* Attempt to read off end of device */
490 ops.mode = MTD_OPS_AUTO_OOB;
493 ops.ooblen = mtd->ecclayout->oobavail + 1;
497 ops.oobbuf = readbuf;
498 pr_info("attempting to read past end of device\n");
499 pr_info("an error is expected...\n");
500 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
502 pr_info("error occurred as expected\n");
505 pr_err("error: read past end of device\n");
509 err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
513 /* Attempt to write off end of device */
514 ops.mode = MTD_OPS_AUTO_OOB;
517 ops.ooblen = mtd->ecclayout->oobavail;
521 ops.oobbuf = writebuf;
522 pr_info("attempting to write past end of device\n");
523 pr_info("an error is expected...\n");
524 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
526 pr_info("error occurred as expected\n");
529 pr_err("error: wrote past end of device\n");
533 /* Attempt to read off end of device */
534 ops.mode = MTD_OPS_AUTO_OOB;
537 ops.ooblen = mtd->ecclayout->oobavail;
541 ops.oobbuf = readbuf;
542 pr_info("attempting to read past end of device\n");
543 pr_info("an error is expected...\n");
544 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
546 pr_info("error occurred as expected\n");
549 pr_err("error: read past end of device\n");
554 /* Fifth test: write / read across block boundaries */
555 pr_info("test 5 of 5\n");
557 /* Erase all eraseblocks */
558 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
562 /* Write all eraseblocks */
563 prandom_seed_state(&rnd_state, 11);
564 pr_info("writing OOBs of whole device\n");
565 for (i = 0; i < ebcnt - 1; ++i) {
568 size_t sz = mtd->ecclayout->oobavail;
569 if (bbt[i] || bbt[i + 1])
571 addr = (i + 1) * mtd->erasesize - mtd->writesize;
572 prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
573 for (pg = 0; pg < cnt; ++pg) {
574 ops.mode = MTD_OPS_AUTO_OOB;
581 ops.oobbuf = writebuf + pg * sz;
582 err = mtd_write_oob(mtd, addr, &ops);
586 pr_info("written up to eraseblock %u\n", i);
588 addr += mtd->writesize;
591 pr_info("written %u eraseblocks\n", i);
593 /* Check all eraseblocks */
594 prandom_seed_state(&rnd_state, 11);
595 pr_info("verifying all eraseblocks\n");
596 for (i = 0; i < ebcnt - 1; ++i) {
597 if (bbt[i] || bbt[i + 1])
599 prandom_bytes_state(&rnd_state, writebuf,
600 mtd->ecclayout->oobavail * 2);
601 addr = (i + 1) * mtd->erasesize - mtd->writesize;
602 ops.mode = MTD_OPS_AUTO_OOB;
605 ops.ooblen = mtd->ecclayout->oobavail * 2;
609 ops.oobbuf = readbuf;
610 err = mtd_read_oob(mtd, addr, &ops);
613 if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) {
614 pr_err("error: verify failed at %#llx\n",
618 pr_err("error: too many errors\n");
623 pr_info("verified up to eraseblock %u\n", i);
626 pr_info("verified %u eraseblocks\n", i);
628 pr_info("finished with %d errors\n", errcnt);
635 pr_info("error %d occurred\n", err);
636 printk(KERN_INFO "=================================================\n");
639 module_init(mtd_oobtest_init);
641 static void __exit mtd_oobtest_exit(void)
645 module_exit(mtd_oobtest_exit);
647 MODULE_DESCRIPTION("Out-of-band test module");
648 MODULE_AUTHOR("Adrian Hunter");
649 MODULE_LICENSE("GPL");