make --help texts more uniform
[oweals/busybox.git] / miscutils / flashcp.c
index 9472c7527b328fdb492eec621c40038b6e2b6a3d..4bbc67727cb3b70c6dfc2063f63e76b17631a70e 100644 (file)
@@ -4,17 +4,34 @@
  *
  * (C) 2009 Stefan Seyfried <seife@sphairon.com>
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
+//config:config FLASHCP
+//config:      bool "flashcp"
+//config:      default n  # doesn't build on Ubuntu 8.04
+//config:      help
+//config:        The flashcp binary, inspired by mtd-utils as of git head 5eceb74f7.
+//config:        This utility is used to copy images into a MTD device.
+
+//applet:IF_FLASHCP(APPLET(flashcp, BB_DIR_USR_SBIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_FLASHCP) += flashcp.o
+
+//usage:#define flashcp_trivial_usage
+//usage:       "-v FILE MTD_DEVICE"
+//usage:#define flashcp_full_usage "\n\n"
+//usage:       "Copy an image to MTD device\n"
+//usage:     "\n       -v      Verbose"
 
 #include "libbb.h"
 #include <mtd/mtd-user.h>
 
+/* If 1, simulates "flashing" by writing to existing regular file */
 #define MTD_DEBUG 0
 
 #define OPT_v (1 << 0)
 
-#define BUFSIZE (8 * 1024)
+#define BUFSIZE (4 * 1024)
 
 static void progress(int mode, uoff_t count, uoff_t total)
 {
@@ -26,7 +43,7 @@ static void progress(int mode, uoff_t count, uoff_t total)
        if (total)
                percent = (unsigned) (percent / total);
        printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
-               (mode == 0) ? "Erasing block" : ((mode == 1) ? "Writing kb" : "Verifying kb"),
+               (mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
                count, total, (unsigned)percent);
        fflush_all();
 }
@@ -44,7 +61,6 @@ int flashcp_main(int argc UNUSED_PARAM, char **argv)
        int fd_f, fd_d; /* input file and mtd device file descriptors */
        int i;
        uoff_t erase_count;
-       unsigned opts;
        struct mtd_info_user mtd;
        struct erase_info_user e;
        struct stat statb;
@@ -53,7 +69,7 @@ int flashcp_main(int argc UNUSED_PARAM, char **argv)
        RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
 
        opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
-       opts = getopt32(argv, "v");
+       /*opts =*/ getopt32(argv, "v");
        argv += optind;
 //     filename = *argv++;
 //     devicename = *argv;
@@ -92,8 +108,7 @@ int flashcp_main(int argc UNUSED_PARAM, char **argv)
 #endif
        e.start = 0;
        for (i = 1; i <= erase_count; i++) {
-               progress(0, i, erase_count);
-               errno = 0;
+               progress(-1, i, erase_count);
 #if !MTD_DEBUG
                if (ioctl(fd_d, MEMERASE, &e) < 0) {
                        bb_perror_msg_and_die("erase error at 0x%llx on %s",
@@ -108,7 +123,7 @@ int flashcp_main(int argc UNUSED_PARAM, char **argv)
 
        /* doing this outer loop gives significantly smaller code
         * than doing two separate loops for writing and verifying */
-       for (i = 1; i <= 2; i++) {
+       for (i = 0; i <= 1; i++) {
                uoff_t done;
                unsigned count;
 
@@ -117,25 +132,29 @@ int flashcp_main(int argc UNUSED_PARAM, char **argv)
                done = 0;
                count = BUFSIZE;
                while (1) {
-                       uoff_t rem = statb.st_size - done;
+                       uoff_t rem;
+
+                       progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
+                       rem = statb.st_size - done;
                        if (rem == 0)
                                break;
                        if (rem < BUFSIZE)
                                count = rem;
-                       progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
                        xread(fd_f, buf, count);
-                       if (i == 1) {
+                       if (i == 0) {
                                int ret;
+                               if (count < BUFSIZE)
+                                       memset((char*)buf + count, 0, BUFSIZE - count);
                                errno = 0;
-                               ret = full_write(fd_d, buf, count);
-                               if (ret != count) {
+                               ret = full_write(fd_d, buf, BUFSIZE);
+                               if (ret != BUFSIZE) {
                                        bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
                                                "write returned %d",
                                                done, devicename, ret);
                                }
-                       } else { /* i == 2 */
+                       } else { /* i == 1 */
                                xread(fd_d, buf2, count);
-                               if (memcmp(buf, buf2, count)) {
+                               if (memcmp(buf, buf2, count) != 0) {
                                        bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
                                }
                        }