| 1 | // SPDX-License-Identifier: GPL-2.0 | 
|---|
| 2 | /* | 
|---|
| 3 | * Copyright (C) 2017 Google, Inc. | 
|---|
| 4 | *     Thiebaud Weksteen <tweek@google.com> | 
|---|
| 5 | */ | 
|---|
| 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> | 
|---|
| 11 | #include <linux/efi.h> | 
|---|
| 12 | #include <linux/init.h> | 
|---|
| 13 | #include <linux/memblock.h> | 
|---|
| 14 | #include <linux/tpm_eventlog.h> | 
|---|
| 15 |  | 
|---|
| 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 *; | 
|---|
| 22 | u32 event_size, size = 0; | 
|---|
| 23 |  | 
|---|
| 24 | while (count > 0) { | 
|---|
| 25 | header = data + size; | 
|---|
| 26 | event_size = __calc_tpm2_event_size(event: header, event_header: size_info, do_mapping: true); | 
|---|
| 27 | if (event_size == 0) | 
|---|
| 28 | return -1; | 
|---|
| 29 | size += event_size; | 
|---|
| 30 | count--; | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | return size; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | /* | 
|---|
| 37 | * Reserve the memory associated with the TPM Event Log configuration table. | 
|---|
| 38 | */ | 
|---|
| 39 | int __init efi_tpm_eventlog_init(void) | 
|---|
| 40 | { | 
|---|
| 41 | struct linux_efi_tpm_eventlog *log_tbl; | 
|---|
| 42 | struct efi_tcg2_final_events_table *final_tbl; | 
|---|
| 43 | unsigned int tbl_size; | 
|---|
| 44 | int final_tbl_size; | 
|---|
| 45 | int ret = 0; | 
|---|
| 46 |  | 
|---|
| 47 | if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) { | 
|---|
| 48 | /* | 
|---|
| 49 | * We can't calculate the size of the final events without the | 
|---|
| 50 | * first entry in the TPM log, so bail here. | 
|---|
| 51 | */ | 
|---|
| 52 | return 0; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | log_tbl = early_memremap(phys_addr: efi.tpm_log, size: sizeof(*log_tbl)); | 
|---|
| 56 | if (!log_tbl) { | 
|---|
| 57 | pr_err( "Failed to map TPM Event Log table @ 0x%lx\n", | 
|---|
| 58 | efi.tpm_log); | 
|---|
| 59 | efi.tpm_log = EFI_INVALID_TABLE_ADDR; | 
|---|
| 60 | return -ENOMEM; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | tbl_size = sizeof(*log_tbl) + log_tbl->size; | 
|---|
| 64 | if (memblock_reserve(base: efi.tpm_log, size: tbl_size)) { | 
|---|
| 65 | pr_err( "TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n", | 
|---|
| 66 | efi.tpm_log, tbl_size); | 
|---|
| 67 | ret = -ENOMEM; | 
|---|
| 68 | goto out; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) { | 
|---|
| 72 | pr_info( "TPM Final Events table not present\n"); | 
|---|
| 73 | goto out; | 
|---|
| 74 | } else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) { | 
|---|
| 75 | pr_warn(FW_BUG "TPM Final Events table invalid\n"); | 
|---|
| 76 | goto out; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | final_tbl = early_memremap(phys_addr: efi.tpm_final_log, size: sizeof(*final_tbl)); | 
|---|
| 80 |  | 
|---|
| 81 | if (!final_tbl) { | 
|---|
| 82 | pr_err( "Failed to map TPM Final Event Log table @ 0x%lx\n", | 
|---|
| 83 | efi.tpm_final_log); | 
|---|
| 84 | efi.tpm_final_log = EFI_INVALID_TABLE_ADDR; | 
|---|
| 85 | ret = -ENOMEM; | 
|---|
| 86 | goto out; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | final_tbl_size = 0; | 
|---|
| 90 | if (final_tbl->nr_events != 0) { | 
|---|
| 91 | void *events = (void *)efi.tpm_final_log | 
|---|
| 92 | + sizeof(final_tbl->version) | 
|---|
| 93 | + sizeof(final_tbl->nr_events); | 
|---|
| 94 |  | 
|---|
| 95 | final_tbl_size = tpm2_calc_event_log_size(data: events, | 
|---|
| 96 | count: final_tbl->nr_events, | 
|---|
| 97 | size_info: log_tbl->log); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | if (final_tbl_size < 0) { | 
|---|
| 101 | pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n"); | 
|---|
| 102 | ret = -EINVAL; | 
|---|
| 103 | goto out_calc; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | memblock_reserve(base: efi.tpm_final_log, | 
|---|
| 107 | size: final_tbl_size + sizeof(*final_tbl)); | 
|---|
| 108 | efi_tpm_final_log_size = final_tbl_size; | 
|---|
| 109 |  | 
|---|
| 110 | out_calc: | 
|---|
| 111 | early_memunmap(addr: final_tbl, size: sizeof(*final_tbl)); | 
|---|
| 112 | out: | 
|---|
| 113 | early_memunmap(addr: log_tbl, size: sizeof(*log_tbl)); | 
|---|
| 114 | return ret; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|