| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "efi.h" |
| 5 |
#include "memory-util.h" |
| 6 |
|
| 7 |
#if SD_BOOT |
| 8 |
|
| 9 |
#include "proto/file-io.h" |
| 10 |
|
| 11 |
/* This is provided by the linker. */ |
| 12 |
extern uint8_t __executable_start[]; |
| 13 |
|
| 14 |
DISABLE_WARNING_REDUNDANT_DECLS; |
| 15 |
void free(void *p); /* NOLINT (readability-redundant-declaration,readability-inconsistent-declaration-parameter-name) */ |
| 16 |
REENABLE_WARNING; |
| 17 |
|
| 18 |
static inline void freep(void *p) { |
| 19 |
free(*(void **) p); |
| 20 |
} |
| 21 |
|
| 22 |
#define _cleanup_free_ _cleanup_(freep) |
| 23 |
|
| 24 |
_malloc_ _alloc_(1) _returns_nonnull_ _warn_unused_result_ |
| 25 |
void *xmalloc(size_t size); |
| 26 |
|
| 27 |
_malloc_ _alloc_(1) _returns_nonnull_ _warn_unused_result_ |
| 28 |
static inline void *xcalloc(size_t size) { |
| 29 |
void *t = xmalloc(size); |
| 30 |
memzero(t, size); |
| 31 |
return t; |
| 32 |
} |
| 33 |
|
| 34 |
_malloc_ _alloc_(1, 2) _returns_nonnull_ _warn_unused_result_ |
| 35 |
static inline void *xcalloc_multiply(size_t n, size_t size) { |
| 36 |
assert_se(MUL_ASSIGN_SAFE(&size, n)); |
| 37 |
return xcalloc(size); |
| 38 |
} |
| 39 |
|
| 40 |
_malloc_ _alloc_(1, 2) _returns_nonnull_ _warn_unused_result_ |
| 41 |
static inline void *xmalloc_multiply(size_t n, size_t size) { |
| 42 |
assert_se(MUL_ASSIGN_SAFE(&size, n)); |
| 43 |
return xmalloc(size); |
| 44 |
} |
| 45 |
|
| 46 |
/* Use malloc attribute as this never returns p like userspace realloc. */ |
| 47 |
_malloc_ _alloc_(3) _returns_nonnull_ _warn_unused_result_ |
| 48 |
static inline void *xrealloc(void *p, size_t old_size, size_t new_size) { |
| 49 |
assert(p || old_size == 0); |
| 50 |
|
| 51 |
void *t = xmalloc(new_size); |
| 52 |
|
| 53 |
size_t size = MIN(old_size, new_size); |
| 54 |
if (size > 0) { |
| 55 |
assert(p); |
| 56 |
memcpy(t, p, size); |
| 57 |
} |
| 58 |
|
| 59 |
free(p); |
| 60 |
return t; |
| 61 |
} |
| 62 |
|
| 63 |
_malloc_ _alloc_(2) _returns_nonnull_ _warn_unused_result_ |
| 64 |
static inline void* xmemdup(const void *p, size_t l) { |
| 65 |
return memcpy(xmalloc(l), p, l); |
| 66 |
} |
| 67 |
|
| 68 |
#define xnew(type, n) ((type *) xmalloc_multiply((n), sizeof(type))) |
| 69 |
#define xnew0(type, n) ((type *) xcalloc_multiply((n), sizeof(type))) |
| 70 |
|
| 71 |
bool free_and_xstrdup16(char16_t **p, const char16_t *s); |
| 72 |
|
| 73 |
typedef struct { |
| 74 |
EFI_PHYSICAL_ADDRESS addr; |
| 75 |
size_t n_pages; |
| 76 |
} Pages; |
| 77 |
|
| 78 |
static inline void cleanup_pages(Pages *p) { |
| 79 |
if (p->n_pages == 0) |
| 80 |
return; |
| 81 |
#ifdef EFI_DEBUG |
| 82 |
assert_se(BS->FreePages(p->addr, p->n_pages) == EFI_SUCCESS); |
| 83 |
#else |
| 84 |
(void) BS->FreePages(p->addr, p->n_pages); |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
#define _cleanup_pages_ _cleanup_(cleanup_pages) |
| 89 |
|
| 90 |
static inline Pages xmalloc_pages( |
| 91 |
EFI_ALLOCATE_TYPE type, EFI_MEMORY_TYPE memory_type, size_t n_pages, EFI_PHYSICAL_ADDRESS addr) { |
| 92 |
assert_se(BS->AllocatePages(type, memory_type, n_pages, &addr) == EFI_SUCCESS); |
| 93 |
return (Pages) { |
| 94 |
.addr = addr, |
| 95 |
.n_pages = n_pages, |
| 96 |
}; |
| 97 |
} |
| 98 |
|
| 99 |
Pages xmalloc_aligned_pages( |
| 100 |
EFI_ALLOCATE_TYPE type, |
| 101 |
EFI_MEMORY_TYPE memory_type, |
| 102 |
size_t n_pages, |
| 103 |
size_t alignment, |
| 104 |
EFI_PHYSICAL_ADDRESS addr); |
| 105 |
|
| 106 |
static inline Pages xmalloc_initrd_pages(size_t n_pages) { |
| 107 |
/* The original native x86 boot protocol of the Linux kernel was not 64bit safe, hence we try to |
| 108 |
* allocate memory for the initrds below the 4G boundary on x86, since we don't know early enough |
| 109 |
* which protocol we'll use to ultimately boot the kernel. This restriction is somewhat obsolete, |
| 110 |
* since these days we generally prefer the kernel's newer EFI entrypoint instead, which has no such |
| 111 |
* limitations. There's a good chance that for large allocations we won't be successful, hence |
| 112 |
* immediately fallback to an unrestricted allocation. On other architectures we do not bother with |
| 113 |
* any restriction on this, in particular as some of them don't even have RAM mapped to such low |
| 114 |
* addresses. */ |
| 115 |
|
| 116 |
#if defined(__i386__) || defined(__x86_64__) |
| 117 |
EFI_PHYSICAL_ADDRESS addr = UINT32_MAX; /* Below 4G boundary. */ |
| 118 |
if (BS->AllocatePages( |
| 119 |
AllocateMaxAddress, |
| 120 |
EfiLoaderData, |
| 121 |
EFI_SIZE_TO_PAGES(n_pages), |
| 122 |
&addr) == EFI_SUCCESS) |
| 123 |
return (Pages) { |
| 124 |
.addr = addr, |
| 125 |
.n_pages = EFI_SIZE_TO_PAGES(n_pages), |
| 126 |
}; |
| 127 |
#endif |
| 128 |
return xmalloc_pages( |
| 129 |
AllocateAnyPages, |
| 130 |
EfiLoaderData, |
| 131 |
EFI_SIZE_TO_PAGES(n_pages), |
| 132 |
0 /* Ignored. */); |
| 133 |
} |
| 134 |
|
| 135 |
void convert_efi_path(char16_t *path); |
| 136 |
char16_t *xstr8_to_path(const char *str8); |
| 137 |
char16_t *mangle_stub_cmdline(char16_t *cmdline); |
| 138 |
|
| 139 |
EFI_STATUS chunked_read(EFI_FILE *file, size_t *size, void *buf); |
| 140 |
EFI_STATUS file_read(EFI_FILE *dir, const char16_t *name, uint64_t offset, size_t size, char **ret, size_t *ret_size); |
| 141 |
EFI_STATUS load_file_from_simple_filesystem(const EFI_DEVICE_PATH *device_path, char **file_buffer, size_t *file_size); |
| 142 |
EFI_STATUS file_handle_read(EFI_FILE *handle, uint64_t offset, size_t size, char **ret, size_t *ret_size); |
| 143 |
|
| 144 |
static inline void file_closep(EFI_FILE **handle) { |
| 145 |
if (!*handle) |
| 146 |
return; |
| 147 |
|
| 148 |
(*handle)->Close(*handle); |
| 149 |
} |
| 150 |
|
| 151 |
#define _cleanup_file_close_ _cleanup_(file_closep) |
| 152 |
|
| 153 |
static inline void unload_imagep(EFI_HANDLE *image) { |
| 154 |
if (*image) |
| 155 |
(void) BS->UnloadImage(*image); |
| 156 |
} |
| 157 |
|
| 158 |
/* Note that GUID is evaluated multiple times! */ |
| 159 |
#define GUID_FORMAT_STR "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X" |
| 160 |
#define GUID_FORMAT_VAL(g) (g).Data1, (g).Data2, (g).Data3, (g).Data4[0], (g).Data4[1], \ |
| 161 |
(g).Data4[2], (g).Data4[3], (g).Data4[4], (g).Data4[5], (g).Data4[6], (g).Data4[7] |
| 162 |
|
| 163 |
void set_attribute_safe(size_t attr); |
| 164 |
void print_at(size_t x, size_t y, size_t attr, const char16_t *str); |
| 165 |
void clear_screen(size_t attr); |
| 166 |
|
| 167 |
typedef int (*compare_pointer_func_t)(const void *a, const void *b); |
| 168 |
void sort_pointer_array(void **array, size_t n_members, compare_pointer_func_t compare); |
| 169 |
|
| 170 |
EFI_STATUS get_file_info(EFI_FILE *handle, EFI_FILE_INFO **ret, size_t *ret_size); |
| 171 |
EFI_STATUS readdir(EFI_FILE *handle, EFI_FILE_INFO **buffer, size_t *buffer_size); |
| 172 |
|
| 173 |
bool is_ascii(const char16_t *f); |
| 174 |
|
| 175 |
char16_t **strv_free(char16_t **l); |
| 176 |
|
| 177 |
DEFINE_TRIVIAL_CLEANUP_FUNC(char16_t**, strv_free); |
| 178 |
|
| 179 |
#define _cleanup_strv_free_ _cleanup_(strv_freep) |
| 180 |
|
| 181 |
EFI_STATUS open_directory(EFI_FILE *root_dir, const char16_t *path, EFI_FILE **ret); |
| 182 |
|
| 183 |
/* Conversion between EFI_PHYSICAL_ADDRESS and pointers is not obvious. The former is always 64-bit, even on |
| 184 |
* 32-bit archs. And gcc complains if we cast a pointer to an integer of a different size. Hence let's do the |
| 185 |
* conversion indirectly: first into uintptr_t and then extended to EFI_PHYSICAL_ADDRESS. */ |
| 186 |
static inline EFI_PHYSICAL_ADDRESS POINTER_TO_PHYSICAL_ADDRESS(const void *p) { |
| 187 |
return (EFI_PHYSICAL_ADDRESS) (uintptr_t) p; |
| 188 |
} |
| 189 |
|
| 190 |
static inline void *PHYSICAL_ADDRESS_TO_POINTER(EFI_PHYSICAL_ADDRESS addr) { |
| 191 |
/* On 32-bit systems the address might not be convertible (as pointers are 32-bit but |
| 192 |
* EFI_PHYSICAL_ADDRESS 64-bit) */ |
| 193 |
assert(addr <= UINTPTR_MAX); |
| 194 |
return (void *) (uintptr_t) addr; |
| 195 |
} |
| 196 |
|
| 197 |
/* If EFI_DEBUG, print our name and version and also report the address of the image base so a debugger can |
| 198 |
* be attached. See debug-sd-boot.sh for how this can be done. */ |
| 199 |
void notify_debugger(const char *identity, bool wait); |
| 200 |
|
| 201 |
/* On x86 the compiler assumes a different incoming stack alignment than what we get. |
| 202 |
* This will cause long long variables to be misaligned when building with |
| 203 |
* '-mlong-double' (for correct struct layouts). Normally, the compiler realigns the |
| 204 |
* stack itself on entry, but we have to do this ourselves here as the compiler does |
| 205 |
* not know that this is our entry point. */ |
| 206 |
#ifdef __i386__ |
| 207 |
# define _realign_stack_ __attribute__((force_align_arg_pointer)) |
| 208 |
#else |
| 209 |
# define _realign_stack_ |
| 210 |
#endif |
| 211 |
|
| 212 |
#define DEFINE_EFI_MAIN_FUNCTION(func, identity, wait_for_debugger) \ |
| 213 |
EFI_SYSTEM_TABLE *ST; \ |
| 214 |
EFI_BOOT_SERVICES *BS; \ |
| 215 |
EFI_RUNTIME_SERVICES *RT; \ |
| 216 |
_realign_stack_ \ |
| 217 |
EFIAPI EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *system_table); \ |
| 218 |
EFIAPI EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *system_table) { \ |
| 219 |
ST = system_table; \ |
| 220 |
BS = system_table->BootServices; \ |
| 221 |
RT = system_table->RuntimeServices; \ |
| 222 |
__stack_chk_guard_init(); \ |
| 223 |
notify_debugger((identity), (wait_for_debugger)); \ |
| 224 |
EFI_STATUS err = func(image); \ |
| 225 |
log_wait(); \ |
| 226 |
return err; \ |
| 227 |
} |
| 228 |
|
| 229 |
#if defined(__i386__) || defined(__x86_64__) |
| 230 |
void beep(unsigned beep_count); |
| 231 |
#else |
| 232 |
static inline void beep(unsigned beep_count) {} |
| 233 |
#endif |
| 234 |
|
| 235 |
EFI_STATUS open_volume(EFI_HANDLE device, EFI_FILE **ret_file); |
| 236 |
|
| 237 |
static inline bool efi_guid_equal(const EFI_GUID *a, const EFI_GUID *b) { |
| 238 |
return memcmp(a, b, sizeof(EFI_GUID)) == 0; |
| 239 |
} |
| 240 |
|
| 241 |
void *find_configuration_table(const EFI_GUID *guid); |
| 242 |
|
| 243 |
char16_t *get_extra_dir(const EFI_DEVICE_PATH *file_path); |
| 244 |
|
| 245 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 246 |
# define be16toh(x) __builtin_bswap16(x) |
| 247 |
# define be32toh(x) __builtin_bswap32(x) |
| 248 |
# define le16toh(x) (x) |
| 249 |
# define le32toh(x) (x) |
| 250 |
#else |
| 251 |
# error "Unexpected byte order in EFI mode?" |
| 252 |
#endif |
| 253 |
|
| 254 |
#define bswap_16(x) __builtin_bswap16(x) |
| 255 |
#define bswap_32(x) __builtin_bswap32(x) |
| 256 |
|
| 257 |
#else |
| 258 |
|
| 259 |
#include "alloc-util.h" |
| 260 |
|
| 261 |
#define xnew0(type, n) ASSERT_PTR(new0(type, n)) |
| 262 |
|
| 263 |
#endif |
| 264 |
|
| 265 |
char16_t *url_replace_last_component(const char16_t *url, const char16_t *filename); |