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