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: "
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_OOB_AUTO
;
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_OOB_AUTO
;
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_OOB_AUTO
;
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_OOB_AUTO
;
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");
369 printk(PRINT_PREF
"MTD device: %d\n", dev
);
371 mtd
= get_mtd_device(NULL
, dev
);
374 printk(PRINT_PREF
"error: cannot get MTD device\n");
378 if (mtd
->type
!= MTD_NANDFLASH
) {
379 printk(PRINT_PREF
"this test requires NAND flash\n");
384 do_div(tmp
, mtd
->erasesize
);
386 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
388 printk(PRINT_PREF
"MTD device size %llu, eraseblock size %u, "
389 "page size %u, count of eraseblocks %u, pages per "
390 "eraseblock %u, OOB size %u\n",
391 (unsigned long long)mtd
->size
, mtd
->erasesize
,
392 mtd
->writesize
, ebcnt
, pgcnt
, mtd
->oobsize
);
395 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
397 printk(PRINT_PREF
"error: cannot allocate memory\n");
400 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
402 printk(PRINT_PREF
"error: cannot allocate memory\n");
406 err
= scan_for_bad_eraseblocks();
411 use_len
= mtd
->ecclayout
->oobavail
;
412 use_len_max
= mtd
->ecclayout
->oobavail
;
415 /* First test: write all OOB, read it back and verify */
416 printk(PRINT_PREF
"test 1 of 5\n");
418 err
= erase_whole_device();
423 err
= write_whole_device();
428 err
= verify_all_eraseblocks();
433 * Second test: write all OOB, a block at a time, read it back and
436 printk(PRINT_PREF
"test 2 of 5\n");
438 err
= erase_whole_device();
443 err
= write_whole_device();
447 /* Check all eraseblocks */
449 printk(PRINT_PREF
"verifying all eraseblocks\n");
450 for (i
= 0; i
< ebcnt
; ++i
) {
453 err
= verify_eraseblock_in_one_go(i
);
457 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
460 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
463 * Third test: write OOB at varying offsets and lengths, read it back
466 printk(PRINT_PREF
"test 3 of 5\n");
468 err
= erase_whole_device();
472 /* Write all eraseblocks */
474 use_len
= mtd
->ecclayout
->oobavail
;
475 use_len_max
= mtd
->ecclayout
->oobavail
;
479 err
= write_whole_device();
483 /* Check all eraseblocks */
485 use_len
= mtd
->ecclayout
->oobavail
;
486 use_len_max
= mtd
->ecclayout
->oobavail
;
489 err
= verify_all_eraseblocks();
494 use_len
= mtd
->ecclayout
->oobavail
;
495 use_len_max
= mtd
->ecclayout
->oobavail
;
498 /* Fourth test: try to write off end of device */
499 printk(PRINT_PREF
"test 4 of 5\n");
501 err
= erase_whole_device();
506 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
507 addr0
+= mtd
->erasesize
;
509 /* Attempt to write off end of OOB */
510 ops
.mode
= MTD_OOB_AUTO
;
515 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
517 ops
.oobbuf
= writebuf
;
518 printk(PRINT_PREF
"attempting to start write past end of OOB\n");
519 printk(PRINT_PREF
"an error is expected...\n");
520 err
= mtd
->write_oob(mtd
, addr0
, &ops
);
522 printk(PRINT_PREF
"error occurred as expected\n");
525 printk(PRINT_PREF
"error: can write past end of OOB\n");
529 /* Attempt to read off end of OOB */
530 ops
.mode
= MTD_OOB_AUTO
;
535 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
537 ops
.oobbuf
= readbuf
;
538 printk(PRINT_PREF
"attempting to start read past end of OOB\n");
539 printk(PRINT_PREF
"an error is expected...\n");
540 err
= mtd
->read_oob(mtd
, addr0
, &ops
);
542 printk(PRINT_PREF
"error occurred as expected\n");
545 printk(PRINT_PREF
"error: can read past end of OOB\n");
550 printk(PRINT_PREF
"skipping end of device tests because last "
553 /* Attempt to write off end of device */
554 ops
.mode
= MTD_OOB_AUTO
;
557 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
561 ops
.oobbuf
= writebuf
;
562 printk(PRINT_PREF
"attempting to write past end of device\n");
563 printk(PRINT_PREF
"an error is expected...\n");
564 err
= mtd
->write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
566 printk(PRINT_PREF
"error occurred as expected\n");
569 printk(PRINT_PREF
"error: wrote past end of device\n");
573 /* Attempt to read off end of device */
574 ops
.mode
= MTD_OOB_AUTO
;
577 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
581 ops
.oobbuf
= readbuf
;
582 printk(PRINT_PREF
"attempting to read past end of device\n");
583 printk(PRINT_PREF
"an error is expected...\n");
584 err
= mtd
->read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
586 printk(PRINT_PREF
"error occurred as expected\n");
589 printk(PRINT_PREF
"error: read past end of device\n");
593 err
= erase_eraseblock(ebcnt
- 1);
597 /* Attempt to write off end of device */
598 ops
.mode
= MTD_OOB_AUTO
;
601 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
605 ops
.oobbuf
= writebuf
;
606 printk(PRINT_PREF
"attempting to write past end of device\n");
607 printk(PRINT_PREF
"an error is expected...\n");
608 err
= mtd
->write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
610 printk(PRINT_PREF
"error occurred as expected\n");
613 printk(PRINT_PREF
"error: wrote past end of device\n");
617 /* Attempt to read off end of device */
618 ops
.mode
= MTD_OOB_AUTO
;
621 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
625 ops
.oobbuf
= readbuf
;
626 printk(PRINT_PREF
"attempting to read past end of device\n");
627 printk(PRINT_PREF
"an error is expected...\n");
628 err
= mtd
->read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
630 printk(PRINT_PREF
"error occurred as expected\n");
633 printk(PRINT_PREF
"error: read past end of device\n");
638 /* Fifth test: write / read across block boundaries */
639 printk(PRINT_PREF
"test 5 of 5\n");
641 /* Erase all eraseblocks */
642 err
= erase_whole_device();
646 /* Write all eraseblocks */
648 printk(PRINT_PREF
"writing OOBs of whole device\n");
649 for (i
= 0; i
< ebcnt
- 1; ++i
) {
652 size_t sz
= mtd
->ecclayout
->oobavail
;
653 if (bbt
[i
] || bbt
[i
+ 1])
655 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
656 for (pg
= 0; pg
< cnt
; ++pg
) {
657 set_random_data(writebuf
, sz
);
658 ops
.mode
= MTD_OOB_AUTO
;
665 ops
.oobbuf
= writebuf
;
666 err
= mtd
->write_oob(mtd
, addr
, &ops
);
670 printk(PRINT_PREF
"written up to eraseblock "
673 addr
+= mtd
->writesize
;
676 printk(PRINT_PREF
"written %u eraseblocks\n", i
);
678 /* Check all eraseblocks */
680 printk(PRINT_PREF
"verifying all eraseblocks\n");
681 for (i
= 0; i
< ebcnt
- 1; ++i
) {
682 if (bbt
[i
] || bbt
[i
+ 1])
684 set_random_data(writebuf
, mtd
->ecclayout
->oobavail
* 2);
685 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
686 ops
.mode
= MTD_OOB_AUTO
;
689 ops
.ooblen
= mtd
->ecclayout
->oobavail
* 2;
693 ops
.oobbuf
= readbuf
;
694 err
= mtd
->read_oob(mtd
, addr
, &ops
);
697 if (memcmp(readbuf
, writebuf
, mtd
->ecclayout
->oobavail
* 2)) {
698 printk(PRINT_PREF
"error: verify failed at %#llx\n",
702 printk(PRINT_PREF
"error: too many errors\n");
707 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
710 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
712 printk(PRINT_PREF
"finished with %d errors\n", errcnt
);
719 printk(PRINT_PREF
"error %d occurred\n", err
);
720 printk(KERN_INFO
"=================================================\n");
723 module_init(mtd_oobtest_init
);
725 static void __exit
mtd_oobtest_exit(void)
729 module_exit(mtd_oobtest_exit
);
731 MODULE_DESCRIPTION("Out-of-band test module");
732 MODULE_AUTHOR("Adrian Hunter");
733 MODULE_LICENSE("GPL");