]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/linux.c
Merge pull request #25168 from valentindavid/valentindavid/umount-move-recursive...
[thirdparty/systemd.git] / src / boot / efi / linux.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /*
4 * Generic Linux boot protocol using the EFI/PE entry point of the kernel. Passes
5 * initrd with the LINUX_INITRD_MEDIA_GUID DevicePath and cmdline with
6 * EFI LoadedImageProtocol.
7 *
8 * This method works for Linux 5.8 and newer on ARM/Aarch64, x86/x68_64 and RISC-V.
9 */
10
11 #include <efi.h>
12 #include <efilib.h>
13
14 #include "initrd.h"
15 #include "linux.h"
16 #include "pe.h"
17 #include "secure-boot.h"
18 #include "util.h"
19
20 #define STUB_PAYLOAD_GUID \
21 { 0x55c5d1f8, 0x04cd, 0x46b5, { 0x8a, 0x20, 0xe5, 0x6c, 0xbb, 0x30, 0x52, 0xd0 } }
22
23 typedef struct {
24 const void *addr;
25 size_t len;
26 const EFI_DEVICE_PATH *device_path;
27 } ValidationContext;
28
29 static bool validate_payload(
30 const void *ctx, const EFI_DEVICE_PATH *device_path, const void *file_buffer, size_t file_size) {
31
32 const ValidationContext *payload = ASSERT_PTR(ctx);
33
34 if (device_path != payload->device_path)
35 return false;
36
37 /* Security arch (1) protocol does not provide a file buffer. Instead we are supposed to fetch the payload
38 * ourselves, which is not needed as we already have everything in memory and the device paths match. */
39 if (file_buffer && (file_buffer != payload->addr || file_size != payload->len))
40 return false;
41
42 return true;
43 }
44
45 static EFI_STATUS load_image(EFI_HANDLE parent, const void *source, size_t len, EFI_HANDLE *ret_image) {
46 assert(parent);
47 assert(source);
48 assert(ret_image);
49
50 /* We could pass a NULL device path, but it's nicer to provide something and it allows us to identify
51 * the loaded image from within the security hooks. */
52 struct {
53 VENDOR_DEVICE_PATH payload;
54 EFI_DEVICE_PATH end;
55 } _packed_ payload_device_path = {
56 .payload = {
57 .Header = {
58 .Type = MEDIA_DEVICE_PATH,
59 .SubType = MEDIA_VENDOR_DP,
60 .Length = { sizeof(payload_device_path.payload), 0 },
61 },
62 .Guid = STUB_PAYLOAD_GUID,
63 },
64 .end = {
65 .Type = END_DEVICE_PATH_TYPE,
66 .SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE,
67 .Length = { sizeof(payload_device_path.end), 0 },
68 },
69 };
70
71 /* We want to support unsigned kernel images as payload, which is safe to do under secure boot
72 * because it is embedded in this stub loader (and since it is already running it must be trusted). */
73 install_security_override(
74 validate_payload,
75 &(ValidationContext) {
76 .addr = source,
77 .len = len,
78 .device_path = &payload_device_path.payload.Header,
79 });
80
81 EFI_STATUS ret = BS->LoadImage(
82 /*BootPolicy=*/false,
83 parent,
84 &payload_device_path.payload.Header,
85 (void *) source,
86 len,
87 ret_image);
88
89 uninstall_security_override();
90
91 return ret;
92 }
93
94 EFI_STATUS linux_exec(
95 EFI_HANDLE parent,
96 const char16_t *cmdline,
97 const void *linux_buffer,
98 size_t linux_length,
99 const void *initrd_buffer,
100 size_t initrd_length) {
101
102 uint32_t compat_address;
103 EFI_STATUS err;
104
105 assert(parent);
106 assert(linux_buffer && linux_length > 0);
107 assert(initrd_buffer || initrd_length == 0);
108
109 err = pe_kernel_info(linux_buffer, &compat_address);
110 #if defined(__i386__) || defined(__x86_64__)
111 if (err == EFI_UNSUPPORTED)
112 /* Kernel is too old to support LINUX_INITRD_MEDIA_GUID, try the deprecated EFI handover
113 * protocol. */
114 return linux_exec_efi_handover(
115 parent,
116 cmdline,
117 linux_buffer,
118 linux_length,
119 initrd_buffer,
120 initrd_length);
121 #endif
122 if (err != EFI_SUCCESS)
123 return log_error_status(err, "Bad kernel image: %m");
124
125 _cleanup_(unload_imagep) EFI_HANDLE kernel_image = NULL;
126 err = load_image(parent, linux_buffer, linux_length, &kernel_image);
127 if (err != EFI_SUCCESS)
128 return log_error_status(err, "Error loading kernel image: %m");
129
130 EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
131 err = BS->HandleProtocol(kernel_image, &LoadedImageProtocol, (void **) &loaded_image);
132 if (err != EFI_SUCCESS)
133 return log_error_status(err, "Error getting kernel loaded image protocol: %m");
134
135 if (cmdline) {
136 loaded_image->LoadOptions = (void *) cmdline;
137 loaded_image->LoadOptionsSize = strsize16(loaded_image->LoadOptions);
138 }
139
140 _cleanup_(cleanup_initrd) EFI_HANDLE initrd_handle = NULL;
141 err = initrd_register(initrd_buffer, initrd_length, &initrd_handle);
142 if (err != EFI_SUCCESS)
143 return log_error_status(err, "Error registering initrd: %m");
144
145 log_wait();
146 err = BS->StartImage(kernel_image, NULL, NULL);
147
148 /* Try calling the kernel compat entry point if one exists. */
149 if (err == EFI_UNSUPPORTED && compat_address > 0) {
150 EFI_IMAGE_ENTRY_POINT compat_entry =
151 (EFI_IMAGE_ENTRY_POINT) ((uint8_t *) loaded_image->ImageBase + compat_address);
152 err = compat_entry(kernel_image, ST);
153 }
154
155 return log_error_status(err, "Error starting kernel image: %m");
156 }