22542eca18
function old new delta vgetopt32 1318 1392 +74 runsvdir_main 703 713 +10 bb_make_directory 423 425 +2 collect_cpu 546 545 -1 opt_chars 3 - -3 opt_complementary 4 - -4 tftpd_main 567 562 -5 ntp_init 476 471 -5 zcip_main 1266 1256 -10 xxd_main 428 418 -10 whois_main 140 130 -10 who_main 463 453 -10 which_main 212 202 -10 wget_main 2535 2525 -10 watchdog_main 291 281 -10 watch_main 222 212 -10 vlock_main 399 389 -10 uuencode_main 332 322 -10 uudecode_main 316 306 -10 unlink_main 45 35 -10 udhcpd_main 1482 1472 -10 udhcpc_main 2762 2752 -10 tune2fs_main 290 280 -10 tunctl_main 366 356 -10 truncate_main 218 208 -10 tr_main 518 508 -10 time_main 1134 1124 -10 tftp_main 286 276 -10 telnetd_main 1873 1863 -10 tcpudpsvd_main 1785 1775 -10 taskset_main 521 511 -10 tar_main 1009 999 -10 tail_main 1644 1634 -10 syslogd_main 1967 1957 -10 switch_root_main 368 358 -10 svlogd_main 1454 1444 -10 sv 1296 1286 -10 stat_main 104 94 -10 start_stop_daemon_main 1028 1018 -10 split_main 542 532 -10 sort_main 796 786 -10 slattach_main 624 614 -10 shuf_main 504 494 -10 setsid_main 96 86 -10 setserial_main 1132 1122 -10 setfont_main 388 378 -10 setconsole_main 78 68 -10 sendmail_main 1209 1199 -10 sed_main 677 667 -10 script_main 1077 1067 -10 run_parts_main 325 315 -10 rtcwake_main 454 444 -10 rm_main 175 165 -10 reformime_main 119 109 -10 readlink_main 123 113 -10 rdate_main 246 236 -10 pwdx_main 189 179 -10 pstree_main 317 307 -10 pscan_main 663 653 -10 popmaildir_main 818 808 -10 pmap_main 80 70 -10 nc_main 1042 1032 -10 mv_main 558 548 -10 mountpoint_main 477 467 -10 mount_main 1264 1254 -10 modprobe_main 768 758 -10 modinfo_main 333 323 -10 mktemp_main 200 190 -10 mkswap_main 324 314 -10 mkfs_vfat_main 1489 1479 -10 microcom_main 715 705 -10 md5_sha1_sum_main 521 511 -10 man_main 867 857 -10 makedevs_main 1052 1042 -10 ls_main 563 553 -10 losetup_main 432 422 -10 loadfont_main 89 79 -10 ln_main 524 514 -10 link_main 75 65 -10 ipcalc_main 544 534 -10 iostat_main 2397 2387 -10 install_main 768 758 -10 id_main 480 470 -10 i2cset_main 1239 1229 -10 i2cget_main 380 370 -10 i2cdump_main 1482 1472 -10 i2cdetect_main 682 672 -10 hwclock_main 406 396 -10 httpd_main 741 731 -10 grep_main 837 827 -10 getty_main 1559 1549 -10 fuser_main 297 287 -10 ftpgetput_main 345 335 -10 ftpd_main 2232 2222 -10 fstrim_main 251 241 -10 fsfreeze_main 77 67 -10 fsck_minix_main 2921 2911 -10 flock_main 314 304 -10 flashcp_main 740 730 -10 flash_eraseall_main 833 823 -10 fdformat_main 532 522 -10 expand_main 680 670 -10 eject_main 335 325 -10 dumpleases_main 630 620 -10 du_main 314 304 -10 dos2unix_main 441 431 -10 diff_main 1350 1340 -10 df_main 1064 1054 -10 date_main 1095 1085 -10 cut_main 961 951 -10 cryptpw_main 228 218 -10 crontab_main 575 565 -10 crond_main 1149 1139 -10 cp_main 370 360 -10 common_traceroute_main 3834 3824 -10 common_ping_main 1767 1757 -10 comm_main 239 229 -10 cmp_main 655 645 -10 chrt_main 379 369 -10 chpst_main 704 694 -10 chpasswd_main 308 298 -10 chown_main 171 161 -10 chmod_main 158 148 -10 cat_main 428 418 -10 bzip2_main 120 110 -10 blkdiscard_main 264 254 -10 base64_main 221 211 -10 arping_main 1665 1655 -10 ar_main 556 546 -10 adjtimex_main 406 396 -10 adduser_main 882 872 -10 addgroup_main 411 401 -10 acpid_main 1198 1188 -10 optstring 11 - -11 opt_string 18 - -18 OPT_STR 25 - -25 ubi_tools_main 1288 1258 -30 ls_options 31 - -31 ------------------------------------------------------------------------------ (add/remove: 0/6 grow/shrink: 3/129 up/down: 86/-1383) Total: -1297 bytes text data bss dec hex filename 915428 485 6876 922789 e14a5 busybox_old 914629 485 6872 921986 e1182 busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
279 lines
8.0 KiB
C
279 lines
8.0 KiB
C
/*
|
|
* nandwrite and nanddump ported to busybox from mtd-utils
|
|
*
|
|
* 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
|
|
//config: bool "nandwrite (5.9 kb)"
|
|
//config: default y
|
|
//config: select PLATFORM_LINUX
|
|
//config: help
|
|
//config: Write to the specified MTD device, with bad blocks awareness
|
|
//config:
|
|
//config:config NANDDUMP
|
|
//config: bool "nanddump (6.3 kb)"
|
|
//config: default y
|
|
//config: select PLATFORM_LINUX
|
|
//config: help
|
|
//config: Dump the content of raw NAND chip
|
|
|
|
//applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
|
//applet:IF_NANDDUMP(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
|
|
|
|
//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
|
|
//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
|
|
|
|
//usage:#define nandwrite_trivial_usage
|
|
//usage: "[-np] [-s ADDR] MTD_DEVICE [FILE]"
|
|
//usage:#define nandwrite_full_usage "\n\n"
|
|
//usage: "Write to MTD_DEVICE\n"
|
|
//usage: "\n -n Write without ecc"
|
|
//usage: "\n -p Pad to page size"
|
|
//usage: "\n -s ADDR Start address"
|
|
|
|
//usage:#define nanddump_trivial_usage
|
|
//usage: "[-no]" IF_LONG_OPTS(" [--bb padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
|
|
//usage:#define nanddump_full_usage "\n\n"
|
|
//usage: "Dump MTD_DEVICE\n"
|
|
//usage: "\n -n Read without ecc"
|
|
//usage: "\n -o Dump oob data"
|
|
//usage: "\n -s ADDR Start address"
|
|
//usage: "\n -l LEN Length"
|
|
//usage: "\n -f FILE Dump to file ('-' for stdout)"
|
|
//usage: IF_LONG_OPTS(
|
|
//usage: "\n --bb METHOD"
|
|
//usage: "\n skipbad: skip bad blocks"
|
|
//usage: "\n padbad: substitute bad blocks by 0xff (default)"
|
|
//usage: )
|
|
|
|
#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')))
|
|
|
|
#define OPT_p (1 << 0) /* nandwrite only */
|
|
#define OPT_o (1 << 0) /* nanddump only */
|
|
#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 */
|
|
|
|
#define BB_PADBAD (1 << 0)
|
|
#define BB_SKIPBAD (1 << 1)
|
|
|
|
/* 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;
|
|
|
|
/* round len to the next page only if len is not already on a page */
|
|
len = ((len - 1) | (meminfo->writesize - 1)) + 1;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
|
|
unsigned block_offset)
|
|
{
|
|
while (1) {
|
|
loff_t offs;
|
|
|
|
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 */
|
|
}
|
|
offs = block_offset;
|
|
if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
|
|
return block_offset;
|
|
/* ioctl returned 1 => "bad block" */
|
|
if (IS_NANDWRITE)
|
|
printf("Skipping bad block at 0x%08x\n", block_offset);
|
|
block_offset += meminfo->erasesize;
|
|
}
|
|
}
|
|
|
|
int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
int nandwrite_main(int argc UNUSED_PARAM, char **argv)
|
|
{
|
|
/* Buffer for OOB data */
|
|
unsigned char *oobbuf;
|
|
unsigned opts;
|
|
unsigned bb_method = BB_SKIPBAD;
|
|
int fd;
|
|
ssize_t cnt;
|
|
unsigned mtdoffset, meminfo_writesize, blockstart, limit;
|
|
unsigned end_addr = ~0;
|
|
struct mtd_info_user meminfo;
|
|
struct mtd_oob_buf oob;
|
|
unsigned char *filebuf;
|
|
const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
|
|
|
|
if (IS_NANDDUMP) {
|
|
opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
|
|
"bb\0" Required_argument "\xff", /* no short equivalent */
|
|
&opt_s, &opt_f, &opt_l, &opt_bb
|
|
);
|
|
} else { /* nandwrite */
|
|
opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s);
|
|
}
|
|
argv += optind;
|
|
|
|
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);
|
|
}
|
|
|
|
fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
|
|
xioctl(fd, MEMGETINFO, &meminfo);
|
|
|
|
if (opts & OPT_n)
|
|
xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
|
|
|
|
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;
|
|
}
|
|
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();
|
|
}
|
|
|
|
/* 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);
|
|
oobbuf = xmalloc(meminfo.oobsize);
|
|
|
|
oob.start = 0;
|
|
oob.length = meminfo.oobsize;
|
|
oob.ptr = oobbuf;
|
|
|
|
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);
|
|
if (tmp != blockstart) {
|
|
/* bad block(s), advance mtdoffset */
|
|
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);
|
|
}
|
|
}
|
|
mtdoffset = tmp;
|
|
}
|
|
}
|
|
|
|
cnt = -1;
|
|
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;
|
|
|
|
blockstart = mtdoffset & ~(meminfo.erasesize - 1);
|
|
if (blockstart == mtdoffset) {
|
|
/* starting a new eraseblock */
|
|
mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
|
|
if (IS_NANDWRITE)
|
|
printf("Writing at 0x%08x\n", mtdoffset);
|
|
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);
|
|
}
|
|
}
|
|
if (mtdoffset >= limit)
|
|
break;
|
|
}
|
|
xlseek(fd, mtdoffset, SEEK_SET);
|
|
|
|
/* get some more data from input */
|
|
cnt = full_read(input_fd, filebuf, meminfo_writesize);
|
|
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) {
|
|
if (IS_NANDDUMP)
|
|
bb_error_msg_and_die("short read");
|
|
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);
|
|
}
|
|
xwrite(output_fd, filebuf, meminfo_writesize);
|
|
|
|
if (IS_NANDDUMP && (opts & OPT_o)) {
|
|
/* Dump OOB data */
|
|
oob.start = mtdoffset;
|
|
xioctl(fd, MEMREADOOB, &oob);
|
|
xwrite(output_fd, oobbuf, meminfo.oobsize);
|
|
}
|
|
|
|
mtdoffset += meminfo_writesize;
|
|
if (cnt < meminfo_writesize)
|
|
break;
|
|
}
|
|
|
|
if (IS_NANDWRITE && cnt != 0) {
|
|
/* 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;
|
|
}
|