linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: "Brian Norris" <norris@broadcom.com>
To: "Artem Bityutskiy" <dedekind1@gmail.com>
Cc: linux-mtd@lists.infradead.org, Brian Norris <norris@broadcom.com>
Subject: [PATCH v2 2/6] mtd-utils/nanddump.c: Robust pretty hexdump
Date: Mon, 19 Jul 2010 10:33:14 -0700	[thread overview]
Message-ID: <1279560796-6388-1-git-send-email-norris@broadcom.com> (raw)
In-Reply-To: <1279437819.16247.19.camel@localhost.localdomain>

These should be what you were looking for.

Adapted code from the linux kernel hex_dump_to_buffer() (lib/hexdump.c)
to provide a more robust hexdump for the pretty option. Now, nanddump
can print out any size of OOB (or page for that matter...) without
having to worry about non-multiples of 16.

This also provides ability to dump ASCII format next to the hex output
once additional command-line flags are added.

Tested with Samsung K9GAG08U0D

Signed-off-by: Brian Norris <norris@broadcom.com>
---
 nanddump.c |  117 ++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 83 insertions(+), 34 deletions(-)

diff --git a/nanddump.c b/nanddump.c
index 735ae48..eccb651 100644
--- a/nanddump.c
+++ b/nanddump.c
@@ -83,6 +83,7 @@ static const char	*mtddev;		// mtd device name
 static const char	*dumpfile;		// dump file name
 static bool		omitbad = false;
 static bool		quiet = false;		// suppress diagnostic output
+static bool		canonical = false;
 
 static void process_options (int argc, char * const argv[])
 {
@@ -171,6 +172,80 @@ static void process_options (int argc, char * const argv[])
 	mtddev = argv[optind];
 }
 
+#define PRETTY_ROW_SIZE 16
+#define PRETTY_BUF_LEN 80
+
+/**
+ * pretty_dump_to_buffer - formats a blob of data to "hex ASCII" in memory
+ * @buf: data blob to dump
+ * @len: number of bytes in the @buf
+ * @linebuf: where to put the converted data
+ * @linebuflen: total size of @linebuf, including space for terminating NULL
+ * @pagedump: true - dumping as page format; false - dumping as OOB format
+ * @ascii: dump ascii formatted data next to hexdump
+ * @prefix: address to print before line in a page dump, ignored if !pagedump
+ *
+ * pretty_dump_to_buffer() works on one "line" of output at a time, i.e.,
+ * PRETTY_ROW_SIZE bytes of input data converted to hex + ASCII output.
+ *
+ * Given a buffer of unsigned char data, pretty_dump_to_buffer() converts the
+ * input data to a hex/ASCII dump at the supplied memory location. A prefix
+ * is included based on whether we are dumping page or OOB data. The converted
+ * output is always NULL-terminated.
+ *
+ * e.g.
+ *   pretty_dump_to_buffer(data, data_len, prettybuf, linelen, true,
+ *                         false, 256);
+ * produces:
+ *   0x00000100: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
+ * NOTE: This function was adapted from linux kernel, "lib/hexdump.c"
+ */
+static void pretty_dump_to_buffer(const unsigned char *buf, size_t len,
+		char *linebuf, size_t linebuflen, bool pagedump, bool ascii,
+		unsigned int prefix)
+{
+	static const char hex_asc[] = "0123456789abcdef";
+	unsigned char ch;
+	int j, lx = 0;
+	int ascii_column;
+
+	if (pagedump)
+		sprintf(linebuf, "0x%.8x: ", prefix);
+	else
+		sprintf(linebuf, "  OOB Data: ");
+	lx += 12;
+
+	if (!len)
+		goto nil;
+	if (len > PRETTY_ROW_SIZE)	/* limit to one line at a time */
+		len = PRETTY_ROW_SIZE;
+
+	for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
+		ch = buf[j];
+		linebuf[lx++] = hex_asc[ch & 0x0f];
+		linebuf[lx++] = hex_asc[(ch & 0xf0) >> 4];
+		linebuf[lx++] = ' ';
+	}
+	if (j)
+		lx--;
+
+	ascii_column = 3 * PRETTY_ROW_SIZE + 14;
+
+	if (!ascii)
+		goto nil;
+
+	while (lx < (linebuflen - 1) && lx < (ascii_column - 1))
+		linebuf[lx++] = ' ';
+	linebuf[lx++] = '|';
+	for (j = 0; (j < len) && (lx + 2) < linebuflen; j++)
+		linebuf[lx++] = (isascii(buf[j]) && isprint(buf[j])) ? buf[j]
+			: '.';
+	linebuf[lx++] = '|';
+nil:
+	linebuf[lx++] = '\n';
+	linebuf[lx++] = '\0';
+}
+
 /*
  * Buffers for reading data from flash
  */
@@ -189,7 +264,7 @@ int main(int argc, char * const argv[])
 	int ret, i, fd, ofd, bs, badblock = 0;
 	struct mtd_oob_buf oob = {0, 16, oobbuf};
 	mtd_info_t meminfo;
-	char pretty_buf[80];
+	char pretty_buf[PRETTY_BUF_LEN];
 	int oobinfochanged = 0 ;
 	struct nand_oobinfo old_oobinfo;
 	struct mtd_ecc_stats stat1, stat2;
