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 /* Old headers call it MTD_MODE_RAW.
57 * FIXME: In kernel headers, MTD_FILE_MODE_RAW is not a define,
58 * it's an enum. How I can test for existence of an enum?
60 #if !defined(MTD_FILE_MODE_RAW)
61 # define MTD_FILE_MODE_RAW 3
65 #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
66 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
68 #define OPT_p (1 << 0) /* nandwrite only */
69 #define OPT_o (1 << 0) /* nanddump only */
70 #define OPT_n (1 << 1)
71 #define OPT_s (1 << 2)
72 #define OPT_f (1 << 3)
73 #define OPT_l (1 << 4)
74 #define OPT_bb (1 << 5) /* must be the last one in the list */
76 #define BB_PADBAD (1 << 0)
77 #define BB_SKIPBAD (1 << 1)
79 /* helper for writing out 0xff for bad blocks pad */
80 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
82 unsigned char buf[meminfo->writesize];
85 /* round len to the next page only if len is not already on a page */
86 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
88 memset(buf, 0xff, sizeof(buf));
89 for (count = 0; count < len; count += meminfo->writesize) {
90 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
92 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
96 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
97 unsigned block_offset)
102 if (block_offset >= meminfo->size) {
104 bb_error_msg_and_die("not enough space in MTD device");
105 return block_offset; /* let the caller exit */
108 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
110 /* ioctl returned 1 => "bad block" */
112 printf("Skipping bad block at 0x%08x\n", block_offset);
113 block_offset += meminfo->erasesize;
117 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
118 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
120 /* Buffer for OOB data */
121 unsigned char *oobbuf;
123 unsigned bb_method = BB_SKIPBAD;
126 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
127 unsigned end_addr = ~0;
128 struct mtd_info_user meminfo;
129 struct mtd_oob_buf oob;
130 unsigned char *filebuf;
131 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
134 opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
135 "bb\0" Required_argument "\xff", /* no short equivalent */
136 &opt_s, &opt_f, &opt_l, &opt_bb
138 } else { /* nandwrite */
139 opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s);
143 if (IS_NANDWRITE && argv[1])
145 if (!LONE_DASH(opt_f)) {
146 int tmp_fd = xopen(opt_f,
147 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
149 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
152 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
153 xioctl(fd, MEMGETINFO, &meminfo);
156 xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
158 mtdoffset = xstrtou(opt_s, 0);
159 if (IS_NANDDUMP && (opts & OPT_l)) {
160 unsigned length = xstrtou(opt_l, 0);
161 if (length < meminfo.size - mtdoffset)
162 end_addr = mtdoffset + length;
164 if (IS_NANDDUMP && (opts & OPT_bb)) {
165 if (strcmp("skipbad", opt_bb) == 0)
166 bb_method = BB_SKIPBAD;
167 else if (strcmp("padbad", opt_bb) == 0)
168 bb_method = BB_PADBAD;
173 /* Pull it into a CPU register (hopefully) - smaller code that way */
174 meminfo_writesize = meminfo.writesize;
176 if (mtdoffset & (meminfo_writesize - 1))
177 bb_error_msg_and_die("start address is not page aligned");
179 filebuf = xmalloc(meminfo_writesize);
180 oobbuf = xmalloc(meminfo.oobsize);
183 oob.length = meminfo.oobsize;
186 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
187 if (blockstart != mtdoffset) {
189 /* mtdoffset is in the middle of an erase block, verify that
190 * this block is OK. Advance mtdoffset only if this block is
193 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
194 if (tmp != blockstart) {
195 /* bad block(s), advance mtdoffset */
197 if (bb_method == BB_PADBAD) {
198 int bad_len = MIN(tmp, end_addr) - mtdoffset;
199 dump_bad(&meminfo, bad_len, opts & OPT_o);
201 /* with option skipbad, increase the total length */
202 if (bb_method == BB_SKIPBAD) {
203 end_addr += (tmp - blockstart);
211 limit = MIN(meminfo.size, end_addr);
212 while (mtdoffset < limit) {
213 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
214 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
216 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
217 if (blockstart == mtdoffset) {
218 /* starting a new eraseblock */
219 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
221 printf("Writing at 0x%08x\n", mtdoffset);
222 else if (mtdoffset > blockstart) {
223 if (bb_method == BB_PADBAD) {
224 /* dump FF padded bad block */
225 int bad_len = MIN(mtdoffset, limit) - blockstart;
226 dump_bad(&meminfo, bad_len, opts & OPT_o);
227 } else if (bb_method == BB_SKIPBAD) {
228 /* for skipbad, increase the length */
229 if ((end_addr + mtdoffset - blockstart) > end_addr)
230 end_addr += (mtdoffset - blockstart);
233 limit = MIN(meminfo.size, end_addr);
236 if (mtdoffset >= limit)
239 xlseek(fd, mtdoffset, SEEK_SET);
241 /* get some more data from input */
242 cnt = full_read(input_fd, filebuf, meminfo_writesize);
244 /* even with -p, we do not pad past the end of input
245 * (-p only zero-pads last incomplete page)
249 if (cnt < meminfo_writesize) {
251 bb_error_msg_and_die("short read");
253 bb_error_msg_and_die("input size is not rounded up to page size, "
254 "use -p to zero pad");
255 /* zero pad to end of write block */
256 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
258 xwrite(output_fd, filebuf, meminfo_writesize);
260 if (IS_NANDDUMP && (opts & OPT_o)) {
262 oob.start = mtdoffset;
263 xioctl(fd, MEMREADOOB, &oob);
264 xwrite(output_fd, oobbuf, meminfo.oobsize);
267 mtdoffset += meminfo_writesize;
268 if (cnt < meminfo_writesize)
272 if (IS_NANDWRITE && cnt != 0) {
273 /* We filled entire MTD, but did we reach EOF on input? */
274 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
276 bb_error_msg_and_die("not enough space in MTD device");
280 if (ENABLE_FEATURE_CLEAN_UP) {