Angel Pons | 986d50e | 2020-04-02 23:48:53 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 2 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 3 | #include <arch/exception.h> |
| 4 | #include <assert.h> |
Elyes HAOUAS | 27718ac | 2020-09-19 09:32:36 +0200 | [diff] [blame] | 5 | #include <console/console.h> |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 6 | #include <bootmode.h> |
Yu-Ping Wu | 29c8fa4 | 2019-11-18 11:25:47 +0800 | [diff] [blame] | 7 | #include <fmap.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 8 | #include <security/tpm/tspi/crtm.h> |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 9 | #include <security/tpm/tss/vendor/cr50/cr50.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 10 | #include <security/vboot/misc.h> |
| 11 | #include <security/vboot/vbnv.h> |
| 12 | #include <security/vboot/tpm_common.h> |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <timestamp.h> |
| 15 | #include <vb2_api.h> |
Patrick Rudolph | 6093c50 | 2019-05-08 18:36:39 +0200 | [diff] [blame] | 16 | #include <boot_device.h> |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 17 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 18 | #include "antirollback.h" |
| 19 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 20 | /* The max hash size to expect is for SHA512. */ |
| 21 | #define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE |
| 22 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 23 | /* exports */ |
| 24 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 25 | vb2_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 Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 30 | { |
| 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 Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 39 | if (vboot_is_firmware_slot_a(ctx)) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 40 | 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 Wu | 29c8fa4 | 2019-11-18 11:25:47 +0800 | [diff] [blame] | 48 | if (fmap_locate_area_as_rdev(name, &rdev)) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 49 | 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 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 57 | static int handle_digest_result(void *slot_hash, size_t slot_hash_sz) |
| 58 | { |
| 59 | int is_resume; |
| 60 | |
| 61 | /* |
Naresh G Solanki | 1d04d16 | 2016-11-08 00:27:41 +0530 | [diff] [blame] | 62 | * Chrome EC is the only support for vboot_save_hash() & |
| 63 | * vboot_retrieve_hash(), if Chrome EC is not enabled then return. |
Naresh G Solanki | 3d44d69 | 2016-11-06 12:51:14 +0530 | [diff] [blame] | 64 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 65 | if (!CONFIG(EC_GOOGLE_CHROMEEC)) |
Naresh G Solanki | 3d44d69 | 2016-11-06 12:51:14 +0530 | [diff] [blame] | 66 | return 0; |
| 67 | |
| 68 | /* |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 69 | * Nothing to do since resuming on the platform doesn't require |
| 70 | * vboot verification again. |
| 71 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 72 | if (!CONFIG(RESUME_PATH_SAME_AS_BOOT)) |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 73 | return 0; |
| 74 | |
| 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 Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 80 | if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK)) |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 81 | return 0; |
| 82 | |
Bill XIE | 516c0a5 | 2020-02-24 23:08:35 +0800 | [diff] [blame] | 83 | is_resume = platform_is_resuming(); |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 84 | |
| 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 | |
| 89 | assert(slot_hash_sz == saved_hash_sz); |
| 90 | |
| 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"); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | if (memcmp(saved_hash, slot_hash, slot_hash_sz)) { |
| 99 | printk(BIOS_ERR, "Hash mismatch on resume.\n"); |
| 100 | return -1; |
| 101 | } |
| 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. */ |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 120 | static vb2_error_t hash_body(struct vb2_context *ctx, |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 121 | struct region_device *fw_body) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 122 | { |
| 123 | uint64_t load_ts; |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 124 | uint32_t remaining; |
Martin Roth | 8839b7f | 2020-10-28 11:38:57 -0600 | [diff] [blame] | 125 | uint8_t block[CONFIG_VBOOT_HASH_BLOCK_SIZE]; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 126 | uint8_t hash_digest[VBOOT_MAX_HASH_SIZE]; |
| 127 | const size_t hash_digest_sz = sizeof(hash_digest); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 128 | size_t block_size = sizeof(block); |
| 129 | size_t offset; |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 130 | vb2_error_t rv; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 131 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 132 | /* 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 Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 136 | /* |
| 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 Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 143 | timestamp_add(TS_HASH_BODY_START, load_ts); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 144 | |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 145 | remaining = region_device_sz(fw_body); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 146 | offset = 0; |
| 147 | |
| 148 | /* Start the body hash */ |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 149 | rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 150 | if (rv) |
| 151 | return rv; |
| 152 | |
| 153 | /* Extend over the body */ |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 154 | while (remaining) { |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 155 | uint64_t temp_ts; |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 156 | if (block_size > remaining) |
| 157 | block_size = remaining; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 158 | |
| 159 | temp_ts = timestamp_get(); |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 160 | if (rdev_readat(fw_body, block, offset, block_size) < 0) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 161 | 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 Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 168 | remaining -= block_size; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 169 | offset += block_size; |
| 170 | } |
| 171 | |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 172 | timestamp_add(TS_LOADING_END, load_ts); |
| 173 | timestamp_add_now(TS_HASHING_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 174 | |
| 175 | /* Check the result (with RSA signature verification) */ |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 176 | rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 177 | if (rv) |
| 178 | return rv; |
| 179 | |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 180 | timestamp_add_now(TS_HASH_BODY_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 181 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 182 | if (handle_digest_result(hash_digest, hash_digest_sz)) |
| 183 | return VB2_ERROR_UNKNOWN; |
| 184 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 185 | return VB2_SUCCESS; |
| 186 | } |
| 187 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 188 | static uint32_t extend_pcrs(struct vb2_context *ctx) |
| 189 | { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 190 | return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) || |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 191 | vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Daisuke Nojiri | 494a5dd | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 194 | #define EC_EFS_BOOT_MODE_VERIFIED_RW 0x00 |
Daisuke Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 195 | #define EC_EFS_BOOT_MODE_UNTRUSTED_RO 0x01 |
Daisuke Nojiri | 494a5dd | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 196 | #define EC_EFS_BOOT_MODE_TRUSTED_RO 0x02 |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 197 | |
| 198 | static const char *get_boot_mode_string(uint8_t boot_mode) |
| 199 | { |
Daisuke Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 200 | if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO) |
| 201 | return "TRUSTED_RO"; |
| 202 | else if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO) |
| 203 | return "UNTRUSTED_RO"; |
| 204 | else if (boot_mode == EC_EFS_BOOT_MODE_VERIFIED_RW) |
| 205 | return "VERIFIED_RW"; |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 206 | else |
| 207 | return "UNDEFINED"; |
| 208 | } |
| 209 | |
| 210 | static void check_boot_mode(struct vb2_context *ctx) |
| 211 | { |
| 212 | uint8_t boot_mode; |
| 213 | int rv; |
| 214 | |
| 215 | rv = tlcl_cr50_get_boot_mode(&boot_mode); |
| 216 | switch (rv) { |
| 217 | case TPM_E_NO_SUCH_COMMAND: |
| 218 | printk(BIOS_WARNING, "Cr50 does not support GET_BOOT_MODE.\n"); |
| 219 | /* Proceed to legacy boot model. */ |
| 220 | return; |
| 221 | case TPM_SUCCESS: |
| 222 | break; |
| 223 | default: |
| 224 | printk(BIOS_ERR, |
| 225 | "Communication error in getting Cr50 boot mode.\n"); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 226 | vb2api_fail(ctx, VB2_RECOVERY_CR50_BOOT_MODE, rv); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
| 230 | printk(BIOS_INFO, "Cr50 says boot_mode is %s(0x%02x).\n", |
| 231 | get_boot_mode_string(boot_mode), boot_mode); |
| 232 | |
Daisuke Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 233 | if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO) |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 234 | ctx->flags |= VB2_CONTEXT_NO_BOOT; |
Daisuke Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 235 | else if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO) |
| 236 | ctx->flags |= VB2_CONTEXT_EC_TRUSTED; |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 239 | /** |
| 240 | * Verify and select the firmware in the RW image |
| 241 | * |
| 242 | * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage). |
| 243 | * when per-stage verification is ready. |
| 244 | */ |
| 245 | void verstage_main(void) |
| 246 | { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 247 | struct vb2_context *ctx; |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 248 | struct region_device fw_body; |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 249 | vb2_error_t rv; |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 250 | |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 251 | timestamp_add_now(TS_VBOOT_START); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 252 | |
Patrick Rudolph | 6093c50 | 2019-05-08 18:36:39 +0200 | [diff] [blame] | 253 | /* Lockdown SPI flash controller if required */ |
| 254 | if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE)) |
| 255 | boot_device_security_lockdown(); |
| 256 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 257 | /* Set up context and work buffer */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 258 | ctx = vboot_get_context(); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 259 | |
Aaron Durbin | 118a84f | 2017-09-15 11:15:07 -0600 | [diff] [blame] | 260 | /* Initialize and read nvdata from non-volatile storage. */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 261 | vbnv_init(ctx->nvdata); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 262 | |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 263 | /* Set S3 resume flag if vboot should behave differently when selecting |
| 264 | * which slot to boot. This is only relevant to vboot if the platform |
| 265 | * does verification of memory init and thus must ensure it resumes with |
| 266 | * the same slot that it booted from. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 267 | if (CONFIG(RESUME_PATH_SAME_AS_BOOT) && |
Bill XIE | 516c0a5 | 2020-02-24 23:08:35 +0800 | [diff] [blame] | 268 | platform_is_resuming()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 269 | ctx->flags |= VB2_CONTEXT_S3_RESUME; |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 270 | |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 271 | /* 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 Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 274 | timestamp_add_now(TS_TPMINIT_START); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 275 | if (vboot_setup_tpm(ctx) == TPM_SUCCESS) { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 276 | antirollback_read_space_firmware(ctx); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 277 | antirollback_read_space_kernel(ctx); |
| 278 | } |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 279 | timestamp_add_now(TS_TPMINIT_END); |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 280 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 281 | if (get_recovery_mode_switch()) { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 282 | ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 283 | if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY)) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 284 | ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 287 | if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) && |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 288 | get_wipeout_mode_switch()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 289 | ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 290 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 291 | if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 292 | ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 293 | |
Joel Kitching | 5923d67 | 2019-04-12 15:57:43 +0800 | [diff] [blame] | 294 | /* Mainboard/SoC always initializes display. */ |
Wim Vervoorn | e7087a1 | 2019-11-15 14:02:02 +0100 | [diff] [blame] | 295 | if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY)) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 296 | ctx->flags |= VB2_CONTEXT_DISPLAY_INIT; |
Joel Kitching | 5923d67 | 2019-04-12 15:57:43 +0800 | [diff] [blame] | 297 | |
Daisuke Nojiri | 850728b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 298 | /* |
| 299 | * Get boot mode from GSC. This allows us to refuse to boot OS |
| 300 | * (with VB2_CONTEXT_NO_BOOT) or to switch to developer mode (with |
| 301 | * !VB2_CONTEXT_EC_TRUSTED). |
| 302 | * |
| 303 | * If there is an communication error, a recovery reason will be set and |
| 304 | * vb2api_fw_phase1 will route us to recovery mode. |
| 305 | */ |
| 306 | if (CONFIG(TPM_CR50)) |
| 307 | check_boot_mode(ctx); |
| 308 | |
Hsuan-ting Chen | 642508a | 2021-10-27 10:59:41 +0000 | [diff] [blame] | 309 | if (get_ec_is_trusted()) |
| 310 | ctx->flags |= VB2_CONTEXT_EC_TRUSTED; |
| 311 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 312 | /* Do early init (set up secdata and NVRAM, load GBB) */ |
| 313 | printk(BIOS_INFO, "Phase 1\n"); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 314 | rv = vb2api_fw_phase1(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 315 | |
| 316 | if (rv) { |
| 317 | /* |
| 318 | * If vb2api_fw_phase1 fails, check for return value. |
| 319 | * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue |
| 320 | * into recovery mode. |
| 321 | * For any other error code, save context if needed and reboot. |
| 322 | */ |
| 323 | if (rv == VB2_ERROR_API_PHASE1_RECOVERY) { |
| 324 | printk(BIOS_INFO, "Recovery requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 325 | vboot_save_data(ctx); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 326 | extend_pcrs(ctx); /* ignore failures */ |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 327 | goto verstage_main_exit; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Raul E Rangel | 3a59174 | 2018-07-17 14:44:43 -0600 | [diff] [blame] | 330 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 331 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 332 | vboot_reboot(); |
| 333 | } |
| 334 | |
| 335 | /* Determine which firmware slot to boot (based on NVRAM) */ |
| 336 | printk(BIOS_INFO, "Phase 2\n"); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 337 | rv = vb2api_fw_phase2(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 338 | if (rv) { |
| 339 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 340 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 341 | vboot_reboot(); |
| 342 | } |
| 343 | |
| 344 | /* Try that slot (verify its keyblock and preamble) */ |
| 345 | printk(BIOS_INFO, "Phase 3\n"); |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 346 | timestamp_add_now(TS_VERIFY_SLOT_START); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 347 | rv = vb2api_fw_phase3(ctx); |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 348 | timestamp_add_now(TS_VERIFY_SLOT_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 349 | if (rv) { |
| 350 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 351 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 352 | vboot_reboot(); |
| 353 | } |
| 354 | |
| 355 | printk(BIOS_INFO, "Phase 4\n"); |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 356 | rv = vboot_locate_firmware(ctx, &fw_body); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 357 | if (rv) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 358 | die_with_post_code(POST_INVALID_ROM, |
| 359 | "Failed to read FMAP to locate firmware"); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 360 | |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 361 | rv = hash_body(ctx, &fw_body); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 362 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 363 | if (rv) { |
| 364 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 365 | vboot_reboot(); |
| 366 | } |
| 367 | |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 368 | /* Only extend PCRs once on boot. */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 369 | if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) { |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 370 | timestamp_add_now(TS_TPMPCR_START); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 371 | rv = extend_pcrs(ctx); |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 372 | if (rv) { |
| 373 | printk(BIOS_WARNING, |
| 374 | "Failed to extend TPM PCRs (%#x)\n", rv); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 375 | vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 376 | vboot_save_data(ctx); |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 377 | vboot_reboot(); |
| 378 | } |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 379 | timestamp_add_now(TS_TPMPCR_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* Lock TPM */ |
Raul E Rangel | 4c518e1 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 383 | |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 384 | timestamp_add_now(TS_TPMLOCK_START); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 385 | rv = antirollback_lock_space_firmware(); |
| 386 | if (rv) { |
| 387 | printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 388 | vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 389 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 390 | vboot_reboot(); |
| 391 | } |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 392 | timestamp_add_now(TS_TPMLOCK_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 393 | |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 394 | /* Lock rec hash space if available. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 395 | if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) { |
Shelley Chen | a79803c | 2020-10-16 13:15:59 -0700 | [diff] [blame] | 396 | rv = antirollback_lock_space_mrc_hash(MRC_REC_HASH_NV_INDEX); |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 397 | if (rv) { |
| 398 | printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n", |
| 399 | rv); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 400 | vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR, |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 401 | 0); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 402 | vboot_save_data(ctx); |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 403 | vboot_reboot(); |
| 404 | } |
| 405 | } |
| 406 | |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 407 | printk(BIOS_INFO, "Slot %c is selected\n", |
| 408 | vboot_is_firmware_slot_a(ctx) ? 'A' : 'B'); |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 409 | |
| 410 | verstage_main_exit: |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 411 | timestamp_add_now(TS_VBOOT_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 412 | } |