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 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 return utee_invoke_ta_command(session, cancellationRequestTimeout, 82 commandID, paramTypes, params, 83 returnOrigin); 84 } 85 86 /* System API - Cancellations */ 87 88 bool TEE_GetCancellationFlag(void) 89 { 90 bool c; 91 TEE_Result res = utee_get_cancellation_flag(&c); 92 93 if (res != TEE_SUCCESS) 94 TEE_Panic(res); 95 return c; 96 } 97 98 bool TEE_UnmaskCancellation(void) 99 { 100 bool old_mask; 101 TEE_Result res = utee_unmask_cancellation(&old_mask); 102 103 if (res != TEE_SUCCESS) 104 TEE_Panic(res); 105 return old_mask; 106 } 107 108 bool TEE_MaskCancellation(void) 109 { 110 bool old_mask; 111 TEE_Result res = utee_mask_cancellation(&old_mask); 112 113 if (res != TEE_SUCCESS) 114 TEE_Panic(res); 115 return old_mask; 116 } 117 118 /* System API - Memory Management */ 119 120 TEE_Result TEE_CheckMemoryAccessRights(uint32_t accessFlags, void *buffer, 121 size_t size) 122 { 123 TEE_Result res; 124 125 if (size == 0) 126 return TEE_SUCCESS; 127 128 /* Check access rights against memory mapping */ 129 res = utee_check_access_rights(accessFlags, buffer, size); 130 if (res != TEE_SUCCESS) 131 goto out; 132 133 /* 134 * Check access rights against input parameters 135 * Previous legacy code was removed and will need to be restored 136 */ 137 138 res = TEE_SUCCESS; 139 out: 140 return res; 141 } 142 143 void TEE_SetInstanceData(void *instanceData) 144 { 145 tee_api_instance_data = instanceData; 146 } 147 148 void *TEE_GetInstanceData(void) 149 { 150 return tee_api_instance_data; 151 } 152 153 void *TEE_MemMove(void *dest, const void *src, uint32_t size) 154 { 155 return memmove(dest, src, size); 156 } 157 158 int32_t TEE_MemCompare(const void *buffer1, const void *buffer2, uint32_t size) 159 { 160 return memcmp(buffer1, buffer2, size); 161 } 162 163 void *TEE_MemFill(void *buff, uint32_t x, uint32_t size) 164 { 165 return memset(buff, x, size); 166 } 167 168 /* Date & Time API */ 169 170 void TEE_GetSystemTime(TEE_Time *time) 171 { 172 TEE_Result res = utee_get_time(UTEE_TIME_CAT_SYSTEM, time); 173 174 if (res != TEE_SUCCESS) 175 TEE_Panic(0); 176 } 177 178 TEE_Result TEE_Wait(uint32_t timeout) 179 { 180 TEE_Result res = utee_wait(timeout); 181 182 if (res != TEE_SUCCESS && res != TEE_ERROR_CANCEL) 183 TEE_Panic(res); 184 185 return res; 186 } 187 188 TEE_Result TEE_GetTAPersistentTime(TEE_Time *time) 189 { 190 return utee_get_time(UTEE_TIME_CAT_TA_PERSISTENT, time); 191 } 192 193 TEE_Result TEE_SetTAPersistentTime(const TEE_Time *time) 194 { 195 return utee_set_ta_time(time); 196 } 197 198 void TEE_GetREETime(TEE_Time *time) 199 { 200 TEE_Result res = utee_get_time(UTEE_TIME_CAT_REE, time); 201 202 if (res != TEE_SUCCESS) 203 TEE_Panic(0); 204 } 205 206 void *TEE_Malloc(size_t len, uint32_t hint) 207 { 208 return tee_user_mem_alloc(len, hint); 209 } 210 211 void *TEE_Realloc(void *buffer, uint32_t newSize) 212 { 213 /* 214 * GP TEE Internal API specifies newSize as 'uint32_t'. 215 * use unsigned 'size_t' type. it is at least 32bit! 216 */ 217 return tee_user_mem_realloc(buffer, (size_t) newSize); 218 } 219 220 void TEE_Free(void *buffer) 221 { 222 tee_user_mem_free(buffer); 223 } 224