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