blob: e4bb3a90291d092491b980fa573e3332374a4032 [file] [log] [blame]
Stefan Seyfriedd095fd42009-11-21 18:32:19 +01001/* vi: set sw=4 ts=4: */
2/*
3 * busybox reimplementation of flashcp
4 *
5 * (C) 2009 Stefan Seyfried <seife@sphairon.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Stefan Seyfriedd095fd42009-11-21 18:32:19 +01008 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#define flashcp_trivial_usage
11//usage: "-v FILE MTD_DEVICE"
12//usage:#define flashcp_full_usage "\n\n"
13//usage: "Copy an image to MTD device\n"
14//usage: "\nOptions:"
15//usage: "\n -v Verbose"
16
Stefan Seyfriedd095fd42009-11-21 18:32:19 +010017#include "libbb.h"
18#include <mtd/mtd-user.h>
19
20#define MTD_DEBUG 0
21
22#define OPT_v (1 << 0)
23
24#define BUFSIZE (8 * 1024)
25
26static void progress(int mode, uoff_t count, uoff_t total)
27{
28 uoff_t percent;
29
30 if (!option_mask32) //if (!(option_mask32 & OPT_v))
31 return;
32 percent = count * 100;
33 if (total)
34 percent = (unsigned) (percent / total);
35 printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
36 (mode == 0) ? "Erasing block" : ((mode == 1) ? "Writing kb" : "Verifying kb"),
37 count, total, (unsigned)percent);
38 fflush_all();
39}
40
41static void progress_newline(void)
42{
43 if (!option_mask32) //if (!(option_mask32 & OPT_v))
44 return;
45 bb_putchar('\n');
46}
47
48int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49int flashcp_main(int argc UNUSED_PARAM, char **argv)
50{
51 int fd_f, fd_d; /* input file and mtd device file descriptors */
52 int i;
53 uoff_t erase_count;
54 unsigned opts;
55 struct mtd_info_user mtd;
56 struct erase_info_user e;
57 struct stat statb;
58// const char *filename, *devicename;
59 RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
60 RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
61
62 opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
63 opts = getopt32(argv, "v");
64 argv += optind;
65// filename = *argv++;
66// devicename = *argv;
67#define filename argv[0]
68#define devicename argv[1]
69
70 /* open input file and mtd device and do sanity checks */
71 fd_f = xopen(filename, O_RDONLY);
72 fstat(fd_f, &statb);
73 fd_d = xopen(devicename, O_SYNC | O_RDWR);
74#if !MTD_DEBUG
75 if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
76 bb_error_msg_and_die("%s is not a MTD flash device", devicename);
77 }
78 if (statb.st_size > mtd.size) {
79 bb_error_msg_and_die("%s bigger than %s", filename, devicename);
80 }
81#else
82 mtd.erasesize = 64 * 1024;
83#endif
84
85 /* always erase a complete block */
86 erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize;
87 /* erase 1 block at a time to be able to give verbose output */
88 e.length = mtd.erasesize;
89#if 0
90/* (1) bloat
91 * (2) will it work for multi-gigabyte devices?
92 * (3) worse wrt error detection granularity
93 */
94 /* optimization: if not verbose, erase in one go */
95 if (!opts) { // if (!(opts & OPT_v))
96 e.length = mtd.erasesize * erase_count;
97 erase_count = 1;
98 }
99#endif
100 e.start = 0;
101 for (i = 1; i <= erase_count; i++) {
102 progress(0, i, erase_count);
103 errno = 0;
104#if !MTD_DEBUG
105 if (ioctl(fd_d, MEMERASE, &e) < 0) {
106 bb_perror_msg_and_die("erase error at 0x%llx on %s",
107 (long long)e.start, devicename);
108 }
109#else
110 usleep(100*1000);
111#endif
112 e.start += mtd.erasesize;
113 }
114 progress_newline();
115
116 /* doing this outer loop gives significantly smaller code
117 * than doing two separate loops for writing and verifying */
118 for (i = 1; i <= 2; i++) {
119 uoff_t done;
120 unsigned count;
121
122 xlseek(fd_f, 0, SEEK_SET);
123 xlseek(fd_d, 0, SEEK_SET);
124 done = 0;
125 count = BUFSIZE;
126 while (1) {
127 uoff_t rem = statb.st_size - done;
128 if (rem == 0)
129 break;
130 if (rem < BUFSIZE)
131 count = rem;
132 progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
133 xread(fd_f, buf, count);
134 if (i == 1) {
135 int ret;
136 errno = 0;
137 ret = full_write(fd_d, buf, count);
138 if (ret != count) {
139 bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
140 "write returned %d",
141 done, devicename, ret);
142 }
143 } else { /* i == 2 */
144 xread(fd_d, buf2, count);
145 if (memcmp(buf, buf2, count)) {
146 bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
147 }
148 }
149
150 done += count;
151 }
152
153 progress_newline();
154 }
155 /* we won't come here if there was an error */
156
157 return EXIT_SUCCESS;
158}