blob: dbaa883080122c6eb2deebf32f39fbaea1984e78 [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>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -07005#include <bootmode.h>
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +08006#include <fmap.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08007#include <security/tpm/tspi/crtm.h>
dnojiridff56a02020-04-03 10:56:43 -07008#include <security/tpm/tss/vendor/cr50/cr50.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08009#include <security/vboot/misc.h>
10#include <security/vboot/vbnv.h>
11#include <security/vboot/tpm_common.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050012#include <string.h>
13#include <timestamp.h>
14#include <vb2_api.h>
Patrick Rudolph6093c502019-05-08 18:36:39 +020015#include <boot_device.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050016
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010017#include "antirollback.h"
18
Aaron Durbin87c9fae2016-01-22 15:26:04 -060019/* The max hash size to expect is for SHA512. */
20#define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE
21
Aaron Durbin588ad7b2015-09-29 17:56:59 -050022#define TODO_BLOCK_SIZE 1024
23
Aaron Durbin588ad7b2015-09-29 17:56:59 -050024/* exports */
25
Joel Kitching220ac042019-07-31 14:19:00 +080026vb2_error_t vb2ex_read_resource(struct vb2_context *ctx,
27 enum vb2_resource_index index,
28 uint32_t offset,
29 void *buf,
30 uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050031{
32 struct region_device rdev;
33 const char *name;
34
35 switch (index) {
36 case VB2_RES_GBB:
37 name = "GBB";
38 break;
39 case VB2_RES_FW_VBLOCK:
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080040 if (vboot_is_firmware_slot_a(ctx))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050041 name = "VBLOCK_A";
42 else
43 name = "VBLOCK_B";
44 break;
45 default:
46 return VB2_ERROR_EX_READ_RESOURCE_INDEX;
47 }
48
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +080049 if (fmap_locate_area_as_rdev(name, &rdev))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050050 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
51
52 if (rdev_readat(&rdev, buf, offset, size) != size)
53 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
54
55 return VB2_SUCCESS;
56}
57
58/* No-op stubs that can be overridden by SoCs with hardware crypto support. */
Joel Kitching220ac042019-07-31 14:19:00 +080059__weak vb2_error_t vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg,
60 uint32_t data_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050061{
62 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
63}
64
Joel Kitching220ac042019-07-31 14:19:00 +080065__weak vb2_error_t vb2ex_hwcrypto_digest_extend(const uint8_t *buf,
66 uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050067{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010068 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -050069 return VB2_ERROR_UNKNOWN;
70}
71
Joel Kitching220ac042019-07-31 14:19:00 +080072__weak vb2_error_t vb2ex_hwcrypto_digest_finalize(uint8_t *digest,
73 uint32_t digest_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050074{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010075 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -050076 return VB2_ERROR_UNKNOWN;
77}
78
Aaron Durbin87c9fae2016-01-22 15:26:04 -060079static int handle_digest_result(void *slot_hash, size_t slot_hash_sz)
80{
81 int is_resume;
82
83 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +053084 * Chrome EC is the only support for vboot_save_hash() &
85 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +053086 */
Julius Wernercd49cce2019-03-05 16:53:33 -080087 if (!CONFIG(EC_GOOGLE_CHROMEEC))
Naresh G Solanki3d44d692016-11-06 12:51:14 +053088 return 0;
89
90 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -060091 * Nothing to do since resuming on the platform doesn't require
92 * vboot verification again.
93 */
Julius Wernercd49cce2019-03-05 16:53:33 -080094 if (!CONFIG(RESUME_PATH_SAME_AS_BOOT))
Aaron Durbin87c9fae2016-01-22 15:26:04 -060095 return 0;
96
97 /*
98 * Assume that if vboot doesn't start in bootblock verified
99 * RW memory init code is not employed. i.e. memory init code
100 * lives in RO CBFS.
101 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800102 if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600103 return 0;
104
Bill XIE516c0a52020-02-24 23:08:35 +0800105 is_resume = platform_is_resuming();
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600106
107 if (is_resume > 0) {
108 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
109 const size_t saved_hash_sz = sizeof(saved_hash);
110
111 assert(slot_hash_sz == saved_hash_sz);
112
113 printk(BIOS_DEBUG, "Platform is resuming.\n");
114
115 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
116 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
117 return -1;
118 }
119
120 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
121 printk(BIOS_ERR, "Hash mismatch on resume.\n");
122 return -1;
123 }
124 } else if (is_resume < 0)
125 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
126
127 printk(BIOS_DEBUG, "Saving vboot hash.\n");
128
129 /* Always save the hash for the current boot. */
130 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
131 printk(BIOS_ERR, "Error saving vboot hash.\n");
132 /* Though this is an error don't report it up since it could
133 * lead to a reboot loop. The consequence of this is that
134 * we will most likely fail resuming because of EC issues or
135 * the hash digest not matching. */
136 return 0;
137 }
138
139 return 0;
140}
141
Joel Kitching220ac042019-07-31 14:19:00 +0800142static vb2_error_t hash_body(struct vb2_context *ctx,
Julius Wernerf8e17642019-12-12 13:23:06 -0800143 struct region_device *fw_body)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500144{
145 uint64_t load_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800146 uint32_t remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500147 uint8_t block[TODO_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600148 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
149 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500150 size_t block_size = sizeof(block);
151 size_t offset;
Joel Kitching220ac042019-07-31 14:19:00 +0800152 vb2_error_t rv;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500153
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600154 /* Clear the full digest so that any hash digests less than the
155 * max have trailing zeros. */
156 memset(hash_digest, 0, hash_digest_sz);
157
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500158 /*
159 * Since loading the firmware and calculating its hash is intertwined,
160 * we use this little trick to measure them separately and pretend it
161 * was first loaded and then hashed in one piece with the timestamps.
162 * (This split won't make sense with memory-mapped media like on x86.)
163 */
164 load_ts = timestamp_get();
165 timestamp_add(TS_START_HASH_BODY, load_ts);
166
Julius Wernerf8e17642019-12-12 13:23:06 -0800167 remaining = region_device_sz(fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500168 offset = 0;
169
170 /* Start the body hash */
Julius Wernerf8e17642019-12-12 13:23:06 -0800171 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500172 if (rv)
173 return rv;
174
175 /* Extend over the body */
Julius Wernerf8e17642019-12-12 13:23:06 -0800176 while (remaining) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500177 uint64_t temp_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800178 if (block_size > remaining)
179 block_size = remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500180
181 temp_ts = timestamp_get();
Julius Wernerf8e17642019-12-12 13:23:06 -0800182 if (rdev_readat(fw_body, block, offset, block_size) < 0)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500183 return VB2_ERROR_UNKNOWN;
184 load_ts += timestamp_get() - temp_ts;
185
186 rv = vb2api_extend_hash(ctx, block, block_size);
187 if (rv)
188 return rv;
189
Julius Wernerf8e17642019-12-12 13:23:06 -0800190 remaining -= block_size;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500191 offset += block_size;
192 }
193
194 timestamp_add(TS_DONE_LOADING, load_ts);
195 timestamp_add_now(TS_DONE_HASHING);
196
197 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600198 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500199 if (rv)
200 return rv;
201
202 timestamp_add_now(TS_END_HASH_BODY);
203
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600204 if (handle_digest_result(hash_digest, hash_digest_sz))
205 return VB2_ERROR_UNKNOWN;
206
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500207 return VB2_SUCCESS;
208}
209
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500210static uint32_t extend_pcrs(struct vb2_context *ctx)
211{
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100212 return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100213 vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500214}
215
dnojiridff56a02020-04-03 10:56:43 -0700216#define EC_EFS_BOOT_MODE_NORMAL 0x00
217#define EC_EFS_BOOT_MODE_NO_BOOT 0x01
218
219static const char *get_boot_mode_string(uint8_t boot_mode)
220{
221 if (boot_mode == EC_EFS_BOOT_MODE_NORMAL)
222 return "NORMAL";
223 else if (boot_mode == EC_EFS_BOOT_MODE_NO_BOOT)
224 return "NO_BOOT";
225 else
226 return "UNDEFINED";
227}
228
229static void check_boot_mode(struct vb2_context *ctx)
230{
231 uint8_t boot_mode;
232 int rv;
233
234 rv = tlcl_cr50_get_boot_mode(&boot_mode);
235 switch (rv) {
236 case TPM_E_NO_SUCH_COMMAND:
237 printk(BIOS_WARNING, "Cr50 does not support GET_BOOT_MODE.\n");
238 /* Proceed to legacy boot model. */
239 return;
240 case TPM_SUCCESS:
241 break;
242 default:
243 printk(BIOS_ERR,
244 "Communication error in getting Cr50 boot mode.\n");
245 if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE)
246 /* Continue to boot in recovery mode */
247 return;
248 vb2api_fail(ctx, VB2_RECOVERY_CR50_BOOT_MODE, rv);
249 vboot_save_data(ctx);
250 vboot_reboot();
251 return;
252 }
253
254 printk(BIOS_INFO, "Cr50 says boot_mode is %s(0x%02x).\n",
255 get_boot_mode_string(boot_mode), boot_mode);
256
257 if (boot_mode == EC_EFS_BOOT_MODE_NO_BOOT)
258 ctx->flags |= VB2_CONTEXT_NO_BOOT;
259}
260
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500261/**
262 * Verify and select the firmware in the RW image
263 *
264 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
265 * when per-stage verification is ready.
266 */
267void verstage_main(void)
268{
Joel Kitching2332c742019-10-23 15:01:37 +0800269 struct vb2_context *ctx;
Julius Wernerf8e17642019-12-12 13:23:06 -0800270 struct region_device fw_body;
Joel Kitching220ac042019-07-31 14:19:00 +0800271 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500272
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500273 timestamp_add_now(TS_START_VBOOT);
274
Patrick Rudolph6093c502019-05-08 18:36:39 +0200275 /* Lockdown SPI flash controller if required */
276 if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE))
277 boot_device_security_lockdown();
278
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500279 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800280 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500281
Aaron Durbin118a84f2017-09-15 11:15:07 -0600282 /* Initialize and read nvdata from non-volatile storage. */
Joel Kitching2332c742019-10-23 15:01:37 +0800283 vbnv_init(ctx->nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500284
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800285 /* Set S3 resume flag if vboot should behave differently when selecting
286 * which slot to boot. This is only relevant to vboot if the platform
287 * does verification of memory init and thus must ensure it resumes with
288 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800289 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Bill XIE516c0a52020-02-24 23:08:35 +0800290 platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800291 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800292
Duncan Lauriea613a312016-03-14 09:32:08 -0700293 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
294 * check the return value here because vb2api_fw_phase1 will catch
295 * invalid secdata and tell us what to do (=reboot). */
296 timestamp_add_now(TS_START_TPMINIT);
dnojiridff56a02020-04-03 10:56:43 -0700297 if (vboot_setup_tpm(ctx) == TPM_SUCCESS) {
Joel Kitching2332c742019-10-23 15:01:37 +0800298 antirollback_read_space_firmware(ctx);
dnojiridff56a02020-04-03 10:56:43 -0700299 antirollback_read_space_kernel(ctx);
300 }
Duncan Lauriea613a312016-03-14 09:32:08 -0700301 timestamp_add_now(TS_END_TPMINIT);
302
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500303 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800304 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800305 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800306 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500307 }
308
Julius Wernercd49cce2019-03-05 16:53:33 -0800309 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100310 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800311 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500312
Julius Wernercd49cce2019-03-05 16:53:33 -0800313 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800314 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500315
Joel Kitching5923d672019-04-12 15:57:43 +0800316 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100317 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800318 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800319
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500320 /* Do early init (set up secdata and NVRAM, load GBB) */
321 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800322 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500323
324 if (rv) {
325 /*
326 * If vb2api_fw_phase1 fails, check for return value.
327 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
328 * into recovery mode.
329 * For any other error code, save context if needed and reboot.
330 */
331 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
332 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600333 vboot_save_data(ctx);
Joel Kitching2332c742019-10-23 15:01:37 +0800334 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800335 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500336 }
337
Raul E Rangel3a591742018-07-17 14:44:43 -0600338 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600339 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500340 vboot_reboot();
341 }
342
343 /* Determine which firmware slot to boot (based on NVRAM) */
344 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800345 rv = vb2api_fw_phase2(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500346 if (rv) {
347 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600348 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500349 vboot_reboot();
350 }
351
352 /* Try that slot (verify its keyblock and preamble) */
353 printk(BIOS_INFO, "Phase 3\n");
354 timestamp_add_now(TS_START_VERIFY_SLOT);
Joel Kitching2332c742019-10-23 15:01:37 +0800355 rv = vb2api_fw_phase3(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500356 timestamp_add_now(TS_END_VERIFY_SLOT);
357 if (rv) {
358 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600359 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500360 vboot_reboot();
361 }
362
363 printk(BIOS_INFO, "Phase 4\n");
Julius Wernerf8e17642019-12-12 13:23:06 -0800364 rv = vboot_locate_firmware(ctx, &fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500365 if (rv)
Keith Short70064582019-05-06 16:12:57 -0600366 die_with_post_code(POST_INVALID_ROM,
367 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500368
Julius Wernerf8e17642019-12-12 13:23:06 -0800369 rv = hash_body(ctx, &fw_body);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600370 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500371 if (rv) {
372 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
373 vboot_reboot();
374 }
375
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800376 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800377 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800378 timestamp_add_now(TS_START_TPMPCR);
Joel Kitching2332c742019-10-23 15:01:37 +0800379 rv = extend_pcrs(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800380 if (rv) {
381 printk(BIOS_WARNING,
382 "Failed to extend TPM PCRs (%#x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800383 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600384 vboot_save_data(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800385 vboot_reboot();
386 }
387 timestamp_add_now(TS_END_TPMPCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500388 }
389
dnojiridff56a02020-04-03 10:56:43 -0700390 if (CONFIG(TPM_CR50))
391 check_boot_mode(ctx);
392
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500393 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600394
395 timestamp_add_now(TS_START_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500396 rv = antirollback_lock_space_firmware();
397 if (rv) {
398 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800399 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600400 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500401 vboot_reboot();
402 }
Raul E Rangel4c518e12018-05-11 11:08:07 -0600403 timestamp_add_now(TS_END_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500404
Furquan Shaikhb038f412016-11-07 23:47:11 -0800405 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800406 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Shelley Chena79803c2020-10-16 13:15:59 -0700407 rv = antirollback_lock_space_mrc_hash(MRC_REC_HASH_NV_INDEX);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800408 if (rv) {
409 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
410 rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800411 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
Furquan Shaikhb038f412016-11-07 23:47:11 -0800412 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600413 vboot_save_data(ctx);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800414 vboot_reboot();
415 }
416 }
417
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800418 printk(BIOS_INFO, "Slot %c is selected\n",
419 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800420
421 verstage_main_exit:
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500422 timestamp_add_now(TS_END_VBOOT);
423}