xref: /linux/drivers/char/tpm/eventlog/acpi.c (revision 156010ed9c2ac1e9df6c11b1f688cf8a6e0152e6)
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 /* Check that the given log is indeed a TPM2 log. */
45 static bool tpm_is_tpm2_log(void *bios_event_log, u64 len)
46 {
47 	struct tcg_efi_specid_event_head *efispecid;
48 	struct tcg_pcr_event *event_header;
49 	int n;
50 
51 	if (len < sizeof(*event_header))
52 		return false;
53 	len -= sizeof(*event_header);
54 	event_header = bios_event_log;
55 
56 	if (len < sizeof(*efispecid))
57 		return false;
58 	efispecid = (struct tcg_efi_specid_event_head *)event_header->event;
59 
60 	n = memcmp(efispecid->signature, TCG_SPECID_SIG,
61 		   sizeof(TCG_SPECID_SIG));
62 	return n == 0;
63 }
64 
65 /* read binary bios log */
66 int tpm_read_log_acpi(struct tpm_chip *chip)
67 {
68 	struct acpi_tcpa *buff;
69 	acpi_status status;
70 	void __iomem *virt;
71 	u64 len, start;
72 	struct tpm_bios_log *log;
73 	struct acpi_table_tpm2 *tbl;
74 	struct acpi_tpm2_phy *tpm2_phy;
75 	int format;
76 	int ret;
77 
78 	log = &chip->log;
79 
80 	/* Unfortuntely ACPI does not associate the event log with a specific
81 	 * TPM, like PPI. Thus all ACPI TPMs will read the same log.
82 	 */
83 	if (!chip->acpi_dev_handle)
84 		return -ENODEV;
85 
86 	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
87 		status = acpi_get_table("TPM2", 1,
88 					(struct acpi_table_header **)&tbl);
89 		if (ACPI_FAILURE(status))
90 			return -ENODEV;
91 
92 		if (tbl->header.length <
93 				sizeof(*tbl) + sizeof(struct acpi_tpm2_phy)) {
94 			acpi_put_table((struct acpi_table_header *)tbl);
95 			return -ENODEV;
96 		}
97 
98 		tpm2_phy = (void *)tbl + sizeof(*tbl);
99 		len = tpm2_phy->log_area_minimum_length;
100 
101 		start = tpm2_phy->log_area_start_address;
102 		if (!start || !len) {
103 			acpi_put_table((struct acpi_table_header *)tbl);
104 			return -ENODEV;
105 		}
106 
107 		acpi_put_table((struct acpi_table_header *)tbl);
108 		format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
109 	} else {
110 		/* Find TCPA entry in RSDT (ACPI_LOGICAL_ADDRESSING) */
111 		status = acpi_get_table(ACPI_SIG_TCPA, 1,
112 					(struct acpi_table_header **)&buff);
113 		if (ACPI_FAILURE(status))
114 			return -ENODEV;
115 
116 		switch (buff->platform_class) {
117 		case BIOS_SERVER:
118 			len = buff->server.log_max_len;
119 			start = buff->server.log_start_addr;
120 			break;
121 		case BIOS_CLIENT:
122 		default:
123 			len = buff->client.log_max_len;
124 			start = buff->client.log_start_addr;
125 			break;
126 		}
127 
128 		acpi_put_table((struct acpi_table_header *)buff);
129 		format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
130 	}
131 
132 	if (!len) {
133 		dev_warn(&chip->dev, "%s: TCPA log area empty\n", __func__);
134 		return -EIO;
135 	}
136 
137 	/* malloc EventLog space */
138 	log->bios_event_log = kmalloc(len, GFP_KERNEL);
139 	if (!log->bios_event_log)
140 		return -ENOMEM;
141 
142 	log->bios_event_log_end = log->bios_event_log + len;
143 
144 	ret = -EIO;
145 	virt = acpi_os_map_iomem(start, len);
146 	if (!virt)
147 		goto err;
148 
149 	memcpy_fromio(log->bios_event_log, virt, len);
150 
151 	acpi_os_unmap_iomem(virt, len);
152 
153 	if (chip->flags & TPM_CHIP_FLAG_TPM2 &&
154 	    !tpm_is_tpm2_log(log->bios_event_log, len)) {
155 		/* try EFI log next */
156 		ret = -ENODEV;
157 		goto err;
158 	}
159 
160 	return format;
161 
162 err:
163 	kfree(log->bios_event_log);
164 	log->bios_event_log = NULL;
165 	return ret;
166 }
167