@@ -336,20 +411,10 @@ int main(int argc, char * const argv[])
 
 		/* Write out page data */
 		if (pretty_print) {
-			for (i = 0; i < bs; i += 16) {
-				sprintf(pretty_buf,
-						"0x%08x: %02x %02x %02x %02x %02x %02x %02x "
-						"%02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
-						(unsigned int) (ofs + i),  readbuf[i],
-						readbuf[i+1], readbuf[i+2],
-						readbuf[i+3], readbuf[i+4],
-						readbuf[i+5], readbuf[i+6],
-						readbuf[i+7], readbuf[i+8],
-						readbuf[i+9], readbuf[i+10],
-						readbuf[i+11], readbuf[i+12],
-						readbuf[i+13], readbuf[i+14],
-						readbuf[i+15]);
-				write(ofd, pretty_buf, 60);
+			for (i = 0; i < bs; i += PRETTY_ROW_SIZE) {
+				pretty_dump_to_buffer(readbuf+i, PRETTY_ROW_SIZE,
+						pretty_buf, PRETTY_BUF_LEN, true, canonical, ofs+i);
+				write(ofd, pretty_buf, strlen(pretty_buf));
 			}
 		} else
 			write(ofd, readbuf, bs);
@@ -372,26 +437,10 @@ int main(int argc, char * const argv[])
 
 		/* Write out OOB data */
 		if (pretty_print) {
-			if (meminfo.oobsize < 16) {
-				sprintf(pretty_buf, "  OOB Data: %02x %02x %02x %02x %02x %02x "
-						"%02x %02x\n",
-						oobbuf[0], oobbuf[1], oobbuf[2],
-						oobbuf[3], oobbuf[4], oobbuf[5],
-						oobbuf[6], oobbuf[7]);
-				write(ofd, pretty_buf, 48);
-				continue;
-			}
-
 			for (i = 0; i < meminfo.oobsize; i += 16) {
-				sprintf(pretty_buf, "  OOB Data: %02x %02x %02x %02x %02x %02x "
-						"%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
-						oobbuf[i], oobbuf[i+1], oobbuf[i+2],
-						oobbuf[i+3], oobbuf[i+4], oobbuf[i+5],
-						oobbuf[i+6], oobbuf[i+7], oobbuf[i+8],
-						oobbuf[i+9], oobbuf[i+10], oobbuf[i+11],
-						oobbuf[i+12], oobbuf[i+13], oobbuf[i+14],
-						oobbuf[i+15]);
-				write(ofd, pretty_buf, 60);
+				pretty_dump_to_buffer(oobbuf+i, meminfo.oobsize-i,
+						pretty_buf, PRETTY_BUF_LEN, false, canonical, 0);
+				write(ofd, pretty_buf, strlen(pretty_buf));
 			}
 		} else
 			write(ofd, oobbuf, meminfo.oobsize);
-- 
1.7.0.4

  reply	other threads:[~2010-07-19 17:33 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-16 16:42 [PATCH] mtd-utils: formatting of odd-sized OOB in nanddump Brian Norris
2010-07-07 14:07 ` Artem Bityutskiy
2010-07-07 18:21   ` Brian Norris
2010-07-08  4:09     ` Artem Bityutskiy
2010-07-08 20:50       ` [PATCH] mtd-utils update Brian Norris
2010-07-08 20:50         ` [PATCH 1/6] mtd-utils/nanddump.c: Increase max OOB size Brian Norris
2010-07-18  7:17           ` Artem Bityutskiy
2010-07-08 20:50         ` [PATCH 2/6] mtd-utils/nanddump.c: Robust pretty hexdump Brian Norris
2010-07-18  7:18           ` Artem Bityutskiy
2010-07-08 20:50         ` [PATCH 3/6] mtd-utils/nanddump.c: Add canonical (hex+ascii) flag Brian Norris
2010-07-08 20:50         ` [PATCH 4/6] mtd-utils/mkfs.jffs2: fixed warnings Brian Norris
2010-07-18  7:19           ` Artem Bityutskiy
2010-07-08 20:50         ` [PATCH 5/6] mtd-utils/nandtest.c: Fixed indentation Brian Norris
2010-07-18  7:20           ` Artem Bityutskiy
2010-07-08 20:50         ` [PATCH 6/6] mtd-utils/nanddump.c: Add "forcebinary" flag Brian Norris
2010-07-08 22:03           ` Brian Norris
2010-07-18  7:23           ` Artem Bityutskiy
2010-07-19 17:33             ` Brian Norris [this message]
2010-07-21  9:54               ` [PATCH v2 2/6] mtd-utils/nanddump.c: Robust pretty hexdump Artem Bityutskiy
2010-07-19 17:33             ` [PATCH v2 3/6] mtd-utils/nanddump.c: Add canonical (hex+ascii) flag Brian Norris
2010-07-19 17:33             ` [PATCH v2 6/6] mtd-utils/nanddump.c: Add "forcebinary" flag Brian Norris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1279560796-6388-1-git-send-email-norris@broadcom.com \
    --to=norris@broadcom.com \
    --cc=dedekind1@gmail.com \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).