[Bugfix][T108][bug-view-1825]Fix the issue where multiple processes cannot be initialized simultaneously

Only Configure: No
Affected branch: GSW_V1453
Affected module: tee
Is it affected on IC: only ASR
Self-test: yes
Doc Update: no

Change-Id: Iaeff61497771aa2e244bd27befc6d6d603c54aa7
diff --git a/marvell/services/optee_app/libsecure_storage/host/libsecure_storage.c b/marvell/services/optee_app/libsecure_storage/host/libsecure_storage.c
index b5701dd..2c47cd8 100755
--- a/marvell/services/optee_app/libsecure_storage/host/libsecure_storage.c
+++ b/marvell/services/optee_app/libsecure_storage/host/libsecure_storage.c
@@ -28,6 +28,12 @@
 #include <err.h>
 #include <stdio.h>
 #include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <semaphore.h>
 #include <tee_client_api.h>
 #include "mbtk_log.h"
 
@@ -41,13 +47,12 @@
 	TEEC_Session sess;
 };
 
-
+sem_t* sem;
+#define TEE_SEM_NAME "/tee_session_mutex"
 int prepare_tee_session(struct test_ctx *ctx)
 {
-	TEEC_UUID uuid = TA_SECURE_STORAGE_UUID;
-	uint32_t origin;
 	TEEC_Result res;
-
+	
 	/* Initialize a context connecting us to the TEE */
 	res = TEEC_InitializeContext(NULL, &ctx->ctx);
 	if (res != TEEC_SUCCESS)
@@ -55,31 +60,78 @@
 	    LOGE("TEEC_InitializeContext failed with code 0x%x", res);
 	    return -1;
 	}
-	/* Open a session with the TA */
-	res = TEEC_OpenSession(&ctx->ctx, &ctx->sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, &origin);
-	if (res != TEEC_SUCCESS)
+	sem = sem_open(TEE_SEM_NAME, O_CREAT | O_EXCL, 0666, 1);
+    if (sem == SEM_FAILED)
 	{
-		LOGE("TEEC_Opensession failed with code 0x%x origin 0x%x",res, origin);
-		return -1;
-	}
+        if (errno == EEXIST)
+		{
+            sem = sem_open(TEE_SEM_NAME, 0);
+        }
+        if (sem == SEM_FAILED)
+		{
+            LOGE("sem_open failed");
+            return -1;
+        }
+    }
 
 	return 0;
 }
 
 void terminate_tee_session(struct test_ctx *ctx)
 {
-	TEEC_CloseSession(&ctx->sess);
+	if (sem) {
+        sem_close(sem);
+        sem_unlink(TEE_SEM_NAME);
+    }
 	TEEC_FinalizeContext(&ctx->ctx);
 }
 
