Updated related Linux Driver Refactoring

Update of the client part, related to the Linux Driver
Refactoring at https://github.com/OP-TEE/optee_linuxdriver/pull/17

Fix #16

Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
Tested-by: Pascal Brand <pascal.brand@linaro.org> (STM platform)
Tested-by: Pascal Brand <pascal.brand@linaro.org> (QEMU)
Tested-by: Cedric Chaumont <cedric.chaumont@linaro.org> (FVP)
Tested-by: Cedric Chaumont <cedric.chaumont@linaro.org> (Juno)
Signed-off-by: Cedric Chaumont <cedric.chaumont@st.com>
diff --git a/Android.mk b/Android.mk
index 4ff7582..1eb138f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -28,6 +28,7 @@
 
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/public \
 		$(LOCAL_PATH)/libteec/include \
+		$(LOCAL_PATH)/../optee_linuxdriver/include \
 
 LOCAL_PRELINK_MODULE := false
 LOCAL_MODULE := libteec
diff --git a/Makefile b/Makefile
index a5c7029..a4c86dc 100644
--- a/Makefile
+++ b/Makefile
@@ -120,10 +120,10 @@
 ifdef ROOTFS_DIR
 copy_rootfs: build
 	cp ${O}/libteec/libteec.so* ${ROOTFS_DIR}/usr/lib
-	cp ${O}/tee-supplicant/tee-supplicant ${ROOTFS_DIR}/usr/local/bin
+	cp ${O}/tee-supplicant/tee-supplicant ${ROOTFS_DIR}/usr/bin
 clean_rootfs:
 	rm -f ${ROOTFS_DIR}/usr/lib/libteec.so*
-	rm -f ${ROOTFS_DIR}/usr/local/bin/tee-supplicant
+	rm -f ${ROOTFS_DIR}/usr/bin/tee-supplicant
 else
 copy_rootfs:
 	@echo Rootfs copy cannot be done because ROOTFS_DIR is not defined
diff --git a/libteec/Makefile b/libteec/Makefile
index 1afa15f..1ce3b3a 100644
--- a/libteec/Makefile
+++ b/libteec/Makefile
@@ -16,12 +16,14 @@
 
 TEEC_SRCS	:= tee_client_api.c \
 		   teec_trace.c
+
 TEEC_SRC_DIR	:= src
 TEEC_OBJ_DIR	:= $(OUT_DIR)
 TEEC_OBJS 	:= $(patsubst %.c,$(TEEC_OBJ_DIR)/%.o, $(TEEC_SRCS))
 TEEC_INCLUDES 	:= \
 		   ${CURDIR}/include \
-		   ${CURDIR}/../public
+		   ${CURDIR}/../public \
+		   ${CURDIR}/../../optee_linuxdriver/include
 
 TEEC_CFLAGS	:= $(addprefix -I, $(TEEC_INCLUDES)) $(CFLAGS) -D_GNU_SOURCE \
 		   -DDEBUGLEVEL_$(CFG_TEE_CLIENT_LOG_LEVEL) \
diff --git a/libteec/include/teec.h b/libteec/include/teec.h
index e8ea445..583b2cb 100644
--- a/libteec/include/teec.h
+++ b/libteec/include/teec.h
@@ -29,38 +29,10 @@
 
 #include <stdint.h>
 #include "tee_client_api.h"
+#include <linux/tee_ioc.h>
 
 #ifndef strlcpy
 #define strlcpy(dst, src, size) snprintf((dst), (size), "%s", (src))
 #endif
 
-/**
- * struct tee_cmd - The command sent to an open tee device.
- * @err: Error code (as in Global Platform TEE Client API spec)
- * @origin: Origin for the error code (also from spec).
- * @cmd: The command to be executed in the trusted application.
- * @uuid: The uuid for the trusted application.
- * @data: The trusted application or memory block.
- * @data_size: The size of the trusted application or memory block.
- * @op: The payload for the trusted application.
- *
- * This structure is mainly used in the Linux kernel for communication
- * with the user space.
- */
-struct tee_cmd {
-	TEEC_Result err;
-	uint32_t origin;
-	uint32_t cmd;
-	TEEC_UUID *uuid;
-	void *data;
-	uint32_t data_size;
-	TEEC_Operation *op;
-};
-
-#define TEE_OPEN_SESSION_IOC _IOWR('t', 161, struct tee_cmd)
-#define TEE_CLOSE_SESSION_IOC _IOWR('t', 162, unsigned long)
-#define TEE_INVOKE_COMMAND_IOC _IOWR('t', 163, struct tee_cmd)
-#define TEE_REQUEST_CANCELLATION_IOC _IOWR('t', 164, struct tee_cmd)
-#define TEE_ALLOC_SHM_IOC _IOWR('t', 165, TEEC_SharedMemory)
-
 #endif
diff --git a/libteec/src/tee_client_api.c b/libteec/src/tee_client_api.c
index 3be8e93..68730de 100644
--- a/libteec/src/tee_client_api.c
+++ b/libteec/src/tee_client_api.c
@@ -43,12 +43,15 @@
 #include <tee_client_api.h>
 #include <malloc.h>
 
-#ifndef TEEC_DEV_PATH
-#define TEEC_DEV_PATH "/dev/teetz"
-#endif
+#define TEE_TZ_DEVICE_NAME "opteearmtz00"
 
 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
 
