summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-12-09 11:57:06 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2021-12-14 08:26:54 +0100
commitb61794d5fd1b06e91b1dcf7075fca46d2183c856 (patch)
tree036c4c188fe08d75f0da504f1b45feb9854807d9
parent5b19f4e5e19d25080272692e9fb45b301017c6e6 (diff)
downloadbarebox-b61794d5fd1b06e91b1dcf7075fca46d2183c856.tar.gz
barebox-b61794d5fd1b06e91b1dcf7075fca46d2183c856.tar.xz
vsprintf: add support for printing raw buffers as hex (%*ph)
Import from Linux support for printing buffers as a hex string with a certain separator. For larger buffers consider using print_hex_dump(). Examples: %*ph 00 01 02 ... 3f %*phC 00:01:02: ... :3f %*phD 00-01-02- ... -3f %*phN 000102 ... 3f Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20211209105708.3517684-1-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--lib/Kconfig3
-rw-r--r--lib/vsprintf.c50
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index 718033e56e..27e7bea685 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -166,6 +166,9 @@ config PRINTF_UUID
config PRINTF_WCHAR
bool
+config PRINTF_HEXSTR
+ bool
+
config GENERIC_LIB_ASHLDI3
bool
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 8681eb43b5..9a94a1bbe8 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -321,6 +321,52 @@ char *uuid_string(char *buf, const char *end, const u8 *addr, int field_width,
}
static noinline_for_stack
+char *hex_string(char *buf, const char *end, const u8 *addr, int field_width,
+ int precision, int flags, const char *fmt)
+{
+ char separator;
+ int i, len;
+
+ if (field_width == 0)
+ /* nothing to print */
+ return buf;
+
+ switch (fmt[1]) {
+ case 'C':
+ separator = ':';
+ break;
+ case 'D':
+ separator = '-';
+ break;
+ case 'N':
+ separator = 0;
+ break;
+ default:
+ separator = ' ';
+ break;
+ }
+
+ len = field_width > 0 ? field_width : 1;
+
+ for (i = 0; i < len; ++i) {
+ if (buf < end)
+ *buf = hex_asc_hi(addr[i]);
+ ++buf;
+ if (buf < end)
+ *buf = hex_asc_lo(addr[i]);
+ ++buf;
+
+ if (separator && i != len - 1) {
+ if (buf < end)
+ *buf = separator;
+ ++buf;
+ }
+ }
+
+ return buf;
+}
+
+static noinline_for_stack
char *address_val(char *buf, const char *end, const void *addr,
int field_width, int precision, int flags, const char *fmt)
{
@@ -406,6 +452,10 @@ static char *pointer(const char *fmt, char *buf, const char *end, const void *pt
break;
case 'e':
return error_string(buf, end, ptr, field_width, precision, flags, fmt);
+ case 'h':
+ if (IS_ENABLED(CONFIG_PRINTF_HEXSTR))
+ return hex_string(buf, end, ptr, field_width, precision, flags, fmt);
+ break;
}
return raw_pointer(buf, end, ptr, field_width, precision, flags);