blob: c830f975a6a3c9e709eeb5c93e89020adce16d1c [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"
Angel Ponsffbb4b22020-10-15 23:25:58 +020016#include "txt_platform.h"
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +010017#include "txt_register.h"
18#include "txt_getsec.h"
19
20/* FIXME: Seems to work only on some platforms */
21static void log_ibb_measurements(void)
22{
23 const uint64_t mseg_size = read64((void *)TXT_MSEG_SIZE);
24 uint64_t mseg_base = read64((void *)TXT_MSEG_BASE);
25
26 if (!mseg_size || !mseg_base || mseg_size <= mseg_base)
27 return;
28 /*
29 * MSEG SIZE and MSEG BASE might contain random values.
30 * Assume below 4GiB and 8byte aligned.
31 */
32 if (mseg_base & ~0xfffffff8ULL || mseg_size & ~0xfffffff8ULL)
33 return;
34
35 printk(BIOS_INFO, "TEE-TXT: IBB Hash 0x");
36 for (; mseg_base < mseg_size; mseg_base++)
37 printk(BIOS_INFO, "%02X", read8((void *)(uintptr_t)mseg_base));
38
39 printk(BIOS_INFO, "\n");
40}
41
42void bootmem_platform_add_ranges(void)
43{
44 uint64_t status = read64((void *)TXT_SPAD);
45
46 if (status & ACMSTS_TXT_DISABLED)
47 return;
48
49 /* Chapter 5.5.5 Intel TXT reserved memory */
50 bootmem_add_range(TXT_RESERVED_SPACE,
51 TXT_RESERVED_SPACE_SIZE,
52 BM_MEM_RESERVED);
53
54 /* Intel TPM decode memory */
55 bootmem_add_range(TXT_TPM_DECODE_AREA,
56 TXT_RESERVED_SPACE - TXT_TPM_DECODE_AREA,
57 BM_MEM_RESERVED);
58
59 /* Intel TXT public space memory */
60 bootmem_add_range(TXT_PUBLIC_SPACE,
61 TXT_TPM_DECODE_AREA - TXT_PUBLIC_SPACE,
62 BM_MEM_RESERVED);
63
64 /* Intel TXT private space memory */
65 bootmem_add_range(TXT_PRIVATE_SPACE,
66 TXT_PUBLIC_SPACE - TXT_PRIVATE_SPACE,
67 BM_MEM_RESERVED);
68
Angel Pons463e44b2020-10-05 13:58:16 +020069 const union dpr_register dpr = {
70 .raw = read32((void *)TXT_DPR),
71 };
72
73 const uint32_t dpr_base = dpr.top - dpr.size * MiB;
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +010074
75 /* Chapter 5.5.6 Intel TXT Device Memory */
Angel Pons463e44b2020-10-05 13:58:16 +020076 bootmem_add_range(dpr_base, dpr.size * MiB, BM_MEM_RESERVED);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +010077}
78
79static bool get_wake_error_status(void)
80{
81 const uint8_t error = read8((void *)TXT_ESTS);
82 return !!(error & TXT_ESTS_WAKE_ERROR_STS);
83}
84
85static void check_secrets_txt(void *unused)
86{
87 uint64_t status = read64((void *)TXT_SPAD);
88
89 if (status & ACMSTS_TXT_DISABLED)
90 return;
91
Arthur Heymansbccb6912020-11-24 17:37:11 +010092 /*
93 * Check if secrets bit needs to be reset. Only platforms that support
94 * CONFIG(PLATFORM_HAS_DRAM_CLEAR) will be able to run this code.
95 * On some platforms FSP-M takes care of the DRAM clearing.
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);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100103
Arthur Heymansbccb6912020-11-24 17:37:11 +0100104 /* Should never reach this point ... */
105 intel_txt_log_acm_error(read32((void *)TXT_BIOSACM_ERRORCODE));
106 die("Waiting for platform reset...\n");
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100107 }
108}
109
110BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_ENTRY, check_secrets_txt, NULL);
111
112/**
113 * Log TXT startup errors, check all bits for TXT, run BIOSACM using
114 * GETSEC[ENTERACCS].
115 *
116 * If a "TXT reset" is detected or "memory had secrets" is set, then do nothing as
117 * 1. Running ACMs will cause a TXT-RESET
118 * 2. Memory will be scrubbed in BS_DEV_INIT
119 * 3. TXT-RESET will be issued by code above later
120 *
121 */
122static void init_intel_txt(void *unused)
123{
124 const uint64_t status = read64((void *)TXT_SPAD);
125
126 if (status & ACMSTS_TXT_DISABLED)
127 return;
128
129 printk(BIOS_INFO, "TEE-TXT: Initializing TEE...\n");
130
131 intel_txt_log_spad();
132
133 if (CONFIG(INTEL_TXT_LOGGING)) {
134 intel_txt_log_bios_acm_error();
135 txt_dump_chipset_info();
136 }
137
138 printk(BIOS_INFO, "TEE-TXT: Validate TEE...\n");
139
140 if (intel_txt_prepare_txt_env()) {
141 printk(BIOS_ERR, "TEE-TXT: Failed to prepare TXT environment\n");
142 return;
143 }
144
145 /* Check for fatal ACM error and TXT reset */
146 if (get_wake_error_status()) {
147 /* Can't run ACMs with TXT_ESTS_WAKE_ERROR_STS set */
148 printk(BIOS_ERR, "TEE-TXT: Fatal BIOS ACM error reported\n");
149 return;
150 }
151
Angel Pons8a285fd82020-10-16 10:49:12 +0200152 if (CONFIG(INTEL_TXT_TEST_BIOS_ACM_CALLING_CODE)) {
153 printk(BIOS_INFO, "TEE-TXT: Testing BIOS ACM calling code...\n");
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100154
Angel Pons8a285fd82020-10-16 10:49:12 +0200155 /*
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,
163 "TEE-TXT: Error calling BIOS ACM with NOP function.\n");
164 return;
165 }
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100166 }
167
168 if (status & (ACMSTS_BIOS_TRUSTED | ACMSTS_IBB_MEASURED)) {
Angel Ponse70a3f82020-10-16 10:58:57 +0200169 printk(BIOS_INFO, "TEE-TXT: Logging IBB measurements...\n");
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100170 log_ibb_measurements();
Angel Ponse70a3f82020-10-16 10:58:57 +0200171 }
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100172
Angel Ponse70a3f82020-10-16 10:58:57 +0200173 int s3resume = acpi_is_wakeup_s3();
Arthur Heymans66dbd9c2021-01-06 14:12:47 +0100174 if (!s3resume && !CONFIG(INTEL_CBNT_SUPPORT)) {
Angel Ponse70a3f82020-10-16 10:58:57 +0200175 printk(BIOS_INFO, "TEE-TXT: Scheck...\n");
176 if (intel_txt_run_bios_acm(ACMINPUT_SCHECK) < 0) {
177 printk(BIOS_ERR, "TEE-TXT: Error calling BIOS ACM.\n");
178 return;
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100179 }
180 }
181}
182
183BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_EXIT, init_intel_txt, NULL);
184
185static void push_sinit_heap(u8 **heap_ptr, void *data, size_t data_length)
186{
187 /* Push size */
188 const uint64_t tmp = data_length + 8;
189 memcpy(*heap_ptr, &tmp, 8);
190 *heap_ptr += 8;
191
192 if (data_length) {
193 /* Push data */
194 memcpy(*heap_ptr, data, data_length);
195 *heap_ptr += data_length;
196 }
197}
198
Angel Pons8f7e2a32020-10-16 01:07:18 +0200199static void txt_initialize_heap(void)
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100200{
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100201 /*
202 * BIOS Data Format
203 * Chapter C.2
204 * Intel TXT Software Development Guide (Document: 315168-015)
205 */
206 struct {
207 struct txt_biosdataregion bdr;
Angel Pons08de7d62020-10-16 01:01:14 +0200208 struct txt_bios_spec_ver_element spec;
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100209 struct txt_heap_acm_element heap_acm;
210 struct txt_extended_data_element_header end;
211 } __packed data = {0};
212
213 /* TPM2.0 requires version 6 of BDT */
214 if (CONFIG(TPM2))
215 data.bdr.version = 6;
216 else
217 data.bdr.version = 5;
218
219 data.bdr.no_logical_procs = dev_count_cpu();
220
221 void *sinit_base = (void *)(uintptr_t)read64((void *)TXT_SINIT_BASE);
Julius Werner834b3ec2020-03-04 16:52:08 -0800222 data.bdr.bios_sinit_size = cbfs_load(CONFIG_INTEL_TXT_CBFS_SINIT_ACM,
223 sinit_base,
224 read64((void *)TXT_SINIT_SIZE));
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100225
226 if (data.bdr.bios_sinit_size) {
227 printk(BIOS_INFO, "TEE-TXT: Placing SINIT ACM in memory.\n");
228 if (CONFIG(INTEL_TXT_LOGGING))
229 txt_dump_acm_info(sinit_base);
230 } else {
231 printk(BIOS_ERR, "TEE-TXT: Couldn't locate SINIT ACM in CBFS.\n");
232 /* Clear memory */
233 memset(sinit_base, 0, read64((void *)TXT_SINIT_SIZE));
234 }
235
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100236 /* The following have been removed from BIOS Data Table in version 6 */
Julius Werner77639e42021-02-05 16:51:25 -0800237 size_t policy_len;
238 void *policy_data = cbfs_map(CONFIG_INTEL_TXT_CBFS_BIOS_POLICY, &policy_len);
239 if (policy_data) {
240 /* Point to FIT Type 9 entry in flash */
241 data.bdr.lcp_pd_base = (uintptr_t)policy_data;
242 data.bdr.lcp_pd_size = (uint64_t)policy_len;
243 cbfs_unmap(policy_data);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100244 } else {
245 printk(BIOS_ERR, "TEE-TXT: Couldn't locate LCP PD Policy in CBFS.\n");
246 }
247
248 data.bdr.support_acpi_ppi = 0;
249 data.bdr.platform_type = 0;
250
Angel Pons08de7d62020-10-16 01:01:14 +0200251 /* Fill in the version of the used TXT BIOS Specification */
252 data.spec.header.type = HEAP_EXTDATA_TYPE_BIOS_SPEC_VER;
253 data.spec.header.size = sizeof(data.spec);
254 data.spec.ver_major = 2;
255 data.spec.ver_minor = 1;
256 data.spec.ver_revision = 0;
257
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100258 /* Extended elements - ACM addresses */
259 data.heap_acm.header.type = HEAP_EXTDATA_TYPE_ACM;
260 data.heap_acm.header.size = sizeof(data.heap_acm);
261 if (data.bdr.bios_sinit_size) {
262 data.heap_acm.num_acms = 2;
263 data.heap_acm.acm_addrs[1] = (uintptr_t)sinit_base;
264 } else {
265 data.heap_acm.num_acms = 1;
266 }
267 data.heap_acm.acm_addrs[0] =
Julius Werner834b3ec2020-03-04 16:52:08 -0800268 (uintptr_t)cbfs_map(CONFIG_INTEL_TXT_CBFS_BIOS_ACM, NULL);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100269 /* Extended elements - End marker */
270 data.end.type = HEAP_EXTDATA_TYPE_END;
271 data.end.size = sizeof(data.end);
272
273 /* Fill TXT.HEAP.BASE with 4 subregions */
274 u8 *heap_struct = (void *)((uintptr_t)read64((void *)TXT_HEAP_BASE));
275
276 /* BiosData */
277 push_sinit_heap(&heap_struct, &data, sizeof(data));
278
279 /* OsMLEData */
280 /* FIXME: Does firmware need to write this? */
281 push_sinit_heap(&heap_struct, NULL, 0);
282
283 /* OsSinitData */
284 /* FIXME: Does firmware need to write this? */
285 push_sinit_heap(&heap_struct, NULL, 0);
286
287 /* SinitMLEData */
288 /* FIXME: Does firmware need to write this? */
289 push_sinit_heap(&heap_struct, NULL, 0);
Angel Pons8f7e2a32020-10-16 01:07:18 +0200290}
291
292/**
293 * Finalize the TXT device.
294 *
295 * - Lock TXT register.
296 * - Protect TSEG using DMA protected regions.
297 * - Setup TXT regions.
298 * - Place SINIT ACM in TXT_SINIT memory segment.
299 * - Fill TXT BIOSDATA region.
300 */
301static void lockdown_intel_txt(void *unused)
302{
303 const uint64_t status = read64((void *)TXT_SPAD);
304
Angel Pons6c4028d2020-10-16 11:52:40 +0200305 uint32_t txt_feature_flags = 0;
Angel Pons8f7e2a32020-10-16 01:07:18 +0200306 uintptr_t tseg_base;
307 size_t tseg_size;
308
309 smm_region(&tseg_base, &tseg_size);
310
311 if (status & ACMSTS_TXT_DISABLED)
312 return;
313
Angel Pons6c4028d2020-10-16 11:52:40 +0200314 /*
315 * Document Number: 558294
316 * Chapter 5.4.3 Detection of Intel TXT Capability
317 */
Angel Pons8f7e2a32020-10-16 01:07:18 +0200318
Angel Pons6c4028d2020-10-16 11:52:40 +0200319 if (!getsec_parameter(NULL, NULL, NULL, NULL, NULL, &txt_feature_flags))
Angel Pons8f7e2a32020-10-16 01:07:18 +0200320 return;
Angel Pons6c4028d2020-10-16 11:52:40 +0200321
322 /* LockConfig only exists on Intel TXT for Servers */
323 if (txt_feature_flags & GETSEC_PARAMS_TXT_EXT_CRTM_SUPPORT) {
324 printk(BIOS_INFO, "TEE-TXT: Locking TEE...\n");
325
326 /* Lock TXT config, unlocks TXT_HEAP_BASE */
327 if (intel_txt_run_bios_acm(ACMINPUT_LOCK_CONFIG) < 0) {
328 printk(BIOS_ERR, "TEE-TXT: Failed to lock registers.\n");
329 printk(BIOS_ERR, "TEE-TXT: SINIT won't be supported.\n");
330 return;
331 }
Angel Pons8f7e2a32020-10-16 01:07:18 +0200332 }
333
334 /*
335 * Document Number: 558294
336 * Chapter 5.5.6.1 DMA Protection Memory Region
337 */
338
339 const u8 dpr_capable = !!(read64((void *)TXT_CAPABILITIES) &
340 TXT_CAPABILITIES_DPR);
341 printk(BIOS_INFO, "TEE-TXT: DPR capable %x\n", dpr_capable);
342
343 if (dpr_capable) {
344 /* Verify the DPR settings on the MCH and mirror them to TXT public space */
345 union dpr_register dpr = txt_get_chipset_dpr();
346
347 printk(BIOS_DEBUG, "TEE-TXT: MCH DPR 0x%08x\n", dpr.raw);
348
349 printk(BIOS_DEBUG, "TEE-TXT: MCH DPR base @ 0x%08x size %u MiB\n",
350 (dpr.top - dpr.size) * MiB, dpr.size);
351
352 // DPR TODO: implement SA_ENABLE_DPR in the intelblocks
353
354 if (!dpr.lock) {
355 printk(BIOS_ERR, "TEE-TXT: MCH DPR not locked.\n");
356 return;
357 }
358
359 if (!dpr.epm || !dpr.prs) {
360 printk(BIOS_ERR, "TEE-TXT: MCH DPR protection not active.\n");
361 return;
362 }
363
Arthur Heymans9059a892020-10-23 11:08:41 +0200364 _Static_assert(CONFIG_INTEL_TXT_HEAP_SIZE + CONFIG_INTEL_TXT_SINIT_SIZE
365 < CONFIG_INTEL_TXT_DPR_SIZE * MiB, "TXT Heap and Sinit must fit DPR");
366
Angel Pons8f7e2a32020-10-16 01:07:18 +0200367 if (dpr.size < CONFIG_INTEL_TXT_DPR_SIZE) {
368 printk(BIOS_ERR, "TEE-TXT: MCH DPR configured size is too small.\n");
369 return;
370 }
371
372 if (dpr.top * MiB != tseg_base) {
373 printk(BIOS_ERR, "TEE-TXT: MCH DPR top does not equal TSEG base.\n");
374 return;
375 }
376
377 /* Clear reserved bits */
378 dpr.prs = 0;
379 dpr.epm = 0;
380
381 write64((void *)TXT_DPR, dpr.raw);
382
383 printk(BIOS_INFO, "TEE-TXT: TXT.DPR 0x%08x\n",
384 read32((void *)TXT_DPR));
385 }
386
387 /*
388 * Document Number: 558294
389 * Chapter 5.5.6.3 Intel TXT Heap Memory Region
390 */
Arthur Heymans9059a892020-10-23 11:08:41 +0200391 write64((void *)TXT_HEAP_SIZE, CONFIG_INTEL_TXT_HEAP_SIZE);
Angel Pons8f7e2a32020-10-16 01:07:18 +0200392 write64((void *)TXT_HEAP_BASE,
393 ALIGN_DOWN(tseg_base - read64((void *)TXT_HEAP_SIZE), 4096));
394
395 /*
396 * Document Number: 558294
397 * Chapter 5.5.6.2 SINIT Memory Region
398 */
Arthur Heymans9059a892020-10-23 11:08:41 +0200399 write64((void *)TXT_SINIT_SIZE, CONFIG_INTEL_TXT_SINIT_SIZE);
Angel Pons8f7e2a32020-10-16 01:07:18 +0200400 write64((void *)TXT_SINIT_BASE,
401 ALIGN_DOWN(read64((void *)TXT_HEAP_BASE) -
402 read64((void *)TXT_SINIT_SIZE), 4096));
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100403
404 /*
405 * FIXME: Server-TXT capable platforms need to install an STM in SMM and set up MSEG.
406 */
407
408 /**
409 * Chapter 5.10.1 SMM in the Intel TXT for Servers Environment
410 * Disable MSEG.
411 */
412 write64((void *)TXT_MSEG_SIZE, 0);
413 write64((void *)TXT_MSEG_BASE, 0);
414
Angel Ponsc0376952020-10-16 01:12:00 +0200415 /* Only initialize the heap on regular boots */
416 if (!acpi_is_wakeup_s3())
417 txt_initialize_heap();
Angel Pons8f7e2a32020-10-16 01:07:18 +0200418
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100419 if (CONFIG(INTEL_TXT_LOGGING))
420 txt_dump_regions();
421}
422
423BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, lockdown_intel_txt, NULL);