flash_torture.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2006-2008 Artem Bityutskiy
  3. * Copyright (C) 2006-2008 Jarkko Lavinen
  4. * Copyright (C) 2006-2008 Adrian Hunter
  5. * Copyright (C) 2015 sigma star gmbh
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; see the file COPYING. If not, write to the Free Software
  18. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. *
  21. * WARNING: this test program may kill your flash and your device. Do not
  22. * use it unless you know what you do. Authors are not responsible for any
  23. * damage caused by this program.
  24. *
  25. * Author: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  26. *
  27. * Based on linux torturetest.c
  28. * Authors: Artem Bityutskiy, Jarkko Lavinen, Adria Hunter
  29. */
  30. #define PROGRAM_NAME "flash_torture"
  31. #define KEEP_CONTENTS 0x01
  32. #define RUN_FOREVER 0x02
  33. #include <mtd/mtd-user.h>
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include <libmtd.h>
  37. #include <signal.h>
  38. #include <getopt.h>
  39. #include <stdio.h>
  40. #include <fcntl.h>
  41. #include "common.h"
  42. static int peb=-1, blocks=-1, skip=-1;
  43. static struct mtd_dev_info mtd;
  44. static sig_atomic_t flags=0;
  45. static const char *mtddev;
  46. static libmtd_t mtd_desc;
  47. static int mtdfd;
  48. static const struct option options[] = {
  49. { "help", no_argument, NULL, 'h' },
  50. { "peb", required_argument, NULL, 'b' },
  51. { "count", required_argument, NULL, 'c' },
  52. { "skip", required_argument, NULL, 's' },
  53. { "keep", no_argument, NULL, 'k' },
  54. { "repeate", no_argument, NULL, 'r' },
  55. { NULL, 0, NULL, 0 },
  56. };
  57. static void sighandler(int sig)
  58. {
  59. if (sig == SIGINT || sig == SIGTERM || sig == SIGHUP)
  60. flags &= ~RUN_FOREVER;
  61. }
  62. static NORETURN void usage(int status)
  63. {
  64. fputs(
  65. "Usage: "PROGRAM_NAME" [OPTIONS] <device>\n\n"
  66. "Options:\n"
  67. " -h, --help Display this help output\n"
  68. " -b, --peb <num> Start from this physical erase block\n"
  69. " -c, --count <num> Number of erase blocks to torture\n"
  70. " -s, --skip <num> Number of erase blocks to skip\n"
  71. " -k, --keep Try to restore existing contents after test\n"
  72. " -r, --repeate Repeate the torture test indefinitely\n",
  73. status==EXIT_SUCCESS ? stdout : stderr);
  74. exit(status);
  75. }
  76. static long read_num(int opt, const char *arg)
  77. {
  78. char *end;
  79. long num;
  80. num = strtol(arg, &end, 0);
  81. if (!end || *end != '\0') {
  82. fprintf(stderr, "-%c: expected integer argument\n", opt);
  83. exit(EXIT_FAILURE);
  84. }
  85. return num;
  86. }
  87. static void process_options(int argc, char **argv)
  88. {
  89. int c;
  90. while (1) {
  91. c = getopt_long(argc, argv, "hb:c:s:kr", options, NULL);
  92. if (c == -1)
  93. break;
  94. switch (c) {
  95. case 'b':
  96. if (peb >= 0)
  97. goto failmulti;
  98. peb = read_num(c, optarg);
  99. if (peb < 0)
  100. goto failarg;
  101. break;
  102. case 'c':
  103. if (blocks > 0)
  104. goto failmulti;
  105. blocks = read_num(c, optarg);
  106. if (blocks <= 0)
  107. goto failarg;
  108. break;
  109. case 's':
  110. if (skip >= 0)
  111. goto failmulti;
  112. skip = read_num(c, optarg);
  113. if (skip < 0)
  114. goto failarg;
  115. break;
  116. case 'k':
  117. if (flags & KEEP_CONTENTS)
  118. goto failmulti;
  119. flags |= KEEP_CONTENTS;
  120. break;
  121. case 'r':
  122. if (flags & RUN_FOREVER)
  123. goto failmulti;
  124. flags |= RUN_FOREVER;
  125. break;
  126. case 'h':
  127. usage(EXIT_SUCCESS);
  128. default:
  129. exit(EXIT_FAILURE);
  130. }
  131. }
  132. if (optind < argc)
  133. mtddev = argv[optind++];
  134. else
  135. errmsg_die("No device specified!\n");
  136. if (optind < argc)
  137. usage(EXIT_FAILURE);
  138. if (peb < 0)
  139. peb = 0;
  140. if (skip < 0)
  141. skip = 0;
  142. if (blocks < 0)
  143. blocks = 1;
  144. return;
  145. failmulti:
  146. errmsg_die("'-%c' specified more than once!\n", c);
  147. failarg:
  148. errmsg_die("Invalid argument for '-%c'!\n", c);
  149. }
  150. int main(int argc, char **argv)
  151. {
  152. int i, eb, err, count = 0;
  153. char *is_bad = NULL;
  154. void *old=NULL;
  155. process_options(argc, argv);
  156. mtd_desc = libmtd_open();
  157. if (!mtd_desc)
  158. return errmsg("can't initialize libmtd");
  159. if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0)
  160. return errmsg("mtd_get_dev_info failed");
  161. if (peb >= mtd.eb_cnt)
  162. return errmsg("Physical erase block %d is out of range!\n", peb);
  163. if ((peb + (blocks - 1)*(skip + 1)) >= mtd.eb_cnt)
  164. return errmsg("Given block range exceeds block count of %d!\n",
  165. mtd.eb_cnt);
  166. signal(SIGINT, sighandler);
  167. signal(SIGTERM, sighandler);
  168. signal(SIGHUP, sighandler);
  169. if (flags & KEEP_CONTENTS)
  170. old = xmalloc(mtd.eb_size);
  171. is_bad = xmalloc(blocks);
  172. if ((mtdfd = open(mtddev, O_RDWR)) == -1) {
  173. perror(mtddev);
  174. free(is_bad);
  175. free(old);
  176. return EXIT_FAILURE;
  177. }
  178. for (i = 0; i < blocks; ++i) {
  179. eb = peb + i * (skip + 1);
  180. is_bad[i] = mtd_is_bad(&mtd, mtdfd, eb);
  181. if (is_bad[i])
  182. fprintf(stderr, "PEB %d marked bad, will be skipped\n", eb);
  183. }
  184. do {
  185. for (i = 0; i < blocks; ++i) {
  186. if (is_bad[i])
  187. continue;
  188. eb = peb + i * (skip + 1);
  189. if (flags & KEEP_CONTENTS) {
  190. err = mtd_read(&mtd, mtdfd, eb, 0, old, mtd.eb_size);
  191. if (err) {
  192. fprintf(stderr, "Failed to create backup copy "
  193. "of PEB %d, skipping!\n", eb);
  194. continue;
  195. }
  196. }
  197. if (mtd_torture(mtd_desc, &mtd, mtdfd, eb))
  198. fprintf(stderr, "Block %d failed torture test!\n", eb);
  199. if (flags & KEEP_CONTENTS) {
  200. err = mtd_erase(mtd_desc, &mtd, mtdfd, eb);
  201. if (err) {
  202. fprintf(stderr, "mtd_erase failed for block %d!\n", eb);
  203. continue;
  204. }
  205. err = mtd_write(mtd_desc, &mtd, mtdfd, eb, 0,
  206. old, mtd.eb_size, NULL, 0, 0);
  207. if (err)
  208. fprintf(stderr, "Failed to restore block %d!\n", eb);
  209. }
  210. }
  211. printf("Torture test iterations done: %d\n", ++count);
  212. } while (flags & RUN_FOREVER);
  213. free(old);
  214. free(is_bad);
  215. close(mtdfd);
  216. return EXIT_SUCCESS;
  217. }