Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * busybox reimplementation of flashcp |
| 4 | * |
| 5 | * (C) 2009 Stefan Seyfried <seife@sphairon.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "libbb.h" |
| 11 | #include <mtd/mtd-user.h> |
| 12 | |
| 13 | #define MTD_DEBUG 0 |
| 14 | |
| 15 | #define OPT_v (1 << 0) |
| 16 | |
| 17 | #define BUFSIZE (8 * 1024) |
| 18 | |
| 19 | static void progress(int mode, uoff_t count, uoff_t total) |
| 20 | { |
| 21 | uoff_t percent; |
| 22 | |
| 23 | if (!option_mask32) //if (!(option_mask32 & OPT_v)) |
| 24 | return; |
| 25 | percent = count * 100; |
| 26 | if (total) |
| 27 | percent = (unsigned) (percent / total); |
| 28 | printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ", |
| 29 | (mode == 0) ? "Erasing block" : ((mode == 1) ? "Writing kb" : "Verifying kb"), |
| 30 | count, total, (unsigned)percent); |
| 31 | fflush_all(); |
| 32 | } |
| 33 | |
| 34 | static void progress_newline(void) |
| 35 | { |
| 36 | if (!option_mask32) //if (!(option_mask32 & OPT_v)) |
| 37 | return; |
| 38 | bb_putchar('\n'); |
| 39 | } |
| 40 | |
| 41 | int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 42 | int flashcp_main(int argc UNUSED_PARAM, char **argv) |
| 43 | { |
| 44 | int fd_f, fd_d; /* input file and mtd device file descriptors */ |
| 45 | int i; |
| 46 | uoff_t erase_count; |
| 47 | unsigned opts; |
| 48 | struct mtd_info_user mtd; |
| 49 | struct erase_info_user e; |
| 50 | struct stat statb; |
| 51 | // const char *filename, *devicename; |
| 52 | RESERVE_CONFIG_UBUFFER(buf, BUFSIZE); |
| 53 | RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE); |
| 54 | |
| 55 | opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */ |
| 56 | opts = getopt32(argv, "v"); |
| 57 | argv += optind; |
| 58 | // filename = *argv++; |
| 59 | // devicename = *argv; |
| 60 | #define filename argv[0] |
| 61 | #define devicename argv[1] |
| 62 | |
| 63 | /* open input file and mtd device and do sanity checks */ |
| 64 | fd_f = xopen(filename, O_RDONLY); |
| 65 | fstat(fd_f, &statb); |
| 66 | fd_d = xopen(devicename, O_SYNC | O_RDWR); |
| 67 | #if !MTD_DEBUG |
| 68 | if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) { |
| 69 | bb_error_msg_and_die("%s is not a MTD flash device", devicename); |
| 70 | } |
| 71 | if (statb.st_size > mtd.size) { |
| 72 | bb_error_msg_and_die("%s bigger than %s", filename, devicename); |
| 73 | } |
| 74 | #else |
| 75 | mtd.erasesize = 64 * 1024; |
| 76 | #endif |
| 77 | |
| 78 | /* always erase a complete block */ |
| 79 | erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize; |
| 80 | /* erase 1 block at a time to be able to give verbose output */ |
| 81 | e.length = mtd.erasesize; |
| 82 | #if 0 |
| 83 | /* (1) bloat |
| 84 | * (2) will it work for multi-gigabyte devices? |
| 85 | * (3) worse wrt error detection granularity |
| 86 | */ |
| 87 | /* optimization: if not verbose, erase in one go */ |
| 88 | if (!opts) { // if (!(opts & OPT_v)) |
| 89 | e.length = mtd.erasesize * erase_count; |
| 90 | erase_count = 1; |
| 91 | } |
| 92 | #endif |
| 93 | e.start = 0; |
| 94 | for (i = 1; i <= erase_count; i++) { |
| 95 | progress(0, i, erase_count); |
| 96 | errno = 0; |
| 97 | #if !MTD_DEBUG |
| 98 | if (ioctl(fd_d, MEMERASE, &e) < 0) { |
| 99 | bb_perror_msg_and_die("erase error at 0x%llx on %s", |
| 100 | (long long)e.start, devicename); |
| 101 | } |
| 102 | #else |
| 103 | usleep(100*1000); |
| 104 | #endif |
| 105 | e.start += mtd.erasesize; |
| 106 | } |
| 107 | progress_newline(); |
| 108 | |
| 109 | /* doing this outer loop gives significantly smaller code |
| 110 | * than doing two separate loops for writing and verifying */ |
| 111 | for (i = 1; i <= 2; i++) { |
| 112 | uoff_t done; |
| 113 | unsigned count; |
| 114 | |
| 115 | xlseek(fd_f, 0, SEEK_SET); |
| 116 | xlseek(fd_d, 0, SEEK_SET); |
| 117 | done = 0; |
| 118 | count = BUFSIZE; |
| 119 | while (1) { |
| 120 | uoff_t rem = statb.st_size - done; |
| 121 | if (rem == 0) |
| 122 | break; |
| 123 | if (rem < BUFSIZE) |
| 124 | count = rem; |
| 125 | progress(i, done / 1024, (uoff_t)statb.st_size / 1024); |
| 126 | xread(fd_f, buf, count); |
| 127 | if (i == 1) { |
| 128 | int ret; |
| 129 | errno = 0; |
| 130 | ret = full_write(fd_d, buf, count); |
| 131 | if (ret != count) { |
| 132 | bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, " |
| 133 | "write returned %d", |
| 134 | done, devicename, ret); |
| 135 | } |
| 136 | } else { /* i == 2 */ |
| 137 | xread(fd_d, buf2, count); |
| 138 | if (memcmp(buf, buf2, count)) { |
| 139 | bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | done += count; |
| 144 | } |
| 145 | |
| 146 | progress_newline(); |
| 147 | } |
| 148 | /* we won't come here if there was an error */ |
| 149 | |
| 150 | return EXIT_SUCCESS; |
| 151 | } |