Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 5 | * Copyright 2017 Facebook Inc. |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 6 | * Copyright 2018 Siemens AG |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <console/cbmem_console.h> |
| 19 | #include <console/console.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 20 | #include <security/tpm/tspi.h> |
| 21 | #include <security/tpm/tss.h> |
| 22 | #include <stdlib.h> |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 23 | #if CONFIG(VBOOT) |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 24 | #include <vb2_api.h> |
Joel Kitching | 2eb89c8 | 2019-04-25 17:45:12 +0800 | [diff] [blame] | 25 | #include <vb2_sha.h> |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 26 | #include <assert.h> |
| 27 | #endif |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 28 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 29 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 30 | static uint32_t tpm1_invoke_state_machine(void) |
| 31 | { |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 32 | uint8_t disabled; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 33 | uint8_t deactivated; |
| 34 | uint32_t result = TPM_SUCCESS; |
| 35 | |
| 36 | /* Check that the TPM is enabled and activated. */ |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 37 | result = tlcl_get_flags(&disabled, &deactivated, NULL); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 38 | if (result != TPM_SUCCESS) { |
| 39 | printk(BIOS_ERR, "TPM: Can't read capabilities.\n"); |
| 40 | return result; |
| 41 | } |
| 42 | |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 43 | if (disabled) { |
| 44 | printk(BIOS_INFO, "TPM: is disabled. Enabling...\n"); |
| 45 | |
| 46 | result = tlcl_set_enable(); |
| 47 | if (result != TPM_SUCCESS) { |
| 48 | printk(BIOS_ERR, "TPM: Can't set enabled state.\n"); |
| 49 | return result; |
| 50 | } |
| 51 | } |
| 52 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 53 | if (!!deactivated != CONFIG(TPM_DEACTIVATE)) { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 54 | printk(BIOS_INFO, |
| 55 | "TPM: Unexpected TPM deactivated state. Toggling...\n"); |
| 56 | result = tlcl_set_deactivated(!deactivated); |
| 57 | if (result != TPM_SUCCESS) { |
| 58 | printk(BIOS_ERR, |
| 59 | "TPM: Can't toggle deactivated state.\n"); |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | deactivated = !deactivated; |
| 64 | result = TPM_E_MUST_REBOOT; |
| 65 | } |
| 66 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 67 | return result; |
| 68 | } |
| 69 | #endif |
| 70 | |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 71 | static uint32_t tpm_setup_s3_helper(void) |
| 72 | { |
| 73 | uint32_t result; |
| 74 | |
| 75 | result = tlcl_resume(); |
| 76 | switch (result) { |
| 77 | case TPM_SUCCESS: |
| 78 | break; |
| 79 | |
| 80 | case TPM_E_INVALID_POSTINIT: |
| 81 | /* |
| 82 | * We're on a platform where the TPM maintains power |
| 83 | * in S3, so it's already initialized. |
| 84 | */ |
| 85 | printk(BIOS_INFO, "TPM: Already initialized.\n"); |
| 86 | result = TPM_SUCCESS; |
| 87 | break; |
| 88 | |
| 89 | default: |
| 90 | printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", result); |
| 91 | break; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | static uint32_t tpm_setup_epilogue(uint32_t result) |
| 98 | { |
| 99 | if (result != TPM_SUCCESS) |
| 100 | post_code(POST_TPM_FAILURE); |
| 101 | else |
| 102 | printk(BIOS_INFO, "TPM: setup succeeded\n"); |
| 103 | |
| 104 | return result; |
| 105 | } |
| 106 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 107 | /* |
| 108 | * tpm_setup starts the TPM and establishes the root of trust for the |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 109 | * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug. |
| 110 | * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 111 | * general we cannot easily distinguish the kind of failure, so our strategy is |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 112 | * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 113 | * again, which executes (almost) the same sequence of operations. There is a |
| 114 | * good chance that, if recovery mode was entered because of a TPM failure, the |
| 115 | * failure will repeat itself. (In general this is impossible to guarantee |
| 116 | * because we have no way of creating the exact TPM initial state at the |
| 117 | * previous boot.) In recovery mode, we ignore the failure and continue, thus |
| 118 | * giving the recovery kernel a chance to fix things (that's why we don't set |
| 119 | * bGlobalLock). The choice is between a knowingly insecure device and a |
| 120 | * bricked device. |
| 121 | * |
| 122 | * As a side note, observe that we go through considerable hoops to avoid using |
| 123 | * the STCLEAR permissions for the index spaces. We do this to avoid writing |
| 124 | * to the TPM flashram at every reboot or wake-up, because of concerns about |
| 125 | * the durability of the NVRAM. |
| 126 | */ |
| 127 | uint32_t tpm_setup(int s3flag) |
| 128 | { |
| 129 | uint32_t result; |
| 130 | |
| 131 | result = tlcl_lib_init(); |
| 132 | if (result != TPM_SUCCESS) { |
| 133 | printk(BIOS_ERR, "TPM: Can't initialize.\n"); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 134 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Handle special init for S3 resume path */ |
| 138 | if (s3flag) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 139 | printk(BIOS_INFO, "TPM: Handle S3 resume.\n"); |
| 140 | return tpm_setup_epilogue(tpm_setup_s3_helper()); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | result = tlcl_startup(); |
Arthur Heymans | 6d5fcf4 | 2019-10-14 17:06:27 +0200 | [diff] [blame^] | 144 | if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT) |
| 145 | && result == TPM_E_INVALID_POSTINIT) { |
| 146 | printk(BIOS_DEBUG, "TPM: ignoring invalid POSTINIT\n"); |
| 147 | result = TPM_SUCCESS; |
| 148 | } |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 149 | if (result != TPM_SUCCESS) { |
| 150 | printk(BIOS_ERR, "TPM: Can't run startup command.\n"); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 151 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | result = tlcl_assert_physical_presence(); |
| 155 | if (result != TPM_SUCCESS) { |
| 156 | /* |
| 157 | * It is possible that the TPM was delivered with the physical |
| 158 | * presence command disabled. This tries enabling it, then |
| 159 | * tries asserting PP again. |
| 160 | */ |
| 161 | result = tlcl_physical_presence_cmd_enable(); |
| 162 | if (result != TPM_SUCCESS) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 163 | printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n"); |
| 164 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | result = tlcl_assert_physical_presence(); |
| 168 | if (result != TPM_SUCCESS) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 169 | printk(BIOS_ERR, "TPM: Can't assert physical presence.\n"); |
| 170 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 174 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 175 | result = tpm1_invoke_state_machine(); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 176 | #endif |
| 177 | |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 178 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | uint32_t tpm_clear_and_reenable(void) |
| 182 | { |
| 183 | uint32_t result; |
| 184 | |
| 185 | printk(BIOS_INFO, "TPM: Clear and re-enable\n"); |
| 186 | result = tlcl_force_clear(); |
| 187 | if (result != TPM_SUCCESS) { |
| 188 | printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n"); |
| 189 | return result; |
| 190 | } |
| 191 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 192 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 193 | result = tlcl_set_enable(); |
| 194 | if (result != TPM_SUCCESS) { |
| 195 | printk(BIOS_ERR, "TPM: Can't set enabled state.\n"); |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | result = tlcl_set_deactivated(0); |
| 200 | if (result != TPM_SUCCESS) { |
| 201 | printk(BIOS_ERR, "TPM: Can't set deactivated state.\n"); |
| 202 | return result; |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | return TPM_SUCCESS; |
| 207 | } |
| 208 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 209 | uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo, |
| 210 | uint8_t *digest, size_t digest_len, const char *name) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 211 | { |
Philipp Deppenwiese | f849972 | 2018-07-30 01:27:47 +0200 | [diff] [blame] | 212 | uint32_t result; |
| 213 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 214 | if (!digest) |
| 215 | return TPM_E_IOERROR; |
| 216 | |
Philipp Deppenwiese | f849972 | 2018-07-30 01:27:47 +0200 | [diff] [blame] | 217 | result = tlcl_extend(pcr, digest, NULL); |
| 218 | if (result != TPM_SUCCESS) |
| 219 | return result; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 220 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 221 | if (CONFIG(VBOOT_MEASURED_BOOT)) |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 222 | tcpa_log_add_table_entry(name, pcr, digest_algo, |
| 223 | digest, digest_len); |
| 224 | |
Furquan Shaikh | 38f3ffa | 2018-07-31 14:26:39 -0700 | [diff] [blame] | 225 | return TPM_SUCCESS; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 226 | } |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 227 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 228 | #if CONFIG(VBOOT) |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 229 | uint32_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr, |
| 230 | const char *rname) |
| 231 | { |
| 232 | uint8_t digest[TPM_PCR_MAX_LEN], digest_len; |
| 233 | uint8_t buf[HASH_DATA_CHUNK_SIZE]; |
| 234 | uint32_t result, offset; |
| 235 | size_t len; |
| 236 | struct vb2_digest_context ctx; |
| 237 | enum vb2_hash_algorithm hash_alg; |
| 238 | |
| 239 | if (!rdev || !rname) |
| 240 | return TPM_E_INVALID_ARG; |
| 241 | result = tlcl_lib_init(); |
| 242 | if (result != TPM_SUCCESS) { |
| 243 | printk(BIOS_ERR, "TPM: Can't initialize library.\n"); |
| 244 | return result; |
| 245 | } |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 246 | if (CONFIG(TPM1)) { |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 247 | hash_alg = VB2_HASH_SHA1; |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 248 | } else { /* CONFIG_TPM2 */ |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 249 | hash_alg = VB2_HASH_SHA256; |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 250 | } |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 251 | |
| 252 | digest_len = vb2_digest_size(hash_alg); |
| 253 | assert(digest_len <= sizeof(digest)); |
| 254 | if (vb2_digest_init(&ctx, hash_alg)) { |
| 255 | printk(BIOS_ERR, "TPM: Error initializing hash.\n"); |
| 256 | return TPM_E_HASH_ERROR; |
| 257 | } |
| 258 | /* |
| 259 | * Though one can mmap the full needed region on x86 this is not the |
| 260 | * case for e.g. ARM. In order to make this code as universal as |
| 261 | * possible across different platforms read the data to hash in chunks. |
| 262 | */ |
| 263 | for (offset = 0; offset < region_device_sz(rdev); offset += len) { |
| 264 | len = MIN(sizeof(buf), region_device_sz(rdev) - offset); |
| 265 | if (rdev_readat(rdev, buf, offset, len) < 0) { |
| 266 | printk(BIOS_ERR, "TPM: Not able to read region %s.\n", |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 267 | rname); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 268 | return TPM_E_READ_FAILURE; |
| 269 | } |
| 270 | if (vb2_digest_extend(&ctx, buf, len)) { |
| 271 | printk(BIOS_ERR, "TPM: Error extending hash.\n"); |
| 272 | return TPM_E_HASH_ERROR; |
| 273 | } |
| 274 | } |
| 275 | if (vb2_digest_finalize(&ctx, digest, digest_len)) { |
| 276 | printk(BIOS_ERR, "TPM: Error finalizing hash.\n"); |
| 277 | return TPM_E_HASH_ERROR; |
| 278 | } |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 279 | result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 280 | if (result != TPM_SUCCESS) { |
| 281 | printk(BIOS_ERR, "TPM: Extending hash into PCR failed.\n"); |
| 282 | return result; |
| 283 | } |
| 284 | printk(BIOS_DEBUG, "TPM: Measured %s into PCR %d\n", rname, pcr); |
| 285 | return TPM_SUCCESS; |
| 286 | } |
| 287 | #endif /* VBOOT */ |