2009-11-21 23:02:19 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* busybox reimplementation of flashcp
|
|
|
|
*
|
|
|
|
* (C) 2009 Stefan Seyfried <seife@sphairon.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2009-11-21 23:02:19 +05:30
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config FLASHCP
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "flashcp (5.4 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default n # doesn't build on Ubuntu 8.04
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: The flashcp binary, inspired by mtd-utils as of git head 5eceb74f7.
|
|
|
|
//config: This utility is used to copy images into a MTD device.
|
2009-11-21 23:02:19 +05:30
|
|
|
|
2016-11-23 04:24:17 +05:30
|
|
|
//applet:IF_FLASHCP(APPLET(flashcp, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
2017-08-07 19:30:25 +05:30
|
|
|
/* not NOEXEC: if flash operation stalls, use less memory in "hung" process */
|
2016-11-23 04:24:17 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_FLASHCP) += flashcp.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define flashcp_trivial_usage
|
|
|
|
//usage: "-v FILE MTD_DEVICE"
|
|
|
|
//usage:#define flashcp_full_usage "\n\n"
|
|
|
|
//usage: "Copy an image to MTD device\n"
|
|
|
|
//usage: "\n -v Verbose"
|
|
|
|
|
2009-11-21 23:02:19 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include <mtd/mtd-user.h>
|
|
|
|
|
2013-01-23 16:11:22 +05:30
|
|
|
/* If 1, simulates "flashing" by writing to existing regular file */
|
2009-11-21 23:02:19 +05:30
|
|
|
#define MTD_DEBUG 0
|
|
|
|
|
|
|
|
#define OPT_v (1 << 0)
|
|
|
|
|
2014-12-10 18:14:27 +05:30
|
|
|
#define BUFSIZE (4 * 1024)
|
2009-11-21 23:02:19 +05:30
|
|
|
|
|
|
|
static void progress(int mode, uoff_t count, uoff_t total)
|
|
|
|
{
|
|
|
|
uoff_t percent;
|
|
|
|
|
|
|
|
if (!option_mask32) //if (!(option_mask32 & OPT_v))
|
|
|
|
return;
|
|
|
|
percent = count * 100;
|
|
|
|
if (total)
|
|
|
|
percent = (unsigned) (percent / total);
|
|
|
|
printf("\r%s: %"OFF_FMT"u/%"OFF_FMT"u (%u%%) ",
|
2013-01-28 16:56:29 +05:30
|
|
|
(mode < 0) ? "Erasing block" : ((mode == 0) ? "Writing kb" : "Verifying kb"),
|
2009-11-21 23:02:19 +05:30
|
|
|
count, total, (unsigned)percent);
|
|
|
|
fflush_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void progress_newline(void)
|
|
|
|
{
|
|
|
|
if (!option_mask32) //if (!(option_mask32 & OPT_v))
|
|
|
|
return;
|
|
|
|
bb_putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
int flashcp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int flashcp_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
int fd_f, fd_d; /* input file and mtd device file descriptors */
|
|
|
|
int i;
|
|
|
|
uoff_t erase_count;
|
|
|
|
struct mtd_info_user mtd;
|
|
|
|
struct erase_info_user e;
|
|
|
|
struct stat statb;
|
|
|
|
// const char *filename, *devicename;
|
|
|
|
RESERVE_CONFIG_UBUFFER(buf, BUFSIZE);
|
|
|
|
RESERVE_CONFIG_UBUFFER(buf2, BUFSIZE);
|
|
|
|
|
|
|
|
opt_complementary = "=2"; /* exactly 2 non-option args: file, dev */
|
2012-09-27 19:36:34 +05:30
|
|
|
/*opts =*/ getopt32(argv, "v");
|
2009-11-21 23:02:19 +05:30
|
|
|
argv += optind;
|
|
|
|
// filename = *argv++;
|
|
|
|
// devicename = *argv;
|
|
|
|
#define filename argv[0]
|
|
|
|
#define devicename argv[1]
|
|
|
|
|
|
|
|
/* open input file and mtd device and do sanity checks */
|
|
|
|
fd_f = xopen(filename, O_RDONLY);
|
|
|
|
fstat(fd_f, &statb);
|
|
|
|
fd_d = xopen(devicename, O_SYNC | O_RDWR);
|
|
|
|
#if !MTD_DEBUG
|
|
|
|
if (ioctl(fd_d, MEMGETINFO, &mtd) < 0) {
|
|
|
|
bb_error_msg_and_die("%s is not a MTD flash device", devicename);
|
|
|
|
}
|
|
|
|
if (statb.st_size > mtd.size) {
|
|
|
|
bb_error_msg_and_die("%s bigger than %s", filename, devicename);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
mtd.erasesize = 64 * 1024;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* always erase a complete block */
|
|
|
|
erase_count = (uoff_t)(statb.st_size + mtd.erasesize - 1) / mtd.erasesize;
|
|
|
|
/* erase 1 block at a time to be able to give verbose output */
|
|
|
|
e.length = mtd.erasesize;
|
|
|
|
#if 0
|
|
|
|
/* (1) bloat
|
|
|
|
* (2) will it work for multi-gigabyte devices?
|
|
|
|
* (3) worse wrt error detection granularity
|
|
|
|
*/
|
|
|
|
/* optimization: if not verbose, erase in one go */
|
|
|
|
if (!opts) { // if (!(opts & OPT_v))
|
|
|
|
e.length = mtd.erasesize * erase_count;
|
|
|
|
erase_count = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
e.start = 0;
|
|
|
|
for (i = 1; i <= erase_count; i++) {
|
2013-01-23 16:11:22 +05:30
|
|
|
progress(-1, i, erase_count);
|
2009-11-21 23:02:19 +05:30
|
|
|
#if !MTD_DEBUG
|
|
|
|
if (ioctl(fd_d, MEMERASE, &e) < 0) {
|
|
|
|
bb_perror_msg_and_die("erase error at 0x%llx on %s",
|
|
|
|
(long long)e.start, devicename);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
usleep(100*1000);
|
|
|
|
#endif
|
|
|
|
e.start += mtd.erasesize;
|
|
|
|
}
|
|
|
|
progress_newline();
|
|
|
|
|
|
|
|
/* doing this outer loop gives significantly smaller code
|
|
|
|
* than doing two separate loops for writing and verifying */
|
2013-01-23 16:11:22 +05:30
|
|
|
for (i = 0; i <= 1; i++) {
|
2009-11-21 23:02:19 +05:30
|
|
|
uoff_t done;
|
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
xlseek(fd_f, 0, SEEK_SET);
|
|
|
|
xlseek(fd_d, 0, SEEK_SET);
|
|
|
|
done = 0;
|
|
|
|
count = BUFSIZE;
|
|
|
|
while (1) {
|
2013-01-23 16:11:22 +05:30
|
|
|
uoff_t rem;
|
|
|
|
|
|
|
|
progress(i, done / 1024, (uoff_t)statb.st_size / 1024);
|
|
|
|
rem = statb.st_size - done;
|
2009-11-21 23:02:19 +05:30
|
|
|
if (rem == 0)
|
|
|
|
break;
|
|
|
|
if (rem < BUFSIZE)
|
|
|
|
count = rem;
|
|
|
|
xread(fd_f, buf, count);
|
2013-01-23 16:11:22 +05:30
|
|
|
if (i == 0) {
|
2009-11-21 23:02:19 +05:30
|
|
|
int ret;
|
2013-01-23 16:11:22 +05:30
|
|
|
if (count < BUFSIZE)
|
|
|
|
memset((char*)buf + count, 0, BUFSIZE - count);
|
2009-11-21 23:02:19 +05:30
|
|
|
errno = 0;
|
2013-01-23 16:11:22 +05:30
|
|
|
ret = full_write(fd_d, buf, BUFSIZE);
|
|
|
|
if (ret != BUFSIZE) {
|
2009-11-21 23:02:19 +05:30
|
|
|
bb_perror_msg_and_die("write error at 0x%"OFF_FMT"x on %s, "
|
|
|
|
"write returned %d",
|
|
|
|
done, devicename, ret);
|
|
|
|
}
|
2013-01-23 16:11:22 +05:30
|
|
|
} else { /* i == 1 */
|
2009-11-21 23:02:19 +05:30
|
|
|
xread(fd_d, buf2, count);
|
2013-01-23 16:11:22 +05:30
|
|
|
if (memcmp(buf, buf2, count) != 0) {
|
2009-11-21 23:02:19 +05:30
|
|
|
bb_error_msg_and_die("verification mismatch at 0x%"OFF_FMT"x", done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_newline();
|
|
|
|
}
|
|
|
|
/* we won't come here if there was an error */
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|