tlcl: support sending raw commands

Implement TlclSendReceive and TlclPacketSize required
for sending raw commands.

BRANCH=none
BUG=chrome-os-partner:55210
TEST=boot on kevin, verify that 'tpmc raw' works

Change-Id: Iba41b95dd7790a6b7a3a7af6cf5f897f45dce1e5
Reviewed-on: https://chromium-review.googlesource.com/363033
Commit-Ready: Andrey Pronin <apronin@chromium.org>
Tested-by: Andrey Pronin <apronin@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
diff --git a/firmware/include/tpm2_marshaling.h b/firmware/include/tpm2_marshaling.h
index 3d6fb8a..cbfded1 100644
--- a/firmware/include/tpm2_marshaling.h
+++ b/firmware/include/tpm2_marshaling.h
@@ -47,6 +47,24 @@
 					     int response_size);
 
 /**
+ * tpm_get_packet_size
+ *
+ * @packet: pointer to the start of the command or response packet.
+ *
+ * Returns the size of the tpm packet.
+ */
+uint32_t tpm_get_packet_size(const uint8_t *packet);
+
+/**
+ * tpm_get_packet_response_code
+ *
+ * @packet: pointer to the start of the response packet.
+ *
+ * Returns the response code.
+ */
+uint32_t tpm_get_packet_response_code(const uint8_t *packet);
+
+/**
  * tpm_set_ph_disabled
  *
  * Sets the flag that indicates if platform hierarchy is disabled.
diff --git a/firmware/lib/tpm2_lite/marshaling.c b/firmware/lib/tpm2_lite/marshaling.c
index 037e696..fd94a51 100644
--- a/firmware/lib/tpm2_lite/marshaling.c
+++ b/firmware/lib/tpm2_lite/marshaling.c
@@ -516,6 +516,23 @@
 	return &tpm2_resp;
 }
 
+uint32_t tpm_get_packet_size(const uint8_t *packet)
+{
+	/* 0: tag (16 bit)
+	 * 2: size (32 bit)
+	 */
+	return read_be32(packet + 2);
+}
+
+uint32_t tpm_get_packet_response_code(const uint8_t *packet)
+{
+	/* 0: tag (16 bit)
+	 * 2: size (32 bit)
+	 * 6: resp code (32 bit)
+	 */
+	return read_be32(packet + 6);
+}
+
 void tpm_set_ph_disabled(int flag)
 {
 	ph_disabled = flag;
diff --git a/firmware/lib/tpm2_lite/tlcl.c b/firmware/lib/tpm2_lite/tlcl.c
index efc528d..d7d853b 100644
--- a/firmware/lib/tpm2_lite/tlcl.c
+++ b/firmware/lib/tpm2_lite/tlcl.c
@@ -82,14 +82,18 @@
 uint32_t TlclSendReceive(const uint8_t *request, uint8_t *response,
                          int max_length)
 {
-        VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
-        return TPM_SUCCESS;
+	uint32_t rv, resp_size;
+
+	resp_size = max_length;
+	rv = VbExTpmSendReceive(request, tpm_get_packet_size(request),
+				response, &resp_size);
+
+	return rv ? rv : tpm_get_packet_response_code(response);
 }
 
 int TlclPacketSize(const uint8_t *packet)
 {
-        VBDEBUG(("%s called, NOT YET IMPLEMENTED\n", __func__));
-        return 0;
+	return tpm_get_packet_size(packet);
 }
 
 uint32_t TlclStartup(void)