diff options
-rw-r--r-- | miscutils/flashcp.c | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/miscutils/flashcp.c b/miscutils/flashcp.c index 81cde9072..36b6efe1e 100644 --- a/miscutils/flashcp.c +++ b/miscutils/flashcp.c | |||
@@ -16,6 +16,7 @@ | |||
16 | #include "libbb.h" | 16 | #include "libbb.h" |
17 | #include <mtd/mtd-user.h> | 17 | #include <mtd/mtd-user.h> |
18 | 18 | ||
19 | /* If 1, simulates "flashing" by writing to existing regular file */ | ||
19 | #define MTD_DEBUG 0 | 20 | #define MTD_DEBUG 0 |
20 | 21 | ||
21 | #define OPT_v (1 << 0) | 22 | #define OPT_v (1 << 0) |
@@ -32,7 +33,7 @@ static void progress(int mode, uoff_t count, uoff_t total) | |||
32 | if (total) | 33 | if (total) |
33 | percent = (unsigned) (percent / total); | 34 | percent = (unsigned) (percent / total); |
34 | printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ", | 35 | printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ", |
35 | (mode == 0) ? "Erasing block" : ((mode == 1) ? "Writing kb" : "Verifying kb"), | 36 | (mode == -1) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"), |
36 | count, total, (unsigned)percent); | 37 | count, total, (unsigned)percent); |
37 | fflush_all(); | 38 | fflush_all(); |
38 | } | 39 | } |
@@ -97,8 +98,7 @@ int flashcp_main(int argc UNUSED_PARAM, char **argv) | |||
97 | #endif | 98 | #endif |
98 | e.start = 0; | 99 | e.start = 0; |
99 | for (i = 1; i <= erase_count; i++) { | 100 | for (i = 1; i <= erase_count; i++) { |
100 | progress(0, i, erase_count); | 101 | progress(-1, i, erase_count); |
101 | errno = 0; | ||
102 | #if !MTD_DEBUG | 102 | #if !MTD_DEBUG |
103 | if (ioctl(fd_d, MEMERASE, &e) < 0) { | 103 | if (ioctl(fd_d, MEMERASE, &e) < 0) { |
104 | bb_perror_msg_and_die("erase error at 0x%llx on %s", | 104 | 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) | |||
113 | 113 | ||
114 | /* doing this outer loop gives significantly smaller code | 114 | /* doing this outer loop gives significantly smaller code |
115 | * than doing two separate loops for writing and verifying */ | 115 | * than doing two separate loops for writing and verifying */ |
116 | for (i = 1; i <= 2; i++) { | 116 | for (i = 0; i <= 1; i++) { |
117 | uoff_t done; | 117 | uoff_t done; |
118 | unsigned count; | 118 | unsigned count; |
119 | 119 | ||
@@ -122,25 +122,29 @@ int flashcp_main(int argc UNUSED_PARAM, char **argv) | |||
122 | done = 0; | 122 | done = 0; |
123 | count = BUFSIZE; | 123 | count = BUFSIZE; |
124 | while (1) { | 124 | while (1) { |
125 | uoff_t rem = statb.st_size - done; | 125 | uoff_t rem; |
126 | |||
127 | progress(i, done / 1024, (uoff_t)statb.st_size / 1024); | ||
128 | rem = statb.st_size - done; | ||
126 | if (rem == 0) | 129 | if (rem == 0) |
127 | break; | 130 | break; |
128 | if (rem < BUFSIZE) | 131 | if (rem < BUFSIZE) |
129 | count = rem; | 132 | count = rem; |
130 | progress(i, done / 1024, (uoff_t)statb.st_size / 1024); | ||
131 | xread(fd_f, buf, count); | 133 | xread(fd_f, buf, count); |
132 | if (i == 1) { | 134 | if (i == 0) { |
133 | int ret; | 135 | int ret; |
136 | if (count < BUFSIZE) | ||
137 | memset((char*)buf + count, 0, BUFSIZE - count); | ||
134 | errno = 0; | 138 | errno = 0; |
135 | ret = full_write(fd_d, buf, count); | 139 | ret = full_write(fd_d, buf, BUFSIZE); |
136 | if (ret != count) { | 140 | if (ret != BUFSIZE) { |
137 | bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, " | 141 | bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, " |
138 | "write returned %d", | 142 | "write returned %d", |
139 | done, devicename, ret); | 143 | done, devicename, ret); |
140 | } | 144 | } |
141 | } else { /* i == 2 */ | 145 | } else { /* i == 1 */ |
142 | xread(fd_d, buf2, count); | 146 | xread(fd_d, buf2, count); |
143 | if (memcmp(buf, buf2, count)) { | 147 | if (memcmp(buf, buf2, count) != 0) { |
144 | bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done); | 148 | bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done); |
145 | } | 149 | } |
146 | } | 150 | } |