+/*
+ * Maximum size of the device name
+ */
+#define TEEC_MAX_DEVNAME_SIZE 256
+
 #ifdef _GNU_SOURCE
 static pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
 #else
@@ -71,6 +74,20 @@
 		EMSG("pthread_mutex_unlock failed: %d\n", e);
 }
 
+static void teec_resetTeeCmd(struct tee_cmd_io *cmd)
+{
+	cmd->fd_sess	= -1;
+	cmd->cmd	= 0;
+	cmd->uuid	= NULL;
+	cmd->origin	= TEEC_ORIGIN_API;
+	cmd->err	= TEEC_SUCCESS;
+	cmd->data	= NULL;
+	cmd->data_size	= 0;
+	cmd->op		= NULL;
+}
+
+
+
 /*
  * This function initializes a new TEE Context, connecting this Client
  * application to the TEE indentified by the name name.
@@ -80,6 +97,9 @@
 TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context)
 {
 	int name_size = 0;
+	const char* _name = name;
+
+	INMSG("%s", name);
 
 	if (context == NULL)
 		return TEEC_ERROR_BAD_PARAMETERS;
@@ -89,12 +109,10 @@
 	 * on a predefined TEE.
 	 */
 	if (name == NULL)
-		name_size = strlcpy(context->devname, TEEC_DEV_PATH,
-				    TEEC_MAX_DEVNAME_SIZE);
-	else {
-		name_size = snprintf(context->devname, TEEC_MAX_DEVNAME_SIZE,
-				     "/dev/%s", name);
-	}
+		_name = TEE_TZ_DEVICE_NAME;
+
+	name_size = snprintf(context->devname, TEEC_MAX_DEVNAME_SIZE,
+			     "/dev/%s", _name);
 
 	if (name_size >= TEEC_MAX_DEVNAME_SIZE)
 		return TEEC_ERROR_BAD_PARAMETERS; /* Device name truncated */
@@ -103,6 +121,7 @@
 	if (context->fd == -1)
 		return TEEC_ERROR_ITEM_NOT_FOUND;
 
+	OUTMSG("");
 	return TEEC_SUCCESS;
 }
 
@@ -125,6 +144,7 @@
 TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context,
 				      TEEC_SharedMemory *shared_memory)
 {
+	struct tee_shm_io shm;
 	size_t size;
 	uint32_t flags;
 
@@ -137,11 +157,20 @@
 	shared_memory->size = size;
 	shared_memory->flags = flags;
 
-	if (ioctl(context->fd, TEE_ALLOC_SHM_IOC, shared_memory) != 0) {
+	shm.buffer = NULL;
+	shm.size   = size;
+	shm.registered = 0;
+	shm.fd_shm = 0;
+	shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
+	if (ioctl(context->fd, TEE_ALLOC_SHM_IOC, &shm) != 0) {
 		EMSG("Ioctl(TEE_ALLOC_SHM_IOC) failed! (%s)\n",
 		     strerror(errno));
 		return TEEC_ERROR_OUT_OF_MEMORY;
 	}
+	DMSG("fd %d size %zu", shared_memory->d.fd, shared_memory->size);
+
+	shared_memory->size = size;
+	shared_memory->d.fd = shm.fd_shm;
 
 	/*
 	 * Map memory to current user space process.
@@ -174,13 +203,15 @@
  */
 void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *shared_memory)
 {
-	if (shared_memory == NULL)
+	if (!shared_memory)
+		return;
+
+	if (shared_memory->registered)
 		return;
 
 	if (shared_memory->d.fd != 0) {
-		if (shared_memory->registered == 0)
-			munmap(shared_memory->buffer, shared_memory->size);
-
+		munmap(shared_memory->buffer, (shared_memory->size ==
+			     0) ? 8 : shared_memory->size);
 		close(shared_memory->d.fd);
 		shared_memory->d.fd = 0;
 	}
@@ -194,17 +225,9 @@
 TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
 				      TEEC_SharedMemory *shared_memory)
 {
-	if (context == NULL || shared_memory == NULL || shared_memory->buffer
-	    == NULL)
+	if (!context || !shared_memory)
 		return TEEC_ERROR_BAD_PARAMETERS;
 
-	if (ioctl(context->fd, TEE_ALLOC_SHM_IOC, shared_memory) != 0)
-		/*
-		 * The buffer not repect platform constraints (not continuous)
-		 * and thus can't be used with zero-copy.
-		 */
-		shared_memory->d.fd = 0;
-
 	shared_memory->registered = 1;
 	return TEEC_SUCCESS;
 }
@@ -224,11 +247,10 @@
 			     TEEC_Operation *operation, uint32_t *error_origin)
 {
 	TEEC_Operation dummy_op;
-	struct tee_cmd tc;
 	uint32_t origin = TEEC_ORIGIN_API;
 	TEEC_Result res = TEEC_SUCCESS;
-	void *ta = NULL;
 	(void)connection_data;
+	struct tee_cmd_io cmd;
 
 	if (session != NULL)
 		session->fd = -1;
@@ -238,33 +260,13 @@
 		goto error;
 	}
 
-	/* Check that context->fd is a valid file descriptor */
-	session->fd = dup(context->fd);
-	if (session->fd == -1) {
-		res = TEEC_ERROR_BAD_PARAMETERS;
-		goto error;
-	}
-	close(session->fd);
-	session->fd = -1;
-
 	if (connection_method != TEEC_LOGIN_PUBLIC) {
 		res = TEEC_ERROR_NOT_SUPPORTED;
 		goto error;
 	}
 
