]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-boot: Add assert implementation
authorJan Janssen <medhefgo@web.de>
Wed, 11 Aug 2021 12:59:46 +0000 (14:59 +0200)
committerJan Janssen <medhefgo@web.de>
Thu, 12 Aug 2021 07:48:37 +0000 (09:48 +0200)
There is a ASSERT() macro from gnu-efi, but that does not show any
output to ConOut. Having to do some additional setup just to get
some debug output is tedious and outright difficult on real hardware.

src/boot/efi/assert.c [new file with mode: 0644]
src/boot/efi/meson.build
src/fundamental/macro-fundamental.h

diff --git a/src/boot/efi/assert.c b/src/boot/efi/assert.c
new file mode 100644 (file)
index 0000000..350b2b0
--- /dev/null
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#ifndef NDEBUG
+
+#include <efi.h>
+#include <efilib.h>
+#include "util.h"
+
+void efi_assert(const char *expr, const char *file, unsigned line, const char *function) {
+      log_error_stall(L"systemd-boot assertion '%a' failed at %a:%u, function %a(). Halting.", expr, file, line, function);
+      for (;;)
+            uefi_call_wrapper(BS->Stall, 1, 60 * 1000 * 1000);
+}
+
+#endif
index 0a96a72d6ecce87ce9d334f36e20e29670d5d9dd..c96a16426a477a6a6b097690d65c031cbe03780b 100644 (file)
@@ -17,6 +17,7 @@ efi_headers = files('''
 '''.split())
 
 common_sources = '''
+        assert.c
         disk.c
         graphics.c
         measure.c
@@ -219,12 +220,16 @@ if have_gnu_efi
                 compile_args += ['-Werror']
         endif
         if get_option('buildtype') == 'debug'
-                compile_args += ['-ggdb', '-O0']
+                compile_args += ['-ggdb', '-O0', '-DEFI_DEBUG']
         elif get_option('buildtype') == 'debugoptimized'
-                compile_args += ['-ggdb', '-Og']
+                compile_args += ['-ggdb', '-Og', '-DEFI_DEBUG']
         else
                 compile_args += ['-O2']
         endif
+        if get_option('b_ndebug') == 'true' or (
+           get_option('b_ndebug') == 'if-release' and ['plain', 'release'].contains(get_option('buildtype')))
+                compile_args += ['-DNDEBUG']
+        endif
 
         efi_ldflags = ['-T', efi_lds,
                        '-shared',
index 91b24a182642ec774aadaae7ce24c47562f5bd34..461d37fa9e19a77f02740e5e503cb0e47fcd3f2a 100644 (file)
 #define CONCATENATE(x, y) XCONCATENATE(x, y)
 
 #ifdef SD_BOOT
-#define assert(expr) do {} while (false)
+        #ifdef NDEBUG
+                #define assert(expr)
+                #define assert_not_reached() __builtin_unreachable()
+        #else
+                void efi_assert(const char *expr, const char *file, unsigned line, const char *function) _noreturn_;
+                #define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
+                #define assert_not_reached() efi_assert("Code should not be reached", __FILE__, __LINE__, __PRETTY_FUNCTION__)
+        #endif
 #endif
 
 #if defined(static_assert)