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