blob: f111c6363fe4ca6492e2213854c681ec3f80a106 [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 */
Baruch Siach6f32ea42010-08-25 16:36:17 +020010//config:config NANDWRITE
Denys Vlasenkob097a842018-12-28 03:20:17 +010011//config: bool "nandwrite (4.8 kb)"
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020012//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010013//config: select PLATFORM_LINUX
Baruch Siach6f32ea42010-08-25 16:36:17 +020014//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: Write to the specified MTD device, with bad blocks awareness
Baruch Siach36af2f72010-10-18 02:36:34 +020016//config:
17//config:config NANDDUMP
Denys Vlasenkob097a842018-12-28 03:20:17 +010018//config: bool "nanddump (5.2 kb)"
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020019//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010020//config: select PLATFORM_LINUX
Baruch Siach36af2f72010-10-18 02:36:34 +020021//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: Dump the content of raw NAND chip
Baruch Siach6f32ea42010-08-25 16:36:17 +020023
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020024//applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
Alexander Shiyan0d61dcd2013-03-15 00:27:41 +010025//applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020026
27//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
28//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
29
Baruch Siach6f32ea42010-08-25 16:36:17 +020030//usage:#define nandwrite_trivial_usage
Aaro Koskinen76915bf2015-12-19 22:34:44 +020031//usage: "[-np] [-s ADDR] MTD_DEVICE [FILE]"
Baruch Siach6f32ea42010-08-25 16:36:17 +020032//usage:#define nandwrite_full_usage "\n\n"
Alexander Shiyan0d61dcd2013-03-15 00:27:41 +010033//usage: "Write to MTD_DEVICE\n"
Aaro Koskinen76915bf2015-12-19 22:34:44 +020034//usage: "\n -n Write without ecc"
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
Denys Vlasenkobbc7bee2017-01-21 02:49:58 +010039//usage: "[-no]" 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"
Aaro Koskinen76915bf2015-12-19 22:34:44 +020042//usage: "\n -n Read without ecc"
Baruch Siach3aeb8702012-10-15 14:32:23 +020043//usage: "\n -o Dump oob data"
Baruch Siach36af2f72010-10-18 02:36:34 +020044//usage: "\n -s ADDR Start address"
45//usage: "\n -l LEN Length"
46//usage: "\n -f FILE Dump to file ('-' for stdout)"
Baruch Siachad0d0092014-12-18 00:27:26 +020047//usage: IF_LONG_OPTS(
Denys Vlasenkobbc7bee2017-01-21 02:49:58 +010048//usage: "\n --bb METHOD"
Richard Genoudf17fbe12014-06-24 12:12:59 +020049//usage: "\n skipbad: skip bad blocks"
50//usage: "\n padbad: substitute bad blocks by 0xff (default)"
Baruch Siachad0d0092014-12-18 00:27:26 +020051//usage: )
Baruch Siach36af2f72010-10-18 02:36:34 +020052
53#include "libbb.h"
54#include <mtd/mtd-user.h>
55
Denys Vlasenko586f1ee2018-01-14 13:43:31 +010056/* Old headers call it MTD_MODE_RAW.
57 * FIXME: In kernel headers, MTD_FILE_MODE_RAW is not a define,
58 * it's an enum. How I can test for existence of an enum?
59 */
60#if !defined(MTD_FILE_MODE_RAW)
61# define MTD_FILE_MODE_RAW 3
62#endif
63
64
Baruch Siach36af2f72010-10-18 02:36:34 +020065#define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
66#define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
67
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020068#define OPT_p (1 << 0) /* nandwrite only */
69#define OPT_o (1 << 0) /* nanddump only */
Aaro Koskinen76915bf2015-12-19 22:34:44 +020070#define OPT_n (1 << 1)
71#define OPT_s (1 << 2)
72#define OPT_f (1 << 3)
73#define OPT_l (1 << 4)
74#define OPT_bb (1 << 5) /* must be the last one in the list */
Richard Genoudf17fbe12014-06-24 12:12:59 +020075
76#define BB_PADBAD (1 << 0)
77#define BB_SKIPBAD (1 << 1)
Baruch Siach36af2f72010-10-18 02:36:34 +020078
Baruch Siach36af2f72010-10-18 02:36:34 +020079/* helper for writing out 0xff for bad blocks pad */
80static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
81{
82 unsigned char buf[meminfo->writesize];
83 unsigned count;
84
Richard Genoudcbf3bfa2014-06-24 12:12:58 +020085 /* round len to the next page only if len is not already on a page */
86 len = ((len - 1) | (meminfo->writesize - 1)) + 1;
Baruch Siach36af2f72010-10-18 02:36:34 +020087
88 memset(buf, 0xff, sizeof(buf));
89 for (count = 0; count < len; count += meminfo->writesize) {
90 xwrite(STDOUT_FILENO, buf, meminfo->writesize);
91 if (oob)
92 xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
93 }
94}
95
Baruch Siach6f32ea42010-08-25 16:36:17 +020096static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
97 unsigned block_offset)
98{
99 while (1) {
100 loff_t offs;
Baruch Siach36af2f72010-10-18 02:36:34 +0200101
102 if (block_offset >= meminfo->size) {
103 if (IS_NANDWRITE)
James Byrne69374872019-07-02 11:35:03 +0200104 bb_simple_error_msg_and_die("not enough space in MTD device");
Baruch Siach36af2f72010-10-18 02:36:34 +0200105 return block_offset; /* let the caller exit */
106 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200107 offs = block_offset;
108 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
109 return block_offset;
110 /* ioctl returned 1 => "bad block" */
Baruch Siach36af2f72010-10-18 02:36:34 +0200111 if (IS_NANDWRITE)
112 printf("Skipping bad block at 0x%08x\n", block_offset);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200113 block_offset += meminfo->erasesize;
114 }
115}
116
117int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
118int nandwrite_main(int argc UNUSED_PARAM, char **argv)
119{
Baruch Siach36af2f72010-10-18 02:36:34 +0200120 /* Buffer for OOB data */
Baruch Siachd982da72010-10-18 11:35:30 +0200121 unsigned char *oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200122 unsigned opts;
Richard Genoud2a870d02014-06-25 17:38:30 +0200123 unsigned bb_method = BB_SKIPBAD;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200124 int fd;
125 ssize_t cnt;
Baruch Siach36af2f72010-10-18 02:36:34 +0200126 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
127 unsigned end_addr = ~0;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200128 struct mtd_info_user meminfo;
Baruch Siach36af2f72010-10-18 02:36:34 +0200129 struct mtd_oob_buf oob;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200130 unsigned char *filebuf;
Richard Genoudf17fbe12014-06-24 12:12:59 +0200131 const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200132
Baruch Siach36af2f72010-10-18 02:36:34 +0200133 if (IS_NANDDUMP) {
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200134 opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
Denys Vlasenko036585a2017-08-08 16:38:18 +0200135 "bb\0" Required_argument "\xff", /* no short equivalent */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200136 &opt_s, &opt_f, &opt_l, &opt_bb
137 );
Baruch Siach36af2f72010-10-18 02:36:34 +0200138 } else { /* nandwrite */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200139 opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s);
Baruch Siach36af2f72010-10-18 02:36:34 +0200140 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200141 argv += optind;
142
Baruch Siach36af2f72010-10-18 02:36:34 +0200143 if (IS_NANDWRITE && argv[1])
144 opt_f = argv[1];
145 if (!LONE_DASH(opt_f)) {
146 int tmp_fd = xopen(opt_f,
147 IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
148 );
149 xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
150 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200151
Matt Reimer69793252012-05-30 10:31:13 -0400152 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200153 xioctl(fd, MEMGETINFO, &meminfo);
154
Aaro Koskinen76915bf2015-12-19 22:34:44 +0200155 if (opts & OPT_n)
156 xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
157
Baruch Siach36af2f72010-10-18 02:36:34 +0200158 mtdoffset = xstrtou(opt_s, 0);
159 if (IS_NANDDUMP && (opts & OPT_l)) {
160 unsigned length = xstrtou(opt_l, 0);
161 if (length < meminfo.size - mtdoffset)
162 end_addr = mtdoffset + length;
163 }
Richard Genoud8feb2592014-06-25 17:33:34 +0200164 if (IS_NANDDUMP && (opts & OPT_bb)) {
165 if (strcmp("skipbad", opt_bb) == 0)
166 bb_method = BB_SKIPBAD;
167 else if (strcmp("padbad", opt_bb) == 0)
168 bb_method = BB_PADBAD;
169 else
170 bb_show_usage();
Richard Genoudf17fbe12014-06-24 12:12:59 +0200171 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200172
173 /* Pull it into a CPU register (hopefully) - smaller code that way */
174 meminfo_writesize = meminfo.writesize;
175
176 if (mtdoffset & (meminfo_writesize - 1))
James Byrne69374872019-07-02 11:35:03 +0200177 bb_simple_error_msg_and_die("start address is not page aligned");
Baruch Siach6f32ea42010-08-25 16:36:17 +0200178
179 filebuf = xmalloc(meminfo_writesize);
Baruch Siachd982da72010-10-18 11:35:30 +0200180 oobbuf = xmalloc(meminfo.oobsize);
181
182 oob.start = 0;
183 oob.length = meminfo.oobsize;
184 oob.ptr = oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200185
Baruch Siach7715b482010-08-29 10:36:50 +0300186 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
187 if (blockstart != mtdoffset) {
188 unsigned tmp;
189 /* mtdoffset is in the middle of an erase block, verify that
190 * this block is OK. Advance mtdoffset only if this block is
191 * bad.
192 */
193 tmp = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200194 if (tmp != blockstart) {
195 /* bad block(s), advance mtdoffset */
Richard Genoudf17fbe12014-06-24 12:12:59 +0200196 if (IS_NANDDUMP) {
197 if (bb_method == BB_PADBAD) {
198 int bad_len = MIN(tmp, end_addr) - mtdoffset;
199 dump_bad(&meminfo, bad_len, opts & OPT_o);
200 }
201 /* with option skipbad, increase the total length */
202 if (bb_method == BB_SKIPBAD) {
203 end_addr += (tmp - blockstart);
204 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200205 }
Baruch Siach7715b482010-08-29 10:36:50 +0300206 mtdoffset = tmp;
Baruch Siach36af2f72010-10-18 02:36:34 +0200207 }
Baruch Siach7715b482010-08-29 10:36:50 +0300208 }
209
Baruch Siach6f32ea42010-08-25 16:36:17 +0200210 cnt = -1;
Baruch Siach36af2f72010-10-18 02:36:34 +0200211 limit = MIN(meminfo.size, end_addr);
212 while (mtdoffset < limit) {
213 int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
214 int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
215
Baruch Siach7715b482010-08-29 10:36:50 +0300216 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200217 if (blockstart == mtdoffset) {
218 /* starting a new eraseblock */
219 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200220 if (IS_NANDWRITE)
221 printf("Writing at 0x%08x\n", mtdoffset);
Richard Genoudf17fbe12014-06-24 12:12:59 +0200222 else if (mtdoffset > blockstart) {
223 if (bb_method == BB_PADBAD) {
224 /* dump FF padded bad block */
225 int bad_len = MIN(mtdoffset, limit) - blockstart;
226 dump_bad(&meminfo, bad_len, opts & OPT_o);
227 } else if (bb_method == BB_SKIPBAD) {
228 /* for skipbad, increase the length */
229 if ((end_addr + mtdoffset - blockstart) > end_addr)
230 end_addr += (mtdoffset - blockstart);
231 else
232 end_addr = ~0;
233 limit = MIN(meminfo.size, end_addr);
234 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200235 }
236 if (mtdoffset >= limit)
237 break;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200238 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200239 xlseek(fd, mtdoffset, SEEK_SET);
240
Baruch Siach6f32ea42010-08-25 16:36:17 +0200241 /* get some more data from input */
Baruch Siach36af2f72010-10-18 02:36:34 +0200242 cnt = full_read(input_fd, filebuf, meminfo_writesize);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200243 if (cnt == 0) {
244 /* even with -p, we do not pad past the end of input
245 * (-p only zero-pads last incomplete page)
246 */
247 break;
248 }
249 if (cnt < meminfo_writesize) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200250 if (IS_NANDDUMP)
James Byrne69374872019-07-02 11:35:03 +0200251 bb_simple_error_msg_and_die("short read");
Baruch Siach6f32ea42010-08-25 16:36:17 +0200252 if (!(opts & OPT_p))
James Byrne69374872019-07-02 11:35:03 +0200253 bb_simple_error_msg_and_die("input size is not rounded up to page size, "
Baruch Siach6f32ea42010-08-25 16:36:17 +0200254 "use -p to zero pad");
255 /* zero pad to end of write block */
256 memset(filebuf + cnt, 0, meminfo_writesize - cnt);
257 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200258 xwrite(output_fd, filebuf, meminfo_writesize);
259
Baruch Siach3aeb8702012-10-15 14:32:23 +0200260 if (IS_NANDDUMP && (opts & OPT_o)) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200261 /* Dump OOB data */
262 oob.start = mtdoffset;
263 xioctl(fd, MEMREADOOB, &oob);
264 xwrite(output_fd, oobbuf, meminfo.oobsize);
265 }
266
Baruch Siach6f32ea42010-08-25 16:36:17 +0200267 mtdoffset += meminfo_writesize;
268 if (cnt < meminfo_writesize)
269 break;
270 }
271
Baruch Siach36af2f72010-10-18 02:36:34 +0200272 if (IS_NANDWRITE && cnt != 0) {
Baruch Siach6f32ea42010-08-25 16:36:17 +0200273 /* We filled entire MTD, but did we reach EOF on input? */
274 if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
275 /* no */
James Byrne69374872019-07-02 11:35:03 +0200276 bb_simple_error_msg_and_die("not enough space in MTD device");
Baruch Siach6f32ea42010-08-25 16:36:17 +0200277 }
278 }
279
280 if (ENABLE_FEATURE_CLEAN_UP) {
281 free(filebuf);
282 close(fd);
283 }
284
285 return EXIT_SUCCESS;
286}