-	memset(&tc, 0, sizeof(struct tee_cmd));
-
-	/*
-	 * Save the fd in the session for later use when invoke command and
-	 * close the session.
-	 */
-	session->fd = open(context->devname, O_RDWR);
-	if (session->fd == -1) {
-		res = TEEC_ERROR_BAD_PARAMETERS;
-		goto error;
-	}
-
-	tc.uuid = (TEEC_UUID *)destination;
+	teec_resetTeeCmd(&cmd);
+	cmd.uuid = (TEEC_UUID *)destination;
 
 	if (operation == NULL) {
 		/*
@@ -278,25 +280,35 @@
 		operation = &dummy_op;
 	}
 
-	tc.op = operation;
+	cmd.op = operation;
 
-	if (ioctl(session->fd, TEE_OPEN_SESSION_IOC, &tc) != 0) {
+	errno = 0;
+	if (ioctl(context->fd, TEE_OPEN_SESSION_IOC, &cmd) != 0) {
 		EMSG("Ioctl(TEE_OPEN_SESSION_IOC) failed! (%s)\n",
 		     strerror(errno));
-		origin = TEEC_ORIGIN_COMMS;
-		res = TEEC_ERROR_COMMUNICATION;
+		if (cmd.origin)
+			origin = cmd.origin;
+		else
+			origin = TEEC_ORIGIN_COMMS;
+		if (cmd.err)
+			res = cmd.err;
+		else
+			res = TEEC_ERROR_COMMUNICATION;
 		goto error;
 	}
+	session->fd = cmd.fd_sess;
 
-	if (tc.err != 0) {
-		EMSG("UUID %x %x %x can't be loaded !!!\n",
+	if (cmd.err != 0) {
+		EMSG("open session to TA UUID %x %x %x failed\n",
 		     destination->timeLow,
 		     destination->timeMid, destination->timeHiAndVersion);
 	}
-	origin = tc.origin;
-	res = tc.err;
+	origin = cmd.origin;
+	res = cmd.err;
 
 error:
+	// printf("**** res=0x%08x, org=%d, seeid=%d ***\n", res, origin, cmd.fd_sess)
+
 	/*
 	 * We do this check at the end instead of checking on every place where
 	 * we set the error origin.
@@ -311,9 +323,6 @@
 	if (error_origin != NULL)
 		*error_origin = origin;
 
-	if (ta)
-		free(ta);
-
 	return res;
 }
 
@@ -323,15 +332,9 @@
  */
 void TEEC_CloseSession(TEEC_Session *session)
 {
-	uint32_t dummyvalue = 0;
-
 	if (session == NULL)
 		return;
 
-	if (ioctl(session->fd, TEE_CLOSE_SESSION_IOC, &dummyvalue) != 0)
-		EMSG("Ioctl(TEE_CLOSE_SESSION_IOC) failed! (%s)\n",
-		     strerror(errno));
-
 	close(session->fd);
 }
 
@@ -344,7 +347,7 @@
 			       uint32_t *error_origin)
 {
 	INMSG("session: [%p], cmd_id: [%d]", session, cmd_id);
-	struct tee_cmd tc;
+	struct tee_cmd_io cmd;
 	TEEC_Operation dummy_op;
 	TEEC_Result result = TEEC_SUCCESS;
 	uint32_t origin = TEEC_ORIGIN_API;
@@ -371,12 +374,12 @@
 	operation->session = session;
 	teec_mutex_unlock(&mutex);
 
-	memset(&tc, 0, sizeof(struct tee_cmd));
+	teec_resetTeeCmd(&cmd);
 
-	tc.cmd = cmd_id;
-	tc.op = operation;
+	cmd.cmd = cmd_id;
+	cmd.op = operation;
 
-	if (ioctl(session->fd, TEE_INVOKE_COMMAND_IOC, &tc) != 0)
+	if (ioctl(session->fd, TEE_INVOKE_COMMAND_IOC, &cmd) != 0)
 		EMSG("Ioctl(TEE_INVOKE_COMMAND_IOC) failed! (%s)\n",
 		     strerror(errno));
 
@@ -388,8 +391,8 @@
 		teec_mutex_unlock(&mutex);
 	}
 
-	origin = tc.origin;
-	result = tc.err;
+	origin = cmd.origin;
+	result = cmd.err;
 
 error:
 
@@ -401,7 +404,7 @@
 
 void TEEC_RequestCancellation(TEEC_Operation *operation)
 {
-	struct tee_cmd tc;
+	struct tee_cmd_io cmd;
 	TEEC_Session *session;
 
 	if (operation == NULL)
@@ -414,11 +417,11 @@
 	if (session == NULL)
 		return;
 
-	memset(&tc, 0, sizeof(struct tee_cmd));
+	teec_resetTeeCmd(&cmd);
 
-	tc.op = operation;
+	cmd.op = operation;
 
-	if (ioctl(session->fd, TEE_REQUEST_CANCELLATION_IOC, &tc) != 0)
+	if (ioctl(session->fd, TEE_REQUEST_CANCELLATION_IOC, &cmd) != 0)
 		EMSG("Ioctl(TEE_REQUEST_CANCELLATION_IOC) failed! (%s)\n",
 		     strerror(errno));
 }
