blob: 11983b9e1ed1d85bd92a0bed74c120268999be16 [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>
Jon Murphya2f08aa2023-09-05 11:43:14 -06007#include <ec/google/chromeec/ec.h>
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +08008#include <fmap.h>
Bill XIEc79e96b2019-08-22 20:28:36 +08009#include <security/tpm/tspi/crtm.h>
dnojiridff56a02020-04-03 10:56:43 -070010#include <security/tpm/tss/vendor/cr50/cr50.h>
Jon Murphyd7b8dc92023-09-05 11:36:43 -060011#include <security/tpm/tss_errors.h>
Bill XIEc79e96b2019-08-22 20:28:36 +080012#include <security/vboot/misc.h>
13#include <security/vboot/vbnv.h>
14#include <security/vboot/tpm_common.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050015#include <string.h>
16#include <timestamp.h>
17#include <vb2_api.h>
Patrick Rudolph6093c502019-05-08 18:36:39 +020018#include <boot_device.h>
Aaron Durbin588ad7b2015-09-29 17:56:59 -050019
Philipp Deppenwiesec07f8fb2018-02-27 19:40:52 +010020#include "antirollback.h"
21
Aaron Durbin87c9fae2016-01-22 15:26:04 -060022/* The max hash size to expect is for SHA512. */
23#define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE
24
Aaron Durbin588ad7b2015-09-29 17:56:59 -050025/* exports */
26
Joel Kitching220ac042019-07-31 14:19:00 +080027vb2_error_t vb2ex_read_resource(struct vb2_context *ctx,
28 enum vb2_resource_index index,
29 uint32_t offset,
30 void *buf,
31 uint32_t size)
Aaron Durbin588ad7b2015-09-29 17:56:59 -050032{
33 struct region_device rdev;
34 const char *name;
35
36 switch (index) {
37 case VB2_RES_GBB:
38 name = "GBB";
39 break;
40 case VB2_RES_FW_VBLOCK:
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080041 if (vboot_is_firmware_slot_a(ctx))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050042 name = "VBLOCK_A";
43 else
44 name = "VBLOCK_B";
45 break;
46 default:
47 return VB2_ERROR_EX_READ_RESOURCE_INDEX;
48 }
49
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +080050 if (fmap_locate_area_as_rdev(name, &rdev))
Aaron Durbin588ad7b2015-09-29 17:56:59 -050051 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
52
53 if (rdev_readat(&rdev, buf, offset, size) != size)
54 return VB2_ERROR_EX_READ_RESOURCE_SIZE;
55
56 return VB2_SUCCESS;
57}
58
Jakub Czapiga967a76b2022-08-19 12:25:27 +020059static vb2_error_t handle_digest_result(void *slot_hash, size_t slot_hash_sz)
Aaron Durbin87c9fae2016-01-22 15:26:04 -060060{
61 int is_resume;
62
63 /*
Naresh G Solanki1d04d162016-11-08 00:27:41 +053064 * Chrome EC is the only support for vboot_save_hash() &
65 * vboot_retrieve_hash(), if Chrome EC is not enabled then return.
Naresh G Solanki3d44d692016-11-06 12:51:14 +053066 */
Julius Wernercd49cce2019-03-05 16:53:33 -080067 if (!CONFIG(EC_GOOGLE_CHROMEEC))
Jakub Czapiga967a76b2022-08-19 12:25:27 +020068 return VB2_SUCCESS;
Naresh G Solanki3d44d692016-11-06 12:51:14 +053069
70 /*
Aaron Durbin87c9fae2016-01-22 15:26:04 -060071 * Nothing to do since resuming on the platform doesn't require
72 * vboot verification again.
73 */
Julius Wernercd49cce2019-03-05 16:53:33 -080074 if (!CONFIG(RESUME_PATH_SAME_AS_BOOT))
Jakub Czapiga967a76b2022-08-19 12:25:27 +020075 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -060076
77 /*
78 * Assume that if vboot doesn't start in bootblock verified
79 * RW memory init code is not employed. i.e. memory init code
80 * lives in RO CBFS.
81 */
Julius Wernercd49cce2019-03-05 16:53:33 -080082 if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK))
Jakub Czapiga967a76b2022-08-19 12:25:27 +020083 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -060084
Bill XIE516c0a52020-02-24 23:08:35 +080085 is_resume = platform_is_resuming();
Aaron Durbin87c9fae2016-01-22 15:26:04 -060086
87 if (is_resume > 0) {
88 uint8_t saved_hash[VBOOT_MAX_HASH_SIZE];
89 const size_t saved_hash_sz = sizeof(saved_hash);
90
Jakub Czapigaa7f66902022-11-17 10:34:48 +000091 assert(slot_hash_sz <= saved_hash_sz);
Aaron Durbin87c9fae2016-01-22 15:26:04 -060092
93 printk(BIOS_DEBUG, "Platform is resuming.\n");
94
95 if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) {
96 printk(BIOS_ERR, "Couldn't retrieve saved hash.\n");
Jakub Czapiga967a76b2022-08-19 12:25:27 +020097 return VB2_ERROR_UNKNOWN;
Aaron Durbin87c9fae2016-01-22 15:26:04 -060098 }
99
100 if (memcmp(saved_hash, slot_hash, slot_hash_sz)) {
101 printk(BIOS_ERR, "Hash mismatch on resume.\n");
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200102 return VB2_ERROR_UNKNOWN;
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600103 }
104 } else if (is_resume < 0)
105 printk(BIOS_ERR, "Unable to determine if platform resuming.\n");
106
107 printk(BIOS_DEBUG, "Saving vboot hash.\n");
108
109 /* Always save the hash for the current boot. */
110 if (vboot_save_hash(slot_hash, slot_hash_sz)) {
111 printk(BIOS_ERR, "Error saving vboot hash.\n");
112 /* Though this is an error don't report it up since it could
113 * lead to a reboot loop. The consequence of this is that
114 * we will most likely fail resuming because of EC issues or
115 * the hash digest not matching. */
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200116 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600117 }
118
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200119 return VB2_SUCCESS;
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600120}
121
Joel Kitching220ac042019-07-31 14:19:00 +0800122static vb2_error_t hash_body(struct vb2_context *ctx,
Julius Wernerf8e17642019-12-12 13:23:06 -0800123 struct region_device *fw_body)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500124{
125 uint64_t load_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800126 uint32_t remaining;
Martin Roth8839b7f2020-10-28 11:38:57 -0600127 uint8_t block[CONFIG_VBOOT_HASH_BLOCK_SIZE];
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600128 uint8_t hash_digest[VBOOT_MAX_HASH_SIZE];
129 const size_t hash_digest_sz = sizeof(hash_digest);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500130 size_t block_size = sizeof(block);
131 size_t offset;
Jon Murphy24604812023-09-05 10:37:05 -0600132 vb2_error_t rc;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500133
Aaron Durbin87c9fae2016-01-22 15:26:04 -0600134 /* Clear the full digest so that any hash digests less than the
135 * max have trailing zeros. */
136 memset(hash_digest, 0, hash_digest_sz);
137
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500138 /*
139 * Since loading the firmware and calculating its hash is intertwined,
140 * we use this little trick to measure them separately and pretend it
141 * was first loaded and then hashed in one piece with the timestamps.
142 * (This split won't make sense with memory-mapped media like on x86.)
143 */
144 load_ts = timestamp_get();
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100145 timestamp_add(TS_HASH_BODY_START, load_ts);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500146
Julius Wernerf8e17642019-12-12 13:23:06 -0800147 remaining = region_device_sz(fw_body);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500148 offset = 0;
149
150 /* Start the body hash */
Jon Murphy24604812023-09-05 10:37:05 -0600151 rc = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY);
152 if (rc)
153 return rc;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500154
155 /* Extend over the body */
Julius Wernerf8e17642019-12-12 13:23:06 -0800156 while (remaining) {
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500157 uint64_t temp_ts;
Julius Wernerf8e17642019-12-12 13:23:06 -0800158 if (block_size > remaining)
159 block_size = remaining;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500160
161 temp_ts = timestamp_get();
Julius Wernerf8e17642019-12-12 13:23:06 -0800162 if (rdev_readat(fw_body, block, offset, block_size) < 0)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500163 return VB2_ERROR_UNKNOWN;
164 load_ts += timestamp_get() - temp_ts;
165
Jon Murphy24604812023-09-05 10:37:05 -0600166 rc = vb2api_extend_hash(ctx, block, block_size);
167 if (rc)
168 return rc;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500169
Julius Wernerf8e17642019-12-12 13:23:06 -0800170 remaining -= block_size;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500171 offset += block_size;
172 }
173
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100174 timestamp_add(TS_LOADING_END, load_ts);
175 timestamp_add_now(TS_HASHING_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500176
177 /* Check the result (with RSA signature verification) */
Jon Murphy24604812023-09-05 10:37:05 -0600178 rc = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz);
179 if (rc)
180 return rc;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500181
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100182 timestamp_add_now(TS_HASH_BODY_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500183
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200184 return handle_digest_result(hash_digest, hash_digest_sz);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500185}
186
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600187static tpm_result_t extend_pcrs(struct vb2_context *ctx)
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500188{
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600189 tpm_result_t rc;
190 rc = vboot_extend_pcr(ctx, CONFIG_PCR_BOOT_MODE, BOOT_MODE_PCR);
191 if (rc)
192 return rc;
Yu-Ping Wua3ff9e72023-09-14 14:56:11 +0800193 return vboot_extend_pcr(ctx, CONFIG_PCR_HWID, HWID_DIGEST_PCR);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500194}
195
Daisuke Nojiri494a5dd2021-05-12 12:50:41 -0700196#define EC_EFS_BOOT_MODE_VERIFIED_RW 0x00
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700197#define EC_EFS_BOOT_MODE_UNTRUSTED_RO 0x01
Daisuke Nojiri494a5dd2021-05-12 12:50:41 -0700198#define EC_EFS_BOOT_MODE_TRUSTED_RO 0x02
dnojiridff56a02020-04-03 10:56:43 -0700199
200static const char *get_boot_mode_string(uint8_t boot_mode)
201{
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700202 if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO)
203 return "TRUSTED_RO";
204 else if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO)
205 return "UNTRUSTED_RO";
206 else if (boot_mode == EC_EFS_BOOT_MODE_VERIFIED_RW)
207 return "VERIFIED_RW";
dnojiridff56a02020-04-03 10:56:43 -0700208 else
209 return "UNDEFINED";
210}
211
212static void check_boot_mode(struct vb2_context *ctx)
213{
214 uint8_t boot_mode;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600215 tpm_result_t rc;
dnojiridff56a02020-04-03 10:56:43 -0700216
Jon Murphy24604812023-09-05 10:37:05 -0600217 rc = tlcl_cr50_get_boot_mode(&boot_mode);
218 switch (rc) {
Jon Murphy056952e2023-09-05 10:44:09 -0600219 case TPM_CB_NO_SUCH_COMMAND:
Yu-Ping Wu7ce343d2023-08-09 09:39:50 +0800220 printk(BIOS_WARNING, "GSC does not support GET_BOOT_MODE.\n");
dnojiridff56a02020-04-03 10:56:43 -0700221 /* Proceed to legacy boot model. */
222 return;
223 case TPM_SUCCESS:
224 break;
225 default:
226 printk(BIOS_ERR,
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600227 "Communication error(%#x) in getting GSC boot mode.\n", rc);
Jon Murphy24604812023-09-05 10:37:05 -0600228 vb2api_fail(ctx, VB2_RECOVERY_GSC_BOOT_MODE, rc);
dnojiridff56a02020-04-03 10:56:43 -0700229 return;
230 }
231
Yu-Ping Wu7ce343d2023-08-09 09:39:50 +0800232 printk(BIOS_INFO, "GSC says boot_mode is %s(0x%02x).\n",
dnojiridff56a02020-04-03 10:56:43 -0700233 get_boot_mode_string(boot_mode), boot_mode);
234
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700235 if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO)
dnojiridff56a02020-04-03 10:56:43 -0700236 ctx->flags |= VB2_CONTEXT_NO_BOOT;
Daisuke Nojirifc7900b2021-05-12 12:50:41 -0700237 else if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO)
238 ctx->flags |= VB2_CONTEXT_EC_TRUSTED;
dnojiridff56a02020-04-03 10:56:43 -0700239}
240
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200241/* Verify and select the firmware in the RW image */
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500242void verstage_main(void)
243{
Joel Kitching2332c742019-10-23 15:01:37 +0800244 struct vb2_context *ctx;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600245 tpm_result_t tpm_rc;
Joel Kitching220ac042019-07-31 14:19:00 +0800246 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -0500247
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100248 timestamp_add_now(TS_VBOOT_START);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500249
Patrick Rudolph6093c502019-05-08 18:36:39 +0200250 /* Lockdown SPI flash controller if required */
251 if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE))
252 boot_device_security_lockdown();
253
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500254 /* Set up context and work buffer */
Joel Kitching2332c742019-10-23 15:01:37 +0800255 ctx = vboot_get_context();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500256
Aaron Durbin118a84f2017-09-15 11:15:07 -0600257 /* Initialize and read nvdata from non-volatile storage. */
Karthikeyan Ramasubramanianf2dcd9d2022-12-05 14:43:46 -0700258 vbnv_init();
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500259
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800260 /* Set S3 resume flag if vboot should behave differently when selecting
261 * which slot to boot. This is only relevant to vboot if the platform
262 * does verification of memory init and thus must ensure it resumes with
263 * the same slot that it booted from. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800264 if (CONFIG(RESUME_PATH_SAME_AS_BOOT) &&
Bill XIE516c0a52020-02-24 23:08:35 +0800265 platform_is_resuming())
Joel Kitching2332c742019-10-23 15:01:37 +0800266 ctx->flags |= VB2_CONTEXT_S3_RESUME;
Duncan Laurie1cdacca2016-02-19 20:26:07 -0800267
Michał Żygowskicb2561d2023-05-25 11:20:14 +0200268 if (!CONFIG(VBOOT_SLOTS_RW_AB))
269 ctx->flags |= VB2_CONTEXT_SLOT_A_ONLY;
270
Duncan Lauriea613a312016-03-14 09:32:08 -0700271 /* Read secdata from TPM. Initialize TPM if secdata not found. We don't
272 * check the return value here because vb2api_fw_phase1 will catch
273 * invalid secdata and tell us what to do (=reboot). */
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100274 timestamp_add_now(TS_TPMINIT_START);
Jon Murphya2f08aa2023-09-05 11:43:14 -0600275 rv = vboot_setup_tpm(ctx);
276 if (rv == TPM_SUCCESS) {
Joel Kitching2332c742019-10-23 15:01:37 +0800277 antirollback_read_space_firmware(ctx);
dnojiridff56a02020-04-03 10:56:43 -0700278 antirollback_read_space_kernel(ctx);
Jon Murphya2f08aa2023-09-05 11:43:14 -0600279 } else {
280 vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_S_ERROR, rv);
281 if (CONFIG(TPM_SETUP_HIBERNATE_ON_ERR) &&
282 rv == TPM_CB_COMMUNICATION_ERROR) {
283 printk(BIOS_ERR, "Failed to communicate with TPM\n"
284 "Next reboot will hibernate to reset TPM");
285 /* Command the EC to hibernate on next AP shutdown */
286 if (google_chromeec_reboot(
287 EC_REBOOT_HIBERNATE,
288 EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
289 printk(BIOS_ERR, "Failed to get EC to schedule hibernate");
290 }
291 }
dnojiridff56a02020-04-03 10:56:43 -0700292 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100293 timestamp_add_now(TS_TPMINIT_END);
Duncan Lauriea613a312016-03-14 09:32:08 -0700294
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500295 if (get_recovery_mode_switch()) {
Joel Kitching2332c742019-10-23 15:01:37 +0800296 ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE;
Julius Wernercd49cce2019-03-05 16:53:33 -0800297 if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY))
Joel Kitching2332c742019-10-23 15:01:37 +0800298 ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500299 }
300
Julius Wernercd49cce2019-03-05 16:53:33 -0800301 if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) &&
Philipp Deppenwiese66f9a092018-11-08 10:59:40 +0100302 get_wipeout_mode_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800303 ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500304
Julius Wernercd49cce2019-03-05 16:53:33 -0800305 if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch())
Joel Kitching2332c742019-10-23 15:01:37 +0800306 ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500307
Joel Kitching5923d672019-04-12 15:57:43 +0800308 /* Mainboard/SoC always initializes display. */
Wim Vervoorne7087a12019-11-15 14:02:02 +0100309 if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY))
Joel Kitching2332c742019-10-23 15:01:37 +0800310 ctx->flags |= VB2_CONTEXT_DISPLAY_INIT;
Joel Kitching5923d672019-04-12 15:57:43 +0800311
Daisuke Nojiri850728b2021-05-12 12:50:41 -0700312 /*
313 * Get boot mode from GSC. This allows us to refuse to boot OS
314 * (with VB2_CONTEXT_NO_BOOT) or to switch to developer mode (with
315 * !VB2_CONTEXT_EC_TRUSTED).
316 *
317 * If there is an communication error, a recovery reason will be set and
318 * vb2api_fw_phase1 will route us to recovery mode.
319 */
Jes B. Klinkec6b041a12022-04-19 14:00:33 -0700320 if (CONFIG(TPM_GOOGLE))
Daisuke Nojiri850728b2021-05-12 12:50:41 -0700321 check_boot_mode(ctx);
322
Hsuan-ting Chen642508a2021-10-27 10:59:41 +0000323 if (get_ec_is_trusted())
324 ctx->flags |= VB2_CONTEXT_EC_TRUSTED;
325
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500326 /* Do early init (set up secdata and NVRAM, load GBB) */
327 printk(BIOS_INFO, "Phase 1\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800328 rv = vb2api_fw_phase1(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500329
330 if (rv) {
331 /*
332 * If vb2api_fw_phase1 fails, check for return value.
333 * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue
334 * into recovery mode.
335 * For any other error code, save context if needed and reboot.
336 */
337 if (rv == VB2_ERROR_API_PHASE1_RECOVERY) {
Jon Murphy53fc6672023-09-26 21:05:37 -0600338 printk(BIOS_INFO, "Recovery requested (%#x)\n", rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600339 vboot_save_data(ctx);
Joel Kitching2332c742019-10-23 15:01:37 +0800340 extend_pcrs(ctx); /* ignore failures */
Joel Kitchingba50e482019-06-10 17:37:37 +0800341 goto verstage_main_exit;
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500342 }
Jakub Czapiga605f7932022-11-04 12:18:04 +0000343 vboot_save_and_reboot(ctx, rv);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500344 }
345
346 /* Determine which firmware slot to boot (based on NVRAM) */
347 printk(BIOS_INFO, "Phase 2\n");
Joel Kitching2332c742019-10-23 15:01:37 +0800348 rv = vb2api_fw_phase2(ctx);
Jakub Czapiga605f7932022-11-04 12:18:04 +0000349 if (rv)
350 vboot_save_and_reboot(ctx, rv);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500351
352 /* Try that slot (verify its keyblock and preamble) */
353 printk(BIOS_INFO, "Phase 3\n");
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100354 timestamp_add_now(TS_VERIFY_SLOT_START);
Joel Kitching2332c742019-10-23 15:01:37 +0800355 rv = vb2api_fw_phase3(ctx);
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100356 timestamp_add_now(TS_VERIFY_SLOT_END);
Jakub Czapiga605f7932022-11-04 12:18:04 +0000357 if (rv)
358 vboot_save_and_reboot(ctx, rv);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500359
360 printk(BIOS_INFO, "Phase 4\n");
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200361 if (CONFIG(VBOOT_CBFS_INTEGRATION)) {
362 struct vb2_hash *metadata_hash;
363 rv = vb2api_get_metadata_hash(ctx, &metadata_hash);
364 if (rv == VB2_SUCCESS)
365 rv = handle_digest_result(metadata_hash->raw,
366 vb2_digest_size(metadata_hash->algo));
367 } else {
368 struct region_device fw_body;
Yu-Ping Wua3ff9e72023-09-14 14:56:11 +0800369 if (vboot_locate_firmware(ctx, &fw_body))
lilacious40cb3fe2023-06-21 23:24:14 +0200370 die_with_post_code(POSTCODE_INVALID_ROM,
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200371 "Failed to read FMAP to locate firmware");
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500372
Jakub Czapiga967a76b2022-08-19 12:25:27 +0200373 rv = hash_body(ctx, &fw_body);
374 }
375
Jakub Czapiga605f7932022-11-04 12:18:04 +0000376 if (rv)
Martin L Rothd599e892023-11-01 00:04:56 +0000377 vboot_save_and_reboot(ctx, rv);
Tim Wawrzynczakd6fc5572019-10-25 14:58:15 -0600378 vboot_save_data(ctx);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500379
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800380 /* Only extend PCRs once on boot. */
Joel Kitching2332c742019-10-23 15:01:37 +0800381 if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) {
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100382 timestamp_add_now(TS_TPMPCR_START);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600383 tpm_rc = extend_pcrs(ctx);
384 if (tpm_rc) {
385 printk(BIOS_WARNING, "Failed to extend TPM PCRs (%#x)\n",
386 tpm_rc);
387 vboot_fail_and_reboot(ctx,
388 VB2_RECOVERY_RO_TPM_U_ERROR,
389 tpm_rc);
Joel Kitching6d88a5d2018-10-12 15:23:31 +0800390 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100391 timestamp_add_now(TS_TPMPCR_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500392 }
393
394 /* Lock TPM */
Raul E Rangel4c518e12018-05-11 11:08:07 -0600395
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100396 timestamp_add_now(TS_TPMLOCK_START);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600397 tpm_rc = antirollback_lock_space_firmware();
398 if (tpm_rc) {
399 printk(BIOS_INFO, "Failed to lock TPM (%#x)\n", tpm_rc);
Jakub Czapiga605f7932022-11-04 12:18:04 +0000400 vboot_fail_and_reboot(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500401 }
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100402 timestamp_add_now(TS_TPMLOCK_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500403
Furquan Shaikhb038f412016-11-07 23:47:11 -0800404 /* Lock rec hash space if available. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800405 if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600406 tpm_rc = antirollback_lock_space_mrc_hash(
407 MRC_REC_HASH_NV_INDEX);
408 if (tpm_rc) {
409 printk(BIOS_INFO, "Failed to lock rec hash space(%#x)\n",
410 tpm_rc);
411 vboot_fail_and_reboot(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR, tpm_rc);
Furquan Shaikhb038f412016-11-07 23:47:11 -0800412 }
413 }
414
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +0800415 printk(BIOS_INFO, "Slot %c is selected\n",
416 vboot_is_firmware_slot_a(ctx) ? 'A' : 'B');
Joel Kitchingba50e482019-06-10 17:37:37 +0800417
418 verstage_main_exit:
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100419 timestamp_add_now(TS_VBOOT_END);
Aaron Durbin588ad7b2015-09-29 17:56:59 -0500420}