]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/util.h
Merge pull request #25602 from fbuihuu/fix-TEST-73-LOCALE
[thirdparty/systemd.git] / src / boot / efi / util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <efi.h>
5 #include <efilib.h>
6 #include <stddef.h>
7
8 #include "string-util-fundamental.h"
9
10 #define UINTN_MAX (~(UINTN)0)
11 #define INTN_MAX ((INTN)(UINTN_MAX>>1))
12
13 #ifndef __has_attribute
14 #define __has_attribute(x) 0
15 #endif
16 #if __has_attribute(__error__)
17 __attribute__((noreturn)) extern void __assert_cl_failure__(void) __attribute__((__error__("compile-time assertion failed")));
18 #else
19 __attribute__((noreturn)) extern void __assert_cl_failure__(void);
20 #endif
21 /* assert_cl generates a later-stage compile-time assertion when constant folding occurs. */
22 #define assert_cl(condition) ({ if (!(condition)) __assert_cl_failure__(); })
23
24 /* gnu-efi format specifiers for integers are fixed to either 64bit with 'l' and 32bit without a size prefix.
25 * We rely on %u/%d/%x to format regular ints, so ensure the size is what we expect. At the same time, we also
26 * need specifiers for (U)INTN which are native (pointer) sized. */
27 assert_cc(sizeof(int) == sizeof(uint32_t));
28 #if __SIZEOF_POINTER__ == 4
29 # define PRIuN L"u"
30 # define PRIiN L"d"
31 #elif __SIZEOF_POINTER__ == 8
32 # define PRIuN L"lu"
33 # define PRIiN L"ld"
34 #else
35 # error "Unexpected pointer size"
36 #endif
37
38 static inline void free(void *p) {
39 if (!p)
40 return;
41
42 /* Debugging an invalid free requires trace logging to find the call site or a debugger attached. For
43 * release builds it is not worth the bother to even warn when we cannot even print a call stack. */
44 #ifdef EFI_DEBUG
45 assert_se(BS->FreePool(p) == EFI_SUCCESS);
46 #else
47 (void) BS->FreePool(p);
48 #endif
49 }
50
51 static inline void freep(void *p) {
52 free(*(void **) p);
53 }
54
55 #define _cleanup_free_ _cleanup_(freep)
56
57 static __always_inline void erase_obj(void *p) {
58 #ifdef __OPTIMIZE__
59 size_t l;
60 assert_cl(p);
61 l = __builtin_object_size(p, 0);
62 assert_cl(l != (size_t) -1);
63 explicit_bzero_safe(p, l);
64 #else
65 #warning "Object will not be erased with -O0; do not release to production."
66 #endif
67 }
68
69 #define _cleanup_erase_ _cleanup_(erase_obj)
70
71 _malloc_ _alloc_(1) _returns_nonnull_ _warn_unused_result_
72 static inline void *xmalloc(size_t size) {
73 void *p;
74 assert_se(BS->AllocatePool(EfiBootServicesData, size, &p) == EFI_SUCCESS);
75 return p;
76 }
77
78 _malloc_ _alloc_(1, 2) _returns_nonnull_ _warn_unused_result_
79 static inline void *xmalloc_multiply(size_t size, size_t n) {
80 assert_se(!__builtin_mul_overflow(size, n, &size));
81 return xmalloc(size);
82 }
83
84 /* Use malloc attribute as this never returns p like userspace realloc. */
85 _malloc_ _alloc_(3) _returns_nonnull_ _warn_unused_result_
86 static inline void *xrealloc(void *p, size_t old_size, size_t new_size) {
87 void *r = xmalloc(new_size);
88 new_size = MIN(old_size, new_size);
89 if (new_size > 0)
90 memcpy(r, p, new_size);
91 free(p);
92 return r;
93 }
94
95 #define xpool_print(fmt, ...) ((char16_t *) ASSERT_SE_PTR(PoolPrint((fmt), ##__VA_ARGS__)))
96 #define xnew(type, n) ((type *) xmalloc_multiply(sizeof(type), (n)))
97
98 typedef struct {
99 EFI_PHYSICAL_ADDRESS addr;
100 size_t n_pages;
101 } Pages;
102
103 static inline void cleanup_pages(Pages *p) {
104 if (p->n_pages == 0)
105 return;
106 #ifdef EFI_DEBUG
107 assert_se(BS->FreePages(p->addr, p->n_pages) == EFI_SUCCESS);
108 #else
109 (void) BS->FreePages(p->addr, p->n_pages);
110 #endif
111 }
112
113 #define _cleanup_pages_ _cleanup_(cleanup_pages)
114
115 static inline Pages xmalloc_pages(
116 EFI_ALLOCATE_TYPE type, EFI_MEMORY_TYPE memory_type, size_t n_pages, EFI_PHYSICAL_ADDRESS addr) {
117 assert_se(BS->AllocatePages(type, memory_type, n_pages, &addr) == EFI_SUCCESS);
118 return (Pages) {
119 .addr = addr,
120 .n_pages = n_pages,
121 };
122 }
123
124 EFI_STATUS parse_boolean(const char *v, bool *b);
125
126 EFI_STATUS efivar_set(const EFI_GUID *vendor, const char16_t *name, const char16_t *value, uint32_t flags);
127 EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, const char16_t *name, const void *buf, UINTN size, uint32_t flags);
128 EFI_STATUS efivar_set_uint_string(const EFI_GUID *vendor, const char16_t *name, UINTN i, uint32_t flags);
129 EFI_STATUS efivar_set_uint32_le(const EFI_GUID *vendor, const char16_t *NAME, uint32_t value, uint32_t flags);
130 EFI_STATUS efivar_set_uint64_le(const EFI_GUID *vendor, const char16_t *name, uint64_t value, uint32_t flags);
131 void efivar_set_time_usec(const EFI_GUID *vendor, const char16_t *name, uint64_t usec);
132
133 EFI_STATUS efivar_get(const EFI_GUID *vendor, const char16_t *name, char16_t **ret);
134 EFI_STATUS efivar_get_raw(const EFI_GUID *vendor, const char16_t *name, char **ret, UINTN *ret_size);
135 EFI_STATUS efivar_get_uint_string(const EFI_GUID *vendor, const char16_t *name, UINTN *ret);
136 EFI_STATUS efivar_get_uint32_le(const EFI_GUID *vendor, const char16_t *name, uint32_t *ret);
137 EFI_STATUS efivar_get_uint64_le(const EFI_GUID *vendor, const char16_t *name, uint64_t *ret);
138 EFI_STATUS efivar_get_boolean_u8(const EFI_GUID *vendor, const char16_t *name, bool *ret);
139
140 void convert_efi_path(char16_t *path);
141 char16_t *xstr8_to_path(const char *stra);
142 void mangle_stub_cmdline(char16_t *cmdline);
143
144 EFI_STATUS file_read(EFI_FILE *dir, const char16_t *name, UINTN off, UINTN size, char **content, UINTN *content_size);
145
146 static inline void file_closep(EFI_FILE **handle) {
147 if (!*handle)
148 return;
149
150 (*handle)->Close(*handle);
151 }
152
153 static inline void unload_imagep(EFI_HANDLE *image) {
154 if (*image)
155 (void) BS->UnloadImage(*image);
156 }
157
158 /*
159 * Allocated random UUID, intended to be shared across tools that implement
160 * the (ESP)\loader\entries\<vendor>-<revision>.conf convention and the
161 * associated EFI variables.
162 */
163 #define LOADER_GUID \
164 &(const EFI_GUID) { 0x4a67b082, 0x0a4c, 0x41cf, { 0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f } }
165 #define EFI_GLOBAL_GUID &(const EFI_GUID) EFI_GLOBAL_VARIABLE
166
167 void log_error_stall(const char16_t *fmt, ...);
168 EFI_STATUS log_oom(void);
169
170 /* This works just like log_error_errno() from userspace, but requires you
171 * to provide err a second time if you want to use %r in the message! */
172 #define log_error_status_stall(err, fmt, ...) \
173 ({ \
174 log_error_stall(fmt, ##__VA_ARGS__); \
175 err; \
176 })
177
178 void print_at(UINTN x, UINTN y, UINTN attr, const char16_t *str);
179 void clear_screen(UINTN attr);
180
181 typedef int (*compare_pointer_func_t)(const void *a, const void *b);
182 void sort_pointer_array(void **array, UINTN n_members, compare_pointer_func_t compare);
183
184 EFI_STATUS get_file_info_harder(EFI_FILE *handle, EFI_FILE_INFO **ret, UINTN *ret_size);
185
186 EFI_STATUS readdir_harder(EFI_FILE *handle, EFI_FILE_INFO **buffer, UINTN *buffer_size);
187
188 bool is_ascii(const char16_t *f);
189
190 char16_t **strv_free(char16_t **l);
191
192 static inline void strv_freep(char16_t ***p) {
193 strv_free(*p);
194 }
195
196 EFI_STATUS open_directory(EFI_FILE *root_dir, const char16_t *path, EFI_FILE **ret);
197
198 /* Conversion between EFI_PHYSICAL_ADDRESS and pointers is not obvious. The former is always 64bit, even on
199 * 32bit archs. And gcc complains if we cast a pointer to an integer of a different size. Hence let's do the
200 * conversion indirectly: first into uintptr_t and then extended to EFI_PHYSICAL_ADDRESS. */
201 static inline EFI_PHYSICAL_ADDRESS POINTER_TO_PHYSICAL_ADDRESS(const void *p) {
202 return (EFI_PHYSICAL_ADDRESS) (uintptr_t) p;
203 }
204
205 static inline void *PHYSICAL_ADDRESS_TO_POINTER(EFI_PHYSICAL_ADDRESS addr) {
206 /* On 32bit systems the address might not be convertible (as pointers are 32bit but
207 * EFI_PHYSICAL_ADDRESS 64bit) */
208 assert(addr <= UINTPTR_MAX);
209 return (void *) (uintptr_t) addr;
210 }
211
212 uint64_t get_os_indications_supported(void);
213
214 #ifdef EFI_DEBUG
215 void debug_break(void);
216 extern uint8_t _text, _data;
217 /* Report the relocated position of text and data sections so that a debugger
218 * can attach to us. See debug-sd-boot.sh for how this can be done. */
219 # define debug_hook(identity) Print(identity L"@0x%lx,0x%lx\n", POINTER_TO_PHYSICAL_ADDRESS(&_text), POINTER_TO_PHYSICAL_ADDRESS(&_data))
220 #else
221 # define debug_hook(identity)
222 #endif
223
224 #ifdef EFI_DEBUG
225 void hexdump(const char16_t *prefix, const void *data, UINTN size);
226 #endif
227
228 #if defined(__i386__) || defined(__x86_64__)
229 void beep(UINTN beep_count);
230 #else
231 static inline void beep(UINTN beep_count) {}
232 #endif
233
234 EFI_STATUS open_volume(EFI_HANDLE device, EFI_FILE **ret_file);
235 EFI_STATUS make_file_device_path(EFI_HANDLE device, const char16_t *file, EFI_DEVICE_PATH **ret_dp);
236 EFI_STATUS device_path_to_str(const EFI_DEVICE_PATH *dp, char16_t **ret);
237
238 #if defined(__i386__) || defined(__x86_64__)
239 bool in_hypervisor(void);
240 #else
241 static inline bool in_hypervisor(void) {
242 return false;
243 }
244 #endif