-TEEC_Result read_secure_object(struct test_ctx *ctx, const char *id,
-			char *data, size_t data_len)
+int open_tee_session(struct test_ctx *ctx)
 {
+	TEEC_Result res;
+	uint32_t origin;
+	TEEC_UUID uuid = TA_SECURE_STORAGE_UUID;
+	struct timespec ts = {0};
+	/* Open a session with the TA */
+	if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) 
+	{
+        LOGE("clock_gettime failed: %s", strerror(errno));
+        return -1;
+    }
+	ts.tv_sec += 1;  // 秒部分
+//    ts.tv_nsec += 300 * 1000000; // 纳秒部分
+	if (sem_timedwait(sem, &ts) != 0)
+	{
+		if (errno == ETIMEDOUT)
+		{
+            LOGE("TEE session lock timeout after 300 milliseconds");
+        } else
+		{
+            LOGE("sem_timedwait error: %s", strerror(errno));
+        }
+        return -1;
+    }
+	res = TEEC_OpenSession(&ctx->ctx, &ctx->sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, &origin);
+	if (res != TEEC_SUCCESS)
+	{
+		LOGE("TEEC_Opensession failed with code 0x%x origin 0x%x",res, origin);
+		sem_post(sem);
+		return -1;
+	}
+	return 0;
+}
+
+TEEC_Result read_secure_object(struct test_ctx *ctx, const char *id,
+			char *data, size_t *data_len)
+{
+	LOGE("read start\n");
 	TEEC_Operation op;
 	uint32_t origin;
 	TEEC_Result res;
 	size_t id_len = strlen(id);
-
+	int ret = open_tee_session(ctx);
+	if (ret)
+		return -1;
 	memset(&op, 0, sizeof(op));
 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
 					 TEEC_MEMREF_TEMP_OUTPUT,
@@ -88,31 +140,43 @@
 	op.params[0].tmpref.size = id_len;
 
 	op.params[1].tmpref.buffer = data;
-	op.params[1].tmpref.size = data_len;
+	op.params[1].tmpref.size = *data_len;
 
 	res = TEEC_InvokeCommand(&ctx->sess,
 				 TA_SECURE_STORAGE_CMD_READ_RAW,
 				 &op, &origin);
 	switch (res) {
 	case TEEC_SUCCESS:
+	{
+		*data_len = op.params[1].tmpref.size;
+	}
 	case TEEC_ERROR_SHORT_BUFFER:
 	case TEEC_ERROR_ITEM_NOT_FOUND:
 		break;
 	default:
 		LOGE("Command READ_RAW failed: 0x%x / %u\n", res, origin);
 	}
-
+	//data[2048] = '\0';
+	//LOGE("str:%d\n",strlen(data));
+	LOGE("DATA-LEN:%d\n",op.params[1].tmpref.size);
+	TEEC_CloseSession(&ctx->sess);
+	sem_post(sem);
+	LOGE("res:%x\n",res);
+	LOGE("read end\n");
 	return res;
 }
 
 TEEC_Result write_secure_object(struct test_ctx *ctx, const char *id,
 			char *data, size_t data_len)
 {
+	LOGE("write start\n");
 	TEEC_Operation op;
 	uint32_t origin;
 	TEEC_Result res;
 	size_t id_len = strlen(id);
-
+	int ret = open_tee_session(ctx);
+	if (ret)
+		return -1;
 	memset(&op, 0, sizeof(op));
 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
 					 TEEC_MEMREF_TEMP_INPUT,
@@ -136,17 +200,22 @@
 	default:
 		LOGE("Command WRITE_RAW failed: 0x%x / %u\n", res, origin);
 	}
-
+	TEEC_CloseSession(&ctx->sess);
+	sem_post(sem);
+	LOGE("write end\n");
 	return res;
 }
 
 TEEC_Result delete_secure_object(struct test_ctx *ctx, const char *id)
 {
+	LOGE("delete start\n");
 	TEEC_Operation op;
 	uint32_t origin;
 	TEEC_Result res;
 	size_t id_len = strlen(id);
-
+	int ret = open_tee_session(ctx);
+	if (ret)
+		return -1;
 	memset(&op, 0, sizeof(op));
 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
 					 TEEC_NONE, TEEC_NONE, TEEC_NONE);
@@ -165,6 +234,8 @@
 	default:
 		LOGE("Command DELETE failed: 0x%x / %u\n", res, origin);
 	}
-
+	TEEC_CloseSession(&ctx->sess);
+	sem_post(sem);
+	LOGE("delete end\n");
 	return res;
 }
diff --git a/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c b/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c
index d724b0b..6013227 100755
--- a/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c
+++ b/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c
@@ -161,7 +161,7 @@
 
 int (*prepare_tee_session)(struct test_ctx *ctx);
 void (*terminate_tee_session)(struct test_ctx *ctx);
-TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
+TEEC_Result (*read_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t *data_len);
 TEEC_Result (*write_secure_object)(struct test_ctx *ctx, const char *id,char *data, size_t data_len);
 TEEC_Result (*delete_secure_object)(struct test_ctx *ctx, const char *id);
 
@@ -206,7 +206,7 @@
         return GSW_HAL_NORMAL_FAIL;
     }
 
