blob: 54ba343ce38b7b70ec2c1be4f2156b5c1a072c6a [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>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01005#include <security/tpm/tspi.h>
6#include <security/tpm/tss.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08007#include <assert.h>
8#include <security/vboot/misc.h>
Werner Zeh30cf14f2018-10-23 07:40:08 +02009#include <vb2_api.h>
Joel Kitching2eb89c82019-04-25 17:45:12 +080010#include <vb2_sha.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010011
Julius Wernercd49cce2019-03-05 16:53:33 -080012#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010013static uint32_t tpm1_invoke_state_machine(void)
14{
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070015 uint8_t disabled;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010016 uint8_t deactivated;
17 uint32_t result = TPM_SUCCESS;
18
19 /* Check that the TPM is enabled and activated. */
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070020 result = tlcl_get_flags(&disabled, &deactivated, NULL);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010021 if (result != TPM_SUCCESS) {
22 printk(BIOS_ERR, "TPM: Can't read capabilities.\n");
23 return result;
24 }
25
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070026 if (disabled) {
27 printk(BIOS_INFO, "TPM: is disabled. Enabling...\n");
28
29 result = tlcl_set_enable();
30 if (result != TPM_SUCCESS) {
31 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
32 return result;
33 }
34 }
35
Julius Wernercd49cce2019-03-05 16:53:33 -080036 if (!!deactivated != CONFIG(TPM_DEACTIVATE)) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010037 printk(BIOS_INFO,
38 "TPM: Unexpected TPM deactivated state. Toggling...\n");
39 result = tlcl_set_deactivated(!deactivated);
40 if (result != TPM_SUCCESS) {
41 printk(BIOS_ERR,
42 "TPM: Can't toggle deactivated state.\n");
43 return result;
44 }
45
46 deactivated = !deactivated;
47 result = TPM_E_MUST_REBOOT;
48 }
49
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010050 return result;
51}
52#endif
53
Joel Kitching9937a062018-10-11 18:16:59 +080054static uint32_t tpm_setup_s3_helper(void)
55{
56 uint32_t result;
57
58 result = tlcl_resume();
59 switch (result) {
60 case TPM_SUCCESS:
61 break;
62
63 case TPM_E_INVALID_POSTINIT:
64 /*
65 * We're on a platform where the TPM maintains power
66 * in S3, so it's already initialized.
67 */
68 printk(BIOS_INFO, "TPM: Already initialized.\n");
69 result = TPM_SUCCESS;
70 break;
71
72 default:
73 printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", result);
74 break;
Joel Kitching9937a062018-10-11 18:16:59 +080075 }
76
77 return result;
78}
79
80static uint32_t tpm_setup_epilogue(uint32_t result)
81{
82 if (result != TPM_SUCCESS)
83 post_code(POST_TPM_FAILURE);
84 else
85 printk(BIOS_INFO, "TPM: setup succeeded\n");
86
87 return result;
88}
89
Bill XIEc79e96b2019-08-22 20:28:36 +080090static int tpm_is_setup;
91static inline int tspi_tpm_is_setup(void)
92{
Julius Werner23a82e82020-03-31 13:32:10 -070093 /*
94 * vboot_logic_executed() only starts returning true at the end of
95 * verstage, but the vboot logic itself already wants to extend PCRs
96 * before that. So in the stage where verification actually runs, we
97 * need to check tpm_is_setup. Skip that check in all other stages so
98 * this whole function can be evaluated at compile time.
99 */
100 if (CONFIG(VBOOT)) {
101 if (verification_should_run())
102 return tpm_is_setup;
103 return vboot_logic_executed();
104 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800105
Arthur Heymans6f8e9442021-03-29 14:23:53 +0200106 if (CONFIG(TPM_MEASURED_BOOT_INIT_BOOTBLOCK))
107 return ENV_BOOTBLOCK ? tpm_is_setup : 1;
108
Bill XIEc79e96b2019-08-22 20:28:36 +0800109 if (ENV_RAMSTAGE)
110 return tpm_is_setup;
111
112 return 0;
113}
114
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100115/*
116 * tpm_setup starts the TPM and establishes the root of trust for the
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100117 * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug.
118 * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100119 * general we cannot easily distinguish the kind of failure, so our strategy is
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100120 * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100121 * again, which executes (almost) the same sequence of operations. There is a
122 * good chance that, if recovery mode was entered because of a TPM failure, the
123 * failure will repeat itself. (In general this is impossible to guarantee
124 * because we have no way of creating the exact TPM initial state at the
125 * previous boot.) In recovery mode, we ignore the failure and continue, thus
126 * giving the recovery kernel a chance to fix things (that's why we don't set
127 * bGlobalLock). The choice is between a knowingly insecure device and a
128 * bricked device.
129 *
130 * As a side note, observe that we go through considerable hoops to avoid using
131 * the STCLEAR permissions for the index spaces. We do this to avoid writing
132 * to the TPM flashram at every reboot or wake-up, because of concerns about
133 * the durability of the NVRAM.
134 */
135uint32_t tpm_setup(int s3flag)
136{
137 uint32_t result;
138
139 result = tlcl_lib_init();
140 if (result != TPM_SUCCESS) {
141 printk(BIOS_ERR, "TPM: Can't initialize.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800142 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100143 }
144
145 /* Handle special init for S3 resume path */
146 if (s3flag) {
Joel Kitching9937a062018-10-11 18:16:59 +0800147 printk(BIOS_INFO, "TPM: Handle S3 resume.\n");
148 return tpm_setup_epilogue(tpm_setup_s3_helper());
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100149 }
150
151 result = tlcl_startup();
Arthur Heymans6d5fcf42019-10-14 17:06:27 +0200152 if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT)
153 && result == TPM_E_INVALID_POSTINIT) {
154 printk(BIOS_DEBUG, "TPM: ignoring invalid POSTINIT\n");
155 result = TPM_SUCCESS;
156 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100157 if (result != TPM_SUCCESS) {
158 printk(BIOS_ERR, "TPM: Can't run startup command.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800159 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) {
164 /*
165 * It is possible that the TPM was delivered with the physical
166 * presence command disabled. This tries enabling it, then
167 * tries asserting PP again.
168 */
169 result = tlcl_physical_presence_cmd_enable();
170 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800171 printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n");
172 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100173 }
174
175 result = tlcl_assert_physical_presence();
176 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800177 printk(BIOS_ERR, "TPM: Can't assert physical presence.\n");
178 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100179 }
180 }
181
Julius Wernercd49cce2019-03-05 16:53:33 -0800182#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100183 result = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100184#endif
Arthur Heymansb192af12021-05-20 09:09:56 +0200185 if (CONFIG(TPM_MEASURED_BOOT))
Bill XIEc79e96b2019-08-22 20:28:36 +0800186 result = tspi_measure_cache_to_pcr();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100187
Bill XIEc79e96b2019-08-22 20:28:36 +0800188 tpm_is_setup = 1;
Joel Kitching9937a062018-10-11 18:16:59 +0800189 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100190}
191
192uint32_t tpm_clear_and_reenable(void)
193{
194 uint32_t result;
195
196 printk(BIOS_INFO, "TPM: Clear and re-enable\n");
197 result = tlcl_force_clear();
198 if (result != TPM_SUCCESS) {
199 printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n");
200 return result;
201 }
202
Julius Wernercd49cce2019-03-05 16:53:33 -0800203#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100204 result = tlcl_set_enable();
205 if (result != TPM_SUCCESS) {
206 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
207 return result;
208 }
209
210 result = tlcl_set_deactivated(0);
211 if (result != TPM_SUCCESS) {
212 printk(BIOS_ERR, "TPM: Can't set deactivated state.\n");
213 return result;
214 }
215#endif
216
217 return TPM_SUCCESS;
218}
219
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100220uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700221 const uint8_t *digest, size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100222{
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200223 uint32_t result;
224
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100225 if (!digest)
226 return TPM_E_IOERROR;
227
Bill XIEc79e96b2019-08-22 20:28:36 +0800228 if (tspi_tpm_is_setup()) {
229 result = tlcl_lib_init();
230 if (result != TPM_SUCCESS) {
231 printk(BIOS_ERR, "TPM: Can't initialize library.\n");
232 return result;
233 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100234
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700235 printk(BIOS_DEBUG, "TPM: Extending digest for `%s` into PCR %d\n", name, pcr);
Sergii Dmytruk7221a6c2022-10-22 20:11:35 +0300236 result = tlcl_extend(pcr, digest, digest_algo);
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700237 if (result != TPM_SUCCESS) {
238 printk(BIOS_ERR, "TPM: Extending hash for `%s` into PCR %d failed.\n",
239 name, pcr);
Bill XIEc79e96b2019-08-22 20:28:36 +0800240 return result;
Julius Werner7e7cc1a2021-08-11 18:19:23 -0700241 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800242 }
243
244 if (CONFIG(TPM_MEASURED_BOOT))
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100245 tcpa_log_add_table_entry(name, pcr, digest_algo,
246 digest, digest_len);
247
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 */