nandbiterrs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright © 2012 NetCommWireless
  3. * Iwo Mergler <Iwo.Mergler@netcommwireless.com.au>
  4. *
  5. * Copyright © 2015 sigma star gmbh
  6. * David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  7. *
  8. * Test for multi-bit error recovery on a NAND page. This mostly tests the
  9. * ECC controller / driver.
  10. *
  11. * There are two test modes:
  12. *
  13. * 0 - artificially inserting bit errors until the ECC fails
  14. * This is the default method and fairly quick. It should
  15. * be independent of the quality of the FLASH.
  16. *
  17. * 1 - re-writing the same pattern repeatedly until the ECC fails.
  18. * This method relies on the physics of NAND FLASH to eventually
  19. * generate '0' bits if '1' has been written sufficient times.
  20. * Depending on the NAND, the first bit errors will appear after
  21. * 1000 or more writes and then will usually snowball, reaching the
  22. * limits of the ECC quickly.
  23. *
  24. * The test stops after 10000 cycles, should your FLASH be
  25. * exceptionally good and not generate bit errors before that. Try
  26. * a different page in that case.
  27. *
  28. * Please note that neither of these tests will significantly 'use up' any
  29. * FLASH endurance. Only a maximum of two erase operations will be performed.
  30. *
  31. *
  32. * This program is free software; you can redistribute it and/or modify it
  33. * under the terms of the GNU General Public License version 2 as published by
  34. * the Free Software Foundation.
  35. *
  36. * This program is distributed in the hope that it will be useful, but WITHOUT
  37. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  38. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  39. * more details.
  40. *
  41. * You should have received a copy of the GNU General Public License along with
  42. * this program; see the file COPYING. If not, write to the Free Software
  43. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  44. */
  45. #define PROGRAM_NAME "nandbiterrs"
  46. #include <mtd/mtd-user.h>
  47. #include <sys/ioctl.h>
  48. #include <unistd.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <libmtd.h>
  52. #include <getopt.h>
  53. #include <stdio.h>
  54. #include <fcntl.h>
  55. #include "common.h"
  56. /* We don't expect more than this many correctable bit errors per page. */
  57. #define MAXBITS 512
  58. #define KEEP_CONTENTS 0x01
  59. #define MODE_INCREMENTAL 0x02
  60. #define MODE_OVERWRITE 0x04
  61. #define PAGE_ERASED 0x08
  62. static int peb = -1, page = -1, max_overwrite = -1, seed = -1;
  63. static const char *mtddev;
  64. static unsigned char *wbuffer, *rbuffer, *old_data;
  65. static int fd, pagesize, pagecount, flags;
  66. static struct mtd_dev_info mtd;
  67. static libmtd_t mtd_desc;
  68. static const struct option options[] = {
  69. { "help", no_argument, NULL, 'h' },
  70. { "keep", no_argument, NULL, 'k' },
  71. { "peb", required_argument, NULL, 'b' },
  72. { "page", required_argument, NULL, 'p' },
  73. { "seed", required_argument, NULL, 's' },
  74. { "erased", no_argument, NULL, 'e' },
  75. { "writes", required_argument, NULL, 'w' },
  76. { "incremental", no_argument, NULL, 'i' },
  77. { "overwrite", no_argument, NULL, 'o' },
  78. { NULL, 0, NULL, 0 },
  79. };
  80. static NORETURN void usage(int status)
  81. {
  82. fputs(
  83. "Usage: "PROGRAM_NAME" [OPTIONS] <device>\n\n"
  84. "Common options:\n"
  85. " -h, --help Display this help output\n"
  86. " -k, --keep Restore existing contents after test\n"
  87. " -b, --peb <num> Use this physical erase block\n"
  88. " -p, --page <num> Use this page within the erase block\n"
  89. " -s, --seed <num> Specify seed for PRNG\n"
  90. " -e, --erased Test erased pages instead of written pages\n\n"
  91. "Options controling test mode:\n"
  92. " -i, --incremental Manually insert bit errors until ECC fails\n"
  93. " -o, --overwrite Rewrite page until bits flip and ECC fails\n\n"
  94. "Test mode specific options:\n"
  95. " -w, --writes <num> Number of writes (default 10000)\n",
  96. status==EXIT_SUCCESS ? stdout : stderr);
  97. exit(status);
  98. }
  99. static long read_num(int opt, const char *arg)
  100. {
  101. char *end;
  102. long num;
  103. num = strtol(arg, &end, 0);
  104. if (!end || *end != '\0') {
  105. fprintf(stderr, "-%c: expected integer argument\n", opt);
  106. exit(EXIT_FAILURE);
  107. }
  108. return num;
  109. }
  110. static void process_options(int argc, char **argv)
  111. {
  112. int c;
  113. while (1) {
  114. c = getopt_long(argc, argv, "hkb:p:s:eiow:", options, NULL);
  115. if (c == -1)
  116. break;
  117. switch (c) {
  118. case 'k':
  119. if (flags & KEEP_CONTENTS)
  120. goto failmulti;
  121. flags |= KEEP_CONTENTS;
  122. break;
  123. case 'b':
  124. if (peb >= 0)
  125. goto failmulti;
  126. peb = read_num(c, optarg);
  127. if (peb < 0)
  128. goto failarg;
  129. break;
  130. case 'i':
  131. if (flags & (MODE_INCREMENTAL|MODE_OVERWRITE))
  132. goto failmultimode;
  133. flags |= MODE_INCREMENTAL;
  134. break;
  135. case 'o':
  136. if (flags & (MODE_INCREMENTAL|MODE_OVERWRITE))
  137. goto failmultimode;
  138. flags |= MODE_OVERWRITE;
  139. break;
  140. case 'w':
  141. if (max_overwrite > 0)
  142. goto failmulti;
  143. max_overwrite = read_num(c, optarg);
  144. if (max_overwrite <= 0)
  145. goto failarg;
  146. break;
  147. case 's':
  148. if (seed >= 0)
  149. goto failmulti;
  150. seed = read_num(c, optarg);
  151. if (seed < 0)
  152. goto failarg;
  153. break;
  154. case 'p':
  155. if (page > 0)
  156. goto failmulti;
  157. page = read_num(c, optarg);
  158. if (page < 0)
  159. goto failarg;
  160. break;
  161. case 'e':
  162. flags |= PAGE_ERASED;
  163. break;
  164. case 'h':
  165. usage(EXIT_SUCCESS);
  166. default:
  167. exit(EXIT_FAILURE);
  168. }
  169. }
  170. if (optind < argc)
  171. mtddev = argv[optind++];
  172. else
  173. errmsg_die("No device specified!\n");
  174. if (optind < argc)
  175. usage(EXIT_FAILURE);
  176. if (!(flags & (MODE_OVERWRITE|MODE_INCREMENTAL)))
  177. errmsg_die("No test mode specified!");
  178. if ((max_overwrite > 0) && !(flags & MODE_OVERWRITE))
  179. errmsg_die("Write count specified but mode is not --overwrite!");
  180. if (max_overwrite < 0)
  181. max_overwrite = 10000;
  182. if (peb < 0)
  183. peb = 0;
  184. if (page < 0)
  185. page = 0;
  186. if (seed < 0)
  187. seed = 0;
  188. return;
  189. failmultimode:
  190. errmsg_die("Test mode specified more than once!");
  191. failmulti:
  192. errmsg_die("'-%c' specified more than once!", c);
  193. failarg:
  194. errmsg_die("Invalid argument for '-%c'!", c);
  195. }
  196. /* 'random' bytes from known offsets */
  197. static unsigned char hash(unsigned int offset)
  198. {
  199. unsigned int v = offset;
  200. unsigned char c;
  201. v ^= 0x7f7edfd3;
  202. v = v ^ (v >> 3);
  203. v = v ^ (v >> 5);
  204. v = v ^ (v >> 13);
  205. c = v & 0xFF;
  206. /* Reverse bits of result. */
  207. c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
  208. c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
  209. c = (c & 0x55) << 1 | (c & 0xAA) >> 1;
  210. return c;
  211. }
  212. static void init_buffer(void)
  213. {
  214. unsigned int i;
  215. if (flags & PAGE_ERASED) {
  216. memset(wbuffer, 0xff, pagesize);
  217. } else {
  218. for (i = 0; i < pagesize; ++i)
  219. wbuffer[i] = hash(i+seed);
  220. }
  221. }
  222. static int write_page(void)
  223. {
  224. int raw = flags & PAGE_ERASED;
  225. int err;
  226. if (raw && ioctl(fd, MTDFILEMODE, MTD_FILE_MODE_RAW) != 0)
  227. goto fail_mode;
  228. err = mtd_write(mtd_desc, &mtd, fd, peb, page*pagesize,
  229. wbuffer, pagesize, NULL, 0, 0);
  230. if (err)
  231. fprintf(stderr, "Failed to write page %d in block %d\n", peb, page);
  232. if (raw && ioctl(fd, MTDFILEMODE, MTD_FILE_MODE_NORMAL) != 0)
  233. goto fail_mode;
  234. return err;
  235. fail_mode:
  236. perror("MTDFILEMODE");
  237. return -1;
  238. }
  239. static int rewrite_page(void)
  240. {
  241. if (ioctl(fd, MTDFILEMODE, MTD_FILE_MODE_RAW) != 0)
  242. goto fail_mode;
  243. if (write_page() != 0)
  244. return -1;
  245. if (ioctl(fd, MTDFILEMODE, MTD_FILE_MODE_NORMAL) != 0)
  246. goto fail_mode;
  247. return 0;
  248. fail_mode:
  249. perror("MTDFILEMODE");
  250. return -1;
  251. }
  252. static int read_page(void)
  253. {
  254. struct mtd_ecc_stats old, new;
  255. int err = 0;
  256. if (ioctl(fd, ECCGETSTATS, &old) != 0)
  257. goto failstats;
  258. err = mtd_read(&mtd, fd, peb, page*pagesize, rbuffer, pagesize);
  259. if (err) {
  260. fputs("Read failed!\n", stderr);
  261. return -1;
  262. }
  263. if (ioctl(fd, ECCGETSTATS, &new) != 0)
  264. goto failstats;
  265. if (new.failed > old.failed) {
  266. fprintf(stderr, "Failed to recover %d bitflips\n",
  267. new.failed - old.failed);
  268. return -1;
  269. }
  270. return new.corrected - old.corrected;
  271. failstats:
  272. perror("ECCGETSTATS");
  273. return -1;
  274. }
  275. static int verify_page(void)
  276. {
  277. int erased = flags & PAGE_ERASED;
  278. unsigned int i, errs = 0;
  279. for (i = 0; i < pagesize; ++i) {
  280. if (rbuffer[i] != (erased ? 0xff : hash(i+seed)))
  281. ++errs;
  282. }
  283. if (errs)
  284. fputs("ECC failure, invalid data despite read success\n", stderr);
  285. return errs;
  286. }
  287. /* Finds the first '1' bit in wbuffer and sets it to '0'. */
  288. static int insert_biterror(void)
  289. {
  290. int bit, mask, byte;
  291. for (byte = 0; byte < pagesize; ++byte) {
  292. for (bit = 7, mask = 0x80; bit >= 0; bit--, mask >>= 1) {
  293. if (wbuffer[byte] & mask) {
  294. wbuffer[byte] &= ~mask;
  295. printf("Inserted biterror @ %u/%u\n", byte, bit);
  296. return 0;
  297. }
  298. }
  299. }
  300. fputs("biterror: Failed to find a '1' bit\n", stderr);
  301. return -1;
  302. }
  303. /* Writes 'random' data to page and then introduces deliberate bit
  304. * errors into the page, while verifying each step. */
  305. static int incremental_errors_test(void)
  306. {
  307. unsigned int errs_per_subpage = 0;
  308. int count = 0;
  309. puts("incremental biterrors test");
  310. init_buffer();
  311. if (write_page() != 0)
  312. return -1;
  313. for (errs_per_subpage = 0; ; ++errs_per_subpage) {
  314. if (rewrite_page() != 0)
  315. return -1;
  316. count = read_page();
  317. if (count > 0)
  318. printf("Read reported %d corrected bit errors\n", count);
  319. if (count < 0) {
  320. fprintf(stderr, "Read error after %d bit errors per page\n",
  321. errs_per_subpage);
  322. return 0;
  323. }
  324. if (verify_page() != 0)
  325. return -1;
  326. printf("Successfully corrected %d bit errors per subpage\n",
  327. errs_per_subpage);
  328. if (insert_biterror() != 0)
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. /* Writes 'random' data to page and then re-writes that same data repeatedly.
  334. This eventually develops bit errors (bits written as '1' will slowly become
  335. '0'), which are corrected as far as the ECC is capable of. */
  336. static int overwrite_test(void)
  337. {
  338. unsigned int i, max_corrected = 0, opno;
  339. unsigned int bitstats[MAXBITS]; /* bit error histogram. */
  340. int err = 0;
  341. memset(bitstats, 0, sizeof(bitstats));
  342. puts("overwrite biterrors test");
  343. init_buffer();
  344. if (write_page() != 0)
  345. return -1;
  346. for (opno = 0; opno < max_overwrite; ++opno) {
  347. err = write_page();
  348. if (err)
  349. break;
  350. err = read_page();
  351. if (err >= 0) {
  352. if (err >= MAXBITS) {
  353. puts("Implausible number of bit errors corrected");
  354. err = -1;
  355. break;
  356. }
  357. bitstats[err]++;
  358. if (err > max_corrected) {
  359. max_corrected = err;
  360. printf("Read reported %d corrected bit errors\n", err);
  361. }
  362. } else {
  363. err = 0;
  364. break;
  365. }
  366. err = verify_page();
  367. if (err) {
  368. bitstats[max_corrected] = opno;
  369. break;
  370. }
  371. }
  372. /* At this point bitstats[0] contains the number of ops with no bit
  373. * errors, bitstats[1] the number of ops with 1 bit error, etc. */
  374. printf("Bit error histogram (%d operations total):\n", opno);
  375. for (i = 0; i < max_corrected; ++i) {
  376. printf("Page reads with %3d corrected bit errors: %d\n",
  377. i, bitstats[i]);
  378. }
  379. return err;
  380. }
  381. int main(int argc, char **argv)
  382. {
  383. int err = 0, status = EXIT_FAILURE;
  384. process_options(argc, argv);
  385. mtd_desc = libmtd_open();
  386. if (!mtd_desc)
  387. return errmsg("can't initialize libmtd");
  388. if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0)
  389. return errmsg("mtd_get_dev_info failed");
  390. if (mtd.type!=MTD_MLCNANDFLASH && mtd.type!=MTD_NANDFLASH)
  391. return errmsg("%s is not a NAND flash!", mtddev);
  392. pagesize = mtd.subpage_size;
  393. pagecount = mtd.eb_size / pagesize;
  394. if (peb >= mtd.eb_cnt)
  395. return errmsg("Physical erase block %d is out of range!", peb);
  396. if (page >= pagecount)
  397. return errmsg("Page number %d is out of range!", page);
  398. if ((fd = open(mtddev, O_RDWR)) == -1) {
  399. perror(mtddev);
  400. return EXIT_FAILURE;
  401. }
  402. if (flags & KEEP_CONTENTS) {
  403. old_data = malloc(mtd.eb_size);
  404. if (!old_data) {
  405. perror(NULL);
  406. goto fail_dev;
  407. }
  408. if (mtd_read(&mtd, fd, peb, 0, old_data, mtd.eb_size)) {
  409. fprintf(stderr, "Reading erase block %d failed!\n", peb);
  410. goto fail_dev;
  411. }
  412. }
  413. wbuffer = malloc(pagesize);
  414. if (!wbuffer) {
  415. perror(NULL);
  416. goto fail_dev;
  417. }
  418. rbuffer = malloc(pagesize);
  419. if (!rbuffer) {
  420. perror(NULL);
  421. goto fail_rbuffer;
  422. }
  423. if (mtd_erase(mtd_desc, &mtd, fd, peb)) {
  424. fprintf(stderr, "Cannot erase block %d\n", peb);
  425. goto fail_test;
  426. }
  427. if (flags & MODE_INCREMENTAL)
  428. err = incremental_errors_test();
  429. else if (flags & MODE_OVERWRITE)
  430. err = overwrite_test();
  431. status = err ? EXIT_FAILURE : EXIT_SUCCESS;
  432. if (flags & KEEP_CONTENTS) {
  433. if (mtd_erase(mtd_desc, &mtd, fd, peb)) {
  434. fprintf(stderr, "Restoring: Cannot erase block %d\n", peb);
  435. status = EXIT_FAILURE;
  436. goto fail_test;
  437. }
  438. err = mtd_write(mtd_desc, &mtd, fd, peb, 0,
  439. old_data, mtd.eb_size, NULL, 0, 0);
  440. if (err) {
  441. fputs("Failed restoring old block contents!\n", stderr);
  442. status = EXIT_FAILURE;
  443. }
  444. } else {
  445. /* We leave the block un-erased in case of test failure. */
  446. if (err)
  447. goto fail_test;
  448. if (mtd_erase(mtd_desc, &mtd, fd, peb)) {
  449. fprintf(stderr, "Cannot erase block %d\n", peb);
  450. status = EXIT_FAILURE;
  451. }
  452. }
  453. fail_test:
  454. free(rbuffer);
  455. fail_rbuffer:
  456. free(wbuffer);
  457. fail_dev:
  458. close(fd);
  459. free(old_data);
  460. return status;
  461. }