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 */ 27*2eb765fcSJens Wiklander #include <util.h> 28b0104773SPascal Brand #include <kernel/tee_common_otp.h> 29b0104773SPascal Brand #include <kernel/tee_common.h> 30b0104773SPascal Brand #include <kernel/tee_compat.h> 31b0104773SPascal Brand #include <tee_api_types.h> 32b0104773SPascal Brand #include <kernel/tee_ta_manager.h> 33b0104773SPascal Brand #include <utee_types.h> 34b0104773SPascal Brand #include <tee/tee_svc.h> 35ffe04039SJerome Forissier #include <tee/tee_cryp_utl.h> 36b0104773SPascal Brand #include <mm/tee_mmu.h> 37b0104773SPascal Brand #include <mm/tee_mm.h> 38b0104773SPascal Brand #include <kernel/tee_rpc.h> 39b0104773SPascal Brand #include <kernel/tee_rpc_types.h> 40b0104773SPascal Brand #include <kernel/tee_time.h> 41b0104773SPascal Brand 42b0104773SPascal Brand #include <user_ta_header.h> 43b0104773SPascal Brand #include <kernel/tee_core_trace.h> 44b0104773SPascal Brand #include <kernel/tee_kta_trace.h> 45b0104773SPascal Brand #include <kernel/chip_services.h> 46b0104773SPascal Brand 47b0104773SPascal Brand void tee_svc_sys_log(const void *buf, size_t len) 48b0104773SPascal Brand { 49b0104773SPascal Brand char *kbuf; 50b0104773SPascal Brand 51b0104773SPascal Brand if (len == 0) 52b0104773SPascal Brand return; 53b0104773SPascal Brand 54b0104773SPascal Brand kbuf = malloc(len); 55b0104773SPascal Brand if (kbuf == NULL) 56b0104773SPascal Brand return; 57c0346845SJens Wiklander *kbuf = '\0'; 58b0104773SPascal Brand 59b0104773SPascal Brand /* log as Info/Raw traces */ 60b0104773SPascal Brand if (tee_svc_copy_from_user(NULL, kbuf, buf, len) == TEE_SUCCESS) 61b0104773SPascal Brand ATAMSG_RAW("%s", kbuf); 62b0104773SPascal Brand 63b0104773SPascal Brand free(kbuf); 64b0104773SPascal Brand } 65b0104773SPascal Brand 66b0104773SPascal Brand void tee_svc_sys_panic(uint32_t code) 67b0104773SPascal Brand { 68b0104773SPascal Brand struct tee_ta_session *sess; 69b0104773SPascal Brand 70b0104773SPascal Brand if (tee_ta_get_current_session(&sess) == TEE_SUCCESS) { 71cebdec51SJens Wiklander EMSG("Set session %p to panicked", (void *)sess); 72b0104773SPascal Brand sess->ctx->panicked = 1; 73b0104773SPascal Brand sess->ctx->panic_code = code; 74b0104773SPascal Brand 75b0104773SPascal Brand { 76cebdec51SJens Wiklander int *p = 0; 77cebdec51SJens Wiklander 78b0104773SPascal Brand /* 79b0104773SPascal Brand * Force panicking. This memory error will be trapped by 80b0104773SPascal Brand * the error exception handler myErrorHandler() 81b0104773SPascal Brand */ 82b0104773SPascal Brand EMSG("Following 'DTLB exception in bundle'"); 83b0104773SPascal Brand EMSG(" is generated with code %d", code); 84b0104773SPascal Brand *p = 1; 85b0104773SPascal Brand } 86b0104773SPascal Brand } else { 87b0104773SPascal Brand DMSG("Panic called from unknown TA"); 88b0104773SPascal Brand } 89b0104773SPascal Brand } 90b0104773SPascal Brand 91b7fc217fSPascal Brand TEE_Result tee_svc_reserved(void) 92b7fc217fSPascal Brand { 93b7fc217fSPascal Brand return TEE_ERROR_GENERIC; 94b7fc217fSPascal Brand } 95b7fc217fSPascal Brand 96cebdec51SJens Wiklander uint32_t tee_svc_sys_dummy(uint32_t *a __unused) 97b0104773SPascal Brand { 98b0104773SPascal Brand DMSG("tee_svc_sys_dummy: a 0x%x", (unsigned int)a); 99b0104773SPascal Brand return 0; 100b0104773SPascal Brand } 101b0104773SPascal Brand 102cebdec51SJens Wiklander uint32_t tee_svc_sys_dummy_7args(uint32_t a1 __unused, uint32_t a2 __unused, 103cebdec51SJens Wiklander uint32_t a3 __unused, uint32_t a4 __unused, 104cebdec51SJens Wiklander uint32_t a5 __unused, uint32_t a6 __unused, 105cebdec51SJens Wiklander uint32_t a7 __unused) 106b0104773SPascal Brand { 107b0104773SPascal Brand DMSG("tee_svc_sys_dummy_7args: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, %x, %x\n", 108b0104773SPascal Brand a1, a2, a3, a4, a5, a6, a7); 109b0104773SPascal Brand return 0; 110b0104773SPascal Brand } 111b0104773SPascal Brand 112b0104773SPascal Brand uint32_t tee_svc_sys_nocall(void) 113b0104773SPascal Brand { 114b0104773SPascal Brand DMSG("No syscall"); 115b0104773SPascal Brand return 0x1; 116b0104773SPascal Brand } 117b0104773SPascal Brand 118b0104773SPascal Brand TEE_Result tee_svc_sys_get_property(uint32_t prop, tee_uaddr_t buf, size_t blen) 119b0104773SPascal Brand { 120b0104773SPascal Brand static const char api_vers[] = "1.0"; 121b0104773SPascal Brand static const char descr[] = "Version N.N"; 122b0104773SPascal Brand /* 123b0104773SPascal Brand * Value 100 means: 124b0104773SPascal Brand * System time based on REE-controlled timers. Can be tampered by the 125b0104773SPascal Brand * REE. The implementation must still guarantee that the system time 126b0104773SPascal Brand * is monotonous, i.e., successive calls to TEE_GetSystemTime must 127b0104773SPascal Brand * return increasing values of the system time. 128b0104773SPascal Brand */ 129b0104773SPascal Brand static const uint32_t sys_time_prot_lvl = 100; 130b0104773SPascal Brand static const uint32_t ta_time_prot_lvl = 100; 131b0104773SPascal Brand struct tee_ta_session *sess; 132b0104773SPascal Brand TEE_Result res; 133b0104773SPascal Brand 134b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 135b0104773SPascal Brand if (res != TEE_SUCCESS) 136b0104773SPascal Brand return res; 137b0104773SPascal Brand 138b0104773SPascal Brand switch (prop) { 139b0104773SPascal Brand case UTEE_PROP_TEE_API_VERSION: 140b0104773SPascal Brand if (blen < sizeof(api_vers)) 141b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 142b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, api_vers, 143b0104773SPascal Brand sizeof(api_vers)); 144b0104773SPascal Brand 145b0104773SPascal Brand case UTEE_PROP_TEE_DESCR: 146b0104773SPascal Brand if (blen < sizeof(descr)) 147b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 148b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, descr, 149b0104773SPascal Brand sizeof(descr)); 150b0104773SPascal Brand 151b0104773SPascal Brand case UTEE_PROP_TEE_DEV_ID: 152b0104773SPascal Brand { 153b0104773SPascal Brand TEE_UUID uuid; 154b0104773SPascal Brand const size_t nslen = 4; 155b0104773SPascal Brand uint8_t data[4 + 156b0104773SPascal Brand FVR_DIE_ID_NUM_REGS * sizeof(uint32_t)] = { 157b0104773SPascal Brand 'S', 'T', 'E', 'E' }; 158b0104773SPascal Brand 159b0104773SPascal Brand if (blen < sizeof(uuid)) 160b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 161b0104773SPascal Brand 162b0104773SPascal Brand if (tee_otp_get_die_id 163b0104773SPascal Brand (data + nslen, sizeof(data) - nslen)) 164b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 165b0104773SPascal Brand 166ffe04039SJerome Forissier res = tee_hash_createdigest(TEE_ALG_SHA256, data, 167ffe04039SJerome Forissier sizeof(data), 168ffe04039SJerome Forissier (uint8_t *)&uuid, 169ffe04039SJerome Forissier sizeof(uuid)); 170b0104773SPascal Brand if (res != TEE_SUCCESS) 171b0104773SPascal Brand return TEE_ERROR_BAD_STATE; 172b0104773SPascal Brand 173b0104773SPascal Brand /* 174b0104773SPascal Brand * Changes the random value into and UUID as specifiec 175b0104773SPascal Brand * in RFC 4122. The magic values are from the example 176b0104773SPascal Brand * code in the RFC. 177b0104773SPascal Brand * 178b0104773SPascal Brand * TEE_UUID is defined slightly different from the RFC, 179b0104773SPascal Brand * but close enough for our purpose. 180b0104773SPascal Brand */ 181b0104773SPascal Brand 182b0104773SPascal Brand uuid.timeHiAndVersion &= 0x0fff; 183b0104773SPascal Brand uuid.timeHiAndVersion |= 5 << 12; 184b0104773SPascal Brand 185b0104773SPascal Brand /* uuid.clock_seq_hi_and_reserved in the RFC */ 186b0104773SPascal Brand uuid.clockSeqAndNode[0] &= 0x3f; 187b0104773SPascal Brand uuid.clockSeqAndNode[0] |= 0x80; 188b0104773SPascal Brand 189b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, &uuid, 190b0104773SPascal Brand sizeof(TEE_UUID)); 191b0104773SPascal Brand } 192b0104773SPascal Brand 193b0104773SPascal Brand case UTEE_PROP_TEE_SYS_TIME_PROT_LEVEL: 194b0104773SPascal Brand if (blen < sizeof(sys_time_prot_lvl)) 195b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 196b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 197b0104773SPascal Brand &sys_time_prot_lvl, 198b0104773SPascal Brand sizeof(sys_time_prot_lvl)); 199b0104773SPascal Brand 200b0104773SPascal Brand case UTEE_PROP_TEE_TA_TIME_PROT_LEVEL: 201b0104773SPascal Brand if (blen < sizeof(ta_time_prot_lvl)) 202b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 203b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 204b0104773SPascal Brand &ta_time_prot_lvl, 205b0104773SPascal Brand sizeof(ta_time_prot_lvl)); 206b0104773SPascal Brand 207b0104773SPascal Brand case UTEE_PROP_CLIENT_ID: 208b0104773SPascal Brand if (blen < sizeof(TEE_Identity)) 209b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 210b0104773SPascal Brand 211b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 21237d6ae92SPascal Brand &sess->clnt_id, sizeof(TEE_Identity)); 21337d6ae92SPascal Brand 214b0104773SPascal Brand case UTEE_PROP_TA_APP_ID: 215b0104773SPascal Brand if (blen < sizeof(TEE_UUID)) 216b0104773SPascal Brand return TEE_ERROR_SHORT_BUFFER; 217b0104773SPascal Brand 218b0104773SPascal Brand return tee_svc_copy_to_user(sess, (void *)buf, 21937d6ae92SPascal Brand &sess->ctx->head->uuid, sizeof(TEE_UUID)); 220b0104773SPascal Brand 221b0104773SPascal Brand default: 222b0104773SPascal Brand break; 223b0104773SPascal Brand } 224b0104773SPascal Brand return TEE_ERROR_NOT_IMPLEMENTED; 225b0104773SPascal Brand } 226b0104773SPascal Brand 227b0104773SPascal Brand /* 228b0104773SPascal Brand * TA invokes some TA with parameter. 229b0104773SPascal Brand * If some parameters are memory references: 230b0104773SPascal Brand * - either the memref is inside TA private RAM: TA is not allowed to expose 231b0104773SPascal Brand * its private RAM: use a temporary memory buffer and copy the data. 232b0104773SPascal Brand * - or the memref is not in the TA private RAM: 233b0104773SPascal Brand * - if the memref was mapped to the TA, TA is allowed to expose it. 234b0104773SPascal Brand * - if so, converts memref virtual address into a physical address. 235b0104773SPascal Brand */ 236b0104773SPascal Brand static TEE_Result tee_svc_copy_param(struct tee_ta_session *sess, 237b0104773SPascal Brand struct tee_ta_session *called_sess, 238b0104773SPascal Brand uint32_t param_types, 239b7fc217fSPascal Brand TEE_Param callee_params[TEE_NUM_PARAMS], 240b0104773SPascal Brand struct tee_ta_param *param, 241b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 242b0104773SPascal Brand tee_mm_entry_t **mm) 243b0104773SPascal Brand { 244b0104773SPascal Brand size_t n; 245b0104773SPascal Brand TEE_Result res; 246b0104773SPascal Brand size_t req_mem = 0; 247b0104773SPascal Brand size_t s; 248b0104773SPascal Brand uint8_t *dst = 0; 249b0104773SPascal Brand tee_paddr_t dst_pa, src_pa = 0; 250b0104773SPascal Brand bool ta_private_memref[TEE_NUM_PARAMS]; 251b0104773SPascal Brand 252b7fc217fSPascal Brand /* fill 'param' input struct with caller params description buffer */ 253b0104773SPascal Brand param->types = param_types; 254b7fc217fSPascal Brand if (!callee_params) { 255b0104773SPascal Brand if (param->types != 0) 256b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 257b0104773SPascal Brand memset(param->params, 0, sizeof(param->params)); 258b0104773SPascal Brand } else { 259b7fc217fSPascal Brand tee_svc_copy_from_user(sess, param->params, callee_params, 260b0104773SPascal Brand sizeof(param->params)); 261b0104773SPascal Brand } 262b0104773SPascal Brand 263b0104773SPascal Brand if ((called_sess != NULL) && 264b0104773SPascal Brand (called_sess->ctx->static_ta == NULL) && 265b0104773SPascal Brand (called_sess->ctx->flags & TA_FLAG_USER_MODE) == 0) { 266b0104773SPascal Brand /* 267b0104773SPascal Brand * kernel TA, borrow the mapping of the calling 268b0104773SPascal Brand * during this call. 269b0104773SPascal Brand */ 270b0104773SPascal Brand called_sess->calling_sess = sess; 271b0104773SPascal Brand return TEE_SUCCESS; 272b0104773SPascal Brand } 273b0104773SPascal Brand 274b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 275b0104773SPascal Brand 276b0104773SPascal Brand ta_private_memref[n] = false; 277b0104773SPascal Brand 278b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 279b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 280b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 281b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 282b0104773SPascal Brand if (param->params[n].memref.buffer == NULL) { 283b0104773SPascal Brand if (param->params[n].memref.size != 0) 284b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 285b0104773SPascal Brand break; 286b0104773SPascal Brand } 287b0104773SPascal Brand /* uTA cannot expose its private memory */ 288b0104773SPascal Brand if (tee_mmu_is_vbuf_inside_ta_private(sess->ctx, 289106d8aa6SPascal Brand param->params[n].memref.buffer, 290b0104773SPascal Brand param->params[n].memref.size)) { 291b0104773SPascal Brand 292a17acc4cSSabrina Ni s = ROUNDUP(param->params[n].memref.size, 293b0104773SPascal Brand sizeof(uint32_t)); 294b0104773SPascal Brand /* Check overflow */ 295b0104773SPascal Brand if (req_mem + s < req_mem) 296b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 297b0104773SPascal Brand req_mem += s; 298b0104773SPascal Brand ta_private_memref[n] = true; 299b0104773SPascal Brand break; 300b0104773SPascal Brand } 301106d8aa6SPascal Brand if (tee_mmu_is_vbuf_intersect_ta_private(sess->ctx, 302106d8aa6SPascal Brand param->params[n].memref.buffer, 303b0104773SPascal Brand param->params[n].memref.size)) 304b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 305b0104773SPascal Brand 306b0104773SPascal Brand if (tee_mmu_user_va2pa(sess->ctx, 307b0104773SPascal Brand (void *)param->params[n].memref.buffer, 3080e692b78SJens Wiklander &src_pa) != TEE_SUCCESS) 309b0104773SPascal Brand return TEE_ERROR_BAD_PARAMETERS; 310b0104773SPascal Brand 311b0104773SPascal Brand param->param_attr[n] = tee_mmu_user_get_cache_attr( 312b0104773SPascal Brand sess->ctx, 313b0104773SPascal Brand (void *)param->params[n].memref.buffer); 314b0104773SPascal Brand 315b0104773SPascal Brand param->params[n].memref.buffer = (void *)src_pa; 316b0104773SPascal Brand break; 317b0104773SPascal Brand 318b0104773SPascal Brand default: 319b0104773SPascal Brand break; 320b0104773SPascal Brand } 321b0104773SPascal Brand } 322b0104773SPascal Brand 323b0104773SPascal Brand if (req_mem == 0) 324b0104773SPascal Brand return TEE_SUCCESS; 325b0104773SPascal Brand 326b0104773SPascal Brand /* Allocate section in secure DDR */ 327b0104773SPascal Brand *mm = tee_mm_alloc(&tee_mm_sec_ddr, req_mem); 328b0104773SPascal Brand if (*mm == NULL) { 329b0104773SPascal Brand DMSG("tee_mm_alloc TEE_ERROR_GENERIC"); 330b0104773SPascal Brand return TEE_ERROR_GENERIC; 331b0104773SPascal Brand } 332b0104773SPascal Brand 333b0104773SPascal Brand /* Get the virtual address for the section in secure DDR */ 334b0104773SPascal Brand res = tee_mmu_kmap(tee_mm_get_smem(*mm), req_mem, &dst); 335b0104773SPascal Brand if (res != TEE_SUCCESS) 336b0104773SPascal Brand return res; 337b0104773SPascal Brand dst_pa = tee_mm_get_smem(*mm); 338b0104773SPascal Brand 339b0104773SPascal Brand for (n = 0; n < 4; n++) { 340b0104773SPascal Brand 341b0104773SPascal Brand if (ta_private_memref[n] == false) 342b0104773SPascal Brand continue; 343b0104773SPascal Brand 344a17acc4cSSabrina Ni s = ROUNDUP(param->params[n].memref.size, sizeof(uint32_t)); 345b0104773SPascal Brand 346b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 347b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INPUT: 348b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 349b0104773SPascal Brand if (param->params[n].memref.buffer != NULL) { 350b0104773SPascal Brand res = tee_svc_copy_from_user(sess, dst, 351b7fc217fSPascal Brand param->params[n].memref.buffer, 352b7fc217fSPascal Brand param->params[n].memref.size); 353b0104773SPascal Brand if (res != TEE_SUCCESS) 354b0104773SPascal Brand return res; 355b0104773SPascal Brand param->param_attr[n] = 356b0104773SPascal Brand tee_mmu_kmap_get_cache_attr(dst); 357b0104773SPascal Brand param->params[n].memref.buffer = (void *)dst_pa; 358b0104773SPascal Brand tmp_buf_pa[n] = dst_pa; 359b0104773SPascal Brand dst += s; 360b0104773SPascal Brand dst_pa += s; 361b0104773SPascal Brand } 362b0104773SPascal Brand break; 363b0104773SPascal Brand 364b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 365b0104773SPascal Brand if (param->params[n].memref.buffer != NULL) { 366b0104773SPascal Brand param->param_attr[n] = 367b0104773SPascal Brand tee_mmu_kmap_get_cache_attr(dst); 368b0104773SPascal Brand param->params[n].memref.buffer = (void *)dst_pa; 369b0104773SPascal Brand tmp_buf_pa[n] = dst_pa; 370b0104773SPascal Brand dst += s; 371b0104773SPascal Brand dst_pa += s; 372b0104773SPascal Brand } 373b0104773SPascal Brand break; 374b0104773SPascal Brand 375b0104773SPascal Brand default: 376b0104773SPascal Brand continue; 377b0104773SPascal Brand } 378b0104773SPascal Brand } 379b0104773SPascal Brand 380b0104773SPascal Brand tee_mmu_kunmap(dst, req_mem); 381b0104773SPascal Brand 382b0104773SPascal Brand return TEE_SUCCESS; 383b0104773SPascal Brand } 384b0104773SPascal Brand 385b0104773SPascal Brand /* 386b0104773SPascal Brand * Back from execution of service: update parameters passed from TA: 387b0104773SPascal Brand * If some parameters were memory references: 388b0104773SPascal Brand * - either the memref was temporary: copy back data and update size 389b0104773SPascal Brand * - or it was the original TA memref: update only the size value. 390b0104773SPascal Brand */ 391b0104773SPascal Brand static TEE_Result tee_svc_update_out_param( 392b0104773SPascal Brand struct tee_ta_session *sess, 393b0104773SPascal Brand struct tee_ta_session *called_sess, 394b0104773SPascal Brand struct tee_ta_param *param, 395b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS], 396b7fc217fSPascal Brand TEE_Param callee_params[TEE_NUM_PARAMS]) 397b0104773SPascal Brand { 398b0104773SPascal Brand size_t n; 399b0104773SPascal Brand bool have_private_mem_map = (called_sess == NULL) || 400b0104773SPascal Brand (called_sess->ctx->static_ta != NULL) || 401b0104773SPascal Brand ((called_sess->ctx->flags & TA_FLAG_USER_MODE) != 0); 402b0104773SPascal Brand 403b0104773SPascal Brand tee_ta_set_current_session(sess); 404b0104773SPascal Brand 405b0104773SPascal Brand for (n = 0; n < TEE_NUM_PARAMS; n++) { 406b0104773SPascal Brand switch (TEE_PARAM_TYPE_GET(param->types, n)) { 407b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_OUTPUT: 408b0104773SPascal Brand case TEE_PARAM_TYPE_MEMREF_INOUT: 409b0104773SPascal Brand 410b0104773SPascal Brand /* outside TA private => memref is valid, update size */ 411b0104773SPascal Brand if (!tee_mmu_is_vbuf_inside_ta_private(sess->ctx, 412106d8aa6SPascal Brand callee_params[n].memref.buffer, 413b0104773SPascal Brand param->params[n].memref.size)) { 414b7fc217fSPascal Brand callee_params[n].memref.size = 415b0104773SPascal Brand param->params[n].memref.size; 416b0104773SPascal Brand break; 417b0104773SPascal Brand } 418b0104773SPascal Brand 419b0104773SPascal Brand /* 420b0104773SPascal Brand * If we called a kernel TA the parameters are in shared 421b0104773SPascal Brand * memory and no copy is needed. 422b0104773SPascal Brand */ 423b0104773SPascal Brand if (have_private_mem_map && 424b0104773SPascal Brand param->params[n].memref.size <= 425b7fc217fSPascal Brand callee_params[n].memref.size) { 426b0104773SPascal Brand uint8_t *src = 0; 427b0104773SPascal Brand TEE_Result res; 428b0104773SPascal Brand 429b0104773SPascal Brand /* FIXME: TA_RAM is already mapped ! */ 430b0104773SPascal Brand res = tee_mmu_kmap(tmp_buf_pa[n], 431b0104773SPascal Brand param->params[n].memref.size, &src); 432b0104773SPascal Brand if (res != TEE_SUCCESS) 433b0104773SPascal Brand return TEE_ERROR_GENERIC; 434b0104773SPascal Brand 435b0104773SPascal Brand res = tee_svc_copy_to_user(sess, 436b7fc217fSPascal Brand callee_params[n].memref. 437b0104773SPascal Brand buffer, src, 438b0104773SPascal Brand param->params[n]. 439b0104773SPascal Brand memref.size); 440b0104773SPascal Brand if (res != TEE_SUCCESS) 441b0104773SPascal Brand return res; 442b0104773SPascal Brand tee_mmu_kunmap(src, 443b0104773SPascal Brand param->params[n].memref.size); 444b0104773SPascal Brand 445b0104773SPascal Brand } 446b7fc217fSPascal Brand callee_params[n].memref.size = param->params[n].memref.size; 447b0104773SPascal Brand break; 448b0104773SPascal Brand 449b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_OUTPUT: 450b0104773SPascal Brand case TEE_PARAM_TYPE_VALUE_INOUT: 451b7fc217fSPascal Brand callee_params[n].value = param->params[n].value; 452b0104773SPascal Brand break; 453b0104773SPascal Brand 454b0104773SPascal Brand default: 455b0104773SPascal Brand continue; 456b0104773SPascal Brand } 457b0104773SPascal Brand } 458b0104773SPascal Brand 459b0104773SPascal Brand return TEE_SUCCESS; 460b0104773SPascal Brand } 461b0104773SPascal Brand 462b0104773SPascal Brand /* Called when a TA calls an OpenSession on another TA */ 463b0104773SPascal Brand TEE_Result tee_svc_open_ta_session(const TEE_UUID *dest, 464b0104773SPascal Brand uint32_t cancel_req_to, uint32_t param_types, 465b0104773SPascal Brand TEE_Param params[4], 466b0104773SPascal Brand TEE_TASessionHandle *ta_sess, 467b0104773SPascal Brand uint32_t *ret_orig) 468b0104773SPascal Brand { 469b0104773SPascal Brand TEE_Result res; 470b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 471b0104773SPascal Brand struct tee_ta_session *s = NULL; 472b0104773SPascal Brand struct tee_ta_session *sess; 473b0104773SPascal Brand tee_mm_entry_t *mm_param = NULL; 474b0104773SPascal Brand 475b0104773SPascal Brand TEE_UUID *uuid = malloc(sizeof(TEE_UUID)); 476b0104773SPascal Brand struct tee_ta_param *param = malloc(sizeof(struct tee_ta_param)); 477b0104773SPascal Brand TEE_Identity *clnt_id = malloc(sizeof(TEE_Identity)); 478b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 479b0104773SPascal Brand 480b0104773SPascal Brand if (uuid == NULL || param == NULL || clnt_id == NULL) { 481b0104773SPascal Brand res = TEE_ERROR_OUT_OF_MEMORY; 482b0104773SPascal Brand goto out_free_only; 483b0104773SPascal Brand } 484b0104773SPascal Brand 485b0104773SPascal Brand memset(param, 0, sizeof(struct tee_ta_param)); 486b0104773SPascal Brand 487b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 488b0104773SPascal Brand if (res != TEE_SUCCESS) 489b0104773SPascal Brand goto out_free_only; 490b0104773SPascal Brand 491b0104773SPascal Brand res = tee_svc_copy_from_user(sess, uuid, dest, sizeof(TEE_UUID)); 492b0104773SPascal Brand if (res != TEE_SUCCESS) 493b0104773SPascal Brand goto function_exit; 494b0104773SPascal Brand 495b0104773SPascal Brand clnt_id->login = TEE_LOGIN_TRUSTED_APP; 496b0104773SPascal Brand memcpy(&clnt_id->uuid, &sess->ctx->head->uuid, sizeof(TEE_UUID)); 497b0104773SPascal Brand 498b0104773SPascal Brand res = tee_svc_copy_param(sess, NULL, param_types, params, param, 499b0104773SPascal Brand tmp_buf_pa, &mm_param); 500b0104773SPascal Brand if (res != TEE_SUCCESS) 501b0104773SPascal Brand goto function_exit; 502b0104773SPascal Brand 503b0104773SPascal Brand /* 504b0104773SPascal Brand * Find session of a multi session TA or a static TA 505b0104773SPascal Brand * In such a case, there is no need to ask the supplicant for the TA 506b0104773SPascal Brand * code 507b0104773SPascal Brand */ 508b0104773SPascal Brand res = tee_ta_open_session(&ret_o, &s, &sess->ctx->open_sessions, uuid, 50927cbcc57SJens Wiklander clnt_id, cancel_req_to, param); 510c0346845SJens Wiklander if (res != TEE_SUCCESS) 511b0104773SPascal Brand goto function_exit; 512b0104773SPascal Brand 513b0104773SPascal Brand res = tee_svc_update_out_param(sess, NULL, param, tmp_buf_pa, params); 514b0104773SPascal Brand 515b0104773SPascal Brand function_exit: 516b0104773SPascal Brand tee_ta_set_current_session(sess); 517b0104773SPascal Brand 518b0104773SPascal Brand if (mm_param != NULL) { 519b0104773SPascal Brand TEE_Result res2; 520b0104773SPascal Brand void *va = 0; 521b0104773SPascal Brand 522b0104773SPascal Brand res2 = 523b0104773SPascal Brand tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 524b0104773SPascal Brand if (res2 == TEE_SUCCESS) 525b0104773SPascal Brand tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 526b0104773SPascal Brand } 527b0104773SPascal Brand tee_mm_free(mm_param); 528b0104773SPascal Brand tee_svc_copy_to_user(sess, ta_sess, &s, sizeof(s)); 529b0104773SPascal Brand tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 530b0104773SPascal Brand 531b0104773SPascal Brand out_free_only: 532b0104773SPascal Brand free(param); 533b0104773SPascal Brand free(uuid); 534b0104773SPascal Brand free(clnt_id); 535b0104773SPascal Brand return res; 536b0104773SPascal Brand } 537b0104773SPascal Brand 538b0104773SPascal Brand TEE_Result tee_svc_close_ta_session(TEE_TASessionHandle ta_sess) 539b0104773SPascal Brand { 540b0104773SPascal Brand TEE_Result res; 541b0104773SPascal Brand struct tee_ta_session *sess; 542b0104773SPascal Brand 543b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 544b0104773SPascal Brand if (res != TEE_SUCCESS) 545b0104773SPascal Brand return res; 546b0104773SPascal Brand 547b0104773SPascal Brand tee_ta_set_current_session(NULL); 548b0104773SPascal Brand 549b0104773SPascal Brand res = 550b0104773SPascal Brand tee_ta_close_session((uint32_t)ta_sess, &sess->ctx->open_sessions); 551b0104773SPascal Brand tee_ta_set_current_session(sess); 552b0104773SPascal Brand return res; 553b0104773SPascal Brand } 554b0104773SPascal Brand 555b0104773SPascal Brand TEE_Result tee_svc_invoke_ta_command(TEE_TASessionHandle ta_sess, 556b0104773SPascal Brand uint32_t cancel_req_to, uint32_t cmd_id, 557b0104773SPascal Brand uint32_t param_types, TEE_Param params[4], 558b0104773SPascal Brand uint32_t *ret_orig) 559b0104773SPascal Brand { 560b0104773SPascal Brand TEE_Result res; 561b0104773SPascal Brand uint32_t ret_o = TEE_ORIGIN_TEE; 562b0104773SPascal Brand struct tee_ta_param param = { 0 }; 563b0104773SPascal Brand struct tee_ta_session *sess; 564b0104773SPascal Brand struct tee_ta_session *called_sess = (struct tee_ta_session *)ta_sess; 565b0104773SPascal Brand tee_mm_entry_t *mm_param = NULL; 566b0104773SPascal Brand tee_paddr_t tmp_buf_pa[TEE_NUM_PARAMS]; 567b0104773SPascal Brand 568b0104773SPascal Brand res = tee_ta_get_current_session(&sess); 569b0104773SPascal Brand if (res != TEE_SUCCESS) 570b0104773SPascal Brand return res; 571b0104773SPascal Brand 572b0104773SPascal Brand res = 573b0104773SPascal Brand tee_ta_verify_session_pointer(called_sess, 574b0104773SPascal Brand &sess->ctx->open_sessions); 575b0104773SPascal Brand if (res != TEE_SUCCESS) 576b0104773SPascal Brand return res; 577b0104773SPascal Brand 578b0104773SPascal Brand res = tee_svc_copy_param(sess, called_sess, param_types, params, 579b0104773SPascal Brand ¶m, tmp_buf_pa, &mm_param); 580b0104773SPascal Brand if (res != TEE_SUCCESS) 581b0104773SPascal Brand goto function_exit; 582b0104773SPascal Brand 583b0104773SPascal Brand res = 584cebdec51SJens Wiklander tee_ta_invoke_command(&ret_o, called_sess, cancel_req_to, 585b0104773SPascal Brand cmd_id, ¶m); 586b0104773SPascal Brand if (res != TEE_SUCCESS) 587b0104773SPascal Brand goto function_exit; 588b0104773SPascal Brand 589b0104773SPascal Brand res = tee_svc_update_out_param(sess, called_sess, ¶m, tmp_buf_pa, 590b0104773SPascal Brand params); 591b0104773SPascal Brand if (res != TEE_SUCCESS) 592b0104773SPascal Brand goto function_exit; 593b0104773SPascal Brand 594b0104773SPascal Brand function_exit: 595b0104773SPascal Brand tee_ta_set_current_session(sess); 596b0104773SPascal Brand called_sess->calling_sess = NULL; /* clear eventual borrowed mapping */ 597b0104773SPascal Brand 598b0104773SPascal Brand if (mm_param != NULL) { 599b0104773SPascal Brand TEE_Result res2; 600b0104773SPascal Brand void *va = 0; 601b0104773SPascal Brand 602b0104773SPascal Brand res2 = 603b0104773SPascal Brand tee_mmu_kmap_pa2va((void *)tee_mm_get_smem(mm_param), &va); 604b0104773SPascal Brand if (res2 == TEE_SUCCESS) 605b0104773SPascal Brand tee_mmu_kunmap(va, tee_mm_get_bytes(mm_param)); 606b0104773SPascal Brand } 607b0104773SPascal Brand tee_mm_free(mm_param); 608b0104773SPascal Brand if (ret_orig) 609b0104773SPascal Brand tee_svc_copy_to_user(sess, ret_orig, &ret_o, sizeof(ret_o)); 610b0104773SPascal Brand return res; 611b0104773SPascal Brand } 612b0104773SPascal Brand 613b0104773SPascal Brand TEE_Result tee_svc_check_access_rights(uint32_t flags, const void *buf, 614b0104773SPascal Brand size_t len) 615b0104773SPascal Brand { 616b0104773SPascal Brand TEE_Result res; 617b0104773SPascal Brand struct tee_ta_session *s; 618b0104773SPascal Brand 619b0104773SPascal Brand res = tee_ta_get_current_session(&s); 620b0104773SPascal Brand if (res != TEE_SUCCESS) 621b0104773SPascal Brand return res; 622b0104773SPascal Brand 623b0104773SPascal Brand return tee_mmu_check_access_rights(s->ctx, flags, (tee_uaddr_t)buf, 624b0104773SPascal Brand len); 625b0104773SPascal Brand } 626b0104773SPascal Brand 627b0104773SPascal Brand TEE_Result tee_svc_copy_from_user(struct tee_ta_session *sess, void *kaddr, 628b0104773SPascal Brand const void *uaddr, size_t len) 629b0104773SPascal Brand { 630b0104773SPascal Brand TEE_Result res; 631b0104773SPascal Brand struct tee_ta_session *s; 632b0104773SPascal Brand 633b0104773SPascal Brand if (sess == NULL) { 634b0104773SPascal Brand res = tee_ta_get_current_session(&s); 635b0104773SPascal Brand if (res != TEE_SUCCESS) 636b0104773SPascal Brand return res; 637b0104773SPascal Brand } else { 638b0104773SPascal Brand s = sess; 639b0104773SPascal Brand tee_ta_set_current_session(s); 640b0104773SPascal Brand } 641b0104773SPascal Brand res = 642b0104773SPascal Brand tee_mmu_check_access_rights(s->ctx, 643b0104773SPascal Brand TEE_MEMORY_ACCESS_READ | 644b0104773SPascal Brand TEE_MEMORY_ACCESS_ANY_OWNER, 645b0104773SPascal Brand (tee_uaddr_t)uaddr, len); 646b0104773SPascal Brand if (res != TEE_SUCCESS) 647b0104773SPascal Brand return res; 648b0104773SPascal Brand 649b0104773SPascal Brand memcpy(kaddr, uaddr, len); 650b0104773SPascal Brand return TEE_SUCCESS; 651b0104773SPascal Brand } 652b0104773SPascal Brand 653b0104773SPascal Brand TEE_Result tee_svc_copy_to_user(struct tee_ta_session *sess, void *uaddr, 654b0104773SPascal Brand const void *kaddr, size_t len) 655b0104773SPascal Brand { 656b0104773SPascal Brand TEE_Result res; 657b0104773SPascal Brand struct tee_ta_session *s; 658b0104773SPascal Brand 659b0104773SPascal Brand if (sess == NULL) { 660b0104773SPascal Brand res = tee_ta_get_current_session(&s); 661b0104773SPascal Brand if (res != TEE_SUCCESS) 662b0104773SPascal Brand return res; 663b0104773SPascal Brand } else { 664b0104773SPascal Brand s = sess; 665b0104773SPascal Brand tee_ta_set_current_session(s); 666b0104773SPascal Brand } 667b0104773SPascal Brand 668b0104773SPascal Brand res = 669b0104773SPascal Brand tee_mmu_check_access_rights(s->ctx, 670b0104773SPascal Brand TEE_MEMORY_ACCESS_WRITE | 671b0104773SPascal Brand TEE_MEMORY_ACCESS_ANY_OWNER, 672b0104773SPascal Brand (tee_uaddr_t)uaddr, len); 673b0104773SPascal Brand if (res != TEE_SUCCESS) 674b0104773SPascal Brand return res; 675b0104773SPascal Brand 676b0104773SPascal Brand memcpy(uaddr, kaddr, len); 677b0104773SPascal Brand return TEE_SUCCESS; 678b0104773SPascal Brand } 679b0104773SPascal Brand 680b0104773SPascal Brand static bool session_is_cancelled(struct tee_ta_session *s, TEE_Time *curr_time) 681b0104773SPascal Brand { 682b0104773SPascal Brand TEE_Time current_time; 683b0104773SPascal Brand 684b0104773SPascal Brand if (s->cancel_mask) 685b0104773SPascal Brand return false; 686b0104773SPascal Brand 687b0104773SPascal Brand if (s->cancel) 688b0104773SPascal Brand return true; 689b0104773SPascal Brand 690b0104773SPascal Brand if (s->cancel_time.seconds == UINT32_MAX) 691b0104773SPascal Brand return false; 692b0104773SPascal Brand 693b0104773SPascal Brand if (curr_time != NULL) 694b0104773SPascal Brand current_time = *curr_time; 695b0104773SPascal Brand else if (tee_time_get_sys_time(¤t_time) != TEE_SUCCESS) 696b0104773SPascal Brand return false; 697b0104773SPascal Brand 698b0104773SPascal Brand if (current_time.seconds > s->cancel_time.seconds || 699b0104773SPascal Brand (current_time.seconds == s->cancel_time.seconds && 700b0104773SPascal Brand current_time.millis >= s->cancel_time.millis)) { 701b0104773SPascal Brand return true; 702b0104773SPascal Brand } 703b0104773SPascal Brand 704b0104773SPascal Brand return false; 705b0104773SPascal Brand } 706b0104773SPascal Brand 707b0104773SPascal Brand TEE_Result tee_svc_get_cancellation_flag(bool *cancel) 708b0104773SPascal Brand { 709b0104773SPascal Brand TEE_Result res; 710b0104773SPascal Brand struct tee_ta_session *s = NULL; 711b0104773SPascal Brand bool c; 712b0104773SPascal Brand 713b0104773SPascal Brand res = tee_ta_get_current_session(&s); 714b0104773SPascal Brand if (res != TEE_SUCCESS) 715b0104773SPascal Brand return res; 716b0104773SPascal Brand 717b0104773SPascal Brand c = session_is_cancelled(s, NULL); 718b0104773SPascal Brand 719b0104773SPascal Brand return tee_svc_copy_to_user(s, cancel, &c, sizeof(c)); 720b0104773SPascal Brand } 721b0104773SPascal Brand 722b0104773SPascal Brand TEE_Result tee_svc_unmask_cancellation(bool *old_mask) 723b0104773SPascal Brand { 724b0104773SPascal Brand TEE_Result res; 725b0104773SPascal Brand struct tee_ta_session *s = NULL; 726b0104773SPascal Brand bool m; 727b0104773SPascal Brand 728b0104773SPascal Brand res = tee_ta_get_current_session(&s); 729b0104773SPascal Brand if (res != TEE_SUCCESS) 730b0104773SPascal Brand return res; 731b0104773SPascal Brand 732b0104773SPascal Brand m = s->cancel_mask; 733b0104773SPascal Brand s->cancel_mask = false; 734b0104773SPascal Brand return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 735b0104773SPascal Brand } 736b0104773SPascal Brand 737b0104773SPascal Brand TEE_Result tee_svc_mask_cancellation(bool *old_mask) 738b0104773SPascal Brand { 739b0104773SPascal Brand TEE_Result res; 740b0104773SPascal Brand struct tee_ta_session *s = NULL; 741b0104773SPascal Brand bool m; 742b0104773SPascal Brand 743b0104773SPascal Brand res = tee_ta_get_current_session(&s); 744b0104773SPascal Brand if (res != TEE_SUCCESS) 745b0104773SPascal Brand return res; 746b0104773SPascal Brand 747b0104773SPascal Brand m = s->cancel_mask; 748b0104773SPascal Brand s->cancel_mask = true; 749b0104773SPascal Brand return tee_svc_copy_to_user(s, old_mask, &m, sizeof(m)); 750b0104773SPascal Brand } 751b0104773SPascal Brand 752b0104773SPascal Brand TEE_Result tee_svc_wait(uint32_t timeout) 753b0104773SPascal Brand { 754b0104773SPascal Brand TEE_Result res = TEE_SUCCESS; 755b0104773SPascal Brand uint32_t mytime = 0; 756b0104773SPascal Brand struct tee_ta_session *s; 757b0104773SPascal Brand TEE_Time base_time; 758b0104773SPascal Brand TEE_Time current_time; 759b0104773SPascal Brand 760b0104773SPascal Brand res = tee_ta_get_current_session(&s); 761b0104773SPascal Brand if (res != TEE_SUCCESS) 762b0104773SPascal Brand return res; 763b0104773SPascal Brand 764b0104773SPascal Brand res = tee_time_get_sys_time(&base_time); 765b0104773SPascal Brand if (res != TEE_SUCCESS) 766b0104773SPascal Brand return res; 767b0104773SPascal Brand 768b0104773SPascal Brand while (true) { 769b0104773SPascal Brand res = tee_time_get_sys_time(¤t_time); 770b0104773SPascal Brand if (res != TEE_SUCCESS) 771b0104773SPascal Brand return res; 772b0104773SPascal Brand 773b0104773SPascal Brand if (session_is_cancelled(s, ¤t_time)) 774b0104773SPascal Brand return TEE_ERROR_CANCEL; 775b0104773SPascal Brand 776b0104773SPascal Brand mytime = (current_time.seconds - base_time.seconds) * 1000 + 777b0104773SPascal Brand (int)current_time.millis - (int)base_time.millis; 778b0104773SPascal Brand if (mytime >= timeout) 779b0104773SPascal Brand return TEE_SUCCESS; 780b0104773SPascal Brand 781d1aea08fSSY Chiu tee_time_wait(timeout - mytime); 782b0104773SPascal Brand } 783b0104773SPascal Brand 784b0104773SPascal Brand return res; 785b0104773SPascal Brand } 786b0104773SPascal Brand 787b0104773SPascal Brand TEE_Result tee_svc_get_time(enum utee_time_category cat, TEE_Time *mytime) 788b0104773SPascal Brand { 789b0104773SPascal Brand TEE_Result res, res2; 790b0104773SPascal Brand struct tee_ta_session *s = NULL; 791b0104773SPascal Brand TEE_Time t; 792b0104773SPascal Brand 793b0104773SPascal Brand res = tee_ta_get_current_session(&s); 794b0104773SPascal Brand if (res != TEE_SUCCESS) 795b0104773SPascal Brand return res; 796b0104773SPascal Brand 797b0104773SPascal Brand switch (cat) { 798b0104773SPascal Brand case UTEE_TIME_CAT_SYSTEM: 799b0104773SPascal Brand res = tee_time_get_sys_time(&t); 800b0104773SPascal Brand break; 801b0104773SPascal Brand case UTEE_TIME_CAT_TA_PERSISTENT: 802b0104773SPascal Brand res = 803b0104773SPascal Brand tee_time_get_ta_time((const void *)&s->ctx->head->uuid, &t); 804b0104773SPascal Brand break; 805b0104773SPascal Brand case UTEE_TIME_CAT_REE: 806b0104773SPascal Brand res = tee_time_get_ree_time(&t); 807b0104773SPascal Brand break; 808b0104773SPascal Brand default: 809b0104773SPascal Brand res = TEE_ERROR_BAD_PARAMETERS; 810b0104773SPascal Brand break; 811b0104773SPascal Brand } 812b0104773SPascal Brand 813b0104773SPascal Brand if (res == TEE_SUCCESS || res == TEE_ERROR_OVERFLOW) { 814b0104773SPascal Brand res2 = tee_svc_copy_to_user(s, mytime, &t, sizeof(t)); 815b0104773SPascal Brand if (res2 != TEE_SUCCESS) 816b0104773SPascal Brand res = res2; 817b0104773SPascal Brand } 818b0104773SPascal Brand 819b0104773SPascal Brand return res; 820b0104773SPascal Brand } 821b0104773SPascal Brand 822b0104773SPascal Brand TEE_Result tee_svc_set_ta_time(const TEE_Time *mytime) 823b0104773SPascal Brand { 824b0104773SPascal Brand TEE_Result res; 825b0104773SPascal Brand struct tee_ta_session *s = NULL; 826b0104773SPascal Brand TEE_Time t; 827b0104773SPascal Brand 828b0104773SPascal Brand res = tee_ta_get_current_session(&s); 829b0104773SPascal Brand if (res != TEE_SUCCESS) 830b0104773SPascal Brand return res; 831b0104773SPascal Brand 832b0104773SPascal Brand res = tee_svc_copy_from_user(s, &t, mytime, sizeof(t)); 833b0104773SPascal Brand if (res != TEE_SUCCESS) 834b0104773SPascal Brand return res; 835b0104773SPascal Brand 836b0104773SPascal Brand return tee_time_set_ta_time((const void *)&s->ctx->head->uuid, &t); 837b0104773SPascal Brand } 838