X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=miscutils%2Fnandwrite.c;h=29c800612467057dd6fd603fa4336e82c2b07517;hb=4c9455f967e21d30db0de2e13b6e1115ab8f36ce;hp=e3f9b565dae41214e2843835ff3c90bd389d53a2;hpb=0d61dcd6411129afa6b880653de0a1c4356974cc;p=oweals%2Fbusybox.git diff --git a/miscutils/nandwrite.c b/miscutils/nandwrite.c index e3f9b565d..29c800612 100644 --- a/miscutils/nandwrite.c +++ b/miscutils/nandwrite.c @@ -7,20 +7,19 @@ * * TODO: add support for large (>4GB) MTD devices */ - //config:config NANDWRITE -//config: bool "nandwrite" +//config: bool "nandwrite (5.9 kb)" //config: default y //config: select PLATFORM_LINUX //config: help -//config: Write to the specified MTD device, with bad blocks awareness +//config: Write to the specified MTD device, with bad blocks awareness //config: //config:config NANDDUMP -//config: bool "nanddump" +//config: bool "nanddump (6.3 kb)" //config: default y //config: select PLATFORM_LINUX //config: help -//config: Dump the content of raw NAND chip +//config: Dump the content of raw NAND chip //applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP)) //applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump)) @@ -29,34 +28,53 @@ //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o //usage:#define nandwrite_trivial_usage -//usage: "[-p] [-s ADDR] MTD_DEVICE [FILE]" +//usage: "[-np] [-s ADDR] MTD_DEVICE [FILE]" //usage:#define nandwrite_full_usage "\n\n" //usage: "Write to MTD_DEVICE\n" +//usage: "\n -n Write without ecc" //usage: "\n -p Pad to page size" //usage: "\n -s ADDR Start address" //usage:#define nanddump_trivial_usage -//usage: "[-o] [-b] [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE" +//usage: "[-no]" IF_LONG_OPTS(" [--bb padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE" //usage:#define nanddump_full_usage "\n\n" //usage: "Dump MTD_DEVICE\n" +//usage: "\n -n Read without ecc" //usage: "\n -o Dump oob data" -//usage: "\n -b Omit bad block from the dump" //usage: "\n -s ADDR Start address" //usage: "\n -l LEN Length" //usage: "\n -f FILE Dump to file ('-' for stdout)" +//usage: IF_LONG_OPTS( +//usage: "\n --bb METHOD" +//usage: "\n skipbad: skip bad blocks" +//usage: "\n padbad: substitute bad blocks by 0xff (default)" +//usage: ) #include "libbb.h" #include +/* Old headers call it MTD_MODE_RAW. + * FIXME: In kernel headers, MTD_FILE_MODE_RAW is not a define, + * it's an enum. How I can test for existence of an enum? + */ +#if !defined(MTD_FILE_MODE_RAW) +# define MTD_FILE_MODE_RAW 3 +#endif + + #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd'))) #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd'))) #define OPT_p (1 << 0) /* nandwrite only */ #define OPT_o (1 << 0) /* nanddump only */ -#define OPT_s (1 << 1) -#define OPT_b (1 << 2) +#define OPT_n (1 << 1) +#define OPT_s (1 << 2) #define OPT_f (1 << 3) #define OPT_l (1 << 4) +#define OPT_bb (1 << 5) /* must be the last one in the list */ + +#define BB_PADBAD (1 << 0) +#define BB_SKIPBAD (1 << 1) /* helper for writing out 0xff for bad blocks pad */ static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob) @@ -64,8 +82,8 @@ static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob) unsigned char buf[meminfo->writesize]; unsigned count; - /* round len to the next page */ - len = (len | ~(meminfo->writesize - 1)) + 1; + /* round len to the next page only if len is not already on a page */ + len = ((len - 1) | (meminfo->writesize - 1)) + 1; memset(buf, 0xff, sizeof(buf)); for (count = 0; count < len; count += meminfo->writesize) { @@ -102,6 +120,7 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv) /* Buffer for OOB data */ unsigned char *oobbuf; unsigned opts; + unsigned bb_method = BB_SKIPBAD; int fd; ssize_t cnt; unsigned mtdoffset, meminfo_writesize, blockstart, limit; @@ -109,14 +128,15 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv) struct mtd_info_user meminfo; struct mtd_oob_buf oob; unsigned char *filebuf; - const char *opt_s = "0", *opt_f = "-", *opt_l; + const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb; if (IS_NANDDUMP) { - opt_complementary = "=1"; - opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l); + opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1", + "bb\0" Required_argument "\xff", /* no short equivalent */ + &opt_s, &opt_f, &opt_l, &opt_bb + ); } else { /* nandwrite */ - opt_complementary = "-1:?2"; - opts = getopt32(argv, "ps:", &opt_s); + opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s); } argv += optind; @@ -132,12 +152,23 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv) fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY); xioctl(fd, MEMGETINFO, &meminfo); + if (opts & OPT_n) + xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW); + mtdoffset = xstrtou(opt_s, 0); if (IS_NANDDUMP && (opts & OPT_l)) { unsigned length = xstrtou(opt_l, 0); if (length < meminfo.size - mtdoffset) end_addr = mtdoffset + length; } + if (IS_NANDDUMP && (opts & OPT_bb)) { + if (strcmp("skipbad", opt_bb) == 0) + bb_method = BB_SKIPBAD; + else if (strcmp("padbad", opt_bb) == 0) + bb_method = BB_PADBAD; + else + bb_show_usage(); + } /* Pull it into a CPU register (hopefully) - smaller code that way */ meminfo_writesize = meminfo.writesize; @@ -162,9 +193,15 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv) tmp = next_good_eraseblock(fd, &meminfo, blockstart); if (tmp != blockstart) { /* bad block(s), advance mtdoffset */ - if (IS_NANDDUMP && !(opts & OPT_b)) { - int bad_len = MIN(tmp, end_addr) - mtdoffset; - dump_bad(&meminfo, bad_len, opts & OPT_o); + if (IS_NANDDUMP) { + if (bb_method == BB_PADBAD) { + int bad_len = MIN(tmp, end_addr) - mtdoffset; + dump_bad(&meminfo, bad_len, opts & OPT_o); + } + /* with option skipbad, increase the total length */ + if (bb_method == BB_SKIPBAD) { + end_addr += (tmp - blockstart); + } } mtdoffset = tmp; } @@ -182,9 +219,19 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv) mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart); if (IS_NANDWRITE) printf("Writing at 0x%08x\n", mtdoffset); - else if (mtdoffset > blockstart && !(opts & OPT_b)) { - int bad_len = MIN(mtdoffset, limit) - blockstart; - dump_bad(&meminfo, bad_len, opts & OPT_o); + else if (mtdoffset > blockstart) { + if (bb_method == BB_PADBAD) { + /* dump FF padded bad block */ + int bad_len = MIN(mtdoffset, limit) - blockstart; + dump_bad(&meminfo, bad_len, opts & OPT_o); + } else if (bb_method == BB_SKIPBAD) { + /* for skipbad, increase the length */ + if ((end_addr + mtdoffset - blockstart) > end_addr) + end_addr += (mtdoffset - blockstart); + else + end_addr = ~0; + limit = MIN(meminfo.size, end_addr); + } } if (mtdoffset >= limit) break;