1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2007 Nokia Corporation
5 * Test sub-page read and write on MTD device.
6 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/err.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/slab.h>
17 #include <linux/sched.h>
18 #include <linux/random.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 *writebuf
;
28 static unsigned char *readbuf
;
29 static unsigned char *bbt
;
36 static struct rnd_state rnd_state
;
38 static inline void clear_data(unsigned char *buf
, size_t len
)
43 static int write_eraseblock(int ebnum
)
47 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
49 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
50 err
= mtd_write(mtd
, addr
, subpgsize
, &written
, writebuf
);
51 if (unlikely(err
|| written
!= subpgsize
)) {
52 pr_err("error: write failed at %#llx\n",
54 if (written
!= subpgsize
) {
55 pr_err(" write size: %#x\n", subpgsize
);
56 pr_err(" written: %#zx\n", written
);
58 return err
? err
: -1;
63 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
64 err
= mtd_write(mtd
, addr
, subpgsize
, &written
, writebuf
);
65 if (unlikely(err
|| written
!= subpgsize
)) {
66 pr_err("error: write failed at %#llx\n",
68 if (written
!= subpgsize
) {
69 pr_err(" write size: %#x\n", subpgsize
);
70 pr_err(" written: %#zx\n", written
);
72 return err
? err
: -1;
78 static int write_eraseblock2(int ebnum
)
82 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
84 for (k
= 1; k
< 33; ++k
) {
85 if (addr
+ (subpgsize
* k
) > (loff_t
)(ebnum
+ 1) * mtd
->erasesize
)
87 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
* k
);
88 err
= mtd_write(mtd
, addr
, subpgsize
* k
, &written
, writebuf
);
89 if (unlikely(err
|| written
!= subpgsize
* k
)) {
90 pr_err("error: write failed at %#llx\n",
92 if (written
!= subpgsize
* k
) {
93 pr_err(" write size: %#x\n",
95 pr_err(" written: %#08zx\n",
98 return err
? err
: -1;
100 addr
+= subpgsize
* k
;
106 static void print_subpage(unsigned char *p
)
110 for (i
= 0; i
< subpgsize
; ) {
111 for (j
= 0; i
< subpgsize
&& j
< 32; ++i
, ++j
)
112 printk("%02x", *p
++);
117 static int verify_eraseblock(int ebnum
)
121 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
123 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
124 clear_data(readbuf
, subpgsize
);
125 err
= mtd_read(mtd
, addr
, subpgsize
, &read
, readbuf
);
126 if (unlikely(err
|| read
!= subpgsize
)) {
127 if (mtd_is_bitflip(err
) && read
== subpgsize
) {
128 pr_info("ECC correction at %#llx\n",
132 pr_err("error: read failed at %#llx\n",
134 return err
? err
: -1;
137 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
))) {
138 pr_err("error: verify failed at %#llx\n",
140 pr_info("------------- written----------------\n");
141 print_subpage(writebuf
);
142 pr_info("------------- read ------------------\n");
143 print_subpage(readbuf
);
144 pr_info("-------------------------------------\n");
150 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
);
151 clear_data(readbuf
, subpgsize
);
152 err
= mtd_read(mtd
, addr
, subpgsize
, &read
, readbuf
);
153 if (unlikely(err
|| read
!= subpgsize
)) {
154 if (mtd_is_bitflip(err
) && read
== subpgsize
) {
155 pr_info("ECC correction at %#llx\n",
159 pr_err("error: read failed at %#llx\n",
161 return err
? err
: -1;
164 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
))) {
165 pr_info("error: verify failed at %#llx\n",
167 pr_info("------------- written----------------\n");
168 print_subpage(writebuf
);
169 pr_info("------------- read ------------------\n");
170 print_subpage(readbuf
);
171 pr_info("-------------------------------------\n");
178 static int verify_eraseblock2(int ebnum
)
182 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
184 for (k
= 1; k
< 33; ++k
) {
185 if (addr
+ (subpgsize
* k
) > (loff_t
)(ebnum
+ 1) * mtd
->erasesize
)
187 prandom_bytes_state(&rnd_state
, writebuf
, subpgsize
* k
);
188 clear_data(readbuf
, subpgsize
* k
);
189 err
= mtd_read(mtd
, addr
, subpgsize
* k
, &read
, readbuf
);
190 if (unlikely(err
|| read
!= subpgsize
* k
)) {
191 if (mtd_is_bitflip(err
) && read
== subpgsize
* k
) {
192 pr_info("ECC correction at %#llx\n",
196 pr_err("error: read failed at "
197 "%#llx\n", (long long)addr
);
198 return err
? err
: -1;
201 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
* k
))) {
202 pr_err("error: verify failed at %#llx\n",
206 addr
+= subpgsize
* k
;
212 static int verify_eraseblock_ff(int ebnum
)
217 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
219 memset(writebuf
, 0xff, subpgsize
);
220 for (j
= 0; j
< mtd
->erasesize
/ subpgsize
; ++j
) {
221 clear_data(readbuf
, subpgsize
);
222 err
= mtd_read(mtd
, addr
, subpgsize
, &read
, readbuf
);
223 if (unlikely(err
|| read
!= subpgsize
)) {
224 if (mtd_is_bitflip(err
) && read
== subpgsize
) {
225 pr_info("ECC correction at %#llx\n",
229 pr_err("error: read failed at "
230 "%#llx\n", (long long)addr
);
231 return err
? err
: -1;
234 if (unlikely(memcmp(readbuf
, writebuf
, subpgsize
))) {
235 pr_err("error: verify 0xff failed at "
236 "%#llx\n", (long long)addr
);
245 static int verify_all_eraseblocks_ff(void)
250 pr_info("verifying all eraseblocks for 0xff\n");
251 for (i
= 0; i
< ebcnt
; ++i
) {
254 err
= verify_eraseblock_ff(i
);
258 pr_info("verified up to eraseblock %u\n", i
);
260 err
= mtdtest_relax();
264 pr_info("verified %u eraseblocks\n", i
);
268 static int __init
mtd_subpagetest_init(void)
274 printk(KERN_INFO
"\n");
275 printk(KERN_INFO
"=================================================\n");
278 pr_info("Please specify a valid mtd-device via module parameter\n");
279 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
283 pr_info("MTD device: %d\n", dev
);
285 mtd
= get_mtd_device(NULL
, dev
);
288 pr_err("error: cannot get MTD device\n");
292 if (!mtd_type_is_nand(mtd
)) {
293 pr_info("this test requires NAND flash\n");
297 subpgsize
= mtd
->writesize
>> mtd
->subpage_sft
;
299 do_div(tmp
, mtd
->erasesize
);
301 pgcnt
= mtd
->erasesize
/ mtd
->writesize
;
303 pr_info("MTD device size %llu, eraseblock size %u, "
304 "page size %u, subpage size %u, count of eraseblocks %u, "
305 "pages per eraseblock %u, OOB size %u\n",
306 (unsigned long long)mtd
->size
, mtd
->erasesize
,
307 mtd
->writesize
, subpgsize
, ebcnt
, pgcnt
, mtd
->oobsize
);
310 bufsize
= subpgsize
* 32;
311 writebuf
= kmalloc(bufsize
, GFP_KERNEL
);
314 readbuf
= kmalloc(bufsize
, GFP_KERNEL
);
317 bbt
= kzalloc(ebcnt
, GFP_KERNEL
);
321 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bbt
, 0, ebcnt
);
325 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
329 pr_info("writing whole device\n");
330 prandom_seed_state(&rnd_state
, 1);
331 for (i
= 0; i
< ebcnt
; ++i
) {
334 err
= write_eraseblock(i
);
338 pr_info("written up to eraseblock %u\n", i
);
340 err
= mtdtest_relax();
344 pr_info("written %u eraseblocks\n", i
);
346 prandom_seed_state(&rnd_state
, 1);
347 pr_info("verifying all eraseblocks\n");
348 for (i
= 0; i
< ebcnt
; ++i
) {
351 err
= verify_eraseblock(i
);
355 pr_info("verified up to eraseblock %u\n", i
);
357 err
= mtdtest_relax();
361 pr_info("verified %u eraseblocks\n", i
);
363 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
367 err
= verify_all_eraseblocks_ff();
371 /* Write all eraseblocks */
372 prandom_seed_state(&rnd_state
, 3);
373 pr_info("writing whole device\n");
374 for (i
= 0; i
< ebcnt
; ++i
) {
377 err
= write_eraseblock2(i
);
381 pr_info("written up to eraseblock %u\n", i
);
383 err
= mtdtest_relax();
387 pr_info("written %u eraseblocks\n", i
);
389 /* Check all eraseblocks */
390 prandom_seed_state(&rnd_state
, 3);
391 pr_info("verifying all eraseblocks\n");
392 for (i
= 0; i
< ebcnt
; ++i
) {
395 err
= verify_eraseblock2(i
);
399 pr_info("verified up to eraseblock %u\n", i
);
401 err
= mtdtest_relax();
405 pr_info("verified %u eraseblocks\n", i
);
407 err
= mtdtest_erase_good_eraseblocks(mtd
, bbt
, 0, ebcnt
);
411 err
= verify_all_eraseblocks_ff();
415 pr_info("finished with %d errors\n", errcnt
);
423 pr_info("error %d occurred\n", err
);
424 printk(KERN_INFO
"=================================================\n");
427 module_init(mtd_subpagetest_init
);
429 static void __exit
mtd_subpagetest_exit(void)
433 module_exit(mtd_subpagetest_exit
);
435 MODULE_DESCRIPTION("Subpage test module");
436 MODULE_AUTHOR("Adrian Hunter");
437 MODULE_LICENSE("GPL");