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 source tree.
9 //config:config FLASHCP
10 //config: bool "flashcp (5.4 kb)"
11 //config: default n # doesn't build on Ubuntu 8.04
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.
16 //applet:IF_FLASHCP(APPLET(flashcp, BB_DIR_USR_SBIN, BB_SUID_DROP))
18 //kbuild:lib-$(CONFIG_FLASHCP) += flashcp.o
20 //usage:#define flashcp_trivial_usage
21 //usage: "-v FILE MTD_DEVICE"
22 //usage:#define flashcp_full_usage "\n\n"
23 //usage: "Copy an image to MTD device\n"
24 //usage: "\n -v Verbose"
27 #include <mtd/mtd-user.h>
29 /* If 1, simulates "flashing" by writing to existing regular file */
32 #define OPT_v (1 << 0)
34 #define BUFSIZE (4 * 1024)
36 static void progress(int mode, uoff_t count, uoff_t total)
40 if (!option_mask32) //if (!(option_mask32 & OPT_v))
42 percent = count * 100;
44 percent = (unsigned) (percent / total);
45 printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
46 (mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
47 count, total, (unsigned)percent);
51 static void progress_newline(void)
53 if (!option_mask32) //if (!(option_mask32 & OPT_v))
58 int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59 int flashcp_main(int argc UNUSED_PARAM, char **argv)
61 int fd_f, fd_d; /* input file and mtd device file descriptors */
64 struct mtd_info_user mtd;
65 struct erase_info_user e;
67 // const char *filename, *devicename;
68 RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
69 RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
71 opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
72 /*opts =*/ getopt32(argv, "v");
74 // filename = *argv++;
75 // devicename = *argv;
76 #define filename argv[0]
77 #define devicename argv[1]
79 /* open input file and mtd device and do sanity checks */
80 fd_f = xopen(filename, O_RDONLY);
82 fd_d = xopen(devicename, O_SYNC | O_RDWR);
84 if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
85 bb_error_msg_and_die("%s is not a MTD flash device", devicename);
87 if (statb.st_size > mtd.size) {
88 bb_error_msg_and_die("%s bigger than %s", filename, devicename);
91 mtd.erasesize = 64 * 1024;
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;
100 * (2) will it work for multi-gigabyte devices?
101 * (3) worse wrt error detection granularity
103 /* optimization: if not verbose, erase in one go */
104 if (!opts) { // if (!(opts & OPT_v))
105 e.length = mtd.erasesize * erase_count;
110 for (i = 1; i <= erase_count; i++) {
111 progress(-1, i, erase_count);
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);
120 e.start += mtd.erasesize;
124 /* doing this outer loop gives significantly smaller code
125 * than doing two separate loops for writing and verifying */
126 for (i = 0; i <= 1; i++) {
130 xlseek(fd_f, 0, SEEK_SET);
131 xlseek(fd_d, 0, SEEK_SET);
137 progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
138 rem = statb.st_size - done;
143 xread(fd_f, buf, count);
147 memset((char*)buf + count, 0, BUFSIZE - count);
149 ret = full_write(fd_d, buf, BUFSIZE);
150 if (ret != BUFSIZE) {
151 bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
153 done, devicename, ret);
155 } else { /* i == 1 */
156 xread(fd_d, buf2, count);
157 if (memcmp(buf, buf2, count) != 0) {
158 bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
167 /* we won't come here if there was an error */