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/sched.h>
30 #define PRINT_PREF KERN_INFO "mtd_oobtest: "
33 module_param(dev
, int, S_IRUGO
);
34 MODULE_PARM_DESC(dev
, "MTD device number to use");
36 static struct mtd_info
*mtd
;
37 static unsigned char *readbuf
;
38 static unsigned char *writebuf
;
39 static unsigned char *bbt
;
44 static int use_offset
;
46 static int use_len_max
;
47 static int vary_offset
;
48 static unsigned long next
= 1;
50 static inline unsigned int simple_rand(void)
52 next
= next
* 1103515245 + 12345;
53 return (unsigned int)((next
/ 65536) % 32768);
56 static inline void simple_srand(unsigned long seed
)
61 static void set_random_data(unsigned char *buf
, size_t len
)
65 for (i
= 0; i
< len
; ++i
)
66 buf
[i
] = simple_rand();
69 static int erase_eraseblock(int ebnum
)
73 loff_t addr
= ebnum
* mtd
->erasesize
;
75 memset(&ei
, 0, sizeof(struct erase_info
));
78 ei
.len
= mtd
->erasesize
;
80 err
= mtd
->erase(mtd
, &ei
);
82 printk(PRINT_PREF
"error %d while erasing EB %d\n", err
, ebnum
);
86 if (ei
.state
== MTD_ERASE_FAILED
) {
87 printk(PRINT_PREF
"some erase error occurred at EB %d\n",
95 static int erase_whole_device(void)
100 printk(PRINT_PREF
"erasing whole device\n");
101 for (i
= 0; i
< ebcnt
; ++i
) {
104 err
= erase_eraseblock(i
);
109 printk(PRINT_PREF
"erased %u eraseblocks\n", i
);
113 static void do_vary_offset(void)
118 if (use_offset
>= use_len_max
)
120 use_len
= use_len_max
- use_offset
;
124 static int write_eraseblock(int ebnum
)
127 struct mtd_oob_ops ops
;
129 loff_t addr
= ebnum
* mtd
->erasesize
;
131 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
132 set_random_data(writebuf
, use_len
);
133 ops
.mode
= MTD_OOB_AUTO
;
136 ops
.ooblen
= use_len
;
138 ops
.ooboffs
= use_offset
;
140 ops
.oobbuf
= writebuf
;
141 err
= mtd
->write_oob(mtd
, addr
, &ops
);
142 if (err
|| ops
.oobretlen
!= use_len
) {
143 printk(PRINT_PREF
"error: writeoob failed at %#llx\n",
145 printk(PRINT_PREF
"error: use_len %d, use_offset %d\n",
146 use_len
, use_offset
);
148 return err
? err
: -1;
157 static int write_whole_device(void)
162 printk(PRINT_PREF
"writing OOBs of whole device\n");
163 for (i
= 0; i
< ebcnt
; ++i
) {
166 err
= write_eraseblock(i
);
170 printk(PRINT_PREF
"written up to eraseblock %u\n", i
);
173 printk(PRINT_PREF
"written %u eraseblocks\n", i
);
177 static int verify_eraseblock(int ebnum
)
180 struct mtd_oob_ops ops
;
182 loff_t addr
= ebnum
* mtd
->erasesize
;
184 for (i
= 0; i
< pgcnt
; ++i
, addr
+= mtd
->writesize
) {
185 set_random_data(writebuf
, use_len
);
186 ops
.mode
= MTD_OOB_AUTO
;
189 ops
.ooblen
= use_len
;
191 ops
.ooboffs
= use_offset
;
193 ops
.oobbuf
= readbuf
;
194 err
= mtd
->read_oob(mtd
, addr
, &ops
);
195 if (err
|| ops
.oobretlen
!= use_len
) {
196 printk(PRINT_PREF
"error: readoob failed at %#llx\n",
199 return err
? err
: -1;
201 if (memcmp(readbuf
, writebuf
, use_len
)) {
202 printk(PRINT_PREF
"error: verify failed at %#llx\n",
206 printk(PRINT_PREF
"error: too many errors\n");
210 if (use_offset
!= 0 || use_len
< mtd
->ecclayout
->oobavail
) {
213 ops
.mode
= MTD_OOB_AUTO
;
216 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
220 ops
.oobbuf
= readbuf
;
221 err
= mtd
->read_oob(mtd
, addr
, &ops
);
222 if (err
|| ops
.oobretlen
!= mtd
->ecclayout
->oobavail
) {
223 printk(PRINT_PREF
"error: readoob failed at "
224 "%#llx\n", (long long)addr
);
226 return err
? err
: -1;
228 if (memcmp(readbuf
+ use_offset
, writebuf
, use_len
)) {
229 printk(PRINT_PREF
"error: verify failed at "
230 "%#llx\n", (long long)addr
);
233 printk(PRINT_PREF
"error: too many "
238 for (k
= 0; k
< use_offset
; ++k
)
239 if (readbuf
[k
] != 0xff) {
240 printk(PRINT_PREF
"error: verify 0xff "
245 printk(PRINT_PREF
"error: too "
250 for (k
= use_offset
+ use_len
;
251 k
< mtd
->ecclayout
->oobavail
; ++k
)
252 if (readbuf
[k
] != 0xff) {
253 printk(PRINT_PREF
"error: verify 0xff "
258 printk(PRINT_PREF
"error: too "
270 static int verify_eraseblock_in_one_go(int ebnum
)
272 struct mtd_oob_ops ops
;
274 loff_t addr
= ebnum
* mtd
->erasesize
;
275 size_t len
= mtd
->ecclayout
->oobavail
* pgcnt
;
277 set_random_data(writebuf
, len
);
278 ops
.mode
= MTD_OOB_AUTO
;
285 ops
.oobbuf
= readbuf
;
286 err
= mtd
->read_oob(mtd
, addr
, &ops
);
287 if (err
|| ops
.oobretlen
!= len
) {
288 printk(PRINT_PREF
"error: readoob failed at %#llx\n",
291 return err
? err
: -1;
293 if (memcmp(readbuf
, writebuf
, len
)) {
294 printk(PRINT_PREF
"error: verify failed at %#llx\n",
298 printk(PRINT_PREF
"error: too many errors\n");
306 static int verify_all_eraseblocks(void)
311 printk(PRINT_PREF
"verifying all eraseblocks\n");
312 for (i
= 0; i
< ebcnt
; ++i
) {
315 err
= verify_eraseblock(i
);
319 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
322 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
326 static int is_block_bad(int ebnum
)
329 loff_t addr
= ebnum
* mtd
->erasesize
;
331 ret
= mtd
->block_isbad(mtd
, addr
);
333 printk(PRINT_PREF
"block %d is bad\n", ebnum
);
337 static int scan_for_bad_eraseblocks(void)
341 bbt
= kmalloc(ebcnt
, GFP_KERNEL
);
343 printk(PRINT_PREF
"error: cannot allocate memory\n");
346 memset(bbt
, 0 , ebcnt
);
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 mtd
->erasesize
= mtd
->erasesize
;
396 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
398 printk(PRINT_PREF
"error: cannot allocate memory\n");
401 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
403 printk(PRINT_PREF
"error: cannot allocate memory\n");
407 err
= scan_for_bad_eraseblocks();
412 use_len
= mtd
->ecclayout
->oobavail
;
413 use_len_max
= mtd
->ecclayout
->oobavail
;
416 /* First test: write all OOB, read it back and verify */
417 printk(PRINT_PREF
"test 1 of 5\n");
419 err
= erase_whole_device();
424 err
= write_whole_device();
429 err
= verify_all_eraseblocks();
434 * Second test: write all OOB, a block at a time, read it back and
437 printk(PRINT_PREF
"test 2 of 5\n");
439 err
= erase_whole_device();
444 err
= write_whole_device();
448 /* Check all eraseblocks */
450 printk(PRINT_PREF
"verifying all eraseblocks\n");
451 for (i
= 0; i
< ebcnt
; ++i
) {
454 err
= verify_eraseblock_in_one_go(i
);
458 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
461 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
464 * Third test: write OOB at varying offsets and lengths, read it back
467 printk(PRINT_PREF
"test 3 of 5\n");
469 err
= erase_whole_device();
473 /* Write all eraseblocks */
475 use_len
= mtd
->ecclayout
->oobavail
;
476 use_len_max
= mtd
->ecclayout
->oobavail
;
479 printk(PRINT_PREF
"writing OOBs of whole device\n");
480 for (i
= 0; i
< ebcnt
; ++i
) {
483 err
= write_eraseblock(i
);
487 printk(PRINT_PREF
"written up to eraseblock %u\n", i
);
490 printk(PRINT_PREF
"written %u eraseblocks\n", i
);
492 /* Check all eraseblocks */
494 use_len
= mtd
->ecclayout
->oobavail
;
495 use_len_max
= mtd
->ecclayout
->oobavail
;
498 err
= verify_all_eraseblocks();
503 use_len
= mtd
->ecclayout
->oobavail
;
504 use_len_max
= mtd
->ecclayout
->oobavail
;
507 /* Fourth test: try to write off end of device */
508 printk(PRINT_PREF
"test 4 of 5\n");
510 err
= erase_whole_device();
515 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
516 addr0
+= mtd
->erasesize
;
518 /* Attempt to write off end of OOB */
519 ops
.mode
= MTD_OOB_AUTO
;
524 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
526 ops
.oobbuf
= writebuf
;
527 printk(PRINT_PREF
"attempting to start write past end of OOB\n");
528 printk(PRINT_PREF
"an error is expected...\n");
529 err
= mtd
->write_oob(mtd
, addr0
, &ops
);
531 printk(PRINT_PREF
"error occurred as expected\n");
534 printk(PRINT_PREF
"error: can write past end of OOB\n");
538 /* Attempt to read off end of OOB */
539 ops
.mode
= MTD_OOB_AUTO
;
544 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
546 ops
.oobbuf
= readbuf
;
547 printk(PRINT_PREF
"attempting to start read past end of OOB\n");
548 printk(PRINT_PREF
"an error is expected...\n");
549 err
= mtd
->read_oob(mtd
, addr0
, &ops
);
551 printk(PRINT_PREF
"error occurred as expected\n");
554 printk(PRINT_PREF
"error: can read past end of OOB\n");
559 printk(PRINT_PREF
"skipping end of device tests because last "
562 /* Attempt to write off end of device */
563 ops
.mode
= MTD_OOB_AUTO
;
566 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
570 ops
.oobbuf
= writebuf
;
571 printk(PRINT_PREF
"attempting to write past end of device\n");
572 printk(PRINT_PREF
"an error is expected...\n");
573 err
= mtd
->write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
575 printk(PRINT_PREF
"error occurred as expected\n");
578 printk(PRINT_PREF
"error: wrote past end of device\n");
582 /* Attempt to read off end of device */
583 ops
.mode
= MTD_OOB_AUTO
;
586 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
590 ops
.oobbuf
= readbuf
;
591 printk(PRINT_PREF
"attempting to read past end of device\n");
592 printk(PRINT_PREF
"an error is expected...\n");
593 err
= mtd
->read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
595 printk(PRINT_PREF
"error occurred as expected\n");
598 printk(PRINT_PREF
"error: read past end of device\n");
602 err
= erase_eraseblock(ebcnt
- 1);
606 /* Attempt to write off end of device */
607 ops
.mode
= MTD_OOB_AUTO
;
610 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
614 ops
.oobbuf
= writebuf
;
615 printk(PRINT_PREF
"attempting to write past end of device\n");
616 printk(PRINT_PREF
"an error is expected...\n");
617 err
= mtd
->write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
619 printk(PRINT_PREF
"error occurred as expected\n");
622 printk(PRINT_PREF
"error: wrote past end of device\n");
626 /* Attempt to read off end of device */
627 ops
.mode
= MTD_OOB_AUTO
;
630 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
634 ops
.oobbuf
= readbuf
;
635 printk(PRINT_PREF
"attempting to read past end of device\n");
636 printk(PRINT_PREF
"an error is expected...\n");
637 err
= mtd
->read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
639 printk(PRINT_PREF
"error occurred as expected\n");
642 printk(PRINT_PREF
"error: read past end of device\n");
647 /* Fifth test: write / read across block boundaries */
648 printk(PRINT_PREF
"test 5 of 5\n");
650 /* Erase all eraseblocks */
651 err
= erase_whole_device();
655 /* Write all eraseblocks */
657 printk(PRINT_PREF
"writing OOBs of whole device\n");
658 for (i
= 0; i
< ebcnt
- 1; ++i
) {
661 size_t sz
= mtd
->ecclayout
->oobavail
;
662 if (bbt
[i
] || bbt
[i
+ 1])
664 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
665 for (pg
= 0; pg
< cnt
; ++pg
) {
666 set_random_data(writebuf
, sz
);
667 ops
.mode
= MTD_OOB_AUTO
;
674 ops
.oobbuf
= writebuf
;
675 err
= mtd
->write_oob(mtd
, addr
, &ops
);
679 printk(PRINT_PREF
"written up to eraseblock "
682 addr
+= mtd
->writesize
;
685 printk(PRINT_PREF
"written %u eraseblocks\n", i
);
687 /* Check all eraseblocks */
689 printk(PRINT_PREF
"verifying all eraseblocks\n");
690 for (i
= 0; i
< ebcnt
- 1; ++i
) {
691 if (bbt
[i
] || bbt
[i
+ 1])
693 set_random_data(writebuf
, mtd
->ecclayout
->oobavail
* 2);
694 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
695 ops
.mode
= MTD_OOB_AUTO
;
698 ops
.ooblen
= mtd
->ecclayout
->oobavail
* 2;
702 ops
.oobbuf
= readbuf
;
703 err
= mtd
->read_oob(mtd
, addr
, &ops
);
706 if (memcmp(readbuf
, writebuf
, mtd
->ecclayout
->oobavail
* 2)) {
707 printk(PRINT_PREF
"error: verify failed at %#llx\n",
711 printk(PRINT_PREF
"error: too many errors\n");
716 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
719 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
721 printk(PRINT_PREF
"finished with %d errors\n", errcnt
);
728 printk(PRINT_PREF
"error %d occurred\n", err
);
729 printk(KERN_INFO
"=================================================\n");
732 module_init(mtd_oobtest_init
);
734 static void __exit
mtd_oobtest_exit(void)
738 module_exit(mtd_oobtest_exit
);
740 MODULE_DESCRIPTION("Out-of-band test module");
741 MODULE_AUTHOR("Adrian Hunter");
742 MODULE_LICENSE("GPL");