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