1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2005 IBM Corporation
4 *
5 * Authors:
6 * Seiji Munetoh <munetoh@jp.ibm.com>
7 * Stefan Berger <stefanb@us.ibm.com>
8 * Reiner Sailer <sailer@watson.ibm.com>
9 * Kylene Hall <kjhall@us.ibm.com>
10 * Nayna Jain <nayna@linux.vnet.ibm.com>
11 *
12 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13 *
14 * Access to the event log extended by the TCG BIOS of PC platform
15 */
16
17 #include <linux/seq_file.h>
18 #include <linux/fs.h>
19 #include <linux/security.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/acpi.h>
23 #include <linux/tpm_eventlog.h>
24
25 #include "../tpm.h"
26 #include "common.h"
27
28 struct acpi_tcpa {
29 struct acpi_table_header hdr;
30 u16 platform_class;
31 union {
32 struct client_hdr {
33 u32 log_max_len __packed;
34 u64 log_start_addr __packed;
35 } client;
36 struct server_hdr {
37 u16 reserved;
38 u64 log_max_len __packed;
39 u64 log_start_addr __packed;
40 } server;
41 };
42 };
43
44 /* read binary bios log */
tpm_read_log_acpi(struct tpm_chip * chip)45 int tpm_read_log_acpi(struct tpm_chip *chip)
46 {
47 struct acpi_tcpa *buff;
48 acpi_status status;
49 void __iomem *virt;
50 u64 len, start;
51 struct tpm_bios_log *log;
52 struct acpi_table_tpm2 *tbl;
53 struct acpi_tpm2_phy *tpm2_phy;
54 int format;
55
56 log = &chip->log;
57
58 /* Unfortuntely ACPI does not associate the event log with a specific
59 * TPM, like PPI. Thus all ACPI TPMs will read the same log.
60 */
61 if (!chip->acpi_dev_handle)
62 return -ENODEV;
63
64 if (chip->flags & TPM_CHIP_FLAG_TPM2) {
65 status = acpi_get_table("TPM2", 1,
66 (struct acpi_table_header **)&tbl);
67 if (ACPI_FAILURE(status))
68 return -ENODEV;
69
70 if (tbl->header.length <
71 sizeof(*tbl) + sizeof(struct acpi_tpm2_phy))
72 return -ENODEV;
73
74 tpm2_phy = (void *)tbl + sizeof(*tbl);
75 len = tpm2_phy->log_area_minimum_length;
76
77 start = tpm2_phy->log_area_start_address;
78 if (!start || !len)
79 return -ENODEV;
80
81 format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
82 } else {
83 /* Find TCPA entry in RSDT (ACPI_LOGICAL_ADDRESSING) */
84 status = acpi_get_table(ACPI_SIG_TCPA, 1,
85 (struct acpi_table_header **)&buff);
86 if (ACPI_FAILURE(status))
87 return -ENODEV;
88
89 switch (buff->platform_class) {
90 case BIOS_SERVER:
91 len = buff->server.log_max_len;
92 start = buff->server.log_start_addr;
93 break;
94 case BIOS_CLIENT:
95 default:
96 len = buff->client.log_max_len;
97 start = buff->client.log_start_addr;
98 break;
99 }
100
101 format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
102 }
103 if (!len) {
104 dev_warn(&chip->dev, "%s: TCPA log area empty\n", __func__);
105 return -EIO;
106 }
107
108 /* malloc EventLog space */
109 log->bios_event_log = kmalloc(len, GFP_KERNEL);
110 if (!log->bios_event_log)
111 return -ENOMEM;
112
113 log->bios_event_log_end = log->bios_event_log + len;
114
115 virt = acpi_os_map_iomem(start, len);
116 if (!virt)
117 goto err;
118
119 memcpy_fromio(log->bios_event_log, virt, len);
120
121 acpi_os_unmap_iomem(virt, len);
122 return format;
123
124 err:
125 kfree(log->bios_event_log);
126 log->bios_event_log = NULL;
127 return -EIO;
128
129 }
130