[coreboot-gerrit] New patch to review for coreboot: tpm2: refactor session header marshalling

Martin Roth (martinroth@google.com) gerrit at coreboot.org
Tue Jul 12 22:06:33 CEST 2016


Martin Roth (martinroth at google.com) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/15633

-gerrit

commit f692411533638604ae7ea6bc463a6bcc543ed668
Author: Vadim Bendebury <vbendeb at chromium.org>
Date:   Thu Jul 7 11:04:06 2016 -0700

    tpm2: refactor session header marshalling
    
    For coreboot TPM2 the use case session header is always the minimal
    possible size, the only difference is that some commands require one
    and some require two handles.
    
    Refactor common session header marshalling code into a separate
    function.  This will be useful when more commands marshalling code is
    added.
    
    BRANCH=none
    BUG=chrome-os-partner:50645
    TEST=flashed the TPM and rebooted the device a few times, it
         successfully loaded chrome os on every attempt.
    
    Change-Id: I9b1697c44f67aab32b9cd556b559a55d5050be06
    Signed-off-by: Martin Roth <martinroth at chromium.org>
    Original-Commit-Id: a97a7fa16ceeb484e90e2e1f0573e58a468350b2
    Original-Change-Id: I86e6426be5200f28ebb2174b418254018e81da8e
    Original-Signed-off-by: Vadim Bendebury <vbendeb at chromium.org>
    Original-Reviewed-on: https://chromium-review.googlesource.com/357972
    Original-Reviewed-by: Aaron Durbin <adurbin at chromium.org>
---
 src/lib/tpm2_marshaling.c | 53 +++++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 22 deletions(-)

diff --git a/src/lib/tpm2_marshaling.c b/src/lib/tpm2_marshaling.c
index 63f0e4c..5aad276 100644
--- a/src/lib/tpm2_marshaling.c
+++ b/src/lib/tpm2_marshaling.c
@@ -235,6 +235,28 @@ static void marshal_session_header(void **buffer,
 	marshal_u32(&size_location, base_size - *buffer_space, &base_size);
 }
 
+/*
+ * Common session header can include one or two handles and an empty
+ * session_header structure.
+ */
+static void marshal_common_session_header(void **buffer,
+					  const uint32_t *handles,
+					  size_t handle_count,
+					  size_t *buffer_space)
+{
+	int i;
+	struct tpm2_session_header session_header;
+
+	tpm_tag = TPM_ST_SESSIONS;
+
+	for (i = 0; i < handle_count; i++)
+		marshal_TPM_HANDLE(buffer, handles[i], buffer_space);
+
+	memset(&session_header, 0, sizeof(session_header));
+	session_header.session_handle = TPM_RS_PW;
+	marshal_session_header(buffer, &session_header, buffer_space);
+}
+
 static void marshal_nv_define_space(void **buffer,
 				    struct tpm2_nv_define_space_cmd *nvd_in,
 				    size_t *buffer_space)
@@ -242,14 +264,10 @@ static void marshal_nv_define_space(void **buffer,
 	void *size_location;
 	size_t base_size;
 	size_t sizeof_nv_public_size = sizeof(uint16_t);
-	struct tpm2_session_header session_header;
-
-	marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space);
-	memset(&session_header, 0, sizeof(session_header));
-	session_header.session_handle = TPM_RS_PW;
-	marshal_session_header(buffer, &session_header, buffer_space);
-	tpm_tag = TPM_ST_SESSIONS;
+	const uint32_t handle[] = { TPM_RH_PLATFORM };
 
+	marshal_common_session_header(buffer, handle,
+				      ARRAY_SIZE(handle), buffer_space);
 	marshal_TPM2B(buffer, &nvd_in->auth.b, buffer_space);
 
 	/* This is where the TPMS_NV_PUBLIC size will be stored. */
@@ -277,15 +295,10 @@ static void marshal_nv_write(void **buffer,
 			     struct tpm2_nv_write_cmd *command_body,
 			     size_t *buffer_space)
 {
-	struct tpm2_session_header session_header;
-
-	marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space);
-	marshal_TPM_HANDLE(buffer, command_body->nvIndex, buffer_space);
-	memset(&session_header, 0, sizeof(session_header));
-	session_header.session_handle = TPM_RS_PW;
-	marshal_session_header(buffer, &session_header, buffer_space);
-	tpm_tag = TPM_ST_SESSIONS;
+	uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
 
+	marshal_common_session_header(buffer, handles,
+				      ARRAY_SIZE(handles), buffer_space);
 	marshal_TPM2B(buffer, &command_body->data.b, buffer_space);
 	marshal_u16(buffer, command_body->offset, buffer_space);
 }
@@ -294,14 +307,10 @@ static void marshal_nv_read(void **buffer,
 			    struct tpm2_nv_read_cmd *command_body,
 			    size_t *buffer_space)
 {
-	struct tpm2_session_header session_header;
+	uint32_t handles[] = { TPM_RH_PLATFORM, command_body->nvIndex };
 
-	marshal_TPM_HANDLE(buffer, TPM_RH_PLATFORM, buffer_space);
-	marshal_TPM_HANDLE(buffer, command_body->nvIndex, buffer_space);
-	memset(&session_header, 0, sizeof(session_header));
-	session_header.session_handle = TPM_RS_PW;
-	marshal_session_header(buffer, &session_header, buffer_space);
-	tpm_tag = TPM_ST_SESSIONS;
+	marshal_common_session_header(buffer, handles,
+				      ARRAY_SIZE(handles), buffer_space);
 	marshal_u16(buffer, command_body->size, buffer_space);
 	marshal_u16(buffer, command_body->offset, buffer_space);
 }



More information about the coreboot-gerrit mailing list