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))
17 /* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
19 //kbuild:lib-$(CONFIG_FLASHCP) += flashcp.o
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"
25 //usage: "\n -v Verbose"
28 #include <mtd/mtd-user.h>
30 /* If 1, simulates "flashing" by writing to existing regular file */
33 #define OPT_v (1 << 0)
35 #define BUFSIZE (4 * 1024)
37 static void progress(int mode, uoff_t count, uoff_t total)
41 if (!option_mask32) //if (!(option_mask32 & OPT_v))
43 percent = count * 100;
45 percent = (unsigned) (percent / total);
46 printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
47 (mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
48 count, total, (unsigned)percent);
52 static void progress_newline(void)
54 if (!option_mask32) //if (!(option_mask32 & OPT_v))
59 int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60 int flashcp_main(int argc UNUSED_PARAM, char **argv)
62 int fd_f, fd_d; /* input file and mtd device file descriptors */
65 struct mtd_info_user mtd;
66 struct erase_info_user e;
68 // const char *filename, *devicename;
69 RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
70 RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
72 opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
73 /*opts =*/ getopt32(argv, "v");
75 // filename = *argv++;
76 // devicename = *argv;
77 #define filename argv[0]
78 #define devicename argv[1]
80 /* open input file and mtd device and do sanity checks */
81 fd_f = xopen(filename, O_RDONLY);
83 fd_d = xopen(devicename, O_SYNC | O_RDWR);
85 if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
86 bb_error_msg_and_die("%s is not a MTD flash device", devicename);
88 if (statb.st_size > mtd.size) {
89 bb_error_msg_and_die("%s bigger than %s", filename, devicename);
92 mtd.erasesize = 64 * 1024;
95 /* always erase a complete block */
96 erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize;
97 /* erase 1 block at a time to be able to give verbose output */
98 e.length = mtd.erasesize;
101 * (2) will it work for multi-gigabyte devices?
102 * (3) worse wrt error detection granularity
104 /* optimization: if not verbose, erase in one go */
105 if (!opts) { // if (!(opts & OPT_v))
106 e.length = mtd.erasesize * erase_count;
111 for (i = 1; i <= erase_count; i++) {
112 progress(-1, i, erase_count);
114 if (ioctl(fd_d, MEMERASE, &e) < 0) {
115 bb_perror_msg_and_die("erase error at 0x%llx on %s",
116 (long long)e.start, devicename);
121 e.start += mtd.erasesize;
125 /* doing this outer loop gives significantly smaller code
126 * than doing two separate loops for writing and verifying */
127 for (i = 0; i <= 1; i++) {
131 xlseek(fd_f, 0, SEEK_SET);
132 xlseek(fd_d, 0, SEEK_SET);
138 progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
139 rem = statb.st_size - done;
144 xread(fd_f, buf, count);
148 memset((char*)buf + count, 0, BUFSIZE - count);
150 ret = full_write(fd_d, buf, BUFSIZE);
151 if (ret != BUFSIZE) {
152 bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
154 done, devicename, ret);
156 } else { /* i == 1 */
157 xread(fd_d, buf2, count);
158 if (memcmp(buf, buf2, count) != 0) {
159 bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
168 /* we won't come here if there was an error */