-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.c
221 lines (190 loc) · 6.05 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* EFI Boot Guard
*
* Copyright (c) Siemens AG, 2017
*
* Authors:
* Jan Kiszka <[email protected]>
* Andreas Reichel <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <efi.h>
#include <efilib.h>
#include <efiprot.h>
#include <efipciio.h>
#include <pci/header.h>
#include <bootguard.h>
#include <configuration.h>
#include "version.h"
#include "utils.h"
#include "loader_interface.h"
extern const unsigned long wdfuncs_start[];
extern const unsigned long wdfuncs_end[];
extern CHAR16 *boot_medium_path;
#define PCI_GET_VENDOR_ID(id) (UINT16)(id)
#define PCI_GET_PRODUCT_ID(id) (UINT16)((id) >> 16)
static EFI_STATUS probe_watchdogs(UINTN timeout)
{
if (wdfuncs_end - wdfuncs_start - 1 == 0) {
if (timeout > 0) {
ERROR(L"No watchdog drivers registered, but timeout is non-zero.\n");
return EFI_UNSUPPORTED;
}
return EFI_SUCCESS;
}
if (timeout == 0) {
WARNING(L"Watchdog is disabled.\n");
return EFI_SUCCESS;
}
UINTN handle_count = 0;
EFI_HANDLE *handle_buffer = NULL;
EFI_STATUS status = BS->LocateHandleBuffer(ByProtocol, &PciIoProtocol,
NULL, &handle_count,
&handle_buffer);
if (EFI_ERROR(status) || (handle_count == 0)) {
ERROR(L"No PCI I/O Protocol handles found.\n");
if (handle_buffer) {
FreePool(handle_buffer);
}
return EFI_UNSUPPORTED;
}
EFI_PCI_IO_PROTOCOL *pci_io;
UINT32 value;
for (UINTN index = 0; index < handle_count; index++) {
status = BS->OpenProtocol(handle_buffer[index], &PciIoProtocol,
(VOID **)&pci_io, this_image, NULL,
EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
if (EFI_ERROR(status)) {
ERROR(L"Cannot not open PciIoProtocol: %r\n", status);
FreePool(handle_buffer);
return status;
}
status = pci_io->Pci.Read(pci_io, EfiPciIoWidthUint32,
PCI_VENDOR_ID, 1, &value);
if (EFI_ERROR(status)) {
WARNING(
L"Cannot not read from PCI device, skipping: %r\n",
status);
(VOID) BS->CloseProtocol(handle_buffer[index],
&PciIoProtocol, this_image,
NULL);
continue;
}
const unsigned long *entry = wdfuncs_start;
for (entry++; entry < wdfuncs_end; entry++) {
WATCHDOG_PROBE probe = (WATCHDOG_PROBE) *entry;
if ((status = probe(pci_io, PCI_GET_VENDOR_ID(value),
PCI_GET_PRODUCT_ID(value),
timeout)) == EFI_SUCCESS) {
break;
}
}
(VOID) BS->CloseProtocol(handle_buffer[index], &PciIoProtocol,
this_image, NULL);
if (status == EFI_SUCCESS) {
break;
}
}
FreePool(handle_buffer);
return status;
}
EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
{
EFI_DEVICE_PATH *payload_dev_path;
EFI_LOADED_IMAGE *loaded_image;
EFI_HANDLE payload_handle;
EFI_STATUS status;
BG_STATUS bg_status;
BG_LOADER_PARAMS bg_loader_params;
BG_INTERFACE_PARAMS bg_interface_params;
CHAR16 *tmp;
ZeroMem(&bg_loader_params, sizeof(bg_loader_params));
this_image = image_handle;
InitializeLib(this_image, system_table);
#if !defined(SILENT_BOOT)
(VOID) ST->ConOut->ClearScreen(ST->ConOut);
PrintC(EFI_CYAN, L"EFI Boot Guard %s\n", L"" EFIBOOTGUARD_VERSION);
#endif
status = BS->OpenProtocol(this_image, &LoadedImageProtocol,
(VOID **)&loaded_image, this_image, NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (EFI_ERROR(status)) {
error_exit(L"Cannot open LoadedImageProtocol to get image information",
status);
}
tmp = DevicePathToStr(DevicePathFromHandle(loaded_image->DeviceHandle));
boot_medium_path = GetBootMediumPath(tmp);
FreePool(tmp);
INFO(L"Boot medium: %s\n", boot_medium_path);
status = get_volumes(&volumes, &volume_count);
if (EFI_ERROR(status)) {
error_exit(L"Cannot get volumes installed on system", status);
}
INFO(L"Loading configuration...\n");
bg_status = load_config(&bg_loader_params);
if (BG_ERROR(bg_status)) {
switch (bg_status) {
case BG_CONFIG_ERROR:
error_exit(L"Environment not set, cannot load config",
EFI_ABORTED);
break;
case BG_CONFIG_PARTIALLY_CORRUPTED:
WARNING(L"Config is partially corrupted. Please check.\n"
L"EFI Boot Guard will try to boot.\n");
break;
default:
error_exit(L"Unknown error occured while loading config",
EFI_ABORTED);
}
}
payload_dev_path = FileDevicePathFromConfig(
loaded_image->DeviceHandle, bg_loader_params.payload_path);
if (!payload_dev_path) {
error_exit(L"Cannot convert payload file path to device path",
EFI_OUT_OF_RESOURCES);
}
status = close_volumes(volumes, volume_count);
if (EFI_ERROR(status)) {
WARNING(L"Cannot close volumes.\n", status);
}
status = probe_watchdogs(bg_loader_params.timeout);
if (EFI_ERROR(status)) {
error_exit(L"Cannot probe watchdog", status);
}
/* Load and start image */
status = BS->LoadImage(TRUE, this_image, payload_dev_path, NULL, 0,
&payload_handle);
if (EFI_ERROR(status)) {
error_exit(L"Cannot load specified kernel image", status);
}
UINT16 *boot_medium_uuidstr =
disk_get_part_uuid(loaded_image->DeviceHandle);
bg_interface_params.loader_device_part_uuid = boot_medium_uuidstr;
status = set_bg_interface_vars(&bg_interface_params);
if (EFI_ERROR(status)) {
error_exit(L"Cannot set bootloader interface variables",
status);
}
INFO(L"LoaderDevicePartUUID=%s\n", boot_medium_uuidstr);
FreePool(boot_medium_uuidstr);
FreePool(payload_dev_path);
FreePool(boot_medium_path);
status = BS->OpenProtocol(payload_handle, &LoadedImageProtocol,
(VOID **)&loaded_image, this_image,
NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (EFI_ERROR(status)) {
error_exit(L"Cannot open LoadedImageProtocol to set kernel load options",
status);
}
loaded_image->LoadOptions = bg_loader_params.payload_options;
loaded_image->LoadOptionsSize =
(StrLen(bg_loader_params.payload_options) + 1) * sizeof(CHAR16);
INFO(L"Starting %s with watchdog set to %d seconds ...\n",
bg_loader_params.payload_path, bg_loader_params.timeout);
BS->Stall(1000 * 1000 * ENV_BOOT_DELAY);
return BS->StartImage(payload_handle, NULL, NULL);
}