
Extend the %p format modifier with 'x' and 'X' mode. This allows to to conveniently print ranges of memory without an extra loop or buffer in the calling code e.g. log_info("Checksum %*pX\n", (int)sizeof(buf), buf); Signed-off-by: Ludwig Nussel <ludwig.nussel@siemens.com> --- doc/develop/printf.rst | 4 ++++ lib/vsprintf.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/develop/printf.rst b/doc/develop/printf.rst index 99d05061b14..5583baabd48 100644 --- a/doc/develop/printf.rst +++ b/doc/develop/printf.rst @@ -197,3 +197,7 @@ Pointers prints text description of a GUID or if such is not known little endian, lower case, e.g. 'system' for a GUID identifying an EFI system partition. + +%pX, %px + prints hex dump of 'field width' bytes in upper resp lower case + e.g. log_info("%*pX", (int)sizeof(buf), buf); diff --git a/lib/vsprintf.c b/lib/vsprintf.c index c7340a047b2..b1b0c52f4f0 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -276,6 +276,20 @@ static char *string(char *buf, char *end, const char *s, int field_width, return buf; } +static char *hexstring(char *buf, char *end, const uint8_t *s, int field_width, + int precision, int flags) +{ + if (!s) { + s = ""; + field_width = 1; + } + + for (int i = 0; i < field_width; ++i, ++s) + buf = number(buf, end, *s, 16, 2, -1, flags | ZEROPAD); + + return buf; +} + /* U-Boot uses UTF-16 strings in the EFI context only. */ static __maybe_unused char *string16(char *buf, char *end, u16 *s, int field_width, int precision, int flags) @@ -508,6 +522,10 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr, return uuid_string(buf, end, ptr, field_width, precision, flags, fmt); #endif + case 'x': + case 'X': + return hexstring(buf, end, (uint8_t *)ptr, field_width, precision, + (*fmt == 'x' ? SMALL : 0)); default: break; } -- 2.34.1