blob: 966b8b7c77ced382f67dd35cfcbec206da31137f [file] [log] [blame]
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01001/*
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 Zeh30cf14f2018-10-23 07:40:08 +02006 * Copyright 2018 Siemens AG
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01007 *
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 Deppenwiesec07f8fb2018-02-27 19:40:52 +010020#include <security/tpm/tspi.h>
21#include <security/tpm/tss.h>
22#include <stdlib.h>
Julius Wernercd49cce2019-03-05 16:53:33 -080023#if CONFIG(VBOOT)
Werner Zeh30cf14f2018-10-23 07:40:08 +020024#include <vb2_api.h>
Joel Kitching2eb89c82019-04-25 17:45:12 +080025#include <vb2_sha.h>
Werner Zeh30cf14f2018-10-23 07:40:08 +020026#include <assert.h>
27#endif
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010028
Julius Wernercd49cce2019-03-05 16:53:33 -080029#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010030static uint32_t tpm1_invoke_state_machine(void)
31{
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070032 uint8_t disabled;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010033 uint8_t deactivated;
34 uint32_t result = TPM_SUCCESS;
35
36 /* Check that the TPM is enabled and activated. */
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070037 result = tlcl_get_flags(&disabled, &deactivated, NULL);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010038 if (result != TPM_SUCCESS) {
39 printk(BIOS_ERR, "TPM: Can't read capabilities.\n");
40 return result;
41 }
42
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070043 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 Wernercd49cce2019-03-05 16:53:33 -080053 if (!!deactivated != CONFIG(TPM_DEACTIVATE)) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010054 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +010067 return result;
68}
69#endif
70
Joel Kitching9937a062018-10-11 18:16:59 +080071static 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 Kitching9937a062018-10-11 18:16:59 +080092 }
93
94 return result;
95}
96
97static 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +0100107/*
108 * tpm_setup starts the TPM and establishes the root of trust for the
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100109 * 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +0100111 * general we cannot easily distinguish the kind of failure, so our strategy is
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100112 * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100113 * 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 */
127uint32_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 Kitching9937a062018-10-11 18:16:59 +0800134 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100135 }
136
137 /* Handle special init for S3 resume path */
138 if (s3flag) {
Joel Kitching9937a062018-10-11 18:16:59 +0800139 printk(BIOS_INFO, "TPM: Handle S3 resume.\n");
140 return tpm_setup_epilogue(tpm_setup_s3_helper());
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100141 }
142
143 result = tlcl_startup();
Arthur Heymans6d5fcf42019-10-14 17:06:27 +0200144 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +0100149 if (result != TPM_SUCCESS) {
150 printk(BIOS_ERR, "TPM: Can't run startup command.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800151 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100152 }
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 Kitching9937a062018-10-11 18:16:59 +0800163 printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n");
164 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100165 }
166
167 result = tlcl_assert_physical_presence();
168 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800169 printk(BIOS_ERR, "TPM: Can't assert physical presence.\n");
170 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100171 }
172 }
173
Julius Wernercd49cce2019-03-05 16:53:33 -0800174#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100175 result = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100176#endif
177
Joel Kitching9937a062018-10-11 18:16:59 +0800178 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100179}
180
181uint32_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 Wernercd49cce2019-03-05 16:53:33 -0800192#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100193 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 Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100209uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
210 uint8_t *digest, size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100211{
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200212 uint32_t result;
213
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100214 if (!digest)
215 return TPM_E_IOERROR;
216
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200217 result = tlcl_extend(pcr, digest, NULL);
218 if (result != TPM_SUCCESS)
219 return result;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100220
Julius Wernercd49cce2019-03-05 16:53:33 -0800221 if (CONFIG(VBOOT_MEASURED_BOOT))
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100222 tcpa_log_add_table_entry(name, pcr, digest_algo,
223 digest, digest_len);
224
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700225 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100226}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200227
Julius Wernercd49cce2019-03-05 16:53:33 -0800228#if CONFIG(VBOOT)
Werner Zeh30cf14f2018-10-23 07:40:08 +0200229uint32_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 Wernercd49cce2019-03-05 16:53:33 -0800246 if (CONFIG(TPM1)) {
Werner Zeh30cf14f2018-10-23 07:40:08 +0200247 hash_alg = VB2_HASH_SHA1;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100248 } else { /* CONFIG_TPM2 */
Werner Zeh30cf14f2018-10-23 07:40:08 +0200249 hash_alg = VB2_HASH_SHA256;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100250 }
Werner Zeh30cf14f2018-10-23 07:40:08 +0200251
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 Deppenwiese66f9a092018-11-08 10:59:40 +0100267 rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200268 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 Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100279 result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200280 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 */