blob: 6ee4d948f3d925a65566ebb253a98fb275d574d9 [file] [log] [blame]
Aaron Durbin588ad7b2015-09-29 17:56:59 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin588ad7b2015-09-29 17:56:59 -050014 */
15
Aaron Durbin588ad7b2015-09-29 17:56:59 -050016#include <arch/exception.h>
17#include <assert.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070018#include <bootmode.h>
Joel Kitching532e0c72019-06-16 17:23:03 +080019#include <cbmem.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050020#include <console/console.h>
21#include <console/vtxprintf.h>
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +080022#include <fmap.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050023#include <string.h>
24#include <timestamp.h>
25#include <vb2_api.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020026#include <security/vboot/misc.h>
27#include <security/vboot/vbnv.h>
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +010028#include <security/vboot/vboot_crtm.h>
Christian Walter0bd84ed2019-07-23 10:26:30 +020029#include <security/vboot/tpm_common.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050030
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010031#include "antirollback.h"
32
Aaron Durbin87c9fae2016-01-22 15:26:04 -060033/* The max hash size to expect is for SHA512. */
34#define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE
35
Aaron Durbin588ad7b2015-09-29 17:56:59 -050036#define TODO_BLOCK_SIZE 1024
37
Aaron Durbin588ad7b2015-09-29 17:56:59 -050038/* exports */
39
40void vb2ex_printf(const char *func, const char *fmt, ...)
41{
42 va_list args;
43
Randall Spanglere80c6f62017-01-20 14:48:53 -080044 if (func)
45 printk(BIOS_INFO, "VB2:%s() ", func);
46
Aaron Durbin588ad7b2015-09-29 17:56:59 -050047 va_start(args, fmt);
Kyösti Mälkki7132f252019-02-14 23:08:29 +020048 vprintk(BIOS_INFO, fmt, args);
Aaron Durbin588ad7b2015-09-29 17:56:59 -050049 va_end(args);
50
51 return;
52}
53
Joel Kitching220ac042019-07-31 14:19:00 +080054vb2_error_t vb2ex_read_resource(struct vb2_context *ctx,
55 enum vb2_resource_index index,
56 uint32_t offset,
57 void *buf,
58 uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050059{
60 struct region_device rdev;
61 const char *name;
62
63 switch (index) {
64 case VB2_RES_GBB:
65 name = "GBB";
66 break;
67 case VB2_RES_FW_VBLOCK:
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080068 if (vboot_is_firmware_slot_a(ctx))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050069 name = "VBLOCK_A";
70 else
71 name = "VBLOCK_B";
72 break;
73 default:
74 return VB2_ERROR_EX_READ_RESOURCE_INDEX;
75 }
76
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +080077 if (fmap_locate_area_as_rdev(name, &rdev))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050078 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
79
80 if (rdev_readat(&rdev, buf, offset, size) != size)
81 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
82
83 return VB2_SUCCESS;
84}
85
Joel Kitchingf3507682019-10-15 01:04:35 +080086void vb2ex_abort(void)
87{
88 die("vboot has aborted execution; exit\n");
89}
90
Aaron Durbin588ad7b2015-09-29 17:56:59 -050091/* No-op stubs that can be overridden by SoCs with hardware crypto support. */
Joel Kitching220ac042019-07-31 14:19:00 +080092__weak vb2_error_t vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg,
93 uint32_t data_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050094{
95 return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED;
96}
97
Joel Kitching220ac042019-07-31 14:19:00 +080098__weak vb2_error_t vb2ex_hwcrypto_digest_extend(const uint8_t *buf,
99 uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500100{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100101 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500102 return VB2_ERROR_UNKNOWN;
103}
104
Joel Kitching220ac042019-07-31 14:19:00 +0800105__weak vb2_error_t vb2ex_hwcrypto_digest_finalize(uint8_t *digest,
106 uint32_t digest_size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500107{
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100108 BUG(); /* Should never get called if init() returned an error. */
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500109 return VB2_ERROR_UNKNOWN;
110}
111
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600112static int handle_digest_result(void *slot_hash, size_t slot_hash_sz)
113{
114 int is_resume;
115
116 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +0530117 * Chrome EC is the only support for vboot_save_hash() &
118 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +0530119 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800120 if (!CONFIG(EC_GOOGLE_CHROMEEC))
Naresh G Solanki3d44d692016-11-06 12:51:14 +0530121 return 0;
122
123 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600124 * Nothing to do since resuming on the platform doesn't require
125 * vboot verification again.
126 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800127 if (!CONFIG(RESUME_PATH_SAME_AS_BOOT))
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600128 return 0;
129
130 /*
131 * Assume that if vboot doesn't start in bootblock verified
132 * RW memory init code is not employed. i.e. memory init code
133 * lives in RO CBFS.
134 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800135 if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600136 return 0;
137
138 is_resume = vboot_platform_is_resuming();
139
140 if (is_resume > 0) {
141 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
142 const size_t saved_hash_sz = sizeof(saved_hash);
143
144 assert(slot_hash_sz == saved_hash_sz);
145
146 printk(BIOS_DEBUG, "Platform is resuming.\n");
147
148 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
149 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
150 return -1;
151 }
152
153 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
154 printk(BIOS_ERR, "Hash mismatch on resume.\n");
155 return -1;
156 }
157 } else if (is_resume < 0)
158 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
159
160 printk(BIOS_DEBUG, "Saving vboot hash.\n");
161
162 /* Always save the hash for the current boot. */
163 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
164 printk(BIOS_ERR, "Error saving vboot hash.\n");
165 /* Though this is an error don't report it up since it could
166 * lead to a reboot loop. The consequence of this is that
167 * we will most likely fail resuming because of EC issues or
168 * the hash digest not matching. */
169 return 0;
170 }
171
172 return 0;
173}
174
Joel Kitching220ac042019-07-31 14:19:00 +0800175static vb2_error_t hash_body(struct vb2_context *ctx,
176 struct region_device *fw_main)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500177{
178 uint64_t load_ts;
179 uint32_t expected_size;
180 uint8_t block[TODO_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600181 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
182 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500183 size_t block_size = sizeof(block);
184 size_t offset;
Joel Kitching220ac042019-07-31 14:19:00 +0800185 vb2_error_t rv;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500186
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600187 /* Clear the full digest so that any hash digests less than the
188 * max have trailing zeros. */
189 memset(hash_digest, 0, hash_digest_sz);
190
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500191 /*
192 * Since loading the firmware and calculating its hash is intertwined,
193 * we use this little trick to measure them separately and pretend it
194 * was first loaded and then hashed in one piece with the timestamps.
195 * (This split won't make sense with memory-mapped media like on x86.)
196 */
197 load_ts = timestamp_get();
198 timestamp_add(TS_START_HASH_BODY, load_ts);
199
200 expected_size = region_device_sz(fw_main);
201 offset = 0;
202
203 /* Start the body hash */
204 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY, &expected_size);
205 if (rv)
206 return rv;
207
Aaron Durbin4121f9e2016-01-27 14:23:17 -0600208 /*
209 * Honor vboot's RW slot size. The expected size is pulled out of
210 * the preamble and obtained through vb2api_init_hash() above. By
211 * creating sub region the RW slot portion of the boot media is
212 * limited.
213 */
214 if (rdev_chain(fw_main, fw_main, 0, expected_size)) {
215 printk(BIOS_ERR, "Unable to restrict CBFS size.\n");
216 return VB2_ERROR_UNKNOWN;
217 }
218
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500219 /* Extend over the body */
220 while (expected_size) {
221 uint64_t temp_ts;
222 if (block_size > expected_size)
223 block_size = expected_size;
224
225 temp_ts = timestamp_get();
226 if (rdev_readat(fw_main, block, offset, block_size) < 0)
227 return VB2_ERROR_UNKNOWN;
228 load_ts += timestamp_get() - temp_ts;
229
230 rv = vb2api_extend_hash(ctx, block, block_size);
231 if (rv)
232 return rv;
233
234 expected_size -= block_size;
235 offset += block_size;
236 }
237
238 timestamp_add(TS_DONE_LOADING, load_ts);
239 timestamp_add_now(TS_DONE_HASHING);
240
241 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600242 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500243 if (rv)
244 return rv;
245
246 timestamp_add_now(TS_END_HASH_BODY);
247
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600248 if (handle_digest_result(hash_digest, hash_digest_sz))
249 return VB2_ERROR_UNKNOWN;
250
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500251 return VB2_SUCCESS;
252}
253
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500254/**
255 * Save non-volatile and/or secure data if needed.
256 */
257static void save_if_needed(struct vb2_context *ctx)
258{
259 if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) {
260 printk(BIOS_INFO, "Saving nvdata\n");
261 save_vbnv(ctx->nvdata);
262 ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED;
263 }
264 if (ctx->flags & VB2_CONTEXT_SECDATA_CHANGED) {
265 printk(BIOS_INFO, "Saving secdata\n");
266 antirollback_write_space_firmware(ctx);
267 ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED;
268 }
269}
270
271static uint32_t extend_pcrs(struct vb2_context *ctx)
272{
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100273 return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100274 vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500275}
276
Joel Kitching532e0c72019-06-16 17:23:03 +0800277static void vboot_log_and_clear_recovery_mode_switch(int unused)
278{
279 /* Log the recovery mode switches if required, before clearing them. */
280 log_recovery_mode_switch();
281
282 /*
283 * The recovery mode switch is cleared (typically backed by EC) here
284 * to allow multiple queries to get_recovery_mode_switch() and have
285 * them return consistent results during the verified boot path as well
286 * as dram initialization. x86 systems ignore the saved dram settings
287 * in the recovery path in order to start from a clean slate. Therefore
288 * clear the state here since this function is called when memory
289 * is known to be up.
290 */
291 clear_recovery_mode_switch();
292}
293#if !CONFIG(VBOOT_STARTS_IN_ROMSTAGE)
294ROMSTAGE_CBMEM_INIT_HOOK(vboot_log_and_clear_recovery_mode_switch)
295#endif
296
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500297/**
298 * Verify and select the firmware in the RW image
299 *
300 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
301 * when per-stage verification is ready.
302 */
303void verstage_main(void)
304{
Joel Kitching2332c742019-10-23 15:01:37 +0800305 struct vb2_context *ctx;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500306 struct region_device fw_main;
Joel Kitching220ac042019-07-31 14:19:00 +0800307 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500308
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500309 timestamp_add_now(TS_START_VBOOT);
310
311 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800312 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500313
Aaron Durbin118a84f2017-09-15 11:15:07 -0600314 /* Initialize and read nvdata from non-volatile storage. */
Joel Kitching2332c742019-10-23 15:01:37 +0800315 vbnv_init(ctx->nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500316
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800317 /* Set S3 resume flag if vboot should behave differently when selecting
318 * which slot to boot. This is only relevant to vboot if the platform
319 * does verification of memory init and thus must ensure it resumes with
320 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800321 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100322 vboot_platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800323 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800324
Duncan Lauriea613a312016-03-14 09:32:08 -0700325 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
326 * check the return value here because vb2api_fw_phase1 will catch
327 * invalid secdata and tell us what to do (=reboot). */
328 timestamp_add_now(TS_START_TPMINIT);
Joel Kitching2332c742019-10-23 15:01:37 +0800329 if (vboot_setup_tpm(ctx) == TPM_SUCCESS)
330 antirollback_read_space_firmware(ctx);
Duncan Lauriea613a312016-03-14 09:32:08 -0700331 timestamp_add_now(TS_END_TPMINIT);
332
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100333 /* Enable measured boot mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800334 if (CONFIG(VBOOT_MEASURED_BOOT) &&
Joel Kitching2332c742019-10-23 15:01:37 +0800335 !(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100336 if (vboot_init_crtm() != VB2_SUCCESS)
Keith Short70064582019-05-06 16:12:57 -0600337 die_with_post_code(POST_INVALID_ROM,
338 "Initializing measured boot mode failed!");
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100339 }
340
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500341 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800342 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800343 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800344 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500345 }
346
Julius Wernercd49cce2019-03-05 16:53:33 -0800347 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100348 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800349 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500350
Julius Wernercd49cce2019-03-05 16:53:33 -0800351 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800352 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500353
Joel Kitching5923d672019-04-12 15:57:43 +0800354 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100355 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800356 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800357
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500358 /* Do early init (set up secdata and NVRAM, load GBB) */
359 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800360 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500361
362 if (rv) {
363 /*
364 * If vb2api_fw_phase1 fails, check for return value.
365 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
366 * into recovery mode.
367 * For any other error code, save context if needed and reboot.
368 */
369 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
370 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800371 save_if_needed(ctx);
372 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800373 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500374 }
375
Raul E Rangel3a591742018-07-17 14:44:43 -0600376 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800377 save_if_needed(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500378 vboot_reboot();
379 }
380
381 /* Determine which firmware slot to boot (based on NVRAM) */
382 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800383 rv = vb2api_fw_phase2(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500384 if (rv) {
385 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800386 save_if_needed(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500387 vboot_reboot();
388 }
389
390 /* Try that slot (verify its keyblock and preamble) */
391 printk(BIOS_INFO, "Phase 3\n");
392 timestamp_add_now(TS_START_VERIFY_SLOT);
Joel Kitching2332c742019-10-23 15:01:37 +0800393 rv = vb2api_fw_phase3(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500394 timestamp_add_now(TS_END_VERIFY_SLOT);
395 if (rv) {
396 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800397 save_if_needed(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500398 vboot_reboot();
399 }
400
401 printk(BIOS_INFO, "Phase 4\n");
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800402 rv = vboot_locate_firmware(ctx, &fw_main);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500403 if (rv)
Keith Short70064582019-05-06 16:12:57 -0600404 die_with_post_code(POST_INVALID_ROM,
405 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500406
Joel Kitching2332c742019-10-23 15:01:37 +0800407 rv = hash_body(ctx, &fw_main);
408 save_if_needed(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500409 if (rv) {
410 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
411 vboot_reboot();
412 }
413
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800414 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800415 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800416 timestamp_add_now(TS_START_TPMPCR);
Joel Kitching2332c742019-10-23 15:01:37 +0800417 rv = extend_pcrs(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800418 if (rv) {
419 printk(BIOS_WARNING,
420 "Failed to extend TPM PCRs (%#x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800421 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
422 save_if_needed(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800423 vboot_reboot();
424 }
425 timestamp_add_now(TS_END_TPMPCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500426 }
427
428 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600429
430 timestamp_add_now(TS_START_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500431 rv = antirollback_lock_space_firmware();
432 if (rv) {
433 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800434 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
435 save_if_needed(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500436 vboot_reboot();
437 }
Raul E Rangel4c518e12018-05-11 11:08:07 -0600438 timestamp_add_now(TS_END_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500439
Furquan Shaikhb038f412016-11-07 23:47:11 -0800440 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800441 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Furquan Shaikhb038f412016-11-07 23:47:11 -0800442 rv = antirollback_lock_space_rec_hash();
443 if (rv) {
444 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
445 rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800446 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
Furquan Shaikhb038f412016-11-07 23:47:11 -0800447 0);
Joel Kitching2332c742019-10-23 15:01:37 +0800448 save_if_needed(ctx);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800449 vboot_reboot();
450 }
451 }
452
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800453 printk(BIOS_INFO, "Slot %c is selected\n",
454 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800455
456 verstage_main_exit:
Joel Kitching532e0c72019-06-16 17:23:03 +0800457 /* If CBMEM is not up yet, let the ROMSTAGE_CBMEM_INIT_HOOK take care
458 of running this function. */
459 if (ENV_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
460 vboot_log_and_clear_recovery_mode_switch(0);
461
Joel Kitching7b10deb2019-06-17 15:22:28 +0800462 /* Save recovery reason in case of unexpected reboots on x86. */
463 vboot_save_recovery_reason_vbnv();
464
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500465 timestamp_add_now(TS_END_VBOOT);
466}