1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2019, Linaro Limited
4  * Copyright (c) 2020, Arm Limited
5  */
6 
7 #include <ldelf.h>
8 #include <trace.h>
9 
10 #include "sys.h"
11 
12 int trace_level = TRACE_LEVEL;
13 const char trace_ext_prefix[]  = "LD";
14 
__panic(const char * file __maybe_unused,const int line __maybe_unused,const char * func __maybe_unused)15 void __panic(const char *file __maybe_unused, const int line __maybe_unused,
16 	     const char *func __maybe_unused)
17 {
18 	if (!file && !func)
19 		EMSG_RAW("Panic");
20 	else
21 		EMSG_RAW("Panic at %s:%d %s%s%s",
22 			 file ? file : "?", file ? line : 0,
23 			 func ? "<" : "", func ? func : "", func ? ">" : "");
24 
25 	_ldelf_panic(1);
26 	/*NOTREACHED*/
27 	while (true)
28 		;
29 }
30 
sys_return_cleanup(void)31 void sys_return_cleanup(void)
32 {
33 	_ldelf_return(0);
34 	/*NOTREACHED*/
35 	while (true)
36 		;
37 }
38 
sys_map_zi(size_t num_bytes,uint32_t flags,vaddr_t * va,size_t pad_begin,size_t pad_end)39 TEE_Result sys_map_zi(size_t num_bytes, uint32_t flags, vaddr_t *va,
40 		      size_t pad_begin, size_t pad_end)
41 {
42 	return _ldelf_map_zi(va, num_bytes, pad_begin, pad_end, flags);
43 }
44 
sys_unmap(vaddr_t va,size_t num_bytes)45 TEE_Result sys_unmap(vaddr_t va, size_t num_bytes)
46 {
47 	return _ldelf_unmap(va, num_bytes);
48 }
49 
sys_open_ta_bin(const TEE_UUID * uuid,uint32_t * handle)50 TEE_Result sys_open_ta_bin(const TEE_UUID *uuid, uint32_t *handle)
51 {
52 	return _ldelf_open_bin(uuid, sizeof(TEE_UUID), handle);
53 }
54 
sys_close_ta_bin(uint32_t handle)55 TEE_Result sys_close_ta_bin(uint32_t handle)
56 {
57 	return _ldelf_close_bin(handle);
58 }
59 
sys_map_ta_bin(vaddr_t * va,size_t num_bytes,uint32_t flags,uint32_t handle,size_t offs,size_t pad_begin,size_t pad_end)60 TEE_Result sys_map_ta_bin(vaddr_t *va, size_t num_bytes, uint32_t flags,
61 			  uint32_t handle, size_t offs, size_t pad_begin,
62 			  size_t pad_end)
63 {
64 	return _ldelf_map_bin(va, num_bytes, handle, offs,
65 			     pad_begin, pad_end, flags);
66 }
67 
68 
sys_copy_from_ta_bin(void * dst,size_t num_bytes,uint32_t handle,size_t offs)69 TEE_Result sys_copy_from_ta_bin(void *dst, size_t num_bytes, uint32_t handle,
70 				size_t offs)
71 {
72 	return _ldelf_cp_from_bin(dst, offs, num_bytes, handle);
73 }
74 
sys_set_prot(vaddr_t va,size_t num_bytes,uint32_t flags)75 TEE_Result sys_set_prot(vaddr_t va, size_t num_bytes, uint32_t flags)
76 {
77 	return _ldelf_set_prot(va, num_bytes, flags);
78 }
79 
sys_remap(vaddr_t old_va,vaddr_t * new_va,size_t num_bytes,size_t pad_begin,size_t pad_end)80 TEE_Result sys_remap(vaddr_t old_va, vaddr_t *new_va, size_t num_bytes,
81 		     size_t pad_begin, size_t pad_end)
82 {
83 	return _ldelf_remap(old_va, new_va, num_bytes, pad_begin, pad_end);
84 }
85 
sys_gen_random_num(void * buf,size_t blen)86 TEE_Result sys_gen_random_num(void *buf, size_t blen)
87 {
88 	return _ldelf_gen_rnd_num(buf, blen);
89 }
90