blob: 3401aef3364f0905fae94389cf93276e1ffcb0fe [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
236 struct cbfsf file;
237 /* The following have been removed from BIOS Data Table in version 6 */
238 if (!cbfs_boot_locate(&file, CONFIG_INTEL_TXT_CBFS_BIOS_POLICY, NULL)) {
239 struct region_device policy;
240
241 cbfs_file_data(&policy, &file);
242 void *policy_data = rdev_mmap_full(&policy);
243 size_t policy_len = region_device_sz(&policy);
244
245 if (policy_data && policy_len) {
246 /* Point to FIT Type 9 entry in flash */
247 data.bdr.lcp_pd_base = (uintptr_t)policy_data;
248 data.bdr.lcp_pd_size = (uint64_t)policy_len;
249 rdev_munmap(&policy, policy_data);
250 } else {
251 printk(BIOS_ERR, "TEE-TXT: Couldn't map LCP PD Policy from CBFS.\n");
252 }
253 } else {
254 printk(BIOS_ERR, "TEE-TXT: Couldn't locate LCP PD Policy in CBFS.\n");
255 }
256
257 data.bdr.support_acpi_ppi = 0;
258 data.bdr.platform_type = 0;
259
Angel Pons08de7d62020-10-16 01:01:14 +0200260 /* Fill in the version of the used TXT BIOS Specification */
261 data.spec.header.type = HEAP_EXTDATA_TYPE_BIOS_SPEC_VER;
262 data.spec.header.size = sizeof(data.spec);
263 data.spec.ver_major = 2;
264 data.spec.ver_minor = 1;
265 data.spec.ver_revision = 0;
266
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100267 /* Extended elements - ACM addresses */
268 data.heap_acm.header.type = HEAP_EXTDATA_TYPE_ACM;
269 data.heap_acm.header.size = sizeof(data.heap_acm);
270 if (data.bdr.bios_sinit_size) {
271 data.heap_acm.num_acms = 2;
272 data.heap_acm.acm_addrs[1] = (uintptr_t)sinit_base;
273 } else {
274 data.heap_acm.num_acms = 1;
275 }
276 data.heap_acm.acm_addrs[0] =
Julius Werner834b3ec2020-03-04 16:52:08 -0800277 (uintptr_t)cbfs_map(CONFIG_INTEL_TXT_CBFS_BIOS_ACM, NULL);
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100278 /* Extended elements - End marker */
279 data.end.type = HEAP_EXTDATA_TYPE_END;
280 data.end.size = sizeof(data.end);
281
282 /* Fill TXT.HEAP.BASE with 4 subregions */
283 u8 *heap_struct = (void *)((uintptr_t)read64((void *)TXT_HEAP_BASE));
284
285 /* BiosData */
286 push_sinit_heap(&heap_struct, &data, sizeof(data));
287
288 /* OsMLEData */
289 /* FIXME: Does firmware need to write this? */
290 push_sinit_heap(&heap_struct, NULL, 0);
291
292 /* OsSinitData */
293 /* FIXME: Does firmware need to write this? */
294 push_sinit_heap(&heap_struct, NULL, 0);
295
296 /* SinitMLEData */
297 /* FIXME: Does firmware need to write this? */
298 push_sinit_heap(&heap_struct, NULL, 0);
Angel Pons8f7e2a32020-10-16 01:07:18 +0200299}
300
301/**
302 * Finalize the TXT device.
303 *
304 * - Lock TXT register.
305 * - Protect TSEG using DMA protected regions.
306 * - Setup TXT regions.
307 * - Place SINIT ACM in TXT_SINIT memory segment.
308 * - Fill TXT BIOSDATA region.
309 */
310static void lockdown_intel_txt(void *unused)
311{
312 const uint64_t status = read64((void *)TXT_SPAD);
313
Angel Pons6c4028d2020-10-16 11:52:40 +0200314 uint32_t txt_feature_flags = 0;
Angel Pons8f7e2a32020-10-16 01:07:18 +0200315 uintptr_t tseg_base;
316 size_t tseg_size;
317
318 smm_region(&tseg_base, &tseg_size);
319
320 if (status & ACMSTS_TXT_DISABLED)
321 return;
322
Angel Pons6c4028d2020-10-16 11:52:40 +0200323 /*
324 * Document Number: 558294
325 * Chapter 5.4.3 Detection of Intel TXT Capability
326 */
Angel Pons8f7e2a32020-10-16 01:07:18 +0200327
Angel Pons6c4028d2020-10-16 11:52:40 +0200328 if (!getsec_parameter(NULL, NULL, NULL, NULL, NULL, &txt_feature_flags))
Angel Pons8f7e2a32020-10-16 01:07:18 +0200329 return;
Angel Pons6c4028d2020-10-16 11:52:40 +0200330
331 /* LockConfig only exists on Intel TXT for Servers */
332 if (txt_feature_flags & GETSEC_PARAMS_TXT_EXT_CRTM_SUPPORT) {
333 printk(BIOS_INFO, "TEE-TXT: Locking TEE...\n");
334
335 /* Lock TXT config, unlocks TXT_HEAP_BASE */
336 if (intel_txt_run_bios_acm(ACMINPUT_LOCK_CONFIG) < 0) {
337 printk(BIOS_ERR, "TEE-TXT: Failed to lock registers.\n");
338 printk(BIOS_ERR, "TEE-TXT: SINIT won't be supported.\n");
339 return;
340 }
Angel Pons8f7e2a32020-10-16 01:07:18 +0200341 }
342
343 /*
344 * Document Number: 558294
345 * Chapter 5.5.6.1 DMA Protection Memory Region
346 */
347
348 const u8 dpr_capable = !!(read64((void *)TXT_CAPABILITIES) &
349 TXT_CAPABILITIES_DPR);
350 printk(BIOS_INFO, "TEE-TXT: DPR capable %x\n", dpr_capable);
351
352 if (dpr_capable) {
353 /* Verify the DPR settings on the MCH and mirror them to TXT public space */
354 union dpr_register dpr = txt_get_chipset_dpr();
355
356 printk(BIOS_DEBUG, "TEE-TXT: MCH DPR 0x%08x\n", dpr.raw);
357
358 printk(BIOS_DEBUG, "TEE-TXT: MCH DPR base @ 0x%08x size %u MiB\n",
359 (dpr.top - dpr.size) * MiB, dpr.size);
360
361 // DPR TODO: implement SA_ENABLE_DPR in the intelblocks
362
363 if (!dpr.lock) {
364 printk(BIOS_ERR, "TEE-TXT: MCH DPR not locked.\n");
365 return;
366 }
367
368 if (!dpr.epm || !dpr.prs) {
369 printk(BIOS_ERR, "TEE-TXT: MCH DPR protection not active.\n");
370 return;
371 }
372
Arthur Heymans9059a892020-10-23 11:08:41 +0200373 _Static_assert(CONFIG_INTEL_TXT_HEAP_SIZE + CONFIG_INTEL_TXT_SINIT_SIZE
374 < CONFIG_INTEL_TXT_DPR_SIZE * MiB, "TXT Heap and Sinit must fit DPR");
375
Angel Pons8f7e2a32020-10-16 01:07:18 +0200376 if (dpr.size < CONFIG_INTEL_TXT_DPR_SIZE) {
377 printk(BIOS_ERR, "TEE-TXT: MCH DPR configured size is too small.\n");
378 return;
379 }
380
381 if (dpr.top * MiB != tseg_base) {
382 printk(BIOS_ERR, "TEE-TXT: MCH DPR top does not equal TSEG base.\n");
383 return;
384 }
385
386 /* Clear reserved bits */
387 dpr.prs = 0;
388 dpr.epm = 0;
389
390 write64((void *)TXT_DPR, dpr.raw);
391
392 printk(BIOS_INFO, "TEE-TXT: TXT.DPR 0x%08x\n",
393 read32((void *)TXT_DPR));
394 }
395
396 /*
397 * Document Number: 558294
398 * Chapter 5.5.6.3 Intel TXT Heap Memory Region
399 */
Arthur Heymans9059a892020-10-23 11:08:41 +0200400 write64((void *)TXT_HEAP_SIZE, CONFIG_INTEL_TXT_HEAP_SIZE);
Angel Pons8f7e2a32020-10-16 01:07:18 +0200401 write64((void *)TXT_HEAP_BASE,
402 ALIGN_DOWN(tseg_base - read64((void *)TXT_HEAP_SIZE), 4096));
403
404 /*
405 * Document Number: 558294
406 * Chapter 5.5.6.2 SINIT Memory Region
407 */
Arthur Heymans9059a892020-10-23 11:08:41 +0200408 write64((void *)TXT_SINIT_SIZE, CONFIG_INTEL_TXT_SINIT_SIZE);
Angel Pons8f7e2a32020-10-16 01:07:18 +0200409 write64((void *)TXT_SINIT_BASE,
410 ALIGN_DOWN(read64((void *)TXT_HEAP_BASE) -
411 read64((void *)TXT_SINIT_SIZE), 4096));
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100412
413 /*
414 * FIXME: Server-TXT capable platforms need to install an STM in SMM and set up MSEG.
415 */
416
417 /**
418 * Chapter 5.10.1 SMM in the Intel TXT for Servers Environment
419 * Disable MSEG.
420 */
421 write64((void *)TXT_MSEG_SIZE, 0);
422 write64((void *)TXT_MSEG_BASE, 0);
423
Angel Ponsc0376952020-10-16 01:12:00 +0200424 /* Only initialize the heap on regular boots */
425 if (!acpi_is_wakeup_s3())
426 txt_initialize_heap();
Angel Pons8f7e2a32020-10-16 01:07:18 +0200427
Philipp Deppenwiese5f9f7762018-11-20 14:22:15 +0100428 if (CONFIG(INTEL_TXT_LOGGING))
429 txt_dump_regions();
430}
431
432BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, lockdown_intel_txt, NULL);