diff mbox series

[v4,12/33] tpm: Add TPM firmware API call get-maximum-cmd-size

Message ID 20191211202728.127996-13-stefanb@linux.vnet.ibm.com
State Superseded
Headers show
Series Add vTPM support to SLOF | expand

Commit Message

Stefan Berger Dec. 11, 2019, 8:27 p.m. UTC
This patch adds support for the firmware API call get-maximum-cmd-size.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
---
 board-qemu/slof/vio-vtpm-cdriver.fs |  7 +++++++
 board-qemu/slof/vtpm-sml.fs         | 10 ++++++++++
 lib/libtpm/tcgbios.c                | 24 ++++++++++++++++++++++++
 lib/libtpm/tcgbios.h                |  1 +
 lib/libtpm/tcgbios_int.h            |  7 +++++++
 lib/libtpm/tpm.code                 | 10 ++++++++++
 lib/libtpm/tpm.in                   |  1 +
 7 files changed, 60 insertions(+)
diff mbox series

Patch

diff --git a/board-qemu/slof/vio-vtpm-cdriver.fs b/board-qemu/slof/vio-vtpm-cdriver.fs
index d4d0690..de69e2d 100644
--- a/board-qemu/slof/vio-vtpm-cdriver.fs
+++ b/board-qemu/slof/vio-vtpm-cdriver.fs
@@ -122,6 +122,13 @@  false VALUE vtpm-debug?
     THEN
 ;
 
+\ firmware API call
+: get-maximum-cmd-size ( -- maximum-size )
+    " get-maximum-cmd-size" vtpm-call-forward IF
+        0
+    THEN
+;
+
 : open ( )
     vtpm-debug? IF ." VTPM: vTPM open()" cr THEN
     true
diff --git a/board-qemu/slof/vtpm-sml.fs b/board-qemu/slof/vtpm-sml.fs
index b7ecb4a..bf0d55e 100644
--- a/board-qemu/slof/vtpm-sml.fs
+++ b/board-qemu/slof/vtpm-sml.fs
@@ -84,6 +84,16 @@  log-base LOG-SIZE tpm-set-log-parameters
     THEN
 ;
 
+: get-maximum-cmd-size ( -- max-size )
+    vtpm-debug? IF
+        ." Call to get-maximum-cmd-size" cr
+    THEN
+    tpm-get-maximum-cmd-size                       ( max-size )
+    dup 0= IF     \ Display if return value is 0
+        ." VTPM: Return value from tpm-get-maximum-cmd-size: " dup . cr
+    THEN
+;
+
 \
 \ internal API calls
 \
diff --git a/lib/libtpm/tcgbios.c b/lib/libtpm/tcgbios.c
index e42b7e2..9114d64 100644
--- a/lib/libtpm/tcgbios.c
+++ b/lib/libtpm/tcgbios.c
@@ -38,6 +38,8 @@ 
 #define dprintf(_x ...)
 #endif
 
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
 struct tpm_state {
 	unsigned tpm_probed:1;
 	unsigned tpm_found:1;
@@ -813,3 +815,25 @@  uint32_t tpm_measure_scrtm(void)
 
 	return rc;
 }
+
+/*
+ * tpm_get_maximum_cmd_size: Function for interfacing with the firmware API
+ *
+ * This function returns the maximum size a TPM command (or response) may have.
+ */
+uint32_t tpm_get_maximum_cmd_size(void)
+{
+	struct tpm_rsp_getcap_buffersize trgb;
+	int ret;
+
+	if (!tpm_is_working())
+		return 0;
+
+	ret = tpm12_get_capability(TPM_CAP_PROPERTY, TPM_CAP_PROP_INPUT_BUFFER,
+				   &trgb.hdr, sizeof(trgb));
+	if (ret)
+		return 0;
+
+	return  MIN(cpu_to_be32(trgb.buffersize),
+	            spapr_vtpm_get_buffersize());
+}
diff --git a/lib/libtpm/tcgbios.h b/lib/libtpm/tcgbios.h
index b08e12f..6eb6751 100644
--- a/lib/libtpm/tcgbios.h
+++ b/lib/libtpm/tcgbios.h
@@ -34,6 +34,7 @@  uint32_t tpm_measure_bcv_mbr(uint32_t bootdrv, const uint8_t *addr,
                              uint32_t length);
 uint32_t tpm_add_event_separators(uint32_t start_pcr, uint32_t end_pcr);
 uint32_t tpm_process_opcode(uint8_t op, bool verbose);
+uint32_t tpm_get_maximum_cmd_size(void);
 
 /* flags returned by tpm_get_state */
 #define TPM_STATE_ENABLED        1
diff --git a/lib/libtpm/tcgbios_int.h b/lib/libtpm/tcgbios_int.h
index 77ed815..39e1ea4 100644
--- a/lib/libtpm/tcgbios_int.h
+++ b/lib/libtpm/tcgbios_int.h
@@ -116,6 +116,7 @@  struct tpm_req_getcap {
 #define TPM_CAP_FLAG_PERMANENT  0x108
 #define TPM_CAP_PROP_OWNER      0x111
 #define TPM_CAP_PROP_DURATION   0x120
+#define TPM_CAP_PROP_INPUT_BUFFER 0x124
 
 struct tpm_req_getcap_perm_flags {
 	struct tpm_req_header hdr;
@@ -179,6 +180,12 @@  struct tpm_rsp_getcap_durations {
 	uint32_t durations[TPM_NUM_DURATIONS];
 } __attribute__((packed));
 
+struct tpm_rsp_getcap_buffersize {
+	struct tpm_rsp_header hdr;
+	uint32_t size;
+	uint32_t buffersize;
+} __attribute__((packed));
+
 #define TPM_PPI_OP_NOOP 0
 #define TPM_PPI_OP_ENABLE 1
 #define TPM_PPI_OP_DISABLE 2
diff --git a/lib/libtpm/tpm.code b/lib/libtpm/tpm.code
index 67877c0..ecfba8c 100644
--- a/lib/libtpm/tpm.code
+++ b/lib/libtpm/tpm.code
@@ -162,3 +162,13 @@  PRIM(tpm_X2d_measure_X2d_scrtm)
 	PUSH;
 	TOS.n = tpm_measure_scrtm();
 MIRP
+
+/****************************************************/
+/* Firmware API                                     */
+/* SLOF:   tpm-get-maximum-cmd-size ( -- max-size)  */
+/* LIBTPM: maxsize = tpm_get_maximum_cmd_size()     */
+/****************************************************/
+PRIM(tpm_X2d_get_X2d_maximum_X2d_cmd_X2d_size)
+	PUSH;
+	TOS.n = tpm_get_maximum_cmd_size();
+MIRP
diff --git a/lib/libtpm/tpm.in b/lib/libtpm/tpm.in
index 59a4ba6..1bd393e 100644
--- a/lib/libtpm/tpm.in
+++ b/lib/libtpm/tpm.in
@@ -27,3 +27,4 @@  cod(tpm-process-opcode)
 cod(tpm-get-state)
 cod(tpm-is-working)
 cod(tpm-measure-scrtm)
+cod(tpm-get-maximum-cmd-size)