2010-08-25 20:06:17 +05:30
|
|
|
/*
|
2010-10-18 06:06:34 +05:30
|
|
|
* nandwrite and nanddump ported to busybox from mtd-utils
|
2010-08-25 20:06:17 +05:30
|
|
|
*
|
|
|
|
* Author: Baruch Siach <baruch@tkos.co.il>, Orex Computed Radiography
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*
|
|
|
|
* TODO: add support for large (>4GB) MTD devices
|
|
|
|
*/
|
|
|
|
//config:config NANDWRITE
|
2017-07-19 18:02:54 +05:30
|
|
|
//config: bool "nandwrite (5.9 kb)"
|
2011-08-13 12:30:29 +05:30
|
|
|
//config: default y
|
2011-02-27 02:54:08 +05:30
|
|
|
//config: select PLATFORM_LINUX
|
2010-08-25 20:06:17 +05:30
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Write to the specified MTD device, with bad blocks awareness
|
2010-10-18 06:06:34 +05:30
|
|
|
//config:
|
|
|
|
//config:config NANDDUMP
|
2017-07-19 18:02:54 +05:30
|
|
|
//config: bool "nanddump (6.3 kb)"
|
2011-08-13 12:30:29 +05:30
|
|
|
//config: default y
|
2011-02-27 02:54:08 +05:30
|
|
|
//config: select PLATFORM_LINUX
|
2010-10-18 06:06:34 +05:30
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Dump the content of raw NAND chip
|
2010-08-25 20:06:17 +05:30
|
|
|
|
2011-08-13 12:30:29 +05:30
|
|
|
//applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
2013-03-15 04:57:41 +05:30
|
|
|
//applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
|
2011-08-13 12:30:29 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
|
|
|
|
//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
|
|
|
|
|
2010-08-25 20:06:17 +05:30
|
|
|
//usage:#define nandwrite_trivial_usage
|
2015-12-20 02:04:44 +05:30
|
|
|
//usage: "[-np] [-s ADDR] MTD_DEVICE [FILE]"
|
2010-08-25 20:06:17 +05:30
|
|
|
//usage:#define nandwrite_full_usage "\n\n"
|
2013-03-15 04:57:41 +05:30
|
|
|
//usage: "Write to MTD_DEVICE\n"
|
2015-12-20 02:04:44 +05:30
|
|
|
//usage: "\n -n Write without ecc"
|
2010-08-25 20:06:17 +05:30
|
|
|
//usage: "\n -p Pad to page size"
|
|
|
|
//usage: "\n -s ADDR Start address"
|
|
|
|
|
2010-10-18 06:06:34 +05:30
|
|
|
//usage:#define nanddump_trivial_usage
|
2017-01-21 07:19:58 +05:30
|
|
|
//usage: "[-no]" IF_LONG_OPTS(" [--bb padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
|
2010-10-18 06:06:34 +05:30
|
|
|
//usage:#define nanddump_full_usage "\n\n"
|
2013-03-15 04:57:41 +05:30
|
|
|
//usage: "Dump MTD_DEVICE\n"
|
2015-12-20 02:04:44 +05:30
|
|
|
//usage: "\n -n Read without ecc"
|
2012-10-15 18:02:23 +05:30
|
|
|
//usage: "\n -o Dump oob data"
|
2010-10-18 06:06:34 +05:30
|
|
|
//usage: "\n -s ADDR Start address"
|
|
|
|
//usage: "\n -l LEN Length"
|
|
|
|
//usage: "\n -f FILE Dump to file ('-' for stdout)"
|
2014-12-18 03:57:26 +05:30
|
|
|
//usage: IF_LONG_OPTS(
|
2017-01-21 07:19:58 +05:30
|
|
|
//usage: "\n --bb METHOD"
|
2014-06-24 15:42:59 +05:30
|
|
|
//usage: "\n skipbad: skip bad blocks"
|
|
|
|
//usage: "\n padbad: substitute bad blocks by 0xff (default)"
|
2014-12-18 03:57:26 +05:30
|
|
|
//usage: )
|
2010-10-18 06:06:34 +05:30
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include <mtd/mtd-user.h>
|
|
|
|
|
|
|
|
#define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
|
|
|
|
#define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
|
|
|
|
|
2010-10-28 22:27:19 +05:30
|
|
|
#define OPT_p (1 << 0) /* nandwrite only */
|
|
|
|
#define OPT_o (1 << 0) /* nanddump only */
|
2015-12-20 02:04:44 +05:30
|
|
|
#define OPT_n (1 << 1)
|
|
|
|
#define OPT_s (1 << 2)
|
|
|
|
#define OPT_f (1 << 3)
|
|
|
|
#define OPT_l (1 << 4)
|
|
|
|
#define OPT_bb (1 << 5) /* must be the last one in the list */
|
2014-06-24 15:42:59 +05:30
|
|
|
|
|
|
|
#define BB_PADBAD (1 << 0)
|
|
|
|
#define BB_SKIPBAD (1 << 1)
|
2010-10-18 06:06:34 +05:30
|
|
|
|
|
|
|
/* helper for writing out 0xff for bad blocks pad */
|
|
|
|
static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
|
|
|
|
{
|
|
|
|
unsigned char buf[meminfo->writesize];
|
|
|
|
unsigned count;
|
|
|
|
|
2014-06-24 15:42:58 +05:30
|
|
|
/* round len to the next page only if len is not already on a page */
|
|
|
|
len = ((len - 1) | (meminfo->writesize - 1)) + 1;
|
2010-10-18 06:06:34 +05:30
|
|
|
|
|
|
|
memset(buf, 0xff, sizeof(buf));
|
|
|
|
for (count = 0; count < len; count += meminfo->writesize) {
|
|
|
|
xwrite(STDOUT_FILENO, buf, meminfo->writesize);
|
|
|
|
if (oob)
|
|
|
|
xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-25 20:06:17 +05:30
|
|
|
static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
|
|
|
|
unsigned block_offset)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
loff_t offs;
|
2010-10-18 06:06:34 +05:30
|
|
|
|
|
|
|
if (block_offset >= meminfo->size) {
|
|
|
|
if (IS_NANDWRITE)
|
|
|
|
bb_error_msg_and_die("not enough space in MTD device");
|
|
|
|
return block_offset; /* let the caller exit */
|
|
|
|
}
|
2010-08-25 20:06:17 +05:30
|
|
|
offs = block_offset;
|
|
|
|
if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
|
|
|
|
return block_offset;
|
|
|
|
/* ioctl returned 1 => "bad block" */
|
2010-10-18 06:06:34 +05:30
|
|
|
if (IS_NANDWRITE)
|
|
|
|
printf("Skipping bad block at 0x%08x\n", block_offset);
|
2010-08-25 20:06:17 +05:30
|
|
|
block_offset += meminfo->erasesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int nandwrite_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
2010-10-18 06:06:34 +05:30
|
|
|
/* Buffer for OOB data */
|
2010-10-18 15:05:30 +05:30
|
|
|
unsigned char *oobbuf;
|
2010-08-25 20:06:17 +05:30
|
|
|
unsigned opts;
|
2014-06-25 21:08:30 +05:30
|
|
|
unsigned bb_method = BB_SKIPBAD;
|
2010-08-25 20:06:17 +05:30
|
|
|
int fd;
|
|
|
|
ssize_t cnt;
|
2010-10-18 06:06:34 +05:30
|
|
|
unsigned mtdoffset, meminfo_writesize, blockstart, limit;
|
|
|
|
unsigned end_addr = ~0;
|
2010-08-25 20:06:17 +05:30
|
|
|
struct mtd_info_user meminfo;
|
2010-10-18 06:06:34 +05:30
|
|
|
struct mtd_oob_buf oob;
|
2010-08-25 20:06:17 +05:30
|
|
|
unsigned char *filebuf;
|
2014-06-24 15:42:59 +05:30
|
|
|
const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
|
2010-10-18 06:06:34 +05:30
|
|
|
|
|
|
|
if (IS_NANDDUMP) {
|
2017-08-09 01:25:02 +05:30
|
|
|
opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
|
getopt32: remove applet_long_options
FEATURE_GETOPT_LONG made dependent on LONG_OPTS.
The folloving options are removed, now LONG_OPTS enables long options
for affected applets:
FEATURE_ENV_LONG_OPTIONS FEATURE_EXPAND_LONG_OPTIONS
FEATURE_UNEXPAND_LONG_OPTIONS FEATURE_MKDIR_LONG_OPTIONS
FEATURE_MV_LONG_OPTIONS FEATURE_RMDIR_LONG_OPTIONS
FEATURE_ADDGROUP_LONG_OPTIONS FEATURE_ADDUSER_LONG_OPTIONS
FEATURE_HWCLOCK_LONG_OPTIONS FEATURE_NSENTER_LONG_OPTS
FEATURE_CHCON_LONG_OPTIONS FEATURE_RUNCON_LONG_OPTIONS
They either had a small number of long options, or their long options are
essential.
Example: upstream addgroup and adduser have ONLY longopts,
we should probably go further and get rid
of non-standard short options.
To this end, make addgroup and adduser "select LONG_OPTS".
We had this breakage caused by us even in our own package!
#if ENABLE_LONG_OPTS || !ENABLE_ADDGROUP
/* We try to use --gid, not -g, because "standard" addgroup
* has no short option -g, it has only long --gid.
*/
argv[1] = (char*)"--gid";
#else
/* Breaks if system in fact does NOT use busybox addgroup */
argv[1] = (char*)"-g";
#endif
xargs: its lone longopt no longer depends on DESKTOP, only on LONG_OPTS.
hwclock TODO: get rid of incompatible -t, -l aliases to --systz, --localtime
Shorten help texts by omitting long option when short opt alternative exists.
Reduction of size comes from the fact that store of an immediate
(an address of longopts) to a fixed address (global variable)
is a longer insn than pushing that immediate or passing it in a register.
This effect is CPU-agnostic.
function old new delta
getopt32 1350 22 -1328
vgetopt32 - 1318 +1318
getopt32long - 24 +24
tftpd_main 562 567 +5
scan_recursive 376 380 +4
collect_cpu 545 546 +1
date_main 1096 1095 -1
hostname_main 262 259 -3
uname_main 259 255 -4
setpriv_main 362 358 -4
rmdir_main 191 187 -4
mv_main 562 558 -4
ipcalc_main 548 544 -4
ifenslave_main 641 637 -4
gzip_main 192 188 -4
gunzip_main 77 73 -4
fsfreeze_main 81 77 -4
flock_main 318 314 -4
deluser_main 337 333 -4
cp_main 374 370 -4
chown_main 175 171 -4
applet_long_options 4 - -4
xargs_main 894 889 -5
wget_main 2540 2535 -5
udhcpc_main 2767 2762 -5
touch_main 436 431 -5
tar_main 1014 1009 -5
start_stop_daemon_main 1033 1028 -5
sed_main 682 677 -5
script_main 1082 1077 -5
run_parts_main 330 325 -5
rtcwake_main 459 454 -5
od_main 2169 2164 -5
nl_main 201 196 -5
modprobe_main 773 768 -5
mkdir_main 160 155 -5
ls_main 568 563 -5
install_main 773 768 -5
hwclock_main 411 406 -5
getopt_main 622 617 -5
fstrim_main 256 251 -5
env_main 198 193 -5
dumpleases_main 635 630 -5
dpkg_main 3991 3986 -5
diff_main 1355 1350 -5
cryptpw_main 233 228 -5
cpio_main 593 588 -5
conspy_main 1135 1130 -5
chpasswd_main 313 308 -5
adduser_main 887 882 -5
addgroup_main 416 411 -5
ftpgetput_main 351 345 -6
get_terminal_width_height 242 234 -8
expand_main 690 680 -10
static.expand_longopts 18 - -18
static.unexpand_longopts 27 - -27
mkdir_longopts 28 - -28
env_longopts 30 - -30
static.ifenslave_longopts 34 - -34
mv_longopts 46 - -46
static.rmdir_longopts 48 - -48
packed_usage 31739 31687 -52
------------------------------------------------------------------------------
(add/remove: 2/8 grow/shrink: 3/49 up/down: 1352/-1840) Total: -488 bytes
text data bss dec hex filename
915681 485 6880 923046 e15a6 busybox_old
915428 485 6876 922789 e14a5 busybox_unstripped
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-08 20:08:18 +05:30
|
|
|
"bb\0" Required_argument "\xff", /* no short equivalent */
|
2017-08-09 01:25:02 +05:30
|
|
|
&opt_s, &opt_f, &opt_l, &opt_bb
|
|
|
|
);
|
2010-10-18 06:06:34 +05:30
|
|
|
} else { /* nandwrite */
|
2017-08-09 01:25:02 +05:30
|
|
|
opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s);
|
2010-10-18 06:06:34 +05:30
|
|
|
}
|
2010-08-25 20:06:17 +05:30
|
|
|
argv += optind;
|
|
|
|
|
2010-10-18 06:06:34 +05:30
|
|
|
if (IS_NANDWRITE && argv[1])
|
|
|
|
opt_f = argv[1];
|
|
|
|
if (!LONE_DASH(opt_f)) {
|
|
|
|
int tmp_fd = xopen(opt_f,
|
|
|
|
IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
|
|
|
|
);
|
|
|
|
xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
|
|
|
|
}
|
2010-08-25 20:06:17 +05:30
|
|
|
|
2012-05-30 20:01:13 +05:30
|
|
|
fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
|
2010-08-25 20:06:17 +05:30
|
|
|
xioctl(fd, MEMGETINFO, &meminfo);
|
|
|
|
|
2015-12-20 02:04:44 +05:30
|
|
|
if (opts & OPT_n)
|
|
|
|
xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
|
|
|
|
|
2010-10-18 06:06:34 +05:30
|
|
|
mtdoffset = xstrtou(opt_s, 0);
|
|
|
|
if (IS_NANDDUMP && (opts & OPT_l)) {
|
|
|
|
unsigned length = xstrtou(opt_l, 0);
|
|
|
|
if (length < meminfo.size - mtdoffset)
|
|
|
|
end_addr = mtdoffset + length;
|
|
|
|
}
|
2014-06-25 21:03:34 +05:30
|
|
|
if (IS_NANDDUMP && (opts & OPT_bb)) {
|
|
|
|
if (strcmp("skipbad", opt_bb) == 0)
|
|
|
|
bb_method = BB_SKIPBAD;
|
|
|
|
else if (strcmp("padbad", opt_bb) == 0)
|
|
|
|
bb_method = BB_PADBAD;
|
|
|
|
else
|
|
|
|
bb_show_usage();
|
2014-06-24 15:42:59 +05:30
|
|
|
}
|
2010-08-25 20:06:17 +05:30
|
|
|
|
|
|
|
/* Pull it into a CPU register (hopefully) - smaller code that way */
|
|
|
|
meminfo_writesize = meminfo.writesize;
|
|
|
|
|
|
|
|
if (mtdoffset & (meminfo_writesize - 1))
|
|
|
|
bb_error_msg_and_die("start address is not page aligned");
|
|
|
|
|
|
|
|
filebuf = xmalloc(meminfo_writesize);
|
2010-10-18 15:05:30 +05:30
|
|
|
oobbuf = xmalloc(meminfo.oobsize);
|
|
|
|
|
|
|
|
oob.start = 0;
|
|
|
|
oob.length = meminfo.oobsize;
|
|
|
|
oob.ptr = oobbuf;
|
2010-08-25 20:06:17 +05:30
|
|
|
|
2010-08-29 13:06:50 +05:30
|
|
|
blockstart = mtdoffset & ~(meminfo.erasesize - 1);
|
|
|
|
if (blockstart != mtdoffset) {
|
|
|
|
unsigned tmp;
|
|
|
|
/* mtdoffset is in the middle of an erase block, verify that
|
|
|
|
* this block is OK. Advance mtdoffset only if this block is
|
|
|
|
* bad.
|
|
|
|
*/
|
|
|
|
tmp = next_good_eraseblock(fd, &meminfo, blockstart);
|
2010-10-18 06:06:34 +05:30
|
|
|
if (tmp != blockstart) {
|
|
|
|
/* bad block(s), advance mtdoffset */
|
2014-06-24 15:42:59 +05:30
|
|
|
if (IS_NANDDUMP) {
|
|
|
|
if (bb_method == BB_PADBAD) {
|
|
|
|
int bad_len = MIN(tmp, end_addr) - mtdoffset;
|
|
|
|
dump_bad(&meminfo, bad_len, opts & OPT_o);
|
|
|
|
}
|
|
|
|
/* with option skipbad, increase the total length */
|
|
|
|
if (bb_method == BB_SKIPBAD) {
|
|
|
|
end_addr += (tmp - blockstart);
|
|
|
|
}
|
2010-10-18 06:06:34 +05:30
|
|
|
}
|
2010-08-29 13:06:50 +05:30
|
|
|
mtdoffset = tmp;
|
2010-10-18 06:06:34 +05:30
|
|
|
}
|
2010-08-29 13:06:50 +05:30
|
|
|
}
|
|
|
|
|
2010-08-25 20:06:17 +05:30
|
|
|
cnt = -1;
|
2010-10-18 06:06:34 +05:30
|
|
|
limit = MIN(meminfo.size, end_addr);
|
|
|
|
while (mtdoffset < limit) {
|
|
|
|
int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
|
|
|
|
int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
|
|
|
|
|
2010-08-29 13:06:50 +05:30
|
|
|
blockstart = mtdoffset & ~(meminfo.erasesize - 1);
|
2010-08-25 20:06:17 +05:30
|
|
|
if (blockstart == mtdoffset) {
|
|
|
|
/* starting a new eraseblock */
|
|
|
|
mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
|
2010-10-18 06:06:34 +05:30
|
|
|
if (IS_NANDWRITE)
|
|
|
|
printf("Writing at 0x%08x\n", mtdoffset);
|
2014-06-24 15:42:59 +05:30
|
|
|
else if (mtdoffset > blockstart) {
|
|
|
|
if (bb_method == BB_PADBAD) {
|
|
|
|
/* dump FF padded bad block */
|
|
|
|
int bad_len = MIN(mtdoffset, limit) - blockstart;
|
|
|
|
dump_bad(&meminfo, bad_len, opts & OPT_o);
|
|
|
|
} else if (bb_method == BB_SKIPBAD) {
|
|
|
|
/* for skipbad, increase the length */
|
|
|
|
if ((end_addr + mtdoffset - blockstart) > end_addr)
|
|
|
|
end_addr += (mtdoffset - blockstart);
|
|
|
|
else
|
|
|
|
end_addr = ~0;
|
|
|
|
limit = MIN(meminfo.size, end_addr);
|
|
|
|
}
|
2010-10-18 06:06:34 +05:30
|
|
|
}
|
|
|
|
if (mtdoffset >= limit)
|
|
|
|
break;
|
2010-08-25 20:06:17 +05:30
|
|
|
}
|
2010-10-18 06:06:34 +05:30
|
|
|
xlseek(fd, mtdoffset, SEEK_SET);
|
|
|
|
|
2010-08-25 20:06:17 +05:30
|
|
|
/* get some more data from input */
|
2010-10-18 06:06:34 +05:30
|
|
|
cnt = full_read(input_fd, filebuf, meminfo_writesize);
|
2010-08-25 20:06:17 +05:30
|
|
|
if (cnt == 0) {
|
|
|
|
/* even with -p, we do not pad past the end of input
|
|
|
|
* (-p only zero-pads last incomplete page)
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cnt < meminfo_writesize) {
|
2010-10-18 06:06:34 +05:30
|
|
|
if (IS_NANDDUMP)
|
|
|
|
bb_error_msg_and_die("short read");
|
2010-08-25 20:06:17 +05:30
|
|
|
if (!(opts & OPT_p))
|
|
|
|
bb_error_msg_and_die("input size is not rounded up to page size, "
|
|
|
|
"use -p to zero pad");
|
|
|
|
/* zero pad to end of write block */
|
|
|
|
memset(filebuf + cnt, 0, meminfo_writesize - cnt);
|
|
|
|
}
|
2010-10-18 06:06:34 +05:30
|
|
|
xwrite(output_fd, filebuf, meminfo_writesize);
|
|
|
|
|
2012-10-15 18:02:23 +05:30
|
|
|
if (IS_NANDDUMP && (opts & OPT_o)) {
|
2010-10-18 06:06:34 +05:30
|
|
|
/* Dump OOB data */
|
|
|
|
oob.start = mtdoffset;
|
|
|
|
xioctl(fd, MEMREADOOB, &oob);
|
|
|
|
xwrite(output_fd, oobbuf, meminfo.oobsize);
|
|
|
|
}
|
|
|
|
|
2010-08-25 20:06:17 +05:30
|
|
|
mtdoffset += meminfo_writesize;
|
|
|
|
if (cnt < meminfo_writesize)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-10-18 06:06:34 +05:30
|
|
|
if (IS_NANDWRITE && cnt != 0) {
|
2010-08-25 20:06:17 +05:30
|
|
|
/* We filled entire MTD, but did we reach EOF on input? */
|
|
|
|
if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
|
|
|
|
/* no */
|
|
|
|
bb_error_msg_and_die("not enough space in MTD device");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
free(filebuf);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|