[coreboot-gerrit] Patch set updated for coreboot: drivers/intel: Add support for recovery MRC space in TPM

Furquan Shaikh (furquan@google.com) gerrit at coreboot.org
Tue Nov 8 09:39:01 CET 2016


Furquan Shaikh (furquan at google.com) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/17274

-gerrit

commit 4015255bf55de1b91175e43a044bd062ce1fd595
Author: Furquan Shaikh <furquan at chromium.org>
Date:   Mon Nov 7 23:57:48 2016 -0800

    drivers/intel: Add support for recovery MRC space in TPM
    
    This space is read/updated only in recovery mode.
    1. During read phase, verify if the hash of MRC data read from
    RECOVERY_MRC_CACHE matches the hash stored in TPM.
    2. During update phase, calculate hash of training data returned by MRC
    and save it in TPM.
    
    BUG=chrome-os-partner:59355
    BRANCH=None
    TEST=Verified MRC data hash comparison and update operation on reef.
    
    Change-Id: Ifcbbf1bd22033767625ec55b659e05fa7a7afc16
    Signed-off-by: Furquan Shaikh <furquan at chromium.org>
---
 src/drivers/intel/fsp2_0/memory_init.c | 106 +++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)

diff --git a/src/drivers/intel/fsp2_0/memory_init.c b/src/drivers/intel/fsp2_0/memory_init.c
index 3ea1897..240adb1 100644
--- a/src/drivers/intel/fsp2_0/memory_init.c
+++ b/src/drivers/intel/fsp2_0/memory_init.c
@@ -11,6 +11,8 @@
  * (at your option) any later version.
  */
 
+#include <antirollback.h>
+#include <arch/early_variables.h>
 #include <arch/io.h>
 #include <arch/cpu.h>
 #include <arch/symbols.h>
@@ -28,7 +30,60 @@
 #include <string.h>
 #include <symbols.h>
 #include <timestamp.h>
+#include <tpm_lite/tlcl.h>
 #include <vboot/vboot_common.h>
+#include <vb2_api.h>
+
+static uint8_t tpm_init_done CAR_GLOBAL;
+
+static int mrc_cache_tpm_init(void)
+{
+	uint8_t done = car_get_var(tpm_init_done);
+
+	if (done)
+		return 0;
+
+	if (tlcl_lib_init() != VB2_SUCCESS)
+		return -1;
+
+	car_set_var(tpm_init_done, 1);
+	return 0;
+}
+
+static void mrc_cache_update_tpm_hash(const uint8_t *data, size_t size)
+{
+	uint8_t data_hash[VB2_SHA256_DIGEST_SIZE];
+
+	/* We do not store normal mode data hash in TPM. */
+	if (!vboot_recovery_mode_enabled())
+		return;
+
+	/* Bail out early if no mrc hash space is supported in TPM. */
+	if (!IS_ENABLED(CONFIG_VBOOT_HAS_REC_MRC_SPACE))
+		return;
+
+	/* Initialize TPM driver. */
+	if (mrc_cache_tpm_init()) {
+		printk(BIOS_ERR, "MRC: TPM driver initialization failed.\n");
+		return;
+	}
+
+	/* Calculate hash of data generated by MRC. */
+	if (vb2_digest_buffer(data, size, VB2_HASH_SHA256, data_hash,
+			      sizeof(data_hash))) {
+		printk(BIOS_ERR, "MRC: SHA-256 calculation failed for data.\n");
+		memset(data_hash, 0, sizeof(data_hash));
+	}
+
+	/* Write hash of data to TPM space. */
+	if (antirollback_write_space_rec_mrc(data_hash, sizeof(data_hash))
+	    != TPM_SUCCESS) {
+		printk(BIOS_ERR, "MRC: Could not save hash to TPM.\n");
+		return;
+	}
+
+	printk(BIOS_INFO, "MRC: TPM MRC hash updated successfully.\n");
+}
 
 static void save_memory_training_data(bool s3wake, uint32_t fsp_version)
 {
@@ -53,6 +108,8 @@ static void save_memory_training_data(bool s3wake, uint32_t fsp_version)
 	if (mrc_cache_stash_data_with_version(mrc_data, mrc_data_size,
 						fsp_version) < 0)
 			printk(BIOS_ERR, "Failed to stash MRC data\n");
+
+	mrc_cache_update_tpm_hash(mrc_data, mrc_data_size);
 }
 
 static void do_fsp_post_memory_init(bool s3wake, uint32_t fsp_version)
@@ -110,6 +167,52 @@ static const char *mrc_cache_get_region_name(void)
 	return RECOVERY_MRC_CACHE;
 }
 
+static int mrc_cache_verify_tpm_hash(const uint8_t *data, size_t size)
+{
+	uint8_t data_hash[VB2_SHA256_DIGEST_SIZE];
+	uint8_t tpm_hash[VB2_SHA256_DIGEST_SIZE];
+
+	/* We do not store normal mode data hash in TPM. */
+	if (!vboot_recovery_mode_enabled())
+		return 1;
+
+	/*
+	 * If hash data storage in TPM is not supported, accept the data from
+	 * RECOVERY_MRC_CACHE.
+	 */
+	if (!IS_ENABLED(CONFIG_VBOOT_HAS_REC_MRC_SPACE))
+		return 1;
+
+	/* Calculate hash of data read from RECOVERY_MRC_CACHE. */
+	if (vb2_digest_buffer(data, size, VB2_HASH_SHA256, data_hash,
+			      sizeof(data_hash))) {
+		printk(BIOS_ERR, "MRC: SHA-256 calculation failed for data.\n");
+		return 0;
+	}
+
+	/* Initialize TPM driver. */
+	if (mrc_cache_tpm_init()) {
+		printk(BIOS_ERR, "MRC: TPM driver initialization failed.\n");
+		return 0;
+	}
+
+	/* Read hash of MRC data saved in TPM. */
+	if (antirollback_read_space_rec_mrc(tpm_hash, sizeof(tpm_hash))
+	    != TPM_SUCCESS) {
+		printk(BIOS_ERR, "MRC: Could not read hash from TPM.\n");
+		return 0;
+	}
+
+	if (memcmp(tpm_hash, data_hash, sizeof(tpm_hash))) {
+		printk(BIOS_ERR, "MRC: Hash comparison failed.\n");
+		return 0;
+	}
+
+	printk(BIOS_INFO, "MRC: Hash comparison successful. "
+	       "Using data from RECOVERY_MRC_CACHE\n");
+	return 1;
+}
+
 static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, bool s3wake,
 				uint32_t fsp_version)
 {
@@ -126,6 +229,9 @@ static void fsp_fill_mrc_cache(FSPM_ARCH_UPD *arch_upd, bool s3wake,
 	if (mrc_cache_get_current_from_region(&mrc_cache, fsp_version, name))
 		return;
 
+	if (!mrc_cache_verify_tpm_hash(mrc_cache->data, mrc_cache->size))
+		return;
+
 	/* MRC cache found */
 	arch_upd->NvsBufferPtr = (void *)mrc_cache->data;
 	arch_upd->BootMode = s3wake ?



More information about the coreboot-gerrit mailing list