blob: 4666f2656c32772ab25b568e4a2eaf8dd17aa01d [file] [log] [blame]
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <ctype.h>
5#include <errno.h>
6#include <fcntl.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -08007#include <unistd.h>
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -07008
9#include <mtd/mtd-user.h>
10#include <sys/ioctl.h>
11
Arve Hjønnevågb6b87932010-03-03 16:48:38 -080012static int test_empty(const char *buf, size_t size)
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070013{
14 while(size--) {
Elliott Hughesb05a2e52013-11-11 16:17:49 -080015 if (*buf++ != (char) 0xff)
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070016 return 0;
17 }
18 return 1;
19}
20
21int nandread_main(int argc, char **argv)
22{
23 char *devname = NULL;
24 char *filename = NULL;
25 char *statusfilename = NULL;
26 char *statusext = ".stat";
27 int fd;
28 int outfd = -1;
29 FILE *statusfile = NULL;
30 int ret;
31 int verbose = 0;
32 void *buffer;
Arve Hjønnevågb6b87932010-03-03 16:48:38 -080033 loff_t pos, opos, end, bpos;
34 loff_t start = 0, len = 0;
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070035 int c;
36 int i;
37 int empty_pages = 0;
38 int page_count = 0;
39 int bad_block;
Arve Hjønnevågb6b87932010-03-03 16:48:38 -080040 int rawmode = 0;
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070041 uint32_t *oob_data;
42 uint8_t *oob_fixed;
43 size_t spare_size = 64;
44 struct mtd_info_user mtdinfo;
45 struct mtd_ecc_stats initial_ecc, last_ecc, ecc;
46 struct mtd_oob_buf oobbuf;
Elliott Hughesb05a2e52013-11-11 16:17:49 -080047 nand_ecclayout_t ecclayout;
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070048
49 do {
Arve Hjønnevågb6b87932010-03-03 16:48:38 -080050 c = getopt(argc, argv, "d:f:s:S:L:Rhv");
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070051 if (c == EOF)
52 break;
53 switch (c) {
54 case 'd':
55 devname = optarg;
56 break;
57 case 'f':
58 filename = optarg;
59 break;
60 case 's':
61 spare_size = atoi(optarg);
62 break;
Arve Hjønnevågb6b87932010-03-03 16:48:38 -080063 case 'S':
64 start = strtoll(optarg, NULL, 0);
65 break;
66 case 'L':
67 len = strtoll(optarg, NULL, 0);
68 break;
69 case 'R':
70 rawmode = 1;
71 break;
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070072 case 'v':
73 verbose++;
74 break;
75 case 'h':
76 fprintf(stderr, "%s [-d <dev>] [-f file] [-s sparesize] [-vh]\n"
77 " -d <dev> Read from <dev>\n"
78 " -f <file> Write to <file>\n"
79 " -s <size> Number of spare bytes in file (default 64)\n"
Arve Hjønnevågb6b87932010-03-03 16:48:38 -080080 " -R Raw mode\n"
81 " -S <start> Start offset (default 0)\n"
82 " -L <len> Length (default 0)\n"
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -070083 " -v Print info\n"
84 " -h Print help\n", argv[0]);
85 return -1;
86 case '?':
87 fprintf(stderr, "%s: invalid option -%c\n",
88 argv[0], optopt);
89 exit(1);
90 }
91 } while (1);
92
93 if (optind < argc) {
94 fprintf(stderr, "%s: extra arguments\n", argv[0]);
95 return 1;
96 }
97 if (!devname) {
98 fprintf(stderr, "%s: specify device name\n", argv[0]);
99 return 1;
100 }
101
102 fd = open(devname, O_RDONLY);
103 if (fd < 0) {
104 fprintf(stderr, "cannot open %s, %s\n", devname, strerror(errno));
105 return 1;
106 }
107
108 if (filename) {
109 outfd = creat(filename, 0666);
110 if (outfd < 0) {
111 fprintf(stderr, "cannot open %s, %s\n", filename, strerror(errno));
112 return 1;
113 }
114 statusfilename = malloc(strlen(filename) + strlen(statusext) + 1);
115 strcpy(statusfilename, filename);
116 strcat(statusfilename, statusext);
117 statusfile = fopen(statusfilename, "w+");
118 if (!statusfile) {
119 fprintf(stderr, "cannot open %s, %s\n", statusfilename, strerror(errno));
120 return 1;
121 }
122 }
123
124 ret = ioctl(fd, MEMGETINFO, &mtdinfo);
125 if (ret) {
126 fprintf(stderr, "failed get mtd info for %s, %s\n",
127 devname, strerror(errno));
128 return 1;
129 }
130
131 if (verbose) {
132 printf("size: %u\n", mtdinfo.size);
133 printf("erase size: %u\n", mtdinfo.erasesize);
134 printf("write size: %u\n", mtdinfo.writesize);
135 printf("oob size: %u\n", mtdinfo.oobsize);
136 }
137
138 buffer = malloc(mtdinfo.writesize + mtdinfo.oobsize + spare_size);
139 if (!buffer) {
140 fprintf(stderr, "failed allocate readbuffer size %u\n",
141 mtdinfo.writesize + mtdinfo.oobsize);
142 return 1;
143 }
144
145 oobbuf.length = mtdinfo.oobsize;
146 oob_data = (uint32_t *)((uint8_t *)buffer + mtdinfo.writesize);
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800147 memset(oob_data, 0xff, mtdinfo.oobsize + spare_size);
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -0700148 oobbuf.ptr = (uint8_t *)oob_data + spare_size;
149
150 ret = ioctl(fd, ECCGETLAYOUT, &ecclayout);
151 if (ret) {
152 fprintf(stderr, "failed get ecc layout for %s, %s\n",
153 devname, strerror(errno));
154 return 1;
155 }
156 if (verbose) {
157 printf("ecc bytes: %u\n", ecclayout.eccbytes);
158 printf("oobavail: %u\n", ecclayout.oobavail);
159 }
160 if (ecclayout.oobavail > spare_size)
161 printf("oobavail, %d > image spare size, %d\n", ecclayout.oobavail, spare_size);
162
163 ret = ioctl(fd, ECCGETSTATS, &initial_ecc);
164 if (ret) {
165 fprintf(stderr, "failed get ecc stats for %s, %s\n",
166 devname, strerror(errno));
167 return 1;
168 }
169 last_ecc = initial_ecc;
170
171 if (verbose) {
172 printf("initial ecc corrected: %u\n", initial_ecc.corrected);
173 printf("initial ecc failed: %u\n", initial_ecc.failed);
174 printf("initial ecc badblocks: %u\n", initial_ecc.badblocks);
175 printf("initial ecc bbtblocks: %u\n", initial_ecc.bbtblocks);
176 }
177
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800178 if (rawmode) {
179 rawmode = mtdinfo.oobsize;
Elliott Hughesb05a2e52013-11-11 16:17:49 -0800180#if !defined(MTD_STUPID_LOCK) /* using uapi kernel headers */
181 ret = ioctl(fd, MTDFILEMODE, MTD_FILE_MODE_RAW);
182#else /* still using old kernel headers */
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800183 ret = ioctl(fd, MTDFILEMODE, MTD_MODE_RAW);
Elliott Hughesb05a2e52013-11-11 16:17:49 -0800184#endif
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800185 if (ret) {
186 fprintf(stderr, "failed set raw mode for %s, %s\n",
187 devname, strerror(errno));
188 return 1;
189 }
190 }
191
192 end = len ? (start + len) : mtdinfo.size;
193 for (pos = start, opos = 0; pos < end; pos += mtdinfo.writesize) {
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -0700194 bad_block = 0;
195 if (verbose > 3)
196 printf("reading at %llx\n", pos);
197 lseek64(fd, pos, SEEK_SET);
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800198 ret = read(fd, buffer, mtdinfo.writesize + rawmode);
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -0700199 if (ret < (int)mtdinfo.writesize) {
200 fprintf(stderr, "short read at %llx, %d\n", pos, ret);
201 bad_block = 2;
202 }
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800203 if (!rawmode) {
204 oobbuf.start = pos;
205 ret = ioctl(fd, MEMREADOOB, &oobbuf);
206 if (ret) {
207 fprintf(stderr, "failed to read oob data at %llx, %d\n", pos, ret);
208 bad_block = 2;
209 }
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -0700210 }
211 ret = ioctl(fd, ECCGETSTATS, &ecc);
212 if (ret) {
213 fprintf(stderr, "failed get ecc stats for %s, %s\n",
214 devname, strerror(errno));
215 return 1;
216 }
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800217 bpos = pos / mtdinfo.erasesize * mtdinfo.erasesize;
218 ret = ioctl(fd, MEMGETBADBLOCK, &bpos);
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -0700219 if (ret && errno != EOPNOTSUPP) {
220 printf("badblock at %llx\n", pos);
221 bad_block = 1;
222 }
223 if (ecc.corrected != last_ecc.corrected)
224 printf("ecc corrected, %u, at %llx\n", ecc.corrected - last_ecc.corrected, pos);
225 if (ecc.failed != last_ecc.failed)
226 printf("ecc failed, %u, at %llx\n", ecc.failed - last_ecc.failed, pos);
227 if (ecc.badblocks != last_ecc.badblocks)
228 printf("ecc badblocks, %u, at %llx\n", ecc.badblocks - last_ecc.badblocks, pos);
229 if (ecc.bbtblocks != last_ecc.bbtblocks)
230 printf("ecc bbtblocks, %u, at %llx\n", ecc.bbtblocks - last_ecc.bbtblocks, pos);
231
Arve Hjønnevågb6b87932010-03-03 16:48:38 -0800232 if (!rawmode) {
233 oob_fixed = (uint8_t *)oob_data;
234 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
235 int len = ecclayout.oobfree[i].length;
236 if (oob_fixed + len > oobbuf.ptr)
237 len = oobbuf.ptr - oob_fixed;
238 if (len) {
239 memcpy(oob_fixed, oobbuf.ptr + ecclayout.oobfree[i].offset, len);
240 oob_fixed += len;
241 }
Arve Hjønnevåg7c953d02009-09-17 17:30:55 -0700242 }
243 }
244
245 if (outfd >= 0) {
246 ret = write(outfd, buffer, mtdinfo.writesize + spare_size);
247 if (ret < (int)(mtdinfo.writesize + spare_size)) {
248 fprintf(stderr, "short write at %llx, %d\n", pos, ret);
249 close(outfd);
250 outfd = -1;
251 }
252 if (ecc.corrected != last_ecc.corrected)
253 fprintf(statusfile, "%08llx: ecc corrected\n", opos);
254 if (ecc.failed != last_ecc.failed)
255 fprintf(statusfile, "%08llx: ecc failed\n", opos);
256 if (bad_block == 1)
257 fprintf(statusfile, "%08llx: badblock\n", opos);
258 if (bad_block == 2)
259 fprintf(statusfile, "%08llx: read error\n", opos);
260 opos += mtdinfo.writesize + spare_size;
261 }
262
263 last_ecc = ecc;
264 page_count++;
265 if (test_empty(buffer, mtdinfo.writesize + mtdinfo.oobsize + spare_size))
266 empty_pages++;
267 else if (verbose > 2 || (verbose > 1 && !(pos & (mtdinfo.erasesize - 1))))
268 printf("page at %llx (%d oobbytes): %08x %08x %08x %08x "
269 "%08x %08x %08x %08x\n", pos, oobbuf.start,
270 oob_data[0], oob_data[1], oob_data[2], oob_data[3],
271 oob_data[4], oob_data[5], oob_data[6], oob_data[7]);
272 }
273
274 if (outfd >= 0) {
275 fprintf(statusfile, "read %d pages, %d empty\n", page_count, empty_pages);
276 fprintf(statusfile, "total ecc corrected, %u\n", ecc.corrected - initial_ecc.corrected);
277 fprintf(statusfile, "total ecc failed, %u\n", ecc.failed - initial_ecc.failed);
278 fprintf(statusfile, "total ecc badblocks, %u\n", ecc.badblocks - initial_ecc.badblocks);
279 fprintf(statusfile, "total ecc bbtblocks, %u\n", ecc.bbtblocks - initial_ecc.bbtblocks);
280 }
281 if (verbose) {
282 printf("total ecc corrected, %u\n", ecc.corrected - initial_ecc.corrected);
283 printf("total ecc failed, %u\n", ecc.failed - initial_ecc.failed);
284 printf("total ecc badblocks, %u\n", ecc.badblocks - initial_ecc.badblocks);
285 printf("total ecc bbtblocks, %u\n", ecc.bbtblocks - initial_ecc.bbtblocks);
286 }
287 printf("read %d pages, %d empty\n", page_count, empty_pages);
288
289 return 0;
290}