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