2019-02-02 17:41:15 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-09-20 16:13:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Google, Inc.
|
|
|
|
* Thiebaud Weksteen <tweek@google.com>
|
|
|
|
*/
|
|
|
|
|
2019-05-21 04:54:59 +08:00
|
|
|
#define TPM_MEMREMAP(start, size) early_memremap(start, size)
|
|
|
|
#define TPM_MEMUNMAP(start, size) early_memunmap(start, size)
|
|
|
|
|
|
|
|
#include <asm/early_ioremap.h>
|
2017-09-20 16:13:39 +08:00
|
|
|
#include <linux/efi.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/memblock.h>
|
2019-05-21 04:54:59 +08:00
|
|
|
#include <linux/tpm_eventlog.h>
|
2017-09-20 16:13:39 +08:00
|
|
|
|
2019-05-21 04:54:59 +08:00
|
|
|
int efi_tpm_final_log_size;
|
|
|
|
EXPORT_SYMBOL(efi_tpm_final_log_size);
|
|
|
|
|
2020-05-10 08:50:03 +08:00
|
|
|
static int __init tpm2_calc_event_log_size(void *data, int count, void *size_info)
|
2019-05-21 04:54:59 +08:00
|
|
|
{
|
|
|
|
struct tcg_pcr_event2_head *header;
|
|
|
|
int event_size, size = 0;
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
header = data + size;
|
|
|
|
event_size = __calc_tpm2_event_size(header, size_info, true);
|
|
|
|
if (event_size == 0)
|
|
|
|
return -1;
|
|
|
|
size += event_size;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2017-09-20 16:13:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reserve the memory associated with the TPM Event Log configuration table.
|
|
|
|
*/
|
|
|
|
int __init efi_tpm_eventlog_init(void)
|
|
|
|
{
|
|
|
|
struct linux_efi_tpm_eventlog *log_tbl;
|
2019-05-21 04:54:59 +08:00
|
|
|
struct efi_tcg2_final_events_table *final_tbl;
|
2019-10-08 18:01:53 +08:00
|
|
|
int tbl_size;
|
2019-05-21 04:54:59 +08:00
|
|
|
int ret = 0;
|
2017-09-20 16:13:39 +08:00
|
|
|
|
2019-05-21 04:54:59 +08:00
|
|
|
if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) {
|
|
|
|
/*
|
|
|
|
* We can't calculate the size of the final events without the
|
|
|
|
* first entry in the TPM log, so bail here.
|
|
|
|
*/
|
2017-09-20 16:13:39 +08:00
|
|
|
return 0;
|
2019-05-21 04:54:59 +08:00
|
|
|
}
|
2017-09-20 16:13:39 +08:00
|
|
|
|
|
|
|
log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl));
|
|
|
|
if (!log_tbl) {
|
|
|
|
pr_err("Failed to map TPM Event Log table @ 0x%lx\n",
|
2019-05-21 04:54:59 +08:00
|
|
|
efi.tpm_log);
|
2017-09-20 16:13:39 +08:00
|
|
|
efi.tpm_log = EFI_INVALID_TABLE_ADDR;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
tbl_size = sizeof(*log_tbl) + log_tbl->size;
|
|
|
|
memblock_reserve(efi.tpm_log, tbl_size);
|
2019-05-21 04:54:59 +08:00
|
|
|
|
2021-07-08 17:46:54 +08:00
|
|
|
if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
|
|
|
|
pr_info("TPM Final Events table not present\n");
|
|
|
|
goto out;
|
|
|
|
} else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
|
|
|
|
pr_warn(FW_BUG "TPM Final Events table invalid\n");
|
2019-05-21 04:54:59 +08:00
|
|
|
goto out;
|
2020-05-12 12:01:13 +08:00
|
|
|
}
|
2019-05-21 04:54:59 +08:00
|
|
|
|
|
|
|
final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
|
|
|
|
|
|
|
|
if (!final_tbl) {
|
|
|
|
pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n",
|
|
|
|
efi.tpm_final_log);
|
|
|
|
efi.tpm_final_log = EFI_INVALID_TABLE_ADDR;
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-10-03 00:59:01 +08:00
|
|
|
tbl_size = 0;
|
|
|
|
if (final_tbl->nr_events != 0) {
|
|
|
|
void *events = (void *)efi.tpm_final_log
|
|
|
|
+ sizeof(final_tbl->version)
|
|
|
|
+ sizeof(final_tbl->nr_events);
|
|
|
|
|
|
|
|
tbl_size = tpm2_calc_event_log_size(events,
|
|
|
|
final_tbl->nr_events,
|
|
|
|
log_tbl->log);
|
|
|
|
}
|
2019-10-03 00:59:02 +08:00
|
|
|
|
|
|
|
if (tbl_size < 0) {
|
|
|
|
pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
|
2019-10-30 01:37:51 +08:00
|
|
|
ret = -EINVAL;
|
2019-10-03 00:59:02 +08:00
|
|
|
goto out_calc;
|
|
|
|
}
|
|
|
|
|
2022-10-22 23:23:52 +08:00
|
|
|
memblock_reserve(efi.tpm_final_log,
|
2019-05-21 04:54:59 +08:00
|
|
|
tbl_size + sizeof(*final_tbl));
|
|
|
|
efi_tpm_final_log_size = tbl_size;
|
|
|
|
|
2019-10-03 00:59:02 +08:00
|
|
|
out_calc:
|
|
|
|
early_memunmap(final_tbl, sizeof(*final_tbl));
|
2019-05-21 04:54:59 +08:00
|
|
|
out:
|
2017-09-20 16:13:39 +08:00
|
|
|
early_memunmap(log_tbl, sizeof(*log_tbl));
|
2019-05-21 04:54:59 +08:00
|
|
|
return ret;
|
2017-09-20 16:13:39 +08:00
|
|
|
}
|
|
|
|
|