| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* |
| 4 |
* x86 specific code to for EFI handover boot protocol |
| 5 |
* Linux kernels version 5.8 and newer support providing the initrd by |
| 6 |
* LINUX_INITRD_MEDIA_GUID DevicePath. In order to support older kernels too, |
| 7 |
* this x86 specific linux_exec function passes the initrd by setting the |
| 8 |
* corresponding fields in the setup_header struct. |
| 9 |
* |
| 10 |
* see https://docs.kernel.org/arch/x86/boot.html |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "efi-log.h" |
| 14 |
#include "linux.h" |
| 15 |
#include "macro.h" |
| 16 |
#include "memory-util.h" |
| 17 |
#include "util.h" |
| 18 |
|
| 19 |
#define KERNEL_SECTOR_SIZE 512u |
| 20 |
#define BOOT_FLAG_MAGIC 0xAA55u |
| 21 |
#define SETUP_MAGIC 0x53726448u /* "HdrS" */ |
| 22 |
#define SETUP_VERSION_2_11 0x20bu |
| 23 |
#define SETUP_VERSION_2_12 0x20cu |
| 24 |
#define SETUP_VERSION_2_15 0x20fu |
| 25 |
#define CMDLINE_PTR_MAX 0xA0000u |
| 26 |
|
| 27 |
enum { |
| 28 |
XLF_KERNEL_64 = 1 << 0, |
| 29 |
XLF_CAN_BE_LOADED_ABOVE_4G = 1 << 1, |
| 30 |
XLF_EFI_HANDOVER_32 = 1 << 2, |
| 31 |
XLF_EFI_HANDOVER_64 = 1 << 3, |
| 32 |
#ifdef __x86_64__ |
| 33 |
XLF_EFI_HANDOVER = XLF_EFI_HANDOVER_64, |
| 34 |
#else |
| 35 |
XLF_EFI_HANDOVER = XLF_EFI_HANDOVER_32, |
| 36 |
#endif |
| 37 |
}; |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
uint8_t setup_sects; |
| 41 |
uint16_t root_flags; |
| 42 |
uint32_t syssize; |
| 43 |
uint16_t ram_size; |
| 44 |
uint16_t vid_mode; |
| 45 |
uint16_t root_dev; |
| 46 |
uint16_t boot_flag; |
| 47 |
uint8_t jump; /* We split the 2-byte jump field from the spec in two for convenience. */ |
| 48 |
uint8_t setup_size; |
| 49 |
uint32_t header; |
| 50 |
uint16_t version; |
| 51 |
uint32_t realmode_swtch; |
| 52 |
uint16_t start_sys_seg; |
| 53 |
uint16_t kernel_version; |
| 54 |
uint8_t type_of_loader; |
| 55 |
uint8_t loadflags; |
| 56 |
uint16_t setup_move_size; |
| 57 |
uint32_t code32_start; |
| 58 |
uint32_t ramdisk_image; |
| 59 |
uint32_t ramdisk_size; |
| 60 |
uint32_t bootsect_kludge; |
| 61 |
uint16_t heap_end_ptr; |
| 62 |
uint8_t ext_loader_ver; |
| 63 |
uint8_t ext_loader_type; |
| 64 |
uint32_t cmd_line_ptr; |
| 65 |
uint32_t initrd_addr_max; |
| 66 |
uint32_t kernel_alignment; |
| 67 |
uint8_t relocatable_kernel; |
| 68 |
uint8_t min_alignment; |
| 69 |
uint16_t xloadflags; |
| 70 |
uint32_t cmdline_size; |
| 71 |
uint32_t hardware_subarch; |
| 72 |
uint64_t hardware_subarch_data; |
| 73 |
uint32_t payload_offset; |
| 74 |
uint32_t payload_length; |
| 75 |
uint64_t setup_data; |
| 76 |
uint64_t pref_address; |
| 77 |
uint32_t init_size; |
| 78 |
uint32_t handover_offset; |
| 79 |
} _packed_ SetupHeader; |
| 80 |
|
| 81 |
/* We really only care about a few fields, but we still have to provide a full page otherwise. */ |
| 82 |
typedef struct { |
| 83 |
uint8_t pad[192]; |
| 84 |
uint32_t ext_ramdisk_image; |
| 85 |
uint32_t ext_ramdisk_size; |
| 86 |
uint32_t ext_cmd_line_ptr; |
| 87 |
uint8_t pad2[293]; |
| 88 |
SetupHeader hdr; |
| 89 |
uint8_t pad3[3480]; |
| 90 |
} _packed_ BootParams; |
| 91 |
assert_cc(offsetof(BootParams, ext_ramdisk_image) == 0x0C0); |
| 92 |
assert_cc(sizeof(BootParams) == 4096); |
| 93 |
|
| 94 |
#ifdef __i386__ |
| 95 |
# define __regparm0__ __attribute__((regparm(0))) |
| 96 |
#else |
| 97 |
# define __regparm0__ |
| 98 |
#endif |
| 99 |
|
| 100 |
typedef void (*handover_f)(void *parent, EFI_SYSTEM_TABLE *table, BootParams *params) __regparm0__ |
| 101 |
__attribute__((sysv_abi)); |
| 102 |
|
| 103 |
static void linux_efi_handover(EFI_HANDLE parent, uintptr_t kernel, BootParams *params) { |
| 104 |
assert(params); |
| 105 |
|
| 106 |
kernel += (params->hdr.setup_sects + 1) * KERNEL_SECTOR_SIZE; /* 32-bit entry address. */ |
| 107 |
|
| 108 |
/* Old kernels needs this set, while newer ones seem to ignore this. */ |
| 109 |
params->hdr.code32_start = kernel; |
| 110 |
|
| 111 |
#ifdef __x86_64__ |
| 112 |
kernel += KERNEL_SECTOR_SIZE; /* 64-bit entry address. */ |
| 113 |
#endif |
| 114 |
|
| 115 |
kernel += params->hdr.handover_offset; /* 32/64-bit EFI handover address. */ |
| 116 |
|
| 117 |
/* Note in EFI mixed mode this now points to the correct 32-bit handover entry point, allowing a 64-bit |
| 118 |
* kernel to be booted from a 32-bit sd-stub. */ |
| 119 |
|
| 120 |
handover_f handover = (handover_f) kernel; |
| 121 |
handover(parent, ST, params); |
| 122 |
} |
| 123 |
|
| 124 |
EFI_STATUS linux_exec_efi_handover( |
| 125 |
EFI_HANDLE parent, |
| 126 |
const char16_t *cmdline, |
| 127 |
const struct iovec *kernel, |
| 128 |
const struct iovec *initrd, |
| 129 |
size_t kernel_size_in_memory) { |
| 130 |
|
| 131 |
assert(parent); |
| 132 |
assert(iovec_is_set(kernel)); |
| 133 |
assert(iovec_is_valid(initrd)); |
| 134 |
|
| 135 |
if (kernel->iov_len < sizeof(BootParams)) |
| 136 |
return EFI_LOAD_ERROR; |
| 137 |
|
| 138 |
const BootParams *image_params = (const BootParams *) kernel->iov_base; |
| 139 |
if (image_params->hdr.header != SETUP_MAGIC || image_params->hdr.boot_flag != BOOT_FLAG_MAGIC) |
| 140 |
return log_error_status(EFI_UNSUPPORTED, "Unsupported kernel image."); |
| 141 |
if (image_params->hdr.version < SETUP_VERSION_2_11) |
| 142 |
return log_error_status(EFI_UNSUPPORTED, "Kernel too old."); |
| 143 |
if (!image_params->hdr.relocatable_kernel) |
| 144 |
return log_error_status(EFI_UNSUPPORTED, "Kernel is not relocatable."); |
| 145 |
|
| 146 |
/* The xloadflags were added in version 2.12+ of the boot protocol but the handover support predates |
| 147 |
* that, so we cannot safety-check this for 2.11. */ |
| 148 |
if (image_params->hdr.version >= SETUP_VERSION_2_12 && |
| 149 |
!FLAGS_SET(image_params->hdr.xloadflags, XLF_EFI_HANDOVER)) |
| 150 |
return log_error_status(EFI_UNSUPPORTED, "Kernel does not support EFI handover protocol."); |
| 151 |
|
| 152 |
bool can_4g = image_params->hdr.version >= SETUP_VERSION_2_12 && |
| 153 |
FLAGS_SET(image_params->hdr.xloadflags, XLF_CAN_BE_LOADED_ABOVE_4G); |
| 154 |
|
| 155 |
/* There is no way to pass the high bits of code32_start. Newer kernels seems to handle this |
| 156 |
* just fine, but older kernels will fail even if they otherwise have above 4G boot support. |
| 157 |
* A PE image's memory footprint can be larger than its file size, due to unallocated virtual |
| 158 |
* memory sections. While normally all PE headers should be taken into account, this case only |
| 159 |
* involves x86 Linux bzImage kernel images, for which unallocated areas are only part of the last |
| 160 |
* header, so parsing SizeOfImage and zeroeing the buffer past the image size is enough. */ |
| 161 |
_cleanup_pages_ Pages linux_relocated = {}; |
| 162 |
const void *linux_buffer; |
| 163 |
if (POINTER_TO_PHYSICAL_ADDRESS(kernel->iov_base) + kernel->iov_len > UINT32_MAX || kernel_size_in_memory > kernel->iov_len) { |
| 164 |
linux_relocated = xmalloc_pages( |
| 165 |
AllocateMaxAddress, |
| 166 |
EfiLoaderCode, |
| 167 |
EFI_SIZE_TO_PAGES(MAX(kernel_size_in_memory, kernel->iov_len)), |
| 168 |
UINT32_MAX); |
| 169 |
linux_buffer = memcpy( |
| 170 |
PHYSICAL_ADDRESS_TO_POINTER(linux_relocated.addr), kernel->iov_base, kernel->iov_len); |
| 171 |
if (kernel_size_in_memory > kernel->iov_len) |
| 172 |
memzero((uint8_t *) linux_buffer + kernel->iov_len, kernel_size_in_memory - kernel->iov_len); |
| 173 |
} else |
| 174 |
linux_buffer = kernel->iov_base; |
| 175 |
|
| 176 |
_cleanup_pages_ Pages initrd_relocated = {}; |
| 177 |
const void *initrd_buffer; |
| 178 |
if (!can_4g && POINTER_TO_PHYSICAL_ADDRESS(initrd->iov_base) + initrd->iov_len > UINT32_MAX) { |
| 179 |
initrd_relocated = xmalloc_pages( |
| 180 |
AllocateMaxAddress, EfiLoaderData, EFI_SIZE_TO_PAGES(initrd->iov_len), UINT32_MAX); |
| 181 |
initrd_buffer = memcpy( |
| 182 |
PHYSICAL_ADDRESS_TO_POINTER(initrd_relocated.addr), |
| 183 |
initrd->iov_base, |
| 184 |
initrd->iov_len); |
| 185 |
} else |
| 186 |
initrd_buffer = initrd->iov_base; |
| 187 |
|
| 188 |
_cleanup_pages_ Pages boot_params_page = xmalloc_pages( |
| 189 |
can_4g ? AllocateAnyPages : AllocateMaxAddress, |
| 190 |
EfiLoaderData, |
| 191 |
EFI_SIZE_TO_PAGES(sizeof(BootParams)), |
| 192 |
UINT32_MAX /* Below the 4G boundary */); |
| 193 |
BootParams *boot_params = PHYSICAL_ADDRESS_TO_POINTER(boot_params_page.addr); |
| 194 |
*boot_params = (BootParams){}; |
| 195 |
|
| 196 |
/* Setup size is determined by offset 0x0202 + byte value at offset 0x0201, which is the same as |
| 197 |
* offset of the header field and the target from the jump field (which we split for this reason). */ |
| 198 |
size_t setup_hdr_len; |
| 199 |
if (!ADD_SAFE(&setup_hdr_len, offsetof(SetupHeader, header), image_params->hdr.setup_size)) |
| 200 |
setup_hdr_len = sizeof(SetupHeader); |
| 201 |
else |
| 202 |
setup_hdr_len = MIN(setup_hdr_len, sizeof(SetupHeader)); |
| 203 |
memcpy(&boot_params->hdr, |
| 204 |
&image_params->hdr, |
| 205 |
setup_hdr_len); |
| 206 |
|
| 207 |
boot_params->hdr.type_of_loader = 0xff; |
| 208 |
|
| 209 |
/* Spec says: For backwards compatibility, if the setup_sects field contains 0, the real value is 4. */ |
| 210 |
if (boot_params->hdr.setup_sects == 0) |
| 211 |
boot_params->hdr.setup_sects = 4; |
| 212 |
|
| 213 |
_cleanup_pages_ Pages cmdline_pages = {}; |
| 214 |
if (cmdline) { |
| 215 |
size_t len = MIN(strlen16(cmdline), image_params->hdr.cmdline_size); |
| 216 |
|
| 217 |
cmdline_pages = xmalloc_pages( |
| 218 |
can_4g ? AllocateAnyPages : AllocateMaxAddress, |
| 219 |
EfiLoaderData, |
| 220 |
EFI_SIZE_TO_PAGES(len + 1), |
| 221 |
CMDLINE_PTR_MAX); |
| 222 |
|
| 223 |
/* Convert cmdline to ASCII. */ |
| 224 |
char *cmdline8 = PHYSICAL_ADDRESS_TO_POINTER(cmdline_pages.addr); |
| 225 |
for (size_t i = 0; i < len; i++) |
| 226 |
cmdline8[i] = cmdline[i] <= 0x7E ? cmdline[i] : ' '; |
| 227 |
cmdline8[len] = '\0'; |
| 228 |
|
| 229 |
boot_params->hdr.cmd_line_ptr = (uint32_t) cmdline_pages.addr; |
| 230 |
boot_params->ext_cmd_line_ptr = cmdline_pages.addr >> 32; |
| 231 |
assert(can_4g || cmdline_pages.addr <= CMDLINE_PTR_MAX); |
| 232 |
} |
| 233 |
|
| 234 |
boot_params->hdr.ramdisk_image = (uintptr_t) initrd_buffer; |
| 235 |
boot_params->ext_ramdisk_image = POINTER_TO_PHYSICAL_ADDRESS(initrd_buffer) >> 32; |
| 236 |
boot_params->hdr.ramdisk_size = initrd->iov_len; |
| 237 |
boot_params->ext_ramdisk_size = ((uint64_t) initrd->iov_len) >> 32; |
| 238 |
|
| 239 |
log_wait(); |
| 240 |
linux_efi_handover(parent, (uintptr_t) linux_buffer, boot_params); |
| 241 |
return EFI_LOAD_ERROR; |
| 242 |
} |