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 <log/log.h>
20
21 #include <cutils/properties.h>
22 #include <ese_config.h>
23 #include <phNxpEseFeatures.h>
24 #include <phNxpEsePal.h>
25 #include <phNxpEsePal_spi.h>
26 #include <phNxpEseProto7816_3.h>
27 #include <phNxpEse_Internal.h>
28
29 #define RECIEVE_PACKET_SOF 0xA5
30 #define PH_PAL_ESE_PRINT_PACKET_TX(data, len) \
31 ({ phPalEse_print_packet("SEND", data, len); })
32 #define PH_PAL_ESE_PRINT_PACKET_RX(data, len) \
33 ({ phPalEse_print_packet("RECV", data, len); })
34 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
35 int nNbBytesToRead);
36 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
37 static ESESTATUS phNxpEse_checkJcopDwnldState(void);
38 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state);
39 #endif
40 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
41 static ESESTATUS phNxpEse_checkFWDwnldStatus(void);
42 #endif
43 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer);
44 #ifdef NXP_SECURE_TIMER_SESSION
45 static unsigned char* phNxpEse_GgetTimerTlvBuffer(unsigned char* timer_buffer,
46 unsigned int value);
47 #endif
48 /*********************** Global Variables *************************************/
49
50 /* ESE Context structure */
51 phNxpEse_Context_t nxpese_ctxt;
52 bool ese_debug_enabled = true;
53
54 /******************************************************************************
55 * Function phNxpLog_InitializeLogLevel
56 *
57 * Description This function is called during phNxpEse_init to initialize
58 * debug log level.
59 *
60 * Returns None
61 *
62 ******************************************************************************/
63
phNxpLog_InitializeLogLevel()64 void phNxpLog_InitializeLogLevel() {
65 ese_debug_enabled =
66 (EseConfig::getUnsigned(NAME_SE_DEBUG_ENABLED, 0) != 0) ? true : false;
67
68 char valueStr[PROPERTY_VALUE_MAX] = {0};
69 int len = property_get("vendor.ese.debug_enabled", valueStr, "");
70 if (len > 0) {
71 // let Android property override .conf variable
72 unsigned debug_enabled = 0;
73 sscanf(valueStr, "%u", &debug_enabled);
74 ese_debug_enabled = (debug_enabled == 0) ? false : true;
75 }
76
77 ALOGD_IF(ese_debug_enabled, "%s: level=%u", __func__, ese_debug_enabled);
78 }
79
80 /******************************************************************************
81 * Function phNxpEse_init
82 *
83 * Description This function is called by Jni/phNxpEse_open during the
84 * initialization of the ESE. It initializes protocol stack
85 *instance variable
86 *
87 * Returns This function return ESESTATUS_SUCCES (0) in case of success
88 * In case of failure returns other failure value.
89 *
90 ******************************************************************************/
phNxpEse_init(phNxpEse_initParams initParams)91 ESESTATUS phNxpEse_init(phNxpEse_initParams initParams) {
92 ESESTATUS wConfigStatus = ESESTATUS_FAILED;
93 unsigned long int num;
94 unsigned long maxTimer = 0;
95 phNxpEseProto7816InitParam_t protoInitParam;
96 phNxpEse_memset(&protoInitParam, 0x00, sizeof(phNxpEseProto7816InitParam_t));
97 /* STATUS_OPEN */
98 nxpese_ctxt.EseLibStatus = ESE_STATUS_OPEN;
99
100 if (EseConfig::hasKey(NAME_NXP_WTX_COUNT_VALUE)) {
101 num = EseConfig::getUnsigned(NAME_NXP_WTX_COUNT_VALUE);
102 protoInitParam.wtx_counter_limit = num;
103 ALOGD_IF(ese_debug_enabled, "Wtx_counter read from config file - %lu",
104 protoInitParam.wtx_counter_limit);
105 } else {
106 protoInitParam.wtx_counter_limit = PH_PROTO_WTX_DEFAULT_COUNT;
107 }
108 if (EseConfig::hasKey(NAME_NXP_MAX_RNACK_RETRY)) {
109 protoInitParam.rnack_retry_limit =
110 EseConfig::getUnsigned(NAME_NXP_MAX_RNACK_RETRY);
111 } else {
112 protoInitParam.rnack_retry_limit = MAX_RNACK_RETRY_LIMIT;
113 }
114 if (ESE_MODE_NORMAL ==
115 initParams.initMode) /* TZ/Normal wired mode should come here*/
116 {
117 if (EseConfig::hasKey(NAME_NXP_SPI_INTF_RST_ENABLE)) {
118 protoInitParam.interfaceReset =
119 (EseConfig::getUnsigned(NAME_NXP_SPI_INTF_RST_ENABLE) == 1) ? true
120 : false;
121 } else {
122 protoInitParam.interfaceReset = true;
123 }
124 } else /* OSU mode, no interface reset is required */
125 {
126 protoInitParam.interfaceReset = false;
127 }
128 /* Sharing lib context for fetching secure timer values */
129 protoInitParam.pSecureTimerParams =
130 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams;
131
132 ALOGD_IF(ese_debug_enabled,
133 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
134 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
135 nxpese_ctxt.secureTimerParams.secureTimer2,
136 nxpese_ctxt.secureTimerParams.secureTimer3);
137
138 phNxpEse_GetMaxTimer(&maxTimer);
139
140 /* T=1 Protocol layer open */
141 wConfigStatus = phNxpEseProto7816_Open(protoInitParam);
142 if (ESESTATUS_FAILED == wConfigStatus) {
143 wConfigStatus = ESESTATUS_FAILED;
144 ALOGE("phNxpEseProto7816_Open failed");
145 }
146 return wConfigStatus;
147 }
148
149 /******************************************************************************
150 * Function phNxpEse_open
151 *
152 * Description This function is called by Jni during the
153 * initialization of the ESE. It opens the physical connection
154 * with ESE and creates required client thread for
155 * operation.
156 * Returns This function return ESESTATUS_SUCCES (0) in case of success
157 * In case of failure returns other failure value.
158 *
159 ******************************************************************************/
phNxpEse_open(phNxpEse_initParams initParams)160 ESESTATUS phNxpEse_open(phNxpEse_initParams initParams) {
161 phPalEse_Config_t tPalConfig;
162 ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
163 unsigned long int tpm_enable = 0;
164 char ese_dev_node[64];
165 std::string ese_node;
166 #ifdef SPM_INTEGRATED
167 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
168 spm_state_t current_spm_state = SPM_STATE_INVALID;
169 #endif
170
171 ALOGE("phNxpEse_open Enter");
172 /*When spi channel is already opened return status as FAILED*/
173 if (nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE) {
174 ALOGD_IF(ese_debug_enabled, "already opened\n");
175 return ESESTATUS_BUSY;
176 }
177
178 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
179 phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
180
181 ALOGE("MW SEAccessKit Version");
182 ALOGE("Android Version:0x%x", NXP_ANDROID_VER);
183 ALOGE("Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
184 ALOGE("Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
185
186 if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
187 tpm_enable = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
188 ALOGE(
189 "SPI Throughput measurement enable/disable read from config file - %lu",
190 tpm_enable);
191 } else {
192 ALOGE("SPI Throughput not defined in config file - %lu", tpm_enable);
193 }
194 #ifdef NXP_POWER_SCHEME_SUPPORT
195 unsigned long int num = 0;
196 if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
197 num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
198 nxpese_ctxt.pwr_scheme = num;
199 ALOGE("Power scheme read from config file - %lu", num);
200 } else {
201 nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
202 ALOGE("Power scheme not defined in config file - %lu", num);
203 }
204 #else
205 nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
206 tpm_enable = 0x00;
207 #endif
208 /* initialize trace level */
209 phNxpLog_InitializeLogLevel();
210
211 /*Read device node path*/
212 ese_node = EseConfig::getString(NAME_NXP_ESE_DEV_NODE, "/dev/pn81a");
213 strlcpy(ese_dev_node, ese_node.c_str(), sizeof(ese_dev_node));
214 tPalConfig.pDevName = (int8_t*)ese_dev_node;
215
216 /* Initialize PAL layer */
217 wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
218 if (wConfigStatus != ESESTATUS_SUCCESS) {
219 ALOGE("phPalEse_Init Failed");
220 goto clean_and_return;
221 }
222 /* Copying device handle to ESE Lib context*/
223 nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
224
225 #ifdef SPM_INTEGRATED
226 /* Get the Access of ESE*/
227 wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
228 if (wSpmStatus != ESESTATUS_SUCCESS) {
229 ALOGE("phNxpEse_SPM_Init Failed");
230 wConfigStatus = ESESTATUS_FAILED;
231 goto clean_and_return_2;
232 }
233 wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
234 if (wSpmStatus != ESESTATUS_SUCCESS) {
235 ALOGE(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
236 wConfigStatus = ESESTATUS_FAILED;
237 goto clean_and_return_1;
238 }
239 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
240 wConfigStatus = phNxpEse_checkFWDwnldStatus();
241 if (wConfigStatus != ESESTATUS_SUCCESS) {
242 ALOGD_IF(ese_debug_enabled,
243 "Failed to open SPI due to VEN pin used by FW download \n");
244 wConfigStatus = ESESTATUS_FAILED;
245 goto clean_and_return_1;
246 }
247 #endif
248 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
249 if (wSpmStatus != ESESTATUS_SUCCESS) {
250 ALOGE(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
251 wConfigStatus = ESESTATUS_FAILED;
252 goto clean_and_return_1;
253 } else {
254 if ((current_spm_state & SPM_STATE_SPI) |
255 (current_spm_state & SPM_STATE_SPI_PRIO)) {
256 ALOGE(" %s : SPI is already opened...second instance not allowed",
257 __FUNCTION__);
258 wConfigStatus = ESESTATUS_FAILED;
259 goto clean_and_return_1;
260 }
261 }
262 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
263 if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
264 ALOGE(" %s : Denying to open JCOP Download in progress", __FUNCTION__);
265 wConfigStatus = ESESTATUS_FAILED;
266 goto clean_and_return_1;
267 }
268 #endif
269 phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams,
270 sizeof(phNxpEse_initParams));
271 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
272 /* Updating ESE power state based on the init mode */
273 if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
274 ALOGE("%s Init mode ---->OSU", __FUNCTION__);
275 wConfigStatus = phNxpEse_checkJcopDwnldState();
276 if (wConfigStatus != ESESTATUS_SUCCESS) {
277 ALOGE("phNxpEse_checkJcopDwnldState failed");
278 goto clean_and_return_1;
279 }
280 }
281 #endif
282 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_ENABLE);
283 if (wSpmStatus != ESESTATUS_SUCCESS) {
284 ALOGE("phNxpEse_SPM_ConfigPwr: enabling power Failed");
285 if (wSpmStatus == ESESTATUS_BUSY) {
286 wConfigStatus = ESESTATUS_BUSY;
287 } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
288 wConfigStatus = ESESTATUS_DWNLD_BUSY;
289 } else {
290 wConfigStatus = ESESTATUS_FAILED;
291 }
292 goto clean_and_return;
293 } else {
294 ALOGD_IF(ese_debug_enabled, "nxpese_ctxt.spm_power_state true");
295 nxpese_ctxt.spm_power_state = true;
296 }
297 #endif
298
299 ALOGD_IF(ese_debug_enabled, "wConfigStatus %x", wConfigStatus);
300 return wConfigStatus;
301
302 clean_and_return:
303 #ifdef SPM_INTEGRATED
304 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
305 if (wSpmStatus != ESESTATUS_SUCCESS) {
306 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
307 }
308 clean_and_return_1:
309 phNxpEse_SPM_DeInit();
310 clean_and_return_2:
311 #endif
312 if (NULL != nxpese_ctxt.pDevHandle) {
313 phPalEse_close(nxpese_ctxt.pDevHandle);
314 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
315 }
316 nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
317 nxpese_ctxt.spm_power_state = false;
318 return ESESTATUS_FAILED;
319 }
320
321 /******************************************************************************
322 * \ingroup spi_libese
323 *
324 * \brief Check if libese has opened
325 *
326 * \retval return false if it is close, otherwise true.
327 *
328 ******************************************************************************/
phNxpEse_isOpen()329 bool phNxpEse_isOpen() { return nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE; }
330
331 /******************************************************************************
332 * Function phNxpEse_openPrioSession
333 *
334 * Description This function is called by Jni during the
335 * initialization of the ESE. It opens the physical connection
336 * with ESE () and creates required client thread for
337 * operation. This will get priority access to ESE for timeout
338 duration.
339
340 * Returns This function return ESESTATUS_SUCCES (0) in case of success
341 * In case of failure returns other failure value.
342 *
343 ******************************************************************************/
phNxpEse_openPrioSession(phNxpEse_initParams initParams)344 ESESTATUS phNxpEse_openPrioSession(phNxpEse_initParams initParams) {
345 phPalEse_Config_t tPalConfig;
346 ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
347 unsigned long int num = 0;
348
349 ALOGE("phNxpEse_openPrioSession Enter");
350 #ifdef SPM_INTEGRATED
351 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
352 spm_state_t current_spm_state = SPM_STATE_INVALID;
353 #endif
354 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
355 phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
356
357 ALOGE("MW SEAccessKit Version");
358 ALOGE("Android Version:0x%x", NXP_ANDROID_VER);
359 ALOGE("Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
360 ALOGE("Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
361
362 #ifdef NXP_POWER_SCHEME_SUPPORT
363 if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
364 num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
365 nxpese_ctxt.pwr_scheme = num;
366 ALOGE("Power scheme read from config file - %lu", num);
367 } else
368 #endif
369 {
370 nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
371 ALOGE("Power scheme not defined in config file - %lu", num);
372 }
373 if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
374 num = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
375 ALOGE(
376 "SPI Throughput measurement enable/disable read from config file - %lu",
377 num);
378 } else {
379 ALOGE("SPI Throughput not defined in config file - %lu", num);
380 }
381 /* initialize trace level */
382 phNxpLog_InitializeLogLevel();
383
384 tPalConfig.pDevName = (int8_t*)"/dev/p73";
385
386 /* Initialize PAL layer */
387 wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
388 if (wConfigStatus != ESESTATUS_SUCCESS) {
389 ALOGE("phPalEse_Init Failed");
390 goto clean_and_return;
391 }
392 /* Copying device handle to hal context*/
393 nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
394
395 #ifdef SPM_INTEGRATED
396 /* Get the Access of ESE*/
397 wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
398 if (wSpmStatus != ESESTATUS_SUCCESS) {
399 ALOGE("phNxpEse_SPM_Init Failed");
400 wConfigStatus = ESESTATUS_FAILED;
401 goto clean_and_return_2;
402 }
403 wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
404 if (wSpmStatus != ESESTATUS_SUCCESS) {
405 ALOGE(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
406 wConfigStatus = ESESTATUS_FAILED;
407 goto clean_and_return_1;
408 }
409 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
410 if (wSpmStatus != ESESTATUS_SUCCESS) {
411 ALOGE(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
412 wConfigStatus = ESESTATUS_FAILED;
413 goto clean_and_return_1;
414 } else {
415 if ((current_spm_state & SPM_STATE_SPI) |
416 (current_spm_state & SPM_STATE_SPI_PRIO)) {
417 ALOGE(" %s : SPI is already opened...second instance not allowed",
418 __FUNCTION__);
419 wConfigStatus = ESESTATUS_FAILED;
420 goto clean_and_return_1;
421 }
422 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
423 if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
424 ALOGE(" %s : Denying to open JCOP Download in progress", __FUNCTION__);
425 wConfigStatus = ESESTATUS_FAILED;
426 goto clean_and_return_1;
427 }
428 #endif
429 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
430 wConfigStatus = phNxpEse_checkFWDwnldStatus();
431 if (wConfigStatus != ESESTATUS_SUCCESS) {
432 ALOGD_IF(ese_debug_enabled,
433 "Failed to open SPI due to VEN pin used by FW download \n");
434 wConfigStatus = ESESTATUS_FAILED;
435 goto clean_and_return_1;
436 }
437 #endif
438 }
439 phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams.initMode,
440 sizeof(phNxpEse_initParams));
441 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
442 /* Updating ESE power state based on the init mode */
443 if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
444 wConfigStatus = phNxpEse_checkJcopDwnldState();
445 if (wConfigStatus != ESESTATUS_SUCCESS) {
446 ALOGE("phNxpEse_checkJcopDwnldState failed");
447 goto clean_and_return_1;
448 }
449 }
450 #endif
451 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_PRIO_ENABLE);
452 if (wSpmStatus != ESESTATUS_SUCCESS) {
453 ALOGE("phNxpEse_SPM_ConfigPwr: enabling power for spi prio Failed");
454 if (wSpmStatus == ESESTATUS_BUSY) {
455 wConfigStatus = ESESTATUS_BUSY;
456 } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
457 wConfigStatus = ESESTATUS_DWNLD_BUSY;
458 } else {
459 wConfigStatus = ESESTATUS_FAILED;
460 }
461 goto clean_and_return;
462 } else {
463 ALOGE("nxpese_ctxt.spm_power_state true");
464 nxpese_ctxt.spm_power_state = true;
465 }
466 #endif
467
468 #ifndef SPM_INTEGRATED
469 wConfigStatus =
470 phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
471 if (wConfigStatus != ESESTATUS_SUCCESS) {
472 ALOGE("phPalEse_IoCtl Failed");
473 goto clean_and_return;
474 }
475 #endif
476 wConfigStatus =
477 phPalEse_ioctl(phPalEse_e_EnableLog, nxpese_ctxt.pDevHandle, 0);
478 if (wConfigStatus != ESESTATUS_SUCCESS) {
479 ALOGE("phPalEse_IoCtl Failed");
480 goto clean_and_return;
481 }
482 wConfigStatus =
483 phPalEse_ioctl(phPalEse_e_EnablePollMode, nxpese_ctxt.pDevHandle, 1);
484 if (wConfigStatus != ESESTATUS_SUCCESS) {
485 ALOGE("phPalEse_IoCtl Failed");
486 goto clean_and_return;
487 }
488
489 ALOGE("wConfigStatus %x", wConfigStatus);
490
491 return wConfigStatus;
492
493 clean_and_return:
494 #ifdef SPM_INTEGRATED
495 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
496 if (wSpmStatus != ESESTATUS_SUCCESS) {
497 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
498 }
499 clean_and_return_1:
500 phNxpEse_SPM_DeInit();
501 clean_and_return_2:
502 #endif
503 if (NULL != nxpese_ctxt.pDevHandle) {
504 phPalEse_close(nxpese_ctxt.pDevHandle);
505 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
506 }
507 nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
508 nxpese_ctxt.spm_power_state = false;
509 return ESESTATUS_FAILED;
510 }
511 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
512 /******************************************************************************
513 * Function phNxpEse_setJcopDwnldState
514 *
515 * Description This function is used to check whether JCOP OS
516 * download can be started or not.
517 *
518 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_FAILED
519 *
520 ******************************************************************************/
phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state)521 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state) {
522 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
523 ESESTATUS wConfigStatus = ESESTATUS_FAILED;
524 ALOGE("phNxpEse_setJcopDwnldState Enter");
525
526 wSpmStatus = phNxpEse_SPM_SetJcopDwnldState(state);
527 if (wSpmStatus == ESESTATUS_SUCCESS) {
528 wConfigStatus = ESESTATUS_SUCCESS;
529 }
530
531 return wConfigStatus;
532 }
533
534 /******************************************************************************
535 * Function phNxpEse_checkJcopDwnldState
536 *
537 * Description This function is used to check whether JCOP OS
538 * download can be started or not.
539 *
540 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_BUSY
541 *
542 ******************************************************************************/
phNxpEse_checkJcopDwnldState(void)543 static ESESTATUS phNxpEse_checkJcopDwnldState(void) {
544 ALOGE("phNxpEse_checkJcopDwnld Enter");
545 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
546 spm_state_t current_spm_state = SPM_STATE_INVALID;
547 uint8_t ese_dwnld_retry = 0x00;
548 ESESTATUS status = ESESTATUS_FAILED;
549
550 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
551 if (wSpmStatus == ESESTATUS_SUCCESS) {
552 /* Check current_spm_state and update config/Spm status*/
553 if ((current_spm_state & SPM_STATE_JCOP_DWNLD) ||
554 (current_spm_state & SPM_STATE_WIRED))
555 return ESESTATUS_BUSY;
556
557 status = phNxpEse_setJcopDwnldState(JCP_DWNLD_INIT);
558 if (status == ESESTATUS_SUCCESS) {
559 while (ese_dwnld_retry < ESE_JCOP_OS_DWNLD_RETRY_CNT) {
560 ALOGE("ESE_JCOP_OS_DWNLD_RETRY_CNT retry count");
561 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
562 if (wSpmStatus == ESESTATUS_SUCCESS) {
563 if ((current_spm_state & SPM_STATE_JCOP_DWNLD)) {
564 status = ESESTATUS_SUCCESS;
565 break;
566 }
567 } else {
568 status = ESESTATUS_FAILED;
569 break;
570 }
571 phNxpEse_Sleep(
572 200000); /*sleep for 200 ms checking for jcop dwnld status*/
573 ese_dwnld_retry++;
574 }
575 }
576 }
577
578 ALOGE("phNxpEse_checkJcopDwnldState status %x", status);
579 return status;
580 }
581 #endif
582 /******************************************************************************
583 * Function phNxpEse_Transceive
584 *
585 * Description This function update the len and provided buffer
586 *
587 * Returns On Success ESESTATUS_SUCCESS else proper error code
588 *
589 ******************************************************************************/
phNxpEse_Transceive(phNxpEse_data * pCmd,phNxpEse_data * pRsp)590 ESESTATUS phNxpEse_Transceive(phNxpEse_data* pCmd, phNxpEse_data* pRsp) {
591 ESESTATUS status = ESESTATUS_FAILED;
592
593 if ((NULL == pCmd) || (NULL == pRsp)) return ESESTATUS_INVALID_PARAMETER;
594
595 if ((pCmd->len == 0) || pCmd->p_data == NULL) {
596 ALOGE(" phNxpEse_Transceive - Invalid Parameter no data\n");
597 return ESESTATUS_INVALID_PARAMETER;
598 } else if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
599 ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
600 return ESESTATUS_NOT_INITIALISED;
601 } else if ((ESE_STATUS_BUSY == nxpese_ctxt.EseLibStatus)) {
602 ALOGE(" %s ESE - BUSY \n", __FUNCTION__);
603 return ESESTATUS_BUSY;
604 } else {
605 nxpese_ctxt.EseLibStatus = ESE_STATUS_BUSY;
606 status = phNxpEseProto7816_Transceive((phNxpEse_data*)pCmd,
607 (phNxpEse_data*)pRsp);
608 if (ESESTATUS_SUCCESS != status) {
609 ALOGE(" %s phNxpEseProto7816_Transceive- Failed \n", __FUNCTION__);
610 }
611 nxpese_ctxt.EseLibStatus = ESE_STATUS_IDLE;
612
613 ALOGD_IF(ese_debug_enabled, " %s Exit status 0x%x \n", __FUNCTION__,
614 status);
615 return status;
616 }
617 }
618
619 /******************************************************************************
620 * Function phNxpEse_reset
621 *
622 * Description This function reset the ESE interface and free all
623 *
624 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
625 *successful else
626 * ESESTATUS_FAILED(1)
627 ******************************************************************************/
phNxpEse_reset(void)628 ESESTATUS phNxpEse_reset(void) {
629 ESESTATUS status = ESESTATUS_SUCCESS;
630 unsigned long maxTimer = 0;
631 #ifdef SPM_INTEGRATED
632 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
633 #endif
634
635 /* TBD : Call the ioctl to reset the ESE */
636 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
637 /* Do an interface reset, don't wait to see if JCOP went through a full power
638 * cycle or not */
639 ESESTATUS bStatus = phNxpEseProto7816_IntfReset(
640 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
641 if (!bStatus) status = ESESTATUS_FAILED;
642 ALOGD_IF(ese_debug_enabled,
643 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
644 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
645 nxpese_ctxt.secureTimerParams.secureTimer2,
646 nxpese_ctxt.secureTimerParams.secureTimer3);
647 phNxpEse_GetMaxTimer(&maxTimer);
648 #ifdef SPM_INTEGRATED
649 #ifdef NXP_SECURE_TIMER_SESSION
650 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
651 if (status != ESESTATUS_SUCCESS) {
652 ALOGE("%s phNxpEse_SPM_DisablePwrControl: failed", __FUNCTION__);
653 }
654 #endif
655 if ((nxpese_ctxt.pwr_scheme == PN67T_POWER_SCHEME) ||
656 (nxpese_ctxt.pwr_scheme == PN80T_LEGACY_SCHEME)) {
657 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
658 if (wSpmStatus != ESESTATUS_SUCCESS) {
659 ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
660 }
661 }
662 #else
663 /* if arg ==2 (hard reset)
664 * if arg ==1 (soft reset)
665 */
666 status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
667 if (status != ESESTATUS_SUCCESS) {
668 ALOGE("phNxpEse_reset Failed");
669 }
670 #endif
671 ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
672 return status;
673 }
674
675 /******************************************************************************
676 * Function phNxpEse_resetJcopUpdate
677 *
678 * Description This function reset the ESE interface during JCOP Update
679 *
680 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
681 *successful else
682 * ESESTATUS_FAILED(1)
683 ******************************************************************************/
phNxpEse_resetJcopUpdate(void)684 ESESTATUS phNxpEse_resetJcopUpdate(void) {
685 ESESTATUS status = ESESTATUS_SUCCESS;
686
687 #ifdef SPM_INTEGRATED
688 #ifdef NXP_POWER_SCHEME_SUPPORT
689 unsigned long int num = 0;
690 #endif
691 #endif
692
693 /* TBD : Call the ioctl to reset the */
694 ALOGD_IF(ese_debug_enabled, " %s Enter \n", __FUNCTION__);
695
696 /* Reset interface after every reset irrespective of
697 whether JCOP did a full power cycle or not. */
698 status = phNxpEseProto7816_Reset();
699
700 #ifdef SPM_INTEGRATED
701 #ifdef NXP_POWER_SCHEME_SUPPORT
702 if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
703 num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
704 if ((num == 1) || (num == 2)) {
705 ALOGD_IF(ese_debug_enabled, " %s Call Config Pwr Reset \n", __FUNCTION__);
706 status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
707 if (status != ESESTATUS_SUCCESS) {
708 ALOGE("phNxpEse_resetJcopUpdate: reset Failed");
709 status = ESESTATUS_FAILED;
710 }
711 } else if (num == 3) {
712 ALOGD_IF(ese_debug_enabled, " %s Call eSE Chip Reset \n", __FUNCTION__);
713 status = phNxpEse_chipReset();
714 if (status != ESESTATUS_SUCCESS) {
715 ALOGE("phNxpEse_resetJcopUpdate: chip reset Failed");
716 status = ESESTATUS_FAILED;
717 }
718 } else {
719 ALOGD_IF(ese_debug_enabled, " %s Invalid Power scheme \n", __FUNCTION__);
720 }
721 }
722 #else
723 {
724 status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
725 if (status != ESESTATUS_SUCCESS) {
726 ALOGE("phNxpEse_SPM_ConfigPwr: reset Failed");
727 status = ESESTATUS_FAILED;
728 }
729 }
730 #endif
731 #else
732 /* if arg ==2 (hard reset)
733 * if arg ==1 (soft reset)
734 */
735 status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
736 if (status != ESESTATUS_SUCCESS) {
737 ALOGE("phNxpEse_resetJcopUpdate Failed");
738 }
739 #endif
740
741 ALOGD_IF(ese_debug_enabled, " %s Exit \n", __FUNCTION__);
742 return status;
743 }
744 /******************************************************************************
745 * Function phNxpEse_EndOfApdu
746 *
747 * Description This function is used to send S-frame to indicate
748 *END_OF_APDU
749 *
750 * Returns It returns ESESTATUS_SUCCESS (0) if the operation is
751 *successful else
752 * ESESTATUS_FAILED(1)
753 *
754 ******************************************************************************/
phNxpEse_EndOfApdu(void)755 ESESTATUS phNxpEse_EndOfApdu(void) {
756 ESESTATUS status = ESESTATUS_SUCCESS;
757 #ifdef NXP_ESE_END_OF_SESSION
758 status = phNxpEseProto7816_Close(
759 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
760 #endif
761 return status;
762 }
763
764 /******************************************************************************
765 * Function phNxpEse_chipReset
766 *
767 * Description This function is used to reset the ESE.
768 *
769 * Returns Always return ESESTATUS_SUCCESS (0).
770 *
771 ******************************************************************************/
phNxpEse_chipReset(void)772 ESESTATUS phNxpEse_chipReset(void) {
773 ESESTATUS status = ESESTATUS_SUCCESS;
774 ESESTATUS bStatus = ESESTATUS_FAILED;
775 if (nxpese_ctxt.pwr_scheme == PN80T_EXT_PMU_SCHEME) {
776 bStatus = phNxpEseProto7816_Reset();
777 if (!bStatus) {
778 status = ESESTATUS_FAILED;
779 ALOGE("Inside phNxpEse_chipReset, phNxpEseProto7816_Reset Failed");
780 }
781 status = phPalEse_ioctl(phPalEse_e_ChipRst, nxpese_ctxt.pDevHandle, 6);
782 if (status != ESESTATUS_SUCCESS) {
783 ALOGE("phNxpEse_chipReset Failed");
784 }
785 } else {
786 ALOGE("phNxpEse_chipReset is not supported in legacy power scheme");
787 status = ESESTATUS_FAILED;
788 }
789 return status;
790 }
791
792 /******************************************************************************
793 * Function phNxpEse_deInit
794 *
795 * Description This function de-initializes all the ESE protocol params
796 *
797 * Returns Always return ESESTATUS_SUCCESS (0).
798 *
799 ******************************************************************************/
phNxpEse_deInit(void)800 ESESTATUS phNxpEse_deInit(void) {
801 ESESTATUS status = ESESTATUS_SUCCESS;
802 unsigned long maxTimer = 0;
803 status = phNxpEseProto7816_Close(
804 (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
805 if (status == ESESTATUS_FAILED) {
806 status = ESESTATUS_FAILED;
807 } else {
808 ALOGD_IF(ese_debug_enabled,
809 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
810 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
811 nxpese_ctxt.secureTimerParams.secureTimer2,
812 nxpese_ctxt.secureTimerParams.secureTimer3);
813 phNxpEse_GetMaxTimer(&maxTimer);
814 #ifdef SPM_INTEGRATED
815 #ifdef NXP_SECURE_TIMER_SESSION
816 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
817 if (status != ESESTATUS_SUCCESS) {
818 ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
819 }
820 #endif
821 #endif
822 }
823 return status;
824 }
825
826 /******************************************************************************
827 * Function phNxpEse_close
828 *
829 * Description This function close the ESE interface and free all
830 * resources.
831 *
832 * Returns Always return ESESTATUS_SUCCESS (0).
833 *
834 ******************************************************************************/
phNxpEse_close(void)835 ESESTATUS phNxpEse_close(void) {
836 ESESTATUS status = ESESTATUS_SUCCESS;
837
838 if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
839 ALOGE(" %s ESE Not Initialized \n", __FUNCTION__);
840 return ESESTATUS_NOT_INITIALISED;
841 }
842
843 #ifdef SPM_INTEGRATED
844 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
845 #endif
846
847 #ifdef SPM_INTEGRATED
848 /* Release the Access of */
849 wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
850 if (wSpmStatus != ESESTATUS_SUCCESS) {
851 ALOGE("phNxpEse_SPM_ConfigPwr: disabling power Failed");
852 } else {
853 nxpese_ctxt.spm_power_state = false;
854 }
855 #ifdef NXP_ESE_JCOP_DWNLD_PROTECTION
856 if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
857 status = phNxpEse_setJcopDwnldState(JCP_SPI_DWNLD_COMPLETE);
858 if (status != ESESTATUS_SUCCESS) {
859 ALOGE("%s: phNxpEse_setJcopDwnldState failed", __FUNCTION__);
860 }
861 }
862 #endif
863 wSpmStatus = phNxpEse_SPM_DeInit();
864 if (wSpmStatus != ESESTATUS_SUCCESS) {
865 ALOGE("phNxpEse_SPM_DeInit Failed");
866 }
867
868 #endif
869 if (NULL != nxpese_ctxt.pDevHandle) {
870 phPalEse_close(nxpese_ctxt.pDevHandle);
871 phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
872 ALOGD_IF(ese_debug_enabled,
873 "phNxpEse_close - ESE Context deinit completed");
874 }
875 /* Return success always */
876 return status;
877 }
878
879 /******************************************************************************
880 * Function phNxpEse_read
881 *
882 * Description This function write the data to ESE through physical
883 * interface (e.g. I2C) using the driver interface.
884 * Before sending the data to ESE, phNxpEse_write_ext
885 * is called to check if there is any extension processing
886 * is required for the SPI packet being sent out.
887 *
888 * Returns It returns ESESTATUS_SUCCESS (0) if read successful else
889 * ESESTATUS_FAILED(1)
890 *
891 ******************************************************************************/
phNxpEse_read(uint32_t * data_len,uint8_t ** pp_data)892 ESESTATUS phNxpEse_read(uint32_t* data_len, uint8_t** pp_data) {
893 ESESTATUS status = ESESTATUS_SUCCESS;
894 int ret = -1;
895
896 ALOGD_IF(ese_debug_enabled, "%s Enter ..", __FUNCTION__);
897
898 ret = phNxpEse_readPacket(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_read_buff,
899 MAX_DATA_LEN);
900 if (ret < 0) {
901 ALOGE("PAL Read status error status = %x", status);
902 *data_len = 2;
903 *pp_data = nxpese_ctxt.p_read_buff;
904 status = ESESTATUS_FAILED;
905 } else {
906 PH_PAL_ESE_PRINT_PACKET_RX(nxpese_ctxt.p_read_buff, (uint16_t)ret);
907 *data_len = ret;
908 *pp_data = nxpese_ctxt.p_read_buff;
909 status = ESESTATUS_SUCCESS;
910 }
911
912 ALOGD_IF(ese_debug_enabled, "%s Exit", __FUNCTION__);
913 return status;
914 }
915
916 /******************************************************************************
917 * Function phNxpEse_readPacket
918 *
919 * Description This function Reads requested number of bytes from
920 * pn547 device into given buffer.
921 *
922 * Returns nNbBytesToRead- number of successfully read bytes
923 * -1 - read operation failure
924 *
925 ******************************************************************************/
phNxpEse_readPacket(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)926 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
927 int nNbBytesToRead) {
928 int ret = -1;
929 int sof_counter = 0; /* one read may take 1 ms*/
930 int total_count = 0, numBytesToRead = 0, headerIndex = 0;
931
932 ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
933 do {
934 sof_counter++;
935 ret = -1;
936 ret = phPalEse_read(pDevHandle, pBuffer, 2);
937 if (ret < 0) {
938 /*Polling for read on spi, hence Debug log*/
939 ALOGD_IF(ese_debug_enabled, "_spi_read() [HDR]errno : %x ret : %X", errno,
940 ret);
941 }
942 if (pBuffer[0] == RECIEVE_PACKET_SOF) {
943 /* Read the HEADR of one byte*/
944 ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
945 numBytesToRead = 1;
946 headerIndex = 1;
947 break;
948 } else if (pBuffer[1] == RECIEVE_PACKET_SOF) {
949 /* Read the HEADR of Two bytes*/
950 ALOGD_IF(ese_debug_enabled, "%s Read HDR", __FUNCTION__);
951 pBuffer[0] = RECIEVE_PACKET_SOF;
952 numBytesToRead = 2;
953 headerIndex = 0;
954 break;
955 }
956 ALOGD_IF(ese_debug_enabled, "%s Normal Pkt, delay read %dus", __FUNCTION__,
957 READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
958 phPalEse_sleep(READ_WAKE_UP_DELAY * NAD_POLLING_SCALER);
959 } while (sof_counter < ESE_NAD_POLLING_MAX);
960 if (pBuffer[0] == RECIEVE_PACKET_SOF) {
961 ALOGD_IF(ese_debug_enabled, "%s SOF FOUND", __FUNCTION__);
962 /* Read the HEADR of one/Two bytes based on how two bytes read A5 PCB or 00
963 * A5*/
964 ret = phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
965 if (ret < 0) {
966 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
967 }
968 total_count = 3;
969 nNbBytesToRead = (pBuffer[2] & 0x000000FF);
970 /* Read the Complete data + one byte CRC*/
971 ret = phPalEse_read(pDevHandle, &pBuffer[3], (nNbBytesToRead + 1));
972 if (ret < 0) {
973 ALOGE("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
974 ret = -1;
975 } else {
976 ret = (total_count + (nNbBytesToRead + 1));
977 }
978 } else if (ret < 0) {
979 /*In case of IO Error*/
980 ret = -2;
981 pBuffer[0] = 0x64;
982 pBuffer[1] = 0xFF;
983 } else {
984 ret = -1;
985 }
986 ALOGD_IF(ese_debug_enabled, "%s Exit ret = %d", __FUNCTION__, ret);
987 return ret;
988 }
989 /******************************************************************************
990 * Function phNxpEse_WriteFrame
991 *
992 * Description This is the actual function which is being called by
993 * phNxpEse_write. This function writes the data to ESE.
994 * It waits till write callback provide the result of write
995 * process.
996 *
997 * Returns It returns ESESTATUS_SUCCESS (0) if write successful else
998 * ESESTATUS_FAILED(1)
999 *
1000 ******************************************************************************/
phNxpEse_WriteFrame(uint32_t data_len,const uint8_t * p_data)1001 ESESTATUS phNxpEse_WriteFrame(uint32_t data_len, const uint8_t* p_data) {
1002 if (data_len > MAX_DATA_LEN) {
1003 ALOGE("%s Data length causes oob write error", __FUNCTION__);
1004 return ESESTATUS_FAILED;
1005 }
1006 ESESTATUS status = ESESTATUS_INVALID_PARAMETER;
1007 int32_t dwNoBytesWrRd = 0;
1008 ALOGD_IF(ese_debug_enabled, "Enter %s ", __FUNCTION__);
1009
1010 /* Create local copy of cmd_data */
1011 phNxpEse_memcpy(nxpese_ctxt.p_cmd_data, p_data, data_len);
1012 nxpese_ctxt.cmd_len = data_len;
1013
1014 dwNoBytesWrRd = phPalEse_write(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_cmd_data,
1015 nxpese_ctxt.cmd_len);
1016 if (-1 == dwNoBytesWrRd) {
1017 ALOGE(" - Error in SPI Write.....\n");
1018 status = ESESTATUS_FAILED;
1019 } else {
1020 status = ESESTATUS_SUCCESS;
1021 PH_PAL_ESE_PRINT_PACKET_TX(nxpese_ctxt.p_cmd_data, nxpese_ctxt.cmd_len);
1022 }
1023
1024 ALOGD_IF(ese_debug_enabled, "Exit %s status %x\n", __FUNCTION__, status);
1025 return status;
1026 }
1027
1028 /******************************************************************************
1029 * Function phNxpEse_setIfsc
1030 *
1031 * Description This function sets the IFSC size to 240/254 support JCOP OS
1032 *Update.
1033 *
1034 * Returns Always return ESESTATUS_SUCCESS (0).
1035 *
1036 ******************************************************************************/
phNxpEse_setIfsc(uint16_t IFSC_Size)1037 ESESTATUS phNxpEse_setIfsc(uint16_t IFSC_Size) {
1038 /*SET the IFSC size to 240 bytes*/
1039 phNxpEseProto7816_SetIfscSize(IFSC_Size);
1040 return ESESTATUS_SUCCESS;
1041 }
1042
1043 /******************************************************************************
1044 * Function phNxpEse_Sleep
1045 *
1046 * Description This function suspends execution of the calling thread for
1047 * (at least) usec microseconds
1048 *
1049 * Returns Always return ESESTATUS_SUCCESS (0).
1050 *
1051 ******************************************************************************/
phNxpEse_Sleep(uint32_t usec)1052 ESESTATUS phNxpEse_Sleep(uint32_t usec) {
1053 phPalEse_sleep(usec);
1054 return ESESTATUS_SUCCESS;
1055 }
1056
1057 /******************************************************************************
1058 * Function phNxpEse_memset
1059 *
1060 * Description This function updates destination buffer with val
1061 * data in len size
1062 *
1063 * Returns Always return ESESTATUS_SUCCESS (0).
1064 *
1065 ******************************************************************************/
phNxpEse_memset(void * buff,int val,size_t len)1066 void* phNxpEse_memset(void* buff, int val, size_t len) {
1067 return phPalEse_memset(buff, val, len);
1068 }
1069
1070 /******************************************************************************
1071 * Function phNxpEse_memcpy
1072 *
1073 * Description This function copies source buffer to destination buffer
1074 * data in len size
1075 *
1076 * Returns Return pointer to allocated memory location.
1077 *
1078 ******************************************************************************/
phNxpEse_memcpy(void * dest,const void * src,size_t len)1079 void* phNxpEse_memcpy(void* dest, const void* src, size_t len) {
1080 return phPalEse_memcpy(dest, src, len);
1081 }
1082
1083 /******************************************************************************
1084 * Function phNxpEse_Memalloc
1085 *
1086 * Description This function allocation memory
1087 *
1088 * Returns Return pointer to allocated memory or NULL.
1089 *
1090 ******************************************************************************/
phNxpEse_memalloc(uint32_t size)1091 void* phNxpEse_memalloc(uint32_t size) {
1092 return phPalEse_memalloc(size);
1093 ;
1094 }
1095
1096 /******************************************************************************
1097 * Function phNxpEse_calloc
1098 *
1099 * Description This is utility function for runtime heap memory allocation
1100 *
1101 * Returns Return pointer to allocated memory or NULL.
1102 *
1103 ******************************************************************************/
phNxpEse_calloc(size_t datatype,size_t size)1104 void* phNxpEse_calloc(size_t datatype, size_t size) {
1105 return phPalEse_calloc(datatype, size);
1106 }
1107
1108 /******************************************************************************
1109 * Function phNxpEse_free
1110 *
1111 * Description This function de-allocation memory
1112 *
1113 * Returns void.
1114 *
1115 ******************************************************************************/
phNxpEse_free(void * ptr)1116 void phNxpEse_free(void* ptr) {
1117 if (ptr != NULL) {
1118 free(ptr);
1119 ptr = NULL;
1120 }
1121 return;
1122 }
1123
1124 /******************************************************************************
1125 * Function phNxpEse_GetMaxTimer
1126 *
1127 * Description This function finds out the max. timer value returned from
1128 *JCOP
1129 *
1130 * Returns void.
1131 *
1132 ******************************************************************************/
phNxpEse_GetMaxTimer(unsigned long * pMaxTimer)1133 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer) {
1134 /* Finding the max. of the timer value */
1135 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer1;
1136 if (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer2)
1137 *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer2;
1138 *pMaxTimer = (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer3)
1139 ? (nxpese_ctxt.secureTimerParams.secureTimer3)
1140 : *pMaxTimer;
1141
1142 /* Converting timer to millisecond from sec */
1143 *pMaxTimer = SECOND_TO_MILLISECOND(*pMaxTimer);
1144 /* Add extra 5% to the timer */
1145 *pMaxTimer +=
1146 CONVERT_TO_PERCENTAGE(*pMaxTimer, ADDITIONAL_SECURE_TIME_PERCENTAGE);
1147 ALOGE("%s Max timer value = %lu", __FUNCTION__, *pMaxTimer);
1148 return;
1149 }
1150
1151 /******************************************************************************
1152 * Function phNxpEseP61_DisablePwrCntrl
1153 *
1154 * Description This function disables eSE GPIO power off/on control
1155 * when enabled
1156 *
1157 * Returns SUCCESS/FAIL.
1158 *
1159 ******************************************************************************/
phNxpEse_DisablePwrCntrl(void)1160 ESESTATUS phNxpEse_DisablePwrCntrl(void) {
1161 ESESTATUS status = ESESTATUS_SUCCESS;
1162 unsigned long maxTimer = 0;
1163 ALOGE("%s Enter", __FUNCTION__);
1164 phNxpEse_GetMaxTimer(&maxTimer);
1165 #ifdef NXP_SECURE_TIMER_SESSION
1166 status = phNxpEse_SPM_DisablePwrControl(maxTimer);
1167 if (status != ESESTATUS_SUCCESS) {
1168 ALOGE("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
1169 }
1170 #else
1171 ALOGE("%s phNxpEseP61_DisablePwrCntrl: not supported", __FUNCTION__);
1172 status = ESESTATUS_FAILED;
1173 #endif
1174 return status;
1175 }
1176
1177 #ifdef NXP_NFCC_SPI_FW_DOWNLOAD_SYNC
1178 /******************************************************************************
1179 * Function phNxpEse_checkFWDwnldStatus
1180 *
1181 * Description This function is used to check whether FW download
1182 * is completed or not.
1183 *
1184 * Returns returns ESESTATUS_SUCCESS or ESESTATUS_BUSY
1185 *
1186 ******************************************************************************/
phNxpEse_checkFWDwnldStatus(void)1187 static ESESTATUS phNxpEse_checkFWDwnldStatus(void) {
1188 ALOGE("phNxpEse_checkFWDwnldStatus Enter");
1189 ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1190 spm_state_t current_spm_state = SPM_STATE_INVALID;
1191 uint8_t ese_dwnld_retry = 0x00;
1192 ESESTATUS status = ESESTATUS_FAILED;
1193
1194 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1195 if (wSpmStatus == ESESTATUS_SUCCESS) {
1196 /* Check current_spm_state and update config/Spm status*/
1197 while (ese_dwnld_retry < ESE_FW_DWNLD_RETRY_CNT) {
1198 ALOGE("ESE_FW_DWNLD_RETRY_CNT retry count");
1199 wSpmStatus = phNxpEse_SPM_GetState(¤t_spm_state);
1200 if (wSpmStatus == ESESTATUS_SUCCESS) {
1201 if ((current_spm_state & SPM_STATE_DWNLD)) {
1202 status = ESESTATUS_FAILED;
1203 } else {
1204 ALOGE("Exit polling no FW Download ..");
1205 status = ESESTATUS_SUCCESS;
1206 break;
1207 }
1208 } else {
1209 status = ESESTATUS_FAILED;
1210 break;
1211 }
1212 phNxpEse_Sleep(500000); /*sleep for 500 ms checking for fw dwnld status*/
1213 ese_dwnld_retry++;
1214 }
1215 }
1216
1217 ALOGE("phNxpEse_checkFWDwnldStatus status %x", status);
1218 return status;
1219 }
1220 #endif
1221 /******************************************************************************
1222 * Function phNxpEse_GetEseStatus(unsigned char *timer_buffer)
1223 *
1224 * Description This function returns the all three timer
1225 * Timeout buffer length should be minimum 18 bytes. Response will be in below
1226 format:
1227 * <0xF1><Len><Timer Value><0xF2><Len><Timer Value><0xF3><Len><Timer Value>
1228 *
1229 * Returns SUCCESS/FAIL.
1230 * ESESTATUS_SUCCESS if 0xF1 or 0xF2 tag timeout >= 0 & 0xF3 == 0
1231 * ESESTATUS_BUSY if 0xF3 tag timeout > 0
1232 * ESESTATUS_FAILED if any other error
1233
1234 ******************************************************************************/
phNxpEse_GetEseStatus(phNxpEse_data * timer_buffer)1235 ESESTATUS phNxpEse_GetEseStatus(phNxpEse_data* timer_buffer) {
1236 ESESTATUS status = ESESTATUS_FAILED;
1237
1238 phNxpEse_SecureTimer_t secureTimerParams;
1239 uint8_t* temp_timer_buffer = NULL;
1240 ALOGD_IF(ese_debug_enabled, "%s Enter", __FUNCTION__);
1241
1242 if (timer_buffer != NULL) {
1243 timer_buffer->len =
1244 (sizeof(secureTimerParams.secureTimer1) +
1245 sizeof(secureTimerParams.secureTimer2) +
1246 sizeof(secureTimerParams.secureTimer3)) +
1247 PH_PROPTO_7816_FRAME_LENGTH_OFFSET * PH_PROPTO_7816_FRAME_LENGTH_OFFSET;
1248 temp_timer_buffer = (uint8_t*)phNxpEse_memalloc(timer_buffer->len);
1249 timer_buffer->p_data = temp_timer_buffer;
1250
1251 #ifdef NXP_SECURE_TIMER_SESSION
1252 phNxpEse_memcpy(&secureTimerParams, &nxpese_ctxt.secureTimerParams,
1253 sizeof(phNxpEse_SecureTimer_t));
1254
1255 ALOGD_IF(
1256 ese_debug_enabled,
1257 "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x len = %d",
1258 __FUNCTION__, secureTimerParams.secureTimer1,
1259 secureTimerParams.secureTimer2, secureTimerParams.secureTimer3,
1260 timer_buffer->len);
1261
1262 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER1;
1263 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer1);
1264 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1265 temp_timer_buffer, secureTimerParams.secureTimer1);
1266 if (temp_timer_buffer != NULL) {
1267 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER2;
1268 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer2);
1269 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1270 temp_timer_buffer, secureTimerParams.secureTimer2);
1271 if (temp_timer_buffer != NULL) {
1272 *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER3;
1273 *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer3);
1274 temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1275 temp_timer_buffer, secureTimerParams.secureTimer3);
1276 if (temp_timer_buffer != NULL) {
1277 if (secureTimerParams.secureTimer3 > 0) {
1278 status = ESESTATUS_BUSY;
1279 } else {
1280 status = ESESTATUS_SUCCESS;
1281 }
1282 }
1283 }
1284 }
1285 #endif
1286 } else {
1287 ALOGE("%s Invalid timer buffer ", __FUNCTION__);
1288 }
1289
1290 ALOGD_IF(ese_debug_enabled, "%s Exit status = 0x%x", __FUNCTION__, status);
1291 return status;
1292 }
1293 #ifdef NXP_SECURE_TIMER_SESSION
phNxpEse_GgetTimerTlvBuffer(uint8_t * timer_buffer,unsigned int value)1294 static unsigned char* phNxpEse_GgetTimerTlvBuffer(uint8_t* timer_buffer,
1295 unsigned int value) {
1296 short int count = 0, shift = 3;
1297 unsigned int mask = 0x000000FF;
1298 ALOGD_IF(ese_debug_enabled, "value = %x \n", value);
1299 for (count = 0; count < 4; count++) {
1300 if (timer_buffer != NULL) {
1301 *timer_buffer = (value >> (shift * 8) & mask);
1302 ALOGD_IF(ese_debug_enabled, "*timer_buffer=0x%x shift=0x%x",
1303 *timer_buffer, shift);
1304 timer_buffer++;
1305 shift--;
1306 } else {
1307 break;
1308 }
1309 }
1310 return timer_buffer;
1311 }
1312 #endif
1313