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 - Misc */ 38 39 void __noreturn TEE_Panic(TEE_Result panicCode) 40 { 41 utee_panic(panicCode); 42 } 43 44 /* System API - Internal Client API */ 45 46 TEE_Result TEE_OpenTASession(const TEE_UUID *destination, 47 uint32_t cancellationRequestTimeout, 48 uint32_t paramTypes, TEE_Param params[4], 49 TEE_TASessionHandle *session, 50 uint32_t *returnOrigin) 51 { 52 TEE_Result res; 53 54 res = utee_open_ta_session(destination, cancellationRequestTimeout, 55 paramTypes, params, session, returnOrigin); 56 /* 57 * Specification says that *session must hold TEE_HANDLE_NULL is 58 * TEE_SUCCESS isn't returned. Set it here explicitly in case 59 * the syscall fails before out parameters has been updated. 60 */ 61 if (res != TEE_SUCCESS) 62 *session = TEE_HANDLE_NULL; 63 64 return res; 65 } 66 67 void TEE_CloseTASession(TEE_TASessionHandle session) 68 { 69 if (session != TEE_HANDLE_NULL) { 70 TEE_Result res = utee_close_ta_session(session); 71 if (res != TEE_SUCCESS) 72 TEE_Panic(res); 73 } 74 } 75 76 TEE_Result TEE_InvokeTACommand(TEE_TASessionHandle session, 77 uint32_t cancellationRequestTimeout, 78 uint32_t commandID, uint32_t paramTypes, 79 TEE_Param params[4], uint32_t *returnOrigin) 80 { 81 TEE_Result res; 82 uint32_t ret_origin; 83 84 res = utee_invoke_ta_command(session, cancellationRequestTimeout, 85 commandID, paramTypes, params, 86 &ret_origin); 87 88 if (returnOrigin != NULL) 89 *returnOrigin = ret_origin; 90 91 if (ret_origin == TEE_ORIGIN_TRUSTED_APP) 92 return res; 93 94 if (res != TEE_SUCCESS && 95 res != TEE_ERROR_OUT_OF_MEMORY && 96 res != TEE_ERROR_TARGET_DEAD) 97 TEE_Panic(res); 98 99 return res; 100 } 101 102 /* System API - Cancellations */ 103 104 bool TEE_GetCancellationFlag(void) 105 { 106 bool c; 107 TEE_Result res = utee_get_cancellation_flag(&c); 108 if (res != TEE_SUCCESS) 109 c = false; 110 return c; 111 } 112 113 bool TEE_UnmaskCancellation(void) 114 { 115 bool old_mask; 116 TEE_Result res = utee_unmask_cancellation(&old_mask); 117 118 if (res != TEE_SUCCESS) 119 TEE_Panic(res); 120 return old_mask; 121 } 122 123 bool TEE_MaskCancellation(void) 124 { 125 bool old_mask; 126 TEE_Result res = utee_mask_cancellation(&old_mask); 127 128 if (res != TEE_SUCCESS) 129 TEE_Panic(res); 130 return old_mask; 131 } 132 133 /* System API - Memory Management */ 134 135 TEE_Result TEE_CheckMemoryAccessRights(uint32_t accessFlags, void *buffer, 136 uint32_t size) 137 { 138 TEE_Result res; 139 140 if (size == 0) 141 return TEE_SUCCESS; 142 143 /* Check access rights against memory mapping */ 144 res = utee_check_access_rights(accessFlags, buffer, size); 145 if (res != TEE_SUCCESS) 146 goto out; 147 148 /* 149 * Check access rights against input parameters 150 * Previous legacy code was removed and will need to be restored 151 */ 152 153 res = TEE_SUCCESS; 154 out: 155 return res; 156 } 157 158 void TEE_SetInstanceData(void *instanceData) 159 { 160 tee_api_instance_data = instanceData; 161 } 162 163 void *TEE_GetInstanceData(void) 164 { 165 return tee_api_instance_data; 166 } 167 168 void *TEE_MemMove(void *dest, const void *src, uint32_t size) 169 { 170 return memmove(dest, src, size); 171 } 172 173 int32_t TEE_MemCompare(const void *buffer1, const void *buffer2, uint32_t size) 174 { 175 return memcmp(buffer1, buffer2, size); 176 } 177 178 void *TEE_MemFill(void *buff, uint32_t x, uint32_t size) 179 { 180 return memset(buff, x, size); 181 } 182 183 /* Date & Time API */ 184 185 void TEE_GetSystemTime(TEE_Time *time) 186 { 187 TEE_Result res = utee_get_time(UTEE_TIME_CAT_SYSTEM, time); 188 189 if (res != TEE_SUCCESS) 190 TEE_Panic(0); 191 } 192 193 TEE_Result TEE_Wait(uint32_t timeout) 194 { 195 TEE_Result res = utee_wait(timeout); 196 197 if (res != TEE_SUCCESS && res != TEE_ERROR_CANCEL) 198 TEE_Panic(res); 199 200 return res; 201 } 202 203 TEE_Result TEE_GetTAPersistentTime(TEE_Time *time) 204 { 205 TEE_Result res; 206 207 res = utee_get_time(UTEE_TIME_CAT_TA_PERSISTENT, time); 208 209 if (res != TEE_SUCCESS && res != TEE_ERROR_OVERFLOW) { 210 time->seconds = 0; 211 time->millis = 0; 212 } 213 214 if (res != TEE_SUCCESS && 215 res != TEE_ERROR_TIME_NOT_SET && 216 res != TEE_ERROR_TIME_NEEDS_RESET && 217 res != TEE_ERROR_OVERFLOW && 218 res != TEE_ERROR_OUT_OF_MEMORY) 219 TEE_Panic(res); 220 221 return res; 222 } 223 224 TEE_Result TEE_SetTAPersistentTime(const TEE_Time *time) 225 { 226 TEE_Result res; 227 228 res = utee_set_ta_time(time); 229 230 if (res != TEE_SUCCESS && 231 res != TEE_ERROR_OUT_OF_MEMORY && 232 res != TEE_ERROR_STORAGE_NO_SPACE) 233 TEE_Panic(res); 234 235 return res; 236 } 237 238 void TEE_GetREETime(TEE_Time *time) 239 { 240 TEE_Result res = utee_get_time(UTEE_TIME_CAT_REE, time); 241 242 if (res != TEE_SUCCESS) 243 TEE_Panic(0); 244 } 245 246 void *TEE_Malloc(uint32_t len, uint32_t hint) 247 { 248 return tee_user_mem_alloc(len, hint); 249 } 250 251 void *TEE_Realloc(void *buffer, uint32_t newSize) 252 { 253 /* 254 * GP TEE Internal API specifies newSize as 'uint32_t'. 255 * use unsigned 'size_t' type. it is at least 32bit! 256 */ 257 return tee_user_mem_realloc(buffer, (size_t) newSize); 258 } 259 260 void TEE_Free(void *buffer) 261 { 262 tee_user_mem_free(buffer); 263 } 264 265 /* Cache maintenance support (TA requires the CACHE_MAINTENANCE property) */ 266 TEE_Result TEE_CacheClean(char *buf, size_t len) 267 { 268 return utee_cache_operation(buf, len, TEE_CACHECLEAN); 269 } 270 TEE_Result TEE_CacheFlush(char *buf, size_t len) 271 { 272 return utee_cache_operation(buf, len, TEE_CACHEFLUSH); 273 } 274 275 TEE_Result TEE_CacheInvalidate(char *buf, size_t len) 276 { 277 return utee_cache_operation(buf, len, TEE_CACHEINVALIDATE); 278 } 279