blob: 4f0cc972a7608048e4984b5de15cc452fc0073c3 [file] [log] [blame]
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01001/*
2 * This file is part of the coreboot project.
3 *
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <console/cbmem_console.h>
16#include <console/console.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080017#include <security/tpm/tspi/crtm.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010018#include <security/tpm/tspi.h>
19#include <security/tpm/tss.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080020#include <assert.h>
21#include <security/vboot/misc.h>
22#include <string.h>
Werner Zeh30cf14f2018-10-23 07:40:08 +020023#include <vb2_api.h>
Joel Kitching2eb89c82019-04-25 17:45:12 +080024#include <vb2_sha.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010025
Julius Wernercd49cce2019-03-05 16:53:33 -080026#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010027static uint32_t tpm1_invoke_state_machine(void)
28{
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070029 uint8_t disabled;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010030 uint8_t deactivated;
31 uint32_t result = TPM_SUCCESS;
32
33 /* Check that the TPM is enabled and activated. */
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070034 result = tlcl_get_flags(&disabled, &deactivated, NULL);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010035 if (result != TPM_SUCCESS) {
36 printk(BIOS_ERR, "TPM: Can't read capabilities.\n");
37 return result;
38 }
39
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070040 if (disabled) {
41 printk(BIOS_INFO, "TPM: is disabled. Enabling...\n");
42
43 result = tlcl_set_enable();
44 if (result != TPM_SUCCESS) {
45 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
46 return result;
47 }
48 }
49
Julius Wernercd49cce2019-03-05 16:53:33 -080050 if (!!deactivated != CONFIG(TPM_DEACTIVATE)) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010051 printk(BIOS_INFO,
52 "TPM: Unexpected TPM deactivated state. Toggling...\n");
53 result = tlcl_set_deactivated(!deactivated);
54 if (result != TPM_SUCCESS) {
55 printk(BIOS_ERR,
56 "TPM: Can't toggle deactivated state.\n");
57 return result;
58 }
59
60 deactivated = !deactivated;
61 result = TPM_E_MUST_REBOOT;
62 }
63
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010064 return result;
65}
66#endif
67
Joel Kitching9937a062018-10-11 18:16:59 +080068static uint32_t tpm_setup_s3_helper(void)
69{
70 uint32_t result;
71
72 result = tlcl_resume();
73 switch (result) {
74 case TPM_SUCCESS:
75 break;
76
77 case TPM_E_INVALID_POSTINIT:
78 /*
79 * We're on a platform where the TPM maintains power
80 * in S3, so it's already initialized.
81 */
82 printk(BIOS_INFO, "TPM: Already initialized.\n");
83 result = TPM_SUCCESS;
84 break;
85
86 default:
87 printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", result);
88 break;
Joel Kitching9937a062018-10-11 18:16:59 +080089 }
90
91 return result;
92}
93
94static uint32_t tpm_setup_epilogue(uint32_t result)
95{
96 if (result != TPM_SUCCESS)
97 post_code(POST_TPM_FAILURE);
98 else
99 printk(BIOS_INFO, "TPM: setup succeeded\n");
100
101 return result;
102}
103
Bill XIEc79e96b2019-08-22 20:28:36 +0800104static int tpm_is_setup;
105static inline int tspi_tpm_is_setup(void)
106{
107 if (CONFIG(VBOOT))
108 return vboot_logic_executed() || tpm_is_setup;
109
110 if (ENV_RAMSTAGE)
111 return tpm_is_setup;
112
113 return 0;
114}
115
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100116/*
117 * tpm_setup starts the TPM and establishes the root of trust for the
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100118 * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug.
119 * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100120 * general we cannot easily distinguish the kind of failure, so our strategy is
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100121 * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100122 * again, which executes (almost) the same sequence of operations. There is a
123 * good chance that, if recovery mode was entered because of a TPM failure, the
124 * failure will repeat itself. (In general this is impossible to guarantee
125 * because we have no way of creating the exact TPM initial state at the
126 * previous boot.) In recovery mode, we ignore the failure and continue, thus
127 * giving the recovery kernel a chance to fix things (that's why we don't set
128 * bGlobalLock). The choice is between a knowingly insecure device and a
129 * bricked device.
130 *
131 * As a side note, observe that we go through considerable hoops to avoid using
132 * the STCLEAR permissions for the index spaces. We do this to avoid writing
133 * to the TPM flashram at every reboot or wake-up, because of concerns about
134 * the durability of the NVRAM.
135 */
136uint32_t tpm_setup(int s3flag)
137{
138 uint32_t result;
139
140 result = tlcl_lib_init();
141 if (result != TPM_SUCCESS) {
142 printk(BIOS_ERR, "TPM: Can't initialize.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800143 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100144 }
145
146 /* Handle special init for S3 resume path */
147 if (s3flag) {
Joel Kitching9937a062018-10-11 18:16:59 +0800148 printk(BIOS_INFO, "TPM: Handle S3 resume.\n");
149 return tpm_setup_epilogue(tpm_setup_s3_helper());
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100150 }
151
152 result = tlcl_startup();
Arthur Heymans6d5fcf42019-10-14 17:06:27 +0200153 if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT)
154 && result == TPM_E_INVALID_POSTINIT) {
155 printk(BIOS_DEBUG, "TPM: ignoring invalid POSTINIT\n");
156 result = TPM_SUCCESS;
157 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100158 if (result != TPM_SUCCESS) {
159 printk(BIOS_ERR, "TPM: Can't run startup command.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800160 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) {
165 /*
166 * It is possible that the TPM was delivered with the physical
167 * presence command disabled. This tries enabling it, then
168 * tries asserting PP again.
169 */
170 result = tlcl_physical_presence_cmd_enable();
171 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800172 printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n");
173 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100174 }
175
176 result = tlcl_assert_physical_presence();
177 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800178 printk(BIOS_ERR, "TPM: Can't assert physical presence.\n");
179 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100180 }
181 }
182
Julius Wernercd49cce2019-03-05 16:53:33 -0800183#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100184 result = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100185#endif
Bill XIEc79e96b2019-08-22 20:28:36 +0800186 if (CONFIG(TPM_MEASURED_BOOT))
187 result = tspi_measure_cache_to_pcr();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100188
Bill XIEc79e96b2019-08-22 20:28:36 +0800189 tpm_is_setup = 1;
Joel Kitching9937a062018-10-11 18:16:59 +0800190 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100191}
192
193uint32_t tpm_clear_and_reenable(void)
194{
195 uint32_t result;
196
197 printk(BIOS_INFO, "TPM: Clear and re-enable\n");
198 result = tlcl_force_clear();
199 if (result != TPM_SUCCESS) {
200 printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n");
201 return result;
202 }
203
Julius Wernercd49cce2019-03-05 16:53:33 -0800204#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100205 result = tlcl_set_enable();
206 if (result != TPM_SUCCESS) {
207 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
208 return result;
209 }
210
211 result = tlcl_set_deactivated(0);
212 if (result != TPM_SUCCESS) {
213 printk(BIOS_ERR, "TPM: Can't set deactivated state.\n");
214 return result;
215 }
216#endif
217
218 return TPM_SUCCESS;
219}
220
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100221uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
222 uint8_t *digest, size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100223{
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200224 uint32_t result;
225
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100226 if (!digest)
227 return TPM_E_IOERROR;
228
Bill XIEc79e96b2019-08-22 20:28:36 +0800229 if (tspi_tpm_is_setup()) {
230 result = tlcl_lib_init();
231 if (result != TPM_SUCCESS) {
232 printk(BIOS_ERR, "TPM: Can't initialize library.\n");
233 return result;
234 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100235
Bill XIEc79e96b2019-08-22 20:28:36 +0800236 printk(BIOS_DEBUG, "TPM: Extending digest for %s into PCR %d\n", name, pcr);
237 result = tlcl_extend(pcr, digest, NULL);
238 if (result != TPM_SUCCESS)
239 return result;
240 }
241
242 if (CONFIG(TPM_MEASURED_BOOT))
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100243 tcpa_log_add_table_entry(name, pcr, digest_algo,
244 digest, digest_len);
245
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700246 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100247}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200248
Bill XIEc79e96b2019-08-22 20:28:36 +0800249#if CONFIG(VBOOT_LIB)
Werner Zeh30cf14f2018-10-23 07:40:08 +0200250uint32_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
251 const char *rname)
252{
253 uint8_t digest[TPM_PCR_MAX_LEN], digest_len;
254 uint8_t buf[HASH_DATA_CHUNK_SIZE];
255 uint32_t result, offset;
256 size_t len;
257 struct vb2_digest_context ctx;
258 enum vb2_hash_algorithm hash_alg;
259
260 if (!rdev || !rname)
261 return TPM_E_INVALID_ARG;
Bill XIEc79e96b2019-08-22 20:28:36 +0800262
Julius Wernercd49cce2019-03-05 16:53:33 -0800263 if (CONFIG(TPM1)) {
Werner Zeh30cf14f2018-10-23 07:40:08 +0200264 hash_alg = VB2_HASH_SHA1;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100265 } else { /* CONFIG_TPM2 */
Werner Zeh30cf14f2018-10-23 07:40:08 +0200266 hash_alg = VB2_HASH_SHA256;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100267 }
Werner Zeh30cf14f2018-10-23 07:40:08 +0200268
269 digest_len = vb2_digest_size(hash_alg);
270 assert(digest_len <= sizeof(digest));
271 if (vb2_digest_init(&ctx, hash_alg)) {
272 printk(BIOS_ERR, "TPM: Error initializing hash.\n");
273 return TPM_E_HASH_ERROR;
274 }
275 /*
276 * Though one can mmap the full needed region on x86 this is not the
277 * case for e.g. ARM. In order to make this code as universal as
278 * possible across different platforms read the data to hash in chunks.
279 */
280 for (offset = 0; offset < region_device_sz(rdev); offset += len) {
281 len = MIN(sizeof(buf), region_device_sz(rdev) - offset);
282 if (rdev_readat(rdev, buf, offset, len) < 0) {
283 printk(BIOS_ERR, "TPM: Not able to read region %s.\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100284 rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200285 return TPM_E_READ_FAILURE;
286 }
287 if (vb2_digest_extend(&ctx, buf, len)) {
288 printk(BIOS_ERR, "TPM: Error extending hash.\n");
289 return TPM_E_HASH_ERROR;
290 }
291 }
292 if (vb2_digest_finalize(&ctx, digest, digest_len)) {
293 printk(BIOS_ERR, "TPM: Error finalizing hash.\n");
294 return TPM_E_HASH_ERROR;
295 }
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100296 result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200297 if (result != TPM_SUCCESS) {
298 printk(BIOS_ERR, "TPM: Extending hash into PCR failed.\n");
299 return result;
300 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800301 printk(BIOS_DEBUG, "TPM: Digest of %s to PCR %d %s\n",
302 rname, pcr, tspi_tpm_is_setup() ? "measured" : "logged");
Werner Zeh30cf14f2018-10-23 07:40:08 +0200303 return TPM_SUCCESS;
304}
Bill XIEc79e96b2019-08-22 20:28:36 +0800305#endif /* VBOOT_LIB */