-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan_bad.c
144 lines (122 loc) · 3.5 KB
/
scan_bad.c
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
/*
* nanddump.c
*
* Copyright (C) 2000 David Woodhouse ([email protected])
* Steven J. Hill ([email protected])
*
* 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.
*
* Overview:
* This utility dumps the contents of raw NAND chips or NAND
* chips contained in DoC devices.
*/
#define _GNU_SOURCE
#include <byteswap.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <mtd/mtd-user.h>
#define PROGRAM "nanddump"
#define VERSION "$Revision: 1.29 $"
static void display_help (void)
{
printf(
"Usage: scanbad [OPTIONS] MTD-device\n"
"Scan for bad blocks.\n"
"\n"
);
exit(EXIT_SUCCESS);
}
// Option variables
static const char *mtddev; // mtd device name
static unsigned char read_marker(mtd_info_t *meminfo, int fd, int offset)
{
unsigned char oobbuf[128];
struct mtd_oob_buf oob;
oob.start = offset;
oob.length = 16;
oob.ptr = oobbuf;
oob.length = meminfo->oobsize;
memset(oobbuf, 0x42, sizeof(oobbuf));
int rc = ioctl(fd, MEMREADOOB, &oob);
if (rc < 0) {
perror("MEMREADOOB");
exit(1);
}
return oobbuf[0];
}
static void report_failure(int eraseblock, int page, unsigned oob, int *count)
{
(*count)++;
printf("eraseblock %d page %d oob[0] = 0x%x\n",
eraseblock, page, oob);
}
int main(int argc, char * const argv[])
{
int fd;
mtd_info_t meminfo;
if ( argc != 2 )
display_help();
mtddev = argv[1];
/* Open MTD device */
if ((fd = open(mtddev, O_RDONLY)) == -1) {
perror(mtddev);
exit (EXIT_FAILURE);
}
printf("scanning %s\n", mtddev);
/* Fill in MTD device capability structure */
if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
perror("MEMGETINFO");
close(fd);
exit (EXIT_FAILURE);
}
/* Make sure device page sizes are valid */
if (!(meminfo.oobsize == 128 && meminfo.writesize == 4096) &&
!(meminfo.oobsize == 64 && meminfo.writesize == 2048) &&
!(meminfo.oobsize == 32 && meminfo.writesize == 1024) &&
!(meminfo.oobsize == 16 && meminfo.writesize == 512) &&
!(meminfo.oobsize == 8 && meminfo.writesize == 256)) {
fprintf(stderr, "Unknown flash (not normal NAND)\n");
close(fd);
exit(EXIT_FAILURE);
}
int pagesize = meminfo.writesize;
int last_page = meminfo.erasesize / pagesize - 1;
int total_erase_blocks = meminfo.size / meminfo.erasesize;
int eraseblock;
printf("%d erase blocks of %d pages size %d bytes\n",
total_erase_blocks, meminfo.erasesize / pagesize,
pagesize);
int count = 0;
int pages_checked = 0;
for (eraseblock = 0; eraseblock < total_erase_blocks; eraseblock++)
{
unsigned offset = eraseblock * meminfo.erasesize;
unsigned char byte0 = read_marker(&meminfo, fd, offset + 0 * pagesize);
unsigned char byte1 = read_marker(&meminfo, fd, offset + 1 * pagesize);
unsigned char byte2 = read_marker(&meminfo, fd, offset + last_page * pagesize);
pages_checked += 3;
if (byte0 != 0xff)
report_failure(eraseblock, 0, byte0, &count);
if (byte1 != 0xff)
report_failure(eraseblock, 1, byte1, &count);
if (byte2 != 0xff)
report_failure(eraseblock, last_page, byte2, &count);
}
printf("%d bad blocks\n", count);
printf("checked %d pages\n", pages_checked);
close(fd);
return 0;
}