blob: d5ae1e7f3ce53555f0d4e7b7b5a130a954e144a5 [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"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +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"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +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
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020025//applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
maxwen27116ba2015-08-14 21:41:28 +020026//applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +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"
maxwen27116ba2015-08-14 21:41:28 +020034//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
maxwen27116ba2015-08-14 21:41:28 +020039//usage: "[-o] [-b] [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
Baruch Siach36af2f72010-10-18 02:36:34 +020040//usage:#define nanddump_full_usage "\n\n"
maxwen27116ba2015-08-14 21:41:28 +020041//usage: "Dump MTD_DEVICE\n"
Tanguy Pruvot823694d2012-11-18 13:20:29 +010042//usage: "\n -o Dump oob data"
Baruch Siach36af2f72010-10-18 02:36:34 +020043//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
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020054#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)
Baruch Siach36af2f72010-10-18 02:36:34 +020060
Baruch Siach36af2f72010-10-18 02:36:34 +020061/* helper for writing out 0xff for bad blocks pad */
62static 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
Baruch Siach6f32ea42010-08-25 16:36:17 +020078static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
79 unsigned block_offset)
80{
81 while (1) {
82 loff_t offs;
Baruch Siach36af2f72010-10-18 02:36:34 +020083
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 }
Baruch Siach6f32ea42010-08-25 16:36:17 +020089 offs = block_offset;
90 if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
91 return block_offset;
92 /* ioctl returned 1 => "bad block" */
Baruch Siach36af2f72010-10-18 02:36:34 +020093 if (IS_NANDWRITE)
94 printf("Skipping bad block at 0x%08x\n", block_offset);
Baruch Siach6f32ea42010-08-25 16:36:17 +020095 block_offset += meminfo->erasesize;
96 }
97}
98
99int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
100int nandwrite_main(int argc UNUSED_PARAM, char **argv)
101{
Baruch Siach36af2f72010-10-18 02:36:34 +0200102 /* Buffer for OOB data */
Baruch Siachd982da72010-10-18 11:35:30 +0200103 unsigned char *oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200104 unsigned opts;
105 int fd;
106 ssize_t cnt;
Baruch Siach36af2f72010-10-18 02:36:34 +0200107 unsigned mtdoffset, meminfo_writesize, blockstart, limit;
108 unsigned end_addr = ~0;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200109 struct mtd_info_user meminfo;
Baruch Siach36af2f72010-10-18 02:36:34 +0200110 struct mtd_oob_buf oob;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200111 unsigned char *filebuf;
Baruch Siach36af2f72010-10-18 02:36:34 +0200112 const char *opt_s = "0", *opt_f = "-", *opt_l;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200113
Baruch Siach36af2f72010-10-18 02:36:34 +0200114 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 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200121 argv += optind;
122
Baruch Siach36af2f72010-10-18 02:36:34 +0200123 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 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200131
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100132 fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200133 xioctl(fd, MEMGETINFO, &meminfo);
134
Baruch Siach36af2f72010-10-18 02:36:34 +0200135 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 }
Baruch Siach6f32ea42010-08-25 16:36:17 +0200141
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);
Baruch Siachd982da72010-10-18 11:35:30 +0200149 oobbuf = xmalloc(meminfo.oobsize);
150
151 oob.start = 0;
152 oob.length = meminfo.oobsize;
153 oob.ptr = oobbuf;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200154
Baruch Siach7715b482010-08-29 10:36:50 +0300155 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);
Baruch Siach36af2f72010-10-18 02:36:34 +0200163 if (tmp != blockstart) {
164 /* bad block(s), advance mtdoffset */
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100165 if (IS_NANDDUMP && !(opts & OPT_b)) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200166 int bad_len = MIN(tmp, end_addr) - mtdoffset;
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100167 dump_bad(&meminfo, bad_len, opts & OPT_o);
Baruch Siach36af2f72010-10-18 02:36:34 +0200168 }
Baruch Siach7715b482010-08-29 10:36:50 +0300169 mtdoffset = tmp;
Baruch Siach36af2f72010-10-18 02:36:34 +0200170 }
Baruch Siach7715b482010-08-29 10:36:50 +0300171 }
172
Baruch Siach6f32ea42010-08-25 16:36:17 +0200173 cnt = -1;
Baruch Siach36af2f72010-10-18 02:36:34 +0200174 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
Baruch Siach7715b482010-08-29 10:36:50 +0300179 blockstart = mtdoffset & ~(meminfo.erasesize - 1);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200180 if (blockstart == mtdoffset) {
181 /* starting a new eraseblock */
182 mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
Baruch Siach36af2f72010-10-18 02:36:34 +0200183 if (IS_NANDWRITE)
184 printf("Writing at 0x%08x\n", mtdoffset);
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100185 else if (mtdoffset > blockstart && !(opts & OPT_b)) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200186 int bad_len = MIN(mtdoffset, limit) - blockstart;
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100187 dump_bad(&meminfo, bad_len, opts & OPT_o);
Baruch Siach36af2f72010-10-18 02:36:34 +0200188 }
189 if (mtdoffset >= limit)
190 break;
Baruch Siach6f32ea42010-08-25 16:36:17 +0200191 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200192 xlseek(fd, mtdoffset, SEEK_SET);
193
Baruch Siach6f32ea42010-08-25 16:36:17 +0200194 /* get some more data from input */
Baruch Siach36af2f72010-10-18 02:36:34 +0200195 cnt = full_read(input_fd, filebuf, meminfo_writesize);
Baruch Siach6f32ea42010-08-25 16:36:17 +0200196 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 }
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200202 if (cnt < (ssize_t) meminfo_writesize) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200203 if (IS_NANDDUMP)
204 bb_error_msg_and_die("short read");
Baruch Siach6f32ea42010-08-25 16:36:17 +0200205 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 }
Baruch Siach36af2f72010-10-18 02:36:34 +0200211 xwrite(output_fd, filebuf, meminfo_writesize);
212
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100213 if (IS_NANDDUMP && (opts & OPT_o)) {
Baruch Siach36af2f72010-10-18 02:36:34 +0200214 /* Dump OOB data */
215 oob.start = mtdoffset;
216 xioctl(fd, MEMREADOOB, &oob);
217 xwrite(output_fd, oobbuf, meminfo.oobsize);
218 }
219
Baruch Siach6f32ea42010-08-25 16:36:17 +0200220 mtdoffset += meminfo_writesize;
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200221 if (cnt < (ssize_t) meminfo_writesize)
Baruch Siach6f32ea42010-08-25 16:36:17 +0200222 break;
223 }
224
Baruch Siach36af2f72010-10-18 02:36:34 +0200225 if (IS_NANDWRITE && cnt != 0) {
Baruch Siach6f32ea42010-08-25 16:36:17 +0200226 /* 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}