blob: 56b8fa8ede3e5761ea8fe9c318c746952aafa4b2 [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)
Jon Murphyd7b8dc92023-09-05 11:36:43 -060014static tpm_result_t tpm1_invoke_state_machine(void)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010015{
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070016 uint8_t disabled;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010017 uint8_t deactivated;
Jon Murphyd7b8dc92023-09-05 11:36:43 -060018 tpm_result_t rc = TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010019
Sergii Dmytruk47e9e8c2022-11-02 00:50:03 +020020 if (tlcl_get_family() != TPM_1)
21 return rc;
22
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010023 /* Check that the TPM is enabled and activated. */
Sergii Dmytruk094a0512022-10-31 18:41:52 +020024 rc = tlcl1_get_flags(&disabled, &deactivated, NULL);
Jon Murphy24604812023-09-05 10:37:05 -060025 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -060026 printk(BIOS_ERR, "TPM Error (%#x): Can't read capabilities.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -060027 return rc;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010028 }
29
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070030 if (disabled) {
31 printk(BIOS_INFO, "TPM: is disabled. Enabling...\n");
32
Sergii Dmytruk094a0512022-10-31 18:41:52 +020033 rc = tlcl1_set_enable();
Jon Murphy24604812023-09-05 10:37:05 -060034 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -060035 printk(BIOS_ERR, "TPM Error (%#x): Can't set enabled state.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -060036 return rc;
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070037 }
38 }
39
Julius Wernercd49cce2019-03-05 16:53:33 -080040 if (!!deactivated != CONFIG(TPM_DEACTIVATE)) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010041 printk(BIOS_INFO,
42 "TPM: Unexpected TPM deactivated state. Toggling...\n");
Sergii Dmytruk094a0512022-10-31 18:41:52 +020043 rc = tlcl1_set_deactivated(!deactivated);
Jon Murphy24604812023-09-05 10:37:05 -060044 if (rc != TPM_SUCCESS) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010045 printk(BIOS_ERR,
Jon Murphyd7b8dc92023-09-05 11:36:43 -060046 "TPM Error (%#x): Can't toggle deactivated state.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -060047 return rc;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010048 }
49
50 deactivated = !deactivated;
Jon Murphy056952e2023-09-05 10:44:09 -060051 rc = TPM_CB_MUST_REBOOT;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010052 }
53
Jon Murphy24604812023-09-05 10:37:05 -060054 return rc;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010055}
56#endif
57
Jon Murphyd7b8dc92023-09-05 11:36:43 -060058static tpm_result_t tpm_setup_s3_helper(void)
Joel Kitching9937a062018-10-11 18:16:59 +080059{
Jon Murphyd7b8dc92023-09-05 11:36:43 -060060 tpm_result_t rc = tlcl_resume();
Jon Murphy24604812023-09-05 10:37:05 -060061 switch (rc) {
Joel Kitching9937a062018-10-11 18:16:59 +080062 case TPM_SUCCESS:
63 break;
64
Jon Murphy056952e2023-09-05 10:44:09 -060065 case TPM_INVALID_POSTINIT:
Joel Kitching9937a062018-10-11 18:16:59 +080066 /*
67 * We're on a platform where the TPM maintains power
68 * in S3, so it's already initialized.
69 */
70 printk(BIOS_INFO, "TPM: Already initialized.\n");
Jon Murphy24604812023-09-05 10:37:05 -060071 rc = TPM_SUCCESS;
Joel Kitching9937a062018-10-11 18:16:59 +080072 break;
73
74 default:
Jon Murphy24604812023-09-05 10:37:05 -060075 printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", rc);
Joel Kitching9937a062018-10-11 18:16:59 +080076 break;
Joel Kitching9937a062018-10-11 18:16:59 +080077 }
78
Jon Murphy24604812023-09-05 10:37:05 -060079 return rc;
Joel Kitching9937a062018-10-11 18:16:59 +080080}
81
Jon Murphyd7b8dc92023-09-05 11:36:43 -060082static tpm_result_t tpm_setup_epilogue(tpm_result_t rc)
Joel Kitching9937a062018-10-11 18:16:59 +080083{
Jon Murphy24604812023-09-05 10:37:05 -060084 if (rc != TPM_SUCCESS)
lilacious40cb3fe2023-06-21 23:24:14 +020085 post_code(POSTCODE_TPM_FAILURE);
Joel Kitching9937a062018-10-11 18:16:59 +080086 else
87 printk(BIOS_INFO, "TPM: setup succeeded\n");
88
Jon Murphy24604812023-09-05 10:37:05 -060089 return rc;
Joel Kitching9937a062018-10-11 18:16:59 +080090}
91
Bill XIEc79e96b2019-08-22 20:28:36 +080092static int tpm_is_setup;
93static inline int tspi_tpm_is_setup(void)
94{
Julius Werner23a82e82020-03-31 13:32:10 -070095 /*
96 * vboot_logic_executed() only starts returning true at the end of
97 * verstage, but the vboot logic itself already wants to extend PCRs
98 * before that. So in the stage where verification actually runs, we
99 * need to check tpm_is_setup. Skip that check in all other stages so
100 * this whole function can be evaluated at compile time.
101 */
102 if (CONFIG(VBOOT)) {
103 if (verification_should_run())
104 return tpm_is_setup;
105 return vboot_logic_executed();
106 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800107
Arthur Heymans6f8e9442021-03-29 14:23:53 +0200108 if (CONFIG(TPM_MEASURED_BOOT_INIT_BOOTBLOCK))
109 return ENV_BOOTBLOCK ? tpm_is_setup : 1;
110
Bill XIEc79e96b2019-08-22 20:28:36 +0800111 if (ENV_RAMSTAGE)
112 return tpm_is_setup;
113
114 return 0;
115}
116
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100117/*
118 * tpm_setup starts the TPM and establishes the root of trust for the
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100119 * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug.
120 * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100121 * general we cannot easily distinguish the kind of failure, so our strategy is
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100122 * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100123 * again, which executes (almost) the same sequence of operations. There is a
124 * good chance that, if recovery mode was entered because of a TPM failure, the
125 * failure will repeat itself. (In general this is impossible to guarantee
126 * because we have no way of creating the exact TPM initial state at the
127 * previous boot.) In recovery mode, we ignore the failure and continue, thus
128 * giving the recovery kernel a chance to fix things (that's why we don't set
129 * bGlobalLock). The choice is between a knowingly insecure device and a
130 * bricked device.
131 *
132 * As a side note, observe that we go through considerable hoops to avoid using
133 * the STCLEAR permissions for the index spaces. We do this to avoid writing
134 * to the TPM flashram at every reboot or wake-up, because of concerns about
135 * the durability of the NVRAM.
136 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600137tpm_result_t tpm_setup(int s3flag)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100138{
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600139 tpm_result_t rc;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100140
Jon Murphy24604812023-09-05 10:37:05 -0600141 rc = tlcl_lib_init();
142 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600143 printk(BIOS_ERR, "TPM Error (%#x): Can't initialize.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600144 return tpm_setup_epilogue(rc);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100145 }
146
147 /* Handle special init for S3 resume path */
148 if (s3flag) {
Joel Kitching9937a062018-10-11 18:16:59 +0800149 printk(BIOS_INFO, "TPM: Handle S3 resume.\n");
150 return tpm_setup_epilogue(tpm_setup_s3_helper());
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100151 }
152
Jon Murphy24604812023-09-05 10:37:05 -0600153 rc = tlcl_startup();
Arthur Heymans6d5fcf42019-10-14 17:06:27 +0200154 if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT)
Jon Murphy056952e2023-09-05 10:44:09 -0600155 && rc == TPM_INVALID_POSTINIT) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600156 printk(BIOS_DEBUG, "TPM Warn(%#x): ignoring invalid POSTINIT\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600157 rc = TPM_SUCCESS;
Arthur Heymans6d5fcf42019-10-14 17:06:27 +0200158 }
Jon Murphy24604812023-09-05 10:37:05 -0600159 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600160 printk(BIOS_ERR, "TPM Error (%#x): Can't run startup command.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600161 return tpm_setup_epilogue(rc);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100162 }
163
Jon Murphy24604812023-09-05 10:37:05 -0600164 rc = tlcl_assert_physical_presence();
165 if (rc != TPM_SUCCESS) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100166 /*
167 * It is possible that the TPM was delivered with the physical
168 * presence command disabled. This tries enabling it, then
169 * tries asserting PP again.
170 */
Jon Murphy24604812023-09-05 10:37:05 -0600171 rc = tlcl_physical_presence_cmd_enable();
172 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600173 printk(BIOS_ERR, "TPM Error (%#x): Can't enable physical presence command.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600174 return tpm_setup_epilogue(rc);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100175 }
176
Jon Murphy24604812023-09-05 10:37:05 -0600177 rc = tlcl_assert_physical_presence();
178 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600179 printk(BIOS_ERR, "TPM Error (%#x): Can't assert physical presence.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600180 return tpm_setup_epilogue(rc);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100181 }
182 }
183
Julius Wernercd49cce2019-03-05 16:53:33 -0800184#if CONFIG(TPM1)
Jon Murphy24604812023-09-05 10:37:05 -0600185 rc = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100186#endif
Arthur Heymansb192af12021-05-20 09:09:56 +0200187 if (CONFIG(TPM_MEASURED_BOOT))
Jon Murphy24604812023-09-05 10:37:05 -0600188 rc = tspi_measure_cache_to_pcr();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100189
Bill XIEc79e96b2019-08-22 20:28:36 +0800190 tpm_is_setup = 1;
Jon Murphy24604812023-09-05 10:37:05 -0600191 return tpm_setup_epilogue(rc);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100192}
193
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600194tpm_result_t tpm_clear_and_reenable(void)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100195{
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600196 tpm_result_t rc;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100197
198 printk(BIOS_INFO, "TPM: Clear and re-enable\n");
Jon Murphy24604812023-09-05 10:37:05 -0600199 rc = tlcl_force_clear();
200 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600201 printk(BIOS_ERR, "TPM Error (%#x): Can't initiate a force clear.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600202 return rc;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100203 }
204
Sergii Dmytruk47e9e8c2022-11-02 00:50:03 +0200205 if (tlcl_get_family() == TPM_1) {
206 rc = tlcl1_set_enable();
207 if (rc != TPM_SUCCESS) {
208 printk(BIOS_ERR, "TPM Error (%#x): Can't set enabled state.\n", rc);
209 return rc;
210 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100211
Sergii Dmytruk47e9e8c2022-11-02 00:50:03 +0200212 rc = tlcl1_set_deactivated(0);
213 if (rc != TPM_SUCCESS) {
214 printk(BIOS_ERR, "TPM Error (%#x): Can't set deactivated state.\n", rc);
215 return rc;
216 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100217 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100218
219 return TPM_SUCCESS;
220}
221
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600222tpm_result_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700223 const uint8_t *digest, size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100224{
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600225 tpm_result_t rc;
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200226
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100227 if (!digest)
Jon Murphy056952e2023-09-05 10:44:09 -0600228 return TPM_IOERROR;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100229
Bill XIEc79e96b2019-08-22 20:28:36 +0800230 if (tspi_tpm_is_setup()) {
Jon Murphy24604812023-09-05 10:37:05 -0600231 rc = tlcl_lib_init();
232 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600233 printk(BIOS_ERR, "TPM Error (%#x): Can't initialize library.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600234 return rc;
Bill XIEc79e96b2019-08-22 20:28:36 +0800235 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100236
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700237 printk(BIOS_DEBUG, "TPM: Extending digest for `%s` into PCR %d\n", name, pcr);
Jon Murphy24604812023-09-05 10:37:05 -0600238 rc = tlcl_extend(pcr, digest, digest_algo);
239 if (rc != TPM_SUCCESS) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600240 printk(BIOS_ERR, "TPM Error (%#x): Extending hash for `%s` into PCR %d failed.\n",
241 rc, name, pcr);
Jon Murphy24604812023-09-05 10:37:05 -0600242 return rc;
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700243 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800244 }
245
246 if (CONFIG(TPM_MEASURED_BOOT))
Sergii Dmytruk2710df72022-11-10 00:40:51 +0200247 tpm_log_add_table_entry(name, pcr, digest_algo, digest, digest_len);
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100248
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700249 printk(BIOS_DEBUG, "TPM: Digest of `%s` to PCR %d %s\n",
250 name, pcr, tspi_tpm_is_setup() ? "measured" : "logged");
251
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700252 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100253}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200254
Bill XIEc79e96b2019-08-22 20:28:36 +0800255#if CONFIG(VBOOT_LIB)
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600256tpm_result_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
Werner Zeh30cf14f2018-10-23 07:40:08 +0200257 const char *rname)
258{
259 uint8_t digest[TPM_PCR_MAX_LEN], digest_len;
260 uint8_t buf[HASH_DATA_CHUNK_SIZE];
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700261 uint32_t offset;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200262 size_t len;
263 struct vb2_digest_context ctx;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200264
265 if (!rdev || !rname)
Jon Murphy056952e2023-09-05 10:44:09 -0600266 return TPM_CB_INVALID_ARG;
Bill XIEc79e96b2019-08-22 20:28:36 +0800267
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700268 digest_len = vb2_digest_size(TPM_MEASURE_ALGO);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200269 assert(digest_len <= sizeof(digest));
Julius Wernerd96ca242022-08-08 18:08:35 -0700270 if (vb2_digest_init(&ctx, vboot_hwcrypto_allowed(), TPM_MEASURE_ALGO,
271 region_device_sz(rdev))) {
Werner Zeh30cf14f2018-10-23 07:40:08 +0200272 printk(BIOS_ERR, "TPM: Error initializing hash.\n");
Jon Murphy056952e2023-09-05 10:44:09 -0600273 return TPM_CB_HASH_ERROR;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200274 }
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);
Jon Murphy056952e2023-09-05 10:44:09 -0600285 return TPM_CB_READ_FAILURE;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200286 }
287 if (vb2_digest_extend(&ctx, buf, len)) {
288 printk(BIOS_ERR, "TPM: Error extending hash.\n");
Jon Murphy056952e2023-09-05 10:44:09 -0600289 return TPM_CB_HASH_ERROR;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200290 }
291 }
292 if (vb2_digest_finalize(&ctx, digest, digest_len)) {
293 printk(BIOS_ERR, "TPM: Error finalizing hash.\n");
Jon Murphy056952e2023-09-05 10:44:09 -0600294 return TPM_CB_HASH_ERROR;
Werner Zeh30cf14f2018-10-23 07:40:08 +0200295 }
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700296 return tpm_extend_pcr(pcr, TPM_MEASURE_ALGO, digest, digest_len, rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200297}
Bill XIEc79e96b2019-08-22 20:28:36 +0800298#endif /* VBOOT_LIB */