[Bugfix][T108][bug-view-1988/1991]Temporary USE :Resolve the timeout issue of TEE calling the interface

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: I00b5c79af5602c8c81665b5f74344b13607c754a
diff --git a/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c b/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c
index 4d95869..8c73d04 100755
--- a/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c
+++ b/mbtk/libgsw_lib/gsw_secrypt_ss_interface.c
@@ -69,6 +69,17 @@
 
 #define lib_mbtk_path "/lib/libmbtk_lib.so"
 static void *dlHandle_mbtk;
+#define PUBLIC_KEY_SEC_NAME "public_key_obj"
+#define SEC_LEVEL1_SEC_NAME "sec_levell_obj"
+typedef struct
+{
+    size_t length;
+    int is_update;
+    //int is_read;
+    unsigned char *content;
+} tmp_sec;
+tmp_sec public_key_sec = {0};
+tmp_sec sec_level1_sec = {0};
 
 typedef uint32_t TEEC_Result;
 // static void (*mbtk_log)(int level, const char *format, ...);
@@ -163,6 +174,17 @@
     }
     ret = prepare_tee_session(&ctx);
     LOGE(GSW_TEE,"init end\n");
+    if (public_key_sec.content == NULL) {
+        public_key_sec.content = malloc(7000);
+        public_key_sec.length = 0;
+        public_key_sec.is_update = 0;
+    }
+    
+    if (sec_level1_sec.content == NULL) {
+        sec_level1_sec.content = malloc(7000);
+        sec_level1_sec.length = 0;
+        sec_level1_sec.is_update = 0;
+    }
     return ret;
 }
 
@@ -177,7 +199,7 @@
 #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(GSW_TEE,"start read\n");
+    //LOGE(GSW_TEE,"start read\n");
     if (in_obj_name == NULL || out_buf == NULL)
     {
         return GSW_HAL_NORMAL_FAIL;
@@ -191,20 +213,83 @@
         LOGE(GSW_TEE,"Failed malloc fail");
         return GSW_HAL_NO_MEMORY;
     }
-    
-    TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size);
-    if (res != TEEC_SUCCESS)
+
+    bool is_pubkey = !strncmp(in_obj_name, PUBLIC_KEY_SEC_NAME, strlen(PUBLIC_KEY_SEC_NAME));
+    bool is_level1 = !strncmp(in_obj_name, SEC_LEVEL1_SEC_NAME, strlen(SEC_LEVEL1_SEC_NAME));
+
+    if (is_pubkey || is_level1)
     {
-        LOGE(GSW_TEE,"Failed to read an object from the secure storage");
-        ret = GSW_HAL_NORMAL_FAIL;
+        tmp_sec *target = is_pubkey ? &public_key_sec : &sec_level1_sec;
+        if (target->is_update)
+        {
+            if (target->length == 0 || target->content[0] == '\0')
+                ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST;
+            else
+            {
+                memcpy(out_buf, target->content, target->length);
+                *p_out_buf_len = target->length;
+                ret = GSW_HAL_SUCCESS;
+            }
+            free(tmp_buf);
+            return ret;
+        }
+        
+
+        TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size);
+        
+        if (res == TEEC_ERROR_ITEM_NOT_FOUND)
+        {
+            LOGE(GSW_TEE, "the obj no found\n");
+            ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST;
+
+            target->is_update = 1; 
+            target->length = 0;
+            target->content[0] = '\0';
+        }
+        else if (res == TEEC_SUCCESS)
+        {
+            LOGE(GSW_TEE, "the obj is exist\n");
+            memcpy(target->content, tmp_buf, size);
+
+            target->length = size;
+            target->is_update = 1;
+            
+            memcpy(out_buf, tmp_buf, size);
+            *p_out_buf_len = size;
+            
+            ret = GSW_HAL_SUCCESS;
+        }
+        else
+        {
+            LOGE(GSW_TEE,"Failed to read an object from the secure storage");
+            ret = GSW_HAL_NORMAL_FAIL;
+        }
+        
         free(tmp_buf);
         return ret;
     }
-    LOGE(GSW_TEE,"really start end\n");
-    memcpy(out_buf, tmp_buf, size);
-    *p_out_buf_len = size;
+
+    TEEC_Result res = read_secure_object(&ctx, in_obj_name, tmp_buf, &size);
+    if (res == TEEC_ERROR_ITEM_NOT_FOUND)
+    {
+        LOGE(GSW_TEE,"the obj no found\n");
+        ret = GSW_HAL_ERROR_TEE_SFS_FILE_NOEXIST;
+    }
+    else if (res == TEEC_SUCCESS)
+    {
+        LOGE(GSW_TEE,"the obj is exist\n");
+        memcpy(out_buf, tmp_buf, size);
+        *p_out_buf_len = size;
+        ret = GSW_HAL_SUCCESS;
+    }
+    else
+    {
+        LOGE(GSW_TEE,"Failed to read an object from the secure storage");
+        ret = GSW_HAL_NORMAL_FAIL;
+    }
+
     free(tmp_buf);
-    return ret;
+    return ret; 
 }
 
 /**
@@ -227,6 +312,13 @@
 	LOGE(GSW_TEE,"Failed to write an object from the secure storage");
         ret = GSW_HAL_NORMAL_FAIL;
     }
+    bool is_pubkey = !strncmp(in_obj_name, PUBLIC_KEY_SEC_NAME, strlen(PUBLIC_KEY_SEC_NAME));
+    bool is_level1 = !strncmp(in_obj_name, SEC_LEVEL1_SEC_NAME, strlen(SEC_LEVEL1_SEC_NAME));
+    if (is_pubkey || is_level1)
+    {
+        tmp_sec *target = is_pubkey ? &public_key_sec : &sec_level1_sec;
+        target->is_update = 0;
+    }
     LOGE(GSW_TEE,"write really end\n");
     return ret;
 }
@@ -250,6 +342,13 @@
 	LOGE(GSW_TEE,"Failed to delete the object: 0x%x", res);
         ret = GSW_HAL_NORMAL_FAIL;
     }
+    bool is_pubkey = !strncmp(in_obj_name, PUBLIC_KEY_SEC_NAME, strlen(PUBLIC_KEY_SEC_NAME));
+    bool is_level1 = !strncmp(in_obj_name, SEC_LEVEL1_SEC_NAME, strlen(SEC_LEVEL1_SEC_NAME));
+    if (is_pubkey || is_level1)
+    {
+        tmp_sec *target = is_pubkey ? &public_key_sec : &sec_level1_sec;
+        target->length = 0;
+    }
     LOGE(GSW_TEE,"delete really end\n");
     return ret;
 }