1 /*
2  * Copyright 2012-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 #include <log/log.h>
17 #include <phDal4Nfc_messageQueueLib.h>
18 #include <phDnldNfc.h>
19 #include <phNxpConfig.h>
20 #include <phNxpLog.h>
21 #include <phNxpNciHal.h>
22 #include <phNxpNciHal_Adaptation.h>
23 #include <phNxpNciHal_NfcDepSWPrio.h>
24 #include <phNxpNciHal_ext.h>
25 #include <phTmlNfc.h>
26 #if (NXP_EXTNS == TRUE)
27 #include "phNxpNciHal.h"
28 #include "phNxpNciHal_IoctlOperations.h"
29 #include "phNxpNciHal_nciParser.h"
30 #endif
31 /* Timeout value to wait for response from PN548AD */
32 #define HAL_EXTNS_WRITE_RSP_TIMEOUT (1000)
33 
34 #undef P2P_PRIO_LOGIC_HAL_IMP
35 
36 /******************* Global variables *****************************************/
37 extern phNxpNciHal_Control_t nxpncihal_ctrl;
38 extern phNxpNciProfile_Control_t nxpprofile_ctrl;
39 extern phNxpNci_getCfg_info_t* mGetCfg_info;
40 
41 extern bool_t gsIsFwRecoveryRequired;
42 
43 extern uint32_t cleanup_timer;
44 extern bool nfc_debug_enabled;
45 uint8_t icode_detected = 0x00;
46 uint8_t icode_send_eof = 0x00;
47 static uint8_t ee_disc_done = 0x00;
48 uint8_t EnableP2P_PrioLogic = false;
49 extern bool bEnableMfcExtns;
50 extern bool bEnableMfcReader;
51 extern bool bDisableLegacyMfcExtns;
52 static uint32_t RfDiscID = 1;
53 static uint32_t RfProtocolType = 4;
54 /* NFCEE Set mode */
55 static uint8_t setEEModeDone = 0x00;
56 /* External global variable to get FW version from NCI response*/
57 extern uint32_t wFwVerRsp;
58 /* External global variable to get FW version from FW file*/
59 extern uint16_t wFwVer;
60 extern bool_t gParserCreated;
61 uint16_t fw_maj_ver;
62 uint16_t rom_version;
63 /* local buffer to store CORE_INIT response */
64 static uint32_t bCoreInitRsp[40];
65 static uint32_t iCoreInitRspLen;
66 
67 extern uint32_t timeoutTimerId;
68 
69 /************** HAL extension functions ***************************************/
70 static void hal_extns_write_rsp_timeout_cb(uint32_t TimerId, void* pContext);
71 
72 /*Proprietary cmd sent to HAL to send reader mode flag
73  * Last byte of 4 byte proprietary cmd data contains ReaderMode flag
74  * If this flag is enabled, NFC-DEP protocol is modified to T3T protocol
75  * if FrameRF interface is selected. This needs to be done as the FW
76  * always sends Ntf for FrameRF with NFC-DEP even though FrameRF with T3T is
77  * previously selected with DISCOVER_SELECT_CMD
78  */
79 #define PROPRIETARY_CMD_FELICA_READER_MODE 0xFE
80 static uint8_t gFelicaReaderMode;
81 static bool mfc_mode = false;
82 
83 static NFCSTATUS phNxpNciHal_ext_process_nfc_init_rsp(uint8_t* p_ntf,
84                                                       uint16_t* p_len);
85 /*******************************************************************************
86 **
87 ** Function         phNxpNciHal_ext_init
88 **
89 ** Description      initialize extension function
90 **
91 *******************************************************************************/
phNxpNciHal_ext_init(void)92 void phNxpNciHal_ext_init(void) {
93   icode_detected = 0x00;
94   if (nfcFL.chipType < sn100u) {
95     icode_send_eof = 0x00;
96   }
97   setEEModeDone = 0x00;
98   EnableP2P_PrioLogic = false;
99 }
100 
101 /*******************************************************************************
102 **
103 ** Function         phNxpNciHal_ext_send_sram_config_to_flash
104 **
105 ** Description      This function is called to update the SRAM contents such as
106 **                  set config to FLASH for permanent storage.
107 **                  Note: This function has to be called after set config and
108 **                  before sending  core_reset command again.
109 **
110 *******************************************************************************/
phNxpNciHal_ext_send_sram_config_to_flash()111 NFCSTATUS phNxpNciHal_ext_send_sram_config_to_flash() {
112   NXPLOG_NCIHAL_D("phNxpNciHal_ext_send_sram_config_to_flash  send");
113   NFCSTATUS status = NFCSTATUS_SUCCESS;
114   uint8_t send_sram_flash[] = {NXP_PROPCMD_GID, NXP_FLUSH_SRAM_AO_TO_FLASH,
115                                0x00};
116   status = phNxpNciHal_send_ext_cmd(sizeof(send_sram_flash), send_sram_flash);
117   return status;
118 }
119 /*******************************************************************************
120 **
121 ** Function         phNxpNciHal_process_ext_rsp
122 **
123 ** Description      Process extension function response
124 **
125 ** Returns          NFCSTATUS_SUCCESS if success
126 **
127 *******************************************************************************/
phNxpNciHal_process_ext_rsp(uint8_t * p_ntf,uint16_t * p_len)128 NFCSTATUS phNxpNciHal_process_ext_rsp(uint8_t* p_ntf, uint16_t* p_len) {
129   NFCSTATUS status = NFCSTATUS_SUCCESS;
130 
131 #if (NXP_EXTNS == TRUE)
132   /*parse and decode LxDebug Notifications*/
133   if (p_ntf[0] == 0x6F && (p_ntf[1] == 0x35 || p_ntf[1] == 0x36)) {
134     if (gParserCreated) phNxpNciHal_parsePacket(p_ntf, *p_len);
135   }
136 #if (NXP_SRD == TRUE)
137   if (p_ntf[0] == 0x01 && p_ntf[1] == 0x00 && p_ntf[5] == 0x81 &&
138       p_ntf[23] == 0x82 && p_ntf[26] == 0xA0 && p_ntf[27] == 0xFE) {
139     if (p_ntf[29] == 0x01) {
140       nxpprofile_ctrl.profile_type = SRD_PROFILE;
141     } else if (p_ntf[29] == 0x00) {
142       nxpprofile_ctrl.profile_type = NFC_FORUM_PROFILE;
143     }
144   } else if (p_ntf[0] == 0x60 && p_ntf[1] == 0x07 && p_ntf[2] == 0x01 &&
145              p_ntf[3] == 0xE2) {
146     nxpprofile_ctrl.profile_type = NFC_FORUM_PROFILE;
147   }
148 #endif
149 #endif
150 
151   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && *p_len < 14) {
152     if (*p_len <= 6) {
153       android_errorWriteLog(0x534e4554, "118152591");
154     }
155     NXPLOG_NCIHAL_E("RF_INTF_ACTIVATED_NTF length error!");
156     status = NFCSTATUS_FAILED;
157     return status;
158   }
159 
160   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && p_ntf[4] == 0x03 &&
161       p_ntf[5] == 0x05 && nxpprofile_ctrl.profile_type == EMV_CO_PROFILE) {
162     p_ntf[4] = 0xFF;
163     p_ntf[5] = 0xFF;
164     p_ntf[6] = 0xFF;
165     NXPLOG_NCIHAL_D("Nfc-Dep Detect in EmvCo profile - Restart polling");
166   }
167 
168   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && p_ntf[4] == 0x01 &&
169       p_ntf[5] == 0x05 && p_ntf[6] == 0x02 && gFelicaReaderMode) {
170     /*If FelicaReaderMode is enabled,Change Protocol to T3T from NFC-DEP
171      * when FrameRF interface is selected*/
172     p_ntf[5] = 0x03;
173     NXPLOG_NCIHAL_D("FelicaReaderMode:Activity 1.1");
174   }
175 
176 #ifdef P2P_PRIO_LOGIC_HAL_IMP
177   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && p_ntf[4] == 0x02 &&
178       p_ntf[5] == 0x04 && nxpprofile_ctrl.profile_type == NFC_FORUM_PROFILE) {
179     EnableP2P_PrioLogic = true;
180   }
181 
182   NXPLOG_NCIHAL_D("Is EnableP2P_PrioLogic: 0x0%X", EnableP2P_PrioLogic);
183   if (phNxpDta_IsEnable() == false) {
184     if ((icode_detected != 1) && (EnableP2P_PrioLogic == true)) {
185       if (phNxpNciHal_NfcDep_comapre_ntf(p_ntf, *p_len) == NFCSTATUS_FAILED) {
186         status = phNxpNciHal_NfcDep_rsp_ext(p_ntf, p_len);
187         if (status != NFCSTATUS_INVALID_PARAMETER) {
188           return status;
189         }
190       }
191     }
192   }
193 #endif
194 
195   status = NFCSTATUS_SUCCESS;
196 
197   if (bDisableLegacyMfcExtns && bEnableMfcExtns && p_ntf[0] == 0) {
198     uint16_t extlen;
199     extlen = *p_len - NCI_HEADER_SIZE;
200     NxpMfcReaderInstance.AnalyzeMfcResp(&p_ntf[3], &extlen);
201     p_ntf[2] = extlen;
202     *p_len = extlen + NCI_HEADER_SIZE;
203   }
204 
205   if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05) {
206     bEnableMfcExtns = false;
207     if (bDisableLegacyMfcExtns && p_ntf[4] == 0x80 && p_ntf[5] == 0x80) {
208       bEnableMfcExtns = true;
209       NXPLOG_NCIHAL_D("NxpNci: RF Interface = Mifare Enable MifareExtns");
210     }
211     switch (p_ntf[4]) {
212       case 0x00:
213         NXPLOG_NCIHAL_D("NxpNci: RF Interface = NFCEE Direct RF");
214         break;
215       case 0x01:
216         NXPLOG_NCIHAL_D("NxpNci: RF Interface = Frame RF");
217         break;
218       case 0x02:
219         NXPLOG_NCIHAL_D("NxpNci: RF Interface = ISO-DEP");
220         break;
221       case 0x03:
222         NXPLOG_NCIHAL_D("NxpNci: RF Interface = NFC-DEP");
223         break;
224       case 0x80:
225         NXPLOG_NCIHAL_D("NxpNci: RF Interface = MIFARE");
226         break;
227       default:
228         NXPLOG_NCIHAL_D("NxpNci: RF Interface = Unknown");
229         break;
230     }
231 
232     switch (p_ntf[5]) {
233       case 0x01:
234         NXPLOG_NCIHAL_D("NxpNci: Protocol = T1T");
235         phNxpDta_T1TEnable();
236         break;
237       case 0x02:
238         NXPLOG_NCIHAL_D("NxpNci: Protocol = T2T");
239         break;
240       case 0x03:
241         NXPLOG_NCIHAL_D("NxpNci: Protocol = T3T");
242         break;
243       case 0x04:
244         NXPLOG_NCIHAL_D("NxpNci: Protocol = ISO-DEP");
245         break;
246       case 0x05:
247         NXPLOG_NCIHAL_D("NxpNci: Protocol = NFC-DEP");
248         break;
249       case 0x06:
250         NXPLOG_NCIHAL_D("NxpNci: Protocol = 15693");
251         break;
252       case 0x80:
253         NXPLOG_NCIHAL_D("NxpNci: Protocol = MIFARE");
254         break;
255       case 0x81:
256         NXPLOG_NCIHAL_D("NxpNci: Protocol = Kovio");
257         break;
258       default:
259         NXPLOG_NCIHAL_D("NxpNci: Protocol = Unknown");
260         break;
261     }
262 
263     switch (p_ntf[6]) {
264       case 0x00:
265         NXPLOG_NCIHAL_D("NxpNci: Mode = A Passive Poll");
266         break;
267       case 0x01:
268         NXPLOG_NCIHAL_D("NxpNci: Mode = B Passive Poll");
269         break;
270       case 0x02:
271         NXPLOG_NCIHAL_D("NxpNci: Mode = F Passive Poll");
272         break;
273       case 0x03:
274         NXPLOG_NCIHAL_D("NxpNci: Mode = A Active Poll");
275         break;
276       case 0x05:
277         NXPLOG_NCIHAL_D("NxpNci: Mode = F Active Poll");
278         break;
279       case 0x06:
280         NXPLOG_NCIHAL_D("NxpNci: Mode = 15693 Passive Poll");
281         break;
282       case 0x70:
283         NXPLOG_NCIHAL_D("NxpNci: Mode = Kovio");
284         break;
285       case 0x80:
286         NXPLOG_NCIHAL_D("NxpNci: Mode = A Passive Listen");
287         break;
288       case 0x81:
289         NXPLOG_NCIHAL_D("NxpNci: Mode = B Passive Listen");
290         break;
291       case 0x82:
292         NXPLOG_NCIHAL_D("NxpNci: Mode = F Passive Listen");
293         break;
294       case 0x83:
295         NXPLOG_NCIHAL_D("NxpNci: Mode = A Active Listen");
296         break;
297       case 0x85:
298         NXPLOG_NCIHAL_D("NxpNci: Mode = F Active Listen");
299         break;
300       case 0x86:
301         NXPLOG_NCIHAL_D("NxpNci: Mode = 15693 Passive Listen");
302         break;
303       default:
304         NXPLOG_NCIHAL_D("NxpNci: Mode = Unknown");
305         break;
306     }
307   }
308   phNxpNciHal_ext_process_nfc_init_rsp(p_ntf, p_len);
309   if (p_ntf[0] == 0x42 && p_ntf[1] == 0x01 && p_ntf[2] == 0x01 &&
310       p_ntf[3] == 0x00) {
311     if (nxpncihal_ctrl.hal_ext_enabled == TRUE && nfcFL.chipType >= sn100u) {
312       nxpncihal_ctrl.nci_info.wait_for_ntf = TRUE;
313       NXPLOG_NCIHAL_D(" Mode set received");
314     }
315   } else if (p_ntf[0] == 0x61 && p_ntf[1] == 0x05 && p_ntf[2] == 0x15 &&
316              p_ntf[4] == 0x01 && p_ntf[5] == 0x06 && p_ntf[6] == 0x06) {
317     NXPLOG_NCIHAL_D("> Going through workaround - notification of ISO 15693");
318     icode_detected = 0x01;
319     p_ntf[21] = 0x01;
320     p_ntf[22] = 0x01;
321   } else if (nfcFL.chipType < sn100u && icode_detected == 1 &&
322              icode_send_eof == 2) {
323     icode_send_eof = 3;
324   } else if (nfcFL.chipType < sn100u && p_ntf[0] == 0x00 && p_ntf[1] == 0x00 &&
325              icode_detected == 1) {
326     if (icode_send_eof == 3) {
327       icode_send_eof = 0;
328     }
329     if (nxpncihal_ctrl.nci_info.nci_version != NCI_VERSION_2_0) {
330       if (p_ntf[p_ntf[2] + 2] == 0x00) {
331         NXPLOG_NCIHAL_D("> Going through workaround - data of ISO 15693");
332         p_ntf[2]--;
333         (*p_len)--;
334       } else {
335         p_ntf[p_ntf[2] + 2] |= 0x01;
336       }
337     }
338   } else if (nfcFL.chipType < sn100u && p_ntf[2] == 0x02 && p_ntf[1] == 0x00 &&
339              icode_detected == 1) {
340     NXPLOG_NCIHAL_D("> ICODE EOF response do not send to upper layer");
341   } else if (p_ntf[0] == 0x61 && p_ntf[1] == 0x06 && icode_detected == 1) {
342     NXPLOG_NCIHAL_D("> Polling Loop Re-Started");
343     icode_detected = 0;
344     if (nfcFL.chipType < sn100u) icode_send_eof = 0;
345   } else if (*p_len == 4 && p_ntf[0] == 0x40 && p_ntf[1] == 0x02 &&
346              p_ntf[2] == 0x01 && p_ntf[3] == 0x06) {
347     NXPLOG_NCIHAL_D("> Deinit workaround for LLCP set_config 0x%x 0x%x 0x%x",
348                     p_ntf[21], p_ntf[22], p_ntf[23]);
349     p_ntf[0] = 0x40;
350     p_ntf[1] = 0x02;
351     p_ntf[2] = 0x02;
352     p_ntf[3] = 0x00;
353     p_ntf[4] = 0x00;
354     *p_len = 5;
355   }
356 
357   if (p_ntf[0] == 0x60 && p_ntf[1] == 0x07 && p_ntf[2] == 0x01) {
358     if (p_ntf[3] == 0xEA) {
359       gsIsFwRecoveryRequired = true;
360       NXPLOG_NCIHAL_D("FW update required");
361       status = NFCSTATUS_FAILED;
362     } else if ((p_ntf[3] == 0xE5) || (p_ntf[3] == 0x60)) {
363       NXPLOG_NCIHAL_D("ignore core generic error");
364       status = NFCSTATUS_FAILED;
365     }
366     return status;
367   } else if (p_ntf[0] == 0x61 && p_ntf[1] == 0x21 && p_ntf[2] == 0x00) {
368     status = NFCSTATUS_FAILED;
369     NXPLOG_NCIHAL_D("ignore core generic error");
370     return status;
371   }
372   // 4200 02 00 01
373   else if (p_ntf[0] == 0x42 && p_ntf[1] == 0x00 && ee_disc_done == 0x01) {
374     NXPLOG_NCIHAL_D("Going through workaround - NFCEE_DISCOVER_RSP");
375     if (p_ntf[4] == 0x01) {
376       p_ntf[4] = 0x00;
377 
378       ee_disc_done = 0x00;
379     }
380     NXPLOG_NCIHAL_D("Going through workaround - NFCEE_DISCOVER_RSP - END");
381 
382   } else if (p_ntf[0] == 0x61 && p_ntf[1] == 0x03 /*&& cleanup_timer!=0*/) {
383     if (cleanup_timer != 0) {
384       /* if RF Notification Type of RF_DISCOVER_NTF is Last Notification */
385       if (0 == (*(p_ntf + 2 + (*(p_ntf + 2))))) {
386         phNxpNciHal_select_RF_Discovery(RfDiscID, RfProtocolType);
387         status = NFCSTATUS_FAILED;
388         return status;
389       } else {
390         RfDiscID = p_ntf[3];
391         RfProtocolType = p_ntf[4];
392       }
393       status = NFCSTATUS_FAILED;
394       return status;
395     }
396   } else if (p_ntf[0] == 0x41 && p_ntf[1] == 0x04 && cleanup_timer != 0) {
397     status = NFCSTATUS_FAILED;
398     return status;
399   } else if (*p_len == 4 && p_ntf[0] == 0x4F && p_ntf[1] == 0x11 &&
400              p_ntf[2] == 0x01) {
401     if (p_ntf[3] == 0x00) {
402       NXPLOG_NCIHAL_D(
403           ">  Workaround for ISO-DEP Presence Check, ignore response and wait "
404           "for notification");
405       p_ntf[0] = 0x60;
406       p_ntf[1] = 0x06;
407       p_ntf[2] = 0x03;
408       p_ntf[3] = 0x01;
409       p_ntf[4] = 0x00;
410       p_ntf[5] = 0x01;
411       *p_len = 6;
412     } else {
413       NXPLOG_NCIHAL_D(
414           ">  Workaround for ISO-DEP Presence Check, presence check return "
415           "failed");
416       p_ntf[0] = 0x60;
417       p_ntf[1] = 0x08;
418       p_ntf[2] = 0x02;
419       p_ntf[3] = 0xB2;
420       p_ntf[4] = 0x00;
421       *p_len = 5;
422     }
423   } else if (*p_len == 4 && p_ntf[0] == 0x6F && p_ntf[1] == 0x11 &&
424              p_ntf[2] == 0x01) {
425     if (p_ntf[3] == 0x01) {
426       NXPLOG_NCIHAL_D(
427           ">  Workaround for ISO-DEP Presence Check - Card still in field");
428       p_ntf[0] = 0x00;
429       p_ntf[1] = 0x00;
430       p_ntf[2] = 0x01;
431       p_ntf[3] = 0x7E;
432     } else {
433       NXPLOG_NCIHAL_D(
434           ">  Workaround for ISO-DEP Presence Check - Card not in field");
435       p_ntf[0] = 0x60;
436       p_ntf[1] = 0x08;
437       p_ntf[2] = 0x02;
438       p_ntf[3] = 0xB2;
439       p_ntf[4] = 0x00;
440       *p_len = 5;
441     }
442   }
443 
444   return status;
445 }
446 
447 /******************************************************************************
448  * Function         phNxpNciHal_ext_process_nfc_init_rsp
449  *
450  * Description      This function is used to process the HAL NFC core reset rsp
451  *                  and ntf and core init rsp of NCI 1.0 or NCI2.0 and update
452  *                  NCI version.
453  *                  It also handles error response such as core_reset_ntf with
454  *                  error status in both NCI2.0 and NCI1.0.
455  *
456  * Returns          Returns NFCSTATUS_SUCCESS if parsing response is successful
457  *                  or returns failure.
458  *
459  ******************************************************************************/
phNxpNciHal_ext_process_nfc_init_rsp(uint8_t * p_ntf,uint16_t * p_len)460 static NFCSTATUS phNxpNciHal_ext_process_nfc_init_rsp(uint8_t* p_ntf,
461                                                       uint16_t* p_len) {
462   NFCSTATUS status = NFCSTATUS_SUCCESS;
463   /* Parsing CORE_RESET_RSP and CORE_RESET_NTF to update NCI version.*/
464   if (p_ntf == NULL || *p_len == 0x00) {
465     return NFCSTATUS_FAILED;
466   }
467   if (p_ntf[0] == NCI_MT_RSP &&
468       ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_RESET)) {
469     if (p_ntf[2] == 0x01 && p_ntf[3] == 0x00) {
470       NXPLOG_NCIHAL_D("CORE_RESET_RSP NCI2.0");
471       if (nxpncihal_ctrl.hal_ext_enabled == TRUE &&
472           nxpncihal_ctrl.fwdnld_mode_reqd != TRUE) {
473         nxpncihal_ctrl.nci_info.wait_for_ntf = TRUE;
474       }
475     } else if (p_ntf[2] == 0x03 && p_ntf[3] == 0x00) {
476       NXPLOG_NCIHAL_D("CORE_RESET_RSP NCI1.0");
477       nxpncihal_ctrl.nci_info.nci_version = p_ntf[4];
478     } else
479       status = NFCSTATUS_FAILED;
480   } else if (p_ntf[0] == NCI_MT_NTF &&
481              ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_RESET)) {
482     if (p_ntf[3] == CORE_RESET_TRIGGER_TYPE_CORE_RESET_CMD_RECEIVED) {
483       NXPLOG_NCIHAL_D("CORE_RESET_NTF NCI2.0 reason CORE_RESET_CMD received !");
484       nxpncihal_ctrl.nci_info.nci_version = p_ntf[5];
485       if (!nxpncihal_ctrl.hal_open_status)
486         phNxpNciHal_configFeatureList(p_ntf, *p_len);
487       int len = p_ntf[2] + 2; /*include 2 byte header*/
488       if (len != *p_len - 1) {
489         NXPLOG_NCIHAL_E(
490             "phNxpNciHal_ext_process_nfc_init_rsp invalid NTF length");
491         android_errorWriteLog(0x534e4554, "121263487");
492         return NFCSTATUS_FAILED;
493       }
494       wFwVerRsp = (((uint32_t)p_ntf[len - 2]) << 16U) |
495                   (((uint32_t)p_ntf[len - 1]) << 8U) | p_ntf[len];
496       fw_maj_ver = p_ntf[len - 1];
497       rom_version = p_ntf[len - 2];
498       NXPLOG_NCIHAL_D("NxpNci> FW Version: %x.%x.%x", p_ntf[len - 2],
499                       p_ntf[len - 1], p_ntf[len]);
500     } else {
501       uint32_t i;
502       char print_buffer[*p_len * 3 + 1];
503 
504       memset(print_buffer, 0, sizeof(print_buffer));
505       for (i = 0; i < *p_len; i++) {
506         snprintf(&print_buffer[i * 2], 3, "%02X", p_ntf[i]);
507       }
508       NXPLOG_NCIHAL_D("CORE_RESET_NTF received !");
509       NXPLOG_NCIR_E("len = %3d > %s", *p_len, print_buffer);
510       phNxpNciHal_emergency_recovery(p_ntf[3]);
511       status = NFCSTATUS_FAILED;
512     } /* Parsing CORE_INIT_RSP*/
513   } else if (p_ntf[0] == NCI_MT_RSP &&
514              ((p_ntf[1] & NCI_OID_MASK) == NCI_MSG_CORE_INIT)) {
515     if (nxpncihal_ctrl.nci_info.nci_version == NCI_VERSION_2_0) {
516       NXPLOG_NCIHAL_D("CORE_INIT_RSP NCI2.0 received !");
517     } else {
518       NXPLOG_NCIHAL_D("CORE_INIT_RSP NCI1.0 received !");
519       if (!nxpncihal_ctrl.hal_open_status &&
520           nxpncihal_ctrl.nci_info.nci_version != NCI_VERSION_2_0) {
521         phNxpNciHal_configFeatureList(p_ntf, *p_len);
522       }
523       int len = p_ntf[2] + 2; /*include 2 byte header*/
524       if (len != *p_len - 1) {
525         NXPLOG_NCIHAL_E(
526             "phNxpNciHal_ext_process_nfc_init_rsp invalid NTF length");
527         android_errorWriteLog(0x534e4554, "121263487");
528         return NFCSTATUS_FAILED;
529       }
530       wFwVerRsp = (((uint32_t)p_ntf[len - 2]) << 16U) |
531                   (((uint32_t)p_ntf[len - 1]) << 8U) | p_ntf[len];
532       if (wFwVerRsp == 0) status = NFCSTATUS_FAILED;
533       iCoreInitRspLen = *p_len;
534       memcpy(bCoreInitRsp, p_ntf, *p_len);
535       NXPLOG_NCIHAL_D("NxpNci> FW Version: %x.%x.%x", p_ntf[len - 2],
536                       p_ntf[len - 1], p_ntf[len]);
537       fw_maj_ver = p_ntf[len - 1];
538       rom_version = p_ntf[len - 2];
539     }
540   }
541   return status;
542 }
543 
544 /******************************************************************************
545  * Function         phNxpNciHal_process_ext_cmd_rsp
546  *
547  * Description      This function process the extension command response. It
548  *                  also checks the received response to expected response.
549  *
550  * Returns          returns NFCSTATUS_SUCCESS if response is as expected else
551  *                  returns failure.
552  *
553  ******************************************************************************/
phNxpNciHal_process_ext_cmd_rsp(uint16_t cmd_len,uint8_t * p_cmd)554 static NFCSTATUS phNxpNciHal_process_ext_cmd_rsp(uint16_t cmd_len,
555                                                  uint8_t* p_cmd) {
556   NFCSTATUS status = NFCSTATUS_FAILED;
557   uint16_t data_written = 0;
558 
559   /* Create the local semaphore */
560   if (phNxpNciHal_init_cb_data(&nxpncihal_ctrl.ext_cb_data, NULL) !=
561       NFCSTATUS_SUCCESS) {
562     NXPLOG_NCIHAL_D("Create ext_cb_data failed");
563     return NFCSTATUS_FAILED;
564   }
565 
566   nxpncihal_ctrl.ext_cb_data.status = NFCSTATUS_SUCCESS;
567 
568   /* Send ext command */
569   data_written = phNxpNciHal_write_unlocked(cmd_len, p_cmd, ORIG_NXPHAL);
570   if (data_written != cmd_len) {
571     NXPLOG_NCIHAL_D("phNxpNciHal_write failed for hal ext");
572     goto clean_and_return;
573   }
574 
575   /* Start timer */
576   status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_EXTNS_WRITE_RSP_TIMEOUT,
577                                  &hal_extns_write_rsp_timeout_cb, NULL);
578   if (NFCSTATUS_SUCCESS == status) {
579     NXPLOG_NCIHAL_D("Response timer started");
580   } else {
581     NXPLOG_NCIHAL_E("Response timer not started!!!");
582     status = NFCSTATUS_FAILED;
583     goto clean_and_return;
584   }
585 
586   /* Wait for rsp */
587   NXPLOG_NCIHAL_D("Waiting after ext cmd sent");
588   if (SEM_WAIT(nxpncihal_ctrl.ext_cb_data)) {
589     NXPLOG_NCIHAL_E("p_hal_ext->ext_cb_data.sem semaphore error");
590     goto clean_and_return;
591   }
592 
593   /* Stop Timer */
594   status = phOsalNfc_Timer_Stop(timeoutTimerId);
595   if (NFCSTATUS_SUCCESS == status) {
596     NXPLOG_NCIHAL_D("Response timer stopped");
597   } else {
598     NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
599     status = NFCSTATUS_FAILED;
600     goto clean_and_return;
601   }
602   /* No NTF expected for OMAPI command */
603   if (p_cmd[0] == 0x2F && p_cmd[1] == 0x1 && p_cmd[2] == 0x01) {
604     nxpncihal_ctrl.nci_info.wait_for_ntf = FALSE;
605   }
606   /* Start timer to wait for NTF*/
607   if (nxpncihal_ctrl.nci_info.wait_for_ntf == TRUE) {
608     status = phOsalNfc_Timer_Start(timeoutTimerId, HAL_EXTNS_WRITE_RSP_TIMEOUT,
609                                    &hal_extns_write_rsp_timeout_cb, NULL);
610     if (NFCSTATUS_SUCCESS == status) {
611       NXPLOG_NCIHAL_D("Response timer started");
612     } else {
613       NXPLOG_NCIHAL_E("Response timer not started!!!");
614       status = NFCSTATUS_FAILED;
615       goto clean_and_return;
616     }
617     if (SEM_WAIT(nxpncihal_ctrl.ext_cb_data)) {
618       NXPLOG_NCIHAL_E("p_hal_ext->ext_cb_data.sem semaphore error");
619       /* Stop Timer */
620       status = phOsalNfc_Timer_Stop(timeoutTimerId);
621       goto clean_and_return;
622     }
623     status = phOsalNfc_Timer_Stop(timeoutTimerId);
624     if (NFCSTATUS_SUCCESS == status) {
625       NXPLOG_NCIHAL_D("Response timer stopped");
626     } else {
627       NXPLOG_NCIHAL_E("Response timer stop ERROR!!!");
628       status = NFCSTATUS_FAILED;
629       goto clean_and_return;
630     }
631   }
632 
633   if (nxpncihal_ctrl.ext_cb_data.status != NFCSTATUS_SUCCESS) {
634     NXPLOG_NCIHAL_E(
635         "Callback Status is failed!! Timer Expired!! Couldn't read it! 0x%x",
636         nxpncihal_ctrl.ext_cb_data.status);
637     status = NFCSTATUS_FAILED;
638     goto clean_and_return;
639   }
640 
641   NXPLOG_NCIHAL_D("Checking response");
642   status = NFCSTATUS_SUCCESS;
643 
644 clean_and_return:
645   phNxpNciHal_cleanup_cb_data(&nxpncihal_ctrl.ext_cb_data);
646   nxpncihal_ctrl.nci_info.wait_for_ntf = FALSE;
647   HAL_DISABLE_EXT();
648   return status;
649 }
650 
651 /******************************************************************************
652  * Function         phNxpNciHal_write_ext
653  *
654  * Description      This function inform the status of phNxpNciHal_open
655  *                  function to libnfc-nci.
656  *
657  * Returns          It return NFCSTATUS_SUCCESS then continue with send else
658  *                  sends NFCSTATUS_FAILED direct response is prepared and
659  *                  do not send anything to NFCC.
660  *
661  ******************************************************************************/
phNxpNciHal_write_ext(uint16_t * cmd_len,uint8_t * p_cmd_data,uint16_t * rsp_len,uint8_t * p_rsp_data)662 NFCSTATUS phNxpNciHal_write_ext(uint16_t* cmd_len, uint8_t* p_cmd_data,
663                                 uint16_t* rsp_len, uint8_t* p_rsp_data) {
664   NFCSTATUS status = NFCSTATUS_SUCCESS;
665 
666   phNxpNciHal_NfcDep_cmd_ext(p_cmd_data, cmd_len);
667 
668   if (phNxpDta_IsEnable() == true) {
669     status = phNxpNHal_DtaUpdate(cmd_len, p_cmd_data, rsp_len, p_rsp_data);
670   }
671 
672   if (p_cmd_data[0] == PROPRIETARY_CMD_FELICA_READER_MODE &&
673       p_cmd_data[1] == PROPRIETARY_CMD_FELICA_READER_MODE &&
674       p_cmd_data[2] == PROPRIETARY_CMD_FELICA_READER_MODE) {
675     NXPLOG_NCIHAL_D("Received proprietary command to set Felica Reader mode:%d",
676                     p_cmd_data[3]);
677     gFelicaReaderMode = p_cmd_data[3];
678     /* frame the fake response */
679     *rsp_len = 4;
680     p_rsp_data[0] = 0x00;
681     p_rsp_data[1] = 0x00;
682     p_rsp_data[2] = 0x00;
683     p_rsp_data[3] = 0x00;
684     status = NFCSTATUS_FAILED;
685   } else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
686              (p_cmd_data[2] == 0x05 || p_cmd_data[2] == 0x32) &&
687              (p_cmd_data[3] == 0x01 || p_cmd_data[3] == 0x02) &&
688              p_cmd_data[4] == 0xA0 && p_cmd_data[5] == 0x44 &&
689              p_cmd_data[6] == 0x01 && p_cmd_data[7] == 0x01) {
690     nxpprofile_ctrl.profile_type = EMV_CO_PROFILE;
691     NXPLOG_NCIHAL_D("EMV_CO_PROFILE mode - Enabled");
692     status = NFCSTATUS_SUCCESS;
693   } else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 &&
694              (p_cmd_data[2] == 0x05 || p_cmd_data[2] == 0x32) &&
695              (p_cmd_data[3] == 0x01 || p_cmd_data[3] == 0x02) &&
696              p_cmd_data[4] == 0xA0 && p_cmd_data[5] == 0x44 &&
697              p_cmd_data[6] == 0x01 && p_cmd_data[7] == 0x00) {
698     NXPLOG_NCIHAL_D("NFC_FORUM_PROFILE mode - Enabled");
699     nxpprofile_ctrl.profile_type = NFC_FORUM_PROFILE;
700     status = NFCSTATUS_SUCCESS;
701   }
702 
703   if (nxpprofile_ctrl.profile_type == EMV_CO_PROFILE) {
704     if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x06 &&
705         p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x03) {
706 #if 0
707             //Needs clarification whether to keep it or not
708             NXPLOG_NCIHAL_D ("EmvCo Poll mode - RF Deactivate discard");
709             phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
710             *rsp_len = 4;
711             p_rsp_data[0] = 0x41;
712             p_rsp_data[1] = 0x06;
713             p_rsp_data[2] = 0x01;
714             p_rsp_data[3] = 0x00;
715             phNxpNciHal_print_packet("RECV", p_rsp_data, 4);
716             status = NFCSTATUS_FAILED;
717 #endif
718     } else if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
719       NXPLOG_NCIHAL_D("EmvCo Poll mode - Discover map only for A and B");
720       p_cmd_data[2] = 0x05;
721       p_cmd_data[3] = 0x02;
722       p_cmd_data[4] = 0x00;
723       p_cmd_data[5] = 0x01;
724       p_cmd_data[6] = 0x01;
725       p_cmd_data[7] = 0x01;
726       *cmd_len = 8;
727     }
728   }
729 
730   if (mfc_mode == true && p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
731     NXPLOG_NCIHAL_D("EmvCo Poll mode - Discover map only for A and B");
732     p_cmd_data[2] = 0x03;
733     p_cmd_data[3] = 0x01;
734     p_cmd_data[4] = 0x00;
735     p_cmd_data[5] = 0x01;
736     *cmd_len = 6;
737     mfc_mode = false;
738   }
739 
740   if (*cmd_len <= (NCI_MAX_DATA_LEN - 3) && bEnableMfcReader &&
741       (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x00) &&
742       (nxpprofile_ctrl.profile_type == NFC_FORUM_PROFILE)) {
743     unsigned long retval = 0;
744     if (p_cmd_data[2] == 0x04 && p_cmd_data[3] == 0x01 &&
745         p_cmd_data[4] == 0x80 && p_cmd_data[5] == 0x01 &&
746         p_cmd_data[6] == 0x83) {
747       mfc_mode = true;
748     } else {
749       if (!GetNxpNumValue(NAME_MIFARE_READER_ENABLE, &retval,
750                           sizeof(unsigned long))) {
751         NXPLOG_NCIHAL_E(
752             "Reading of MIFARE_READER_ENABLE failed. Default retval = %lu",
753             retval);
754       }
755       if (retval == 0x01) {
756         NXPLOG_NCIHAL_D("Going through extns - Adding Mifare in RF Discovery");
757         p_cmd_data[2] += 3;
758         p_cmd_data[3] += 1;
759         p_cmd_data[*cmd_len] = 0x80;
760         p_cmd_data[*cmd_len + 1] = 0x01;
761         p_cmd_data[*cmd_len + 2] = 0x80;
762         *cmd_len += 3;
763         status = NFCSTATUS_SUCCESS;
764         NXPLOG_NCIHAL_D(
765             "Going through extns - Adding Mifare in RF Discovery - END");
766       }
767     }
768   } else if ((*cmd_len >= 6) &&
769              (p_cmd_data[3] == 0x81 && p_cmd_data[4] == 0x01 &&
770               p_cmd_data[5] == 0x03)) {
771     NXPLOG_NCIHAL_D("> Going through the set host list");
772     if (nfcFL.chipType >= sn100u) {
773       *cmd_len = 10;
774 
775       p_cmd_data[2] = 0x07;
776 
777       p_cmd_data[6] = 0x02;
778       p_cmd_data[7] = 0x80;
779       p_cmd_data[8] = 0x81;
780       p_cmd_data[9] = 0xC0;
781     } else {
782       *cmd_len = 8;
783 
784       p_cmd_data[2] = 0x05;
785       p_cmd_data[6] = 0x02;
786       p_cmd_data[7] = 0xC0;
787     }
788     status = NFCSTATUS_SUCCESS;
789   } else if (icode_detected) {
790     if (nfcFL.chipType < sn100u && (p_cmd_data[3] & 0x40) == 0x40 &&
791         (p_cmd_data[4] == 0x21 || p_cmd_data[4] == 0x22 ||
792          p_cmd_data[4] == 0x24 || p_cmd_data[4] == 0x27 ||
793          p_cmd_data[4] == 0x28 || p_cmd_data[4] == 0x29 ||
794          p_cmd_data[4] == 0x2a)) {
795       NXPLOG_NCIHAL_D("> Send EOF set");
796       icode_send_eof = 1;
797     }
798 
799     if (p_cmd_data[3] == 0x20 || p_cmd_data[3] == 0x24 ||
800         p_cmd_data[3] == 0x60) {
801       NXPLOG_NCIHAL_D("> NFC ISO_15693 Proprietary CMD ");
802       p_cmd_data[3] += 0x02;
803     }
804   } else if (p_cmd_data[0] == 0x21 && p_cmd_data[1] == 0x03) {
805     NXPLOG_NCIHAL_D("> Polling Loop Started");
806     icode_detected = 0;
807     if (nfcFL.chipType < sn100u) {
808       icode_send_eof = 0;
809     }
810   }
811   // 22000100
812   else if (p_cmd_data[0] == 0x22 && p_cmd_data[1] == 0x00 &&
813            p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x00) {
814     // ee_disc_done = 0x01;//Reader Over SWP event getting
815     *rsp_len = 0x05;
816     p_rsp_data[0] = 0x42;
817     p_rsp_data[1] = 0x00;
818     p_rsp_data[2] = 0x02;
819     p_rsp_data[3] = 0x00;
820     p_rsp_data[4] = 0x00;
821     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
822     status = NFCSTATUS_FAILED;
823   }
824   // 2002 0904 3000 3100 3200 5000
825   else if ((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
826            ((p_cmd_data[2] == 0x09 && p_cmd_data[3] == 0x04) /*||
827             (p_cmd_data[2] == 0x0D && p_cmd_data[3] == 0x04)*/
828             )) {
829     *cmd_len += 0x01;
830     p_cmd_data[2] += 0x01;
831     p_cmd_data[9] = 0x01;
832     p_cmd_data[10] = 0x40;
833     p_cmd_data[11] = 0x50;
834     p_cmd_data[12] = 0x00;
835 
836     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
837     //        phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
838     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
839   }
840   //    20020703300031003200
841   //    2002 0301 3200
842   else if ((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
843            ((p_cmd_data[2] == 0x07 && p_cmd_data[3] == 0x03) ||
844             (p_cmd_data[2] == 0x03 && p_cmd_data[3] == 0x01 &&
845              p_cmd_data[4] == 0x32))) {
846     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
847     phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
848     *rsp_len = 5;
849     p_rsp_data[0] = 0x40;
850     p_rsp_data[1] = 0x02;
851     p_rsp_data[2] = 0x02;
852     p_rsp_data[3] = 0x00;
853     p_rsp_data[4] = 0x00;
854 
855     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
856     status = NFCSTATUS_FAILED;
857     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
858   }
859 
860   // 2002 0D04 300104 310100 320100 500100
861   // 2002 0401 320100
862   else if ((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
863            (
864                /*(p_cmd_data[2] == 0x0D && p_cmd_data[3] == 0x04)*/
865                (p_cmd_data[2] == 0x04 && p_cmd_data[3] == 0x01 &&
866                 p_cmd_data[4] == 0x32 && p_cmd_data[5] == 0x00))) {
867     //        p_cmd_data[12] = 0x40;
868 
869     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config ");
870     phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
871     p_cmd_data[6] = 0x60;
872 
873     phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
874     //        status = NFCSTATUS_FAILED;
875     NXPLOG_NCIHAL_D("> Going through workaround - Dirty Set Config - End ");
876   }
877 #if 0
878     else if ( (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02 ) &&
879                  ((p_cmd_data[2] == 0x09 && p_cmd_data[3] == 0x04) ||
880                      (p_cmd_data[2] == 0x0B && p_cmd_data[3] == 0x05) ||
881                      (p_cmd_data[2] == 0x07 && p_cmd_data[3] == 0x02) ||
882                      (p_cmd_data[2] == 0x0A && p_cmd_data[3] == 0x03) ||
883                      (p_cmd_data[2] == 0x0A && p_cmd_data[3] == 0x04) ||
884                      (p_cmd_data[2] == 0x05 && p_cmd_data[3] == 0x02))
885              )
886     {
887         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config ");
888         phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
889         *rsp_len = 5;
890         p_rsp_data[0] = 0x40;
891         p_rsp_data[1] = 0x02;
892         p_rsp_data[2] = 0x02;
893         p_rsp_data[3] = 0x00;
894         p_rsp_data[4] = 0x00;
895 
896         phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
897         status = NFCSTATUS_FAILED;
898         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config - End ");
899     }
900 
901     else if((p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x02) &&
902            ((p_cmd_data[3] == 0x00) ||
903            ((*cmd_len >= 0x06) && (p_cmd_data[5] == 0x00)))) /*If the length of the first param id is zero don't allow*/
904     {
905         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config ");
906         phNxpNciHal_print_packet("SEND", p_cmd_data, *cmd_len);
907         *rsp_len = 5;
908         p_rsp_data[0] = 0x40;
909         p_rsp_data[1] = 0x02;
910         p_rsp_data[2] = 0x02;
911         p_rsp_data[3] = 0x00;
912         p_rsp_data[4] = 0x00;
913 
914         phNxpNciHal_print_packet("RECV", p_rsp_data, 5);
915         status = NFCSTATUS_FAILED;
916         NXPLOG_NCIHAL_D ("> Going through workaround - Dirty Set Config - End ");
917     }
918 #endif
919   else if ((wFwVerRsp & 0x0000FFFF) == wFwVer) {
920     /* skip CORE_RESET and CORE_INIT from Brcm */
921     if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x00 &&
922         p_cmd_data[2] == 0x01 && p_cmd_data[3] == 0x01) {
923       //            *rsp_len = 6;
924       //
925       //            NXPLOG_NCIHAL_D("> Going - core reset optimization");
926       //
927       //            p_rsp_data[0] = 0x40;
928       //            p_rsp_data[1] = 0x00;
929       //            p_rsp_data[2] = 0x03;
930       //            p_rsp_data[3] = 0x00;
931       //            p_rsp_data[4] = 0x10;
932       //            p_rsp_data[5] = 0x01;
933       //
934       //            status = NFCSTATUS_FAILED;
935       //            NXPLOG_NCIHAL_D("> Going - core reset optimization - END");
936     }
937     /* CORE_INIT */
938     else if (p_cmd_data[0] == 0x20 && p_cmd_data[1] == 0x01 &&
939              p_cmd_data[2] == 0x00) {
940       //            NXPLOG_NCIHAL_D("> Going - core init optimization");
941       //            *rsp_len = iCoreInitRspLen;
942       //            memcpy(p_rsp_data, bCoreInitRsp, iCoreInitRspLen);
943       //            status = NFCSTATUS_FAILED;
944       //            NXPLOG_NCIHAL_D("> Going - core init optimization - END");
945     }
946   }
947 
948   return status;
949 }
950 
951 /******************************************************************************
952  * Function         phNxpNciHal_send_ext_cmd
953  *
954  * Description      This function send the extension command to NFCC. No
955  *                  response is checked by this function but it waits for
956  *                  the response to come.
957  *
958  * Returns          Returns NFCSTATUS_SUCCESS if sending cmd is successful and
959  *                  response is received.
960  *
961  ******************************************************************************/
phNxpNciHal_send_ext_cmd(uint16_t cmd_len,uint8_t * p_cmd)962 NFCSTATUS phNxpNciHal_send_ext_cmd(uint16_t cmd_len, uint8_t* p_cmd) {
963   NFCSTATUS status = NFCSTATUS_FAILED;
964   nxpncihal_ctrl.cmd_len = cmd_len;
965   memcpy(nxpncihal_ctrl.p_cmd_data, p_cmd, cmd_len);
966   status = phNxpNciHal_process_ext_cmd_rsp(nxpncihal_ctrl.cmd_len,
967                                            nxpncihal_ctrl.p_cmd_data);
968 
969   return status;
970 }
971 
972 /******************************************************************************
973  * Function         phNxpNciHal_send_ese_hal_cmd
974  *
975  * Description      This function send the extension command to NFCC. No
976  *                  response is checked by this function but it waits for
977  *                  the response to come.
978  *
979  * Returns          Returns NFCSTATUS_SUCCESS if sending cmd is successful and
980  *                  response is received.
981  *
982  ******************************************************************************/
phNxpNciHal_send_ese_hal_cmd(uint16_t cmd_len,uint8_t * p_cmd)983 NFCSTATUS phNxpNciHal_send_ese_hal_cmd(uint16_t cmd_len, uint8_t* p_cmd) {
984   NFCSTATUS status = NFCSTATUS_FAILED;
985   if (cmd_len > NCI_MAX_DATA_LEN) {
986     NXPLOG_NCIHAL_E("cmd_len exceeds limit NCI_MAX_DATA_LEN");
987     return status;
988   }
989   nxpncihal_ctrl.cmd_len = cmd_len;
990   memcpy(nxpncihal_ctrl.p_cmd_data, p_cmd, cmd_len);
991   status = phNxpNciHal_process_ext_cmd_rsp(nxpncihal_ctrl.cmd_len,
992                                            nxpncihal_ctrl.p_cmd_data);
993   return status;
994 }
995 
996 /******************************************************************************
997  * Function         hal_extns_write_rsp_timeout_cb
998  *
999  * Description      Timer call back function
1000  *
1001  * Returns          None
1002  *
1003  ******************************************************************************/
hal_extns_write_rsp_timeout_cb(uint32_t timerId,void * pContext)1004 static void hal_extns_write_rsp_timeout_cb(uint32_t timerId, void* pContext) {
1005   UNUSED_PROP(timerId);
1006   UNUSED_PROP(pContext);
1007   NXPLOG_NCIHAL_D("hal_extns_write_rsp_timeout_cb - write timeout!!!");
1008   nxpncihal_ctrl.ext_cb_data.status = NFCSTATUS_FAILED;
1009   usleep(1);
1010   sem_post(&(nxpncihal_ctrl.syncSpiNfc));
1011   SEM_POST(&(nxpncihal_ctrl.ext_cb_data));
1012 
1013   return;
1014 }
1015 
1016 /*******************************************************************************
1017  **
1018  ** Function:        request_EEPROM()
1019  **
1020  ** Description:     get and set EEPROM data
1021  **                  In case of request_modes GET_EEPROM_DATA or
1022  *SET_EEPROM_DATA,
1023  **                   1.caller has to pass the buffer and the length of data
1024  *required
1025  **                     to be read/written.
1026  **                   2.Type of information required to be read/written
1027  **                     (Example - EEPROM_RF_CFG)
1028  **
1029  ** Returns:         Returns NFCSTATUS_SUCCESS if sending cmd is successful and
1030  **                  status failed if not successful
1031  **
1032  *******************************************************************************/
request_EEPROM(phNxpNci_EEPROM_info_t * mEEPROM_info)1033 NFCSTATUS request_EEPROM(phNxpNci_EEPROM_info_t* mEEPROM_info) {
1034   NXPLOG_NCIHAL_D(
1035       "%s Enter  request_type : 0x%02x,  request_mode : 0x%02x,  bufflen : "
1036       "0x%02x",
1037       __func__, mEEPROM_info->request_type, mEEPROM_info->request_mode,
1038       mEEPROM_info->bufflen);
1039   NFCSTATUS status = NFCSTATUS_FAILED;
1040   uint8_t retry_cnt = 0;
1041   uint8_t getCfgStartIndex = 0x08;
1042   uint8_t setCfgStartIndex = 0x07;
1043   uint8_t memIndex = 0x00;
1044   uint8_t fieldLen = 0x01;  // Memory field len 1bytes
1045   char addr[2] = {0};
1046   uint8_t cur_value = 0, len = 5;
1047   uint8_t b_position = 0;
1048   bool_t update_req = false;
1049   uint8_t set_cfg_cmd_len = 0;
1050   uint8_t *set_cfg_eeprom, *base_addr;
1051 
1052   mEEPROM_info->update_mode = BITWISE;
1053 
1054   switch (mEEPROM_info->request_type) {
1055     case EEPROM_RF_CFG:
1056       memIndex = 0x00;
1057       fieldLen = 0x20;
1058       len = fieldLen + 4;  // 4 - numParam+2add+val
1059       addr[0] = 0xA0;
1060       addr[1] = 0x14;
1061       mEEPROM_info->update_mode = BYTEWISE;
1062       break;
1063 
1064     case EEPROM_FW_DWNLD:
1065       fieldLen = 0x20;
1066       memIndex = 0x0C;
1067       len = fieldLen + 4;
1068       addr[0] = 0xA0;
1069       addr[1] = 0x0F;
1070       mEEPROM_info->update_mode = BYTEWISE;
1071       break;
1072 
1073     case EEPROM_WIREDMODE_RESUME_TIMEOUT:
1074       mEEPROM_info->update_mode = BYTEWISE;
1075       memIndex = 0x00;
1076       fieldLen = 0x04;
1077       len = fieldLen + 4;
1078       addr[0] = 0xA0;
1079       addr[1] = 0xFC;
1080       break;
1081 
1082     case EEPROM_ESE_SVDD_POWER:
1083       b_position = 0;
1084       memIndex = 0x00;
1085       addr[0] = 0xA0;
1086       addr[1] = 0xF2;
1087       break;
1088     case EEPROM_ESE_POWER_EXT_PMU:
1089       mEEPROM_info->update_mode = BYTEWISE;
1090       memIndex = 0x00;
1091       addr[0] = 0xA0;
1092       addr[1] = 0xD7;
1093       break;
1094 
1095     case EEPROM_PROP_ROUTING:
1096       b_position = 7;
1097       memIndex = 0x00;
1098       addr[0] = 0xA0;
1099       addr[1] = 0x98;
1100       break;
1101 
1102     case EEPROM_ESE_SESSION_ID:
1103       b_position = 0;
1104       memIndex = 0x00;
1105       addr[0] = 0xA0;
1106       addr[1] = 0xEB;
1107       break;
1108 
1109     case EEPROM_SWP1_INTF:
1110       b_position = 0;
1111       memIndex = 0x00;
1112       addr[0] = 0xA0;
1113       addr[1] = 0xEC;
1114       break;
1115 
1116     case EEPROM_SWP1A_INTF:
1117       b_position = 0;
1118       memIndex = 0x00;
1119       addr[0] = 0xA0;
1120       addr[1] = 0xD4;
1121       break;
1122     case EEPROM_SWP2_INTF:
1123       b_position = 0;
1124       memIndex = 0x00;
1125       addr[0] = 0xA0;
1126       addr[1] = 0xED;
1127       break;
1128     case EEPROM_FLASH_UPDATE:
1129       /* This flag is no more used in MW */
1130       fieldLen = 0x20;
1131       memIndex = 0x00;
1132       len = fieldLen + 4;
1133       addr[0] = 0xA0;
1134       addr[1] = 0x0F;
1135       break;
1136     case EEPROM_AUTH_CMD_TIMEOUT:
1137       mEEPROM_info->update_mode = BYTEWISE;
1138       memIndex = 0x00;
1139       fieldLen = 0x05;
1140       len = fieldLen + 4;
1141       addr[0] = 0xA0;
1142       addr[1] = 0xF7;
1143       break;
1144     case EEPROM_GUARD_TIMER:
1145       mEEPROM_info->update_mode = BYTEWISE;
1146       memIndex = 0x00;
1147       addr[0] = 0xA1;
1148       addr[1] = 0x0B;
1149       break;
1150     case EEPROM_AUTONOMOUS_MODE:
1151       mEEPROM_info->update_mode = BYTEWISE;
1152       memIndex = 0x00;
1153       addr[0] = 0xA0;
1154       addr[1] = 0x15;
1155       break;
1156     case EEPROM_T4T_NFCEE_ENABLE:
1157       mEEPROM_info->update_mode = BYTEWISE;
1158       b_position = 0;
1159       memIndex = 0x00;
1160       addr[0] = 0xA0;
1161       addr[1] = 0x95;
1162       break;
1163     case EEPROM_CE_PHONE_OFF_CFG:
1164       mEEPROM_info->update_mode = BYTEWISE;
1165       b_position = 0;
1166       memIndex = 0x00;
1167       addr[0] = 0xA0;
1168       addr[1] = 0x8E;
1169       break;
1170     case EEPROM_ENABLE_VEN_CFG:
1171       mEEPROM_info->update_mode = BYTEWISE;
1172       b_position = 0;
1173       memIndex = 0x00;
1174       addr[0] = 0xA0;
1175       addr[1] = 0x07;
1176       break;
1177     case EEPROM_ISODEP_MERGE_SAK:
1178       mEEPROM_info->update_mode = BYTEWISE;
1179       b_position = 0;
1180       memIndex = 0x00;
1181       addr[0] = 0xA1;
1182       addr[1] = 0x1B;
1183       break;
1184     case EEPROM_SRD_TIMEOUT:
1185       mEEPROM_info->update_mode = BYTEWISE;
1186       memIndex = 0x00;
1187       fieldLen = 0x02;
1188       len = fieldLen + 4;
1189       addr[0] = 0xA1;
1190       addr[1] = 0x17;
1191       break;
1192     case EEPROM_UICC1_SESSION_ID:
1193       fieldLen = mEEPROM_info->bufflen;
1194       len = fieldLen + 4;
1195       memIndex = 0x00;
1196       addr[0] = 0xA0;
1197       addr[1] = 0xE4;
1198       mEEPROM_info->update_mode = BYTEWISE;
1199       break;
1200     case EEPROM_UICC2_SESSION_ID:
1201       fieldLen = mEEPROM_info->bufflen;
1202       len = fieldLen + 4;
1203       memIndex = 0x00;
1204       addr[0] = 0xA0;
1205       addr[1] = 0xE5;
1206       mEEPROM_info->update_mode = BYTEWISE;
1207       break;
1208     default:
1209       ALOGE("No valid request information found");
1210       break;
1211   }
1212 
1213   uint8_t get_cfg_eeprom[6] = {
1214       0x20,
1215       0x03,              // get_cfg header
1216       0x03,              // len of following value
1217       0x01,              // Num Parameters
1218       (uint8_t)addr[0],  // First byte of Address
1219       (uint8_t)addr[1]   // Second byte of Address
1220   };
1221   uint8_t set_cfg_cmd_hdr[7] = {
1222       0x20,
1223       0x02,              // set_cfg header
1224       len,               // len of following value
1225       0x01,              // Num Param
1226       (uint8_t)addr[0],  // First byte of Address
1227       (uint8_t)addr[1],  // Second byte of Address
1228       fieldLen           // Data len
1229   };
1230 
1231   set_cfg_cmd_len = sizeof(set_cfg_cmd_hdr) + fieldLen;
1232   set_cfg_eeprom = (uint8_t*)malloc(set_cfg_cmd_len);
1233   if (set_cfg_eeprom == NULL) {
1234     ALOGE("memory allocation failed");
1235     return status;
1236   }
1237   base_addr = set_cfg_eeprom;
1238   memset(set_cfg_eeprom, 0, set_cfg_cmd_len);
1239   memcpy(set_cfg_eeprom, set_cfg_cmd_hdr, sizeof(set_cfg_cmd_hdr));
1240 
1241 retryget:
1242   status = phNxpNciHal_send_ext_cmd(sizeof(get_cfg_eeprom), get_cfg_eeprom);
1243   if (status == NFCSTATUS_SUCCESS) {
1244     status = nxpncihal_ctrl.p_rx_data[3];
1245     if (status != NFCSTATUS_SUCCESS) {
1246       ALOGE("failed to get requested memory address");
1247     } else if (mEEPROM_info->request_mode == GET_EEPROM_DATA) {
1248       if (mEEPROM_info->bufflen == 0xFF) {
1249         /* Max bufferlenth for single Get Config Command is 0xFF.
1250          * If buffer length set to max value, reassign buffer value
1251          * depends on response from Get Config command */
1252         mEEPROM_info->bufflen =
1253             *(nxpncihal_ctrl.p_rx_data + getCfgStartIndex + memIndex - 1);
1254       }
1255       memcpy(mEEPROM_info->buffer,
1256              nxpncihal_ctrl.p_rx_data + getCfgStartIndex + memIndex,
1257              mEEPROM_info->bufflen);
1258     } else if (mEEPROM_info->request_mode == SET_EEPROM_DATA) {
1259       // Clear the buffer first
1260       memset(set_cfg_eeprom + setCfgStartIndex, 0x00,
1261              (set_cfg_cmd_len - setCfgStartIndex));
1262 
1263       // copy get config data into set_cfg_eeprom
1264       memcpy(set_cfg_eeprom + setCfgStartIndex,
1265              nxpncihal_ctrl.p_rx_data + getCfgStartIndex, fieldLen);
1266       if (mEEPROM_info->update_mode == BITWISE) {
1267         cur_value =
1268             (set_cfg_eeprom[setCfgStartIndex + memIndex] >> b_position) & 0x01;
1269         if (cur_value != mEEPROM_info->buffer[0]) {
1270           update_req = true;
1271           if (mEEPROM_info->buffer[0] == 1) {
1272             set_cfg_eeprom[setCfgStartIndex + memIndex] |= (1 << b_position);
1273           } else if (mEEPROM_info->buffer[0] == 0) {
1274             set_cfg_eeprom[setCfgStartIndex + memIndex] &= (~(1 << b_position));
1275           }
1276         }
1277       } else if (mEEPROM_info->update_mode == BYTEWISE) {
1278         if (memcmp(set_cfg_eeprom + setCfgStartIndex + memIndex,
1279                    mEEPROM_info->buffer, mEEPROM_info->bufflen)) {
1280           update_req = true;
1281           memcpy(set_cfg_eeprom + setCfgStartIndex + memIndex,
1282                  mEEPROM_info->buffer, mEEPROM_info->bufflen);
1283         }
1284       } else {
1285         ALOGE("%s, invalid update mode", __func__);
1286       }
1287 
1288       if (update_req) {
1289       // do set config
1290       retryset:
1291         status = phNxpNciHal_send_ext_cmd(set_cfg_cmd_len, set_cfg_eeprom);
1292         if (status != NFCSTATUS_SUCCESS && retry_cnt < 3) {
1293           retry_cnt++;
1294           ALOGE("Set Cfg Retry cnt=%x", retry_cnt);
1295           goto retryset;
1296         }
1297       } else {
1298         ALOGD("%s: values are same no update required", __func__);
1299       }
1300     }
1301   } else if (retry_cnt < 3) {
1302     retry_cnt++;
1303     ALOGE("Get Cfg Retry cnt=%x", retry_cnt);
1304     goto retryget;
1305   }
1306 
1307   if (base_addr != NULL) {
1308     free(base_addr);
1309     base_addr = NULL;
1310   }
1311   retry_cnt = 0;
1312   return status;
1313 }
1314 
1315 /*******************************************************************************
1316  **
1317  ** Function:        phNxpNciHal_enableDefaultUICC2SWPline()
1318  **
1319  ** Description:     Select UICC2 or UICC3
1320  **
1321  ** Returns:         status
1322  **
1323  ********************************************************************************/
phNxpNciHal_enableDefaultUICC2SWPline(uint8_t uicc2_sel)1324 NFCSTATUS phNxpNciHal_enableDefaultUICC2SWPline(uint8_t uicc2_sel) {
1325   NFCSTATUS status = NFCSTATUS_FAILED;
1326   uint8_t p_data[255] = {NCI_MT_CMD, NXP_CORE_SET_CONFIG_CMD};
1327   uint8_t LEN_INDEX = 2, PARAM_INDEX = 3;
1328   uint8_t* p = p_data;
1329   NXPLOG_NCIHAL_D("phNxpNciHal_enableDefaultUICC2SWPline %d", uicc2_sel);
1330   p_data[LEN_INDEX] = 1;
1331   p += 4;
1332   if (uicc2_sel == 0x03) {
1333     UINT8_TO_STREAM(p, NXP_NFC_SET_CONFIG_PARAM_EXT);
1334     UINT8_TO_STREAM(p, NXP_NFC_PARAM_ID_SWP2);
1335     UINT8_TO_STREAM(p, 0x01);
1336     UINT8_TO_STREAM(p, 0x01);
1337     p_data[LEN_INDEX] += 4;
1338     p_data[PARAM_INDEX] += 1;
1339   }
1340   if (uicc2_sel == 0x04) {
1341     UINT8_TO_STREAM(p, NXP_NFC_SET_CONFIG_PARAM_EXT);
1342     UINT8_TO_STREAM(p, NXP_NFC_PARAM_ID_SWPUICC3);
1343     UINT8_TO_STREAM(p, 0x01);
1344     UINT8_TO_STREAM(p, 0x01);
1345     p_data[LEN_INDEX] += 4;
1346     p_data[PARAM_INDEX] += 1;
1347   }
1348   if (p_data[PARAM_INDEX] > 0x00)
1349     status = phNxpNciHal_send_ext_cmd(p - p_data, p_data);
1350   return status;
1351 }
1352 
1353 /******************************************************************************
1354  * Function         phNxpNciHal_prop_conf_lpcd
1355  *
1356  * Description      If NFCC is not in Nfc Forum mode, then this function will
1357  *                  configure it back to the Nfc Forum mode.
1358  *
1359  * Returns          none
1360  *
1361  ******************************************************************************/
phNxpNciHal_prop_conf_lpcd(bool enableLPCD)1362 void phNxpNciHal_prop_conf_lpcd(bool enableLPCD) {
1363   uint8_t cmd_get_lpcdval[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x68};
1364   vector<uint8_t> cmd_set_lpcdval{0x20, 0x02, 0x2E};
1365 
1366   if (NFCSTATUS_SUCCESS ==
1367       phNxpNciHal_send_ext_cmd(sizeof(cmd_get_lpcdval), cmd_get_lpcdval)) {
1368     if (NFCSTATUS_SUCCESS == nxpncihal_ctrl.p_rx_data[3]) {
1369       if (!(nxpncihal_ctrl.p_rx_data[17] & (1 << 7)) && enableLPCD) {
1370         nxpncihal_ctrl.p_rx_data[17] |= (1 << 7);
1371         cmd_set_lpcdval.insert(
1372             cmd_set_lpcdval.end(), &nxpncihal_ctrl.p_rx_data[4],
1373             (&nxpncihal_ctrl.p_rx_data[4] + cmd_set_lpcdval[2]));
1374         if (NFCSTATUS_SUCCESS ==
1375             phNxpNciHal_send_ext_cmd(cmd_set_lpcdval.size(),
1376                                      &cmd_set_lpcdval[0])) {
1377           return;
1378         }
1379       } else if (!enableLPCD && (nxpncihal_ctrl.p_rx_data[17] & (1 << 7))) {
1380         nxpncihal_ctrl.p_rx_data[17] &= ~(1 << 7);
1381         cmd_set_lpcdval.insert(
1382             cmd_set_lpcdval.end(), &nxpncihal_ctrl.p_rx_data[4],
1383             (&nxpncihal_ctrl.p_rx_data[4] + cmd_set_lpcdval[2]));
1384         if (NFCSTATUS_SUCCESS ==
1385             phNxpNciHal_send_ext_cmd(cmd_set_lpcdval.size(),
1386                                      &cmd_set_lpcdval[0])) {
1387           return;
1388         }
1389       } else {
1390         return;
1391       }
1392     }
1393   }
1394   NXPLOG_NCIHAL_E("%s: failed!!", __func__);
1395   return;
1396 }
1397 
1398 /******************************************************************************
1399  * Function         phNxpNciHal_conf_nfc_forum_mode
1400  *
1401  * Description      If NFCC is not in Nfc Forum mode, then this function will
1402  *                  configure it back to the Nfc Forum mode.
1403  *
1404  * Returns          none
1405  *
1406  ******************************************************************************/
phNxpNciHal_conf_nfc_forum_mode()1407 void phNxpNciHal_conf_nfc_forum_mode() {
1408   uint8_t cmd_get_emvcocfg[] = {0x20, 0x03, 0x03, 0x01, 0xA0, 0x44};
1409   uint8_t cmd_reset_emvcocfg[8];
1410   long cmdlen = 8;
1411   long retlen = 0;
1412 
1413   if (GetNxpByteArrayValue(NAME_NXP_PROP_RESET_EMVCO_CMD,
1414                            (char*)cmd_reset_emvcocfg, cmdlen, &retlen)) {
1415   }
1416   if (retlen != 0x08) {
1417     NXPLOG_NCIHAL_E("%s: command is not provided", __func__);
1418     return;
1419   }
1420   /* Update the flag address from the Nxp config file */
1421   cmd_get_emvcocfg[4] = cmd_reset_emvcocfg[4];
1422   cmd_get_emvcocfg[5] = cmd_reset_emvcocfg[5];
1423 
1424   if (NFCSTATUS_SUCCESS ==
1425       phNxpNciHal_send_ext_cmd(sizeof(cmd_get_emvcocfg), cmd_get_emvcocfg)) {
1426     if (NFCSTATUS_SUCCESS == nxpncihal_ctrl.p_rx_data[3]) {
1427       if (0x01 & nxpncihal_ctrl.p_rx_data[8]) {
1428         if (NFCSTATUS_SUCCESS ==
1429             phNxpNciHal_send_ext_cmd(sizeof(cmd_reset_emvcocfg),
1430                                      cmd_reset_emvcocfg)) {
1431           return;
1432         }
1433       } else {
1434         return;
1435       }
1436     }
1437   }
1438   NXPLOG_NCIHAL_E("%s: failed!!", __func__);
1439   return;
1440 }
1441