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_NANDWRITE(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 the specified 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] [-s ADDR] [-f FILE] MTD_DEVICE"
40 //usage:#define nanddump_full_usage "\n\n"
41 //usage: "Dump the specified 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)"
49 #include <mtd/mtd-user.h>
51 #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
52 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
54 #define OPT_p (1 << 0) /* nandwrite only */
55 #define OPT_o (1 << 0) /* nanddump only */
56 #define OPT_s (1 << 1)
57 #define OPT_b (1 << 2)
58 #define OPT_f (1 << 3)
59 #define OPT_l (1 << 4)
61 /* helper for writing out 0xff for bad blocks pad */
62 static void dump_bad(struct mtd_info_user
*meminfo
, unsigned len
, int oob
)
64 unsigned char buf
[meminfo
->writesize
];
67 /* round len to the next page */
68 len
= (len
| ~(meminfo
->writesize
- 1)) + 1;
70 memset(buf
, 0xff, sizeof(buf
));
71 for (count
= 0; count
< len
; count
+= meminfo
->writesize
) {
72 xwrite(STDOUT_FILENO
, buf
, meminfo
->writesize
);
74 xwrite(STDOUT_FILENO
, buf
, meminfo
->oobsize
);
78 static unsigned next_good_eraseblock(int fd
, struct mtd_info_user
*meminfo
,
79 unsigned block_offset
)
84 if (block_offset
>= meminfo
->size
) {
86 bb_error_msg_and_die("not enough space in MTD device");
87 return block_offset
; /* let the caller exit */
90 if (xioctl(fd
, MEMGETBADBLOCK
, &offs
) == 0)
92 /* ioctl returned 1 => "bad block" */
94 printf("Skipping bad block at 0x%08x\n", block_offset
);
95 block_offset
+= meminfo
->erasesize
;
99 int nandwrite_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
100 int nandwrite_main(int argc UNUSED_PARAM
, char **argv
)
102 /* Buffer for OOB data */
103 unsigned char *oobbuf
;
107 unsigned mtdoffset
, meminfo_writesize
, blockstart
, limit
;
108 unsigned end_addr
= ~0;
109 struct mtd_info_user meminfo
;
110 struct mtd_oob_buf oob
;
111 unsigned char *filebuf
;
112 const char *opt_s
= "0", *opt_f
= "-", *opt_l
;
115 opt_complementary
= "=1";
116 opts
= getopt32(argv
, "os:bf:l:", &opt_s
, &opt_f
, &opt_l
);
117 } else { /* nandwrite */
118 opt_complementary
= "-1:?2";
119 opts
= getopt32(argv
, "ps:", &opt_s
);
123 if (IS_NANDWRITE
&& argv
[1])
125 if (!LONE_DASH(opt_f
)) {
126 int tmp_fd
= xopen(opt_f
,
127 IS_NANDDUMP
? O_WRONLY
| O_TRUNC
| O_CREAT
: O_RDONLY
129 xmove_fd(tmp_fd
, IS_NANDDUMP
? STDOUT_FILENO
: STDIN_FILENO
);
132 fd
= xopen(argv
[0], IS_NANDWRITE
? O_RDWR
: O_RDONLY
);
133 xioctl(fd
, MEMGETINFO
, &meminfo
);
135 mtdoffset
= xstrtou(opt_s
, 0);
136 if (IS_NANDDUMP
&& (opts
& OPT_l
)) {
137 unsigned length
= xstrtou(opt_l
, 0);
138 if (length
< meminfo
.size
- mtdoffset
)
139 end_addr
= mtdoffset
+ length
;
142 /* Pull it into a CPU register (hopefully) - smaller code that way */
143 meminfo_writesize
= meminfo
.writesize
;
145 if (mtdoffset
& (meminfo_writesize
- 1))
146 bb_error_msg_and_die("start address is not page aligned");
148 filebuf
= xmalloc(meminfo_writesize
);
149 oobbuf
= xmalloc(meminfo
.oobsize
);
152 oob
.length
= meminfo
.oobsize
;
155 blockstart
= mtdoffset
& ~(meminfo
.erasesize
- 1);
156 if (blockstart
!= mtdoffset
) {
158 /* mtdoffset is in the middle of an erase block, verify that
159 * this block is OK. Advance mtdoffset only if this block is
162 tmp
= next_good_eraseblock(fd
, &meminfo
, blockstart
);
163 if (tmp
!= blockstart
) {
164 /* bad block(s), advance mtdoffset */
165 if (IS_NANDDUMP
&& !(opts
& OPT_b
)) {
166 int bad_len
= MIN(tmp
, end_addr
) - mtdoffset
;
167 dump_bad(&meminfo
, bad_len
, opts
& OPT_o
);
174 limit
= MIN(meminfo
.size
, end_addr
);
175 while (mtdoffset
< limit
) {
176 int input_fd
= IS_NANDWRITE
? STDIN_FILENO
: fd
;
177 int output_fd
= IS_NANDWRITE
? fd
: STDOUT_FILENO
;
179 blockstart
= mtdoffset
& ~(meminfo
.erasesize
- 1);
180 if (blockstart
== mtdoffset
) {
181 /* starting a new eraseblock */
182 mtdoffset
= next_good_eraseblock(fd
, &meminfo
, blockstart
);
184 printf("Writing at 0x%08x\n", mtdoffset
);
185 else if (mtdoffset
> blockstart
&& !(opts
& OPT_b
)) {
186 int bad_len
= MIN(mtdoffset
, limit
) - blockstart
;
187 dump_bad(&meminfo
, bad_len
, opts
& OPT_o
);
189 if (mtdoffset
>= limit
)
192 xlseek(fd
, mtdoffset
, SEEK_SET
);
194 /* get some more data from input */
195 cnt
= full_read(input_fd
, filebuf
, meminfo_writesize
);
197 /* even with -p, we do not pad past the end of input
198 * (-p only zero-pads last incomplete page)
202 if (cnt
< meminfo_writesize
) {
204 bb_error_msg_and_die("short read");
206 bb_error_msg_and_die("input size is not rounded up to page size, "
207 "use -p to zero pad");
208 /* zero pad to end of write block */
209 memset(filebuf
+ cnt
, 0, meminfo_writesize
- cnt
);
211 xwrite(output_fd
, filebuf
, meminfo_writesize
);
213 if (IS_NANDDUMP
&& (opts
& OPT_o
)) {
215 oob
.start
= mtdoffset
;
216 xioctl(fd
, MEMREADOOB
, &oob
);
217 xwrite(output_fd
, oobbuf
, meminfo
.oobsize
);
220 mtdoffset
+= meminfo_writesize
;
221 if (cnt
< meminfo_writesize
)
225 if (IS_NANDWRITE
&& cnt
!= 0) {
226 /* We filled entire MTD, but did we reach EOF on input? */
227 if (full_read(STDIN_FILENO
, filebuf
, meminfo_writesize
) != 0) {
229 bb_error_msg_and_die("not enough space in MTD device");
233 if (ENABLE_FEATURE_CLEAN_UP
) {