blob: 6c4f8fd2a81d2ec548d526e774059d78978f0c9e [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
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600254void vboot_save_nvdata_only(struct vb2_context *ctx)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500255{
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600256 assert(!(ctx->flags & (VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED |
257 VB2_CONTEXT_SECDATA_KERNEL_CHANGED)));
258
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500259 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 }
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600264}
265
266void vboot_save_data(struct vb2_context *ctx)
267{
Julius Werner683657e2019-12-04 12:50:43 -0800268 if (ctx->flags & VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500269 printk(BIOS_INFO, "Saving secdata\n");
270 antirollback_write_space_firmware(ctx);
Julius Werner683657e2019-12-04 12:50:43 -0800271 ctx->flags &= ~VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500272 }
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600273
274 vboot_save_nvdata_only(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500275}
276
277static uint32_t extend_pcrs(struct vb2_context *ctx)
278{
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +0100279 return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) ||
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100280 vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500281}
282
Joel Kitching532e0c72019-06-16 17:23:03 +0800283static void vboot_log_and_clear_recovery_mode_switch(int unused)
284{
285 /* Log the recovery mode switches if required, before clearing them. */
286 log_recovery_mode_switch();
287
288 /*
289 * The recovery mode switch is cleared (typically backed by EC) here
290 * to allow multiple queries to get_recovery_mode_switch() and have
291 * them return consistent results during the verified boot path as well
292 * as dram initialization. x86 systems ignore the saved dram settings
293 * in the recovery path in order to start from a clean slate. Therefore
294 * clear the state here since this function is called when memory
295 * is known to be up.
296 */
297 clear_recovery_mode_switch();
298}
299#if !CONFIG(VBOOT_STARTS_IN_ROMSTAGE)
300ROMSTAGE_CBMEM_INIT_HOOK(vboot_log_and_clear_recovery_mode_switch)
301#endif
302
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500303/**
304 * Verify and select the firmware in the RW image
305 *
306 * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage).
307 * when per-stage verification is ready.
308 */
309void verstage_main(void)
310{
Joel Kitching2332c742019-10-23 15:01:37 +0800311 struct vb2_context *ctx;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500312 struct region_device fw_main;
Joel Kitching220ac042019-07-31 14:19:00 +0800313 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500314
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500315 timestamp_add_now(TS_START_VBOOT);
316
317 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800318 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500319
Aaron Durbin118a84f2017-09-15 11:15:07 -0600320 /* Initialize and read nvdata from non-volatile storage. */
Joel Kitching2332c742019-10-23 15:01:37 +0800321 vbnv_init(ctx->nvdata);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500322
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800323 /* Set S3 resume flag if vboot should behave differently when selecting
324 * which slot to boot. This is only relevant to vboot if the platform
325 * does verification of memory init and thus must ensure it resumes with
326 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800327 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100328 vboot_platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800329 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800330
Duncan Lauriea613a312016-03-14 09:32:08 -0700331 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
332 * check the return value here because vb2api_fw_phase1 will catch
333 * invalid secdata and tell us what to do (=reboot). */
334 timestamp_add_now(TS_START_TPMINIT);
Joel Kitching2332c742019-10-23 15:01:37 +0800335 if (vboot_setup_tpm(ctx) == TPM_SUCCESS)
336 antirollback_read_space_firmware(ctx);
Duncan Lauriea613a312016-03-14 09:32:08 -0700337 timestamp_add_now(TS_END_TPMINIT);
338
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100339 /* Enable measured boot mode */
Julius Wernercd49cce2019-03-05 16:53:33 -0800340 if (CONFIG(VBOOT_MEASURED_BOOT) &&
Joel Kitching2332c742019-10-23 15:01:37 +0800341 !(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100342 if (vboot_init_crtm() != VB2_SUCCESS)
Keith Short70064582019-05-06 16:12:57 -0600343 die_with_post_code(POST_INVALID_ROM,
344 "Initializing measured boot mode failed!");
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100345 }
346
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500347 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800348 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800349 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800350 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500351 }
352
Julius Wernercd49cce2019-03-05 16:53:33 -0800353 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100354 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800355 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500356
Julius Wernercd49cce2019-03-05 16:53:33 -0800357 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800358 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500359
Joel Kitching5923d672019-04-12 15:57:43 +0800360 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100361 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800362 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800363
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500364 /* Do early init (set up secdata and NVRAM, load GBB) */
365 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800366 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500367
368 if (rv) {
369 /*
370 * If vb2api_fw_phase1 fails, check for return value.
371 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
372 * into recovery mode.
373 * For any other error code, save context if needed and reboot.
374 */
375 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
376 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600377 vboot_save_data(ctx);
Joel Kitching2332c742019-10-23 15:01:37 +0800378 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800379 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500380 }
381
Raul E Rangel3a591742018-07-17 14:44:43 -0600382 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600383 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500384 vboot_reboot();
385 }
386
387 /* Determine which firmware slot to boot (based on NVRAM) */
388 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800389 rv = vb2api_fw_phase2(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500390 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 /* Try that slot (verify its keyblock and preamble) */
397 printk(BIOS_INFO, "Phase 3\n");
398 timestamp_add_now(TS_START_VERIFY_SLOT);
Joel Kitching2332c742019-10-23 15:01:37 +0800399 rv = vb2api_fw_phase3(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500400 timestamp_add_now(TS_END_VERIFY_SLOT);
401 if (rv) {
402 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600403 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500404 vboot_reboot();
405 }
406
407 printk(BIOS_INFO, "Phase 4\n");
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800408 rv = vboot_locate_firmware(ctx, &fw_main);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500409 if (rv)
Keith Short70064582019-05-06 16:12:57 -0600410 die_with_post_code(POST_INVALID_ROM,
411 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500412
Joel Kitching2332c742019-10-23 15:01:37 +0800413 rv = hash_body(ctx, &fw_main);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600414 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500415 if (rv) {
416 printk(BIOS_INFO, "Reboot requested (%x)\n", rv);
417 vboot_reboot();
418 }
419
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800420 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800421 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800422 timestamp_add_now(TS_START_TPMPCR);
Joel Kitching2332c742019-10-23 15:01:37 +0800423 rv = extend_pcrs(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800424 if (rv) {
425 printk(BIOS_WARNING,
426 "Failed to extend TPM PCRs (%#x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800427 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600428 vboot_save_data(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800429 vboot_reboot();
430 }
431 timestamp_add_now(TS_END_TPMPCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500432 }
433
434 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600435
436 timestamp_add_now(TS_START_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500437 rv = antirollback_lock_space_firmware();
438 if (rv) {
439 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800440 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600441 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500442 vboot_reboot();
443 }
Raul E Rangel4c518e12018-05-11 11:08:07 -0600444 timestamp_add_now(TS_END_TPMLOCK);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500445
Furquan Shaikhb038f412016-11-07 23:47:11 -0800446 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800447 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Furquan Shaikhb038f412016-11-07 23:47:11 -0800448 rv = antirollback_lock_space_rec_hash();
449 if (rv) {
450 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n",
451 rv);
Joel Kitching2332c742019-10-23 15:01:37 +0800452 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR,
Furquan Shaikhb038f412016-11-07 23:47:11 -0800453 0);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600454 vboot_save_data(ctx);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800455 vboot_reboot();
456 }
457 }
458
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800459 printk(BIOS_INFO, "Slot %c is selected\n",
460 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800461
462 verstage_main_exit:
Joel Kitching532e0c72019-06-16 17:23:03 +0800463 /* If CBMEM is not up yet, let the ROMSTAGE_CBMEM_INIT_HOOK take care
464 of running this function. */
465 if (ENV_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
466 vboot_log_and_clear_recovery_mode_switch(0);
467
Joel Kitching7b10deb2019-06-17 15:22:28 +0800468 /* Save recovery reason in case of unexpected reboots on x86. */
469 vboot_save_recovery_reason_vbnv();
470
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500471 timestamp_add_now(TS_END_VBOOT);
472}