Port of the mtd-utils nanddump utility. This nanddump implementation is added
to nandwrite.c for the sake of code sharing.

Signed-off-by: Baruch Siach <[email protected]>
---
 miscutils/nandwrite.c |  156 +++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 132 insertions(+), 24 deletions(-)

diff --git a/miscutils/nandwrite.c b/miscutils/nandwrite.c
index f422426..decdcad 100644
--- a/miscutils/nandwrite.c
+++ b/miscutils/nandwrite.c
@@ -1,5 +1,5 @@
 /*
- * nandwrite.c - ported to busybox from mtd-utils
+ * nandwrite.c - nandwrite and nanddump ported to busybox from mtd-utils
  *
  * Author: Baruch Siach <[email protected]>, Orex Computed Radiography
  *
@@ -9,8 +9,10 @@
  */
 
 //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_NANDDUMP) += nandwrite.o
 
 //config:config NANDWRITE
 //config:      bool "nandwrite"
@@ -18,6 +20,13 @@
 //config:      depends on PLATFORM_LINUX
 //config:      help
 //config:        Write to the specified MTD device, with bad blocks awareness
+//config:
+//config:config NANDDUMP
+//config:      bool "nanddump"
+//config:      default n
+//config:      depends on PLATFORM_LINUX
+//config:      help
+//config:        Dump the content of raw NAND chip
 
 #include "libbb.h"
 #include <mtd/mtd-user.h>
@@ -30,18 +39,68 @@
 //usage:     "\n       -p      Pad to page size"
 //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)"
+
+#define do_nanddump (ENABLE_NANDDUMP && \
+               (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
+
+#define do_nandwrite (!do_nanddump)
+
+#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)
+
+/* Buffer for OOB data */
+#define NAND_MAX_OOBSIZE 256
+static unsigned char oobbuf[NAND_MAX_OOBSIZE];
+
+/* 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,
                unsigned block_offset)
 {
        while (1) {
                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 (do_nandwrite)
+                               bb_error_msg_and_die("not enough space in MTD 
device");
+                       else
+                               return block_offset; /* let the caller exit */
+               }
                offs = block_offset;
                if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
                        return block_offset;
                /* ioctl returned 1 => "bad block" */
-               printf("Skipping bad block at 0x%08x\n", block_offset);
+               if (do_nandwrite)
+                       printf("Skipping bad block at 0x%08x\n", block_offset);
                block_offset += meminfo->erasesize;
        }
 }
@@ -52,28 +111,47 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
        unsigned opts;
        int fd;
        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_oob_buf oob;
        unsigned char *filebuf;
-       const char *opt_s = "0";
-       enum {
-               OPT_p = (1 << 0),
-               OPT_s = (1 << 1),
-       };
-
-       opt_complementary = "-1:?2";
-       opts = getopt32(argv, "ps:", &opt_s);
+       const char *opt_s = "0", *opt_f = "-", *opt_l;
+
+       if (do_nanddump) {
+               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;
 
-       if (argv[1])
-               xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO);
+       if (do_nandwrite && argv[1])
+               opt_f = argv[1];
+       if (!LONE_DASH(opt_f)) {
+               int tmp_fd = xopen3(opt_f, do_nandwrite ? O_RDONLY :
+                               O_WRONLY | O_TRUNC | O_CREAT, 0644);
+               xmove_fd(tmp_fd, do_nandwrite ? STDIN_FILENO : STDOUT_FILENO);
+       }
 
        fd = xopen(argv[0], O_RDWR);
        xioctl(fd, MEMGETINFO, &meminfo);
 
+       oob.start       = 0;
+       oob.length      = meminfo.oobsize;
+       oob.ptr         = oobbuf;
+
        mtdoffset = bb_strtou(opt_s, NULL, 0);
        if (errno)
                bb_error_msg_and_die("invalid number '%s'", opt_s);
+       if (do_nanddump && (opts & OPT_l)) {
+               unsigned length = bb_strtou(opt_l, NULL, 0);
+               if (errno)
+                       bb_error_msg_and_die("invalid number '%s'", opt_l);
+               if (length < meminfo.size - mtdoffset)
+                       end_addr = mtdoffset + length;
+       }
 
        /* Pull it into a CPU register (hopefully) - smaller code that way */
        meminfo_writesize = meminfo.writesize;
@@ -91,20 +169,41 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
                 * bad.
                 */
                tmp = next_good_eraseblock(fd, &meminfo, blockstart);
-               if (tmp != blockstart) /* bad block(s), advance mtdoffset */
+               if (tmp != blockstart) { /* bad block(s), advance mtdoffset */
+                       if (do_nanddump & !(opts & OPT_b)) {
+                               int bad_len = MIN(tmp, end_addr) - mtdoffset;
+                               dump_bad(&meminfo, bad_len, !(opts & OPT_o));
+                       }
                        mtdoffset = tmp;
+               }
        }
 
        cnt = -1;
-       while (mtdoffset < meminfo.size) {
+       limit = MIN(meminfo.size, end_addr);
+       while (mtdoffset < limit) {
+               int input_fd = do_nandwrite ? STDIN_FILENO : fd;
+               int output_fd = do_nandwrite ? fd : STDOUT_FILENO;
+
                blockstart = mtdoffset & ~(meminfo.erasesize - 1);
                if (blockstart == mtdoffset) {
                        /* starting a new eraseblock */
-                       mtdoffset = next_good_eraseblock(fd, &meminfo, 
blockstart);
-                       printf("Writing at 0x%08x\n", mtdoffset);
+                       mtdoffset = next_good_eraseblock(fd, &meminfo,
+                                       blockstart);
+                       if (do_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 */
-               cnt = full_read(STDIN_FILENO, filebuf, meminfo_writesize);
+               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)
@@ -112,20 +211,29 @@ int nandwrite_main(int argc UNUSED_PARAM, char **argv)
                        break;
                }
                if (cnt < meminfo_writesize) {
-                       if (!(opts & OPT_p))
+                       if (do_nanddump)
+                               bb_error_msg_and_die("mtd short read");
+                       else 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);
                }
-               xlseek(fd, mtdoffset, SEEK_SET);
-               xwrite(fd, filebuf, meminfo_writesize);
+               xwrite(output_fd, filebuf, meminfo_writesize);
+
+               if (do_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 (cnt != 0) {
+       if (do_nandwrite && cnt != 0) {
                /* We filled entire MTD, but did we reach EOF on input? */
                if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
                        /* no */
-- 
1.7.1

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to