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