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 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 9 | //config:config FLASHCP |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "flashcp (5.4 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 11 | //config: default n # doesn't build on Ubuntu 8.04 |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: The flashcp binary, inspired by mtd-utils as of git head 5eceb74f7. |
| 14 | //config: This utility is used to copy images into a MTD device. |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 15 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 16 | //applet:IF_FLASHCP(APPLET(flashcp, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
Denys Vlasenko | 798b945 | 2017-08-07 16:00:25 +0200 | [diff] [blame] | 17 | /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */ |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 18 | |
| 19 | //kbuild:lib-$(CONFIG_FLASHCP) += flashcp.o |
| 20 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | //usage:#define flashcp_trivial_usage |
| 22 | //usage: "-v FILE MTD_DEVICE" |
| 23 | //usage:#define flashcp_full_usage "\n\n" |
| 24 | //usage: "Copy an image to MTD device\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage: "\n -v Verbose" |
| 26 | |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 27 | #include "libbb.h" |
| 28 | #include <mtd/mtd-user.h> |
| 29 | |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 30 | /* If 1, simulates "flashing" by writing to existing regular file */ |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 31 | #define MTD_DEBUG 0 |
| 32 | |
| 33 | #define OPT_v (1 << 0) |
| 34 | |
Jacob Kjaergaard | bd7c1f2 | 2014-12-10 13:44:27 +0100 | [diff] [blame] | 35 | #define BUFSIZE (4 * 1024) |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 36 | |
| 37 | static void progress(int mode, uoff_t count, uoff_t total) |
| 38 | { |
| 39 | uoff_t percent; |
| 40 | |
| 41 | if (!option_mask32) //if (!(option_mask32 & OPT_v)) |
| 42 | return; |
| 43 | percent = count * 100; |
| 44 | if (total) |
| 45 | percent = (unsigned) (percent / total); |
| 46 | printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ", |
Denys Vlasenko | 6be6f3b | 2013-01-28 12:26:29 +0100 | [diff] [blame] | 47 | (mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"), |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 48 | count, total, (unsigned)percent); |
| 49 | fflush_all(); |
| 50 | } |
| 51 | |
| 52 | static void progress_newline(void) |
| 53 | { |
| 54 | if (!option_mask32) //if (!(option_mask32 & OPT_v)) |
| 55 | return; |
| 56 | bb_putchar('\n'); |
| 57 | } |
| 58 | |
| 59 | int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 60 | int flashcp_main(int argc UNUSED_PARAM, char **argv) |
| 61 | { |
| 62 | int fd_f, fd_d; /* input file and mtd device file descriptors */ |
| 63 | int i; |
| 64 | uoff_t erase_count; |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 65 | struct mtd_info_user mtd; |
| 66 | struct erase_info_user e; |
| 67 | struct stat statb; |
| 68 | // const char *filename, *devicename; |
| 69 | RESERVE_CONFIG_UBUFFER(buf, BUFSIZE); |
| 70 | RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE); |
| 71 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 72 | /*opts =*/ getopt32(argv, "^" "v" "\0" "=2"/*exactly 2 non-option args: file,dev*/); |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 73 | argv += optind; |
| 74 | // filename = *argv++; |
| 75 | // devicename = *argv; |
| 76 | #define filename argv[0] |
| 77 | #define devicename argv[1] |
| 78 | |
| 79 | /* open input file and mtd device and do sanity checks */ |
| 80 | fd_f = xopen(filename, O_RDONLY); |
| 81 | fstat(fd_f, &statb); |
| 82 | fd_d = xopen(devicename, O_SYNC | O_RDWR); |
| 83 | #if !MTD_DEBUG |
| 84 | if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) { |
| 85 | bb_error_msg_and_die("%s is not a MTD flash device", devicename); |
| 86 | } |
| 87 | if (statb.st_size > mtd.size) { |
| 88 | bb_error_msg_and_die("%s bigger than %s", filename, devicename); |
| 89 | } |
| 90 | #else |
| 91 | mtd.erasesize = 64 * 1024; |
| 92 | #endif |
| 93 | |
| 94 | /* always erase a complete block */ |
| 95 | erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize; |
| 96 | /* erase 1 block at a time to be able to give verbose output */ |
| 97 | e.length = mtd.erasesize; |
| 98 | #if 0 |
| 99 | /* (1) bloat |
| 100 | * (2) will it work for multi-gigabyte devices? |
| 101 | * (3) worse wrt error detection granularity |
| 102 | */ |
| 103 | /* optimization: if not verbose, erase in one go */ |
| 104 | if (!opts) { // if (!(opts & OPT_v)) |
| 105 | e.length = mtd.erasesize * erase_count; |
| 106 | erase_count = 1; |
| 107 | } |
| 108 | #endif |
| 109 | e.start = 0; |
| 110 | for (i = 1; i <= erase_count; i++) { |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 111 | progress(-1, i, erase_count); |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 112 | #if !MTD_DEBUG |
| 113 | if (ioctl(fd_d, MEMERASE, &e) < 0) { |
| 114 | bb_perror_msg_and_die("erase error at 0x%llx on %s", |
| 115 | (long long)e.start, devicename); |
| 116 | } |
| 117 | #else |
| 118 | usleep(100*1000); |
| 119 | #endif |
| 120 | e.start += mtd.erasesize; |
| 121 | } |
| 122 | progress_newline(); |
| 123 | |
| 124 | /* doing this outer loop gives significantly smaller code |
| 125 | * than doing two separate loops for writing and verifying */ |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 126 | for (i = 0; i <= 1; i++) { |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 127 | uoff_t done; |
| 128 | unsigned count; |
| 129 | |
| 130 | xlseek(fd_f, 0, SEEK_SET); |
| 131 | xlseek(fd_d, 0, SEEK_SET); |
| 132 | done = 0; |
| 133 | count = BUFSIZE; |
| 134 | while (1) { |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 135 | uoff_t rem; |
| 136 | |
| 137 | progress(i, done / 1024, (uoff_t)statb.st_size / 1024); |
| 138 | rem = statb.st_size - done; |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 139 | if (rem == 0) |
| 140 | break; |
| 141 | if (rem < BUFSIZE) |
| 142 | count = rem; |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 143 | xread(fd_f, buf, count); |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 144 | if (i == 0) { |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 145 | int ret; |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 146 | if (count < BUFSIZE) |
| 147 | memset((char*)buf + count, 0, BUFSIZE - count); |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 148 | errno = 0; |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 149 | ret = full_write(fd_d, buf, BUFSIZE); |
| 150 | if (ret != BUFSIZE) { |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 151 | bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, " |
| 152 | "write returned %d", |
| 153 | done, devicename, ret); |
| 154 | } |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 155 | } else { /* i == 1 */ |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 156 | xread(fd_d, buf2, count); |
Denys Vlasenko | 243e733 | 2013-01-23 11:41:22 +0100 | [diff] [blame] | 157 | if (memcmp(buf, buf2, count) != 0) { |
Stefan Seyfried | d095fd4 | 2009-11-21 18:32:19 +0100 | [diff] [blame] | 158 | bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | done += count; |
| 163 | } |
| 164 | |
| 165 | progress_newline(); |
| 166 | } |
| 167 | /* we won't come here if there was an error */ |
| 168 | |
| 169 | return EXIT_SUCCESS; |
| 170 | } |