xref: /linux/drivers/firmware/efi/tpm.c (revision f4cd18c5)
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 
tpm2_calc_event_log_size(void * data,int count,void * size_info)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  */
efi_tpm_eventlog_init(void)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;
43be59d57fSColin Ian King 	int tbl_size;
44c46f3405SMatthew Garrett 	int ret = 0;
4533b6d034SThiebaud Weksteen 
46c46f3405SMatthew Garrett 	if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) {
47c46f3405SMatthew Garrett 		/*
48c46f3405SMatthew Garrett 		 * We can't calculate the size of the final events without the
49c46f3405SMatthew Garrett 		 * first entry in the TPM log, so bail here.
50c46f3405SMatthew Garrett 		 */
5133b6d034SThiebaud Weksteen 		return 0;
52c46f3405SMatthew Garrett 	}
5333b6d034SThiebaud Weksteen 
5433b6d034SThiebaud Weksteen 	log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl));
5533b6d034SThiebaud Weksteen 	if (!log_tbl) {
5633b6d034SThiebaud Weksteen 		pr_err("Failed to map TPM Event Log table @ 0x%lx\n",
5733b6d034SThiebaud Weksteen 		       efi.tpm_log);
5833b6d034SThiebaud Weksteen 		efi.tpm_log = EFI_INVALID_TABLE_ADDR;
5933b6d034SThiebaud Weksteen 		return -ENOMEM;
6033b6d034SThiebaud Weksteen 	}
6133b6d034SThiebaud Weksteen 
6233b6d034SThiebaud Weksteen 	tbl_size = sizeof(*log_tbl) + log_tbl->size;
6333b6d034SThiebaud Weksteen 	memblock_reserve(efi.tpm_log, tbl_size);
64c46f3405SMatthew Garrett 
65674a9f1fSMichal Suchanek 	if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
66674a9f1fSMichal Suchanek 		pr_info("TPM Final Events table not present\n");
67674a9f1fSMichal Suchanek 		goto out;
68674a9f1fSMichal Suchanek 	} else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
69674a9f1fSMichal Suchanek 		pr_warn(FW_BUG "TPM Final Events table invalid\n");
70c46f3405SMatthew Garrett 		goto out;
71b4f1874cSLoïc Yhuel 	}
72c46f3405SMatthew Garrett 
73c46f3405SMatthew Garrett 	final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
74c46f3405SMatthew Garrett 
75c46f3405SMatthew Garrett 	if (!final_tbl) {
76c46f3405SMatthew Garrett 		pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n",
77c46f3405SMatthew Garrett 		       efi.tpm_final_log);
78c46f3405SMatthew Garrett 		efi.tpm_final_log = EFI_INVALID_TABLE_ADDR;
79c46f3405SMatthew Garrett 		ret = -ENOMEM;
80c46f3405SMatthew Garrett 		goto out;
81c46f3405SMatthew Garrett 	}
82c46f3405SMatthew Garrett 
8305c8c1ffSPeter Jones 	tbl_size = 0;
8405c8c1ffSPeter Jones 	if (final_tbl->nr_events != 0) {
8505c8c1ffSPeter Jones 		void *events = (void *)efi.tpm_final_log
86c46f3405SMatthew Garrett 				+ sizeof(final_tbl->version)
8705c8c1ffSPeter Jones 				+ sizeof(final_tbl->nr_events);
8805c8c1ffSPeter Jones 
8905c8c1ffSPeter Jones 		tbl_size = tpm2_calc_event_log_size(events,
90c46f3405SMatthew Garrett 						    final_tbl->nr_events,
91c46f3405SMatthew Garrett 						    log_tbl->log);
9205c8c1ffSPeter Jones 	}
93e658c82bSJerry Snitselaar 
94e658c82bSJerry Snitselaar 	if (tbl_size < 0) {
95e658c82bSJerry Snitselaar 		pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
962bb6a816SJerry Snitselaar 		ret = -EINVAL;
97e658c82bSJerry Snitselaar 		goto out_calc;
98e658c82bSJerry Snitselaar 	}
99e658c82bSJerry Snitselaar 
100*f4cd18c5SJerry Snitselaar 	memblock_reserve(efi.tpm_final_log,
101c46f3405SMatthew Garrett 			 tbl_size + sizeof(*final_tbl));
102c46f3405SMatthew Garrett 	efi_tpm_final_log_size = tbl_size;
103c46f3405SMatthew Garrett 
104e658c82bSJerry Snitselaar out_calc:
105e658c82bSJerry Snitselaar 	early_memunmap(final_tbl, sizeof(*final_tbl));
106c46f3405SMatthew Garrett out:
10733b6d034SThiebaud Weksteen 	early_memunmap(log_tbl, sizeof(*log_tbl));
108c46f3405SMatthew Garrett 	return ret;
10933b6d034SThiebaud Weksteen }
11033b6d034SThiebaud Weksteen 
111