nandsubpagetest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright (C) 2006-2007 Nokia Corporation
  3. * Copyright (C) 2016 sigma star gmbh
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; see the file COPYING. If not, write to the Free Software
  16. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Test sub-page read and write on MTD device.
  19. *
  20. * Author: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  21. *
  22. * Based on linux subpagetest.c
  23. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  24. */
  25. #define PROGRAM_NAME "nandsubpagetest"
  26. #include <mtd/mtd-user.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <libmtd.h>
  30. #include <getopt.h>
  31. #include <stdio.h>
  32. #include <fcntl.h>
  33. #include <time.h>
  34. #include "common.h"
  35. #define KEEP_CONTENTS 0x01
  36. #define SEED_SET 0x02
  37. static struct mtd_dev_info mtd;
  38. static const char *mtddev;
  39. static libmtd_t mtd_desc;
  40. static unsigned char *bbt=NULL, *writebuf=NULL, *backup=NULL, *readbuf=NULL;
  41. static int peb = -1, seed = -1, skip = -1, ebcnt = -1, flags = 0;
  42. static int fd, bufsize;
  43. static unsigned int rnd_state;
  44. static const struct option options[] = {
  45. { "help", no_argument, NULL, 'h' },
  46. { "keep", no_argument, NULL, 'k' },
  47. { "peb", required_argument, NULL, 'b' },
  48. { "count", required_argument, NULL, 'c' },
  49. { "skip", required_argument, NULL, 's' },
  50. { "seed", required_argument, NULL, 'S' },
  51. { NULL, 0, NULL, 0 },
  52. };
  53. static NORETURN void usage(int status)
  54. {
  55. fputs(
  56. "Usage: "PROGRAM_NAME" [OPTIONS] <device>\n\n"
  57. "Options:\n"
  58. " -h, --help Display this help output\n"
  59. " -b, --peb <num> Index of the first erase block to use\n"
  60. " -c, --count <num> Number of erase blocks to use (default all)\n"
  61. " -s, --skip <num> Number of erase blocks to skip\n"
  62. " -S, --seed <num> Seed for pseudor random number generator\n"
  63. " -k, --keep Restore existing contents after test\n",
  64. status==EXIT_SUCCESS ? stdout : stderr);
  65. exit(status);
  66. }
  67. static long read_num(int opt, const char *arg)
  68. {
  69. char *end;
  70. long num;
  71. num = strtol(arg, &end, 0);
  72. if (!end || *end != '\0') {
  73. fprintf(stderr, "-%c: expected integer argument\n", opt);
  74. exit(EXIT_FAILURE);
  75. }
  76. return num;
  77. }
  78. static void process_options(int argc, char **argv)
  79. {
  80. int c;
  81. while (1) {
  82. c = getopt_long(argc, argv, "hb:c:s:Sk", options, NULL);
  83. if (c == -1)
  84. break;
  85. switch (c) {
  86. case 'b':
  87. if (peb >= 0)
  88. goto failmulti;
  89. peb = read_num(c, optarg);
  90. if (peb < 0)
  91. goto failarg;
  92. break;
  93. case 'c':
  94. if (ebcnt >= 0)
  95. goto failmulti;
  96. ebcnt = read_num(c, optarg);
  97. if (ebcnt < 0)
  98. goto failarg;
  99. break;
  100. case 's':
  101. if (skip >= 0)
  102. goto failmulti;
  103. skip = read_num(c, optarg);
  104. if (skip < 0)
  105. goto failarg;
  106. break;
  107. case 'S':
  108. if (flags & SEED_SET)
  109. goto failmulti;
  110. seed = read_num(c, optarg);
  111. flags |= SEED_SET;
  112. break;
  113. case 'k':
  114. if (flags & KEEP_CONTENTS)
  115. goto failmulti;
  116. flags |= KEEP_CONTENTS;
  117. break;
  118. case 'h':
  119. usage(EXIT_SUCCESS);
  120. default:
  121. exit(EXIT_FAILURE);
  122. }
  123. }
  124. if (optind < argc)
  125. mtddev = argv[optind++];
  126. else
  127. errmsg_die("No device specified!\n");
  128. if (optind < argc)
  129. usage(EXIT_FAILURE);
  130. if (peb < 0)
  131. peb = 0;
  132. if (!(flags & SEED_SET))
  133. seed = time(NULL);
  134. if (skip < 0)
  135. skip = 0;
  136. return;
  137. failmulti:
  138. errmsg_die("'-%c' specified more than once!", c);
  139. failarg:
  140. errmsg_die("Invalid argument for '-%c'!", c);
  141. }
  142. static int write_eraseblock(int ebnum)
  143. {
  144. int i, j, err, off = 0;
  145. printf("writing first 2 sub-pages on PEB %d\n", ebnum);
  146. for (j = 0; j < 2; ++j) {
  147. for (i = 0; i < mtd.subpage_size; ++i)
  148. writebuf[i] = rand_r(&rnd_state);
  149. err = mtd_write(mtd_desc, &mtd, fd, ebnum, off, writebuf,
  150. mtd.subpage_size, NULL, 0, 0);
  151. if (err)
  152. return -1;
  153. off += mtd.subpage_size;
  154. }
  155. return 0;
  156. }
  157. static int write_eraseblock2(int ebnum)
  158. {
  159. int err, off = 0, i, k;
  160. printf("writing with exponential offsets & sizes on PEB %d\n", ebnum);
  161. for (k = 1; k < 33; ++k) {
  162. if (off + (mtd.subpage_size * k) > mtd.eb_size)
  163. break;
  164. for (i = 0; i < (mtd.subpage_size * k); ++i)
  165. writebuf[i] = rand_r(&rnd_state);
  166. err = mtd_write(mtd_desc, &mtd, fd, ebnum, off,
  167. writebuf, mtd.subpage_size * k, NULL, 0, 0);
  168. if (err)
  169. return -1;
  170. off += mtd.subpage_size * k;
  171. }
  172. return 0;
  173. }
  174. static void print_subpage(unsigned char *p)
  175. {
  176. int i, j;
  177. for (i = 0; i < mtd.subpage_size; ) {
  178. for (j = 0; i < mtd.subpage_size && j < 32; ++i, ++j)
  179. fprintf(stderr, "%02x", *p++);
  180. fprintf(stderr, "\n");
  181. }
  182. }
  183. static int verify_eraseblock(int ebnum)
  184. {
  185. int i, j, ret = 0, off = 0;
  186. printf("verifying first 2 sub-pages of PEB %d\n", ebnum);
  187. for (j = 0; j < 2; ++j) {
  188. for (i = 0; i < mtd.subpage_size; ++i)
  189. writebuf[i] = rand_r(&rnd_state);
  190. memset(readbuf, 0, mtd.subpage_size);
  191. if (mtd_read(&mtd, fd, ebnum, off, readbuf, mtd.subpage_size))
  192. return -1;
  193. if (memcmp(readbuf, writebuf, mtd.subpage_size)) {
  194. fprintf(stderr, "error: verify failed at PEB %d, offset %#x\n",
  195. ebnum, off);
  196. fputs("------------- written----------------\n", stderr);
  197. print_subpage(writebuf);
  198. fputs("------------- read ------------------\n", stderr);
  199. print_subpage(readbuf);
  200. fputs("-------------------------------------\n", stderr);
  201. ret = -1;
  202. }
  203. off += mtd.subpage_size;
  204. }
  205. return ret;
  206. }
  207. static int verify_eraseblock2(int ebnum)
  208. {
  209. int ret = 0, i, k, off = 0;
  210. printf("verifying exponential offset & size writes on PEB %d\n", ebnum);
  211. for (k = 1; k < 33; ++k) {
  212. if (off + (mtd.subpage_size * k) > mtd.eb_size)
  213. break;
  214. for (i = 0; i < (mtd.subpage_size * k); ++i)
  215. writebuf[i] = rand_r(&rnd_state);
  216. memset(readbuf, 0, mtd.subpage_size * k);
  217. if (mtd_read(&mtd, fd, ebnum, off, readbuf, mtd.subpage_size * k))
  218. return -1;
  219. if (memcmp(readbuf, writebuf, mtd.subpage_size * k)) {
  220. fprintf(stderr, "error: verify failed at PEB %d, offset %#x\n",
  221. ebnum, off);
  222. ret = -1;
  223. }
  224. off += mtd.subpage_size * k;
  225. }
  226. return ret;
  227. }
  228. static int verify_eraseblock_ff(int ebnum)
  229. {
  230. int j, ret = 0, off = 0;
  231. memset(writebuf, 0xFF, mtd.subpage_size);
  232. for (j = 0; j < mtd.eb_size / mtd.subpage_size; ++j) {
  233. memset(readbuf, 0, mtd.subpage_size);
  234. if (mtd_read(&mtd, fd, ebnum, off, readbuf, mtd.subpage_size))
  235. return -1;
  236. if (memcmp(readbuf, writebuf, mtd.subpage_size)) {
  237. fprintf(stderr, "error: verify 0xff failed at PEB %d, "
  238. "offset %#x\n", ebnum, off);
  239. ret = -1;
  240. }
  241. off += mtd.subpage_size;
  242. }
  243. return ret;
  244. }
  245. static int verify_all_eraseblocks_ff(void)
  246. {
  247. int i, eb, err;
  248. puts("verifying all eraseblocks for 0xff");
  249. for (i = 0; i < ebcnt; ++i) {
  250. if (bbt[i])
  251. continue;
  252. eb = peb + i * (skip + 1);
  253. err = verify_eraseblock_ff(eb);
  254. if (err)
  255. return err;
  256. }
  257. printf("verified %d eraseblocks\n", ebcnt);
  258. return 0;
  259. }
  260. static int erase_good_eraseblocks(void)
  261. {
  262. int i, eb;
  263. printf("erasing good eraseblocks\n");
  264. for (i = 0; i < ebcnt; ++i) {
  265. if (bbt[i])
  266. continue;
  267. eb = peb + i * (skip + 1);
  268. if (mtd_erase(mtd_desc, &mtd, fd, eb))
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. static int remove_test_data(void)
  274. {
  275. if (erase_good_eraseblocks())
  276. return -1;
  277. if (verify_all_eraseblocks_ff())
  278. return -1;
  279. return 0;
  280. }
  281. int main(int argc, char **argv)
  282. {
  283. int i, eb, err = 0, status = EXIT_FAILURE;
  284. unsigned char *backupptr;
  285. process_options(argc, argv);
  286. mtd_desc = libmtd_open();
  287. if (!mtd_desc)
  288. return errmsg("can't initialize libmtd");
  289. if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0)
  290. return errmsg("mtd_get_dev_info failed");
  291. if (mtd.type!=MTD_MLCNANDFLASH && mtd.type!=MTD_NANDFLASH)
  292. return errmsg("%s is not a NAND flash!", mtddev);
  293. if (ebcnt < 0)
  294. ebcnt = (mtd.eb_cnt - peb) / (skip + 1);
  295. if (peb >= mtd.eb_cnt)
  296. return errmsg("physical erase block %d is out of range!", peb);
  297. eb = peb + (ebcnt - 1)*(skip + 1);
  298. if (eb >= mtd.eb_cnt)
  299. return errmsg("last physical erase block %d is out of range!", eb);
  300. bufsize = mtd.subpage_size * 32;
  301. writebuf = xmalloc(bufsize);
  302. readbuf = xmalloc(bufsize);
  303. bbt = xzalloc(ebcnt);
  304. if ((fd = open(mtddev, O_RDWR)) == -1) {
  305. perror(mtddev);
  306. goto out_cleanup;
  307. }
  308. /* find bad blocks */
  309. for (i = 0; i < ebcnt; ++i) {
  310. eb = peb + i * (skip + 1);
  311. bbt[i] = mtd_is_bad(&mtd, fd, eb);
  312. if (bbt[i])
  313. printf("ignoring bad erase block %d\n", eb);
  314. }
  315. /* create block backup */
  316. if (flags & KEEP_CONTENTS) {
  317. eb = 0;
  318. for (i = 0; i < ebcnt; ++i) {
  319. if (!bbt[i])
  320. ++eb;
  321. }
  322. backup = malloc(mtd.eb_size * eb);
  323. if (!backup) {
  324. fprintf(stderr, "not enough memory to keep block contents!\n");
  325. goto out_cleanup;
  326. }
  327. printf("reading %d blocks into memory\n", eb);
  328. backupptr = backup;
  329. for (i = 0; i < ebcnt; ++i) {
  330. if (bbt[i])
  331. continue;
  332. eb = peb + i*(skip+1);
  333. err = mtd_read(&mtd, fd, eb, 0, backupptr, mtd.eb_size);
  334. if (err) {
  335. fprintf(stderr, "error reading block %d!\n", eb);
  336. goto out_cleanup;
  337. }
  338. backupptr += mtd.eb_size;
  339. }
  340. }
  341. /* write first 2 sub-pages of each block */
  342. if (remove_test_data())
  343. goto out;
  344. rnd_state = seed;
  345. for (i = 0; i < ebcnt; ++i) {
  346. if (bbt[i])
  347. continue;
  348. err = write_eraseblock(i);
  349. if (err)
  350. goto out;
  351. }
  352. rnd_state = seed;
  353. for (i = 0; i < ebcnt; ++i) {
  354. if (bbt[i])
  355. continue;
  356. err = verify_eraseblock(i);
  357. if (err)
  358. goto out;
  359. }
  360. /* write with exponential offset & size */
  361. if (remove_test_data())
  362. goto out;
  363. rnd_state = seed;
  364. for (i = 0; i < ebcnt; ++i) {
  365. if (bbt[i])
  366. continue;
  367. err = write_eraseblock2(i);
  368. if (err)
  369. goto out;
  370. }
  371. rnd_state = seed;
  372. for (i = 0; i < ebcnt; ++i) {
  373. if (bbt[i])
  374. continue;
  375. err = verify_eraseblock2(i);
  376. if (err)
  377. goto out;
  378. }
  379. if (remove_test_data())
  380. goto out;
  381. status = EXIT_SUCCESS;
  382. out:
  383. if (flags & KEEP_CONTENTS) {
  384. puts("restoring original contents");
  385. backupptr = backup;
  386. for (i = 0; i < ebcnt; ++i) {
  387. if (bbt[i])
  388. continue;
  389. eb = peb + i*(skip+1);
  390. if (status != EXIT_SUCCESS && mtd_erase(mtd_desc, &mtd, fd, eb))
  391. fprintf(stderr, "error erasing block %d!\n", eb);
  392. err = mtd_write(mtd_desc, &mtd, fd, eb, 0,
  393. backupptr, mtd.eb_size, NULL, 0, 0);
  394. if (err) {
  395. fprintf(stderr, "error restoring block %d!\n", eb);
  396. status = EXIT_FAILURE;
  397. }
  398. backupptr += mtd.eb_size;
  399. }
  400. }
  401. out_cleanup:
  402. free(bbt);
  403. free(readbuf);
  404. free(writebuf);
  405. free(backup);
  406. close(fd);
  407. return status;
  408. }