mirror of
https://github.com/sheumann/hush.git
synced 2024-12-27 01:32:08 +00:00
nanddump: new applet
function old new delta nandwrite_main 418 936 +518 dump_bad - 119 +119 packed_usage 27752 27810 +58 next_good_eraseblock 84 106 +22 applet_names 2366 2375 +9 applet_main 1380 1384 +4 applet_nameofs 690 692 +2 bbconfig_config_bz2 4932 4922 -10 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 6/1 up/down: 732/-10) Total: 722 bytes Signed-off-by: Baruch Siach <baruch@tkos.co.il> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
873bb31d17
commit
36af2f7977
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* nandwrite.c - ported to busybox from mtd-utils
|
* nandwrite and nanddump ported to busybox from mtd-utils
|
||||||
*
|
*
|
||||||
* Author: Baruch Siach <baruch@tkos.co.il>, Orex Computed Radiography
|
* Author: Baruch Siach <baruch@tkos.co.il>, Orex Computed Radiography
|
||||||
*
|
*
|
||||||
@ -9,8 +9,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
//applet:IF_NANDWRITE(APPLET(nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
|
//applet:IF_NANDWRITE(APPLET(nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
|
||||||
|
//applet:IF_NANDWRITE(APPLET_ODDNAME(nanddump, nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP, nanddump))
|
||||||
|
|
||||||
//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
|
//kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
|
||||||
|
//kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
|
||||||
|
|
||||||
//config:config NANDWRITE
|
//config:config NANDWRITE
|
||||||
//config: bool "nandwrite"
|
//config: bool "nandwrite"
|
||||||
@ -18,9 +20,13 @@
|
|||||||
//config: depends on PLATFORM_LINUX
|
//config: depends on PLATFORM_LINUX
|
||||||
//config: help
|
//config: help
|
||||||
//config: Write to the specified MTD device, with bad blocks awareness
|
//config: Write to the specified MTD device, with bad blocks awareness
|
||||||
|
//config:
|
||||||
#include "libbb.h"
|
//config:config NANDDUMP
|
||||||
#include <mtd/mtd-user.h>
|
//config: bool "nanddump"
|
||||||
|
//config: default n
|
||||||
|
//config: depends on PLATFORM_LINUX
|
||||||
|
//config: help
|
||||||
|
//config: Dump the content of raw NAND chip
|
||||||
|
|
||||||
//usage:#define nandwrite_trivial_usage
|
//usage:#define nandwrite_trivial_usage
|
||||||
//usage: "[-p] [-s ADDR] MTD_DEVICE [FILE]"
|
//usage: "[-p] [-s ADDR] MTD_DEVICE [FILE]"
|
||||||
@ -30,18 +36,65 @@
|
|||||||
//usage: "\n -p Pad to page size"
|
//usage: "\n -p Pad to page size"
|
||||||
//usage: "\n -s ADDR Start address"
|
//usage: "\n -s ADDR Start address"
|
||||||
|
|
||||||
|
//usage:#define nanddump_trivial_usage
|
||||||
|
//usage: "[-o] [-b] [-s ADDR] [-f FILE] MTD_DEVICE"
|
||||||
|
//usage:#define nanddump_full_usage "\n\n"
|
||||||
|
//usage: "Dump the sepcified MTD device\n"
|
||||||
|
//usage: "\nOptions:"
|
||||||
|
//usage: "\n -o Omit oob data"
|
||||||
|
//usage: "\n -b Omit bad block from the dump"
|
||||||
|
//usage: "\n -s ADDR Start address"
|
||||||
|
//usage: "\n -l LEN Length"
|
||||||
|
//usage: "\n -f FILE Dump to file ('-' for stdout)"
|
||||||
|
|
||||||
|
#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_s (1 << 1)
|
||||||
|
#define OPT_b (1 << 2)
|
||||||
|
#define OPT_f (1 << 3)
|
||||||
|
#define OPT_l (1 << 4)
|
||||||
|
|
||||||
|
#define NAND_MAX_OOBSIZE 256
|
||||||
|
/* 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 */
|
||||||
|
len = (len | ~(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,
|
static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
|
||||||
unsigned block_offset)
|
unsigned block_offset)
|
||||||
{
|
{
|
||||||
while (1) {
|
while (1) {
|
||||||
loff_t offs;
|
loff_t offs;
|
||||||
if (block_offset >= meminfo->size)
|
|
||||||
bb_error_msg_and_die("not enough space in MTD device");
|
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;
|
offs = block_offset;
|
||||||
if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
|
if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
|
||||||
return block_offset;
|
return block_offset;
|
||||||
/* ioctl returned 1 => "bad block" */
|
/* ioctl returned 1 => "bad block" */
|
||||||
printf("Skipping bad block at 0x%08x\n", block_offset);
|
if (IS_NANDWRITE)
|
||||||
|
printf("Skipping bad block at 0x%08x\n", block_offset);
|
||||||
block_offset += meminfo->erasesize;
|
block_offset += meminfo->erasesize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,31 +102,49 @@ static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
|
|||||||
int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||||
int nandwrite_main(int argc UNUSED_PARAM, char **argv)
|
int nandwrite_main(int argc UNUSED_PARAM, char **argv)
|
||||||
{
|
{
|
||||||
|
/* Buffer for OOB data */
|
||||||
|
unsigned char oobbuf[NAND_MAX_OOBSIZE];
|
||||||
unsigned opts;
|
unsigned opts;
|
||||||
int fd;
|
int fd;
|
||||||
ssize_t cnt;
|
ssize_t cnt;
|
||||||
unsigned mtdoffset, meminfo_writesize, blockstart;
|
unsigned mtdoffset, meminfo_writesize, blockstart, limit;
|
||||||
|
unsigned end_addr = ~0;
|
||||||
struct mtd_info_user meminfo;
|
struct mtd_info_user meminfo;
|
||||||
|
struct mtd_oob_buf oob;
|
||||||
unsigned char *filebuf;
|
unsigned char *filebuf;
|
||||||
const char *opt_s = "0";
|
const char *opt_s = "0", *opt_f = "-", *opt_l;
|
||||||
enum {
|
|
||||||
OPT_p = (1 << 0),
|
|
||||||
OPT_s = (1 << 1),
|
|
||||||
};
|
|
||||||
|
|
||||||
opt_complementary = "-1:?2";
|
if (IS_NANDDUMP) {
|
||||||
opts = getopt32(argv, "ps:", &opt_s);
|
opt_complementary = "=1";
|
||||||
|
opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l);
|
||||||
|
} else { /* nandwrite */
|
||||||
|
opt_complementary = "-1:?2";
|
||||||
|
opts = getopt32(argv, "ps:", &opt_s);
|
||||||
|
}
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (argv[1])
|
if (IS_NANDWRITE && argv[1])
|
||||||
xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO);
|
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], O_RDWR);
|
fd = xopen(argv[0], O_RDWR);
|
||||||
xioctl(fd, MEMGETINFO, &meminfo);
|
xioctl(fd, MEMGETINFO, &meminfo);
|
||||||
|
|
||||||
mtdoffset = bb_strtou(opt_s, NULL, 0);
|
oob.start = 0;
|
||||||
if (errno)
|
oob.length = meminfo.oobsize;
|
||||||
bb_error_msg_and_die("invalid number '%s'", opt_s);
|
oob.ptr = oobbuf;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/* Pull it into a CPU register (hopefully) - smaller code that way */
|
/* Pull it into a CPU register (hopefully) - smaller code that way */
|
||||||
meminfo_writesize = meminfo.writesize;
|
meminfo_writesize = meminfo.writesize;
|
||||||
@ -91,20 +162,39 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
* bad.
|
* bad.
|
||||||
*/
|
*/
|
||||||
tmp = next_good_eraseblock(fd, &meminfo, blockstart);
|
tmp = next_good_eraseblock(fd, &meminfo, blockstart);
|
||||||
if (tmp != blockstart) /* bad block(s), advance mtdoffset */
|
if (tmp != blockstart) {
|
||||||
|
/* bad block(s), advance mtdoffset */
|
||||||
|
if (IS_NANDDUMP & !(opts & OPT_b)) {
|
||||||
|
int bad_len = MIN(tmp, end_addr) - mtdoffset;
|
||||||
|
dump_bad(&meminfo, bad_len, !(opts & OPT_o));
|
||||||
|
}
|
||||||
mtdoffset = tmp;
|
mtdoffset = tmp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cnt = -1;
|
cnt = -1;
|
||||||
while (mtdoffset < meminfo.size) {
|
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);
|
blockstart = mtdoffset & ~(meminfo.erasesize - 1);
|
||||||
if (blockstart == mtdoffset) {
|
if (blockstart == mtdoffset) {
|
||||||
/* starting a new eraseblock */
|
/* starting a new eraseblock */
|
||||||
mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
|
mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
|
||||||
printf("Writing at 0x%08x\n", mtdoffset);
|
if (IS_NANDWRITE)
|
||||||
|
printf("Writing at 0x%08x\n", mtdoffset);
|
||||||
|
else if (mtdoffset > blockstart) {
|
||||||
|
int bad_len = MIN(mtdoffset, limit) - blockstart;
|
||||||
|
dump_bad(&meminfo, bad_len, !(opts & OPT_o));
|
||||||
|
}
|
||||||
|
if (mtdoffset >= limit)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
xlseek(fd, mtdoffset, SEEK_SET);
|
||||||
|
|
||||||
/* get some more data from input */
|
/* get some more data from input */
|
||||||
cnt = full_read(STDIN_FILENO, filebuf, meminfo_writesize);
|
cnt = full_read(input_fd, filebuf, meminfo_writesize);
|
||||||
if (cnt == 0) {
|
if (cnt == 0) {
|
||||||
/* even with -p, we do not pad past the end of input
|
/* even with -p, we do not pad past the end of input
|
||||||
* (-p only zero-pads last incomplete page)
|
* (-p only zero-pads last incomplete page)
|
||||||
@ -112,20 +202,29 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (cnt < meminfo_writesize) {
|
if (cnt < meminfo_writesize) {
|
||||||
|
if (IS_NANDDUMP)
|
||||||
|
bb_error_msg_and_die("short read");
|
||||||
if (!(opts & OPT_p))
|
if (!(opts & OPT_p))
|
||||||
bb_error_msg_and_die("input size is not rounded up to page size, "
|
bb_error_msg_and_die("input size is not rounded up to page size, "
|
||||||
"use -p to zero pad");
|
"use -p to zero pad");
|
||||||
/* zero pad to end of write block */
|
/* zero pad to end of write block */
|
||||||
memset(filebuf + cnt, 0, meminfo_writesize - cnt);
|
memset(filebuf + cnt, 0, meminfo_writesize - cnt);
|
||||||
}
|
}
|
||||||
xlseek(fd, mtdoffset, SEEK_SET);
|
xwrite(output_fd, filebuf, meminfo_writesize);
|
||||||
xwrite(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;
|
mtdoffset += meminfo_writesize;
|
||||||
if (cnt < meminfo_writesize)
|
if (cnt < meminfo_writesize)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cnt != 0) {
|
if (IS_NANDWRITE && cnt != 0) {
|
||||||
/* We filled entire MTD, but did we reach EOF on input? */
|
/* We filled entire MTD, but did we reach EOF on input? */
|
||||||
if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
|
if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
|
||||||
/* no */
|
/* no */
|
||||||
|
Loading…
Reference in New Issue
Block a user