diff --git a/public/tee_client_api.h b/public/tee_client_api.h
index 70c739a..17ce6e5 100644
--- a/public/tee_client_api.h
+++ b/public/tee_client_api.h
@@ -24,40 +24,28 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-#ifndef _TEE_CLIENT_API_H
-#define _TEE_CLIENT_API_H
+#ifndef TEE_CLIENT_API_H
+#define TEE_CLIENT_API_H
 
-#include <stddef.h>
+#ifndef __KERNEL__
 #include <stdint.h>
-#include <string.h>
-
-#define TEEC_VERSION    0x0100
-
+#include <stddef.h>
+#endif /* __KERNEL__ */
 
 /*
  * Defines the number of available memory references in an open session or
  * invoke command operation payload.
  */
-#define TEEC_PAYLOAD_REF_COUNT 4
+#define TEEC_CONFIG_PAYLOAD_REF_COUNT 4
 
-/*
+/**
  * Defines the maximum size of a single shared memory block, in bytes, of both
  * API allocated and API registered memory. The size is currently set to
  * 512 * kB (512 * 1024).
  */
 #define TEEC_CONFIG_SHAREDMEM_MAX_SIZE 0x8000
 
-/*
- * Flag constants indicating which of the memory references in an open session
- * or invoke command operation payload (TEEC_Operation) that are used. Type is
- * uint32_t.
- */
-#define TEEC_MEMREF_0_USED  0x00000001
-#define TEEC_MEMREF_1_USED  0x00000002
-#define TEEC_MEMREF_2_USED  0x00000004
-#define TEEC_MEMREF_3_USED  0x00000008
-
-/*
+/**
  * Flag constants indicating the type of parameters encoded inside the
  * operation payload (TEEC_Operation), Type is uint32_t.
  *
@@ -123,7 +111,7 @@
 #define TEEC_MEMREF_PARTIAL_OUTPUT  0x0000000E
 #define TEEC_MEMREF_PARTIAL_INOUT   0x0000000F
 
-/*
+/**
  * Flag constants indicating the data transfer direction of memory in
  * TEEC_Parameter. TEEC_MEM_INPUT signifies data transfer direction from the
  * client application to the TEE. TEEC_MEM_OUTPUT signifies data transfer
@@ -137,7 +125,7 @@
 #define TEEC_MEM_INPUT   0x00000001
 #define TEEC_MEM_OUTPUT  0x00000002
 
-/*
+/**
  * Return values. Type is TEEC_Result
  *
  * TEEC_SUCCESS                 The operation was successful.
@@ -162,9 +150,11 @@
  * TEEC_ERROR_SECURITY          A security fault was detected.
  * TEEC_ERROR_SHORT_BUFFER      The supplied buffer is too short for the
  *                              generated output.
+ * TEEC_ERROR_TARGET_DEAD       Trusted Application has panicked
+ *                              during the operation.
  */
 
-/*
+/**
  *  Standard defined error codes.
  */
 #define TEEC_SUCCESS                0x00000000
@@ -185,8 +175,9 @@
 #define TEEC_ERROR_COMMUNICATION    0xFFFF000E
 #define TEEC_ERROR_SECURITY         0xFFFF000F
 #define TEEC_ERROR_SHORT_BUFFER     0xFFFF0010
+#define TEEC_ERROR_TARGET_DEAD      0xFFFF3024
 
-/*
+/**
  * Function error origins, of type TEEC_ErrorOrigin. These indicate where in
  * the software stack a particular return value originates from.
  *
@@ -204,9 +195,9 @@
 #define TEEC_ORIGIN_TEE          0x00000003
 #define TEEC_ORIGIN_TRUSTED_APP  0x00000004
 
-/*
+/**
  * Session login methods, for use in TEEC_OpenSession() as parameter
- * connection_method. Type is uint32_t.
+ * connectionMethod. Type is uint32_t.
  *
  * TEEC_LOGIN_PUBLIC       No login data is provided.
  * TEEC_LOGIN_USER         Login data about the user running the Client
@@ -221,7 +212,7 @@
 #define TEEC_LOGIN_GROUP        0x00000002
 #define TEEC_LOGIN_APPLICATION  0x00000004
 
-/*
+/**
  * Encode the paramTypes according to the supplied types.
  *
  * @param p0 The first param type.
@@ -232,7 +223,7 @@
 #define TEEC_PARAM_TYPES(p0, p1, p2, p3) \
 	((p0) | ((p1) << 4) | ((p2) << 8) | ((p3) << 12))
 
-/*
+/**
  * Get the i_th param type from the paramType.
  *
  * @param p The paramType.
@@ -240,24 +231,18 @@
  */
 #define TEEC_PARAM_TYPE_GET(p, i) (((p) >> (i * 4)) & 0xF)
 
-/*
- * Maximum size of the device name
- */
-#define TEEC_MAX_DEVNAME_SIZE 256
-
 typedef uint32_t TEEC_Result;
-typedef uint32_t TEEC_ErrorOrigin;
 
-/*
+/**
  * struct TEEC_Context - Represents a connection between a client application
  * and a TEE.
  */
 typedef struct {
-	char devname[TEEC_MAX_DEVNAME_SIZE];
+	char devname[256];
 	int fd;
 } TEEC_Context;
 
