blob: c7fbe7d9c1c0411dee3f1c95e27a45ed59861856 [file] [log] [blame]
Baruch Siach6f32ea42010-08-25 16:36:17 +02001/*
Baruch Siach36af2f72010-10-18 02:36:34 +02002 * nandwrite and nanddump ported to busybox from mtd-utils
Baruch Siach6f32ea42010-08-25 16:36:17 +02003 *
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
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010011//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))
Baruch Siach6f32ea42010-08-25 16:36:17 +020013
14//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
Baruch Siach36af2f72010-10-18 02:36:34 +020015//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
Baruch Siach6f32ea42010-08-25 16:36:17 +020016
17//config:config NANDWRITE
18//config: bool "nandwrite"
19//config: default n
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010020//config: select PLATFORM_LINUX
Baruch Siach6f32ea42010-08-25 16:36:17 +020021//config: help
22//config: Write to the specified MTD device, with bad blocks awareness
Baruch Siach36af2f72010-10-18 02:36:34 +020023//config:
24//config:config NANDDUMP
25//config: bool "nanddump"
26//config: default n
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010027//config: select PLATFORM_LINUX
Baruch Siach36af2f72010-10-18 02:36:34 +020028//config: help
29//config: Dump the content of raw NAND chip
Baruch Siach6f32ea42010-08-25 16:36:17 +020030
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
Baruch Siach36af2f72010-10-18 02:36:34 +020039//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
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020056#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)
Baruch Siach36af2f72010-10-18 02:36:34 +020062
Baruch Siach36af2f72010-10-18 02:36:34 +020063/* helper for writing out 0xff for bad blocks pad */
64static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
65{
66 unsigned char buf[meminfo->writesize];
67 unsigned count;
68
69 /* round len to the next page */
70 len = (len | ~(meminfo->writesize - 1)) + 1;
71
72 memset(buf, 0xff, sizeof(buf));
73 for (count = 0; count < len; count += meminfo->writesize) {
74 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
75 if (oob)
76 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
77 }
78}
79
Baruch Siach6f32ea42010-08-25 16:36:17 +020080static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
81 unsigned block_offset)
82{
83 while (1) {
84 loff_t offs;
Baruch Siach36af2f72010-10-18 02:36:34 +020085
86 if (block_offset >= meminfo->size) {
87 if (IS_NANDWRITE)
88 bb_error_msg_and_die("not enough space in MTD device");
89 return block_offset; /* let the caller exit */
90 }
Baruch Siach6f32ea42010-08-25 16:36:17 +020091 offs = block_offset;
92 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
93 return block_offset;
94 /* ioctl returned 1 => "bad block" */
Baruch Siach36af2f72010-10-18 02:36:34 +020095 if (IS_NANDWRITE)
96 printf("Skipping bad block at 0x%08x\n", block_offset);
Baruch Siach6f32ea42010-08-25 16:36:17 +020097 block_offset += meminfo->erasesize;
98 }
99}
100
101int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
102int nandwrite_main(int argc UNUSED_PARAM, char **argv)
103{
Baruch Siach36af2f72010-10-18 02:36:34 +0200104 /* Buffer for OOB data */
Baruch Siachd982da72010-10-18 11:35:30 +0200105 unsigned char *oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200106 unsigned opts;
107 int fd;
108 ssize_t cnt;
Baruch Siach36af2f72010-10-18 02:36:34 +0200109 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
110 unsigned end_addr = ~0;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200111 struct mtd_info_user meminfo;
Baruch Siach36af2f72010-10-18 02:36:34 +0200112 struct mtd_oob_buf oob;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200113 unsigned char *filebuf;
Baruch Siach36af2f72010-10-18 02:36:34 +0200114 const char *opt_s = "0", *opt_f = "-", *opt_l;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200115
Baruch Siach36af2f72010-10-18 02:36:34 +0200116 if (IS_NANDDUMP) {
117 opt_complementary = "=1";
118 opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l);
119 } else { /* nandwrite */
120 opt_complementary = "-1:?2";
121 opts = getopt32(argv, "ps:", &opt_s);
122 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200123 argv += optind;
124
Baruch Siach36af2f72010-10-18 02:36:34 +0200125 if (IS_NANDWRITE && argv[1])
126 opt_f = argv[1];
127 if (!LONE_DASH(opt_f)) {
128 int tmp_fd = xopen(opt_f,
129 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
130 );
131 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
132 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200133
134 fd = xopen(argv[0], O_RDWR);
135 xioctl(fd, MEMGETINFO, &meminfo);
136
Baruch Siach36af2f72010-10-18 02:36:34 +0200137 mtdoffset = xstrtou(opt_s, 0);
138 if (IS_NANDDUMP && (opts & OPT_l)) {
139 unsigned length = xstrtou(opt_l, 0);
140 if (length < meminfo.size - mtdoffset)
141 end_addr = mtdoffset + length;
142 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200143
144 /* Pull it into a CPU register (hopefully) - smaller code that way */
145 meminfo_writesize = meminfo.writesize;
146
147 if (mtdoffset & (meminfo_writesize - 1))
148 bb_error_msg_and_die("start address is not page aligned");
149
150 filebuf = xmalloc(meminfo_writesize);
Baruch Siachd982da72010-10-18 11:35:30 +0200151 oobbuf = xmalloc(meminfo.oobsize);
152
153 oob.start = 0;
154 oob.length = meminfo.oobsize;
155 oob.ptr = oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200156
Baruch Siach7715b482010-08-29 10:36:50 +0300157 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);
Baruch Siach36af2f72010-10-18 02:36:34 +0200165 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 }
Baruch Siach7715b482010-08-29 10:36:50 +0300171 mtdoffset = tmp;
Baruch Siach36af2f72010-10-18 02:36:34 +0200172 }
Baruch Siach7715b482010-08-29 10:36:50 +0300173 }
174
Baruch Siach6f32ea42010-08-25 16:36:17 +0200175 cnt = -1;
Baruch Siach36af2f72010-10-18 02:36:34 +0200176 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
Baruch Siach7715b482010-08-29 10:36:50 +0300181 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200182 if (blockstart == mtdoffset) {
183 /* starting a new eraseblock */
184 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200185 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;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200193 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200194 xlseek(fd, mtdoffset, SEEK_SET);
195
Baruch Siach6f32ea42010-08-25 16:36:17 +0200196 /* get some more data from input */
Baruch Siach36af2f72010-10-18 02:36:34 +0200197 cnt = full_read(input_fd, filebuf, meminfo_writesize);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200198 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) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200205 if (IS_NANDDUMP)
206 bb_error_msg_and_die("short read");
Baruch Siach6f32ea42010-08-25 16:36:17 +0200207 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 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200213 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
Baruch Siach6f32ea42010-08-25 16:36:17 +0200222 mtdoffset += meminfo_writesize;
223 if (cnt < meminfo_writesize)
224 break;
225 }
226
Baruch Siach36af2f72010-10-18 02:36:34 +0200227 if (IS_NANDWRITE && cnt != 0) {
Baruch Siach6f32ea42010-08-25 16:36:17 +0200228 /* 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}