blob: 247fc72f4becb5a145d822ef8ac46256c2339e51 [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
32//usage: "[-p] [-s ADDR] MTD_DEVICE [FILE]"
33//usage:#define nandwrite_full_usage "\n\n"
Alexander Shiyan0d61dcd2013-03-15 00:27:41 +010034//usage: "Write to MTD_DEVICE\n"
Baruch Siach6f32ea42010-08-25 16:36:17 +020035//usage: "\n -p Pad to page size"
36//usage: "\n -s ADDR Start address"
37
Baruch Siach36af2f72010-10-18 02:36:34 +020038//usage:#define nanddump_trivial_usage
Baruch Siachad0d0092014-12-18 00:27:26 +020039//usage: "[-o]" IF_LONG_OPTS(" [--bb=padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
Baruch Siach36af2f72010-10-18 02:36:34 +020040//usage:#define nanddump_full_usage "\n\n"
Alexander Shiyan0d61dcd2013-03-15 00:27:41 +010041//usage: "Dump MTD_DEVICE\n"
Baruch Siach3aeb8702012-10-15 14:32:23 +020042//usage: "\n -o Dump oob data"
Baruch Siach36af2f72010-10-18 02:36:34 +020043//usage: "\n -s ADDR Start address"
44//usage: "\n -l LEN Length"
45//usage: "\n -f FILE Dump to file ('-' for stdout)"
Baruch Siachad0d0092014-12-18 00:27:26 +020046//usage: IF_LONG_OPTS(
Richard Genoudf17fbe12014-06-24 12:12:59 +020047//usage: "\n --bb=METHOD:"
48//usage: "\n skipbad: skip bad blocks"
49//usage: "\n padbad: substitute bad blocks by 0xff (default)"
Baruch Siachad0d0092014-12-18 00:27:26 +020050//usage: )
Baruch Siach36af2f72010-10-18 02:36:34 +020051
52#include "libbb.h"
53#include <mtd/mtd-user.h>
54
55#define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
56#define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
57
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020058#define OPT_p (1 << 0) /* nandwrite only */
59#define OPT_o (1 << 0) /* nanddump only */
60#define OPT_s (1 << 1)
Richard Genoud8feb2592014-06-25 17:33:34 +020061#define OPT_f (1 << 2)
62#define OPT_l (1 << 3)
63#define OPT_bb (1 << 4) /* must be the last one in the list */
Richard Genoudf17fbe12014-06-24 12:12:59 +020064
65#define BB_PADBAD (1 << 0)
66#define BB_SKIPBAD (1 << 1)
Baruch Siach36af2f72010-10-18 02:36:34 +020067
Baruch Siach36af2f72010-10-18 02:36:34 +020068/* helper for writing out 0xff for bad blocks pad */
69static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
70{
71 unsigned char buf[meminfo->writesize];
72 unsigned count;
73
Richard Genoudcbf3bfa2014-06-24 12:12:58 +020074 /* round len to the next page only if len is not already on a page */
75 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
Baruch Siach36af2f72010-10-18 02:36:34 +020076
77 memset(buf, 0xff, sizeof(buf));
78 for (count = 0; count < len; count += meminfo->writesize) {
79 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
80 if (oob)
81 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
82 }
83}
84
Baruch Siach6f32ea42010-08-25 16:36:17 +020085static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
86 unsigned block_offset)
87{
88 while (1) {
89 loff_t offs;
Baruch Siach36af2f72010-10-18 02:36:34 +020090
91 if (block_offset >= meminfo->size) {
92 if (IS_NANDWRITE)
93 bb_error_msg_and_die("not enough space in MTD device");
94 return block_offset; /* let the caller exit */
95 }
Baruch Siach6f32ea42010-08-25 16:36:17 +020096 offs = block_offset;
97 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
98 return block_offset;
99 /* ioctl returned 1 => "bad block" */
Baruch Siach36af2f72010-10-18 02:36:34 +0200100 if (IS_NANDWRITE)
101 printf("Skipping bad block at 0x%08x\n", block_offset);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200102 block_offset += meminfo->erasesize;
103 }
104}
105
106int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
107int nandwrite_main(int argc UNUSED_PARAM, char **argv)
108{
Baruch Siach36af2f72010-10-18 02:36:34 +0200109 /* Buffer for OOB data */
Baruch Siachd982da72010-10-18 11:35:30 +0200110 unsigned char *oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200111 unsigned opts;
Richard Genoud2a870d02014-06-25 17:38:30 +0200112 unsigned bb_method = BB_SKIPBAD;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200113 int fd;
114 ssize_t cnt;
Baruch Siach36af2f72010-10-18 02:36:34 +0200115 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
116 unsigned end_addr = ~0;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200117 struct mtd_info_user meminfo;
Baruch Siach36af2f72010-10-18 02:36:34 +0200118 struct mtd_oob_buf oob;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200119 unsigned char *filebuf;
Richard Genoudf17fbe12014-06-24 12:12:59 +0200120 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200121
Baruch Siach36af2f72010-10-18 02:36:34 +0200122 if (IS_NANDDUMP) {
123 opt_complementary = "=1";
Baruch Siache835afa2014-12-17 17:02:37 +0100124#if ENABLE_LONG_OPTS
125 applet_long_options =
126 "bb\0" Required_argument "\xff"; /* no short equivalent */
127#endif
Richard Genoud8feb2592014-06-25 17:33:34 +0200128 opts = getopt32(argv, "os:f:l:", &opt_s, &opt_f, &opt_l, &opt_bb);
Baruch Siach36af2f72010-10-18 02:36:34 +0200129 } else { /* nandwrite */
130 opt_complementary = "-1:?2";
131 opts = getopt32(argv, "ps:", &opt_s);
132 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200133 argv += optind;
134
Baruch Siach36af2f72010-10-18 02:36:34 +0200135 if (IS_NANDWRITE && argv[1])
136 opt_f = argv[1];
137 if (!LONE_DASH(opt_f)) {
138 int tmp_fd = xopen(opt_f,
139 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
140 );
141 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
142 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200143
Matt Reimer69793252012-05-30 10:31:13 -0400144 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200145 xioctl(fd, MEMGETINFO, &meminfo);
146
Baruch Siach36af2f72010-10-18 02:36:34 +0200147 mtdoffset = xstrtou(opt_s, 0);
148 if (IS_NANDDUMP && (opts & OPT_l)) {
149 unsigned length = xstrtou(opt_l, 0);
150 if (length < meminfo.size - mtdoffset)
151 end_addr = mtdoffset + length;
152 }
Richard Genoud8feb2592014-06-25 17:33:34 +0200153 if (IS_NANDDUMP && (opts & OPT_bb)) {
154 if (strcmp("skipbad", opt_bb) == 0)
155 bb_method = BB_SKIPBAD;
156 else if (strcmp("padbad", opt_bb) == 0)
157 bb_method = BB_PADBAD;
158 else
159 bb_show_usage();
Richard Genoudf17fbe12014-06-24 12:12:59 +0200160 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200161
162 /* Pull it into a CPU register (hopefully) - smaller code that way */
163 meminfo_writesize = meminfo.writesize;
164
165 if (mtdoffset & (meminfo_writesize - 1))
166 bb_error_msg_and_die("start address is not page aligned");
167
168 filebuf = xmalloc(meminfo_writesize);
Baruch Siachd982da72010-10-18 11:35:30 +0200169 oobbuf = xmalloc(meminfo.oobsize);
170
171 oob.start = 0;
172 oob.length = meminfo.oobsize;
173 oob.ptr = oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200174
Baruch Siach7715b482010-08-29 10:36:50 +0300175 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
176 if (blockstart != mtdoffset) {
177 unsigned tmp;
178 /* mtdoffset is in the middle of an erase block, verify that
179 * this block is OK. Advance mtdoffset only if this block is
180 * bad.
181 */
182 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200183 if (tmp != blockstart) {
184 /* bad block(s), advance mtdoffset */
Richard Genoudf17fbe12014-06-24 12:12:59 +0200185 if (IS_NANDDUMP) {
186 if (bb_method == BB_PADBAD) {
187 int bad_len = MIN(tmp, end_addr) - mtdoffset;
188 dump_bad(&meminfo, bad_len, opts & OPT_o);
189 }
190 /* with option skipbad, increase the total length */
191 if (bb_method == BB_SKIPBAD) {
192 end_addr += (tmp - blockstart);
193 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200194 }
Baruch Siach7715b482010-08-29 10:36:50 +0300195 mtdoffset = tmp;
Baruch Siach36af2f72010-10-18 02:36:34 +0200196 }
Baruch Siach7715b482010-08-29 10:36:50 +0300197 }
198
Baruch Siach6f32ea42010-08-25 16:36:17 +0200199 cnt = -1;
Baruch Siach36af2f72010-10-18 02:36:34 +0200200 limit = MIN(meminfo.size, end_addr);
201 while (mtdoffset < limit) {
202 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
203 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
204
Baruch Siach7715b482010-08-29 10:36:50 +0300205 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200206 if (blockstart == mtdoffset) {
207 /* starting a new eraseblock */
208 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200209 if (IS_NANDWRITE)
210 printf("Writing at 0x%08x\n", mtdoffset);
Richard Genoudf17fbe12014-06-24 12:12:59 +0200211 else if (mtdoffset > blockstart) {
212 if (bb_method == BB_PADBAD) {
213 /* dump FF padded bad block */
214 int bad_len = MIN(mtdoffset, limit) - blockstart;
215 dump_bad(&meminfo, bad_len, opts & OPT_o);
216 } else if (bb_method == BB_SKIPBAD) {
217 /* for skipbad, increase the length */
218 if ((end_addr + mtdoffset - blockstart) > end_addr)
219 end_addr += (mtdoffset - blockstart);
220 else
221 end_addr = ~0;
222 limit = MIN(meminfo.size, end_addr);
223 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200224 }
225 if (mtdoffset >= limit)
226 break;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200227 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200228 xlseek(fd, mtdoffset, SEEK_SET);
229
Baruch Siach6f32ea42010-08-25 16:36:17 +0200230 /* get some more data from input */
Baruch Siach36af2f72010-10-18 02:36:34 +0200231 cnt = full_read(input_fd, filebuf, meminfo_writesize);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200232 if (cnt == 0) {
233 /* even with -p, we do not pad past the end of input
234 * (-p only zero-pads last incomplete page)
235 */
236 break;
237 }
238 if (cnt < meminfo_writesize) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200239 if (IS_NANDDUMP)
240 bb_error_msg_and_die("short read");
Baruch Siach6f32ea42010-08-25 16:36:17 +0200241 if (!(opts & OPT_p))
242 bb_error_msg_and_die("input size is not rounded up to page size, "
243 "use -p to zero pad");
244 /* zero pad to end of write block */
245 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
246 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200247 xwrite(output_fd, filebuf, meminfo_writesize);
248
Baruch Siach3aeb8702012-10-15 14:32:23 +0200249 if (IS_NANDDUMP && (opts & OPT_o)) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200250 /* Dump OOB data */
251 oob.start = mtdoffset;
252 xioctl(fd, MEMREADOOB, &oob);
253 xwrite(output_fd, oobbuf, meminfo.oobsize);
254 }
255
Baruch Siach6f32ea42010-08-25 16:36:17 +0200256 mtdoffset += meminfo_writesize;
257 if (cnt < meminfo_writesize)
258 break;
259 }
260
Baruch Siach36af2f72010-10-18 02:36:34 +0200261 if (IS_NANDWRITE && cnt != 0) {
Baruch Siach6f32ea42010-08-25 16:36:17 +0200262 /* We filled entire MTD, but did we reach EOF on input? */
263 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
264 /* no */
265 bb_error_msg_and_die("not enough space in MTD device");
266 }
267 }
268
269 if (ENABLE_FEATURE_CLEAN_UP) {
270 free(filebuf);
271 close(fd);
272 }
273
274 return EXIT_SUCCESS;
275}