blob: 7a8e2befdf0a7b325cb05ddd0a1fdf28d4ce7fe7 [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
Arthur Heymans6f8e9442021-03-29 14:23:53 +02003#include <rules.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01004#include <console/cbmem_console.h>
5#include <console/console.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08006#include <security/tpm/tspi/crtm.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +01007#include <security/tpm/tspi.h>
8#include <security/tpm/tss.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08009#include <assert.h>
10#include <security/vboot/misc.h>
Werner Zeh30cf14f2018-10-23 07:40:08 +020011#include <vb2_api.h>
Joel Kitching2eb89c82019-04-25 17:45:12 +080012#include <vb2_sha.h>
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010013
Julius Wernercd49cce2019-03-05 16:53:33 -080014#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010015static uint32_t tpm1_invoke_state_machine(void)
16{
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070017 uint8_t disabled;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010018 uint8_t deactivated;
19 uint32_t result = TPM_SUCCESS;
20
21 /* Check that the TPM is enabled and activated. */
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070022 result = tlcl_get_flags(&disabled, &deactivated, NULL);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010023 if (result != TPM_SUCCESS) {
24 printk(BIOS_ERR, "TPM: Can't read capabilities.\n");
25 return result;
26 }
27
Philipp Deppenwiese4d2af9d2018-08-14 09:46:55 -070028 if (disabled) {
29 printk(BIOS_INFO, "TPM: is disabled. Enabling...\n");
30
31 result = tlcl_set_enable();
32 if (result != TPM_SUCCESS) {
33 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
34 return result;
35 }
36 }
37
Julius Wernercd49cce2019-03-05 16:53:33 -080038 if (!!deactivated != CONFIG(TPM_DEACTIVATE)) {
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010039 printk(BIOS_INFO,
40 "TPM: Unexpected TPM deactivated state. Toggling...\n");
41 result = tlcl_set_deactivated(!deactivated);
42 if (result != TPM_SUCCESS) {
43 printk(BIOS_ERR,
44 "TPM: Can't toggle deactivated state.\n");
45 return result;
46 }
47
48 deactivated = !deactivated;
49 result = TPM_E_MUST_REBOOT;
50 }
51
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010052 return result;
53}
54#endif
55
Joel Kitching9937a062018-10-11 18:16:59 +080056static uint32_t tpm_setup_s3_helper(void)
57{
58 uint32_t result;
59
60 result = tlcl_resume();
61 switch (result) {
62 case TPM_SUCCESS:
63 break;
64
65 case TPM_E_INVALID_POSTINIT:
66 /*
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");
71 result = TPM_SUCCESS;
72 break;
73
74 default:
75 printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", result);
76 break;
Joel Kitching9937a062018-10-11 18:16:59 +080077 }
78
79 return result;
80}
81
82static uint32_t tpm_setup_epilogue(uint32_t result)
83{
84 if (result != TPM_SUCCESS)
85 post_code(POST_TPM_FAILURE);
86 else
87 printk(BIOS_INFO, "TPM: setup succeeded\n");
88
89 return result;
90}
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 */
137uint32_t tpm_setup(int s3flag)
138{
139 uint32_t result;
140
141 result = tlcl_lib_init();
142 if (result != TPM_SUCCESS) {
143 printk(BIOS_ERR, "TPM: Can't initialize.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800144 return tpm_setup_epilogue(result);
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
153 result = tlcl_startup();
Arthur Heymans6d5fcf42019-10-14 17:06:27 +0200154 if (CONFIG(TPM_STARTUP_IGNORE_POSTINIT)
155 && result == TPM_E_INVALID_POSTINIT) {
156 printk(BIOS_DEBUG, "TPM: ignoring invalid POSTINIT\n");
157 result = TPM_SUCCESS;
158 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100159 if (result != TPM_SUCCESS) {
160 printk(BIOS_ERR, "TPM: Can't run startup command.\n");
Joel Kitching9937a062018-10-11 18:16:59 +0800161 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100162 }
163
164 result = tlcl_assert_physical_presence();
165 if (result != TPM_SUCCESS) {
166 /*
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 */
171 result = tlcl_physical_presence_cmd_enable();
172 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800173 printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n");
174 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100175 }
176
177 result = tlcl_assert_physical_presence();
178 if (result != TPM_SUCCESS) {
Joel Kitching9937a062018-10-11 18:16:59 +0800179 printk(BIOS_ERR, "TPM: Can't assert physical presence.\n");
180 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100181 }
182 }
183
Julius Wernercd49cce2019-03-05 16:53:33 -0800184#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100185 result = tpm1_invoke_state_machine();
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100186#endif
Arthur Heymans6f8e9442021-03-29 14:23:53 +0200187 if (CONFIG(TPM_MEASURED_BOOT) && !CONFIG(TPM_MEASURED_BOOT_INIT_BOOTBLOCK))
Bill XIEc79e96b2019-08-22 20:28:36 +0800188 result = 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;
Joel Kitching9937a062018-10-11 18:16:59 +0800191 return tpm_setup_epilogue(result);
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100192}
193
194uint32_t tpm_clear_and_reenable(void)
195{
196 uint32_t result;
197
198 printk(BIOS_INFO, "TPM: Clear and re-enable\n");
199 result = tlcl_force_clear();
200 if (result != TPM_SUCCESS) {
201 printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n");
202 return result;
203 }
204
Julius Wernercd49cce2019-03-05 16:53:33 -0800205#if CONFIG(TPM1)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100206 result = tlcl_set_enable();
207 if (result != TPM_SUCCESS) {
208 printk(BIOS_ERR, "TPM: Can't set enabled state.\n");
209 return result;
210 }
211
212 result = tlcl_set_deactivated(0);
213 if (result != TPM_SUCCESS) {
214 printk(BIOS_ERR, "TPM: Can't set deactivated state.\n");
215 return result;
216 }
217#endif
218
219 return TPM_SUCCESS;
220}
221
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100222uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo,
223 uint8_t *digest, size_t digest_len, const char *name)
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100224{
Philipp Deppenwiesef8499722018-07-30 01:27:47 +0200225 uint32_t result;
226
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100227 if (!digest)
228 return TPM_E_IOERROR;
229
Bill XIEc79e96b2019-08-22 20:28:36 +0800230 if (tspi_tpm_is_setup()) {
231 result = tlcl_lib_init();
232 if (result != TPM_SUCCESS) {
233 printk(BIOS_ERR, "TPM: Can't initialize library.\n");
234 return result;
235 }
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100236
Bill XIEc79e96b2019-08-22 20:28:36 +0800237 printk(BIOS_DEBUG, "TPM: Extending digest for %s into PCR %d\n", name, pcr);
238 result = tlcl_extend(pcr, digest, NULL);
239 if (result != TPM_SUCCESS)
240 return result;
241 }
242
243 if (CONFIG(TPM_MEASURED_BOOT))
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100244 tcpa_log_add_table_entry(name, pcr, digest_algo,
245 digest, digest_len);
246
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700247 return TPM_SUCCESS;
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100248}
Werner Zeh30cf14f2018-10-23 07:40:08 +0200249
Bill XIEc79e96b2019-08-22 20:28:36 +0800250#if CONFIG(VBOOT_LIB)
Werner Zeh30cf14f2018-10-23 07:40:08 +0200251uint32_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr,
252 const char *rname)
253{
254 uint8_t digest[TPM_PCR_MAX_LEN], digest_len;
255 uint8_t buf[HASH_DATA_CHUNK_SIZE];
256 uint32_t result, offset;
257 size_t len;
258 struct vb2_digest_context ctx;
259 enum vb2_hash_algorithm hash_alg;
260
261 if (!rdev || !rname)
262 return TPM_E_INVALID_ARG;
Bill XIEc79e96b2019-08-22 20:28:36 +0800263
Julius Wernercd49cce2019-03-05 16:53:33 -0800264 if (CONFIG(TPM1)) {
Werner Zeh30cf14f2018-10-23 07:40:08 +0200265 hash_alg = VB2_HASH_SHA1;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100266 } else { /* CONFIG_TPM2 */
Werner Zeh30cf14f2018-10-23 07:40:08 +0200267 hash_alg = VB2_HASH_SHA256;
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100268 }
Werner Zeh30cf14f2018-10-23 07:40:08 +0200269
270 digest_len = vb2_digest_size(hash_alg);
271 assert(digest_len <= sizeof(digest));
272 if (vb2_digest_init(&ctx, hash_alg)) {
273 printk(BIOS_ERR, "TPM: Error initializing hash.\n");
274 return TPM_E_HASH_ERROR;
275 }
276 /*
277 * Though one can mmap the full needed region on x86 this is not the
278 * case for e.g. ARM. In order to make this code as universal as
279 * possible across different platforms read the data to hash in chunks.
280 */
281 for (offset = 0; offset < region_device_sz(rdev); offset += len) {
282 len = MIN(sizeof(buf), region_device_sz(rdev) - offset);
283 if (rdev_readat(rdev, buf, offset, len) < 0) {
284 printk(BIOS_ERR, "TPM: Not able to read region %s.\n",
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100285 rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200286 return TPM_E_READ_FAILURE;
287 }
288 if (vb2_digest_extend(&ctx, buf, len)) {
289 printk(BIOS_ERR, "TPM: Error extending hash.\n");
290 return TPM_E_HASH_ERROR;
291 }
292 }
293 if (vb2_digest_finalize(&ctx, digest, digest_len)) {
294 printk(BIOS_ERR, "TPM: Error finalizing hash.\n");
295 return TPM_E_HASH_ERROR;
296 }
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100297 result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname);
Werner Zeh30cf14f2018-10-23 07:40:08 +0200298 if (result != TPM_SUCCESS) {
299 printk(BIOS_ERR, "TPM: Extending hash into PCR failed.\n");
300 return result;
301 }
Bill XIEc79e96b2019-08-22 20:28:36 +0800302 printk(BIOS_DEBUG, "TPM: Digest of %s to PCR %d %s\n",
303 rname, pcr, tspi_tpm_is_setup() ? "measured" : "logged");
Werner Zeh30cf14f2018-10-23 07:40:08 +0200304 return TPM_SUCCESS;
305}
Bill XIEc79e96b2019-08-22 20:28:36 +0800306#endif /* VBOOT_LIB */