-/*
+/**
  * This type contains a Universally Unique Resource Identifier (UUID) type as
  * defined in RFC4122. These UUID values are used to identify Trusted
  * Applications.
@@ -269,7 +254,7 @@
 	uint8_t clockSeqAndNode[8];
 } TEEC_UUID;
 
-/*
+/**
  * struct TEEC_SharedMemory - Memory to transfer data between a client
  * application and trusted code.
  *
@@ -297,7 +282,7 @@
 	uint8_t registered;
 } TEEC_SharedMemory;
 
-/*
+/**
  * struct TEEC_TempMemoryReference - Temporary memory to transfer data between
  * a client application and trusted code, only used for the duration of the
  * operation.
@@ -314,7 +299,7 @@
 	size_t size;
 } TEEC_TempMemoryReference;
 
-/*
+/**
  * struct TEEC_RegisteredMemoryReference - use a pre-registered or
  * pre-allocated shared memory block of memory to transfer data between
  * a client application and trusted code.
@@ -335,7 +320,7 @@
 	size_t offset;
 } TEEC_RegisteredMemoryReference;
 
-/*
+/**
  * struct TEEC_Value - Small raw data container
  *
  * Instead of allocating a shared memory buffer this structure can be used
@@ -350,7 +335,7 @@
 	uint32_t b;
 } TEEC_Value;
 
-/*
+/**
  * union TEEC_Parameter - Memory container to be used when passing data between
  *                        client application and trusted code.
  *
@@ -370,7 +355,7 @@
 	TEEC_Value value;
 } TEEC_Parameter;
 
-/*
+/**
  * struct TEEC_Session - Represents a connection between a client application
  * and a trusted application.
  */
@@ -378,7 +363,7 @@
 	int fd;
 } TEEC_Session;
 
-/*
+/**
  * struct TEEC_Operation - Holds information and memory references used in
  * TEEC_InvokeCommand().
  *
@@ -395,14 +380,14 @@
 typedef struct {
 	uint32_t started;
 	uint32_t paramTypes;
-	TEEC_Parameter params[TEEC_PAYLOAD_REF_COUNT];
+	TEEC_Parameter params[TEEC_CONFIG_PAYLOAD_REF_COUNT];
 	/* Implementation-Defined */
 	TEEC_Session *session;
-	TEEC_SharedMemory mem_refs[TEEC_PAYLOAD_REF_COUNT];
+	TEEC_SharedMemory memRefs[TEEC_CONFIG_PAYLOAD_REF_COUNT];
 	uint32_t flags;
 } TEEC_Operation;
 
-/*
+/**
  * TEEC_InitializeContext() - Initializes a context holding connection
  * information on the specific TEE, designated by the name string.
 
@@ -418,7 +403,7 @@
  */
 TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context);
 
-/*
+/**
  * TEEC_FinalizeContext() - Destroys a context holding connection information
  * on the specific TEE.
  *
@@ -435,7 +420,7 @@
  */
 TEEC_Result TEEC_FinalizeContext(TEEC_Context *context);
 
-/*
+/**
  * TEEC_OpenSession() - Opens a new session with the specified trusted
  *                      application.
  *
@@ -445,15 +430,15 @@
  * @param destination        A structure identifying the trusted application
  *                           with which to open a session.
  *
- * @param connection_method  The connection method to use.
- * @param connection_data    Any data necessary to connect with the chosen
+ * @param connectionMethod   The connection method to use.
+ * @param connectionData     Any data necessary to connect with the chosen
  *                           connection method. Not supported, should be set to
  *                           NULL.
  * @param operation          An operation structure to use in the session. May
  *                           be set to NULL to signify no operation structure
  *                           needed.
  *
- * @param return_origin      A parameter which will hold the error origin if
+ * @param returnOrigin       A parameter which will hold the error origin if
  *                           this function returns any value other than
  *                           TEEC_SUCCESS.
  *
@@ -464,12 +449,12 @@
 TEEC_Result TEEC_OpenSession(TEEC_Context *context,
 			     TEEC_Session *session,
 			     const TEEC_UUID *destination,
-			     uint32_t connection_method,
-			     const void *connection_data,
+			     uint32_t connectionMethod,
+			     const void *connectionData,
 			     TEEC_Operation *operation,
-			     uint32_t *return_origin);
+			     uint32_t *returnOrigin);
 
-/*
+/**
  * TEEC_CloseSession() - Closes the session which has been opened with the
  * specific trusted application.
  *
@@ -477,65 +462,65 @@
  */
 void TEEC_CloseSession(TEEC_Session *session);
 
-/*
+/**
  * TEEC_InvokeCommand() - Executes a command in the specified trusted
  * application.
  *
  * @param session        A handle to an open connection to the trusted
  *                       application.
- * @param cmd_id         Identifier of the command in the trusted application
+ * @param commandID      Identifier of the command in the trusted application
  *                       to invoke.
  * @param operation      An operation structure to use in the invoke command.
  *                       May be set to NULL to signify no operation structure
  *                       needed.
- * @param return_origin  A parameter which will hold the error origin if this
+ * @param returnOrigin   A parameter which will hold the error origin if this
  *                       function returns any value other than TEEC_SUCCESS.
  *
  * @return TEEC_SUCCESS  OpenSession successfully opened a new session.
  * @return TEEC_Result   Something failed.
  */
 TEEC_Result TEEC_InvokeCommand(TEEC_Session *session,
-			       uint32_t cmd_id,
+			       uint32_t commandID,
 			       TEEC_Operation *operation,
-			       uint32_t *return_origin);
+			       uint32_t *returnOrigin);
 
