1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  */
5 
6 #ifndef XTEST_HELPERS_H
7 #define XTEST_HELPERS_H
8 
9 #include <adbg.h>
10 #include <pthread.h>
11 #include <tee_api_types.h>
12 #include <tee_client_api.h>
13 
14 extern unsigned int level;
15 
16 /* Global context to use if any context is needed as input to a function */
17 extern TEEC_Context xtest_teec_ctx;
18 
19 /*
20  * Initializes the context above, should be called before the ADBG test
21  * cases are run.
22  */
23 TEEC_Result xtest_teec_ctx_init(void);
24 void xtest_teec_ctx_deinit(void);
25 
26 /* Opens a session */
27 TEEC_Result xtest_teec_open_session(TEEC_Session *session,
28 				    const TEEC_UUID *uuid, TEEC_Operation *op,
29 				    uint32_t *ret_orig);
30 
31 TEEC_Result xtest_teec_open_static_session(TEEC_Session *session,
32 					   TEEC_Operation *op,
33 					   uint32_t *ret_orig);
34 
35 #define TEEC_OPERATION_INITIALIZER	{ }
36 
37 /* IO access macro */
38 #define  IO(addr)  (*((volatile unsigned long *)(addr)))
39 
40 #define UNUSED(x) (void)(x)
41 /*
42  * Helpers for commands towards the crypt TA
43  */
44 TEEC_Result ta_crypt_cmd_allocate_operation(ADBG_Case_t *c,
45 			   TEEC_Session *s,
46 			   TEE_OperationHandle *oph,
47 			   uint32_t algo,
48 			   uint32_t mode,
49 			   uint32_t max_key_size);
50 
51 TEEC_Result ta_crypt_cmd_allocate_transient_object(ADBG_Case_t *c,
52 	TEEC_Session *s,
53 	TEE_ObjectType obj_type, uint32_t max_obj_size,
54 	TEE_ObjectHandle *o);
55 
56 TEEC_Result ta_crypt_cmd_populate_transient_object(ADBG_Case_t *c,
57 	TEEC_Session *s,
58 	TEE_ObjectHandle o,
59 	const TEE_Attribute *attrs,
60 	uint32_t attr_count);
61 
62 TEE_Result ta_crypt_cmd_set_operation_key(ADBG_Case_t *c,
63 			 TEEC_Session *s,
64 			 TEE_OperationHandle oph,
65 			 TEE_ObjectHandle key);
66 
67 TEEC_Result ta_crypt_cmd_free_transient_object(ADBG_Case_t *c,
68 			      TEEC_Session *s,
69 			      TEE_ObjectHandle o);
70 
71 TEEC_Result ta_crypt_cmd_derive_key(ADBG_Case_t *c,
72 					   TEEC_Session *s,
73 					   TEE_OperationHandle oph,
74 					   TEE_ObjectHandle o,
75 					   const TEE_Attribute *params,
76 					   uint32_t paramCount);
77 
78 TEEC_Result ta_crypt_cmd_get_object_buffer_attribute(ADBG_Case_t *c,
79 				    TEEC_Session *s,
80 				    TEE_ObjectHandle o,
81 				    uint32_t attr_id,
82 				    void *buf,
83 				    size_t *blen);
84 
85 TEEC_Result ta_crypt_cmd_free_operation(ADBG_Case_t *c,
86 					       TEEC_Session *s,
87 					       TEE_OperationHandle oph);
88 
89 bool ta_crypt_cmd_is_algo_supported(ADBG_Case_t *c, TEEC_Session *s,
90 				    uint32_t alg, uint32_t element);
91 
92 TEEC_Result ta_os_test_cmd_client_identity(TEEC_Session *session,
93 					   uint32_t *login,
94 					   TEEC_UUID *client_uuid);
95 
96 void xtest_add_attr(size_t *attr_count, TEE_Attribute *attrs,
97 			   uint32_t attr_id, const void *buf, size_t len);
98 void xtest_add_attr_value(size_t *attr_count, TEE_Attribute *attrs,
99 			  uint32_t attr_id, uint32_t value_a, uint32_t value_b);
100 
101 TEE_Result pack_attrs(const TEE_Attribute *attrs, uint32_t attr_count,
102 			     uint8_t **buf, size_t *blen);
103 
104 void xtest_mutex_init(pthread_mutex_t *mutex);
105 void xtest_mutex_destroy(pthread_mutex_t *mutex);
106 void xtest_mutex_lock(pthread_mutex_t *mutex);
107 void xtest_mutex_unlock(pthread_mutex_t *mutex);
108 
109 void xtest_barrier_init(pthread_barrier_t *barrier, unsigned count);
110 void xtest_barrier_destroy(pthread_barrier_t *barrier);
111 int xtest_barrier_wait(pthread_barrier_t *barrier);
112 
113 #endif /*XTEST_HELPERS_H*/
114