From: Jan Janssen Date: Sun, 18 Jun 2023 08:44:39 +0000 (+0200) Subject: boot: Split log_hexdump() X-Git-Tag: v254-rc1~181^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e53e5c0ac16adf2e29d31fda73656391efc38600;p=thirdparty%2Fsystemd.git boot: Split log_hexdump() --- diff --git a/src/boot/efi/efi-string.c b/src/boot/efi/efi-string.c index d199410881b..6c6d644c51a 100644 --- a/src/boot/efi/efi-string.c +++ b/src/boot/efi/efi-string.c @@ -391,6 +391,23 @@ bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack) { DEFINE_PARSE_NUMBER(char, parse_number8); DEFINE_PARSE_NUMBER(char16_t, parse_number16); +char16_t *hexdump(const void *data, size_t size) { + static const char hex[16] = "0123456789abcdef"; + const uint8_t *d = data; + + assert(data || size == 0); + + char16_t *buf = xnew(char16_t, size * 2 + 1); + + for (size_t i = 0; i < size; i++) { + buf[i * 2] = hex[d[i] >> 4]; + buf[i * 2 + 1] = hex[d[i] & 0x0F]; + } + + buf[size * 2] = 0; + return buf; +} + static const char * const warn_table[] = { [EFI_SUCCESS] = "Success", [EFI_WARN_UNKNOWN_GLYPH] = "Unknown glyph", diff --git a/src/boot/efi/efi-string.h b/src/boot/efi/efi-string.h index b97e16990a4..9b82bfeca05 100644 --- a/src/boot/efi/efi-string.h +++ b/src/boot/efi/efi-string.h @@ -108,6 +108,8 @@ bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack); bool parse_number8(const char *s, uint64_t *ret_u, const char **ret_tail); bool parse_number16(const char16_t *s, uint64_t *ret_u, const char16_t **ret_tail); +char16_t *hexdump(const void *data, size_t size); + #ifdef __clang__ # define _gnu_printf_(a, b) _printf_(a, b) #else diff --git a/src/boot/efi/log.c b/src/boot/efi/log.c index 6d0edec2cfa..93631aca942 100644 --- a/src/boot/efi/log.c +++ b/src/boot/efi/log.c @@ -3,6 +3,7 @@ #include "log.h" #include "proto/rng.h" #include "proto/simple-text-io.h" +#include "util.h" static unsigned log_count = 0; @@ -52,6 +53,15 @@ EFI_STATUS log_internal(EFI_STATUS status, const char *format, ...) { return status; } +#ifdef EFI_DEBUG +void log_hexdump(const char16_t *prefix, const void *data, size_t size) { + /* Debugging helper — please keep this around, even if not used */ + + _cleanup_free_ char16_t *hex = hexdump(data, size); + log_internal(EFI_SUCCESS, "%ls[%zu]: %ls", prefix, size, hex); +} +#endif + void log_wait(void) { if (log_count == 0) return; diff --git a/src/boot/efi/log.h b/src/boot/efi/log.h index 973e13c260a..13f3887b8a8 100644 --- a/src/boot/efi/log.h +++ b/src/boot/efi/log.h @@ -26,3 +26,7 @@ _gnu_printf_(2, 3) EFI_STATUS log_internal(EFI_STATUS status, const char *format #define log_error(...) log_internal(EFI_INVALID_PARAMETER, __VA_ARGS__) #define log_oom() log_internal(EFI_OUT_OF_RESOURCES, "Out of memory.") #define log_trace() log_internal(EFI_SUCCESS, "%s:%i@%s", __FILE__, __LINE__, __func__) + +#ifdef EFI_DEBUG +void log_hexdump(const char16_t *prefix, const void *data, size_t size); +#endif diff --git a/src/boot/efi/test-efi-string.c b/src/boot/efi/test-efi-string.c index d214b1536e3..8f777c1ce3c 100644 --- a/src/boot/efi/test-efi-string.c +++ b/src/boot/efi/test-efi-string.c @@ -484,6 +484,26 @@ TEST(parse_number16) { assert_se(streq16(tail, u"rest")); } +TEST(test_hexdump) { + char16_t *hex; + + hex = hexdump(NULL, 0); + assert(streq16(hex, u"")); + free(hex); + + hex = hexdump("1", 2); + assert(streq16(hex, u"3100")); + free(hex); + + hex = hexdump("abc", 4); + assert(streq16(hex, u"61626300")); + free(hex); + + hex = hexdump((uint8_t[]){ 0x0, 0x42, 0xFF, 0xF1, 0x1F }, 5); + assert(streq16(hex, u"0042fff11f")); + free(hex); +} + _printf_(1, 2) static void test_printf_one(const char *format, ...) { va_list ap, ap_efi; va_start(ap, format); diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index 42526d70053..d2b8f882500 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -568,30 +568,6 @@ __attribute__((noinline)) void notify_debugger(const char *identity, volatile bo #endif } -#ifdef EFI_DEBUG -void hexdump(const char16_t *prefix, const void *data, size_t size) { - static const char hex[16] = "0123456789abcdef"; - _cleanup_free_ char16_t *buf = NULL; - const uint8_t *d = data; - - assert(prefix); - assert(data || size == 0); - - /* Debugging helper — please keep this around, even if not used */ - - buf = xnew(char16_t, size*2+1); - - for (size_t i = 0; i < size; i++) { - buf[i*2] = hex[d[i] >> 4]; - buf[i*2+1] = hex[d[i] & 0x0F]; - } - - buf[size*2] = 0; - - log_error("%ls[%zu]: %ls", prefix, size, buf); -} -#endif - #if defined(__i386__) || defined(__x86_64__) static inline uint8_t inb(uint16_t port) { uint8_t value; diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index aadbbdad0d1..03cd928b2f5 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -167,10 +167,6 @@ uint64_t get_os_indications_supported(void); * be attached. See debug-sd-boot.sh for how this can be done. */ void notify_debugger(const char *identity, bool wait); -#ifdef EFI_DEBUG -void hexdump(const char16_t *prefix, const void *data, size_t size); -#endif - /* On x86 the compiler assumes a different incoming stack alignment than what we get. * This will cause long long variables to be misaligned when building with * '-mlong-double' (for correct struct layouts). Normally, the compiler realigns the