1 /*
2 * Copyright 2019-2023 NXP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "phNxpNciHal_IoctlOperations.h"
18
19 #include <android-base/file.h>
20 #include <android-base/parseint.h>
21 #include <android-base/strings.h>
22
23 #include <map>
24 #include <set>
25
26 #include "EseAdaptation.h"
27 #include "NfccTransport.h"
28 #include "NfccTransportFactory.h"
29 #include "phDnldNfc_Internal.h"
30 #include "phNfcCommon.h"
31 #include "phNxpNciHal_Adaptation.h"
32 #include "phNxpNciHal_ULPDet.h"
33 #include "phNxpNciHal_ext.h"
34 #include "phNxpNciHal_extOperations.h"
35 #include "phNxpNciHal_utils.h"
36
37 using android::base::WriteStringToFile;
38 using namespace ::std;
39 using namespace ::android::base;
40
41 #define TERMINAL_LEN 5
42 /* HAL_NFC_STATUS_REFUSED sent to restart NFC service */
43 #define HAL_NFC_STATUS_RESTART HAL_NFC_STATUS_REFUSED
44
45 /****************************************************************
46 * Global Variables Declaration
47 ***************************************************************/
48 /* External global variable to get FW version from NCI response*/
49 extern uint32_t wFwVerRsp;
50 /* External global variable to get FW version from FW file*/
51 extern uint16_t wFwVer;
52 /* NCI HAL Control structure */
53 extern phNxpNciHal_Control_t nxpncihal_ctrl;
54 extern phNxpNci_getCfg_info_t* mGetCfg_info;
55 extern EseAdaptation* gpEseAdapt;
56 extern nfc_stack_callback_t* p_nfc_stack_cback_backup;
57 #ifndef FW_DWNLD_FLAG
58 extern uint8_t fw_dwnld_flag;
59 #endif
60
61 /* TML Context */
62 extern phTmlNfc_Context_t* gpphTmlNfc_Context;
63 extern bool nfc_debug_enabled;
64 extern NFCSTATUS phNxpLog_EnableDisableLogLevel(uint8_t enable);
65 extern phNxpNciClock_t phNxpNciClock;
66 extern NfcHalThreadMutex sHalFnLock;
67
68 /*******************************************************************************
69 **
70 ** Function: property_get_intf()
71 **
72 ** Description: Gets property value for the input property name
73 **
74 ** Parameters propName: Name of the property whichs value need to get
75 ** valueStr: output value of the property.
76 ** defaultStr: default value of the property if value is not
77 ** there this will be set to output value.
78 **
79 ** Returns: actual length of the property value
80 **
81 ********************************************************************************/
property_get_intf(const char * propName,char * valueStr,const char * defaultStr)82 int property_get_intf(const char* propName, char* valueStr,
83 const char* defaultStr) {
84 string propValue;
85 string propValueDefault = defaultStr;
86 int len = 0;
87
88 propValue = phNxpNciHal_getSystemProperty(propName);
89 if (propValue.length() > 0) {
90 NXPLOG_NCIHAL_D("property_get_intf , key[%s], propValue[%s], length[%zu]",
91 propName, propValue.c_str(), propValue.length());
92 len = propValue.length();
93 strlcpy(valueStr, propValue.c_str(), PROPERTY_VALUE_MAX);
94 } else {
95 if (propValueDefault.length() > 0) {
96 len = propValueDefault.length();
97 strlcpy(valueStr, propValueDefault.c_str(), PROPERTY_VALUE_MAX);
98 }
99 }
100
101 return len;
102 }
103
104 /*******************************************************************************
105 **
106 ** Function: property_set_intf()
107 **
108 ** Description: Sets property value for the input property name
109 **
110 ** Parameters propName: Name of the property whichs value need to set
111 ** valueStr: value of the property.
112 **
113 ** Returns: returns 0 on success, < 0 on failure
114 **
115 ********************************************************************************/
property_set_intf(const char * propName,const char * valueStr)116 int property_set_intf(const char* propName, const char* valueStr) {
117 NXPLOG_NCIHAL_D("property_set_intf, key[%s], value[%s]", propName, valueStr);
118 if (phNxpNciHal_setSystemProperty(propName, valueStr))
119 return NFCSTATUS_SUCCESS;
120 else
121 return NFCSTATUS_FAILED;
122 }
123
124 extern size_t readConfigFile(const char* fileName, uint8_t** p_data);
125
126 static string phNxpNciHal_parseBytesString(string in);
127 static bool phNxpNciHal_parseValueFromString(string& in);
128 static bool phNxpNciHal_CheckKeyNeeded(string key);
129 static string phNxpNciHal_UpdatePwrStateConfigs(string& config);
130 static bool phNxpNciHal_IsAutonmousModeSet(string config);
131 static string phNxpNciHal_extractConfig(string& config);
132 static void phNxpNciHal_getFilteredConfig(string& config);
133
134 typedef std::map<std::string, std::string> systemProperty;
135 systemProperty gsystemProperty = {
136 {"nfc.nxp_log_level_global", ""},
137 {"nfc.nxp_log_level_extns", ""},
138 {"nfc.nxp_log_level_hal", ""},
139 {"nfc.nxp_log_level_nci", ""},
140 {"nfc.nxp_log_level_dnld", ""},
141 {"nfc.nxp_log_level_tml", ""},
142 {"nfc.fw.dfl", ""},
143 {"nfc.fw.downloadmode_force", ""},
144 {"nfc.debug_enabled", ""},
145 {"nfc.product.support.ese", ""},
146 {"nfc.product.support.uicc", ""},
147 {"nfc.product.support.uicc2", ""},
148 {"nfc.fw.rfreg_ver", ""},
149 {"nfc.fw.rfreg_display_ver", ""},
150 {"nfc.fw.dfl_areacode", ""},
151 {"nfc.cover.cover_id", ""},
152 {"nfc.cover.state", ""},
153 {"ro.factory.factory_binary", ""},
154 };
155 const char default_nxp_config_path[] = "/vendor/etc/libnfc-nxp.conf";
156 std::set<string> gNciConfigs = {"NXP_SE_COLD_TEMP_ERROR_DELAY",
157 "NXP_SWP_RD_TAG_OP_TIMEOUT",
158 "NXP_DUAL_UICC_ENABLE",
159 "DEFAULT_AID_ROUTE",
160 "DEFAULT_MIFARE_CLT_ROUTE",
161 "DEFAULT_FELICA_CLT_ROUTE",
162 "DEFAULT_AID_PWR_STATE",
163 "DEFAULT_DESFIRE_PWR_STATE",
164 "DEFAULT_MIFARE_CLT_PWR_STATE",
165 "DEFAULT_FELICA_CLT_PWR_STATE",
166 "HOST_LISTEN_TECH_MASK",
167 "FORWARD_FUNCTIONALITY_ENABLE",
168 "DEFAULT_GSMA_PWR_STATE",
169 "NXP_DEFAULT_UICC2_SELECT",
170 "NXP_SMB_TRANSCEIVE_TIMEOUT",
171 "NXP_SMB_ERROR_RETRY",
172 "NXP_CHECK_DEFAULT_PROTO_SE_ID",
173 "NXPLOG_NCIHAL_LOGLEVEL",
174 "NXPLOG_EXTNS_LOGLEVEL",
175 "NXPLOG_TML_LOGLEVEL",
176 "NXPLOG_FWDNLD_LOGLEVEL",
177 "NXPLOG_NCIX_LOGLEVEL",
178 "NXPLOG_NCIR_LOGLEVEL",
179 "NXP_NFC_SE_TERMINAL_NUM",
180 "NXP_POLL_FOR_EFD_TIMEDELAY",
181 "NXP_NFCC_MERGE_SAK_ENABLE",
182 "NXP_STAG_TIMEOUT_CFG",
183 "DEFAULT_T4TNFCEE_AID_POWER_STATE",
184 "RF_STORAGE",
185 "FW_STORAGE",
186 "NXP_CORE_CONF",
187 "NXP_RF_FILE_VERSION_INFO",
188 "NXP_AUTONOMOUS_ENABLE",
189 "NXP_PROP_RESET_EMVCO_CMD",
190 "NFA_CONFIG_FORMAT",
191 "NXP_T4T_NFCEE_ENABLE",
192 "NXP_DISCONNECT_TAG_IN_SCRN_OFF",
193 "NXP_CE_PRIORITY_ENABLED",
194 "NXP_RDR_REQ_GUARD_TIME",
195 "OFF_HOST_SIM2_PIPE_ID",
196 "NXP_ENABLE_DISABLE_LOGS",
197 "NXP_RDR_DISABLE_ENABLE_LPCD",
198 "NXP_SUPPORT_NON_STD_CARD",
199 "NXP_GET_HW_INFO_LOG",
200 "NXP_WLC_MODE",
201 "NXP_T4T_NDEF_NFCEE_AID",
202 "NXP_NON_STD_CARD_TIMEDIFF",
203 "NXP_SRD_TIMEOUT",
204 "NXP_UICC_ETSI_SUPPORT",
205 "NXP_MINIMAL_FW_VERSION",
206 "NXP_RESTART_RF_FOR_NFCEE_RECOVERY",
207 "NXP_NFCC_RECOVERY_SUPPORT",
208 "NXP_AGC_DEBUG_ENABLE",
209 "NXP_EXTENDED_FIELD_DETECT_MODE",
210 "NXP_SE_SMB_TERMINAL_TYPE",
211 "OFF_HOST_ESIM_PIPE_ID",
212 "OFF_HOST_ESIM2_PIPE_ID"};
213
214 /****************************************************************
215 * Local Functions
216 ***************************************************************/
217
218 /******************************************************************************
219 ** Function phNxpNciHal_ioctlIf
220 **
221 ** Description This function shall be called from HAL when libnfc-nci
222 ** calls phNxpNciHal_ioctl() to perform any IOCTL operation
223 **
224 ** Returns return 0 on success and -1 on fail,
225 ******************************************************************************/
phNxpNciHal_ioctlIf(long arg,void * p_data)226 int phNxpNciHal_ioctlIf(long arg, void* p_data) {
227 NXPLOG_NCIHAL_D("%s : enter - arg = %ld", __func__, arg);
228 ese_nxp_IoctlInOutData_t* pInpOutData = (ese_nxp_IoctlInOutData_t*)p_data;
229 int ret = -1;
230
231 switch (arg) {
232 case HAL_ESE_IOCTL_NFC_JCOP_DWNLD:
233 if (pInpOutData == NULL) {
234 NXPLOG_NCIHAL_E("%s : received invalid param", __func__);
235 break;
236 }
237
238 if (gpEseAdapt == NULL) {
239 gpEseAdapt = &EseAdaptation::GetInstance();
240 if (gpEseAdapt == NULL) {
241 NXPLOG_NCIHAL_E("%s :invalid gpEseAdapt param", __func__);
242 break;
243 }
244 gpEseAdapt->Initialize();
245 }
246
247 NXPLOG_NCIHAL_D("HAL_ESE_IOCTL_NFC_JCOP_DWNLD Enter value is %d: \n",
248 pInpOutData->inp.data.nxpCmd.p_cmd[0]);
249
250 gpEseAdapt->HalIoctl(HAL_ESE_IOCTL_NFC_JCOP_DWNLD, pInpOutData);
251 ret = 0;
252 break;
253 default:
254 NXPLOG_NCIHAL_E("%s : Wrong arg = %ld", __func__, arg);
255 break;
256 }
257 NXPLOG_NCIHAL_D("%s : exit - ret = %d", __func__, ret);
258 return ret;
259 }
260
261 /*******************************************************************************
262 **
263 ** Function phNxpNciHal_getSystemProperty
264 **
265 ** Description It shall be used to get property value of the given Key
266 **
267 ** Parameters string key
268 **
269 ** Returns If Key is found, returns the respective property values
270 ** else returns the null/empty string
271 *******************************************************************************/
phNxpNciHal_getSystemProperty(string key)272 string phNxpNciHal_getSystemProperty(string key) {
273 string propValue;
274 std::map<std::string, std::string>::iterator prop;
275
276 if (key == "libnfc-nxp.conf") {
277 return phNxpNciHal_getNxpConfigIf();
278 } else {
279 prop = gsystemProperty.find(key);
280 if (prop != gsystemProperty.end()) {
281 propValue = prop->second;
282 } else {
283 /* else Pass a null string */
284 }
285 }
286 return propValue;
287 }
288 /*******************************************************************************
289 **
290 ** Function phNxpNciHal_setSystemProperty
291 **
292 ** Description It shall be used to save/change value to system property
293 ** based on provided key.
294 **
295 ** Parameters string key, string value
296 **
297 ** Returns true if success, false if fail
298 *******************************************************************************/
phNxpNciHal_setSystemProperty(string key,string value)299 bool phNxpNciHal_setSystemProperty(string key, string value) {
300 bool stat = true;
301 if (strcmp(key.c_str(), "nfc.debug_enabled") != 0)
302 NXPLOG_NCIHAL_D("%s : Enter Key = %s, value = %s", __func__, key.c_str(),
303 value.c_str());
304
305 unsigned tmp = 0;
306 if (strcmp(key.c_str(), "nfc.debug_enabled") == 0) {
307 if (ParseUint(value.c_str(), &tmp)) {
308 if (phNxpLog_EnableDisableLogLevel((uint8_t)tmp) != NFCSTATUS_SUCCESS) {
309 stat = false;
310 }
311 } else {
312 NXPLOG_NCIHAL_W(
313 "%s : Failed to parse the string to uint. "
314 "nfc.debug_enabled string : %s",
315 __func__, value.c_str());
316 }
317 } else if (strcmp(key.c_str(), "nfc.cover.state") == 0) {
318 unsigned cid, cstate;
319 string strtmp;
320 if (ParseUint(value.c_str(), &cstate)) {
321 strtmp = phNxpNciHal_getSystemProperty("nfc.cover.cover_id");
322 if (ParseUint(strtmp.c_str(), &cid)) {
323 if (fpPropConfCover != NULL) {
324 stat = (fpPropConfCover(cstate, cid) == NFCSTATUS_SUCCESS) ? true
325 : false;
326 }
327 } else {
328 NXPLOG_NCIHAL_W(
329 "%s : Failed to parse the string to uint. "
330 "nfc.cover.cover_id string : %s",
331 __func__, value.c_str());
332 }
333 } else {
334 NXPLOG_NCIHAL_W(
335 "%s : Failed to parse the string to uint. "
336 "nfc.cover.state string : %s",
337 __func__, value.c_str());
338 }
339 } else if (strcmp(key.c_str(), "nfc.cmd_timeout") == 0) {
340 NXPLOG_NCIHAL_E("%s : nci_timeout, sem post", __func__);
341 sem_post(&(nxpncihal_ctrl.syncSpiNfc));
342 } else if (strcmp(key.c_str(), "nfc.ulpdet") == 0) {
343 NXPLOG_NCIHAL_E("%s : set ulpdet", __func__);
344 if (!phNxpNciHal_isULPDetSupported()) return false;
345 bool flag = false;
346 if (strcmp(value.c_str(), "1") == 0) {
347 flag = true;
348 }
349 phNxpNciHal_setULPDetFlag(flag);
350 }
351 gsystemProperty[key] = std::move(value);
352 return stat;
353 }
354
355 /*******************************************************************************
356 **
357 ** Function phNxpNciHal_getNxpConfig
358 **
359 ** Description It shall be used to read config values from the
360 *libnfc-nxp.conf
361 **
362 ** Parameters nxpConfigs config
363 **
364 ** Returns void
365 *******************************************************************************/
phNxpNciHal_getNxpConfigIf()366 string phNxpNciHal_getNxpConfigIf() {
367 std::string config;
368 uint8_t* p_config = nullptr;
369 size_t config_size = readConfigFile(default_nxp_config_path, &p_config);
370 if (config_size) {
371 config.assign((char*)p_config, config_size);
372 free(p_config);
373 phNxpNciHal_getFilteredConfig(config);
374 }
375 return config;
376 }
377
378 /*******************************************************************************
379 **
380 ** Function phNxpNciHal_getFilteredConfig
381 **
382 ** Description It reads only configs needed for libnfc from
383 * libnfc-nxp.conf
384 **
385 ** Parameters string config
386 **
387 ** Returns void
388 *******************************************************************************/
phNxpNciHal_getFilteredConfig(string & config)389 static void phNxpNciHal_getFilteredConfig(string& config) {
390 config = phNxpNciHal_extractConfig(config);
391 if (phNxpNciHal_IsAutonmousModeSet(config)) {
392 config = phNxpNciHal_UpdatePwrStateConfigs(config);
393 }
394 }
395
396 /*******************************************************************************
397 **
398 ** Function phNxpNciHal_extractConfig
399 **
400 ** Description It parses complete config file and extracts only
401 * enabled options ignores comments etc.
402 **
403 ** Parameters string config
404 **
405 ** Returns Resultant string
406 *******************************************************************************/
phNxpNciHal_extractConfig(string & config)407 static string phNxpNciHal_extractConfig(string& config) {
408 stringstream ss(config);
409 string line;
410 string result;
411 bool apduGate = false;
412 while (getline(ss, line)) {
413 line = Trim(line);
414 if (line.empty()) continue;
415 if (line.at(0) == '#') continue;
416 if (line.at(0) == 0) continue;
417
418 auto search = line.find('=');
419 if (search == string::npos) continue;
420
421 string key(Trim(line.substr(0, search)));
422 if (!phNxpNciHal_CheckKeyNeeded(key)) continue;
423 if (key == "NXP_NFC_SE_TERMINAL_NUM" && !apduGate) {
424 line = "NXP_SE_APDU_GATE_SUPPORT=0x01\n";
425 result += line;
426 apduGate = true;
427 continue;
428 }
429 string value_string(Trim(line.substr(search + 1, string::npos)));
430
431 if (value_string[0] == '{' &&
432 value_string[value_string.length() - 1] != '}') {
433 string line_append;
434
435 do {
436 getline(ss, line_append);
437 if (line_append.empty()) break;
438 if (line_append.at(0) == '#') break;
439 if (line_append.at(0) == 0) break;
440 line_append = Trim(line_append);
441 value_string.append(line_append);
442 } while (line_append[line_append.length() - 1] != '}');
443 }
444
445 if (!phNxpNciHal_parseValueFromString(value_string)) continue;
446
447 line = key + "=" + value_string + "\n";
448 result += line;
449 if (key == "NXP_GET_HW_INFO_LOG" &&
450 (value_string == "1" || value_string == "0x01")) {
451 if (!apduGate) {
452 line = "NXP_SE_APDU_GATE_SUPPORT=0x01\n";
453 result += line;
454 apduGate = true;
455 }
456 }
457 }
458
459 return result;
460 }
461
462 /*******************************************************************************
463 **
464 ** Function phNxpNciHal_IsAutonmousModeSet
465 **
466 ** Description It check whether autonomous mode is enabled
467 * in config file
468 **
469 ** Parameters string config
470 **
471 ** Returns boolean(TRUE/FALSE)
472 *******************************************************************************/
phNxpNciHal_IsAutonmousModeSet(string config)473 static bool phNxpNciHal_IsAutonmousModeSet(string config) {
474 stringstream ss(config);
475 string line;
476 unsigned tmp = 0;
477 while (getline(ss, line)) {
478 auto search = line.find('=');
479 if (search == string::npos) continue;
480
481 string key(Trim(line.substr(0, search)));
482 if (key == "NXP_AUTONOMOUS_ENABLE") {
483 string value(Trim(line.substr(search + 1, string::npos)));
484 if (ParseUint(value.c_str(), &tmp)) {
485 if (tmp == 1) {
486 return true;
487 } else {
488 NXPLOG_NCIHAL_D("Autonomous flag disabled");
489 return false;
490 }
491 }
492 } else {
493 continue;
494 }
495 }
496 NXPLOG_NCIHAL_D("Autonomous flag disabled");
497 return false;
498 }
499
500 /*******************************************************************************
501 **
502 ** Function phNxpNciHal_UpdatePwrStateConfigs
503 **
504 ** Description Updates default pwr state accordingly if autonomous mode
505 * is enabled
506 **
507 ** Parameters string config
508 **
509 ** Returns Resultant string
510 *******************************************************************************/
phNxpNciHal_UpdatePwrStateConfigs(string & config)511 static string phNxpNciHal_UpdatePwrStateConfigs(string& config) {
512 stringstream ss(config);
513 string line;
514 string result;
515 unsigned tmp = 0;
516 while (getline(ss, line)) {
517 auto search = line.find('=');
518 if (search == string::npos) continue;
519
520 string key(Trim(line.substr(0, search)));
521 if ((key == "DEFAULT_AID_PWR_STATE" || key == "DEFAULT_DESFIRE_PWR_STATE" ||
522 key == "DEFAULT_MIFARE_CLT_PWR_STATE" ||
523 key == "DEFAULT_FELICA_CLT_PWR_STATE")) {
524 string value(Trim(line.substr(search + 1, string::npos)));
525 if (ParseUint(value.c_str(), &tmp)) {
526 tmp = phNxpNciHal_updateAutonomousPwrState(tmp);
527 value = to_string(tmp);
528 line = key + "=" + value + "\n";
529 result += line;
530 }
531 } else {
532 result += (line + "\n");
533 continue;
534 }
535 }
536 return result;
537 }
538
539 /*******************************************************************************
540 **
541 ** Function phNxpNciHal_CheckKeyNeeded
542 **
543 ** Description Check if the config needed for libnfc as per gNciConfigs
544 * list
545 **
546 ** Parameters string config
547 **
548 ** Returns bool(true/false)
549 *******************************************************************************/
phNxpNciHal_CheckKeyNeeded(string key)550 static bool phNxpNciHal_CheckKeyNeeded(string key) {
551 return ((gNciConfigs.find(key) != gNciConfigs.end()) ? true : false);
552 }
553
554 /*******************************************************************************
555 **
556 ** Function phNxpNciHal_parseValueFromString
557 **
558 ** Description Parse value determine data type of config option
559 **
560 ** Parameters string config
561 **
562 ** Returns bool(true/false)
563 *******************************************************************************/
phNxpNciHal_parseValueFromString(string & in)564 static bool phNxpNciHal_parseValueFromString(string& in) {
565 unsigned tmp = 0;
566 bool stat = false;
567 if (in.length() >= 1) {
568 switch (in[0]) {
569 case '"':
570 if (in[in.length() - 1] == '"' && in.length() > 2) stat = true;
571 break;
572 case '{':
573 if (in[in.length() - 1] == '}' && in.length() >= 3) {
574 in = phNxpNciHal_parseBytesString(in);
575 stat = true;
576 }
577 break;
578 default:
579 if (ParseUint(in.c_str(), &tmp)) stat = true;
580 break;
581 }
582 } else {
583 NXPLOG_NCIHAL_E("%s : Invalid config string ", __func__);
584 }
585 return stat;
586 }
587
588 /*******************************************************************************
589 **
590 ** Function phNxpNciHal_parseBytesString
591 **
592 ** Description Parse bytes from string
593 **
594 ** Parameters string config
595 **
596 ** Returns Resultant string
597 *******************************************************************************/
phNxpNciHal_parseBytesString(string in)598 static string phNxpNciHal_parseBytesString(string in) {
599 size_t pos;
600 in.erase(remove(in.begin(), in.end(), ' '), in.end());
601 pos = in.find(",");
602 while (pos != string::npos) {
603 in = in.replace(pos, 1, ":");
604 pos = in.find(",", pos);
605 }
606 return in;
607 }
608
609 /*******************************************************************************
610 **
611 ** Function phNxpNciHal_resetEse
612 **
613 ** Description It shall be used to reset eSE by proprietary command.
614 **
615 ** Parameters
616 **
617 ** Returns status of eSE reset response
618 *******************************************************************************/
phNxpNciHal_resetEse(uint64_t resetType)619 NFCSTATUS phNxpNciHal_resetEse(uint64_t resetType) {
620 NFCSTATUS status = NFCSTATUS_FAILED;
621
622 {
623 NfcHalAutoThreadMutex a(sHalFnLock);
624 if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE) {
625 if (NFCSTATUS_SUCCESS != phNxpNciHal_MinOpen()) {
626 return NFCSTATUS_FAILED;
627 }
628 }
629 }
630
631 CONCURRENCY_LOCK();
632 status = gpTransportObj->EseReset(gpphTmlNfc_Context->pDevHandle,
633 (EseResetType)resetType);
634 CONCURRENCY_UNLOCK();
635 if (status != NFCSTATUS_SUCCESS) {
636 NXPLOG_NCIHAL_E("EsePowerCycle failed");
637 }
638
639 if (nxpncihal_ctrl.halStatus == HAL_STATUS_MIN_OPEN) {
640 phNxpNciHal_close(false);
641 }
642
643 return status;
644 }
645
646 /*******************************************************************************
647 **
648 ** Function: phNxpNciHal_GetNfcGpiosStatus()
649 **
650 ** Description: Sets the gpios status flag byte
651 **
652 ** Parameters gpiostatus: flag byte
653 **
654 ** Returns: returns 0 on success, < 0 on failure
655 **
656 ********************************************************************************/
phNxpNciHal_GetNfcGpiosStatus(uint32_t * gpiosstatus)657 NFCSTATUS phNxpNciHal_GetNfcGpiosStatus(uint32_t* gpiosstatus) {
658 NFCSTATUS status = NFCSTATUS_FAILED;
659 status = gpTransportObj->NfcGetGpioStatus(gpphTmlNfc_Context->pDevHandle,
660 gpiosstatus);
661 return status;
662 }
663 /******************************************************************************
664 * Function phNxpNciHal_setNxpTransitConfig
665 *
666 * Description This function overwrite libnfc-nxpTransit.conf file
667 * with transitConfValue.
668 *
669 * Returns bool.
670 *
671 ******************************************************************************/
phNxpNciHal_setNxpTransitConfig(char * transitConfValue)672 bool phNxpNciHal_setNxpTransitConfig(char* transitConfValue) {
673 bool status = true;
674 NXPLOG_NCIHAL_D("%s : Enter", __func__);
675 std::string transitConfFileName = "/data/vendor/nfc/libnfc-nxpTransit.conf";
676 long transitConfValueLen = strlen(transitConfValue) + 1;
677
678 if (transitConfValueLen > 1) {
679 if (!WriteStringToFile(transitConfValue, transitConfFileName)) {
680 NXPLOG_NCIHAL_E("WriteStringToFile: Failed");
681 status = false;
682 }
683 } else {
684 if (!WriteStringToFile("", transitConfFileName)) {
685 NXPLOG_NCIHAL_E("WriteStringToFile: Failed");
686 status = false;
687 }
688 if (remove(transitConfFileName.c_str())) {
689 NXPLOG_NCIHAL_E("Unable to remove file");
690 status = false;
691 }
692 }
693 NXPLOG_NCIHAL_D("%s : Exit", __func__);
694 return status;
695 }
696
697 /******************************************************************************
698 ** Function phNxpNciHal_Abort
699 **
700 ** Description This function shall be used to trigger the abort in libnfc
701 **
702 ** Parameters None
703 **
704 ** Returns bool.
705 **
706 *******************************************************************************/
phNxpNciHal_Abort()707 bool phNxpNciHal_Abort() {
708 bool ret = true;
709
710 NXPLOG_NCIHAL_D("phNxpNciHal_Abort aborting. \n");
711 /* When JCOP download is triggered phNxpNciHal_open is blocked, in this case
712 only we need to abort the libnfc , this can be done only by check the
713 p_nfc_stack_cback_backup pointer which is assigned before the JCOP
714 download.*/
715 if (p_nfc_stack_cback_backup != NULL) {
716 (*p_nfc_stack_cback_backup)(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_RESTART);
717 } else {
718 ret = false;
719 NXPLOG_NCIHAL_D("phNxpNciHal_Abort not triggered\n");
720 }
721 return ret;
722 }
723
724 /*******************************************************************************
725 **
726 ** Function: phNxpNciHal_CheckFwRegFlashRequired()
727 **
728 ** Description: Updates FW and Reg configurations if required
729 **
730 ** Returns: status
731 **
732 ********************************************************************************/
phNxpNciHal_CheckFwRegFlashRequired(uint8_t * fw_update_req,uint8_t * rf_update_req,uint8_t skipEEPROMRead)733 int phNxpNciHal_CheckFwRegFlashRequired(uint8_t* fw_update_req,
734 uint8_t* rf_update_req,
735 uint8_t skipEEPROMRead) {
736 NXPLOG_NCIHAL_D("phNxpNciHal_CheckFwRegFlashRequired() : enter");
737 int status = NFCSTATUS_OK;
738 long option;
739 if (fpRegRfFwDndl != NULL) {
740 status = fpRegRfFwDndl(fw_update_req, rf_update_req, skipEEPROMRead);
741 } else {
742 status = phDnldNfc_InitImgInfo();
743 NXPLOG_NCIHAL_D("FW version from the binary(.so/bin) = 0x%x", wFwVer);
744 NXPLOG_NCIHAL_D("FW version found on the device = 0x%x", wFwVerRsp);
745
746 if (!GetNxpNumValue(NAME_NXP_FLASH_CONFIG, &option,
747 sizeof(unsigned long))) {
748 NXPLOG_NCIHAL_D("Flash option not found; giving default value");
749 option = 1;
750 }
751 switch (option) {
752 case FLASH_UPPER_VERSION:
753 wFwUpdateReq = (utf8_t)wFwVer > (utf8_t)wFwVerRsp ? true : false;
754 break;
755 case FLASH_DIFFERENT_VERSION:
756 wFwUpdateReq = ((wFwVerRsp & 0x0000FFFF) != wFwVer) ? true : false;
757 break;
758 case FLASH_ALWAYS:
759 wFwUpdateReq = true;
760 break;
761 default:
762 NXPLOG_NCIHAL_D("Invalid flash option selected");
763 status = NFCSTATUS_INVALID_PARAMETER;
764 break;
765 }
766 }
767 *fw_update_req = wFwUpdateReq;
768
769 if (false == wFwUpdateReq) {
770 NXPLOG_NCIHAL_D("FW update not required");
771 phDnldNfc_ReSetHwDevHandle();
772 } else {
773 property_set("nfc.fw.downloadmode_force", "1");
774 }
775
776 NXPLOG_NCIHAL_D(
777 "phNxpNciHal_CheckFwRegFlashRequired() : exit - status = %x "
778 "wFwUpdateReq=%u, wRfUpdateReq=%u",
779 status, *fw_update_req, *rf_update_req);
780 return status;
781 }
782
783 /******************************************************************************
784 * Function phNxpNciHal_txNfccClockSetCmd
785 *
786 * Description This function is called after successful download
787 * to apply the clock setting provided in config file
788 *
789 * Returns void.
790 *
791 ******************************************************************************/
phNxpNciHal_txNfccClockSetCmd(void)792 void phNxpNciHal_txNfccClockSetCmd(void) {
793 NFCSTATUS status = NFCSTATUS_FAILED;
794
795 uint8_t set_clock_cmd[] = {0x20, 0x02, 0x05, 0x01, 0xA0, 0x03, 0x01, 0x08};
796 uint8_t setClkCmdLen = sizeof(set_clock_cmd);
797 unsigned long clockSource = 0;
798 unsigned long frequency = 0;
799 uint32_t pllSetRetryCount = 3, dpllSetRetryCount = 3,
800 setClockCmdWriteRetryCnt = 0;
801 uint8_t* pCmd4PllSetting = NULL;
802 uint8_t* pCmd4DpllSetting = NULL;
803 uint32_t pllCmdLen = 0, dpllCmdLen = 0;
804 int srcCfgFound = 0, freqCfgFound = 0;
805
806 srcCfgFound = (GetNxpNumValue(NAME_NXP_SYS_CLK_SRC_SEL, &clockSource,
807 sizeof(clockSource)) > 0);
808
809 freqCfgFound = (GetNxpNumValue(NAME_NXP_SYS_CLK_FREQ_SEL, &frequency,
810 sizeof(frequency)) > 0);
811
812 NXPLOG_NCIHAL_D("%s : clock source = %lu, frequency = %lu", __FUNCTION__,
813 clockSource, frequency);
814
815 if (srcCfgFound && freqCfgFound && (clockSource == CLK_SRC_PLL)) {
816 phNxpNciClock.isClockSet = TRUE;
817
818 switch (frequency) {
819 case CLK_FREQ_13MHZ: {
820 NXPLOG_NCIHAL_D("PLL setting for CLK_FREQ_13MHZ");
821 pCmd4PllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_PLL_13MHZ;
822 pllCmdLen = sizeof(PN557_SET_CONFIG_CMD_PLL_13MHZ);
823 pCmd4DpllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_DPLL_13MHZ;
824 dpllCmdLen = sizeof(PN557_SET_CONFIG_CMD_DPLL_13MHZ);
825 break;
826 }
827 case CLK_FREQ_19_2MHZ: {
828 NXPLOG_NCIHAL_D("PLL setting for CLK_FREQ_19_2MHZ");
829 pCmd4PllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_PLL_19_2MHZ;
830 pllCmdLen = sizeof(PN557_SET_CONFIG_CMD_PLL_19_2MHZ);
831 pCmd4DpllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_DPLL_19_2MHZ;
832 dpllCmdLen = sizeof(PN557_SET_CONFIG_CMD_DPLL_19_2MHZ);
833 break;
834 }
835 case CLK_FREQ_24MHZ: {
836 NXPLOG_NCIHAL_D("PLL setting for CLK_FREQ_24MHZ");
837 pCmd4PllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_PLL_24MHZ;
838 pllCmdLen = sizeof(PN557_SET_CONFIG_CMD_PLL_24MHZ);
839 pCmd4DpllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_DPLL_24MHZ;
840 dpllCmdLen = sizeof(PN557_SET_CONFIG_CMD_DPLL_24MHZ);
841 break;
842 }
843 case CLK_FREQ_26MHZ: {
844 NXPLOG_NCIHAL_D("PLL setting for CLK_FREQ_26MHZ");
845 pCmd4PllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_PLL_26MHZ;
846 pllCmdLen = sizeof(PN557_SET_CONFIG_CMD_PLL_26MHZ);
847 pCmd4DpllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_DPLL_26MHZ;
848 dpllCmdLen = sizeof(PN557_SET_CONFIG_CMD_DPLL_26MHZ);
849 break;
850 }
851 case CLK_FREQ_32MHZ: {
852 NXPLOG_NCIHAL_D("PLL setting for CLK_FREQ_32MHZ");
853 pCmd4PllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_PLL_32MHZ;
854 pllCmdLen = sizeof(PN557_SET_CONFIG_CMD_PLL_32MHZ);
855 pCmd4DpllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_DPLL_32MHZ;
856 dpllCmdLen = sizeof(PN557_SET_CONFIG_CMD_DPLL_32MHZ);
857 break;
858 }
859 case CLK_FREQ_38_4MHZ: {
860 NXPLOG_NCIHAL_D("PLL setting for CLK_FREQ_38_4MHZ");
861 pCmd4PllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_PLL_38_4MHZ;
862 pllCmdLen = sizeof(PN557_SET_CONFIG_CMD_PLL_38_4MHZ);
863 pCmd4DpllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_DPLL_38_4MHZ;
864 dpllCmdLen = sizeof(PN557_SET_CONFIG_CMD_DPLL_38_4MHZ);
865 break;
866 }
867 case CLK_FREQ_48MHZ: {
868 NXPLOG_NCIHAL_D("PLL setting for CLK_FREQ_48MHZ");
869 pCmd4PllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_PLL_48MHZ;
870 pllCmdLen = sizeof(PN557_SET_CONFIG_CMD_PLL_48MHZ);
871 pCmd4DpllSetting = (uint8_t*)PN557_SET_CONFIG_CMD_DPLL_48MHZ;
872 dpllCmdLen = sizeof(PN557_SET_CONFIG_CMD_DPLL_48MHZ);
873 break;
874 }
875 default:
876 phNxpNciClock.isClockSet = FALSE;
877 NXPLOG_NCIHAL_E("ERROR: Invalid clock frequency!!");
878 return;
879 }
880 }
881 switch (clockSource) {
882 case CLK_SRC_PLL: {
883 set_clock_cmd[setClkCmdLen - 1] = 0x00;
884 while (status != NFCSTATUS_SUCCESS &&
885 setClockCmdWriteRetryCnt++ < MAX_RETRY_COUNT)
886 status = phNxpNciHal_send_ext_cmd(setClkCmdLen, set_clock_cmd);
887
888 status = NFCSTATUS_FAILED;
889
890 while (status != NFCSTATUS_SUCCESS && pllSetRetryCount-- > 0)
891 status = phNxpNciHal_send_ext_cmd(pllCmdLen, pCmd4PllSetting);
892
893 status = NFCSTATUS_FAILED;
894
895 while (status != NFCSTATUS_SUCCESS && dpllSetRetryCount-- > 0)
896 status = phNxpNciHal_send_ext_cmd(dpllCmdLen, pCmd4DpllSetting);
897
898 break;
899 }
900 case CLK_SRC_XTAL: {
901 status = phNxpNciHal_send_ext_cmd(setClkCmdLen, set_clock_cmd);
902 if (status != NFCSTATUS_SUCCESS) {
903 NXPLOG_NCIHAL_E("XTAL clock setting failed !!");
904 }
905 break;
906 }
907 default:
908 NXPLOG_NCIHAL_E("Wrong clock source. Don't apply any modification");
909 return;
910 }
911 phNxpNciClock.isClockSet = FALSE;
912 if (status == NFCSTATUS_SUCCESS &&
913 phNxpNciClock.p_rx_data[3] == NFCSTATUS_SUCCESS) {
914 NXPLOG_NCIHAL_D("PLL and DPLL settings applied successfully");
915 }
916 return;
917 }
918