summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2019-02-06 16:19:42 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-02-15 01:55:24 -0800
commit751fb79680b3477ad0f6c7530cdc673b015d0673 (patch)
treee70a7ad326f2b58e7358a0316fc5178b33770a98
parent0aa79f950b0532da5952e9e70e6a1f5f62069684 (diff)
downloadchrome-ec-751fb79680b3477ad0f6c7530cdc673b015d0673.tar.gz
Util: Add hexdump API
This patch adds the hexdump API, which prints binary data in hex and ASCII. Sample output of hexdump(image_data.version, 30): 6e 61 6d 69 5f 76 32 2e 30 2e 37 37 34 2d 63 66 |nami_v2.0.774-cf| 34 62 64 33 34 38 30 00 00 00 00 00 00 00 |4bd3480....... | Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> BUG=none BRANCH=none TEST=Verify image_data.version is printed as expected on Nami Change-Id: I8a10a9ac4d329657bf65b64117448cb9b3e75a52 Reviewed-on: https://chromium-review.googlesource.com/1457577 Commit-Ready: Daisuke Nojiri <dnojiri@chromium.org> Tested-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Jett Rink <jettrink@chromium.org>
-rw-r--r--common/util.c26
-rw-r--r--include/util.h13
2 files changed, 39 insertions, 0 deletions
diff --git a/common/util.c b/common/util.c
index 054e8e839c..e93eeebb16 100644
--- a/common/util.c
+++ b/common/util.c
@@ -5,6 +5,7 @@
/* Utility functions for Chrome EC */
+#include "console.h"
#include "util.h"
size_t strlen(const char *s)
@@ -598,3 +599,28 @@ int parse_offset_size(int argc, char **argv, int shift,
return EC_SUCCESS;
}
+
+void hexdump(const uint8_t *data, int len)
+{
+ int i, j;
+
+ if (!data || !len)
+ return;
+
+ for (i = 0; i < len; i += 16) {
+ /* Left column (Hex) */
+ for (j = i; j < i + 16; j++) {
+ if (j < len)
+ ccprintf(" %02x", data[j]);
+ else
+ ccprintf(" ");
+ }
+ /* Right column (ASCII) */
+ ccprintf(" |");
+ for (j = i; j < i + 16; j++) {
+ int c = j < len ? data[j] : ' ';
+ ccprintf("%c", isprint(c) ? c : '.');
+ }
+ ccprintf("|\n");
+ }
+}
diff --git a/include/util.h b/include/util.h
index aa15ba4fa8..edef7d9b38 100644
--- a/include/util.h
+++ b/include/util.h
@@ -210,6 +210,19 @@ static inline int cond_went_true(cond_t *c) { return cond_went(c, 1); }
int parse_offset_size(int argc, char **argv, int shift,
int *offset, int *size);
+/**
+ * Print binary in hex and ASCII
+ *
+ * Sample output of hexdump(image_data.version, 30):
+ *
+ * 6e 61 6d 69 5f 76 32 2e 30 2e 37 37 34 2d 63 66 |nami_v2.0.774-cf|
+ * 34 62 64 33 34 38 30 00 00 00 00 00 00 00 |4bd3480....... |
+ *
+ * @param data Data to be dumped
+ * @param len Size of data
+ */
+void hexdump(const uint8_t *data, int len);
+
#ifdef CONFIG_ASSEMBLY_MULA32
/*
* Compute (a*b)+c[+d], where a, b, c[, d] are 32-bit integers, and the result