Bump to version 1.22.1
[platform/upstream/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] [-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
48 #include "libbb.h"
49 #include <mtd/mtd-user.h>
50
51 #define IS_NANDDUMP  (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
52 #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
53
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)
60
61 /* helper for writing out 0xff for bad blocks pad */
62 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
63 {
64         unsigned char buf[meminfo->writesize];
65         unsigned count;
66
67         /* round len to the next page */
68         len = (len | ~(meminfo->writesize - 1)) + 1;
69
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);
75         }
76 }
77
78 static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
79                 unsigned block_offset)
80 {
81         while (1) {
82                 loff_t offs;
83
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 */
88                 }
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;
96         }
97 }
98
99 int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
100 int nandwrite_main(int argc UNUSED_PARAM, char **argv)
101 {
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;
113
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);
120         }
121         argv += optind;
122
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
128                 );
129                 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
130         }
131
132         fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
133         xioctl(fd, MEMGETINFO, &meminfo);
134
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;
140         }
141
142         /* Pull it into a CPU register (hopefully) - smaller code that way */
143         meminfo_writesize = meminfo.writesize;
144
145         if (mtdoffset & (meminfo_writesize - 1))
146                 bb_error_msg_and_die("start address is not page aligned");
147
148         filebuf = xmalloc(meminfo_writesize);
149         oobbuf = xmalloc(meminfo.oobsize);
150
151         oob.start  = 0;
152         oob.length = meminfo.oobsize;
153         oob.ptr    = oobbuf;
154
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.
161                  */
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);
168                         }
169                         mtdoffset = tmp;
170                 }
171         }
172
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;
178
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 && !(opts & OPT_b)) {
186                                 int bad_len = MIN(mtdoffset, limit) - blockstart;
187                                 dump_bad(&meminfo, bad_len, opts & OPT_o);
188                         }
189                         if (mtdoffset >= limit)
190                                 break;
191                 }
192                 xlseek(fd, mtdoffset, SEEK_SET);
193
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)
199                          */
200                         break;
201                 }
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);
210                 }
211                 xwrite(output_fd, filebuf, meminfo_writesize);
212
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);
218                 }
219
220                 mtdoffset += meminfo_writesize;
221                 if (cnt < meminfo_writesize)
222                         break;
223         }
224
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");
230                 }
231         }
232
233         if (ENABLE_FEATURE_CLEAN_UP) {
234                 free(filebuf);
235                 close(fd);
236         }
237
238         return EXIT_SUCCESS;
239 }