blob: 509169fc95cd9b8c61ceab5e4202a105e947018a [file] [log] [blame]
Angel Pons986d50e2020-04-02 23:48:53 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin588ad7b2015-09-29 17:56:59 -05002
Aaron Durbin588ad7b2015-09-29 17:56:59 -05003#include <arch/exception.h>
4#include <assert.h>
Elyes HAOUAS27718ac2020-09-19 09:32:36 +02005#include <console/console.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -07006#include <bootmode.h>
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +08007#include <fmap.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08008#include <security/tpm/tspi/crtm.h>
dnojiridff56a02020-04-03 10:56:43 -07009#include <security/tpm/tss/vendor/cr50/cr50.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080010#include <security/vboot/misc.h>
11#include <security/vboot/vbnv.h>
12#include <security/vboot/tpm_common.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050013#include <string.h>
14#include <timestamp.h>
15#include <vb2_api.h>
Patrick Rudolph6093c502019-05-08 18:36:39 +020016#include <boot_device.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050017
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010018#include "antirollback.h"
19
Aaron Durbin87c9fae2016-01-22 15:26:04 -060020/* The max hash size to expect is for SHA512. */
21#define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE
22
Aaron Durbin588ad7b2015-09-29 17:56:59 -050023/* exports */
24
Joel Kitching220ac042019-07-31 14:19:00 +080025vb2_error_t vb2ex_read_resource(struct vb2_context *ctx,
26 enum vb2_resource_index index,
27 uint32_t offset,
28 void *buf,
29 uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050030{
31 struct region_device rdev;
32 const char *name;
33
34 switch (index) {
35 case VB2_RES_GBB:
36 name = "GBB";
37 break;
38 case VB2_RES_FW_VBLOCK:
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080039 if (vboot_is_firmware_slot_a(ctx))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050040 name = "VBLOCK_A";
41 else
42 name = "VBLOCK_B";
43 break;
44 default:
45 return VB2_ERROR_EX_READ_RESOURCE_INDEX;
46 }
47
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +080048 if (fmap_locate_area_as_rdev(name, &rdev))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050049 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
50
51 if (rdev_readat(&rdev, buf, offset, size) != size)
52 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
53
54 return VB2_SUCCESS;
55}
56
Aaron Durbin87c9fae2016-01-22 15:26:04 -060057static int handle_digest_result(void *slot_hash, size_t slot_hash_sz)
58{
59 int is_resume;
60
61 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +053062 * Chrome EC is the only support for vboot_save_hash() &
63 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +053064 */
Julius Wernercd49cce2019-03-05 16:53:33 -080065 if (!CONFIG(EC_GOOGLE_CHROMEEC))
Naresh G Solanki3d44d692016-11-06 12:51:14 +053066 return 0;
67
68 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -060069 * Nothing to do since resuming on the platform doesn't require
70 * vboot verification again.
71 */
Julius Wernercd49cce2019-03-05 16:53:33 -080072 if (!CONFIG(RESUME_PATH_SAME_AS_BOOT))
Aaron Durbin87c9fae2016-01-22 15:26:04 -060073 return 0;
74
75 /*
76 * Assume that if vboot doesn't start in bootblock verified
77 * RW memory init code is not employed. i.e. memory init code
78 * lives in RO CBFS.
79 */
Julius Wernercd49cce2019-03-05 16:53:33 -080080 if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Aaron Durbin87c9fae2016-01-22 15:26:04 -060081 return 0;
82
Bill XIE516c0a52020-02-24 23:08:35 +080083 is_resume = platform_is_resuming();
Aaron Durbin87c9fae2016-01-22 15:26:04 -060084
85 if (is_resume > 0) {
86 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
87 const size_t saved_hash_sz = sizeof(saved_hash);
88
89 assert(slot_hash_sz == saved_hash_sz);
90
91 printk(BIOS_DEBUG, "Platform is resuming.\n");
92
93 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
94 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
95 return -1;
96 }
97
98 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
99 printk(BIOS_ERR, "Hash mismatch on resume.\n");
100 return -1;
101 }
102 } else if (is_resume < 0)
103 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
104
105 printk(BIOS_DEBUG, "Saving vboot hash.\n");
106
107 /* Always save the hash for the current boot. */
108 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
109 printk(BIOS_ERR, "Error saving vboot hash.\n");
110 /* Though this is an error don't report it up since it could
111 * lead to a reboot loop. The consequence of this is that
112 * we will most likely fail resuming because of EC issues or
113 * the hash digest not matching. */
114 return 0;
115 }
116
117 return 0;
118}
119
Joel Kitching220ac042019-07-31 14:19:00 +0800120static vb2_error_t hash_body(struct vb2_context *ctx,
Julius Wernerf8e17642019-12-12 13:23:06 -0800121 struct region_device *fw_body)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500122{
123 uint64_t load_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800124 uint32_t remaining;
Martin Roth8839b7f2020-10-28 11:38:57 -0600125 uint8_t block[CONFIG_VBOOT_HASH_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600126 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
127 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500128 size_t block_size = sizeof(block);
129 size_t offset;
Joel Kitching220ac042019-07-31 14:19:00 +0800130 vb2_error_t rv;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500131
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600132 /* Clear the full digest so that any hash digests less than the
133 * max have trailing zeros. */
134 memset(hash_digest, 0, hash_digest_sz);
135
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500136 /*
137 * Since loading the firmware and calculating its hash is intertwined,
138 * we use this little trick to measure them separately and pretend it
139 * was first loaded and then hashed in one piece with the timestamps.
140 * (This split won't make sense with memory-mapped media like on x86.)
141 */
142 load_ts = timestamp_get();
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100143 timestamp_add(TS_HASH_BODY_START, load_ts);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500144
Julius Wernerf8e17642019-12-12 13:23:06 -0800145 remaining = region_device_sz(fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500146 offset = 0;
147
148 /* Start the body hash */
Julius Wernerf8e17642019-12-12 13:23:06 -0800149 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500150 if (rv)
151 return rv;
152
153 /* Extend over the body */
Julius Wernerf8e17642019-12-12 13:23:06 -0800154 while (remaining) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500155 uint64_t temp_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800156 if (block_size > remaining)
157 block_size = remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500158
159 temp_ts = timestamp_get();
Julius Wernerf8e17642019-12-12 13:23:06 -0800160 if (rdev_readat(fw_body, block, offset, block_size) < 0)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500161 return VB2_ERROR_UNKNOWN;
162 load_ts += timestamp_get() - temp_ts;
163
164 rv = vb2api_extend_hash(ctx, block, block_size);
165 if (rv)
166 return rv;
167
Julius Wernerf8e17642019-12-12 13:23:06 -0800168 remaining -= block_size;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500169 offset += block_size;
170 }
171
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100172 timestamp_add(TS_LOADING_END, load_ts);
173 timestamp_add_now(TS_HASHING_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500174
175 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600176 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500177 if (rv)
178 return rv;
179
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100180 timestamp_add_now(TS_HASH_BODY_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500181
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600182 if (handle_digest_result(hash_digest, hash_digest_sz))
183 return VB2_ERROR_UNKNOWN;
184
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500185 return VB2_SUCCESS;
186}
187
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500188static uint32_t extend_pcrs(struct vb2_context *ctx)
189{
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100190 return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100191 vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500192}
193
Daisuke Nojiri494a5dd2021-05-12 12:50:41 -0700194#define EC_EFS_BOOT_MODE_VERIFIED_RW 0x00
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700195#define EC_EFS_BOOT_MODE_UNTRUSTED_RO 0x01
Daisuke Nojiri494a5dd2021-05-12 12:50:41 -0700196#define EC_EFS_BOOT_MODE_TRUSTED_RO 0x02
dnojiridff56a02020-04-03 10:56:43 -0700197
198static const char *get_boot_mode_string(uint8_t boot_mode)
199{
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700200 if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO)
201 return "TRUSTED_RO";
202 else if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO)
203 return "UNTRUSTED_RO";
204 else if (boot_mode == EC_EFS_BOOT_MODE_VERIFIED_RW)
205 return "VERIFIED_RW";
dnojiridff56a02020-04-03 10:56:43 -0700206 else
207 return "UNDEFINED";
208}
209
210static void check_boot_mode(struct vb2_context *ctx)
211{
212 uint8_t boot_mode;
213 int rv;
214
215 rv = tlcl_cr50_get_boot_mode(&boot_mode);
216 switch (rv) {
217 case TPM_E_NO_SUCH_COMMAND:
218 printk(BIOS_WARNING, "Cr50 does not support GET_BOOT_MODE.\n");
219 /* Proceed to legacy boot model. */
220 return;
221 case TPM_SUCCESS:
222 break;
223 default:
224 printk(BIOS_ERR,
225 "Communication error in getting Cr50 boot mode.\n");
dnojiridff56a02020-04-03 10:56:43 -0700226 vb2api_fail(ctx, VB2_RECOVERY_CR50_BOOT_MODE, rv);
dnojiridff56a02020-04-03 10:56:43 -0700227 return;
228 }
229
230 printk(BIOS_INFO, "Cr50 says boot_mode is %s(0x%02x).\n",
231 get_boot_mode_string(boot_mode), boot_mode);
232
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700233 if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO)
dnojiridff56a02020-04-03 10:56:43 -0700234 ctx->flags |= VB2_CONTEXT_NO_BOOT;
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700235 else if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO)
236 ctx->flags |= VB2_CONTEXT_EC_TRUSTED;
dnojiridff56a02020-04-03 10:56:43 -0700237}
238
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500239/**
240 * Verify and select the firmware in the RW image
241 *
242 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
243 * when per-stage verification is ready.
244 */
245void verstage_main(void)
246{
Joel Kitching2332c742019-10-23 15:01:37 +0800247 struct vb2_context *ctx;
Julius Wernerf8e17642019-12-12 13:23:06 -0800248 struct region_device fw_body;
Joel Kitching220ac042019-07-31 14:19:00 +0800249 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500250
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100251 timestamp_add_now(TS_VBOOT_START);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500252
Patrick Rudolph6093c502019-05-08 18:36:39 +0200253 /* Lockdown SPI flash controller if required */
254 if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE))
255 boot_device_security_lockdown();
256
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500257 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800258 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500259
Aaron Durbin118a84f2017-09-15 11:15:07 -0600260 /* Initialize and read nvdata from non-volatile storage. */
Joel Kitching2332c742019-10-23 15:01:37 +0800261 vbnv_init(ctx->nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500262
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800263 /* Set S3 resume flag if vboot should behave differently when selecting
264 * which slot to boot. This is only relevant to vboot if the platform
265 * does verification of memory init and thus must ensure it resumes with
266 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800267 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Bill XIE516c0a52020-02-24 23:08:35 +0800268 platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800269 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800270
Duncan Lauriea613a312016-03-14 09:32:08 -0700271 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
272 * check the return value here because vb2api_fw_phase1 will catch
273 * invalid secdata and tell us what to do (=reboot). */
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100274 timestamp_add_now(TS_TPMINIT_START);
dnojiridff56a02020-04-03 10:56:43 -0700275 if (vboot_setup_tpm(ctx) == TPM_SUCCESS) {
Joel Kitching2332c742019-10-23 15:01:37 +0800276 antirollback_read_space_firmware(ctx);
dnojiridff56a02020-04-03 10:56:43 -0700277 antirollback_read_space_kernel(ctx);
278 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100279 timestamp_add_now(TS_TPMINIT_END);
Duncan Lauriea613a312016-03-14 09:32:08 -0700280
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500281 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800282 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800283 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800284 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500285 }
286
Julius Wernercd49cce2019-03-05 16:53:33 -0800287 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100288 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800289 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500290
Julius Wernercd49cce2019-03-05 16:53:33 -0800291 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800292 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500293
Joel Kitching5923d672019-04-12 15:57:43 +0800294 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100295 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800296 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800297
Daisuke Nojiri850728b2021-05-12 12:50:41 -0700298 /*
299 * Get boot mode from GSC. This allows us to refuse to boot OS
300 * (with VB2_CONTEXT_NO_BOOT) or to switch to developer mode (with
301 * !VB2_CONTEXT_EC_TRUSTED).
302 *
303 * If there is an communication error, a recovery reason will be set and
304 * vb2api_fw_phase1 will route us to recovery mode.
305 */
306 if (CONFIG(TPM_CR50))
307 check_boot_mode(ctx);
308
Hsuan-ting Chen642508a2021-10-27 10:59:41 +0000309 if (get_ec_is_trusted())
310 ctx->flags |= VB2_CONTEXT_EC_TRUSTED;
311
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500312 /* Do early init (set up secdata and NVRAM, load GBB) */
313 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800314 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500315
316 if (rv) {
317 /*
318 * If vb2api_fw_phase1 fails, check for return value.
319 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
320 * into recovery mode.
321 * For any other error code, save context if needed and reboot.
322 */
323 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
324 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600325 vboot_save_data(ctx);
Joel Kitching2332c742019-10-23 15:01:37 +0800326 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800327 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500328 }
329
Raul E Rangel3a591742018-07-17 14:44:43 -0600330 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600331 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500332 vboot_reboot();
333 }
334
335 /* Determine which firmware slot to boot (based on NVRAM) */
336 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800337 rv = vb2api_fw_phase2(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500338 if (rv) {
339 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600340 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500341 vboot_reboot();
342 }
343
344 /* Try that slot (verify its keyblock and preamble) */
345 printk(BIOS_INFO, "Phase 3\n");
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100346 timestamp_add_now(TS_VERIFY_SLOT_START);
Joel Kitching2332c742019-10-23 15:01:37 +0800347 rv = vb2api_fw_phase3(ctx);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100348 timestamp_add_now(TS_VERIFY_SLOT_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500349 if (rv) {
350 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600351 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500352 vboot_reboot();
353 }
354
355 printk(BIOS_INFO, "Phase 4\n");
Julius Wernerf8e17642019-12-12 13:23:06 -0800356 rv = vboot_locate_firmware(ctx, &fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500357 if (rv)
Keith Short70064582019-05-06 16:12:57 -0600358 die_with_post_code(POST_INVALID_ROM,
359 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500360
Julius Wernerf8e17642019-12-12 13:23:06 -0800361 rv = hash_body(ctx, &fw_body);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600362 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500363 if (rv) {
364 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
365 vboot_reboot();
366 }
367
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800368 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800369 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100370 timestamp_add_now(TS_TPMPCR_START);
Joel Kitching2332c742019-10-23 15:01:37 +0800371 rv = extend_pcrs(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800372 if (rv) {
373 printk(BIOS_WARNING,
374 "Failed to extend TPM PCRs (%#x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800375 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600376 vboot_save_data(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800377 vboot_reboot();
378 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100379 timestamp_add_now(TS_TPMPCR_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500380 }
381
382 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600383
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100384 timestamp_add_now(TS_TPMLOCK_START);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500385 rv = antirollback_lock_space_firmware();
386 if (rv) {
387 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800388 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600389 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500390 vboot_reboot();
391 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100392 timestamp_add_now(TS_TPMLOCK_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500393
Furquan Shaikhb038f412016-11-07 23:47:11 -0800394 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800395 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Shelley Chena79803c2020-10-16 13:15:59 -0700396 rv = antirollback_lock_space_mrc_hash(MRC_REC_HASH_NV_INDEX);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800397 if (rv) {
398 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
399 rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800400 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
Furquan Shaikhb038f412016-11-07 23:47:11 -0800401 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600402 vboot_save_data(ctx);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800403 vboot_reboot();
404 }
405 }
406
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800407 printk(BIOS_INFO, "Slot %c is selected\n",
408 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800409
410 verstage_main_exit:
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100411 timestamp_add_now(TS_VBOOT_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500412}