blob: ec695819fd84fbfc91db99b139a60d5e66aef284 [file] [log] [blame]
Angel Pons986d50e2020-04-02 23:48:53 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01002
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01003#include <console/console.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08004#include <security/tpm/tspi/crtm.h>
Sergii Dmytruk4191dbf2022-10-23 00:34:32 +03005#include <security/tpm/tspi/logs.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01006#include <security/tpm/tspi.h>
7#include <security/tpm/tss.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08008#include <assert.h>
9#include <security/vboot/misc.h>
Werner Zeh30cf14f2018-10-23 07:40:08 +020010#include <vb2_api.h>
Joel Kitching2eb89c82019-04-25 17:45:12 +080011#include <vb2_sha.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010012
Julius Wernercd49cce2019-03-05 16:53:33 -080013#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010014static uint32_t tpm1_invoke_state_machine(void)
15{
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070016 uint8_t disabled;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010017 uint8_t deactivated;
18 uint32_t result = TPM_SUCCESS;
19
20 /* Check that the TPM is enabled and activated. */
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070021 result = tlcl_get_flags(&disabled, &deactivated, NULL);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010022 if (result != TPM_SUCCESS) {
23 printk(BIOS_ERR, "TPM: Can't read capabilities.\n");
24 return result;
25 }
26
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070027 if (disabled) {
28 printk(BIOS_INFO, "TPM: is disabled. Enabling...\n");
29
30 result = tlcl_set_enable();
31 if (result != TPM_SUCCESS) {
32 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
33 return result;
34 }
35 }
36
Julius Wernercd49cce2019-03-05 16:53:33 -080037 if (!!deactivated != CONFIG(TPM_DEACTIVATE)) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010038 printk(BIOS_INFO,
39 "TPM: Unexpected TPM deactivated state. Toggling...\n");
40 result = tlcl_set_deactivated(!deactivated);
41 if (result != TPM_SUCCESS) {
42 printk(BIOS_ERR,
43 "TPM: Can't toggle deactivated state.\n");
44 return result;
45 }
46
47 deactivated = !deactivated;
48 result = TPM_E_MUST_REBOOT;
49 }
50
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010051 return result;
52}
53#endif
54
Joel Kitching9937a062018-10-11 18:16:59 +080055static uint32_t tpm_setup_s3_helper(void)
56{
57 uint32_t result;
58
59 result = tlcl_resume();
60 switch (result) {
61 case TPM_SUCCESS:
62 break;
63
64 case TPM_E_INVALID_POSTINIT:
65 /*
66 * We're on a platform where the TPM maintains power
67 * in S3, so it's already initialized.
68 */
69 printk(BIOS_INFO, "TPM: Already initialized.\n");
70 result = TPM_SUCCESS;
71 break;
72
73 default:
74 printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", result);
75 break;
Joel Kitching9937a062018-10-11 18:16:59 +080076 }
77
78 return result;
79}
80
81static uint32_t tpm_setup_epilogue(uint32_t result)
82{
83 if (result != TPM_SUCCESS)
lilacious40cb3fe2023-06-21 23:24:14 +020084 post_code(POSTCODE_TPM_FAILURE);
Joel Kitching9937a062018-10-11 18:16:59 +080085 else
86 printk(BIOS_INFO, "TPM: setup succeeded\n");
87
88 return result;
89}
90
Bill XIEc79e96b2019-08-22 20:28:36 +080091static int tpm_is_setup;
92static inline int tspi_tpm_is_setup(void)
93{
Julius Werner23a82e82020-03-31 13:32:10 -070094 /*
95 * vboot_logic_executed() only starts returning true at the end of
96 * verstage, but the vboot logic itself already wants to extend PCRs
97 * before that. So in the stage where verification actually runs, we
98 * need to check tpm_is_setup. Skip that check in all other stages so
99 * this whole function can be evaluated at compile time.
100 */
101 if (CONFIG(VBOOT)) {
102 if (verification_should_run())
103 return tpm_is_setup;
104 return vboot_logic_executed();
105 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800106
Arthur Heymans6f8e9442021-03-29 14:23:53 +0200107 if (CONFIG(TPM_MEASURED_BOOT_INIT_BOOTBLOCK))
108 return ENV_BOOTBLOCK ? tpm_is_setup : 1;
109
Bill XIEc79e96b2019-08-22 20:28:36 +0800110 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
Arthur Heymansb192af12021-05-20 09:09:56 +0200186 if (CONFIG(TPM_MEASURED_BOOT))
Bill XIEc79e96b2019-08-22 20:28:36 +0800187 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,
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700222 const 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
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700236 printk(BIOS_DEBUG, "TPM: Extending digest for `%s` into PCR %d\n", name, pcr);
Sergii Dmytruk7221a6c2022-10-22 20:11:35 +0300237 result = tlcl_extend(pcr, digest, digest_algo);
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700238 if (result != TPM_SUCCESS) {
239 printk(BIOS_ERR, "TPM: Extending hash for `%s` into PCR %d failed.\n",
240 name, pcr);
Bill XIEc79e96b2019-08-22 20:28:36 +0800241 return result;
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700242 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800243 }
244
245 if (CONFIG(TPM_MEASURED_BOOT))
Sergii Dmytruk2710df72022-11-10 00:40:51 +0200246 tpm_log_add_table_entry(name, pcr, digest_algo, digest, digest_len);
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100247
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700248 printk(BIOS_DEBUG, "TPM: Digest of `%s` to PCR %d %s\n",
249 name, pcr, tspi_tpm_is_setup() ? "measured" : "logged");
250
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700251 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100252}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200253
Bill XIEc79e96b2019-08-22 20:28:36 +0800254#if CONFIG(VBOOT_LIB)
Werner Zeh30cf14f2018-10-23 07:40:08 +0200255uint32_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
256 const char *rname)
257{
258 uint8_t digest[TPM_PCR_MAX_LEN], digest_len;
259 uint8_t buf[HASH_DATA_CHUNK_SIZE];
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700260 uint32_t offset;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200261 size_t len;
262 struct vb2_digest_context ctx;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200263
264 if (!rdev || !rname)
265 return TPM_E_INVALID_ARG;
Bill XIEc79e96b2019-08-22 20:28:36 +0800266
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700267 digest_len = vb2_digest_size(TPM_MEASURE_ALGO);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200268 assert(digest_len <= sizeof(digest));
Julius Wernerd96ca242022-08-08 18:08:35 -0700269 if (vb2_digest_init(&ctx, vboot_hwcrypto_allowed(), TPM_MEASURE_ALGO,
270 region_device_sz(rdev))) {
Werner Zeh30cf14f2018-10-23 07:40:08 +0200271 printk(BIOS_ERR, "TPM: Error initializing hash.\n");
272 return TPM_E_HASH_ERROR;
273 }
274 /*
275 * Though one can mmap the full needed region on x86 this is not the
276 * case for e.g. ARM. In order to make this code as universal as
277 * possible across different platforms read the data to hash in chunks.
278 */
279 for (offset = 0; offset < region_device_sz(rdev); offset += len) {
280 len = MIN(sizeof(buf), region_device_sz(rdev) - offset);
281 if (rdev_readat(rdev, buf, offset, len) < 0) {
282 printk(BIOS_ERR, "TPM: Not able to read region %s.\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100283 rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200284 return TPM_E_READ_FAILURE;
285 }
286 if (vb2_digest_extend(&ctx, buf, len)) {
287 printk(BIOS_ERR, "TPM: Error extending hash.\n");
288 return TPM_E_HASH_ERROR;
289 }
290 }
291 if (vb2_digest_finalize(&ctx, digest, digest_len)) {
292 printk(BIOS_ERR, "TPM: Error finalizing hash.\n");
293 return TPM_E_HASH_ERROR;
294 }
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700295 return tpm_extend_pcr(pcr, TPM_MEASURE_ALGO, digest, digest_len, rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200296}
Bill XIEc79e96b2019-08-22 20:28:36 +0800297#endif /* VBOOT_LIB */