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