1 /******************************************************************************
2 *
3 * Copyright (C) 2018 ST Microelectronics S.A.
4 * Copyright 2018 NXP
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19 #define LOG_TAG "StEse_HalApi"
20
21 #include "StEseApi.h"
22 #include <cutils/properties.h>
23 #include <ese_config.h>
24 #include <pthread.h>
25 #include "SpiLayerComm.h"
26 #include "T1protocol.h"
27 #include "android_logmsg.h"
28
29 /*********************** Global Variables *************************************/
30
31 /* ESE Context structure */
32 ese_Context_t ese_ctxt;
33
34 const char* halVersion = "ST54-SE HAL1.0 Version 1.0.22";
35
36 pthread_mutex_t mutex;
37
38 bool ConfRead = 0;
39 unsigned int PollInt_confvalue = 1000;
40 unsigned int BGT_confvalue = 1000;
41
42 /******************************************************************************
43 * Function StEseLog_InitializeLogLevel
44 *
45 * Description This function is called during StEse_init to initialize
46 * debug log level.
47 *
48 * Returns None
49 *
50 ******************************************************************************/
51
StEseLog_InitializeLogLevel()52 void StEseLog_InitializeLogLevel() { InitializeSTLogLevel(); }
53
54 /******************************************************************************
55 * Function StEse_init
56 *
57 * Description This function is called by Jni during the
58 * initialization of the ESE. It opens the physical connection
59 * with ESE and creates required client thread for
60 * operation.
61 * Returns This function return ESESTATUS_SUCCES (0) in case of success
62 * In case of failure returns other failure value.
63 *
64 ******************************************************************************/
StEse_init()65 ESESTATUS StEse_init() {
66 SpiDriver_config_t tSpiDriver;
67 ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
68 int ret;
69
70 char ese_dev_node[64];
71 std::string ese_node;
72
73 STLOG_HAL_D("%s : SteSE_open Enter halVersion = %s ", __func__, halVersion);
74 /*When spi channel is already opened return status as FAILED*/
75 if (ese_ctxt.EseLibStatus != ESE_STATUS_CLOSE) {
76 STLOG_HAL_D("already opened\n");
77 return ESESTATUS_BUSY;
78 }
79
80 memset(&ese_ctxt, 0x00, sizeof(ese_ctxt));
81 memset(&tSpiDriver, 0x00, sizeof(tSpiDriver));
82
83 /* initialize trace level */
84 StEseLog_InitializeLogLevel();
85
86 /*Read device node path*/
87 ese_node = EseConfig::getString(NAME_ST_ESE_DEV_NODE, "/dev/st54j");
88 strcpy(ese_dev_node, ese_node.c_str());
89 tSpiDriver.pDevName = ese_dev_node;
90
91 if (!ConfRead) {
92 PollInt_confvalue =
93 EseConfig::getUnsigned(NAME_ST_ESE_DEV_POLLING_INTERVAL, 1000);
94 BGT_confvalue = EseConfig::getUnsigned(NAME_ST_ESE_DEV_BGT, 1000);
95 ConfRead = true;
96 }
97
98 tSpiDriver.polling_interval = PollInt_confvalue;
99 tSpiDriver.bgt = BGT_confvalue;
100
101 /* Initialize SPI Driver layer */
102 if (T1protocol_init(&tSpiDriver) != ESESTATUS_SUCCESS) {
103 STLOG_HAL_E("T1protocol_init Failed");
104 goto clean_and_return;
105 }
106 /* Copying device handle to ESE Lib context*/
107 ese_ctxt.pDevHandle = tSpiDriver.pDevHandle;
108
109 ret = pthread_mutex_init(&mutex, NULL);
110 if (ret != 0) {
111 STLOG_HAL_E("HAL: %s pthread_mutex_init failed", __func__);
112 }
113
114 STLOG_HAL_D("wConfigStatus %x", wConfigStatus);
115 ese_ctxt.EseLibStatus = ESE_STATUS_OPEN;
116 return wConfigStatus;
117
118 clean_and_return:
119 if (NULL != ese_ctxt.pDevHandle) {
120 SpiLayerInterface_close(ese_ctxt.pDevHandle);
121 memset(&ese_ctxt, 0x00, sizeof(ese_ctxt));
122 }
123 ese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
124 return ESESTATUS_FAILED;
125 }
126
127 /******************************************************************************
128 * Function StEseApi_isOpen
129 *
130 * \brief Check if the hal is opened
131 *
132 * \retval return false if it is close, otherwise true.
133 *
134 ******************************************************************************/
StEseApi_isOpen()135 bool StEseApi_isOpen() {
136 STLOG_HAL_D(" %s status 0x%x \n", __FUNCTION__, ese_ctxt.EseLibStatus);
137 return ese_ctxt.EseLibStatus != ESE_STATUS_CLOSE;
138 }
139
140 /******************************************************************************
141 * Function StEse_Transceive
142 *
143 * Description This function update the len and provided buffer
144 *
145 * Returns On Success ESESTATUS_SUCCESS else proper error code
146 *
147 ******************************************************************************/
StEse_Transceive(StEse_data * pCmd,StEse_data * pRsp)148 ESESTATUS StEse_Transceive(StEse_data* pCmd, StEse_data* pRsp) {
149 ESESTATUS status = ESESTATUS_SUCCESS;
150 static int pTxBlock_len = 0;
151 int retry_count = 0;
152 uint16_t pCmdlen = pCmd->len;
153
154 STLOG_HAL_D("%s : Enter EseLibStatus = %d ", __func__, ese_ctxt.EseLibStatus);
155
156 if ((NULL == pCmd) || (NULL == pRsp)) return ESESTATUS_INVALID_PARAMETER;
157
158 if ((pCmd->len == 0) || pCmd->p_data == NULL) {
159 STLOG_HAL_E(" StEse_Transceive - Invalid Parameter no data\n");
160 return ESESTATUS_INVALID_PARAMETER;
161 } else if ((ESE_STATUS_CLOSE == ese_ctxt.EseLibStatus)) {
162 STLOG_HAL_E(" %s ESE Not Initialized \n", __FUNCTION__);
163 return ESESTATUS_NOT_INITIALISED;
164 }
165
166 STLOG_HAL_D(" %s ESE - No access, waiting \n", __FUNCTION__);
167 pthread_mutex_lock(&mutex);
168
169 STLOG_HAL_D(" %s ESE - Access granted, processing \n", __FUNCTION__);
170
171 uint8_t* CmdPart = pCmd->p_data;
172
173 retry:
174 while (pCmdlen > ATP.ifsc) {
175 pTxBlock_len = ATP.ifsc;
176
177 int rc = T1protocol_transcieveApduPart(CmdPart, pTxBlock_len, false,
178 (StEse_data*)pRsp, I_block);
179
180 if ((rc == -2) && (retry_count < 3)) {
181 retry_count++;
182 STLOG_HAL_E(" %s ESE - resync was needed, resend the whole frame\n",
183 __FUNCTION__);
184 pCmdlen = pCmd->len;
185 CmdPart = pCmd->p_data;
186 goto retry;
187 } else if (rc < 0) {
188 STLOG_HAL_E(" %s ESE - Error, release access \n", __FUNCTION__);
189 status = ESESTATUS_FAILED;
190 retry_count = 0;
191 pthread_mutex_unlock(&mutex);
192
193 return status;
194 } else {
195 retry_count = 0;
196 }
197
198 pCmdlen -= pTxBlock_len;
199 CmdPart = CmdPart + pTxBlock_len;
200 }
201
202 int rc = T1protocol_transcieveApduPart(CmdPart, pCmdlen, true,
203 (StEse_data*)pRsp, I_block);
204 if ((rc == -2) && (retry_count < 3)) {
205 retry_count++;
206 STLOG_HAL_E(" %s ESE - resync was needed, resend\n", __FUNCTION__);
207 pCmdlen = pCmd->len;
208 CmdPart = pCmd->p_data;
209 goto retry;
210 } else if (rc < 0)
211 status = ESESTATUS_FAILED;
212
213 if (ESESTATUS_SUCCESS != status) {
214 STLOG_HAL_E(" %s T1protocol_transcieveApduPart- Failed \n", __FUNCTION__);
215 }
216 retry_count = 0;
217 STLOG_HAL_D(" %s ESE - Processing complete, release access \n", __FUNCTION__);
218
219 pthread_mutex_unlock(&mutex);
220
221 STLOG_HAL_D(" %s Exit status 0x%x \n", __FUNCTION__, status);
222
223 return status;
224 }
225
226 /******************************************************************************
227 * Function StEse_close
228 *
229 * Description This function close the ESE interface and free all
230 * resources.
231 *
232 * Returns Always return ESESTATUS_SUCCESS (0).
233 *
234 ******************************************************************************/
StEse_close(void)235 ESESTATUS StEse_close(void) {
236 ESESTATUS status = ESESTATUS_SUCCESS;
237
238 if ((ESE_STATUS_CLOSE == ese_ctxt.EseLibStatus)) {
239 STLOG_HAL_E(" %s ESE Not Initialized \n", __FUNCTION__);
240 return ESESTATUS_NOT_INITIALISED;
241 }
242
243 if (NULL != ese_ctxt.pDevHandle) {
244 SpiLayerInterface_close(ese_ctxt.pDevHandle);
245 memset(&ese_ctxt, 0x00, sizeof(ese_ctxt));
246 STLOG_HAL_D("StEse_close - ESE Context deinit completed");
247 ese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
248 }
249
250 pthread_mutex_destroy(&mutex);
251 /* Return success always */
252 return status;
253 }
254
255 /******************************************************************************
256 * Function StEse_getAtr
257 *
258 * Description This function get the last ATR received.
259 *
260 * Returns pointer to the ATR array.
261 *
262 ******************************************************************************/
StEse_getAtr(void)263 uint8_t* StEse_getAtr(void) {
264 STLOG_HAL_D("%s : Enter", __func__);
265 // The ATR is not supported by SPI in the secure element
266 return nullptr;
267 }
268
269 /******************************************************************************
270 * Function StEse_Reset
271 *
272 * Description This function resets the eSE SPI interface by sending a
273 * SE reset and negotiating the ifs.
274 *
275 * Returns ESESTATUS_SUCCESS is successful, ESESTATUS_SUCCESS otherwise
276 *
277 ******************************************************************************/
StEse_Reset(void)278 ESESTATUS StEse_Reset(void) {
279 STLOG_HAL_D("%s : Enter", __func__);
280 if (SpiLayerInterface_setup() != 0) {
281 return ESESTATUS_FAILED;
282 }
283
284 return ESESTATUS_SUCCESS;
285 }
286