1 /* vi: set sw=4 ts=4: */
3 * busybox reimplementation of flashcp
5 * (C) 2009 Stefan Seyfried <seife@sphairon.com>
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
11 #include <mtd/mtd-user.h>
15 #define OPT_v (1 << 0)
17 #define BUFSIZE (8 * 1024)
19 static void progress(int mode, uoff_t count, uoff_t total)
23 if (!option_mask32) //if (!(option_mask32 & OPT_v))
25 percent = count * 100;
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);
34 static void progress_newline(void)
36 if (!option_mask32) //if (!(option_mask32 & OPT_v))
41 int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int flashcp_main(int argc UNUSED_PARAM, char **argv)
44 int fd_f, fd_d; /* input file and mtd device file descriptors */
48 struct mtd_info_user mtd;
49 struct erase_info_user e;
51 // const char *filename, *devicename;
52 RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
53 RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
55 opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
56 opts = getopt32(argv, "v");
58 // filename = *argv++;
59 // devicename = *argv;
60 #define filename argv[0]
61 #define devicename argv[1]
63 /* open input file and mtd device and do sanity checks */
64 fd_f = xopen(filename, O_RDONLY);
66 fd_d = xopen(devicename, O_SYNC | O_RDWR);
68 if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
69 bb_error_msg_and_die("%s is not a MTD flash device", devicename);
71 if (statb.st_size > mtd.size) {
72 bb_error_msg_and_die("%s bigger than %s", filename, devicename);
75 mtd.erasesize = 64 * 1024;
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;
84 * (2) will it work for multi-gigabyte devices?
85 * (3) worse wrt error detection granularity
87 /* optimization: if not verbose, erase in one go */
88 if (!opts) { // if (!(opts & OPT_v))
89 e.length = mtd.erasesize * erase_count;
94 for (i = 1; i <= erase_count; i++) {
95 progress(0, i, erase_count);
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);
105 e.start += mtd.erasesize;
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++) {
115 xlseek(fd_f, 0, SEEK_SET);
116 xlseek(fd_d, 0, SEEK_SET);
120 uoff_t rem = statb.st_size - done;
125 progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
126 xread(fd_f, buf, count);
130 ret = full_write(fd_d, buf, count);
132 bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
134 done, devicename, ret);
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);
148 /* we won't come here if there was an error */