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