flash_stress.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * Test random reads, writes and erases on MTD device.
  19. *
  20. * Author: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
  21. *
  22. * Based on linux stresstest.c
  23. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  24. */
  25. #define PROGRAM_NAME "flash_stress"
  26. #define KEEP_CONTENTS 0x01
  27. #define COUNT_CHANGED 0x02
  28. #define SEED_SET 0x04
  29. #include <mtd/mtd-user.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <libmtd.h>
  33. #include <getopt.h>
  34. #include <stdio.h>
  35. #include <fcntl.h>
  36. #include <time.h>
  37. #include "common.h"
  38. static struct mtd_dev_info mtd;
  39. static const char *mtddev;
  40. static libmtd_t mtd_desc;
  41. static int fd;
  42. static unsigned char *writebuf;
  43. static unsigned char *readbuf;
  44. static unsigned char *old;
  45. static unsigned char *bbt;
  46. static int pgsize;
  47. static int pgcnt;
  48. static int count = 10000;
  49. static int flags = 0;
  50. static const struct option options[] = {
  51. { "help", no_argument, NULL, 'h' },
  52. { "keep", no_argument, NULL, 'k' },
  53. { "seed", required_argument, NULL, 's' },
  54. { "count", required_argument, NULL, 'c' },
  55. { NULL, 0, NULL, 0 },
  56. };
  57. static NORETURN void usage(int status)
  58. {
  59. fputs(
  60. "Usage: "PROGRAM_NAME" [OPTIONS] <device>\n\n"
  61. "Options:\n"
  62. " -h, --help Display this help output\n"
  63. " -c, --count <num> Number of operations to do (default is 10000)\n"
  64. " -s, --seed <num> Seed for pseudor random number generator\n"
  65. " -k, --keep Restore existing contents after test\n",
  66. status==EXIT_SUCCESS ? stdout : stderr);
  67. exit(status);
  68. }
  69. static long read_num(int opt, const char *arg)
  70. {
  71. char *end;
  72. long num;
  73. num = strtol(arg, &end, 0);
  74. if (!end || *end != '\0') {
  75. fprintf(stderr, "-%c: expected integer argument\n", opt);
  76. exit(EXIT_FAILURE);
  77. }
  78. return num;
  79. }
  80. static void process_options(int argc, char **argv)
  81. {
  82. int c;
  83. while (1) {
  84. c = getopt_long(argc, argv, "hc:s:k", options, NULL);
  85. if (c == -1)
  86. break;
  87. switch (c) {
  88. case 'k':
  89. if (flags & KEEP_CONTENTS)
  90. goto failmulti;
  91. flags |= KEEP_CONTENTS;
  92. break;
  93. case 's':
  94. if (flags & SEED_SET)
  95. goto failmulti;
  96. srand(read_num(c, optarg));
  97. flags |= SEED_SET;
  98. break;
  99. case 'c':
  100. if (flags & COUNT_CHANGED)
  101. goto failmulti;
  102. count = read_num(c, optarg);
  103. if (count <= 0)
  104. goto failarg;
  105. flags |= COUNT_CHANGED;
  106. break;
  107. case 'h':
  108. usage(EXIT_SUCCESS);
  109. default:
  110. exit(EXIT_FAILURE);
  111. }
  112. }
  113. if (optind < argc)
  114. mtddev = argv[optind++];
  115. else
  116. errmsg_die("No device specified!\n");
  117. if (optind < argc)
  118. usage(EXIT_FAILURE);
  119. if (!(flags & SEED_SET))
  120. srand(time(NULL));
  121. return;
  122. failmulti:
  123. errmsg_die("'-%c' specified more than once!\n", c);
  124. failarg:
  125. errmsg_die("Invalid argument for '-%c'!\n", c);
  126. }
  127. static int rand_eb(void)
  128. {
  129. unsigned int eb;
  130. /* Read or write up 2 eraseblocks at a time - hence 'mtd.eb_cnt - 1' */
  131. do {
  132. eb = rand() % (mtd.eb_cnt - 1);
  133. } while (bbt[eb]);
  134. return eb;
  135. }
  136. static int do_read(void)
  137. {
  138. int eb = rand_eb();
  139. int offs = rand() % pgcnt;
  140. int len = rand() % (pgcnt - offs);
  141. offs *= pgsize;
  142. len *= pgsize;
  143. return mtd_read(&mtd, fd, eb, offs, readbuf, len);
  144. }
  145. static int do_write(void)
  146. {
  147. int eb = rand_eb(), err, err1;
  148. int offs = rand() % pgcnt;
  149. int len = rand() % (pgcnt - offs);
  150. offs *= pgsize;
  151. len *= pgsize;
  152. if (flags & KEEP_CONTENTS) {
  153. err = mtd_read(&mtd, fd, eb, 0, old, mtd.eb_size);
  154. if (err) {
  155. fputs("Error backing up old erase block contents\n", stderr);
  156. return -1;
  157. }
  158. }
  159. err = mtd_erase(mtd_desc, &mtd, fd, eb);
  160. if (err)
  161. goto out;
  162. err = mtd_write(mtd_desc, &mtd, fd, eb, offs,
  163. writebuf, len, NULL, 0, 0);
  164. if (err)
  165. goto out;
  166. err = 0;
  167. out:
  168. if (flags & KEEP_CONTENTS) {
  169. if (mtd_erase(mtd_desc, &mtd, fd, eb)) {
  170. fprintf(stderr, "mtd_erase: PEB %d", eb);
  171. return -1;
  172. }
  173. err1 = mtd_write(mtd_desc, &mtd, fd, eb, 0,
  174. old, mtd.eb_size, NULL, 0, 0);
  175. if (err1) {
  176. fprintf(stderr, "Failed to restore old contents\n");
  177. return -1;
  178. }
  179. }
  180. return err;
  181. }
  182. static void scan_for_bad_eraseblocks(unsigned int eb, int ebcnt)
  183. {
  184. int i, bad = 0;
  185. puts("scanning for bad eraseblocks");
  186. for (i = 0; i < ebcnt; ++i) {
  187. bbt[i] = mtd_is_bad(&mtd, fd, eb + i) ? 1 : 0;
  188. if (bbt[i])
  189. bad += 1;
  190. }
  191. printf("scanned %d eraseblocks, %d are bad\n", ebcnt, bad);
  192. }
  193. int main(int argc, char **argv)
  194. {
  195. int status = EXIT_FAILURE, i, op, err;
  196. process_options(argc, argv);
  197. mtd_desc = libmtd_open();
  198. if (!mtd_desc)
  199. return errmsg("can't initialize libmtd");
  200. if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0)
  201. return errmsg("mtd_get_dev_info failed");
  202. if (mtd.subpage_size == 1) {
  203. puts("not NAND flash, assume page size is 512 bytes.");
  204. pgsize = 512;
  205. } else {
  206. pgsize = mtd.subpage_size;
  207. }
  208. pgcnt = mtd.eb_size / pgsize;
  209. readbuf = xmalloc(mtd.eb_size);
  210. writebuf = xmalloc(mtd.eb_size);
  211. bbt = xzalloc(mtd.eb_cnt);
  212. if (flags & KEEP_CONTENTS)
  213. old = xmalloc(mtd.eb_size);
  214. for (i = 0; i < mtd.eb_size; ++i)
  215. writebuf[i] = rand();
  216. /* Open device file */
  217. if ((fd = open(mtddev, O_RDWR)) == -1) {
  218. perror(mtddev);
  219. goto out;
  220. }
  221. /* Do operations */
  222. scan_for_bad_eraseblocks(0, mtd.eb_cnt);
  223. puts("doing operations");
  224. for (op = 0; op < count; op++) {
  225. if ((op & 1023) == 0)
  226. printf("%d operations done\n", op);
  227. err = (rand() & 1) ? do_read() : do_write();
  228. if (err)
  229. goto out;
  230. }
  231. printf("finished, %d operations done\n", op);
  232. status = EXIT_SUCCESS;
  233. out:
  234. close(fd);
  235. free(bbt);
  236. free(writebuf);
  237. free(readbuf);
  238. free(old);
  239. return status;
  240. }