Index: firmware/lib/vboot_firmware.c |
diff --git a/firmware/lib/vboot_firmware.c b/firmware/lib/vboot_firmware.c |
index 2f358522a92c2cb5c4956d3ce1bea2369b1cbda6..4be4cb299b05ec396d6279655a20e496cc70d954 100644 |
--- a/firmware/lib/vboot_firmware.c |
+++ b/firmware/lib/vboot_firmware.c |
@@ -9,6 +9,7 @@ |
#include "gbb_header.h" |
#include "load_firmware_fw.h" |
#include "rollback_index.h" |
+#include "tpm_bootmode.h" |
#include "utility.h" |
#include "vboot_common.h" |
#include "vboot_nvstorage.h" |
@@ -34,6 +35,8 @@ void UpdateFirmwareBodyHash(LoadFirmwareParams* params, |
int LoadFirmwareSetup(void) { |
+ /* TODO: handle test errors (requires passing in VbNvContext) */ |
+ /* TODO: record timer values (requires passing in VbSharedData) */ |
/* TODO: start initializing the TPM */ |
return LOAD_FIRMWARE_SUCCESS; |
} |
@@ -50,7 +53,9 @@ int LoadFirmware(LoadFirmwareParams* params) { |
uint32_t tpm_version = 0; |
uint64_t lowest_version = 0xFFFFFFFF; |
uint32_t status; |
+ uint32_t test_err = 0; |
int good_index = -1; |
+ uint64_t boot_fw_keyblock_flags = 0; |
int is_dev; |
int index; |
int i; |
@@ -72,6 +77,28 @@ int LoadFirmware(LoadFirmwareParams* params) { |
recovery = VBNV_RECOVERY_RO_SHARED_DATA; |
goto LoadFirmwareExit; |
} |
+ shared->timer_load_firmware_enter = VbGetTimer(); |
+ |
+ /* Handle test errors */ |
+ VbNvGet(vnc, VBNV_TEST_ERROR_FUNC, &test_err); |
+ if (VBNV_TEST_ERROR_LOAD_FIRMWARE == test_err) { |
+ /* Get error code */ |
+ VbNvGet(vnc, VBNV_TEST_ERROR_NUM, &test_err); |
+ /* Clear test params so we don't repeat the error */ |
+ VbNvSet(vnc, VBNV_TEST_ERROR_FUNC, 0); |
+ VbNvSet(vnc, VBNV_TEST_ERROR_NUM, 0); |
+ /* Handle error codes */ |
+ switch (test_err) { |
+ case LOAD_FIRMWARE_RECOVERY: |
+ recovery = VBNV_RECOVERY_RO_TEST_LF; |
+ goto LoadFirmwareExit; |
+ case LOAD_FIRMWARE_REBOOT: |
+ retval = test_err; |
+ goto LoadFirmwareExit; |
+ default: |
+ break; |
+ } |
+ } |
/* Must have a root key from the GBB */ |
if (!gbb) { |
@@ -82,6 +109,8 @@ int LoadFirmware(LoadFirmwareParams* params) { |
/* Parse flags */ |
is_dev = (params->boot_flags & BOOT_FLAG_DEVELOPER ? 1 : 0); |
+ if (is_dev) |
+ shared->flags |= VBSD_LF_DEV_SWITCH_ON; |
/* Initialize the TPM and read rollback indices. */ |
VBPERFSTART("VB_TPMI"); |
@@ -95,13 +124,16 @@ int LoadFirmware(LoadFirmwareParams* params) { |
recovery = VBNV_RECOVERY_RO_TPM_ERROR; |
goto LoadFirmwareExit; |
} |
+ shared->fw_version_tpm_start = tpm_version; |
+ shared->fw_version_tpm = tpm_version; |
VBPERFEND("VB_TPMI"); |
/* Read try-b count and decrement if necessary */ |
VbNvGet(vnc, VBNV_TRY_B_COUNT, &try_b_count); |
- if (0 != try_b_count) |
+ if (0 != try_b_count) { |
VbNvSet(vnc, VBNV_TRY_B_COUNT, try_b_count - 1); |
- VbNvSet(vnc, VBNV_TRIED_FIRMWARE_B, try_b_count ? 1 : 0); |
+ shared->flags |= VBSD_FWB_TRIED; |
+ } |
/* Allocate our internal data */ |
lfi = (VbLoadFirmwareInternal*)Malloc(sizeof(VbLoadFirmwareInternal)); |
@@ -119,15 +151,18 @@ int LoadFirmware(LoadFirmwareParams* params) { |
uint64_t key_version; |
uint64_t combined_version; |
uint8_t* body_digest; |
+ uint8_t* check_result; |
/* If try B count is non-zero try firmware B first */ |
index = (try_b_count ? 1 - i : i); |
if (0 == index) { |
key_block = (VbKeyBlockHeader*)params->verification_block_0; |
vblock_size = params->verification_size_0; |
+ check_result = &shared->check_fw_a_result; |
} else { |
key_block = (VbKeyBlockHeader*)params->verification_block_1; |
vblock_size = params->verification_size_1; |
+ check_result = &shared->check_fw_b_result; |
} |
/* Check the key block flags against the current boot mode. Do this |
@@ -137,11 +172,13 @@ int LoadFirmware(LoadFirmwareParams* params) { |
(is_dev ? KEY_BLOCK_FLAG_DEVELOPER_1 : |
KEY_BLOCK_FLAG_DEVELOPER_0))) { |
VBDEBUG(("Developer flag mismatch.\n")); |
+ *check_result = VBSD_LF_CHECK_DEV_MISMATCH; |
continue; |
} |
/* RW firmware never runs in recovery mode. */ |
if (!(key_block->key_block_flags & KEY_BLOCK_FLAG_RECOVERY_0)) { |
VBDEBUG(("Recovery flag mismatch.\n")); |
+ *check_result = VBSD_LF_CHECK_REC_MISMATCH; |
continue; |
} |
@@ -149,6 +186,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
VBPERFSTART("VB_VKB"); |
if ((0 != KeyBlockVerify(key_block, vblock_size, root_key, 0))) { |
VBDEBUG(("Key block verification failed.\n")); |
+ *check_result = VBSD_LF_CHECK_VERIFY_KEYBLOCK; |
VBPERFEND("VB_VKB"); |
continue; |
} |
@@ -158,6 +196,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
key_version = key_block->data_key.key_version; |
if (key_version < (tpm_version >> 16)) { |
VBDEBUG(("Key rollback detected.\n")); |
+ *check_result = VBSD_LF_CHECK_KEY_ROLLBACK; |
continue; |
} |
@@ -165,6 +204,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
data_key = PublicKeyToRSA(&key_block->data_key); |
if (!data_key) { |
VBDEBUG(("Unable to parse data key.\n")); |
+ *check_result = VBSD_LF_CHECK_DATA_KEY_PARSE; |
continue; |
} |
@@ -176,6 +216,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
vblock_size - key_block->key_block_size, |
data_key))) { |
VBDEBUG(("Preamble verfication failed.\n")); |
+ *check_result = VBSD_LF_CHECK_VERIFY_PREAMBLE; |
RSAPublicKeyFree(data_key); |
VBPERFEND("VB_VPB"); |
continue; |
@@ -187,10 +228,14 @@ int LoadFirmware(LoadFirmwareParams* params) { |
(preamble->firmware_version & 0xFFFF)); |
if (combined_version < tpm_version) { |
VBDEBUG(("Firmware version rollback detected.\n")); |
+ *check_result = VBSD_LF_CHECK_FW_ROLLBACK; |
RSAPublicKeyFree(data_key); |
continue; |
} |
+ /* Header for this firmware is valid */ |
+ *check_result = VBSD_LF_CHECK_HEADER_VALID; |
+ |
/* Check for lowest key version from a valid header. */ |
if (lowest_version > combined_version) |
lowest_version = combined_version; |
@@ -207,6 +252,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
lfi->body_size_accum = 0; |
if (0 != GetFirmwareBody(params, index)) { |
VBDEBUG(("GetFirmwareBody() failed for index %d\n", index)); |
+ *check_result = VBSD_LF_CHECK_GET_FW_BODY; |
RSAPublicKeyFree(data_key); |
VBPERFEND("VB_RFD"); |
continue; |
@@ -215,6 +261,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
VBDEBUG(("Hash updated %d bytes but expected %d\n", |
(int)lfi->body_size_accum, |
(int)preamble->body_signature.data_size)); |
+ *check_result = VBSD_LF_CHECK_HASH_WRONG_SIZE; |
RSAPublicKeyFree(data_key); |
VBPERFEND("VB_RFD"); |
continue; |
@@ -226,6 +273,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
body_digest = DigestFinal(&lfi->body_digest_context); |
if (0 != VerifyDigest(body_digest, &preamble->body_signature, data_key)) { |
VBDEBUG(("Firmware body verification failed.\n")); |
+ *check_result = VBSD_LF_CHECK_VERIFY_BODY; |
RSAPublicKeyFree(data_key); |
Free(body_digest); |
VBPERFEND("VB_VFD"); |
@@ -239,6 +287,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
/* If we're still here, the firmware is valid. */ |
VBDEBUG(("Firmware %d is valid.\n", index)); |
+ *check_result = VBSD_LF_CHECK_VALID; |
if (-1 == good_index) { |
/* Save the key we actually used */ |
if (0 != VbSharedDataSetKernelKey(shared, &preamble->kernel_subkey)) { |
@@ -251,6 +300,9 @@ int LoadFirmware(LoadFirmwareParams* params) { |
* this firmware. That's the one we'll boot. */ |
good_index = index; |
params->firmware_index = index; |
+ /* Since we now know which firmware to boot, we can update the |
+ * bootable firmware key block mode. */ |
+ boot_fw_keyblock_flags = key_block->key_block_flags; |
/* If the good firmware's key version is the same as the tpm, |
* then the TPM doesn't need updating; we can stop now. |
@@ -261,6 +313,19 @@ int LoadFirmware(LoadFirmwareParams* params) { |
} |
} |
+ /* At this point, we have a good idea of how we are going to boot. Update the |
+ * TPM with this state information. |
+ */ |
+ status = SetTPMBootModeState(is_dev, 0, (int)boot_fw_keyblock_flags); |
+ if (0 != status) { |
+ VBDEBUG(("Unable to update the TPM with boot mode information.\n")); |
+ if (status == TPM_E_MUST_REBOOT) |
+ retval = LOAD_FIRMWARE_REBOOT; |
+ else |
+ recovery = VBNV_RECOVERY_RO_TPM_ERROR; |
+ goto LoadFirmwareExit; |
+ } |
+ |
/* Free internal data */ |
Free(lfi); |
params->load_firmware_internal = NULL; |
@@ -269,6 +334,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
if (good_index >= 0) { |
/* Update TPM if necessary */ |
+ shared->fw_version_lowest = (uint32_t)lowest_version; |
if (lowest_version > tpm_version) { |
VBPERFSTART("VB_TPMU"); |
status = RollbackFirmwareWrite((uint32_t)lowest_version); |
@@ -281,6 +347,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
recovery = VBNV_RECOVERY_RO_TPM_ERROR; |
goto LoadFirmwareExit; |
} |
+ shared->fw_version_tpm = (uint32_t)lowest_version; |
} |
/* Lock firmware versions in TPM */ |
@@ -298,6 +365,7 @@ int LoadFirmware(LoadFirmwareParams* params) { |
/* Success */ |
VBDEBUG(("Will boot firmware index %d\n", (int)params->firmware_index)); |
+ shared->firmware_index = (uint8_t)params->firmware_index; |
retval = LOAD_FIRMWARE_SUCCESS; |
} else { |
/* No good firmware, so go to recovery mode. */ |
@@ -311,6 +379,8 @@ LoadFirmwareExit: |
recovery : VBNV_RECOVERY_NOT_REQUESTED); |
VbNvTeardown(vnc); |
+ shared->timer_load_firmware_exit = VbGetTimer(); |
+ |
/* Note that we don't reduce params->shared_data_size to shared->data_used, |
* since we want to leave space for LoadKernel() to add to the shared data |
* buffer. */ |
@@ -320,6 +390,9 @@ LoadFirmwareExit: |
int S3Resume(void) { |
+ |
+ /* TODO: handle test errors (requires passing in VbNvContext) */ |
+ |
/* Resume the TPM */ |
uint32_t status = RollbackS3Resume(); |