1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2008 Nokia Corporation
5 * Check MTD device read.
7 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/err.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
22 static int dev
= -EINVAL
;
23 module_param(dev
, int, S_IRUGO
);
24 MODULE_PARM_DESC(dev
, "MTD device number to use");
26 static struct mtd_info
*mtd
;
27 static unsigned char *iobuf
;
28 static unsigned char *iobuf1
;
29 static unsigned char *bbt
;
35 static int read_eraseblock_by_page(int ebnum
)
38 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
40 void *oobbuf
= iobuf1
;
42 for (i
= 0; i
< pgcnt
; i
++) {
43 memset(buf
, 0 , pgsize
);
44 ret
= mtdtest_read(mtd
, addr
, pgsize
, buf
);
50 struct mtd_oob_ops ops
;
52 ops
.mode
= MTD_OPS_PLACE_OOB
;
55 ops
.ooblen
= mtd
->oobsize
;
60 ret
= mtd_read_oob(mtd
, addr
, &ops
);
61 if ((ret
&& !mtd_is_bitflip(ret
)) ||
62 ops
.oobretlen
!= mtd
->oobsize
) {
63 pr_err("error: read oob failed at "
64 "%#llx\n", (long long)addr
);
70 oobbuf
+= mtd
->oobsize
;
79 static void dump_eraseblock(int ebnum
)
85 pr_info("dumping eraseblock %d\n", ebnum
);
90 p
+= sprintf(p
, "%05x: ", i
);
91 for (j
= 0; j
< 32 && i
< n
; j
++, i
++)
92 p
+= sprintf(p
, "%02x", (unsigned int)iobuf
[i
]);
93 printk(KERN_CRIT
"%s\n", line
);
98 pr_info("dumping oob from eraseblock %d\n", ebnum
);
100 for (pg
= 0, i
= 0; pg
< pgcnt
; pg
++)
101 for (oob
= 0; oob
< n
;) {
104 p
+= sprintf(p
, "%05x: ", i
);
105 for (j
= 0; j
< 32 && oob
< n
; j
++, oob
++, i
++)
106 p
+= sprintf(p
, "%02x",
107 (unsigned int)iobuf1
[i
]);
108 printk(KERN_CRIT
"%s\n", line
);
113 static int __init
mtd_readtest_init(void)
118 printk(KERN_INFO
"\n");
119 printk(KERN_INFO
"=================================================\n");
122 pr_info("Please specify a valid mtd-device via module parameter\n");
126 pr_info("MTD device: %d\n", dev
);
128 mtd
= get_mtd_device(NULL
, dev
);
131 pr_err("error: Cannot get MTD device\n");
135 if (mtd
->writesize
== 1) {
136 pr_info("not NAND flash, assume page size is 512 "
140 pgsize
= mtd
->writesize
;
143 do_div(tmp
, mtd
->erasesize
);
145 pgcnt
= mtd
->erasesize
/ pgsize
;
147 pr_info("MTD device size %llu, eraseblock size %u, "
148 "page size %u, count of eraseblocks %u, pages per "
149 "eraseblock %u, OOB size %u\n",
150 (unsigned long long)mtd
->size
, mtd
->erasesize
,
151 pgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
154 iobuf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
157 iobuf1
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
161 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
164 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
168 /* Read all eraseblocks 1 page at a time */
169 pr_info("testing page read\n");
170 for (i
= 0; i
< ebcnt
; ++i
) {
175 ret
= read_eraseblock_by_page(i
);
182 ret
= mtdtest_relax();
190 pr_info("finished with errors\n");
192 pr_info("finished\n");
201 pr_info("error %d occurred\n", err
);
202 printk(KERN_INFO
"=================================================\n");
205 module_init(mtd_readtest_init
);
207 static void __exit
mtd_readtest_exit(void)
211 module_exit(mtd_readtest_exit
);
213 MODULE_DESCRIPTION("Read test module");
214 MODULE_AUTHOR("Adrian Hunter");
215 MODULE_LICENSE("GPL");