2 * nandwrite and nanddump ported to busybox from mtd-utils
4 * Author: Baruch Siach <baruch@tkos.co.il>, Orex Computed Radiography
6 * Licensed under GPLv2, see file LICENSE in this source tree.
8 * TODO: add support for large (>4GB) MTD devices
11 //config:config NANDWRITE
12 //config: bool "nandwrite"
14 //config: select PLATFORM_LINUX
16 //config: Write to the specified MTD device, with bad blocks awareness
18 //config:config NANDDUMP
19 //config: bool "nanddump"
21 //config: select PLATFORM_LINUX
23 //config: Dump the content of raw NAND chip
25 //applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
26 //applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
28 //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
29 //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
31 //usage:#define nandwrite_trivial_usage
32 //usage: "[-p] [-s ADDR] MTD_DEVICE [FILE]"
33 //usage:#define nandwrite_full_usage "\n\n"
34 //usage: "Write to MTD_DEVICE\n"
35 //usage: "\n -p Pad to page size"
36 //usage: "\n -s ADDR Start address"
38 //usage:#define nanddump_trivial_usage
39 //usage: "[-o] [-b|--bb=padbad|skipbad] [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
40 //usage:#define nanddump_full_usage "\n\n"
41 //usage: "Dump MTD_DEVICE\n"
42 //usage: "\n -o Dump oob data"
43 //usage: "\n -b Omit bad block from the dump"
44 //usage: "\n -s ADDR Start address"
45 //usage: "\n -l LEN Length"
46 //usage: "\n -f FILE Dump to file ('-' for stdout)"
47 //usage: "\n --bb=METHOD:"
48 //usage: "\n skipbad: skip bad blocks"
49 //usage: "\n padbad: substitute bad blocks by 0xff (default)"
50 //usage: "\n The difference between omit and skip bad block is that in the omit"
51 //usage: "\n case, the length of the bad block is counted as part of the total"
52 //usage: "\n dump length, and in the skip case, it's not."
55 #include <mtd/mtd-user.h>
57 #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
58 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
60 #define OPT_p (1 << 0) /* nandwrite only */
61 #define OPT_o (1 << 0) /* nanddump only */
62 #define OPT_s (1 << 1)
63 #define OPT_b (1 << 2)
64 #define OPT_f (1 << 3)
65 #define OPT_l (1 << 4)
66 #define OPT_bb (1 << 5) /* must be the last one in the list */
68 #define BB_PADBAD (1 << 0)
69 #define BB_SKIPBAD (1 << 1)
70 #define BB_OMITBAD (1 << 2)
72 /* helper for writing out 0xff for bad blocks pad */
73 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
75 unsigned char buf[meminfo->writesize];
78 /* round len to the next page only if len is not already on a page */
79 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
81 memset(buf, 0xff, sizeof(buf));
82 for (count = 0; count < len; count += meminfo->writesize) {
83 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
85 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
89 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
90 unsigned block_offset)
95 if (block_offset >= meminfo->size) {
97 bb_error_msg_and_die("not enough space in MTD device");
98 return block_offset; /* let the caller exit */
101 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
103 /* ioctl returned 1 => "bad block" */
105 printf("Skipping bad block at 0x%08x\n", block_offset);
106 block_offset += meminfo->erasesize;
110 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
111 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
113 /* Buffer for OOB data */
114 unsigned char *oobbuf;
116 unsigned bb_method = BB_PADBAD;
119 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
120 unsigned end_addr = ~0;
121 struct mtd_info_user meminfo;
122 struct mtd_oob_buf oob;
123 unsigned char *filebuf;
124 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
125 static const char nanddump_longopts[] ALIGN1 =
126 "bb\0" Required_argument "\xff"; /* no short equivalent */
129 opt_complementary = "=1";
130 applet_long_options = nanddump_longopts;
131 opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l, &opt_bb);
132 } else { /* nandwrite */
133 opt_complementary = "-1:?2";
134 opts = getopt32(argv, "ps:", &opt_s);
138 if (IS_NANDWRITE && argv[1])
140 if (!LONE_DASH(opt_f)) {
141 int tmp_fd = xopen(opt_f,
142 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
144 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
147 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
148 xioctl(fd, MEMGETINFO, &meminfo);
150 mtdoffset = xstrtou(opt_s, 0);
151 if (IS_NANDDUMP && (opts & OPT_l)) {
152 unsigned length = xstrtou(opt_l, 0);
153 if (length < meminfo.size - mtdoffset)
154 end_addr = mtdoffset + length;
157 if ((opts & OPT_b) && (opts & OPT_bb))
160 bb_method = BB_OMITBAD;
162 if (!strcmp("skipbad", opt_bb))
163 bb_method = BB_SKIPBAD;
164 else if (!strcmp("padbad", opt_bb))
165 bb_method = BB_PADBAD;
171 /* Pull it into a CPU register (hopefully) - smaller code that way */
172 meminfo_writesize = meminfo.writesize;
174 if (mtdoffset & (meminfo_writesize - 1))
175 bb_error_msg_and_die("start address is not page aligned");
177 filebuf = xmalloc(meminfo_writesize);
178 oobbuf = xmalloc(meminfo.oobsize);
181 oob.length = meminfo.oobsize;
184 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
185 if (blockstart != mtdoffset) {
187 /* mtdoffset is in the middle of an erase block, verify that
188 * this block is OK. Advance mtdoffset only if this block is
191 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
192 if (tmp != blockstart) {
193 /* bad block(s), advance mtdoffset */
195 if (bb_method == BB_PADBAD) {
196 int bad_len = MIN(tmp, end_addr) - mtdoffset;
197 dump_bad(&meminfo, bad_len, opts & OPT_o);
199 /* with option skipbad, increase the total length */
200 if (bb_method == BB_SKIPBAD) {
201 end_addr += (tmp - blockstart);
203 /* omitbad: do nothing */
210 limit = MIN(meminfo.size, end_addr);
211 while (mtdoffset < limit) {
212 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
213 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
215 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
216 if (blockstart == mtdoffset) {
217 /* starting a new eraseblock */
218 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
220 printf("Writing at 0x%08x\n", mtdoffset);
221 else if (mtdoffset > blockstart) {
222 if (bb_method == BB_PADBAD) {
223 /* dump FF padded bad block */
224 int bad_len = MIN(mtdoffset, limit) - blockstart;
225 dump_bad(&meminfo, bad_len, opts & OPT_o);
226 } else if (bb_method == BB_SKIPBAD) {
227 /* for skipbad, increase the length */
228 if ((end_addr + mtdoffset - blockstart) > end_addr)
229 end_addr += (mtdoffset - blockstart);
232 limit = MIN(meminfo.size, end_addr);
234 /* omitbad: do nothing */
236 if (mtdoffset >= limit)
239 xlseek(fd, mtdoffset, SEEK_SET);
241 /* get some more data from input */
242 cnt = full_read(input_fd, filebuf, meminfo_writesize);
244 /* even with -p, we do not pad past the end of input
245 * (-p only zero-pads last incomplete page)
249 if (cnt < meminfo_writesize) {
251 bb_error_msg_and_die("short read");
253 bb_error_msg_and_die("input size is not rounded up to page size, "
254 "use -p to zero pad");
255 /* zero pad to end of write block */
256 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
258 xwrite(output_fd, filebuf, meminfo_writesize);
260 if (IS_NANDDUMP && (opts & OPT_o)) {
262 oob.start = mtdoffset;
263 xioctl(fd, MEMREADOOB, &oob);
264 xwrite(output_fd, oobbuf, meminfo.oobsize);
267 mtdoffset += meminfo_writesize;
268 if (cnt < meminfo_writesize)
272 if (IS_NANDWRITE && cnt != 0) {
273 /* We filled entire MTD, but did we reach EOF on input? */
274 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
276 bb_error_msg_and_die("not enough space in MTD device");
280 if (ENABLE_FEATURE_CLEAN_UP) {