drivers/tpm: Split out common utilities into separate file

There are some common tpm functions (ie: marshalling) that
are used by multiple tpm drivers.  Splitting into common util
file.

BUG=b:37751915, b:63893483
BRANCH=None
TEST=Make sure depthcharge compiles properly

Change-Id: I1d97b80d74942338563df4bca7ce2ee23e2c4eb6
Signed-off-by: Shelley Chen <shchen@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/751697
Reviewed-by: Furquan Shaikh <furquan@chromium.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
diff --git a/src/drivers/tpm/tpm_state.c b/src/drivers/tpm/tpm_state.c
index e7a7473..068bf64 100644
--- a/src/drivers/tpm/tpm_state.c
+++ b/src/drivers/tpm/tpm_state.c
@@ -11,6 +11,7 @@
 #include <libpayload.h>
 
 #include "drivers/tpm/tpm.h"
+#include "drivers/tpm/tpm_utils.h"
 
 /*
  * The below structure represents the body of the response to the 'report tpm
@@ -47,55 +48,6 @@
 	/* The below fields are present in version 2 and above. */
 } __attribute__((packed));
 
-/*
- * This structure describes the header used for TPM Vendor commands and their
- * responses. Command payload or response (if any) are concatenated with the
- * header. All values are transmitted in big endian format.
- */
-
-struct tpm_vendor_header {
-	uint16_t tag;		  /* TPM_ST_NO_SESSIONS */
-	uint32_t size;		  /* including this header */
-	uint32_t code;		  /* Command out, Response code back. */
-	uint16_t subcommand_code; /* Vendor subcommand, not used on response. */
-} __attribute__((packed));
-
-/*
- * TPMv2 Spec mandates that vendor-specific command codes have bit 29 set,
- * while bits 15-0 indicate the command. All other bits should be zero. We
- * define one of those 16-bit command values for Cr50 purposes, and use the
- * subcommand_code in struct TpmCmdHeader to further distinguish the desired
- * operation.
- */
-#define TPM_CC_VENDOR_BIT   0x20000000
-
-/* Cr50 vendor-specific subcommand codes. 16 bits available. */
-enum vendor_cmd_cc {
-	VENDOR_CC_REPORT_TPM_STATE = 23,
-};
-
-#define TPM_ST_NO_SESSIONS 0x8001
-
-static void marshal_u16(void *buf, uint16_t value)
-{
-	value = htobe16(value);
-	memcpy(buf, &value, sizeof(value));
-}
-
-static void marshal_u32(void *buf, uint32_t value)
-{
-	value = htobe32(value);
-	memcpy(buf, &value, sizeof(value));
-}
-
-static uint32_t unmarshal_u32(void *buf)
-{
-	uint32_t value;
-
-	memcpy(&value, buf, sizeof(value));
-	return be32toh(value);
-}
-
 static void stringify_state(struct tpm_vendor_state *s,
 			    char *state_str,
 			    size_t state_size)
diff --git a/src/drivers/tpm/tpm_utils.h b/src/drivers/tpm/tpm_utils.h
new file mode 100644
index 0000000..bc3a0fb
--- /dev/null
+++ b/src/drivers/tpm/tpm_utils.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __DRIVERS_TPM_TPM_UTILS_H__
+#define __DRIVERS_TPM_TPM_UTILS_H__
+
+#include <libpayload.h>
+
+/*
+ * This structure describes the header used for TPM Vendor commands and their
+ * responses. Command payload or response (if any) are concatenated with the
+ * header. All values are transmitted in big endian format.
+ */
+
+struct tpm_vendor_header {
+	uint16_t tag;		  /* TPM_ST_NO_SESSIONS */
+	uint32_t size;		  /* including this header */
+	uint32_t code;		  /* Command out, Response code back. */
+	uint16_t subcommand_code; /* Vendor subcommand, not used on response. */
+} __attribute__((packed));
+
+/*
+ * TPMv2 Spec mandates that vendor-specific command codes have bit 29 set,
+ * while bits 15-0 indicate the command. All other bits should be zero. We
+ * define one of those 16-bit command values for Cr50 purposes, and use the
+ * subcommand_code in struct TpmCmdHeader to further distinguish the desired
+ * operation.
+ */
+#define TPM_CC_VENDOR_BIT   0x20000000
+
+/* Cr50 vendor-specific subcommand codes. 16 bits available. */
+enum vendor_cmd_cc {
+	VENDOR_CC_REPORT_TPM_STATE = 23,
+	VENDOR_CC_GET_REC_BTN = 29,
+};
+
+#define TPM_ST_NO_SESSIONS 0x8001
+
+static inline void marshal_u16(void *buf, u16 val) { be16enc(buf, val); }
+static inline void marshal_u32(void *buf, u32 val) { be32enc(buf, val); }
+static inline uint32_t unmarshal_u32(void *buf) { return be32dec(buf); }
+
+#endif /* __DRIVERS_TPM_TPM_UTILS_H__*/