1 /* 2 * Copyright (c) 2014, STMicroelectronics International N.V. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include <stdlib.h> 28 #include <string.h> 29 30 #include <tee_api.h> 31 #include <utee_syscalls.h> 32 #include <user_ta_header.h> 33 #include "tee_user_mem.h" 34 35 static void *tee_api_instance_data; 36 37 /* System API - Internal Client API */ 38 39 TEE_Result TEE_OpenTASession(const TEE_UUID *destination, 40 uint32_t cancellationRequestTimeout, 41 uint32_t paramTypes, TEE_Param params[4], 42 TEE_TASessionHandle *session, 43 uint32_t *returnOrigin) 44 { 45 TEE_Result res; 46 47 res = utee_open_ta_session(destination, cancellationRequestTimeout, 48 paramTypes, params, session, returnOrigin); 49 /* 50 * Specification says that *session must hold TEE_HANDLE_NULL is 51 * TEE_SUCCESS isn't returned. Set it here explicitly in case 52 * the syscall fails before out parameters has been updated. 53 */ 54 if (res != TEE_SUCCESS) 55 *session = TEE_HANDLE_NULL; 56 57 return res; 58 } 59 60 void TEE_CloseTASession(TEE_TASessionHandle session) 61 { 62 if (session != TEE_HANDLE_NULL) { 63 TEE_Result res = utee_close_ta_session(session); 64 if (res != TEE_SUCCESS) 65 TEE_Panic(res); 66 } 67 } 68 69 TEE_Result TEE_InvokeTACommand(TEE_TASessionHandle session, 70 uint32_t cancellationRequestTimeout, 71 uint32_t commandID, uint32_t paramTypes, 72 TEE_Param params[4], uint32_t *returnOrigin) 73 { 74 TEE_Result res; 75 uint32_t ret_origin; 76 77 res = utee_invoke_ta_command(session, cancellationRequestTimeout, 78 commandID, paramTypes, params, 79 &ret_origin); 80 81 if (returnOrigin != NULL) 82 *returnOrigin = ret_origin; 83 84 if (ret_origin == TEE_ORIGIN_TRUSTED_APP) 85 return res; 86 87 if (res != TEE_SUCCESS && 88 res != TEE_ERROR_OUT_OF_MEMORY && 89 res != TEE_ERROR_TARGET_DEAD) 90 TEE_Panic(res); 91 92 return res; 93 } 94 95 /* System API - Cancellations */ 96 97 bool TEE_GetCancellationFlag(void) 98 { 99 bool c; 100 TEE_Result res = utee_get_cancellation_flag(&c); 101 if (res != TEE_SUCCESS) 102 c = false; 103 return c; 104 } 105 106 bool TEE_UnmaskCancellation(void) 107 { 108 bool old_mask; 109 TEE_Result res = utee_unmask_cancellation(&old_mask); 110 111 if (res != TEE_SUCCESS) 112 TEE_Panic(res); 113 return old_mask; 114 } 115 116 bool TEE_MaskCancellation(void) 117 { 118 bool old_mask; 119 TEE_Result res = utee_mask_cancellation(&old_mask); 120 121 if (res != TEE_SUCCESS) 122 TEE_Panic(res); 123 return old_mask; 124 } 125 126 /* System API - Memory Management */ 127 128 TEE_Result TEE_CheckMemoryAccessRights(uint32_t accessFlags, void *buffer, 129 uint32_t size) 130 { 131 TEE_Result res; 132 133 if (size == 0) 134 return TEE_SUCCESS; 135 136 /* Check access rights against memory mapping */ 137 res = utee_check_access_rights(accessFlags, buffer, size); 138 if (res != TEE_SUCCESS) 139 goto out; 140 141 /* 142 * Check access rights against input parameters 143 * Previous legacy code was removed and will need to be restored 144 */ 145 146 res = TEE_SUCCESS; 147 out: 148 return res; 149 } 150 151 void TEE_SetInstanceData(void *instanceData) 152 { 153 tee_api_instance_data = instanceData; 154 } 155 156 void *TEE_GetInstanceData(void) 157 { 158 return tee_api_instance_data; 159 } 160 161 void *TEE_MemMove(void *dest, const void *src, uint32_t size) 162 { 163 return memmove(dest, src, size); 164 } 165 166 int32_t TEE_MemCompare(const void *buffer1, const void *buffer2, uint32_t size) 167 { 168 return memcmp(buffer1, buffer2, size); 169 } 170 171 void *TEE_MemFill(void *buff, uint32_t x, uint32_t size) 172 { 173 return memset(buff, x, size); 174 } 175 176 /* Date & Time API */ 177 178 void TEE_GetSystemTime(TEE_Time *time) 179 { 180 TEE_Result res = utee_get_time(UTEE_TIME_CAT_SYSTEM, time); 181 182 if (res != TEE_SUCCESS) 183 TEE_Panic(0); 184 } 185 186 TEE_Result TEE_Wait(uint32_t timeout) 187 { 188 TEE_Result res = utee_wait(timeout); 189 190 if (res != TEE_SUCCESS && res != TEE_ERROR_CANCEL) 191 TEE_Panic(res); 192 193 return res; 194 } 195 196 TEE_Result TEE_GetTAPersistentTime(TEE_Time *time) 197 { 198 TEE_Result res; 199 200 res = utee_get_time(UTEE_TIME_CAT_TA_PERSISTENT, time); 201 202 if (res != TEE_SUCCESS && res != TEE_ERROR_OVERFLOW) { 203 time->seconds = 0; 204 time->millis = 0; 205 } 206 207 if (res != TEE_SUCCESS && 208 res != TEE_ERROR_TIME_NOT_SET && 209 res != TEE_ERROR_TIME_NEEDS_RESET && 210 res != TEE_ERROR_OVERFLOW && 211 res != TEE_ERROR_OUT_OF_MEMORY) 212 TEE_Panic(res); 213 214 return res; 215 } 216 217 TEE_Result TEE_SetTAPersistentTime(const TEE_Time *time) 218 { 219 TEE_Result res; 220 221 res = utee_set_ta_time(time); 222 223 if (res != TEE_SUCCESS && 224 res != TEE_ERROR_OUT_OF_MEMORY && 225 res != TEE_ERROR_STORAGE_NO_SPACE) 226 TEE_Panic(res); 227 228 return res; 229 } 230 231 void TEE_GetREETime(TEE_Time *time) 232 { 233 TEE_Result res = utee_get_time(UTEE_TIME_CAT_REE, time); 234 235 if (res != TEE_SUCCESS) 236 TEE_Panic(0); 237 } 238 239 void *TEE_Malloc(uint32_t len, uint32_t hint) 240 { 241 return tee_user_mem_alloc(len, hint); 242 } 243 244 void *TEE_Realloc(void *buffer, uint32_t newSize) 245 { 246 /* 247 * GP TEE Internal API specifies newSize as 'uint32_t'. 248 * use unsigned 'size_t' type. it is at least 32bit! 249 */ 250 return tee_user_mem_realloc(buffer, (size_t) newSize); 251 } 252 253 void TEE_Free(void *buffer) 254 { 255 tee_user_mem_free(buffer); 256 } 257 258 /* Cache maintenance support (TA requires the CACHE_MAINTENANCE property) */ 259 TEE_Result TEE_CacheClean(char *buf, size_t len) 260 { 261 return utee_cache_operation(buf, len, TEE_CACHECLEAN); 262 } 263 TEE_Result TEE_CacheFlush(char *buf, size_t len) 264 { 265 return utee_cache_operation(buf, len, TEE_CACHEFLUSH); 266 } 267 268 TEE_Result TEE_CacheInvalidate(char *buf, size_t len) 269 { 270 return utee_cache_operation(buf, len, TEE_CACHEINVALIDATE); 271 } 272