Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 16 | #include <arch/exception.h> |
| 17 | #include <assert.h> |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 18 | #include <bootmode.h> |
Joel Kitching | 532e0c7 | 2019-06-16 17:23:03 +0800 | [diff] [blame] | 19 | #include <cbmem.h> |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 20 | #include <console/console.h> |
| 21 | #include <console/vtxprintf.h> |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <timestamp.h> |
| 24 | #include <vb2_api.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 25 | #include <security/vboot/misc.h> |
| 26 | #include <security/vboot/vbnv.h> |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 27 | #include <security/vboot/vboot_crtm.h> |
Christian Walter | 0bd84ed | 2019-07-23 10:26:30 +0200 | [diff] [blame] | 28 | #include <security/vboot/tpm_common.h> |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 29 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 30 | #include "antirollback.h" |
| 31 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 32 | /* The max hash size to expect is for SHA512. */ |
| 33 | #define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE |
| 34 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 35 | #define TODO_BLOCK_SIZE 1024 |
| 36 | |
| 37 | static int is_slot_a(struct vb2_context *ctx) |
| 38 | { |
| 39 | return !(ctx->flags & VB2_CONTEXT_FW_SLOT_B); |
| 40 | } |
| 41 | |
| 42 | /* exports */ |
| 43 | |
| 44 | void vb2ex_printf(const char *func, const char *fmt, ...) |
| 45 | { |
| 46 | va_list args; |
| 47 | |
Randall Spangler | e80c6f6 | 2017-01-20 14:48:53 -0800 | [diff] [blame] | 48 | if (func) |
| 49 | printk(BIOS_INFO, "VB2:%s() ", func); |
| 50 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 51 | va_start(args, fmt); |
Kyösti Mälkki | 7132f25 | 2019-02-14 23:08:29 +0200 | [diff] [blame] | 52 | vprintk(BIOS_INFO, fmt, args); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 53 | va_end(args); |
| 54 | |
| 55 | return; |
| 56 | } |
| 57 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 58 | vb2_error_t vb2ex_read_resource(struct vb2_context *ctx, |
| 59 | enum vb2_resource_index index, |
| 60 | uint32_t offset, |
| 61 | void *buf, |
| 62 | uint32_t size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 63 | { |
| 64 | struct region_device rdev; |
| 65 | const char *name; |
| 66 | |
| 67 | switch (index) { |
| 68 | case VB2_RES_GBB: |
| 69 | name = "GBB"; |
| 70 | break; |
| 71 | case VB2_RES_FW_VBLOCK: |
| 72 | if (is_slot_a(ctx)) |
| 73 | name = "VBLOCK_A"; |
| 74 | else |
| 75 | name = "VBLOCK_B"; |
| 76 | break; |
| 77 | default: |
| 78 | return VB2_ERROR_EX_READ_RESOURCE_INDEX; |
| 79 | } |
| 80 | |
| 81 | if (vboot_named_region_device(name, &rdev)) |
| 82 | return VB2_ERROR_EX_READ_RESOURCE_SIZE; |
| 83 | |
| 84 | if (rdev_readat(&rdev, buf, offset, size) != size) |
| 85 | return VB2_ERROR_EX_READ_RESOURCE_SIZE; |
| 86 | |
| 87 | return VB2_SUCCESS; |
| 88 | } |
| 89 | |
Joel Kitching | f350768 | 2019-10-15 01:04:35 +0800 | [diff] [blame] | 90 | void vb2ex_abort(void) |
| 91 | { |
| 92 | die("vboot has aborted execution; exit\n"); |
| 93 | } |
| 94 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 95 | /* 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] | 96 | __weak vb2_error_t vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg, |
| 97 | uint32_t data_size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 98 | { |
| 99 | return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED; |
| 100 | } |
| 101 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 102 | __weak vb2_error_t vb2ex_hwcrypto_digest_extend(const uint8_t *buf, |
| 103 | uint32_t size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 104 | { |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 105 | BUG(); /* Should never get called if init() returned an error. */ |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 106 | return VB2_ERROR_UNKNOWN; |
| 107 | } |
| 108 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 109 | __weak vb2_error_t vb2ex_hwcrypto_digest_finalize(uint8_t *digest, |
| 110 | uint32_t digest_size) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 111 | { |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 112 | BUG(); /* Should never get called if init() returned an error. */ |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 113 | return VB2_ERROR_UNKNOWN; |
| 114 | } |
| 115 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 116 | static int handle_digest_result(void *slot_hash, size_t slot_hash_sz) |
| 117 | { |
| 118 | int is_resume; |
| 119 | |
| 120 | /* |
Naresh G Solanki | 1d04d16 | 2016-11-08 00:27:41 +0530 | [diff] [blame] | 121 | * Chrome EC is the only support for vboot_save_hash() & |
| 122 | * vboot_retrieve_hash(), if Chrome EC is not enabled then return. |
Naresh G Solanki | 3d44d69 | 2016-11-06 12:51:14 +0530 | [diff] [blame] | 123 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 124 | if (!CONFIG(EC_GOOGLE_CHROMEEC)) |
Naresh G Solanki | 3d44d69 | 2016-11-06 12:51:14 +0530 | [diff] [blame] | 125 | return 0; |
| 126 | |
| 127 | /* |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 128 | * Nothing to do since resuming on the platform doesn't require |
| 129 | * vboot verification again. |
| 130 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 131 | if (!CONFIG(RESUME_PATH_SAME_AS_BOOT)) |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 132 | return 0; |
| 133 | |
| 134 | /* |
| 135 | * Assume that if vboot doesn't start in bootblock verified |
| 136 | * RW memory init code is not employed. i.e. memory init code |
| 137 | * lives in RO CBFS. |
| 138 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 139 | if (!CONFIG(VBOOT_STARTS_IN_BOOTBLOCK)) |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 140 | return 0; |
| 141 | |
| 142 | is_resume = vboot_platform_is_resuming(); |
| 143 | |
| 144 | if (is_resume > 0) { |
| 145 | uint8_t saved_hash[VBOOT_MAX_HASH_SIZE]; |
| 146 | const size_t saved_hash_sz = sizeof(saved_hash); |
| 147 | |
| 148 | assert(slot_hash_sz == saved_hash_sz); |
| 149 | |
| 150 | printk(BIOS_DEBUG, "Platform is resuming.\n"); |
| 151 | |
| 152 | if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) { |
| 153 | printk(BIOS_ERR, "Couldn't retrieve saved hash.\n"); |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | if (memcmp(saved_hash, slot_hash, slot_hash_sz)) { |
| 158 | printk(BIOS_ERR, "Hash mismatch on resume.\n"); |
| 159 | return -1; |
| 160 | } |
| 161 | } else if (is_resume < 0) |
| 162 | printk(BIOS_ERR, "Unable to determine if platform resuming.\n"); |
| 163 | |
| 164 | printk(BIOS_DEBUG, "Saving vboot hash.\n"); |
| 165 | |
| 166 | /* Always save the hash for the current boot. */ |
| 167 | if (vboot_save_hash(slot_hash, slot_hash_sz)) { |
| 168 | printk(BIOS_ERR, "Error saving vboot hash.\n"); |
| 169 | /* Though this is an error don't report it up since it could |
| 170 | * lead to a reboot loop. The consequence of this is that |
| 171 | * we will most likely fail resuming because of EC issues or |
| 172 | * the hash digest not matching. */ |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 179 | static vb2_error_t hash_body(struct vb2_context *ctx, |
| 180 | struct region_device *fw_main) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 181 | { |
| 182 | uint64_t load_ts; |
| 183 | uint32_t expected_size; |
| 184 | uint8_t block[TODO_BLOCK_SIZE]; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 185 | uint8_t hash_digest[VBOOT_MAX_HASH_SIZE]; |
| 186 | const size_t hash_digest_sz = sizeof(hash_digest); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 187 | size_t block_size = sizeof(block); |
| 188 | size_t offset; |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 189 | vb2_error_t rv; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 190 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 191 | /* Clear the full digest so that any hash digests less than the |
| 192 | * max have trailing zeros. */ |
| 193 | memset(hash_digest, 0, hash_digest_sz); |
| 194 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 195 | /* |
| 196 | * Since loading the firmware and calculating its hash is intertwined, |
| 197 | * we use this little trick to measure them separately and pretend it |
| 198 | * was first loaded and then hashed in one piece with the timestamps. |
| 199 | * (This split won't make sense with memory-mapped media like on x86.) |
| 200 | */ |
| 201 | load_ts = timestamp_get(); |
| 202 | timestamp_add(TS_START_HASH_BODY, load_ts); |
| 203 | |
| 204 | expected_size = region_device_sz(fw_main); |
| 205 | offset = 0; |
| 206 | |
| 207 | /* Start the body hash */ |
| 208 | rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY, &expected_size); |
| 209 | if (rv) |
| 210 | return rv; |
| 211 | |
Aaron Durbin | 4121f9e | 2016-01-27 14:23:17 -0600 | [diff] [blame] | 212 | /* |
| 213 | * Honor vboot's RW slot size. The expected size is pulled out of |
| 214 | * the preamble and obtained through vb2api_init_hash() above. By |
| 215 | * creating sub region the RW slot portion of the boot media is |
| 216 | * limited. |
| 217 | */ |
| 218 | if (rdev_chain(fw_main, fw_main, 0, expected_size)) { |
| 219 | printk(BIOS_ERR, "Unable to restrict CBFS size.\n"); |
| 220 | return VB2_ERROR_UNKNOWN; |
| 221 | } |
| 222 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 223 | /* Extend over the body */ |
| 224 | while (expected_size) { |
| 225 | uint64_t temp_ts; |
| 226 | if (block_size > expected_size) |
| 227 | block_size = expected_size; |
| 228 | |
| 229 | temp_ts = timestamp_get(); |
| 230 | if (rdev_readat(fw_main, block, offset, block_size) < 0) |
| 231 | return VB2_ERROR_UNKNOWN; |
| 232 | load_ts += timestamp_get() - temp_ts; |
| 233 | |
| 234 | rv = vb2api_extend_hash(ctx, block, block_size); |
| 235 | if (rv) |
| 236 | return rv; |
| 237 | |
| 238 | expected_size -= block_size; |
| 239 | offset += block_size; |
| 240 | } |
| 241 | |
| 242 | timestamp_add(TS_DONE_LOADING, load_ts); |
| 243 | timestamp_add_now(TS_DONE_HASHING); |
| 244 | |
| 245 | /* Check the result (with RSA signature verification) */ |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 246 | rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 247 | if (rv) |
| 248 | return rv; |
| 249 | |
| 250 | timestamp_add_now(TS_END_HASH_BODY); |
| 251 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 252 | if (handle_digest_result(hash_digest, hash_digest_sz)) |
| 253 | return VB2_ERROR_UNKNOWN; |
| 254 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 255 | return VB2_SUCCESS; |
| 256 | } |
| 257 | |
| 258 | static int locate_firmware(struct vb2_context *ctx, |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 259 | struct region_device *fw_main) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 260 | { |
| 261 | const char *name; |
| 262 | |
| 263 | if (is_slot_a(ctx)) |
| 264 | name = "FW_MAIN_A"; |
| 265 | else |
| 266 | name = "FW_MAIN_B"; |
| 267 | |
| 268 | return vboot_named_region_device(name, fw_main); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Save non-volatile and/or secure data if needed. |
| 273 | */ |
| 274 | static void save_if_needed(struct vb2_context *ctx) |
| 275 | { |
| 276 | if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) { |
| 277 | printk(BIOS_INFO, "Saving nvdata\n"); |
| 278 | save_vbnv(ctx->nvdata); |
| 279 | ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED; |
| 280 | } |
| 281 | if (ctx->flags & VB2_CONTEXT_SECDATA_CHANGED) { |
| 282 | printk(BIOS_INFO, "Saving secdata\n"); |
| 283 | antirollback_write_space_firmware(ctx); |
| 284 | ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static uint32_t extend_pcrs(struct vb2_context *ctx) |
| 289 | { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 290 | return vboot_extend_pcr(ctx, 0, BOOT_MODE_PCR) || |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 291 | vboot_extend_pcr(ctx, 1, HWID_DIGEST_PCR); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Joel Kitching | 532e0c7 | 2019-06-16 17:23:03 +0800 | [diff] [blame] | 294 | static void vboot_log_and_clear_recovery_mode_switch(int unused) |
| 295 | { |
| 296 | /* Log the recovery mode switches if required, before clearing them. */ |
| 297 | log_recovery_mode_switch(); |
| 298 | |
| 299 | /* |
| 300 | * The recovery mode switch is cleared (typically backed by EC) here |
| 301 | * to allow multiple queries to get_recovery_mode_switch() and have |
| 302 | * them return consistent results during the verified boot path as well |
| 303 | * as dram initialization. x86 systems ignore the saved dram settings |
| 304 | * in the recovery path in order to start from a clean slate. Therefore |
| 305 | * clear the state here since this function is called when memory |
| 306 | * is known to be up. |
| 307 | */ |
| 308 | clear_recovery_mode_switch(); |
| 309 | } |
| 310 | #if !CONFIG(VBOOT_STARTS_IN_ROMSTAGE) |
| 311 | ROMSTAGE_CBMEM_INIT_HOOK(vboot_log_and_clear_recovery_mode_switch) |
| 312 | #endif |
| 313 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 314 | /** |
| 315 | * Verify and select the firmware in the RW image |
| 316 | * |
| 317 | * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage). |
| 318 | * when per-stage verification is ready. |
| 319 | */ |
| 320 | void verstage_main(void) |
| 321 | { |
| 322 | struct vb2_context ctx; |
| 323 | struct region_device fw_main; |
Joel Kitching | 220ac04 | 2019-07-31 14:19:00 +0800 | [diff] [blame] | 324 | vb2_error_t rv; |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 325 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 326 | timestamp_add_now(TS_START_VBOOT); |
| 327 | |
| 328 | /* Set up context and work buffer */ |
Joel Kitching | af8471c | 2019-03-13 22:38:07 +0800 | [diff] [blame] | 329 | vboot_init_work_context(&ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 330 | |
Aaron Durbin | 118a84f | 2017-09-15 11:15:07 -0600 | [diff] [blame] | 331 | /* Initialize and read nvdata from non-volatile storage. */ |
| 332 | vbnv_init(ctx.nvdata); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 333 | |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 334 | /* Set S3 resume flag if vboot should behave differently when selecting |
| 335 | * which slot to boot. This is only relevant to vboot if the platform |
| 336 | * does verification of memory init and thus must ensure it resumes with |
| 337 | * the same slot that it booted from. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 338 | if (CONFIG(RESUME_PATH_SAME_AS_BOOT) && |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 339 | vboot_platform_is_resuming()) |
Duncan Laurie | 1cdacca | 2016-02-19 20:26:07 -0800 | [diff] [blame] | 340 | ctx.flags |= VB2_CONTEXT_S3_RESUME; |
| 341 | |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 342 | /* Read secdata from TPM. Initialize TPM if secdata not found. We don't |
| 343 | * check the return value here because vb2api_fw_phase1 will catch |
| 344 | * invalid secdata and tell us what to do (=reboot). */ |
| 345 | timestamp_add_now(TS_START_TPMINIT); |
Joel Kitching | 2a20d13 | 2019-08-08 14:20:26 +0800 | [diff] [blame] | 346 | if (vboot_setup_tpm(&ctx) == TPM_SUCCESS) |
Christian Walter | 0bd84ed | 2019-07-23 10:26:30 +0200 | [diff] [blame] | 347 | antirollback_read_space_firmware(&ctx); |
Duncan Laurie | a613a31 | 2016-03-14 09:32:08 -0700 | [diff] [blame] | 348 | timestamp_add_now(TS_END_TPMINIT); |
| 349 | |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 350 | /* Enable measured boot mode */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 351 | if (CONFIG(VBOOT_MEASURED_BOOT) && |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 352 | !(ctx.flags & VB2_CONTEXT_S3_RESUME)) { |
| 353 | if (vboot_init_crtm() != VB2_SUCCESS) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 354 | die_with_post_code(POST_INVALID_ROM, |
| 355 | "Initializing measured boot mode failed!"); |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 358 | if (get_recovery_mode_switch()) { |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 359 | ctx.flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 360 | if (CONFIG(VBOOT_DISABLE_DEV_ON_RECOVERY)) |
Joel Kitching | ba5ae5b | 2019-01-11 19:30:42 +0800 | [diff] [blame] | 361 | ctx.flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 362 | } |
| 363 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 364 | if (CONFIG(VBOOT_WIPEOUT_SUPPORTED) && |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 365 | get_wipeout_mode_switch()) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 366 | ctx.flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE; |
| 367 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 368 | if (CONFIG(VBOOT_LID_SWITCH) && !get_lid_switch()) |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 369 | ctx.flags |= VB2_CONTEXT_NOFAIL_BOOT; |
| 370 | |
Joel Kitching | 5923d67 | 2019-04-12 15:57:43 +0800 | [diff] [blame] | 371 | /* Mainboard/SoC always initializes display. */ |
| 372 | if (!CONFIG(VBOOT_MUST_REQUEST_DISPLAY)) |
| 373 | ctx.flags |= VB2_CONTEXT_DISPLAY_INIT; |
| 374 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 375 | /* Do early init (set up secdata and NVRAM, load GBB) */ |
| 376 | printk(BIOS_INFO, "Phase 1\n"); |
| 377 | rv = vb2api_fw_phase1(&ctx); |
| 378 | |
Eric Lai | b6ee0569 | 2019-05-21 16:55:14 +0800 | [diff] [blame] | 379 | /* Jot down some information from vboot which may be required later on |
| 380 | in coreboot boot flow. */ |
| 381 | if (ctx.flags & VB2_CONTEXT_DISPLAY_INIT) |
| 382 | /* Mainboard/SoC should initialize display. */ |
| 383 | vboot_get_working_data()->flags |= VBOOT_WD_FLAG_DISPLAY_INIT; |
| 384 | if (ctx.flags & VB2_CONTEXT_DEVELOPER_MODE) |
| 385 | vboot_get_working_data()->flags |= VBOOT_WD_FLAG_DEVELOPER_MODE; |
| 386 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 387 | if (rv) { |
| 388 | /* |
| 389 | * If vb2api_fw_phase1 fails, check for return value. |
| 390 | * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue |
| 391 | * into recovery mode. |
| 392 | * For any other error code, save context if needed and reboot. |
| 393 | */ |
| 394 | if (rv == VB2_ERROR_API_PHASE1_RECOVERY) { |
| 395 | printk(BIOS_INFO, "Recovery requested (%x)\n", rv); |
| 396 | save_if_needed(&ctx); |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 397 | extend_pcrs(&ctx); /* ignore failures */ |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 398 | goto verstage_main_exit; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 399 | } |
| 400 | |
Raul E Rangel | 3a59174 | 2018-07-17 14:44:43 -0600 | [diff] [blame] | 401 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 402 | save_if_needed(&ctx); |
| 403 | vboot_reboot(); |
| 404 | } |
| 405 | |
| 406 | /* Determine which firmware slot to boot (based on NVRAM) */ |
| 407 | printk(BIOS_INFO, "Phase 2\n"); |
| 408 | rv = vb2api_fw_phase2(&ctx); |
| 409 | if (rv) { |
| 410 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 411 | save_if_needed(&ctx); |
| 412 | vboot_reboot(); |
| 413 | } |
| 414 | |
| 415 | /* Try that slot (verify its keyblock and preamble) */ |
| 416 | printk(BIOS_INFO, "Phase 3\n"); |
| 417 | timestamp_add_now(TS_START_VERIFY_SLOT); |
| 418 | rv = vb2api_fw_phase3(&ctx); |
| 419 | timestamp_add_now(TS_END_VERIFY_SLOT); |
| 420 | if (rv) { |
| 421 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 422 | save_if_needed(&ctx); |
| 423 | vboot_reboot(); |
| 424 | } |
| 425 | |
| 426 | printk(BIOS_INFO, "Phase 4\n"); |
| 427 | rv = locate_firmware(&ctx, &fw_main); |
| 428 | if (rv) |
Keith Short | 7006458 | 2019-05-06 16:12:57 -0600 | [diff] [blame] | 429 | die_with_post_code(POST_INVALID_ROM, |
| 430 | "Failed to read FMAP to locate firmware"); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 431 | |
| 432 | rv = hash_body(&ctx, &fw_main); |
| 433 | save_if_needed(&ctx); |
| 434 | if (rv) { |
| 435 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 436 | vboot_reboot(); |
| 437 | } |
| 438 | |
Joel Kitching | 6d88a5d | 2018-10-12 15:23:31 +0800 | [diff] [blame] | 439 | /* Only extend PCRs once on boot. */ |
| 440 | if (!(ctx.flags & VB2_CONTEXT_S3_RESUME)) { |
| 441 | timestamp_add_now(TS_START_TPMPCR); |
| 442 | rv = extend_pcrs(&ctx); |
| 443 | if (rv) { |
| 444 | printk(BIOS_WARNING, |
| 445 | "Failed to extend TPM PCRs (%#x)\n", rv); |
| 446 | vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv); |
| 447 | save_if_needed(&ctx); |
| 448 | vboot_reboot(); |
| 449 | } |
| 450 | timestamp_add_now(TS_END_TPMPCR); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* Lock TPM */ |
Raul E Rangel | 4c518e1 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 454 | |
| 455 | timestamp_add_now(TS_START_TPMLOCK); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 456 | rv = antirollback_lock_space_firmware(); |
| 457 | if (rv) { |
| 458 | printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv); |
| 459 | vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0); |
| 460 | save_if_needed(&ctx); |
| 461 | vboot_reboot(); |
| 462 | } |
Raul E Rangel | 4c518e1 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 463 | timestamp_add_now(TS_END_TPMLOCK); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 464 | |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 465 | /* Lock rec hash space if available. */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 466 | if (CONFIG(VBOOT_HAS_REC_HASH_SPACE)) { |
Furquan Shaikh | b038f41 | 2016-11-07 23:47:11 -0800 | [diff] [blame] | 467 | rv = antirollback_lock_space_rec_hash(); |
| 468 | if (rv) { |
| 469 | printk(BIOS_INFO, "Failed to lock rec hash space(%x)\n", |
| 470 | rv); |
| 471 | vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_REC_HASH_L_ERROR, |
| 472 | 0); |
| 473 | save_if_needed(&ctx); |
| 474 | vboot_reboot(); |
| 475 | } |
| 476 | } |
| 477 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 478 | printk(BIOS_INFO, "Slot %c is selected\n", is_slot_a(&ctx) ? 'A' : 'B'); |
Joel Kitching | af8471c | 2019-03-13 22:38:07 +0800 | [diff] [blame] | 479 | vboot_set_selected_region(region_device_region(&fw_main)); |
Joel Kitching | ba50e48 | 2019-06-10 17:37:37 +0800 | [diff] [blame] | 480 | |
| 481 | verstage_main_exit: |
Joel Kitching | 532e0c7 | 2019-06-16 17:23:03 +0800 | [diff] [blame] | 482 | /* If CBMEM is not up yet, let the ROMSTAGE_CBMEM_INIT_HOOK take care |
| 483 | of running this function. */ |
| 484 | if (ENV_ROMSTAGE && CONFIG(VBOOT_STARTS_IN_ROMSTAGE)) |
| 485 | vboot_log_and_clear_recovery_mode_switch(0); |
| 486 | |
Joel Kitching | 7b10deb | 2019-06-17 15:22:28 +0800 | [diff] [blame] | 487 | /* Save recovery reason in case of unexpected reboots on x86. */ |
| 488 | vboot_save_recovery_reason_vbnv(); |
| 489 | |
Joel Kitching | af8471c | 2019-03-13 22:38:07 +0800 | [diff] [blame] | 490 | vboot_finalize_work_context(&ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 491 | timestamp_add_now(TS_END_VBOOT); |
| 492 | } |