1 //SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2021 Open Mobile Platform LLC
4  */
5 
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <ta_supp_plugin.h>
11 #include <tee_internal_api.h>
12 #include <tee_internal_api_extensions.h>
13 #include <test_supp_plugin.h>
14 #include <trace.h>
15 
16 static const TEE_UUID uuid = TEST_PLUGIN_UUID;
17 
TA_CreateEntryPoint(void)18 TEE_Result TA_CreateEntryPoint(void)
19 {
20 	return TEE_SUCCESS;
21 }
22 
TA_DestroyEntryPoint(void)23 void TA_DestroyEntryPoint(void)
24 {
25 }
26 
TA_OpenSessionEntryPoint(uint32_t nParamTypes __unused,TEE_Param pParams[4]__unused,void ** ppSessionContext __unused)27 TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes __unused,
28 				    TEE_Param pParams[4] __unused,
29 				    void **ppSessionContext __unused)
30 {
31 	/* check the plugin was loaded */
32 	return tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_PING, 0, NULL, 0,
33 				      NULL);
34 }
35 
TA_CloseSessionEntryPoint(void * pSessionContext __unused)36 void TA_CloseSessionEntryPoint(void *pSessionContext __unused)
37 {
38 }
39 
pass_values(uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])40 static TEE_Result pass_values(uint32_t param_types,
41 			     TEE_Param params[TEE_NUM_PARAMS])
42 {
43 	uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
44 					  TEE_PARAM_TYPE_VALUE_INPUT,
45 					  TEE_PARAM_TYPE_NONE,
46 					  TEE_PARAM_TYPE_NONE);
47 	TEE_Result res = TEE_ERROR_GENERIC;
48 	size_t outlen = 0;
49 	struct add_op {
50 		uint32_t a;
51 		uint32_t b;
52 	} values = { 0 };
53 
54 	if (exp_pt != param_types)
55 		return TEE_ERROR_BAD_PARAMETERS;
56 
57 	values.a = params[0].value.a;
58 	values.b = params[0].value.b;
59 
60 	res = tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_PASS_VALUES,
61 				     params[1].value.a, (void *)&values,
62 				     sizeof(struct add_op), &outlen);
63 	params[0].value.a = values.a;
64 
65 	return res;
66 }
67 
write_array(uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])68 static TEE_Result write_array(uint32_t param_types,
69 			      TEE_Param params[TEE_NUM_PARAMS])
70 {
71 	uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
72 					  TEE_PARAM_TYPE_VALUE_OUTPUT,
73 					  TEE_PARAM_TYPE_NONE,
74 					  TEE_PARAM_TYPE_NONE);
75 	TEE_Result res = TEE_ERROR_GENERIC;
76 	size_t outlen = 0;
77 	uint8_t *d = params[0].memref.buffer;
78 
79 	if (exp_pt != param_types)
80 		return TEE_ERROR_BAD_PARAMETERS;
81 
82 	res = tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_WRITE_ARR, 0,
83 				     params[0].memref.buffer,
84 				     params[0].memref.size, &outlen);
85 	params[1].value.a = d[0];
86 
87 	return res;
88 }
89 
get_array(uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])90 static TEE_Result get_array(uint32_t param_types,
91 			    TEE_Param params[TEE_NUM_PARAMS])
92 {
93 	uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_OUTPUT,
94 					  TEE_PARAM_TYPE_VALUE_OUTPUT,
95 					  TEE_PARAM_TYPE_NONE,
96 					  TEE_PARAM_TYPE_NONE);
97 	TEE_Result res = TEE_ERROR_GENERIC;
98 	size_t outlen = 0;
99 
100 	if (exp_pt != param_types)
101 		return TEE_ERROR_BAD_PARAMETERS;
102 
103 	res = tee_invoke_supp_plugin(&uuid, TEST_PLUGIN_CMD_GET_ARR, 0,
104 				     params[0].memref.buffer,
105 				     params[0].memref.size,
106 				     &outlen);
107 	params[1].value.a = (uint32_t)outlen;
108 
109 	return res;
110 }
111 
bad_input_uuid(uint32_t param_types __unused,TEE_Param params[TEE_NUM_PARAMS]__unused)112 static TEE_Result bad_input_uuid(uint32_t param_types __unused,
113 				 TEE_Param params[TEE_NUM_PARAMS] __unused)
114 {
115 	char data[32] = {  };
116 	size_t inlen = sizeof(data);
117 
118 	return tee_invoke_supp_plugin(NULL, 0, 0, data, inlen, NULL);
119 }
120 
bad_input_data(uint32_t param_types __unused,TEE_Param params[TEE_NUM_PARAMS]__unused)121 static TEE_Result bad_input_data(uint32_t param_types __unused,
122 				 TEE_Param params[TEE_NUM_PARAMS] __unused)
123 {
124 	return tee_invoke_supp_plugin(&uuid, 0, 0, NULL, 10, NULL);
125 }
126 
bad_input_inlen(uint32_t param_types __unused,TEE_Param params[TEE_NUM_PARAMS]__unused)127 static TEE_Result bad_input_inlen(uint32_t param_types __unused,
128 				  TEE_Param params[TEE_NUM_PARAMS] __unused)
129 {
130 	char data[32] = {  };
131 
132 	return tee_invoke_supp_plugin(&uuid, 0, 0, data, 0, NULL);
133 }
134 
call_unknown_plugin(uint32_t param_types __unused,TEE_Param params[TEE_NUM_PARAMS]__unused)135 static TEE_Result call_unknown_plugin(uint32_t param_types __unused,
136 				      TEE_Param params[TEE_NUM_PARAMS]
137 				      __unused)
138 {
139 	const TEE_UUID nulluuid = {  };
140 	char data[32] = {  };
141 	size_t inlen = sizeof(data);
142 
143 	return tee_invoke_supp_plugin(&nulluuid, 0, 0, data, inlen, NULL);
144 }
145 
TA_InvokeCommandEntryPoint(void * pSessionContext __unused,uint32_t nCommandID,uint32_t nParamTypes,TEE_Param pParams[4])146 TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext __unused,
147 				      uint32_t nCommandID,
148 				      uint32_t nParamTypes,
149 				      TEE_Param pParams[4])
150 {
151 	switch (nCommandID) {
152 	case TA_SUPP_PLUGIN_CMD_PASS_VALUES:
153 		return pass_values(nParamTypes, pParams);
154 	case TA_SUPP_PLUGIN_CMD_WRITE_ARR:
155 		return write_array(nParamTypes, pParams);
156 	case TA_SUPP_PLUGIN_CMD_GET_ARR:
157 		return get_array(nParamTypes, pParams);
158 	case TA_SUPP_PLUGIN_CMD_BAD_UUID:
159 		return bad_input_uuid(nParamTypes, pParams);
160 	case TA_SUPP_PLUGIN_CMD_BAD_IN_DATA:
161 		return bad_input_data(nParamTypes, pParams);
162 	case TA_SUPP_PLUGIN_CMD_BAD_IN_LEN:
163 		return bad_input_inlen(nParamTypes, pParams);
164 	case TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID:
165 		return call_unknown_plugin(nParamTypes, pParams);
166 	default:
167 		return TEE_ERROR_NOT_SUPPORTED;
168 	}
169 }
170