cli: move memory dump printing into a separate module

Make a generic memory printing function available across CLI.

BUG=none
TEST=verify that memory dump commands still work:

  storm: md 40000000 10
  40000000: edfad6d1 f7bdfeff fa9bdfdb d9fffb77    ............w...
  40000010: fecdbeef febe76eb 668db7fe fb7fbfff    .....v.....f....
  40000020: ffbcfb2f f7fcfffb ffffffff fbffd6fa    /...............
  40000030: befcfefa fef3ffbf fffbff3b f8e7bbbd    ........;.......
  storm: md.b 40000000 10
  40000000: d1 d6 fa ed ff fe bd f7 db df 9b fa 77 fb ff d9    ............w...
  storm:

Change-Id: I511e819b98a2e6900269d899b4b1f8e704241c67
Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/202936
Reviewed-by: Stefan Reinauer <reinauer@chromium.org>
Reviewed-by: David Hendricks <dhendrix@chromium.org>
diff --git a/src/debug/cli/Makefile.inc b/src/debug/cli/Makefile.inc
index 42d990b..edd8f71 100644
--- a/src/debug/cli/Makefile.inc
+++ b/src/debug/cli/Makefile.inc
@@ -18,4 +18,5 @@
 depthcharge-y += command.c
 depthcharge-y += console_main.c
 depthcharge-y += memory.c
+depthcharge-y += printbuf.c
 depthcharge-y += storage.c
diff --git a/src/debug/cli/common.h b/src/debug/cli/common.h
index 61efd58..1354b00 100644
--- a/src/debug/cli/common.h
+++ b/src/debug/cli/common.h
@@ -80,4 +80,21 @@
 
 char *getenv(const char *);
 
+/*
+ * Print data buffer in hex and ascii form to the terminal.
+ *
+ * data reads are buffered so that each memory address is only read once.
+ * Useful when displaying the contents of volatile registers.
+ *
+ * parameters:
+ *    addr: Starting address to display at start of line
+ *    data: pointer to data buffer
+ *    width: data value width.  May be 1, 2, or 4.
+ *    count: number of values to display
+ *    linelen: Number of values to print per line; specify 0 for default length
+ */
+
+int print_buffer(unsigned long addr, const void *data, unsigned width,
+		 unsigned count, unsigned linelen);
+
 #endif	/* __DEBUG_CLI_COMMON_H__ */
diff --git a/src/debug/cli/memory.c b/src/debug/cli/memory.c
index 70b4d58..c6dac12 100644
--- a/src/debug/cli/memory.c
+++ b/src/debug/cli/memory.c
@@ -50,86 +50,6 @@
 
 static inline void unmap_sysmem(const void *ptr) {}
 
