[SLOF] [PATCH v2 01/20] Add a TPM driver implementation

Stefan Berger stefanb at us.ibm.com
Wed Nov 18 04:02:17 AEDT 2015


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

This patch adds a TPM driver for the CRQ interface as used by
the QEMU PAPR implementation.

Signed-off-by: Stefan Berger <stefanb at linux.vnet.ibm.com>
---
 include/helpers.h        |   1 +
 lib/libtpm/Makefile      |  51 +++++
 lib/libtpm/tpm_drivers.c | 495 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/libtpm/tpm_drivers.h |  97 ++++++++++
 slof/helpers.c           |   6 +
 5 files changed, 650 insertions(+)
 create mode 100644 lib/libtpm/Makefile
 create mode 100644 lib/libtpm/tpm_drivers.c
 create mode 100644 lib/libtpm/tpm_drivers.h

diff --git a/include/helpers.h b/include/helpers.h
index fb10534..9e6bdf8 100644
--- a/include/helpers.h
+++ b/include/helpers.h
@@ -33,6 +33,7 @@ extern long SLOF_pci_config_read16(long offset);
 extern void SLOF_pci_config_write32(long offset, long value);
 extern void SLOF_pci_config_write16(long offset, long value);
 extern void *SLOF_translate_my_address(void *addr);
+extern unsigned long SLOF_get_vtpm_unit(void);
 
 #define offset_of(type, member) ((long) &((type *)0)->member)
 #define container_of(ptr, type, member) ({                      \
