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