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>
34 static int dev
= -EINVAL
;
35 module_param(dev
, int, S_IRUGO
);
36 MODULE_PARM_DESC(dev
, "MTD device number to use");
38 static struct mtd_info
*mtd
;
39 static unsigned char *readbuf
;
40 static unsigned char *writebuf
;
41 static unsigned char *bbt
;
46 static int use_offset
;
48 static int use_len_max
;
49 static int vary_offset
;
50 static struct rnd_state rnd_state
;
52 static int erase_eraseblock(int ebnum
)
56 loff_t addr
= ebnum
* mtd
->erasesize
;
58 memset(&ei
, 0, sizeof(struct erase_info
));
61 ei
.len
= mtd
->erasesize
;
63 err
= mtd_erase(mtd
, &ei
);
65 pr_err("error %d while erasing EB %d\n", err
, ebnum
);
69 if (ei
.state
== MTD_ERASE_FAILED
) {
70 pr_err("some erase error occurred at EB %d\n", ebnum
);
77 static int erase_whole_device(void)
82 pr_info("erasing whole device\n");
83 for (i
= 0; i
< ebcnt
; ++i
) {
86 err
= erase_eraseblock(i
);
91 pr_info("erased %u eraseblocks\n", i
);
95 static void do_vary_offset(void)
100 if (use_offset
>= use_len_max
)
102 use_len
= use_len_max
- use_offset
;
106 static int write_eraseblock(int ebnum
)
109 struct mtd_oob_ops ops
;
111 loff_t addr
= ebnum
* mtd
->erasesize
;
113 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
114 prandom_bytes_state(&rnd_state
, writebuf
, use_len
);
115 ops
.mode
= MTD_OPS_AUTO_OOB
;
118 ops
.ooblen
= use_len
;
120 ops
.ooboffs
= use_offset
;
122 ops
.oobbuf
= writebuf
;
123 err
= mtd_write_oob(mtd
, addr
, &ops
);
124 if (err
|| ops
.oobretlen
!= use_len
) {
125 pr_err("error: writeoob failed at %#llx\n",
127 pr_err("error: use_len %d, use_offset %d\n",
128 use_len
, use_offset
);
130 return err
? err
: -1;
139 static int write_whole_device(void)
144 pr_info("writing OOBs of whole device\n");
145 for (i
= 0; i
< ebcnt
; ++i
) {
148 err
= write_eraseblock(i
);
152 pr_info("written up to eraseblock %u\n", i
);
155 pr_info("written %u eraseblocks\n", i
);
159 static int verify_eraseblock(int ebnum
)
162 struct mtd_oob_ops ops
;
164 loff_t addr
= ebnum
* mtd
->erasesize
;
166 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
167 prandom_bytes_state(&rnd_state
, writebuf
, use_len
);
168 ops
.mode
= MTD_OPS_AUTO_OOB
;
171 ops
.ooblen
= use_len
;
173 ops
.ooboffs
= use_offset
;
175 ops
.oobbuf
= readbuf
;
176 err
= mtd_read_oob(mtd
, addr
, &ops
);
177 if (err
|| ops
.oobretlen
!= use_len
) {
178 pr_err("error: readoob failed at %#llx\n",
181 return err
? err
: -1;
183 if (memcmp(readbuf
, writebuf
, use_len
)) {
184 pr_err("error: verify failed at %#llx\n",
188 pr_err("error: too many errors\n");
192 if (use_offset
!= 0 || use_len
< mtd
->ecclayout
->oobavail
) {
195 ops
.mode
= MTD_OPS_AUTO_OOB
;
198 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
202 ops
.oobbuf
= readbuf
;
203 err
= mtd_read_oob(mtd
, addr
, &ops
);
204 if (err
|| ops
.oobretlen
!= mtd
->ecclayout
->oobavail
) {
205 pr_err("error: readoob failed at %#llx\n",
208 return err
? err
: -1;
210 if (memcmp(readbuf
+ use_offset
, writebuf
, use_len
)) {
211 pr_err("error: verify failed at %#llx\n",
215 pr_err("error: too many errors\n");
219 for (k
= 0; k
< use_offset
; ++k
)
220 if (readbuf
[k
] != 0xff) {
221 pr_err("error: verify 0xff "
231 for (k
= use_offset
+ use_len
;
232 k
< mtd
->ecclayout
->oobavail
; ++k
)
233 if (readbuf
[k
] != 0xff) {
234 pr_err("error: verify 0xff "
251 static int verify_eraseblock_in_one_go(int ebnum
)
253 struct mtd_oob_ops ops
;
255 loff_t addr
= ebnum
* mtd
->erasesize
;
256 size_t len
= mtd
->ecclayout
->oobavail
* pgcnt
;
258 prandom_bytes_state(&rnd_state
, writebuf
, len
);
259 ops
.mode
= MTD_OPS_AUTO_OOB
;
266 ops
.oobbuf
= readbuf
;
267 err
= mtd_read_oob(mtd
, addr
, &ops
);
268 if (err
|| ops
.oobretlen
!= len
) {
269 pr_err("error: readoob failed at %#llx\n",
272 return err
? err
: -1;
274 if (memcmp(readbuf
, writebuf
, len
)) {
275 pr_err("error: verify failed at %#llx\n",
279 pr_err("error: too many errors\n");
287 static int verify_all_eraseblocks(void)
292 pr_info("verifying all eraseblocks\n");
293 for (i
= 0; i
< ebcnt
; ++i
) {
296 err
= verify_eraseblock(i
);
300 pr_info("verified up to eraseblock %u\n", i
);
303 pr_info("verified %u eraseblocks\n", i
);
307 static int is_block_bad(int ebnum
)
310 loff_t addr
= ebnum
* mtd
->erasesize
;
312 ret
= mtd_block_isbad(mtd
, addr
);
314 pr_info("block %d is bad\n", ebnum
);
318 static int scan_for_bad_eraseblocks(void)
322 bbt
= kmalloc(ebcnt
, GFP_KERNEL
);
324 pr_err("error: cannot allocate memory\n");
328 pr_info("scanning for bad eraseblocks\n");
329 for (i
= 0; i
< ebcnt
; ++i
) {
330 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
335 pr_info("scanned %d eraseblocks, %d are bad\n", i
, bad
);
339 static int __init
mtd_oobtest_init(void)
344 struct mtd_oob_ops ops
;
345 loff_t addr
= 0, addr0
;
347 printk(KERN_INFO
"\n");
348 printk(KERN_INFO
"=================================================\n");
351 pr_info("Please specify a valid mtd-device via module parameter\n");
352 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
356 pr_info("MTD device: %d\n", dev
);
358 mtd
= get_mtd_device(NULL
, dev
);
361 pr_err("error: cannot get MTD device\n");
365 if (mtd
->type
!= MTD_NANDFLASH
) {
366 pr_info("this test requires NAND flash\n");
371 do_div(tmp
, mtd
->erasesize
);
373 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
375 pr_info("MTD device size %llu, eraseblock size %u, "
376 "page size %u, count of eraseblocks %u, pages per "
377 "eraseblock %u, OOB size %u\n",
378 (unsigned long long)mtd
->size
, mtd
->erasesize
,
379 mtd
->writesize
, ebcnt
, pgcnt
, mtd
->oobsize
);
382 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
384 pr_err("error: cannot allocate memory\n");
387 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
389 pr_err("error: cannot allocate memory\n");
393 err
= scan_for_bad_eraseblocks();
398 use_len
= mtd
->ecclayout
->oobavail
;
399 use_len_max
= mtd
->ecclayout
->oobavail
;
402 /* First test: write all OOB, read it back and verify */
403 pr_info("test 1 of 5\n");
405 err
= erase_whole_device();
409 prandom_seed_state(&rnd_state
, 1);
410 err
= write_whole_device();
414 prandom_seed_state(&rnd_state
, 1);
415 err
= verify_all_eraseblocks();
420 * Second test: write all OOB, a block at a time, read it back and
423 pr_info("test 2 of 5\n");
425 err
= erase_whole_device();
429 prandom_seed_state(&rnd_state
, 3);
430 err
= write_whole_device();
434 /* Check all eraseblocks */
435 prandom_seed_state(&rnd_state
, 3);
436 pr_info("verifying all eraseblocks\n");
437 for (i
= 0; i
< ebcnt
; ++i
) {
440 err
= verify_eraseblock_in_one_go(i
);
444 pr_info("verified up to eraseblock %u\n", i
);
447 pr_info("verified %u eraseblocks\n", i
);
450 * Third test: write OOB at varying offsets and lengths, read it back
453 pr_info("test 3 of 5\n");
455 err
= erase_whole_device();
459 /* Write all eraseblocks */
461 use_len
= mtd
->ecclayout
->oobavail
;
462 use_len_max
= mtd
->ecclayout
->oobavail
;
464 prandom_seed_state(&rnd_state
, 5);
466 err
= write_whole_device();
470 /* Check all eraseblocks */
472 use_len
= mtd
->ecclayout
->oobavail
;
473 use_len_max
= mtd
->ecclayout
->oobavail
;
475 prandom_seed_state(&rnd_state
, 5);
476 err
= verify_all_eraseblocks();
481 use_len
= mtd
->ecclayout
->oobavail
;
482 use_len_max
= mtd
->ecclayout
->oobavail
;
485 /* Fourth test: try to write off end of device */
486 pr_info("test 4 of 5\n");
488 err
= erase_whole_device();
493 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
494 addr0
+= mtd
->erasesize
;
496 /* Attempt to write off end of OOB */
497 ops
.mode
= MTD_OPS_AUTO_OOB
;
502 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
504 ops
.oobbuf
= writebuf
;
505 pr_info("attempting to start write past end of OOB\n");
506 pr_info("an error is expected...\n");
507 err
= mtd_write_oob(mtd
, addr0
, &ops
);
509 pr_info("error occurred as expected\n");
512 pr_err("error: can write past end of OOB\n");
516 /* Attempt to read off end of OOB */
517 ops
.mode
= MTD_OPS_AUTO_OOB
;
522 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
524 ops
.oobbuf
= readbuf
;
525 pr_info("attempting to start read past end of OOB\n");
526 pr_info("an error is expected...\n");
527 err
= mtd_read_oob(mtd
, addr0
, &ops
);
529 pr_info("error occurred as expected\n");
532 pr_err("error: can read past end of OOB\n");
537 pr_info("skipping end of device tests because last "
540 /* Attempt to write off end of device */
541 ops
.mode
= MTD_OPS_AUTO_OOB
;
544 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
548 ops
.oobbuf
= writebuf
;
549 pr_info("attempting to write past end of device\n");
550 pr_info("an error is expected...\n");
551 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
553 pr_info("error occurred as expected\n");
556 pr_err("error: wrote past end of device\n");
560 /* Attempt to read off end of device */
561 ops
.mode
= MTD_OPS_AUTO_OOB
;
564 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
568 ops
.oobbuf
= readbuf
;
569 pr_info("attempting to read past end of device\n");
570 pr_info("an error is expected...\n");
571 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
573 pr_info("error occurred as expected\n");
576 pr_err("error: read past end of device\n");
580 err
= erase_eraseblock(ebcnt
- 1);
584 /* Attempt to write off end of device */
585 ops
.mode
= MTD_OPS_AUTO_OOB
;
588 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
592 ops
.oobbuf
= writebuf
;
593 pr_info("attempting to write past end of device\n");
594 pr_info("an error is expected...\n");
595 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
597 pr_info("error occurred as expected\n");
600 pr_err("error: wrote past end of device\n");
604 /* Attempt to read off end of device */
605 ops
.mode
= MTD_OPS_AUTO_OOB
;
608 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
612 ops
.oobbuf
= readbuf
;
613 pr_info("attempting to read past end of device\n");
614 pr_info("an error is expected...\n");
615 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
617 pr_info("error occurred as expected\n");
620 pr_err("error: read past end of device\n");
625 /* Fifth test: write / read across block boundaries */
626 pr_info("test 5 of 5\n");
628 /* Erase all eraseblocks */
629 err
= erase_whole_device();
633 /* Write all eraseblocks */
634 prandom_seed_state(&rnd_state
, 11);
635 pr_info("writing OOBs of whole device\n");
636 for (i
= 0; i
< ebcnt
- 1; ++i
) {
639 size_t sz
= mtd
->ecclayout
->oobavail
;
640 if (bbt
[i
] || bbt
[i
+ 1])
642 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
643 for (pg
= 0; pg
< cnt
; ++pg
) {
644 prandom_bytes_state(&rnd_state
, writebuf
, sz
);
645 ops
.mode
= MTD_OPS_AUTO_OOB
;
652 ops
.oobbuf
= writebuf
;
653 err
= mtd_write_oob(mtd
, addr
, &ops
);
657 pr_info("written up to eraseblock %u\n", i
);
659 addr
+= mtd
->writesize
;
662 pr_info("written %u eraseblocks\n", i
);
664 /* Check all eraseblocks */
665 prandom_seed_state(&rnd_state
, 11);
666 pr_info("verifying all eraseblocks\n");
667 for (i
= 0; i
< ebcnt
- 1; ++i
) {
668 if (bbt
[i
] || bbt
[i
+ 1])
670 prandom_bytes_state(&rnd_state
, writebuf
,
671 mtd
->ecclayout
->oobavail
* 2);
672 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
673 ops
.mode
= MTD_OPS_AUTO_OOB
;
676 ops
.ooblen
= mtd
->ecclayout
->oobavail
* 2;
680 ops
.oobbuf
= readbuf
;
681 err
= mtd_read_oob(mtd
, addr
, &ops
);
684 if (memcmp(readbuf
, writebuf
, mtd
->ecclayout
->oobavail
* 2)) {
685 pr_err("error: verify failed at %#llx\n",
689 pr_err("error: too many errors\n");
694 pr_info("verified up to eraseblock %u\n", i
);
697 pr_info("verified %u eraseblocks\n", i
);
699 pr_info("finished with %d errors\n", errcnt
);
706 pr_info("error %d occurred\n", err
);
707 printk(KERN_INFO
"=================================================\n");
710 module_init(mtd_oobtest_init
);
712 static void __exit
mtd_oobtest_exit(void)
716 module_exit(mtd_oobtest_exit
);
718 MODULE_DESCRIPTION("Out-of-band test module");
719 MODULE_AUTHOR("Adrian Hunter");
720 MODULE_LICENSE("GPL");