mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph)
Date: Thu,  9 Dec 2021 11:57:06 +0100	[thread overview]
Message-ID: <20211209105708.3517684-1-a.fatoum@pengutronix.de> (raw)

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>
---
 lib/Kconfig    |  3 +++
 lib/vsprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/lib/Kconfig b/lib/Kconfig
index 718033e56e64..27e7bea6852f 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 85147e8d2e25..ce92787c58ef 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -320,6 +320,52 @@ char *uuid_string(char *buf, const char *end, const u8 *addr, int field_width,
 	return string(buf, end, uuid, field_width, precision, flags);
 }
 
+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);
-- 
2.30.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


             reply	other threads:[~2021-12-09 10:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-09 10:57 Ahmad Fatoum [this message]
2021-12-09 10:57 ` [PATCH 2/3] common: add new PRINTF_FULL option Ahmad Fatoum
2021-12-09 10:57 ` [PATCH 3/3] of: implement new of_property_sprintf Ahmad Fatoum
2021-12-15 20:16   ` [PATCH] fixup! " Ahmad Fatoum
2021-12-13 22:28 ` [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Sascha Hauer
2021-12-14  6:57   ` Ahmad Fatoum
2021-12-14  7:27     ` Sascha Hauer
2022-01-03 10:40       ` Ahmad Fatoum

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=20211209105708.3517684-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@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