| OLD | NEW |
| 1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Functions for loading a kernel from disk. | 5 * Functions for loading a kernel from disk. |
| 6 * (Firmware portion) | 6 * (Firmware portion) |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "vboot_kernel.h" | 9 #include "vboot_kernel.h" |
| 10 | 10 |
| 11 #include "boot_device.h" | 11 #include "boot_device.h" |
| 12 #include "cgptlib.h" | 12 #include "cgptlib.h" |
| 13 #include "cgptlib_internal.h" | 13 #include "cgptlib_internal.h" |
| 14 #include "gbb_header.h" | 14 #include "gbb_header.h" |
| 15 #include "load_kernel_fw.h" | 15 #include "load_kernel_fw.h" |
| 16 #include "rollback_index.h" | 16 #include "rollback_index.h" |
| 17 #include "utility.h" | 17 #include "utility.h" |
| 18 #include "vboot_common.h" | 18 #include "vboot_common.h" |
| 19 | 19 |
| 20 #define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */ | 20 #define KBUF_SIZE 65536 /* Bytes to read at start of kernel partition */ |
| 21 #define LOWEST_TPM_VERSION 0xffffffff |
| 21 | 22 |
| 22 typedef enum BootMode { | 23 typedef enum BootMode { |
| 23 kBootNormal, /* Normal firmware */ | 24 kBootNormal, /* Normal firmware */ |
| 24 kBootDev, /* Dev firmware AND dev switch is on */ | 25 kBootDev, /* Dev firmware AND dev switch is on */ |
| 25 kBootRecovery /* Recovery firmware, regardless of dev switch position */ | 26 kBootRecovery /* Recovery firmware, regardless of dev switch position */ |
| 26 } BootMode; | 27 } BootMode; |
| 27 | 28 |
| 28 | 29 |
| 29 /* Allocates and reads GPT data from the drive. The sector_bytes and | 30 /* Allocates and reads GPT data from the drive. The sector_bytes and |
| 30 * drive_sectors fields should be filled on input. The primary and | 31 * drive_sectors fields should be filled on input. The primary and |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 VbPublicKey* kernel_subkey; | 126 VbPublicKey* kernel_subkey; |
| 126 GptData gpt; | 127 GptData gpt; |
| 127 uint64_t part_start, part_size; | 128 uint64_t part_start, part_size; |
| 128 uint64_t blba; | 129 uint64_t blba; |
| 129 uint64_t kbuf_sectors; | 130 uint64_t kbuf_sectors; |
| 130 uint8_t* kbuf = NULL; | 131 uint8_t* kbuf = NULL; |
| 131 int found_partitions = 0; | 132 int found_partitions = 0; |
| 132 int good_partition = -1; | 133 int good_partition = -1; |
| 133 int good_partition_key_block_valid = 0; | 134 int good_partition_key_block_valid = 0; |
| 134 uint32_t tpm_version = 0; | 135 uint32_t tpm_version = 0; |
| 135 uint64_t lowest_version = 0xFFFFFFFF; | 136 uint64_t lowest_version = LOWEST_TPM_VERSION; |
| 136 int rec_switch, dev_switch; | 137 int rec_switch, dev_switch; |
| 137 BootMode boot_mode; | 138 BootMode boot_mode; |
| 138 uint32_t status; | 139 uint32_t status; |
| 139 | 140 |
| 140 /* TODO: differentiate between finding an invalid kernel (found_partitions>0) | 141 /* TODO: differentiate between finding an invalid kernel (found_partitions>0) |
| 141 * and not finding one at all. Right now we treat them the same, and return | 142 * and not finding one at all. Right now we treat them the same, and return |
| 142 * LOAD_KERNEL_INVALID for both. */ | 143 * LOAD_KERNEL_INVALID for both. */ |
| 143 int retval = LOAD_KERNEL_INVALID; | 144 int retval = LOAD_KERNEL_INVALID; |
| 144 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED; | 145 int recovery = VBNV_RECOVERY_RO_UNSPECIFIED; |
| 145 | 146 |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 if (kBootDev != boot_mode) | 350 if (kBootDev != boot_mode) |
| 350 goto bad_kernel; | 351 goto bad_kernel; |
| 351 } | 352 } |
| 352 } | 353 } |
| 353 | 354 |
| 354 VBDEBUG(("Kernel preamble is good.\n")); | 355 VBDEBUG(("Kernel preamble is good.\n")); |
| 355 | 356 |
| 356 /* Check for lowest version from a valid header. */ | 357 /* Check for lowest version from a valid header. */ |
| 357 if (key_block_valid && lowest_version > combined_version) | 358 if (key_block_valid && lowest_version > combined_version) |
| 358 lowest_version = combined_version; | 359 lowest_version = combined_version; |
| 360 else { |
| 361 VBDEBUG(("Key block valid: %d\n", key_block_valid)); |
| 362 VBDEBUG(("Combined version: %" PRIu64 "\n", combined_version)); |
| 363 } |
| 359 | 364 |
| 360 /* If we already have a good kernel, no need to read another | 365 /* If we already have a good kernel, no need to read another |
| 361 * one; we only needed to look at the versions to check for | 366 * one; we only needed to look at the versions to check for |
| 362 * rollback. So skip to the next kernel preamble. */ | 367 * rollback. So skip to the next kernel preamble. */ |
| 363 if (-1 != good_partition) | 368 if (-1 != good_partition) |
| 364 continue; | 369 continue; |
| 365 | 370 |
| 366 /* Verify body load address matches what we expect */ | 371 /* Verify body load address matches what we expect */ |
| 367 if ((preamble->body_load_address != (size_t)params->kernel_buffer) && | 372 if ((preamble->body_load_address != (size_t)params->kernel_buffer) && |
| 368 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) { | 373 !(params->boot_flags & BOOT_FLAG_SKIP_ADDR_CHECK)) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 VBDEBUG(("Good_partition >= 0\n")); | 480 VBDEBUG(("Good_partition >= 0\n")); |
| 476 | 481 |
| 477 /* See if we need to update the TPM */ | 482 /* See if we need to update the TPM */ |
| 478 if (kBootRecovery != boot_mode && good_partition_key_block_valid) { | 483 if (kBootRecovery != boot_mode && good_partition_key_block_valid) { |
| 479 /* We only update the TPM in normal and developer boot modes. In | 484 /* We only update the TPM in normal and developer boot modes. In |
| 480 * developer mode, we only advanced lowest_version for kernels with valid | 485 * developer mode, we only advanced lowest_version for kernels with valid |
| 481 * key blocks, and didn't count self-signed key blocks. In recovery | 486 * key blocks, and didn't count self-signed key blocks. In recovery |
| 482 * mode, the TPM stays PP-unlocked, so anything we write gets blown away | 487 * mode, the TPM stays PP-unlocked, so anything we write gets blown away |
| 483 * by the firmware when we go back to normal mode. */ | 488 * by the firmware when we go back to normal mode. */ |
| 484 VBDEBUG(("Boot_flags = not recovery\n")); | 489 VBDEBUG(("Boot_flags = not recovery\n")); |
| 485 if (lowest_version > tpm_version) { | 490 |
| 491 if ((lowest_version > tpm_version) && |
| 492 (lowest_version != LOWEST_TPM_VERSION)) { |
| 486 status = RollbackKernelWrite((uint32_t)lowest_version); | 493 status = RollbackKernelWrite((uint32_t)lowest_version); |
| 487 if (0 != status) { | 494 if (0 != status) { |
| 488 VBDEBUG(("Error writing kernel versions to TPM.\n")); | 495 VBDEBUG(("Error writing kernel versions to TPM.\n")); |
| 489 if (status == TPM_E_MUST_REBOOT) | 496 if (status == TPM_E_MUST_REBOOT) |
| 490 retval = LOAD_KERNEL_REBOOT; | 497 retval = LOAD_KERNEL_REBOOT; |
| 491 else | 498 else |
| 492 recovery = VBNV_RECOVERY_RW_TPM_ERROR; | 499 recovery = VBNV_RECOVERY_RW_TPM_ERROR; |
| 493 goto LoadKernelExit; | 500 goto LoadKernelExit; |
| 494 } | 501 } |
| 495 } | 502 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 522 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ? | 529 VbNvSet(vnc, VBNV_RECOVERY_REQUEST, LOAD_KERNEL_RECOVERY == retval ? |
| 523 recovery : VBNV_RECOVERY_NOT_REQUESTED); | 530 recovery : VBNV_RECOVERY_NOT_REQUESTED); |
| 524 VbNvTeardown(vnc); | 531 VbNvTeardown(vnc); |
| 525 | 532 |
| 526 /* Store how much shared data we used, if any */ | 533 /* Store how much shared data we used, if any */ |
| 527 if (shared) | 534 if (shared) |
| 528 params->shared_data_size = shared->data_used; | 535 params->shared_data_size = shared->data_used; |
| 529 | 536 |
| 530 return retval; | 537 return retval; |
| 531 } | 538 } |
| OLD | NEW |