]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/stub.c
c107a07ee7400c186902d438640a8480693ea35e
[thirdparty/systemd.git] / src / boot / efi / stub.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /* This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation; either version 2.1 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * Copyright (C) 2015 Kay Sievers <kay@vrfy.org>
13 */
14
15 #include <efi.h>
16 #include <efilib.h>
17
18 #include "disk.h"
19 #include "graphics.h"
20 #include "linux.h"
21 #include "measure.h"
22 #include "pe.h"
23 #include "splash.h"
24 #include "util.h"
25
26 /* magic string to find in the binary image */
27 static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " PACKAGE_VERSION " ####";
28
29 static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
30
31 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
32 EFI_LOADED_IMAGE *loaded_image;
33 _cleanup_freepool_ CHAR8 *b = NULL;
34 UINTN size;
35 BOOLEAN secure = FALSE;
36 CHAR8 *sections[] = {
37 (UINT8 *)".cmdline",
38 (UINT8 *)".linux",
39 (UINT8 *)".initrd",
40 (UINT8 *)".splash",
41 NULL
42 };
43 UINTN addrs[ELEMENTSOF(sections)-1] = {};
44 UINTN offs[ELEMENTSOF(sections)-1] = {};
45 UINTN szs[ELEMENTSOF(sections)-1] = {};
46 CHAR8 *cmdline = NULL;
47 UINTN cmdline_len;
48 CHAR16 uuid[37];
49 EFI_STATUS err;
50
51 InitializeLib(image, sys_table);
52
53 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
54 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
55 if (EFI_ERROR(err)) {
56 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
57 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
58 return err;
59 }
60
61 if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS)
62 if (*b > 0)
63 secure = TRUE;
64
65 err = pe_memory_locate_sections(loaded_image->ImageBase, sections, addrs, offs, szs);
66 if (EFI_ERROR(err)) {
67 Print(L"Unable to locate embedded .linux section: %r ", err);
68 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
69 return err;
70 }
71
72 if (szs[0] > 0)
73 cmdline = (CHAR8 *)(loaded_image->ImageBase + addrs[0]);
74
75 cmdline_len = szs[0];
76
77 /* if we are not in secure boot mode, accept a custom command line and replace the built-in one */
78 if (!secure && loaded_image->LoadOptionsSize > 0 && *(CHAR16 *)loaded_image->LoadOptions != 0) {
79 CHAR16 *options;
80 CHAR8 *line;
81 UINTN i;
82
83 options = (CHAR16 *)loaded_image->LoadOptions;
84 cmdline_len = (loaded_image->LoadOptionsSize / sizeof(CHAR16)) * sizeof(CHAR8);
85 line = AllocatePool(cmdline_len);
86 for (i = 0; i < cmdline_len; i++)
87 line[i] = options[i];
88 cmdline = line;
89
90 #if ENABLE_TPM
91 /* Try to log any options to the TPM, especially manually edited options */
92 err = tpm_log_event(SD_TPM_PCR,
93 (EFI_PHYSICAL_ADDRESS) (UINTN) loaded_image->LoadOptions,
94 loaded_image->LoadOptionsSize, loaded_image->LoadOptions);
95 if (EFI_ERROR(err)) {
96 Print(L"Unable to add image options measurement: %r", err);
97 uefi_call_wrapper(BS->Stall, 1, 200 * 1000);
98 }
99 #endif
100 }
101
102 /* export the device path this image is started from */
103 if (disk_get_part_uuid(loaded_image->DeviceHandle, uuid) == EFI_SUCCESS)
104 efivar_set(L"LoaderDevicePartUUID", uuid, FALSE);
105
106 /* if LoaderImageIdentifier is not set, assume the image with this stub was loaded directly from UEFI */
107 if (efivar_get_raw(&global_guid, L"LoaderImageIdentifier", &b, &size) != EFI_SUCCESS) {
108 _cleanup_freepool_ CHAR16 *s;
109
110 s = DevicePathToStr(loaded_image->FilePath);
111 efivar_set(L"LoaderImageIdentifier", s, FALSE);
112 }
113
114 /* if LoaderFirmwareInfo is not set, let's set it */
115 if (efivar_get_raw(&global_guid, L"LoaderFirmwareInfo", &b, &size) != EFI_SUCCESS) {
116 _cleanup_freepool_ CHAR16 *s;
117
118 s = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
119 efivar_set(L"LoaderFirmwareInfo", s, FALSE);
120 }
121
122 /* ditto for LoaderFirmwareType */
123 if (efivar_get_raw(&global_guid, L"LoaderFirmwareType", &b, &size) != EFI_SUCCESS) {
124 _cleanup_freepool_ CHAR16 *s;
125
126 s = PoolPrint(L"UEFI %d.%02d", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
127 efivar_set(L"LoaderFirmwareType", s, FALSE);
128 }
129
130 /* add StubInfo */
131 if (efivar_get_raw(&global_guid, L"StubInfo", &b, &size) != EFI_SUCCESS)
132 efivar_set(L"StubInfo", L"systemd-stub " PACKAGE_VERSION, FALSE);
133
134 if (szs[3] > 0)
135 graphics_splash((UINT8 *)((UINTN)loaded_image->ImageBase + addrs[3]), szs[3], NULL);
136
137 err = linux_exec(image, cmdline, cmdline_len,
138 (UINTN)loaded_image->ImageBase + addrs[1],
139 (UINTN)loaded_image->ImageBase + addrs[2], szs[2], secure);
140
141 graphics_mode(FALSE);
142 Print(L"Execution of embedded linux image failed: %r\n", err);
143 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
144 return err;
145 }