|
7 | 7 | #include "efi-api.h" |
8 | 8 | #include "efivars.h" |
9 | 9 | #include "fd-util.h" |
| 10 | +#include "fileio.h" |
10 | 11 | #include "sort-util.h" |
11 | 12 | #include "stat-util.h" |
12 | 13 | #include "stdio-util.h" |
@@ -521,37 +522,43 @@ int efi_get_boot_options(uint16_t **ret_options) { |
521 | 522 |
|
522 | 523 | bool efi_has_tpm2(void) { |
523 | 524 | static int cache = -1; |
| 525 | + int r; |
524 | 526 |
|
525 | 527 | /* Returns whether the system has a TPM2 chip which is known to the EFI firmware. */ |
526 | 528 |
|
527 | 529 | if (cache >= 0) |
528 | 530 | return cache; |
529 | 531 |
|
530 | 532 | /* First, check if we are on an EFI boot at all. */ |
531 | | - if (!is_efi_boot()) { |
532 | | - cache = 0; |
533 | | - return cache; |
534 | | - } |
| 533 | + if (!is_efi_boot()) |
| 534 | + return (cache = false); |
535 | 535 |
|
536 | 536 | /* Then, check if the ACPI table "TPM2" exists, which is the TPM2 event log table, see: |
537 | 537 | * https://trustedcomputinggroup.org/wp-content/uploads/TCG_ACPIGeneralSpecification_v1.20_r8.pdf |
538 | | - * This table exists whenever the firmware is hooked up to TPM2. */ |
539 | | - cache = access("/sys/firmware/acpi/tables/TPM2", F_OK) >= 0; |
540 | | - if (cache) |
541 | | - return cache; |
542 | | - |
| 538 | + * This table exists whenever the firmware knows ACPI and is hooked up to TPM2. */ |
| 539 | + if (access("/sys/firmware/acpi/tables/TPM2", F_OK) >= 0) |
| 540 | + return (cache = true); |
543 | 541 | if (errno != ENOENT) |
544 | 542 | log_debug_errno(errno, "Unable to test whether /sys/firmware/acpi/tables/TPM2 exists, assuming it doesn't: %m"); |
545 | 543 |
|
546 | 544 | /* As the last try, check if the EFI firmware provides the EFI_TCG2_FINAL_EVENTS_TABLE |
547 | 545 | * stored in EFI configuration table, see: |
548 | | - * https://trustedcomputinggroup.org/wp-content/uploads/EFI-Protocol-Specification-rev13-160330final.pdf |
549 | | - */ |
550 | | - cache = access("/sys/kernel/security/tpm0/binary_bios_measurements", F_OK) >= 0; |
551 | | - if (!cache && errno != ENOENT) |
552 | | - log_debug_errno(errno, "Unable to test whether /sys/kernel/security/tpm0/binary_bios_measurements exists, assuming it doesn't: %m"); |
| 546 | + * |
| 547 | + * https://trustedcomputinggroup.org/wp-content/uploads/EFI-Protocol-Specification-rev13-160330final.pdf */ |
| 548 | + if (access("/sys/kernel/security/tpm0/binary_bios_measurements", F_OK) >= 0) { |
| 549 | + _cleanup_free_ char *major = NULL; |
| 550 | + |
| 551 | + /* The EFI table might exist for TPM 1.2 as well, hence let's check explicitly which TPM version we are looking at here. */ |
| 552 | + r = read_virtual_file("/sys/class/tpm/tpm0/tpm_version_major", SIZE_MAX, &major, /* ret_size= */ NULL); |
| 553 | + if (r >= 0) |
| 554 | + return (cache = streq(strstrip(major), "2")); |
| 555 | + |
| 556 | + log_debug_errno(r, "Unable to read /sys/class/tpm/tpm0/tpm_version_major, assuming TPM does not qualify as TPM2: %m"); |
| 557 | + |
| 558 | + } else if (errno != ENOENT) |
| 559 | + log_debug_errno(errno, "Unable to test whether /sys/kernel/security/tpm0/binary_bios_measurements exists, assuming it doesn't: %m"); |
553 | 560 |
|
554 | | - return cache; |
| 561 | + return (cache = false); |
555 | 562 | } |
556 | 563 |
|
557 | 564 | #endif |
0 commit comments