1 /******************************************************************************
2 *
3 * Copyright 2018-2021 NXP
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 #define LOG_TAG "NxpEseHal"
19 #include <log/log.h>
20
21 #include <EseTransport.h>
22 #include <cutils/properties.h>
23 #include <ese_config.h>
24 #include <phNxpEseFeatures.h>
25 #include <phNxpEsePal.h>
26 #include <phNxpEseProto7816_3.h>
27 #include <phNxpEse_Internal.h>
28
29 #define RECEIVE_PACKET_SOF 0xA5
30 #define CHAINED_PACKET_WITHSEQN 0x60
31 #define CHAINED_PACKET_WITHOUTSEQN 0x20
32 #define PH_PAL_ESE_PRINT_PACKET_TX(data, len) \
33 ({ phPalEse_print_packet("SEND", data, len); })
34 #define PH_PAL_ESE_PRINT_PACKET_RX(data, len) \
35 ({ phPalEse_print_packet("RECV", data, len); })
36 /* 32K(0x8000) Datasize + 10(0xA) Byte Max Header Size + 1 byte negative
37 * testcase support */
38 #define MAX_SUPPORTED_DATA_SIZE 0x800B
39 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
40 int nNbBytesToRead);
41 static int phNxpEse_readPacket_legacy(void* pDevHandle, uint8_t* pBuffer,
42 int nNbBytesToRead);
43
44 static ESESTATUS phNxpEse_checkJcopDwnldState(void);
45 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state);
46 static ESESTATUS phNxpEse_checkFWDwnldStatus(void);
47 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer);
48 static unsigned char* phNxpEse_GgetTimerTlvBuffer(unsigned char* timer_buffer,
49 unsigned int value);
50 static __inline bool phNxpEse_isColdResetRequired(phNxpEse_initMode mode,
51 ESESTATUS status);
52 static int poll_sof_chained_delay = 0;
53 static phNxpEse_OsVersion_t sOsVersion = INVALID_OS_VERSION;
54 /* To Overwrite the value of wtx_counter_limit from config file*/
55 static unsigned long int app_wtx_cnt = RESET_APP_WTX_COUNT;
56
57 /*********************** Global Variables *************************************/
58
59 /* ESE Context structure */
60 phNxpEse_Context_t nxpese_ctxt;
61 bool ese_debug_enabled = false;
62
63 /******************************************************************************
64 * Function phNxpEse_SetEndPoint_Cntxt
65 *
66 * Description This function is called set the SE endpoint
67 *
68 * Returns None
69 *
70 ******************************************************************************/
71
phNxpEse_SetEndPoint_Cntxt(uint8_t uEndPoint)72 ESESTATUS phNxpEse_SetEndPoint_Cntxt(uint8_t uEndPoint) {
73 ESESTATUS status = ESESTATUS_FAILED;
74 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
75 status = phNxpEseProto7816_SetEndPoint(uEndPoint);
76 if (status == ESESTATUS_SUCCESS) {
77 nxpese_ctxt.nadInfo.nadRx = nadInfoRx_ptr[uEndPoint];
78 nxpese_ctxt.nadInfo.nadTx = nadInfoTx_ptr[uEndPoint];
79 nxpese_ctxt.endPointInfo = uEndPoint;
80 }
81 ALOGD_IF(ese_debug_enabled, "%s: Enpoint=%d", __FUNCTION__, uEndPoint);
82 } else {
83 ALOGE("%s- Function not supported", __FUNCTION__);
84 }
85 return status;
86 }
87
88 /******************************************************************************
89 * Function phNxpEse_ResetEndPoint_Cntxt
90 *
91 * Description This function is called to reset the SE endpoint
92 *
93 * Returns None
94 *
95 ******************************************************************************/
phNxpEse_ResetEndPoint_Cntxt(uint8_t uEndPoint)96 ESESTATUS phNxpEse_ResetEndPoint_Cntxt(uint8_t uEndPoint) {
97 ESESTATUS status = ESESTATUS_FAILED;
98 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
99 status = phNxpEseProto7816_ResetEndPoint(uEndPoint);
100 } else {
101 ALOGE("%s- Function not supported", __FUNCTION__);
102 }
103 return status;
104 }
105 /******************************************************************************
106 * Function phNxpLog_InitializeLogLevel
107 *
108 * Description This function is called during phNxpEse_init to initialize
109 * debug log level.
110 *
111 * Returns None
112 *
113 ******************************************************************************/
114
phNxpLog_InitializeLogLevel()115 void phNxpLog_InitializeLogLevel() {
116 ese_debug_enabled =
117 (EseConfig::getUnsigned(NAME_SE_DEBUG_ENABLED, 0) != 0) ? true : false;
118
119 char valueStr[PROPERTY_VALUE_MAX] = {0};
120 int len = property_get("vendor.ese.debug_enabled", valueStr, "");
121 if (len > 0) {
122 // let Android property override .conf variable
123 unsigned debug_enabled = 0;
124 sscanf(valueStr, "%u", &debug_enabled);
125 ese_debug_enabled = (debug_enabled == 0) ? false : true;
126 }
127
128 ALOGD_IF(ese_debug_enabled, "%s: level=%u", __func__, ese_debug_enabled);
129 }
130
131 /******************************************************************************
132 * Function phNxpEse_init
133 *
134 * Description This function is called by Jni/phNxpEse_open during the
135 * initialization of the ESE. It initializes protocol stack
136 *instance variable
137 *
138 * Returns This function return ESESTATUS_SUCCESS (0) in case of
139 *success In case of failure returns other failure value.
140 *
141 ******************************************************************************/
phNxpEse_init(phNxpEse_initParams initParams)142 ESESTATUS phNxpEse_init(phNxpEse_initParams initParams) {
143 ESESTATUS wConfigStatus = ESESTATUS_FAILED;
144 unsigned long int num, ifsd_value = 0;
145 unsigned long maxTimer = 0;
146 uint8_t retry = 0;
147 phNxpEseProto7816InitParam_t protoInitParam;
148 phNxpEse_memset(&protoInitParam, 0x00, sizeof(phNxpEseProto7816InitParam_t));
149 /* STATUS_OPEN */
150 nxpese_ctxt.EseLibStatus = ESE_STATUS_OPEN;
151
152 if (app_wtx_cnt > RESET_APP_WTX_COUNT) {
153 protoInitParam.wtx_counter_limit = app_wtx_cnt;
154 ALOGD_IF(ese_debug_enabled, "Wtx_counter limit from app setting - %lu",
155 protoInitParam.wtx_counter_limit);
156 } else {
157 protoInitParam.wtx_counter_limit = EseConfig::getUnsigned(
158 NAME_NXP_WTX_COUNT_VALUE, PH_PROTO_WTX_DEFAULT_COUNT);
159 ALOGD_IF(ese_debug_enabled, "Wtx_counter read from config file - %lu",
160 protoInitParam.wtx_counter_limit);
161 }
162 if (EseConfig::hasKey(NAME_RNACK_RETRY_DELAY)) {
163 num = EseConfig::getUnsigned(NAME_RNACK_RETRY_DELAY);
164 nxpese_ctxt.invalidFrame_Rnack_Delay = num;
165 ALOGD_IF(ese_debug_enabled, "Rnack retry_delay read from config file - %lu",
166 num);
167 } else {
168 nxpese_ctxt.invalidFrame_Rnack_Delay = 7000;
169 }
170 if (EseConfig::hasKey(NAME_NXP_MAX_RNACK_RETRY)) {
171 protoInitParam.rnack_retry_limit =
172 EseConfig::getUnsigned(NAME_NXP_MAX_RNACK_RETRY);
173 } else {
174 protoInitParam.rnack_retry_limit = MAX_RNACK_RETRY_LIMIT;
175 }
176 if (ESE_MODE_NORMAL ==
177 initParams.initMode) /* TZ/Normal wired mode should come here*/
178 {
179 if (EseConfig::hasKey(NAME_NXP_SPI_INTF_RST_ENABLE)) {
180 protoInitParam.interfaceReset =
181 (EseConfig::getUnsigned(NAME_NXP_SPI_INTF_RST_ENABLE) == 1) ? true
182 : false;
183 } else {
184 protoInitParam.interfaceReset = true;
185 }
186 } else /* OSU mode, no interface reset is required */
187 {
188 if (phNxpEse_doResetProtection(true)) {
189 ALOGE("%s Reset Potection failed. returning...", __FUNCTION__);
190 return ESESTATUS_FAILED;
191 }
192 protoInitParam.interfaceReset = false;
193 }
194 if (EseConfig::hasKey(NAME_NXP_WTX_NTF_COUNT)) {
195 num = EseConfig::getUnsigned(NAME_NXP_WTX_NTF_COUNT);
196 protoInitParam.wtx_ntf_limit = num;
197 ALOGD_IF(ese_debug_enabled, "Wtx_ntf limit from config file - %lu",
198 protoInitParam.wtx_ntf_limit);
199 } else {
200 protoInitParam.wtx_ntf_limit = PH_DEFAULT_WTX_NTF_LIMIT;
201 }
202 nxpese_ctxt.fPtr_WtxNtf = initParams.fPtr_WtxNtf;
203 /* Sharing lib context for fetching secure timer values */
204 protoInitParam.pSecureTimerParams =
205 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams;
206
207 ALOGD_IF(ese_debug_enabled,
208 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
209 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
210 nxpese_ctxt.secureTimerParams.secureTimer2,
211 nxpese_ctxt.secureTimerParams.secureTimer3);
212
213 phNxpEse_GetMaxTimer(&maxTimer);
214 #ifdef SPM_INTEGRATED
215 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
216 wConfigStatus = phNxpEse_SPM_DisablePwrControl(maxTimer);
217 if (wConfigStatus != ESESTATUS_SUCCESS) {
218 ALOGE("%s phNxpEse_SPM_DisablePwrControl: failed", __FUNCTION__);
219 }
220 }
221 #endif
222 do {
223 /* T=1 Protocol layer open */
224 wConfigStatus = phNxpEseProto7816_Open(protoInitParam);
225 if (phNxpEse_isColdResetRequired(initParams.initMode, wConfigStatus))
226 phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
227 } while (phNxpEse_isColdResetRequired(initParams.initMode, wConfigStatus) &&
228 retry++ < 1);
229 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
230 if (ESESTATUS_TRANSCEIVE_FAILED == wConfigStatus ||
231 ESESTATUS_FAILED == wConfigStatus) {
232 nxpese_ctxt.EseLibStatus = ESE_STATUS_RECOVERY;
233 }
234 }
235
236 if (ESESTATUS_SUCCESS == wConfigStatus) {
237 ALOGD_IF(ese_debug_enabled, "phNxpEseProto7816_Open completed >>>>>");
238 /* Retrieving the IFS-D value configured in the config file and applying to
239 * Card */
240 if ((nxpese_ctxt.endPointInfo == END_POINT_ESE) &&
241 (EseConfig::hasKey(NAME_NXP_ESE_IFSD_VALUE))) {
242 ifsd_value = EseConfig::getUnsigned(NAME_NXP_ESE_IFSD_VALUE);
243 if ((0xFFFF > ifsd_value) && (ifsd_value > 0)) {
244 ALOGD_IF(ese_debug_enabled,
245 "phNxpEseProto7816_SetIFS IFS adjustment requested with %ld",
246 ifsd_value);
247 phNxpEse_setIfs(ifsd_value);
248 } else {
249 ALOGD_IF(ese_debug_enabled,
250 "phNxpEseProto7816_SetIFS IFS adjustment argument invalid");
251 }
252 } else if ((nxpese_ctxt.endPointInfo == END_POINT_EUICC) &&
253 (EseConfig::hasKey(NAME_NXP_EUICC_IFSD_VALUE))) {
254 ifsd_value = EseConfig::getUnsigned(NAME_NXP_EUICC_IFSD_VALUE);
255 if ((0xFFFF > ifsd_value) && (ifsd_value > 0)) {
256 ALOGD_IF(ese_debug_enabled,
257 "phNxpEseProto7816_SetIFS IFS adjustment requested with %ld",
258 ifsd_value);
259 phNxpEse_setIfs(ifsd_value);
260 } else {
261 ALOGD_IF(ese_debug_enabled,
262 "phNxpEseProto7816_SetIFS IFS adjustment argument invalid");
263 }
264 }
265 } else {
266 ALOGE("phNxpEseProto7816_Open failed with status = %x", wConfigStatus);
267 }
268
269 return wConfigStatus;
270 }
271
272 /******************************************************************************
273 * Function phNxpEse_open
274 *
275 * Description This function is called by Jni during the
276 * initialization of the ESE. It opens the physical connection
277 * with ESE and creates required NAME_NXP_MAX_RNACK_RETRYclient
278 * thread for operation.
279 * Returns This function return ESESTATUS_SUCCESS (0) in case of
280 * success. In case of failure returns other failure values.
281 ******************************************************************************/
phNxpEse_open(phNxpEse_initParams initParams)282 ESESTATUS phNxpEse_open(phNxpEse_initParams initParams) {
283 phPalEse_Config_t tPalConfig;
284 ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
285 unsigned long int num = 0, tpm_enable = 0;
286 char ese_dev_node[64];
287 std::string ese_node;
288 #ifdef SPM_INTEGRATED
289 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
290 spm_state_t current_spm_state = SPM_STATE_INVALID;
291 #endif
292 /* initialize trace level */
293 phNxpLog_InitializeLogLevel();
294
295 ALOGD_IF(ese_debug_enabled, "phNxpEse_open Enter");
296 /*When spi channel is already opened return status as FAILED*/
297 if (nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE) {
298 ALOGD_IF(ese_debug_enabled, "already opened\n");
299 return ESESTATUS_BUSY;
300 }
301
302 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
303 phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
304
305 ALOGD_IF(ese_debug_enabled, "MW SEAccessKit Version");
306 ALOGD_IF(ese_debug_enabled, "Android Version:0x%x", NXP_ANDROID_VER);
307 ALOGD_IF(ese_debug_enabled, "Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
308 ALOGD_IF(ese_debug_enabled, "Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
309
310 if (EseConfig::hasKey(NAME_NXP_OS_VERSION)) {
311 num = EseConfig::getUnsigned(NAME_NXP_OS_VERSION);
312 ALOGD_IF(ese_debug_enabled, "Chip type read from config file - %lu", num);
313 sOsVersion = (num == 1) ? OS_VERSION_4_0
314 : ((num == 2) ? OS_VERSION_5_1 : OS_VERSION_5_2);
315 } else {
316 sOsVersion = OS_VERSION_5_2;
317 ALOGD_IF(ese_debug_enabled,
318 "Chip type not defined in config file osVersion- %d", sOsVersion);
319 }
320 if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
321 tpm_enable = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
322 ALOGD_IF(
323 ese_debug_enabled,
324 "SPI Throughput measurement enable/disable read from config file - %lu",
325 tpm_enable);
326 } else {
327 ALOGD_IF(ese_debug_enabled,
328 "SPI Throughput not defined in config file - %lu", tpm_enable);
329 }
330 #if (NXP_POWER_SCHEME_SUPPORT == true)
331 if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
332 num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
333 nxpese_ctxt.pwr_scheme = num;
334 ALOGD_IF(ese_debug_enabled, "Power scheme read from config file - %lu",
335 num);
336 } else {
337 nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
338 ALOGD_IF(ese_debug_enabled, "Power scheme not defined in config file - %lu",
339 num);
340 }
341 #else
342 nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
343 tpm_enable = 0x00;
344 #endif
345
346 if (EseConfig::hasKey(NAME_NXP_NAD_POLL_RETRY_TIME)) {
347 num = EseConfig::getUnsigned(NAME_NXP_NAD_POLL_RETRY_TIME);
348 nxpese_ctxt.nadPollingRetryTime = num;
349 } else {
350 nxpese_ctxt.nadPollingRetryTime = 5;
351 }
352
353 ALOGD_IF(ese_debug_enabled, "Nad poll retry time in us - %lu us",
354 nxpese_ctxt.nadPollingRetryTime * GET_WAKE_UP_DELAY() *
355 NAD_POLLING_SCALER);
356
357 /*Read device node path*/
358 ese_node = EseConfig::getString(NAME_NXP_ESE_DEV_NODE, "/dev/pn81a");
359 strlcpy(ese_dev_node, ese_node.c_str(), sizeof(ese_dev_node));
360 tPalConfig.pDevName = (int8_t*)ese_dev_node;
361
362 /* Initialize PAL layer */
363 wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
364 if (wConfigStatus != ESESTATUS_SUCCESS) {
365 ALOGE("phPalEse_Init Failed");
366 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
367 if (ESESTATUS_DRIVER_BUSY == wConfigStatus)
368 ALOGE("Ese Driver is Busy!!!");
369 }
370 goto clean_and_return;
371 }
372 /* Copying device handle to ESE Lib context*/
373 nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
374 if (ESE_PROTOCOL_MEDIA_SPI == initParams.mediaType) {
375 ALOGD_IF(ese_debug_enabled,
376 "Inform eSE about the starting of trusted Mode");
377 wConfigStatus =
378 phPalEse_ioctl(phPalEse_e_SetSecureMode, tPalConfig.pDevHandle, 0x01);
379 if (ESESTATUS_SUCCESS != wConfigStatus) goto clean_and_return_2;
380 }
381 #ifdef SPM_INTEGRATED
382 /* Get the Access of ESE*/
383 wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
384 if (wSpmStatus != ESESTATUS_SUCCESS) {
385 ALOGE("phNxpEse_SPM_Init Failed");
386 wConfigStatus = ESESTATUS_FAILED;
387 goto clean_and_return_2;
388 }
389 wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
390 if (wSpmStatus != ESESTATUS_SUCCESS) {
391 ALOGE(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
392 wConfigStatus = ESESTATUS_FAILED;
393 goto clean_and_return_1;
394 }
395 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
396 wConfigStatus = phNxpEse_checkFWDwnldStatus();
397 if (wConfigStatus != ESESTATUS_SUCCESS) {
398 ALOGE("Failed to open SPI due to VEN pin used by FW download \n");
399 wConfigStatus = ESESTATUS_FAILED;
400 goto clean_and_return_1;
401 }
402 }
403 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
404 if (wSpmStatus != ESESTATUS_SUCCESS) {
405 ALOGE(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
406 wConfigStatus = ESESTATUS_FAILED;
407 goto clean_and_return_1;
408 } else {
409 if (((current_spm_state & SPM_STATE_SPI) |
410 (current_spm_state & SPM_STATE_SPI_PRIO)) &&
411 !(current_spm_state & SPM_STATE_SPI_FAILED)) {
412 ALOGE(" %s : SPI is already opened...second instance not allowed",
413 __FUNCTION__);
414 wConfigStatus = ESESTATUS_FAILED;
415 goto clean_and_return_1;
416 }
417 }
418 if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
419 ALOGE(" %s : Denying to open JCOP Download in progress", __FUNCTION__);
420 wConfigStatus = ESESTATUS_FAILED;
421 goto clean_and_return_1;
422 }
423 phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams,
424 sizeof(phNxpEse_initParams));
425 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
426 /* Updating ESE power state based on the init mode */
427 if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
428 ALOGD_IF(ese_debug_enabled, "%s Init mode ---->OSU", __FUNCTION__);
429 wConfigStatus = phNxpEse_checkJcopDwnldState();
430 if (wConfigStatus != ESESTATUS_SUCCESS) {
431 ALOGE("phNxpEse_checkJcopDwnldState failed");
432 goto clean_and_return_1;
433 }
434 }
435 }
436 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_ENABLE);
437 if (wSpmStatus != ESESTATUS_SUCCESS) {
438 ALOGE("phNxpEse_SPM_ConfigPwr: enabling power Failed");
439 if (wSpmStatus == ESESTATUS_BUSY) {
440 wConfigStatus = ESESTATUS_BUSY;
441 } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
442 wConfigStatus = ESESTATUS_DWNLD_BUSY;
443 } else {
444 wConfigStatus = ESESTATUS_FAILED;
445 }
446 goto clean_and_return;
447 } else {
448 ALOGD_IF(ese_debug_enabled, "nxpese_ctxt.spm_power_state true");
449 nxpese_ctxt.spm_power_state = true;
450 }
451 #endif
452 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
453 if (tpm_enable) {
454 wConfigStatus = phPalEse_ioctl(phPalEse_e_EnableThroughputMeasurement,
455 nxpese_ctxt.pDevHandle, 0);
456 if (wConfigStatus != ESESTATUS_SUCCESS) {
457 ALOGE("phPalEse_IoCtl Failed");
458 goto clean_and_return;
459 }
460 }
461 }
462 ALOGD_IF(ese_debug_enabled, "wConfigStatus %x", wConfigStatus);
463 return wConfigStatus;
464
465 clean_and_return:
466 #ifdef SPM_INTEGRATED
467 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
468 if (wSpmStatus != ESESTATUS_SUCCESS) {
469 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
470 }
471 clean_and_return_1:
472 phNxpEse_SPM_DeInit();
473 clean_and_return_2:
474 #endif
475 if (NULL != nxpese_ctxt.pDevHandle) {
476 phPalEse_close(nxpese_ctxt.pDevHandle);
477 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
478 }
479 nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
480 nxpese_ctxt.spm_power_state = false;
481 return ESESTATUS_FAILED;
482 }
483
484 /******************************************************************************
485 * \ingroup spi_libese
486 *
487 * \brief Check if libese has opened
488 *
489 * \retval return false if it is close, otherwise true.
490 **
491 ******************************************************************************/
phNxpEse_isOpen()492 bool phNxpEse_isOpen() { return nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE; }
493
494 /******************************************************************************
495 * Function phNxpEse_openPrioSession
496 *
497 * Description This function is called by Jni during the
498 * initialization of the ESE. It opens the physical connection
499 * with ESE () and creates required client thread for
500 * operation. This will get priority access to ESE for timeout
501 duration.
502
503 * Returns This function return ESESTATUS_SUCCESS (0) in case of
504 success
505 * In case of failure returns other failure value.
506 *
507 ******************************************************************************/
phNxpEse_openPrioSession(phNxpEse_initParams initParams)508 ESESTATUS phNxpEse_openPrioSession(phNxpEse_initParams initParams) {
509 phPalEse_Config_t tPalConfig;
510 ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
511 unsigned long int num = 0, tpm_enable = 0;
512
513 /* initialize trace level */
514 phNxpLog_InitializeLogLevel();
515 ALOGD_IF(ese_debug_enabled, "phNxpEse_openPrioSession Enter");
516 #ifdef SPM_INTEGRATED
517 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
518 spm_state_t current_spm_state = SPM_STATE_INVALID;
519 #endif
520 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
521 phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
522
523 ALOGD_IF(ese_debug_enabled, "MW SEAccessKit Version");
524 ALOGD_IF(ese_debug_enabled, "Android Version:0x%x", NXP_ANDROID_VER);
525 ALOGD_IF(ese_debug_enabled, "Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
526 ALOGD_IF(ese_debug_enabled, "Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
527
528 #if (NXP_POWER_SCHEME_SUPPORT == true)
529 if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
530 num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
531 nxpese_ctxt.pwr_scheme = num;
532 ALOGD_IF(ese_debug_enabled, "Power scheme read from config file - %lu",
533 num);
534 } else
535 #endif
536 {
537 nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
538 ALOGD_IF(ese_debug_enabled, "Power scheme not defined in config file - %lu",
539 num);
540 }
541 if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
542 tpm_enable = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
543 ALOGD_IF(
544 ese_debug_enabled,
545 "SPI Throughput measurement enable/disable read from config file - %lu",
546 tpm_enable);
547 } else {
548 ALOGD_IF(ese_debug_enabled,
549 "SPI Throughput not defined in config file - %lu", num);
550 }
551
552 tPalConfig.pDevName = (int8_t*)"/dev/p73";
553
554 /* Initialize PAL layer */
555 wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
556 if (wConfigStatus != ESESTATUS_SUCCESS) {
557 ALOGE("phPalEse_Init Failed");
558 goto clean_and_return;
559 }
560 /* Copying device handle to hal context*/
561 nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
562
563 #ifdef SPM_INTEGRATED
564 /* Get the Access of ESE*/
565 wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
566 if (wSpmStatus != ESESTATUS_SUCCESS) {
567 ALOGE("phNxpEse_SPM_Init Failed");
568 wConfigStatus = ESESTATUS_FAILED;
569 goto clean_and_return_2;
570 }
571 wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
572 if (wSpmStatus != ESESTATUS_SUCCESS) {
573 ALOGE(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
574 wConfigStatus = ESESTATUS_FAILED;
575 goto clean_and_return_1;
576 }
577 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
578 if (wSpmStatus != ESESTATUS_SUCCESS) {
579 ALOGE(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
580 wConfigStatus = ESESTATUS_FAILED;
581 goto clean_and_return_1;
582 } else {
583 if ((current_spm_state & SPM_STATE_SPI) |
584 (current_spm_state & SPM_STATE_SPI_PRIO)) {
585 ALOGE(" %s : SPI is already opened...second instance not allowed",
586 __FUNCTION__);
587 wConfigStatus = ESESTATUS_FAILED;
588 goto clean_and_return_1;
589 }
590 if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
591 ALOGE(" %s : Denying to open JCOP Download in progress", __FUNCTION__);
592 wConfigStatus = ESESTATUS_FAILED;
593 goto clean_and_return_1;
594 }
595 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
596 wConfigStatus = phNxpEse_checkFWDwnldStatus();
597 if (wConfigStatus != ESESTATUS_SUCCESS) {
598 ALOGD_IF(ese_debug_enabled,
599 "Failed to open SPI due to VEN pin used by FW download \n");
600 wConfigStatus = ESESTATUS_FAILED;
601 goto clean_and_return_1;
602 }
603 }
604 }
605 phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams.initMode,
606 sizeof(phNxpEse_initParams));
607 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
608 /* Updating ESE power state based on the init mode */
609 if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
610 wConfigStatus = phNxpEse_checkJcopDwnldState();
611 if (wConfigStatus != ESESTATUS_SUCCESS) {
612 ALOGE("phNxpEse_checkJcopDwnldState failed");
613 goto clean_and_return_1;
614 }
615 }
616 }
617 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_PRIO_ENABLE);
618 if (wSpmStatus != ESESTATUS_SUCCESS) {
619 ALOGE("phNxpEse_SPM_ConfigPwr: enabling power for spi prio Failed");
620 if (wSpmStatus == ESESTATUS_BUSY) {
621 wConfigStatus = ESESTATUS_BUSY;
622 } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
623 wConfigStatus = ESESTATUS_DWNLD_BUSY;
624 } else {
625 wConfigStatus = ESESTATUS_FAILED;
626 }
627 goto clean_and_return;
628 } else {
629 ALOGE("nxpese_ctxt.spm_power_state true");
630 nxpese_ctxt.spm_power_state = true;
631 }
632 #endif
633
634 #ifndef SPM_INTEGRATED
635 wConfigStatus =
636 phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
637 if (wConfigStatus != ESESTATUS_SUCCESS) {
638 ALOGE("phPalEse_IoCtl Failed");
639 goto clean_and_return;
640 }
641 #endif
642 wConfigStatus =
643 phPalEse_ioctl(phPalEse_e_EnableLog, nxpese_ctxt.pDevHandle, 0);
644 if (wConfigStatus != ESESTATUS_SUCCESS) {
645 ALOGE("phPalEse_IoCtl Failed");
646 goto clean_and_return;
647 }
648 wConfigStatus =
649 phPalEse_ioctl(phPalEse_e_EnablePollMode, nxpese_ctxt.pDevHandle, 1);
650 if (wConfigStatus != ESESTATUS_SUCCESS) {
651 ALOGE("phPalEse_IoCtl Failed");
652 goto clean_and_return;
653 }
654 ALOGD_IF(ese_debug_enabled, "wConfigStatus %x", wConfigStatus);
655 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
656 if (tpm_enable) {
657 wConfigStatus = phPalEse_ioctl(phPalEse_e_EnableThroughputMeasurement,
658 nxpese_ctxt.pDevHandle, 0);
659 if (wConfigStatus != ESESTATUS_SUCCESS) {
660 ALOGE("phPalEse_IoCtl Failed");
661 goto clean_and_return;
662 }
663 }
664 }
665 return wConfigStatus;
666
667 clean_and_return:
668 #ifdef SPM_INTEGRATED
669 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
670 if (wSpmStatus != ESESTATUS_SUCCESS) {
671 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
672 }
673 clean_and_return_1:
674 phNxpEse_SPM_DeInit();
675 clean_and_return_2:
676 #endif
677 if (NULL != nxpese_ctxt.pDevHandle) {
678 phPalEse_close(nxpese_ctxt.pDevHandle);
679 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
680 }
681 nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
682 nxpese_ctxt.spm_power_state = false;
683 return ESESTATUS_FAILED;
684 }
685
686 /******************************************************************************
687 * Function phNxpEse_setJcopDwnldState
688 *
689 * Description This function is used to check whether JCOP OS
690 * download can be started or not.
691 *
692 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_FAILED
693 *
694 ******************************************************************************/
phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state)695 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state) {
696 ESESTATUS wConfigStatus = ESESTATUS_FAILED;
697 ALOGD_IF(ese_debug_enabled, "phNxpEse_setJcopDwnldState Enter");
698
699 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
700 wConfigStatus = phNxpEse_SPM_SetJcopDwnldState(state);
701 } else {
702 ALOGE("%s function not supported", __FUNCTION__);
703 }
704 return wConfigStatus;
705 }
706
707 /******************************************************************************
708 * Function phNxpEse_checkJcopDwnldState
709 *
710 * Description This function is used to check whether JCOP OS
711 * download can be started or not.
712 *
713 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_BUSY
714 *
715 ******************************************************************************/
phNxpEse_checkJcopDwnldState(void)716 static ESESTATUS phNxpEse_checkJcopDwnldState(void) {
717 ALOGD_IF(ese_debug_enabled, "phNxpEse_checkJcopDwnld Enter");
718 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
719 spm_state_t current_spm_state = SPM_STATE_INVALID;
720 uint8_t ese_dwnld_retry = 0x00;
721 ESESTATUS status = ESESTATUS_FAILED;
722
723 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
724 if (wSpmStatus == ESESTATUS_SUCCESS) {
725 /* Check current_spm_state and update config/Spm status*/
726 if ((current_spm_state & SPM_STATE_JCOP_DWNLD) ||
727 (current_spm_state & SPM_STATE_WIRED))
728 return ESESTATUS_BUSY;
729
730 status = phNxpEse_setJcopDwnldState(JCP_DWNLD_INIT);
731 if (status == ESESTATUS_SUCCESS) {
732 while (ese_dwnld_retry < ESE_JCOP_OS_DWNLD_RETRY_CNT) {
733 ALOGD_IF(ese_debug_enabled, "ESE_JCOP_OS_DWNLD_RETRY_CNT retry count");
734 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
735 if (wSpmStatus == ESESTATUS_SUCCESS) {
736 if ((current_spm_state & SPM_STATE_JCOP_DWNLD)) {
737 status = ESESTATUS_SUCCESS;
738 break;
739 }
740 } else {
741 status = ESESTATUS_FAILED;
742 break;
743 }
744 phNxpEse_Sleep(
745 200000); /*sleep for 200 ms checking for jcop dwnld status*/
746 ese_dwnld_retry++;
747 }
748 }
749 }
750
751 ALOGD_IF(ese_debug_enabled, "phNxpEse_checkJcopDwnldState status %x", status);
752 return status;
753 }
754
755 /******************************************************************************
756 * Function phNxpEse_Transceive
757 *
758 * Description This function update the len and provided buffer
759 *
760 * Returns On Success ESESTATUS_SUCCESS else proper error code
761 *
762 ******************************************************************************/
phNxpEse_Transceive(phNxpEse_data * pCmd,phNxpEse_data * pRsp)763 ESESTATUS phNxpEse_Transceive(phNxpEse_data* pCmd, phNxpEse_data* pRsp) {
764 ESESTATUS status = ESESTATUS_FAILED;
765
766 if ((NULL == pCmd) || (NULL == pRsp)) return ESESTATUS_INVALID_PARAMETER;
767
768 if ((pCmd->len == 0) || pCmd->p_data == NULL) {
769 ALOGE(" phNxpEse_Transceive - Invalid Parameter no data\n");
770 return ESESTATUS_INVALID_PARAMETER;
771 } else if (pCmd->len > MAX_SUPPORTED_DATA_SIZE) {
772 ALOGE(" phNxpEse_Transceive - Invalid data size \n");
773 return ESESTATUS_INVALID_RECEIVE_LENGTH;
774 } else if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
775 ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
776 return ESESTATUS_NOT_INITIALISED;
777 } else if ((ESE_STATUS_BUSY == nxpese_ctxt.EseLibStatus)) {
778 ALOGE(" %s ESE - BUSY \n", __FUNCTION__);
779 return ESESTATUS_BUSY;
780 } else if ((ESE_STATUS_RECOVERY == nxpese_ctxt.EseLibStatus)) {
781 ALOGE(" %s ESE - RECOVERY \n", __FUNCTION__);
782 return ESESTATUS_RECOVERY_STARTED;
783 } else {
784 nxpese_ctxt.EseLibStatus = ESE_STATUS_BUSY;
785 status = phNxpEseProto7816_Transceive((phNxpEse_data*)pCmd,
786 (phNxpEse_data*)pRsp);
787 if (ESESTATUS_SUCCESS != status) {
788 ALOGE(" %s phNxpEseProto7816_Transceive- Failed \n", __FUNCTION__);
789 if (ESESTATUS_TRANSCEIVE_FAILED == status) {
790 /*MAX WTX reached*/
791 nxpese_ctxt.EseLibStatus = ESE_STATUS_RECOVERY;
792 } else {
793 /*Timeout/ No response*/
794 nxpese_ctxt.EseLibStatus = ESE_STATUS_IDLE;
795 }
796 } else {
797 nxpese_ctxt.EseLibStatus = ESE_STATUS_IDLE;
798 }
799 nxpese_ctxt.rnack_sent = false;
800
801 ALOGD_IF(ese_debug_enabled, " %s Exit status 0x%x \n", __FUNCTION__,
802 status);
803 return status;
804 }
805 }
806 /******************************************************************************
807 * Function phNxpEse_coldReset
808 *
809 * Description This function power cycles the ESE
810 * (cold reset by prop. FW command) interface by
811 * talking to NFC HAL
812 *
813 * Note:
814 * After cold reset, phNxpEse_init need to be called to
815 * reset the host AP T=1 stack parameters
816 *
817 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
818 *successful else
819 * ESESTATUS_FAILED(1)
820 ******************************************************************************/
phNxpEse_coldReset(void)821 ESESTATUS phNxpEse_coldReset(void) {
822 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
823 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
824 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
825 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
826 } else {
827 wSpmStatus = ESESTATUS_FAILED;
828 ALOGE(" %s Function not supported \n", __FUNCTION__);
829 }
830 ALOGD_IF(ese_debug_enabled, " %s Exit status 0x%x \n", __FUNCTION__,
831 wSpmStatus);
832 return wSpmStatus;
833 }
834
835 /******************************************************************************
836 * Function phNxpEse_reset
837 *
838 * Description This function reset the ESE interface and free all
839 *
840 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
841 *successful else
842 * ESESTATUS_FAILED(1)
843 ******************************************************************************/
phNxpEse_reset(void)844 ESESTATUS phNxpEse_reset(void) {
845 ESESTATUS status = ESESTATUS_FAILED;
846 unsigned long maxTimer = 0;
847 #ifdef SPM_INTEGRATED
848 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
849 #endif
850
851 /* TBD : Call the ioctl to reset the ESE */
852 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
853 /* Do an interface reset, don't wait to see if JCOP went through a full power
854 * cycle or not */
855 status = phNxpEseProto7816_IntfReset(
856 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
857 if (status) {
858 ALOGE("%s Ese status Failed", __FUNCTION__);
859 }
860
861 ALOGD_IF(ese_debug_enabled,
862 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
863 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
864 nxpese_ctxt.secureTimerParams.secureTimer2,
865 nxpese_ctxt.secureTimerParams.secureTimer3);
866 phNxpEse_GetMaxTimer(&maxTimer);
867 #ifdef SPM_INTEGRATED
868 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
869 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
870 if (status != ESESTATUS_SUCCESS) {
871 ALOGE("%s phNxpEse_SPM_DisablePwrControl: failed", __FUNCTION__);
872 }
873 }
874 if ((nxpese_ctxt.pwr_scheme == PN67T_POWER_SCHEME) ||
875 (nxpese_ctxt.pwr_scheme == PN80T_LEGACY_SCHEME)) {
876 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
877 if (wSpmStatus != ESESTATUS_SUCCESS) {
878 ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
879 }
880 }
881 #else
882 /* if arg ==2 (hard reset)
883 * if arg ==1 (soft reset)
884 */
885 status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
886 if (status != ESESTATUS_SUCCESS) {
887 ALOGE("phNxpEse_reset Failed");
888 }
889 #endif
890 ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
891 return status;
892 }
893
894 /******************************************************************************
895 * Function phNxpEse_resetJcopUpdate
896 *
897 * Description This function reset the ESE interface during JCOP Update
898 *
899 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
900 *successful else
901 * ESESTATUS_FAILED(1)
902 ******************************************************************************/
phNxpEse_resetJcopUpdate(void)903 ESESTATUS phNxpEse_resetJcopUpdate(void) {
904 ESESTATUS status = ESESTATUS_SUCCESS;
905 uint8_t retry = 0;
906 #ifdef SPM_INTEGRATED
907 unsigned long int num = 0;
908 #endif
909
910 /* TBD : Call the ioctl to reset the */
911 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
912
913 /* Reset interface after every reset irrespective of
914 whether JCOP did a full power cycle or not. */
915 do {
916 status = phNxpEseProto7816_Reset();
917 if (status != ESESTATUS_SUCCESS) phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
918 } while (status != ESESTATUS_SUCCESS && retry++ < 1);
919
920 /* Retrieving the IFS-D value configured in the config file and applying to
921 * Card */
922 if (EseConfig::hasKey(NAME_NXP_ESE_IFSD_VALUE)) {
923 unsigned long int ifsd_value = 0;
924 ifsd_value = EseConfig::getUnsigned(NAME_NXP_ESE_IFSD_VALUE);
925 if ((0xFFFF > ifsd_value) && (ifsd_value > 0)) {
926 ALOGD_IF(ese_debug_enabled,
927 "phNxpEseProto7816_SetIFS IFS adjustment requested with %ld",
928 ifsd_value);
929 phNxpEse_setIfs(ifsd_value);
930 } else {
931 ALOGD_IF(ese_debug_enabled,
932 "phNxpEseProto7816_SetIFS IFS adjustment argument invalid");
933 }
934 }
935 #ifdef SPM_INTEGRATED
936 #if (NXP_POWER_SCHEME_SUPPORT == true)
937 if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
938 num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
939 if ((num == 1) || (num == 2)) {
940 ALOGD_IF(ese_debug_enabled, " %s Call Config Pwr Reset \n", __FUNCTION__);
941 status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
942 if (status != ESESTATUS_SUCCESS) {
943 ALOGE("phNxpEse_resetJcopUpdate: reset Failed");
944 status = ESESTATUS_FAILED;
945 }
946 } else if (num == 3) {
947 ALOGD_IF(ese_debug_enabled, " %s Call eSE Chip Reset \n", __FUNCTION__);
948 status = phNxpEse_chipReset();
949 if (status != ESESTATUS_SUCCESS) {
950 ALOGE("phNxpEse_resetJcopUpdate: chip reset Failed");
951 status = ESESTATUS_FAILED;
952 }
953 } else {
954 ALOGD_IF(ese_debug_enabled, " %s Invalid Power scheme \n", __FUNCTION__);
955 }
956 }
957 #else
958 {
959 status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
960 if (status != ESESTATUS_SUCCESS) {
961 ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
962 status = ESESTATUS_FAILED;
963 }
964 }
965 #endif
966 #else
967 /* if arg ==2 (hard reset)
968 * if arg ==1 (soft reset)
969 */
970 status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
971 if (status != ESESTATUS_SUCCESS) {
972 ALOGE("phNxpEse_resetJcopUpdate Failed");
973 }
974 #endif
975
976 ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
977 return status;
978 }
979 /******************************************************************************
980 * Function phNxpEse_EndOfApdu
981 *
982 * Description This function is used to send S-frame to indicate
983 *END_OF_APDU
984 *
985 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
986 *successful else
987 * ESESTATUS_FAILED(1)
988 *
989 ******************************************************************************/
phNxpEse_EndOfApdu(void)990 ESESTATUS phNxpEse_EndOfApdu(void) {
991 ESESTATUS status = ESESTATUS_SUCCESS;
992 #if (NXP_ESE_END_OF_SESSION == true)
993 status = phNxpEseProto7816_Close(
994 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
995 #endif
996 return status;
997 }
998
999 /******************************************************************************
1000 * Function phNxpEse_chipReset
1001 *
1002 * Description This function is used to reset the ESE.
1003 *
1004 * Returns Always return ESESTATUS_SUCCESS (0).
1005 *
1006 ******************************************************************************/
phNxpEse_chipReset(void)1007 ESESTATUS phNxpEse_chipReset(void) {
1008 ESESTATUS status = ESESTATUS_FAILED;
1009 ESESTATUS bStatus = ESESTATUS_FAILED;
1010 if (nxpese_ctxt.pwr_scheme == PN80T_EXT_PMU_SCHEME) {
1011 bStatus = phNxpEseProto7816_Reset();
1012 if (!bStatus) {
1013 ALOGE("Inside phNxpEse_chipReset, phNxpEseProto7816_Reset Failed");
1014 }
1015 status = phPalEse_ioctl(phPalEse_e_ChipRst, nxpese_ctxt.pDevHandle, 6);
1016 if (status != ESESTATUS_SUCCESS) {
1017 ALOGE("phNxpEse_chipReset Failed");
1018 }
1019 } else {
1020 ALOGD_IF(ese_debug_enabled,
1021 "phNxpEse_chipReset is not supported in legacy power scheme");
1022 }
1023 return status;
1024 }
1025
1026 /******************************************************************************
1027 * Function phNxpEse_GetOsMode
1028 *
1029 * Description This function is used to get OS mode(JCOP/OSU)
1030 *
1031 * Returns 0x01 : JCOP_MODE
1032 * 0x02 : OSU_MODE
1033 *
1034 ******************************************************************************/
phNxpEse_GetOsMode(void)1035 phNxpEseProto7816_OsType_t phNxpEse_GetOsMode(void) {
1036 return phNxpEseProto7816_GetOsMode();
1037 }
1038
1039 /******************************************************************************
1040 * Function phNxpEse_isColdResetRequired
1041 *
1042 * Description This function determines whether hard reset recovery is
1043 * required or not on protocol recovery failure.
1044 * Returns TRUE(required)/FALSE(not required).
1045 *
1046 ******************************************************************************/
phNxpEse_isColdResetRequired(phNxpEse_initMode mode,ESESTATUS status)1047 static __inline bool phNxpEse_isColdResetRequired(phNxpEse_initMode mode,
1048 ESESTATUS status) {
1049 return (mode == ESE_MODE_OSU && status != ESESTATUS_SUCCESS);
1050 }
1051
1052 /******************************************************************************
1053 * Function phNxpEse_doResetProtection
1054 *
1055 * Description This function enables/disables reset protection
1056 *
1057 * Returns SUCCESS(0)/FAIL(-1).
1058 *
1059 ******************************************************************************/
phNxpEse_doResetProtection(bool flag)1060 ESESTATUS phNxpEse_doResetProtection(bool flag) {
1061 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1062 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
1063 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1064 wSpmStatus = phPalEse_ioctl(phPalEse_e_ResetProtection,
1065 nxpese_ctxt.pDevHandle, flag);
1066 } else {
1067 wSpmStatus = ESESTATUS_FAILED;
1068 ALOGE(" %s Function not supported \n", __FUNCTION__);
1069 }
1070 ALOGD_IF(ese_debug_enabled, " %s Exit status 0x%x \n", __FUNCTION__,
1071 wSpmStatus);
1072 return wSpmStatus;
1073 }
1074
1075 /******************************************************************************
1076 * Function phNxpEse_deInit
1077 *
1078 * Description This function de-initializes all the ESE protocol params
1079 *
1080 * Returns Always return ESESTATUS_SUCCESS (0).
1081 *
1082 ******************************************************************************/
phNxpEse_deInit(void)1083 ESESTATUS phNxpEse_deInit(void) {
1084 ESESTATUS status = ESESTATUS_SUCCESS;
1085 unsigned long maxTimer = 0;
1086 unsigned long num = 0;
1087 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0 &&
1088 (ESE_STATUS_RECOVERY == nxpese_ctxt.EseLibStatus) &&
1089 ESE_PROTOCOL_MEDIA_SPI != nxpese_ctxt.initParams.mediaType) {
1090 return status;
1091 }
1092 if (nxpese_ctxt.initParams.initMode == ESE_MODE_OSU) {
1093 phNxpEse_doResetProtection(false);
1094 }
1095 /*TODO : to be removed after JCOP fix*/
1096 if (EseConfig::hasKey(NAME_NXP_VISO_DPD_ENABLED)) {
1097 num = EseConfig::getUnsigned(NAME_NXP_VISO_DPD_ENABLED);
1098 }
1099 if (num == 0 && nxpese_ctxt.nadInfo.nadRx == EUICC_NAD_RX) {
1100 // do nothing
1101 } else {
1102 status = phNxpEseProto7816_Close(
1103 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
1104 if (status == ESESTATUS_SUCCESS) {
1105 ALOGD_IF(ese_debug_enabled,
1106 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
1107 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
1108 nxpese_ctxt.secureTimerParams.secureTimer2,
1109 nxpese_ctxt.secureTimerParams.secureTimer3);
1110 phNxpEse_GetMaxTimer(&maxTimer);
1111 #ifdef SPM_INTEGRATED
1112 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
1113 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
1114 if (status != ESESTATUS_SUCCESS) {
1115 ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
1116 }
1117 } else {
1118 ALOGD_IF(ese_debug_enabled, "Interface reset for DPD");
1119 status = phNxpEseProto7816_IntfReset(
1120 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
1121 if (status != ESESTATUS_SUCCESS) {
1122 ALOGE("%s IntfReset Failed ", __FUNCTION__);
1123 }
1124 }
1125 #endif
1126 }
1127 }
1128 return status;
1129 }
1130
1131 /******************************************************************************
1132 * Function phNxpEse_close
1133 *
1134 * Description This function close the ESE interface and free all
1135 * resources.
1136 *
1137 * Returns Always return ESESTATUS_SUCCESS (0).
1138 *
1139 ******************************************************************************/
phNxpEse_close(ESESTATUS deInitStatus)1140 ESESTATUS phNxpEse_close(ESESTATUS deInitStatus) {
1141 ESESTATUS status = ESESTATUS_SUCCESS;
1142 ALOGD_IF(ese_debug_enabled, "phNxpEse_close Enter");
1143 if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
1144 ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
1145 return ESESTATUS_NOT_INITIALISED;
1146 }
1147
1148 #ifdef SPM_INTEGRATED
1149 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1150 #endif
1151
1152 #ifdef SPM_INTEGRATED
1153 /* Release the Access of */
1154 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
1155 if (wSpmStatus != ESESTATUS_SUCCESS) {
1156 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
1157 } else {
1158 nxpese_ctxt.spm_power_state = false;
1159 }
1160
1161 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
1162 if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
1163 status = phNxpEse_setJcopDwnldState(JCP_SPI_DWNLD_COMPLETE);
1164 if (status != ESESTATUS_SUCCESS) {
1165 ALOGE("%s: phNxpEse_setJcopDwnldState failed", __FUNCTION__);
1166 }
1167 }
1168 } else {
1169 if (NULL != nxpese_ctxt.pDevHandle) {
1170 if (ESE_PROTOCOL_MEDIA_SPI == nxpese_ctxt.initParams.mediaType) {
1171 ALOGD_IF(ese_debug_enabled, "Inform eSE that trusted Mode is over");
1172 status = phPalEse_ioctl(phPalEse_e_SetSecureMode,
1173 nxpese_ctxt.pDevHandle, 0x00);
1174 if (status != ESESTATUS_SUCCESS) {
1175 ALOGE("%s: phPalEse_e_SetSecureMode failed", __FUNCTION__);
1176 }
1177 if (ESESTATUS_SUCCESS != phNxpEseProto7816_CloseAllSessions()) {
1178 ALOGD_IF(ese_debug_enabled, "eSE not responding perform hard reset");
1179 phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
1180 }
1181 } else {
1182 if (nxpese_ctxt.EseLibStatus == ESE_STATUS_RECOVERY ||
1183 (deInitStatus == ESESTATUS_RESPONSE_TIMEOUT) ||
1184 ESESTATUS_SUCCESS != phNxpEseProto7816_CloseAllSessions()) {
1185 ALOGD_IF(ese_debug_enabled, "eSE not responding perform hard reset");
1186 phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
1187 }
1188 }
1189 ALOGD_IF(ese_debug_enabled, "Interface reset for DPD");
1190 status = phNxpEseProto7816_IntfReset(
1191 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
1192 if (status == ESESTATUS_TRANSCEIVE_FAILED || status == ESESTATUS_FAILED) {
1193 ALOGE("%s IntfReset Failed, perform hard reset", __FUNCTION__);
1194 // max wtx or no response of interface reset after protocol recovery
1195 phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
1196 }
1197 }
1198 }
1199
1200 wSpmStatus = phNxpEse_SPM_DeInit();
1201 if (wSpmStatus != ESESTATUS_SUCCESS) {
1202 ALOGE("phNxpEse_SPM_DeInit Failed");
1203 }
1204 #endif
1205 if (NULL != nxpese_ctxt.pDevHandle) {
1206 phPalEse_close(nxpese_ctxt.pDevHandle);
1207 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
1208 ALOGD_IF(ese_debug_enabled,
1209 "phNxpEse_close - ESE Context deinit completed");
1210 }
1211 /* Return success always */
1212 return status;
1213 }
1214
1215 /******************************************************************************
1216 * Function phNxpEse_read
1217 *
1218 * Description This function write the data to ESE through physical
1219 * interface (e.g. I2C) using the driver interface.
1220 * Before sending the data to ESE, phNxpEse_write_ext
1221 * is called to check if there is any extension processing
1222 * is required for the SPI packet being sent out.
1223 *
1224 * Returns It returns ESESTATUS_SUCCESS (0) if read successful else
1225 * ESESTATUS_FAILED(1)
1226 *
1227 ******************************************************************************/
phNxpEse_read(uint32_t * data_len,uint8_t ** pp_data)1228 ESESTATUS phNxpEse_read(uint32_t* data_len, uint8_t** pp_data) {
1229 ESESTATUS status = ESESTATUS_SUCCESS;
1230 int ret = -1;
1231
1232 ALOGD_IF(ese_debug_enabled, "%s Enter ..", __FUNCTION__);
1233
1234 ret = phNxpEse_readPacket(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_read_buff,
1235 MAX_DATA_LEN);
1236 if (ret < 0) {
1237 ALOGE("PAL Read status error status = %x", status);
1238 *data_len = 2;
1239 *pp_data = nxpese_ctxt.p_read_buff;
1240 status = ESESTATUS_FAILED;
1241 } else {
1242 PH_PAL_ESE_PRINT_PACKET_RX(nxpese_ctxt.p_read_buff, (uint16_t)ret);
1243 *data_len = ret;
1244 *pp_data = nxpese_ctxt.p_read_buff;
1245 status = ESESTATUS_SUCCESS;
1246 }
1247
1248 ALOGD_IF(ese_debug_enabled, "%s Exit", __FUNCTION__);
1249 return status;
1250 }
1251
1252 /******************************************************************************
1253 * Function phNxpEse_readPacket
1254 *
1255 * Description This function Reads requested number of bytes from
1256 * pn547 device into given buffer.
1257 *
1258 * Returns nNbBytesToRead- number of successfully read bytes
1259 * -1 - read operation failure
1260 *
1261 ******************************************************************************/
phNxpEse_readPacket(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)1262 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
1263 int nNbBytesToRead) {
1264 bool flushData = false;
1265 int ret = -1;
1266 int sof_counter = 0; /* one read may take 1 ms*/
1267 int total_count = 0, numBytesToRead = 0, headerIndex = 0;
1268
1269 ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
1270 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1271 int max_sof_counter = 0;
1272 /*Max retry to get SOF in case of chaining*/
1273 if (poll_sof_chained_delay == 1) {
1274 /*Wait Max for 1.3 sec before retry/recvoery*/
1275 /*(max_sof_counter(1300) * 10 us) = 1.3 sec */
1276 max_sof_counter = ESE_POLL_TIMEOUT * 10;
1277 }
1278 /*Max retry to get SOF in case of Non-chaining*/
1279 else {
1280 /*wait based on config option */
1281 /*(nadPollingRetryTime * WAKE_UP_DELAY_SN1xx * NAD_POLLING_SCALER_SN1xx)*/
1282 max_sof_counter = ((ESE_POLL_TIMEOUT * 1000) /
1283 (nxpese_ctxt.nadPollingRetryTime *
1284 GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER));
1285 }
1286 if (nxpese_ctxt.rnack_sent) {
1287 phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1288 }
1289 ALOGD_IF(ese_debug_enabled,
1290 "read() max_sof_counter: "
1291 "%X ESE_POLL_TIMEOUT %2X",
1292 max_sof_counter, ESE_POLL_TIMEOUT);
1293 do {
1294 ret = -1;
1295 ret = phPalEse_read(pDevHandle, pBuffer, 2);
1296 if (ret < 0) {
1297 /*Polling for read on spi, hence Debug log*/
1298 ALOGD_IF(ese_debug_enabled, "_spi_read() [HDR]errno : %x ret : %X",
1299 errno, ret);
1300 } else {
1301 if ((pBuffer[0] == nxpese_ctxt.nadInfo.nadRx) ||
1302 (pBuffer[0] == RECEIVE_PACKET_SOF)) {
1303 /* Read the HEADR of one byte*/
1304 ALOGD_IF(ese_debug_enabled, "%s Read HDR SOF + PCB", __FUNCTION__);
1305 numBytesToRead = 1; /*Read only INF LEN*/
1306 headerIndex = 1;
1307 break;
1308 } else if (((pBuffer[0] == 0x00) || (pBuffer[0] == 0xFF)) &&
1309 ((pBuffer[1] == nxpese_ctxt.nadInfo.nadRx) ||
1310 (pBuffer[1] == RECEIVE_PACKET_SOF))) {
1311 /* Read the HEADR of Two bytes*/
1312 ALOGD_IF(ese_debug_enabled, "%s Read HDR only SOF", __FUNCTION__);
1313 pBuffer[0] = pBuffer[1];
1314 numBytesToRead = 2; /*Read PCB + INF LEN*/
1315 headerIndex = 0;
1316 break;
1317 } else if (((pBuffer[0] == 0x00) && (pBuffer[1] == 0x00)) ||
1318 ((pBuffer[0] == 0xFF) && (pBuffer[1] == 0xFF))) {
1319 // LOG(ERROR) << StringPrintf("_spi_read() Buf[0]: %X Buf[1]: %X",
1320 // pBuffer[0], pBuffer[1]);
1321 } else if (ret >= 0) { /* Corruption happened during the receipt from
1322 Card, go flush out the data */
1323 ALOGE("_spi_read() Corruption Buf[0]: %X Buf[1]: %X ..len=%d",
1324 pBuffer[0], pBuffer[1], ret);
1325 break;
1326 }
1327 }
1328 /*If it is Chained packet wait for 100 usec*/
1329 if (poll_sof_chained_delay == 1) {
1330 ALOGD_IF(ese_debug_enabled, "%s Chained Pkt, delay read %dus",
1331 __FUNCTION__, GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1332 phPalEse_sleep(GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1333 } else {
1334 /*DLOG_IF(INFO, ese_debug_enabled)
1335 << StringPrintf("%s Normal Pkt, delay read %dus", __FUNCTION__,
1336 WAKE_UP_DELAY_SN1xx * NAD_POLLING_SCALER_SN1xx);*/
1337 phPalEse_sleep(nxpese_ctxt.nadPollingRetryTime * GET_WAKE_UP_DELAY() *
1338 NAD_POLLING_SCALER);
1339 }
1340 sof_counter++;
1341 } while (sof_counter < max_sof_counter);
1342
1343 /*SOF Read timeout happened, go for frame retransmission*/
1344 if (sof_counter == max_sof_counter) {
1345 ret = -1;
1346 }
1347 if (ret < 0) {
1348 /*In case of IO Error*/
1349 ret = -2;
1350 pBuffer[0] = 0x64;
1351 pBuffer[1] = 0xFF;
1352 } else if ((pBuffer[0] == nxpese_ctxt.nadInfo.nadRx) ||
1353 (pBuffer[0] == RECEIVE_PACKET_SOF)) {
1354 ALOGD_IF(ese_debug_enabled, "%s SOF FOUND", __FUNCTION__);
1355 /* Read the HEADR of one/Two bytes based on how two bytes read A5 PCB or
1356 * 00 A5*/
1357 ret =
1358 phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
1359 if (ret < 0) {
1360 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1361 flushData = true;
1362 } else {
1363 if ((pBuffer[1] == CHAINED_PACKET_WITHOUTSEQN) ||
1364 (pBuffer[1] == CHAINED_PACKET_WITHSEQN)) {
1365 poll_sof_chained_delay = 1;
1366 ALOGD_IF(ese_debug_enabled, "poll_sof_chained_delay value is %d ",
1367 poll_sof_chained_delay);
1368 } else {
1369 poll_sof_chained_delay = 0;
1370 ALOGD_IF(ese_debug_enabled, "poll_sof_chained_delay value is %d ",
1371 poll_sof_chained_delay);
1372 }
1373 total_count = 3;
1374 uint8_t pcb;
1375 phNxpEseProto7816_PCB_bits_t pcb_bits;
1376 pcb = pBuffer[PH_PROPTO_7816_PCB_OFFSET];
1377
1378 phNxpEse_memset(&pcb_bits, 0x00, sizeof(phNxpEseProto7816_PCB_bits_t));
1379 phNxpEse_memcpy(&pcb_bits, &pcb, sizeof(uint8_t));
1380
1381 /*For I-Frame Only*/
1382 if (0 == pcb_bits.msb) {
1383 if (pBuffer[2] != EXTENDED_FRAME_MARKER) {
1384 nNbBytesToRead = pBuffer[2];
1385 headerIndex = 3;
1386 } else {
1387 ret = phPalEse_read(pDevHandle, &pBuffer[3], 2);
1388 if (ret < 0) {
1389 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1390 flushData = true;
1391 } else {
1392 nNbBytesToRead = (pBuffer[3] << 8);
1393 nNbBytesToRead = nNbBytesToRead | pBuffer[4];
1394 /*If I-Frame received with invalid length respond with RNACK*/
1395 if ((nNbBytesToRead == 0) || (nNbBytesToRead > MAX_DATA_LEN) ||
1396 (nNbBytesToRead > phNxpEseProto7816_GetIfs())) {
1397 ALOGD_IF(ese_debug_enabled, "I-Frame with invalid len == %d",
1398 nNbBytesToRead);
1399 flushData = true;
1400 } else {
1401 ALOGE("_spi_read() [HDR]EXTENDED_FRAME_MARKER, ret=%d", ret);
1402 total_count += 2;
1403 headerIndex = 5;
1404 }
1405 }
1406 }
1407 } else {
1408 /*For Non-IFrame*/
1409 nNbBytesToRead = (int)pBuffer[2];
1410 headerIndex = 3;
1411 }
1412 if (!flushData) {
1413 /* Read the Complete data + one byte CRC*/
1414 ret = phPalEse_read(pDevHandle, &pBuffer[headerIndex],
1415 (nNbBytesToRead + 1));
1416 if (ret < 0) {
1417 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1418 ret = -1;
1419 } else {
1420 ret = (total_count + (nNbBytesToRead + 1));
1421 }
1422 nxpese_ctxt.rnack_sent = false;
1423 }
1424 }
1425 } else {
1426 flushData = true;
1427 }
1428 if (flushData) {
1429 /* Received corrupted frame:
1430 Flushing out data in the Rx buffer so that Card can switch the mode */
1431 uint16_t ifsd_size = phNxpEseProto7816_GetIfs();
1432 uint32_t total_frame_size = 0;
1433 ALOGE("_spi_read() corrupted, IFSD size=%d flushing it out!!", ifsd_size);
1434 /* If a non-zero byte is received while polling for NAD byte and the byte
1435 is not a valid NAD byte (0xA5 or 0xB4): 1) Read & discard (without
1436 de-asserting SPI CS line) : a. Max IFSD size + 5 (remaining four
1437 prologue + one LRC bytes) bytes from eSE if max IFS size is greater
1438 than 254 bytes OR b. Max IFSD size + 3 (remaining two prologue + one
1439 LRC bytes) bytes from eSE if max IFS size is less than 255 bytes.
1440 2) Send R-NACK to request eSE to re-transmit the frame*/
1441 if (ifsd_size > IFSC_SIZE_SEND) {
1442 total_frame_size = ifsd_size + 4;
1443 } else {
1444 total_frame_size = ifsd_size + 2;
1445 }
1446 nxpese_ctxt.rnack_sent = true;
1447 phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1448 ret = phPalEse_read(pDevHandle, &pBuffer[2], total_frame_size);
1449 if (ret < 0) {
1450 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1451 } else { /* LRC fail expected for this frame to send R-NACK*/
1452 ALOGD_IF(
1453 ese_debug_enabled,
1454 "_spi_read() SUCCESS ret : %X LRC fail excpected for this frame",
1455 ret);
1456 PH_PAL_ESE_PRINT_PACKET_RX(pBuffer, ret);
1457 }
1458 pBuffer[0] = 0x90;
1459 pBuffer[1] = RECEIVE_PACKET_SOF;
1460 ret = 0x02;
1461 phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1462 }
1463 } else {
1464 ret = phNxpEse_readPacket_legacy(pDevHandle, pBuffer, nNbBytesToRead);
1465 }
1466 ALOGD_IF(ese_debug_enabled, "%s Exit ret = %d", __FUNCTION__, ret);
1467 return ret;
1468 }
1469
1470 /******************************************************************************
1471 * Function phNxpEse_readPacket_legacy
1472 *
1473 * Description This function Reads requested number of bytes from
1474 * pn547 device into given buffer.
1475 *
1476 * Returns nNbBytesToRead- number of successfully read bytes
1477 * -1 - read operation failure
1478 *
1479 ******************************************************************************/
phNxpEse_readPacket_legacy(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)1480 static int phNxpEse_readPacket_legacy(void* pDevHandle, uint8_t* pBuffer,
1481 int nNbBytesToRead) {
1482 int ret = -1;
1483 int sof_counter = 0; /* one read may take 1 ms*/
1484 int total_count = 0, numBytesToRead = 0, headerIndex = 0;
1485 do {
1486 sof_counter++;
1487 ret = -1;
1488 ret = phPalEse_read(pDevHandle, pBuffer, 2);
1489 if (ret < 0) {
1490 /*Polling for read on spi, hence Debug log*/
1491 ALOGD_IF(ese_debug_enabled, "_spi_read() [HDR]errno : %x ret : %X", errno,
1492 ret);
1493 }
1494 if (pBuffer[0] == RECEIVE_PACKET_SOF) {
1495 /* Read the HEADR of one byte*/
1496 ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
1497 numBytesToRead = 1;
1498 headerIndex = 1;
1499 break;
1500 } else if (pBuffer[1] == RECEIVE_PACKET_SOF) {
1501 /* Read the HEADR of Two bytes*/
1502 ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
1503 pBuffer[0] = RECEIVE_PACKET_SOF;
1504 numBytesToRead = 2;
1505 headerIndex = 0;
1506 break;
1507 }
1508 /*If it is Chained packet wait for 100 usec*/
1509 if (poll_sof_chained_delay == 1) {
1510 ALOGD_IF(ese_debug_enabled, "%s Chained Pkt, delay read %dus",
1511 __FUNCTION__, GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1512 phPalEse_sleep(GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1513 } else {
1514 ALOGD_IF(ese_debug_enabled, "%s Normal Pkt, delay read %dus",
1515 __FUNCTION__, GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1516 phPalEse_sleep(GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1517 }
1518 } while (sof_counter < ESE_NAD_POLLING_MAX);
1519 if (pBuffer[0] == RECEIVE_PACKET_SOF) {
1520 ALOGD_IF(ese_debug_enabled, "%s SOF FOUND", __FUNCTION__);
1521 /* Read the HEADR of one/Two bytes based on how two bytes read A5 PCB or
1522 * 00 A5*/
1523 ret = phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
1524 if (ret < 0) {
1525 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1526 }
1527 if ((pBuffer[1] == CHAINED_PACKET_WITHOUTSEQN) ||
1528 (pBuffer[1] == CHAINED_PACKET_WITHSEQN)) {
1529 poll_sof_chained_delay = 1;
1530 ALOGD_IF(ese_debug_enabled, "poll_sof_chained_delay value is %d ",
1531 poll_sof_chained_delay);
1532 } else {
1533 poll_sof_chained_delay = 0;
1534 ALOGD_IF(ese_debug_enabled, "poll_sof_chained_delay value is %d ",
1535 poll_sof_chained_delay);
1536 }
1537 total_count = 3;
1538 nNbBytesToRead = (int)pBuffer[2];
1539 /* Read the Complete data + one byte CRC*/
1540 ret = phPalEse_read(pDevHandle, &pBuffer[3], (nNbBytesToRead + 1));
1541 if (ret < 0) {
1542 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1543 ret = -1;
1544 } else {
1545 ret = (total_count + (nNbBytesToRead + 1));
1546 }
1547 } else if (ret < 0) {
1548 /*In case of IO Error*/
1549 ret = -2;
1550 pBuffer[0] = 0x64;
1551 pBuffer[1] = 0xFF;
1552 } else {
1553 ret = -1;
1554 }
1555 return ret;
1556 }
1557
1558 /******************************************************************************
1559 * Function phNxpEse_WriteFrame
1560 *
1561 * Description This is the actual function which is being called by
1562 * phNxpEse_write. This function writes the data to ESE.
1563 * It waits till write callback provide the result of write
1564 * process.
1565 *
1566 * Returns It returns ESESTATUS_SUCCESS (0) if write successful else
1567 * ESESTATUS_FAILED(1)
1568 *
1569 ******************************************************************************/
phNxpEse_WriteFrame(uint32_t data_len,uint8_t * p_data)1570 ESESTATUS phNxpEse_WriteFrame(uint32_t data_len, uint8_t* p_data) {
1571 ESESTATUS status = ESESTATUS_INVALID_PARAMETER;
1572 int32_t dwNoBytesWrRd = 0;
1573 ALOGD_IF(ese_debug_enabled, "Enter %s ", __FUNCTION__);
1574 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1575 /* TODO where to set the nad id */
1576 p_data[0] = nxpese_ctxt.nadInfo.nadTx;
1577 } else {
1578 p_data[0] = ESE_NAD_TX;
1579 }
1580 /* Create local copy of cmd_data */
1581 phNxpEse_memcpy(nxpese_ctxt.p_cmd_data, p_data, data_len);
1582 nxpese_ctxt.cmd_len = data_len;
1583
1584 dwNoBytesWrRd = phPalEse_write(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_cmd_data,
1585 nxpese_ctxt.cmd_len);
1586 if (-1 == dwNoBytesWrRd) {
1587 ALOGE(" - Error in SPI Write.....%d\n", errno);
1588 status = ESESTATUS_FAILED;
1589 } else {
1590 status = ESESTATUS_SUCCESS;
1591 PH_PAL_ESE_PRINT_PACKET_TX(nxpese_ctxt.p_cmd_data, nxpese_ctxt.cmd_len);
1592 }
1593 ALOGD_IF(ese_debug_enabled, "Exit %s status %x\n", __FUNCTION__, status);
1594 return status;
1595 }
1596
1597 /******************************************************************************
1598 * Function phNxpEse_getAtr
1599 *
1600 * Description This function retrieves ATR bytes from 7816-3 layer
1601 *Update.
1602 *
1603 * Returns It returns ESESTATUS_SUCCESS (0) if write successful else
1604 * ESESTATUS_FAILED(1
1605 *
1606 ******************************************************************************/
phNxpEse_getAtr(phNxpEse_data * pATR)1607 ESESTATUS phNxpEse_getAtr(phNxpEse_data* pATR) {
1608 ESESTATUS status = ESESTATUS_FAILED;
1609 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1610 status = phNxpEseProto7816_getAtr(pATR);
1611 } else {
1612 ALOGE(" %s - Function not supported\n", __FUNCTION__);
1613 }
1614 return status;
1615 }
1616
1617 /******************************************************************************
1618 * Function phNxpEse_setIfs
1619 *
1620 * Description This function sets the IFS size to 240/254 support JCOP OS
1621 *Update.
1622 *
1623 * Returns Always return ESESTATUS_SUCCESS (0).
1624 *
1625 ******************************************************************************/
phNxpEse_setIfs(uint16_t IFS_Size)1626 ESESTATUS phNxpEse_setIfs(uint16_t IFS_Size) {
1627 phNxpEseProto7816_SetIfs(IFS_Size);
1628 return ESESTATUS_SUCCESS;
1629 }
1630
1631 /******************************************************************************
1632 * Function phNxpEse_Sleep
1633 *
1634 * Description This function suspends execution of the calling thread for
1635 * (at least) usec microseconds
1636 *
1637 * Returns Always return ESESTATUS_SUCCESS (0).
1638 *
1639 ******************************************************************************/
phNxpEse_Sleep(uint32_t usec)1640 ESESTATUS phNxpEse_Sleep(uint32_t usec) {
1641 phPalEse_sleep(usec);
1642 return ESESTATUS_SUCCESS;
1643 }
1644
1645 /******************************************************************************
1646 * Function phNxpEse_memset
1647 *
1648 * Description This function updates destination buffer with val
1649 * data in len size
1650 *
1651 * Returns Always return ESESTATUS_SUCCESS (0).
1652 *
1653 ******************************************************************************/
phNxpEse_memset(void * buff,int val,size_t len)1654 void* phNxpEse_memset(void* buff, int val, size_t len) {
1655 return phPalEse_memset(buff, val, len);
1656 }
1657
1658 /******************************************************************************
1659 * Function phNxpEse_memcpy
1660 *
1661 * Description This function copies source buffer to destination buffer
1662 * data in len size
1663 *
1664 * Returns Return pointer to allocated memory location.
1665 *
1666 ******************************************************************************/
phNxpEse_memcpy(void * dest,const void * src,size_t len)1667 void* phNxpEse_memcpy(void* dest, const void* src, size_t len) {
1668 return phPalEse_memcpy(dest, src, len);
1669 }
1670
1671 /******************************************************************************
1672 * Function phNxpEse_Memalloc
1673 *
1674 * Description This function allocation memory
1675 *
1676 * Returns Return pointer to allocated memory or NULL.
1677 *
1678 ******************************************************************************/
phNxpEse_memalloc(uint32_t size)1679 void* phNxpEse_memalloc(uint32_t size) {
1680 return phPalEse_memalloc(size);
1681 ;
1682 }
1683
1684 /******************************************************************************
1685 * Function phNxpEse_calloc
1686 *
1687 * Description This is utility function for runtime heap memory allocation
1688 *
1689 * Returns Return pointer to allocated memory or NULL.
1690 *
1691 ******************************************************************************/
phNxpEse_calloc(size_t datatype,size_t size)1692 void* phNxpEse_calloc(size_t datatype, size_t size) {
1693 return phPalEse_calloc(datatype, size);
1694 }
1695
1696 /******************************************************************************
1697 * Function phNxpEse_free
1698 *
1699 * Description This function de-allocation memory
1700 *
1701 * Returns void.
1702 *
1703 ******************************************************************************/
phNxpEse_free(void * ptr)1704 void phNxpEse_free(void* ptr) { return phPalEse_free(ptr); }
1705
1706 /******************************************************************************
1707 * Function phNxpEse_GetMaxTimer
1708 *
1709 * Description This function finds out the max. timer value returned from
1710 *JCOP
1711 *
1712 * Returns void.
1713 *
1714 ******************************************************************************/
phNxpEse_GetMaxTimer(unsigned long * pMaxTimer)1715 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer) {
1716 /* Finding the max. of the timer value */
1717 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer1;
1718 if (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer2)
1719 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer2;
1720 *pMaxTimer = (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer3)
1721 ? (nxpese_ctxt.secureTimerParams.secureTimer3)
1722 : *pMaxTimer;
1723
1724 /* Converting timer to millisecond from sec */
1725 *pMaxTimer = SECOND_TO_MILLISECOND(*pMaxTimer);
1726 /* Add extra 5% to the timer */
1727 *pMaxTimer +=
1728 CONVERT_TO_PERCENTAGE(*pMaxTimer, ADDITIONAL_SECURE_TIME_PERCENTAGE);
1729 ALOGD_IF(ese_debug_enabled, "%s Max timer value = %lu", __FUNCTION__,
1730 *pMaxTimer);
1731 return;
1732 }
1733
1734 /******************************************************************************
1735 * Function phNxpEseP61_DisablePwrCntrl
1736 *
1737 * Description This function disables eSE GPIO power off/on control
1738 * when enabled
1739 *
1740 * Returns SUCCESS/FAIL.
1741 *
1742 ******************************************************************************/
phNxpEse_DisablePwrCntrl(void)1743 ESESTATUS phNxpEse_DisablePwrCntrl(void) {
1744 ESESTATUS status = ESESTATUS_SUCCESS;
1745 unsigned long maxTimer = 0;
1746 ALOGE("%s Enter", __FUNCTION__);
1747 phNxpEse_GetMaxTimer(&maxTimer);
1748 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
1749 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
1750 if (status != ESESTATUS_SUCCESS) {
1751 ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
1752 }
1753 } else {
1754 ALOGE("%s phNxpEseP61_DisablePwrCntrl: not supported", __FUNCTION__);
1755 status = ESESTATUS_FAILED;
1756 }
1757 return status;
1758 }
1759
1760 /******************************************************************************
1761 * Function phNxpEse_getOsVersion
1762 *
1763 * Description This function returns OS version from config file &
1764 * runtime from ATR response
1765 *
1766 * Returns SUCCESS/FAIL.
1767 *
1768 ******************************************************************************/
phNxpEse_getOsVersion()1769 phNxpEse_OsVersion_t phNxpEse_getOsVersion() { return sOsVersion; }
1770
1771 /******************************************************************************
1772 * Function phNxpEse_setOsVersion
1773 *
1774 * Description This function sets chip type based on ATR response
1775 *
1776 * Returns None.
1777 *
1778 ******************************************************************************/
phNxpEse_setOsVersion(phNxpEse_OsVersion_t chipType)1779 void phNxpEse_setOsVersion(phNxpEse_OsVersion_t chipType) {
1780 sOsVersion = chipType;
1781 }
1782
1783 /******************************************************************************
1784 * Function phNxpEse_checkFWDwnldStatus
1785 *
1786 * Description This function is used to check whether FW download
1787 * is completed or not.
1788 *
1789 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_BUSY
1790 *
1791 ******************************************************************************/
phNxpEse_checkFWDwnldStatus(void)1792 static ESESTATUS phNxpEse_checkFWDwnldStatus(void) {
1793 ALOGD_IF(ese_debug_enabled, "phNxpEse_checkFWDwnldStatus Enter");
1794 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1795 spm_state_t current_spm_state = SPM_STATE_INVALID;
1796 uint8_t ese_dwnld_retry = 0x00;
1797 ESESTATUS status = ESESTATUS_FAILED;
1798
1799 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1800 if (wSpmStatus == ESESTATUS_SUCCESS) {
1801 /* Check current_spm_state and update config/Spm status*/
1802 while (ese_dwnld_retry < ESE_FW_DWNLD_RETRY_CNT) {
1803 ALOGD_IF(ese_debug_enabled, "ESE_FW_DWNLD_RETRY_CNT retry count");
1804 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1805 if (wSpmStatus == ESESTATUS_SUCCESS) {
1806 if ((current_spm_state & SPM_STATE_DWNLD)) {
1807 status = ESESTATUS_FAILED;
1808 } else {
1809 ALOGE("Exit polling no FW Download ..");
1810 status = ESESTATUS_SUCCESS;
1811 break;
1812 }
1813 } else {
1814 status = ESESTATUS_FAILED;
1815 break;
1816 }
1817 phNxpEse_Sleep(500000); /*sleep for 500 ms checking for fw dwnld status*/
1818 ese_dwnld_retry++;
1819 }
1820 }
1821
1822 ALOGD_IF(ese_debug_enabled, "phNxpEse_checkFWDwnldStatus status %x", status);
1823 return status;
1824 }
1825
1826 /******************************************************************************
1827 * Function phNxpEse_GetEseStatus(unsigned char *timer_buffer)
1828 *
1829 * Description This function returns the all three timer
1830 * Timeout buffer length should be minimum 18 bytes. Response will be in below
1831 format:
1832 * <0xF1><Len><Timer Value><0xF2><Len><Timer Value><0xF3><Len><Timer Value>
1833 *
1834 * Returns SUCCESS/FAIL.
1835 * ESESTATUS_SUCCESS if 0xF1 or 0xF2 tag timeout >= 0 & 0xF3 == 0
1836 * ESESTATUS_BUSY if 0xF3 tag timeout > 0
1837 * ESESTATUS_FAILED if any other error
1838
1839 ******************************************************************************/
phNxpEse_GetEseStatus(phNxpEse_data * timer_buffer)1840 ESESTATUS phNxpEse_GetEseStatus(phNxpEse_data* timer_buffer) {
1841 ESESTATUS status = ESESTATUS_FAILED;
1842
1843 phNxpEse_SecureTimer_t secureTimerParams;
1844 uint8_t* temp_timer_buffer = NULL;
1845 ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
1846 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1847 ALOGE("%s function not supported", __FUNCTION__);
1848 return status;
1849 }
1850 if (timer_buffer != NULL) {
1851 timer_buffer->len =
1852 (sizeof(secureTimerParams.secureTimer1) +
1853 sizeof(secureTimerParams.secureTimer2) +
1854 sizeof(secureTimerParams.secureTimer3)) +
1855 PH_PROPTO_7816_FRAME_LENGTH_OFFSET * PH_PROPTO_7816_FRAME_LENGTH_OFFSET;
1856 temp_timer_buffer = (uint8_t*)phNxpEse_memalloc(timer_buffer->len);
1857 timer_buffer->p_data = temp_timer_buffer;
1858
1859 phNxpEse_memcpy(&secureTimerParams, &nxpese_ctxt.secureTimerParams,
1860 sizeof(phNxpEse_SecureTimer_t));
1861
1862 ALOGD_IF(
1863 ese_debug_enabled,
1864 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x len = %d",
1865 __FUNCTION__, secureTimerParams.secureTimer1,
1866 secureTimerParams.secureTimer2, secureTimerParams.secureTimer3,
1867 timer_buffer->len);
1868
1869 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER1;
1870 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer1);
1871 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1872 temp_timer_buffer, secureTimerParams.secureTimer1);
1873 if (temp_timer_buffer != NULL) {
1874 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER2;
1875 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer2);
1876 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1877 temp_timer_buffer, secureTimerParams.secureTimer2);
1878 if (temp_timer_buffer != NULL) {
1879 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER3;
1880 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer3);
1881 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1882 temp_timer_buffer, secureTimerParams.secureTimer3);
1883 if (temp_timer_buffer != NULL) {
1884 if (secureTimerParams.secureTimer3 > 0) {
1885 status = ESESTATUS_BUSY;
1886 } else {
1887 status = ESESTATUS_SUCCESS;
1888 }
1889 }
1890 }
1891 }
1892 } else {
1893 ALOGE("%s Invalid timer buffer ", __FUNCTION__);
1894 }
1895
1896 ALOGD_IF(ese_debug_enabled, "%s Exit status = 0x%x", __FUNCTION__, status);
1897 return status;
1898 }
1899
phNxpEse_GgetTimerTlvBuffer(uint8_t * timer_buffer,unsigned int value)1900 static unsigned char* phNxpEse_GgetTimerTlvBuffer(uint8_t* timer_buffer,
1901 unsigned int value) {
1902 short int count = 0, shift = 3;
1903 unsigned int mask = 0x000000FF;
1904 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1905 ALOGE("%s function not supported", __FUNCTION__);
1906 } else {
1907 ALOGD_IF(ese_debug_enabled, "value = %x \n", value);
1908 for (count = 0; count < 4; count++) {
1909 if (timer_buffer != NULL) {
1910 *timer_buffer = (value >> (shift * 8) & mask);
1911 ALOGD_IF(ese_debug_enabled, "*timer_buffer=0x%x shift=0x%x",
1912 *timer_buffer, shift);
1913 timer_buffer++;
1914 shift--;
1915 } else {
1916 break;
1917 }
1918 }
1919 }
1920 return timer_buffer;
1921 }
1922
1923 /******************************************************************************
1924 * Function phNxpEse_NotifySEWtxRequest
1925 *
1926 * Description This function notifies SE hal service if it registers
1927 * about WTX ongoing & end status
1928 *
1929 * Returns None
1930 *
1931 ******************************************************************************/
phNxpEse_NotifySEWtxRequest(phNxpEse_wtxState state)1932 void phNxpEse_NotifySEWtxRequest(phNxpEse_wtxState state) {
1933 if (nxpese_ctxt.fPtr_WtxNtf) {
1934 (nxpese_ctxt.fPtr_WtxNtf)(state);
1935 } else {
1936 ALOGE("%s function not supported", __FUNCTION__);
1937 }
1938 }
1939
1940 /******************************************************************************
1941 * Function phNxpEse_setWtxCountLimit
1942 *
1943 * Description This function sets the counter limit for wtx
1944 *
1945 * Returns None
1946 *
1947 ******************************************************************************/
phNxpEse_setWtxCountLimit(unsigned long int wtxCount)1948 void phNxpEse_setWtxCountLimit(unsigned long int wtxCount) {
1949 app_wtx_cnt = wtxCount;
1950 }
1951