Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 1 | /* |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 2 | * nandwrite and nanddump ported to busybox from mtd-utils |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 3 | * |
| 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 Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 11 | //config:config NANDWRITE |
| 12 | //config: bool "nandwrite" |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 13 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 14 | //config: select PLATFORM_LINUX |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 15 | //config: help |
| 16 | //config: Write to the specified MTD device, with bad blocks awareness |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 17 | //config: |
| 18 | //config:config NANDDUMP |
| 19 | //config: bool "nanddump" |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 20 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 21 | //config: select PLATFORM_LINUX |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 22 | //config: help |
| 23 | //config: Dump the content of raw NAND chip |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 24 | |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 25 | //applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
Alexander Shiyan | 0d61dcd | 2013-03-15 00:27:41 +0100 | [diff] [blame] | 26 | //applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump)) |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 27 | |
| 28 | //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o |
| 29 | //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o |
| 30 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 31 | //usage:#define nandwrite_trivial_usage |
| 32 | //usage: "[-p] [-s ADDR] MTD_DEVICE [FILE]" |
| 33 | //usage:#define nandwrite_full_usage "\n\n" |
Alexander Shiyan | 0d61dcd | 2013-03-15 00:27:41 +0100 | [diff] [blame] | 34 | //usage: "Write to MTD_DEVICE\n" |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 35 | //usage: "\n -p Pad to page size" |
| 36 | //usage: "\n -s ADDR Start address" |
| 37 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 38 | //usage:#define nanddump_trivial_usage |
Alexander Shiyan | 0d61dcd | 2013-03-15 00:27:41 +0100 | [diff] [blame] | 39 | //usage: "[-o] [-b] [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE" |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 40 | //usage:#define nanddump_full_usage "\n\n" |
Alexander Shiyan | 0d61dcd | 2013-03-15 00:27:41 +0100 | [diff] [blame] | 41 | //usage: "Dump MTD_DEVICE\n" |
Baruch Siach | 3aeb870 | 2012-10-15 14:32:23 +0200 | [diff] [blame] | 42 | //usage: "\n -o Dump oob data" |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 43 | //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 Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 54 | #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 Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 60 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 61 | /* helper for writing out 0xff for bad blocks pad */ |
| 62 | static 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 Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 78 | static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo, |
| 79 | unsigned block_offset) |
| 80 | { |
| 81 | while (1) { |
| 82 | loff_t offs; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 83 | |
| 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 Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 89 | offs = block_offset; |
| 90 | if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0) |
| 91 | return block_offset; |
| 92 | /* ioctl returned 1 => "bad block" */ |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 93 | if (IS_NANDWRITE) |
| 94 | printf("Skipping bad block at 0x%08x\n", block_offset); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 95 | block_offset += meminfo->erasesize; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 100 | int nandwrite_main(int argc UNUSED_PARAM, char **argv) |
| 101 | { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 102 | /* Buffer for OOB data */ |
Baruch Siach | d982da7 | 2010-10-18 11:35:30 +0200 | [diff] [blame] | 103 | unsigned char *oobbuf; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 104 | unsigned opts; |
| 105 | int fd; |
| 106 | ssize_t cnt; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 107 | unsigned mtdoffset, meminfo_writesize, blockstart, limit; |
| 108 | unsigned end_addr = ~0; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 109 | struct mtd_info_user meminfo; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 110 | struct mtd_oob_buf oob; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 111 | unsigned char *filebuf; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 112 | const char *opt_s = "0", *opt_f = "-", *opt_l; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 113 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 114 | 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 Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 121 | argv += optind; |
| 122 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 123 | 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 Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 131 | |
Matt Reimer | 6979325 | 2012-05-30 10:31:13 -0400 | [diff] [blame] | 132 | fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 133 | xioctl(fd, MEMGETINFO, &meminfo); |
| 134 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 135 | 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 Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 141 | |
| 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 Siach | d982da7 | 2010-10-18 11:35:30 +0200 | [diff] [blame] | 149 | oobbuf = xmalloc(meminfo.oobsize); |
| 150 | |
| 151 | oob.start = 0; |
| 152 | oob.length = meminfo.oobsize; |
| 153 | oob.ptr = oobbuf; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 154 | |
Baruch Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 155 | 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 Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 163 | if (tmp != blockstart) { |
| 164 | /* bad block(s), advance mtdoffset */ |
Baruch Siach | da0200a | 2012-10-15 14:21:45 +0200 | [diff] [blame] | 165 | if (IS_NANDDUMP && !(opts & OPT_b)) { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 166 | int bad_len = MIN(tmp, end_addr) - mtdoffset; |
Baruch Siach | 3aeb870 | 2012-10-15 14:32:23 +0200 | [diff] [blame] | 167 | dump_bad(&meminfo, bad_len, opts & OPT_o); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 168 | } |
Baruch Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 169 | mtdoffset = tmp; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 170 | } |
Baruch Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 171 | } |
| 172 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 173 | cnt = -1; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 174 | 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 Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 179 | blockstart = mtdoffset & ~(meminfo.erasesize - 1); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 180 | if (blockstart == mtdoffset) { |
| 181 | /* starting a new eraseblock */ |
| 182 | mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 183 | if (IS_NANDWRITE) |
| 184 | printf("Writing at 0x%08x\n", mtdoffset); |
Baruch Siach | 98f6b2d | 2012-10-15 14:25:26 +0200 | [diff] [blame] | 185 | else if (mtdoffset > blockstart && !(opts & OPT_b)) { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 186 | int bad_len = MIN(mtdoffset, limit) - blockstart; |
Baruch Siach | 3aeb870 | 2012-10-15 14:32:23 +0200 | [diff] [blame] | 187 | dump_bad(&meminfo, bad_len, opts & OPT_o); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 188 | } |
| 189 | if (mtdoffset >= limit) |
| 190 | break; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 191 | } |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 192 | xlseek(fd, mtdoffset, SEEK_SET); |
| 193 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 194 | /* get some more data from input */ |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 195 | cnt = full_read(input_fd, filebuf, meminfo_writesize); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 196 | 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 | } |
| 202 | if (cnt < meminfo_writesize) { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 203 | if (IS_NANDDUMP) |
| 204 | bb_error_msg_and_die("short read"); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 205 | 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 Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 211 | xwrite(output_fd, filebuf, meminfo_writesize); |
| 212 | |
Baruch Siach | 3aeb870 | 2012-10-15 14:32:23 +0200 | [diff] [blame] | 213 | if (IS_NANDDUMP && (opts & OPT_o)) { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 214 | /* Dump OOB data */ |
| 215 | oob.start = mtdoffset; |
| 216 | xioctl(fd, MEMREADOOB, &oob); |
| 217 | xwrite(output_fd, oobbuf, meminfo.oobsize); |
| 218 | } |
| 219 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 220 | mtdoffset += meminfo_writesize; |
| 221 | if (cnt < meminfo_writesize) |
| 222 | break; |
| 223 | } |
| 224 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 225 | if (IS_NANDWRITE && cnt != 0) { |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 226 | /* 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 | } |