1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2006-2008 Artem Bityutskiy
4 * Copyright (C) 2006-2008 Jarkko Lavinen
5 * Copyright (C) 2006-2008 Adrian Hunter
6 *
7 * Authors: Artem Bityutskiy, Jarkko Lavinen, Adria Hunter
8 *
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.
12 */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
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/random.h>
23 #include <linux/slab.h>
24 #include <linux/sched.h>
25 #include "mtd_test.h"
26
27 #define RETRIES 3
28
29 static int eb = 8;
30 module_param(eb, int, S_IRUGO);
31 MODULE_PARM_DESC(eb, "eraseblock number within the selected MTD device");
32
33 static int ebcnt = 32;
34 module_param(ebcnt, int, S_IRUGO);
35 MODULE_PARM_DESC(ebcnt, "number of consecutive eraseblocks to torture");
36
37 static int pgcnt;
38 module_param(pgcnt, int, S_IRUGO);
39 MODULE_PARM_DESC(pgcnt, "number of pages per eraseblock to torture (0 => all)");
40
41 static int dev = -EINVAL;
42 module_param(dev, int, S_IRUGO);
43 MODULE_PARM_DESC(dev, "MTD device number to use");
44
45 static int gran = 512;
46 module_param(gran, int, S_IRUGO);
47 MODULE_PARM_DESC(gran, "how often the status information should be printed");
48
49 static int check = 1;
50 module_param(check, int, S_IRUGO);
51 MODULE_PARM_DESC(check, "if the written data should be checked");
52
53 static unsigned int cycles_count;
54 module_param(cycles_count, uint, S_IRUGO);
55 MODULE_PARM_DESC(cycles_count, "how many erase cycles to do "
56 "(infinite by default)");
57
58 static int random_pattern;
59 module_param(random_pattern, int, S_IRUGO);
60 MODULE_PARM_DESC(random_pattern, "if choose random pattern to program");
61
62 static struct mtd_info *mtd;
63
64 /* This buffer contains random pattern */
65 static unsigned char *patt_random;
66 /* This buffer contains 0x555555...0xAAAAAA... pattern */
67 static unsigned char *patt_5A5;
68 /* This buffer contains 0xAAAAAA...0x555555... pattern */
69 static unsigned char *patt_A5A;
70 /* This buffer contains all 0xFF bytes */
71 static unsigned char *patt_FF;
72 /* This a temporary buffer is use when checking data */
73 static unsigned char *check_buf;
74 /* How many erase cycles were done */
75 static unsigned int erase_cycles;
76
77 static int pgsize;
78 static ktime_t start, finish;
79
80 static void report_corrupt(unsigned char *read, unsigned char *written);
81
start_timing(void)82 static inline void start_timing(void)
83 {
84 start = ktime_get();
85 }
86
stop_timing(void)87 static inline void stop_timing(void)
88 {
89 finish = ktime_get();
90 }
91
92 /*
93 * Check that the contents of eraseblock number @enbum is equivalent to the
94 * @buf buffer.
95 */
check_eraseblock(int ebnum,unsigned char * buf)96 static inline int check_eraseblock(int ebnum, unsigned char *buf)
97 {
98 int err, retries = 0;
99 size_t read;
100 loff_t addr = (loff_t)ebnum * mtd->erasesize;
101 size_t len = mtd->erasesize;
102
103 if (pgcnt) {
104 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
105 len = pgcnt * pgsize;
106 }
107
108 retry:
109 err = mtd_read(mtd, addr, len, &read, check_buf);
110 if (mtd_is_bitflip(err))
111 pr_err("single bit flip occurred at EB %d "
112 "MTD reported that it was fixed.\n", ebnum);
113 else if (err) {
114 pr_err("error %d while reading EB %d, "
115 "read %zd\n", err, ebnum, read);
116 return err;
117 }
118
119 if (read != len) {
120 pr_err("failed to read %zd bytes from EB %d, "
121 "read only %zd, but no error reported\n",
122 len, ebnum, read);
123 return -EIO;
124 }
125
126 if (memcmp(buf, check_buf, len)) {
127 pr_err("read wrong data from EB %d\n", ebnum);
128 report_corrupt(check_buf, buf);
129
130 if (retries++ < RETRIES) {
131 /* Try read again */
132 yield();
133 pr_info("re-try reading data from EB %d\n",
134 ebnum);
135 goto retry;
136 } else {
137 pr_info("retried %d times, still errors, "
138 "give-up\n", RETRIES);
139 return -EINVAL;
140 }
141 }
142
143 if (retries != 0)
144 pr_info("only attempt number %d was OK (!!!)\n",
145 retries);
146
147 return 0;
148 }
149
write_pattern(int ebnum,void * buf)150 static inline int write_pattern(int ebnum, void *buf)
151 {
152 int err;
153 size_t written;
154 loff_t addr = (loff_t)ebnum * mtd->erasesize;
155 size_t len = mtd->erasesize;
156
157 if (pgcnt) {
158 addr = (loff_t)(ebnum + 1) * mtd->erasesize - pgcnt * pgsize;
159 len = pgcnt * pgsize;
160 }
161 err = mtd_write(mtd, addr, len, &written, buf);
162 if (err) {
163 pr_err("error %d while writing EB %d, written %zd"
164 " bytes\n", err, ebnum, written);
165 return err;
166 }
167 if (written != len) {
168 pr_info("written only %zd bytes of %zd, but no error"
169 " reported\n", written, len);
170 return -EIO;
171 }
172
173 return 0;
174 }
175
tort_init(void)176 static int __init tort_init(void)
177 {
178 int err = 0, i, infinite = !cycles_count;
179 unsigned char *bad_ebs;
180
181 printk(KERN_INFO "\n");
182 printk(KERN_INFO "=================================================\n");
183 pr_info("Warning: this program is trying to wear out your "
184 "flash, stop it if this is not wanted.\n");
185
186 if (dev < 0) {
187 pr_info("Please specify a valid mtd-device via module parameter\n");
188 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
189 return -EINVAL;
190 }
191
192 pr_info("MTD device: %d\n", dev);
193 pr_info("torture %d eraseblocks (%d-%d) of mtd%d\n",
194 ebcnt, eb, eb + ebcnt - 1, dev);
195 if (pgcnt)
196 pr_info("torturing just %d pages per eraseblock\n",
197 pgcnt);
198 pr_info("write verify %s\n", check ? "enabled" : "disabled");
199
200 mtd = get_mtd_device(NULL, dev);
201 if (IS_ERR(mtd)) {
202 err = PTR_ERR(mtd);
203 pr_err("error: cannot get MTD device\n");
204 return err;
205 }
206
207 if (mtd->writesize == 1) {
208 pr_info("not NAND flash, assume page size is 512 "
209 "bytes.\n");
210 pgsize = 512;
211 } else
212 pgsize = mtd->writesize;
213
214 if (pgcnt && (pgcnt > mtd->erasesize / pgsize || pgcnt < 0)) {
215 pr_err("error: invalid pgcnt value %d\n", pgcnt);
216 goto out_mtd;
217 }
218
219 err = -ENOMEM;
220 patt_random = kmalloc(mtd->erasesize, GFP_KERNEL);
221 if (!patt_random)
222 goto out_mtd;
223
224 patt_5A5 = kmalloc(mtd->erasesize, GFP_KERNEL);
225 if (!patt_5A5)
226 goto out_patt_random;
227
228 patt_A5A = kmalloc(mtd->erasesize, GFP_KERNEL);
229 if (!patt_A5A)
230 goto out_patt_5A5;
231
232 patt_FF = kmalloc(mtd->erasesize, GFP_KERNEL);
233 if (!patt_FF)
234 goto out_patt_A5A;
235
236 check_buf = kmalloc(mtd->erasesize, GFP_KERNEL);
237 if (!check_buf)
238 goto out_patt_FF;
239
240 bad_ebs = kzalloc(ebcnt, GFP_KERNEL);
241 if (!bad_ebs)
242 goto out_check_buf;
243
244 err = 0;
245
246 /* Initialize patterns */
247 memset(patt_FF, 0xFF, mtd->erasesize);
248 for (i = 0; i < mtd->erasesize / pgsize; i++) {
249 if (!(i & 1)) {
250 memset(patt_5A5 + i * pgsize, 0x55, pgsize);
251 memset(patt_A5A + i * pgsize, 0xAA, pgsize);
252 } else {
253 memset(patt_5A5 + i * pgsize, 0xAA, pgsize);
254 memset(patt_A5A + i * pgsize, 0x55, pgsize);
255 }
256 }
257
258 prandom_bytes(patt_random, mtd->erasesize);
259
260 err = mtdtest_scan_for_bad_eraseblocks(mtd, bad_ebs, eb, ebcnt);
261 if (err)
262 goto out;
263
264 start_timing();
265 while (1) {
266 int i;
267 void *patt;
268
269 err = mtdtest_erase_good_eraseblocks(mtd, bad_ebs, eb, ebcnt);
270 if (err)
271 goto out;
272
273 /* Check if the eraseblocks contain only 0xFF bytes */
274 if (check) {
275 for (i = eb; i < eb + ebcnt; i++) {
276 if (bad_ebs[i - eb])
277 continue;
278 err = check_eraseblock(i, patt_FF);
279 if (err) {
280 pr_info("verify failed"
281 " for 0xFF... pattern\n");
282 goto out;
283 }
284
285 err = mtdtest_relax();
286 if (err)
287 goto out;
288 }
289 }
290
291 /* Write the pattern */
292 for (i = eb; i < eb + ebcnt; i++) {
293 if (bad_ebs[i - eb])
294 continue;
295 if ((eb + erase_cycles) & 1)
296 patt = patt_5A5;
297 else
298 patt = patt_A5A;
299 if (random_pattern)
300 patt = patt_random;
301 err = write_pattern(i, patt);
302 if (err)
303 goto out;
304
305 err = mtdtest_relax();
306 if (err)
307 goto out;
308 }
309
310 /* Verify what we wrote */
311 if (check) {
312 for (i = eb; i < eb + ebcnt; i++) {
313 if (bad_ebs[i - eb])
314 continue;
315 if ((eb + erase_cycles) & 1)
316 patt = patt_5A5;
317 else
318 patt = patt_A5A;
319 if (random_pattern)
320 patt = patt_random;
321 err = check_eraseblock(i, patt);
322 if (err) {
323 if (random_pattern) {
324 pr_info("verify failed for random pattern\n");
325 goto out;
326 }
327 pr_info("verify failed for %s"
328 " pattern\n",
329 ((eb + erase_cycles) & 1) ?
330 "0x55AA55..." : "0xAA55AA...");
331 goto out;
332 }
333
334 err = mtdtest_relax();
335 if (err)
336 goto out;
337 }
338 }
339
340 erase_cycles += 1;
341
342 if (erase_cycles % gran == 0) {
343 long ms;
344
345 stop_timing();
346 ms = ktime_ms_delta(finish, start);
347 pr_info("%08u erase cycles done, took %lu "
348 "milliseconds (%lu seconds)\n",
349 erase_cycles, ms, ms / 1000);
350 start_timing();
351 }
352
353 if (!infinite && --cycles_count == 0)
354 break;
355 }
356 out:
357
358 pr_info("finished after %u erase cycles\n",
359 erase_cycles);
360 kfree(bad_ebs);
361 out_check_buf:
362 kfree(check_buf);
363 out_patt_FF:
364 kfree(patt_FF);
365 out_patt_A5A:
366 kfree(patt_A5A);
367 out_patt_5A5:
368 kfree(patt_5A5);
369 out_patt_random:
370 kfree(patt_random);
371 out_mtd:
372 put_mtd_device(mtd);
373 if (err)
374 pr_info("error %d occurred during torturing\n", err);
375 printk(KERN_INFO "=================================================\n");
376 return err;
377 }
378 module_init(tort_init);
379
tort_exit(void)380 static void __exit tort_exit(void)
381 {
382 return;
383 }
384 module_exit(tort_exit);
385
386 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
387 unsigned offset, unsigned len, unsigned *bytesp,
388 unsigned *bitsp);
389 static void print_bufs(unsigned char *read, unsigned char *written, int start,
390 int len);
391
392 /*
393 * Report the detailed information about how the read EB differs from what was
394 * written.
395 */
report_corrupt(unsigned char * read,unsigned char * written)396 static void report_corrupt(unsigned char *read, unsigned char *written)
397 {
398 int i;
399 int bytes, bits, pages, first;
400 int offset, len;
401 size_t check_len = mtd->erasesize;
402
403 if (pgcnt)
404 check_len = pgcnt * pgsize;
405
406 bytes = bits = pages = 0;
407 for (i = 0; i < check_len; i += pgsize)
408 if (countdiffs(written, read, i, pgsize, &bytes,
409 &bits) >= 0)
410 pages++;
411
412 pr_info("verify fails on %d pages, %d bytes/%d bits\n",
413 pages, bytes, bits);
414 pr_info("The following is a list of all differences between"
415 " what was read from flash and what was expected\n");
416
417 for (i = 0; i < check_len; i += pgsize) {
418 cond_resched();
419 bytes = bits = 0;
420 first = countdiffs(written, read, i, pgsize, &bytes,
421 &bits);
422 if (first < 0)
423 continue;
424
425 printk("-------------------------------------------------------"
426 "----------------------------------\n");
427
428 pr_info("Page %zd has %d bytes/%d bits failing verify,"
429 " starting at offset 0x%x\n",
430 (mtd->erasesize - check_len + i) / pgsize,
431 bytes, bits, first);
432
433 offset = first & ~0x7;
434 len = ((first + bytes) | 0x7) + 1 - offset;
435
436 print_bufs(read, written, offset, len);
437 }
438 }
439
print_bufs(unsigned char * read,unsigned char * written,int start,int len)440 static void print_bufs(unsigned char *read, unsigned char *written, int start,
441 int len)
442 {
443 int i = 0, j1, j2;
444 char *diff;
445
446 printk("Offset Read Written\n");
447 while (i < len) {
448 printk("0x%08x: ", start + i);
449 diff = " ";
450 for (j1 = 0; j1 < 8 && i + j1 < len; j1++) {
451 printk(" %02x", read[start + i + j1]);
452 if (read[start + i + j1] != written[start + i + j1])
453 diff = "***";
454 }
455
456 while (j1 < 8) {
457 printk(" ");
458 j1 += 1;
459 }
460
461 printk(" %s ", diff);
462
463 for (j2 = 0; j2 < 8 && i + j2 < len; j2++)
464 printk(" %02x", written[start + i + j2]);
465 printk("\n");
466 i += 8;
467 }
468 }
469
470 /*
471 * Count the number of differing bytes and bits and return the first differing
472 * offset.
473 */
countdiffs(unsigned char * buf,unsigned char * check_buf,unsigned offset,unsigned len,unsigned * bytesp,unsigned * bitsp)474 static int countdiffs(unsigned char *buf, unsigned char *check_buf,
475 unsigned offset, unsigned len, unsigned *bytesp,
476 unsigned *bitsp)
477 {
478 unsigned i, bit;
479 int first = -1;
480
481 for (i = offset; i < offset + len; i++)
482 if (buf[i] != check_buf[i]) {
483 first = i;
484 break;
485 }
486
487 while (i < offset + len) {
488 if (buf[i] != check_buf[i]) {
489 (*bytesp)++;
490 bit = 1;
491 while (bit < 256) {
492 if ((buf[i] & bit) != (check_buf[i] & bit))
493 (*bitsp)++;
494 bit <<= 1;
495 }
496 }
497 i++;
498 }
499
500 return first;
501 }
502
503 MODULE_DESCRIPTION("Eraseblock torturing module");
504 MODULE_AUTHOR("Artem Bityutskiy, Jarkko Lavinen, Adrian Hunter");
505 MODULE_LICENSE("GPL");
506