blob: 4698a4dc8c99f9727d931a791a11bbf17a80f136 [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();
144 if (result != TPM_SUCCESS) {
145 printk(BIOS_ERR, "TPM: Can't run startup command.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800146 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100147 }
148
149 result = tlcl_assert_physical_presence();
150 if (result != TPM_SUCCESS) {
151 /*
152 * It is possible that the TPM was delivered with the physical
153 * presence command disabled. This tries enabling it, then
154 * tries asserting PP again.
155 */
156 result = tlcl_physical_presence_cmd_enable();
157 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800158 printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n");
159 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100160 }
161
162 result = tlcl_assert_physical_presence();
163 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800164 printk(BIOS_ERR, "TPM: Can't assert physical presence.\n");
165 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100166 }
167 }
168
Julius Wernercd49cce2019-03-05 16:53:33 -0800169#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100170 result = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100171#endif
172
Joel Kitching9937a062018-10-11 18:16:59 +0800173 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100174}
175
176uint32_t tpm_clear_and_reenable(void)
177{
178 uint32_t result;
179
180 printk(BIOS_INFO, "TPM: Clear and re-enable\n");
181 result = tlcl_force_clear();
182 if (result != TPM_SUCCESS) {
183 printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n");
184 return result;
185 }
186
Julius Wernercd49cce2019-03-05 16:53:33 -0800187#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100188 result = tlcl_set_enable();
189 if (result != TPM_SUCCESS) {
190 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
191 return result;
192 }
193
194 result = tlcl_set_deactivated(0);
195 if (result != TPM_SUCCESS) {
196 printk(BIOS_ERR, "TPM: Can't set deactivated state.\n");
197 return result;
198 }
199#endif
200
201 return TPM_SUCCESS;
202}
203
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100204uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
205 uint8_t *digest, size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100206{
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200207 uint32_t result;
208
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100209 if (!digest)
210 return TPM_E_IOERROR;
211
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200212 result = tlcl_extend(pcr, digest, NULL);
213 if (result != TPM_SUCCESS)
214 return result;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100215
Julius Wernercd49cce2019-03-05 16:53:33 -0800216 if (CONFIG(VBOOT_MEASURED_BOOT))
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100217 tcpa_log_add_table_entry(name, pcr, digest_algo,
218 digest, digest_len);
219
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700220 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100221}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200222
Julius Wernercd49cce2019-03-05 16:53:33 -0800223#if CONFIG(VBOOT)
Werner Zeh30cf14f2018-10-23 07:40:08 +0200224uint32_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 }
Julius Wernercd49cce2019-03-05 16:53:33 -0800241 if (CONFIG(TPM1)) {
Werner Zeh30cf14f2018-10-23 07:40:08 +0200242 hash_alg = VB2_HASH_SHA1;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100243 } else { /* CONFIG_TPM2 */
Werner Zeh30cf14f2018-10-23 07:40:08 +0200244 hash_alg = VB2_HASH_SHA256;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100245 }
Werner Zeh30cf14f2018-10-23 07:40:08 +0200246
247 digest_len = vb2_digest_size(hash_alg);
248 assert(digest_len <= sizeof(digest));
249 if (vb2_digest_init(&ctx, hash_alg)) {
250 printk(BIOS_ERR, "TPM: Error initializing hash.\n");
251 return TPM_E_HASH_ERROR;
252 }
253 /*
254 * Though one can mmap the full needed region on x86 this is not the
255 * case for e.g. ARM. In order to make this code as universal as
256 * possible across different platforms read the data to hash in chunks.
257 */
258 for (offset = 0; offset < region_device_sz(rdev); offset += len) {
259 len = MIN(sizeof(buf), region_device_sz(rdev) - offset);
260 if (rdev_readat(rdev, buf, offset, len) < 0) {
261 printk(BIOS_ERR, "TPM: Not able to read region %s.\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100262 rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200263 return TPM_E_READ_FAILURE;
264 }
265 if (vb2_digest_extend(&ctx, buf, len)) {
266 printk(BIOS_ERR, "TPM: Error extending hash.\n");
267 return TPM_E_HASH_ERROR;
268 }
269 }
270 if (vb2_digest_finalize(&ctx, digest, digest_len)) {
271 printk(BIOS_ERR, "TPM: Error finalizing hash.\n");
272 return TPM_E_HASH_ERROR;
273 }
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100274 result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200275 if (result != TPM_SUCCESS) {
276 printk(BIOS_ERR, "TPM: Extending hash into PCR failed.\n");
277 return result;
278 }
279 printk(BIOS_DEBUG, "TPM: Measured %s into PCR %d\n", rname, pcr);
280 return TPM_SUCCESS;
281}
282#endif /* VBOOT */