1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2008 Nokia Corporation
5 * Test OOB read and write on MTD device.
7 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <asm/div64.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/err.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/random.h>
24 static int dev
= -EINVAL
;
25 static int bitflip_limit
;
26 module_param(dev
, int, S_IRUGO
);
27 MODULE_PARM_DESC(dev
, "MTD device number to use");
28 module_param(bitflip_limit
, int, S_IRUGO
);
29 MODULE_PARM_DESC(bitflip_limit
, "Max. allowed bitflips per page");
31 static struct mtd_info
*mtd
;
32 static unsigned char *readbuf
;
33 static unsigned char *writebuf
;
34 static unsigned char *bbt
;
39 static int use_offset
;
41 static int use_len_max
;
42 static int vary_offset
;
43 static struct rnd_state rnd_state
;
45 static void do_vary_offset(void)
50 if (use_offset
>= use_len_max
)
52 use_len
= use_len_max
- use_offset
;
56 static int write_eraseblock(int ebnum
)
59 struct mtd_oob_ops ops
;
61 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
63 prandom_bytes_state(&rnd_state
, writebuf
, use_len_max
* pgcnt
);
64 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
65 ops
.mode
= MTD_OPS_AUTO_OOB
;
70 ops
.ooboffs
= use_offset
;
72 ops
.oobbuf
= writebuf
+ (use_len_max
* i
) + use_offset
;
73 err
= mtd_write_oob(mtd
, addr
, &ops
);
74 if (err
|| ops
.oobretlen
!= use_len
) {
75 pr_err("error: writeoob failed at %#llx\n",
77 pr_err("error: use_len %d, use_offset %d\n",
80 return err
? err
: -1;
89 static int write_whole_device(void)
94 pr_info("writing OOBs of whole device\n");
95 for (i
= 0; i
< ebcnt
; ++i
) {
98 err
= write_eraseblock(i
);
102 pr_info("written up to eraseblock %u\n", i
);
104 err
= mtdtest_relax();
108 pr_info("written %u eraseblocks\n", i
);
113 * Display the address, offset and data bytes at comparison failure.
114 * Return number of bitflips encountered.
116 static size_t memcmpshowoffset(loff_t addr
, loff_t offset
, const void *cs
,
117 const void *ct
, size_t count
)
119 const unsigned char *su1
, *su2
;
124 for (su1
= cs
, su2
= ct
; 0 < count
; ++su1
, ++su2
, count
--, i
++) {
127 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0x%x diff 0x%x\n",
128 (unsigned long)addr
, (unsigned long)offset
+ i
,
130 bitflips
+= hweight8(res
);
137 #define memcmpshow(addr, cs, ct, count) memcmpshowoffset((addr), 0, (cs), (ct),\
141 * Compare with 0xff and show the address, offset and data bytes at
142 * comparison failure. Return number of bitflips encountered.
144 static size_t memffshow(loff_t addr
, loff_t offset
, const void *cs
,
147 const unsigned char *su1
;
152 for (su1
= cs
; 0 < count
; ++su1
, count
--, i
++) {
155 pr_info("error @addr[0x%lx:0x%lx] 0x%x -> 0xff diff 0x%x\n",
156 (unsigned long)addr
, (unsigned long)offset
+ i
,
158 bitflips
+= hweight8(res
);
165 static int verify_eraseblock(int ebnum
)
168 struct mtd_oob_ops ops
;
170 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
173 prandom_bytes_state(&rnd_state
, writebuf
, use_len_max
* pgcnt
);
174 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
175 ops
.mode
= MTD_OPS_AUTO_OOB
;
178 ops
.ooblen
= use_len
;
180 ops
.ooboffs
= use_offset
;
182 ops
.oobbuf
= readbuf
;
183 err
= mtd_read_oob(mtd
, addr
, &ops
);
184 if (mtd_is_bitflip(err
))
187 if (err
|| ops
.oobretlen
!= use_len
) {
188 pr_err("error: readoob failed at %#llx\n",
191 return err
? err
: -1;
194 bitflips
= memcmpshow(addr
, readbuf
,
195 writebuf
+ (use_len_max
* i
) + use_offset
,
197 if (bitflips
> bitflip_limit
) {
198 pr_err("error: verify failed at %#llx\n",
202 pr_err("error: too many errors\n");
205 } else if (bitflips
) {
206 pr_info("ignoring error as within bitflip_limit\n");
209 if (use_offset
!= 0 || use_len
< mtd
->oobavail
) {
212 ops
.mode
= MTD_OPS_AUTO_OOB
;
215 ops
.ooblen
= mtd
->oobavail
;
219 ops
.oobbuf
= readbuf
;
220 err
= mtd_read_oob(mtd
, addr
, &ops
);
221 if (mtd_is_bitflip(err
))
224 if (err
|| ops
.oobretlen
!= mtd
->oobavail
) {
225 pr_err("error: readoob failed at %#llx\n",
228 return err
? err
: -1;
230 bitflips
= memcmpshowoffset(addr
, use_offset
,
231 readbuf
+ use_offset
,
232 writebuf
+ (use_len_max
* i
) + use_offset
,
235 /* verify pre-offset area for 0xff */
236 bitflips
+= memffshow(addr
, 0, readbuf
, use_offset
);
238 /* verify post-(use_offset + use_len) area for 0xff */
239 k
= use_offset
+ use_len
;
240 bitflips
+= memffshow(addr
, k
, readbuf
+ k
,
243 if (bitflips
> bitflip_limit
) {
244 pr_err("error: verify failed at %#llx\n",
248 pr_err("error: too many errors\n");
251 } else if (bitflips
) {
252 pr_info("ignoring errors as within bitflip limit\n");
261 static int verify_eraseblock_in_one_go(int ebnum
)
263 struct mtd_oob_ops ops
;
265 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
266 size_t len
= mtd
->oobavail
* pgcnt
;
267 size_t oobavail
= mtd
->oobavail
;
271 prandom_bytes_state(&rnd_state
, writebuf
, len
);
272 ops
.mode
= MTD_OPS_AUTO_OOB
;
279 ops
.oobbuf
= readbuf
;
281 /* read entire block's OOB at one go */
282 err
= mtd_read_oob(mtd
, addr
, &ops
);
283 if (mtd_is_bitflip(err
))
286 if (err
|| ops
.oobretlen
!= len
) {
287 pr_err("error: readoob failed at %#llx\n",
290 return err
? err
: -1;
293 /* verify one page OOB at a time for bitflip per page limit check */
294 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
295 bitflips
= memcmpshow(addr
, readbuf
+ (i
* oobavail
),
296 writebuf
+ (i
* oobavail
), oobavail
);
297 if (bitflips
> bitflip_limit
) {
298 pr_err("error: verify failed at %#llx\n",
302 pr_err("error: too many errors\n");
305 } else if (bitflips
) {
306 pr_info("ignoring error as within bitflip_limit\n");
313 static int verify_all_eraseblocks(void)
318 pr_info("verifying all eraseblocks\n");
319 for (i
= 0; i
< ebcnt
; ++i
) {
322 err
= verify_eraseblock(i
);
326 pr_info("verified up to eraseblock %u\n", i
);
328 err
= mtdtest_relax();
332 pr_info("verified %u eraseblocks\n", i
);
336 static int __init
mtd_oobtest_init(void)
341 struct mtd_oob_ops ops
;
342 loff_t addr
= 0, addr0
;
344 printk(KERN_INFO
"\n");
345 printk(KERN_INFO
"=================================================\n");
348 pr_info("Please specify a valid mtd-device via module parameter\n");
349 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
353 pr_info("MTD device: %d\n", dev
);
355 mtd
= get_mtd_device(NULL
, dev
);
358 pr_err("error: cannot get MTD device\n");
362 if (!mtd_type_is_nand(mtd
)) {
363 pr_info("this test requires NAND flash\n");
368 do_div(tmp
, mtd
->erasesize
);
370 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
372 pr_info("MTD device size %llu, eraseblock size %u, "
373 "page size %u, count of eraseblocks %u, pages per "
374 "eraseblock %u, OOB size %u\n",
375 (unsigned long long)mtd
->size
, mtd
->erasesize
,
376 mtd
->writesize
, ebcnt
, pgcnt
, mtd
->oobsize
);
379 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
382 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
385 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
389 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
394 use_len
= mtd
->oobavail
;
395 use_len_max
= mtd
->oobavail
;
398 /* First test: write all OOB, read it back and verify */
399 pr_info("test 1 of 5\n");
401 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
405 prandom_seed_state(&rnd_state
, 1);
406 err
= write_whole_device();
410 prandom_seed_state(&rnd_state
, 1);
411 err
= verify_all_eraseblocks();
416 * Second test: write all OOB, a block at a time, read it back and
419 pr_info("test 2 of 5\n");
421 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
425 prandom_seed_state(&rnd_state
, 3);
426 err
= write_whole_device();
430 /* Check all eraseblocks */
431 prandom_seed_state(&rnd_state
, 3);
432 pr_info("verifying all eraseblocks\n");
433 for (i
= 0; i
< ebcnt
; ++i
) {
436 err
= verify_eraseblock_in_one_go(i
);
440 pr_info("verified up to eraseblock %u\n", i
);
442 err
= mtdtest_relax();
446 pr_info("verified %u eraseblocks\n", i
);
449 * Third test: write OOB at varying offsets and lengths, read it back
452 pr_info("test 3 of 5\n");
454 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
458 /* Write all eraseblocks */
460 use_len
= mtd
->oobavail
;
461 use_len_max
= mtd
->oobavail
;
463 prandom_seed_state(&rnd_state
, 5);
465 err
= write_whole_device();
469 /* Check all eraseblocks */
471 use_len
= mtd
->oobavail
;
472 use_len_max
= mtd
->oobavail
;
474 prandom_seed_state(&rnd_state
, 5);
475 err
= verify_all_eraseblocks();
480 use_len
= mtd
->oobavail
;
481 use_len_max
= mtd
->oobavail
;
484 /* Fourth test: try to write off end of device */
485 pr_info("test 4 of 5\n");
487 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
492 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
493 addr0
+= mtd
->erasesize
;
495 /* Attempt to write off end of OOB */
496 ops
.mode
= MTD_OPS_AUTO_OOB
;
501 ops
.ooboffs
= mtd
->oobavail
;
503 ops
.oobbuf
= writebuf
;
504 pr_info("attempting to start write past end of OOB\n");
505 pr_info("an error is expected...\n");
506 err
= mtd_write_oob(mtd
, addr0
, &ops
);
508 pr_info("error occurred as expected\n");
511 pr_err("error: can write past end of OOB\n");
515 /* Attempt to read off end of OOB */
516 ops
.mode
= MTD_OPS_AUTO_OOB
;
521 ops
.ooboffs
= mtd
->oobavail
;
523 ops
.oobbuf
= readbuf
;
524 pr_info("attempting to start read past end of OOB\n");
525 pr_info("an error is expected...\n");
526 err
= mtd_read_oob(mtd
, addr0
, &ops
);
527 if (mtd_is_bitflip(err
))
531 pr_info("error occurred as expected\n");
534 pr_err("error: can read past end of OOB\n");
539 pr_info("skipping end of device tests because last "
542 /* Attempt to write off end of device */
543 ops
.mode
= MTD_OPS_AUTO_OOB
;
546 ops
.ooblen
= mtd
->oobavail
+ 1;
550 ops
.oobbuf
= writebuf
;
551 pr_info("attempting to write past end of device\n");
552 pr_info("an error is expected...\n");
553 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
555 pr_info("error occurred as expected\n");
558 pr_err("error: wrote past end of device\n");
562 /* Attempt to read off end of device */
563 ops
.mode
= MTD_OPS_AUTO_OOB
;
566 ops
.ooblen
= mtd
->oobavail
+ 1;
570 ops
.oobbuf
= readbuf
;
571 pr_info("attempting to read past end of device\n");
572 pr_info("an error is expected...\n");
573 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
574 if (mtd_is_bitflip(err
))
578 pr_info("error occurred as expected\n");
581 pr_err("error: read past end of device\n");
585 err
= mtdtest_erase_eraseblock(mtd
, ebcnt
- 1);
589 /* Attempt to write off end of device */
590 ops
.mode
= MTD_OPS_AUTO_OOB
;
593 ops
.ooblen
= mtd
->oobavail
;
597 ops
.oobbuf
= writebuf
;
598 pr_info("attempting to write past end of device\n");
599 pr_info("an error is expected...\n");
600 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
602 pr_info("error occurred as expected\n");
605 pr_err("error: wrote past end of device\n");
609 /* Attempt to read off end of device */
610 ops
.mode
= MTD_OPS_AUTO_OOB
;
613 ops
.ooblen
= mtd
->oobavail
;
617 ops
.oobbuf
= readbuf
;
618 pr_info("attempting to read past end of device\n");
619 pr_info("an error is expected...\n");
620 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
621 if (mtd_is_bitflip(err
))
625 pr_info("error occurred as expected\n");
628 pr_err("error: read past end of device\n");
633 /* Fifth test: write / read across block boundaries */
634 pr_info("test 5 of 5\n");
636 /* Erase all eraseblocks */
637 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
641 /* Write all eraseblocks */
642 prandom_seed_state(&rnd_state
, 11);
643 pr_info("writing OOBs of whole device\n");
644 for (i
= 0; i
< ebcnt
- 1; ++i
) {
647 size_t sz
= mtd
->oobavail
;
648 if (bbt
[i
] || bbt
[i
+ 1])
650 addr
= (loff_t
)(i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
651 prandom_bytes_state(&rnd_state
, writebuf
, sz
* cnt
);
652 for (pg
= 0; pg
< cnt
; ++pg
) {
653 ops
.mode
= MTD_OPS_AUTO_OOB
;
660 ops
.oobbuf
= writebuf
+ pg
* sz
;
661 err
= mtd_write_oob(mtd
, addr
, &ops
);
665 pr_info("written up to eraseblock %u\n", i
);
667 err
= mtdtest_relax();
671 addr
+= mtd
->writesize
;
674 pr_info("written %u eraseblocks\n", i
);
676 /* Check all eraseblocks */
677 prandom_seed_state(&rnd_state
, 11);
678 pr_info("verifying all eraseblocks\n");
679 for (i
= 0; i
< ebcnt
- 1; ++i
) {
680 if (bbt
[i
] || bbt
[i
+ 1])
682 prandom_bytes_state(&rnd_state
, writebuf
, mtd
->oobavail
* 2);
683 addr
= (loff_t
)(i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
684 ops
.mode
= MTD_OPS_AUTO_OOB
;
687 ops
.ooblen
= mtd
->oobavail
* 2;
691 ops
.oobbuf
= readbuf
;
692 err
= mtd_read_oob(mtd
, addr
, &ops
);
693 if (mtd_is_bitflip(err
))
698 if (memcmpshow(addr
, readbuf
, writebuf
,
699 mtd
->oobavail
* 2)) {
700 pr_err("error: verify failed at %#llx\n",
704 pr_err("error: too many errors\n");
709 pr_info("verified up to eraseblock %u\n", i
);
711 err
= mtdtest_relax();
715 pr_info("verified %u eraseblocks\n", i
);
717 pr_info("finished with %d errors\n", errcnt
);
724 pr_info("error %d occurred\n", err
);
725 printk(KERN_INFO
"=================================================\n");
728 module_init(mtd_oobtest_init
);
730 static void __exit
mtd_oobtest_exit(void)
734 module_exit(mtd_oobtest_exit
);
736 MODULE_DESCRIPTION("Out-of-band test module");
737 MODULE_AUTHOR("Adrian Hunter");
738 MODULE_LICENSE("GPL");