509c0641274f80b4e19b0fe4d169522ec655b1d5
[oweals/busybox.git] / miscutils / nandwrite.c
1 /*
2  * nandwrite and nanddump ported to busybox from mtd-utils
3  *
4  * Author: Baruch Siach <baruch@tkos.co.il>, Orex Computed Radiography
5  *
6  * Licensed under GPLv2, see file LICENSE in this source tree.
7  *
8  * TODO: add support for large (>4GB) MTD devices
9  */
10
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
24
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))
27
28 //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
29 //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
30
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"
37
38 //usage:#define nanddump_trivial_usage
39 //usage:        "[-o] [--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:     "\n        --bb=METHOD:"
47 //usage:     "\n                skipbad: skip bad blocks"
48 //usage:     "\n                padbad: substitute bad blocks by 0xff (default)"
49
50 #include "libbb.h"
51 #include <mtd/mtd-user.h>
52
53 #define IS_NANDDUMP  (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
54 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
55
56 #define OPT_p  (1 << 0) /* nandwrite only */
57 #define OPT_o  (1 << 0) /* nanddump only */
58 #define OPT_s  (1 << 1)
59 #define OPT_f  (1 << 2)
60 #define OPT_l  (1 << 3)
61 #define OPT_bb (1 << 4) /* must be the last one in the list */
62
63 #define BB_PADBAD (1 << 0)
64 #define BB_SKIPBAD (1 << 1)
65
66 /* helper for writing out 0xff for bad blocks pad */
67 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
68 {
69         unsigned char buf[meminfo->writesize];
70         unsigned count;
71
72         /* round len to the next page only if len is not already on a page */
73         len = ((len - 1) | (meminfo->writesize - 1)) + 1;
74
75         memset(buf, 0xff, sizeof(buf));
76         for (count = 0; count < len; count += meminfo->writesize) {
77                 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
78                 if (oob)
79                         xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
80         }
81 }
82
83 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
84                 unsigned block_offset)
85 {
86         while (1) {
87                 loff_t offs;
88
89                 if (block_offset >= meminfo->size) {
90                         if (IS_NANDWRITE)
91                                 bb_error_msg_and_die("not enough space in MTD device");
92                         return block_offset; /* let the caller exit */
93                 }
94                 offs = block_offset;
95                 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
96                         return block_offset;
97                 /* ioctl returned 1 => "bad block" */
98                 if (IS_NANDWRITE)
99                         printf("Skipping bad block at 0x%08x\n", block_offset);
100                 block_offset += meminfo->erasesize;
101         }
102 }
103
104 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
106 {
107         /* Buffer for OOB data */
108         unsigned char *oobbuf;
109         unsigned opts;
110         unsigned bb_method = BB_PADBAD;
111         int fd;
112         ssize_t cnt;
113         unsigned mtdoffset, meminfo_writesize, blockstart, limit;
114         unsigned end_addr = ~0;
115         struct mtd_info_user meminfo;
116         struct mtd_oob_buf oob;
117         unsigned char *filebuf;
118         const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
119         static const char nanddump_longopts[] ALIGN1 =
120                 "bb\0" Required_argument "\xff"; /* no short equivalent */
121
122         if (IS_NANDDUMP) {
123                 opt_complementary = "=1";
124                 applet_long_options = nanddump_longopts;
125                 opts = getopt32(argv, "os:f:l:", &opt_s, &opt_f, &opt_l, &opt_bb);
126         } else { /* nandwrite */
127                 opt_complementary = "-1:?2";
128                 opts = getopt32(argv, "ps:", &opt_s);
129         }
130         argv += optind;
131
132         if (IS_NANDWRITE && argv[1])
133                 opt_f = argv[1];
134         if (!LONE_DASH(opt_f)) {
135                 int tmp_fd = xopen(opt_f,
136                         IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
137                 );
138                 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
139         }
140
141         fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
142         xioctl(fd, MEMGETINFO, &meminfo);
143
144         mtdoffset = xstrtou(opt_s, 0);
145         if (IS_NANDDUMP && (opts & OPT_l)) {
146                 unsigned length = xstrtou(opt_l, 0);
147                 if (length < meminfo.size - mtdoffset)
148                         end_addr = mtdoffset + length;
149         }
150         if (IS_NANDDUMP && (opts & OPT_bb)) {
151                 if (strcmp("skipbad", opt_bb) == 0)
152                         bb_method = BB_SKIPBAD;
153                 else if (strcmp("padbad", opt_bb) == 0)
154                         bb_method = BB_PADBAD;
155                 else
156                         bb_show_usage();
157         }
158
159         /* Pull it into a CPU register (hopefully) - smaller code that way */
160         meminfo_writesize = meminfo.writesize;
161
162         if (mtdoffset & (meminfo_writesize - 1))
163                 bb_error_msg_and_die("start address is not page aligned");
164
165         filebuf = xmalloc(meminfo_writesize);
166         oobbuf = xmalloc(meminfo.oobsize);
167
168         oob.start  = 0;
169         oob.length = meminfo.oobsize;
170         oob.ptr    = oobbuf;
171
172         blockstart = mtdoffset & ~(meminfo.erasesize - 1);
173         if (blockstart != mtdoffset) {
174                 unsigned tmp;
175                 /* mtdoffset is in the middle of an erase block, verify that
176                  * this block is OK. Advance mtdoffset only if this block is
177                  * bad.
178                  */
179                 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
180                 if (tmp != blockstart) {
181                         /* bad block(s), advance mtdoffset */
182                         if (IS_NANDDUMP) {
183                                 if (bb_method == BB_PADBAD) {
184                                         int bad_len = MIN(tmp, end_addr) - mtdoffset;
185                                         dump_bad(&meminfo, bad_len, opts & OPT_o);
186                                 }
187                                 /* with option skipbad, increase the total length */
188                                 if (bb_method == BB_SKIPBAD) {
189                                         end_addr += (tmp - blockstart);
190                                 }
191                         }
192                         mtdoffset = tmp;
193                 }
194         }
195
196         cnt = -1;
197         limit = MIN(meminfo.size, end_addr);
198         while (mtdoffset < limit) {
199                 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
200                 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
201
202                 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
203                 if (blockstart == mtdoffset) {
204                         /* starting a new eraseblock */
205                         mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
206                         if (IS_NANDWRITE)
207                                 printf("Writing at 0x%08x\n", mtdoffset);
208                         else if (mtdoffset > blockstart) {
209                                 if (bb_method == BB_PADBAD) {
210                                         /* dump FF padded bad block */
211                                         int bad_len = MIN(mtdoffset, limit) - blockstart;
212                                         dump_bad(&meminfo, bad_len, opts & OPT_o);
213                                 } else if (bb_method == BB_SKIPBAD) {
214                                         /* for skipbad, increase the length */
215                                         if ((end_addr + mtdoffset - blockstart) > end_addr)
216                                                 end_addr += (mtdoffset - blockstart);
217                                         else
218                                                 end_addr = ~0;
219                                         limit = MIN(meminfo.size, end_addr);
220                                 }
221                         }
222                         if (mtdoffset >= limit)
223                                 break;
224                 }
225                 xlseek(fd, mtdoffset, SEEK_SET);
226
227                 /* get some more data from input */
228                 cnt = full_read(input_fd, filebuf, meminfo_writesize);
229                 if (cnt == 0) {
230                         /* even with -p, we do not pad past the end of input
231                          * (-p only zero-pads last incomplete page)
232                          */
233                         break;
234                 }
235                 if (cnt < meminfo_writesize) {
236                         if (IS_NANDDUMP)
237                                 bb_error_msg_and_die("short read");
238                         if (!(opts & OPT_p))
239                                 bb_error_msg_and_die("input size is not rounded up to page size, "
240                                                 "use -p to zero pad");
241                         /* zero pad to end of write block */
242                         memset(filebuf + cnt, 0, meminfo_writesize - cnt);
243                 }
244                 xwrite(output_fd, filebuf, meminfo_writesize);
245
246                 if (IS_NANDDUMP && (opts & OPT_o)) {
247                         /* Dump OOB data */
248                         oob.start = mtdoffset;
249                         xioctl(fd, MEMREADOOB, &oob);
250                         xwrite(output_fd, oobbuf, meminfo.oobsize);
251                 }
252
253                 mtdoffset += meminfo_writesize;
254                 if (cnt < meminfo_writesize)
255                         break;
256         }
257
258         if (IS_NANDWRITE && cnt != 0) {
259                 /* We filled entire MTD, but did we reach EOF on input? */
260                 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
261                         /* no */
262                         bb_error_msg_and_die("not enough space in MTD device");
263                 }
264         }
265
266         if (ENABLE_FEATURE_CLEAN_UP) {
267                 free(filebuf);
268                 close(fd);
269         }
270
271         return EXIT_SUCCESS;
272 }