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> |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 5 | #include <bootmode.h> |
Yu-Ping Wu | 29c8fa4 | 2019-11-18 11:25:47 +0800 | [diff] [blame] | 6 | #include <fmap.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 7 | #include <security/tpm/tspi/crtm.h> |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 8 | #include <security/tpm/tss/vendor/cr50/cr50.h> |
Bill XIE | c79e96b | 2019-08-22 20:28:36 +0800 | [diff] [blame] | 9 | #include <security/vboot/misc.h> |
| 10 | #include <security/vboot/vbnv.h> |
| 11 | #include <security/vboot/tpm_common.h> |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <timestamp.h> |
| 14 | #include <vb2_api.h> |
Patrick Rudolph | 6093c50 | 2019-05-08 18:36:39 +0200 | [diff] [blame] | 15 | #include <boot_device.h> |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 16 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 17 | #include "antirollback.h" |
| 18 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 19 | /* The max hash size to expect is for SHA512. */ |
| 20 | #define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE |
| 21 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 22 | /* exports */ |
| 23 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 24 | vb2_error_t vb2ex_read_resource(struct vb2_context *ctx, |
| 25 | enum vb2_resource_index index, |
| 26 | uint32_t offset, |
| 27 | void *buf, |
| 28 | uint32_t size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 29 | { |
| 30 | struct region_device rdev; |
| 31 | const char *name; |
| 32 | |
| 33 | switch (index) { |
| 34 | case VB2_RES_GBB: |
| 35 | name = "GBB"; |
| 36 | break; |
| 37 | case VB2_RES_FW_VBLOCK: |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 38 | if (vboot_is_firmware_slot_a(ctx)) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 39 | name = "VBLOCK_A"; |
| 40 | else |
| 41 | name = "VBLOCK_B"; |
| 42 | break; |
| 43 | default: |
| 44 | return VB2_ERROR_EX_READ_RESOURCE_INDEX; |
| 45 | } |
| 46 | |
Yu-Ping Wu | 29c8fa4 | 2019-11-18 11:25:47 +0800 | [diff] [blame] | 47 | if (fmap_locate_area_as_rdev(name, &rdev)) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 48 | return VB2_ERROR_EX_READ_RESOURCE_SIZE; |
| 49 | |
| 50 | if (rdev_readat(&rdev, buf, offset, size) != size) |
| 51 | return VB2_ERROR_EX_READ_RESOURCE_SIZE; |
| 52 | |
| 53 | return VB2_SUCCESS; |
| 54 | } |
| 55 | |
| 56 | /* No-op stubs that can be overridden by SoCs with hardware crypto support. */ |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 57 | __weak vb2_error_t vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg, |
| 58 | uint32_t data_size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 59 | { |
| 60 | return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED; |
| 61 | } |
| 62 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 63 | __weak vb2_error_t vb2ex_hwcrypto_digest_extend(const uint8_t *buf, |
| 64 | uint32_t size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 65 | { |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 66 | BUG(); /* Should never get called if init() returned an error. */ |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 67 | return VB2_ERROR_UNKNOWN; |
| 68 | } |
| 69 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 70 | __weak vb2_error_t vb2ex_hwcrypto_digest_finalize(uint8_t *digest, |
| 71 | uint32_t digest_size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 72 | { |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 73 | BUG(); /* Should never get called if init() returned an error. */ |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 74 | return VB2_ERROR_UNKNOWN; |
| 75 | } |
| 76 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 77 | static int handle_digest_result(void *slot_hash, size_t slot_hash_sz) |
| 78 | { |
| 79 | int is_resume; |
| 80 | |
| 81 | /* |
Naresh G Solanki | 1d04d16 | 2016-11-08 00:27:41 +0530 | [diff] [blame] | 82 | * Chrome EC is the only support for vboot_save_hash() & |
| 83 | * vboot_retrieve_hash(), if Chrome EC is not enabled then return. |
Naresh G Solanki | 3d44d69 | 2016-11-06 12:51:14 +0530 | [diff] [blame] | 84 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 85 | if (!CONFIG(EC_GOOGLE_CHROMEEC)) |
Naresh G Solanki | 3d44d69 | 2016-11-06 12:51:14 +0530 | [diff] [blame] | 86 | return 0; |
| 87 | |
| 88 | /* |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 89 | * Nothing to do since resuming on the platform doesn't require |
| 90 | * vboot verification again. |
| 91 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 92 | if (!CONFIG(RESUME_PATH_SAME_AS_BOOT)) |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 93 | return 0; |
| 94 | |
| 95 | /* |
| 96 | * Assume that if vboot doesn't start in bootblock verified |
| 97 | * RW memory init code is not employed. i.e. memory init code |
| 98 | * lives in RO CBFS. |
| 99 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 100 | if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK)) |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 101 | return 0; |
| 102 | |
Bill XIE | 516c0a5 | 2020-02-24 23:08:35 +0800 | [diff] [blame] | 103 | is_resume = platform_is_resuming(); |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 104 | |
| 105 | if (is_resume > 0) { |
| 106 | uint8_t saved_hash[VBOOT_MAX_HASH_SIZE]; |
| 107 | const size_t saved_hash_sz = sizeof(saved_hash); |
| 108 | |
| 109 | assert(slot_hash_sz == saved_hash_sz); |
| 110 | |
| 111 | printk(BIOS_DEBUG, "Platform is resuming.\n"); |
| 112 | |
| 113 | if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) { |
| 114 | printk(BIOS_ERR, "Couldn't retrieve saved hash.\n"); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | if (memcmp(saved_hash, slot_hash, slot_hash_sz)) { |
| 119 | printk(BIOS_ERR, "Hash mismatch on resume.\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | } else if (is_resume < 0) |
| 123 | printk(BIOS_ERR, "Unable to determine if platform resuming.\n"); |
| 124 | |
| 125 | printk(BIOS_DEBUG, "Saving vboot hash.\n"); |
| 126 | |
| 127 | /* Always save the hash for the current boot. */ |
| 128 | if (vboot_save_hash(slot_hash, slot_hash_sz)) { |
| 129 | printk(BIOS_ERR, "Error saving vboot hash.\n"); |
| 130 | /* Though this is an error don't report it up since it could |
| 131 | * lead to a reboot loop. The consequence of this is that |
| 132 | * we will most likely fail resuming because of EC issues or |
| 133 | * the hash digest not matching. */ |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 140 | static vb2_error_t hash_body(struct vb2_context *ctx, |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 141 | struct region_device *fw_body) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 142 | { |
| 143 | uint64_t load_ts; |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 144 | uint32_t remaining; |
Martin Roth | 8839b7f | 2020-10-28 11:38:57 -0600 | [diff] [blame] | 145 | uint8_t block[CONFIG_VBOOT_HASH_BLOCK_SIZE]; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 146 | uint8_t hash_digest[VBOOT_MAX_HASH_SIZE]; |
| 147 | const size_t hash_digest_sz = sizeof(hash_digest); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 148 | size_t block_size = sizeof(block); |
| 149 | size_t offset; |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 150 | vb2_error_t rv; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 151 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 152 | /* Clear the full digest so that any hash digests less than the |
| 153 | * max have trailing zeros. */ |
| 154 | memset(hash_digest, 0, hash_digest_sz); |
| 155 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 156 | /* |
| 157 | * Since loading the firmware and calculating its hash is intertwined, |
| 158 | * we use this little trick to measure them separately and pretend it |
| 159 | * was first loaded and then hashed in one piece with the timestamps. |
| 160 | * (This split won't make sense with memory-mapped media like on x86.) |
| 161 | */ |
| 162 | load_ts = timestamp_get(); |
| 163 | timestamp_add(TS_START_HASH_BODY, load_ts); |
| 164 | |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 165 | remaining = region_device_sz(fw_body); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 166 | offset = 0; |
| 167 | |
| 168 | /* Start the body hash */ |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 169 | rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 170 | if (rv) |
| 171 | return rv; |
| 172 | |
| 173 | /* Extend over the body */ |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 174 | while (remaining) { |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 175 | uint64_t temp_ts; |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 176 | if (block_size > remaining) |
| 177 | block_size = remaining; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 178 | |
| 179 | temp_ts = timestamp_get(); |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 180 | if (rdev_readat(fw_body, block, offset, block_size) < 0) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 181 | return VB2_ERROR_UNKNOWN; |
| 182 | load_ts += timestamp_get() - temp_ts; |
| 183 | |
| 184 | rv = vb2api_extend_hash(ctx, block, block_size); |
| 185 | if (rv) |
| 186 | return rv; |
| 187 | |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 188 | remaining -= block_size; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 189 | offset += block_size; |
| 190 | } |
| 191 | |
| 192 | timestamp_add(TS_DONE_LOADING, load_ts); |
| 193 | timestamp_add_now(TS_DONE_HASHING); |
| 194 | |
| 195 | /* Check the result (with RSA signature verification) */ |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 196 | rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 197 | if (rv) |
| 198 | return rv; |
| 199 | |
| 200 | timestamp_add_now(TS_END_HASH_BODY); |
| 201 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 202 | if (handle_digest_result(hash_digest, hash_digest_sz)) |
| 203 | return VB2_ERROR_UNKNOWN; |
| 204 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 205 | return VB2_SUCCESS; |
| 206 | } |
| 207 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 208 | static uint32_t extend_pcrs(struct vb2_context *ctx) |
| 209 | { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 210 | return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) || |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 211 | vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 212 | } |
| 213 | |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 214 | #define EC_EFS_BOOT_MODE_NORMAL 0x00 |
| 215 | #define EC_EFS_BOOT_MODE_NO_BOOT 0x01 |
| 216 | |
| 217 | static const char *get_boot_mode_string(uint8_t boot_mode) |
| 218 | { |
| 219 | if (boot_mode == EC_EFS_BOOT_MODE_NORMAL) |
| 220 | return "NORMAL"; |
| 221 | else if (boot_mode == EC_EFS_BOOT_MODE_NO_BOOT) |
| 222 | return "NO_BOOT"; |
| 223 | else |
| 224 | return "UNDEFINED"; |
| 225 | } |
| 226 | |
| 227 | static void check_boot_mode(struct vb2_context *ctx) |
| 228 | { |
| 229 | uint8_t boot_mode; |
| 230 | int rv; |
| 231 | |
| 232 | rv = tlcl_cr50_get_boot_mode(&boot_mode); |
| 233 | switch (rv) { |
| 234 | case TPM_E_NO_SUCH_COMMAND: |
| 235 | printk(BIOS_WARNING, "Cr50 does not support GET_BOOT_MODE.\n"); |
| 236 | /* Proceed to legacy boot model. */ |
| 237 | return; |
| 238 | case TPM_SUCCESS: |
| 239 | break; |
| 240 | default: |
| 241 | printk(BIOS_ERR, |
| 242 | "Communication error in getting Cr50 boot mode.\n"); |
| 243 | if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) |
| 244 | /* Continue to boot in recovery mode */ |
| 245 | return; |
| 246 | vb2api_fail(ctx, VB2_RECOVERY_CR50_BOOT_MODE, rv); |
| 247 | vboot_save_data(ctx); |
| 248 | vboot_reboot(); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | printk(BIOS_INFO, "Cr50 says boot_mode is %s(0x%02x).\n", |
| 253 | get_boot_mode_string(boot_mode), boot_mode); |
| 254 | |
| 255 | if (boot_mode == EC_EFS_BOOT_MODE_NO_BOOT) |
| 256 | ctx->flags |= VB2_CONTEXT_NO_BOOT; |
| 257 | } |
| 258 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 259 | /** |
| 260 | * Verify and select the firmware in the RW image |
| 261 | * |
| 262 | * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage). |
| 263 | * when per-stage verification is ready. |
| 264 | */ |
| 265 | void verstage_main(void) |
| 266 | { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 267 | struct vb2_context *ctx; |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 268 | struct region_device fw_body; |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 269 | vb2_error_t rv; |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 270 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 271 | timestamp_add_now(TS_START_VBOOT); |
| 272 | |
Patrick Rudolph | 6093c50 | 2019-05-08 18:36:39 +0200 | [diff] [blame] | 273 | /* Lockdown SPI flash controller if required */ |
| 274 | if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE)) |
| 275 | boot_device_security_lockdown(); |
| 276 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 277 | /* Set up context and work buffer */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 278 | ctx = vboot_get_context(); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 279 | |
Aaron Durbin | 118a84f | 2017-09-15 11:15:07 -0600 | [diff] [blame] | 280 | /* Initialize and read nvdata from non-volatile storage. */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 281 | vbnv_init(ctx->nvdata); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 282 | |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 283 | /* Set S3 resume flag if vboot should behave differently when selecting |
| 284 | * which slot to boot. This is only relevant to vboot if the platform |
| 285 | * does verification of memory init and thus must ensure it resumes with |
| 286 | * the same slot that it booted from. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 287 | if (CONFIG(RESUME_PATH_SAME_AS_BOOT) && |
Bill XIE | 516c0a5 | 2020-02-24 23:08:35 +0800 | [diff] [blame] | 288 | platform_is_resuming()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 289 | ctx->flags |= VB2_CONTEXT_S3_RESUME; |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 290 | |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 291 | /* Read secdata from TPM. Initialize TPM if secdata not found. We don't |
| 292 | * check the return value here because vb2api_fw_phase1 will catch |
| 293 | * invalid secdata and tell us what to do (=reboot). */ |
| 294 | timestamp_add_now(TS_START_TPMINIT); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 295 | if (vboot_setup_tpm(ctx) == TPM_SUCCESS) { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 296 | antirollback_read_space_firmware(ctx); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 297 | antirollback_read_space_kernel(ctx); |
| 298 | } |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 299 | timestamp_add_now(TS_END_TPMINIT); |
| 300 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 301 | if (get_recovery_mode_switch()) { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 302 | ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 303 | if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY)) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 304 | ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 307 | if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) && |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 308 | get_wipeout_mode_switch()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 309 | ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 310 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 311 | if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 312 | ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 313 | |
Joel Kitching | 5923d67 | 2019-04-12 15:57:43 +0800 | [diff] [blame] | 314 | /* Mainboard/SoC always initializes display. */ |
Wim Vervoorn | e7087a1 | 2019-11-15 14:02:02 +0100 | [diff] [blame] | 315 | if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY)) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 316 | ctx->flags |= VB2_CONTEXT_DISPLAY_INIT; |
Joel Kitching | 5923d67 | 2019-04-12 15:57:43 +0800 | [diff] [blame] | 317 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 318 | /* Do early init (set up secdata and NVRAM, load GBB) */ |
| 319 | printk(BIOS_INFO, "Phase 1\n"); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 320 | rv = vb2api_fw_phase1(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 321 | |
| 322 | if (rv) { |
| 323 | /* |
| 324 | * If vb2api_fw_phase1 fails, check for return value. |
| 325 | * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue |
| 326 | * into recovery mode. |
| 327 | * For any other error code, save context if needed and reboot. |
| 328 | */ |
| 329 | if (rv == VB2_ERROR_API_PHASE1_RECOVERY) { |
| 330 | printk(BIOS_INFO, "Recovery requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 331 | vboot_save_data(ctx); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 332 | extend_pcrs(ctx); /* ignore failures */ |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 333 | goto verstage_main_exit; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Raul E Rangel | 3a59174 | 2018-07-17 14:44:43 -0600 | [diff] [blame] | 336 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 337 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 338 | vboot_reboot(); |
| 339 | } |
| 340 | |
| 341 | /* Determine which firmware slot to boot (based on NVRAM) */ |
| 342 | printk(BIOS_INFO, "Phase 2\n"); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 343 | rv = vb2api_fw_phase2(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 344 | if (rv) { |
| 345 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 346 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 347 | vboot_reboot(); |
| 348 | } |
| 349 | |
| 350 | /* Try that slot (verify its keyblock and preamble) */ |
| 351 | printk(BIOS_INFO, "Phase 3\n"); |
| 352 | timestamp_add_now(TS_START_VERIFY_SLOT); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 353 | rv = vb2api_fw_phase3(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 354 | timestamp_add_now(TS_END_VERIFY_SLOT); |
| 355 | if (rv) { |
| 356 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 357 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 358 | vboot_reboot(); |
| 359 | } |
| 360 | |
| 361 | printk(BIOS_INFO, "Phase 4\n"); |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 362 | rv = vboot_locate_firmware(ctx, &fw_body); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 363 | if (rv) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 364 | die_with_post_code(POST_INVALID_ROM, |
| 365 | "Failed to read FMAP to locate firmware"); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 366 | |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 367 | rv = hash_body(ctx, &fw_body); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 368 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 369 | if (rv) { |
| 370 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 371 | vboot_reboot(); |
| 372 | } |
| 373 | |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 374 | /* Only extend PCRs once on boot. */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 375 | if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) { |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 376 | timestamp_add_now(TS_START_TPMPCR); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 377 | rv = extend_pcrs(ctx); |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 378 | if (rv) { |
| 379 | printk(BIOS_WARNING, |
| 380 | "Failed to extend TPM PCRs (%#x)\n", rv); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 381 | vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 382 | vboot_save_data(ctx); |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 383 | vboot_reboot(); |
| 384 | } |
| 385 | timestamp_add_now(TS_END_TPMPCR); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 386 | } |
| 387 | |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 388 | if (CONFIG(TPM_CR50)) |
| 389 | check_boot_mode(ctx); |
| 390 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 391 | /* Lock TPM */ |
Raul E Rangel | 4c518e1 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 392 | |
| 393 | timestamp_add_now(TS_START_TPMLOCK); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 394 | rv = antirollback_lock_space_firmware(); |
| 395 | if (rv) { |
| 396 | printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 397 | vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 398 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 399 | vboot_reboot(); |
| 400 | } |
Raul E Rangel | 4c518e1 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 401 | timestamp_add_now(TS_END_TPMLOCK); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 402 | |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 403 | /* Lock rec hash space if available. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 404 | if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) { |
Shelley Chen | a79803c | 2020-10-16 13:15:59 -0700 | [diff] [blame] | 405 | rv = antirollback_lock_space_mrc_hash(MRC_REC_HASH_NV_INDEX); |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 406 | if (rv) { |
| 407 | printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n", |
| 408 | rv); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 409 | vb2api_fail(ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR, |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 410 | 0); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 411 | vboot_save_data(ctx); |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 412 | vboot_reboot(); |
| 413 | } |
| 414 | } |
| 415 | |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 416 | printk(BIOS_INFO, "Slot %c is selected\n", |
| 417 | vboot_is_firmware_slot_a(ctx) ? 'A' : 'B'); |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 418 | |
| 419 | verstage_main_exit: |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 420 | timestamp_add_now(TS_END_VBOOT); |
| 421 | } |