[SeaBIOS] [PATCH v3 4/5] tpm: Refactor pass_through_to_tpm

Stefan Berger stefanb at us.ibm.com
Sat Nov 21 20:54:43 CET 2015


From: Stefan Berger <stefanb at linux.vnet.ibm.com>

Refactor the signature of the pass_through_to_tpm function to take
individual pointers as parameters and introduce pass_through_to_tpm_int
as a function to be called with the parameters passed from the BIOS
interrupt.

Refactor existing callers that now do not have to build up the data
structure expected by the BIOS interface.

Signed-off-by: Stefan Berger <stefanb at linux.vnet.ibm.com>
---
 src/std/tcg.h | 12 ---------
 src/tcgbios.c | 82 +++++++++++++++++++++++++++--------------------------------
 2 files changed, 37 insertions(+), 57 deletions(-)

diff --git a/src/std/tcg.h b/src/std/tcg.h
index ba2e243..da3e1f6 100644
--- a/src/std/tcg.h
+++ b/src/std/tcg.h
@@ -329,18 +329,6 @@ struct tpm_res_sha1complete {
     u8     hash[20];
 } PACKED;
 
-struct pttti_extend {
-    struct pttti pttti;
-    struct tpm_req_extend req;
-} PACKED;
-
-
-struct pttto_extend {
-    struct pttto pttto;
-    struct tpm_rsp_extend rsp;
-} PACKED;
-
-
 enum ipltype {
     IPL_BCV = 0,
     IPL_EL_TORITO_1,
diff --git a/src/tcgbios.c b/src/tcgbios.c
index 018c580..7a60138 100644
--- a/src/tcgbios.c
+++ b/src/tcgbios.c
@@ -666,24 +666,34 @@ shutdown_preboot_interface(void)
     return rc;
 }
 
-
-static void
-tpm_shutdown(void)
+static u32
+pass_through_to_tpm(u8 locty, const u8 *cmd, u32 cmd_length,
+                    u8 *resp, u32 *resp_length)
 {
-    reset_acpi_log();
-    shutdown_preboot_interface();
-}
+    struct iovec iovec[2] = {{ 0 }};
+    const u32 *tmp;
+
+    if (cmd_length < TPM_REQ_HEADER_SIZE)
+        return TCG_INVALID_INPUT_PARA;
+
+    iovec[0].data = cmd;
+    tmp = (const u32 *)&((u8 *)iovec[0].data)[2];
+    iovec[0].length = cpu_to_be32(*tmp);
+
+    if (cmd_length != iovec[0].length)
+        return TCG_INVALID_INPUT_PARA;
+
+    return transmit(locty, iovec, resp, resp_length,
+                    TPM_DURATION_TYPE_LONG /* worst case */);
 
+}
 
 static u32
-pass_through_to_tpm(struct pttti *pttti, struct pttto *pttto)
+pass_through_to_tpm_int(struct pttti *pttti, struct pttto *pttto)
 {
     u32 rc = 0;
     u32 resbuflen = 0;
     struct tpm_req_header *trh;
-    u8 locty = 0;
-    struct iovec iovec[2];
-    const u32 *tmp;
 
     if (is_preboot_if_shutdown()) {
         rc = TCG_INTERFACE_SHUTDOWN;
@@ -701,15 +711,10 @@ pass_through_to_tpm(struct pttti *pttti, struct pttto *pttto)
 
     resbuflen = pttti->opblength - offsetof(struct pttto, tpmopout);
 
-    iovec[0].data   = pttti->tpmopin;
-    tmp = (const u32 *)&((u8 *)iovec[0].data)[2];
-    iovec[0].length = cpu_to_be32(*tmp);
-
-    iovec[1].data   = NULL;
-    iovec[1].length = 0;
+    rc = pass_through_to_tpm(0, pttti->tpmopin,
+                             pttti->ipblength - offsetof(struct pttti, tpmopin),
+                             pttto->tpmopout, &resbuflen);
 
-    rc = transmit(locty, iovec, pttto->tpmopout, &resbuflen,
-                  TPM_DURATION_TYPE_LONG /* worst case */);
     if (rc)
         goto err_exit;
 
@@ -730,35 +735,22 @@ static u32
 tpm_extend(u8 *hash, u32 pcrindex)
 {
     u32 rc;
-    struct pttto_extend pttto;
-    struct pttti_extend pttti = {
-        .pttti = {
-            .ipblength = sizeof(struct pttti_extend),
-            .opblength = sizeof(struct pttto_extend),
-        },
-        .req = {
-            .tag      = cpu_to_be16(0xc1),
-            .totlen   = cpu_to_be32(sizeof(pttti.req)),
-            .ordinal  = cpu_to_be32(TPM_ORD_Extend),
-            .pcrindex = cpu_to_be32(pcrindex),
-        },
+    struct tpm_req_extend tre = {
+        .tag      = cpu_to_be16(TPM_TAG_RQU_CMD),
+        .totlen   = cpu_to_be32(sizeof(tre)),
+        .ordinal  = cpu_to_be32(TPM_ORD_Extend),
+        .pcrindex = cpu_to_be32(pcrindex),
     };
+    struct tpm_rsp_extend rsp;
+    u32 resp_length = sizeof(rsp);
 
-    memcpy(pttti.req.digest, hash, sizeof(pttti.req.digest));
-
-    rc = pass_through_to_tpm(&pttti.pttti, &pttto.pttto);
+    memcpy(tre.digest, hash, sizeof(tre.digest));
 
-    if (rc == 0) {
-        if (pttto.pttto.opblength < TPM_RSP_HEADER_SIZE ||
-            pttto.pttto.opblength !=
-                sizeof(struct pttto) + be32_to_cpu(pttto.rsp.totlen) ||
-            be16_to_cpu(pttto.rsp.tag) != 0xc4) {
-            rc = TCG_FATAL_COM_ERROR;
-        }
-    }
+    rc = pass_through_to_tpm(0, (u8 *)&tre, sizeof(tre),
+                             (u8 *)&rsp, &resp_length);
 
-    if (rc)
-        tpm_shutdown();
+    if (rc || resp_length != sizeof(rsp))
+        tpm_set_failure();
 
     return rc;
 }
@@ -1014,8 +1006,8 @@ tpm_interrupt_handler32(struct bregs *regs)
 
     case TCG_PassThroughToTPM:
         regs->eax =
-            pass_through_to_tpm((struct pttti *)input_buf32(regs),
-                                (struct pttto *)output_buf32(regs));
+            pass_through_to_tpm_int((struct pttti *)input_buf32(regs),
+                                    (struct pttto *)output_buf32(regs));
         break;
 
     case TCG_ShutdownPreBootInterface:
-- 
2.4.3




More information about the SeaBIOS mailing list