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