-/*
+/**
  * TEEC_RegisterSharedMemory() - Register a block of existing memory as a
  * shared block within the scope of the specified context.
  *
- * @param context     The initialized TEE context structure in which scope to
- *                    open the session.
- * @param shared_mem  pointer to the shared memory structure to register.
+ * @param context    The initialized TEE context structure in which scope to
+ *                   open the session.
+ * @param sharedMem  pointer to the shared memory structure to register.
  *
  * @return TEEC_SUCCESS              The registration was successful.
  * @return TEEC_ERROR_OUT_OF_MEMORY  Memory exhaustion.
  * @return TEEC_Result               Something failed.
  */
 TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
-				      TEEC_SharedMemory *shared_mem);
+				      TEEC_SharedMemory *sharedMem);
 
-/*
+/**
  * TEEC_AllocateSharedMemory() - Allocate shared memory for TEE.
  *
  * @param context     The initialized TEE context structure in which scope to
  *                    open the session.
- * @param shared_mem  Pointer to the allocated shared memory.
+ * @param sharedMem   Pointer to the allocated shared memory.
  *
  * @return TEEC_SUCCESS              The registration was successful.
  * @return TEEC_ERROR_OUT_OF_MEMORY  Memory exhaustion.
  * @return TEEC_Result               Something failed.
  */
 TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context,
-				      TEEC_SharedMemory *shared_mem);
+				      TEEC_SharedMemory *sharedMem);
 
-/*
+/**
  * TEEC_ReleaseSharedMemory() - Free or deregister the shared memory.
  *
- * @param shared_mem  Pointer to the shared memory to be freed.
+ * @param sharedMem  Pointer to the shared memory to be freed.
  */
-void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *shared_memory);
+void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMemory);
 
-/*
+/**
  * TEEC_RequestCancellation() - Request the cancellation of a pending open
  *                              session or command invocation.
  *
@@ -544,12 +529,10 @@
  */
 void TEEC_RequestCancellation(TEEC_Operation *operation);
 
-/*
+/**
  * Register a pre-allocated Trusted Application This is mainly intended for
  * OS-FREE contexts or when a filesystem is not available.
  *
- * This function is an extension to TEE Client API.
- *
  * @param ta   Pointer to the trusted application binary
  * @param size The size of the TA binary
  *
@@ -558,12 +541,10 @@
  */
 TEEC_Result TEEC_RegisterTA(const void *ta, const size_t size);
 
-/*
+/**
  * Unregister a pre-allocated Trusted Application This is mainly intended for
  * OS-FREE contexts or when a filesystem is not available.
  *
- * This function is an extension to TEE Client API.
- *
  * @param ta Pointer to the trusted application binary
  */
 void TEEC_UnregisterTA(const void *ta);
diff --git a/tee-supplicant/Makefile b/tee-supplicant/Makefile
index e917da7..2f666d6 100644
--- a/tee-supplicant/Makefile
+++ b/tee-supplicant/Makefile
@@ -11,8 +11,8 @@
 PACKAGE_NAME	:= tee-supplicant
 
 TEES_SRCS	:= tee_supplicant.c \
-		   tee_supp_fs.c \
 		   teec_ta_load.c \
+		   tee_supp_fs.c \
 		   handle.c
 
 TEES_SRC_DIR	:= src
@@ -20,7 +20,8 @@
 TEES_OBJS 	:= $(patsubst %.c,$(TEES_OBJ_DIR)/%.o, $(TEES_SRCS))
 TEES_INCLUDES 	:= ${CURDIR}/../libteec/include \
 		   ${CURDIR}/src \
-		   ${CURDIR}/../public
+		   ${CURDIR}/../public \
+		   ${CURDIR}/../../optee_linuxdriver/include
 
 TEES_CFLAGS	:= $(addprefix -I, $(TEES_INCLUDES)) $(CFLAGS) \
 		   -DDEBUGLEVEL_$(CFG_TEE_SUPP_LOG_LEVEL) \
diff --git a/tee-supplicant/src/tee_supp_fs.c b/tee-supplicant/src/tee_supp_fs.c
index 9ab17ad..bebbfda 100644
--- a/tee-supplicant/src/tee_supp_fs.c
+++ b/tee-supplicant/src/tee_supp_fs.c
@@ -31,13 +31,10 @@
 #include <dirent.h>
 #include <unistd.h>
 #include <string.h>
-#include <limits.h>
 #include <tee_supp_fs.h>
 #include <pthread.h>
 #include "handle.h"
 
-#include <assert.h>
-
 /*
  * Operations and defines shared with TEE.
  */
diff --git a/tee-supplicant/src/tee_supplicant.c b/tee-supplicant/src/tee_supplicant.c
index 65ce085..1e1c8d8 100644
--- a/tee-supplicant/src/tee_supplicant.c
+++ b/tee-supplicant/src/tee_supplicant.c
@@ -47,13 +47,16 @@
 #include <tee_supp_fs.h>
 #include <teec.h>
 
-#include <assert.h>
-
-#define BUFFER_LENGTH 0x100
 #define TEE_RPC_BUFFER_NUMBER 5
 
 /* Flags of the shared memory. Also defined in tee_service.h in the kernel. */
