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