6c85ea346247c221b40b528daa2916ad0b941e6c
[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 //applet:IF_NANDWRITE(APPLET(nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
12 //applet:IF_NANDWRITE(APPLET_ODDNAME(nanddump, nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP, nanddump))
13
14 //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
15 //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
16
17 //config:config NANDWRITE
18 //config:       bool "nandwrite"
19 //config:       default n
20 //config:       depends on PLATFORM_LINUX
21 //config:       help
22 //config:         Write to the specified MTD device, with bad blocks awareness
23 //config:
24 //config:config NANDDUMP
25 //config:       bool "nanddump"
26 //config:       default n
27 //config:       depends on PLATFORM_LINUX
28 //config:       help
29 //config:         Dump the content of raw NAND chip
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 the specified MTD device\n"
35 //usage:     "\nOptions:"
36 //usage:     "\n        -p      Pad to page size"
37 //usage:     "\n        -s ADDR Start address"
38
39 //usage:#define nanddump_trivial_usage
40 //usage:        "[-o] [-b] [-s ADDR] [-f FILE] MTD_DEVICE"
41 //usage:#define nanddump_full_usage "\n\n"
42 //usage:        "Dump the sepcified MTD device\n"
43 //usage:     "\nOptions:"
44 //usage:     "\n        -o      Omit oob data"
45 //usage:     "\n        -b      Omit bad block from the dump"
46 //usage:     "\n        -s ADDR Start address"
47 //usage:     "\n        -l LEN  Length"
48 //usage:     "\n        -f FILE Dump to file ('-' for stdout)"
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_b   (1 << 2)
60 #define OPT_f   (1 << 3)
61 #define OPT_l   (1 << 4)
62
63 #define NAND_MAX_OOBSIZE 256
64 /* helper for writing out 0xff for bad blocks pad */
65 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
66 {
67         unsigned char buf[meminfo->writesize];
68         unsigned count;
69
70         /* round len to the next page */
71         len = (len | ~(meminfo->writesize - 1)) + 1;
72
73         memset(buf, 0xff, sizeof(buf));
74         for (count = 0; count < len; count += meminfo->writesize) {
75                 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
76                 if (oob)
77                         xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
78         }
79 }
80
81 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
82                 unsigned block_offset)
83 {
84         while (1) {
85                 loff_t offs;
86
87                 if (block_offset >= meminfo->size) {
88                         if (IS_NANDWRITE)
89                                 bb_error_msg_and_die("not enough space in MTD device");
90                         return block_offset; /* let the caller exit */
91                 }
92                 offs = block_offset;
93                 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
94                         return block_offset;
95                 /* ioctl returned 1 => "bad block" */
96                 if (IS_NANDWRITE)
97                         printf("Skipping bad block at 0x%08x\n", block_offset);
98                 block_offset += meminfo->erasesize;
99         }
100 }
101
102 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
103 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
104 {
105         /* Buffer for OOB data */
106         unsigned char oobbuf[NAND_MAX_OOBSIZE];
107         unsigned opts;
108         int fd;
109         ssize_t cnt;
110         unsigned mtdoffset, meminfo_writesize, blockstart, limit;
111         unsigned end_addr = ~0;
112         struct mtd_info_user meminfo;
113         struct mtd_oob_buf oob;
114         unsigned char *filebuf;
115         const char *opt_s = "0", *opt_f = "-", *opt_l;
116
117         if (IS_NANDDUMP) {
118                 opt_complementary = "=1";
119                 opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l);
120         } else { /* nandwrite */
121                 opt_complementary = "-1:?2";
122                 opts = getopt32(argv, "ps:", &opt_s);
123         }
124         argv += optind;
125
126         if (IS_NANDWRITE && argv[1])
127                 opt_f = argv[1];
128         if (!LONE_DASH(opt_f)) {
129                 int tmp_fd = xopen(opt_f,
130                         IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
131                 );
132                 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
133         }
134
135         fd = xopen(argv[0], O_RDWR);
136         xioctl(fd, MEMGETINFO, &meminfo);
137
138         oob.start  = 0;
139         oob.length = meminfo.oobsize;
140         oob.ptr    = oobbuf;
141
142         mtdoffset = xstrtou(opt_s, 0);
143         if (IS_NANDDUMP && (opts & OPT_l)) {
144                 unsigned length = xstrtou(opt_l, 0);
145                 if (length < meminfo.size - mtdoffset)
146                         end_addr = mtdoffset + length;
147         }
148
149         /* Pull it into a CPU register (hopefully) - smaller code that way */
150         meminfo_writesize = meminfo.writesize;
151
152         if (mtdoffset & (meminfo_writesize - 1))
153                 bb_error_msg_and_die("start address is not page aligned");
154
155         filebuf = xmalloc(meminfo_writesize);
156
157         blockstart = mtdoffset & ~(meminfo.erasesize - 1);
158         if (blockstart != mtdoffset) {
159                 unsigned tmp;
160                 /* mtdoffset is in the middle of an erase block, verify that
161                  * this block is OK. Advance mtdoffset only if this block is
162                  * bad.
163                  */
164                 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
165                 if (tmp != blockstart) {
166                         /* bad block(s), advance mtdoffset */
167                         if (IS_NANDDUMP & !(opts & OPT_b)) {
168                                 int bad_len = MIN(tmp, end_addr) - mtdoffset;
169                                 dump_bad(&meminfo, bad_len, !(opts & OPT_o));
170                         }
171                         mtdoffset = tmp;
172                 }
173         }
174
175         cnt = -1;
176         limit = MIN(meminfo.size, end_addr);
177         while (mtdoffset < limit) {
178                 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
179                 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
180
181                 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
182                 if (blockstart == mtdoffset) {
183                         /* starting a new eraseblock */
184                         mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
185                         if (IS_NANDWRITE)
186                                 printf("Writing at 0x%08x\n", mtdoffset);
187                         else if (mtdoffset > blockstart) {
188                                 int bad_len = MIN(mtdoffset, limit) - blockstart;
189                                 dump_bad(&meminfo, bad_len, !(opts & OPT_o));
190                         }
191                         if (mtdoffset >= limit)
192                                 break;
193                 }
194                 xlseek(fd, mtdoffset, SEEK_SET);
195
196                 /* get some more data from input */
197                 cnt = full_read(input_fd, filebuf, meminfo_writesize);
198                 if (cnt == 0) {
199                         /* even with -p, we do not pad past the end of input
200                          * (-p only zero-pads last incomplete page)
201                          */
202                         break;
203                 }
204                 if (cnt < meminfo_writesize) {
205                         if (IS_NANDDUMP)
206                                 bb_error_msg_and_die("short read");
207                         if (!(opts & OPT_p))
208                                 bb_error_msg_and_die("input size is not rounded up to page size, "
209                                                 "use -p to zero pad");
210                         /* zero pad to end of write block */
211                         memset(filebuf + cnt, 0, meminfo_writesize - cnt);
212                 }
213                 xwrite(output_fd, filebuf, meminfo_writesize);
214
215                 if (IS_NANDDUMP && !(opts & OPT_o)) {
216                         /* Dump OOB data */
217                         oob.start = mtdoffset;
218                         xioctl(fd, MEMREADOOB, &oob);
219                         xwrite(output_fd, oobbuf, meminfo.oobsize);
220                 }
221
222                 mtdoffset += meminfo_writesize;
223                 if (cnt < meminfo_writesize)
224                         break;
225         }
226
227         if (IS_NANDWRITE && cnt != 0) {
228                 /* We filled entire MTD, but did we reach EOF on input? */
229                 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
230                         /* no */
231                         bb_error_msg_and_die("not enough space in MTD device");
232                 }
233         }
234
235         if (ENABLE_FEATURE_CLEAN_UP) {
236                 free(filebuf);
237                 close(fd);
238         }
239
240         return EXIT_SUCCESS;
241 }