-#define SHM_ALLOCATE_FROM_PHYSICAL 0x100
+/*
+ * Maximum size of the device name
+ */
+#define TEEC_MAX_DEVNAME_SIZE 256
+
+char devname1[TEEC_MAX_DEVNAME_SIZE];
+char devname2[TEEC_MAX_DEVNAME_SIZE];
 
 struct tee_rpc_cmd {
 	void *buffer;
@@ -74,8 +77,6 @@
 	uint32_t supp_ta_handle;
 };
 
-static enum tee_target tee_target = TEE_TARGET_UNKNOWN;
-
 static bool read_request(int fd, struct tee_rpc_invoke *request);
 static void write_response(int fd, struct tee_rpc_invoke *request);
 static void free_param(TEEC_SharedMemory *shared_mem);
@@ -127,6 +128,7 @@
 
 static TEEC_SharedMemory *add_shared_memory(int fd, size_t size)
 {
+	struct tee_shm_io shm;
 	TEEC_SharedMemory *shared_mem;
 	struct share_mem_entry *entry;
 
@@ -135,14 +137,21 @@
 		return NULL;
 
 	shared_mem = &entry->shared_mem;
-	shared_mem->size = size;
 
-	if (ioctl(fd, TEE_ALLOC_SHM_IOC, shared_mem) != 0) {
+	shm.buffer = NULL;
+	shm.size   = size;
+	shm.registered = 0;
+	shm.fd_shm = 0;
+	shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
+	if (ioctl(fd, TEE_ALLOC_SHM_IOC, &shm) != 0) {
 		EMSG("Ioctl(TEE_ALLOC_SHM_IOC) failed! (%s)", strerror(errno));
 		shared_mem = NULL;
 		goto out;
 	}
 
+	shared_mem->size = size;
+	shared_mem->d.fd = shm.fd_shm;
+
 	shared_mem->buffer = mmap(NULL, size,
 				  PROT_READ | PROT_WRITE, MAP_SHARED,
 				  shared_mem->d.fd, 0);
@@ -166,22 +175,32 @@
 static int get_param(int fd, struct tee_rpc_invoke *inv, const uint32_t idx,
 		     TEEC_SharedMemory *shared_mem)
 {
+	struct tee_shm_io shm;
+
 	if (idx >= inv->nbr_bf)
 		return -1;
 
-	memset(shared_mem, 0, sizeof(TEEC_SharedMemory));
-	shared_mem->size = inv->cmds[idx].size;
-	shared_mem->flags |= SHM_ALLOCATE_FROM_PHYSICAL;
-	shared_mem->buffer = inv->cmds[idx].buffer;
-	if (ioctl(fd, TEE_ALLOC_SHM_IOC, shared_mem) != 0) {
+	shm.buffer = inv->cmds[idx].buffer;
+	shm.size   = inv->cmds[idx].size;
+	shm.registered = 0;
+	shm.fd_shm = 0;
+	shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
+
+	if (ioctl(fd, TEE_GET_FD_FOR_RPC_SHM_IOC, &shm) != 0) {
 		EMSG("Ioctl(TEE_ALLOC_SHM_IOC) failed! (%s)", strerror(errno));
 		return -1;
 	}
 
+	memset(shared_mem, 0, sizeof(TEEC_SharedMemory));
+	shared_mem->size = shm.size;
+	shared_mem->flags = shm.flags;
+	shared_mem->d.fd = shm.fd_shm;
+
+	DMSG("size %zu fd_shm %d", shared_mem->size, shared_mem->d.fd);
+
 	shared_mem->buffer = mmap(NULL, shared_mem->size,
 				     PROT_READ | PROT_WRITE, MAP_SHARED,
 				     shared_mem->d.fd, 0);
-	shared_mem->flags &= (~SHM_ALLOCATE_FROM_PHYSICAL);
 
 	if (shared_mem->buffer == (void *)MAP_FAILED) {
 		dprintf(TRACE_ERROR, "mmap(%d, %p) failed - Error = %s\n",
@@ -271,7 +290,10 @@
 	}
 	cmd = (struct tee_rpc_ta *)shared_mem.buffer;
 
-	ta_found = TEECI_LoadSecureModule(tee_target, &cmd->uuid, &ta, &size);
+	ta_found = TEECI_LoadSecureModule(devname1, &cmd->uuid, &ta, &size);
+	/* Tracked by 6408 */
+	if (ta_found != TA_BINARY_FOUND)
+		ta_found = TEECI_LoadSecureModule(devname2, &cmd->uuid, &ta, &size);
 
 	if (ta_found == TA_BINARY_FOUND) {
 		TEEC_SharedMemory *ta_shm = alloc_param(fd, inv, 1, size);
@@ -305,6 +327,7 @@
 	/* TODO This parameter should come as a value parameter instead. */
 	fd = (int)(uintptr_t)inv->cmds[0].buffer;
 	free_shared_memory_with_fd(fd);
+	inv->cmds[0].buffer = NULL;
 	inv->res = TEEC_SUCCESS;
 	OUTMSG();
 }
@@ -353,31 +376,27 @@
 {
 	int fd;
 	int n = 0;
-	char devname[TEEC_MAX_DEVNAME_SIZE];
-	memset(&devname, 0, sizeof(devname));
+	char devpath[TEEC_MAX_DEVNAME_SIZE];
+
+	sprintf(devpath, "/dev/opteearmtz00");
+	sprintf(devname1, "optee_armtz");
+	sprintf(devname2, "teetz");
 
 	while (--argc) {
 		n++;
-		if ((strlen(argv[n]) == 5) &&
-		    (strncmp(argv[n], "teetz", 5) == 0)) {
-			tee_target = TEE_TARGET_TZ;
-			snprintf(devname, TEEC_MAX_DEVNAME_SIZE, "%s",
-				 "/dev/teetz");
+		if (strncmp(argv[n], "opteearmtz00", 12) == 0) {
+			snprintf(devpath, TEEC_MAX_DEVNAME_SIZE, "%s", "/dev/opteearmtz00");
+			snprintf(devname1, TEEC_MAX_DEVNAME_SIZE, "%s", "optee_armtz");
+			snprintf(devname2, TEEC_MAX_DEVNAME_SIZE, "%s", "teetz");
 		} else {
 			EMSG("Invalid argument #%d", n);
 			exit(EXIT_FAILURE);
 		}
 	}
 
-	/* If no arguments have been given, then we default to LX. */
-	if (tee_target == TEE_TARGET_UNKNOWN) {
-		tee_target = TEE_TARGET_TZ;
-		sprintf(devname, "/dev/teetz");
-	}
-
-	fd = open(devname, O_RDWR);
+	fd = open(devpath, O_RDWR);
 	if (fd < 0) {
-		EMSG("error opening [%s]", devname);
+		EMSG("error opening [%s]", devpath);
 		exit(EXIT_FAILURE);
 	}
 
@@ -386,7 +405,7 @@
 		exit(EXIT_FAILURE);
 	}
 
-	IMSG("tee-supplicant running on %s", devname);
+	IMSG("tee-supplicant running on %s", devpath);
 
 	while (true) {
 		struct tee_rpc_invoke request;
@@ -439,7 +458,7 @@
 		return false;
 	}
 
-	res = read(fd, request, BUFFER_LENGTH);
+	res = read(fd, request, sizeof(*request));
 	if (res < sizeof(*request) - sizeof(request->cmds)) {
 		EMSG("error reading from driver");
 		return false;
diff --git a/tee-supplicant/src/teec_ta_load.c b/tee-supplicant/src/teec_ta_load.c
index 09b7e66..3314fac 100644
--- a/tee-supplicant/src/teec_ta_load.c
+++ b/tee-supplicant/src/teec_ta_load.c
@@ -35,10 +35,6 @@
 #include <teec_trace.h>
 #include <teec_ta_load.h>
 
-#ifndef TEEC_DEV_PATH
-#define TEEC_DEV_PATH "/dev/tee"
-#endif
-
 #ifndef TEEC_LOAD_PATH
 #define TEEC_LOAD_PATH "/lib"
 #endif
@@ -47,8 +43,6 @@
 #define PATH_MAX 255
 #endif
 
-static const char ta_path_tz[] = "teetz";
-
 struct tee_rpc_cmd {
 	void *buffer;
 	uint32_t size;
@@ -69,20 +63,14 @@
  *
  * @return              0 if TA was found, otherwise -1.
  */
-int TEECI_LoadSecureModule(enum tee_target target,
+int TEECI_LoadSecureModule(const char* dev_path,
 			   const TEEC_UUID *destination, void **ta,
 			   size_t *ta_size)
 {
 	char fname[PATH_MAX];
-	char const *p;
 	FILE *file = NULL;
 	int n;
 
-	if (target == TEE_TARGET_TZ)
-		p = ta_path_tz;
-	else
-		return TA_BINARY_NOT_FOUND;
-
 	if (!ta_size || !ta || !destination) {
 		printf("wrong inparameter to TEECI_LoadSecureModule\n");
 		return TA_BINARY_NOT_FOUND;
@@ -90,7 +78,7 @@
 
 	n = snprintf(fname, PATH_MAX,
 		     "%s/%s/%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x.ta",
-		     TEEC_LOAD_PATH, p,
+		     TEEC_LOAD_PATH, dev_path,
 		     destination->timeLow,
 		     destination->timeMid,
 		     destination->timeHiAndVersion,
@@ -103,8 +91,10 @@
 		     destination->clockSeqAndNode[6],
 		     destination->clockSeqAndNode[7]);
 
+	DMSG("Attempt to load %s", fname);
+
 	if ((n < 0) || (n >= PATH_MAX)) {
-		printf("wrong TA path in TEECI_LoadSecureModule\n");
+		EMSG("wrong TA path [%s]", fname);
 		return TA_BINARY_NOT_FOUND;
 	}
 
diff --git a/tee-supplicant/src/teec_ta_load.h b/tee-supplicant/src/teec_ta_load.h
index 232621d..06dbb5f 100644
--- a/tee-supplicant/src/teec_ta_load.h
+++ b/tee-supplicant/src/teec_ta_load.h
@@ -28,12 +28,6 @@
 #define _TEEC_TA_LOAD_H
 #include <tee_client_api.h>
 
-/* store target TEE instance */
-enum tee_target {
-	TEE_TARGET_TZ,
-	TEE_TARGET_UNKNOWN,
-};
-
 #define TA_BINARY_FOUND 0
 #define TA_BINARY_NOT_FOUND -1
 
@@ -50,7 +44,7 @@
  *
  * @return              0 if TA was found, otherwise -1.
  */
-int TEECI_LoadSecureModule(enum tee_target target,
+int TEECI_LoadSecureModule(const char *name,
 			   const TEEC_UUID *destination, void **ta,
 			   size_t *ta_size);
 #endif