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