-
-/*
- * Print data buffer in hex and ascii form to the terminal.
- *
- * data reads are buffered so that each memory address is only read once.
- * Useful when displaying the contents of volatile registers.
- *
- * parameters:
- *    addr: Starting address to display at start of line
- *    data: pointer to data buffer
- *    width: data value width.  May be 1, 2, or 4.
- *    count: number of values to display
- *    linelen: Number of values to print per line; specify 0 for default length
- */
-#define MAX_LINE_LENGTH_BYTES (64)
-#define DEFAULT_LINE_LENGTH_BYTES (16)
-
-static int print_buffer(ulong addr, const void *data, unsigned width,
-			unsigned count, unsigned linelen)
-{
-	/* linebuf as a union causes proper alignment */
-	union linebuf {
-		u32 ui[MAX_LINE_LENGTH_BYTES/sizeof(u32) + 1];
-		u16 us[MAX_LINE_LENGTH_BYTES/sizeof(u16) + 1];
-		u8  uc[MAX_LINE_LENGTH_BYTES/sizeof(u8) + 1];
-	} lb;
-	int i;
-
-	if (linelen*width > MAX_LINE_LENGTH_BYTES)
-		linelen = MAX_LINE_LENGTH_BYTES / width;
-	if (linelen < 1)
-		linelen = DEFAULT_LINE_LENGTH_BYTES / width;
-
-	while (count) {
-		unsigned thislinelen = linelen;
-		printf("%08lx:", addr);
-
-		/* check for overflow condition */
-		if (count < thislinelen)
-			thislinelen = count;
-
-		/* Copy from memory into linebuf and print hex values */
-		for (i = 0; i < thislinelen; i++) {
-			u32 x;
-			if (width == 4)
-				x = lb.ui[i] = *(volatile u32 *)data;
-			else if (width == 2)
-				x = lb.us[i] = *(volatile u16 *)data;
-			else
-				x = lb.uc[i] = *(volatile u8 *)data;
-			printf(" %0*x", width * 2, x);
-			data += width;
-		}
-
-		while (thislinelen < linelen) {
-			/* fill line with whitespace for nice ASCII print */
-			for (i=0; i<width*2+1; i++)
-				puts(" ");
-			linelen--;
-		}
-
-		/* Print data in ASCII characters */
-		for (i = 0; i < thislinelen * width; i++) {
-			if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
-				lb.uc[i] = '.';
-		}
-		lb.uc[i] = '\0';
-		printf("    %s\n", lb.uc);
-
-		/* update references */
-		addr += thislinelen * width;
-		count -= thislinelen;
-
-		if (ctrlc())
-			return -1;
-	}
-
-	return 0;
-}
-
 /* Memory Display
  *
  * Syntax:
diff --git a/src/debug/cli/printbuf.c b/src/debug/cli/printbuf.c
new file mode 100644
index 0000000..018d39e
--- /dev/null
+++ b/src/debug/cli/printbuf.c
@@ -0,0 +1,74 @@
+/*
+ * (C) Copyright 2000
+ * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+ *
+ * Copyright 2014 Google Inc.
+ */
+
+#include "common.h"
+
+#define MAX_LINE_LENGTH_BYTES (64)
+#define DEFAULT_LINE_LENGTH_BYTES (16)
+
+int print_buffer(unsigned long addr, const void *data, unsigned width,
+		 unsigned count, unsigned linelen)
+{
+	/* linebuf as a union causes proper alignment */
+	union linebuf {
+		u32 ui[MAX_LINE_LENGTH_BYTES/sizeof(u32) + 1];
+		u16 us[MAX_LINE_LENGTH_BYTES/sizeof(u16) + 1];
+		u8  uc[MAX_LINE_LENGTH_BYTES/sizeof(u8) + 1];
+	} lb;
+	int i;
+
+	if (linelen*width > MAX_LINE_LENGTH_BYTES)
+		linelen = MAX_LINE_LENGTH_BYTES / width;
+	if (linelen < 1)
+		linelen = DEFAULT_LINE_LENGTH_BYTES / width;
+
+	while (count) {
+		unsigned thislinelen = linelen;
+		printf("%08lx:", addr);
+
+		/* check for overflow condition */
+		if (count < thislinelen)
+			thislinelen = count;
+
+		/* Copy from memory into linebuf and print hex values */
+		for (i = 0; i < thislinelen; i++) {
+			u32 x;
+			if (width == 4)
+				x = lb.ui[i] = *(volatile u32 *)data;
+			else if (width == 2)
+				x = lb.us[i] = *(volatile u16 *)data;
+			else
+				x = lb.uc[i] = *(volatile u8 *)data;
+			printf(" %0*x", width * 2, x);
+			data += width;
+		}
+
+		while (thislinelen < linelen) {
+			/* fill line with whitespace for nice ASCII print */
+			for (i=0; i<width*2+1; i++)
+				puts(" ");
+			linelen--;
+		}
+
+		/* Print data in ASCII characters */
+		for (i = 0; i < thislinelen * width; i++) {
+			if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
+				lb.uc[i] = '.';
+		}
+		lb.uc[i] = '\0';
+		printf("    %s\n", lb.uc);
+
+		/* update references */
+		addr += thislinelen * width;
+		count -= thislinelen;
+
+		if (ctrlc())
+			return -1;
+	}
+
+	return 0;
+}