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 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
73 prandom_bytes_state(&rnd_state, writebuf, use_len);
74 ops.mode = MTD_OPS_AUTO_OOB;
79 ops.ooboffs = use_offset;
81 ops.oobbuf = writebuf;
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 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
126 prandom_bytes_state(&rnd_state, writebuf, use_len);
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)) {
143 pr_err("error: verify failed at %#llx\n",
147 pr_err("error: too many errors\n");
151 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
154 ops.mode = MTD_OPS_AUTO_OOB;
157 ops.ooblen = mtd->ecclayout->oobavail;
161 ops.oobbuf = readbuf;
162 err = mtd_read_oob(mtd, addr, &ops);
163 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
164 pr_err("error: readoob failed at %#llx\n",
167 return err ? err : -1;
169 if (memcmp(readbuf + use_offset, writebuf, use_len)) {
170 pr_err("error: verify failed at %#llx\n",
174 pr_err("error: too many errors\n");
178 for (k = 0; k < use_offset; ++k)
179 if (readbuf[k] != 0xff) {
180 pr_err("error: verify 0xff "
190 for (k = use_offset + use_len;
191 k < mtd->ecclayout->oobavail; ++k)
192 if (readbuf[k] != 0xff) {
193 pr_err("error: verify 0xff "
210 static int verify_eraseblock_in_one_go(int ebnum)
212 struct mtd_oob_ops ops;
214 loff_t addr = ebnum * mtd->erasesize;
215 size_t len = mtd->ecclayout->oobavail * pgcnt;
217 prandom_bytes_state(&rnd_state, writebuf, len);
218 ops.mode = MTD_OPS_AUTO_OOB;
225 ops.oobbuf = readbuf;
226 err = mtd_read_oob(mtd, addr, &ops);
227 if (err || ops.oobretlen != len) {
228 pr_err("error: readoob failed at %#llx\n",
231 return err ? err : -1;
233 if (memcmp(readbuf, writebuf, len)) {
234 pr_err("error: verify failed at %#llx\n",
238 pr_err("error: too many errors\n");
246 static int verify_all_eraseblocks(void)
251 pr_info("verifying all eraseblocks\n");
252 for (i = 0; i < ebcnt; ++i) {
255 err = verify_eraseblock(i);
259 pr_info("verified up to eraseblock %u\n", i);
262 pr_info("verified %u eraseblocks\n", i);
266 static int __init mtd_oobtest_init(void)
271 struct mtd_oob_ops ops;
272 loff_t addr = 0, addr0;
274 printk(KERN_INFO "\n");
275 printk(KERN_INFO "=================================================\n");
278 pr_info("Please specify a valid mtd-device via module parameter\n");
279 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
283 pr_info("MTD device: %d\n", dev);
285 mtd = get_mtd_device(NULL, dev);
288 pr_err("error: cannot get MTD device\n");
292 if (!mtd_type_is_nand(mtd)) {
293 pr_info("this test requires NAND flash\n");
298 do_div(tmp, mtd->erasesize);
300 pgcnt = mtd->erasesize / mtd->writesize;
302 pr_info("MTD device size %llu, eraseblock size %u, "
303 "page size %u, count of eraseblocks %u, pages per "
304 "eraseblock %u, OOB size %u\n",
305 (unsigned long long)mtd->size, mtd->erasesize,
306 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
309 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
312 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
315 bbt = kzalloc(ebcnt, GFP_KERNEL);
319 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
324 use_len = mtd->ecclayout->oobavail;
325 use_len_max = mtd->ecclayout->oobavail;
328 /* First test: write all OOB, read it back and verify */
329 pr_info("test 1 of 5\n");
331 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
335 prandom_seed_state(&rnd_state, 1);
336 err = write_whole_device();
340 prandom_seed_state(&rnd_state, 1);
341 err = verify_all_eraseblocks();
346 * Second test: write all OOB, a block at a time, read it back and
349 pr_info("test 2 of 5\n");
351 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
355 prandom_seed_state(&rnd_state, 3);
356 err = write_whole_device();
360 /* Check all eraseblocks */
361 prandom_seed_state(&rnd_state, 3);
362 pr_info("verifying all eraseblocks\n");
363 for (i = 0; i < ebcnt; ++i) {
366 err = verify_eraseblock_in_one_go(i);
370 pr_info("verified up to eraseblock %u\n", i);
373 pr_info("verified %u eraseblocks\n", i);
376 * Third test: write OOB at varying offsets and lengths, read it back
379 pr_info("test 3 of 5\n");
381 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
385 /* Write all eraseblocks */
387 use_len = mtd->ecclayout->oobavail;
388 use_len_max = mtd->ecclayout->oobavail;
390 prandom_seed_state(&rnd_state, 5);
392 err = write_whole_device();
396 /* Check all eraseblocks */
398 use_len = mtd->ecclayout->oobavail;
399 use_len_max = mtd->ecclayout->oobavail;
401 prandom_seed_state(&rnd_state, 5);
402 err = verify_all_eraseblocks();
407 use_len = mtd->ecclayout->oobavail;
408 use_len_max = mtd->ecclayout->oobavail;
411 /* Fourth test: try to write off end of device */
412 pr_info("test 4 of 5\n");
414 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
419 for (i = 0; i < ebcnt && bbt[i]; ++i)
420 addr0 += mtd->erasesize;
422 /* Attempt to write off end of OOB */
423 ops.mode = MTD_OPS_AUTO_OOB;
428 ops.ooboffs = mtd->ecclayout->oobavail;
430 ops.oobbuf = writebuf;
431 pr_info("attempting to start write past end of OOB\n");
432 pr_info("an error is expected...\n");
433 err = mtd_write_oob(mtd, addr0, &ops);
435 pr_info("error occurred as expected\n");
438 pr_err("error: can write past end of OOB\n");
442 /* Attempt to read off end of OOB */
443 ops.mode = MTD_OPS_AUTO_OOB;
448 ops.ooboffs = mtd->ecclayout->oobavail;
450 ops.oobbuf = readbuf;
451 pr_info("attempting to start read past end of OOB\n");
452 pr_info("an error is expected...\n");
453 err = mtd_read_oob(mtd, addr0, &ops);
455 pr_info("error occurred as expected\n");
458 pr_err("error: can read past end of OOB\n");
463 pr_info("skipping end of device tests because last "
466 /* Attempt to write off end of device */
467 ops.mode = MTD_OPS_AUTO_OOB;
470 ops.ooblen = mtd->ecclayout->oobavail + 1;
474 ops.oobbuf = writebuf;
475 pr_info("attempting to write past end of device\n");
476 pr_info("an error is expected...\n");
477 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
479 pr_info("error occurred as expected\n");
482 pr_err("error: wrote past end of device\n");
486 /* Attempt to read off end of device */
487 ops.mode = MTD_OPS_AUTO_OOB;
490 ops.ooblen = mtd->ecclayout->oobavail + 1;
494 ops.oobbuf = readbuf;
495 pr_info("attempting to read past end of device\n");
496 pr_info("an error is expected...\n");
497 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
499 pr_info("error occurred as expected\n");
502 pr_err("error: read past end of device\n");
506 err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
510 /* Attempt to write off end of device */
511 ops.mode = MTD_OPS_AUTO_OOB;
514 ops.ooblen = mtd->ecclayout->oobavail;
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);
523 pr_info("error occurred as expected\n");
526 pr_err("error: wrote past end of device\n");
530 /* Attempt to read off end of device */
531 ops.mode = MTD_OPS_AUTO_OOB;
534 ops.ooblen = mtd->ecclayout->oobavail;
538 ops.oobbuf = readbuf;
539 pr_info("attempting to read past end of device\n");
540 pr_info("an error is expected...\n");
541 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
543 pr_info("error occurred as expected\n");
546 pr_err("error: read past end of device\n");
551 /* Fifth test: write / read across block boundaries */
552 pr_info("test 5 of 5\n");
554 /* Erase all eraseblocks */
555 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
559 /* Write all eraseblocks */
560 prandom_seed_state(&rnd_state, 11);
561 pr_info("writing OOBs of whole device\n");
562 for (i = 0; i < ebcnt - 1; ++i) {
565 size_t sz = mtd->ecclayout->oobavail;
566 if (bbt[i] || bbt[i + 1])
568 addr = (i + 1) * mtd->erasesize - mtd->writesize;
569 for (pg = 0; pg < cnt; ++pg) {
570 prandom_bytes_state(&rnd_state, writebuf, sz);
571 ops.mode = MTD_OPS_AUTO_OOB;
578 ops.oobbuf = writebuf;
579 err = mtd_write_oob(mtd, addr, &ops);
583 pr_info("written up to eraseblock %u\n", i);
585 addr += mtd->writesize;
588 pr_info("written %u eraseblocks\n", i);
590 /* Check all eraseblocks */
591 prandom_seed_state(&rnd_state, 11);
592 pr_info("verifying all eraseblocks\n");
593 for (i = 0; i < ebcnt - 1; ++i) {
594 if (bbt[i] || bbt[i + 1])
596 prandom_bytes_state(&rnd_state, writebuf,
597 mtd->ecclayout->oobavail * 2);
598 addr = (i + 1) * mtd->erasesize - mtd->writesize;
599 ops.mode = MTD_OPS_AUTO_OOB;
602 ops.ooblen = mtd->ecclayout->oobavail * 2;
606 ops.oobbuf = readbuf;
607 err = mtd_read_oob(mtd, addr, &ops);
610 if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) {
611 pr_err("error: verify failed at %#llx\n",
615 pr_err("error: too many errors\n");
620 pr_info("verified up to eraseblock %u\n", i);
623 pr_info("verified %u eraseblocks\n", i);
625 pr_info("finished with %d errors\n", errcnt);
632 pr_info("error %d occurred\n", err);
633 printk(KERN_INFO "=================================================\n");
636 module_init(mtd_oobtest_init);
638 static void __exit mtd_oobtest_exit(void)
642 module_exit(mtd_oobtest_exit);
644 MODULE_DESCRIPTION("Out-of-band test module");
645 MODULE_AUTHOR("Adrian Hunter");
646 MODULE_LICENSE("GPL");