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 | |
| 16 | #include <antirollback.h> |
| 17 | #include <arch/exception.h> |
| 18 | #include <assert.h> |
| 19 | #include <console/console.h> |
| 20 | #include <console/vtxprintf.h> |
| 21 | #include <delay.h> |
| 22 | #include <string.h> |
| 23 | #include <timestamp.h> |
| 24 | #include <vb2_api.h> |
| 25 | |
| 26 | #include "../chromeos.h" |
| 27 | #include "misc.h" |
| 28 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 29 | /* The max hash size to expect is for SHA512. */ |
| 30 | #define VBOOT_MAX_HASH_SIZE VB2_SHA512_DIGEST_SIZE |
| 31 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 32 | #define TODO_BLOCK_SIZE 1024 |
| 33 | |
| 34 | static int is_slot_a(struct vb2_context *ctx) |
| 35 | { |
| 36 | return !(ctx->flags & VB2_CONTEXT_FW_SLOT_B); |
| 37 | } |
| 38 | |
| 39 | /* exports */ |
| 40 | |
| 41 | void vb2ex_printf(const char *func, const char *fmt, ...) |
| 42 | { |
| 43 | va_list args; |
| 44 | |
| 45 | printk(BIOS_INFO, "VB2:%s() ", func); |
| 46 | va_start(args, fmt); |
| 47 | do_printk_va_list(BIOS_INFO, fmt, args); |
| 48 | va_end(args); |
| 49 | |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | int vb2ex_tpm_clear_owner(struct vb2_context *ctx) |
| 54 | { |
| 55 | uint32_t rv; |
| 56 | printk(BIOS_INFO, "Clearing TPM owner\n"); |
| 57 | rv = tpm_clear_and_reenable(); |
| 58 | if (rv) |
| 59 | return VB2_ERROR_EX_TPM_CLEAR_OWNER; |
| 60 | return VB2_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | int vb2ex_read_resource(struct vb2_context *ctx, |
| 64 | enum vb2_resource_index index, |
| 65 | uint32_t offset, |
| 66 | void *buf, |
| 67 | uint32_t size) |
| 68 | { |
| 69 | struct region_device rdev; |
| 70 | const char *name; |
| 71 | |
| 72 | switch (index) { |
| 73 | case VB2_RES_GBB: |
| 74 | name = "GBB"; |
| 75 | break; |
| 76 | case VB2_RES_FW_VBLOCK: |
| 77 | if (is_slot_a(ctx)) |
| 78 | name = "VBLOCK_A"; |
| 79 | else |
| 80 | name = "VBLOCK_B"; |
| 81 | break; |
| 82 | default: |
| 83 | return VB2_ERROR_EX_READ_RESOURCE_INDEX; |
| 84 | } |
| 85 | |
| 86 | if (vboot_named_region_device(name, &rdev)) |
| 87 | return VB2_ERROR_EX_READ_RESOURCE_SIZE; |
| 88 | |
| 89 | if (rdev_readat(&rdev, buf, offset, size) != size) |
| 90 | return VB2_ERROR_EX_READ_RESOURCE_SIZE; |
| 91 | |
| 92 | return VB2_SUCCESS; |
| 93 | } |
| 94 | |
| 95 | /* No-op stubs that can be overridden by SoCs with hardware crypto support. */ |
| 96 | __attribute__((weak)) |
| 97 | int vb2ex_hwcrypto_digest_init(enum vb2_hash_algorithm hash_alg, |
| 98 | uint32_t data_size) |
| 99 | { |
| 100 | return VB2_ERROR_EX_HWCRYPTO_UNSUPPORTED; |
| 101 | } |
| 102 | |
| 103 | __attribute__((weak)) |
| 104 | int vb2ex_hwcrypto_digest_extend(const uint8_t *buf, uint32_t size) |
| 105 | { |
| 106 | BUG(); /* Should never get called if init() returned an error. */ |
| 107 | return VB2_ERROR_UNKNOWN; |
| 108 | } |
| 109 | |
| 110 | __attribute__((weak)) |
| 111 | int vb2ex_hwcrypto_digest_finalize(uint8_t *digest, uint32_t digest_size) |
| 112 | { |
| 113 | BUG(); /* Should never get called if init() returned an error. */ |
| 114 | return VB2_ERROR_UNKNOWN; |
| 115 | } |
| 116 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 117 | static int handle_digest_result(void *slot_hash, size_t slot_hash_sz) |
| 118 | { |
| 119 | int is_resume; |
| 120 | |
| 121 | /* |
| 122 | * Nothing to do since resuming on the platform doesn't require |
| 123 | * vboot verification again. |
| 124 | */ |
| 125 | if (!IS_ENABLED(CONFIG_RESUME_PATH_SAME_AS_BOOT)) |
| 126 | return 0; |
| 127 | |
| 128 | /* |
| 129 | * Assume that if vboot doesn't start in bootblock verified |
| 130 | * RW memory init code is not employed. i.e. memory init code |
| 131 | * lives in RO CBFS. |
| 132 | */ |
| 133 | if (!IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK)) |
| 134 | return 0; |
| 135 | |
| 136 | is_resume = vboot_platform_is_resuming(); |
| 137 | |
| 138 | if (is_resume > 0) { |
| 139 | uint8_t saved_hash[VBOOT_MAX_HASH_SIZE]; |
| 140 | const size_t saved_hash_sz = sizeof(saved_hash); |
| 141 | |
| 142 | assert(slot_hash_sz == saved_hash_sz); |
| 143 | |
| 144 | printk(BIOS_DEBUG, "Platform is resuming.\n"); |
| 145 | |
| 146 | if (vboot_retrieve_hash(saved_hash, saved_hash_sz)) { |
| 147 | printk(BIOS_ERR, "Couldn't retrieve saved hash.\n"); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | if (memcmp(saved_hash, slot_hash, slot_hash_sz)) { |
| 152 | printk(BIOS_ERR, "Hash mismatch on resume.\n"); |
| 153 | return -1; |
| 154 | } |
| 155 | } else if (is_resume < 0) |
| 156 | printk(BIOS_ERR, "Unable to determine if platform resuming.\n"); |
| 157 | |
| 158 | printk(BIOS_DEBUG, "Saving vboot hash.\n"); |
| 159 | |
| 160 | /* Always save the hash for the current boot. */ |
| 161 | if (vboot_save_hash(slot_hash, slot_hash_sz)) { |
| 162 | printk(BIOS_ERR, "Error saving vboot hash.\n"); |
| 163 | /* Though this is an error don't report it up since it could |
| 164 | * lead to a reboot loop. The consequence of this is that |
| 165 | * we will most likely fail resuming because of EC issues or |
| 166 | * the hash digest not matching. */ |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 173 | static int hash_body(struct vb2_context *ctx, struct region_device *fw_main) |
| 174 | { |
| 175 | uint64_t load_ts; |
| 176 | uint32_t expected_size; |
| 177 | uint8_t block[TODO_BLOCK_SIZE]; |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 178 | uint8_t hash_digest[VBOOT_MAX_HASH_SIZE]; |
| 179 | const size_t hash_digest_sz = sizeof(hash_digest); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 180 | size_t block_size = sizeof(block); |
| 181 | size_t offset; |
| 182 | int rv; |
| 183 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 184 | /* Clear the full digest so that any hash digests less than the |
| 185 | * max have trailing zeros. */ |
| 186 | memset(hash_digest, 0, hash_digest_sz); |
| 187 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 188 | /* |
| 189 | * Since loading the firmware and calculating its hash is intertwined, |
| 190 | * we use this little trick to measure them separately and pretend it |
| 191 | * was first loaded and then hashed in one piece with the timestamps. |
| 192 | * (This split won't make sense with memory-mapped media like on x86.) |
| 193 | */ |
| 194 | load_ts = timestamp_get(); |
| 195 | timestamp_add(TS_START_HASH_BODY, load_ts); |
| 196 | |
| 197 | expected_size = region_device_sz(fw_main); |
| 198 | offset = 0; |
| 199 | |
| 200 | /* Start the body hash */ |
| 201 | rv = vb2api_init_hash(ctx, VB2_HASH_TAG_FW_BODY, &expected_size); |
| 202 | if (rv) |
| 203 | return rv; |
| 204 | |
Aaron Durbin | 4121f9e | 2016-01-27 14:23:17 -0600 | [diff] [blame] | 205 | /* |
| 206 | * Honor vboot's RW slot size. The expected size is pulled out of |
| 207 | * the preamble and obtained through vb2api_init_hash() above. By |
| 208 | * creating sub region the RW slot portion of the boot media is |
| 209 | * limited. |
| 210 | */ |
| 211 | if (rdev_chain(fw_main, fw_main, 0, expected_size)) { |
| 212 | printk(BIOS_ERR, "Unable to restrict CBFS size.\n"); |
| 213 | return VB2_ERROR_UNKNOWN; |
| 214 | } |
| 215 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 216 | /* Extend over the body */ |
| 217 | while (expected_size) { |
| 218 | uint64_t temp_ts; |
| 219 | if (block_size > expected_size) |
| 220 | block_size = expected_size; |
| 221 | |
| 222 | temp_ts = timestamp_get(); |
| 223 | if (rdev_readat(fw_main, block, offset, block_size) < 0) |
| 224 | return VB2_ERROR_UNKNOWN; |
| 225 | load_ts += timestamp_get() - temp_ts; |
| 226 | |
| 227 | rv = vb2api_extend_hash(ctx, block, block_size); |
| 228 | if (rv) |
| 229 | return rv; |
| 230 | |
| 231 | expected_size -= block_size; |
| 232 | offset += block_size; |
| 233 | } |
| 234 | |
| 235 | timestamp_add(TS_DONE_LOADING, load_ts); |
| 236 | timestamp_add_now(TS_DONE_HASHING); |
| 237 | |
| 238 | /* Check the result (with RSA signature verification) */ |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 239 | rv = vb2api_check_hash_get_digest(ctx, hash_digest, hash_digest_sz); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 240 | if (rv) |
| 241 | return rv; |
| 242 | |
| 243 | timestamp_add_now(TS_END_HASH_BODY); |
| 244 | |
Aaron Durbin | 87c9fae | 2016-01-22 15:26:04 -0600 | [diff] [blame] | 245 | if (handle_digest_result(hash_digest, hash_digest_sz)) |
| 246 | return VB2_ERROR_UNKNOWN; |
| 247 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 248 | return VB2_SUCCESS; |
| 249 | } |
| 250 | |
| 251 | static int locate_firmware(struct vb2_context *ctx, |
| 252 | struct region_device *fw_main) |
| 253 | { |
| 254 | const char *name; |
| 255 | |
| 256 | if (is_slot_a(ctx)) |
| 257 | name = "FW_MAIN_A"; |
| 258 | else |
| 259 | name = "FW_MAIN_B"; |
| 260 | |
| 261 | return vboot_named_region_device(name, fw_main); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Save non-volatile and/or secure data if needed. |
| 266 | */ |
| 267 | static void save_if_needed(struct vb2_context *ctx) |
| 268 | { |
| 269 | if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) { |
| 270 | printk(BIOS_INFO, "Saving nvdata\n"); |
| 271 | save_vbnv(ctx->nvdata); |
| 272 | ctx->flags &= ~VB2_CONTEXT_NVDATA_CHANGED; |
| 273 | } |
| 274 | if (ctx->flags & VB2_CONTEXT_SECDATA_CHANGED) { |
| 275 | printk(BIOS_INFO, "Saving secdata\n"); |
| 276 | antirollback_write_space_firmware(ctx); |
| 277 | ctx->flags &= ~VB2_CONTEXT_SECDATA_CHANGED; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | static uint32_t extend_pcrs(struct vb2_context *ctx) |
| 282 | { |
| 283 | return tpm_extend_pcr(ctx, 0, BOOT_MODE_PCR) || |
| 284 | tpm_extend_pcr(ctx, 1, HWID_DIGEST_PCR); |
| 285 | } |
| 286 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 287 | /** |
| 288 | * Verify and select the firmware in the RW image |
| 289 | * |
| 290 | * TODO: Avoid loading a stage twice (once in hash_body & again in load_stage). |
| 291 | * when per-stage verification is ready. |
| 292 | */ |
| 293 | void verstage_main(void) |
| 294 | { |
| 295 | struct vb2_context ctx; |
| 296 | struct region_device fw_main; |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 297 | int rv; |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 298 | |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 299 | timestamp_add_now(TS_START_VBOOT); |
| 300 | |
| 301 | /* Set up context and work buffer */ |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 302 | vb2_init_work_context(&ctx); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 303 | |
| 304 | /* Read nvdata from a non-volatile storage */ |
| 305 | read_vbnv(ctx.nvdata); |
| 306 | |
| 307 | /* Read secdata from TPM. Initialize TPM if secdata not found. We don't |
| 308 | * check the return value here because vb2api_fw_phase1 will catch |
| 309 | * invalid secdata and tell us what to do (=reboot). */ |
| 310 | timestamp_add_now(TS_START_TPMINIT); |
| 311 | antirollback_read_space_firmware(&ctx); |
| 312 | timestamp_add_now(TS_END_TPMINIT); |
| 313 | |
| 314 | if (!IS_ENABLED(CONFIG_VIRTUAL_DEV_SWITCH) && |
| 315 | get_developer_mode_switch()) |
| 316 | ctx.flags |= VB2_CONTEXT_FORCE_DEVELOPER_MODE; |
| 317 | |
| 318 | if (get_recovery_mode_switch()) { |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 319 | ctx.flags |= VB2_CONTEXT_FORCE_RECOVERY_MODE; |
| 320 | if (IS_ENABLED(CONFIG_VBOOT_DISABLE_DEV_ON_RECOVERY)) |
| 321 | ctx.flags |= VB2_DISABLE_DEVELOPER_MODE; |
| 322 | } |
| 323 | |
| 324 | if (IS_ENABLED(CONFIG_WIPEOUT_SUPPORTED) && get_wipeout_mode_switch()) |
| 325 | ctx.flags |= VB2_CONTEXT_FORCE_WIPEOUT_MODE; |
| 326 | |
| 327 | if (IS_ENABLED(CONFIG_LID_SWITCH) && !get_lid_switch()) |
| 328 | ctx.flags |= VB2_CONTEXT_NOFAIL_BOOT; |
| 329 | |
| 330 | /* Do early init (set up secdata and NVRAM, load GBB) */ |
| 331 | printk(BIOS_INFO, "Phase 1\n"); |
| 332 | rv = vb2api_fw_phase1(&ctx); |
| 333 | |
| 334 | if (rv) { |
| 335 | /* |
| 336 | * If vb2api_fw_phase1 fails, check for return value. |
| 337 | * If it is set to VB2_ERROR_API_PHASE1_RECOVERY, then continue |
| 338 | * into recovery mode. |
| 339 | * For any other error code, save context if needed and reboot. |
| 340 | */ |
| 341 | if (rv == VB2_ERROR_API_PHASE1_RECOVERY) { |
| 342 | printk(BIOS_INFO, "Recovery requested (%x)\n", rv); |
| 343 | save_if_needed(&ctx); |
| 344 | extend_pcrs(&ctx); /* ignore failures */ |
| 345 | timestamp_add_now(TS_END_VBOOT); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | printk(BIOS_INFO, "Reboot reqested (%x)\n", rv); |
| 350 | save_if_needed(&ctx); |
| 351 | vboot_reboot(); |
| 352 | } |
| 353 | |
| 354 | /* Determine which firmware slot to boot (based on NVRAM) */ |
| 355 | printk(BIOS_INFO, "Phase 2\n"); |
| 356 | rv = vb2api_fw_phase2(&ctx); |
| 357 | if (rv) { |
| 358 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 359 | save_if_needed(&ctx); |
| 360 | vboot_reboot(); |
| 361 | } |
| 362 | |
| 363 | /* Try that slot (verify its keyblock and preamble) */ |
| 364 | printk(BIOS_INFO, "Phase 3\n"); |
| 365 | timestamp_add_now(TS_START_VERIFY_SLOT); |
| 366 | rv = vb2api_fw_phase3(&ctx); |
| 367 | timestamp_add_now(TS_END_VERIFY_SLOT); |
| 368 | if (rv) { |
| 369 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 370 | save_if_needed(&ctx); |
| 371 | vboot_reboot(); |
| 372 | } |
| 373 | |
| 374 | printk(BIOS_INFO, "Phase 4\n"); |
| 375 | rv = locate_firmware(&ctx, &fw_main); |
| 376 | if (rv) |
| 377 | die("Failed to read FMAP to locate firmware"); |
| 378 | |
| 379 | rv = hash_body(&ctx, &fw_main); |
| 380 | save_if_needed(&ctx); |
| 381 | if (rv) { |
| 382 | printk(BIOS_INFO, "Reboot requested (%x)\n", rv); |
| 383 | vboot_reboot(); |
| 384 | } |
| 385 | |
| 386 | rv = extend_pcrs(&ctx); |
| 387 | if (rv) { |
| 388 | printk(BIOS_WARNING, "Failed to extend TPM PCRs (%#x)\n", rv); |
| 389 | vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_U_ERROR, rv); |
| 390 | save_if_needed(&ctx); |
| 391 | vboot_reboot(); |
| 392 | } |
| 393 | |
| 394 | /* Lock TPM */ |
| 395 | rv = antirollback_lock_space_firmware(); |
| 396 | if (rv) { |
| 397 | printk(BIOS_INFO, "Failed to lock TPM (%x)\n", rv); |
| 398 | vb2api_fail(&ctx, VB2_RECOVERY_RO_TPM_L_ERROR, 0); |
| 399 | save_if_needed(&ctx); |
| 400 | vboot_reboot(); |
| 401 | } |
| 402 | |
| 403 | printk(BIOS_INFO, "Slot %c is selected\n", is_slot_a(&ctx) ? 'A' : 'B'); |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 404 | vb2_set_selected_region(region_device_region(&fw_main)); |
Aaron Durbin | 588ad7b | 2015-09-29 17:56:59 -0500 | [diff] [blame] | 405 | timestamp_add_now(TS_END_VBOOT); |
| 406 | } |