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