1 /******************************************************************************
2 *
3 * Copyright 2018-2024 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 /*NXP_LOG_ESE_D("%s Normal Pkt, delay read %dus", __FUNCTION__,
1115 WAKE_UP_DELAY_SN1xx * NAD_POLLING_SCALER_SN1xx);*/
1116 phPalEse_BusyWait(nxpese_ctxt.nadPollingRetryTime *
1117 GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1118 }
1119 sof_counter++;
1120 } while (sof_counter < max_sof_counter);
1121
1122 /*SOF Read timeout happened, go for frame retransmission*/
1123 if (sof_counter == max_sof_counter) {
1124 ret = -1;
1125 }
1126 if (ret < 0) {
1127 /*In case of IO Error*/
1128 ret = -2;
1129 pBuffer[0] = 0x64;
1130 pBuffer[1] = 0xFF;
1131 } else if ((pBuffer[0] == nxpese_ctxt.nadInfo.nadRx) ||
1132 (pBuffer[0] == RECEIVE_PACKET_SOF)) {
1133 NXP_LOG_ESE_D("%s SOF FOUND", __FUNCTION__);
1134 /* Read the HEADER of one/Two bytes based on how two bytes read A5 PCB or
1135 * 00 A5*/
1136 ret =
1137 phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
1138 if (ret < 0) {
1139 NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1140 flushData = true;
1141 } else {
1142 if ((pBuffer[1] == CHAINED_PACKET_WITHOUTSEQN) ||
1143 (pBuffer[1] == CHAINED_PACKET_WITHSEQN)) {
1144 poll_sof_chained_delay = 1;
1145 NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1146 poll_sof_chained_delay);
1147 } else {
1148 poll_sof_chained_delay = 0;
1149 NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1150 poll_sof_chained_delay);
1151 }
1152 total_count = 3;
1153 uint8_t pcb;
1154 phNxpEseProto7816_PCB_bits_t pcb_bits;
1155 pcb = pBuffer[PH_PROPTO_7816_PCB_OFFSET];
1156
1157 phNxpEse_memset(&pcb_bits, 0x00, sizeof(phNxpEseProto7816_PCB_bits_t));
1158 phNxpEse_memcpy(&pcb_bits, &pcb, sizeof(uint8_t));
1159
1160 /*For I-Frame Only*/
1161 if (0 == pcb_bits.msb) {
1162 if (pBuffer[2] != EXTENDED_FRAME_MARKER) {
1163 nNbBytesToRead = (pBuffer[2] & 0x000000FF);
1164 headerIndex = 3;
1165 } else {
1166 ret = phPalEse_read(pDevHandle, &pBuffer[3], 2);
1167 if (ret < 0) {
1168 NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1169 flushData = true;
1170 } else {
1171 nNbBytesToRead = (pBuffer[3] << 8);
1172 nNbBytesToRead = nNbBytesToRead | pBuffer[4];
1173 /*If I-Frame received with invalid length respond with RNACK*/
1174 if ((nNbBytesToRead == 0) || (nNbBytesToRead > MAX_DATA_LEN) ||
1175 (nNbBytesToRead > phNxpEseProto7816_GetIfs())) {
1176 NXP_LOG_ESE_D("I-Frame with invalid len == %d", nNbBytesToRead);
1177 flushData = true;
1178 } else {
1179 NXP_LOG_ESE_E("_spi_read() [HDR]EXTENDED_FRAME_MARKER, ret=%d",
1180 ret);
1181 total_count += 2;
1182 headerIndex = 5;
1183 }
1184 }
1185 }
1186 } else {
1187 /*For Non-IFrame*/
1188 nNbBytesToRead = (pBuffer[2] & 0x000000FF);
1189 headerIndex = 3;
1190 }
1191 if (!flushData) {
1192 /* Read the Complete data + one byte CRC*/
1193 ret = phPalEse_read(pDevHandle, &pBuffer[headerIndex],
1194 (nNbBytesToRead + 1));
1195 if (ret < 0) {
1196 NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1197 ret = -1;
1198 } else {
1199 ret = (total_count + (nNbBytesToRead + 1));
1200 }
1201 nxpese_ctxt.rnack_sent = false;
1202 }
1203 }
1204 } else {
1205 flushData = true;
1206 }
1207 if (flushData) {
1208 /* Received corrupted frame:
1209 Flushing out data in the Rx buffer so that Card can switch the mode */
1210 uint16_t ifsd_size = phNxpEseProto7816_GetIfs();
1211 uint32_t total_frame_size = 0;
1212 NXP_LOG_ESE_E("_spi_read() corrupted, IFSD size=%d flushing it out!!",
1213 ifsd_size);
1214 /* If a non-zero byte is received while polling for NAD byte and the byte
1215 is not a valid NAD byte (0xA5 or 0xB4): 1) Read & discard (without
1216 de-asserting SPI CS line) : a. Max IFSD size + 5 (remaining four
1217 prologue + one LRC bytes) bytes from eSE if max IFS size is greater
1218 than 254 bytes OR b. Max IFSD size + 3 (remaining two prologue + one
1219 LRC bytes) bytes from eSE if max IFS size is less than 255 bytes.
1220 2) Send R-NACK to request eSE to re-transmit the frame*/
1221 if (ifsd_size > IFSC_SIZE_SEND) {
1222 total_frame_size = ifsd_size + 4;
1223 } else {
1224 total_frame_size = ifsd_size + 2;
1225 }
1226 nxpese_ctxt.rnack_sent = true;
1227 phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1228 ret = phPalEse_read(pDevHandle, &pBuffer[2], total_frame_size);
1229 if (ret < 0) {
1230 NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1231 } else { /* LRC fail expected for this frame to send R-NACK*/
1232 NXP_LOG_ESE_D(
1233 "_spi_read() SUCCESS ret : %X LRC fail expected for this frame",
1234 ret);
1235 PH_PAL_ESE_PRINT_PACKET_RX(pBuffer, ret);
1236 }
1237 pBuffer[0] = 0x90;
1238 pBuffer[1] = RECEIVE_PACKET_SOF;
1239 ret = 0x02;
1240 phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1241 }
1242 } else {
1243 ret = phNxpEse_readPacket_legacy(pDevHandle, pBuffer, nNbBytesToRead);
1244 }
1245 NXP_LOG_ESE_D("%s Exit ret = %d", __FUNCTION__, ret);
1246 return ret;
1247 }
1248
1249 /******************************************************************************
1250 * Function phNxpEse_readPacket_legacy
1251 *
1252 * Description This function Reads requested number of bytes from
1253 * pn547 device into given buffer.
1254 *
1255 * Returns nNbBytesToRead- number of successfully read bytes
1256 * -1 - read operation failure
1257 *
1258 ******************************************************************************/
phNxpEse_readPacket_legacy(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)1259 static int phNxpEse_readPacket_legacy(void* pDevHandle, uint8_t* pBuffer,
1260 int nNbBytesToRead) {
1261 int ret = -1;
1262 int sof_counter = 0; /* one read may take 1 ms*/
1263 int total_count = 0, numBytesToRead = 0, headerIndex = 0;
1264 do {
1265 sof_counter++;
1266 ret = -1;
1267 ret = phPalEse_read(pDevHandle, pBuffer, 2);
1268 if (ret < 0) {
1269 /*Polling for read on spi, hence Debug log*/
1270 NXP_LOG_ESE_D("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1271 }
1272 if (pBuffer[0] == RECEIVE_PACKET_SOF) {
1273 /* Read the HEADER of one byte*/
1274 NXP_LOG_ESE_D("%s Read HDR", __FUNCTION__);
1275 numBytesToRead = 1;
1276 headerIndex = 1;
1277 break;
1278 } else if (pBuffer[1] == RECEIVE_PACKET_SOF) {
1279 /* Read the HEADER of Two bytes*/
1280 NXP_LOG_ESE_D("%s Read HDR", __FUNCTION__);
1281 pBuffer[0] = RECEIVE_PACKET_SOF;
1282 numBytesToRead = 2;
1283 headerIndex = 0;
1284 break;
1285 }
1286 /*If it is Chained packet wait for 100 usec*/
1287 if (poll_sof_chained_delay == 1) {
1288 NXP_LOG_ESE_D("%s Chained Pkt, delay read %dus", __FUNCTION__,
1289 GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1290 phPalEse_sleep(GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1291 } else {
1292 NXP_LOG_ESE_D("%s Normal Pkt, delay read %dus", __FUNCTION__,
1293 GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1294 phPalEse_sleep(GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1295 }
1296 } while (sof_counter < ESE_NAD_POLLING_MAX);
1297 if (pBuffer[0] == RECEIVE_PACKET_SOF) {
1298 NXP_LOG_ESE_D("%s SOF FOUND", __FUNCTION__);
1299 /* Read the HEADER of one/Two bytes based on how two bytes read A5 PCB or
1300 * 00 A5*/
1301 ret = phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
1302 if (ret < 0) {
1303 NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1304 }
1305 if ((pBuffer[1] == CHAINED_PACKET_WITHOUTSEQN) ||
1306 (pBuffer[1] == CHAINED_PACKET_WITHSEQN)) {
1307 poll_sof_chained_delay = 1;
1308 NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1309 poll_sof_chained_delay);
1310 } else {
1311 poll_sof_chained_delay = 0;
1312 NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1313 poll_sof_chained_delay);
1314 }
1315 total_count = 3;
1316 nNbBytesToRead = (pBuffer[2] & 0x000000FF);
1317 /* Read the Complete data + one byte CRC*/
1318 ret = phPalEse_read(pDevHandle, &pBuffer[3], (nNbBytesToRead + 1));
1319 if (ret < 0) {
1320 NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1321 ret = -1;
1322 } else {
1323 ret = (total_count + (nNbBytesToRead + 1));
1324 }
1325 } else if (ret < 0) {
1326 /*In case of IO Error*/
1327 ret = -2;
1328 pBuffer[0] = 0x64;
1329 pBuffer[1] = 0xFF;
1330 } else {
1331 ret = -1;
1332 }
1333 return ret;
1334 }
1335
1336 /******************************************************************************
1337 * Function phNxpEse_WriteFrame
1338 *
1339 * Description This is the actual function which is being called by
1340 * phNxpEse_write. This function writes the data to ESE.
1341 * It waits till write callback provide the result of write
1342 * process.
1343 *
1344 * Returns It returns ESESTATUS_SUCCESS (0) if write successful else
1345 * ESESTATUS_FAILED(1)
1346 *
1347 ******************************************************************************/
phNxpEse_WriteFrame(uint32_t data_len,uint8_t * p_data)1348 ESESTATUS phNxpEse_WriteFrame(uint32_t data_len, uint8_t* p_data) {
1349 if (data_len > MAX_DATA_LEN || data_len == 0) {
1350 ALOGE("%s Data length causes oob write error", __FUNCTION__);
1351 return ESESTATUS_FAILED;
1352 }
1353 ESESTATUS status = ESESTATUS_INVALID_PARAMETER;
1354 int32_t dwNoBytesWrRd = 0;
1355 NXP_LOG_ESE_D("Enter %s ", __FUNCTION__);
1356 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1357 /* TODO where to set the nad id */
1358 p_data[0] = nxpese_ctxt.nadInfo.nadTx;
1359 } else {
1360 p_data[0] = ESE_NAD_TX;
1361 }
1362 /* Create local copy of cmd_data */
1363 phNxpEse_memcpy(nxpese_ctxt.p_cmd_data, p_data, data_len);
1364 nxpese_ctxt.cmd_len = data_len;
1365
1366 if (GET_CHIP_OS_VERSION() < OS_VERSION_8_9) {
1367 // eSE requires around 200 usec to switch from tx to rx mode
1368 // As per the observation, debug logs when enabled introduces around
1369 // same amount of delay, therefore below explicit delay is required
1370 // only if debug logs are disabled
1371 if (ese_log_level < NXPESE_LOGLEVEL_DEBUG) phPalEse_BusyWait(200 /*usecs*/);
1372 }
1373
1374 dwNoBytesWrRd = phPalEse_write(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_cmd_data,
1375 nxpese_ctxt.cmd_len);
1376 if (-1 == dwNoBytesWrRd) {
1377 NXP_LOG_ESE_E(" - Error in SPI Write.....%d\n", errno);
1378 status = ESESTATUS_FAILED;
1379 } else {
1380 status = ESESTATUS_SUCCESS;
1381 PH_PAL_ESE_PRINT_PACKET_TX(nxpese_ctxt.p_cmd_data, nxpese_ctxt.cmd_len);
1382 }
1383 NXP_LOG_ESE_I("Exit %s status %x\n", __FUNCTION__, status);
1384 return status;
1385 }
1386
1387 /******************************************************************************
1388 * Function phNxpEse_getAtr
1389 *
1390 * Description This function retrieves ATR bytes from 7816-3 layer
1391 *Update.
1392 *
1393 * Returns It returns ESESTATUS_SUCCESS (0) if write successful else
1394 * ESESTATUS_FAILED(1
1395 *
1396 ******************************************************************************/
phNxpEse_getAtr(phNxpEse_data * pATR)1397 ESESTATUS phNxpEse_getAtr(phNxpEse_data* pATR) {
1398 ESESTATUS status = ESESTATUS_FAILED;
1399 if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1400 status = phNxpEseProto7816_getAtr(pATR);
1401 } else {
1402 NXP_LOG_ESE_E(" %s - Function not supported\n", __FUNCTION__);
1403 }
1404 return status;
1405 }
1406
1407 /******************************************************************************
1408 * Function phNxpEse_setIfs
1409 *
1410 * Description This function sets the IFS size to 240/254 support JCOP OS
1411 *Update.
1412 *
1413 * Returns Always return ESESTATUS_SUCCESS (0).
1414 *
1415 ******************************************************************************/
phNxpEse_setIfs(uint16_t IFS_Size)1416 ESESTATUS phNxpEse_setIfs(uint16_t IFS_Size) {
1417 phNxpEseProto7816_SetIfs(IFS_Size);
1418 return ESESTATUS_SUCCESS;
1419 }
1420
1421 /******************************************************************************
1422 * Function phNxpEse_Sleep
1423 *
1424 * Description This function suspends execution of the calling thread for
1425 * (at least) usec microseconds
1426 *
1427 * Returns Always return ESESTATUS_SUCCESS (0).
1428 *
1429 ******************************************************************************/
phNxpEse_Sleep(uint32_t usec)1430 ESESTATUS phNxpEse_Sleep(uint32_t usec) {
1431 phPalEse_sleep(usec);
1432 return ESESTATUS_SUCCESS;
1433 }
1434
1435 /******************************************************************************
1436 * Function phNxpEse_memset
1437 *
1438 * Description This function updates destination buffer with val
1439 * data in len size
1440 *
1441 * Returns Always return ESESTATUS_SUCCESS (0).
1442 *
1443 ******************************************************************************/
phNxpEse_memset(void * buff,int val,size_t len)1444 void* phNxpEse_memset(void* buff, int val, size_t len) {
1445 return phPalEse_memset(buff, val, len);
1446 }
1447
1448 /******************************************************************************
1449 * Function phNxpEse_memcpy
1450 *
1451 * Description This function copies source buffer to destination buffer
1452 * data in len size
1453 *
1454 * Returns Return pointer to allocated memory location.
1455 *
1456 ******************************************************************************/
phNxpEse_memcpy(void * dest,const void * src,size_t len)1457 void* phNxpEse_memcpy(void* dest, const void* src, size_t len) {
1458 return phPalEse_memcpy(dest, src, len);
1459 }
1460
1461 /******************************************************************************
1462 * Function phNxpEse_Memalloc
1463 *
1464 * Description This function allocation memory
1465 *
1466 * Returns Return pointer to allocated memory or NULL.
1467 *
1468 ******************************************************************************/
phNxpEse_memalloc(uint32_t size)1469 void* phNxpEse_memalloc(uint32_t size) {
1470 return phPalEse_memalloc(size);
1471 ;
1472 }
1473
1474 /******************************************************************************
1475 * Function phNxpEse_calloc
1476 *
1477 * Description This is utility function for runtime heap memory allocation
1478 *
1479 * Returns Return pointer to allocated memory or NULL.
1480 *
1481 ******************************************************************************/
phNxpEse_calloc(size_t datatype,size_t size)1482 void* phNxpEse_calloc(size_t datatype, size_t size) {
1483 return phPalEse_calloc(datatype, size);
1484 }
1485
1486 /******************************************************************************
1487 * Function phNxpEse_free
1488 *
1489 * Description This function de-allocation memory
1490 *
1491 * Returns void.
1492 *
1493 ******************************************************************************/
phNxpEse_free(void * ptr)1494 void phNxpEse_free(void* ptr) { return phPalEse_free(ptr); }
1495
1496 /******************************************************************************
1497 * Function phNxpEse_GetMaxTimer
1498 *
1499 * Description This function finds out the max. timer value returned from
1500 *JCOP
1501 *
1502 * Returns void.
1503 *
1504 ******************************************************************************/
phNxpEse_GetMaxTimer(unsigned long * pMaxTimer)1505 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer) {
1506 /* Finding the max. of the timer value */
1507 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer1;
1508 if (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer2)
1509 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer2;
1510 *pMaxTimer = (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer3)
1511 ? (nxpese_ctxt.secureTimerParams.secureTimer3)
1512 : *pMaxTimer;
1513
1514 /* Converting timer to millisecond from sec */
1515 *pMaxTimer = SECOND_TO_MILLISECOND(*pMaxTimer);
1516 /* Add extra 5% to the timer */
1517 *pMaxTimer +=
1518 CONVERT_TO_PERCENTAGE(*pMaxTimer, ADDITIONAL_SECURE_TIME_PERCENTAGE);
1519 NXP_LOG_ESE_D("%s Max timer value = %lu", __FUNCTION__, *pMaxTimer);
1520 return;
1521 }
1522
1523 /******************************************************************************
1524 * Function phNxpEse_getOsVersion
1525 *
1526 * Description This function returns OS version from config file &
1527 * runtime from ATR response
1528 *
1529 * Returns SUCCESS/FAIL.
1530 *
1531 ******************************************************************************/
phNxpEse_getOsVersion()1532 phNxpEse_OsVersion_t phNxpEse_getOsVersion() { return sOsVersion; }
1533
1534 /******************************************************************************
1535 * Function phNxpEse_setOsVersion
1536 *
1537 * Description This function sets chip type based on ATR response
1538 *
1539 * Returns None.
1540 *
1541 ******************************************************************************/
phNxpEse_setOsVersion(phNxpEse_OsVersion_t chipType)1542 void phNxpEse_setOsVersion(phNxpEse_OsVersion_t chipType) {
1543 sOsVersion = chipType;
1544 }
1545
1546 /******************************************************************************
1547 * Function phNxpEse_checkFWDwnldStatus
1548 *
1549 * Description This function is used to check whether FW download
1550 * is completed or not.
1551 *
1552 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_BUSY
1553 *
1554 ******************************************************************************/
phNxpEse_checkFWDwnldStatus(void)1555 static ESESTATUS phNxpEse_checkFWDwnldStatus(void) {
1556 NXP_LOG_ESE_D("phNxpEse_checkFWDwnldStatus Enter");
1557 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1558 spm_state_t current_spm_state = SPM_STATE_INVALID;
1559 uint8_t ese_dwnld_retry = 0x00;
1560 ESESTATUS status = ESESTATUS_FAILED;
1561
1562 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1563 if (wSpmStatus == ESESTATUS_SUCCESS) {
1564 /* Check current_spm_state and update config/Spm status*/
1565 while (ese_dwnld_retry < ESE_FW_DWNLD_RETRY_CNT) {
1566 NXP_LOG_ESE_D("ESE_FW_DWNLD_RETRY_CNT retry count");
1567 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1568 if (wSpmStatus == ESESTATUS_SUCCESS) {
1569 if ((current_spm_state & SPM_STATE_DWNLD)) {
1570 status = ESESTATUS_FAILED;
1571 } else {
1572 NXP_LOG_ESE_E("Exit polling no FW Download ..");
1573 status = ESESTATUS_SUCCESS;
1574 break;
1575 }
1576 } else {
1577 status = ESESTATUS_FAILED;
1578 break;
1579 }
1580 phNxpEse_Sleep(500000); /*sleep for 500 ms checking for fw dwnld status*/
1581 ese_dwnld_retry++;
1582 }
1583 }
1584
1585 NXP_LOG_ESE_D("phNxpEse_checkFWDwnldStatus status %x", status);
1586 return status;
1587 }
1588
1589 /******************************************************************************
1590 * Function phNxpEse_NotifySEWtxRequest
1591 *
1592 * Description This function notifies SE hal service if it registers
1593 * about WTX ongoing & end status
1594 *
1595 * Returns None
1596 *
1597 ******************************************************************************/
phNxpEse_NotifySEWtxRequest(phNxpEse_wtxState state)1598 void phNxpEse_NotifySEWtxRequest(phNxpEse_wtxState state) {
1599 if (nxpese_ctxt.fPtr_WtxNtf) {
1600 (nxpese_ctxt.fPtr_WtxNtf)(state);
1601 } else {
1602 NXP_LOG_ESE_E("%s function not supported", __FUNCTION__);
1603 }
1604 }
1605
1606 /******************************************************************************
1607 * Function phNxpEse_setWtxCountLimit
1608 *
1609 * Description This function sets the counter limit for wtx
1610 *
1611 * Returns None
1612 *
1613 ******************************************************************************/
phNxpEse_setWtxCountLimit(unsigned long int wtxCount)1614 void phNxpEse_setWtxCountLimit(unsigned long int wtxCount) {
1615 app_wtx_cnt = wtxCount;
1616 }
1617
1618 /******************************************************************************
1619 * Function phNxpEse_isPriorityAccessEnabled
1620 *
1621 * Description This function returns whether priority channel enabled or
1622 * not.
1623 *
1624 * Returns Priority Access enabled(1)/disabled(0).
1625 *
1626 ******************************************************************************/
phNxpEse_isPriorityAccessEnabled(void)1627 bool phNxpEse_isPriorityAccessEnabled(void) {
1628 uint8_t isPriorityAccess = 0;
1629 if (EseConfig::hasKey(NAME_NXP_SE_PRIORITY_ACCESS)) {
1630 isPriorityAccess = EseConfig::getUnsigned(NAME_NXP_SE_PRIORITY_ACCESS);
1631 }
1632 NXP_LOG_ESE_D("Reserve channel enabled = %d", isPriorityAccess);
1633 return (isPriorityAccess != 0);
1634 }
1635