find:: get rid of nested function (it's a gcc-ism)
[oweals/busybox.git] / miscutils / nandwrite.c
index de30a0c1f93debbbde2e0cfc1ce1c2b9f79ce3bf..e3f9b565dae41214e2843835ff3c90bd389d53a2 100644 (file)
@@ -8,40 +8,38 @@
  * TODO: add support for large (>4GB) MTD devices
  */
 
-//applet:IF_NANDWRITE(APPLET(nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
-//applet:IF_NANDWRITE(APPLET_ODDNAME(nanddump, nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP, nanddump))
-
-//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
-//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
-
 //config:config NANDWRITE
 //config:      bool "nandwrite"
-//config:      default n
-//config:      depends on PLATFORM_LINUX
+//config:      default y
+//config:      select PLATFORM_LINUX
 //config:      help
 //config:        Write to the specified MTD device, with bad blocks awareness
 //config:
 //config:config NANDDUMP
 //config:      bool "nanddump"
-//config:      default n
-//config:      depends on PLATFORM_LINUX
+//config:      default y
+//config:      select PLATFORM_LINUX
 //config:      help
 //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))
+
+//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
+//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
+
 //usage:#define nandwrite_trivial_usage
 //usage:       "[-p] [-s ADDR] MTD_DEVICE [FILE]"
 //usage:#define nandwrite_full_usage "\n\n"
-//usage:       "Write to the specified MTD device\n"
-//usage:     "\nOptions:"
+//usage:       "Write to MTD_DEVICE\n"
 //usage:     "\n       -p      Pad to page size"
 //usage:     "\n       -s ADDR Start address"
 
 //usage:#define nanddump_trivial_usage
-//usage:       "[-o] [-b] [-s ADDR] [-f FILE] MTD_DEVICE"
+//usage:       "[-o] [-b] [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
 //usage:#define nanddump_full_usage "\n\n"
-//usage:       "Dump the sepcified MTD device\n"
-//usage:     "\nOptions:"
-//usage:     "\n       -o      Omit oob data"
+//usage:       "Dump MTD_DEVICE\n"
+//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"
 #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_f  (1 << 3)
-#define OPT_l  (1 << 4)
+#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_f  (1 << 3)
+#define OPT_l  (1 << 4)
 
 /* helper for writing out 0xff for bad blocks pad */
 static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
@@ -131,7 +129,7 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
                xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
        }
 
-       fd = xopen(argv[0], O_RDWR);
+       fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
        xioctl(fd, MEMGETINFO, &meminfo);
 
        mtdoffset = xstrtou(opt_s, 0);
@@ -164,9 +162,9 @@ 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)) {
+                       if (IS_NANDDUMP && !(opts & OPT_b)) {
                                int bad_len = MIN(tmp, end_addr) - mtdoffset;
-                               dump_bad(&meminfo, bad_len, !(opts & OPT_o));
+                               dump_bad(&meminfo, bad_len, opts & OPT_o);
                        }
                        mtdoffset = tmp;
                }
@@ -184,9 +182,9 @@ 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) {
+                       else if (mtdoffset > blockstart && !(opts & OPT_b)) {
                                int bad_len = MIN(mtdoffset, limit) - blockstart;
-                               dump_bad(&meminfo, bad_len, !(opts & OPT_o));
+                               dump_bad(&meminfo, bad_len, opts & OPT_o);
                        }
                        if (mtdoffset >= limit)
                                break;
@@ -212,7 +210,7 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
                }
                xwrite(output_fd, filebuf, meminfo_writesize);
 
-               if (IS_NANDDUMP && !(opts & OPT_o)) {
+               if (IS_NANDDUMP && (opts & OPT_o)) {
                        /* Dump OOB data */
                        oob.start = mtdoffset;
                        xioctl(fd, MEMREADOOB, &oob);