1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2008 Artem Bityutskiy
4 * Copyright (C) 2006-2008 Jarkko Lavinen
5 * Copyright (C) 2006-2008 Adrian Hunter
7 * Authors: Artem Bityutskiy, Jarkko Lavinen, Adria Hunter
9 * WARNING: this test program may kill your flash and your device. Do not
10 * use it unless you know what you do. Authors are not responsible for any
11 * damage caused by this program.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/init.h>
17 #include <linux/ktime.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/err.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
29 module_param(eb
, int, S_IRUGO
);
30 MODULE_PARM_DESC(eb
, "eraseblock number within the selected MTD device");
32 static int ebcnt
= 32;
33 module_param(ebcnt
, int, S_IRUGO
);
34 MODULE_PARM_DESC(ebcnt
, "number of consecutive eraseblocks to torture");
37 module_param(pgcnt
, int, S_IRUGO
);
38 MODULE_PARM_DESC(pgcnt
, "number of pages per eraseblock to torture (0 => all)");
40 static int dev
= -EINVAL
;
41 module_param(dev
, int, S_IRUGO
);
42 MODULE_PARM_DESC(dev
, "MTD device number to use");
44 static int gran
= 512;
45 module_param(gran
, int, S_IRUGO
);
46 MODULE_PARM_DESC(gran
, "how often the status information should be printed");
49 module_param(check
, int, S_IRUGO
);
50 MODULE_PARM_DESC(check
, "if the written data should be checked");
52 static unsigned int cycles_count
;
53 module_param(cycles_count
, uint
, S_IRUGO
);
54 MODULE_PARM_DESC(cycles_count
, "how many erase cycles to do "
55 "(infinite by default)");
57 static struct mtd_info
*mtd
;
59 /* This buffer contains 0x555555...0xAAAAAA... pattern */
60 static unsigned char *patt_5A5
;
61 /* This buffer contains 0xAAAAAA...0x555555... pattern */
62 static unsigned char *patt_A5A
;
63 /* This buffer contains all 0xFF bytes */
64 static unsigned char *patt_FF
;
65 /* This a temporary buffer is use when checking data */
66 static unsigned char *check_buf
;
67 /* How many erase cycles were done */
68 static unsigned int erase_cycles
;
71 static ktime_t start
, finish
;
73 static void report_corrupt(unsigned char *read
, unsigned char *written
);
75 static inline void start_timing(void)
80 static inline void stop_timing(void)
86 * Check that the contents of eraseblock number @enbum is equivalent to the
89 static inline int check_eraseblock(int ebnum
, unsigned char *buf
)
93 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
94 size_t len
= mtd
->erasesize
;
97 addr
= (loff_t
)(ebnum
+ 1) * mtd
->erasesize
- pgcnt
* pgsize
;
102 err
= mtd_read(mtd
, addr
, len
, &read
, check_buf
);
103 if (mtd_is_bitflip(err
))
104 pr_err("single bit flip occurred at EB %d "
105 "MTD reported that it was fixed.\n", ebnum
);
107 pr_err("error %d while reading EB %d, "
108 "read %zd\n", err
, ebnum
, read
);
113 pr_err("failed to read %zd bytes from EB %d, "
114 "read only %zd, but no error reported\n",
119 if (memcmp(buf
, check_buf
, len
)) {
120 pr_err("read wrong data from EB %d\n", ebnum
);
121 report_corrupt(check_buf
, buf
);
123 if (retries
++ < RETRIES
) {
126 pr_info("re-try reading data from EB %d\n",
130 pr_info("retried %d times, still errors, "
131 "give-up\n", RETRIES
);
137 pr_info("only attempt number %d was OK (!!!)\n",
143 static inline int write_pattern(int ebnum
, void *buf
)
147 loff_t addr
= (loff_t
)ebnum
* mtd
->erasesize
;
148 size_t len
= mtd
->erasesize
;
151 addr
= (loff_t
)(ebnum
+ 1) * mtd
->erasesize
- pgcnt
* pgsize
;
152 len
= pgcnt
* pgsize
;
154 err
= mtd_write(mtd
, addr
, len
, &written
, buf
);
156 pr_err("error %d while writing EB %d, written %zd"
157 " bytes\n", err
, ebnum
, written
);
160 if (written
!= len
) {
161 pr_info("written only %zd bytes of %zd, but no error"
162 " reported\n", written
, len
);
169 static int __init
tort_init(void)
171 int err
= 0, i
, infinite
= !cycles_count
;
172 unsigned char *bad_ebs
;
174 printk(KERN_INFO
"\n");
175 printk(KERN_INFO
"=================================================\n");
176 pr_info("Warning: this program is trying to wear out your "
177 "flash, stop it if this is not wanted.\n");
180 pr_info("Please specify a valid mtd-device via module parameter\n");
181 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
185 pr_info("MTD device: %d\n", dev
);
186 pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
187 ebcnt
, eb
, eb
+ ebcnt
- 1, dev
);
189 pr_info("torturing just %d pages per eraseblock\n",
191 pr_info("write verify %s\n", check
? "enabled" : "disabled");
193 mtd
= get_mtd_device(NULL
, dev
);
196 pr_err("error: cannot get MTD device\n");
200 if (mtd
->writesize
== 1) {
201 pr_info("not NAND flash, assume page size is 512 "
205 pgsize
= mtd
->writesize
;
207 if (pgcnt
&& (pgcnt
> mtd
->erasesize
/ pgsize
|| pgcnt
< 0)) {
208 pr_err("error: invalid pgcnt value %d\n", pgcnt
);
213 patt_5A5
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
217 patt_A5A
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
221 patt_FF
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
225 check_buf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
229 bad_ebs
= kzalloc(ebcnt
, GFP_KERNEL
);
235 /* Initialize patterns */
236 memset(patt_FF
, 0xFF, mtd
->erasesize
);
237 for (i
= 0; i
< mtd
->erasesize
/ pgsize
; i
++) {
239 memset(patt_5A5
+ i
* pgsize
, 0x55, pgsize
);
240 memset(patt_A5A
+ i
* pgsize
, 0xAA, pgsize
);
242 memset(patt_5A5
+ i
* pgsize
, 0xAA, pgsize
);
243 memset(patt_A5A
+ i
* pgsize
, 0x55, pgsize
);
247 err
= mtdtest_scan_for_bad_eraseblocks(mtd
, bad_ebs
, eb
, ebcnt
);
256 err
= mtdtest_erase_good_eraseblocks(mtd
, bad_ebs
, eb
, ebcnt
);
260 /* Check if the eraseblocks contain only 0xFF bytes */
262 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
265 err
= check_eraseblock(i
, patt_FF
);
267 pr_info("verify failed"
268 " for 0xFF... pattern\n");
272 err
= mtdtest_relax();
278 /* Write the pattern */
279 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
282 if ((eb
+ erase_cycles
) & 1)
286 err
= write_pattern(i
, patt
);
290 err
= mtdtest_relax();
295 /* Verify what we wrote */
297 for (i
= eb
; i
< eb
+ ebcnt
; i
++) {
300 if ((eb
+ erase_cycles
) & 1)
304 err
= check_eraseblock(i
, patt
);
306 pr_info("verify failed for %s"
308 ((eb
+ erase_cycles
) & 1) ?
309 "0x55AA55..." : "0xAA55AA...");
313 err
= mtdtest_relax();
321 if (erase_cycles
% gran
== 0) {
325 ms
= ktime_ms_delta(finish
, start
);
326 pr_info("%08u erase cycles done, took %lu "
327 "milliseconds (%lu seconds)\n",
328 erase_cycles
, ms
, ms
/ 1000);
332 if (!infinite
&& --cycles_count
== 0)
337 pr_info("finished after %u erase cycles\n",
351 pr_info("error %d occurred during torturing\n", err
);
352 printk(KERN_INFO
"=================================================\n");
355 module_init(tort_init
);
357 static void __exit
tort_exit(void)
361 module_exit(tort_exit
);
363 static int countdiffs(unsigned char *buf
, unsigned char *check_buf
,
364 unsigned offset
, unsigned len
, unsigned *bytesp
,
366 static void print_bufs(unsigned char *read
, unsigned char *written
, int start
,
370 * Report the detailed information about how the read EB differs from what was
373 static void report_corrupt(unsigned char *read
, unsigned char *written
)
376 int bytes
, bits
, pages
, first
;
378 size_t check_len
= mtd
->erasesize
;
381 check_len
= pgcnt
* pgsize
;
383 bytes
= bits
= pages
= 0;
384 for (i
= 0; i
< check_len
; i
+= pgsize
)
385 if (countdiffs(written
, read
, i
, pgsize
, &bytes
,
389 pr_info("verify fails on %d pages, %d bytes/%d bits\n",
391 pr_info("The following is a list of all differences between"
392 " what was read from flash and what was expected\n");
394 for (i
= 0; i
< check_len
; i
+= pgsize
) {
397 first
= countdiffs(written
, read
, i
, pgsize
, &bytes
,
402 printk("-------------------------------------------------------"
403 "----------------------------------\n");
405 pr_info("Page %zd has %d bytes/%d bits failing verify,"
406 " starting at offset 0x%x\n",
407 (mtd
->erasesize
- check_len
+ i
) / pgsize
,
410 offset
= first
& ~0x7;
411 len
= ((first
+ bytes
) | 0x7) + 1 - offset
;
413 print_bufs(read
, written
, offset
, len
);
417 static void print_bufs(unsigned char *read
, unsigned char *written
, int start
,
423 printk("Offset Read Written\n");
425 printk("0x%08x: ", start
+ i
);
427 for (j1
= 0; j1
< 8 && i
+ j1
< len
; j1
++) {
428 printk(" %02x", read
[start
+ i
+ j1
]);
429 if (read
[start
+ i
+ j1
] != written
[start
+ i
+ j1
])
438 printk(" %s ", diff
);
440 for (j2
= 0; j2
< 8 && i
+ j2
< len
; j2
++)
441 printk(" %02x", written
[start
+ i
+ j2
]);
448 * Count the number of differing bytes and bits and return the first differing
451 static int countdiffs(unsigned char *buf
, unsigned char *check_buf
,
452 unsigned offset
, unsigned len
, unsigned *bytesp
,
458 for (i
= offset
; i
< offset
+ len
; i
++)
459 if (buf
[i
] != check_buf
[i
]) {
464 while (i
< offset
+ len
) {
465 if (buf
[i
] != check_buf
[i
]) {
469 if ((buf
[i
] & bit
) != (check_buf
[i
] & bit
))
480 MODULE_DESCRIPTION("Eraseblock torturing module");
481 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
482 MODULE_LICENSE("GPL");