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");
347 printk(PRINT_PREF
"scanning for bad eraseblocks\n");
348 for (i
= 0; i
< ebcnt
; ++i
) {
349 bbt
[i
] = is_block_bad(i
) ? 1 : 0;
354 printk(PRINT_PREF
"scanned %d eraseblocks, %d are bad\n", i
, bad
);
358 static int __init
mtd_oobtest_init(void)
363 struct mtd_oob_ops ops
;
364 loff_t addr
= 0, addr0
;
366 printk(KERN_INFO
"\n");
367 printk(KERN_INFO
"=================================================\n");
368 printk(PRINT_PREF
"MTD device: %d\n", dev
);
370 mtd
= get_mtd_device(NULL
, dev
);
373 printk(PRINT_PREF
"error: cannot get MTD device\n");
377 if (mtd
->type
!= MTD_NANDFLASH
) {
378 printk(PRINT_PREF
"this test requires NAND flash\n");
383 do_div(tmp
, mtd
->erasesize
);
385 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
387 printk(PRINT_PREF
"MTD device size %llu, eraseblock size %u, "
388 "page size %u, count of eraseblocks %u, pages per "
389 "eraseblock %u, OOB size %u\n",
390 (unsigned long long)mtd
->size
, mtd
->erasesize
,
391 mtd
->writesize
, ebcnt
, pgcnt
, mtd
->oobsize
);
394 readbuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
396 printk(PRINT_PREF
"error: cannot allocate memory\n");
399 writebuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
401 printk(PRINT_PREF
"error: cannot allocate memory\n");
405 err
= scan_for_bad_eraseblocks();
410 use_len
= mtd
->ecclayout
->oobavail
;
411 use_len_max
= mtd
->ecclayout
->oobavail
;
414 /* First test: write all OOB, read it back and verify */
415 printk(PRINT_PREF
"test 1 of 5\n");
417 err
= erase_whole_device();
422 err
= write_whole_device();
427 err
= verify_all_eraseblocks();
432 * Second test: write all OOB, a block at a time, read it back and
435 printk(PRINT_PREF
"test 2 of 5\n");
437 err
= erase_whole_device();
442 err
= write_whole_device();
446 /* Check all eraseblocks */
448 printk(PRINT_PREF
"verifying all eraseblocks\n");
449 for (i
= 0; i
< ebcnt
; ++i
) {
452 err
= verify_eraseblock_in_one_go(i
);
456 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
459 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
462 * Third test: write OOB at varying offsets and lengths, read it back
465 printk(PRINT_PREF
"test 3 of 5\n");
467 err
= erase_whole_device();
471 /* Write all eraseblocks */
473 use_len
= mtd
->ecclayout
->oobavail
;
474 use_len_max
= mtd
->ecclayout
->oobavail
;
478 err
= write_whole_device();
482 /* Check all eraseblocks */
484 use_len
= mtd
->ecclayout
->oobavail
;
485 use_len_max
= mtd
->ecclayout
->oobavail
;
488 err
= verify_all_eraseblocks();
493 use_len
= mtd
->ecclayout
->oobavail
;
494 use_len_max
= mtd
->ecclayout
->oobavail
;
497 /* Fourth test: try to write off end of device */
498 printk(PRINT_PREF
"test 4 of 5\n");
500 err
= erase_whole_device();
505 for (i
= 0; i
< ebcnt
&& bbt
[i
]; ++i
)
506 addr0
+= mtd
->erasesize
;
508 /* Attempt to write off end of OOB */
509 ops
.mode
= MTD_OOB_AUTO
;
514 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
516 ops
.oobbuf
= writebuf
;
517 printk(PRINT_PREF
"attempting to start write past end of OOB\n");
518 printk(PRINT_PREF
"an error is expected...\n");
519 err
= mtd
->write_oob(mtd
, addr0
, &ops
);
521 printk(PRINT_PREF
"error occurred as expected\n");
524 printk(PRINT_PREF
"error: can write past end of OOB\n");
528 /* Attempt to read off end of OOB */
529 ops
.mode
= MTD_OOB_AUTO
;
534 ops
.ooboffs
= mtd
->ecclayout
->oobavail
;
536 ops
.oobbuf
= readbuf
;
537 printk(PRINT_PREF
"attempting to start read past end of OOB\n");
538 printk(PRINT_PREF
"an error is expected...\n");
539 err
= mtd
->read_oob(mtd
, addr0
, &ops
);
541 printk(PRINT_PREF
"error occurred as expected\n");
544 printk(PRINT_PREF
"error: can read past end of OOB\n");
549 printk(PRINT_PREF
"skipping end of device tests because last "
552 /* Attempt to write off end of device */
553 ops
.mode
= MTD_OOB_AUTO
;
556 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
560 ops
.oobbuf
= writebuf
;
561 printk(PRINT_PREF
"attempting to write past end of device\n");
562 printk(PRINT_PREF
"an error is expected...\n");
563 err
= mtd
->write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
565 printk(PRINT_PREF
"error occurred as expected\n");
568 printk(PRINT_PREF
"error: wrote past end of device\n");
572 /* Attempt to read off end of device */
573 ops
.mode
= MTD_OOB_AUTO
;
576 ops
.ooblen
= mtd
->ecclayout
->oobavail
+ 1;
580 ops
.oobbuf
= readbuf
;
581 printk(PRINT_PREF
"attempting to read past end of device\n");
582 printk(PRINT_PREF
"an error is expected...\n");
583 err
= mtd
->read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
585 printk(PRINT_PREF
"error occurred as expected\n");
588 printk(PRINT_PREF
"error: read past end of device\n");
592 err
= erase_eraseblock(ebcnt
- 1);
596 /* Attempt to write off end of device */
597 ops
.mode
= MTD_OOB_AUTO
;
600 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
604 ops
.oobbuf
= writebuf
;
605 printk(PRINT_PREF
"attempting to write past end of device\n");
606 printk(PRINT_PREF
"an error is expected...\n");
607 err
= mtd
->write_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
609 printk(PRINT_PREF
"error occurred as expected\n");
612 printk(PRINT_PREF
"error: wrote past end of device\n");
616 /* Attempt to read off end of device */
617 ops
.mode
= MTD_OOB_AUTO
;
620 ops
.ooblen
= mtd
->ecclayout
->oobavail
;
624 ops
.oobbuf
= readbuf
;
625 printk(PRINT_PREF
"attempting to read past end of device\n");
626 printk(PRINT_PREF
"an error is expected...\n");
627 err
= mtd
->read_oob(mtd
, mtd
->size
- mtd
->writesize
, &ops
);
629 printk(PRINT_PREF
"error occurred as expected\n");
632 printk(PRINT_PREF
"error: read past end of device\n");
637 /* Fifth test: write / read across block boundaries */
638 printk(PRINT_PREF
"test 5 of 5\n");
640 /* Erase all eraseblocks */
641 err
= erase_whole_device();
645 /* Write all eraseblocks */
647 printk(PRINT_PREF
"writing OOBs of whole device\n");
648 for (i
= 0; i
< ebcnt
- 1; ++i
) {
651 size_t sz
= mtd
->ecclayout
->oobavail
;
652 if (bbt
[i
] || bbt
[i
+ 1])
654 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
655 for (pg
= 0; pg
< cnt
; ++pg
) {
656 set_random_data(writebuf
, sz
);
657 ops
.mode
= MTD_OOB_AUTO
;
664 ops
.oobbuf
= writebuf
;
665 err
= mtd
->write_oob(mtd
, addr
, &ops
);
669 printk(PRINT_PREF
"written up to eraseblock "
672 addr
+= mtd
->writesize
;
675 printk(PRINT_PREF
"written %u eraseblocks\n", i
);
677 /* Check all eraseblocks */
679 printk(PRINT_PREF
"verifying all eraseblocks\n");
680 for (i
= 0; i
< ebcnt
- 1; ++i
) {
681 if (bbt
[i
] || bbt
[i
+ 1])
683 set_random_data(writebuf
, mtd
->ecclayout
->oobavail
* 2);
684 addr
= (i
+ 1) * mtd
->erasesize
- mtd
->writesize
;
685 ops
.mode
= MTD_OOB_AUTO
;
688 ops
.ooblen
= mtd
->ecclayout
->oobavail
* 2;
692 ops
.oobbuf
= readbuf
;
693 err
= mtd
->read_oob(mtd
, addr
, &ops
);
696 if (memcmp(readbuf
, writebuf
, mtd
->ecclayout
->oobavail
* 2)) {
697 printk(PRINT_PREF
"error: verify failed at %#llx\n",
701 printk(PRINT_PREF
"error: too many errors\n");
706 printk(PRINT_PREF
"verified up to eraseblock %u\n", i
);
709 printk(PRINT_PREF
"verified %u eraseblocks\n", i
);
711 printk(PRINT_PREF
"finished with %d errors\n", errcnt
);
718 printk(PRINT_PREF
"error %d occurred\n", err
);
719 printk(KERN_INFO
"=================================================\n");
722 module_init(mtd_oobtest_init
);
724 static void __exit
mtd_oobtest_exit(void)
728 module_exit(mtd_oobtest_exit
);
730 MODULE_DESCRIPTION("Out-of-band test module");
731 MODULE_AUTHOR("Adrian Hunter");
732 MODULE_LICENSE("GPL");