Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / miscutils / nandwrite.c
blob2ba6e3fe55c280883a45e7b13f84bb4c25a80dbd
1 /*
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
9 */
11 //config:config NANDWRITE
12 //config: bool "nandwrite"
13 //config: default y
14 //config: select PLATFORM_LINUX
15 //config: help
16 //config: Write to the specified MTD device, with bad blocks awareness
17 //config:
18 //config:config NANDDUMP
19 //config: bool "nanddump"
20 //config: default y
21 //config: select PLATFORM_LINUX
22 //config: help
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 Omit 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)"
48 #include "libbb.h"
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];
65 unsigned count;
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);
73 if (oob)
74 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
78 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
79 unsigned block_offset)
81 while (1) {
82 loff_t offs;
84 if (block_offset >= meminfo->size) {
85 if (IS_NANDWRITE)
86 bb_error_msg_and_die("not enough space in MTD device");
87 return block_offset; /* let the caller exit */
89 offs = block_offset;
90 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
91 return block_offset;
92 /* ioctl returned 1 => "bad block" */
93 if (IS_NANDWRITE)
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;
104 unsigned opts;
105 int fd;
106 ssize_t cnt;
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;
114 if (IS_NANDDUMP) {
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);
121 argv += optind;
123 if (IS_NANDWRITE && argv[1])
124 opt_f = 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], O_RDWR);
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);
151 oob.start = 0;
152 oob.length = meminfo.oobsize;
153 oob.ptr = oobbuf;
155 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
156 if (blockstart != mtdoffset) {
157 unsigned tmp;
158 /* mtdoffset is in the middle of an erase block, verify that
159 * this block is OK. Advance mtdoffset only if this block is
160 * bad.
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));
169 mtdoffset = tmp;
173 cnt = -1;
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);
183 if (IS_NANDWRITE)
184 printf("Writing at 0x%08x\n", mtdoffset);
185 else if (mtdoffset > blockstart) {
186 int bad_len = MIN(mtdoffset, limit) - blockstart;
187 dump_bad(&meminfo, bad_len, !(opts & OPT_o));
189 if (mtdoffset >= limit)
190 break;
192 xlseek(fd, mtdoffset, SEEK_SET);
194 /* get some more data from input */
195 cnt = full_read(input_fd, filebuf, meminfo_writesize);
196 if (cnt == 0) {
197 /* even with -p, we do not pad past the end of input
198 * (-p only zero-pads last incomplete page)
200 break;
202 if (cnt < meminfo_writesize) {
203 if (IS_NANDDUMP)
204 bb_error_msg_and_die("short read");
205 if (!(opts & OPT_p))
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)) {
214 /* Dump OOB data */
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)
222 break;
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) {
228 /* no */
229 bb_error_msg_and_die("not enough space in MTD device");
233 if (ENABLE_FEATURE_CLEAN_UP) {
234 free(filebuf);
235 close(fd);
238 return EXIT_SUCCESS;