]>
Commit | Line | Data |
---|---|---|
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 |
20 | static 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; | |
db4122d1 | 29 | uint8_t *p; |
845707aa LP |
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 | 36 | |
b4e7df4a | 37 | n = ALIGN4(initrd_size); /* main initrd might not be padded yet */ |
845707aa LP |
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); | |
2a5e4fe4 | 62 | if (err != EFI_SUCCESS) |
845707aa LP |
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 */ | |
bbc1f2ea | 71 | memcpy(p, PHYSICAL_ADDRESS_TO_POINTER(initrd_base), initrd_size); |
845707aa LP |
72 | p += initrd_size; |
73 | ||
b4e7df4a | 74 | pad = ALIGN4(initrd_size) - initrd_size; |
845707aa | 75 | if (pad > 0) { |
bbc1f2ea | 76 | memset(p, 0, pad); |
845707aa LP |
77 | p += pad; |
78 | } | |
79 | } | |
80 | ||
81 | if (credential_initrd) { | |
bbc1f2ea | 82 | memcpy(p, credential_initrd, credential_initrd_size); |
845707aa LP |
83 | p += credential_initrd_size; |
84 | } | |
85 | ||
f3b6f333 | 86 | if (global_credential_initrd) { |
bbc1f2ea | 87 | memcpy(p, global_credential_initrd, global_credential_initrd_size); |
f3b6f333 AV |
88 | p += global_credential_initrd_size; |
89 | } | |
90 | ||
845707aa | 91 | if (sysext_initrd) { |
bbc1f2ea | 92 | memcpy(p, sysext_initrd, sysext_initrd_size); |
845707aa LP |
93 | p += sysext_initrd_size; |
94 | } | |
95 | ||
db4122d1 | 96 | assert((uint8_t*) 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 | ||
b30a43df | 104 | static void export_variables(EFI_LOADED_IMAGE_PROTOCOL *loaded_image) { |
3639d1b0 | 105 | char16_t uuid[37]; |
5a186322 LP |
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) { | |
3639d1b0 | 122 | _cleanup_free_ char16_t *s = NULL; |
5a186322 LP |
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) { | |
3639d1b0 | 133 | _cleanup_free_ char16_t *s = NULL; |
ec4106af | 134 | s = xpool_print(L"%s %u.%02u", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); |
0a15a824 | 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) { | |
3639d1b0 | 140 | _cleanup_free_ char16_t *s = NULL; |
ec4106af | 141 | s = xpool_print(L"UEFI %u.%02u", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff); |
0a15a824 | 142 | efivar_set(LOADER_GUID, L"LoaderFirmwareType", s, 0); |
5a186322 LP |
143 | } |
144 | ||
24120e40 LP |
145 | /* add StubInfo (this is one is owned by the stub, hence we unconditionally override this with our |
146 | * own data) */ | |
147 | (void) efivar_set(LOADER_GUID, L"StubInfo", L"systemd-stub " GIT_VERSION, 0); | |
5a186322 LP |
148 | } |
149 | ||
0fa2cac4 | 150 | EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { |
e41d3d89 | 151 | |
72c97c19 | 152 | enum Section { |
16700cb8 | 153 | /* This is the canonical order in which we measure the sections. PLEASE DO NOT REORDER! */ |
e41d3d89 | 154 | SECTION_LINUX, |
16700cb8 LP |
155 | SECTION_OSREL, |
156 | SECTION_CMDLINE, | |
e41d3d89 LP |
157 | SECTION_INITRD, |
158 | SECTION_SPLASH, | |
33bc9b75 | 159 | SECTION_DTB, |
e41d3d89 LP |
160 | _SECTION_MAX, |
161 | }; | |
162 | ||
07d0fde4 | 163 | static const char * const sections[_SECTION_MAX + 1] = { |
07d0fde4 | 164 | [SECTION_LINUX] = ".linux", |
16700cb8 LP |
165 | [SECTION_OSREL] = ".osrel", |
166 | [SECTION_CMDLINE] = ".cmdline", | |
07d0fde4 JJ |
167 | [SECTION_INITRD] = ".initrd", |
168 | [SECTION_SPLASH] = ".splash", | |
169 | [SECTION_DTB] = ".dtb", | |
e41d3d89 | 170 | NULL, |
0fa2cac4 | 171 | }; |
5b5d365d | 172 | |
33bc9b75 | 173 | UINTN cmdline_len = 0, linux_size, initrd_size, dt_size; |
f3b6f333 | 174 | UINTN credential_initrd_size = 0, global_credential_initrd_size = 0, sysext_initrd_size = 0; |
93521e55 JJ |
175 | _cleanup_free_ void *credential_initrd = NULL, *global_credential_initrd = NULL; |
176 | _cleanup_free_ void *sysext_initrd = NULL; | |
33bc9b75 MR |
177 | EFI_PHYSICAL_ADDRESS linux_base, initrd_base, dt_base; |
178 | _cleanup_(devicetree_cleanup) struct devicetree_state dt_state = {}; | |
b30a43df | 179 | EFI_LOADED_IMAGE_PROTOCOL *loaded_image; |
e41d3d89 LP |
180 | UINTN addrs[_SECTION_MAX] = {}; |
181 | UINTN szs[_SECTION_MAX] = {}; | |
07d0fde4 JJ |
182 | char *cmdline = NULL; |
183 | _cleanup_free_ char *cmdline_owned = NULL; | |
72c97c19 | 184 | int sections_measured = -1, parameters_measured = -1; |
0fa2cac4 | 185 | EFI_STATUS err; |
599fe002 | 186 | bool m; |
0fa2cac4 KS |
187 | |
188 | InitializeLib(image, sys_table); | |
948d085e JJ |
189 | debug_hook(L"systemd-stub"); |
190 | /* Uncomment the next line if you need to wait for debugger. */ | |
191 | // debug_break(); | |
0fa2cac4 | 192 | |
12f32748 JJ |
193 | err = BS->OpenProtocol( |
194 | image, | |
195 | &LoadedImageProtocol, | |
196 | (void **)&loaded_image, | |
197 | image, | |
198 | NULL, | |
199 | EFI_OPEN_PROTOCOL_GET_PROTOCOL); | |
2a5e4fe4 | 200 | if (err != EFI_SUCCESS) |
8aba0eec | 201 | return log_error_status_stall(err, L"Error getting a LoadedImageProtocol handle: %r", err); |
0fa2cac4 | 202 | |
6028dd6d | 203 | err = pe_memory_locate_sections(loaded_image->ImageBase, sections, addrs, szs); |
2a5e4fe4 JJ |
204 | if (err != EFI_SUCCESS || szs[SECTION_LINUX] == 0) { |
205 | if (err == EFI_SUCCESS) | |
65ff3d26 | 206 | err = EFI_NOT_FOUND; |
8aba0eec | 207 | return log_error_status_stall(err, L"Unable to locate embedded .linux section: %r", err); |
65ff3d26 | 208 | } |
0fa2cac4 | 209 | |
72c97c19 LP |
210 | /* Measure all "payload" of this PE image into a separate PCR (i.e. where nothing else is written |
211 | * into so far), so that we have one PCR that we can nicely write policies against because it | |
212 | * contains all static data of this image, and thus can be easily be pre-calculated. */ | |
213 | for (enum Section section = 0; section < _SECTION_MAX; section++) { | |
214 | m = false; | |
215 | ||
216 | if (szs[section] == 0) /* not found */ | |
217 | continue; | |
218 | ||
219 | /* First measure the name of the section */ | |
220 | (void) tpm_log_event_ascii( | |
221 | TPM_PCR_INDEX_KERNEL_IMAGE, | |
222 | POINTER_TO_PHYSICAL_ADDRESS(sections[section]), | |
223 | strsize8(sections[section]), /* including NUL byte */ | |
224 | sections[section], | |
225 | &m); | |
226 | ||
227 | sections_measured = sections_measured < 0 ? m : (sections_measured && m); | |
228 | ||
229 | /* Then measure the data of the section */ | |
230 | (void) tpm_log_event_ascii( | |
231 | TPM_PCR_INDEX_KERNEL_IMAGE, | |
232 | POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[section], | |
233 | szs[section], | |
234 | sections[section], | |
235 | &m); | |
236 | ||
237 | sections_measured = sections_measured < 0 ? m : (sections_measured && m); | |
238 | } | |
239 | ||
240 | /* After we are done, set an EFI variable that tells userspace this was done successfully, and encode | |
241 | * in it which PCR was used. */ | |
242 | if (sections_measured > 0) | |
243 | (void) efivar_set_uint_string(LOADER_GUID, L"StubPcrKernelImage", TPM_PCR_INDEX_KERNEL_IMAGE, 0); | |
244 | ||
94b81afb | 245 | /* Show splash screen as early as possible */ |
db4122d1 | 246 | graphics_splash((const uint8_t*) loaded_image->ImageBase + addrs[SECTION_SPLASH], szs[SECTION_SPLASH], NULL); |
94b81afb | 247 | |
e41d3d89 | 248 | if (szs[SECTION_CMDLINE] > 0) { |
07d0fde4 | 249 | cmdline = (char *) loaded_image->ImageBase + addrs[SECTION_CMDLINE]; |
e41d3d89 LP |
250 | cmdline_len = szs[SECTION_CMDLINE]; |
251 | } | |
0fa2cac4 | 252 | |
53a20455 | 253 | /* if we are not in secure boot mode, or none was provided, accept a custom command line and replace the built-in one */ |
ce0f078f | 254 | if ((!secure_boot_enabled() || cmdline_len == 0) && loaded_image->LoadOptionsSize > 0 && |
3639d1b0 | 255 | *(char16_t *) loaded_image->LoadOptions > 0x1F) { |
07d0fde4 | 256 | cmdline_len = (loaded_image->LoadOptionsSize / sizeof(char16_t)) * sizeof(char); |
0af26643 | 257 | cmdline = cmdline_owned = xmalloc(cmdline_len); |
9f048123 | 258 | |
258f0970 | 259 | for (UINTN i = 0; i < cmdline_len; i++) |
3639d1b0 | 260 | cmdline[i] = ((char16_t *) loaded_image->LoadOptions)[i]; |
92ed3bb4 | 261 | |
e6e24af5 LP |
262 | /* Let's measure the passed kernel command line into the TPM. Note that this possibly |
263 | * duplicates what we already did in the boot menu, if that was already used. However, since | |
264 | * we want the boot menu to support an EFI binary, and want to this stub to be usable from | |
265 | * any boot menu, let's measure things anyway. */ | |
599fe002 LP |
266 | m = false; |
267 | (void) tpm_log_load_options(loaded_image->LoadOptions, &m); | |
268 | parameters_measured = m; | |
0fa2cac4 KS |
269 | } |
270 | ||
5a186322 | 271 | export_variables(loaded_image); |
1aa15def | 272 | |
599fe002 LP |
273 | if (pack_cpio(loaded_image, |
274 | NULL, | |
275 | L".cred", | |
276 | ".extra/credentials", | |
277 | /* dir_mode= */ 0500, | |
278 | /* access_mode= */ 0400, | |
279 | /* tpm_pcr= */ (uint32_t[]) { TPM_PCR_INDEX_KERNEL_PARAMETERS, TPM_PCR_INDEX_KERNEL_PARAMETERS_COMPAT }, | |
280 | /* n_tpm_pcr= */ 2, | |
281 | L"Credentials initrd", | |
282 | &credential_initrd, | |
283 | &credential_initrd_size, | |
284 | &m) == EFI_SUCCESS) | |
285 | parameters_measured = parameters_measured < 0 ? m : (parameters_measured && m); | |
286 | ||
287 | if (pack_cpio(loaded_image, | |
288 | L"\\loader\\credentials", | |
289 | L".cred", | |
290 | ".extra/global_credentials", | |
291 | /* dir_mode= */ 0500, | |
292 | /* access_mode= */ 0400, | |
293 | /* tpm_pcr= */ (uint32_t[]) { TPM_PCR_INDEX_KERNEL_PARAMETERS, TPM_PCR_INDEX_KERNEL_PARAMETERS_COMPAT }, | |
294 | /* n_tpm_pcr= */ 2, | |
295 | L"Global credentials initrd", | |
296 | &global_credential_initrd, | |
297 | &global_credential_initrd_size, | |
298 | &m) == EFI_SUCCESS) | |
299 | parameters_measured = parameters_measured < 0 ? m : (parameters_measured && m); | |
f3b6f333 AV |
300 | |
301 | (void) pack_cpio(loaded_image, | |
302 | NULL, | |
845707aa | 303 | L".raw", |
07d0fde4 | 304 | ".extra/sysext", |
845707aa LP |
305 | /* dir_mode= */ 0555, |
306 | /* access_mode= */ 0444, | |
db4122d1 | 307 | /* tpm_pcr= */ (uint32_t[]) { TPM_PCR_INDEX_INITRD }, |
4d32507f | 308 | /* n_tpm_pcr= */ 1, |
845707aa LP |
309 | L"System extension initrd", |
310 | &sysext_initrd, | |
599fe002 LP |
311 | &sysext_initrd_size, |
312 | NULL); | |
313 | ||
314 | if (parameters_measured > 0) | |
315 | (void) efivar_set_uint_string(LOADER_GUID, L"StubPcrKernelParameters", TPM_PCR_INDEX_KERNEL_PARAMETERS, 0); | |
845707aa | 316 | |
dc467928 | 317 | linux_size = szs[SECTION_LINUX]; |
a0a644be | 318 | linux_base = POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[SECTION_LINUX]; |
5b5d365d LP |
319 | |
320 | initrd_size = szs[SECTION_INITRD]; | |
a0a644be | 321 | initrd_base = initrd_size != 0 ? POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[SECTION_INITRD] : 0; |
37fa3690 | 322 | |
33bc9b75 MR |
323 | dt_size = szs[SECTION_DTB]; |
324 | dt_base = dt_size != 0 ? POINTER_TO_PHYSICAL_ADDRESS(loaded_image->ImageBase) + addrs[SECTION_DTB] : 0; | |
325 | ||
f3b6f333 | 326 | if (credential_initrd || global_credential_initrd || sysext_initrd) { |
845707aa LP |
327 | /* If we have generated initrds dynamically, let's combine them with the built-in initrd. */ |
328 | err = combine_initrd( | |
329 | initrd_base, initrd_size, | |
330 | credential_initrd, credential_initrd_size, | |
f3b6f333 | 331 | global_credential_initrd, global_credential_initrd_size, |
845707aa LP |
332 | sysext_initrd, sysext_initrd_size, |
333 | &initrd_base, &initrd_size); | |
2a5e4fe4 | 334 | if (err != EFI_SUCCESS) |
845707aa LP |
335 | return err; |
336 | ||
337 | /* Given these might be large let's free them explicitly, quickly. */ | |
f3b6f333 AV |
338 | credential_initrd = mfree(credential_initrd); |
339 | global_credential_initrd = mfree(global_credential_initrd); | |
340 | sysext_initrd = mfree(sysext_initrd); | |
845707aa | 341 | } |
0fa2cac4 | 342 | |
33bc9b75 MR |
343 | if (dt_size > 0) { |
344 | err = devicetree_install_from_memory( | |
345 | &dt_state, PHYSICAL_ADDRESS_TO_POINTER(dt_base), dt_size); | |
2a5e4fe4 | 346 | if (err != EFI_SUCCESS) |
33bc9b75 MR |
347 | log_error_stall(L"Error loading embedded devicetree: %r", err); |
348 | } | |
349 | ||
a6089431 | 350 | err = linux_exec(image, cmdline, cmdline_len, |
dc467928 | 351 | PHYSICAL_ADDRESS_TO_POINTER(linux_base), linux_size, |
a6089431 | 352 | PHYSICAL_ADDRESS_TO_POINTER(initrd_base), initrd_size); |
e5a1b8f9 | 353 | graphics_mode(false); |
8aba0eec | 354 | return log_error_status_stall(err, L"Execution of embedded linux image failed: %r", err); |
0fa2cac4 | 355 | } |