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 static int bitflip_limit;
38 module_param(dev, int, S_IRUGO);
39 MODULE_PARM_DESC(dev, "MTD device number to use");
40 module_param(bitflip_limit, int, S_IRUGO);
41 MODULE_PARM_DESC(bitflip_limit, "Max. allowed bitflips per page");
43 static struct mtd_info *mtd;
44 static unsigned char *readbuf;
45 static unsigned char *writebuf;
46 static unsigned char *bbt;
51 static int use_offset;
53 static int use_len_max;
54 static int vary_offset;
55 static struct rnd_state rnd_state;
57 static void do_vary_offset(void)
62 if (use_offset >= use_len_max)
64 use_len = use_len_max - use_offset;
68 static int write_eraseblock(int ebnum)
71 struct mtd_oob_ops ops;
73 loff_t addr = (loff_t)ebnum * mtd->erasesize;
75 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
76 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
77 ops.mode = MTD_OPS_AUTO_OOB;
82 ops.ooboffs = use_offset;
84 ops.oobbuf = writebuf + (use_len_max * i) + use_offset;
85 err = mtd_write_oob(mtd, addr, &ops);
86 if (err || ops.oobretlen != use_len) {
87 pr_err("error: writeoob failed at %#llx\n",
89 pr_err("error: use_len %d, use_offset %d\n",
92 return err ? err : -1;
101 static int write_whole_device(void)
106 pr_info("writing OOBs of whole device\n");
107 for (i = 0; i < ebcnt; ++i) {
110 err = write_eraseblock(i);
114 pr_info("written up to eraseblock %u\n", i);
117 pr_info("written %u eraseblocks\n", i);
122 * Display the address, offset and data bytes at comparison failure.
123 * Return number of bitflips encountered.
125 static size_t memcmpshow(loff_t addr, const void *cs, const void *ct, size_t count)
127 const unsigned char *su1, *su2;
132 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--, i++) {
135 pr_info("error @addr[0x%lx:0x%zx] 0x%x -> 0x%x diff 0x%x\n",
136 (unsigned long)addr, i, *su1, *su2, res);
137 bitflips += hweight8(res);
145 * Compare with 0xff and show the address, offset and data bytes at
146 * comparison failure. Return number of bitflips encountered.
148 static size_t memffshow(loff_t addr, loff_t offset, const void *cs,
151 const unsigned char *su1;
156 for (su1 = cs; 0 < count; ++su1, count--, i++) {
159 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
160 (unsigned long)addr, (unsigned long)offset + i,
162 bitflips += hweight8(res);
169 static int verify_eraseblock(int ebnum)
172 struct mtd_oob_ops ops;
174 loff_t addr = (loff_t)ebnum * mtd->erasesize;
177 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
178 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
179 ops.mode = MTD_OPS_AUTO_OOB;
182 ops.ooblen = use_len;
184 ops.ooboffs = use_offset;
186 ops.oobbuf = readbuf;
187 err = mtd_read_oob(mtd, addr, &ops);
188 if (err || ops.oobretlen != use_len) {
189 pr_err("error: readoob failed at %#llx\n",
192 return err ? err : -1;
195 bitflips = memcmpshow(addr, readbuf,
196 writebuf + (use_len_max * i) + use_offset,
198 if (bitflips > bitflip_limit) {
199 pr_err("error: verify failed at %#llx\n",
203 pr_err("error: too many errors\n");
206 } else if (bitflips) {
207 pr_info("ignoring error as within bitflip_limit\n");
210 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
213 ops.mode = MTD_OPS_AUTO_OOB;
216 ops.ooblen = mtd->ecclayout->oobavail;
220 ops.oobbuf = readbuf;
221 err = mtd_read_oob(mtd, addr, &ops);
222 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
223 pr_err("error: readoob failed at %#llx\n",
226 return err ? err : -1;
228 bitflips = memcmpshow(addr, readbuf + use_offset,
229 writebuf + (use_len_max * i) + use_offset,
232 /* verify pre-offset area for 0xff */
233 bitflips += memffshow(addr, 0, readbuf, use_offset);
235 /* verify post-(use_offset + use_len) area for 0xff */
236 k = use_offset + use_len;
237 bitflips += memffshow(addr, k, readbuf + k,
238 mtd->ecclayout->oobavail - k);
240 if (bitflips > bitflip_limit) {
241 pr_err("error: verify failed at %#llx\n",
245 pr_err("error: too many errors\n");
248 } else if (bitflips) {
249 pr_info("ignoring errors as within bitflip limit\n");
258 static int verify_eraseblock_in_one_go(int ebnum)
260 struct mtd_oob_ops ops;
262 loff_t addr = (loff_t)ebnum * mtd->erasesize;
263 size_t len = mtd->ecclayout->oobavail * pgcnt;
264 size_t oobavail = mtd->ecclayout->oobavail;
268 prandom_bytes_state(&rnd_state, writebuf, len);
269 ops.mode = MTD_OPS_AUTO_OOB;
276 ops.oobbuf = readbuf;
278 /* read entire block's OOB at one go */
279 err = mtd_read_oob(mtd, addr, &ops);
280 if (err || ops.oobretlen != len) {
281 pr_err("error: readoob failed at %#llx\n",
284 return err ? err : -1;
287 /* verify one page OOB at a time for bitflip per page limit check */
288 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
289 bitflips = memcmpshow(addr, readbuf + (i * oobavail),
290 writebuf + (i * oobavail), oobavail);
291 if (bitflips > bitflip_limit) {
292 pr_err("error: verify failed at %#llx\n",
296 pr_err("error: too many errors\n");
299 } else if (bitflips) {
300 pr_info("ignoring error as within bitflip_limit\n");
307 static int verify_all_eraseblocks(void)
312 pr_info("verifying all eraseblocks\n");
313 for (i = 0; i < ebcnt; ++i) {
316 err = verify_eraseblock(i);
320 pr_info("verified up to eraseblock %u\n", i);
323 pr_info("verified %u eraseblocks\n", i);
327 static int __init mtd_oobtest_init(void)
332 struct mtd_oob_ops ops;
333 loff_t addr = 0, addr0;
335 printk(KERN_INFO "\n");
336 printk(KERN_INFO "=================================================\n");
339 pr_info("Please specify a valid mtd-device via module parameter\n");
340 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
344 pr_info("MTD device: %d\n", dev);
346 mtd = get_mtd_device(NULL, dev);
349 pr_err("error: cannot get MTD device\n");
353 if (!mtd_type_is_nand(mtd)) {
354 pr_info("this test requires NAND flash\n");
359 do_div(tmp, mtd->erasesize);
361 pgcnt = mtd->erasesize / mtd->writesize;
363 pr_info("MTD device size %llu, eraseblock size %u, "
364 "page size %u, count of eraseblocks %u, pages per "
365 "eraseblock %u, OOB size %u\n",
366 (unsigned long long)mtd->size, mtd->erasesize,
367 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
370 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
373 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
376 bbt = kzalloc(ebcnt, GFP_KERNEL);
380 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
385 use_len = mtd->ecclayout->oobavail;
386 use_len_max = mtd->ecclayout->oobavail;
389 /* First test: write all OOB, read it back and verify */
390 pr_info("test 1 of 5\n");
392 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
396 prandom_seed_state(&rnd_state, 1);
397 err = write_whole_device();
401 prandom_seed_state(&rnd_state, 1);
402 err = verify_all_eraseblocks();
407 * Second test: write all OOB, a block at a time, read it back and
410 pr_info("test 2 of 5\n");
412 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
416 prandom_seed_state(&rnd_state, 3);
417 err = write_whole_device();
421 /* Check all eraseblocks */
422 prandom_seed_state(&rnd_state, 3);
423 pr_info("verifying all eraseblocks\n");
424 for (i = 0; i < ebcnt; ++i) {
427 err = verify_eraseblock_in_one_go(i);
431 pr_info("verified up to eraseblock %u\n", i);
434 pr_info("verified %u eraseblocks\n", i);
437 * Third test: write OOB at varying offsets and lengths, read it back
440 pr_info("test 3 of 5\n");
442 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
446 /* Write all eraseblocks */
448 use_len = mtd->ecclayout->oobavail;
449 use_len_max = mtd->ecclayout->oobavail;
451 prandom_seed_state(&rnd_state, 5);
453 err = write_whole_device();
457 /* Check all eraseblocks */
459 use_len = mtd->ecclayout->oobavail;
460 use_len_max = mtd->ecclayout->oobavail;
462 prandom_seed_state(&rnd_state, 5);
463 err = verify_all_eraseblocks();
468 use_len = mtd->ecclayout->oobavail;
469 use_len_max = mtd->ecclayout->oobavail;
472 /* Fourth test: try to write off end of device */
473 pr_info("test 4 of 5\n");
475 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
480 for (i = 0; i < ebcnt && bbt[i]; ++i)
481 addr0 += mtd->erasesize;
483 /* Attempt to write off end of OOB */
484 ops.mode = MTD_OPS_AUTO_OOB;
489 ops.ooboffs = mtd->ecclayout->oobavail;
491 ops.oobbuf = writebuf;
492 pr_info("attempting to start write past end of OOB\n");
493 pr_info("an error is expected...\n");
494 err = mtd_write_oob(mtd, addr0, &ops);
496 pr_info("error occurred as expected\n");
499 pr_err("error: can write past end of OOB\n");
503 /* Attempt to read off end of OOB */
504 ops.mode = MTD_OPS_AUTO_OOB;
509 ops.ooboffs = mtd->ecclayout->oobavail;
511 ops.oobbuf = readbuf;
512 pr_info("attempting to start read past end of OOB\n");
513 pr_info("an error is expected...\n");
514 err = mtd_read_oob(mtd, addr0, &ops);
516 pr_info("error occurred as expected\n");
519 pr_err("error: can read past end of OOB\n");
524 pr_info("skipping end of device tests because last "
527 /* Attempt to write off end of device */
528 ops.mode = MTD_OPS_AUTO_OOB;
531 ops.ooblen = mtd->ecclayout->oobavail + 1;
535 ops.oobbuf = writebuf;
536 pr_info("attempting to write past end of device\n");
537 pr_info("an error is expected...\n");
538 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
540 pr_info("error occurred as expected\n");
543 pr_err("error: wrote past end of device\n");
547 /* Attempt to read off end of device */
548 ops.mode = MTD_OPS_AUTO_OOB;
551 ops.ooblen = mtd->ecclayout->oobavail + 1;
555 ops.oobbuf = readbuf;
556 pr_info("attempting to read past end of device\n");
557 pr_info("an error is expected...\n");
558 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
560 pr_info("error occurred as expected\n");
563 pr_err("error: read past end of device\n");
567 err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
571 /* Attempt to write off end of device */
572 ops.mode = MTD_OPS_AUTO_OOB;
575 ops.ooblen = mtd->ecclayout->oobavail;
579 ops.oobbuf = writebuf;
580 pr_info("attempting to write past end of device\n");
581 pr_info("an error is expected...\n");
582 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
584 pr_info("error occurred as expected\n");
587 pr_err("error: wrote past end of device\n");
591 /* Attempt to read off end of device */
592 ops.mode = MTD_OPS_AUTO_OOB;
595 ops.ooblen = mtd->ecclayout->oobavail;
599 ops.oobbuf = readbuf;
600 pr_info("attempting to read past end of device\n");
601 pr_info("an error is expected...\n");
602 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
604 pr_info("error occurred as expected\n");
607 pr_err("error: read past end of device\n");
612 /* Fifth test: write / read across block boundaries */
613 pr_info("test 5 of 5\n");
615 /* Erase all eraseblocks */
616 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
620 /* Write all eraseblocks */
621 prandom_seed_state(&rnd_state, 11);
622 pr_info("writing OOBs of whole device\n");
623 for (i = 0; i < ebcnt - 1; ++i) {
626 size_t sz = mtd->ecclayout->oobavail;
627 if (bbt[i] || bbt[i + 1])
629 addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
630 prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
631 for (pg = 0; pg < cnt; ++pg) {
632 ops.mode = MTD_OPS_AUTO_OOB;
639 ops.oobbuf = writebuf + pg * sz;
640 err = mtd_write_oob(mtd, addr, &ops);
644 pr_info("written up to eraseblock %u\n", i);
646 addr += mtd->writesize;
649 pr_info("written %u eraseblocks\n", i);
651 /* Check all eraseblocks */
652 prandom_seed_state(&rnd_state, 11);
653 pr_info("verifying all eraseblocks\n");
654 for (i = 0; i < ebcnt - 1; ++i) {
655 if (bbt[i] || bbt[i + 1])
657 prandom_bytes_state(&rnd_state, writebuf,
658 mtd->ecclayout->oobavail * 2);
659 addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize;
660 ops.mode = MTD_OPS_AUTO_OOB;
663 ops.ooblen = mtd->ecclayout->oobavail * 2;
667 ops.oobbuf = readbuf;
668 err = mtd_read_oob(mtd, addr, &ops);
671 if (memcmpshow(addr, readbuf, writebuf,
672 mtd->ecclayout->oobavail * 2)) {
673 pr_err("error: verify failed at %#llx\n",
677 pr_err("error: too many errors\n");
682 pr_info("verified up to eraseblock %u\n", i);
685 pr_info("verified %u eraseblocks\n", i);
687 pr_info("finished with %d errors\n", errcnt);
694 pr_info("error %d occurred\n", err);
695 printk(KERN_INFO "=================================================\n");
698 module_init(mtd_oobtest_init);
700 static void __exit mtd_oobtest_exit(void)
704 module_exit(mtd_oobtest_exit);
706 MODULE_DESCRIPTION("Out-of-band test module");
707 MODULE_AUTHOR("Adrian Hunter");
708 MODULE_LICENSE("GPL");