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]" 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 -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: IF_LONG_OPTS(
47 //usage: "\n --bb=METHOD:"
48 //usage: "\n skipbad: skip bad blocks"
49 //usage: "\n padbad: substitute bad blocks by 0xff (default)"
53 #include <mtd/mtd-user.h>
55 #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
56 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
58 #define OPT_p (1 << 0) /* nandwrite only */
59 #define OPT_o (1 << 0) /* nanddump only */
60 #define OPT_s (1 << 1)
61 #define OPT_f (1 << 2)
62 #define OPT_l (1 << 3)
63 #define OPT_bb (1 << 4) /* must be the last one in the list */
65 #define BB_PADBAD (1 << 0)
66 #define BB_SKIPBAD (1 << 1)
68 /* helper for writing out 0xff for bad blocks pad */
69 static void dump_bad(struct mtd_info_user
*meminfo
, unsigned len
, int oob
)
71 unsigned char buf
[meminfo
->writesize
];
74 /* round len to the next page only if len is not already on a page */
75 len
= ((len
- 1) | (meminfo
->writesize
- 1)) + 1;
77 memset(buf
, 0xff, sizeof(buf
));
78 for (count
= 0; count
< len
; count
+= meminfo
->writesize
) {
79 xwrite(STDOUT_FILENO
, buf
, meminfo
->writesize
);
81 xwrite(STDOUT_FILENO
, buf
, meminfo
->oobsize
);
85 static unsigned next_good_eraseblock(int fd
, struct mtd_info_user
*meminfo
,
86 unsigned block_offset
)
91 if (block_offset
>= meminfo
->size
) {
93 bb_error_msg_and_die("not enough space in MTD device");
94 return block_offset
; /* let the caller exit */
97 if (xioctl(fd
, MEMGETBADBLOCK
, &offs
) == 0)
99 /* ioctl returned 1 => "bad block" */
101 printf("Skipping bad block at 0x%08x\n", block_offset
);
102 block_offset
+= meminfo
->erasesize
;
106 int nandwrite_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
107 int nandwrite_main(int argc UNUSED_PARAM
, char **argv
)
109 /* Buffer for OOB data */
110 unsigned char *oobbuf
;
112 unsigned bb_method
= BB_SKIPBAD
;
115 unsigned mtdoffset
, meminfo_writesize
, blockstart
, limit
;
116 unsigned end_addr
= ~0;
117 struct mtd_info_user meminfo
;
118 struct mtd_oob_buf oob
;
119 unsigned char *filebuf
;
120 const char *opt_s
= "0", *opt_f
= "-", *opt_l
, *opt_bb
;
123 opt_complementary
= "=1";
125 applet_long_options
=
126 "bb\0" Required_argument
"\xff"; /* no short equivalent */
128 opts
= getopt32(argv
, "os:f:l:", &opt_s
, &opt_f
, &opt_l
, &opt_bb
);
129 } else { /* nandwrite */
130 opt_complementary
= "-1:?2";
131 opts
= getopt32(argv
, "ps:", &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
);
147 mtdoffset
= xstrtou(opt_s
, 0);
148 if (IS_NANDDUMP
&& (opts
& OPT_l
)) {
149 unsigned length
= xstrtou(opt_l
, 0);
150 if (length
< meminfo
.size
- mtdoffset
)
151 end_addr
= mtdoffset
+ length
;
153 if (IS_NANDDUMP
&& (opts
& OPT_bb
)) {
154 if (strcmp("skipbad", opt_bb
) == 0)
155 bb_method
= BB_SKIPBAD
;
156 else if (strcmp("padbad", opt_bb
) == 0)
157 bb_method
= BB_PADBAD
;
162 /* Pull it into a CPU register (hopefully) - smaller code that way */
163 meminfo_writesize
= meminfo
.writesize
;
165 if (mtdoffset
& (meminfo_writesize
- 1))
166 bb_error_msg_and_die("start address is not page aligned");
168 filebuf
= xmalloc(meminfo_writesize
);
169 oobbuf
= xmalloc(meminfo
.oobsize
);
172 oob
.length
= meminfo
.oobsize
;
175 blockstart
= mtdoffset
& ~(meminfo
.erasesize
- 1);
176 if (blockstart
!= mtdoffset
) {
178 /* mtdoffset is in the middle of an erase block, verify that
179 * this block is OK. Advance mtdoffset only if this block is
182 tmp
= next_good_eraseblock(fd
, &meminfo
, blockstart
);
183 if (tmp
!= blockstart
) {
184 /* bad block(s), advance mtdoffset */
186 if (bb_method
== BB_PADBAD
) {
187 int bad_len
= MIN(tmp
, end_addr
) - mtdoffset
;
188 dump_bad(&meminfo
, bad_len
, opts
& OPT_o
);
190 /* with option skipbad, increase the total length */
191 if (bb_method
== BB_SKIPBAD
) {
192 end_addr
+= (tmp
- blockstart
);
200 limit
= MIN(meminfo
.size
, end_addr
);
201 while (mtdoffset
< limit
) {
202 int input_fd
= IS_NANDWRITE
? STDIN_FILENO
: fd
;
203 int output_fd
= IS_NANDWRITE
? fd
: STDOUT_FILENO
;
205 blockstart
= mtdoffset
& ~(meminfo
.erasesize
- 1);
206 if (blockstart
== mtdoffset
) {
207 /* starting a new eraseblock */
208 mtdoffset
= next_good_eraseblock(fd
, &meminfo
, blockstart
);
210 printf("Writing at 0x%08x\n", mtdoffset
);
211 else if (mtdoffset
> blockstart
) {
212 if (bb_method
== BB_PADBAD
) {
213 /* dump FF padded bad block */
214 int bad_len
= MIN(mtdoffset
, limit
) - blockstart
;
215 dump_bad(&meminfo
, bad_len
, opts
& OPT_o
);
216 } else if (bb_method
== BB_SKIPBAD
) {
217 /* for skipbad, increase the length */
218 if ((end_addr
+ mtdoffset
- blockstart
) > end_addr
)
219 end_addr
+= (mtdoffset
- blockstart
);
222 limit
= MIN(meminfo
.size
, end_addr
);
225 if (mtdoffset
>= limit
)
228 xlseek(fd
, mtdoffset
, SEEK_SET
);
230 /* get some more data from input */
231 cnt
= full_read(input_fd
, filebuf
, meminfo_writesize
);
233 /* even with -p, we do not pad past the end of input
234 * (-p only zero-pads last incomplete page)
238 if (cnt
< meminfo_writesize
) {
240 bb_error_msg_and_die("short read");
242 bb_error_msg_and_die("input size is not rounded up to page size, "
243 "use -p to zero pad");
244 /* zero pad to end of write block */
245 memset(filebuf
+ cnt
, 0, meminfo_writesize
- cnt
);
247 xwrite(output_fd
, filebuf
, meminfo_writesize
);
249 if (IS_NANDDUMP
&& (opts
& OPT_o
)) {
251 oob
.start
= mtdoffset
;
252 xioctl(fd
, MEMREADOOB
, &oob
);
253 xwrite(output_fd
, oobbuf
, meminfo
.oobsize
);
256 mtdoffset
+= meminfo_writesize
;
257 if (cnt
< meminfo_writesize
)
261 if (IS_NANDWRITE
&& cnt
!= 0) {
262 /* We filled entire MTD, but did we reach EOF on input? */
263 if (full_read(STDIN_FILENO
, filebuf
, meminfo_writesize
) != 0) {
265 bb_error_msg_and_die("not enough space in MTD device");
269 if (ENABLE_FEATURE_CLEAN_UP
) {