1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2021, Open Mobile Platform LLC
4  */
5 
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stddef.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <limits.h>
12 #include <tee_plugin_method.h>
13 #include <test_supp_plugin.h>
14 #include <unistd.h>
15 
pass_values(unsigned int sub_cmd,void * data,size_t data_len,size_t * out_len)16 static TEEC_Result pass_values(unsigned int sub_cmd, void *data,
17 			      size_t data_len, size_t *out_len)
18 {
19 	struct add_op {
20 		uint32_t a;
21 		uint32_t b;
22 	} *values = NULL;
23 
24 	values = (struct add_op *)data;
25 	*out_len = sizeof(uint32_t);
26 
27 	if (sub_cmd == '+')
28 		values->a = values->a + values->b;
29 	else
30 		values->a = values->a - values->b;
31 
32 	return TEEC_SUCCESS;
33 }
34 
proc_input_arr(unsigned int sub_cmd,void * data,size_t data_len,size_t * out_len)35 static TEEC_Result proc_input_arr(unsigned int sub_cmd, void *data,
36 				  size_t data_len, size_t *out_len)
37 {
38 	(void)sub_cmd;
39 
40 	size_t i = 0;
41 	uint8_t sum = 0;
42 	uint8_t *d = data;
43 
44 	for (; i < data_len; ++i)
45 		sum += d[i];
46 
47 	d[0] = sum;
48 	*out_len = sizeof(sum);
49 
50 	return TEEC_SUCCESS;
51 }
52 
get_test_arr(unsigned int sub_cmd,void * data,size_t data_len,size_t * out_len)53 static TEEC_Result get_test_arr(unsigned int sub_cmd, void *data,
54 				size_t data_len, size_t *out_len)
55 {
56 	(void)sub_cmd;
57 	char test_arr[] = "Array from plugin";
58 	size_t test_size = sizeof(test_arr);
59 
60 	if (data_len < test_size)
61 		return TEEC_ERROR_GENERIC;
62 
63 	memcpy(data, test_arr, test_size);
64 	*out_len = test_size;
65 
66 	return TEEC_SUCCESS;
67 }
68 
test_plugin_invoke(unsigned int cmd,unsigned int sub_cmd,void * data,size_t data_len,size_t * out_len)69 static TEEC_Result test_plugin_invoke(unsigned int cmd, unsigned int sub_cmd,
70 				      void *data, size_t data_len,
71 				      size_t *out_len)
72 {
73 	switch (cmd) {
74 	case TEST_PLUGIN_CMD_PING:
75 		return TEEC_SUCCESS;
76 	case TEST_PLUGIN_CMD_PASS_VALUES:
77 		return pass_values(sub_cmd, data, data_len, out_len);
78 	case TEST_PLUGIN_CMD_WRITE_ARR:
79 		return proc_input_arr(sub_cmd, data, data_len, out_len);
80 	case TEST_PLUGIN_CMD_GET_ARR:
81 		return get_test_arr(sub_cmd, data, data_len, out_len);
82 	default:
83 		break;
84 	}
85 
86 	return TEEC_ERROR_NOT_SUPPORTED;
87 }
88 
89 struct plugin_method plugin_method = {
90 	.name = "test",
91 	.uuid = TEST_PLUGIN_UUID,
92 	.invoke = test_plugin_invoke,
93 };
94