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);
233 /* Initialize patterns */
234 memset(patt_FF, 0xFF, mtd->erasesize);
235 for (i = 0; i < mtd->erasesize / pgsize; i++) {
237 memset(patt_5A5 + i * pgsize, 0x55, pgsize);
238 memset(patt_A5A + i * pgsize, 0xAA, pgsize);
240 memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
241 memset(patt_A5A + i * pgsize, 0x55, pgsize);
245 err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
254 err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
258 /* Check if the eraseblocks contain only 0xFF bytes */
260 for (i = eb; i < eb + ebcnt; i++) {
263 err = check_eraseblock(i, patt_FF);
265 pr_info("verify failed"
266 " for 0xFF... pattern\n");
270 err = mtdtest_relax();
276 /* Write the pattern */
277 for (i = eb; i < eb + ebcnt; i++) {
280 if ((eb + erase_cycles) & 1)
284 err = write_pattern(i, patt);
288 err = mtdtest_relax();
293 /* Verify what we wrote */
295 for (i = eb; i < eb + ebcnt; i++) {
298 if ((eb + erase_cycles) & 1)
302 err = check_eraseblock(i, patt);
304 pr_info("verify failed for %s"
306 ((eb + erase_cycles) & 1) ?
307 "0x55AA55..." : "0xAA55AA...");
311 err = mtdtest_relax();
319 if (erase_cycles % gran == 0) {
323 ms = ktime_ms_delta(finish, start);
324 pr_info("%08u erase cycles done, took %lu "
325 "milliseconds (%lu seconds)\n",
326 erase_cycles, ms, ms / 1000);
330 if (!infinite && --cycles_count == 0)
335 pr_info("finished after %u erase cycles\n",
349 pr_info("error %d occurred during torturing\n", err);
350 printk(KERN_INFO "=================================================\n");
353 module_init(tort_init);
355 static void __exit tort_exit(void)
359 module_exit(tort_exit);
361 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
362 unsigned offset, unsigned len, unsigned *bytesp,
364 static void print_bufs(unsigned char *read, unsigned char *written, int start,
368 * Report the detailed information about how the read EB differs from what was
371 static void report_corrupt(unsigned char *read, unsigned char *written)
374 int bytes, bits, pages, first;
376 size_t check_len = mtd->erasesize;
379 check_len = pgcnt * pgsize;
381 bytes = bits = pages = 0;
382 for (i = 0; i < check_len; i += pgsize)
383 if (countdiffs(written, read, i, pgsize, &bytes,
387 pr_info("verify fails on %d pages, %d bytes/%d bits\n",
389 pr_info("The following is a list of all differences between"
390 " what was read from flash and what was expected\n");
392 for (i = 0; i < check_len; i += pgsize) {
395 first = countdiffs(written, read, i, pgsize, &bytes,
400 printk("-------------------------------------------------------"
401 "----------------------------------\n");
403 pr_info("Page %zd has %d bytes/%d bits failing verify,"
404 " starting at offset 0x%x\n",
405 (mtd->erasesize - check_len + i) / pgsize,
408 offset = first & ~0x7;
409 len = ((first + bytes) | 0x7) + 1 - offset;
411 print_bufs(read, written, offset, len);
415 static void print_bufs(unsigned char *read, unsigned char *written, int start,
421 printk("Offset Read Written\n");
423 printk("0x%08x: ", start + i);
425 for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
426 printk(" %02x", read[start + i + j1]);
427 if (read[start + i + j1] != written[start + i + j1])
436 printk(" %s ", diff);
438 for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
439 printk(" %02x", written[start + i + j2]);
446 * Count the number of differing bytes and bits and return the first differing
449 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
450 unsigned offset, unsigned len, unsigned *bytesp,
456 for (i = offset; i < offset + len; i++)
457 if (buf[i] != check_buf[i]) {
462 while (i < offset + len) {
463 if (buf[i] != check_buf[i]) {
467 if ((buf[i] & bit) != (check_buf[i] & bit))
478 MODULE_DESCRIPTION("Eraseblock torturing module");
479 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
480 MODULE_LICENSE("GPL");