summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Dynowski <k.dynowski@samsung.com>2017-09-19 15:50:21 +0200
committerKrzysztof Dynowski <k.dynowski@samsung.com>2017-09-26 16:16:45 +0200
commitd387ec2d126a196eba9c8746f1da09257f55b453 (patch)
tree15dc11cd1deccc225b5824ef88e7ce5b6e477a88
parent2e965bbe41a43690cf9533cf12e05422652d05a1 (diff)
downloadlibteec-d387ec2d126a196eba9c8746f1da09257f55b453.tar.gz
libteec-d387ec2d126a196eba9c8746f1da09257f55b453.tar.bz2
libteec-d387ec2d126a196eba9c8746f1da09257f55b453.zip
Change-Id: I606e6890c46ba2c438c6b76fa45b2e968d6f1e5c
-rw-r--r--api/tef/tee_client_api.h2
-rw-r--r--packaging/tef-libteec.spec1
-rw-r--r--src/optee/creators.c16
-rw-r--r--src/optee/creators.h1
-rw-r--r--src/simulator/creators.c29
-rw-r--r--src/simulator/creators.h1
-rw-r--r--src/tef_libteec.c126
7 files changed, 124 insertions, 52 deletions
diff --git a/api/tef/tee_client_api.h b/api/tef/tee_client_api.h
index c3c637a..6258375 100644
--- a/api/tef/tee_client_api.h
+++ b/api/tef/tee_client_api.h
@@ -524,7 +524,7 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
*
* @param[in] context The initialized TEE context structure in which scope to
* open the session.
- * @param[in] sharedMem Pointer to the allocated shared memory.
+ * @param[in,out] sharedMem Pointer to the allocated shared memory.
*
* @return TEEC_SUCCESS on success, or another Return Code on failure
* @retval TEEC_ERROR_OUT_OF_MEMORY Memory exhaustion.
diff --git a/packaging/tef-libteec.spec b/packaging/tef-libteec.spec
index c6e65e5..3e7e463 100644
--- a/packaging/tef-libteec.spec
+++ b/packaging/tef-libteec.spec
@@ -51,4 +51,3 @@ The package provides Global Platform client API development files.
%files devel
%{_includedir}/tef
%{_libdir}/pkgconfig/tef-libteec.pc
-%{_libdir}/libteec.so*
diff --git a/src/optee/creators.c b/src/optee/creators.c
index 8c580bb..94a2a83 100644
--- a/src/optee/creators.c
+++ b/src/optee/creators.c
@@ -19,15 +19,23 @@
#include "tee_client_api.h"
#include <stdlib.h>
-void *optee_TEEC_Context() {
+void *optee_TEEC_Context()
+{
return malloc(sizeof(TEEC_Context));
}
-void *optee_TEEC_Session() {
+void *optee_TEEC_Session()
+{
return malloc(sizeof(TEEC_Session));
}
-void *optee_TEEC_Operation() {
+void *optee_TEEC_Operation()
+{
return malloc(sizeof(TEEC_Operation));
}
-void *optee_TEEC_SharedMemory() {
+void *optee_TEEC_SharedMemory()
+{
return malloc(sizeof(TEEC_SharedMemory));
}
+uint32_t optee_paramTypes(uint32_t api_params)
+{
+ return api_params;
+}
diff --git a/src/optee/creators.h b/src/optee/creators.h
index 12725cf..7c6e5d5 100644
--- a/src/optee/creators.h
+++ b/src/optee/creators.h
@@ -23,5 +23,6 @@ void *optee_TEEC_Context();
void *optee_TEEC_Session();
void *optee_TEEC_Operation();
void *optee_TEEC_SharedMemory();
+uint32_t optee_paramTypes(uint32_t api_params);
#endif
diff --git a/src/simulator/creators.c b/src/simulator/creators.c
index e747d2a..981176a 100644
--- a/src/simulator/creators.c
+++ b/src/simulator/creators.c
@@ -19,15 +19,36 @@
#include <tef/tee_client_api.h>
#include <stdlib.h>
-void *simulator_TEEC_Context() {
+void *simulator_TEEC_Context()
+{
return malloc(sizeof(TEEC_Context));
}
-void *simulator_TEEC_Session() {
+void *simulator_TEEC_Session()
+{
return malloc(sizeof(TEEC_Session));
}
-void *simulator_TEEC_Operation() {
+void *simulator_TEEC_Operation()
+{
return malloc(sizeof(TEEC_Operation));
}
-void *simulator_TEEC_SharedMemory() {
+void *simulator_TEEC_SharedMemory()
+{
return malloc(sizeof(TEEC_SharedMemory));
}
+
+/* copied from simulator tee_client_api.h
+ */
+#define SIMULATOR_PARAM_TYPES(param0Type, param1Type, param2Type, param3Type) \
+ (uint32_t)(((param0Type) & 0x7f) | \
+ (((param1Type) & 0x7f) << 8) | \
+ (((param2Type) & 0x7f) << 16) | \
+ (((param3Type) & 0x7f) << 24))
+
+uint32_t simulator_paramTypes(uint32_t api_params)
+{
+ uint8_t p[4];
+ for (int i = 0; i < 4; ++i) {
+ p[i] = TEEC_PARAM_TYPE_GET(api_params, i);
+ }
+ return SIMULATOR_PARAM_TYPES(p[0], p[1], p[2], p[3]);
+}
diff --git a/src/simulator/creators.h b/src/simulator/creators.h
index c7abebd..4758347 100644
--- a/src/simulator/creators.h
+++ b/src/simulator/creators.h
@@ -23,5 +23,6 @@ void *simulator_TEEC_Context();
void *simulator_TEEC_Session();
void *simulator_TEEC_Operation();
void *simulator_TEEC_SharedMemory();
+uint32_t simulator_paramTypes(uint32_t api_params);
#endif
diff --git a/src/tef_libteec.c b/src/tef_libteec.c
index a5f1914..b479743 100644
--- a/src/tef_libteec.c
+++ b/src/tef_libteec.c
@@ -45,21 +45,22 @@ typedef struct {
void *(*createSession)();
void *(*createOperation)();
void *(*createSharedMemory)();
+ uint32_t (*paramTypes)(uint32_t);
TEEC_Result (*wInitializeContext)(const char *name, TEEC_Context *context);
void (*wFinalizeContext)(TEEC_Context *context);
TEEC_Result (*wOpenSession)(TEEC_Context *context,
- TEEC_Session *session,
- const TEEC_UUID *destination,
- uint32_t connectionMethod,
- const void *connectionData,
- TEEC_Operation *operation,
- uint32_t *returnOrigin);
+ TEEC_Session *session,
+ const TEEC_UUID *destination,
+ uint32_t connectionMethod,
+ const void *connectionData,
+ TEEC_Operation *operation,
+ uint32_t *returnOrigin);
void (*wCloseSession)(TEEC_Session *session);
TEEC_Result (*wInvokeCommand)(TEEC_Session *session,
- uint32_t commandID,
- TEEC_Operation *operation,
- uint32_t *returnOrigin);
+ uint32_t commandID,
+ TEEC_Operation *operation,
+ uint32_t *returnOrigin);
TEEC_Result (*wRegisterSharedMemory)(TEEC_Context *context, TEEC_SharedMemory *sharedMemory);
TEEC_Result (*wAllocateSharedMemory)(TEEC_Context *context, TEEC_SharedMemory *sharedMemory);
void (*wReleaseSharedMemory)(TEEC_SharedMemory *sharedMemory);
@@ -117,7 +118,8 @@ static TEF_SessionImpl *allocSession(TEEC_Session *session)
return tefSession;
}
-static void freeSession(TEF_SessionImpl *tefSession) {
+static void freeSession(TEF_SessionImpl *tefSession)
+{
if (tefSession == NULL) {
return;
}
@@ -128,7 +130,7 @@ static void freeSession(TEF_SessionImpl *tefSession) {
free(tefSession);
}
-static TEEC_Operation *libOperation(TEF_SessionImpl *tefSession, TEEC_Operation *operation)
+static TEEC_Operation *setOperation(TEF_SessionImpl *tefSession, TEEC_Operation *operation)
{
if (operation == NULL) {
return NULL;
@@ -137,21 +139,46 @@ static TEEC_Operation *libOperation(TEF_SessionImpl *tefSession, TEEC_Operation
operation->imp = tefSession->lastOperation;
TEEC_Operation *op = (TEEC_Operation *)operation->imp;
op->started = operation->started;
- op->paramTypes = operation->paramTypes;
- memcpy(op->params, operation->params, sizeof(op->params));
+ op->paramTypes = lib.paramTypes(operation->paramTypes);
+ for (int i = 0; i < 4; ++i) {
+ uint8_t type = TEEC_PARAM_TYPE_GET(operation->paramTypes, i);
+ if (type >= TEEC_MEMREF_WHOLE && type <= TEEC_MEMREF_PARTIAL_INOUT) {
+ op->params[i].memref.parent = (TEEC_SharedMemory*)operation->params[i].memref.parent->imp;
+ } else {
+ op->params[i] = operation->params[i];
+ }
+ }
return op;
}
+static void getOperation(TEEC_Operation *operation)
+{
+ if (operation == NULL) {
+ return;
+ }
+
+ TEEC_Operation *op = (TEEC_Operation *)operation->imp;
+ operation->started = op->started;
+ for (int i = 0; i < 4; ++i) {
+ uint8_t type = TEEC_PARAM_TYPE_GET(operation->paramTypes, i);
+ if (type >= TEEC_MEMREF_WHOLE && type <= TEEC_MEMREF_PARTIAL_INOUT) {
+ } else {
+ operation->params[i] = op->params[i];
+ }
+ }
+}
TEEC_Result TEEC_OpenSession(TEEC_Context *context,
- TEEC_Session *session,
- const TEEC_UUID *destination,
- uint32_t connectionMethod,
- const void *connectionData,
- TEEC_Operation *operation,
- uint32_t *returnOrigin)
+ TEEC_Session *session,
+ const TEEC_UUID *destination,
+ uint32_t connectionMethod,
+ const void *connectionData,
+ TEEC_Operation *operation,
+ uint32_t *returnOrigin)
{
- if (lib.handle == NULL) {
+ if (returnOrigin != NULL) {
*returnOrigin = TEEC_ORIGIN_API;
+ }
+ if (lib.handle == NULL) {
return TEEC_ERROR_NOT_IMPLEMENTED;
}
if (context == NULL || session == NULL || context->imp == NULL) {
@@ -161,13 +188,15 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context,
if (tefSession == NULL) {
return TEEC_ERROR_OUT_OF_MEMORY;
}
- TEEC_Operation *op = libOperation(tefSession, operation);
+ TEEC_Operation *op = setOperation(tefSession, operation);
TEEC_Result result = lib.wOpenSession((TEEC_Context *)context->imp,
- (TEEC_Session *)tefSession->implementation,
- destination, connectionMethod, connectionData, op, returnOrigin);
+ (TEEC_Session *)tefSession->implementation,
+ destination, connectionMethod, connectionData, op, returnOrigin);
if (result != TEEC_SUCCESS) {
freeSession(tefSession);
session->imp = NULL;
+ } else {
+ getOperation(operation);
}
return result;
}
@@ -189,32 +218,32 @@ void TEEC_CloseSession(TEEC_Session *session)
TEEC_Result TEEC_InvokeCommand(TEEC_Session *session,
- uint32_t commandID,
- TEEC_Operation *operation,
- uint32_t *returnOrigin)
+ uint32_t commandID,
+ TEEC_Operation *operation,
+ uint32_t *returnOrigin)
{
- if (lib.handle == NULL) {
+ if (returnOrigin != NULL) {
*returnOrigin = TEEC_ORIGIN_API;
+ }
+ if (lib.handle == NULL) {
return TEEC_ERROR_NOT_IMPLEMENTED;
}
TEF_SessionImpl *tefSession = (TEF_SessionImpl *)session->imp;
if (tefSession == NULL) {
return TEEC_ERROR_BAD_PARAMETERS;
}
- TEEC_Operation *op = libOperation(tefSession, operation);
+ TEEC_Operation *op = setOperation(tefSession, operation);
TEEC_Result result = lib.wInvokeCommand((TEEC_Session *)tefSession->implementation,
- commandID, op, returnOrigin);
- if (op != NULL) {
- operation->started = op->started;
- operation->paramTypes = op->paramTypes;
- memcpy(operation->params, op->params, sizeof(operation->params));
+ commandID, op, returnOrigin);
+ if (result == TEEC_SUCCESS) {
+ getOperation(operation);
}
return result;
}
TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
- TEEC_SharedMemory *sharedMemory)
+ TEEC_SharedMemory *sharedMemory)
{
if (lib.handle == NULL) {
return TEEC_ERROR_NOT_IMPLEMENTED;
@@ -226,7 +255,11 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
if (sharedMemory->imp == NULL) {
return TEEC_ERROR_OUT_OF_MEMORY;
}
- TEEC_Result result = lib.wRegisterSharedMemory((TEEC_Context *)context->imp, sharedMemory->imp);
+ TEEC_SharedMemory *shm = (TEEC_SharedMemory *)sharedMemory->imp;
+ shm->buffer = sharedMemory->buffer;
+ shm->size = sharedMemory->size;
+ shm->flags = sharedMemory->flags;
+ TEEC_Result result = lib.wRegisterSharedMemory((TEEC_Context *)context->imp, shm);
if (result != TEEC_SUCCESS) {
free(sharedMemory->imp);
sharedMemory->imp = NULL;
@@ -236,7 +269,7 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context,
TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context,
- TEEC_SharedMemory *sharedMemory)
+ TEEC_SharedMemory *sharedMemory)
{
if (lib.handle == NULL) {
return TEEC_ERROR_NOT_IMPLEMENTED;
@@ -248,10 +281,16 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context,
if (sharedMemory->imp == NULL) {
return TEEC_ERROR_OUT_OF_MEMORY;
}
- TEEC_Result result = lib.wAllocateSharedMemory((TEEC_Context *)context->imp, sharedMemory->imp);
+ TEEC_SharedMemory *shm = (TEEC_SharedMemory *)sharedMemory->imp;
+ shm->buffer = NULL;
+ shm->size = sharedMemory->size;
+ shm->flags = sharedMemory->flags;
+ TEEC_Result result = lib.wAllocateSharedMemory((TEEC_Context *)context->imp, shm);
if (result != TEEC_SUCCESS) {
free(sharedMemory->imp);
sharedMemory->imp = NULL;
+ } else {
+ sharedMemory->buffer = shm->buffer;
}
return result;
}
@@ -287,13 +326,12 @@ static void configParam(const char *p)
}
if (strncmp(p, "lib=", 4) == 0) {
const char *libname = p + 4;
- size_t libnamesize = strlen(libname);
- if (libnamesize > sizeof(config.libname) - 1) {
+ if (strlen(libname) > sizeof(config.libname) - 1) {
fprintf(stderr, "tef-libteec: name to long '%s'\n", libname);
return;
}
- strncpy(config.libname, libname, libnamesize);
- config.libname[libnamesize] = '\0';
+ strncpy(config.libname, libname, sizeof(config.libname));
+ config.libname[sizeof(config.libname) - 1] = '\0';
}
}
@@ -302,6 +340,7 @@ static void readConfig()
int fd;
memset(&config, 0, sizeof(config));
if ((fd = open(TEF_CONFIG_FILE, O_RDONLY)) < 0) {
+ fprintf(stderr, "tef-libteec: can't read config %s\n", TEF_CONFIG_FILE);
return;
}
@@ -312,6 +351,7 @@ static void readConfig()
buf[l + r] = 0;
char *p = strchr(buf, '\n');
if (p == NULL) {
+ fprintf(stderr, "tef-libteec: config line too long\n");
break;
}
l = 0;
@@ -322,7 +362,7 @@ static void readConfig()
p = strchr(buf + l, '\n');
} while (p != NULL);
memcpy(buf, buf + l, r - l);
- l = r - l;
+ l = (size_t)(r - l);
}
close(fd);
}
@@ -354,11 +394,13 @@ static void loadLibrary(const char *name)
lib.createSession = simulator_TEEC_Session;
lib.createOperation = simulator_TEEC_Operation;
lib.createSharedMemory = simulator_TEEC_SharedMemory;
+ lib.paramTypes = simulator_paramTypes;
} else if (strcmp(name, "optee") == 0) {
lib.createContext = optee_TEEC_Context;
lib.createSession = optee_TEEC_Session;
lib.createOperation = optee_TEEC_Operation;
lib.createSharedMemory = optee_TEEC_SharedMemory;
+ lib.paramTypes = optee_paramTypes;
} else {
fprintf(stderr, "tef-libteec: implementation '%s' is not supported\n", name);
dlclose(lib.handle);