blob: c1955d320fecd6548cf99e9692562bac10f3998f [file] [log] [blame]
Ard Biesheuvel4febfb82019-02-02 10:41:15 +01001// SPDX-License-Identifier: GPL-2.0
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +02002/*
3 * Copyright (C) 2017 Google, Inc.
4 * Thiebaud Weksteen <tweek@google.com>
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +02005 */
6
Matthew Garrettc46f3402019-05-20 13:54:59 -07007#define TPM_MEMREMAP(start, size) early_memremap(start, size)
8#define TPM_MEMUNMAP(start, size) early_memunmap(start, size)
9
10#include <asm/early_ioremap.h>
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020011#include <linux/efi.h>
12#include <linux/init.h>
13#include <linux/memblock.h>
Matthew Garrettc46f3402019-05-20 13:54:59 -070014#include <linux/tpm_eventlog.h>
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020015
Matthew Garrettc46f3402019-05-20 13:54:59 -070016int efi_tpm_final_log_size;
17EXPORT_SYMBOL(efi_tpm_final_log_size);
18
Linus Torvaldse99332e2020-05-09 17:50:03 -070019static int __init tpm2_calc_event_log_size(void *data, int count, void *size_info)
Matthew Garrettc46f3402019-05-20 13:54:59 -070020{
21 struct tcg_pcr_event2_head *header;
22 int event_size, size = 0;
23
24 while (count > 0) {
25 header = data + size;
26 event_size = __calc_tpm2_event_size(header, size_info, true);
27 if (event_size == 0)
28 return -1;
29 size += event_size;
30 count--;
31 }
32
33 return size;
34}
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020035
36/*
37 * Reserve the memory associated with the TPM Event Log configuration table.
38 */
39int __init efi_tpm_eventlog_init(void)
40{
41 struct linux_efi_tpm_eventlog *log_tbl;
Matthew Garrettc46f3402019-05-20 13:54:59 -070042 struct efi_tcg2_final_events_table *final_tbl;
Colin Ian Kingbe59d572019-10-08 11:01:53 +010043 int tbl_size;
Matthew Garrettc46f3402019-05-20 13:54:59 -070044 int ret = 0;
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020045
Matthew Garrettc46f3402019-05-20 13:54:59 -070046 if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) {
47 /*
48 * We can't calculate the size of the final events without the
49 * first entry in the TPM log, so bail here.
50 */
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020051 return 0;
Matthew Garrettc46f3402019-05-20 13:54:59 -070052 }
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020053
54 log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl));
55 if (!log_tbl) {
56 pr_err("Failed to map TPM Event Log table @ 0x%lx\n",
Matthew Garrettc46f3402019-05-20 13:54:59 -070057 efi.tpm_log);
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020058 efi.tpm_log = EFI_INVALID_TABLE_ADDR;
59 return -ENOMEM;
60 }
61
62 tbl_size = sizeof(*log_tbl) + log_tbl->size;
63 memblock_reserve(efi.tpm_log, tbl_size);
Matthew Garrettc46f3402019-05-20 13:54:59 -070064
Loïc Yhuelb4f18742020-05-12 06:01:13 +020065 if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR ||
66 log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
67 pr_warn(FW_BUG "TPM Final Events table missing or invalid\n");
Matthew Garrettc46f3402019-05-20 13:54:59 -070068 goto out;
Loïc Yhuelb4f18742020-05-12 06:01:13 +020069 }
Matthew Garrettc46f3402019-05-20 13:54:59 -070070
71 final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
72
73 if (!final_tbl) {
74 pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n",
75 efi.tpm_final_log);
76 efi.tpm_final_log = EFI_INVALID_TABLE_ADDR;
77 ret = -ENOMEM;
78 goto out;
79 }
80
Peter Jones05c8c1f2019-10-02 18:59:01 +020081 tbl_size = 0;
82 if (final_tbl->nr_events != 0) {
83 void *events = (void *)efi.tpm_final_log
84 + sizeof(final_tbl->version)
85 + sizeof(final_tbl->nr_events);
86
87 tbl_size = tpm2_calc_event_log_size(events,
88 final_tbl->nr_events,
89 log_tbl->log);
90 }
Jerry Snitselaare658c822019-10-02 18:59:02 +020091
92 if (tbl_size < 0) {
93 pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
Jerry Snitselaar2bb6a812019-10-29 18:37:51 +010094 ret = -EINVAL;
Jerry Snitselaare658c822019-10-02 18:59:02 +020095 goto out_calc;
96 }
97
Matthew Garrettc46f3402019-05-20 13:54:59 -070098 memblock_reserve((unsigned long)final_tbl,
99 tbl_size + sizeof(*final_tbl));
Matthew Garrettc46f3402019-05-20 13:54:59 -0700100 efi_tpm_final_log_size = tbl_size;
101
Jerry Snitselaare658c822019-10-02 18:59:02 +0200102out_calc:
103 early_memunmap(final_tbl, sizeof(*final_tbl));
Matthew Garrettc46f3402019-05-20 13:54:59 -0700104out:
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +0200105 early_memunmap(log_tbl, sizeof(*log_tbl));
Matthew Garrettc46f3402019-05-20 13:54:59 -0700106 return ret;
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +0200107}
108