blob: dbe9043f4be34a19f9686a77125ae6bf75cbe9d2 [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
Baruch Siach6f32ea42010-08-25 16:36:17 +020011//config:config NANDWRITE
12//config: bool "nandwrite"
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020013//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010014//config: select PLATFORM_LINUX
Baruch Siach6f32ea42010-08-25 16:36:17 +020015//config: help
16//config: Write to the specified MTD device, with bad blocks awareness
Baruch Siach36af2f72010-10-18 02:36:34 +020017//config:
18//config:config NANDDUMP
19//config: bool "nanddump"
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020020//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010021//config: select PLATFORM_LINUX
Baruch Siach36af2f72010-10-18 02:36:34 +020022//config: help
23//config: Dump the content of raw NAND chip
Baruch Siach6f32ea42010-08-25 16:36:17 +020024
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020025//applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
Alexander Shiyan0d61dcd2013-03-15 00:27:41 +010026//applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020027
28//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
29//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
30
Baruch Siach6f32ea42010-08-25 16:36:17 +020031//usage:#define nandwrite_trivial_usage
Aaro Koskinen76915bf2015-12-19 22:34:44 +020032//usage: "[-np] [-s ADDR] MTD_DEVICE [FILE]"
Baruch Siach6f32ea42010-08-25 16:36:17 +020033//usage:#define nandwrite_full_usage "\n\n"
Alexander Shiyan0d61dcd2013-03-15 00:27:41 +010034//usage: "Write to MTD_DEVICE\n"
Aaro Koskinen76915bf2015-12-19 22:34:44 +020035//usage: "\n -n Write without ecc"
Baruch Siach6f32ea42010-08-25 16:36:17 +020036//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
Denys Vlasenkobbc7bee2017-01-21 02:49:58 +010040//usage: "[-no]" IF_LONG_OPTS(" [--bb padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
Baruch Siach36af2f72010-10-18 02:36:34 +020041//usage:#define nanddump_full_usage "\n\n"
Alexander Shiyan0d61dcd2013-03-15 00:27:41 +010042//usage: "Dump MTD_DEVICE\n"
Aaro Koskinen76915bf2015-12-19 22:34:44 +020043//usage: "\n -n Read without ecc"
Baruch Siach3aeb8702012-10-15 14:32:23 +020044//usage: "\n -o Dump oob data"
Baruch Siach36af2f72010-10-18 02:36:34 +020045//usage: "\n -s ADDR Start address"
46//usage: "\n -l LEN Length"
47//usage: "\n -f FILE Dump to file ('-' for stdout)"
Baruch Siachad0d0092014-12-18 00:27:26 +020048//usage: IF_LONG_OPTS(
Denys Vlasenkobbc7bee2017-01-21 02:49:58 +010049//usage: "\n --bb METHOD"
Richard Genoudf17fbe12014-06-24 12:12:59 +020050//usage: "\n skipbad: skip bad blocks"
51//usage: "\n padbad: substitute bad blocks by 0xff (default)"
Baruch Siachad0d0092014-12-18 00:27:26 +020052//usage: )
Baruch Siach36af2f72010-10-18 02:36:34 +020053
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
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020060#define OPT_p (1 << 0) /* nandwrite only */
61#define OPT_o (1 << 0) /* nanddump only */
Aaro Koskinen76915bf2015-12-19 22:34:44 +020062#define OPT_n (1 << 1)
63#define OPT_s (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 */
Richard Genoudf17fbe12014-06-24 12:12:59 +020067
68#define BB_PADBAD (1 << 0)
69#define BB_SKIPBAD (1 << 1)
Baruch Siach36af2f72010-10-18 02:36:34 +020070
Baruch Siach36af2f72010-10-18 02:36:34 +020071/* helper for writing out 0xff for bad blocks pad */
72static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
73{
74 unsigned char buf[meminfo->writesize];
75 unsigned count;
76
Richard Genoudcbf3bfa2014-06-24 12:12:58 +020077 /* round len to the next page only if len is not already on a page */
78 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
Baruch Siach36af2f72010-10-18 02:36:34 +020079
80 memset(buf, 0xff, sizeof(buf));
81 for (count = 0; count < len; count += meminfo->writesize) {
82 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
83 if (oob)
84 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
85 }
86}
87
Baruch Siach6f32ea42010-08-25 16:36:17 +020088static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
89 unsigned block_offset)
90{
91 while (1) {
92 loff_t offs;
Baruch Siach36af2f72010-10-18 02:36:34 +020093
94 if (block_offset >= meminfo->size) {
95 if (IS_NANDWRITE)
96 bb_error_msg_and_die("not enough space in MTD device");
97 return block_offset; /* let the caller exit */
98 }
Baruch Siach6f32ea42010-08-25 16:36:17 +020099 offs = block_offset;
100 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
101 return block_offset;
102 /* ioctl returned 1 => "bad block" */
Baruch Siach36af2f72010-10-18 02:36:34 +0200103 if (IS_NANDWRITE)
104 printf("Skipping bad block at 0x%08x\n", block_offset);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200105 block_offset += meminfo->erasesize;
106 }
107}
108
109int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
110int nandwrite_main(int argc UNUSED_PARAM, char **argv)
111{
Baruch Siach36af2f72010-10-18 02:36:34 +0200112 /* Buffer for OOB data */
Baruch Siachd982da72010-10-18 11:35:30 +0200113 unsigned char *oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200114 unsigned opts;
Richard Genoud2a870d02014-06-25 17:38:30 +0200115 unsigned bb_method = BB_SKIPBAD;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200116 int fd;
117 ssize_t cnt;
Baruch Siach36af2f72010-10-18 02:36:34 +0200118 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
119 unsigned end_addr = ~0;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200120 struct mtd_info_user meminfo;
Baruch Siach36af2f72010-10-18 02:36:34 +0200121 struct mtd_oob_buf oob;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200122 unsigned char *filebuf;
Richard Genoudf17fbe12014-06-24 12:12:59 +0200123 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200124
Baruch Siach36af2f72010-10-18 02:36:34 +0200125 if (IS_NANDDUMP) {
126 opt_complementary = "=1";
Baruch Siache835afa2014-12-17 17:02:37 +0100127#if ENABLE_LONG_OPTS
128 applet_long_options =
129 "bb\0" Required_argument "\xff"; /* no short equivalent */
130#endif
Aaro Koskinen76915bf2015-12-19 22:34:44 +0200131 opts = getopt32(argv, "ons:f:l:", &opt_s, &opt_f, &opt_l, &opt_bb);
Baruch Siach36af2f72010-10-18 02:36:34 +0200132 } else { /* nandwrite */
133 opt_complementary = "-1:?2";
Aaro Koskinen76915bf2015-12-19 22:34:44 +0200134 opts = getopt32(argv, "pns:", &opt_s);
Baruch Siach36af2f72010-10-18 02:36:34 +0200135 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200136 argv += optind;
137
Baruch Siach36af2f72010-10-18 02:36:34 +0200138 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 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200146
Matt Reimer69793252012-05-30 10:31:13 -0400147 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200148 xioctl(fd, MEMGETINFO, &meminfo);
149
Aaro Koskinen76915bf2015-12-19 22:34:44 +0200150 if (opts & OPT_n)
151 xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
152
Baruch Siach36af2f72010-10-18 02:36:34 +0200153 mtdoffset = xstrtou(opt_s, 0);
154 if (IS_NANDDUMP && (opts & OPT_l)) {
155 unsigned length = xstrtou(opt_l, 0);
156 if (length < meminfo.size - mtdoffset)
157 end_addr = mtdoffset + length;
158 }
Richard Genoud8feb2592014-06-25 17:33:34 +0200159 if (IS_NANDDUMP && (opts & OPT_bb)) {
160 if (strcmp("skipbad", opt_bb) == 0)
161 bb_method = BB_SKIPBAD;
162 else if (strcmp("padbad", opt_bb) == 0)
163 bb_method = BB_PADBAD;
164 else
165 bb_show_usage();
Richard Genoudf17fbe12014-06-24 12:12:59 +0200166 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200167
168 /* Pull it into a CPU register (hopefully) - smaller code that way */
169 meminfo_writesize = meminfo.writesize;
170
171 if (mtdoffset & (meminfo_writesize - 1))
172 bb_error_msg_and_die("start address is not page aligned");
173
174 filebuf = xmalloc(meminfo_writesize);
Baruch Siachd982da72010-10-18 11:35:30 +0200175 oobbuf = xmalloc(meminfo.oobsize);
176
177 oob.start = 0;
178 oob.length = meminfo.oobsize;
179 oob.ptr = oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200180
Baruch Siach7715b482010-08-29 10:36:50 +0300181 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
182 if (blockstart != mtdoffset) {
183 unsigned tmp;
184 /* mtdoffset is in the middle of an erase block, verify that
185 * this block is OK. Advance mtdoffset only if this block is
186 * bad.
187 */
188 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200189 if (tmp != blockstart) {
190 /* bad block(s), advance mtdoffset */
Richard Genoudf17fbe12014-06-24 12:12:59 +0200191 if (IS_NANDDUMP) {
192 if (bb_method == BB_PADBAD) {
193 int bad_len = MIN(tmp, end_addr) - mtdoffset;
194 dump_bad(&meminfo, bad_len, opts & OPT_o);
195 }
196 /* with option skipbad, increase the total length */
197 if (bb_method == BB_SKIPBAD) {
198 end_addr += (tmp - blockstart);
199 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200200 }
Baruch Siach7715b482010-08-29 10:36:50 +0300201 mtdoffset = tmp;
Baruch Siach36af2f72010-10-18 02:36:34 +0200202 }
Baruch Siach7715b482010-08-29 10:36:50 +0300203 }
204
Baruch Siach6f32ea42010-08-25 16:36:17 +0200205 cnt = -1;
Baruch Siach36af2f72010-10-18 02:36:34 +0200206 limit = MIN(meminfo.size, end_addr);
207 while (mtdoffset < limit) {
208 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
209 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
210
Baruch Siach7715b482010-08-29 10:36:50 +0300211 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200212 if (blockstart == mtdoffset) {
213 /* starting a new eraseblock */
214 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200215 if (IS_NANDWRITE)
216 printf("Writing at 0x%08x\n", mtdoffset);
Richard Genoudf17fbe12014-06-24 12:12:59 +0200217 else if (mtdoffset > blockstart) {
218 if (bb_method == BB_PADBAD) {
219 /* dump FF padded bad block */
220 int bad_len = MIN(mtdoffset, limit) - blockstart;
221 dump_bad(&meminfo, bad_len, opts & OPT_o);
222 } else if (bb_method == BB_SKIPBAD) {
223 /* for skipbad, increase the length */
224 if ((end_addr + mtdoffset - blockstart) > end_addr)
225 end_addr += (mtdoffset - blockstart);
226 else
227 end_addr = ~0;
228 limit = MIN(meminfo.size, end_addr);
229 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200230 }
231 if (mtdoffset >= limit)
232 break;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200233 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200234 xlseek(fd, mtdoffset, SEEK_SET);
235
Baruch Siach6f32ea42010-08-25 16:36:17 +0200236 /* get some more data from input */
Baruch Siach36af2f72010-10-18 02:36:34 +0200237 cnt = full_read(input_fd, filebuf, meminfo_writesize);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200238 if (cnt == 0) {
239 /* even with -p, we do not pad past the end of input
240 * (-p only zero-pads last incomplete page)
241 */
242 break;
243 }
244 if (cnt < meminfo_writesize) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200245 if (IS_NANDDUMP)
246 bb_error_msg_and_die("short read");
Baruch Siach6f32ea42010-08-25 16:36:17 +0200247 if (!(opts & OPT_p))
248 bb_error_msg_and_die("input size is not rounded up to page size, "
249 "use -p to zero pad");
250 /* zero pad to end of write block */
251 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
252 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200253 xwrite(output_fd, filebuf, meminfo_writesize);
254
Baruch Siach3aeb8702012-10-15 14:32:23 +0200255 if (IS_NANDDUMP && (opts & OPT_o)) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200256 /* Dump OOB data */
257 oob.start = mtdoffset;
258 xioctl(fd, MEMREADOOB, &oob);
259 xwrite(output_fd, oobbuf, meminfo.oobsize);
260 }
261
Baruch Siach6f32ea42010-08-25 16:36:17 +0200262 mtdoffset += meminfo_writesize;
263 if (cnt < meminfo_writesize)
264 break;
265 }
266
Baruch Siach36af2f72010-10-18 02:36:34 +0200267 if (IS_NANDWRITE && cnt != 0) {
Baruch Siach6f32ea42010-08-25 16:36:17 +0200268 /* We filled entire MTD, but did we reach EOF on input? */
269 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
270 /* no */
271 bb_error_msg_and_die("not enough space in MTD device");
272 }
273 }
274
275 if (ENABLE_FEATURE_CLEAN_UP) {
276 free(filebuf);
277 close(fd);
278 }
279
280 return EXIT_SUCCESS;
281}