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 (5.9 kb)"
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 (6.3 kb)"
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: "[-np] [-s ADDR] MTD_DEVICE [FILE]"
33 //usage:#define nandwrite_full_usage "\n\n"
34 //usage: "Write to MTD_DEVICE\n"
35 //usage: "\n -n Write without ecc"
36 //usage: "\n -p Pad to page size"
37 //usage: "\n -s ADDR Start address"
39 //usage:#define nanddump_trivial_usage
40 //usage: "[-no]" IF_LONG_OPTS(" [--bb padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
41 //usage:#define nanddump_full_usage "\n\n"
42 //usage: "Dump MTD_DEVICE\n"
43 //usage: "\n -n Read without ecc"
44 //usage: "\n -o Dump oob data"
45 //usage: "\n -s ADDR Start address"
46 //usage: "\n -l LEN Length"
47 //usage: "\n -f FILE Dump to file ('-' for stdout)"
48 //usage: IF_LONG_OPTS(
49 //usage: "\n --bb METHOD"
50 //usage: "\n skipbad: skip bad blocks"
51 //usage: "\n padbad: substitute bad blocks by 0xff (default)"
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_n (1 << 1)
63 #define OPT_s (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)
71 /* helper for writing out 0xff for bad blocks pad */
72 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
74 unsigned char buf[meminfo->writesize];
77 /* round len to the next page only if len is not already on a page */
78 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
80 memset(buf, 0xff, sizeof(buf));
81 for (count = 0; count < len; count += meminfo->writesize) {
82 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
84 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
88 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
89 unsigned block_offset)
94 if (block_offset >= meminfo->size) {
96 bb_error_msg_and_die("not enough space in MTD device");
97 return block_offset; /* let the caller exit */
100 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
102 /* ioctl returned 1 => "bad block" */
104 printf("Skipping bad block at 0x%08x\n", block_offset);
105 block_offset += meminfo->erasesize;
109 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
110 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
112 /* Buffer for OOB data */
113 unsigned char *oobbuf;
115 unsigned bb_method = BB_SKIPBAD;
118 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
119 unsigned end_addr = ~0;
120 struct mtd_info_user meminfo;
121 struct mtd_oob_buf oob;
122 unsigned char *filebuf;
123 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
126 opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
127 "bb\0" Required_argument "\xff", /* no short equivalent */
128 &opt_s, &opt_f, &opt_l, &opt_bb
130 } else { /* nandwrite */
131 opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s);
135 if (IS_NANDWRITE && argv[1])
137 if (!LONE_DASH(opt_f)) {
138 int tmp_fd = xopen(opt_f,
139 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
141 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
144 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
145 xioctl(fd, MEMGETINFO, &meminfo);
148 xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
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;
156 if (IS_NANDDUMP && (opts & OPT_bb)) {
157 if (strcmp("skipbad", opt_bb) == 0)
158 bb_method = BB_SKIPBAD;
159 else if (strcmp("padbad", opt_bb) == 0)
160 bb_method = BB_PADBAD;
165 /* Pull it into a CPU register (hopefully) - smaller code that way */
166 meminfo_writesize = meminfo.writesize;
168 if (mtdoffset & (meminfo_writesize - 1))
169 bb_error_msg_and_die("start address is not page aligned");
171 filebuf = xmalloc(meminfo_writesize);
172 oobbuf = xmalloc(meminfo.oobsize);
175 oob.length = meminfo.oobsize;
178 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
179 if (blockstart != mtdoffset) {
181 /* mtdoffset is in the middle of an erase block, verify that
182 * this block is OK. Advance mtdoffset only if this block is
185 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
186 if (tmp != blockstart) {
187 /* bad block(s), advance mtdoffset */
189 if (bb_method == BB_PADBAD) {
190 int bad_len = MIN(tmp, end_addr) - mtdoffset;
191 dump_bad(&meminfo, bad_len, opts & OPT_o);
193 /* with option skipbad, increase the total length */
194 if (bb_method == BB_SKIPBAD) {
195 end_addr += (tmp - blockstart);
203 limit = MIN(meminfo.size, end_addr);
204 while (mtdoffset < limit) {
205 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
206 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
208 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
209 if (blockstart == mtdoffset) {
210 /* starting a new eraseblock */
211 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
213 printf("Writing at 0x%08x\n", mtdoffset);
214 else if (mtdoffset > blockstart) {
215 if (bb_method == BB_PADBAD) {
216 /* dump FF padded bad block */
217 int bad_len = MIN(mtdoffset, limit) - blockstart;
218 dump_bad(&meminfo, bad_len, opts & OPT_o);
219 } else if (bb_method == BB_SKIPBAD) {
220 /* for skipbad, increase the length */
221 if ((end_addr + mtdoffset - blockstart) > end_addr)
222 end_addr += (mtdoffset - blockstart);
225 limit = MIN(meminfo.size, end_addr);
228 if (mtdoffset >= limit)
231 xlseek(fd, mtdoffset, SEEK_SET);
233 /* get some more data from input */
234 cnt = full_read(input_fd, filebuf, meminfo_writesize);
236 /* even with -p, we do not pad past the end of input
237 * (-p only zero-pads last incomplete page)
241 if (cnt < meminfo_writesize) {
243 bb_error_msg_and_die("short read");
245 bb_error_msg_and_die("input size is not rounded up to page size, "
246 "use -p to zero pad");
247 /* zero pad to end of write block */
248 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
250 xwrite(output_fd, filebuf, meminfo_writesize);
252 if (IS_NANDDUMP && (opts & OPT_o)) {
254 oob.start = mtdoffset;
255 xioctl(fd, MEMREADOOB, &oob);
256 xwrite(output_fd, oobbuf, meminfo.oobsize);
259 mtdoffset += meminfo_writesize;
260 if (cnt < meminfo_writesize)
264 if (IS_NANDWRITE && cnt != 0) {
265 /* We filled entire MTD, but did we reach EOF on input? */
266 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
268 bb_error_msg_and_die("not enough space in MTD device");
272 if (ENABLE_FEATURE_CLEAN_UP) {