| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "dirent-util.h" |
| 7 |
#include "efi-api.h" |
| 8 |
#include "efi.h" |
| 9 |
#include "efivars.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "fileio.h" |
| 12 |
#include "log.h" |
| 13 |
#include "parse-util.h" |
| 14 |
#include "sort-util.h" |
| 15 |
#include "stat-util.h" |
| 16 |
#include "stdio-util.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "tpm2-util.h" /* IWYU pragma: keep */ |
| 19 |
#include "utf8.h" |
| 20 |
|
| 21 |
#define EFI_TCG2_BOOT_HASH_ALG_SHA1 0x01 |
| 22 |
#define EFI_TCG2_BOOT_HASH_ALG_SHA256 0x02 |
| 23 |
#define EFI_TCG2_BOOT_HASH_ALG_SHA384 0x04 |
| 24 |
#define EFI_TCG2_BOOT_HASH_ALG_SHA512 0x08 |
| 25 |
|
| 26 |
#define LOAD_OPTION_ACTIVE 0x00000001 |
| 27 |
#define MEDIA_DEVICE_PATH 0x04 |
| 28 |
#define MEDIA_HARDDRIVE_DP 0x01 |
| 29 |
#define MEDIA_FILEPATH_DP 0x04 |
| 30 |
#define SIGNATURE_TYPE_GUID 0x02 |
| 31 |
#define MBR_TYPE_EFI_PARTITION_TABLE_HEADER 0x02 |
| 32 |
#define END_DEVICE_PATH_TYPE 0x7f |
| 33 |
#define END_ENTIRE_DEVICE_PATH_SUBTYPE 0xff |
| 34 |
|
| 35 |
#define EFI_OS_INDICATIONS_BOOT_TO_FW_UI UINT64_C(0x0000000000000001) |
| 36 |
|
| 37 |
#define boot_option__contents \ |
| 38 |
{ \ |
| 39 |
uint32_t attr; \ |
| 40 |
uint16_t path_len; \ |
| 41 |
uint16_t title[]; \ |
| 42 |
} |
| 43 |
|
| 44 |
struct boot_option boot_option__contents; |
| 45 |
struct boot_option__packed boot_option__contents _packed_; |
| 46 |
assert_cc(offsetof(struct boot_option, title) == offsetof(struct boot_option__packed, title)); |
| 47 |
/* sizeof(struct boot_option) != sizeof(struct boot_option__packed), so |
| 48 |
* the *size* of the structure should not be used anywhere below. */ |
| 49 |
|
| 50 |
struct drive_path { |
| 51 |
uint32_t part_nr; |
| 52 |
uint64_t part_start; |
| 53 |
uint64_t part_size; |
| 54 |
char signature[16]; |
| 55 |
uint8_t mbr_type; |
| 56 |
uint8_t signature_type; |
| 57 |
} _packed_; |
| 58 |
|
| 59 |
#define device_path__contents \ |
| 60 |
{ \ |
| 61 |
uint8_t type; \ |
| 62 |
uint8_t sub_type; \ |
| 63 |
uint16_t length; \ |
| 64 |
union { \ |
| 65 |
uint16_t path[0]; \ |
| 66 |
struct drive_path drive; \ |
| 67 |
}; \ |
| 68 |
} |
| 69 |
|
| 70 |
struct device_path device_path__contents; |
| 71 |
struct device_path__packed device_path__contents _packed_; |
| 72 |
assert_cc(sizeof(struct device_path) == sizeof(struct device_path__packed)); |
| 73 |
|
| 74 |
#if ENABLE_EFI |
| 75 |
static int get_os_indications(uint64_t *ret) { |
| 76 |
static struct stat cache_stat = {}; |
| 77 |
_cleanup_free_ void *v = NULL; |
| 78 |
static uint64_t cache; |
| 79 |
struct stat new_stat; |
| 80 |
size_t s; |
| 81 |
int r; |
| 82 |
|
| 83 |
assert(ret); |
| 84 |
|
| 85 |
/* Let's verify general support first */ |
| 86 |
r = efi_reboot_to_firmware_supported(); |
| 87 |
if (r < 0) |
| 88 |
return r; |
| 89 |
|
| 90 |
/* stat() the EFI variable, to see if the mtime changed. If it did we need to cache again. */ |
| 91 |
if (stat(EFIVAR_PATH(EFI_GLOBAL_VARIABLE_STR("OsIndications")), &new_stat) < 0) { |
| 92 |
if (errno != ENOENT) |
| 93 |
return -errno; |
| 94 |
|
| 95 |
/* Doesn't exist? Then we can exit early (also see below) */ |
| 96 |
*ret = 0; |
| 97 |
return 0; |
| 98 |
|
| 99 |
} else if (stat_inode_unmodified(&new_stat, &cache_stat)) { |
| 100 |
/* inode didn't change, we can return the cached value */ |
| 101 |
*ret = cache; |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
r = efi_get_variable(EFI_GLOBAL_VARIABLE_STR("OsIndications"), NULL, &v, &s); |
| 106 |
if (r == -ENOENT) { |
| 107 |
/* Some firmware implementations that do support OsIndications and report that with |
| 108 |
* OsIndicationsSupported will remove the OsIndications variable when it is unset. Let's |
| 109 |
* pretend it's 0 then, to hide this implementation detail. Note that this call will return |
| 110 |
* -ENOENT then only if the support for OsIndications is missing entirely, as determined by |
| 111 |
* efi_reboot_to_firmware_supported() above. */ |
| 112 |
*ret = 0; |
| 113 |
return 0; |
| 114 |
} |
| 115 |
if (r < 0) |
| 116 |
return r; |
| 117 |
if (s != sizeof(uint64_t)) |
| 118 |
return -EINVAL; |
| 119 |
|
| 120 |
cache_stat = new_stat; |
| 121 |
*ret = cache = *(uint64_t *)v; |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static ssize_t utf16_size(const uint16_t *s, size_t buf_len_bytes) { |
| 126 |
size_t l = 0; |
| 127 |
|
| 128 |
/* Returns the size of the string in bytes without the terminating two zero bytes */ |
| 129 |
|
| 130 |
while (l < buf_len_bytes / sizeof(uint16_t)) { |
| 131 |
if (s[l] == 0) |
| 132 |
return (l + 1) * sizeof(uint16_t); |
| 133 |
l++; |
| 134 |
} |
| 135 |
|
| 136 |
return -EINVAL; /* The terminator was not found */ |
| 137 |
} |
| 138 |
|
| 139 |
static void to_utf16(uint16_t *dest, const char *src) { |
| 140 |
int i; |
| 141 |
|
| 142 |
for (i = 0; src[i] != '\0'; i++) |
| 143 |
dest[i] = src[i]; |
| 144 |
dest[i] = '\0'; |
| 145 |
} |
| 146 |
|
| 147 |
static uint16_t *tilt_slashes(uint16_t *s) { |
| 148 |
for (uint16_t *p = s; *p; p++) |
| 149 |
if (*p == '/') |
| 150 |
*p = '\\'; |
| 151 |
|
| 152 |
return s; |
| 153 |
} |
| 154 |
|
| 155 |
static int boot_id_hex(const char s[static 4]) { |
| 156 |
int id = 0; |
| 157 |
|
| 158 |
assert(s); |
| 159 |
|
| 160 |
for (int i = 0; i < 4; i++) |
| 161 |
if (s[i] >= '0' && s[i] <= '9') |
| 162 |
id |= (s[i] - '0') << (3 - i) * 4; |
| 163 |
else if (s[i] >= 'A' && s[i] <= 'F') |
| 164 |
id |= (s[i] - 'A' + 10) << (3 - i) * 4; |
| 165 |
else |
| 166 |
return -EINVAL; |
| 167 |
|
| 168 |
return id; |
| 169 |
} |
| 170 |
#endif |
| 171 |
|
| 172 |
int efi_reboot_to_firmware_supported(void) { |
| 173 |
#if ENABLE_EFI |
| 174 |
_cleanup_free_ void *v = NULL; |
| 175 |
static int cache = -1; |
| 176 |
uint64_t b; |
| 177 |
size_t s; |
| 178 |
int r; |
| 179 |
|
| 180 |
if (cache > 0) |
| 181 |
return 0; |
| 182 |
if (cache == 0) |
| 183 |
return -EOPNOTSUPP; |
| 184 |
|
| 185 |
if (!is_efi_boot()) |
| 186 |
goto not_supported; |
| 187 |
|
| 188 |
r = efi_get_variable(EFI_GLOBAL_VARIABLE_STR("OsIndicationsSupported"), NULL, &v, &s); |
| 189 |
if (r == -ENOENT) |
| 190 |
goto not_supported; /* variable doesn't exist? it's not supported then */ |
| 191 |
if (r < 0) |
| 192 |
return r; |
| 193 |
if (s != sizeof(uint64_t)) |
| 194 |
return -EINVAL; |
| 195 |
|
| 196 |
b = *(uint64_t*) v; |
| 197 |
if (!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI)) |
| 198 |
goto not_supported; /* bit unset? it's not supported then */ |
| 199 |
|
| 200 |
cache = 1; |
| 201 |
return 0; |
| 202 |
|
| 203 |
not_supported: |
| 204 |
cache = 0; |
| 205 |
return -EOPNOTSUPP; |
| 206 |
#else |
| 207 |
return -EOPNOTSUPP; |
| 208 |
#endif |
| 209 |
} |
| 210 |
|
| 211 |
int efi_get_reboot_to_firmware(void) { |
| 212 |
#if ENABLE_EFI |
| 213 |
int r; |
| 214 |
uint64_t b; |
| 215 |
|
| 216 |
r = get_os_indications(&b); |
| 217 |
if (r < 0) |
| 218 |
return r; |
| 219 |
|
| 220 |
return !!(b & EFI_OS_INDICATIONS_BOOT_TO_FW_UI); |
| 221 |
#else |
| 222 |
return -EOPNOTSUPP; |
| 223 |
#endif |
| 224 |
} |
| 225 |
|
| 226 |
int efi_set_reboot_to_firmware(bool value) { |
| 227 |
#if ENABLE_EFI |
| 228 |
int r; |
| 229 |
uint64_t b, b_new; |
| 230 |
|
| 231 |
r = get_os_indications(&b); |
| 232 |
if (r < 0) |
| 233 |
return r; |
| 234 |
|
| 235 |
b_new = UPDATE_FLAG(b, EFI_OS_INDICATIONS_BOOT_TO_FW_UI, value); |
| 236 |
|
| 237 |
/* Avoid writing to efi vars store if we can due to firmware bugs. */ |
| 238 |
if (b != b_new) |
| 239 |
return efi_set_variable(EFI_GLOBAL_VARIABLE_STR("OsIndications"), &b_new, sizeof(uint64_t)); |
| 240 |
|
| 241 |
return 0; |
| 242 |
#else |
| 243 |
return -EOPNOTSUPP; |
| 244 |
#endif |
| 245 |
} |
| 246 |
|
| 247 |
int efi_get_boot_option( |
| 248 |
uint16_t id, |
| 249 |
char **ret_title, |
| 250 |
sd_id128_t *ret_part_uuid, |
| 251 |
char **ret_path, |
| 252 |
bool *ret_active) { |
| 253 |
#if ENABLE_EFI |
| 254 |
char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1]; |
| 255 |
_cleanup_free_ uint8_t *buf = NULL; |
| 256 |
size_t l; |
| 257 |
struct boot_option *header; |
| 258 |
ssize_t title_size; |
| 259 |
_cleanup_free_ char *s = NULL, *p = NULL; |
| 260 |
sd_id128_t p_uuid = SD_ID128_NULL; |
| 261 |
int r; |
| 262 |
|
| 263 |
if (!is_efi_boot()) |
| 264 |
return -EOPNOTSUPP; |
| 265 |
|
| 266 |
xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id); |
| 267 |
r = efi_get_variable(variable, NULL, (void **)&buf, &l); |
| 268 |
if (r < 0) |
| 269 |
return r; |
| 270 |
if (l < offsetof(struct boot_option, title)) |
| 271 |
return -ENOENT; |
| 272 |
|
| 273 |
header = (struct boot_option *)buf; |
| 274 |
title_size = utf16_size(header->title, l - offsetof(struct boot_option, title)); |
| 275 |
if (title_size < 0) |
| 276 |
return title_size; |
| 277 |
|
| 278 |
if (ret_title) { |
| 279 |
s = utf16_to_utf8(header->title, title_size); |
| 280 |
if (!s) |
| 281 |
return -ENOMEM; |
| 282 |
} |
| 283 |
|
| 284 |
if (header->path_len > 0) { |
| 285 |
uint8_t *dbuf; |
| 286 |
size_t dnext, doff; |
| 287 |
|
| 288 |
doff = offsetof(struct boot_option, title) + title_size; |
| 289 |
dbuf = buf + doff; |
| 290 |
if (header->path_len > l - doff) |
| 291 |
return -EINVAL; |
| 292 |
|
| 293 |
dnext = 0; |
| 294 |
while (dnext < header->path_len) { |
| 295 |
struct device_path *dpath; |
| 296 |
|
| 297 |
dpath = (struct device_path *)(dbuf + dnext); |
| 298 |
if (dpath->length < 4) |
| 299 |
break; |
| 300 |
|
| 301 |
/* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */ |
| 302 |
if (dpath->type == END_DEVICE_PATH_TYPE && dpath->sub_type == END_ENTIRE_DEVICE_PATH_SUBTYPE) |
| 303 |
break; |
| 304 |
|
| 305 |
dnext += dpath->length; |
| 306 |
|
| 307 |
/* Type 0x04 – Media Device Path */ |
| 308 |
if (dpath->type != MEDIA_DEVICE_PATH) |
| 309 |
continue; |
| 310 |
|
| 311 |
/* Sub-Type 1 – Hard Drive */ |
| 312 |
if (dpath->sub_type == MEDIA_HARDDRIVE_DP) { |
| 313 |
/* 0x02 – GUID Partition Table */ |
| 314 |
if (dpath->drive.mbr_type != MBR_TYPE_EFI_PARTITION_TABLE_HEADER) |
| 315 |
continue; |
| 316 |
|
| 317 |
/* 0x02 – GUID signature */ |
| 318 |
if (dpath->drive.signature_type != SIGNATURE_TYPE_GUID) |
| 319 |
continue; |
| 320 |
|
| 321 |
if (ret_part_uuid) |
| 322 |
p_uuid = efi_guid_to_id128(dpath->drive.signature); |
| 323 |
continue; |
| 324 |
} |
| 325 |
|
| 326 |
/* Sub-Type 4 – File Path */ |
| 327 |
if (dpath->sub_type == MEDIA_FILEPATH_DP && !p && ret_path) { |
| 328 |
p = utf16_to_utf8(dpath->path, dpath->length-4); |
| 329 |
if (!p) |
| 330 |
return -ENOMEM; |
| 331 |
|
| 332 |
efi_tilt_backslashes(p); |
| 333 |
continue; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if (ret_title) |
| 339 |
*ret_title = TAKE_PTR(s); |
| 340 |
if (ret_part_uuid) |
| 341 |
*ret_part_uuid = p_uuid; |
| 342 |
if (ret_path) |
| 343 |
*ret_path = TAKE_PTR(p); |
| 344 |
if (ret_active) |
| 345 |
*ret_active = header->attr & LOAD_OPTION_ACTIVE; |
| 346 |
|
| 347 |
return 0; |
| 348 |
#else |
| 349 |
return -EOPNOTSUPP; |
| 350 |
#endif |
| 351 |
} |
| 352 |
|
| 353 |
int efi_add_boot_option( |
| 354 |
uint16_t id, |
| 355 |
const char *title, |
| 356 |
uint32_t part, |
| 357 |
uint64_t pstart, |
| 358 |
uint64_t psize, |
| 359 |
sd_id128_t part_uuid, |
| 360 |
const char *path) { |
| 361 |
#if ENABLE_EFI |
| 362 |
size_t size, title_len, path_len; |
| 363 |
_cleanup_free_ char *buf = NULL; |
| 364 |
struct boot_option *option; |
| 365 |
struct device_path *devicep; |
| 366 |
char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1]; |
| 367 |
|
| 368 |
if (!is_efi_boot()) |
| 369 |
return -EOPNOTSUPP; |
| 370 |
|
| 371 |
title_len = (strlen(title)+1) * 2; |
| 372 |
path_len = (strlen(path)+1) * 2; |
| 373 |
|
| 374 |
buf = malloc0(offsetof(struct boot_option, title) + title_len + |
| 375 |
sizeof(struct drive_path) + |
| 376 |
sizeof(struct device_path) + path_len); |
| 377 |
if (!buf) |
| 378 |
return -ENOMEM; |
| 379 |
|
| 380 |
/* header */ |
| 381 |
option = (struct boot_option *)buf; |
| 382 |
option->attr = LOAD_OPTION_ACTIVE; |
| 383 |
option->path_len = offsetof(struct device_path, drive) + sizeof(struct drive_path) + |
| 384 |
offsetof(struct device_path, path) + path_len + |
| 385 |
offsetof(struct device_path, path); |
| 386 |
to_utf16(option->title, title); |
| 387 |
size = offsetof(struct boot_option, title) + title_len; |
| 388 |
|
| 389 |
/* partition info */ |
| 390 |
devicep = (struct device_path *)(buf + size); |
| 391 |
devicep->type = MEDIA_DEVICE_PATH; |
| 392 |
devicep->sub_type = MEDIA_HARDDRIVE_DP; |
| 393 |
devicep->length = offsetof(struct device_path, drive) + sizeof(struct drive_path); |
| 394 |
memcpy(&devicep->drive.part_nr, &part, sizeof(uint32_t)); |
| 395 |
memcpy(&devicep->drive.part_start, &pstart, sizeof(uint64_t)); |
| 396 |
memcpy(&devicep->drive.part_size, &psize, sizeof(uint64_t)); |
| 397 |
efi_id128_to_guid(part_uuid, devicep->drive.signature); |
| 398 |
devicep->drive.mbr_type = MBR_TYPE_EFI_PARTITION_TABLE_HEADER; |
| 399 |
devicep->drive.signature_type = SIGNATURE_TYPE_GUID; |
| 400 |
size += devicep->length; |
| 401 |
|
| 402 |
/* path to loader */ |
| 403 |
devicep = (struct device_path *)(buf + size); |
| 404 |
devicep->type = MEDIA_DEVICE_PATH; |
| 405 |
devicep->sub_type = MEDIA_FILEPATH_DP; |
| 406 |
devicep->length = offsetof(struct device_path, path) + path_len; |
| 407 |
to_utf16(devicep->path, path); |
| 408 |
tilt_slashes(devicep->path); |
| 409 |
size += devicep->length; |
| 410 |
|
| 411 |
/* end of path */ |
| 412 |
devicep = (struct device_path *)(buf + size); |
| 413 |
devicep->type = END_DEVICE_PATH_TYPE; |
| 414 |
devicep->sub_type = END_ENTIRE_DEVICE_PATH_SUBTYPE; |
| 415 |
devicep->length = offsetof(struct device_path, path); |
| 416 |
size += devicep->length; |
| 417 |
|
| 418 |
xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id); |
| 419 |
return efi_set_variable(variable, buf, size); |
| 420 |
#else |
| 421 |
return -EOPNOTSUPP; |
| 422 |
#endif |
| 423 |
} |
| 424 |
|
| 425 |
int efi_remove_boot_option(uint16_t id) { |
| 426 |
#if ENABLE_EFI |
| 427 |
char variable[STRLEN(EFI_GLOBAL_VARIABLE_STR("Boot")) + 4 + 1]; |
| 428 |
|
| 429 |
if (!is_efi_boot()) |
| 430 |
return -EOPNOTSUPP; |
| 431 |
|
| 432 |
xsprintf(variable, EFI_GLOBAL_VARIABLE_STR("Boot%04X"), id); |
| 433 |
return efi_set_variable(variable, NULL, 0); |
| 434 |
#else |
| 435 |
return -EOPNOTSUPP; |
| 436 |
#endif |
| 437 |
} |
| 438 |
|
| 439 |
int efi_get_boot_order(uint16_t **ret_order) { |
| 440 |
#if ENABLE_EFI |
| 441 |
_cleanup_free_ void *buf = NULL; |
| 442 |
size_t l; |
| 443 |
int r; |
| 444 |
|
| 445 |
assert(ret_order); |
| 446 |
|
| 447 |
if (!is_efi_boot()) |
| 448 |
return -EOPNOTSUPP; |
| 449 |
|
| 450 |
r = efi_get_variable(EFI_GLOBAL_VARIABLE_STR("BootOrder"), NULL, &buf, &l); |
| 451 |
if (r < 0) |
| 452 |
return r; |
| 453 |
|
| 454 |
if (l <= 0) |
| 455 |
return -ENOENT; |
| 456 |
|
| 457 |
if (l % sizeof(uint16_t) > 0 || |
| 458 |
l / sizeof(uint16_t) > INT_MAX) |
| 459 |
return -EINVAL; |
| 460 |
|
| 461 |
*ret_order = TAKE_PTR(buf); |
| 462 |
return (int) (l / sizeof(uint16_t)); |
| 463 |
#else |
| 464 |
return -EOPNOTSUPP; |
| 465 |
#endif |
| 466 |
} |
| 467 |
|
| 468 |
int efi_set_boot_order(const uint16_t *order, size_t n) { |
| 469 |
#if ENABLE_EFI |
| 470 |
if (!is_efi_boot()) |
| 471 |
return -EOPNOTSUPP; |
| 472 |
|
| 473 |
return efi_set_variable(EFI_GLOBAL_VARIABLE_STR("BootOrder"), order, n * sizeof(uint16_t)); |
| 474 |
#else |
| 475 |
return -EOPNOTSUPP; |
| 476 |
#endif |
| 477 |
} |
| 478 |
|
| 479 |
int efi_get_boot_options(uint16_t **ret_options) { |
| 480 |
#if ENABLE_EFI |
| 481 |
_cleanup_closedir_ DIR *dir = NULL; |
| 482 |
_cleanup_free_ uint16_t *list = NULL; |
| 483 |
int count = 0; |
| 484 |
|
| 485 |
assert(ret_options); |
| 486 |
|
| 487 |
if (!is_efi_boot()) |
| 488 |
return -EOPNOTSUPP; |
| 489 |
|
| 490 |
dir = opendir(EFIVAR_PATH(".")); |
| 491 |
if (!dir) |
| 492 |
return -errno; |
| 493 |
|
| 494 |
FOREACH_DIRENT(de, dir, return -errno) { |
| 495 |
int id; |
| 496 |
|
| 497 |
if (!startswith(de->d_name, "Boot")) |
| 498 |
continue; |
| 499 |
|
| 500 |
if (strlen(de->d_name) != 45) |
| 501 |
continue; |
| 502 |
|
| 503 |
if (!streq(de->d_name + 8, EFI_GLOBAL_VARIABLE_STR(""))) /* generate variable suffix using macro */ |
| 504 |
continue; |
| 505 |
|
| 506 |
id = boot_id_hex(de->d_name + 4); |
| 507 |
if (id < 0) |
| 508 |
continue; |
| 509 |
|
| 510 |
if (!GREEDY_REALLOC(list, count + 1)) |
| 511 |
return -ENOMEM; |
| 512 |
|
| 513 |
list[count++] = id; |
| 514 |
} |
| 515 |
|
| 516 |
typesafe_qsort(list, count, cmp_uint16); |
| 517 |
|
| 518 |
*ret_options = TAKE_PTR(list); |
| 519 |
|
| 520 |
return count; |
| 521 |
#else |
| 522 |
return -EOPNOTSUPP; |
| 523 |
#endif |
| 524 |
} |
| 525 |
|
| 526 |
int efi_get_active_pcr_banks(uint32_t *ret) { |
| 527 |
#if ENABLE_EFI |
| 528 |
static uint32_t cache = 0; |
| 529 |
static bool cache_valid = false; |
| 530 |
int r; |
| 531 |
|
| 532 |
/* Returns the enabled PCR banks as bitmask, as reported by firmware. If the bitmask is returned as |
| 533 |
* UINT32_MAX, the firmware supports the TCG protocol, but in a version too old to report this |
| 534 |
* information. */ |
| 535 |
|
| 536 |
if (!cache_valid) { |
| 537 |
_cleanup_free_ char *active_pcr_banks = NULL; |
| 538 |
r = efi_get_variable_string(EFI_LOADER_VARIABLE_STR("LoaderTpm2ActivePcrBanks"), &active_pcr_banks); |
| 539 |
if (r < 0) |
| 540 |
return log_debug_errno(r, "Failed to read LoaderTpm2ActivePcrBanks variable: %m"); |
| 541 |
|
| 542 |
uint32_t efi_bits; |
| 543 |
r = safe_atou32_full(active_pcr_banks, 16, &efi_bits); |
| 544 |
if (r < 0) |
| 545 |
return log_debug_errno(r, "Failed to parse LoaderTpm2ActivePcrBanks variable: %m"); |
| 546 |
|
| 547 |
if (efi_bits == UINT32_MAX) |
| 548 |
/* UINT32_MAX means that the firmware API doesn't implement GetActivePcrBanks() and caller must guess */ |
| 549 |
cache = UINT32_MAX; |
| 550 |
else { |
| 551 |
/* EFI TPM protocol uses different bit values for the hash algorithms, let's convert */ |
| 552 |
static const struct { |
| 553 |
uint32_t efi; |
| 554 |
uint32_t tcg; |
| 555 |
} table[] = { |
| 556 |
{ EFI_TCG2_BOOT_HASH_ALG_SHA1, 1U << TPM2_ALG_SHA1 }, |
| 557 |
{ EFI_TCG2_BOOT_HASH_ALG_SHA256, 1U << TPM2_ALG_SHA256 }, |
| 558 |
{ EFI_TCG2_BOOT_HASH_ALG_SHA384, 1U << TPM2_ALG_SHA384 }, |
| 559 |
{ EFI_TCG2_BOOT_HASH_ALG_SHA512, 1U << TPM2_ALG_SHA512 }, |
| 560 |
}; |
| 561 |
|
| 562 |
uint32_t tcg_bits = 0; |
| 563 |
FOREACH_ELEMENT(t, table) |
| 564 |
SET_FLAG(tcg_bits, t->tcg, efi_bits & t->efi); |
| 565 |
|
| 566 |
cache = tcg_bits; |
| 567 |
} |
| 568 |
|
| 569 |
cache_valid = true; |
| 570 |
} |
| 571 |
|
| 572 |
if (ret) |
| 573 |
*ret = cache; |
| 574 |
|
| 575 |
return 0; |
| 576 |
#else |
| 577 |
return -EOPNOTSUPP; |
| 578 |
#endif |
| 579 |
} |
| 580 |
|
| 581 |
#if ENABLE_EFI |
| 582 |
static int loader_has_tpm2(void) { |
| 583 |
uint32_t active_pcr_banks; |
| 584 |
int r; |
| 585 |
|
| 586 |
r = efi_get_active_pcr_banks(&active_pcr_banks); |
| 587 |
if (r < 0) |
| 588 |
return r; |
| 589 |
|
| 590 |
return active_pcr_banks != 0; |
| 591 |
} |
| 592 |
#endif |
| 593 |
|
| 594 |
bool efi_has_tpm2(void) { |
| 595 |
#if ENABLE_EFI |
| 596 |
static int cache = -1; |
| 597 |
int r; |
| 598 |
|
| 599 |
/* Returns whether the system has a TPM2 chip which is known to the EFI firmware. */ |
| 600 |
|
| 601 |
if (cache >= 0) |
| 602 |
return cache; |
| 603 |
|
| 604 |
/* First, check if we are on an EFI boot at all. */ |
| 605 |
if (!is_efi_boot()) |
| 606 |
return (cache = false); |
| 607 |
|
| 608 |
/* Secondly, check if the loader told us, as that is the most accurate source of information |
| 609 |
* regarding the firmware's setup */ |
| 610 |
r = loader_has_tpm2(); |
| 611 |
if (r >= 0) |
| 612 |
return (cache = r); |
| 613 |
|
| 614 |
/* Then, check if the ACPI table "TPM2" exists, which is the TPM2 event log table, see: |
| 615 |
* https://trustedcomputinggroup.org/wp-content/uploads/TCG_ACPIGeneralSpecification_v1.20_r8.pdf |
| 616 |
* This table exists whenever the firmware knows ACPI and is hooked up to TPM2. |
| 617 |
* Note that in some cases, for example with EDK2 2025.2 with the default arm64 config, this ACPI |
| 618 |
* table is present even if TPM2 support is not enabled in the firmware. */ |
| 619 |
if (access("/sys/firmware/acpi/tables/TPM2", F_OK) >= 0) |
| 620 |
return (cache = true); |
| 621 |
if (errno != ENOENT) |
| 622 |
log_debug_errno(errno, "Unable to test whether /sys/firmware/acpi/tables/TPM2 exists, assuming it doesn't: %m"); |
| 623 |
|
| 624 |
/* As the last try, check if the EFI firmware provides the EFI_TCG2_FINAL_EVENTS_TABLE |
| 625 |
* stored in EFI configuration table, see: |
| 626 |
* |
| 627 |
* https://trustedcomputinggroup.org/wp-content/uploads/EFI-Protocol-Specification-rev13-160330final.pdf */ |
| 628 |
if (access("/sys/kernel/security/tpm0/binary_bios_measurements", F_OK) >= 0) { |
| 629 |
_cleanup_free_ char *major = NULL; |
| 630 |
|
| 631 |
/* The EFI table might exist for TPM 1.2 as well, hence let's check explicitly which TPM version we are looking at here. */ |
| 632 |
r = read_virtual_file("/sys/class/tpm/tpm0/tpm_version_major", SIZE_MAX, &major, /* ret_size= */ NULL); |
| 633 |
if (r >= 0) |
| 634 |
return (cache = streq(strstrip(major), "2")); |
| 635 |
|
| 636 |
log_debug_errno(r, "Unable to read /sys/class/tpm/tpm0/tpm_version_major, assuming TPM does not qualify as TPM2: %m"); |
| 637 |
|
| 638 |
} else if (errno != ENOENT) |
| 639 |
log_debug_errno(errno, "Unable to test whether /sys/kernel/security/tpm0/binary_bios_measurements exists, assuming it doesn't: %m"); |
| 640 |
|
| 641 |
return (cache = false); |
| 642 |
#else |
| 643 |
return -EOPNOTSUPP; |
| 644 |
#endif |
| 645 |
} |
| 646 |
|
| 647 |
sd_id128_t efi_guid_to_id128(const void *guid) { |
| 648 |
EFI_GUID uuid; |
| 649 |
sd_id128_t id128; |
| 650 |
|
| 651 |
/* The input pointer is not guaranteed to be aligned (e.g. when pointing into a serialized EFI |
| 652 |
* variable buffer), so copy into a properly aligned local first. */ |
| 653 |
memcpy(&uuid, ASSERT_PTR(guid), sizeof(uuid)); |
| 654 |
|
| 655 |
id128.bytes[0] = (uuid.Data1 >> 24) & 0xff; |
| 656 |
id128.bytes[1] = (uuid.Data1 >> 16) & 0xff; |
| 657 |
id128.bytes[2] = (uuid.Data1 >> 8) & 0xff; |
| 658 |
id128.bytes[3] = uuid.Data1 & 0xff; |
| 659 |
|
| 660 |
id128.bytes[4] = (uuid.Data2 >> 8) & 0xff; |
| 661 |
id128.bytes[5] = uuid.Data2 & 0xff; |
| 662 |
|
| 663 |
id128.bytes[6] = (uuid.Data3 >> 8) & 0xff; |
| 664 |
id128.bytes[7] = uuid.Data3 & 0xff; |
| 665 |
|
| 666 |
memcpy(&id128.bytes[8], uuid.Data4, sizeof(uuid.Data4)); |
| 667 |
|
| 668 |
return id128; |
| 669 |
} |
| 670 |
|
| 671 |
void efi_id128_to_guid(sd_id128_t id, void *ret_guid) { |
| 672 |
assert(ret_guid); |
| 673 |
|
| 674 |
EFI_GUID uuid = { |
| 675 |
.Data1 = (uint32_t) id.bytes[0] << 24 | (uint32_t) id.bytes[1] << 16 | (uint32_t) id.bytes[2] << 8 | id.bytes[3], |
| 676 |
.Data2 = (uint16_t) id.bytes[4] << 8 | id.bytes[5], |
| 677 |
.Data3 = (uint16_t) id.bytes[6] << 8 | id.bytes[7], |
| 678 |
}; |
| 679 |
memcpy(uuid.Data4, id.bytes+8, sizeof(uuid.Data4)); |
| 680 |
memcpy(ret_guid, &uuid, sizeof(uuid)); |
| 681 |
} |