| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "device-path-util.h" |
| 4 |
#include "drivers.h" |
| 5 |
#include "efi-log.h" |
| 6 |
#include "string-util.h" |
| 7 |
#include "util.h" |
| 8 |
|
| 9 |
static EFI_STATUS load_one_driver( |
| 10 |
EFI_HANDLE parent_image, |
| 11 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 12 |
const char16_t *fname) { |
| 13 |
|
| 14 |
_cleanup_(unload_imagep) EFI_HANDLE image = NULL; |
| 15 |
_cleanup_free_ EFI_DEVICE_PATH *path = NULL; |
| 16 |
_cleanup_free_ char16_t *spath = NULL; |
| 17 |
EFI_STATUS err; |
| 18 |
|
| 19 |
assert(parent_image); |
| 20 |
assert(loaded_image); |
| 21 |
assert(fname); |
| 22 |
|
| 23 |
spath = xasprintf("\\EFI\\systemd\\drivers\\%ls", fname); |
| 24 |
err = make_file_device_path(loaded_image->DeviceHandle, spath, &path); |
| 25 |
if (err != EFI_SUCCESS) |
| 26 |
return log_error_status(err, "Error making file device path: %m"); |
| 27 |
|
| 28 |
err = BS->LoadImage(false, parent_image, path, NULL, 0, &image); |
| 29 |
if (err != EFI_SUCCESS) |
| 30 |
return log_error_status(err, "Failed to load image %ls: %m", fname); |
| 31 |
|
| 32 |
err = BS->HandleProtocol(image, MAKE_GUID_PTR(EFI_LOADED_IMAGE_PROTOCOL), (void **) &loaded_image); |
| 33 |
if (err != EFI_SUCCESS) |
| 34 |
return log_error_status(err, "Failed to find protocol in driver image %ls: %m", fname); |
| 35 |
|
| 36 |
if (loaded_image->ImageCodeType != EfiBootServicesCode && |
| 37 |
loaded_image->ImageCodeType != EfiRuntimeServicesCode) |
| 38 |
return log_error_status(EFI_INVALID_PARAMETER, "Image %ls is not a driver, refusing.", fname); |
| 39 |
|
| 40 |
err = BS->StartImage(image, NULL, NULL); |
| 41 |
if (err != EFI_SUCCESS) { |
| 42 |
/* EFI_ABORTED signals an initializing driver. It uses this error code on success |
| 43 |
* so that it is unloaded after. */ |
| 44 |
if (err != EFI_ABORTED) |
| 45 |
log_error_status(err, "Failed to start image %ls: %m", fname); |
| 46 |
return err; |
| 47 |
} |
| 48 |
|
| 49 |
TAKE_PTR(image); |
| 50 |
return EFI_SUCCESS; |
| 51 |
} |
| 52 |
|
| 53 |
EFI_STATUS reconnect_all_drivers(void) { |
| 54 |
_cleanup_free_ EFI_HANDLE *handles = NULL; |
| 55 |
size_t n_handles = 0; |
| 56 |
EFI_STATUS err; |
| 57 |
|
| 58 |
/* Reconnects all handles, so that any loaded drivers can take effect. */ |
| 59 |
|
| 60 |
err = BS->LocateHandleBuffer( |
| 61 |
AllHandles, |
| 62 |
/* Protocol= */ NULL, |
| 63 |
/* SearchKey= */ NULL, |
| 64 |
&n_handles, &handles); |
| 65 |
if (!IN_SET(err, EFI_SUCCESS, EFI_NOT_FOUND)) |
| 66 |
return log_error_status(err, "Failed to get list of handles: %m"); |
| 67 |
|
| 68 |
FOREACH_ARRAY(h, handles, n_handles) |
| 69 |
/* Some firmware gives us some bogus handles (or they might become bad due to |
| 70 |
* reconnecting everything). Security policy may also prevent us from doing so too. |
| 71 |
* There is nothing we can realistically do on errors anyways, so just ignore them. */ |
| 72 |
(void) BS->ConnectController( |
| 73 |
*h, |
| 74 |
/* DriverImageHandle= */ NULL, |
| 75 |
/* RemainingDevicePath= */ NULL, |
| 76 |
/* Recursive= */ true); |
| 77 |
|
| 78 |
return EFI_SUCCESS; |
| 79 |
} |
| 80 |
|
| 81 |
EFI_STATUS load_drivers( |
| 82 |
EFI_HANDLE parent_image, |
| 83 |
EFI_LOADED_IMAGE_PROTOCOL *loaded_image, |
| 84 |
EFI_FILE *root_dir) { |
| 85 |
|
| 86 |
_cleanup_file_close_ EFI_FILE *drivers_dir = NULL; |
| 87 |
_cleanup_free_ EFI_FILE_INFO *dirent = NULL; |
| 88 |
size_t dirent_size = 0, n_succeeded = 0; |
| 89 |
EFI_STATUS err; |
| 90 |
|
| 91 |
err = open_directory( |
| 92 |
root_dir, |
| 93 |
u"\\EFI\\systemd\\drivers", |
| 94 |
&drivers_dir); |
| 95 |
if (err == EFI_NOT_FOUND) |
| 96 |
return EFI_SUCCESS; |
| 97 |
if (err != EFI_SUCCESS) |
| 98 |
return log_error_status(err, "Failed to open \\EFI\\systemd\\drivers: %m"); |
| 99 |
|
| 100 |
for (;;) { |
| 101 |
err = readdir(drivers_dir, &dirent, &dirent_size); |
| 102 |
if (err != EFI_SUCCESS) |
| 103 |
return log_error_status(err, "Failed to read extra directory of loaded image: %m"); |
| 104 |
if (!dirent) /* End of directory */ |
| 105 |
break; |
| 106 |
|
| 107 |
if (dirent->FileName[0] == '.') |
| 108 |
continue; |
| 109 |
if (FLAGS_SET(dirent->Attribute, EFI_FILE_DIRECTORY)) |
| 110 |
continue; |
| 111 |
if (!endswith_no_case(dirent->FileName, EFI_MACHINE_TYPE_NAME u".efi")) |
| 112 |
continue; |
| 113 |
|
| 114 |
err = load_one_driver(parent_image, loaded_image, dirent->FileName); |
| 115 |
if (err != EFI_SUCCESS) |
| 116 |
continue; |
| 117 |
|
| 118 |
n_succeeded++; |
| 119 |
} |
| 120 |
|
| 121 |
if (n_succeeded > 0) |
| 122 |
(void) reconnect_all_drivers(); |
| 123 |
|
| 124 |
return EFI_SUCCESS; |
| 125 |
} |