Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2017 Google, Inc.
0004  *     Thiebaud Weksteen <tweek@google.com>
0005  */
0006 
0007 #define TPM_MEMREMAP(start, size) early_memremap(start, size)
0008 #define TPM_MEMUNMAP(start, size) early_memunmap(start, size)
0009 
0010 #include <asm/early_ioremap.h>
0011 #include <linux/efi.h>
0012 #include <linux/init.h>
0013 #include <linux/memblock.h>
0014 #include <linux/tpm_eventlog.h>
0015 
0016 int efi_tpm_final_log_size;
0017 EXPORT_SYMBOL(efi_tpm_final_log_size);
0018 
0019 static int __init tpm2_calc_event_log_size(void *data, int count, void *size_info)
0020 {
0021     struct tcg_pcr_event2_head *header;
0022     int event_size, size = 0;
0023 
0024     while (count > 0) {
0025         header = data + size;
0026         event_size = __calc_tpm2_event_size(header, size_info, true);
0027         if (event_size == 0)
0028             return -1;
0029         size += event_size;
0030         count--;
0031     }
0032 
0033     return size;
0034 }
0035 
0036 /*
0037  * Reserve the memory associated with the TPM Event Log configuration table.
0038  */
0039 int __init efi_tpm_eventlog_init(void)
0040 {
0041     struct linux_efi_tpm_eventlog *log_tbl;
0042     struct efi_tcg2_final_events_table *final_tbl;
0043     int tbl_size;
0044     int ret = 0;
0045 
0046     if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) {
0047         /*
0048          * We can't calculate the size of the final events without the
0049          * first entry in the TPM log, so bail here.
0050          */
0051         return 0;
0052     }
0053 
0054     log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl));
0055     if (!log_tbl) {
0056         pr_err("Failed to map TPM Event Log table @ 0x%lx\n",
0057                efi.tpm_log);
0058         efi.tpm_log = EFI_INVALID_TABLE_ADDR;
0059         return -ENOMEM;
0060     }
0061 
0062     tbl_size = sizeof(*log_tbl) + log_tbl->size;
0063     memblock_reserve(efi.tpm_log, tbl_size);
0064 
0065     if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
0066         pr_info("TPM Final Events table not present\n");
0067         goto out;
0068     } else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
0069         pr_warn(FW_BUG "TPM Final Events table invalid\n");
0070         goto out;
0071     }
0072 
0073     final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
0074 
0075     if (!final_tbl) {
0076         pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n",
0077                efi.tpm_final_log);
0078         efi.tpm_final_log = EFI_INVALID_TABLE_ADDR;
0079         ret = -ENOMEM;
0080         goto out;
0081     }
0082 
0083     tbl_size = 0;
0084     if (final_tbl->nr_events != 0) {
0085         void *events = (void *)efi.tpm_final_log
0086                 + sizeof(final_tbl->version)
0087                 + sizeof(final_tbl->nr_events);
0088 
0089         tbl_size = tpm2_calc_event_log_size(events,
0090                             final_tbl->nr_events,
0091                             log_tbl->log);
0092     }
0093 
0094     if (tbl_size < 0) {
0095         pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
0096         ret = -EINVAL;
0097         goto out_calc;
0098     }
0099 
0100     memblock_reserve((unsigned long)final_tbl,
0101              tbl_size + sizeof(*final_tbl));
0102     efi_tpm_final_log_size = tbl_size;
0103 
0104 out_calc:
0105     early_memunmap(final_tbl, sizeof(*final_tbl));
0106 out:
0107     early_memunmap(log_tbl, sizeof(*log_tbl));
0108     return ret;
0109 }
0110