blob: 53314b21436ee4201c5c2c3815c48c88d9e145cb [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
Jakub Czapiga967a76b2022-08-19 12:25:27 +020057static vb2_error_t handle_digest_result(void *slot_hash, size_t slot_hash_sz)
Aaron Durbin87c9fae2016-01-22 15:26:04 -060058{
59 int is_resume;
60
61 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +053062 * Chrome EC is the only support for vboot_save_hash() &
63 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +053064 */
Julius Wernercd49cce2019-03-05 16:53:33 -080065 if (!CONFIG(EC_GOOGLE_CHROMEEC))
Jakub Czapiga967a76b2022-08-19 12:25:27 +020066 return VB2_SUCCESS;
Naresh G Solanki3d44d692016-11-06 12:51:14 +053067
68 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -060069 * Nothing to do since resuming on the platform doesn't require
70 * vboot verification again.
71 */
Julius Wernercd49cce2019-03-05 16:53:33 -080072 if (!CONFIG(RESUME_PATH_SAME_AS_BOOT))
Jakub Czapiga967a76b2022-08-19 12:25:27 +020073 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -060074
75 /*
76 * Assume that if vboot doesn't start in bootblock verified
77 * RW memory init code is not employed. i.e. memory init code
78 * lives in RO CBFS.
79 */
Julius Wernercd49cce2019-03-05 16:53:33 -080080 if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Jakub Czapiga967a76b2022-08-19 12:25:27 +020081 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -060082
Bill XIE516c0a52020-02-24 23:08:35 +080083 is_resume = platform_is_resuming();
Aaron Durbin87c9fae2016-01-22 15:26:04 -060084
85 if (is_resume > 0) {
86 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
87 const size_t saved_hash_sz = sizeof(saved_hash);
88
Jakub Czapigaa7f66902022-11-17 10:34:48 +000089 assert(slot_hash_sz <= saved_hash_sz);
Aaron Durbin87c9fae2016-01-22 15:26:04 -060090
91 printk(BIOS_DEBUG, "Platform is resuming.\n");
92
93 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
94 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
Jakub Czapiga967a76b2022-08-19 12:25:27 +020095 return VB2_ERROR_UNKNOWN;
Aaron Durbin87c9fae2016-01-22 15:26:04 -060096 }
97
98 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
99 printk(BIOS_ERR, "Hash mismatch on resume.\n");
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200100 return VB2_ERROR_UNKNOWN;
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600101 }
102 } else if (is_resume < 0)
103 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
104
105 printk(BIOS_DEBUG, "Saving vboot hash.\n");
106
107 /* Always save the hash for the current boot. */
108 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
109 printk(BIOS_ERR, "Error saving vboot hash.\n");
110 /* Though this is an error don't report it up since it could
111 * lead to a reboot loop. The consequence of this is that
112 * we will most likely fail resuming because of EC issues or
113 * the hash digest not matching. */
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200114 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600115 }
116
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200117 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600118}
119
Joel Kitching220ac042019-07-31 14:19:00 +0800120static vb2_error_t hash_body(struct vb2_context *ctx,
Julius Wernerf8e17642019-12-12 13:23:06 -0800121 struct region_device *fw_body)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500122{
123 uint64_t load_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800124 uint32_t remaining;
Martin Roth8839b7f2020-10-28 11:38:57 -0600125 uint8_t block[CONFIG_VBOOT_HASH_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600126 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
127 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500128 size_t block_size = sizeof(block);
129 size_t offset;
Joel Kitching220ac042019-07-31 14:19:00 +0800130 vb2_error_t rv;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500131
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600132 /* Clear the full digest so that any hash digests less than the
133 * max have trailing zeros. */
134 memset(hash_digest, 0, hash_digest_sz);
135
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500136 /*
137 * Since loading the firmware and calculating its hash is intertwined,
138 * we use this little trick to measure them separately and pretend it
139 * was first loaded and then hashed in one piece with the timestamps.
140 * (This split won't make sense with memory-mapped media like on x86.)
141 */
142 load_ts = timestamp_get();
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100143 timestamp_add(TS_HASH_BODY_START, load_ts);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500144
Julius Wernerf8e17642019-12-12 13:23:06 -0800145 remaining = region_device_sz(fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500146 offset = 0;
147
148 /* Start the body hash */
Julius Wernerf8e17642019-12-12 13:23:06 -0800149 rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500150 if (rv)
151 return rv;
152
153 /* Extend over the body */
Julius Wernerf8e17642019-12-12 13:23:06 -0800154 while (remaining) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500155 uint64_t temp_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800156 if (block_size > remaining)
157 block_size = remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500158
159 temp_ts = timestamp_get();
Julius Wernerf8e17642019-12-12 13:23:06 -0800160 if (rdev_readat(fw_body, block, offset, block_size) < 0)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500161 return VB2_ERROR_UNKNOWN;
162 load_ts += timestamp_get() - temp_ts;
163
164 rv = vb2api_extend_hash(ctx, block, block_size);
165 if (rv)
166 return rv;
167
Julius Wernerf8e17642019-12-12 13:23:06 -0800168 remaining -= block_size;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500169 offset += block_size;
170 }
171
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100172 timestamp_add(TS_LOADING_END, load_ts);
173 timestamp_add_now(TS_HASHING_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500174
175 /* Check the result (with RSA signature verification) */
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600176 rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500177 if (rv)
178 return rv;
179
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100180 timestamp_add_now(TS_HASH_BODY_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500181
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200182 return handle_digest_result(hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500183}
184
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500185static uint32_t extend_pcrs(struct vb2_context *ctx)
186{
Sergii Dmytruk4129c262022-10-24 01:17:41 +0300187 return vboot_extend_pcr(ctx, CONFIG_PCR_BOOT_MODE, BOOT_MODE_PCR) ||
188 vboot_extend_pcr(ctx, CONFIG_PCR_HWID, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500189}
190
Daisuke Nojiri494a5dd2021-05-12 12:50:41 -0700191#define EC_EFS_BOOT_MODE_VERIFIED_RW 0x00
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700192#define EC_EFS_BOOT_MODE_UNTRUSTED_RO 0x01
Daisuke Nojiri494a5dd2021-05-12 12:50:41 -0700193#define EC_EFS_BOOT_MODE_TRUSTED_RO 0x02
dnojiridff56a02020-04-03 10:56:43 -0700194
195static const char *get_boot_mode_string(uint8_t boot_mode)
196{
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700197 if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO)
198 return "TRUSTED_RO";
199 else if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO)
200 return "UNTRUSTED_RO";
201 else if (boot_mode == EC_EFS_BOOT_MODE_VERIFIED_RW)
202 return "VERIFIED_RW";
dnojiridff56a02020-04-03 10:56:43 -0700203 else
204 return "UNDEFINED";
205}
206
207static void check_boot_mode(struct vb2_context *ctx)
208{
209 uint8_t boot_mode;
210 int rv;
211
212 rv = tlcl_cr50_get_boot_mode(&boot_mode);
213 switch (rv) {
214 case TPM_E_NO_SUCH_COMMAND:
215 printk(BIOS_WARNING, "Cr50 does not support GET_BOOT_MODE.\n");
216 /* Proceed to legacy boot model. */
217 return;
218 case TPM_SUCCESS:
219 break;
220 default:
221 printk(BIOS_ERR,
222 "Communication error in getting Cr50 boot mode.\n");
dnojiridff56a02020-04-03 10:56:43 -0700223 vb2api_fail(ctx, VB2_RECOVERY_CR50_BOOT_MODE, rv);
dnojiridff56a02020-04-03 10:56:43 -0700224 return;
225 }
226
227 printk(BIOS_INFO, "Cr50 says boot_mode is %s(0x%02x).\n",
228 get_boot_mode_string(boot_mode), boot_mode);
229
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700230 if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO)
dnojiridff56a02020-04-03 10:56:43 -0700231 ctx->flags |= VB2_CONTEXT_NO_BOOT;
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700232 else if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO)
233 ctx->flags |= VB2_CONTEXT_EC_TRUSTED;
dnojiridff56a02020-04-03 10:56:43 -0700234}
235
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200236/* Verify and select the firmware in the RW image */
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500237void verstage_main(void)
238{
Joel Kitching2332c742019-10-23 15:01:37 +0800239 struct vb2_context *ctx;
Joel Kitching220ac042019-07-31 14:19:00 +0800240 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500241
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100242 timestamp_add_now(TS_VBOOT_START);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500243
Patrick Rudolph6093c502019-05-08 18:36:39 +0200244 /* Lockdown SPI flash controller if required */
245 if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE))
246 boot_device_security_lockdown();
247
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500248 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800249 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500250
Aaron Durbin118a84f2017-09-15 11:15:07 -0600251 /* Initialize and read nvdata from non-volatile storage. */
Karthikeyan Ramasubramanianf2dcd9d2022-12-05 14:43:46 -0700252 vbnv_init();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500253
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800254 /* Set S3 resume flag if vboot should behave differently when selecting
255 * which slot to boot. This is only relevant to vboot if the platform
256 * does verification of memory init and thus must ensure it resumes with
257 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800258 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Bill XIE516c0a52020-02-24 23:08:35 +0800259 platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800260 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800261
Duncan Lauriea613a312016-03-14 09:32:08 -0700262 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
263 * check the return value here because vb2api_fw_phase1 will catch
264 * invalid secdata and tell us what to do (=reboot). */
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100265 timestamp_add_now(TS_TPMINIT_START);
dnojiridff56a02020-04-03 10:56:43 -0700266 if (vboot_setup_tpm(ctx) == TPM_SUCCESS) {
Joel Kitching2332c742019-10-23 15:01:37 +0800267 antirollback_read_space_firmware(ctx);
dnojiridff56a02020-04-03 10:56:43 -0700268 antirollback_read_space_kernel(ctx);
269 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100270 timestamp_add_now(TS_TPMINIT_END);
Duncan Lauriea613a312016-03-14 09:32:08 -0700271
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500272 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800273 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800274 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800275 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500276 }
277
Julius Wernercd49cce2019-03-05 16:53:33 -0800278 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100279 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800280 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500281
Julius Wernercd49cce2019-03-05 16:53:33 -0800282 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800283 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500284
Joel Kitching5923d672019-04-12 15:57:43 +0800285 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100286 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800287 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800288
Daisuke Nojiri850728b2021-05-12 12:50:41 -0700289 /*
290 * Get boot mode from GSC. This allows us to refuse to boot OS
291 * (with VB2_CONTEXT_NO_BOOT) or to switch to developer mode (with
292 * !VB2_CONTEXT_EC_TRUSTED).
293 *
294 * If there is an communication error, a recovery reason will be set and
295 * vb2api_fw_phase1 will route us to recovery mode.
296 */
Jes B. Klinkec6b041a12022-04-19 14:00:33 -0700297 if (CONFIG(TPM_GOOGLE))
Daisuke Nojiri850728b2021-05-12 12:50:41 -0700298 check_boot_mode(ctx);
299
Hsuan-ting Chen642508a2021-10-27 10:59:41 +0000300 if (get_ec_is_trusted())
301 ctx->flags |= VB2_CONTEXT_EC_TRUSTED;
302
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500303 /* Do early init (set up secdata and NVRAM, load GBB) */
304 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800305 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500306
307 if (rv) {
308 /*
309 * If vb2api_fw_phase1 fails, check for return value.
310 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
311 * into recovery mode.
312 * For any other error code, save context if needed and reboot.
313 */
314 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
315 printk(BIOS_INFO, "Recovery requested (%x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600316 vboot_save_data(ctx);
Joel Kitching2332c742019-10-23 15:01:37 +0800317 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800318 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500319 }
Jakub Czapiga605f7932022-11-04 12:18:04 +0000320 vboot_save_and_reboot(ctx, rv);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500321 }
322
323 /* Determine which firmware slot to boot (based on NVRAM) */
324 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800325 rv = vb2api_fw_phase2(ctx);
Jakub Czapiga605f7932022-11-04 12:18:04 +0000326 if (rv)
327 vboot_save_and_reboot(ctx, rv);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500328
329 /* Try that slot (verify its keyblock and preamble) */
330 printk(BIOS_INFO, "Phase 3\n");
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100331 timestamp_add_now(TS_VERIFY_SLOT_START);
Joel Kitching2332c742019-10-23 15:01:37 +0800332 rv = vb2api_fw_phase3(ctx);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100333 timestamp_add_now(TS_VERIFY_SLOT_END);
Jakub Czapiga605f7932022-11-04 12:18:04 +0000334 if (rv)
335 vboot_save_and_reboot(ctx, rv);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500336
337 printk(BIOS_INFO, "Phase 4\n");
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200338 if (CONFIG(VBOOT_CBFS_INTEGRATION)) {
339 struct vb2_hash *metadata_hash;
340 rv = vb2api_get_metadata_hash(ctx, &metadata_hash);
341 if (rv == VB2_SUCCESS)
342 rv = handle_digest_result(metadata_hash->raw,
343 vb2_digest_size(metadata_hash->algo));
344 } else {
345 struct region_device fw_body;
346 rv = vboot_locate_firmware(ctx, &fw_body);
347 if (rv)
lilacious40cb3fe2023-06-21 23:24:14 +0200348 die_with_post_code(POSTCODE_INVALID_ROM,
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200349 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500350
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200351 rv = hash_body(ctx, &fw_body);
352 }
353
Jakub Czapiga605f7932022-11-04 12:18:04 +0000354 if (rv)
355 vboot_save_and_reboot(ctx, rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600356 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500357
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800358 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800359 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100360 timestamp_add_now(TS_TPMPCR_START);
Joel Kitching2332c742019-10-23 15:01:37 +0800361 rv = extend_pcrs(ctx);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800362 if (rv) {
Jakub Czapiga605f7932022-11-04 12:18:04 +0000363 printk(BIOS_WARNING, "Failed to extend TPM PCRs (%#x)\n", rv);
364 vboot_fail_and_reboot(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800365 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100366 timestamp_add_now(TS_TPMPCR_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500367 }
368
369 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600370
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100371 timestamp_add_now(TS_TPMLOCK_START);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500372 rv = antirollback_lock_space_firmware();
373 if (rv) {
374 printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv);
Jakub Czapiga605f7932022-11-04 12:18:04 +0000375 vboot_fail_and_reboot(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500376 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100377 timestamp_add_now(TS_TPMLOCK_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500378
Furquan Shaikhb038f412016-11-07 23:47:11 -0800379 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800380 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Shelley Chena79803c2020-10-16 13:15:59 -0700381 rv = antirollback_lock_space_mrc_hash(MRC_REC_HASH_NV_INDEX);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800382 if (rv) {
Jakub Czapiga605f7932022-11-04 12:18:04 +0000383 printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n", rv);
384 vboot_fail_and_reboot(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR, 0);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800385 }
386 }
387
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800388 printk(BIOS_INFO, "Slot %c is selected\n",
389 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800390
391 verstage_main_exit:
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100392 timestamp_add_now(TS_VBOOT_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500393}