diff --git a/lib/libtpm/Makefile b/lib/libtpm/Makefile
new file mode 100644
index 0000000..a174815
--- /dev/null
+++ b/lib/libtpm/Makefile
@@ -0,0 +1,51 @@
+# *****************************************************************************
+# * Copyright (c) 2015 IBM Corporation
+# * All rights reserved.
+# * This program and the accompanying materials
+# * are made available under the terms of the BSD License
+# * which accompanies this distribution, and is available at
+# * http://www.opensource.org/licenses/bsd-license.php
+# *
+# * Contributors:
+# *     IBM Corporation - initial implementation
+# ****************************************************************************/
+
+TOPCMNDIR ?= ../..
+
+ASFLAGS = $(FLAG) $(RELEASE) $(CPUARCHDEF) -Wa,-mregnames
+CPPFLAGS = -I../libc/include $(CPUARCHDEF) -I$(INCLBRDDIR) \
+	   -I$(INCLCMNDIR) -I$(INCLCMNDIR)/$(CPUARCH) -I$(SLOFCMNDIR)
+CPPFLAGS += -I../libhvcall
+
+LDFLAGS = -nostdlib
+
+TARGET = ../libtpm.a
+
+
+all: $(TARGET)
+
+SRCS = tpm_drivers.c
+
+OBJS = $(SRCS:%.c=%.o)
+
+$(TARGET): $(OBJS)
+	$(AR) -rc $@ $(OBJS)
+	$(RANLIB) $@
+
+clean:
+	$(RM) $(TARGET) $(OBJS)
+
+distclean: clean
+	$(RM) Makefile.dep
+
+
+# Rules for creating the dependency file:
+depend:
+	$(RM) Makefile.dep
+	$(MAKE) Makefile.dep
+
+Makefile.dep: Makefile
+	$(CC) -M $(CPPFLAGS) $(CFLAGS) $(SRCS) $(SRCSS) > Makefile.dep
+
+# Include dependency file if available:
+-include Makefile.dep
diff --git a/lib/libtpm/tpm_drivers.c b/lib/libtpm/tpm_drivers.c
new file mode 100644
index 0000000..900ba38
--- /dev/null
+++ b/lib/libtpm/tpm_drivers.c
@@ -0,0 +1,495 @@
+/*****************************************************************************
+ * Copyright (c) 2015 IBM Corporation
+ * All rights reserved.
+ * This program and the accompanying materials
+ * are made available under the terms of the BSD License
+ * which accompanies this distribution, and is available at
+ * http://www.opensource.org/licenses/bsd-license.php
+ *
+ * Contributors:
+ *     IBM Corporation - initial implementation
+ *****************************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+#include "string.h"
+#include "helpers.h"
+#include "byteorder.h"
+#include "tpm_drivers.h"
+#include "tcgbios.h"
+#include "libhvcall.h"
+#include "paflof.h"
+
+#undef PAPR_VTPM_DEBUG
+//#define PAPR_VTPM_DEBUG
+#ifdef PAPR_VTPM_DEBUG
+#define dprintf(_x ...) do { printf("VTPM CRQ: " _x); } while(0)
+#else
+#define dprintf(_x ...)
+#endif
+
+#define MIN(a, b) ((a) > (b) ? (b) : (a))
+
+struct crq {
+	uint8_t valid;
+	uint8_t msg;
+	uint16_t len;
+	uint32_t data;
+	uint64_t reserved;
+} __attribute__((packed));
+
+#define PAPR_VTPM_INIT_CRQ_COMMAND      0xC0
+#define PAPR_VTPM_VALID_COMMAND         0x80
+#define PAPR_VTPM_MSG_RESULT            0x80
+
+/* crq.msg request types when crq.valid = PAPR_VTPM_INIT_CRQ_COMMAND */
+#define PAPR_VTPM_INIT_CRQ_RESULT       0x1
+
+/* crq.msg request types when crq.valid = PAPR_VTPM_VALID_COMMAND */
+#define PAPR_VTPM_GET_VERSION           0x1
+#define PAPR_VTPM_TPM_COMMAND           0x2
+#define PAPR_VTPM_GET_RTCE_BUFFER_SIZE  0x3
+
+/* check for error state */
+#define SPAPR_CHECK_FAILURE_MODE \
+	if (vtpm_drv_error_get() != VTPM_DRV_ERROR_NO_FAILURE) {	\
+		printf("VTPM CRQ: In failure mode\n");		\
+		return false;					\
+	}
+
+static const uint32_t tpm_default_durations[TPM_NUM_DURATIONS] = {
+	TPM_DEFAULT_DURATION_SHORT,
+	TPM_DEFAULT_DURATION_MEDIUM,
+	TPM_DEFAULT_DURATION_LONG,
+};
+
+#define PAGE_SIZE 4096
+
+/* state of the PAPR CRQ VTPM driver */
+static struct spapr_vtpm_driver_state {
+	/* whether it driver been initialized */
+	bool initialized;
+
+        /* durations of short, medium, & long commands */
+	uint32_t durations[TPM_NUM_DURATIONS];
+
+	/* unit number */
+	unsigned long unit;
+
+	/* CRQ queue address and size */
+	unsigned char *qaddr;
+	unsigned long qsize;
+
+	/* current q_entry */
+	unsigned int curr_q_entry;
+
+	/* current response CRQ */
+	struct crq *response;
+
+	/* power firmware defined state and error code */
+	vtpm_drv_state driver_state;
+	vtpm_drv_error driver_error;
+
+	/* version of the TPM we talk to -- from CRQ message */
+	uint32_t tpm_version;
+
+	/* buffer offset in buffer for sending */
+	unsigned int buffer_offset;
+
+	/* actual size of the buffer being used */
+	unsigned int buffer_size;
+
+	/* the buffer; may be bigger than buffer_size */
+	unsigned char buffer[PAPR_VTPM_MAX_BUFFER_SIZE];
+} spapr_vtpm = {
+	.qsize = PAGE_SIZE,
+	.driver_state = VTPM_DRV_STATE_INVALID,
+	.driver_error = VTPM_DRV_ERROR_NO_FAILURE,
+	.buffer_size = sizeof(spapr_vtpm.buffer),
+};
+
+static void vtpm_drv_state_set(vtpm_drv_state s, vtpm_drv_error e)
+{
+	spapr_vtpm.driver_state = s;
+	spapr_vtpm.driver_error = e;
+}
+
+static vtpm_drv_state vtpm_drv_state_get(void)
+{
+	return spapr_vtpm.driver_state;
+}
+
+static vtpm_drv_error vtpm_drv_error_get(void)
+{
+	return spapr_vtpm.driver_error;
+}
+
+static void spapr_vtpm_set_durations(
+                              const uint32_t durations[TPM_NUM_DURATIONS])
+{
+        memcpy(spapr_vtpm.durations, durations,
+               TPM_NUM_DURATIONS * sizeof(durations[0]));
+}
+
+static struct crq* get_crq(void *qaddr, unsigned long q_entry)
+{
+	return &((struct crq *)qaddr)[q_entry];
+}
+
+/*
+ * Get the crq where the response will be found. This
+ * function will clear the CRQ's valid field and advance
+ * the entry counter to the next entry.
+ */
+static struct crq *get_response_crq(void)
+{
+	struct crq *crq;
+
+	dprintf("curr_q_entry = %d\n", spapr_vtpm.curr_q_entry);
+
+	crq = get_crq(spapr_vtpm.qaddr, spapr_vtpm.curr_q_entry);
+	memset(crq, 0, sizeof(*crq));
+
+	spapr_vtpm.curr_q_entry += 1;
+	if (spapr_vtpm.curr_q_entry == (spapr_vtpm.qsize / sizeof(struct crq)))
+		spapr_vtpm.curr_q_entry = 0;
+
+	return crq;
+}
+
+/*
+ * Send a message via CRQ and wait for the response
+ */
+static bool spapr_send_crq_and_wait(unsigned long unit,
+                                    struct crq *crq,
+                                    struct crq **response,
+                                    unsigned timeout,
+                                    vtpm_drv_state state1,
+                                    vtpm_drv_state state2)
+{
+	long rc;
+	unsigned i;
+
+	*response = get_response_crq();
+
+	vtpm_drv_state_set(state1, VTPM_DRV_ERROR_NO_FAILURE);
+
+	rc = hv_send_crq(unit, (uint64_t *)crq);
+	if (rc != H_SUCCESS) {
+		vtpm_drv_state_set(VTPM_DRV_STATE_WAIT_INIT,
+				   VTPM_DRV_ERROR_TPM_CRQ_ERROR);
+		return false;
+	}
+
+	vtpm_drv_state_set(state2,
+	                   VTPM_DRV_ERROR_NO_FAILURE);
+
+	for (i = 0; i < timeout; i++) {
+		if (((*response)->valid & PAPR_VTPM_MSG_RESULT))
+			return true;
+		SLOF_msleep(1);
+	}
+
+	vtpm_drv_state_set(VTPM_DRV_STATE_FAILURE,
+			   VTPM_DRV_ERROR_WAIT_TIMEOUT);
+
+	dprintf("Received no response from CRQ\n");
+	return false;
+}
+
+/*
+ * Get parameters from the CRQ
+ */
+static bool spapr_vtpm_get_params(void)
+{
+	struct crq crq, *response;
+	static bool completed = false; /* only once */
+
+	if (completed)
+		return true;
+
+	/* get the TPM version */
+	crq.valid = PAPR_VTPM_VALID_COMMAND;
+	crq.msg = PAPR_VTPM_GET_VERSION;
+
+	if (!spapr_send_crq_and_wait(spapr_vtpm.unit, &crq, &response, 10,
+	                             VTPM_DRV_STATE_SEND_GET_VERSION,
+	                             VTPM_DRV_STATE_WAIT_VERSION)) {
+		dprintf("Failure getting TPM version from CRQ\n");
+		return false;
+	}
+
+	vtpm_drv_state_set(VTPM_DRV_STATE_CHECK_VERSION,
+	                   VTPM_DRV_ERROR_NO_FAILURE);
+
+	spapr_vtpm.tpm_version = be32_to_cpu(response->data);
+	dprintf("TPM backend version: %d\n", spapr_vtpm.tpm_version);
+
+	/* get the TPM's buffer size */
+	crq.valid = PAPR_VTPM_VALID_COMMAND;
+	crq.msg = PAPR_VTPM_GET_RTCE_BUFFER_SIZE;
+
+	if (!spapr_send_crq_and_wait(spapr_vtpm.unit, &crq, &response, 10,
+	                             VTPM_DRV_STATE_SEND_BUFSIZE_REQ,
+	                             VTPM_DRV_STATE_WAIT_BUFSIZE)) {
+		dprintf("Failure getting RTCE buffer size from CRQ\n");
+		return false;
+	}
+
+	vtpm_drv_state_set(VTPM_DRV_STATE_ALLOC_RTCE_BUF,
+	                   VTPM_DRV_ERROR_NO_FAILURE);
+
+	dprintf("RTCE buffer size: %u\n", be16_to_cpu(response->len));
+	spapr_vtpm.buffer_size = MIN(spapr_vtpm.buffer_size,
+				     be16_to_cpu(response->len));
+	if (spapr_vtpm.buffer_size < 1024) {
+		dprintf("RTCE buffer size of %u bytes is too small. "
+		        "Minimum is 1024 bytes.\n", spapr_vtpm.buffer_size);
+		vtpm_drv_state_set(VTPM_DRV_STATE_FAILURE,
+				   VTPM_DRV_ERROR_BAD_RTCE_SIZE);
+		return false;
+	}
+
+	completed = true;
+
+	return true;
+}
+
+static bool spapr_vtpm_activate(uint8_t locty)
+{
+	long rc;
+	struct crq crq, *response;
+
+	SPAPR_CHECK_FAILURE_MODE
+
+	spapr_vtpm.buffer_offset = 0;
+
+	vtpm_drv_state_set(VTPM_DRV_STATE_REG_CRQ,
+			   VTPM_DRV_ERROR_NO_FAILURE);
+
+	rc = hv_reg_crq(spapr_vtpm.unit, (unsigned long)spapr_vtpm.qaddr,
+	                spapr_vtpm.qsize);
+	if (rc != H_SUCCESS) {
+		vtpm_drv_state_set(VTPM_DRV_STATE_WAIT_INIT,
+		                   VTPM_DRV_ERROR_UNEXPECTED_REG_ERROR);
+		dprintf("CRQ registration failed\n");
+		return false;
+	}
+
+	/* we always start with curr_q_entry 0 */
+	spapr_vtpm.curr_q_entry = 0;
+
+	if (spapr_vtpm.initialized)
+		goto skip_init;
+
+	crq.valid = PAPR_VTPM_INIT_CRQ_COMMAND;
+	crq.msg = PAPR_VTPM_INIT_CRQ_RESULT;
+
+	if (!spapr_send_crq_and_wait(spapr_vtpm.unit,
+	                             &crq,
+	                             &response,
+	                             10,
+	                             VTPM_DRV_STATE_SEND_INIT,
+	                             VTPM_DRV_STATE_WAIT_INIT_COMP)) {
+		dprintf("Initializing CRQ failed\n");
+		goto err_exit;
+	}
+	dprintf("Successfully initialized CRQ\n");
+
+	spapr_vtpm.initialized = true;
+
+skip_init:
+	if (!spapr_vtpm_get_params())
+		goto err_exit;
+
+	return true;
+
+err_exit:
+	hv_free_crq(spapr_vtpm.unit);
+
+	return false;
+}
+
+static bool spapr_vtpm_init(void)
+{
+        spapr_vtpm_set_durations(tpm_default_durations);
+
+	return true;
+}
+
+/*
+ * Check whether we have a CRQ underneath us
+ */
+static bool spapr_vtpm_probe(void)
+{
+	spapr_vtpm_init();
+
+	if (!spapr_vtpm.qaddr) {
+		spapr_vtpm.qaddr = SLOF_alloc_mem(spapr_vtpm.qsize);
+		if (!spapr_vtpm.qaddr) {
+			printf("spapr-vtpm: Unable to allocate memory\n");
+			return false;
+		}
+		memset(spapr_vtpm.qaddr, 0, spapr_vtpm.qsize);
+
+		dprintf("getting FORTH vtpm-unit\n");
+		spapr_vtpm.unit = SLOF_get_vtpm_unit();
+		if (!spapr_vtpm.unit) {
+			printf("spapr-vtpm: Could not get valid vtpm-unit\n");
+			return false;
+		}
+	}
+
+	dprintf("vtpm_unit = %lx, buffer = %p\n",
+	        spapr_vtpm.unit, spapr_vtpm.qaddr);
+
+	if (!spapr_vtpm_activate(0))
+		return false;
+
+	hv_free_crq(spapr_vtpm.unit);
+
+	return true;
+}
+
+static bool spapr_vtpm_senddata(const uint8_t *const data, uint32_t len)
+{
+	SPAPR_CHECK_FAILURE_MODE
+
+	/*
+	 * we have to collect all data to be sent in a buffer
+	 */
+
+	if (spapr_vtpm.buffer_offset + len > spapr_vtpm.buffer_size) {
+		spapr_vtpm.buffer_offset = 0;
+		return false;
+	}
+
+	memcpy(&spapr_vtpm.buffer[spapr_vtpm.buffer_offset], data, len);
+
+	spapr_vtpm.buffer_offset += len;
+
+	return true;
+}
+
+static bool spapr_vtpm_transfer(void)
+{
+	struct crq crq;
+	long rc;
+
+	SPAPR_CHECK_FAILURE_MODE
+
+	spapr_vtpm.response = get_response_crq();
+	/* response CRQ has been set and valid field cleared */
+
+	crq.valid = PAPR_VTPM_VALID_COMMAND;
+	crq.msg = PAPR_VTPM_TPM_COMMAND;
+	crq.len = cpu_to_be16(spapr_vtpm.buffer_offset);
+	crq.data = cpu_to_be32((uint64_t)spapr_vtpm.buffer);
+
+	vtpm_drv_state_set(VTPM_DRV_STATE_SEND_TPM_CMD,
+	                   VTPM_DRV_ERROR_NO_FAILURE);
+
+	rc = hv_send_crq(spapr_vtpm.unit, (uint64_t *)&crq);
+
+	if (rc == H_SUCCESS) {
+		vtpm_drv_state_set(VTPM_DRV_STATE_WAIT_TPM_RSP,
+		                   VTPM_DRV_ERROR_NO_FAILURE);
+	} else {
+		vtpm_drv_state_set(VTPM_DRV_STATE_WAIT_INIT,
+		                   VTPM_DRV_ERROR_UNEXPECTED_SEND_ERROR);
+	}
+
+	spapr_vtpm.buffer_offset = 0;
+
+	return (rc == H_SUCCESS);
+}
+
+static bool spapr_vtpm_waitresponseready(enum tpm_duration_type to_t)
+{
+	uint32_t timeout = spapr_vtpm.durations[to_t];
+	int i;
+
+	SPAPR_CHECK_FAILURE_MODE
+
+	/* response CRQ has been set */
+
+	for (i = 0; i < timeout; i++) {
+		if (spapr_vtpm.response->valid & PAPR_VTPM_MSG_RESULT) {
+			/* TPM responded: move to Send tpm-cmd state */
+			vtpm_drv_state_set(VTPM_DRV_STATE_SEND_TPM_CMD,
+					   VTPM_DRV_ERROR_NO_FAILURE);
+			dprintf("Received response to TPM command\n");
+			return true;
+		}
+		SLOF_msleep(1);
+	}
+
+	vtpm_drv_state_set(VTPM_DRV_STATE_FAILURE,
+			   VTPM_DRV_ERROR_WAIT_TIMEOUT);
+
+	dprintf("Received NO response to TPM command");
+
+	return false;
+}
+
+static bool spapr_vtpm_readresponse(uint8_t *buffer, uint32_t *len)
+{
+	SPAPR_CHECK_FAILURE_MODE
+
+	/* response CRQ has been set */
+	*len = MIN(*len, be32_to_cpu(spapr_vtpm.response->len));
+
+	memcpy(buffer, (void *)(uint64_t)spapr_vtpm.response->data, *len);
+
+	dprintf("Length of copied response: %d\n", *len);
+
+	return true;
+}
+
+static bool spapr_vtpm_endcycle(void)
+{
+	SPAPR_CHECK_FAILURE_MODE
+
+	hv_free_crq(spapr_vtpm.unit);
+
+	spapr_vtpm.response = NULL;
+
+	return true;
+}
+
+static uint32_t spapr_vtpm_get_buffersize(void)
+{
+	return spapr_vtpm.buffer_size;
+}
+
+static vtpm_drv_state spapr_vtpm_get_state(void)
+{
+	return vtpm_drv_state_get();
+}
+
+static vtpm_drv_error spapr_vtpm_get_error(void)
+{
+	return vtpm_drv_error_get();
+}
+
+/**** driver structures ****/
+
+struct tpm_driver tpm_drivers[TPM_NUM_DRIVERS] = {
+	[PAPR_DRIVER_IDX] = {
+		.setdurations      = spapr_vtpm_set_durations,
+		.probe             = spapr_vtpm_probe,
+		.init              = spapr_vtpm_init,
+		.activate          = spapr_vtpm_activate,
+		.ready             = spapr_vtpm_endcycle,
+		.senddata          = spapr_vtpm_senddata,
+		.transfer          = spapr_vtpm_transfer,
+		.waitresponseready = spapr_vtpm_waitresponseready,
+		.readresponse      = spapr_vtpm_readresponse,
+		.sha1threshold     = 100 * 1024,
+		.getbuffersize     = spapr_vtpm_get_buffersize,
+		.getstate          = spapr_vtpm_get_state,
+		.geterror          = spapr_vtpm_get_error,
+	},
+};
diff --git a/lib/libtpm/tpm_drivers.h b/lib/libtpm/tpm_drivers.h
new file mode 100644
index 0000000..0e81762
--- /dev/null
+++ b/lib/libtpm/tpm_drivers.h
@@ -0,0 +1,97 @@
+/*****************************************************************************
+ * Copyright (c) 2015 IBM Corporation
+ * All rights reserved.
+ * This program and the accompanying materials
+ * are made available under the terms of the BSD License
+ * which accompanies this distribution, and is available at
+ * http://www.opensource.org/licenses/bsd-license.php
+ *
+ * Contributors:
+ *     IBM Corporation - initial implementation
+ *****************************************************************************/
+
+#ifndef TPM_DRIVERS_H
+#define TPM_DRIVERS_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+enum tpm_duration_type {
+	TPM_DURATION_TYPE_SHORT = 0,
+	TPM_DURATION_TYPE_MEDIUM,
+	TPM_DURATION_TYPE_LONG,
+};
+
+#define TPM_NUM_DURATIONS    3
+
+#define TPM_DEFAULT_DURATION_SHORT     (2000)
+#define TPM_DEFAULT_DURATION_MEDIUM    (20000)
+#define TPM_DEFAULT_DURATION_LONG      (60000)
+
+#define PAPR_DRIVER_IDX      0
+#define TPM_NUM_DRIVERS      1
+
+/* firmware driver states */
+typedef enum {
+	VTPM_DRV_STATE_INVALID = 0,
+	VTPM_DRV_STATE_INIT_CALLED = 1,
+	VTPM_DRV_STATE_REG_CRQ = 2,
+	VTPM_DRV_STATE_WAIT_INIT = 3,
+	VTPM_DRV_STATE_SEND_INIT = 4,
+	VTPM_DRV_STATE_FAILURE = 5,
+	VTPM_DRV_STATE_WAIT_INIT_COMP = 6,
+	VTPM_DRV_STATE_SEND_INIT_COMP = 7,
+	VTPM_DRV_STATE_SEND_GET_VERSION = 8,
+	VTPM_DRV_STATE_WAIT_VERSION = 9,
+	VTPM_DRV_STATE_CHECK_VERSION = 10,
+	VTPM_DRV_STATE_SEND_BUFSIZE_REQ = 11,
+	VTPM_DRV_STATE_WAIT_BUFSIZE = 12,
+	VTPM_DRV_STATE_ALLOC_RTCE_BUF = 13,
+	VTPM_DRV_STATE_SEND_TPM_CMD = 14,
+	VTPM_DRV_STATE_WAIT_TPM_RSP = 15,
+} vtpm_drv_state;
+
+/* firmware driver errors */
+typedef enum {
+	VTPM_DRV_ERROR_NO_FAILURE = -1,
+	VTPM_DRV_ERROR_NOT_FOUND_TIMEOUT = 0,
+	VTPM_DRV_ERROR_UNEXPECTED_REG_ERROR = 1,
+	VTPM_DRV_ERROR_PARTNER_FAILED = 2,
+	VTPM_DRV_ERROR_UNEXPECTED_TSP_ERROR = 3,
+	VTPM_DRV_ERROR_TPM_PROTOCOL_ERROR = 4,
+	VTPM_DRV_ERROR_WAIT_TIMEOUT = 5,
+	VTPM_DRV_ERROR_UNEXPECTED_SEND_ERROR = 6,
+	VTPM_DRV_ERROR_CRQ_OPEN_FAIL = 7,
+	VTPM_DRV_ERROR_BAD_STATE = 8,
+	VTPM_DRV_ERROR_TPM_FAIL = 9,
+	VTPM_DRV_ERROR_TPM_CRQ_ERROR = 10,
+	VTPM_DRV_ERROR_BAD_VERSION = 11,
+	VTPM_DRV_ERROR_BAD_RTCE_SIZE = 12,
+	VTPM_DRV_ERROR_SML_FAILURE = 13,
+	VTPM_DRV_ERROR_SML_HANDED_OVER = 14,
+} vtpm_drv_error;
+
+/* low level driver implementation */
+struct tpm_driver {
+	void (*setdurations)(const uint32_t durations[TPM_NUM_DURATIONS]);
+	bool (*probe)(void);
+	bool (*init)(void);
+	bool (*activate)(uint8_t locty);
+	bool (*ready)(void);
+	bool (*senddata)(const uint8_t *const data, uint32_t len);
+	bool (*transfer)(void);
+	bool (*waitresponseready)(enum tpm_duration_type to_t);
+	bool (*readresponse)(uint8_t *buffer, uint32_t *len);
+	/* the TPM will be used for buffers of sizes below the sha1threshold
+	   for calculating the hash */
+	uint32_t sha1threshold;
+	void (*get_vers_data)(uint16_t *did, uint16_t *vid, uint16_t *rid);
+	uint32_t (*getbuffersize)(void);
+	vtpm_drv_state (*getstate)(void);
+	vtpm_drv_error (*geterror)(void);
+};
+
+/* the max. buffer size by the external TPM is 4k */
+#define PAPR_VTPM_MAX_BUFFER_SIZE       4096
+
+#endif /* TPM_DRIVERS_H */
diff --git a/slof/helpers.c b/slof/helpers.c
index d7c1888..dc7f08c 100644
--- a/slof/helpers.c
+++ b/slof/helpers.c
@@ -134,3 +134,9 @@ void *SLOF_translate_my_address(void *addr)
 	forth_eval("translate-my-address");
 	return (void *)forth_pop();
 }
+
+unsigned long SLOF_get_vtpm_unit(void)
+{
+	forth_eval("vtpm-unit");
+	return forth_pop();
+}
-- 
2.4.3



More information about the SLOF mailing list