blob: b8ebf7b809bfa65789cefe2be8cc5a3fd7dc5f6d [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>
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 Zeh30cf14f2018-10-23 07:40:08 +020025#if IS_ENABLED(CONFIG_VBOOT)
26#include <vb2_api.h>
27#include <assert.h>
28#endif
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010029
30#if IS_ENABLED(CONFIG_TPM1)
31static uint32_t tpm1_invoke_state_machine(void)
32{
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070033 uint8_t disabled;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010034 uint8_t deactivated;
35 uint32_t result = TPM_SUCCESS;
36
37 /* Check that the TPM is enabled and activated. */
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070038 result = tlcl_get_flags(&disabled, &deactivated, NULL);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010039 if (result != TPM_SUCCESS) {
40 printk(BIOS_ERR, "TPM: Can't read capabilities.\n");
41 return result;
42 }
43
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070044 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +010054 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +010068 return result;
69}
70#endif
71
Joel Kitching9937a062018-10-11 18:16:59 +080072static 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 Kitching9937a062018-10-11 18:16:59 +080093 }
94
95 return result;
96}
97
98static 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +0100108/*
109 * tpm_setup starts the TPM and establishes the root of trust for the
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100110 * 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 Deppenwiesec07f8fb2018-02-27 19:40:52 +0100112 * general we cannot easily distinguish the kind of failure, so our strategy is
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100113 * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100114 * 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 */
128uint32_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 Kitching9937a062018-10-11 18:16:59 +0800135 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100136 }
137
138 /* Handle special init for S3 resume path */
139 if (s3flag) {
Joel Kitching9937a062018-10-11 18:16:59 +0800140 printk(BIOS_INFO, "TPM: Handle S3 resume.\n");
141 return tpm_setup_epilogue(tpm_setup_s3_helper());
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100142 }
143
144 result = tlcl_startup();
145 if (result != TPM_SUCCESS) {
146 printk(BIOS_ERR, "TPM: Can't run startup command.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800147 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100148 }
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 Kitching9937a062018-10-11 18:16:59 +0800159 printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n");
160 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100161 }
162
163 result = tlcl_assert_physical_presence();
164 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800165 printk(BIOS_ERR, "TPM: Can't assert physical presence.\n");
166 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100167 }
168 }
169
170#if IS_ENABLED(CONFIG_TPM1)
171 result = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100172#endif
173
Joel Kitching9937a062018-10-11 18:16:59 +0800174 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100175}
176
177uint32_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 Deppenwiesef8499722018-07-30 01:27:47 +0200205uint32_t tpm_extend_pcr(int pcr, uint8_t *digest,
206 size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100207{
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200208 uint32_t result;
209
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100210 if (!digest)
211 return TPM_E_IOERROR;
212
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200213 result = tlcl_extend(pcr, digest, NULL);
214 if (result != TPM_SUCCESS)
215 return result;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100216
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700217 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100218}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200219
220#if IS_ENABLED(CONFIG_VBOOT)
221uint32_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 Deppenwiese66f9a092018-11-08 10:59:40 +0100240 else /* CONFIG_TPM2 */
Werner Zeh30cf14f2018-10-23 07:40:08 +0200241 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 Deppenwiese66f9a092018-11-08 10:59:40 +0100258 rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200259 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 */