1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Google
6 * Thiebaud Weksteen <tweek@google.com>
10 #include <linux/tpm_eventlog.h>
15 /* read binary bios log from EFI configuration table */
16 int tpm_read_log_efi(struct tpm_chip *chip)
19 struct efi_tcg2_final_events_table *final_tbl = NULL;
20 int final_events_log_size = efi_tpm_final_log_size;
21 struct linux_efi_tpm_eventlog *log_tbl;
22 struct tpm_bios_log *log;
28 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
31 if (efi.tpm_log == EFI_INVALID_TABLE_ADDR)
36 log_tbl = memremap(efi.tpm_log, sizeof(*log_tbl), MEMREMAP_WB);
38 pr_err("Could not map UEFI TPM log table !\n");
42 log_size = log_tbl->size;
46 pr_warn("UEFI TPM log area empty\n");
50 log_tbl = memremap(efi.tpm_log, sizeof(*log_tbl) + log_size,
53 pr_err("Could not map UEFI TPM log table payload!\n");
57 /* malloc EventLog space */
58 log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
59 if (!log->bios_event_log) {
64 log->bios_event_log_end = log->bios_event_log + log_size;
65 tpm_log_version = log_tbl->version;
67 ret = tpm_log_version;
69 if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR ||
70 final_events_log_size == 0 ||
71 tpm_log_version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
74 final_tbl = memremap(efi.tpm_final_log,
75 sizeof(*final_tbl) + final_events_log_size,
78 pr_err("Could not map UEFI TPM final log\n");
79 kfree(log->bios_event_log);
85 * The 'final events log' size excludes the 'final events preboot log'
88 final_events_log_size -= log_tbl->final_events_preboot_size;
91 * Allocate memory for the 'combined log' where we will append the
92 * 'final events log' to.
94 tmp = krealloc(log->bios_event_log,
95 log_size + final_events_log_size,
98 kfree(log->bios_event_log);
103 log->bios_event_log = tmp;
106 * Append any of the 'final events log' that didn't also end up in the
107 * 'main log'. Events can be logged in both if events are generated
108 * between GetEventLog() and ExitBootServices().
110 memcpy((void *)log->bios_event_log + log_size,
111 final_tbl->events + log_tbl->final_events_preboot_size,
112 final_events_log_size);
114 * The size of the 'combined log' is the size of the 'main log' plus
115 * the size of the 'final events log'.
117 log->bios_event_log_end = log->bios_event_log +
118 log_size + final_events_log_size;