flash_readtest.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2006-2008 Nokia Corporation
  3. * Copyright (C) 2015 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. * Check MTD device read.
  19. *
  20. * Author: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  21. *
  22. * Based on linux readtest.c
  23. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  24. */
  25. #define PROGRAM_NAME "flash_readtest"
  26. #include <mtd/mtd-user.h>
  27. #include <sys/ioctl.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <libmtd.h>
  32. #include <getopt.h>
  33. #include <stdio.h>
  34. #include <fcntl.h>
  35. #include <time.h>
  36. #include "common.h"
  37. #define FLAG_VERBOSE 1
  38. static int peb=-1, skip=-1, count=-1, flags=0, pgcnt, pgsize, fd;
  39. static unsigned char *iobuf, *iobuf1;
  40. static struct mtd_dev_info mtd;
  41. static const char *mtddev;
  42. static libmtd_t mtd_desc;
  43. static const struct option options[] = {
  44. { "help", no_argument, NULL, 'h' },
  45. { "verbose", no_argument, NULL, 'v' },
  46. { "peb", required_argument, NULL, 'b' },
  47. { "count", required_argument, NULL, 'c' },
  48. { "skip", required_argument, NULL, 's' },
  49. { NULL, 0, NULL, 0 },
  50. };
  51. static NORETURN void usage(int status)
  52. {
  53. fputs(
  54. "Usage: "PROGRAM_NAME" [OPTIONS] <device>\n\n"
  55. "Common options:\n"
  56. " -h, --help Display this help output\n"
  57. " -b, --peb <num> Start from this physical erase block\n"
  58. " -c, --count <num> Number of erase blocks to process (default: all)\n"
  59. " -s, --skip <num> Number of blocks to skip\n"
  60. " -v, --verbose Generate more verbose output\n\n",
  61. status==EXIT_SUCCESS ? stdout : stderr);
  62. exit(status);
  63. }
  64. static long read_num(int opt, const char *arg)
  65. {
  66. char *end;
  67. long num;
  68. num = strtol(arg, &end, 0);
  69. if (!end || *end != '\0') {
  70. fprintf(stderr, "-%c: expected integer argument\n", opt);
  71. exit(EXIT_FAILURE);
  72. }
  73. return num;
  74. }
  75. static void process_options(int argc, char **argv)
  76. {
  77. int c;
  78. while (1) {
  79. c = getopt_long(argc, argv, "hb:c:s:v", options, NULL);
  80. if (c == -1)
  81. break;
  82. switch (c) {
  83. case 'v':
  84. if (flags & FLAG_VERBOSE)
  85. goto failmulti;
  86. flags |= FLAG_VERBOSE;
  87. break;
  88. case 'b':
  89. if (peb >= 0)
  90. goto failmulti;
  91. peb = read_num(c, optarg);
  92. if (peb < 0)
  93. goto failarg;
  94. break;
  95. case 'c':
  96. if (count >= 0)
  97. goto failmulti;
  98. count = read_num(c, optarg);
  99. if (count < 0)
  100. goto failarg;
  101. break;
  102. case 's':
  103. if (skip >= 0)
  104. goto failmulti;
  105. skip = read_num(c, optarg);
  106. if (skip < 0)
  107. goto failarg;
  108. break;
  109. case 'h':
  110. usage(EXIT_SUCCESS);
  111. default:
  112. exit(EXIT_FAILURE);
  113. }
  114. }
  115. if (optind < argc)
  116. mtddev = argv[optind++];
  117. else
  118. errmsg_die("No device specified!\n");
  119. if (optind < argc)
  120. usage(EXIT_FAILURE);
  121. if (peb < 0)
  122. peb = 0;
  123. if (skip < 0)
  124. skip = 0;
  125. return;
  126. failmulti:
  127. errmsg_die("'-%c' specified more than once!", c);
  128. failarg:
  129. errmsg_die("Invalid argument for '-%c'!", c);
  130. }
  131. static int read_eraseblock_by_page(int ebnum)
  132. {
  133. unsigned char *buf = iobuf, *oobbuf = iobuf1;
  134. uint64_t addr = ((uint64_t)ebnum) * ((uint64_t)mtd.eb_size);
  135. int i, ret;
  136. for (i = 0; i < pgcnt; ++i) {
  137. memset(buf, 0, pgsize);
  138. ret = mtd_read(&mtd, fd, ebnum, i*pgsize, buf, pgsize);
  139. if (ret) {
  140. fprintf(stderr, "Error reading block %d, page %d\n", ebnum, i);
  141. return -1;
  142. }
  143. if (mtd.oob_size) {
  144. ret = mtd_read_oob(mtd_desc, &mtd, fd,
  145. addr, mtd.oob_size, oobbuf);
  146. if (ret) {
  147. fprintf(stderr, "Error reading OOB in block %d, page %d\n",
  148. ebnum, i);
  149. return -1;
  150. }
  151. oobbuf += mtd.oob_size;
  152. }
  153. buf += pgsize;
  154. addr += pgsize;
  155. }
  156. if (flags & FLAG_VERBOSE)
  157. printf("Successfully read erase block %d\n", ebnum);
  158. return 0;
  159. }
  160. static void dump_eraseblock(int ebnum)
  161. {
  162. char line[128];
  163. int i, j, n;
  164. int pg, oob;
  165. printf("dumping eraseblock %d\n", ebnum);
  166. n = mtd.eb_size;
  167. for (i = 0; i < n;) {
  168. char *p = line;
  169. p += sprintf(p, "%05x: ", i);
  170. for (j = 0; j < 32 && i < n; j++, i++)
  171. p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
  172. printf("%s\n", line);
  173. }
  174. if (!mtd.oob_size)
  175. return;
  176. printf("dumping oob from eraseblock %d\n", ebnum);
  177. n = mtd.oob_size;
  178. for (pg = 0, i = 0; pg < pgcnt; ++pg) {
  179. for (oob = 0; oob < n;) {
  180. char *p = line;
  181. p += sprintf(p, "%05x: ", i);
  182. for (j = 0; j < 32 && oob < n; ++j, ++oob, ++i)
  183. p += sprintf(p, "%02x", (unsigned int)iobuf1[i]);
  184. printf("%s\n", line);
  185. }
  186. }
  187. putchar('\n');
  188. }
  189. int main(int argc, char **argv)
  190. {
  191. int status = EXIT_SUCCESS, i, ret, blk;
  192. process_options(argc, argv);
  193. mtd_desc = libmtd_open();
  194. if (!mtd_desc)
  195. return errmsg("can't initialize libmtd");
  196. if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0)
  197. return errmsg("mtd_get_dev_info failed");
  198. if (mtd.subpage_size == 1) {
  199. puts("not NAND flash, assume page size is 512 bytes.");
  200. pgsize = 512;
  201. } else {
  202. pgsize = mtd.subpage_size;
  203. }
  204. pgcnt = mtd.eb_size / pgsize;
  205. if (count < 0)
  206. count = mtd.eb_cnt;
  207. if (peb >= mtd.eb_cnt)
  208. return errmsg("Physical erase block %d is out of range!\n", peb);
  209. if ((peb + (count - 1)*(skip + 1)) >= mtd.eb_cnt) {
  210. return errmsg("Given block range exceeds block count of %d!\n",
  211. mtd.eb_cnt);
  212. }
  213. iobuf = xmalloc(mtd.eb_size);
  214. iobuf1 = xmalloc(mtd.eb_size);
  215. if ((fd = open(mtddev, O_RDWR)) == -1) {
  216. perror(mtddev);
  217. status = EXIT_FAILURE;
  218. goto out;
  219. }
  220. /* Read all eraseblocks 1 page at a time */
  221. puts("testing page read");
  222. for (i = 0; i < count; ++i) {
  223. blk = peb + i*(skip+1);
  224. if (mtd_is_bad(&mtd, fd, blk)) {
  225. printf("Skipping bad block %d\n", blk);
  226. continue;
  227. }
  228. ret = read_eraseblock_by_page(blk);
  229. if (ret && (flags & FLAG_VERBOSE)) {
  230. dump_eraseblock(blk);
  231. status = EXIT_FAILURE;
  232. }
  233. }
  234. out:
  235. free(iobuf);
  236. free(iobuf1);
  237. return status;
  238. }