flashcp: pad output to BUFSIZE. Hopefully closes 5882
authorDenys Vlasenko <vda.linux@googlemail.com>
Wed, 23 Jan 2013 10:41:22 +0000 (11:41 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 23 Jan 2013 10:41:22 +0000 (11:41 +0100)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/flashcp.c

index 81cde90..36b6efe 100644 (file)
@@ -16,6 +16,7 @@
 #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)
@@ -32,7 +33,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 == -1) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
                count, total, (unsigned)percent);
        fflush_all();
 }
@@ -97,8 +98,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",
@@ -113,7 +113,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;
 
@@ -122,25 +122,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);
                                }
                        }