123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- #include "libbb.h"
- #include <mtd/mtd-user.h>
- #define MTD_DEBUG 0
- #define OPT_v (1 << 0)
- #define BUFSIZE (4 * 1024)
- static void progress(int mode, uoff_t count, uoff_t total)
- {
- uoff_t percent;
- if (!option_mask32)
- return;
- percent = count * 100;
- if (total)
- percent = (unsigned) (percent / total);
- printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
- (mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
- count, total, (unsigned)percent);
- fflush_all();
- }
- static void progress_newline(void)
- {
- if (!option_mask32)
- return;
- bb_putchar('\n');
- }
- int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int flashcp_main(int argc UNUSED_PARAM, char **argv)
- {
- int fd_f, fd_d;
- int i;
- uoff_t erase_count;
- struct mtd_info_user mtd;
- struct erase_info_user e;
- struct stat statb;
- RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
- RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
- getopt32(argv, "^" "v" "\0" "=2");
- argv += optind;
- #define filename argv[0]
- #define devicename argv[1]
-
- fd_f = xopen(filename, O_RDONLY);
- fstat(fd_f, &statb);
- fd_d = xopen(devicename, O_SYNC | O_RDWR);
- #if !MTD_DEBUG
- if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
- bb_error_msg_and_die("%s is not a MTD flash device", devicename);
- }
- if (statb.st_size > mtd.size) {
- bb_error_msg_and_die("%s bigger than %s", filename, devicename);
- }
- #else
- mtd.erasesize = 64 * 1024;
- #endif
-
- erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize;
-
- e.length = mtd.erasesize;
- #if 0
-
- if (!opts) {
- e.length = mtd.erasesize * erase_count;
- erase_count = 1;
- }
- #endif
- e.start = 0;
- for (i = 1; i <= erase_count; i++) {
- 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",
- (long long)e.start, devicename);
- }
- #else
- usleep(100*1000);
- #endif
- e.start += mtd.erasesize;
- }
- progress_newline();
-
- for (i = 0; i <= 1; i++) {
- uoff_t done;
- unsigned count;
- xlseek(fd_f, 0, SEEK_SET);
- xlseek(fd_d, 0, SEEK_SET);
- done = 0;
- count = BUFSIZE;
- while (1) {
- 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;
- xread(fd_f, buf, count);
- if (i == 0) {
- int ret;
- if (count < BUFSIZE)
- memset((char*)buf + count, 0, BUFSIZE - count);
- errno = 0;
- 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 {
- xread(fd_d, buf2, count);
- if (memcmp(buf, buf2, count) != 0) {
- bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
- }
- }
- done += count;
- }
- progress_newline();
- }
-
- return EXIT_SUCCESS;
- }
|