]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/boot/efi/stub.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / boot / efi / stub.c
CommitLineData
0fa2cac4
KS
1/* This program is free software; you can redistribute it and/or modify it
2 * under the terms of the GNU Lesser General Public License as published by
3 * the Free Software Foundation; either version 2.1 of the License, or
4 * (at your option) any later version.
5 *
6 * This program is distributed in the hope that it will be useful, but
7 * WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * Lesser General Public License for more details.
10 *
11 * Copyright (C) 2015 Kay Sievers <kay@vrfy.org>
12 */
13
14#include <efi.h>
15#include <efilib.h>
16
8110e144 17#include "disk.h"
37fa3690 18#include "graphics.h"
0fa2cac4 19#include "linux.h"
d4cbada2
MG
20#include "measure.h"
21#include "pe.h"
cf0fbc49
TA
22#include "splash.h"
23#include "util.h"
0fa2cac4
KS
24
25/* magic string to find in the binary image */
948aaa7c 26static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " PACKAGE_VERSION " ####";
0fa2cac4
KS
27
28static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
29
30EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
31 EFI_LOADED_IMAGE *loaded_image;
0fa2cac4
KS
32 CHAR8 *b;
33 UINTN size;
34 BOOLEAN secure = FALSE;
35 CHAR8 *sections[] = {
36 (UINT8 *)".cmdline",
37 (UINT8 *)".linux",
38 (UINT8 *)".initrd",
37fa3690 39 (UINT8 *)".splash",
0fa2cac4
KS
40 NULL
41 };
42 UINTN addrs[ELEMENTSOF(sections)-1] = {};
43 UINTN offs[ELEMENTSOF(sections)-1] = {};
44 UINTN szs[ELEMENTSOF(sections)-1] = {};
45 CHAR8 *cmdline = NULL;
46 UINTN cmdline_len;
8110e144 47 CHAR16 uuid[37];
0fa2cac4
KS
48 EFI_STATUS err;
49
50 InitializeLib(image, sys_table);
51
52 err = uefi_call_wrapper(BS->OpenProtocol, 6, image, &LoadedImageProtocol, (VOID **)&loaded_image,
53 image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
54 if (EFI_ERROR(err)) {
55 Print(L"Error getting a LoadedImageProtocol handle: %r ", err);
56 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
57 return err;
58 }
59
0fa2cac4
KS
60 if (efivar_get_raw(&global_guid, L"SecureBoot", &b, &size) == EFI_SUCCESS) {
61 if (*b > 0)
62 secure = TRUE;
63 FreePool(b);
64 }
d4cbada2 65 err = pe_memory_locate_sections(loaded_image->ImageBase, sections, addrs, offs, szs);
0fa2cac4
KS
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 */
293b1673 78 if (!secure && loaded_image->LoadOptionsSize > 0 && *(CHAR16 *)loaded_image->LoadOptions != 0) {
0fa2cac4
KS
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;
92ed3bb4 89
349cc4a5 90#if ENABLE_TPM
7db5706e 91 /* Try to log any options to the TPM, especially manually edited options */
92ed3bb4
HH
92 err = tpm_log_event(SD_TPM_PCR,
93 (EFI_PHYSICAL_ADDRESS) 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);
522aa9f5 97 uefi_call_wrapper(BS->Stall, 1, 200 * 1000);
92ed3bb4
HH
98 }
99#endif
0fa2cac4
KS
100 }
101
8110e144
KS
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
37fa3690
KS
106 if (szs[3] > 0)
107 graphics_splash((UINT8 *)((UINTN)loaded_image->ImageBase + addrs[3]), szs[3], NULL);
108
0fa2cac4
KS
109 err = linux_exec(image, cmdline, cmdline_len,
110 (UINTN)loaded_image->ImageBase + addrs[1],
111 (UINTN)loaded_image->ImageBase + addrs[2], szs[2]);
112
37fa3690 113 graphics_mode(FALSE);
0fa2cac4
KS
114 Print(L"Execution of embedded linux image failed: %r\n", err);
115 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
116 return err;
117}