aboutsummaryrefslogtreecommitdiff
path: root/nandtest.c
blob: 35766d81dd683dcb9f695ea50456a237f7dd3007 (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
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <getopt.h>

#include <asm/types.h>
#include "mtd/mtd-user.h"



/*
 * Main program
 */
int main(int argc, char **argv)
{
	int fd;
	int block, i;
	unsigned char *wbuf, *rbuf;
	struct mtd_info_user meminfo;
	struct mtd_ecc_stats oldstats, newstats;
	int seed;
	int pass;
	int nr_passes = 1;

	if (argc == 4) {
		seed = atol(argv[3]);
		argc = 3;
	}
	if (argc == 3) {
		nr_passes = atol(argv[2]);
		argc = 2;
	}
	if (argc != 2) {
		fprintf(stderr, "usage: %s <device> [<passes>] [<random seed>]\n",
			(strrchr(argv[0],',')?:argv[0]-1)+1);
		exit(1);
	}

	fd = open(argv[1], O_RDWR);
	if (fd < 0) {
		perror("open");
		exit(1);
	}
	
	if (ioctl(fd, MEMGETINFO, &meminfo)) {
		perror("MEMGETINFO");
		close(fd);
		exit(1);
	}

	wbuf = malloc(meminfo.erasesize * 2);
	if (!wbuf) {
		fprintf(stderr, "Could not allocate %d bytes for buffer\n",
			meminfo.erasesize * 2);
		exit(1);
	}
	rbuf = wbuf + meminfo.erasesize;

	if (ioctl(fd, ECCGETSTATS, &oldstats)) {
		perror("ECCGETSTATS");
		close(fd);
		exit(1);
	}

	printf("ECC corrections: %d\n", oldstats.corrected);
	printf("ECC failures   : %d\n", oldstats.failed);
	printf("Bad blocks     : %d\n", oldstats.badblocks);
	printf("BBT blocks     : %d\n", oldstats.bbtblocks);

	for (pass = 0; pass < nr_passes; pass++) {
		
		for (block = 0; block < meminfo.size / meminfo.erasesize ; block++) {
			loff_t ofs = block * meminfo.erasesize;
			struct erase_info_user er;
			ssize_t len;

			seed = rand();
			srand(seed);

			if (ioctl(fd, MEMGETBADBLOCK, &ofs)) {
				printf("\rBad block at 0x%08x\n", (unsigned)ofs);
				continue;
			}

			printf("\r%08x: erasing... ", (unsigned)ofs);
			fflush(stdout);

			er.start = ofs;
			er.length = meminfo.erasesize;

			if (ioctl(fd, MEMERASE, &er)) {
				perror("MEMERASE");
				exit(1);
			}

			printf("\r%08x: writing...", (unsigned)ofs);
			fflush(stdout);

			for (i=0; i<meminfo.erasesize; i++)
				wbuf[i] = rand();

			len = pwrite(fd, wbuf, meminfo.erasesize, ofs);
			if (len < 0) {
				printf("\n");
				perror("write");
				ioctl(fd, MEMSETBADBLOCK, &ofs);
				continue;
			}
			if (len < meminfo.erasesize) {
				printf("\n");
				fprintf(stderr, "Short write (%d bytes)\n", len);
				exit(1);
			}

			printf("\r%08x: reading...", (unsigned)ofs);
			fflush(stdout);

			len = pread(fd, rbuf, meminfo.erasesize, ofs);
			if (len < meminfo.erasesize) {
				printf("\n");
				if (len)
					fprintf(stderr, "Short read (%d bytes)\n", len);
				else
					perror("read");
				exit(1);
			}
		
			if (ioctl(fd, ECCGETSTATS, &newstats)) {
				printf("\n");
				perror("ECCGETSTATS");
				close(fd);
				exit(1);
			}
		
			if (newstats.corrected > oldstats.corrected) {
				printf("\nECC corrected at %08x\n", (unsigned) ofs);
				oldstats.corrected = newstats.corrected;
			}
			if (newstats.failed > oldstats.failed) {
				printf("\nECC failed at %08x\n", (unsigned) ofs);
				oldstats.corrected = newstats.corrected;
			}
			if (len < meminfo.erasesize)
				exit(1);

			printf("\r%08x: checking...", (unsigned)ofs);
			fflush(stdout);

			if (memcmp(wbuf, rbuf, meminfo.erasesize)) {
				printf("\n");
				fprintf(stderr, "compare failed. seed %d\n", seed);
				for (i=0; i<meminfo.erasesize; i++) {
					if (wbuf[i] != rbuf[i])
						printf("Byte 0x%x is %02x should be %02x\n",
						       i, rbuf[i], wbuf[i]);
				}
				exit(1);
			}
		}
		printf("\nFinished pass %d successfully\n", pass+1);
	}
	/* Return happy */
	return 0;
}