blob: 1d17a176576fc68dd096c3f653a225fddbe222bd [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,
Julius Wernerf8e17642019-12-12 13:23:06 -0800176 struct region_device *fw_body)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500177{
178 uint64_t load_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800179 uint32_t remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500180 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
Julius Wernerf8e17642019-12-12 13:23:06 -0800200 remaining = region_device_sz(fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500201 offset = 0;
202
203 /* Start the body hash */
Julius Wernerf8e17642019-12-12 13:23:06 -0800204 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500205 if (rv)
206 return rv;
207
208 /* Extend over the body */
Julius Wernerf8e17642019-12-12 13:23:06 -0800209 while (remaining) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500210 uint64_t temp_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800211 if (block_size > remaining)
212 block_size = remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500213
214 temp_ts = timestamp_get();
Julius Wernerf8e17642019-12-12 13:23:06 -0800215 if (rdev_readat(fw_body, block, offset, block_size) < 0)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500216 return VB2_ERROR_UNKNOWN;
217 load_ts += timestamp_get() - temp_ts;
218
219 rv = vb2api_extend_hash(ctx, block, block_size);
220 if (rv)
221 return rv;
222
Julius Wernerf8e17642019-12-12 13:23:06 -0800223 remaining -= block_size;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500224 offset += block_size;
225 }
226
227 timestamp_add(TS_DONE_LOADING, load_ts);
228 timestamp_add_now(TS_DONE_HASHING);
229
230 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600231 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500232 if (rv)
233 return rv;
234
235 timestamp_add_now(TS_END_HASH_BODY);
236
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600237 if (handle_digest_result(hash_digest, hash_digest_sz))
238 return VB2_ERROR_UNKNOWN;
239
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500240 return VB2_SUCCESS;
241}
242
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600243void vboot_save_nvdata_only(struct vb2_context *ctx)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500244{
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600245 assert(!(ctx->flags & (VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED |
246 VB2_CONTEXT_SECDATA_KERNEL_CHANGED)));
247
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500248 if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) {
249 printk(BIOS_INFO, "Saving nvdata\n");
250 save_vbnv(ctx->nvdata);
251 ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED;
252 }
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600253}
254
255void vboot_save_data(struct vb2_context *ctx)
256{
Julius Werner683657e2019-12-04 12:50:43 -0800257 if (ctx->flags & VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500258 printk(BIOS_INFO, "Saving secdata\n");
259 antirollback_write_space_firmware(ctx);
Julius Werner683657e2019-12-04 12:50:43 -0800260 ctx->flags &= ~VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500261 }
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600262
263 vboot_save_nvdata_only(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500264}
265
266static uint32_t extend_pcrs(struct vb2_context *ctx)
267{
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100268 return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100269 vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500270}
271
Joel Kitching532e0c72019-06-16 17:23:03 +0800272static void vboot_log_and_clear_recovery_mode_switch(int unused)
273{
274 /* Log the recovery mode switches if required, before clearing them. */
275 log_recovery_mode_switch();
276
277 /*
278 * The recovery mode switch is cleared (typically backed by EC) here
279 * to allow multiple queries to get_recovery_mode_switch() and have
280 * them return consistent results during the verified boot path as well
281 * as dram initialization. x86 systems ignore the saved dram settings
282 * in the recovery path in order to start from a clean slate. Therefore
283 * clear the state here since this function is called when memory
284 * is known to be up.
285 */
286 clear_recovery_mode_switch();
287}
288#if !CONFIG(VBOOT_STARTS_IN_ROMSTAGE)
289ROMSTAGE_CBMEM_INIT_HOOK(vboot_log_and_clear_recovery_mode_switch)
290#endif
291
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500292/**
293 * Verify and select the firmware in the RW image
294 *
295 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
296 * when per-stage verification is ready.
297 */
298void verstage_main(void)
299{
Joel Kitching2332c742019-10-23 15:01:37 +0800300 struct vb2_context *ctx;
Julius Wernerf8e17642019-12-12 13:23:06 -0800301 struct region_device fw_body;
Joel Kitching220ac042019-07-31 14:19:00 +0800302 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500303
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500304 timestamp_add_now(TS_START_VBOOT);
305
306 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800307 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500308
Aaron Durbin118a84f2017-09-15 11:15:07 -0600309 /* Initialize and read nvdata from non-volatile storage. */
Joel Kitching2332c742019-10-23 15:01:37 +0800310 vbnv_init(ctx->nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500311
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800312 /* Set S3 resume flag if vboot should behave differently when selecting
313 * which slot to boot. This is only relevant to vboot if the platform
314 * does verification of memory init and thus must ensure it resumes with
315 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800316 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100317 vboot_platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800318 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800319
Duncan Lauriea613a312016-03-14 09:32:08 -0700320 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
321 * check the return value here because vb2api_fw_phase1 will catch
322 * invalid secdata and tell us what to do (=reboot). */
323 timestamp_add_now(TS_START_TPMINIT);
Joel Kitching2332c742019-10-23 15:01:37 +0800324 if (vboot_setup_tpm(ctx) == TPM_SUCCESS)
325 antirollback_read_space_firmware(ctx);
Duncan Lauriea613a312016-03-14 09:32:08 -0700326 timestamp_add_now(TS_END_TPMINIT);
327
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100328 /* Enable measured boot mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800329 if (CONFIG(VBOOT_MEASURED_BOOT) &&
Joel Kitching2332c742019-10-23 15:01:37 +0800330 !(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100331 if (vboot_init_crtm() != VB2_SUCCESS)
Keith Short70064582019-05-06 16:12:57 -0600332 die_with_post_code(POST_INVALID_ROM,
333 "Initializing measured boot mode failed!");
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100334 }
335
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500336 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800337 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800338 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800339 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500340 }
341
Julius Wernercd49cce2019-03-05 16:53:33 -0800342 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100343 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800344 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500345
Julius Wernercd49cce2019-03-05 16:53:33 -0800346 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800347 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500348
Joel Kitching5923d672019-04-12 15:57:43 +0800349 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100350 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800351 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800352
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500353 /* Do early init (set up secdata and NVRAM, load GBB) */
354 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800355 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500356
357 if (rv) {
358 /*
359 * If vb2api_fw_phase1 fails, check for return value.
360 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
361 * into recovery mode.
362 * For any other error code, save context if needed and reboot.
363 */
364 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
365 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600366 vboot_save_data(ctx);
Joel Kitching2332c742019-10-23 15:01:37 +0800367 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800368 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500369 }
370
Raul E Rangel3a591742018-07-17 14:44:43 -0600371 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600372 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500373 vboot_reboot();
374 }
375
376 /* Determine which firmware slot to boot (based on NVRAM) */
377 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800378 rv = vb2api_fw_phase2(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500379 if (rv) {
380 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600381 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500382 vboot_reboot();
383 }
384
385 /* Try that slot (verify its keyblock and preamble) */
386 printk(BIOS_INFO, "Phase 3\n");
387 timestamp_add_now(TS_START_VERIFY_SLOT);
Joel Kitching2332c742019-10-23 15:01:37 +0800388 rv = vb2api_fw_phase3(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500389 timestamp_add_now(TS_END_VERIFY_SLOT);
390 if (rv) {
391 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600392 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500393 vboot_reboot();
394 }
395
396 printk(BIOS_INFO, "Phase 4\n");
Julius Wernerf8e17642019-12-12 13:23:06 -0800397 rv = vboot_locate_firmware(ctx, &fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500398 if (rv)
Keith Short70064582019-05-06 16:12:57 -0600399 die_with_post_code(POST_INVALID_ROM,
400 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500401
Julius Wernerf8e17642019-12-12 13:23:06 -0800402 rv = hash_body(ctx, &fw_body);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600403 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500404 if (rv) {
405 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
406 vboot_reboot();
407 }
408
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800409 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800410 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800411 timestamp_add_now(TS_START_TPMPCR);
Joel Kitching2332c742019-10-23 15:01:37 +0800412 rv = extend_pcrs(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800413 if (rv) {
414 printk(BIOS_WARNING,
415 "Failed to extend TPM PCRs (%#x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800416 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600417 vboot_save_data(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800418 vboot_reboot();
419 }
420 timestamp_add_now(TS_END_TPMPCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500421 }
422
423 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600424
425 timestamp_add_now(TS_START_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500426 rv = antirollback_lock_space_firmware();
427 if (rv) {
428 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800429 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600430 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500431 vboot_reboot();
432 }
Raul E Rangel4c518e12018-05-11 11:08:07 -0600433 timestamp_add_now(TS_END_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500434
Furquan Shaikhb038f412016-11-07 23:47:11 -0800435 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800436 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Furquan Shaikhb038f412016-11-07 23:47:11 -0800437 rv = antirollback_lock_space_rec_hash();
438 if (rv) {
439 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
440 rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800441 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
Furquan Shaikhb038f412016-11-07 23:47:11 -0800442 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600443 vboot_save_data(ctx);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800444 vboot_reboot();
445 }
446 }
447
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800448 printk(BIOS_INFO, "Slot %c is selected\n",
449 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800450
451 verstage_main_exit:
Joel Kitching532e0c72019-06-16 17:23:03 +0800452 /* If CBMEM is not up yet, let the ROMSTAGE_CBMEM_INIT_HOOK take care
453 of running this function. */
454 if (ENV_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
455 vboot_log_and_clear_recovery_mode_switch(0);
456
Joel Kitching7b10deb2019-06-17 15:22:28 +0800457 /* Save recovery reason in case of unexpected reboots on x86. */
458 vboot_save_recovery_reason_vbnv();
459
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500460 timestamp_add_now(TS_END_VBOOT);
461}