]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Split log_hexdump()
authorJan Janssen <medhefgo@web.de>
Sun, 18 Jun 2023 08:44:39 +0000 (10:44 +0200)
committerJan Janssen <medhefgo@web.de>
Sun, 18 Jun 2023 09:13:03 +0000 (11:13 +0200)
src/boot/efi/efi-string.c
src/boot/efi/efi-string.h
src/boot/efi/log.c
src/boot/efi/log.h
src/boot/efi/test-efi-string.c
src/boot/efi/util.c
src/boot/efi/util.h

index d199410881b0c29060a6384cc29173da238337ed..6c6d644c51a09b26c6438c6d6ffb1305ac4ceaf2 100644 (file)
@@ -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",
index b97e16990a4e2114c5db68a1a8d0e24aa1cb46a6..9b82bfeca05e0bde09eccd9be6f455f0b1a306f3 100644 (file)
@@ -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
index 6d0edec2cfac2fb10ee4192bf72a2ea31ffe2a72..93631aca942ca6eb61857d8b8f068800c5f65cf9 100644 (file)
@@ -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;
index 973e13c260ad704d0c41af041a753a067f3a38b8..13f3887b8a8452b290a34fd577d77156b201773d 100644 (file)
@@ -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
index d214b1536e3160863b4cd7a2bd6f2f8b22c3aefe..8f777c1ce3c3031e2476ccabc0d58cb2fcdcbd89 100644 (file)
@@ -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);
index 42526d7005359c6a1e4e703724a5a3c79ec3d507..d2b8f882500f7d1a907ec21fe2c550fa7fd658df 100644 (file)
@@ -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;
index aadbbdad0d15d5de2555942fb040625dccbd996c..03cd928b2f5faa6ca81123c1fc832ca9d81814ae 100644 (file)
@@ -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