nandtest.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #define PROGRAM_NAME "nandtest"
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include <sys/stat.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/types.h>
  13. #include <getopt.h>
  14. #include <asm/types.h>
  15. #include "mtd/mtd-user.h"
  16. #include "common.h"
  17. static NORETURN void usage(int status)
  18. {
  19. fprintf(status ? stderr : stdout,
  20. "usage: %s [OPTIONS] <device>\n\n"
  21. " -h, --help Display this help output\n"
  22. " -V, --version Display version information and exit\n"
  23. " -m, --markbad Mark blocks bad if they appear so\n"
  24. " -s, --seed Supply random seed\n"
  25. " -p, --passes Number of passes\n"
  26. " -r <n>, --reads=<n> Read & check <n> times per pass\n"
  27. " -o, --offset Start offset on flash\n"
  28. " -l, --length Length of flash to test\n"
  29. " -k, --keep Restore existing contents after test\n",
  30. PROGRAM_NAME);
  31. exit(status);
  32. }
  33. struct mtd_info_user meminfo;
  34. struct mtd_ecc_stats oldstats, newstats;
  35. int fd;
  36. int markbad=0;
  37. int seed;
  38. static int read_and_compare(loff_t ofs, unsigned char *data,
  39. unsigned char *rbuf)
  40. {
  41. ssize_t len;
  42. int i;
  43. len = pread(fd, rbuf, meminfo.erasesize, ofs);
  44. if (len < meminfo.erasesize) {
  45. printf("\n");
  46. if (len)
  47. fprintf(stderr, "Short read (%zd bytes)\n", len);
  48. else
  49. perror("read");
  50. exit(1);
  51. }
  52. if (ioctl(fd, ECCGETSTATS, &newstats)) {
  53. printf("\n");
  54. perror("ECCGETSTATS");
  55. close(fd);
  56. exit(1);
  57. }
  58. if (newstats.corrected > oldstats.corrected) {
  59. printf("\n %d bit(s) ECC corrected at %08x\n",
  60. newstats.corrected - oldstats.corrected,
  61. (unsigned) ofs);
  62. oldstats.corrected = newstats.corrected;
  63. }
  64. if (newstats.failed > oldstats.failed) {
  65. printf("\nECC failed at %08x\n", (unsigned) ofs);
  66. oldstats.failed = newstats.failed;
  67. }
  68. printf("\r%08x: checking...", (unsigned)ofs);
  69. fflush(stdout);
  70. if (memcmp(data, rbuf, meminfo.erasesize)) {
  71. printf("\n");
  72. fprintf(stderr, "compare failed. seed %d\n", seed);
  73. for (i=0; i<meminfo.erasesize; i++) {
  74. if (data[i] != rbuf[i])
  75. printf("Byte 0x%x is %02x should be %02x\n",
  76. i, rbuf[i], data[i]);
  77. }
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static int erase_and_write(loff_t ofs, unsigned char *data, unsigned char *rbuf,
  83. int nr_reads)
  84. {
  85. struct erase_info_user er;
  86. ssize_t len;
  87. int i, read_errs = 0;
  88. printf("\r%08x: erasing... ", (unsigned)ofs);
  89. fflush(stdout);
  90. er.start = ofs;
  91. er.length = meminfo.erasesize;
  92. if (ioctl(fd, MEMERASE, &er)) {
  93. perror("MEMERASE");
  94. if (markbad) {
  95. printf("Mark block bad at %08lx\n", (long)ofs);
  96. ioctl(fd, MEMSETBADBLOCK, &ofs);
  97. }
  98. return 1;
  99. }
  100. printf("\r%08x: writing...", (unsigned)ofs);
  101. fflush(stdout);
  102. len = pwrite(fd, data, meminfo.erasesize, ofs);
  103. if (len < 0) {
  104. printf("\n");
  105. perror("write");
  106. if (markbad) {
  107. printf("Mark block bad at %08lx\n", (long)ofs);
  108. ioctl(fd, MEMSETBADBLOCK, &ofs);
  109. }
  110. return 1;
  111. }
  112. if (len < meminfo.erasesize) {
  113. printf("\n");
  114. fprintf(stderr, "Short write (%zd bytes)\n", len);
  115. exit(1);
  116. }
  117. for (i=1; i<=nr_reads; i++) {
  118. printf("\r%08x: reading (%d of %d)...", (unsigned)ofs, i, nr_reads);
  119. fflush(stdout);
  120. if (read_and_compare(ofs, data, rbuf))
  121. read_errs++;
  122. }
  123. if (read_errs) {
  124. fprintf(stderr, "read/check %d of %d failed. seed %d\n", read_errs, nr_reads, seed);
  125. return 1;
  126. }
  127. return 0;
  128. }
  129. /*
  130. * Main program
  131. */
  132. int main(int argc, char **argv)
  133. {
  134. int i;
  135. unsigned char *wbuf, *rbuf, *kbuf;
  136. int pass;
  137. int nr_passes = 1;
  138. int nr_reads = 4;
  139. int keep_contents = 0;
  140. uint32_t offset = 0;
  141. uint32_t length = -1;
  142. int error = 0;
  143. seed = time(NULL);
  144. for (;;) {
  145. static const char short_options[] = "hkl:mo:p:r:s:V";
  146. static const struct option long_options[] = {
  147. { "help", no_argument, 0, 'h' },
  148. { "version", no_argument, 0, 'V' },
  149. { "markbad", no_argument, 0, 'm' },
  150. { "seed", required_argument, 0, 's' },
  151. { "passes", required_argument, 0, 'p' },
  152. { "offset", required_argument, 0, 'o' },
  153. { "length", required_argument, 0, 'l' },
  154. { "reads", required_argument, 0, 'r' },
  155. { "keep", no_argument, 0, 'k' },
  156. {0, 0, 0, 0},
  157. };
  158. int option_index = 0;
  159. int c = getopt_long(argc, argv, short_options, long_options, &option_index);
  160. if (c == EOF)
  161. break;
  162. switch (c) {
  163. case 'h':
  164. usage(EXIT_SUCCESS);
  165. case 'V':
  166. common_print_version();
  167. exit(EXIT_SUCCESS);
  168. break;
  169. case '?':
  170. usage(EXIT_FAILURE);
  171. case 'm':
  172. markbad = 1;
  173. break;
  174. case 'k':
  175. keep_contents = 1;
  176. break;
  177. case 's':
  178. seed = atol(optarg);
  179. break;
  180. case 'p':
  181. nr_passes = atol(optarg);
  182. break;
  183. case 'r':
  184. nr_reads = atol(optarg);
  185. break;
  186. case 'o':
  187. offset = simple_strtoul(optarg, &error);
  188. break;
  189. case 'l':
  190. length = simple_strtoul(optarg, &error);
  191. break;
  192. }
  193. }
  194. if (argc - optind != 1)
  195. usage(EXIT_FAILURE);
  196. if (error)
  197. errmsg_die("Try --help for more information");
  198. fd = open(argv[optind], O_RDWR);
  199. if (fd < 0) {
  200. perror("open");
  201. exit(1);
  202. }
  203. if (ioctl(fd, MEMGETINFO, &meminfo)) {
  204. perror("MEMGETINFO");
  205. close(fd);
  206. exit(1);
  207. }
  208. if (length == -1)
  209. length = meminfo.size;
  210. if (offset % meminfo.erasesize) {
  211. fprintf(stderr, "Offset %x not multiple of erase size %x\n",
  212. offset, meminfo.erasesize);
  213. exit(1);
  214. }
  215. if (length % meminfo.erasesize) {
  216. fprintf(stderr, "Length %x not multiple of erase size %x\n",
  217. length, meminfo.erasesize);
  218. exit(1);
  219. }
  220. if (length + offset > meminfo.size) {
  221. fprintf(stderr, "Length %x + offset %x exceeds device size %x\n",
  222. length, offset, meminfo.size);
  223. exit(1);
  224. }
  225. wbuf = malloc(meminfo.erasesize * 3);
  226. if (!wbuf) {
  227. fprintf(stderr, "Could not allocate %d bytes for buffer\n",
  228. meminfo.erasesize * 2);
  229. exit(1);
  230. }
  231. rbuf = wbuf + meminfo.erasesize;
  232. kbuf = rbuf + meminfo.erasesize;
  233. if (ioctl(fd, ECCGETSTATS, &oldstats)) {
  234. perror("ECCGETSTATS");
  235. close(fd);
  236. exit(1);
  237. }
  238. printf("ECC corrections: %d\n", oldstats.corrected);
  239. printf("ECC failures : %d\n", oldstats.failed);
  240. printf("Bad blocks : %d\n", oldstats.badblocks);
  241. printf("BBT blocks : %d\n", oldstats.bbtblocks);
  242. srand(seed);
  243. for (pass = 0; pass < nr_passes; pass++) {
  244. loff_t test_ofs;
  245. for (test_ofs = offset; test_ofs < offset+length; test_ofs += meminfo.erasesize) {
  246. ssize_t len;
  247. seed = rand();
  248. srand(seed);
  249. if (ioctl(fd, MEMGETBADBLOCK, &test_ofs)) {
  250. printf("\rBad block at 0x%08x\n", (unsigned)test_ofs);
  251. continue;
  252. }
  253. for (i=0; i<meminfo.erasesize; i++)
  254. wbuf[i] = rand();
  255. if (keep_contents) {
  256. printf("\r%08x: reading... ", (unsigned)test_ofs);
  257. fflush(stdout);
  258. len = pread(fd, kbuf, meminfo.erasesize, test_ofs);
  259. if (len < meminfo.erasesize) {
  260. printf("\n");
  261. if (len)
  262. fprintf(stderr, "Short read (%zd bytes)\n", len);
  263. else
  264. perror("read");
  265. exit(1);
  266. }
  267. }
  268. if (erase_and_write(test_ofs, wbuf, rbuf, nr_reads))
  269. continue;
  270. if (keep_contents)
  271. erase_and_write(test_ofs, kbuf, rbuf, 1);
  272. }
  273. printf("\nFinished pass %d successfully\n", pass+1);
  274. }
  275. /* Return happy */
  276. return 0;
  277. }