blob: 70c7d77073f6469b7a7ae1777e81838ccae8e175 [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
57/* No-op stubs that can be overridden by SoCs with hardware crypto support. */
Joel Kitching220ac042019-07-31 14:19:00 +080058__weak vb2_error_t vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg,
59 uint32_t data_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050060{
61 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
62}
63
Joel Kitching220ac042019-07-31 14:19:00 +080064__weak vb2_error_t vb2ex_hwcrypto_digest_extend(const uint8_t *buf,
65 uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050066{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010067 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -050068 return VB2_ERROR_UNKNOWN;
69}
70
Joel Kitching220ac042019-07-31 14:19:00 +080071__weak vb2_error_t vb2ex_hwcrypto_digest_finalize(uint8_t *digest,
72 uint32_t digest_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050073{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010074 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -050075 return VB2_ERROR_UNKNOWN;
76}
77
Aaron Durbin87c9fae2016-01-22 15:26:04 -060078static int handle_digest_result(void *slot_hash, size_t slot_hash_sz)
79{
80 int is_resume;
81
82 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +053083 * Chrome EC is the only support for vboot_save_hash() &
84 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +053085 */
Julius Wernercd49cce2019-03-05 16:53:33 -080086 if (!CONFIG(EC_GOOGLE_CHROMEEC))
Naresh G Solanki3d44d692016-11-06 12:51:14 +053087 return 0;
88
89 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -060090 * Nothing to do since resuming on the platform doesn't require
91 * vboot verification again.
92 */
Julius Wernercd49cce2019-03-05 16:53:33 -080093 if (!CONFIG(RESUME_PATH_SAME_AS_BOOT))
Aaron Durbin87c9fae2016-01-22 15:26:04 -060094 return 0;
95
96 /*
97 * Assume that if vboot doesn't start in bootblock verified
98 * RW memory init code is not employed. i.e. memory init code
99 * lives in RO CBFS.
100 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800101 if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600102 return 0;
103
Bill XIE516c0a52020-02-24 23:08:35 +0800104 is_resume = platform_is_resuming();
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600105
106 if (is_resume > 0) {
107 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
108 const size_t saved_hash_sz = sizeof(saved_hash);
109
110 assert(slot_hash_sz == saved_hash_sz);
111
112 printk(BIOS_DEBUG, "Platform is resuming.\n");
113
114 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
115 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
116 return -1;
117 }
118
119 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
120 printk(BIOS_ERR, "Hash mismatch on resume.\n");
121 return -1;
122 }
123 } else if (is_resume < 0)
124 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
125
126 printk(BIOS_DEBUG, "Saving vboot hash.\n");
127
128 /* Always save the hash for the current boot. */
129 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
130 printk(BIOS_ERR, "Error saving vboot hash.\n");
131 /* Though this is an error don't report it up since it could
132 * lead to a reboot loop. The consequence of this is that
133 * we will most likely fail resuming because of EC issues or
134 * the hash digest not matching. */
135 return 0;
136 }
137
138 return 0;
139}
140
Joel Kitching220ac042019-07-31 14:19:00 +0800141static vb2_error_t hash_body(struct vb2_context *ctx,
Julius Wernerf8e17642019-12-12 13:23:06 -0800142 struct region_device *fw_body)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500143{
144 uint64_t load_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800145 uint32_t remaining;
Martin Roth8839b7f2020-10-28 11:38:57 -0600146 uint8_t block[CONFIG_VBOOT_HASH_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600147 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
148 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500149 size_t block_size = sizeof(block);
150 size_t offset;
Joel Kitching220ac042019-07-31 14:19:00 +0800151 vb2_error_t rv;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500152
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600153 /* Clear the full digest so that any hash digests less than the
154 * max have trailing zeros. */
155 memset(hash_digest, 0, hash_digest_sz);
156
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500157 /*
158 * Since loading the firmware and calculating its hash is intertwined,
159 * we use this little trick to measure them separately and pretend it
160 * was first loaded and then hashed in one piece with the timestamps.
161 * (This split won't make sense with memory-mapped media like on x86.)
162 */
163 load_ts = timestamp_get();
164 timestamp_add(TS_START_HASH_BODY, load_ts);
165
Julius Wernerf8e17642019-12-12 13:23:06 -0800166 remaining = region_device_sz(fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500167 offset = 0;
168
169 /* Start the body hash */
Julius Wernerf8e17642019-12-12 13:23:06 -0800170 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500171 if (rv)
172 return rv;
173
174 /* Extend over the body */
Julius Wernerf8e17642019-12-12 13:23:06 -0800175 while (remaining) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500176 uint64_t temp_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800177 if (block_size > remaining)
178 block_size = remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500179
180 temp_ts = timestamp_get();
Julius Wernerf8e17642019-12-12 13:23:06 -0800181 if (rdev_readat(fw_body, block, offset, block_size) < 0)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500182 return VB2_ERROR_UNKNOWN;
183 load_ts += timestamp_get() - temp_ts;
184
185 rv = vb2api_extend_hash(ctx, block, block_size);
186 if (rv)
187 return rv;
188
Julius Wernerf8e17642019-12-12 13:23:06 -0800189 remaining -= block_size;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500190 offset += block_size;
191 }
192
193 timestamp_add(TS_DONE_LOADING, load_ts);
194 timestamp_add_now(TS_DONE_HASHING);
195
196 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600197 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500198 if (rv)
199 return rv;
200
201 timestamp_add_now(TS_END_HASH_BODY);
202
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600203 if (handle_digest_result(hash_digest, hash_digest_sz))
204 return VB2_ERROR_UNKNOWN;
205
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500206 return VB2_SUCCESS;
207}
208
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500209static uint32_t extend_pcrs(struct vb2_context *ctx)
210{
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100211 return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100212 vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500213}
214
dnojiridff56a02020-04-03 10:56:43 -0700215#define EC_EFS_BOOT_MODE_NORMAL 0x00
216#define EC_EFS_BOOT_MODE_NO_BOOT 0x01
217
218static const char *get_boot_mode_string(uint8_t boot_mode)
219{
220 if (boot_mode == EC_EFS_BOOT_MODE_NORMAL)
221 return "NORMAL";
222 else if (boot_mode == EC_EFS_BOOT_MODE_NO_BOOT)
223 return "NO_BOOT";
224 else
225 return "UNDEFINED";
226}
227
228static void check_boot_mode(struct vb2_context *ctx)
229{
230 uint8_t boot_mode;
231 int rv;
232
233 rv = tlcl_cr50_get_boot_mode(&boot_mode);
234 switch (rv) {
235 case TPM_E_NO_SUCH_COMMAND:
236 printk(BIOS_WARNING, "Cr50 does not support GET_BOOT_MODE.\n");
237 /* Proceed to legacy boot model. */
238 return;
239 case TPM_SUCCESS:
240 break;
241 default:
242 printk(BIOS_ERR,
243 "Communication error in getting Cr50 boot mode.\n");
244 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE)
245 /* Continue to boot in recovery mode */
246 return;
247 vb2api_fail(ctx, VB2_RECOVERY_CR50_BOOT_MODE, rv);
248 vboot_save_data(ctx);
249 vboot_reboot();
250 return;
251 }
252
253 printk(BIOS_INFO, "Cr50 says boot_mode is %s(0x%02x).\n",
254 get_boot_mode_string(boot_mode), boot_mode);
255
256 if (boot_mode == EC_EFS_BOOT_MODE_NO_BOOT)
257 ctx->flags |= VB2_CONTEXT_NO_BOOT;
258}
259
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500260/**
261 * Verify and select the firmware in the RW image
262 *
263 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
264 * when per-stage verification is ready.
265 */
266void verstage_main(void)
267{
Joel Kitching2332c742019-10-23 15:01:37 +0800268 struct vb2_context *ctx;
Julius Wernerf8e17642019-12-12 13:23:06 -0800269 struct region_device fw_body;
Joel Kitching220ac042019-07-31 14:19:00 +0800270 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500271
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500272 timestamp_add_now(TS_START_VBOOT);
273
Patrick Rudolph6093c502019-05-08 18:36:39 +0200274 /* Lockdown SPI flash controller if required */
275 if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE))
276 boot_device_security_lockdown();
277
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500278 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800279 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500280
Aaron Durbin118a84f2017-09-15 11:15:07 -0600281 /* Initialize and read nvdata from non-volatile storage. */
Joel Kitching2332c742019-10-23 15:01:37 +0800282 vbnv_init(ctx->nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500283
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800284 /* Set S3 resume flag if vboot should behave differently when selecting
285 * which slot to boot. This is only relevant to vboot if the platform
286 * does verification of memory init and thus must ensure it resumes with
287 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800288 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Bill XIE516c0a52020-02-24 23:08:35 +0800289 platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800290 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800291
Duncan Lauriea613a312016-03-14 09:32:08 -0700292 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
293 * check the return value here because vb2api_fw_phase1 will catch
294 * invalid secdata and tell us what to do (=reboot). */
295 timestamp_add_now(TS_START_TPMINIT);
dnojiridff56a02020-04-03 10:56:43 -0700296 if (vboot_setup_tpm(ctx) == TPM_SUCCESS) {
Joel Kitching2332c742019-10-23 15:01:37 +0800297 antirollback_read_space_firmware(ctx);
dnojiridff56a02020-04-03 10:56:43 -0700298 antirollback_read_space_kernel(ctx);
299 }
Duncan Lauriea613a312016-03-14 09:32:08 -0700300 timestamp_add_now(TS_END_TPMINIT);
301
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500302 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800303 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800304 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800305 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500306 }
307
Julius Wernercd49cce2019-03-05 16:53:33 -0800308 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100309 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800310 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500311
Julius Wernercd49cce2019-03-05 16:53:33 -0800312 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800313 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500314
Joel Kitching5923d672019-04-12 15:57:43 +0800315 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100316 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800317 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800318
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500319 /* Do early init (set up secdata and NVRAM, load GBB) */
320 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800321 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500322
323 if (rv) {
324 /*
325 * If vb2api_fw_phase1 fails, check for return value.
326 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
327 * into recovery mode.
328 * For any other error code, save context if needed and reboot.
329 */
330 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
331 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600332 vboot_save_data(ctx);
Joel Kitching2332c742019-10-23 15:01:37 +0800333 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800334 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500335 }
336
Raul E Rangel3a591742018-07-17 14:44:43 -0600337 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600338 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500339 vboot_reboot();
340 }
341
342 /* Determine which firmware slot to boot (based on NVRAM) */
343 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800344 rv = vb2api_fw_phase2(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500345 if (rv) {
346 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600347 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500348 vboot_reboot();
349 }
350
351 /* Try that slot (verify its keyblock and preamble) */
352 printk(BIOS_INFO, "Phase 3\n");
353 timestamp_add_now(TS_START_VERIFY_SLOT);
Joel Kitching2332c742019-10-23 15:01:37 +0800354 rv = vb2api_fw_phase3(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500355 timestamp_add_now(TS_END_VERIFY_SLOT);
356 if (rv) {
357 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600358 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500359 vboot_reboot();
360 }
361
362 printk(BIOS_INFO, "Phase 4\n");
Julius Wernerf8e17642019-12-12 13:23:06 -0800363 rv = vboot_locate_firmware(ctx, &fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500364 if (rv)
Keith Short70064582019-05-06 16:12:57 -0600365 die_with_post_code(POST_INVALID_ROM,
366 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500367
Julius Wernerf8e17642019-12-12 13:23:06 -0800368 rv = hash_body(ctx, &fw_body);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600369 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500370 if (rv) {
371 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
372 vboot_reboot();
373 }
374
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800375 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800376 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800377 timestamp_add_now(TS_START_TPMPCR);
Joel Kitching2332c742019-10-23 15:01:37 +0800378 rv = extend_pcrs(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800379 if (rv) {
380 printk(BIOS_WARNING,
381 "Failed to extend TPM PCRs (%#x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800382 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600383 vboot_save_data(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800384 vboot_reboot();
385 }
386 timestamp_add_now(TS_END_TPMPCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500387 }
388
dnojiridff56a02020-04-03 10:56:43 -0700389 if (CONFIG(TPM_CR50))
390 check_boot_mode(ctx);
391
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500392 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600393
394 timestamp_add_now(TS_START_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500395 rv = antirollback_lock_space_firmware();
396 if (rv) {
397 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800398 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600399 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500400 vboot_reboot();
401 }
Raul E Rangel4c518e12018-05-11 11:08:07 -0600402 timestamp_add_now(TS_END_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500403
Furquan Shaikhb038f412016-11-07 23:47:11 -0800404 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800405 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Shelley Chena79803c2020-10-16 13:15:59 -0700406 rv = antirollback_lock_space_mrc_hash(MRC_REC_HASH_NV_INDEX);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800407 if (rv) {
408 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
409 rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800410 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
Furquan Shaikhb038f412016-11-07 23:47:11 -0800411 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600412 vboot_save_data(ctx);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800413 vboot_reboot();
414 }
415 }
416
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800417 printk(BIOS_INFO, "Slot %c is selected\n",
418 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800419
420 verstage_main_exit:
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500421 timestamp_add_now(TS_END_VBOOT);
422}