blob: 6ef01383ce2317a999fcccfb5e3569f995dfa7c3 [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
3#include <console/cbmem_console.h>
4#include <console/console.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08005#include <security/tpm/tspi/crtm.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)
84 post_code(POST_TPM_FAILURE);
85 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
107 if (ENV_RAMSTAGE)
108 return tpm_is_setup;
109
110 return 0;
111}
112
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100113/*
114 * tpm_setup starts the TPM and establishes the root of trust for the
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100115 * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug.
116 * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100117 * general we cannot easily distinguish the kind of failure, so our strategy is
Jonathan Neuschäfer61322d72018-10-29 22:52:42 +0100118 * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100119 * again, which executes (almost) the same sequence of operations. There is a
120 * good chance that, if recovery mode was entered because of a TPM failure, the
121 * failure will repeat itself. (In general this is impossible to guarantee
122 * because we have no way of creating the exact TPM initial state at the
123 * previous boot.) In recovery mode, we ignore the failure and continue, thus
124 * giving the recovery kernel a chance to fix things (that's why we don't set
125 * bGlobalLock). The choice is between a knowingly insecure device and a
126 * bricked device.
127 *
128 * As a side note, observe that we go through considerable hoops to avoid using
129 * the STCLEAR permissions for the index spaces. We do this to avoid writing
130 * to the TPM flashram at every reboot or wake-up, because of concerns about
131 * the durability of the NVRAM.
132 */
133uint32_t tpm_setup(int s3flag)
134{
135 uint32_t result;
136
137 result = tlcl_lib_init();
138 if (result != TPM_SUCCESS) {
139 printk(BIOS_ERR, "TPM: Can't initialize.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800140 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100141 }
142
143 /* Handle special init for S3 resume path */
144 if (s3flag) {
Joel Kitching9937a062018-10-11 18:16:59 +0800145 printk(BIOS_INFO, "TPM: Handle S3 resume.\n");
146 return tpm_setup_epilogue(tpm_setup_s3_helper());
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100147 }
148
149 result = tlcl_startup();
Arthur Heymans6d5fcf42019-10-14 17:06:27 +0200150 if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT)
151 && result == TPM_E_INVALID_POSTINIT) {
152 printk(BIOS_DEBUG, "TPM: ignoring invalid POSTINIT\n");
153 result = TPM_SUCCESS;
154 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100155 if (result != TPM_SUCCESS) {
156 printk(BIOS_ERR, "TPM: Can't run startup command.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800157 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100158 }
159
160 result = tlcl_assert_physical_presence();
161 if (result != TPM_SUCCESS) {
162 /*
163 * It is possible that the TPM was delivered with the physical
164 * presence command disabled. This tries enabling it, then
165 * tries asserting PP again.
166 */
167 result = tlcl_physical_presence_cmd_enable();
168 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800169 printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n");
170 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100171 }
172
173 result = tlcl_assert_physical_presence();
174 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800175 printk(BIOS_ERR, "TPM: Can't assert physical presence.\n");
176 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100177 }
178 }
179
Julius Wernercd49cce2019-03-05 16:53:33 -0800180#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100181 result = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100182#endif
Bill XIEc79e96b2019-08-22 20:28:36 +0800183 if (CONFIG(TPM_MEASURED_BOOT))
184 result = tspi_measure_cache_to_pcr();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100185
Bill XIEc79e96b2019-08-22 20:28:36 +0800186 tpm_is_setup = 1;
Joel Kitching9937a062018-10-11 18:16:59 +0800187 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100188}
189
190uint32_t tpm_clear_and_reenable(void)
191{
192 uint32_t result;
193
194 printk(BIOS_INFO, "TPM: Clear and re-enable\n");
195 result = tlcl_force_clear();
196 if (result != TPM_SUCCESS) {
197 printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n");
198 return result;
199 }
200
Julius Wernercd49cce2019-03-05 16:53:33 -0800201#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100202 result = tlcl_set_enable();
203 if (result != TPM_SUCCESS) {
204 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
205 return result;
206 }
207
208 result = tlcl_set_deactivated(0);
209 if (result != TPM_SUCCESS) {
210 printk(BIOS_ERR, "TPM: Can't set deactivated state.\n");
211 return result;
212 }
213#endif
214
215 return TPM_SUCCESS;
216}
217
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100218uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
219 uint8_t *digest, size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100220{
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200221 uint32_t result;
222
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100223 if (!digest)
224 return TPM_E_IOERROR;
225
Bill XIEc79e96b2019-08-22 20:28:36 +0800226 if (tspi_tpm_is_setup()) {
227 result = tlcl_lib_init();
228 if (result != TPM_SUCCESS) {
229 printk(BIOS_ERR, "TPM: Can't initialize library.\n");
230 return result;
231 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100232
Bill XIEc79e96b2019-08-22 20:28:36 +0800233 printk(BIOS_DEBUG, "TPM: Extending digest for %s into PCR %d\n", name, pcr);
234 result = tlcl_extend(pcr, digest, NULL);
235 if (result != TPM_SUCCESS)
236 return result;
237 }
238
239 if (CONFIG(TPM_MEASURED_BOOT))
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100240 tcpa_log_add_table_entry(name, pcr, digest_algo,
241 digest, digest_len);
242
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700243 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100244}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200245
Bill XIEc79e96b2019-08-22 20:28:36 +0800246#if CONFIG(VBOOT_LIB)
Werner Zeh30cf14f2018-10-23 07:40:08 +0200247uint32_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
248 const char *rname)
249{
250 uint8_t digest[TPM_PCR_MAX_LEN], digest_len;
251 uint8_t buf[HASH_DATA_CHUNK_SIZE];
252 uint32_t result, offset;
253 size_t len;
254 struct vb2_digest_context ctx;
255 enum vb2_hash_algorithm hash_alg;
256
257 if (!rdev || !rname)
258 return TPM_E_INVALID_ARG;
Bill XIEc79e96b2019-08-22 20:28:36 +0800259
Julius Wernercd49cce2019-03-05 16:53:33 -0800260 if (CONFIG(TPM1)) {
Werner Zeh30cf14f2018-10-23 07:40:08 +0200261 hash_alg = VB2_HASH_SHA1;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100262 } else { /* CONFIG_TPM2 */
Werner Zeh30cf14f2018-10-23 07:40:08 +0200263 hash_alg = VB2_HASH_SHA256;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100264 }
Werner Zeh30cf14f2018-10-23 07:40:08 +0200265
266 digest_len = vb2_digest_size(hash_alg);
267 assert(digest_len <= sizeof(digest));
268 if (vb2_digest_init(&ctx, hash_alg)) {
269 printk(BIOS_ERR, "TPM: Error initializing hash.\n");
270 return TPM_E_HASH_ERROR;
271 }
272 /*
273 * Though one can mmap the full needed region on x86 this is not the
274 * case for e.g. ARM. In order to make this code as universal as
275 * possible across different platforms read the data to hash in chunks.
276 */
277 for (offset = 0; offset < region_device_sz(rdev); offset += len) {
278 len = MIN(sizeof(buf), region_device_sz(rdev) - offset);
279 if (rdev_readat(rdev, buf, offset, len) < 0) {
280 printk(BIOS_ERR, "TPM: Not able to read region %s.\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100281 rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200282 return TPM_E_READ_FAILURE;
283 }
284 if (vb2_digest_extend(&ctx, buf, len)) {
285 printk(BIOS_ERR, "TPM: Error extending hash.\n");
286 return TPM_E_HASH_ERROR;
287 }
288 }
289 if (vb2_digest_finalize(&ctx, digest, digest_len)) {
290 printk(BIOS_ERR, "TPM: Error finalizing hash.\n");
291 return TPM_E_HASH_ERROR;
292 }
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100293 result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200294 if (result != TPM_SUCCESS) {
295 printk(BIOS_ERR, "TPM: Extending hash into PCR failed.\n");
296 return result;
297 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800298 printk(BIOS_DEBUG, "TPM: Digest of %s to PCR %d %s\n",
299 rname, pcr, tspi_tpm_is_setup() ? "measured" : "logged");
Werner Zeh30cf14f2018-10-23 07:40:08 +0200300 return TPM_SUCCESS;
301}
Bill XIEc79e96b2019-08-22 20:28:36 +0800302#endif /* VBOOT_LIB */