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