1 /*
2  * Copyright (c) 2016, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <tee_internal_api.h>
29 #include <tee_internal_api_extensions.h>
30 
31 #include <hello_world_ta.h>
32 
33 /*
34  * Called when the instance of the TA is created. This is the first call in
35  * the TA.
36  */
TA_CreateEntryPoint(void)37 TEE_Result TA_CreateEntryPoint(void)
38 {
39 	DMSG("has been called");
40 
41 	return TEE_SUCCESS;
42 }
43 
44 /*
45  * Called when the instance of the TA is destroyed if the TA has not
46  * crashed or panicked. This is the last call in the TA.
47  */
TA_DestroyEntryPoint(void)48 void TA_DestroyEntryPoint(void)
49 {
50 	DMSG("has been called");
51 }
52 
53 /*
54  * Called when a new session is opened to the TA. *sess_ctx can be updated
55  * with a value to be able to identify this session in subsequent calls to the
56  * TA. In this function you will normally do the global initialization for the
57  * TA.
58  */
TA_OpenSessionEntryPoint(uint32_t param_types,TEE_Param __maybe_unused params[4],void __maybe_unused ** sess_ctx)59 TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
60 		TEE_Param __maybe_unused params[4],
61 		void __maybe_unused **sess_ctx)
62 {
63 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
64 						   TEE_PARAM_TYPE_NONE,
65 						   TEE_PARAM_TYPE_NONE,
66 						   TEE_PARAM_TYPE_NONE);
67 
68 	DMSG("has been called");
69 
70 	if (param_types != exp_param_types)
71 		return TEE_ERROR_BAD_PARAMETERS;
72 
73 	/* Unused parameters */
74 	(void)&params;
75 	(void)&sess_ctx;
76 
77 	/*
78 	 * The DMSG() macro is non-standard, TEE Internal API doesn't
79 	 * specify any means to logging from a TA.
80 	 */
81 	IMSG("Hello World!\n");
82 
83 	/* If return value != TEE_SUCCESS the session will not be created. */
84 	return TEE_SUCCESS;
85 }
86 
87 /*
88  * Called when a session is closed, sess_ctx hold the value that was
89  * assigned by TA_OpenSessionEntryPoint().
90  */
TA_CloseSessionEntryPoint(void __maybe_unused * sess_ctx)91 void TA_CloseSessionEntryPoint(void __maybe_unused *sess_ctx)
92 {
93 	(void)&sess_ctx; /* Unused parameter */
94 	IMSG("Goodbye!\n");
95 }
96 
inc_value(uint32_t param_types,TEE_Param params[4])97 static TEE_Result inc_value(uint32_t param_types,
98 	TEE_Param params[4])
99 {
100 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
101 						   TEE_PARAM_TYPE_NONE,
102 						   TEE_PARAM_TYPE_NONE,
103 						   TEE_PARAM_TYPE_NONE);
104 
105 	DMSG("has been called");
106 
107 	if (param_types != exp_param_types)
108 		return TEE_ERROR_BAD_PARAMETERS;
109 
110 	IMSG("Got value: %u from NW", params[0].value.a);
111 	params[0].value.a++;
112 	IMSG("Increase value to: %u", params[0].value.a);
113 
114 	return TEE_SUCCESS;
115 }
116 
dec_value(uint32_t param_types,TEE_Param params[4])117 static TEE_Result dec_value(uint32_t param_types,
118 	TEE_Param params[4])
119 {
120 	uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
121 						   TEE_PARAM_TYPE_NONE,
122 						   TEE_PARAM_TYPE_NONE,
123 						   TEE_PARAM_TYPE_NONE);
124 
125 	DMSG("has been called");
126 
127 	if (param_types != exp_param_types)
128 		return TEE_ERROR_BAD_PARAMETERS;
129 
130 	IMSG("Got value: %u from NW", params[0].value.a);
131 	params[0].value.a--;
132 	IMSG("Decrease value to: %u", params[0].value.a);
133 
134 	return TEE_SUCCESS;
135 }
136 /*
137  * Called when a TA is invoked. sess_ctx hold that value that was
138  * assigned by TA_OpenSessionEntryPoint(). The rest of the paramters
139  * comes from normal world.
140  */
TA_InvokeCommandEntryPoint(void __maybe_unused * sess_ctx,uint32_t cmd_id,uint32_t param_types,TEE_Param params[4])141 TEE_Result TA_InvokeCommandEntryPoint(void __maybe_unused *sess_ctx,
142 			uint32_t cmd_id,
143 			uint32_t param_types, TEE_Param params[4])
144 {
145 	(void)&sess_ctx; /* Unused parameter */
146 
147 	switch (cmd_id) {
148 	case TA_HELLO_WORLD_CMD_INC_VALUE:
149 		return inc_value(param_types, params);
150 	case TA_HELLO_WORLD_CMD_DEC_VALUE:
151 		return dec_value(param_types, params);
152 	default:
153 		return TEE_ERROR_BAD_PARAMETERS;
154 	}
155 }
156