blob: 263bc9daa6ffb289f94df562ac77b484473cf125 [file] [log] [blame]
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +01003#include <acpi/acpi.h>
Angel Pons52082be2020-10-05 12:34:29 +02004#include <arch/mmio.h>
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +01005#include <bootmem.h>
Angel Pons52082be2020-10-05 12:34:29 +02006#include <bootstate.h>
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +01007#include <cbfs.h>
Angel Pons52082be2020-10-05 12:34:29 +02008#include <console/console.h>
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +01009#include <cpu/intel/common/common.h>
10#include <cpu/x86/msr.h>
Angel Pons11334722020-10-05 16:34:03 +020011#include <cpu/x86/smm.h>
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +010012#include <device/pci_ops.h>
Angel Pons52082be2020-10-05 12:34:29 +020013#include <types.h>
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +010014
15#include "txt.h"
16#include "txt_register.h"
17#include "txt_getsec.h"
18
19/* FIXME: Seems to work only on some platforms */
20static void log_ibb_measurements(void)
21{
22 const uint64_t mseg_size = read64((void *)TXT_MSEG_SIZE);
23 uint64_t mseg_base = read64((void *)TXT_MSEG_BASE);
24
25 if (!mseg_size || !mseg_base || mseg_size <= mseg_base)
26 return;
27 /*
28 * MSEG SIZE and MSEG BASE might contain random values.
29 * Assume below 4GiB and 8byte aligned.
30 */
31 if (mseg_base & ~0xfffffff8ULL || mseg_size & ~0xfffffff8ULL)
32 return;
33
34 printk(BIOS_INFO, "TEE-TXT: IBB Hash 0x");
35 for (; mseg_base < mseg_size; mseg_base++)
36 printk(BIOS_INFO, "%02X", read8((void *)(uintptr_t)mseg_base));
37
38 printk(BIOS_INFO, "\n");
39}
40
41void bootmem_platform_add_ranges(void)
42{
43 uint64_t status = read64((void *)TXT_SPAD);
44
45 if (status & ACMSTS_TXT_DISABLED)
46 return;
47
48 /* Chapter 5.5.5 Intel TXT reserved memory */
49 bootmem_add_range(TXT_RESERVED_SPACE,
50 TXT_RESERVED_SPACE_SIZE,
51 BM_MEM_RESERVED);
52
53 /* Intel TPM decode memory */
54 bootmem_add_range(TXT_TPM_DECODE_AREA,
55 TXT_RESERVED_SPACE - TXT_TPM_DECODE_AREA,
56 BM_MEM_RESERVED);
57
58 /* Intel TXT public space memory */
59 bootmem_add_range(TXT_PUBLIC_SPACE,
60 TXT_TPM_DECODE_AREA - TXT_PUBLIC_SPACE,
61 BM_MEM_RESERVED);
62
63 /* Intel TXT private space memory */
64 bootmem_add_range(TXT_PRIVATE_SPACE,
65 TXT_PUBLIC_SPACE - TXT_PRIVATE_SPACE,
66 BM_MEM_RESERVED);
67
Angel Pons463e44b2020-10-05 13:58:16 +020068 const union dpr_register dpr = {
69 .raw = read32((void *)TXT_DPR),
70 };
71
72 const uint32_t dpr_base = dpr.top - dpr.size * MiB;
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +010073
74 /* Chapter 5.5.6 Intel TXT Device Memory */
Angel Pons463e44b2020-10-05 13:58:16 +020075 bootmem_add_range(dpr_base, dpr.size * MiB, BM_MEM_RESERVED);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +010076}
77
78static bool get_wake_error_status(void)
79{
80 const uint8_t error = read8((void *)TXT_ESTS);
81 return !!(error & TXT_ESTS_WAKE_ERROR_STS);
82}
83
84static void check_secrets_txt(void *unused)
85{
86 uint64_t status = read64((void *)TXT_SPAD);
87
88 if (status & ACMSTS_TXT_DISABLED)
89 return;
90
91 /* Check for fatal ACM error and TXT reset */
92 if (get_wake_error_status()) {
93 /*
94 * Check if secrets bit needs to be reset. Only platforms that support
95 * CONFIG(PLATFORM_HAS_DRAM_CLEAR) will be able to run this code.
96 * Assume all memory really was cleared.
97 *
98 * TXT will issue a platform reset to come up sober.
99 */
100 if (intel_txt_memory_has_secrets()) {
101 printk(BIOS_INFO, "TEE-TXT: Wiping TEE...\n");
102 intel_txt_run_bios_acm(ACMINPUT_CLEAR_SECRETS);
103
104 /* Should never reach this point ... */
105 intel_txt_log_acm_error(read32((void *)TXT_BIOSACM_ERRORCODE));
106 die("Waiting for platform reset...\n");
107 }
108 }
109}
110
111BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_ENTRY, check_secrets_txt, NULL);
112
113/**
114 * Log TXT startup errors, check all bits for TXT, run BIOSACM using
115 * GETSEC[ENTERACCS].
116 *
117 * If a "TXT reset" is detected or "memory had secrets" is set, then do nothing as
118 * 1. Running ACMs will cause a TXT-RESET
119 * 2. Memory will be scrubbed in BS_DEV_INIT
120 * 3. TXT-RESET will be issued by code above later
121 *
122 */
123static void init_intel_txt(void *unused)
124{
125 const uint64_t status = read64((void *)TXT_SPAD);
126
127 if (status & ACMSTS_TXT_DISABLED)
128 return;
129
130 printk(BIOS_INFO, "TEE-TXT: Initializing TEE...\n");
131
132 intel_txt_log_spad();
133
134 if (CONFIG(INTEL_TXT_LOGGING)) {
135 intel_txt_log_bios_acm_error();
136 txt_dump_chipset_info();
137 }
138
139 printk(BIOS_INFO, "TEE-TXT: Validate TEE...\n");
140
141 if (intel_txt_prepare_txt_env()) {
142 printk(BIOS_ERR, "TEE-TXT: Failed to prepare TXT environment\n");
143 return;
144 }
145
146 /* Check for fatal ACM error and TXT reset */
147 if (get_wake_error_status()) {
148 /* Can't run ACMs with TXT_ESTS_WAKE_ERROR_STS set */
149 printk(BIOS_ERR, "TEE-TXT: Fatal BIOS ACM error reported\n");
150 return;
151 }
152
153 printk(BIOS_INFO, "TEE-TXT: Testing BIOS ACM calling code...\n");
154
155 /*
156 * Test BIOS ACM code.
157 * ACM should do nothing on reserved functions, and return an error code
158 * in TXT_BIOSACM_ERRORCODE. Tests showed that this is not true.
159 * Use special function "NOP" that does 'nothing'.
160 */
161 if (intel_txt_run_bios_acm(ACMINPUT_NOP) < 0) {
162 printk(BIOS_ERR, "TEE-TXT: Error calling BIOS ACM with NOP function.\n");
163 return;
164 }
165
166 if (status & (ACMSTS_BIOS_TRUSTED | ACMSTS_IBB_MEASURED)) {
167 log_ibb_measurements();
168
169 int s3resume = acpi_is_wakeup_s3();
170 if (!s3resume) {
171 printk(BIOS_INFO, "TEE-TXT: Scheck...\n");
172 if (intel_txt_run_bios_acm(ACMINPUT_SCHECK) < 0) {
173 printk(BIOS_ERR, "TEE-TXT: Error calling BIOS ACM.\n");
174 return;
175 }
176 }
177 }
178}
179
180BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_EXIT, init_intel_txt, NULL);
181
182static void push_sinit_heap(u8 **heap_ptr, void *data, size_t data_length)
183{
184 /* Push size */
185 const uint64_t tmp = data_length + 8;
186 memcpy(*heap_ptr, &tmp, 8);
187 *heap_ptr += 8;
188
189 if (data_length) {
190 /* Push data */
191 memcpy(*heap_ptr, data, data_length);
192 *heap_ptr += data_length;
193 }
194}
195
196/**
197 * Finalize the TXT device.
198 *
199 * - Lock TXT register.
200 * - Protect TSEG using DMA protected regions.
201 * - Setup TXT regions.
202 * - Place SINIT ACM in TXT_SINIT memory segment.
203 * - Fill TXT BIOSDATA region.
204 */
205static void lockdown_intel_txt(void *unused)
206{
207 const uint64_t status = read64((void *)TXT_SPAD);
Angel Pons11334722020-10-05 16:34:03 +0200208
209 uintptr_t tseg_base;
210 size_t tseg_size;
211
212 smm_region(&tseg_base, &tseg_size);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100213
214 if (status & ACMSTS_TXT_DISABLED)
215 return;
216
217 printk(BIOS_INFO, "TEE-TXT: Locking TEE...\n");
218
219 /* Lock TXT config, unlocks TXT_HEAP_BASE */
220 if (intel_txt_run_bios_acm(ACMINPUT_LOCK_CONFIG) < 0) {
221 printk(BIOS_ERR, "TEE-TXT: Failed to lock registers.\n");
222 printk(BIOS_ERR, "TEE-TXT: SINIT won't be supported.\n");
223 return;
224 }
225
226 /*
227 * Document Number: 558294
228 * Chapter 5.5.6.1 DMA Protection Memory Region
229 */
230
231 const u8 dpr_capable = !!(read64((void *)TXT_CAPABILITIES) &
232 TXT_CAPABILITIES_DPR);
233 printk(BIOS_INFO, "TEE-TXT: DPR capable %x\n", dpr_capable);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100234
Angel Pons463e44b2020-10-05 13:58:16 +0200235 if (dpr_capable) {
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100236 /* Protect 3 MiB below TSEG and lock register */
Angel Pons463e44b2020-10-05 13:58:16 +0200237 union dpr_register dpr = {
238 .lock = 1,
239 .size = 3,
Angel Pons11334722020-10-05 16:34:03 +0200240 .top = tseg_base / MiB,
Angel Pons463e44b2020-10-05 13:58:16 +0200241 };
242 write64((void *)TXT_DPR, dpr.raw);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100243
244 // DPR TODO: implement SA_ENABLE_DPR in the intelblocks
245
246 printk(BIOS_INFO, "TEE-TXT: TXT.DPR 0x%08x\n",
247 read32((void *)TXT_DPR));
248 }
249
250 /*
251 * Document Number: 558294
252 * Chapter 5.5.6.3 Intel TXT Heap Memory Region
253 */
254 write64((void *)TXT_HEAP_SIZE, 0xE0000);
255 write64((void *)TXT_HEAP_BASE,
Angel Pons11334722020-10-05 16:34:03 +0200256 ALIGN_DOWN(tseg_base - read64((void *)TXT_HEAP_SIZE), 4096));
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100257
258 /*
259 * Document Number: 558294
260 * Chapter 5.5.6.2 SINIT Memory Region
261 */
262 write64((void *)TXT_SINIT_SIZE, 0x20000);
263 write64((void *)TXT_SINIT_BASE,
264 ALIGN_DOWN(read64((void *)TXT_HEAP_BASE) -
265 read64((void *)TXT_SINIT_SIZE), 4096));
266
267 /*
268 * BIOS Data Format
269 * Chapter C.2
270 * Intel TXT Software Development Guide (Document: 315168-015)
271 */
272 struct {
273 struct txt_biosdataregion bdr;
274 struct txt_heap_acm_element heap_acm;
275 struct txt_extended_data_element_header end;
276 } __packed data = {0};
277
278 /* TPM2.0 requires version 6 of BDT */
279 if (CONFIG(TPM2))
280 data.bdr.version = 6;
281 else
282 data.bdr.version = 5;
283
284 data.bdr.no_logical_procs = dev_count_cpu();
285
286 void *sinit_base = (void *)(uintptr_t)read64((void *)TXT_SINIT_BASE);
287 data.bdr.bios_sinit_size = cbfs_boot_load_file(CONFIG_INTEL_TXT_CBFS_SINIT_ACM,
288 sinit_base,
289 read64((void *)TXT_SINIT_SIZE),
290 CBFS_TYPE_RAW);
291
292 if (data.bdr.bios_sinit_size) {
293 printk(BIOS_INFO, "TEE-TXT: Placing SINIT ACM in memory.\n");
294 if (CONFIG(INTEL_TXT_LOGGING))
295 txt_dump_acm_info(sinit_base);
296 } else {
297 printk(BIOS_ERR, "TEE-TXT: Couldn't locate SINIT ACM in CBFS.\n");
298 /* Clear memory */
299 memset(sinit_base, 0, read64((void *)TXT_SINIT_SIZE));
300 }
301
302 struct cbfsf file;
303 /* The following have been removed from BIOS Data Table in version 6 */
304 if (!cbfs_boot_locate(&file, CONFIG_INTEL_TXT_CBFS_BIOS_POLICY, NULL)) {
305 struct region_device policy;
306
307 cbfs_file_data(&policy, &file);
308 void *policy_data = rdev_mmap_full(&policy);
309 size_t policy_len = region_device_sz(&policy);
310
311 if (policy_data && policy_len) {
312 /* Point to FIT Type 9 entry in flash */
313 data.bdr.lcp_pd_base = (uintptr_t)policy_data;
314 data.bdr.lcp_pd_size = (uint64_t)policy_len;
315 rdev_munmap(&policy, policy_data);
316 } else {
317 printk(BIOS_ERR, "TEE-TXT: Couldn't map LCP PD Policy from CBFS.\n");
318 }
319 } else {
320 printk(BIOS_ERR, "TEE-TXT: Couldn't locate LCP PD Policy in CBFS.\n");
321 }
322
323 data.bdr.support_acpi_ppi = 0;
324 data.bdr.platform_type = 0;
325
326 /* Extended elements - ACM addresses */
327 data.heap_acm.header.type = HEAP_EXTDATA_TYPE_ACM;
328 data.heap_acm.header.size = sizeof(data.heap_acm);
329 if (data.bdr.bios_sinit_size) {
330 data.heap_acm.num_acms = 2;
331 data.heap_acm.acm_addrs[1] = (uintptr_t)sinit_base;
332 } else {
333 data.heap_acm.num_acms = 1;
334 }
335 data.heap_acm.acm_addrs[0] =
336 (uintptr_t)cbfs_boot_map_with_leak(CONFIG_INTEL_TXT_CBFS_BIOS_ACM,
337 CBFS_TYPE_RAW,
338 NULL);
339 /* Extended elements - End marker */
340 data.end.type = HEAP_EXTDATA_TYPE_END;
341 data.end.size = sizeof(data.end);
342
343 /* Fill TXT.HEAP.BASE with 4 subregions */
344 u8 *heap_struct = (void *)((uintptr_t)read64((void *)TXT_HEAP_BASE));
345
346 /* BiosData */
347 push_sinit_heap(&heap_struct, &data, sizeof(data));
348
349 /* OsMLEData */
350 /* FIXME: Does firmware need to write this? */
351 push_sinit_heap(&heap_struct, NULL, 0);
352
353 /* OsSinitData */
354 /* FIXME: Does firmware need to write this? */
355 push_sinit_heap(&heap_struct, NULL, 0);
356
357 /* SinitMLEData */
358 /* FIXME: Does firmware need to write this? */
359 push_sinit_heap(&heap_struct, NULL, 0);
360
361 /*
362 * FIXME: Server-TXT capable platforms need to install an STM in SMM and set up MSEG.
363 */
364
365 /**
366 * Chapter 5.10.1 SMM in the Intel TXT for Servers Environment
367 * Disable MSEG.
368 */
369 write64((void *)TXT_MSEG_SIZE, 0);
370 write64((void *)TXT_MSEG_BASE, 0);
371
372 if (CONFIG(INTEL_TXT_LOGGING))
373 txt_dump_regions();
374}
375
376BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, lockdown_intel_txt, NULL);