aboutsummaryrefslogtreecommitdiff
path: root/tests/mtd-tests/flash_stress.c
blob: 249d8cbc6c4cbd5e445b69a38d4fdd7a6a5c5334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2006-2008 Nokia Corporation
 * Copyright (C) 2015 sigma star gmbh
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; see the file COPYING. If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Test random reads, writes and erases on MTD device.
 *
 * Author: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
 *
 * Based on linux stresstest.c
 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
 */
#define PROGRAM_NAME "flash_stress"

#define KEEP_CONTENTS 0x01
#define COUNT_CHANGED 0x02
#define SEED_SET 0x04

#include <mtd/mtd-user.h>
#include <unistd.h>
#include <stdlib.h>
#include <libmtd.h>
#include <getopt.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>

#include "common.h"

static struct mtd_dev_info mtd;
static const char *mtddev;
static libmtd_t mtd_desc;
static int fd;

static unsigned char *writebuf;
static unsigned char *readbuf;
static unsigned char *old;
static unsigned char *bbt;

static int pgsize;
static int pgcnt;

static int count = 10000;
static int flags = 0;

static const struct option options[] = {
	{ "help", no_argument, NULL, 'h' },
	{ "keep", no_argument, NULL, 'k' },
	{ "seed", required_argument, NULL, 's' },
	{ "count", required_argument, NULL, 'c' },
	{ NULL, 0, NULL, 0 },
};

static NORETURN void usage(int status)
{
	fputs(
	"Usage: "PROGRAM_NAME" [OPTIONS] <device>\n\n"
	"Options:\n"
	"  -h, --help         Display this help output\n"
	"  -c, --count <num>  Number of operations to do (default is 10000)\n"
	"  -s, --seed <num>   Seed for pseudor random number generator\n"
	"  -k, --keep         Restore existing contents after test\n",
	status==EXIT_SUCCESS ? stdout : stderr);
	exit(status);
}

static long read_num(int opt, const char *arg)
{
	char *end;
	long num;

	num = strtol(arg, &end, 0);

	if (!end || *end != '\0') {
		fprintf(stderr, "-%c: expected integer argument\n", opt);
		exit(EXIT_FAILURE);
	}
	return num;
}

static void process_options(int argc, char **argv)
{
	int c;

	while (1) {
		c = getopt_long(argc, argv, "hc:s:k", options, NULL);
		if (c == -1)
			break;

		switch (c) {
		case 'k':
			if (flags & KEEP_CONTENTS)
				goto failmulti;
			flags |= KEEP_CONTENTS;
			break;
		case 's':
			if (flags & SEED_SET)
				goto failmulti;
			srand(read_num(c, optarg));
			flags |= SEED_SET;
			break;
		case 'c':
			if (flags & COUNT_CHANGED)
				goto failmulti;
			count = read_num(c, optarg);
			if (count <= 0)
				goto failarg;
			flags |= COUNT_CHANGED;
			break;
		case 'h':
			usage(EXIT_SUCCESS);
		default:
			exit(EXIT_FAILURE);
		}
	}

	if (optind < argc)
		mtddev = argv[optind++];
	else
		errmsg_die("No device specified!\n");

	if (optind < argc)
		usage(EXIT_FAILURE);
	if (!(flags & SEED_SET))
		srand(time(NULL));
	return;
failmulti:
	errmsg_die("'-%c' specified more than once!\n", c);
failarg:
	errmsg_die("Invalid argument for '-%c'!\n", c);
}

static int rand_eb(void)
{
	unsigned int eb;

	/* Read or write up 2 eraseblocks at a time - hence 'mtd.eb_cnt - 1' */
	do {
		eb = rand() % (mtd.eb_cnt - 1);
	} while (bbt[eb]);

	return eb;
}

static int do_read(void)
{
	int eb = rand_eb();
	int offs = rand() % pgcnt;
	int len = rand() % (pgcnt - offs);

	offs *= pgsize;
	len *= pgsize;
	return mtd_read(&mtd, fd, eb, offs, readbuf, len);
}

static int do_write(void)
{
	int eb = rand_eb(), err, err1;
	int offs = rand() % pgcnt;
	int len = rand() % (pgcnt - offs);

	offs *= pgsize;
	len *= pgsize;

	if (flags & KEEP_CONTENTS) {
		err = mtd_read(&mtd, fd, eb, 0, old, mtd.eb_size);
		if (err) {
			fputs("Error backing up old erase block contents\n", stderr);
			return -1;
		}
	}

	err = mtd_erase(mtd_desc, &mtd, fd, eb);
	if (err)
		goto out;

	err = mtd_write(mtd_desc, &mtd, fd, eb, offs,
			writebuf, len, NULL, 0, 0);
	if (err)
		goto out;

	err = 0;
out:
	if (flags & KEEP_CONTENTS) {
		if (mtd_erase(mtd_desc, &mtd, fd, eb)) {
			fprintf(stderr, "mtd_erase: PEB %d", eb);
			return -1;
		}

		err1 = mtd_write(mtd_desc, &mtd, fd, eb, 0,
					old, mtd.eb_size, NULL, 0, 0);

		if (err1) {
			fprintf(stderr, "Failed to restore old contents\n");
			return -1;
		}
	}
	return err;
}

static void scan_for_bad_eraseblocks(unsigned int eb, int ebcnt)
{
	int i, bad = 0;

	puts("scanning for bad eraseblocks");

	for (i = 0; i < ebcnt; ++i) {
		bbt[i] = mtd_is_bad(&mtd, fd, eb + i) ? 1 : 0;
		if (bbt[i])
			bad += 1;
	}

	printf("scanned %d eraseblocks, %d are bad\n", ebcnt, bad);
}

int main(int argc, char **argv)
{
	int status = EXIT_FAILURE, i, op, err;

	process_options(argc, argv);

	mtd_desc = libmtd_open();
	if (!mtd_desc)
		return errmsg("can't initialize libmtd");

	if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0)
		return errmsg("mtd_get_dev_info failed");

	if (mtd.subpage_size == 1) {
		puts("not NAND flash, assume page size is 512 bytes.");
		pgsize = 512;
	} else {
		pgsize = mtd.subpage_size;
	}

	pgcnt = mtd.eb_size / pgsize;

	readbuf = xmalloc(mtd.eb_size);
	writebuf = xmalloc(mtd.eb_size);
	bbt = xzalloc(mtd.eb_cnt);

	if (flags & KEEP_CONTENTS)
		old = xmalloc(mtd.eb_size);

	for (i = 0; i < mtd.eb_size; ++i)
		writebuf[i] = rand();

	/* Open device file */
	if ((fd = open(mtddev, O_RDWR)) == -1) {
		perror(mtddev);
		goto out;
	}

	/* Do operations */
	scan_for_bad_eraseblocks(0, mtd.eb_cnt);

	puts("doing operations");
	for (op = 0; op < count; op++) {
		if ((op & 1023) == 0)
			printf("%d operations done\n", op);
		err = (rand() & 1) ? do_read() : do_write();
		if (err)
			goto out;
	}
	printf("finished, %d operations done\n", op);

	status = EXIT_SUCCESS;
out:
	close(fd);
	free(bbt);
	free(writebuf);
	free(readbuf);
	free(old);
	return status;
}