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] [--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 -s ADDR Start address"
44 //usage: "\n -l LEN Length"
45 //usage: "\n -f FILE Dump to file ('-' for stdout)"
46 //usage: "\n --bb=METHOD:"
47 //usage: "\n skipbad: skip bad blocks"
48 //usage: "\n padbad: substitute bad blocks by 0xff (default)"
51 #include <mtd/mtd-user.h>
53 #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
54 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
56 #define OPT_p (1 << 0) /* nandwrite only */
57 #define OPT_o (1 << 0) /* nanddump only */
58 #define OPT_s (1 << 1)
59 #define OPT_f (1 << 2)
60 #define OPT_l (1 << 3)
61 #define OPT_bb (1 << 4) /* must be the last one in the list */
63 #define BB_PADBAD (1 << 0)
64 #define BB_SKIPBAD (1 << 1)
66 /* helper for writing out 0xff for bad blocks pad */
67 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
69 unsigned char buf[meminfo->writesize];
72 /* round len to the next page only if len is not already on a page */
73 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
75 memset(buf, 0xff, sizeof(buf));
76 for (count = 0; count < len; count += meminfo->writesize) {
77 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
79 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
83 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
84 unsigned block_offset)
89 if (block_offset >= meminfo->size) {
91 bb_error_msg_and_die("not enough space in MTD device");
92 return block_offset; /* let the caller exit */
95 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
97 /* ioctl returned 1 => "bad block" */
99 printf("Skipping bad block at 0x%08x\n", block_offset);
100 block_offset += meminfo->erasesize;
104 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
107 /* Buffer for OOB data */
108 unsigned char *oobbuf;
110 unsigned bb_method = BB_PADBAD;
113 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
114 unsigned end_addr = ~0;
115 struct mtd_info_user meminfo;
116 struct mtd_oob_buf oob;
117 unsigned char *filebuf;
118 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
119 static const char nanddump_longopts[] ALIGN1 =
120 "bb\0" Required_argument "\xff"; /* no short equivalent */
123 opt_complementary = "=1";
124 applet_long_options = nanddump_longopts;
125 opts = getopt32(argv, "os:f:l:", &opt_s, &opt_f, &opt_l, &opt_bb);
126 } else { /* nandwrite */
127 opt_complementary = "-1:?2";
128 opts = getopt32(argv, "ps:", &opt_s);
132 if (IS_NANDWRITE && argv[1])
134 if (!LONE_DASH(opt_f)) {
135 int tmp_fd = xopen(opt_f,
136 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
138 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
141 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
142 xioctl(fd, MEMGETINFO, &meminfo);
144 mtdoffset = xstrtou(opt_s, 0);
145 if (IS_NANDDUMP && (opts & OPT_l)) {
146 unsigned length = xstrtou(opt_l, 0);
147 if (length < meminfo.size - mtdoffset)
148 end_addr = mtdoffset + length;
150 if (IS_NANDDUMP && (opts & OPT_bb)) {
151 if (strcmp("skipbad", opt_bb) == 0)
152 bb_method = BB_SKIPBAD;
153 else if (strcmp("padbad", opt_bb) == 0)
154 bb_method = BB_PADBAD;
159 /* Pull it into a CPU register (hopefully) - smaller code that way */
160 meminfo_writesize = meminfo.writesize;
162 if (mtdoffset & (meminfo_writesize - 1))
163 bb_error_msg_and_die("start address is not page aligned");
165 filebuf = xmalloc(meminfo_writesize);
166 oobbuf = xmalloc(meminfo.oobsize);
169 oob.length = meminfo.oobsize;
172 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
173 if (blockstart != mtdoffset) {
175 /* mtdoffset is in the middle of an erase block, verify that
176 * this block is OK. Advance mtdoffset only if this block is
179 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
180 if (tmp != blockstart) {
181 /* bad block(s), advance mtdoffset */
183 if (bb_method == BB_PADBAD) {
184 int bad_len = MIN(tmp, end_addr) - mtdoffset;
185 dump_bad(&meminfo, bad_len, opts & OPT_o);
187 /* with option skipbad, increase the total length */
188 if (bb_method == BB_SKIPBAD) {
189 end_addr += (tmp - blockstart);
197 limit = MIN(meminfo.size, end_addr);
198 while (mtdoffset < limit) {
199 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
200 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
202 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
203 if (blockstart == mtdoffset) {
204 /* starting a new eraseblock */
205 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
207 printf("Writing at 0x%08x\n", mtdoffset);
208 else if (mtdoffset > blockstart) {
209 if (bb_method == BB_PADBAD) {
210 /* dump FF padded bad block */
211 int bad_len = MIN(mtdoffset, limit) - blockstart;
212 dump_bad(&meminfo, bad_len, opts & OPT_o);
213 } else if (bb_method == BB_SKIPBAD) {
214 /* for skipbad, increase the length */
215 if ((end_addr + mtdoffset - blockstart) > end_addr)
216 end_addr += (mtdoffset - blockstart);
219 limit = MIN(meminfo.size, end_addr);
222 if (mtdoffset >= limit)
225 xlseek(fd, mtdoffset, SEEK_SET);
227 /* get some more data from input */
228 cnt = full_read(input_fd, filebuf, meminfo_writesize);
230 /* even with -p, we do not pad past the end of input
231 * (-p only zero-pads last incomplete page)
235 if (cnt < meminfo_writesize) {
237 bb_error_msg_and_die("short read");
239 bb_error_msg_and_die("input size is not rounded up to page size, "
240 "use -p to zero pad");
241 /* zero pad to end of write block */
242 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
244 xwrite(output_fd, filebuf, meminfo_writesize);
246 if (IS_NANDDUMP && (opts & OPT_o)) {
248 oob.start = mtdoffset;
249 xioctl(fd, MEMREADOOB, &oob);
250 xwrite(output_fd, oobbuf, meminfo.oobsize);
253 mtdoffset += meminfo_writesize;
254 if (cnt < meminfo_writesize)
258 if (IS_NANDWRITE && cnt != 0) {
259 /* We filled entire MTD, but did we reach EOF on input? */
260 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
262 bb_error_msg_and_die("not enough space in MTD device");
266 if (ENABLE_FEATURE_CLEAN_UP) {