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 <pthread.h>
22 #include "StEseApi.h"
23 #include "SpiLayerComm.h"
24 #include <cutils/properties.h>
25 #include <ese_config.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.20";
35 
36 pthread_mutex_t mutex;
37 
38 /******************************************************************************
39  * Function         StEseLog_InitializeLogLevel
40  *
41  * Description      This function is called during StEse_init to initialize
42  *                  debug log level.
43  *
44  * Returns          None
45  *
46  ******************************************************************************/
47 
StEseLog_InitializeLogLevel()48 void StEseLog_InitializeLogLevel() { InitializeSTLogLevel(); }
49 
50 /******************************************************************************
51  * Function         StEse_init
52  *
53  * Description      This function is called by Jni during the
54  *                  initialization of the ESE. It opens the physical connection
55  *                  with ESE and creates required client thread for
56  *                  operation.
57  * Returns          This function return ESESTATUS_SUCCES (0) in case of success
58  *                  In case of failure returns other failure value.
59  *
60  ******************************************************************************/
StEse_init()61 ESESTATUS StEse_init() {
62   SpiDriver_config_t tSpiDriver;
63   ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
64   int ret;
65 
66   char ese_dev_node[64];
67   std::string ese_node;
68 
69   STLOG_HAL_D("%s : SteSE_open Enter halVersion = %s ", __func__, halVersion);
70   /*When spi channel is already opened return status as FAILED*/
71   if (ese_ctxt.EseLibStatus != ESE_STATUS_CLOSE) {
72     STLOG_HAL_D("already opened\n");
73     return ESESTATUS_BUSY;
74   }
75 
76   memset(&ese_ctxt, 0x00, sizeof(ese_ctxt));
77   memset(&tSpiDriver, 0x00, sizeof(tSpiDriver));
78 
79   /* initialize trace level */
80   StEseLog_InitializeLogLevel();
81 
82   /*Read device node path*/
83   ese_node = EseConfig::getString(NAME_ST_ESE_DEV_NODE, "/dev/st54j");
84   strcpy(ese_dev_node, ese_node.c_str());
85   tSpiDriver.pDevName = ese_dev_node;
86 
87   /* Initialize SPI Driver layer */
88   if (T1protocol_init(&tSpiDriver) != ESESTATUS_SUCCESS) {
89     STLOG_HAL_E("T1protocol_init Failed");
90     goto clean_and_return;
91   }
92   /* Copying device handle to ESE Lib context*/
93   ese_ctxt.pDevHandle = tSpiDriver.pDevHandle;
94 
95   ret = pthread_mutex_init(&mutex, NULL);
96   if (ret != 0) {
97     STLOG_HAL_E("HAL: %s pthread_mutex_init failed", __func__);
98   }
99 
100   STLOG_HAL_D("wConfigStatus %x", wConfigStatus);
101   ese_ctxt.EseLibStatus = ESE_STATUS_OPEN;
102   return wConfigStatus;
103 
104 clean_and_return:
105   if (NULL != ese_ctxt.pDevHandle) {
106     SpiLayerInterface_close(ese_ctxt.pDevHandle);
107     memset(&ese_ctxt, 0x00, sizeof(ese_ctxt));
108   }
109   ese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
110   return ESESTATUS_FAILED;
111 }
112 
113 /******************************************************************************
114  * Function         StEseApi_isOpen
115  *
116  * \brief           Check if the hal is opened
117  *
118  * \retval return false if it is close, otherwise true.
119  *
120  ******************************************************************************/
StEseApi_isOpen()121 bool StEseApi_isOpen() {
122   STLOG_HAL_D(" %s  status 0x%x \n", __FUNCTION__, ese_ctxt.EseLibStatus);
123   return ese_ctxt.EseLibStatus != ESE_STATUS_CLOSE;
124 }
125 
126 /******************************************************************************
127  * Function         StEse_Transceive
128  *
129  * Description      This function update the len and provided buffer
130  *
131  * Returns          On Success ESESTATUS_SUCCESS else proper error code
132  *
133  ******************************************************************************/
StEse_Transceive(StEse_data * pCmd,StEse_data * pRsp)134 ESESTATUS StEse_Transceive(StEse_data* pCmd, StEse_data* pRsp) {
135   ESESTATUS status = ESESTATUS_SUCCESS;
136   static int pTxBlock_len = 0;
137   uint16_t pCmdlen = pCmd->len;
138 
139   STLOG_HAL_D("%s : Enter EseLibStatus = %d ", __func__, ese_ctxt.EseLibStatus);
140 
141   if ((NULL == pCmd) || (NULL == pRsp)) return ESESTATUS_INVALID_PARAMETER;
142 
143   if ((pCmd->len == 0) || pCmd->p_data == NULL) {
144     STLOG_HAL_E(" StEse_Transceive - Invalid Parameter no data\n");
145     return ESESTATUS_INVALID_PARAMETER;
146   } else if ((ESE_STATUS_CLOSE == ese_ctxt.EseLibStatus)) {
147     STLOG_HAL_E(" %s ESE Not Initialized \n", __FUNCTION__);
148     return ESESTATUS_NOT_INITIALISED;
149   }
150 
151   STLOG_HAL_D(" %s ESE - No access, waiting \n", __FUNCTION__);
152   pthread_mutex_lock(&mutex);
153 
154   STLOG_HAL_D(" %s ESE - Access granted, processing \n", __FUNCTION__);
155 
156   uint8_t* CmdPart = pCmd->p_data;
157 
158   while (pCmdlen > ATP.ifsc) {
159     pTxBlock_len = ATP.ifsc;
160 
161     int rc = T1protocol_transcieveApduPart(CmdPart, pTxBlock_len, false,
162                                            (StEse_data*) pRsp);
163     if (rc < 0) {
164       STLOG_HAL_E(" %s ESE - Error, release access \n", __FUNCTION__);
165       status = ESESTATUS_FAILED;
166 
167       pthread_mutex_unlock(&mutex);
168 
169       return status;
170     }
171     pCmdlen -= pTxBlock_len;
172     CmdPart = CmdPart + pTxBlock_len;
173     if (ESESTATUS_SUCCESS != status) {
174       STLOG_HAL_E(" %s T1protocol_transcieveApduPart- Failed \n", __FUNCTION__);
175     }
176   }
177   int rc = T1protocol_transcieveApduPart(CmdPart, pCmdlen, true,
178                                          (StEse_data*) pRsp);
179   if (rc < 0) status = ESESTATUS_FAILED;
180 
181   if (ESESTATUS_SUCCESS != status) {
182     STLOG_HAL_E(" %s T1protocol_transcieveApduPart- Failed \n", __FUNCTION__);
183   }
184 
185   STLOG_HAL_D(" %s ESE - Processing complete, release access \n", __FUNCTION__);
186 
187   pthread_mutex_unlock(&mutex);
188 
189   STLOG_HAL_D(" %s Exit status 0x%x \n", __FUNCTION__, status);
190 
191   return status;
192 }
193 
194 /******************************************************************************
195  * Function         StEse_close
196  *
197  * Description      This function close the ESE interface and free all
198  *                  resources.
199  *
200  * Returns          Always return ESESTATUS_SUCCESS (0).
201  *
202  ******************************************************************************/
StEse_close(void)203 ESESTATUS StEse_close(void) {
204   ESESTATUS status = ESESTATUS_SUCCESS;
205 
206   if ((ESE_STATUS_CLOSE == ese_ctxt.EseLibStatus)) {
207     STLOG_HAL_E(" %s ESE Not Initialized \n", __FUNCTION__);
208     return ESESTATUS_NOT_INITIALISED;
209   }
210 
211   if (NULL != ese_ctxt.pDevHandle) {
212     SpiLayerInterface_close(ese_ctxt.pDevHandle);
213     memset(&ese_ctxt, 0x00, sizeof(ese_ctxt));
214     STLOG_HAL_D("StEse_close - ESE Context deinit completed");
215     ese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
216   }
217 
218   pthread_mutex_destroy(&mutex);
219   /* Return success always */
220   return status;
221 }
222 
223 /******************************************************************************
224  * Function         StEse_getAtr
225  *
226  * Description      This function get the last ATR received.
227  *
228  * Returns          pointer to the ATR array.
229  *
230  ******************************************************************************/
StEse_getAtr(void)231 uint8_t* StEse_getAtr(void) {
232   STLOG_HAL_D("%s : Enter", __func__);
233   // The ATR is not supported by SPI in the secure element
234   return nullptr;
235 }
236 
237 /******************************************************************************
238  * Function         StEse_Reset
239  *
240  * Description      This function resets the eSE SPI interface by sending a
241  *                  SE reset and negotiating the ifs.
242  *
243  * Returns          ESESTATUS_SUCCESS is successful, ESESTATUS_SUCCESS otherwise
244  *
245  ******************************************************************************/
StEse_Reset(void)246 ESESTATUS StEse_Reset(void) {
247   STLOG_HAL_D("%s : Enter", __func__);
248   if (SpiLayerInterface_setup() != 0) {
249     return ESESTATUS_FAILED;
250   }
251 
252   return ESESTATUS_SUCCESS;
253 }
254