14febfb8dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 233b6d034SThiebaud Weksteen /* 333b6d034SThiebaud Weksteen * Copyright (C) 2017 Google, Inc. 433b6d034SThiebaud Weksteen * Thiebaud Weksteen <tweek@google.com> 533b6d034SThiebaud Weksteen */ 633b6d034SThiebaud Weksteen 7c46f3405SMatthew Garrett #define TPM_MEMREMAP(start, size) early_memremap(start, size) 8c46f3405SMatthew Garrett #define TPM_MEMUNMAP(start, size) early_memunmap(start, size) 9c46f3405SMatthew Garrett 10c46f3405SMatthew Garrett #include <asm/early_ioremap.h> 1133b6d034SThiebaud Weksteen #include <linux/efi.h> 1233b6d034SThiebaud Weksteen #include <linux/init.h> 1333b6d034SThiebaud Weksteen #include <linux/memblock.h> 14c46f3405SMatthew Garrett #include <linux/tpm_eventlog.h> 1533b6d034SThiebaud Weksteen 16c46f3405SMatthew Garrett int efi_tpm_final_log_size; 17c46f3405SMatthew Garrett EXPORT_SYMBOL(efi_tpm_final_log_size); 18c46f3405SMatthew Garrett 19e99332e7SLinus Torvalds static int __init tpm2_calc_event_log_size(void *data, int count, void *size_info) 20c46f3405SMatthew Garrett { 21c46f3405SMatthew Garrett struct tcg_pcr_event2_head *header; 22c46f3405SMatthew Garrett int event_size, size = 0; 23c46f3405SMatthew Garrett 24c46f3405SMatthew Garrett while (count > 0) { 25c46f3405SMatthew Garrett header = data + size; 26c46f3405SMatthew Garrett event_size = __calc_tpm2_event_size(header, size_info, true); 27c46f3405SMatthew Garrett if (event_size == 0) 28c46f3405SMatthew Garrett return -1; 29c46f3405SMatthew Garrett size += event_size; 30c46f3405SMatthew Garrett count--; 31c46f3405SMatthew Garrett } 32c46f3405SMatthew Garrett 33c46f3405SMatthew Garrett return size; 34c46f3405SMatthew Garrett } 3533b6d034SThiebaud Weksteen 3633b6d034SThiebaud Weksteen /* 3733b6d034SThiebaud Weksteen * Reserve the memory associated with the TPM Event Log configuration table. 3833b6d034SThiebaud Weksteen */ 3933b6d034SThiebaud Weksteen int __init efi_tpm_eventlog_init(void) 4033b6d034SThiebaud Weksteen { 4133b6d034SThiebaud Weksteen struct linux_efi_tpm_eventlog *log_tbl; 42c46f3405SMatthew Garrett struct efi_tcg2_final_events_table *final_tbl; 43*f8549e3fSGregory Price unsigned int tbl_size; 44*f8549e3fSGregory Price int final_tbl_size; 45c46f3405SMatthew Garrett int ret = 0; 4633b6d034SThiebaud Weksteen 47c46f3405SMatthew Garrett if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) { 48c46f3405SMatthew Garrett /* 49c46f3405SMatthew Garrett * We can't calculate the size of the final events without the 50c46f3405SMatthew Garrett * first entry in the TPM log, so bail here. 51c46f3405SMatthew Garrett */ 5233b6d034SThiebaud Weksteen return 0; 53c46f3405SMatthew Garrett } 5433b6d034SThiebaud Weksteen 5533b6d034SThiebaud Weksteen log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl)); 5633b6d034SThiebaud Weksteen if (!log_tbl) { 5733b6d034SThiebaud Weksteen pr_err("Failed to map TPM Event Log table @ 0x%lx\n", 5833b6d034SThiebaud Weksteen efi.tpm_log); 5933b6d034SThiebaud Weksteen efi.tpm_log = EFI_INVALID_TABLE_ADDR; 6033b6d034SThiebaud Weksteen return -ENOMEM; 6133b6d034SThiebaud Weksteen } 6233b6d034SThiebaud Weksteen 6333b6d034SThiebaud Weksteen tbl_size = sizeof(*log_tbl) + log_tbl->size; 6433b6d034SThiebaud Weksteen memblock_reserve(efi.tpm_log, tbl_size); 65c46f3405SMatthew Garrett 66674a9f1fSMichal Suchanek if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) { 67674a9f1fSMichal Suchanek pr_info("TPM Final Events table not present\n"); 68674a9f1fSMichal Suchanek goto out; 69674a9f1fSMichal Suchanek } else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) { 70674a9f1fSMichal Suchanek pr_warn(FW_BUG "TPM Final Events table invalid\n"); 71c46f3405SMatthew Garrett goto out; 72b4f1874cSLoïc Yhuel } 73c46f3405SMatthew Garrett 74c46f3405SMatthew Garrett final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl)); 75c46f3405SMatthew Garrett 76c46f3405SMatthew Garrett if (!final_tbl) { 77c46f3405SMatthew Garrett pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n", 78c46f3405SMatthew Garrett efi.tpm_final_log); 79c46f3405SMatthew Garrett efi.tpm_final_log = EFI_INVALID_TABLE_ADDR; 80c46f3405SMatthew Garrett ret = -ENOMEM; 81c46f3405SMatthew Garrett goto out; 82c46f3405SMatthew Garrett } 83c46f3405SMatthew Garrett 84*f8549e3fSGregory Price final_tbl_size = 0; 8505c8c1ffSPeter Jones if (final_tbl->nr_events != 0) { 8605c8c1ffSPeter Jones void *events = (void *)efi.tpm_final_log 87c46f3405SMatthew Garrett + sizeof(final_tbl->version) 8805c8c1ffSPeter Jones + sizeof(final_tbl->nr_events); 8905c8c1ffSPeter Jones 90*f8549e3fSGregory Price final_tbl_size = tpm2_calc_event_log_size(events, 91c46f3405SMatthew Garrett final_tbl->nr_events, 92c46f3405SMatthew Garrett log_tbl->log); 9305c8c1ffSPeter Jones } 94e658c82bSJerry Snitselaar 95*f8549e3fSGregory Price if (final_tbl_size < 0) { 96e658c82bSJerry Snitselaar pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n"); 972bb6a816SJerry Snitselaar ret = -EINVAL; 98e658c82bSJerry Snitselaar goto out_calc; 99e658c82bSJerry Snitselaar } 100e658c82bSJerry Snitselaar 101f4cd18c5SJerry Snitselaar memblock_reserve(efi.tpm_final_log, 102*f8549e3fSGregory Price final_tbl_size + sizeof(*final_tbl)); 103*f8549e3fSGregory Price efi_tpm_final_log_size = final_tbl_size; 104c46f3405SMatthew Garrett 105e658c82bSJerry Snitselaar out_calc: 106e658c82bSJerry Snitselaar early_memunmap(final_tbl, sizeof(*final_tbl)); 107c46f3405SMatthew Garrett out: 10833b6d034SThiebaud Weksteen early_memunmap(log_tbl, sizeof(*log_tbl)); 109c46f3405SMatthew Garrett return ret; 11033b6d034SThiebaud Weksteen } 11133b6d034SThiebaud Weksteen 112