]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/efi/stub.c
Merge pull request #21977 from systemd/wip/hadess/minipro-uaccess
[thirdparty/systemd.git] / src / boot / efi / stub.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
0fa2cac4
KS
2
3#include <efi.h>
4#include <efilib.h>
5
845707aa 6#include "cpio.h"
33bc9b75 7#include "devicetree.h"
8110e144 8#include "disk.h"
37fa3690 9#include "graphics.h"
0fa2cac4 10#include "linux.h"
d4cbada2
MG
11#include "measure.h"
12#include "pe.h"
ce0f078f 13#include "secure-boot.h"
cf0fbc49
TA
14#include "splash.h"
15#include "util.h"
0fa2cac4
KS
16
17/* magic string to find in the binary image */
2ccd5986 18_used_ _section_(".sdmagic") static const char magic[] = "#### LoaderInfo: systemd-stub " GIT_VERSION " ####";
0fa2cac4 19
845707aa
LP
20static EFI_STATUS combine_initrd(
21 EFI_PHYSICAL_ADDRESS initrd_base, UINTN initrd_size,
70cd15e9 22 const void *credential_initrd, UINTN credential_initrd_size,
f3b6f333 23 const void *global_credential_initrd, UINTN global_credential_initrd_size,
70cd15e9 24 const void *sysext_initrd, UINTN sysext_initrd_size,
845707aa
LP
25 EFI_PHYSICAL_ADDRESS *ret_initrd_base, UINTN *ret_initrd_size) {
26
27 EFI_PHYSICAL_ADDRESS base = UINT32_MAX; /* allocate an area below the 32bit boundary for this */
28 EFI_STATUS err;
29 UINT8 *p;
30 UINTN n;
31
32 assert(ret_initrd_base);
33 assert(ret_initrd_size);
34
f3b6f333 35 /* Combines four initrds into one, by simple concatenation in memory */
845707aa
LP
36
37 n = ALIGN_TO(initrd_size, 4); /* main initrd might not be padded yet */
38 if (credential_initrd) {
39 if (n > UINTN_MAX - credential_initrd_size)
40 return EFI_OUT_OF_RESOURCES;
41
42 n += credential_initrd_size;
43 }
f3b6f333
AV
44 if (global_credential_initrd) {
45 if (n > UINTN_MAX - global_credential_initrd_size)
46 return EFI_OUT_OF_RESOURCES;
47
48 n += global_credential_initrd_size;
49 }
845707aa
LP
50 if (sysext_initrd) {
51 if (n > UINTN_MAX - sysext_initrd_size)
52 return EFI_OUT_OF_RESOURCES;
53
54 n += sysext_initrd_size;
55 }
56
12f32748 57 err = BS->AllocatePages(
845707aa
LP
58 AllocateMaxAddress,
59 EfiLoaderData,
60 EFI_SIZE_TO_PAGES(n),
61 &base);
62 if (EFI_ERROR(err))
63 return log_error_status_stall(err, L"Failed to allocate space for combined initrd: %r", err);
64
a0a644be 65 p = PHYSICAL_ADDRESS_TO_POINTER(base);
845707aa
LP
66 if (initrd_base != 0) {
67 UINTN pad;
68
69 /* Order matters, the real initrd must come first, since it might include microcode updates
70 * which the kernel only looks for in the first cpio archive */
a0a644be 71 CopyMem(p, PHYSICAL_ADDRESS_TO_POINTER(initrd_base), initrd_size);
845707aa
LP
72 p += initrd_size;
73
74 pad = ALIGN_TO(initrd_size, 4) - initrd_size;
75 if (pad > 0) {
76 ZeroMem(p, pad);
77 p += pad;
78 }
79 }
80
81 if (credential_initrd) {
82 CopyMem(p, credential_initrd, credential_initrd_size);
83 p += credential_initrd_size;
84 }
85
f3b6f333
AV
86 if (global_credential_initrd) {
87 CopyMem(p, global_credential_initrd, global_credential_initrd_size);
88 p += global_credential_initrd_size;
89 }
90
845707aa
LP
91 if (sysext_initrd) {
92 CopyMem(p, sysext_initrd, sysext_initrd_size);
93 p += sysext_initrd_size;
94 }
95
a0a644be 96 assert((UINT8*) PHYSICAL_ADDRESS_TO_POINTER(base) + n == p);
845707aa
LP
97
98 *ret_initrd_base = base;
99 *ret_initrd_size = n;
100
101 return EFI_SUCCESS;
102}
103
70cd15e9 104static void export_variables(EFI_LOADED_IMAGE *loaded_image) {
5a186322
LP
105 CHAR16 uuid[37];
106
107 assert(loaded_image);
108
109 /* Export the device path this image is started from, if it's not set yet */
110 if (efivar_get_raw(LOADER_GUID, L"LoaderDevicePartUUID", NULL, NULL) != EFI_SUCCESS)
111 if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS)
112 efivar_set(LOADER_GUID, L"LoaderDevicePartUUID", uuid, 0);
113
114 /* If LoaderImageIdentifier is not set, assume the image with this stub was loaded directly from the
115 * UEFI firmware without any boot loader, and hence set the LoaderImageIdentifier ourselves. Note
116 * that some boot chain loaders neither set LoaderImageIdentifier nor make FilePath available to us,
117 * in which case there's simple nothing to set for us. (The UEFI spec doesn't really say who's wrong
118 * here, i.e. whether FilePath may be NULL or not, hence handle this gracefully and check if FilePath
119 * is non-NULL explicitly.) */
120 if (efivar_get_raw(LOADER_GUID, L"LoaderImageIdentifier", NULL, NULL) != EFI_SUCCESS &&
121 loaded_image->FilePath) {
122 _cleanup_freepool_ CHAR16 *s = NULL;
123
124 s = DevicePathToStr(loaded_image->FilePath);
9f048123
JJ
125 if (s)
126 efivar_set(LOADER_GUID, L"LoaderImageIdentifier", s, 0);
127 else
128 log_oom();
5a186322
LP
129 }
130
131 /* if LoaderFirmwareInfo is not set, let's set it */
132 if (efivar_get_raw(LOADER_GUID, L"LoaderFirmwareInfo", NULL, NULL) != EFI_SUCCESS) {
133 _cleanup_freepool_ CHAR16 *s = NULL;
0a15a824
JJ
134 s = xpool_print(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
135 efivar_set(LOADER_GUID, L"LoaderFirmwareInfo", s, 0);
5a186322
LP
136 }
137
138 /* ditto for LoaderFirmwareType */
139 if (efivar_get_raw(LOADER_GUID, L"LoaderFirmwareType", NULL, NULL) != EFI_SUCCESS) {
140 _cleanup_freepool_ CHAR16 *s = NULL;
0a15a824
JJ
141 s = xpool_print(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
142 efivar_set(LOADER_GUID, L"LoaderFirmwareType", s, 0);
5a186322
LP
143 }
144
145 /* add StubInfo */
146 if (efivar_get_raw(LOADER_GUID, L"StubInfo", NULL, NULL) != EFI_SUCCESS)
147 efivar_set(LOADER_GUID, L"StubInfo", L"systemd-stub " GIT_VERSION, 0);
148}
149
0fa2cac4 150EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
e41d3d89
LP
151
152 enum {
153 SECTION_CMDLINE,
154 SECTION_LINUX,
155 SECTION_INITRD,
156 SECTION_SPLASH,
33bc9b75 157 SECTION_DTB,
e41d3d89
LP
158 _SECTION_MAX,
159 };
160
65ff3d26 161 static const CHAR8* const sections[_SECTION_MAX + 1] = {
e41d3d89
LP
162 [SECTION_CMDLINE] = (const CHAR8*) ".cmdline",
163 [SECTION_LINUX] = (const CHAR8*) ".linux",
164 [SECTION_INITRD] = (const CHAR8*) ".initrd",
165 [SECTION_SPLASH] = (const CHAR8*) ".splash",
33bc9b75 166 [SECTION_DTB] = (const CHAR8*) ".dtb",
e41d3d89 167 NULL,
0fa2cac4 168 };
5b5d365d 169
33bc9b75 170 UINTN cmdline_len = 0, linux_size, initrd_size, dt_size;
f3b6f333
AV
171 UINTN credential_initrd_size = 0, global_credential_initrd_size = 0, sysext_initrd_size = 0;
172 _cleanup_freepool_ void *credential_initrd = NULL, *global_credential_initrd = NULL;
173 _cleanup_freepool_ void *sysext_initrd = NULL;
33bc9b75
MR
174 EFI_PHYSICAL_ADDRESS linux_base, initrd_base, dt_base;
175 _cleanup_(devicetree_cleanup) struct devicetree_state dt_state = {};
845707aa 176 EFI_LOADED_IMAGE *loaded_image;
e41d3d89
LP
177 UINTN addrs[_SECTION_MAX] = {};
178 UINTN szs[_SECTION_MAX] = {};
0fa2cac4 179 CHAR8 *cmdline = NULL;
1f6f233f 180 _cleanup_freepool_ CHAR8 *cmdline_owned = NULL;
0fa2cac4
KS
181 EFI_STATUS err;
182
183 InitializeLib(image, sys_table);
184
12f32748
JJ
185 err = BS->OpenProtocol(
186 image,
187 &LoadedImageProtocol,
188 (void **)&loaded_image,
189 image,
190 NULL,
191 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
8aba0eec
JJ
192 if (EFI_ERROR(err))
193 return log_error_status_stall(err, L"Error getting a LoadedImageProtocol handle: %r", err);
0fa2cac4 194
e41d3d89 195 err = pe_memory_locate_sections(loaded_image->ImageBase, (const CHAR8**) sections, addrs, szs);
65ff3d26
JJ
196 if (EFI_ERROR(err) || szs[SECTION_LINUX] == 0) {
197 if (!EFI_ERROR(err))
198 err = EFI_NOT_FOUND;
8aba0eec 199 return log_error_status_stall(err, L"Unable to locate embedded .linux section: %r", err);
65ff3d26 200 }
0fa2cac4 201
94b81afb 202 /* Show splash screen as early as possible */
6e161dc8 203 graphics_splash((const UINT8*) loaded_image->ImageBase + addrs[SECTION_SPLASH], szs[SECTION_SPLASH], NULL);
94b81afb 204
e41d3d89 205 if (szs[SECTION_CMDLINE] > 0) {
5b5d365d 206 cmdline = (CHAR8*) loaded_image->ImageBase + addrs[SECTION_CMDLINE];
e41d3d89
LP
207 cmdline_len = szs[SECTION_CMDLINE];
208 }
0fa2cac4 209
53a20455 210 /* if we are not in secure boot mode, or none was provided, accept a custom command line and replace the built-in one */
ce0f078f
DDM
211 if ((!secure_boot_enabled() || cmdline_len == 0) && loaded_image->LoadOptionsSize > 0 &&
212 *(CHAR16 *) loaded_image->LoadOptions > 0x1F) {
0fa2cac4 213 cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8);
0a15a824 214 cmdline = cmdline_owned = xallocate_pool(cmdline_len);
9f048123 215
258f0970 216 for (UINTN i = 0; i < cmdline_len; i++)
1f6f233f 217 cmdline[i] = ((CHAR16 *) loaded_image->LoadOptions)[i];
92ed3bb4 218
e6e24af5
LP
219 /* Let's measure the passed kernel command line into the TPM. Note that this possibly
220 * duplicates what we already did in the boot menu, if that was already used. However, since
221 * we want the boot menu to support an EFI binary, and want to this stub to be usable from
222 * any boot menu, let's measure things anyway. */
70cd15e9 223 (void) tpm_log_load_options(loaded_image->LoadOptions);
0fa2cac4
KS
224 }
225
5a186322 226 export_variables(loaded_image);
1aa15def 227
70cd15e9 228 (void) pack_cpio(loaded_image,
f3b6f333 229 NULL,
845707aa
LP
230 L".cred",
231 (const CHAR8*) ".extra/credentials",
232 /* dir_mode= */ 0500,
233 /* access_mode= */ 0400,
234 /* tpm_pcr= */ TPM_PCR_INDEX_KERNEL_PARAMETERS,
235 L"Credentials initrd",
236 &credential_initrd,
237 &credential_initrd_size);
238
70cd15e9 239 (void) pack_cpio(loaded_image,
f3b6f333
AV
240 L"\\loader\\credentials",
241 L".cred",
242 (const CHAR8*) ".extra/global_credentials",
243 /* dir_mode= */ 0500,
244 /* access_mode= */ 0400,
245 /* tpm_pcr= */ TPM_PCR_INDEX_KERNEL_PARAMETERS,
246 L"Global credentials initrd",
247 &global_credential_initrd,
248 &global_credential_initrd_size);
249
250 (void) pack_cpio(loaded_image,
251 NULL,
845707aa
LP
252 L".raw",
253 (const CHAR8*) ".extra/sysext",
254 /* dir_mode= */ 0555,
255 /* access_mode= */ 0444,
256 /* tpm_pcr= */ TPM_PCR_INDEX_INITRD,
257 L"System extension initrd",
258 &sysext_initrd,
259 &sysext_initrd_size);
260
dc467928 261 linux_size = szs[SECTION_LINUX];
a0a644be 262 linux_base = POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[SECTION_LINUX];
5b5d365d
LP
263
264 initrd_size = szs[SECTION_INITRD];
a0a644be 265 initrd_base = initrd_size != 0 ? POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[SECTION_INITRD] : 0;
37fa3690 266
33bc9b75
MR
267 dt_size = szs[SECTION_DTB];
268 dt_base = dt_size != 0 ? POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[SECTION_DTB] : 0;
269
f3b6f333 270 if (credential_initrd || global_credential_initrd || sysext_initrd) {
845707aa
LP
271 /* If we have generated initrds dynamically, let's combine them with the built-in initrd. */
272 err = combine_initrd(
273 initrd_base, initrd_size,
274 credential_initrd, credential_initrd_size,
f3b6f333 275 global_credential_initrd, global_credential_initrd_size,
845707aa
LP
276 sysext_initrd, sysext_initrd_size,
277 &initrd_base, &initrd_size);
278 if (EFI_ERROR(err))
279 return err;
280
281 /* Given these might be large let's free them explicitly, quickly. */
f3b6f333
AV
282 credential_initrd = mfree(credential_initrd);
283 global_credential_initrd = mfree(global_credential_initrd);
284 sysext_initrd = mfree(sysext_initrd);
845707aa 285 }
0fa2cac4 286
33bc9b75
MR
287 if (dt_size > 0) {
288 err = devicetree_install_from_memory(
289 &dt_state, PHYSICAL_ADDRESS_TO_POINTER(dt_base), dt_size);
290 if (EFI_ERROR(err))
291 log_error_stall(L"Error loading embedded devicetree: %r", err);
292 }
293
a6089431 294 err = linux_exec(image, cmdline, cmdline_len,
dc467928 295 PHYSICAL_ADDRESS_TO_POINTER(linux_base), linux_size,
a6089431 296 PHYSICAL_ADDRESS_TO_POINTER(initrd_base), initrd_size);
37fa3690 297 graphics_mode(FALSE);
8aba0eec 298 return log_error_status_stall(err, L"Execution of embedded linux image failed: %r", err);
0fa2cac4 299}