1 /*
2  * Copyright 2019-2021 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 #include <android-base/file.h>
19 #include <android-base/parseint.h>
20 #include <android-base/strings.h>
21 #include <map>
22 #include <set>
23 #include "EseAdaptation.h"
24 #include "NfccTransport.h"
25 #include "NfccTransportFactory.h"
26 #include "phDnldNfc_Internal.h"
27 #include "phNfcCommon.h"
28 #include "phNxpNciHal_Adaptation.h"
29 #include "phNxpNciHal_ext.h"
30 #include "phNxpNciHal_extOperations.h"
31 #include "phNxpNciHal_utils.h"
32 
33 using android::base::WriteStringToFile;
34 using namespace ::std;
35 using namespace ::android::base;
36 
37 #define TERMINAL_LEN 5
38 /* HAL_NFC_STATUS_REFUSED sent to restart NFC service */
39 #define HAL_NFC_STATUS_RESTART HAL_NFC_STATUS_REFUSED
40 
41 /****************************************************************
42  * Global Variables Declaration
43  ***************************************************************/
44 /* External global variable to get FW version from NCI response*/
45 extern uint32_t wFwVerRsp;
46 /* External global variable to get FW version from FW file*/
47 extern uint16_t wFwVer;
48 /* NCI HAL Control structure */
49 extern phNxpNciHal_Control_t nxpncihal_ctrl;
50 extern phNxpNci_getCfg_info_t* mGetCfg_info;
51 extern EseAdaptation* gpEseAdapt;
52 extern nfc_stack_callback_t* p_nfc_stack_cback_backup;
53 #ifndef FW_DWNLD_FLAG
54 extern uint8_t fw_dwnld_flag;
55 #endif
56 
57 /* TML Context */
58 extern phTmlNfc_Context_t* gpphTmlNfc_Context;
59 extern bool nfc_debug_enabled;
60 extern NFCSTATUS phNxpLog_EnableDisableLogLevel(uint8_t enable);
61 
62 /*******************************************************************************
63  **
64  ** Function:        property_get_intf()
65  **
66  ** Description:     Gets property value for the input property name
67  **
68  ** Parameters       propName:   Name of the property whichs value need to get
69  **                  valueStr:   output value of the property.
70  **                  defaultStr: default value of the property if value is not
71  **                              there this will be set to output value.
72  **
73  ** Returns:         actual length of the property value
74  **
75  ********************************************************************************/
property_get_intf(const char * propName,char * valueStr,const char * defaultStr)76 int property_get_intf(const char* propName, char* valueStr,
77                       const char* defaultStr) {
78   string paramPropName = propName;
79   string propValue;
80   string propValueDefault = defaultStr;
81   int len = 0;
82 
83   propValue = phNxpNciHal_getSystemProperty(paramPropName);
84   if (propValue.length() > 0) {
85     NXPLOG_NCIHAL_D("property_get_intf , key[%s], propValue[%s], length[%zu]",
86                     propName, propValue.c_str(), propValue.length());
87     len = propValue.length();
88     strlcpy(valueStr, propValue.c_str(), PROPERTY_VALUE_MAX);
89   } else {
90     if (propValueDefault.length() > 0) {
91       len = propValueDefault.length();
92       strlcpy(valueStr, propValueDefault.c_str(), PROPERTY_VALUE_MAX);
93     }
94   }
95 
96   return len;
97 }
98 
99 /*******************************************************************************
100  **
101  ** Function:        property_set_intf()
102  **
103  ** Description:     Sets property value for the input property name
104  **
105  ** Parameters       propName:   Name of the property whichs value need to set
106  **                  valueStr:   value of the property.
107  **
108  ** Returns:        returns 0 on success, < 0 on failure
109  **
110  ********************************************************************************/
property_set_intf(const char * propName,const char * valueStr)111 int property_set_intf(const char* propName, const char* valueStr) {
112   string paramPropName = propName;
113   string propValue = valueStr;
114   NXPLOG_NCIHAL_D("property_set_intf, key[%s], value[%s]", propName, valueStr);
115   if (phNxpNciHal_setSystemProperty(paramPropName, propValue))
116     return NFCSTATUS_SUCCESS;
117   else
118     return NFCSTATUS_FAILED;
119 }
120 
121 extern size_t readConfigFile(const char* fileName, uint8_t** p_data);
122 
123 static string phNxpNciHal_parseBytesString(string in);
124 static bool phNxpNciHal_parseValueFromString(string& in);
125 static bool phNxpNciHal_CheckKeyNeeded(string key);
126 static string phNxpNciHal_UpdatePwrStateConfigs(string& config);
127 static bool phNxpNciHal_IsAutonmousModeSet(string config);
128 static string phNxpNciHal_extractConfig(string& config);
129 static void phNxpNciHal_getFilteredConfig(string& config);
130 
131 typedef std::map<std::string, std::string> systemProperty;
132 systemProperty gsystemProperty = {
133     {"nfc.nxp_log_level_global", ""},
134     {"nfc.nxp_log_level_extns", ""},
135     {"nfc.nxp_log_level_hal", ""},
136     {"nfc.nxp_log_level_nci", ""},
137     {"nfc.nxp_log_level_dnld", ""},
138     {"nfc.nxp_log_level_tml", ""},
139     {"nfc.fw.dfl", ""},
140     {"nfc.fw.downloadmode_force", ""},
141     {"nfc.debug_enabled", ""},
142     {"nfc.product.support.ese", ""},
143     {"nfc.product.support.uicc", ""},
144     {"nfc.product.support.uicc2", ""},
145     {"nfc.fw.rfreg_ver", ""},
146     {"nfc.fw.rfreg_display_ver", ""},
147     {"nfc.fw.dfl_areacode", ""},
148     {"nfc.cover.cover_id", ""},
149     {"nfc.cover.state", ""},
150 };
151 const char default_nxp_config_path[] = "/vendor/etc/libnfc-nxp.conf";
152 std::set<string> gNciConfigs = {"NXP_SE_COLD_TEMP_ERROR_DELAY",
153                                 "NXP_SWP_RD_TAG_OP_TIMEOUT",
154                                 "NXP_DUAL_UICC_ENABLE",
155                                 "DEFAULT_AID_ROUTE",
156                                 "DEFAULT_MIFARE_CLT_ROUTE",
157                                 "DEFAULT_FELICA_CLT_ROUTE",
158                                 "DEFAULT_AID_PWR_STATE",
159                                 "DEFAULT_DESFIRE_PWR_STATE",
160                                 "DEFAULT_MIFARE_CLT_PWR_STATE",
161                                 "DEFAULT_FELICA_CLT_PWR_STATE",
162                                 "HOST_LISTEN_TECH_MASK",
163                                 "FORWARD_FUNCTIONALITY_ENABLE",
164                                 "DEFAULT_GSMA_PWR_STATE",
165                                 "NXP_DEFAULT_UICC2_SELECT",
166                                 "NXP_SMB_TRANSCEIVE_TIMEOUT",
167                                 "NXP_SMB_ERROR_RETRY",
168                                 "NXP_CHECK_DEFAULT_PROTO_SE_ID",
169                                 "NXPLOG_NCIHAL_LOGLEVEL",
170                                 "NXPLOG_EXTNS_LOGLEVEL",
171                                 "NXPLOG_TML_LOGLEVEL",
172                                 "NXPLOG_FWDNLD_LOGLEVEL",
173                                 "NXPLOG_NCIX_LOGLEVEL",
174                                 "NXPLOG_NCIR_LOGLEVEL",
175                                 "NXP_NFC_SE_TERMINAL_NUM",
176                                 "NXP_POLL_FOR_EFD_TIMEDELAY",
177                                 "NXP_NFCC_MERGE_SAK_ENABLE",
178                                 "NXP_STAG_TIMEOUT_CFG",
179                                 "DEFAULT_T4TNFCEE_AID_POWER_STATE",
180                                 "RF_STORAGE",
181                                 "FW_STORAGE",
182                                 "NXP_CORE_CONF",
183                                 "NXP_RF_FILE_VERSION_INFO",
184                                 "NXP_AUTONOMOUS_ENABLE",
185                                 "NXP_PROP_RESET_EMVCO_CMD",
186                                 "NFA_CONFIG_FORMAT",
187                                 "NXP_T4T_NFCEE_ENABLE",
188                                 "NXP_DISCONNECT_TAG_IN_SCRN_OFF",
189                                 "NXP_RDR_REQ_GUARD_TIME",
190                                 "OFF_HOST_SIM2_PIPE_ID",
191                                 "NXP_ENABLE_DISABLE_LOGS",
192                                 "NXP_RDR_DISABLE_ENABLE_LPCD",
193                                 "NXP_SUPPORT_NON_STD_CARD",
194                                 "NXP_GET_HW_INFO_LOG",
195                                 "NXP_WLC_MODE",
196                                 "NXP_T4T_NDEF_NFCEE_AID",
197                                 "NXP_NON_STD_CARD_TIMEDIFF",
198                                 "NXP_SRD_TIMEOUT"};
199 
200 /****************************************************************
201  * Local Functions
202  ***************************************************************/
203 
204 /******************************************************************************
205  ** Function         phNxpNciHal_ioctlIf
206  **
207  ** Description      This function shall be called from HAL when libnfc-nci
208  **                  calls phNxpNciHal_ioctl() to perform any IOCTL operation
209  **
210  ** Returns          return 0 on success and -1 on fail,
211  ******************************************************************************/
phNxpNciHal_ioctlIf(long arg,void * p_data)212 int phNxpNciHal_ioctlIf(long arg, void* p_data) {
213   NXPLOG_NCIHAL_D("%s : enter - arg = %ld", __func__, arg);
214   ese_nxp_IoctlInOutData_t* pInpOutData = (ese_nxp_IoctlInOutData_t*)p_data;
215   int ret = -1;
216 
217   switch (arg) {
218     case HAL_ESE_IOCTL_NFC_JCOP_DWNLD:
219       if (pInpOutData == NULL) {
220         NXPLOG_NCIHAL_E("%s : received invalid param", __func__);
221         break;
222       }
223 
224       if (gpEseAdapt == NULL) {
225         gpEseAdapt = &EseAdaptation::GetInstance();
226         if (gpEseAdapt == NULL) {
227           NXPLOG_NCIHAL_E("%s :invalid gpEseAdapt param", __func__);
228           break;
229         }
230         gpEseAdapt->Initialize();
231       }
232 
233       NXPLOG_NCIHAL_D("HAL_ESE_IOCTL_NFC_JCOP_DWNLD Enter value is %d: \n",
234                       pInpOutData->inp.data.nxpCmd.p_cmd[0]);
235 
236       gpEseAdapt->HalIoctl(HAL_ESE_IOCTL_NFC_JCOP_DWNLD, pInpOutData);
237       ret = 0;
238       break;
239     default:
240       NXPLOG_NCIHAL_E("%s : Wrong arg = %ld", __func__, arg);
241       break;
242   }
243   NXPLOG_NCIHAL_D("%s : exit - ret = %d", __func__, ret);
244   return ret;
245 }
246 
247 /*******************************************************************************
248  **
249  ** Function         phNxpNciHal_savePersistLog
250  **
251  ** Description      Save persist log with “reason” at available index.
252  **
253  ** Parameters       uint8_t reason
254  **
255  ** Returns          returns the  index of saved reason/Log.
256  *******************************************************************************/
phNxpNciHal_savePersistLog(uint8_t reason)257 uint8_t phNxpNciHal_savePersistLog(uint8_t reason) {
258   (void)reason;
259   uint8_t index = 1;
260   NXPLOG_NCIHAL_D(" %s returning index %d", __func__, index);
261   return index;
262 }
263 
264 /*******************************************************************************
265  **
266  ** Function         phNxpNciHal_loadPersistLog
267  **
268  ** Description      If given index is valid, return a log at the given index.
269  **
270  ** Parameters       uint8_t index
271  **
272  ** Returns          If index found, return a log as string else
273  **                  return a "" string
274  *******************************************************************************/
phNxpNciHal_loadPersistLog(uint8_t index)275 string phNxpNciHal_loadPersistLog(uint8_t index) {
276   string reason;
277   switch (index) {
278     case 1:
279       NXPLOG_NCIHAL_D("index found");
280       reason = "Reason";
281       break;
282     default:
283       NXPLOG_NCIHAL_E("index not found");
284   }
285   return reason;
286 }
287 /*******************************************************************************
288  **
289  ** Function         phNxpNciHal_getSystemProperty
290  **
291  ** Description      It shall be used to get property value of the given Key
292  **
293  ** Parameters       string key
294  **
295  ** Returns          If Key is found, returns the respective property values
296  **                  else returns the null/empty string
297  *******************************************************************************/
phNxpNciHal_getSystemProperty(string key)298 string phNxpNciHal_getSystemProperty(string key) {
299   string propValue;
300   std::map<std::string, std::string>::iterator prop;
301 
302   if (key == "libnfc-nxp.conf") {
303     return phNxpNciHal_getNxpConfigIf();
304   } else {
305     prop = gsystemProperty.find(key);
306     if (prop != gsystemProperty.end()) {
307       propValue = prop->second;
308     } else {
309       /* else Pass a null string */
310     }
311   }
312   return propValue;
313 }
314 /*******************************************************************************
315  **
316  ** Function         phNxpNciHal_setSystemProperty
317  **
318  ** Description      It shall be used to save/change value to system property
319  **                  based on provided key.
320  **
321  ** Parameters       string key, string value
322  **
323  ** Returns          true if success, false if fail
324  *******************************************************************************/
phNxpNciHal_setSystemProperty(string key,string value)325 bool phNxpNciHal_setSystemProperty(string key, string value) {
326   bool stat = true;
327   if (strcmp(key.c_str(), "nfc.debug_enabled") != 0)
328     NXPLOG_NCIHAL_D("%s : Enter Key = %s, value = %s", __func__, key.c_str(),
329                     value.c_str());
330 
331   unsigned tmp = 0;
332   if (strcmp(key.c_str(), "nfc.debug_enabled") == 0) {
333     ParseUint(value.c_str(), &tmp);
334     if (phNxpLog_EnableDisableLogLevel((uint8_t)tmp) != NFCSTATUS_SUCCESS) {
335       stat = false;
336     }
337   } else if (strcmp(key.c_str(), "nfc.cover.state") == 0) {
338     unsigned cid, cstate;
339     string strtmp;
340     ParseUint(value.c_str(), &cstate);
341     strtmp = phNxpNciHal_getSystemProperty("nfc.cover.cover_id");
342     ParseUint(strtmp.c_str(), &cid);
343     if (fpPropConfCover != NULL) {
344       stat = (fpPropConfCover(cstate, cid) == NFCSTATUS_SUCCESS) ? true : false;
345     }
346   }
347 
348   gsystemProperty[key] = value;
349   return stat;
350 }
351 
352 /*******************************************************************************
353 **
354 ** Function         phNxpNciHal_getNxpConfig
355 **
356 ** Description      It shall be used to read config values from the
357 *libnfc-nxp.conf
358 **
359 ** Parameters       nxpConfigs config
360 **
361 ** Returns          void
362 *******************************************************************************/
phNxpNciHal_getNxpConfigIf()363 string phNxpNciHal_getNxpConfigIf() {
364   std::string config;
365   uint8_t* p_config = nullptr;
366   size_t config_size = readConfigFile(default_nxp_config_path, &p_config);
367   if (config_size) {
368     config.assign((char*)p_config, config_size);
369     free(p_config);
370     phNxpNciHal_getFilteredConfig(config);
371   }
372   return config;
373 }
374 
375 /*******************************************************************************
376 **
377 ** Function         phNxpNciHal_getFilteredConfig
378 **
379 ** Description      It reads only configs needed for libnfc from
380 *                   libnfc-nxp.conf
381 **
382 ** Parameters       string config
383 **
384 ** Returns          void
385 *******************************************************************************/
phNxpNciHal_getFilteredConfig(string & config)386 static void phNxpNciHal_getFilteredConfig(string& config) {
387   config = phNxpNciHal_extractConfig(config);
388 
389   if (phNxpNciHal_IsAutonmousModeSet(config)) {
390     config = phNxpNciHal_UpdatePwrStateConfigs(config);
391   }
392 }
393 
394 /*******************************************************************************
395 **
396 ** Function         phNxpNciHal_extractConfig
397 **
398 ** Description      It parses complete config file and extracts only
399 *                   enabled options ignores comments etc.
400 **
401 ** Parameters       string config
402 **
403 ** Returns          Resultant string
404 *******************************************************************************/
phNxpNciHal_extractConfig(string & config)405 static string phNxpNciHal_extractConfig(string& config) {
406   stringstream ss(config);
407   string line;
408   string result;
409   bool apduGate = false;
410   while (getline(ss, line)) {
411     line = Trim(line);
412     if (line.empty()) continue;
413     if (line.at(0) == '#') continue;
414     if (line.at(0) == 0) continue;
415 
416     auto search = line.find('=');
417     if (search == string::npos) continue;
418 
419     string key(Trim(line.substr(0, search)));
420     if (!phNxpNciHal_CheckKeyNeeded(key)) continue;
421     if (key == "NXP_NFC_SE_TERMINAL_NUM" && !apduGate) {
422       line = "NXP_SE_APDU_GATE_SUPPORT=0x01\n";
423       result += line;
424       apduGate = true;
425       continue;
426     }
427     string value_string(Trim(line.substr(search + 1, string::npos)));
428 
429     if (!phNxpNciHal_parseValueFromString(value_string)) continue;
430 
431     line = key + "=" + value_string + "\n";
432     result += line;
433     if (key == "NXP_GET_HW_INFO_LOG" &&
434         (value_string == "1" || value_string == "0x01")) {
435       if (!apduGate) {
436         line = "NXP_SE_APDU_GATE_SUPPORT=0x01\n";
437         result += line;
438         apduGate = true;
439       }
440     }
441   }
442 
443   return result;
444 }
445 
446 /*******************************************************************************
447 **
448 ** Function         phNxpNciHal_IsAutonmousModeSet
449 **
450 ** Description      It check whether autonomous mode is enabled
451 *                   in config file
452 **
453 ** Parameters       string config
454 **
455 ** Returns          boolean(TRUE/FALSE)
456 *******************************************************************************/
phNxpNciHal_IsAutonmousModeSet(string config)457 static bool phNxpNciHal_IsAutonmousModeSet(string config) {
458   stringstream ss(config);
459   string line;
460   unsigned tmp = 0;
461   while (getline(ss, line)) {
462     auto search = line.find('=');
463     if (search == string::npos) continue;
464 
465     string key(Trim(line.substr(0, search)));
466     if (key == "NXP_AUTONOMOUS_ENABLE") {
467       string value(Trim(line.substr(search + 1, string::npos)));
468       if (ParseUint(value.c_str(), &tmp)) {
469         if (tmp == 1) {
470           return true;
471         } else {
472           NXPLOG_NCIHAL_D("Autonomous flag disabled");
473           return false;
474         }
475       }
476     } else {
477       continue;
478     }
479   }
480   NXPLOG_NCIHAL_D("Autonomous flag disabled");
481   return false;
482 }
483 
484 /*******************************************************************************
485 **
486 ** Function         phNxpNciHal_UpdatePwrStateConfigs
487 **
488 ** Description      Updates default pwr state accordingly if autonomous mode
489 *                   is enabled
490 **
491 ** Parameters       string config
492 **
493 ** Returns          Resultant string
494 *******************************************************************************/
phNxpNciHal_UpdatePwrStateConfigs(string & config)495 static string phNxpNciHal_UpdatePwrStateConfigs(string& config) {
496   stringstream ss(config);
497   string line;
498   string result;
499   unsigned tmp = 0;
500   while (getline(ss, line)) {
501     auto search = line.find('=');
502     if (search == string::npos) continue;
503 
504     string key(Trim(line.substr(0, search)));
505     if ((key == "DEFAULT_AID_PWR_STATE" || key == "DEFAULT_DESFIRE_PWR_STATE" ||
506          key == "DEFAULT_MIFARE_CLT_PWR_STATE" ||
507          key == "DEFAULT_FELICA_CLT_PWR_STATE")) {
508       string value(Trim(line.substr(search + 1, string::npos)));
509       if (ParseUint(value.c_str(), &tmp)) {
510         tmp = phNxpNciHal_updateAutonomousPwrState(tmp);
511         value = to_string(tmp);
512         line = key + "=" + value + "\n";
513         result += line;
514       }
515     } else {
516       result += (line + "\n");
517       continue;
518     }
519   }
520   return result;
521 }
522 
523 /*******************************************************************************
524 **
525 ** Function         phNxpNciHal_CheckKeyNeeded
526 **
527 ** Description      Check if the config needed for libnfc as per gNciConfigs
528 *                   list
529 **
530 ** Parameters       string config
531 **
532 ** Returns          bool(true/false)
533 *******************************************************************************/
phNxpNciHal_CheckKeyNeeded(string key)534 static bool phNxpNciHal_CheckKeyNeeded(string key) {
535   return ((gNciConfigs.find(key) != gNciConfigs.end()) ? true : false);
536 }
537 
538 /*******************************************************************************
539 **
540 ** Function         phNxpNciHal_parseValueFromString
541 **
542 ** Description      Parse value determine data type of config option
543 **
544 ** Parameters       string config
545 **
546 ** Returns          bool(true/false)
547 *******************************************************************************/
phNxpNciHal_parseValueFromString(string & in)548 static bool phNxpNciHal_parseValueFromString(string& in) {
549   unsigned tmp = 0;
550   bool stat = false;
551   if (in.length() >= 1) {
552     switch (in[0]) {
553       case '"':
554         if (in[in.length() - 1] == '"' && in.length() > 2) stat = true;
555         break;
556       case '{':
557         if (in[in.length() - 1] == '}' && in.length() >= 3) {
558           in = phNxpNciHal_parseBytesString(in);
559           stat = true;
560         }
561         break;
562       default:
563         if (ParseUint(in.c_str(), &tmp)) stat = true;
564         break;
565     }
566   } else {
567     NXPLOG_NCIHAL_E("%s : Invalid config string ", __func__);
568   }
569   return stat;
570 }
571 
572 /*******************************************************************************
573 **
574 ** Function         phNxpNciHal_parseBytesString
575 **
576 ** Description      Parse bytes from string
577 **
578 ** Parameters       string config
579 **
580 ** Returns          Resultant string
581 *******************************************************************************/
phNxpNciHal_parseBytesString(string in)582 static string phNxpNciHal_parseBytesString(string in) {
583   size_t pos;
584   in.erase(remove(in.begin(), in.end(), ' '), in.end());
585   pos = in.find(",");
586   while (pos != string::npos) {
587     in = in.replace(pos, 1, ":");
588     pos = in.find(",", pos);
589   }
590   return in;
591 }
592 
593 /*******************************************************************************
594 **
595 ** Function         phNxpNciHal_resetEse
596 **
597 ** Description      It shall be used to reset eSE by proprietary command.
598 **
599 ** Parameters
600 **
601 ** Returns          status of eSE reset response
602 *******************************************************************************/
phNxpNciHal_resetEse(uint64_t resetType)603 NFCSTATUS phNxpNciHal_resetEse(uint64_t resetType) {
604   NFCSTATUS status = NFCSTATUS_FAILED;
605 
606   if (nxpncihal_ctrl.halStatus == HAL_STATUS_CLOSE) {
607     if (NFCSTATUS_SUCCESS != phNxpNciHal_MinOpen()) {
608       return NFCSTATUS_FAILED;
609     }
610   }
611 
612   CONCURRENCY_LOCK();
613   status = gpTransportObj->EseReset(gpphTmlNfc_Context->pDevHandle,
614                                     (EseResetType)resetType);
615   CONCURRENCY_UNLOCK();
616   if (status != NFCSTATUS_SUCCESS) {
617     NXPLOG_NCIHAL_E("EsePowerCycle failed");
618   }
619 
620   if (nxpncihal_ctrl.halStatus == HAL_STATUS_MIN_OPEN) {
621     phNxpNciHal_close(false);
622   }
623 
624   return status;
625 }
626 
627 /******************************************************************************
628  * Function         phNxpNciHal_setNxpTransitConfig
629  *
630  * Description      This function overwrite libnfc-nxpTransit.conf file
631  *                  with transitConfValue.
632  *
633  * Returns          bool.
634  *
635  ******************************************************************************/
phNxpNciHal_setNxpTransitConfig(char * transitConfValue)636 bool phNxpNciHal_setNxpTransitConfig(char* transitConfValue) {
637   bool status = true;
638   NXPLOG_NCIHAL_D("%s : Enter", __func__);
639   std::string transitConfFileName = "/data/vendor/nfc/libnfc-nxpTransit.conf";
640   long transitConfValueLen = strlen(transitConfValue) + 1;
641 
642   if (transitConfValueLen > 1) {
643     if (!WriteStringToFile(transitConfValue, transitConfFileName)) {
644       NXPLOG_NCIHAL_E("WriteStringToFile: Failed");
645       status = false;
646     }
647   } else {
648     if (!WriteStringToFile("", transitConfFileName)) {
649       NXPLOG_NCIHAL_E("WriteStringToFile: Failed");
650       status = false;
651     }
652     if (remove(transitConfFileName.c_str())) {
653       NXPLOG_NCIHAL_E("Unable to remove file");
654       status = false;
655     }
656   }
657   NXPLOG_NCIHAL_D("%s : Exit", __func__);
658   return status;
659 }
660 
661 /******************************************************************************
662 ** Function         phNxpNciHal_Abort
663 **
664 ** Description      This function shall be used to trigger the abort in libnfc
665 **
666 ** Parameters       None
667 **
668 ** Returns          bool.
669 **
670 *******************************************************************************/
phNxpNciHal_Abort()671 bool phNxpNciHal_Abort() {
672   bool ret = true;
673 
674   NXPLOG_NCIHAL_D("phNxpNciHal_Abort aborting. \n");
675   /* When JCOP download is triggered phNxpNciHal_open is blocked, in this case
676      only we need to abort the libnfc , this can be done only by check the
677      p_nfc_stack_cback_backup pointer which is assigned before the JCOP
678      download.*/
679   if (p_nfc_stack_cback_backup != NULL) {
680     (*p_nfc_stack_cback_backup)(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_RESTART);
681   } else {
682     ret = false;
683     NXPLOG_NCIHAL_D("phNxpNciHal_Abort not triggered\n");
684   }
685   return ret;
686 }
687 
688 /*******************************************************************************
689  **
690  ** Function:        phNxpNciHal_CheckFwRegFlashRequired()
691  **
692  ** Description:     Updates FW and Reg configurations if required
693  **
694  ** Returns:         status
695  **
696  ********************************************************************************/
phNxpNciHal_CheckFwRegFlashRequired(uint8_t * fw_update_req,uint8_t * rf_update_req,uint8_t skipEEPROMRead)697 int phNxpNciHal_CheckFwRegFlashRequired(uint8_t* fw_update_req,
698                                         uint8_t* rf_update_req,
699                                         uint8_t skipEEPROMRead) {
700   NXPLOG_NCIHAL_D("phNxpNciHal_CheckFwRegFlashRequired() : enter");
701   int status = NFCSTATUS_OK;
702   long option;
703   if (fpRegRfFwDndl != NULL) {
704     status = fpRegRfFwDndl(fw_update_req, rf_update_req, skipEEPROMRead);
705   } else {
706     status = phDnldNfc_InitImgInfo();
707     NXPLOG_NCIHAL_D("FW version from the binary(.so/bin) = 0x%x", wFwVer);
708     NXPLOG_NCIHAL_D("FW version found on the device = 0x%x", wFwVerRsp);
709 
710     if (!GetNxpNumValue(NAME_NXP_FLASH_CONFIG, &option,
711                         sizeof(unsigned long))) {
712       NXPLOG_NCIHAL_D("Flash option not found; giving default value");
713       option = 1;
714     }
715     switch (option) {
716       case FLASH_UPPER_VERSION:
717         wFwUpdateReq = (utf8_t)wFwVer > (utf8_t)wFwVerRsp ? true : false;
718         break;
719       case FLASH_DIFFERENT_VERSION:
720         wFwUpdateReq = ((wFwVerRsp & 0x0000FFFF) != wFwVer) ? true : false;
721         break;
722       case FLASH_ALWAYS:
723         wFwUpdateReq = true;
724         break;
725       default:
726         NXPLOG_NCIHAL_D("Invalid flash option selected");
727         status = NFCSTATUS_INVALID_PARAMETER;
728         break;
729     }
730   }
731   *fw_update_req = wFwUpdateReq;
732 
733   if (false == wFwUpdateReq) {
734     NXPLOG_NCIHAL_D("FW update not required");
735     phDnldNfc_ReSetHwDevHandle();
736   } else {
737     property_set("nfc.fw.downloadmode_force", "1");
738   }
739 
740   NXPLOG_NCIHAL_D(
741       "phNxpNciHal_CheckFwRegFlashRequired() : exit - status = %x "
742       "wFwUpdateReq=%u, wRfUpdateReq=%u",
743       status, *fw_update_req, *rf_update_req);
744   return status;
745 }
746