18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2017 Google
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors:
68c2ecf20Sopenharmony_ci *      Thiebaud Weksteen <tweek@google.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/efi.h>
108c2ecf20Sopenharmony_ci#include <linux/tpm_eventlog.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "../tpm.h"
138c2ecf20Sopenharmony_ci#include "common.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* read binary bios log from EFI configuration table */
168c2ecf20Sopenharmony_ciint tpm_read_log_efi(struct tpm_chip *chip)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	struct efi_tcg2_final_events_table *final_tbl = NULL;
208c2ecf20Sopenharmony_ci	int final_events_log_size = efi_tpm_final_log_size;
218c2ecf20Sopenharmony_ci	struct linux_efi_tpm_eventlog *log_tbl;
228c2ecf20Sopenharmony_ci	struct tpm_bios_log *log;
238c2ecf20Sopenharmony_ci	u32 log_size;
248c2ecf20Sopenharmony_ci	u8 tpm_log_version;
258c2ecf20Sopenharmony_ci	void *tmp;
268c2ecf20Sopenharmony_ci	int ret;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
298c2ecf20Sopenharmony_ci		return -ENODEV;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	if (efi.tpm_log == EFI_INVALID_TABLE_ADDR)
328c2ecf20Sopenharmony_ci		return -ENODEV;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	log = &chip->log;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	log_tbl = memremap(efi.tpm_log, sizeof(*log_tbl), MEMREMAP_WB);
378c2ecf20Sopenharmony_ci	if (!log_tbl) {
388c2ecf20Sopenharmony_ci		pr_err("Could not map UEFI TPM log table !\n");
398c2ecf20Sopenharmony_ci		return -ENOMEM;
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	log_size = log_tbl->size;
438c2ecf20Sopenharmony_ci	memunmap(log_tbl);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	if (!log_size) {
468c2ecf20Sopenharmony_ci		pr_warn("UEFI TPM log area empty\n");
478c2ecf20Sopenharmony_ci		return -EIO;
488c2ecf20Sopenharmony_ci	}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	log_tbl = memremap(efi.tpm_log, sizeof(*log_tbl) + log_size,
518c2ecf20Sopenharmony_ci			   MEMREMAP_WB);
528c2ecf20Sopenharmony_ci	if (!log_tbl) {
538c2ecf20Sopenharmony_ci		pr_err("Could not map UEFI TPM log table payload!\n");
548c2ecf20Sopenharmony_ci		return -ENOMEM;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/* malloc EventLog space */
588c2ecf20Sopenharmony_ci	log->bios_event_log = kmemdup(log_tbl->log, log_size, GFP_KERNEL);
598c2ecf20Sopenharmony_ci	if (!log->bios_event_log) {
608c2ecf20Sopenharmony_ci		ret = -ENOMEM;
618c2ecf20Sopenharmony_ci		goto out;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	log->bios_event_log_end = log->bios_event_log + log_size;
658c2ecf20Sopenharmony_ci	tpm_log_version = log_tbl->version;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	ret = tpm_log_version;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR ||
708c2ecf20Sopenharmony_ci	    final_events_log_size == 0 ||
718c2ecf20Sopenharmony_ci	    tpm_log_version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2)
728c2ecf20Sopenharmony_ci		goto out;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	final_tbl = memremap(efi.tpm_final_log,
758c2ecf20Sopenharmony_ci			     sizeof(*final_tbl) + final_events_log_size,
768c2ecf20Sopenharmony_ci			     MEMREMAP_WB);
778c2ecf20Sopenharmony_ci	if (!final_tbl) {
788c2ecf20Sopenharmony_ci		pr_err("Could not map UEFI TPM final log\n");
798c2ecf20Sopenharmony_ci		kfree(log->bios_event_log);
808c2ecf20Sopenharmony_ci		ret = -ENOMEM;
818c2ecf20Sopenharmony_ci		goto out;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/*
858c2ecf20Sopenharmony_ci	 * The 'final events log' size excludes the 'final events preboot log'
868c2ecf20Sopenharmony_ci	 * at its beginning.
878c2ecf20Sopenharmony_ci	 */
888c2ecf20Sopenharmony_ci	final_events_log_size -= log_tbl->final_events_preboot_size;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * Allocate memory for the 'combined log' where we will append the
928c2ecf20Sopenharmony_ci	 * 'final events log' to.
938c2ecf20Sopenharmony_ci	 */
948c2ecf20Sopenharmony_ci	tmp = krealloc(log->bios_event_log,
958c2ecf20Sopenharmony_ci		       log_size + final_events_log_size,
968c2ecf20Sopenharmony_ci		       GFP_KERNEL);
978c2ecf20Sopenharmony_ci	if (!tmp) {
988c2ecf20Sopenharmony_ci		kfree(log->bios_event_log);
998c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1008c2ecf20Sopenharmony_ci		goto out;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	log->bios_event_log = tmp;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/*
1068c2ecf20Sopenharmony_ci	 * Append any of the 'final events log' that didn't also end up in the
1078c2ecf20Sopenharmony_ci	 * 'main log'. Events can be logged in both if events are generated
1088c2ecf20Sopenharmony_ci	 * between GetEventLog() and ExitBootServices().
1098c2ecf20Sopenharmony_ci	 */
1108c2ecf20Sopenharmony_ci	memcpy((void *)log->bios_event_log + log_size,
1118c2ecf20Sopenharmony_ci	       final_tbl->events + log_tbl->final_events_preboot_size,
1128c2ecf20Sopenharmony_ci	       final_events_log_size);
1138c2ecf20Sopenharmony_ci	/*
1148c2ecf20Sopenharmony_ci	 * The size of the 'combined log' is the size of the 'main log' plus
1158c2ecf20Sopenharmony_ci	 * the size of the 'final events log'.
1168c2ecf20Sopenharmony_ci	 */
1178c2ecf20Sopenharmony_ci	log->bios_event_log_end = log->bios_event_log +
1188c2ecf20Sopenharmony_ci		log_size + final_events_log_size;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciout:
1218c2ecf20Sopenharmony_ci	memunmap(final_tbl);
1228c2ecf20Sopenharmony_ci	memunmap(log_tbl);
1238c2ecf20Sopenharmony_ci	return ret;
1248c2ecf20Sopenharmony_ci}
125