1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2021, Linaro Limited
4  * All rights reserved.
5  */
6 
7 #include <inttypes.h>
8 #include <string.h>
9 #include <ta_arm_bti.h>
10 #include <ta_arm_bti_priv.h>
11 #include <tee_internal_api.h>
12 
13 /*
14  * Trusted Application Entry Points
15  */
16 
17 /* Called each time a new instance is created */
TA_CreateEntryPoint(void)18 TEE_Result TA_CreateEntryPoint(void)
19 {
20 	return TEE_SUCCESS;
21 }
22 
23 /* Called each time an instance is destroyed */
TA_DestroyEntryPoint(void)24 void TA_DestroyEntryPoint(void)
25 {
26 }
27 
28 /* Called each time a session is opened */
TA_OpenSessionEntryPoint(uint32_t nParamTypes __unused,TEE_Param pParams[4]__unused,void ** ppSessionContext __unused)29 TEE_Result TA_OpenSessionEntryPoint(uint32_t nParamTypes __unused,
30 				    TEE_Param pParams[4] __unused,
31 				    void **ppSessionContext __unused)
32 {
33 	return TEE_SUCCESS;
34 }
35 
36 /* Called each time a session is closed */
TA_CloseSessionEntryPoint(void * pSessionContext __unused)37 void TA_CloseSessionEntryPoint(void *pSessionContext __unused)
38 {
39 }
40 
check_bti_implemented(uint32_t param_types,TEE_Param params[4])41 static TEE_Result check_bti_implemented(uint32_t param_types,
42 					TEE_Param params[4])
43 {
44 	bool implemented = false;
45 	TEE_Result res = TEE_ERROR_GENERIC;
46 
47 	if (param_types !=
48 	    TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
49 			    TEE_PARAM_TYPE_NONE,
50 			    TEE_PARAM_TYPE_NONE,
51 			    TEE_PARAM_TYPE_NONE)) {
52 		return TEE_ERROR_BAD_PARAMETERS;
53 	}
54 
55 	res = TEE_GetPropertyAsBool(
56 			TEE_PROPSET_TEE_IMPLEMENTATION,
57 			"org.trustedfirmware.optee.cpu.feat_bti_implemented",
58 			&implemented);
59 	if (res == TEE_SUCCESS && implemented)
60 		params[0].value.a = 1;
61 
62 	if (res == TEE_ERROR_ITEM_NOT_FOUND) {
63 		params[0].value.a = 0;
64 		res = TEE_SUCCESS;
65 	}
66 
67 	return res;
68 }
69 
test_bti(uint32_t nCommandID __unused,uint32_t nParamTypes __unused,TEE_Param pParams[4]__unused)70 __weak TEE_Result test_bti(uint32_t nCommandID __unused,
71 			   uint32_t nParamTypes __unused,
72 			   TEE_Param pParams[4] __unused)
73 {
74 	return TEE_ERROR_NOT_IMPLEMENTED;
75 }
76 
77 /* Called when a command is invoked */
TA_InvokeCommandEntryPoint(void * pSessionContext __unused,uint32_t nCommandID,uint32_t nParamTypes,TEE_Param pParams[4])78 TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext __unused,
79 				      uint32_t nCommandID,
80 				      uint32_t nParamTypes,
81 				      TEE_Param pParams[4] )
82 {
83 	TEE_Result res = TEE_SUCCESS;
84 
85 	switch (nCommandID) {
86 	case TA_TEST_USING_BLR :
87 	case TA_TEST_USING_BR :
88 	case TA_TEST_USING_BR_X16 :
89 		res = test_bti(nCommandID, nParamTypes, pParams);
90 		break;
91 	case TA_FEAT_BTI_IMPLEMENTED :
92 		res = check_bti_implemented(nParamTypes, pParams);
93 		break;
94 	default:
95 		return TEE_ERROR_BAD_PARAMETERS;
96 	}
97 
98 	return res;
99 }
100