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