[SeaBIOS] [PATCH 02/10] tpm: Avoid macro expansion of tpm request / response structs

Kevin O'Connor kevin at koconnor.net
Wed Dec 30 01:17:42 CET 2015


Avoid macros and use regular struct definitions for the request and
response headers.  This simplifies the header and reduces the need for
casts in the code.

Signed-off-by: Kevin O'Connor <kevin at koconnor.net>
---
 src/std/tcg.h | 40 +++++++++++++++++-----------------------
 src/tcgbios.c | 11 +++++------
 2 files changed, 22 insertions(+), 29 deletions(-)

diff --git a/src/std/tcg.h b/src/std/tcg.h
index d7b3fc4..70daa41 100644
--- a/src/std/tcg.h
+++ b/src/std/tcg.h
@@ -227,41 +227,35 @@ struct pcctes_romex
 } PACKED;
 
 
-#define TPM_REQ_HEADER \
-    u16    tag; \
-    u32    totlen; \
-    u32    ordinal;
-
-#define TPM_RSP_HEADER \
-    u16    tag; \
-    u32    totlen; \
-    u32    errcode;
-
 struct tpm_req_header {
-    TPM_REQ_HEADER;
+    u16    tag;
+    u32    totlen;
+    u32    ordinal;
 } PACKED;
 
 
 struct tpm_rsp_header {
-    TPM_RSP_HEADER;
+    u16    tag;
+    u32    totlen;
+    u32    errcode;
 } PACKED;
 
 
 struct tpm_req_extend {
-    TPM_REQ_HEADER
+    struct tpm_req_header hdr;
     u32    pcrindex;
     u8     digest[SHA1_BUFSIZE];
 } PACKED;
 
 
 struct tpm_rsp_extend {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u8     digest[SHA1_BUFSIZE];
 } PACKED;
 
 
 struct tpm_req_getcap_perm_flags {
-    TPM_REQ_HEADER
+    struct tpm_req_header hdr;
     u32    capArea;
     u32    subCapSize;
     u32    subCap;
@@ -287,13 +281,13 @@ enum permFlagsIndex {
 
 
 struct tpm_res_getcap_perm_flags {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u32    size;
     struct tpm_permanent_flags perm_flags;
 } PACKED;
 
 struct tpm_req_getcap_stclear_flags {
-    TPM_REQ_HEADER
+    struct tpm_req_header hdr;
     u32    capArea;
     u32    subCapSize;
     u32    subCap;
@@ -311,40 +305,40 @@ struct tpm_stclear_flags {
 #define STCLEAR_FLAG_IDX_GLOBAL_LOCK 4
 
 struct tpm_res_getcap_stclear_flags {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u32    size;
     struct tpm_stclear_flags stclear_flags;
 } PACKED;
 
 struct tpm_res_getcap_ownerauth {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u32    size;
     u8     flag;
 } PACKED;
 
 
 struct tpm_res_getcap_timeouts {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u32    size;
     u32    timeouts[4];
 } PACKED;
 
 
 struct tpm_res_getcap_durations {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u32    size;
     u32    durations[3];
 } PACKED;
 
 
 struct tpm_res_sha1start {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u32    max_num_bytes;
 } PACKED;
 
 
 struct tpm_res_sha1complete {
-    TPM_RSP_HEADER
+    struct tpm_rsp_header hdr;
     u8     hash[20];
 } PACKED;
 
diff --git a/src/tcgbios.c b/src/tcgbios.c
index 4f78c42..55e38a9 100644
--- a/src/tcgbios.c
+++ b/src/tcgbios.c
@@ -472,17 +472,16 @@ tpm_log_extend_event(struct pcpes *pcpes, const void *event)
         return TCG_INVALID_INPUT_PARA;
 
     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(pcpes->pcrindex),
+        .hdr.tag     = cpu_to_be16(TPM_TAG_RQU_CMD),
+        .hdr.totlen  = cpu_to_be32(sizeof(tre)),
+        .hdr.ordinal = cpu_to_be32(TPM_ORD_Extend),
+        .pcrindex    = cpu_to_be32(pcpes->pcrindex),
     };
     memcpy(tre.digest, pcpes->digest, sizeof(tre.digest));
 
     struct tpm_rsp_extend rsp;
     u32 resp_length = sizeof(rsp);
-    u32 rc = transmit(0, (void*)&tre, &rsp, &resp_length,
-                      TPM_DURATION_TYPE_SHORT);
+    u32 rc = transmit(0, &tre.hdr, &rsp, &resp_length, TPM_DURATION_TYPE_SHORT);
     if (rc || resp_length != sizeof(rsp)) {
         tpm_set_failure();
         return rc;
-- 
2.5.0




More information about the SeaBIOS mailing list