.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2017 Google, Inc. |
---|
3 | 4 | * Thiebaud Weksteen <tweek@google.com> |
---|
4 | | - * |
---|
5 | | - * This program is free software; you can redistribute it and/or modify |
---|
6 | | - * it under the terms of the GNU General Public License version 2 as |
---|
7 | | - * published by the Free Software Foundation. |
---|
8 | 5 | */ |
---|
9 | 6 | |
---|
| 7 | +#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> |
---|
10 | 11 | #include <linux/efi.h> |
---|
11 | 12 | #include <linux/init.h> |
---|
12 | 13 | #include <linux/memblock.h> |
---|
| 14 | +#include <linux/tpm_eventlog.h> |
---|
13 | 15 | |
---|
14 | | -#include <asm/early_ioremap.h> |
---|
| 16 | +int efi_tpm_final_log_size; |
---|
| 17 | +EXPORT_SYMBOL(efi_tpm_final_log_size); |
---|
| 18 | + |
---|
| 19 | +static int __init tpm2_calc_event_log_size(void *data, int count, void *size_info) |
---|
| 20 | +{ |
---|
| 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 | +} |
---|
15 | 35 | |
---|
16 | 36 | /* |
---|
17 | 37 | * Reserve the memory associated with the TPM Event Log configuration table. |
---|
.. | .. |
---|
19 | 39 | int __init efi_tpm_eventlog_init(void) |
---|
20 | 40 | { |
---|
21 | 41 | struct linux_efi_tpm_eventlog *log_tbl; |
---|
22 | | - unsigned int tbl_size; |
---|
| 42 | + struct efi_tcg2_final_events_table *final_tbl; |
---|
| 43 | + int tbl_size; |
---|
| 44 | + int ret = 0; |
---|
23 | 45 | |
---|
24 | | - if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) |
---|
| 46 | + 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 | + */ |
---|
25 | 51 | return 0; |
---|
| 52 | + } |
---|
26 | 53 | |
---|
27 | 54 | log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl)); |
---|
28 | 55 | if (!log_tbl) { |
---|
29 | 56 | pr_err("Failed to map TPM Event Log table @ 0x%lx\n", |
---|
30 | | - efi.tpm_log); |
---|
| 57 | + efi.tpm_log); |
---|
31 | 58 | efi.tpm_log = EFI_INVALID_TABLE_ADDR; |
---|
32 | 59 | return -ENOMEM; |
---|
33 | 60 | } |
---|
34 | 61 | |
---|
35 | 62 | tbl_size = sizeof(*log_tbl) + log_tbl->size; |
---|
36 | 63 | memblock_reserve(efi.tpm_log, tbl_size); |
---|
| 64 | + |
---|
| 65 | + if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) { |
---|
| 66 | + pr_info("TPM Final Events table not present\n"); |
---|
| 67 | + goto out; |
---|
| 68 | + } else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) { |
---|
| 69 | + pr_warn(FW_BUG "TPM Final Events table invalid\n"); |
---|
| 70 | + goto out; |
---|
| 71 | + } |
---|
| 72 | + |
---|
| 73 | + final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl)); |
---|
| 74 | + |
---|
| 75 | + if (!final_tbl) { |
---|
| 76 | + pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n", |
---|
| 77 | + efi.tpm_final_log); |
---|
| 78 | + efi.tpm_final_log = EFI_INVALID_TABLE_ADDR; |
---|
| 79 | + ret = -ENOMEM; |
---|
| 80 | + goto out; |
---|
| 81 | + } |
---|
| 82 | + |
---|
| 83 | + tbl_size = 0; |
---|
| 84 | + if (final_tbl->nr_events != 0) { |
---|
| 85 | + void *events = (void *)efi.tpm_final_log |
---|
| 86 | + + sizeof(final_tbl->version) |
---|
| 87 | + + sizeof(final_tbl->nr_events); |
---|
| 88 | + |
---|
| 89 | + tbl_size = tpm2_calc_event_log_size(events, |
---|
| 90 | + final_tbl->nr_events, |
---|
| 91 | + log_tbl->log); |
---|
| 92 | + } |
---|
| 93 | + |
---|
| 94 | + if (tbl_size < 0) { |
---|
| 95 | + pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n"); |
---|
| 96 | + ret = -EINVAL; |
---|
| 97 | + goto out_calc; |
---|
| 98 | + } |
---|
| 99 | + |
---|
| 100 | + memblock_reserve(efi.tpm_final_log, |
---|
| 101 | + tbl_size + sizeof(*final_tbl)); |
---|
| 102 | + efi_tpm_final_log_size = tbl_size; |
---|
| 103 | + |
---|
| 104 | +out_calc: |
---|
| 105 | + early_memunmap(final_tbl, sizeof(*final_tbl)); |
---|
| 106 | +out: |
---|
37 | 107 | early_memunmap(log_tbl, sizeof(*log_tbl)); |
---|
38 | | - return 0; |
---|
| 108 | + return ret; |
---|
39 | 109 | } |
---|
40 | 110 | |
---|