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 | |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 57 | static vb2_error_t handle_digest_result(void *slot_hash, size_t slot_hash_sz) |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 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)) |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 66 | return VB2_SUCCESS; |
Naresh G Solanki | 3d44d69 | 2016-11-06 12:51:14 +0530 | [diff] [blame] | 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)) |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 73 | return VB2_SUCCESS; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 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)) |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 81 | return VB2_SUCCESS; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 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 | |
Jakub Czapiga | a7f6690 | 2022-11-17 10:34:48 +0000 | [diff] [blame] | 89 | assert(slot_hash_sz <= saved_hash_sz); |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 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"); |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 95 | return VB2_ERROR_UNKNOWN; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (memcmp(saved_hash, slot_hash, slot_hash_sz)) { |
| 99 | printk(BIOS_ERR, "Hash mismatch on resume.\n"); |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 100 | return VB2_ERROR_UNKNOWN; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 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. */ |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 114 | return VB2_SUCCESS; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 117 | return VB2_SUCCESS; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 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 | |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 182 | return handle_digest_result(hash_digest, hash_digest_sz); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 185 | static uint32_t extend_pcrs(struct vb2_context *ctx) |
| 186 | { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 187 | return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) || |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 188 | vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Daisuke Nojiri | 494a5dd | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 191 | #define EC_EFS_BOOT_MODE_VERIFIED_RW 0x00 |
Daisuke Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 192 | #define EC_EFS_BOOT_MODE_UNTRUSTED_RO 0x01 |
Daisuke Nojiri | 494a5dd | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 193 | #define EC_EFS_BOOT_MODE_TRUSTED_RO 0x02 |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 194 | |
| 195 | static const char *get_boot_mode_string(uint8_t boot_mode) |
| 196 | { |
Daisuke Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 197 | 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"; |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 203 | else |
| 204 | return "UNDEFINED"; |
| 205 | } |
| 206 | |
| 207 | static 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"); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 223 | vb2api_fail(ctx, VB2_RECOVERY_CR50_BOOT_MODE, rv); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 224 | 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 Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 230 | if (boot_mode == EC_EFS_BOOT_MODE_UNTRUSTED_RO) |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 231 | ctx->flags |= VB2_CONTEXT_NO_BOOT; |
Daisuke Nojiri | fc7900b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 232 | else if (boot_mode == EC_EFS_BOOT_MODE_TRUSTED_RO) |
| 233 | ctx->flags |= VB2_CONTEXT_EC_TRUSTED; |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 236 | /* Verify and select the firmware in the RW image */ |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 237 | void verstage_main(void) |
| 238 | { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 239 | struct vb2_context *ctx; |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 240 | vb2_error_t rv; |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 241 | |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 242 | timestamp_add_now(TS_VBOOT_START); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 243 | |
Patrick Rudolph | 6093c50 | 2019-05-08 18:36:39 +0200 | [diff] [blame] | 244 | /* Lockdown SPI flash controller if required */ |
| 245 | if (CONFIG(BOOTMEDIA_LOCK_IN_VERSTAGE)) |
| 246 | boot_device_security_lockdown(); |
| 247 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 248 | /* Set up context and work buffer */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 249 | ctx = vboot_get_context(); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 250 | |
Aaron Durbin | 118a84f | 2017-09-15 11:15:07 -0600 | [diff] [blame] | 251 | /* Initialize and read nvdata from non-volatile storage. */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 252 | vbnv_init(ctx->nvdata); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 253 | |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 254 | /* 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 Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 258 | if (CONFIG(RESUME_PATH_SAME_AS_BOOT) && |
Bill XIE | 516c0a5 | 2020-02-24 23:08:35 +0800 | [diff] [blame] | 259 | platform_is_resuming()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 260 | ctx->flags |= VB2_CONTEXT_S3_RESUME; |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 261 | |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 262 | /* 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 Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 265 | timestamp_add_now(TS_TPMINIT_START); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 266 | if (vboot_setup_tpm(ctx) == TPM_SUCCESS) { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 267 | antirollback_read_space_firmware(ctx); |
dnojiri | dff56a0 | 2020-04-03 10:56:43 -0700 | [diff] [blame] | 268 | antirollback_read_space_kernel(ctx); |
| 269 | } |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 270 | timestamp_add_now(TS_TPMINIT_END); |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 271 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 272 | if (get_recovery_mode_switch()) { |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 273 | ctx->flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 274 | if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY)) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 275 | ctx->flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 278 | if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) && |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 279 | get_wipeout_mode_switch()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 280 | ctx->flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 281 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 282 | if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch()) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 283 | ctx->flags |= VB2_CONTEXT_NOFAIL_BOOT; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 284 | |
Joel Kitching | 5923d67 | 2019-04-12 15:57:43 +0800 | [diff] [blame] | 285 | /* Mainboard/SoC always initializes display. */ |
Wim Vervoorn | e7087a1 | 2019-11-15 14:02:02 +0100 | [diff] [blame] | 286 | if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY) || CONFIG(VBOOT_ALWAYS_ENABLE_DISPLAY)) |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 287 | ctx->flags |= VB2_CONTEXT_DISPLAY_INIT; |
Joel Kitching | 5923d67 | 2019-04-12 15:57:43 +0800 | [diff] [blame] | 288 | |
Daisuke Nojiri | 850728b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 289 | /* |
| 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. Klinke | c6b041a1 | 2022-04-19 14:00:33 -0700 | [diff] [blame] | 297 | if (CONFIG(TPM_GOOGLE)) |
Daisuke Nojiri | 850728b | 2021-05-12 12:50:41 -0700 | [diff] [blame] | 298 | check_boot_mode(ctx); |
| 299 | |
Hsuan-ting Chen | 642508a | 2021-10-27 10:59:41 +0000 | [diff] [blame] | 300 | if (get_ec_is_trusted()) |
| 301 | ctx->flags |= VB2_CONTEXT_EC_TRUSTED; |
| 302 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 303 | /* Do early init (set up secdata and NVRAM, load GBB) */ |
| 304 | printk(BIOS_INFO, "Phase 1\n"); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 305 | rv = vb2api_fw_phase1(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 306 | |
| 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 Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 316 | vboot_save_data(ctx); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 317 | extend_pcrs(ctx); /* ignore failures */ |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 318 | goto verstage_main_exit; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 319 | } |
Jakub Czapiga | 605f793 | 2022-11-04 12:18:04 +0000 | [diff] [blame] | 320 | vboot_save_and_reboot(ctx, rv); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* Determine which firmware slot to boot (based on NVRAM) */ |
| 324 | printk(BIOS_INFO, "Phase 2\n"); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 325 | rv = vb2api_fw_phase2(ctx); |
Jakub Czapiga | 605f793 | 2022-11-04 12:18:04 +0000 | [diff] [blame] | 326 | if (rv) |
| 327 | vboot_save_and_reboot(ctx, rv); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 328 | |
| 329 | /* Try that slot (verify its keyblock and preamble) */ |
| 330 | printk(BIOS_INFO, "Phase 3\n"); |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 331 | timestamp_add_now(TS_VERIFY_SLOT_START); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 332 | rv = vb2api_fw_phase3(ctx); |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 333 | timestamp_add_now(TS_VERIFY_SLOT_END); |
Jakub Czapiga | 605f793 | 2022-11-04 12:18:04 +0000 | [diff] [blame] | 334 | if (rv) |
| 335 | vboot_save_and_reboot(ctx, rv); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 336 | |
| 337 | printk(BIOS_INFO, "Phase 4\n"); |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 338 | 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) |
| 348 | die_with_post_code(POST_INVALID_ROM, |
| 349 | "Failed to read FMAP to locate firmware"); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 350 | |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 351 | rv = hash_body(ctx, &fw_body); |
| 352 | } |
| 353 | |
Jakub Czapiga | 605f793 | 2022-11-04 12:18:04 +0000 | [diff] [blame] | 354 | if (rv) |
| 355 | vboot_save_and_reboot(ctx, rv); |
Tim Wawrzynczak | d6fc557 | 2019-10-25 14:58:15 -0600 | [diff] [blame] | 356 | vboot_save_data(ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 357 | |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 358 | /* Only extend PCRs once on boot. */ |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 359 | if (!(ctx->flags & VB2_CONTEXT_S3_RESUME)) { |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 360 | timestamp_add_now(TS_TPMPCR_START); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 361 | rv = extend_pcrs(ctx); |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 362 | if (rv) { |
Jakub Czapiga | 605f793 | 2022-11-04 12:18:04 +0000 | [diff] [blame] | 363 | 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 Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 365 | } |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 366 | timestamp_add_now(TS_TPMPCR_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* Lock TPM */ |
Raul E Rangel | 4c518e1 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 370 | |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 371 | timestamp_add_now(TS_TPMLOCK_START); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 372 | rv = antirollback_lock_space_firmware(); |
| 373 | if (rv) { |
| 374 | printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv); |
Jakub Czapiga | 605f793 | 2022-11-04 12:18:04 +0000 | [diff] [blame] | 375 | vboot_fail_and_reboot(ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 376 | } |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 377 | timestamp_add_now(TS_TPMLOCK_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 378 | |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 379 | /* Lock rec hash space if available. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 380 | if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) { |
Shelley Chen | a79803c | 2020-10-16 13:15:59 -0700 | [diff] [blame] | 381 | rv = antirollback_lock_space_mrc_hash(MRC_REC_HASH_NV_INDEX); |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 382 | if (rv) { |
Jakub Czapiga | 605f793 | 2022-11-04 12:18:04 +0000 | [diff] [blame] | 383 | 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 Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 388 | printk(BIOS_INFO, "Slot %c is selected\n", |
| 389 | vboot_is_firmware_slot_a(ctx) ? 'A' : 'B'); |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 390 | |
| 391 | verstage_main_exit: |
Jakub Czapiga | ad6157e | 2022-02-15 11:50:31 +0100 | [diff] [blame] | 392 | timestamp_add_now(TS_VBOOT_END); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 393 | } |