-    read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t data_len))dlsym(dlHandle_secure, "read_secure_object");
+    read_secure_object = (TEEC_Result (*)(struct test_ctx *ctx, const char *id,char *data, size_t *data_len))dlsym(dlHandle_secure, "read_secure_object");
     if (read_secure_object == NULL) 
     {
         LOGE("read_secure_object dlsym fail\n");
@@ -246,7 +246,7 @@
         return ret;
     }
     ret = prepare_tee_session(&ctx);
-
+    LOGE("init end\n");
     return ret;
 }
 
@@ -261,12 +261,14 @@
 #define basic_buf_len  7000
 int32_t gsw_tee_read_secure_data(const char* in_obj_name, char* out_buf, unsigned int* p_out_buf_len)
 {
+    LOGE("start read\n");
     if (in_obj_name == NULL || out_buf == NULL)
     {
         return GSW_HAL_NORMAL_FAIL;
     }
-        
+    
     int32_t ret = 0;
+    size_t size = basic_buf_len;
     char *tmp_buf = (char*)malloc(basic_buf_len);
     if (NULL == tmp_buf)
     {
@@ -274,14 +276,15 @@
         return GSW_HAL_NO_MEMORY;
     }
     
-    TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, basic_buf_len);
+    TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size);
     if (res != TEEC_SUCCESS)
     {
 	LOGE("Failed to read an object from the secure storage");
         ret = GSW_HAL_NORMAL_FAIL;
     }
-    memcpy(out_buf, tmp_buf, strlen(tmp_buf)+1);
-    *p_out_buf_len = strlen(out_buf);
+    LOGE("really start end\n");
+    memcpy(out_buf, tmp_buf, size);
+    *p_out_buf_len = size;
     free(tmp_buf);
     return ret;
 }
@@ -295,6 +298,7 @@
 */
 int32_t gsw_tee_write_secure_data(const char* in_obj_name, char* in_buf, unsigned int in_buf_len)
 {
+    LOGE("write start\n");
     if (in_obj_name == NULL || in_buf == NULL)
         return GSW_HAL_NORMAL_FAIL;
     int32_t ret = 0;
@@ -305,7 +309,7 @@
 	LOGE("Failed to write an object from the secure storage");
         ret = GSW_HAL_NORMAL_FAIL;
     }
-
+    LOGE("write really end\n");
     return ret;
 }
 
@@ -317,6 +321,7 @@
 */
 int32_t gsw_tee_delete_secure_data(const char* in_obj_name)
 {
+    LOGE("delete start\n");
     if (in_obj_name == NULL)
         return GSW_HAL_NORMAL_FAIL;
     int32_t ret = 0;
@@ -327,7 +332,7 @@
 	LOGE("Failed to delete the object: 0x%x", res);
         ret = GSW_HAL_NORMAL_FAIL;
     }
-
+    LOGE("delete really end\n");
     return ret;
 }
 
@@ -341,14 +346,14 @@
     if (in_obj_name == NULL)
         return GSW_HAL_NORMAL_FAIL;
     int32_t ret = 1;
-
+    size_t size = basic_buf_len;
     char *tmp_buf = (char*)malloc(basic_buf_len);
     if (NULL == tmp_buf)
     {
         LOGE("Failed malloc fail");
         return GSW_HAL_NO_MEMORY;
     }
-    TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, basic_buf_len);
+    TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size);
     if (res == TEEC_ERROR_ITEM_NOT_FOUND)
     {
         LOGE("the obj no found\n");
@@ -376,6 +381,7 @@
 */
 int32_t gsw_tee_sdk_deinit(void)
 {
+    LOGE("deinit start\n");
     if (terminate_tee_session) {
         terminate_tee_session(&ctx);  // 终止TEE会话
         terminate_tee_session = NULL;
@@ -390,7 +396,7 @@
         dlclose(dlHandle_mbtk);  // 卸载日志库
         dlHandle_mbtk = NULL;
     }
-
+    LOGE("deinit end\n");
     return GSW_HAL_SUCCESS;
 }