Debian Codegraph @

src/boot/efi-log.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "efi-log.h"
4 #include "efi-string-table.h"
5 #include "proto/rng.h"
6 #include "smbios.h"
7 #include "util.h"
8 #include "vmm.h"
9
10 static unsigned log_count = 0;
11 static LogLevel log_max_level = LOG_INFO;
12
13 static const uint8_t log_level_color[_LOG_MAX] = {
14 [LOG_EMERG] = EFI_LIGHTRED,
15 [LOG_ALERT] = EFI_LIGHTRED,
16 [LOG_CRIT] = EFI_LIGHTRED,
17 [LOG_ERR] = EFI_LIGHTRED,
18 [LOG_WARNING] = EFI_YELLOW,
19 [LOG_NOTICE] = EFI_WHITE,
20 [LOG_INFO] = EFI_WHITE,
21 [LOG_DEBUG] = EFI_LIGHTGRAY,
22 };
23
24 static const char *const log_level_table[_LOG_MAX] = {
25 [LOG_EMERG] = "emerg",
26 [LOG_ALERT] = "alert",
27 [LOG_CRIT] = "crit",
28 [LOG_ERR] = "err",
29 [LOG_WARNING] = "warning",
30 [LOG_NOTICE] = "notice",
31 [LOG_INFO] = "info",
32 [LOG_DEBUG] = "debug",
33 };
34
35 DEFINE_STRING_TABLE_LOOKUP(log_level, LogLevel);
36
37 LogLevel log_get_max_level(void) {
38 return log_max_level;
39 }
40
41 int log_set_max_level(LogLevel level) {
42 assert(level >= 0 && level < _LOG_MAX);
43
44 int old = log_max_level;
45 log_max_level = level;
46 return old;
47 }
48
49 int log_set_max_level_from_string(const char *e) {
50 int r;
51
52 assert(e);
53
54 r = log_level_from_string(e);
55 if (r < 0)
56 return r;
57
58 log_set_max_level(r);
59 return 0;
60 }
61
62 void log_set_max_level_from_smbios(void) {
63 int r;
64
65 if (is_confidential_vm())
66 return; /* Don't consume SMBIOS in Confidential Computing contexts */
67
68 const char *level_str = smbios_find_oem_string("io.systemd.boot.loglevel=", /* after= */ NULL);
69 if (!level_str)
70 return;
71
72 r = log_set_max_level_from_string(level_str);
73 if (r < 0)
74 log_warning("Failed to parse log level '%s', ignoring.", level_str);
75 }
76
77 void freeze(void) {
78 for (;;)
79 BS->Stall(60 * 1000 * 1000);
80 }
81
82 _noreturn_ static void panic(const char16_t *message) {
83 if (ST->ConOut->Mode->CursorColumn > 0)
84 ST->ConOut->OutputString(ST->ConOut, (char16_t *) u"\r\n");
85 ST->ConOut->SetAttribute(ST->ConOut, EFI_TEXT_ATTR(EFI_LIGHTRED, EFI_BLACK));
86 ST->ConOut->OutputString(ST->ConOut, (char16_t *) message);
87 freeze();
88 }
89
90 void efi_assert(const char *expr, const char *file, unsigned line, const char *function) {
91 static bool asserting = false;
92
93 /* Let's be paranoid. */
94 if (asserting)
95 panic(u"systemd-boot: Nested assertion failure, halting.");
96
97 asserting = true;
98 log_error("systemd-boot: Assertion '%s' failed at %s:%u@%s, halting.", expr, file, line, function);
99 freeze();
100 }
101
102 EFI_STATUS log_internal(EFI_STATUS status, LogLevel log_level, const char *format, ...) {
103 assert(format);
104 assert(log_level >= 0 && log_level < _LOG_MAX);
105
106 if (log_level > log_max_level)
107 return status;
108
109 int32_t attr = ST->ConOut->Mode->Attribute;
110
111 if (ST->ConOut->Mode->CursorColumn > 0)
112 ST->ConOut->OutputString(ST->ConOut, (char16_t *) u"\r\n");
113 ST->ConOut->SetAttribute(ST->ConOut, EFI_TEXT_ATTR(log_level_color[log_level], EFI_BLACK));
114
115 va_list ap;
116 va_start(ap, format);
117 vprintf_status(status, format, ap);
118 va_end(ap);
119
120 ST->ConOut->OutputString(ST->ConOut, (char16_t *) u"\r\n");
121 ST->ConOut->SetAttribute(ST->ConOut, attr);
122
123 log_count++;
124 return status;
125 }
126
127 void log_wait(void) {
128 if (log_count == 0)
129 return;
130
131 BS->Stall(MIN(4u, log_count) * 2500 * 1000);
132 log_count = 0;
133 }
134
135 // NOLINTNEXTLINE(misc-use-internal-linkage)
136 _used_ uintptr_t __stack_chk_guard = (uintptr_t) 0x70f6967de78acae3;
137
138 /* We can only set a random stack canary if this function attribute is available,
139 * otherwise this may create a stack check fail. */
140 #if STACK_PROTECTOR_RANDOM
141 void __stack_chk_guard_init(void) {
142 EFI_RNG_PROTOCOL *rng;
143 if (BS->LocateProtocol(MAKE_GUID_PTR(EFI_RNG_PROTOCOL), NULL, (void **) &rng) == EFI_SUCCESS)
144 (void) rng->GetRNG(rng, NULL, sizeof(__stack_chk_guard), (void *) &__stack_chk_guard);
145 else
146 /* Better than no extra entropy. */
147 __stack_chk_guard ^= (uintptr_t) __executable_start;
148 }
149 #endif
150
151 // NOLINTBEGIN(misc-use-internal-linkage)
152 _used_ _noreturn_ void __stack_chk_fail(void);
153 _used_ _noreturn_ void __stack_chk_fail_local(void);
154 // NOLINTEND(misc-use-internal-linkage)
155 void __stack_chk_fail(void) {
156 panic(u"systemd-boot: Stack check failed, halting.");
157 }
158 void __stack_chk_fail_local(void) {
159 __stack_chk_fail();
160 }
161
162 /* Called by libgcc for some fatal errors like integer overflow with -ftrapv. */
163 // NOLINTNEXTLINE(misc-use-internal-linkage)
164 _used_ _noreturn_ void abort(void);
165 void abort(void) {
166 panic(u"systemd-boot: Unknown error, halting.");
167 }
168
169 #if defined(__ARM_EABI__)
170 /* These override the (weak) div0 handlers from libgcc as they would otherwise call raise() instead. */
171 // NOLINTBEGIN(misc-use-internal-linkage)
172 _used_ _noreturn_ int __aeabi_idiv0(int return_value);
173 _used_ _noreturn_ long long __aeabi_ldiv0(long long return_value);
174 // NOLINTEND(misc-use-internal-linkage)
175
176 int __aeabi_idiv0(int return_value) {
177 panic(u"systemd-boot: Division by zero, halting.");
178 }
179
180 long long __aeabi_ldiv0(long long return_value) {
181 panic(u"systemd-boot: Division by zero, halting.");
182 }
183 #endif