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#include "libbb.h"
51#include <mtd/mtd-user.h>
52
53#define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
54#define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
55
56#define OPT_p (1 << 0)
57#define OPT_o (1 << 0)
58#define OPT_s (1 << 1)
59#define OPT_b (1 << 2)
60#define OPT_f (1 << 3)
61#define OPT_l (1 << 4)
62
63
64static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
65{
66 unsigned char buf[meminfo->writesize];
67 unsigned count;
68
69
70 len = (len | ~(meminfo->writesize - 1)) + 1;
71
72 memset(buf, 0xff, sizeof(buf));
73 for (count = 0; count < len; count += meminfo->writesize) {
74 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
75 if (oob)
76 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
77 }
78}
79
80static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
81 unsigned block_offset)
82{
83 while (1) {
84 loff_t offs;
85
86 if (block_offset >= meminfo->size) {
87 if (IS_NANDWRITE)
88 bb_error_msg_and_die("not enough space in MTD device");
89 return block_offset;
90 }
91 offs = block_offset;
92 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
93 return block_offset;
94
95 if (IS_NANDWRITE)
96 printf("Skipping bad block at 0x%08x\n", block_offset);
97 block_offset += meminfo->erasesize;
98 }
99}
100
101int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
102int nandwrite_main(int argc UNUSED_PARAM, char **argv)
103{
104
105 unsigned char *oobbuf;
106 unsigned opts;
107 int fd;
108 ssize_t cnt;
109 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
110 unsigned end_addr = ~0;
111 struct mtd_info_user meminfo;
112 struct mtd_oob_buf oob;
113 unsigned char *filebuf;
114 const char *opt_s = "0", *opt_f = "-", *opt_l;
115
116 if (IS_NANDDUMP) {
117 opt_complementary = "=1";
118 opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l);
119 } else {
120 opt_complementary = "-1:?2";
121 opts = getopt32(argv, "ps:", &opt_s);
122 }
123 argv += optind;
124
125 if (IS_NANDWRITE && argv[1])
126 opt_f = argv[1];
127 if (!LONE_DASH(opt_f)) {
128 int tmp_fd = xopen(opt_f,
129 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
130 );
131 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
132 }
133
134 fd = xopen(argv[0], O_RDWR);
135 xioctl(fd, MEMGETINFO, &meminfo);
136
137 mtdoffset = xstrtou(opt_s, 0);
138 if (IS_NANDDUMP && (opts & OPT_l)) {
139 unsigned length = xstrtou(opt_l, 0);
140 if (length < meminfo.size - mtdoffset)
141 end_addr = mtdoffset + length;
142 }
143
144
145 meminfo_writesize = meminfo.writesize;
146
147 if (mtdoffset & (meminfo_writesize - 1))
148 bb_error_msg_and_die("start address is not page aligned");
149
150 filebuf = xmalloc(meminfo_writesize);
151 oobbuf = xmalloc(meminfo.oobsize);
152
153 oob.start = 0;
154 oob.length = meminfo.oobsize;
155 oob.ptr = oobbuf;
156
157 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
158 if (blockstart != mtdoffset) {
159 unsigned tmp;
160
161
162
163
164 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
165 if (tmp != blockstart) {
166
167 if (IS_NANDDUMP & !(opts & OPT_b)) {
168 int bad_len = MIN(tmp, end_addr) - mtdoffset;
169 dump_bad(&meminfo, bad_len, !(opts & OPT_o));
170 }
171 mtdoffset = tmp;
172 }
173 }
174
175 cnt = -1;
176 limit = MIN(meminfo.size, end_addr);
177 while (mtdoffset < limit) {
178 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
179 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
180
181 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
182 if (blockstart == mtdoffset) {
183
184 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
185 if (IS_NANDWRITE)
186 printf("Writing at 0x%08x\n", mtdoffset);
187 else if (mtdoffset > blockstart) {
188 int bad_len = MIN(mtdoffset, limit) - blockstart;
189 dump_bad(&meminfo, bad_len, !(opts & OPT_o));
190 }
191 if (mtdoffset >= limit)
192 break;
193 }
194 xlseek(fd, mtdoffset, SEEK_SET);
195
196
197 cnt = full_read(input_fd, filebuf, meminfo_writesize);
198 if (cnt == 0) {
199
200
201
202 break;
203 }
204 if (cnt < meminfo_writesize) {
205 if (IS_NANDDUMP)
206 bb_error_msg_and_die("short read");
207 if (!(opts & OPT_p))
208 bb_error_msg_and_die("input size is not rounded up to page size, "
209 "use -p to zero pad");
210
211 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
212 }
213 xwrite(output_fd, filebuf, meminfo_writesize);
214
215 if (IS_NANDDUMP && !(opts & OPT_o)) {
216
217 oob.start = mtdoffset;
218 xioctl(fd, MEMREADOOB, &oob);
219 xwrite(output_fd, oobbuf, meminfo.oobsize);
220 }
221
222 mtdoffset += meminfo_writesize;
223 if (cnt < meminfo_writesize)
224 break;
225 }
226
227 if (IS_NANDWRITE && cnt != 0) {
228
229 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
230
231 bb_error_msg_and_die("not enough space in MTD device");
232 }
233 }
234
235 if (ENABLE_FEATURE_CLEAN_UP) {
236 free(filebuf);
237 close(fd);
238 }
239
240 return EXIT_SUCCESS;
241}
242