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
10 //config:config NANDWRITE
11 //config: bool "nandwrite (5.9 kb)"
13 //config: select PLATFORM_LINUX
15 //config: Write to the specified MTD device, with bad blocks awareness
17 //config:config NANDDUMP
18 //config: bool "nanddump (6.3 kb)"
20 //config: select PLATFORM_LINUX
22 //config: Dump the content of raw NAND chip
24 //applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
25 //applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
27 //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
28 //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
30 //usage:#define nandwrite_trivial_usage
31 //usage: "[-np] [-s ADDR] MTD_DEVICE [FILE]"
32 //usage:#define nandwrite_full_usage "\n\n"
33 //usage: "Write to MTD_DEVICE\n"
34 //usage: "\n -n Write without ecc"
35 //usage: "\n -p Pad to page size"
36 //usage: "\n -s ADDR Start address"
38 //usage:#define nanddump_trivial_usage
39 //usage: "[-no]" IF_LONG_OPTS(" [--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 -n Read without ecc"
43 //usage: "\n -o Dump oob data"
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: IF_LONG_OPTS(
48 //usage: "\n --bb METHOD"
49 //usage: "\n skipbad: skip bad blocks"
50 //usage: "\n padbad: substitute bad blocks by 0xff (default)"
54 #include <mtd/mtd-user.h>
56 #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
57 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
59 #define OPT_p (1 << 0) /* nandwrite only */
60 #define OPT_o (1 << 0) /* nanddump only */
61 #define OPT_n (1 << 1)
62 #define OPT_s (1 << 2)
63 #define OPT_f (1 << 3)
64 #define OPT_l (1 << 4)
65 #define OPT_bb (1 << 5) /* must be the last one in the list */
67 #define BB_PADBAD (1 << 0)
68 #define BB_SKIPBAD (1 << 1)
70 /* helper for writing out 0xff for bad blocks pad */
71 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
73 unsigned char buf[meminfo->writesize];
76 /* round len to the next page only if len is not already on a page */
77 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
79 memset(buf, 0xff, sizeof(buf));
80 for (count = 0; count < len; count += meminfo->writesize) {
81 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
83 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
87 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
88 unsigned block_offset)
93 if (block_offset >= meminfo->size) {
95 bb_error_msg_and_die("not enough space in MTD device");
96 return block_offset; /* let the caller exit */
99 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
101 /* ioctl returned 1 => "bad block" */
103 printf("Skipping bad block at 0x%08x\n", block_offset);
104 block_offset += meminfo->erasesize;
108 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
109 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
111 /* Buffer for OOB data */
112 unsigned char *oobbuf;
114 unsigned bb_method = BB_SKIPBAD;
117 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
118 unsigned end_addr = ~0;
119 struct mtd_info_user meminfo;
120 struct mtd_oob_buf oob;
121 unsigned char *filebuf;
122 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
125 opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
126 "bb\0" Required_argument "\xff", /* no short equivalent */
127 &opt_s, &opt_f, &opt_l, &opt_bb
129 } else { /* nandwrite */
130 opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s);
134 if (IS_NANDWRITE && argv[1])
136 if (!LONE_DASH(opt_f)) {
137 int tmp_fd = xopen(opt_f,
138 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
140 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
143 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
144 xioctl(fd, MEMGETINFO, &meminfo);
147 xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
149 mtdoffset = xstrtou(opt_s, 0);
150 if (IS_NANDDUMP && (opts & OPT_l)) {
151 unsigned length = xstrtou(opt_l, 0);
152 if (length < meminfo.size - mtdoffset)
153 end_addr = mtdoffset + length;
155 if (IS_NANDDUMP && (opts & OPT_bb)) {
156 if (strcmp("skipbad", opt_bb) == 0)
157 bb_method = BB_SKIPBAD;
158 else if (strcmp("padbad", opt_bb) == 0)
159 bb_method = BB_PADBAD;
164 /* Pull it into a CPU register (hopefully) - smaller code that way */
165 meminfo_writesize = meminfo.writesize;
167 if (mtdoffset & (meminfo_writesize - 1))
168 bb_error_msg_and_die("start address is not page aligned");
170 filebuf = xmalloc(meminfo_writesize);
171 oobbuf = xmalloc(meminfo.oobsize);
174 oob.length = meminfo.oobsize;
177 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
178 if (blockstart != mtdoffset) {
180 /* mtdoffset is in the middle of an erase block, verify that
181 * this block is OK. Advance mtdoffset only if this block is
184 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
185 if (tmp != blockstart) {
186 /* bad block(s), advance mtdoffset */
188 if (bb_method == BB_PADBAD) {
189 int bad_len = MIN(tmp, end_addr) - mtdoffset;
190 dump_bad(&meminfo, bad_len, opts & OPT_o);
192 /* with option skipbad, increase the total length */
193 if (bb_method == BB_SKIPBAD) {
194 end_addr += (tmp - blockstart);
202 limit = MIN(meminfo.size, end_addr);
203 while (mtdoffset < limit) {
204 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
205 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
207 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
208 if (blockstart == mtdoffset) {
209 /* starting a new eraseblock */
210 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
212 printf("Writing at 0x%08x\n", mtdoffset);
213 else if (mtdoffset > blockstart) {
214 if (bb_method == BB_PADBAD) {
215 /* dump FF padded bad block */
216 int bad_len = MIN(mtdoffset, limit) - blockstart;
217 dump_bad(&meminfo, bad_len, opts & OPT_o);
218 } else if (bb_method == BB_SKIPBAD) {
219 /* for skipbad, increase the length */
220 if ((end_addr + mtdoffset - blockstart) > end_addr)
221 end_addr += (mtdoffset - blockstart);
224 limit = MIN(meminfo.size, end_addr);
227 if (mtdoffset >= limit)
230 xlseek(fd, mtdoffset, SEEK_SET);
232 /* get some more data from input */
233 cnt = full_read(input_fd, filebuf, meminfo_writesize);
235 /* even with -p, we do not pad past the end of input
236 * (-p only zero-pads last incomplete page)
240 if (cnt < meminfo_writesize) {
242 bb_error_msg_and_die("short read");
244 bb_error_msg_and_die("input size is not rounded up to page size, "
245 "use -p to zero pad");
246 /* zero pad to end of write block */
247 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
249 xwrite(output_fd, filebuf, meminfo_writesize);
251 if (IS_NANDDUMP && (opts & OPT_o)) {
253 oob.start = mtdoffset;
254 xioctl(fd, MEMREADOOB, &oob);
255 xwrite(output_fd, oobbuf, meminfo.oobsize);
258 mtdoffset += meminfo_writesize;
259 if (cnt < meminfo_writesize)
263 if (IS_NANDWRITE && cnt != 0) {
264 /* We filled entire MTD, but did we reach EOF on input? */
265 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
267 bb_error_msg_and_die("not enough space in MTD device");
271 if (ENABLE_FEATURE_CLEAN_UP) {