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 | */ |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 10 | //config:config NANDWRITE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 11 | //config: bool "nandwrite (4.8 kb)" |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 12 | //config: default y |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: Write to the specified MTD device, with bad blocks awareness |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 15 | //config: |
| 16 | //config:config NANDDUMP |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 17 | //config: bool "nanddump (5.2 kb)" |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 18 | //config: default y |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 19 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 20 | //config: Dump the content of raw NAND chip |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 21 | |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 22 | //applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
Alexander Shiyan | 0d61dcd | 2013-03-15 00:27:41 +0100 | [diff] [blame] | 23 | //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] | 24 | |
| 25 | //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o |
| 26 | //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o |
| 27 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 28 | //usage:#define nandwrite_trivial_usage |
Aaro Koskinen | 76915bf | 2015-12-19 22:34:44 +0200 | [diff] [blame] | 29 | //usage: "[-np] [-s ADDR] MTD_DEVICE [FILE]" |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 30 | //usage:#define nandwrite_full_usage "\n\n" |
Alexander Shiyan | 0d61dcd | 2013-03-15 00:27:41 +0100 | [diff] [blame] | 31 | //usage: "Write to MTD_DEVICE\n" |
Aaro Koskinen | 76915bf | 2015-12-19 22:34:44 +0200 | [diff] [blame] | 32 | //usage: "\n -n Write without ecc" |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 33 | //usage: "\n -p Pad to page size" |
| 34 | //usage: "\n -s ADDR Start address" |
| 35 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 36 | //usage:#define nanddump_trivial_usage |
Denys Vlasenko | bbc7bee | 2017-01-21 02:49:58 +0100 | [diff] [blame] | 37 | //usage: "[-no]" IF_LONG_OPTS(" [--bb padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE" |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 38 | //usage:#define nanddump_full_usage "\n\n" |
Alexander Shiyan | 0d61dcd | 2013-03-15 00:27:41 +0100 | [diff] [blame] | 39 | //usage: "Dump MTD_DEVICE\n" |
Aaro Koskinen | 76915bf | 2015-12-19 22:34:44 +0200 | [diff] [blame] | 40 | //usage: "\n -n Read without ecc" |
Baruch Siach | 3aeb870 | 2012-10-15 14:32:23 +0200 | [diff] [blame] | 41 | //usage: "\n -o Dump oob data" |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 42 | //usage: "\n -s ADDR Start address" |
| 43 | //usage: "\n -l LEN Length" |
| 44 | //usage: "\n -f FILE Dump to file ('-' for stdout)" |
Baruch Siach | ad0d009 | 2014-12-18 00:27:26 +0200 | [diff] [blame] | 45 | //usage: IF_LONG_OPTS( |
Denys Vlasenko | bbc7bee | 2017-01-21 02:49:58 +0100 | [diff] [blame] | 46 | //usage: "\n --bb METHOD" |
Richard Genoud | f17fbe1 | 2014-06-24 12:12:59 +0200 | [diff] [blame] | 47 | //usage: "\n skipbad: skip bad blocks" |
| 48 | //usage: "\n padbad: substitute bad blocks by 0xff (default)" |
Baruch Siach | ad0d009 | 2014-12-18 00:27:26 +0200 | [diff] [blame] | 49 | //usage: ) |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 50 | |
| 51 | #include "libbb.h" |
| 52 | #include <mtd/mtd-user.h> |
| 53 | |
Denys Vlasenko | 586f1ee | 2018-01-14 13:43:31 +0100 | [diff] [blame] | 54 | /* Old headers call it MTD_MODE_RAW. |
| 55 | * FIXME: In kernel headers, MTD_FILE_MODE_RAW is not a define, |
| 56 | * it's an enum. How I can test for existence of an enum? |
| 57 | */ |
| 58 | #if !defined(MTD_FILE_MODE_RAW) |
| 59 | # define MTD_FILE_MODE_RAW 3 |
| 60 | #endif |
| 61 | |
| 62 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 63 | #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd'))) |
| 64 | #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd'))) |
| 65 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 66 | #define OPT_p (1 << 0) /* nandwrite only */ |
| 67 | #define OPT_o (1 << 0) /* nanddump only */ |
Aaro Koskinen | 76915bf | 2015-12-19 22:34:44 +0200 | [diff] [blame] | 68 | #define OPT_n (1 << 1) |
| 69 | #define OPT_s (1 << 2) |
| 70 | #define OPT_f (1 << 3) |
| 71 | #define OPT_l (1 << 4) |
| 72 | #define OPT_bb (1 << 5) /* must be the last one in the list */ |
Richard Genoud | f17fbe1 | 2014-06-24 12:12:59 +0200 | [diff] [blame] | 73 | |
| 74 | #define BB_PADBAD (1 << 0) |
| 75 | #define BB_SKIPBAD (1 << 1) |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 76 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 77 | /* helper for writing out 0xff for bad blocks pad */ |
| 78 | static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob) |
| 79 | { |
| 80 | unsigned char buf[meminfo->writesize]; |
| 81 | unsigned count; |
| 82 | |
Richard Genoud | cbf3bfa | 2014-06-24 12:12:58 +0200 | [diff] [blame] | 83 | /* round len to the next page only if len is not already on a page */ |
| 84 | len = ((len - 1) | (meminfo->writesize - 1)) + 1; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 85 | |
| 86 | memset(buf, 0xff, sizeof(buf)); |
| 87 | for (count = 0; count < len; count += meminfo->writesize) { |
| 88 | xwrite(STDOUT_FILENO, buf, meminfo->writesize); |
| 89 | if (oob) |
| 90 | xwrite(STDOUT_FILENO, buf, meminfo->oobsize); |
| 91 | } |
| 92 | } |
| 93 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 94 | static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo, |
| 95 | unsigned block_offset) |
| 96 | { |
| 97 | while (1) { |
| 98 | loff_t offs; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 99 | |
| 100 | if (block_offset >= meminfo->size) { |
| 101 | if (IS_NANDWRITE) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 102 | bb_simple_error_msg_and_die("not enough space in MTD device"); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 103 | return block_offset; /* let the caller exit */ |
| 104 | } |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 105 | offs = block_offset; |
| 106 | if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0) |
| 107 | return block_offset; |
| 108 | /* ioctl returned 1 => "bad block" */ |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 109 | if (IS_NANDWRITE) |
| 110 | printf("Skipping bad block at 0x%08x\n", block_offset); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 111 | block_offset += meminfo->erasesize; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 116 | int nandwrite_main(int argc UNUSED_PARAM, char **argv) |
| 117 | { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 118 | /* Buffer for OOB data */ |
Baruch Siach | d982da7 | 2010-10-18 11:35:30 +0200 | [diff] [blame] | 119 | unsigned char *oobbuf; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 120 | unsigned opts; |
Richard Genoud | 2a870d0 | 2014-06-25 17:38:30 +0200 | [diff] [blame] | 121 | unsigned bb_method = BB_SKIPBAD; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 122 | int fd; |
| 123 | ssize_t cnt; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 124 | unsigned mtdoffset, meminfo_writesize, blockstart, limit; |
| 125 | unsigned end_addr = ~0; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 126 | struct mtd_info_user meminfo; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 127 | struct mtd_oob_buf oob; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 128 | unsigned char *filebuf; |
Richard Genoud | f17fbe1 | 2014-06-24 12:12:59 +0200 | [diff] [blame] | 129 | const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 130 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 131 | if (IS_NANDDUMP) { |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 132 | opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1", |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 133 | "bb\0" Required_argument "\xff", /* no short equivalent */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 134 | &opt_s, &opt_f, &opt_l, &opt_bb |
| 135 | ); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 136 | } else { /* nandwrite */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 137 | opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 138 | } |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 139 | argv += optind; |
| 140 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 141 | if (IS_NANDWRITE && argv[1]) |
| 142 | opt_f = argv[1]; |
| 143 | if (!LONE_DASH(opt_f)) { |
| 144 | int tmp_fd = xopen(opt_f, |
| 145 | IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY |
| 146 | ); |
| 147 | xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO); |
| 148 | } |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 149 | |
Matt Reimer | 6979325 | 2012-05-30 10:31:13 -0400 | [diff] [blame] | 150 | fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 151 | xioctl(fd, MEMGETINFO, &meminfo); |
| 152 | |
Aaro Koskinen | 76915bf | 2015-12-19 22:34:44 +0200 | [diff] [blame] | 153 | if (opts & OPT_n) |
| 154 | xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW); |
| 155 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 156 | mtdoffset = xstrtou(opt_s, 0); |
| 157 | if (IS_NANDDUMP && (opts & OPT_l)) { |
| 158 | unsigned length = xstrtou(opt_l, 0); |
| 159 | if (length < meminfo.size - mtdoffset) |
| 160 | end_addr = mtdoffset + length; |
| 161 | } |
Richard Genoud | 8feb259 | 2014-06-25 17:33:34 +0200 | [diff] [blame] | 162 | if (IS_NANDDUMP && (opts & OPT_bb)) { |
| 163 | if (strcmp("skipbad", opt_bb) == 0) |
| 164 | bb_method = BB_SKIPBAD; |
| 165 | else if (strcmp("padbad", opt_bb) == 0) |
| 166 | bb_method = BB_PADBAD; |
| 167 | else |
| 168 | bb_show_usage(); |
Richard Genoud | f17fbe1 | 2014-06-24 12:12:59 +0200 | [diff] [blame] | 169 | } |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 170 | |
| 171 | /* Pull it into a CPU register (hopefully) - smaller code that way */ |
| 172 | meminfo_writesize = meminfo.writesize; |
| 173 | |
| 174 | if (mtdoffset & (meminfo_writesize - 1)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 175 | bb_simple_error_msg_and_die("start address is not page aligned"); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 176 | |
| 177 | filebuf = xmalloc(meminfo_writesize); |
Baruch Siach | d982da7 | 2010-10-18 11:35:30 +0200 | [diff] [blame] | 178 | oobbuf = xmalloc(meminfo.oobsize); |
| 179 | |
| 180 | oob.start = 0; |
| 181 | oob.length = meminfo.oobsize; |
| 182 | oob.ptr = oobbuf; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 183 | |
Baruch Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 184 | blockstart = mtdoffset & ~(meminfo.erasesize - 1); |
| 185 | if (blockstart != mtdoffset) { |
| 186 | unsigned tmp; |
| 187 | /* mtdoffset is in the middle of an erase block, verify that |
| 188 | * this block is OK. Advance mtdoffset only if this block is |
| 189 | * bad. |
| 190 | */ |
| 191 | tmp = next_good_eraseblock(fd, &meminfo, blockstart); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 192 | if (tmp != blockstart) { |
| 193 | /* bad block(s), advance mtdoffset */ |
Richard Genoud | f17fbe1 | 2014-06-24 12:12:59 +0200 | [diff] [blame] | 194 | if (IS_NANDDUMP) { |
| 195 | if (bb_method == BB_PADBAD) { |
| 196 | int bad_len = MIN(tmp, end_addr) - mtdoffset; |
| 197 | dump_bad(&meminfo, bad_len, opts & OPT_o); |
| 198 | } |
| 199 | /* with option skipbad, increase the total length */ |
| 200 | if (bb_method == BB_SKIPBAD) { |
| 201 | end_addr += (tmp - blockstart); |
| 202 | } |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 203 | } |
Baruch Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 204 | mtdoffset = tmp; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 205 | } |
Baruch Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 206 | } |
| 207 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 208 | cnt = -1; |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 209 | limit = MIN(meminfo.size, end_addr); |
| 210 | while (mtdoffset < limit) { |
| 211 | int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd; |
| 212 | int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO; |
| 213 | |
Baruch Siach | 7715b48 | 2010-08-29 10:36:50 +0300 | [diff] [blame] | 214 | blockstart = mtdoffset & ~(meminfo.erasesize - 1); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 215 | if (blockstart == mtdoffset) { |
| 216 | /* starting a new eraseblock */ |
| 217 | mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart); |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 218 | if (IS_NANDWRITE) |
| 219 | printf("Writing at 0x%08x\n", mtdoffset); |
Richard Genoud | f17fbe1 | 2014-06-24 12:12:59 +0200 | [diff] [blame] | 220 | else if (mtdoffset > blockstart) { |
| 221 | if (bb_method == BB_PADBAD) { |
| 222 | /* dump FF padded bad block */ |
| 223 | int bad_len = MIN(mtdoffset, limit) - blockstart; |
| 224 | dump_bad(&meminfo, bad_len, opts & OPT_o); |
| 225 | } else if (bb_method == BB_SKIPBAD) { |
| 226 | /* for skipbad, increase the length */ |
| 227 | if ((end_addr + mtdoffset - blockstart) > end_addr) |
| 228 | end_addr += (mtdoffset - blockstart); |
| 229 | else |
| 230 | end_addr = ~0; |
| 231 | limit = MIN(meminfo.size, end_addr); |
| 232 | } |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 233 | } |
| 234 | if (mtdoffset >= limit) |
| 235 | break; |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 236 | } |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 237 | xlseek(fd, mtdoffset, SEEK_SET); |
| 238 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 239 | /* get some more data from input */ |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 240 | cnt = full_read(input_fd, filebuf, meminfo_writesize); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 241 | if (cnt == 0) { |
| 242 | /* even with -p, we do not pad past the end of input |
| 243 | * (-p only zero-pads last incomplete page) |
| 244 | */ |
| 245 | break; |
| 246 | } |
| 247 | if (cnt < meminfo_writesize) { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 248 | if (IS_NANDDUMP) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 249 | bb_simple_error_msg_and_die("short read"); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 250 | if (!(opts & OPT_p)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 251 | bb_simple_error_msg_and_die("input size is not rounded up to page size, " |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 252 | "use -p to zero pad"); |
| 253 | /* zero pad to end of write block */ |
| 254 | memset(filebuf + cnt, 0, meminfo_writesize - cnt); |
| 255 | } |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 256 | xwrite(output_fd, filebuf, meminfo_writesize); |
| 257 | |
Baruch Siach | 3aeb870 | 2012-10-15 14:32:23 +0200 | [diff] [blame] | 258 | if (IS_NANDDUMP && (opts & OPT_o)) { |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 259 | /* Dump OOB data */ |
| 260 | oob.start = mtdoffset; |
| 261 | xioctl(fd, MEMREADOOB, &oob); |
| 262 | xwrite(output_fd, oobbuf, meminfo.oobsize); |
| 263 | } |
| 264 | |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 265 | mtdoffset += meminfo_writesize; |
| 266 | if (cnt < meminfo_writesize) |
| 267 | break; |
| 268 | } |
| 269 | |
Baruch Siach | 36af2f7 | 2010-10-18 02:36:34 +0200 | [diff] [blame] | 270 | if (IS_NANDWRITE && cnt != 0) { |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 271 | /* We filled entire MTD, but did we reach EOF on input? */ |
| 272 | if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) { |
| 273 | /* no */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 274 | bb_simple_error_msg_and_die("not enough space in MTD device"); |
Baruch Siach | 6f32ea4 | 2010-08-25 16:36:17 +0200 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 279 | free(filebuf); |
| 280 | close(fd); |
| 281 | } |
| 282 | |
| 283 | return EXIT_SUCCESS; |
| 284 | } |