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 #include <asm/div64.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/err.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/slab.h>
29 #include <linux/sched.h>
31 #define PRINT_PREF KERN_INFO "mtd_oobtest: "
33 static int dev
= -EINVAL
;
34 module_param(dev
, int, S_IRUGO
);
35 MODULE_PARM_DESC(dev
, "MTD device number to use");
37 static struct mtd_info
*mtd
;
38 static unsigned char *readbuf
;
39 static unsigned char *writebuf
;
40 static unsigned char *bbt
;
45 static int use_offset
;
47 static int use_len_max
;
48 static int vary_offset
;
49 static unsigned long next
= 1;
51 static inline unsigned int simple_rand(void)
53 next
= next
* 1103515245 + 12345;
54 return (unsigned int)((next
/ 65536) % 32768);
57 static inline void simple_srand(unsigned long seed
)
62 static void set_random_data(unsigned char *buf
, size_t len
)
66 for (i
= 0; i
< len
; ++i
)
67 buf
[i
] = simple_rand();
70 static int erase_eraseblock(int ebnum
)
74 loff_t addr
= ebnum
* mtd
->erasesize
;
76 memset(&ei
, 0, sizeof(struct erase_info
));
79 ei
.len
= mtd
->erasesize
;
81 err
= mtd_erase(mtd
, &ei
);
83 printk(PRINT_PREF
"error %d while erasing EB %d\n", err
, ebnum
);
87 if (ei
.state
== MTD_ERASE_FAILED
) {
88 printk(PRINT_PREF
"some erase error occurred at EB %d\n",
96 static int erase_whole_device(void)
101 printk(PRINT_PREF
"erasing whole device\n");
102 for (i
= 0; i
< ebcnt
; ++i
) {
105 err
= erase_eraseblock(i
);
110 printk(PRINT_PREF
"erased %u eraseblocks\n", i
);
114 static void do_vary_offset(void)
119 if (use_offset
>= use_len_max
)
121 use_len
= use_len_max
- use_offset
;
125 static int write_eraseblock(int ebnum
)
128 struct mtd_oob_ops ops
;
130 loff_t addr
= ebnum
* mtd
->erasesize
;
132 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
133 set_random_data(writebuf
, use_len
);
134 ops
.mode
= MTD_OPS_AUTO_OOB
;
137 ops
.ooblen
= use_len
;
139 ops
.ooboffs
= use_offset
;
141 ops
.oobbuf
= writebuf
;
142 err
= mtd_write_oob(mtd
, addr
, &ops
);
143 if (err
|| ops
.oobretlen
!= use_len
) {
144 printk(PRINT_PREF
"error: writeoob failed at %#llx\n",
146 printk(PRINT_PREF
"error: use_len %d, use_offset %d\n",
147 use_len
, use_offset
);
149 return err
? err
: -1;
158 static int write_whole_device(void)
163 printk(PRINT_PREF
"writing OOBs of whole device\n");
164 for (i
= 0; i
< ebcnt
; ++i
) {
167 err
= write_eraseblock(i
);
171 printk(PRINT_PREF
"written up to eraseblock %u\n", i
);
174 printk(PRINT_PREF
"written %u eraseblocks\n", i
);
178 static int verify_eraseblock(int ebnum
)
181 struct mtd_oob_ops ops
;
183 loff_t addr
= ebnum
* mtd
->erasesize
;
185 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
186 set_random_data(writebuf
, use_len
);
187 ops
.mode
= MTD_OPS_AUTO_OOB
;
190 ops
.ooblen
= use_len
;
192 ops
.ooboffs
= use_offset
;
194 ops
.oobbuf
= readbuf
;
195 err
= mtd_read_oob(mtd
, addr
, &ops
);
196 if (err
|| ops
.oobretlen
!= use_len
) {
197 printk(PRINT_PREF
"error: readoob failed at %#llx\n",
200 return err
? err
: -1;
202 if (memcmp(readbuf
, writebuf
, use_len
)) {
203 printk(PRINT_PREF
"error: verify failed at %#llx\n",
207 printk(PRINT_PREF
"error: too many errors\n");
211 if (use_offset
!= 0 || use_len
< mtd
->ecclayout
->oobavail
) {
214 ops
.mode
= MTD_OPS_AUTO_OOB
;
217 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
221 ops
.oobbuf
= readbuf
;
222 err
= mtd_read_oob(mtd
, addr
, &ops
);
223 if (err
|| ops
.oobretlen
!= mtd
->ecclayout
->oobavail
) {
224 printk(PRINT_PREF
"error: readoob failed at "
225 "%#llx\n", (long long)addr
);
227 return err
? err
: -1;
229 if (memcmp(readbuf
+ use_offset
, writebuf
, use_len
)) {
230 printk(PRINT_PREF
"error: verify failed at "
231 "%#llx\n", (long long)addr
);
234 printk(PRINT_PREF
"error: too many "
239 for (k
= 0; k
< use_offset
; ++k
)
240 if (readbuf
[k
] != 0xff) {
241 printk(PRINT_PREF
"error: verify 0xff "
246 printk(PRINT_PREF
"error: too "
251 for (k
= use_offset
+ use_len
;
252 k
< mtd
->ecclayout
->oobavail
; ++k
)
253 if (readbuf
[k
] != 0xff) {
254 printk(PRINT_PREF
"error: verify 0xff "
259 printk(PRINT_PREF
"error: too "
271 static int verify_eraseblock_in_one_go(int ebnum
)
273 struct mtd_oob_ops ops
;
275 loff_t addr
= ebnum
* mtd
->erasesize
;
276 size_t len
= mtd
->ecclayout
->oobavail
* pgcnt
;
278 set_random_data(writebuf
, len
);
279 ops
.mode
= MTD_OPS_AUTO_OOB
;
286 ops
.oobbuf
= readbuf
;
287 err
= mtd_read_oob(mtd
, addr
, &ops
);
288 if (err
|| ops
.oobretlen
!= len
) {
289 printk(PRINT_PREF
"error: readoob failed at %#llx\n",
292 return err
? err
: -1;
294 if (memcmp(readbuf
, writebuf
, len
)) {
295 printk(PRINT_PREF
"error: verify failed at %#llx\n",
299 printk(PRINT_PREF
"error: too many errors\n");
307 static int verify_all_eraseblocks(void)
312 printk(PRINT_PREF
"verifying all eraseblocks\n");
313 for (i
= 0; i
< ebcnt
; ++i
) {
316 err
= verify_eraseblock(i
);
320 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
323 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
327 static int is_block_bad(int ebnum
)
330 loff_t addr
= ebnum
* mtd
->erasesize
;
332 ret
= mtd_block_isbad(mtd
, addr
);
334 printk(PRINT_PREF
"block %d is bad\n", ebnum
);
338 static int scan_for_bad_eraseblocks(void)
342 bbt
= kmalloc(ebcnt
, GFP_KERNEL
);
344 printk(PRINT_PREF
"error: cannot allocate memory\n");
348 printk(PRINT_PREF
"scanning for bad eraseblocks\n");
349 for (i
= 0; i
< ebcnt
; ++i
) {
350 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
355 printk(PRINT_PREF
"scanned %d eraseblocks, %d are bad\n", i
, bad
);
359 static int __init
mtd_oobtest_init(void)
364 struct mtd_oob_ops ops
;
365 loff_t addr
= 0, addr0
;
367 printk(KERN_INFO
"\n");
368 printk(KERN_INFO
"=================================================\n");
371 printk(PRINT_PREF
"Please specify a valid mtd-device via module paramter\n");
372 printk(KERN_CRIT
"CAREFUL: This test wipes all data on the specified MTD device!\n");
376 printk(PRINT_PREF
"MTD device: %d\n", dev
);
378 mtd
= get_mtd_device(NULL
, dev
);
381 printk(PRINT_PREF
"error: cannot get MTD device\n");
385 if (mtd
->type
!= MTD_NANDFLASH
) {
386 printk(PRINT_PREF
"this test requires NAND flash\n");
391 do_div(tmp
, mtd
->erasesize
);
393 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
395 printk(PRINT_PREF
"MTD device size %llu, eraseblock size %u, "
396 "page size %u, count of eraseblocks %u, pages per "
397 "eraseblock %u, OOB size %u\n",
398 (unsigned long long)mtd
->size
, mtd
->erasesize
,
399 mtd
->writesize
, ebcnt
, pgcnt
, mtd
->oobsize
);
402 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
404 printk(PRINT_PREF
"error: cannot allocate memory\n");
407 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
409 printk(PRINT_PREF
"error: cannot allocate memory\n");
413 err
= scan_for_bad_eraseblocks();
418 use_len
= mtd
->ecclayout
->oobavail
;
419 use_len_max
= mtd
->ecclayout
->oobavail
;
422 /* First test: write all OOB, read it back and verify */
423 printk(PRINT_PREF
"test 1 of 5\n");
425 err
= erase_whole_device();
430 err
= write_whole_device();
435 err
= verify_all_eraseblocks();
440 * Second test: write all OOB, a block at a time, read it back and
443 printk(PRINT_PREF
"test 2 of 5\n");
445 err
= erase_whole_device();
450 err
= write_whole_device();
454 /* Check all eraseblocks */
456 printk(PRINT_PREF
"verifying all eraseblocks\n");
457 for (i
= 0; i
< ebcnt
; ++i
) {
460 err
= verify_eraseblock_in_one_go(i
);
464 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
467 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
470 * Third test: write OOB at varying offsets and lengths, read it back
473 printk(PRINT_PREF
"test 3 of 5\n");
475 err
= erase_whole_device();
479 /* Write all eraseblocks */
481 use_len
= mtd
->ecclayout
->oobavail
;
482 use_len_max
= mtd
->ecclayout
->oobavail
;
486 err
= write_whole_device();
490 /* Check all eraseblocks */
492 use_len
= mtd
->ecclayout
->oobavail
;
493 use_len_max
= mtd
->ecclayout
->oobavail
;
496 err
= verify_all_eraseblocks();
501 use_len
= mtd
->ecclayout
->oobavail
;
502 use_len_max
= mtd
->ecclayout
->oobavail
;
505 /* Fourth test: try to write off end of device */
506 printk(PRINT_PREF
"test 4 of 5\n");
508 err
= erase_whole_device();
513 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
514 addr0
+= mtd
->erasesize
;
516 /* Attempt to write off end of OOB */
517 ops
.mode
= MTD_OPS_AUTO_OOB
;
522 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
524 ops
.oobbuf
= writebuf
;
525 printk(PRINT_PREF
"attempting to start write past end of OOB\n");
526 printk(PRINT_PREF
"an error is expected...\n");
527 err
= mtd_write_oob(mtd
, addr0
, &ops
);
529 printk(PRINT_PREF
"error occurred as expected\n");
532 printk(PRINT_PREF
"error: can write past end of OOB\n");
536 /* Attempt to read off end of OOB */
537 ops
.mode
= MTD_OPS_AUTO_OOB
;
542 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
544 ops
.oobbuf
= readbuf
;
545 printk(PRINT_PREF
"attempting to start read past end of OOB\n");
546 printk(PRINT_PREF
"an error is expected...\n");
547 err
= mtd_read_oob(mtd
, addr0
, &ops
);
549 printk(PRINT_PREF
"error occurred as expected\n");
552 printk(PRINT_PREF
"error: can read past end of OOB\n");
557 printk(PRINT_PREF
"skipping end of device tests because last "
560 /* Attempt to write off end of device */
561 ops
.mode
= MTD_OPS_AUTO_OOB
;
564 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
568 ops
.oobbuf
= writebuf
;
569 printk(PRINT_PREF
"attempting to write past end of device\n");
570 printk(PRINT_PREF
"an error is expected...\n");
571 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
573 printk(PRINT_PREF
"error occurred as expected\n");
576 printk(PRINT_PREF
"error: wrote past end of device\n");
580 /* Attempt to read off end of device */
581 ops
.mode
= MTD_OPS_AUTO_OOB
;
584 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
588 ops
.oobbuf
= readbuf
;
589 printk(PRINT_PREF
"attempting to read past end of device\n");
590 printk(PRINT_PREF
"an error is expected...\n");
591 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
593 printk(PRINT_PREF
"error occurred as expected\n");
596 printk(PRINT_PREF
"error: read past end of device\n");
600 err
= erase_eraseblock(ebcnt
- 1);
604 /* Attempt to write off end of device */
605 ops
.mode
= MTD_OPS_AUTO_OOB
;
608 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
612 ops
.oobbuf
= writebuf
;
613 printk(PRINT_PREF
"attempting to write past end of device\n");
614 printk(PRINT_PREF
"an error is expected...\n");
615 err
= mtd_write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
617 printk(PRINT_PREF
"error occurred as expected\n");
620 printk(PRINT_PREF
"error: wrote past end of device\n");
624 /* Attempt to read off end of device */
625 ops
.mode
= MTD_OPS_AUTO_OOB
;
628 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
632 ops
.oobbuf
= readbuf
;
633 printk(PRINT_PREF
"attempting to read past end of device\n");
634 printk(PRINT_PREF
"an error is expected...\n");
635 err
= mtd_read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
637 printk(PRINT_PREF
"error occurred as expected\n");
640 printk(PRINT_PREF
"error: read past end of device\n");
645 /* Fifth test: write / read across block boundaries */
646 printk(PRINT_PREF
"test 5 of 5\n");
648 /* Erase all eraseblocks */
649 err
= erase_whole_device();
653 /* Write all eraseblocks */
655 printk(PRINT_PREF
"writing OOBs of whole device\n");
656 for (i
= 0; i
< ebcnt
- 1; ++i
) {
659 size_t sz
= mtd
->ecclayout
->oobavail
;
660 if (bbt
[i
] || bbt
[i
+ 1])
662 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
663 for (pg
= 0; pg
< cnt
; ++pg
) {
664 set_random_data(writebuf
, sz
);
665 ops
.mode
= MTD_OPS_AUTO_OOB
;
672 ops
.oobbuf
= writebuf
;
673 err
= mtd_write_oob(mtd
, addr
, &ops
);
677 printk(PRINT_PREF
"written up to eraseblock "
680 addr
+= mtd
->writesize
;
683 printk(PRINT_PREF
"written %u eraseblocks\n", i
);
685 /* Check all eraseblocks */
687 printk(PRINT_PREF
"verifying all eraseblocks\n");
688 for (i
= 0; i
< ebcnt
- 1; ++i
) {
689 if (bbt
[i
] || bbt
[i
+ 1])
691 set_random_data(writebuf
, mtd
->ecclayout
->oobavail
* 2);
692 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
693 ops
.mode
= MTD_OPS_AUTO_OOB
;
696 ops
.ooblen
= mtd
->ecclayout
->oobavail
* 2;
700 ops
.oobbuf
= readbuf
;
701 err
= mtd_read_oob(mtd
, addr
, &ops
);
704 if (memcmp(readbuf
, writebuf
, mtd
->ecclayout
->oobavail
* 2)) {
705 printk(PRINT_PREF
"error: verify failed at %#llx\n",
709 printk(PRINT_PREF
"error: too many errors\n");
714 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
717 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
719 printk(PRINT_PREF
"finished with %d errors\n", errcnt
);
726 printk(PRINT_PREF
"error %d occurred\n", err
);
727 printk(KERN_INFO
"=================================================\n");
730 module_init(mtd_oobtest_init
);
732 static void __exit
mtd_oobtest_exit(void)
736 module_exit(mtd_oobtest_exit
);
738 MODULE_DESCRIPTION("Out-of-band test module");
739 MODULE_AUTHOR("Adrian Hunter");
740 MODULE_LICENSE("GPL");