| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "boot-entry.h" |
| 8 |
#include "bootctl.h" |
| 9 |
#include "bootctl-util.h" |
| 10 |
#include "efivars.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "log.h" |
| 14 |
#include "pe-binary.h" |
| 15 |
#include "string-util.h" |
| 16 |
#include "sync-util.h" |
| 17 |
#include "virt.h" |
| 18 |
|
| 19 |
bool touch_variables(void) { |
| 20 |
/* If we run in a container or on a non-EFI system, automatically turn off EFI file system access, |
| 21 |
* unless explicitly overridden. */ |
| 22 |
|
| 23 |
if (arg_touch_variables >= 0) |
| 24 |
return set_efi_boot(arg_touch_variables); |
| 25 |
|
| 26 |
if (arg_root) { |
| 27 |
log_once(LOG_NOTICE, |
| 28 |
"Operating on %s, skipping EFI variable modifications.", |
| 29 |
arg_image ? "image" : "root directory"); |
| 30 |
return set_efi_boot(false); |
| 31 |
} |
| 32 |
|
| 33 |
if (!is_efi_boot()) { /* NB: this internally checks if we run in a container */ |
| 34 |
log_once(LOG_NOTICE, |
| 35 |
"Not booted with EFI or running in a container, skipping EFI variable modifications."); |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
int verify_touch_variables_allowed(const char *command) { |
| 43 |
/* Note: changing EFI variables is the primary purpose of these verbs, hence unlike in the other |
| 44 |
* verbs that might touch EFI variables where we skip things gracefully, here we fail loudly if we |
| 45 |
* are not run on EFI or EFI variable modifications were turned off. */ |
| 46 |
|
| 47 |
if (arg_touch_variables > 0) { |
| 48 |
/* If we explicitly allowed to touch EFI variables, then skip the is_efi_boot() checks used |
| 49 |
* at various places. */ |
| 50 |
set_efi_boot(true); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
if (arg_touch_variables == 0) |
| 55 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 56 |
"'%s' operation cannot be combined with --variables=no.", |
| 57 |
command); |
| 58 |
|
| 59 |
if (arg_root) |
| 60 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 61 |
"Acting on %s, refusing EFI variable setup.", |
| 62 |
arg_image ? "image" : "root directory"); |
| 63 |
|
| 64 |
if (detect_container() > 0) |
| 65 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 66 |
"'%s' operation not supported in a container.", |
| 67 |
command); |
| 68 |
|
| 69 |
if (!is_efi_boot()) |
| 70 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 71 |
"Not booted with UEFI."); |
| 72 |
|
| 73 |
if (access(EFIVAR_PATH(EFI_LOADER_VARIABLE_STR("LoaderInfo")), F_OK) < 0) { |
| 74 |
if (errno == ENOENT) { |
| 75 |
log_error_errno(errno, "Not booted with a supported boot loader."); |
| 76 |
return -EOPNOTSUPP; |
| 77 |
} |
| 78 |
|
| 79 |
return log_error_errno(errno, "Failed to detect whether boot loader supports '%s' operation: %m", command); |
| 80 |
} |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
int sync_everything(void) { |
| 86 |
int r = 0, k; |
| 87 |
|
| 88 |
if (arg_esp_path) { |
| 89 |
k = syncfs_path(AT_FDCWD, arg_esp_path); |
| 90 |
if (k < 0) |
| 91 |
RET_GATHER(r, log_error_errno(k, "Failed to synchronize the ESP '%s': %m", arg_esp_path)); |
| 92 |
} |
| 93 |
|
| 94 |
if (arg_xbootldr_path) { |
| 95 |
k = syncfs_path(AT_FDCWD, arg_xbootldr_path); |
| 96 |
if (k < 0) |
| 97 |
RET_GATHER(r, log_error_errno(k, "Failed to synchronize $BOOT '%s': %m", arg_xbootldr_path)); |
| 98 |
} |
| 99 |
|
| 100 |
return r; |
| 101 |
} |
| 102 |
|
| 103 |
const char* get_efi_arch(void) { |
| 104 |
/* Detect EFI firmware architecture of the running system. On mixed mode systems, it could be 32-bit |
| 105 |
* while the kernel is running in 64-bit. */ |
| 106 |
|
| 107 |
#ifdef __x86_64__ |
| 108 |
_cleanup_free_ char *platform_size = NULL; |
| 109 |
int r; |
| 110 |
|
| 111 |
r = read_one_line_file("/sys/firmware/efi/fw_platform_size", &platform_size); |
| 112 |
if (r == -ENOENT) |
| 113 |
return EFI_MACHINE_TYPE_NAME; |
| 114 |
if (r < 0) { |
| 115 |
log_warning_errno(r, |
| 116 |
"Error reading EFI firmware word size, assuming machine type '%s': %m", |
| 117 |
EFI_MACHINE_TYPE_NAME); |
| 118 |
return EFI_MACHINE_TYPE_NAME; |
| 119 |
} |
| 120 |
|
| 121 |
if (streq(platform_size, "64")) |
| 122 |
return EFI_MACHINE_TYPE_NAME; |
| 123 |
if (streq(platform_size, "32")) |
| 124 |
return "ia32"; |
| 125 |
|
| 126 |
log_warning( |
| 127 |
"Unknown EFI firmware word size '%s', using machine type '%s'.", |
| 128 |
platform_size, |
| 129 |
EFI_MACHINE_TYPE_NAME); |
| 130 |
#endif |
| 131 |
|
| 132 |
return EFI_MACHINE_TYPE_NAME; |
| 133 |
} |
| 134 |
|
| 135 |
int get_file_version(int fd, char **ret) { |
| 136 |
int r; |
| 137 |
|
| 138 |
assert(fd >= 0); |
| 139 |
assert(ret); |
| 140 |
|
| 141 |
/* Reads the version marker that systemd-boot/systemd-stub and friends store in their ".sdmagic" PE |
| 142 |
* section, i.e. a string such as "#### LoaderInfo: systemd-boot 218 ####", and returns the inner |
| 143 |
* part, e.g. "systemd-boot 218". Does not reposition the file offset (as it uses pread()). */ |
| 144 |
|
| 145 |
_cleanup_free_ IMAGE_DOS_HEADER *dos_header = NULL; |
| 146 |
_cleanup_free_ PeHeader *pe_header = NULL; |
| 147 |
r = pe_load_headers(fd, &dos_header, &pe_header); |
| 148 |
if (r == -EBADMSG) |
| 149 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "EFI binary is not a valid PE file, assuming no version information."); |
| 150 |
if (r < 0) |
| 151 |
return r; |
| 152 |
|
| 153 |
_cleanup_free_ IMAGE_SECTION_HEADER *sections = NULL; |
| 154 |
r = pe_load_sections(fd, dos_header, pe_header, §ions); |
| 155 |
if (r == -EBADMSG) |
| 156 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "Failed to load PE section table, assuming no version information."); |
| 157 |
if (r < 0) |
| 158 |
return r; |
| 159 |
|
| 160 |
_cleanup_free_ char *sdmagic = NULL; |
| 161 |
r = pe_read_section_data_by_name( |
| 162 |
fd, |
| 163 |
pe_header, |
| 164 |
sections, |
| 165 |
".sdmagic", |
| 166 |
/* max_size= */ 4U*1024U, |
| 167 |
(void**) &sdmagic, |
| 168 |
/* ret_size= */ NULL); |
| 169 |
if (IN_SET(r, -ENXIO, -EBADMSG)) |
| 170 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "EFI binary has no .sdmagic section, assuming no version information."); |
| 171 |
if (r < 0) |
| 172 |
return log_debug_errno(r, "Failed to read .sdmagic section of EFI binary: %m"); |
| 173 |
|
| 174 |
const char *p = startswith(sdmagic, "#### LoaderInfo: "); |
| 175 |
if (!p) |
| 176 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "EFI binary .sdmagic section lacks LoaderInfo marker."); |
| 177 |
|
| 178 |
const char *e = endswith(p, " ####"); |
| 179 |
if (!e || e <= p) |
| 180 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "EFI binary has malformed LoaderInfo marker."); |
| 181 |
|
| 182 |
char *marker = strndup(p, e - p); |
| 183 |
if (!marker) |
| 184 |
return log_oom_debug(); |
| 185 |
|
| 186 |
log_debug("EFI binary LoaderInfo marker: \"%s\"", marker); |
| 187 |
*ret = TAKE_PTR(marker); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
int settle_entry_token(void) { |
| 192 |
int r; |
| 193 |
|
| 194 |
r = boot_entry_token_ensure( |
| 195 |
arg_root, |
| 196 |
secure_getenv("KERNEL_INSTALL_CONF_ROOT"), |
| 197 |
arg_machine_id, |
| 198 |
/* machine_id_is_random= */ false, |
| 199 |
&arg_entry_token_type, |
| 200 |
&arg_entry_token); |
| 201 |
if (r < 0) |
| 202 |
return r; |
| 203 |
|
| 204 |
log_debug("Using entry token: %s", arg_entry_token); |
| 205 |
return 0; |
| 206 |
} |