1 /*
2  * Copyright (C) 2010-2014 NXP Semiconductors
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 /*
18  * Download Component
19  * Download Interface routines implementation
20  */
21 
22 #include <dlfcn.h>
23 #include <phDnldNfc_Internal.h>
24 #include <phNxpConfig.h>
25 #include <phNxpLog.h>
26 #include <phTmlNfc.h>
27 #include <string>
28 static void* pFwHandle; /* Global firmware handle */
29 uint16_t wMwVer = 0;    /* Middleware version no */
30 uint16_t wFwVer = 0;    /* Firmware version no */
31 uint8_t gRecFWDwnld;    // flag set to true to indicate dummy FW download
32 phTmlNfc_i2cfragmentation_t fragmentation_enabled = I2C_FRAGMENATATION_DISABLED;
33 static pphDnldNfc_DlContext_t gpphDnldContext = NULL; /* Download contex */
34 #undef EEPROM_Read_Mem_IMP
35 
36 /*******************************************************************************
37 **
38 ** Function         phDnldNfc_Reset
39 **
40 ** Description      Performs a soft reset of the download module
41 **
42 ** Parameters       pNotify  - notify caller after getting response
43 **                  pContext - caller context
44 **
45 ** Returns          NFC status:
46 **                  NFCSTATUS_SUCCESS - reset request to NFCC is successful
47 **                  NFCSTATUS_FAILED - reset request failed due to internal
48 **                                     error
49 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
50 **                  Other command specific errors
51 **
52 *******************************************************************************/
phDnldNfc_Reset(pphDnldNfc_RspCb_t pNotify,void * pContext)53 NFCSTATUS phDnldNfc_Reset(pphDnldNfc_RspCb_t pNotify, void* pContext) {
54   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
55 
56   if ((NULL == pNotify) || (NULL == pContext)) {
57     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
58     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
59   } else {
60     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
61       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
62       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
63     } else {
64       (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTNone;
65       (gpphDnldContext->tCmdId) = PH_DL_CMD_RESET;
66       (gpphDnldContext->tRspBuffInfo.pBuff) = NULL;
67       (gpphDnldContext->tRspBuffInfo.wLen) = 0;
68       (gpphDnldContext->tUserData.pBuff) = NULL;
69       (gpphDnldContext->tUserData.wLen) = 0;
70       (gpphDnldContext->UserCb) = pNotify;
71       (gpphDnldContext->UserCtxt) = pContext;
72 
73       wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventReset);
74 
75       if (NFCSTATUS_PENDING == wStatus) {
76         NXPLOG_FWDNLD_D("Reset Request submitted successfully");
77       } else {
78         NXPLOG_FWDNLD_E("Reset Request Failed!!");
79       }
80     }
81   }
82 
83   return wStatus;
84 }
85 
86 /*******************************************************************************
87 **
88 ** Function         phDnldNfc_GetVersion
89 **
90 ** Description      Retrieves Hardware version, ROM Code version, Protected Data
91 **                  version, Trim data version, User data version, and Firmware
92 **                  version information
93 **
94 ** Parameters       pVersionInfo - response buffer which gets updated with
95 **                                 complete version info from NFCC
96 **                  pNotify - notify caller after getting response
97 **                  pContext - caller context
98 **
99 ** Returns          NFC status:
100 **                  NFCSTATUS_SUCCESS - GetVersion request to NFCC is successful
101 **                  NFCSTATUS_FAILED - GetVersion request failed due to internal
102 **                                     error
103 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
104 **                  Other command specific errors
105 **
106 *******************************************************************************/
phDnldNfc_GetVersion(pphDnldNfc_Buff_t pVersionInfo,pphDnldNfc_RspCb_t pNotify,void * pContext)107 NFCSTATUS phDnldNfc_GetVersion(pphDnldNfc_Buff_t pVersionInfo,
108                                pphDnldNfc_RspCb_t pNotify, void* pContext) {
109   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
110 
111   if ((NULL == pVersionInfo) || (NULL == pNotify) || (NULL == pContext)) {
112     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
113     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
114   } else {
115     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
116       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
117       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
118     } else {
119       if ((NULL != pVersionInfo->pBuff) && (0 != pVersionInfo->wLen)) {
120         (gpphDnldContext->tRspBuffInfo.pBuff) = pVersionInfo->pBuff;
121         (gpphDnldContext->tRspBuffInfo.wLen) = pVersionInfo->wLen;
122         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTNone;
123         (gpphDnldContext->tCmdId) = PH_DL_CMD_GETVERSION;
124         (gpphDnldContext->tUserData.pBuff) = NULL;
125         (gpphDnldContext->tUserData.wLen) = 0;
126         (gpphDnldContext->UserCb) = pNotify;
127         (gpphDnldContext->UserCtxt) = pContext;
128 
129         wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventGetVer);
130 
131         if (NFCSTATUS_PENDING == wStatus) {
132           NXPLOG_FWDNLD_D("GetVersion Request submitted successfully");
133         } else {
134           NXPLOG_FWDNLD_E("GetVersion Request Failed!!");
135         }
136       } else {
137         NXPLOG_FWDNLD_E("Invalid Buff Parameters!!");
138         wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
139       }
140     }
141   }
142 
143   return wStatus;
144 }
145 
146 /*******************************************************************************
147 **
148 ** Function         phDnldNfc_GetSessionState
149 **
150 ** Description      Retrieves the current session state of NFCC
151 **
152 ** Parameters       pSession - response buffer which gets updated with complete
153 **                             version info from NFCC
154 **                  pNotify - notify caller after getting response
155 **                  pContext - caller context
156 **
157 ** Returns          NFC status:
158 **                  NFCSTATUS_SUCCESS - GetSessionState request to NFCC is
159 **                                      successful
160 **                  NFCSTATUS_FAILED - GetSessionState request failed due to
161 **                                     internal error
162 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
163 **                  Other command specific errors
164 **
165 *******************************************************************************/
phDnldNfc_GetSessionState(pphDnldNfc_Buff_t pSession,pphDnldNfc_RspCb_t pNotify,void * pContext)166 NFCSTATUS phDnldNfc_GetSessionState(pphDnldNfc_Buff_t pSession,
167                                     pphDnldNfc_RspCb_t pNotify,
168                                     void* pContext) {
169   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
170 
171   if ((NULL == pSession) || (NULL == pNotify) || (NULL == pContext)) {
172     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
173     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
174   } else {
175     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
176       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
177       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
178     } else {
179       if ((NULL != pSession->pBuff) && (0 != pSession->wLen)) {
180         (gpphDnldContext->tRspBuffInfo.pBuff) = pSession->pBuff;
181         (gpphDnldContext->tRspBuffInfo.wLen) = pSession->wLen;
182         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTNone;
183         (gpphDnldContext->tCmdId) = PH_DL_CMD_GETSESSIONSTATE;
184         (gpphDnldContext->tUserData.pBuff) = NULL;
185         (gpphDnldContext->tUserData.wLen) = 0;
186         (gpphDnldContext->UserCb) = pNotify;
187         (gpphDnldContext->UserCtxt) = pContext;
188 
189         wStatus =
190             phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventGetSesnSt);
191 
192         if (NFCSTATUS_PENDING == wStatus) {
193           NXPLOG_FWDNLD_D("GetSessionState Request submitted successfully");
194         } else {
195           NXPLOG_FWDNLD_E("GetSessionState Request Failed!!");
196         }
197       } else {
198         NXPLOG_FWDNLD_E("Invalid Buff Parameters!!");
199         wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
200       }
201     }
202   }
203 
204   return wStatus;
205 }
206 
207 /*******************************************************************************
208 **
209 ** Function         phDnldNfc_CheckIntegrity
210 **
211 ** Description      Inspects the integrity of EEPROM and FLASH contents of the
212 **                  NFCC, provides CRC for each section
213 **                  NOTE: The user data section CRC is valid only after fresh
214 **                        download
215 **
216 ** Parameters       bChipVer - current ChipVersion for including additional
217 **                             parameters in request payload
218 **                  pCRCData - response buffer which gets updated with
219 **                             respective section CRC status and CRC bytes from
220 **                             NFCC
221 **                  pNotify - notify caller after getting response
222 **                  pContext - caller context
223 **
224 ** Returns          NFC status:
225 **                  NFCSTATUS_SUCCESS - CheckIntegrity request is successful
226 **                  NFCSTATUS_FAILED - CheckIntegrity request failed due to
227 **                                     internal error
228 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
229 **                  Other command specific errors
230 **
231 *******************************************************************************/
phDnldNfc_CheckIntegrity(uint8_t bChipVer,pphDnldNfc_Buff_t pCRCData,pphDnldNfc_RspCb_t pNotify,void * pContext)232 NFCSTATUS phDnldNfc_CheckIntegrity(uint8_t bChipVer, pphDnldNfc_Buff_t pCRCData,
233                                    pphDnldNfc_RspCb_t pNotify, void* pContext) {
234   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
235 
236   if ((NULL == pNotify) || (NULL == pContext)) {
237     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
238     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
239   } else {
240     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
241       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
242       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
243     } else {
244       if ((PHDNLDNFC_HWVER_MRA2_1 == bChipVer) ||
245           (PHDNLDNFC_HWVER_MRA2_2 == bChipVer) ||
246           ((nfcFL.chipType == pn551) &&
247            (PHDNLDNFC_HWVER_PN551_MRA1_0 == bChipVer)) ||
248           (((nfcFL.chipType == pn553) || (nfcFL.chipType == pn557)) &&
249            ((PHDNLDNFC_HWVER_PN553_MRA1_0 == bChipVer) ||
250             (PHDNLDNFC_HWVER_PN553_MRA1_0_UPDATED & bChipVer) ||
251             (PHDNLDNFC_HWVER_PN557_MRA1_0 == bChipVer)))) {
252         (gpphDnldContext->FrameInp.Type) = phDnldNfc_ChkIntg;
253       } else {
254         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTNone;
255       }
256 
257       if ((NULL != pCRCData->pBuff) && (0 != pCRCData->wLen)) {
258         (gpphDnldContext->tRspBuffInfo.pBuff) = pCRCData->pBuff;
259         (gpphDnldContext->tRspBuffInfo.wLen) = pCRCData->wLen;
260         (gpphDnldContext->tCmdId) = PH_DL_CMD_CHECKINTEGRITY;
261         (gpphDnldContext->tUserData.pBuff) = NULL;
262         (gpphDnldContext->tUserData.wLen) = 0;
263         (gpphDnldContext->UserCb) = pNotify;
264         (gpphDnldContext->UserCtxt) = pContext;
265 
266         wStatus =
267             phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventIntegChk);
268 
269         if (NFCSTATUS_PENDING == wStatus) {
270           NXPLOG_FWDNLD_D("CheckIntegrity Request submitted successfully");
271         } else {
272           NXPLOG_FWDNLD_E("CheckIntegrity Request Failed!!");
273         }
274       } else {
275         NXPLOG_FWDNLD_E("Invalid Buff Parameters!!");
276         wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
277       }
278     }
279   }
280 
281   return wStatus;
282 }
283 /*******************************************************************************
284 **
285 ** Function         phDnldNfc_ReadLog
286 **
287 ** Description      Retrieves log data from EEPROM
288 **
289 ** Parameters       pData - response buffer which gets updated with data from
290 **                          EEPROM
291 **                  pNotify - notify caller after getting response
292 **                  pContext - caller context
293 **
294 ** Returns          NFC status:
295 **                  NFCSTATUS_SUCCESS - Read request to NFCC is successful
296 **                  NFCSTATUS_FAILED - Read request failed due to internal error
297 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
298 **                  Other command specific errors
299 **
300 *******************************************************************************/
phDnldNfc_ReadLog(pphDnldNfc_Buff_t pData,pphDnldNfc_RspCb_t pNotify,void * pContext)301 NFCSTATUS phDnldNfc_ReadLog(pphDnldNfc_Buff_t pData, pphDnldNfc_RspCb_t pNotify,
302                             void* pContext) {
303   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
304 
305   if ((NULL == pNotify) || (NULL == pData) || (NULL == pContext)) {
306     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
307     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
308   } else {
309     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
310       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
311       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
312     } else {
313       if ((NULL != pData->pBuff) && (0 != pData->wLen)) {
314         (gpphDnldContext->tCmdId) = PH_DL_CMD_READ;
315         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTRead;
316         (gpphDnldContext->FrameInp.dwAddr) = PHDNLDNFC_EEPROM_LOG_START_ADDR;
317         (gpphDnldContext->tRspBuffInfo.pBuff) = pData->pBuff;
318         (gpphDnldContext->tRspBuffInfo.wLen) = pData->wLen;
319         (gpphDnldContext->tUserData.pBuff) = NULL;
320         (gpphDnldContext->tUserData.wLen) = 0;
321         (gpphDnldContext->UserCb) = pNotify;
322         (gpphDnldContext->UserCtxt) = pContext;
323 
324         memset(&(gpphDnldContext->tRWInfo), 0,
325                sizeof(gpphDnldContext->tRWInfo));
326 
327         wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventRead);
328 
329         if (NFCSTATUS_PENDING == wStatus) {
330           NXPLOG_FWDNLD_D("Read Request submitted successfully");
331         } else {
332           NXPLOG_FWDNLD_E("Read Request Failed!!");
333         }
334       } else {
335         NXPLOG_FWDNLD_E("Invalid Buff Parameters!!");
336         wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
337       }
338     }
339   }
340 
341   return wStatus;
342 }
343 
344 /*******************************************************************************
345 **
346 ** Function         phDnldNfc_Write
347 **
348 ** Description      Writes requested  data of length len to desired EEPROM/FLASH
349 **                  address
350 **
351 ** Parameters       bRecoverSeq - flag to indicate whether recover sequence data
352 **                                needs to be written or not
353 **                  pData - data buffer to write into EEPROM/FLASH by user
354 **                  pNotify - notify caller after getting response
355 **                  pContext - caller context
356 **
357 ** Returns          NFC status:
358 **                  NFCSTATUS_SUCCESS - Write request to NFCC is successful
359 **                  NFCSTATUS_FAILED - Write request failed due to internal
360 **                                     error
361 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
362 **                  Other command specific errors
363 **
364 *******************************************************************************/
phDnldNfc_Write(bool_t bRecoverSeq,pphDnldNfc_Buff_t pData,pphDnldNfc_RspCb_t pNotify,void * pContext)365 NFCSTATUS phDnldNfc_Write(bool_t bRecoverSeq, pphDnldNfc_Buff_t pData,
366                           pphDnldNfc_RspCb_t pNotify, void* pContext) {
367   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
368   uint8_t* pImgPtr = NULL;
369   uint16_t wLen = 0;
370   phDnldNfc_Buff_t tImgBuff;
371 
372   if ((NULL == pNotify) || (NULL == pContext)) {
373     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
374     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
375   } else {
376     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
377       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
378       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
379     } else {
380       if (NULL != pData) {
381         pImgPtr = pData->pBuff;
382         wLen = pData->wLen;
383       } else {
384         if (bRecoverSeq == false) {
385           pImgPtr = (uint8_t*)gpphDnldContext->nxp_nfc_fw;
386           wLen = gpphDnldContext->nxp_nfc_fw_len;
387 
388         } else {
389           if (PH_DL_STATUS_PLL_ERROR == (gpphDnldContext->tLastStatus)) {
390             wStatus = phDnldNfc_LoadRecInfo();
391           } else if (PH_DL_STATUS_SIGNATURE_ERROR ==
392                      (gpphDnldContext->tLastStatus)) {
393             wStatus = phDnldNfc_LoadPKInfo();
394           } else {
395           }
396 
397           if (NFCSTATUS_SUCCESS == wStatus) {
398             pImgPtr = (uint8_t*)gpphDnldContext->nxp_nfc_fwp;
399             wLen = gpphDnldContext->nxp_nfc_fwp_len;
400           } else {
401             NXPLOG_FWDNLD_E("Platform Recovery Image extraction Failed!!");
402             pImgPtr = NULL;
403             wLen = 0;
404           }
405         }
406       }
407 
408       if ((NULL != pImgPtr) && (0 != wLen)) {
409         tImgBuff.pBuff = pImgPtr;
410         tImgBuff.wLen = wLen;
411 
412         (gpphDnldContext->tCmdId) = PH_DL_CMD_WRITE;
413         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTWrite;
414         (gpphDnldContext->tRspBuffInfo.pBuff) = NULL;
415         (gpphDnldContext->tRspBuffInfo.wLen) = 0;
416         (gpphDnldContext->tUserData.pBuff) = pImgPtr;
417         (gpphDnldContext->tUserData.wLen) = wLen;
418         (gpphDnldContext->bResendLastFrame) = false;
419 
420         memset(&(gpphDnldContext->tRWInfo), 0,
421                sizeof(gpphDnldContext->tRWInfo));
422         (gpphDnldContext->tRWInfo.bFirstWrReq) = true;
423         (gpphDnldContext->UserCb) = pNotify;
424         (gpphDnldContext->UserCtxt) = pContext;
425 
426         wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventWrite);
427 
428         if (NFCSTATUS_PENDING == wStatus) {
429           NXPLOG_FWDNLD_D("Write Request submitted successfully");
430         } else {
431           NXPLOG_FWDNLD_E("Write Request Failed!!");
432         }
433       } else {
434         NXPLOG_FWDNLD_E("Download Image Primitives extraction failed!!");
435         wStatus = NFCSTATUS_FAILED;
436       }
437     }
438   }
439 
440   return wStatus;
441 }
442 
443 /*******************************************************************************
444 **
445 ** Function         phDnldNfc_Log
446 **
447 ** Description      Provides a full page free write to EEPROM
448 **
449 ** Parameters       pData - data buffer to write into EEPROM/FLASH by user
450 **                  pNotify - notify caller after getting response
451 **                  pContext - caller context
452 **
453 ** Returns          NFC status:
454 **                  NFCSTATUS_SUCCESS - Write request to NFCC is successful
455 **                  NFCSTATUS_FAILED - Write request failed due to internal
456 **                                     error
457 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
458 **                  Other command specific error
459 **
460 *******************************************************************************/
phDnldNfc_Log(pphDnldNfc_Buff_t pData,pphDnldNfc_RspCb_t pNotify,void * pContext)461 NFCSTATUS phDnldNfc_Log(pphDnldNfc_Buff_t pData, pphDnldNfc_RspCb_t pNotify,
462                         void* pContext) {
463   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
464 
465   if ((NULL == pNotify) || (NULL == pData) || (NULL == pContext)) {
466     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
467     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
468   } else {
469     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
470       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
471       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
472     } else {
473       if ((NULL != (pData->pBuff)) &&
474           ((0 != (pData->wLen) && (PHDNLDNFC_MAX_LOG_SIZE >= (pData->wLen))))) {
475         (gpphDnldContext->tCmdId) = PH_DL_CMD_LOG;
476         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTLog;
477         (gpphDnldContext->tRspBuffInfo.pBuff) = NULL;
478         (gpphDnldContext->tRspBuffInfo.wLen) = 0;
479         (gpphDnldContext->tUserData.pBuff) = (pData->pBuff);
480         (gpphDnldContext->tUserData.wLen) = (pData->wLen);
481 
482         memset(&(gpphDnldContext->tRWInfo), 0,
483                sizeof(gpphDnldContext->tRWInfo));
484         (gpphDnldContext->UserCb) = pNotify;
485         (gpphDnldContext->UserCtxt) = pContext;
486 
487         wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventLog);
488 
489         if (NFCSTATUS_PENDING == wStatus) {
490           NXPLOG_FWDNLD_D("Log Request submitted successfully");
491         } else {
492           NXPLOG_FWDNLD_E("Log Request Failed!!");
493         }
494       } else {
495         NXPLOG_FWDNLD_E("Invalid Input Parameters for Log!!");
496         wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
497       }
498     }
499   }
500 
501   return wStatus;
502 }
503 
504 /*******************************************************************************
505 **
506 ** Function         phDnldNfc_Force
507 **
508 ** Description      Used as an emergency recovery procedure for NFCC due to
509 **                  corrupt settings of system platform specific parameters by
510 **                  the host
511 **
512 ** Parameters       pInputs - input buffer which contains  clk src & clk freq
513 **                            settings for desired platform
514 **                  pNotify - notify caller after getting response
515 **                  pContext - caller context
516 **
517 ** Returns          NFC status:
518 **                  NFCSTATUS_SUCCESS - Emergency Recovery request is successful
519 **                  NFCSTATUS_FAILED - Emergency Recovery failed due to internal
520 **                                     error
521 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
522 **                  Other command specific errors
523 **
524 *******************************************************************************/
phDnldNfc_Force(pphDnldNfc_Buff_t pInputs,pphDnldNfc_RspCb_t pNotify,void * pContext)525 NFCSTATUS phDnldNfc_Force(pphDnldNfc_Buff_t pInputs, pphDnldNfc_RspCb_t pNotify,
526                           void* pContext) {
527   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
528   uint8_t bClkSrc = 0x00, bClkFreq = 0x00;
529   uint8_t bPldVal[3] = {
530       0x11, 0x00, 0x00}; /* default values to be used if input not provided */
531 
532   if ((NULL == pNotify) || (NULL == pContext)) {
533     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
534     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
535   } else {
536     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
537       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
538       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
539     } else {
540       (gpphDnldContext->tCmdId) = PH_DL_CMD_FORCE;
541       (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTForce;
542       (gpphDnldContext->tRspBuffInfo.pBuff) = NULL;
543       (gpphDnldContext->tRspBuffInfo.wLen) = 0;
544 
545       if ((0 != (pInputs->wLen)) || (NULL != (pInputs->pBuff))) {
546         if (CLK_SRC_XTAL == (pInputs->pBuff[0])) {
547           bClkSrc = phDnldNfc_ClkSrcXtal;
548         } else if (CLK_SRC_PLL == (pInputs->pBuff[0])) {
549           bClkSrc = phDnldNfc_ClkSrcPLL;
550           if (CLK_FREQ_13MHZ == (pInputs->pBuff[1])) {
551             bClkFreq = phDnldNfc_ClkFreq_13Mhz;
552           } else if (CLK_FREQ_19_2MHZ == (pInputs->pBuff[1])) {
553             bClkFreq = phDnldNfc_ClkFreq_19_2Mhz;
554           } else if (CLK_FREQ_24MHZ == (pInputs->pBuff[1])) {
555             bClkFreq = phDnldNfc_ClkFreq_24Mhz;
556           } else if (CLK_FREQ_26MHZ == (pInputs->pBuff[1])) {
557             bClkFreq = phDnldNfc_ClkFreq_26Mhz;
558           } else if (CLK_FREQ_38_4MHZ == (pInputs->pBuff[1])) {
559             bClkFreq = phDnldNfc_ClkFreq_38_4Mhz;
560           } else if (CLK_FREQ_52MHZ == (pInputs->pBuff[1])) {
561             bClkFreq = phDnldNfc_ClkFreq_52Mhz;
562           } else {
563             NXPLOG_FWDNLD_E(
564                 "Invalid Clk Frequency !! Using default value of 19.2Mhz..");
565             bClkFreq = phDnldNfc_ClkFreq_19_2Mhz;
566           }
567 
568         } else if (CLK_SRC_PADDIRECT == (pInputs->pBuff[0])) {
569           bClkSrc = phDnldNfc_ClkSrcPad;
570         } else {
571           NXPLOG_FWDNLD_E("Invalid Clk src !! Using default value of PLL..");
572           bClkSrc = phDnldNfc_ClkSrcPLL;
573         }
574 
575         bPldVal[0] = 0U;
576         bPldVal[0] = ((bClkSrc << 3U) | bClkFreq);
577       } else {
578         NXPLOG_FWDNLD_E("Clk src inputs not provided!! Using default values..");
579       }
580 
581       (gpphDnldContext->tUserData.pBuff) = bPldVal;
582       (gpphDnldContext->tUserData.wLen) = sizeof(bPldVal);
583 
584       memset(&(gpphDnldContext->tRWInfo), 0, sizeof(gpphDnldContext->tRWInfo));
585       (gpphDnldContext->UserCb) = pNotify;
586       (gpphDnldContext->UserCtxt) = pContext;
587 
588       wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventForce);
589 
590       if (NFCSTATUS_PENDING == wStatus) {
591         NXPLOG_FWDNLD_D("Force Command Request submitted successfully");
592       } else {
593         NXPLOG_FWDNLD_E("Force Command Request Failed!!");
594       }
595     }
596   }
597 
598   return wStatus;
599 }
600 
601 /*******************************************************************************
602 **
603 ** Function         phDnldNfc_SetHwDevHandle
604 **
605 ** Description      Stores the HwDev handle to download context. The handle is
606 **                  required for subsequent operations
607 **
608 ** Parameters       None
609 **
610 ** Returns          None                -
611 **
612 *******************************************************************************/
phDnldNfc_SetHwDevHandle(void)613 void phDnldNfc_SetHwDevHandle(void) {
614   pphDnldNfc_DlContext_t psDnldContext = NULL;
615 
616   if (NULL == gpphDnldContext) {
617     NXPLOG_FWDNLD_D("Allocating Mem for Dnld Context..");
618     /* Create the memory for Download Mgmt Context */
619     psDnldContext =
620         (pphDnldNfc_DlContext_t)malloc(sizeof(phDnldNfc_DlContext_t));
621 
622     if (psDnldContext != NULL) {
623       (void)memset((void*)psDnldContext, 0, sizeof(phDnldNfc_DlContext_t));
624       gpphDnldContext = psDnldContext;
625     } else {
626       NXPLOG_FWDNLD_E("Error Allocating Mem for Dnld Context..")
627     }
628   } else {
629     (void)memset((void*)gpphDnldContext, 0, sizeof(phDnldNfc_DlContext_t));
630   }
631   return;
632 }
633 
634 /*******************************************************************************
635 **
636 ** Function         phDnldNfc_ReSetHwDevHandle
637 **
638 ** Description      Frees the HwDev handle to download context.
639 **
640 ** Parameters       None
641 **
642 ** Returns          None                -
643 **
644 *******************************************************************************/
phDnldNfc_ReSetHwDevHandle(void)645 void phDnldNfc_ReSetHwDevHandle(void) {
646   if (gpphDnldContext != NULL) {
647     NXPLOG_FWDNLD_D("Freeing Mem for Dnld Context..")
648     free(gpphDnldContext);
649     gpphDnldContext = NULL;
650   }
651 }
652 
653 /*******************************************************************************
654 **
655 ** Function         phDnldNfc_RawReq
656 **
657 ** Description      Sends raw frame request to NFCC.
658 **                  It is currently used for sending an NCI RESET cmd after
659 **                  doing a production key update
660 **
661 ** Parameters       pFrameData - input buffer, contains raw frame packet to be
662 **                               sent to NFCC
663 **                  pRspData - response buffer received from NFCC
664 **                  pNotify - notify caller after getting response
665 **                  pContext - caller context
666 **
667 ** Returns          NFC status:
668 **                  NFCSTATUS_SUCCESS - GetSessionState request to NFCC is
669 **                                      successful
670 **                  NFCSTATUS_FAILED - GetSessionState request failed due to
671 **                                     internal error
672 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
673 **                  Other command specific errors
674 **
675 *******************************************************************************/
phDnldNfc_RawReq(pphDnldNfc_Buff_t pFrameData,pphDnldNfc_Buff_t pRspData,pphDnldNfc_RspCb_t pNotify,void * pContext)676 NFCSTATUS phDnldNfc_RawReq(pphDnldNfc_Buff_t pFrameData,
677                            pphDnldNfc_Buff_t pRspData,
678                            pphDnldNfc_RspCb_t pNotify, void* pContext) {
679   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
680 
681   if ((NULL == pFrameData) || (NULL == pNotify) || (NULL == pRspData) ||
682       (NULL == pContext)) {
683     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
684     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
685   } else {
686     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
687       NXPLOG_FWDNLD_E("Raw Cmd Request in Progress..Cannot Continue!!");
688       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
689     } else {
690       if (((NULL != pFrameData->pBuff) && (0 != pFrameData->wLen)) &&
691           ((NULL != pRspData->pBuff) && (0 != pRspData->wLen))) {
692         (gpphDnldContext->tRspBuffInfo.pBuff) = pRspData->pBuff;
693         (gpphDnldContext->tRspBuffInfo.wLen) = pRspData->wLen;
694         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTRaw;
695         (gpphDnldContext->tCmdId) = PH_DL_CMD_NONE;
696         (gpphDnldContext->tUserData.pBuff) = pFrameData->pBuff;
697         (gpphDnldContext->tUserData.wLen) = pFrameData->wLen;
698         (gpphDnldContext->UserCb) = pNotify;
699         (gpphDnldContext->UserCtxt) = pContext;
700 
701         wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventRaw);
702 
703         if (NFCSTATUS_PENDING == wStatus) {
704           NXPLOG_FWDNLD_D("RawFrame Request submitted successfully");
705         } else {
706           NXPLOG_FWDNLD_E("RawFrame Request Failed!!");
707         }
708       } else {
709         NXPLOG_FWDNLD_E("Invalid Buff Parameters!!");
710         wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
711       }
712     }
713   }
714 
715   return wStatus;
716 }
717 
718 /*******************************************************************************
719 **
720 ** Function         phDnldNfc_InitImgInfo
721 **
722 ** Description      Extracts image information and stores it in respective
723 **                  variables, to be used internally for write operation
724 **
725 ** Parameters       None
726 **
727 ** Returns          NFC status
728 **
729 *******************************************************************************/
phDnldNfc_InitImgInfo(void)730 NFCSTATUS phDnldNfc_InitImgInfo(void) {
731   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
732   uint8_t* pImageInfo = NULL;
733   uint16_t ImageInfoLen = 0;
734   unsigned long fwType = FW_FORMAT_SO;
735 
736   /* if memory is not allocated then allocate memory for download context
737    * structure */
738   phDnldNfc_SetHwDevHandle();
739 
740   gpphDnldContext->FwFormat = FW_FORMAT_UNKNOWN;
741 
742   /*Read Firmware file name from config file*/
743   if (GetNxpNumValue(NAME_NXP_FW_TYPE, &fwType, sizeof(fwType)) == true) {
744     NXPLOG_FWDNLD_D("firmware type from conf file: %lu", fwType);
745   } else {
746     NXPLOG_FWDNLD_W("firmware type not found. Taking default value: %lu",
747                     fwType);
748   }
749 
750   if (fwType == FW_FORMAT_BIN) {
751     gpphDnldContext->FwFormat = FW_FORMAT_BIN;
752     wStatus = phDnldNfc_LoadBinFW(&pImageInfo, &ImageInfoLen);
753   } else if (fwType == FW_FORMAT_SO) {
754     gpphDnldContext->FwFormat = FW_FORMAT_SO;
755     if (gRecFWDwnld == true) {
756       wStatus = phDnldNfc_LoadRecoveryFW(&pImageInfo, &ImageInfoLen);
757     } else {
758       wStatus = phDnldNfc_LoadFW(&pImageInfo, &ImageInfoLen);
759     }
760   } else {
761     NXPLOG_FWDNLD_E("firmware file format mismatch!!!\n");
762     return NFCSTATUS_FAILED;
763   }
764 
765   NXPLOG_FWDNLD_D("FW Image Length - ImageInfoLen %d", ImageInfoLen);
766   NXPLOG_FWDNLD_D("FW Image Info Pointer - pImageInfo %p", pImageInfo);
767 
768   if ((pImageInfo == NULL) || (ImageInfoLen == 0)) {
769     NXPLOG_FWDNLD_E(
770         "Image extraction Failed - invalid imginfo or imginfolen!!");
771     wStatus = NFCSTATUS_FAILED;
772   }
773 
774   if (wStatus != NFCSTATUS_SUCCESS) {
775     NXPLOG_FWDNLD_E("Error loading libpn54x_fw !!\n");
776   }
777 
778   /* get the MW version */
779   if (NFCSTATUS_SUCCESS == wStatus) {
780     // NXPLOG_FWDNLD_D("MW Major Version Num - %x",NXP_MW_VERSION_MAJ);
781     // NXPLOG_FWDNLD_D("MW Minor Version Num - %x",NXP_MW_VERSION_MIN);
782     wMwVer = (((uint16_t)(NXP_MW_VERSION_MAJ) << 8U) | (NXP_MW_VERSION_MIN));
783   }
784 
785   if (NFCSTATUS_SUCCESS == wStatus) {
786     gpphDnldContext->nxp_nfc_fw = (uint8_t*)pImageInfo;
787     gpphDnldContext->nxp_nfc_fw_len = ImageInfoLen;
788     if ((NULL != gpphDnldContext->nxp_nfc_fw) &&
789         (0 != gpphDnldContext->nxp_nfc_fw_len)) {
790       NXPLOG_FWDNLD_D("FW Major Version Num - %x",
791                       gpphDnldContext->nxp_nfc_fw[5]);
792       NXPLOG_FWDNLD_D("FW Minor Version Num - %x",
793                       gpphDnldContext->nxp_nfc_fw[4]);
794       NXPLOG_FWDNLD_D("FW Image Length - %d", ImageInfoLen);
795       NXPLOG_FWDNLD_D("FW Image Info Pointer - %p", pImageInfo);
796 
797       /* get the FW version */
798       wFwVer = (((uint16_t)(gpphDnldContext->nxp_nfc_fw[5]) << 8U) |
799                 (gpphDnldContext->nxp_nfc_fw[4]));
800       wStatus = NFCSTATUS_SUCCESS;
801     } else {
802       NXPLOG_FWDNLD_E("Image details extraction Failed!!");
803       wStatus = NFCSTATUS_FAILED;
804     }
805   }
806 
807   return wStatus;
808 }
809 
810 /*******************************************************************************
811 **
812 ** Function         phDnldNfc_LoadRecInfo
813 **
814 ** Description      Extracts recovery sequence image information and stores it
815 **                  in respective variables, to be used internally for write
816 **                  operation
817 **
818 ** Parameters       None
819 **
820 ** Returns          NFC status
821 **
822 *******************************************************************************/
phDnldNfc_LoadRecInfo(void)823 NFCSTATUS phDnldNfc_LoadRecInfo(void) {
824   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
825   uint8_t* pImageInfo = NULL;
826   uint16_t ImageInfoLen = 0;
827 
828   /* if memory is not allocated then allocate memory for donwload context
829    * structure */
830   phDnldNfc_SetHwDevHandle();
831   if (gRecFWDwnld == true)
832     wStatus = phDnldNfc_LoadRecoveryFW(&pImageInfo, &ImageInfoLen);
833   else
834     wStatus = phDnldNfc_LoadFW(&pImageInfo, &ImageInfoLen);
835 
836   if ((pImageInfo == NULL) || (ImageInfoLen == 0)) {
837     NXPLOG_FWDNLD_E(
838         "Image extraction Failed - invalid imginfo or imginfolen!!");
839     wStatus = NFCSTATUS_FAILED;
840   }
841 
842   /* load the PLL recovery image library */
843   if (wStatus != NFCSTATUS_SUCCESS) {
844     NXPLOG_FWDNLD_E("Error loading libpn54x_fw_platform !!\n");
845   }
846 
847   if (NFCSTATUS_SUCCESS == wStatus) {
848     /* fetch the PLL recovery image pointer and the image length */
849     gpphDnldContext->nxp_nfc_fwp = (uint8_t*)pImageInfo;
850     gpphDnldContext->nxp_nfc_fwp_len = ImageInfoLen;
851     if ((NULL != gpphDnldContext->nxp_nfc_fwp) &&
852         (0 != gpphDnldContext->nxp_nfc_fwp_len)) {
853       NXPLOG_FWDNLD_D("Recovery Image Length - %d", ImageInfoLen);
854       NXPLOG_FWDNLD_D("Recovery Image Info Pointer - %p", pImageInfo);
855       wStatus = NFCSTATUS_SUCCESS;
856     } else {
857       NXPLOG_FWDNLD_E("Recovery Image details extraction Failed!!");
858       wStatus = NFCSTATUS_FAILED;
859     }
860   }
861 
862   return wStatus;
863 }
864 
865 /*******************************************************************************
866 **
867 ** Function         phDnldNfc_LoadPKInfo
868 **
869 ** Description      Extracts production sequence image information and stores it
870 **                  in respective variables, to be used internally for write
871 **                  operation
872 **
873 ** Parameters       None
874 **
875 ** Returns          NFC status
876 **
877 *******************************************************************************/
phDnldNfc_LoadPKInfo(void)878 NFCSTATUS phDnldNfc_LoadPKInfo(void) {
879   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
880   uint8_t* pImageInfo = NULL;
881   uint16_t ImageInfoLen = 0;
882 
883   /* if memory is not allocated then allocate memory for donwload context
884    * structure */
885   phDnldNfc_SetHwDevHandle();
886   /* load the PKU image library */
887   if (gRecFWDwnld == true)
888     wStatus = phDnldNfc_LoadRecoveryFW(&pImageInfo, &ImageInfoLen);
889   else
890     wStatus = phDnldNfc_LoadFW(&pImageInfo, &ImageInfoLen);
891   if ((pImageInfo == NULL) || (ImageInfoLen == 0)) {
892     NXPLOG_FWDNLD_E(
893         "Image extraction Failed - invalid imginfo or imginfolen!!");
894     wStatus = NFCSTATUS_FAILED;
895   }
896 
897   if (wStatus != NFCSTATUS_SUCCESS) {
898     NXPLOG_FWDNLD_E("Error loading libpn54x_fw_pku !!\n");
899   }
900 
901   if (NFCSTATUS_SUCCESS == wStatus) {
902     /* fetch the PKU image pointer and the image length */
903     gpphDnldContext->nxp_nfc_fwp = (uint8_t*)pImageInfo;
904     gpphDnldContext->nxp_nfc_fwp_len = ImageInfoLen;
905 
906     if ((NULL != gpphDnldContext->nxp_nfc_fwp) &&
907         (0 != gpphDnldContext->nxp_nfc_fwp_len)) {
908       NXPLOG_FWDNLD_D("PKU Image Length - %d", ImageInfoLen);
909       NXPLOG_FWDNLD_D("PKU Image Info Pointer - %p", pImageInfo);
910       wStatus = NFCSTATUS_SUCCESS;
911     } else {
912       NXPLOG_FWDNLD_E("PKU Image details extraction Failed!!");
913       wStatus = NFCSTATUS_FAILED;
914     }
915   }
916 
917   return wStatus;
918 }
919 
920 /*******************************************************************************
921 **
922 ** Function         phDnldNfc_CloseFwLibHandle
923 **
924 ** Description      Closes previously opened fw library handle as part of
925 **                  dynamic loader processing
926 **
927 ** Parameters       None
928 **
929 ** Returns          None
930 **
931 *******************************************************************************/
phDnldNfc_CloseFwLibHandle(void)932 void phDnldNfc_CloseFwLibHandle(void) {
933   NFCSTATUS wStatus = NFCSTATUS_FAILED;
934   if (gpphDnldContext->FwFormat == FW_FORMAT_SO) {
935     wStatus = phDnldNfc_UnloadFW();
936     if (wStatus != NFCSTATUS_SUCCESS) {
937       NXPLOG_FWDNLD_E("free library FAILED !!\n");
938     } else {
939       NXPLOG_FWDNLD_E("free library SUCCESS !!\n");
940     }
941   } else if (gpphDnldContext->FwFormat == FW_FORMAT_BIN) {
942     if (pFwHandle != NULL) {
943       free(pFwHandle);
944       pFwHandle = NULL;
945     }
946   }
947   return;
948 }
949 
950 /*******************************************************************************
951 **
952 ** Function         phDnldNfc_LoadFW
953 **
954 ** Description      Load the firmware version form firmware lib
955 **
956 ** Parameters       pImgInfo    - Firmware image handle
957 **                  pImgInfoLen - Firmware image length
958 **
959 ** Returns          NFC status
960 **
961 *******************************************************************************/
phDnldNfc_LoadFW(uint8_t ** pImgInfo,uint16_t * pImgInfoLen)962 NFCSTATUS phDnldNfc_LoadFW(uint8_t** pImgInfo, uint16_t* pImgInfoLen) {
963   void* pImageInfo = NULL;
964   void* pImageInfoLen = NULL;
965 
966   /* check if the handle is not NULL then free the library */
967   if (pFwHandle != NULL) {
968     phDnldNfc_CloseFwLibHandle();
969     pFwHandle = NULL;
970   }
971 
972   /* load the DLL file */
973   pFwHandle = dlopen(nfcFL._FW_LIB_PATH.c_str(), RTLD_LAZY);
974   NXPLOG_FWDNLD_D("@@@%s", nfcFL._FW_LIB_PATH.c_str());
975 
976   /* if library load failed then handle will be NULL */
977   if (pFwHandle == NULL) {
978     NXPLOG_FWDNLD_E(
979         "NULL handler : unable to load the library file, specify correct path");
980     return NFCSTATUS_FAILED;
981   }
982 
983   dlerror(); /* Clear any existing error */
984 
985   /* load the address of download image pointer and image size */
986   pImageInfo = (void*)dlsym(pFwHandle, "gphDnldNfc_DlSeq");
987 
988   if (dlerror() || (NULL == pImageInfo)) {
989     NXPLOG_FWDNLD_E("Problem loading symbol : gphDnldNfc_DlSeq");
990     return NFCSTATUS_FAILED;
991   }
992   (*pImgInfo) = (*(uint8_t**)pImageInfo);
993 
994   pImageInfoLen = (void*)dlsym(pFwHandle, "gphDnldNfc_DlSeqSz");
995   if (dlerror() || (NULL == pImageInfoLen)) {
996     NXPLOG_FWDNLD_E("Problem loading symbol : gphDnldNfc_DlSeqSz");
997     return NFCSTATUS_FAILED;
998   }
999 
1000   (*pImgInfoLen) = (uint16_t)(*((uint16_t*)pImageInfoLen));
1001 
1002   return NFCSTATUS_SUCCESS;
1003 }
1004 
1005 /*******************************************************************************
1006 **
1007 ** Function         phDnldNfc_LoadBinFW
1008 **
1009 ** Description      Load the firmware version form firmware lib
1010 **
1011 ** Parameters       pImgInfo    - Firmware image handle
1012 **                  pImgInfoLen - Firmware image length
1013 **
1014 ** Returns          NFC status
1015 **
1016 *******************************************************************************/
phDnldNfc_LoadBinFW(uint8_t ** pImgInfo,uint16_t * pImgInfoLen)1017 NFCSTATUS phDnldNfc_LoadBinFW(uint8_t** pImgInfo, uint16_t* pImgInfoLen) {
1018   FILE* pFile = NULL;
1019   long fileSize = 0;
1020   long bytesRead = 0;
1021   long ftellFileSize = 0;
1022 
1023   /* check for path name */
1024   if (nfcFL._FW_BIN_PATH.c_str() == NULL) {
1025     NXPLOG_FWDNLD_E("Invalid FW file path!!!\n");
1026     return NFCSTATUS_FAILED;
1027   }
1028 
1029   /* check if the handle is not NULL then free the memory*/
1030   if (pFwHandle != NULL) {
1031     phDnldNfc_CloseFwLibHandle();
1032     pFwHandle = NULL;
1033   }
1034 
1035   /* Open the FW binary image file to be read */
1036   pFile = fopen(nfcFL._FW_BIN_PATH.c_str(), "r");
1037   if (NULL == pFile) {
1038     NXPLOG_FWDNLD_E("Failed to load FW binary image file!!!\n");
1039     return NFCSTATUS_FAILED;
1040   }
1041 
1042   /* Seek to the end of the file */
1043   fseek(pFile, 0, SEEK_END);
1044 
1045   /* get the actual length of the file */
1046   ftellFileSize = ftell(pFile);
1047 
1048   if (ftellFileSize > 0) {
1049     fileSize = ftellFileSize;
1050   } else {
1051     fileSize = 0;
1052   }
1053 
1054   /* Seek to the start of the file, to move file handle back to start of file*/
1055   fseek(pFile, 0, SEEK_SET);
1056 
1057   /* allocate the memory to read the FW binary image */
1058   pFwHandle = (void*)malloc(sizeof(uint8_t) * fileSize);
1059 
1060   /* check for valid memory allocation */
1061   if (NULL == pFwHandle) {
1062     NXPLOG_FWDNLD_E("Failed to allocate memory to load FW image !!!\n");
1063     fclose(pFile);
1064     return NFCSTATUS_FAILED;
1065   }
1066 
1067   /* Read the actual contents of the FW binary image */
1068   bytesRead =
1069       (uint32_t)fread(pFwHandle, sizeof(uint8_t), (size_t)fileSize, pFile);
1070   if (bytesRead != fileSize) {
1071     NXPLOG_FWDNLD_E("Unable to read the specified size from file !!!\n");
1072     fclose(pFile);
1073     free(pFwHandle);
1074     pFwHandle = NULL;
1075     return NFCSTATUS_FAILED;
1076   }
1077 
1078   /* Update the image info pointer to the caller */
1079   *pImgInfo = (uint8_t*)pFwHandle;
1080   *pImgInfoLen = (uint16_t)(bytesRead & 0xFFFF);
1081 
1082   /* close the FW binary image file */
1083   fclose(pFile);
1084   return NFCSTATUS_SUCCESS;
1085 }
1086 
1087 /*******************************************************************************
1088 **
1089 ** Function         phDnldNfc_LoadRecoveryFW
1090 **
1091 ** Description      Load the dummy firmware version form firmware lib for
1092 **                  recovery. This will change the FW version of the NFCC
1093 **                  firmware and enable flashing of firmware of same version.
1094 **
1095 ** Parameters       pImgInfo    - Firmware image handle
1096 **                  pImgInfoLen - Firmware image length
1097 **
1098 ** Returns          NFCSTATUS
1099 **
1100 *******************************************************************************/
phDnldNfc_LoadRecoveryFW(uint8_t ** pImgInfo,uint16_t * pImgInfoLen)1101 NFCSTATUS phDnldNfc_LoadRecoveryFW(uint8_t** pImgInfo, uint16_t* pImgInfoLen) {
1102   void* pImageInfo = NULL;
1103   void* pImageInfoLen = NULL;
1104 
1105   /* check if the handle is not NULL then free the library */
1106   if (pFwHandle != NULL) {
1107     phDnldNfc_CloseFwLibHandle();
1108     pFwHandle = NULL;
1109   }
1110   /* load the DLL file */
1111   pFwHandle = dlopen(nfcFL._FW_LIB_PATH.c_str(), RTLD_LAZY);
1112   NXPLOG_FWDNLD_D("phDnldNfc_LoadRecoveryFW %s ", nfcFL._FW_LIB_PATH.c_str());
1113 
1114   /* if library load failed then handle will be NULL */
1115   if (pFwHandle == NULL) {
1116     NXPLOG_FWDNLD_E(
1117         "NULL handler : unable to load the library file, specify correct path");
1118     return NFCSTATUS_FAILED;
1119   }
1120 
1121   dlerror(); /* Clear any existing error */
1122 
1123   /* load the address of download image pointer and image size */
1124   pImageInfo = (void*)dlsym(pFwHandle, "gphDnldNfc_DummyDlSeq");
1125 
1126   if (dlerror() || (NULL == pImageInfo)) {
1127     NXPLOG_FWDNLD_E("Problem loading symbol : gphDnldNfc_DummyDlSeq");
1128     return NFCSTATUS_FAILED;
1129   }
1130 
1131   (*pImgInfo) = (*(uint8_t**)pImageInfo);
1132   pImageInfoLen = (void*)dlsym(pFwHandle, "gphDnldNfc_DlSeqDummyFwSz");
1133   if (dlerror() || (NULL == pImageInfoLen)) {
1134     NXPLOG_FWDNLD_E("Problem loading symbol : gphDnldNfc_DlSeqDummyFwSz");
1135     return NFCSTATUS_FAILED;
1136   }
1137 
1138   (*pImgInfoLen) = (uint16_t)(*((uint16_t*)pImageInfoLen));
1139 
1140   return NFCSTATUS_SUCCESS;
1141 }
1142 
1143 /*******************************************************************************
1144 **
1145 ** Function         phDnldNfc_UnloadFW
1146 **
1147 ** Description      Deinit the firmware handle
1148 **
1149 ** Parameters       None
1150 **
1151 ** Returns          NFC status
1152 **
1153 *******************************************************************************/
phDnldNfc_UnloadFW(void)1154 NFCSTATUS phDnldNfc_UnloadFW(void) {
1155   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1156   int32_t status;
1157 
1158   /* check if the handle is not NULL then free the library */
1159   if (pFwHandle != NULL) {
1160     status = dlclose(pFwHandle);
1161     pFwHandle = NULL;
1162 
1163     dlerror(); /* Clear any existing error */
1164     if (status != 0) {
1165       wStatus = NFCSTATUS_FAILED;
1166       NXPLOG_FWDNLD_E("Free library file failed");
1167     }
1168   }
1169 
1170   return wStatus;
1171 }
1172 
1173 #ifdef EEPROM_Read_Mem_IMP
1174 static pphDnldNfc_RspCb_t UserCb; /* Upper layer call back function */
1175 static void* UserCtxt;            /* Pointer to upper layer context */
1176 /* Function prototype declaration */
1177 static void phDnldNfc_ReadComplete(void* pContext, NFCSTATUS status,
1178                                    void* pInfo);
1179 
1180 /*******************************************************************************
1181 **
1182 ** Function         phDnldNfc_ReadMem
1183 **
1184 ** Description      Dumps the contents of EEPROM. The handle is required for
1185 **                  subsequent operations
1186 **
1187 ** Parameters       pHwRef - pointer to the hardware device
1188 **                  pNotify - notify caller after getting response
1189 **                  pContext - caller context
1190 **
1191 ** Returns          NFC status:
1192 **                  NFCSTATUS_SUCCESS - request to NFCC is successful
1193 **                  NFCSTATUS_FAILED - request failed due to internal error
1194 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
1195 **                  Other command specific errors
1196 **
1197 *******************************************************************************/
phDnldNfc_ReadMem(void * pHwRef,pphDnldNfc_RspCb_t pNotify,void * pContext)1198 NFCSTATUS phDnldNfc_ReadMem(void* pHwRef, pphDnldNfc_RspCb_t pNotify,
1199                             void* pContext) {
1200   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1201   uint32_t wAddr = 0x2011C0; /* eeprom platform specific area start address */
1202   uint32_t wRdAddr = 0;
1203   uint8_t* pAddr;
1204   static uint8_t bRdData[3519]; /* buffer to hold the read data */
1205   static phDnldNfc_Buff_t Data;
1206 
1207   if ((NULL == pNotify) || (NULL == pContext)) {
1208     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
1209     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
1210   } else {
1211     /* Call Tml Ioctl to enable download mode */
1212     wStatus = phTmlNfc_IoCtl(phTmlNfc_e_EnableDownloadMode);
1213 
1214     if (NFCSTATUS_SUCCESS == wStatus) {
1215       /* Set the obtained device handle to download module */
1216       phDnldNfc_SetHwDevHandle();
1217     } else {
1218       wStatus = NFCSTATUS_FAILED;
1219     }
1220 
1221     if (NFCSTATUS_SUCCESS == wStatus) {
1222       pAddr = (uint8_t*)&wAddr;
1223 
1224       wRdAddr = (pAddr[3]);
1225       wRdAddr <<= 8;
1226       wRdAddr |= (pAddr[2]);
1227       wRdAddr <<= 8;
1228       wRdAddr |= (pAddr[1]);
1229       wRdAddr <<= 8;
1230       wRdAddr |= (pAddr[0]);
1231 
1232       Data.pBuff = bRdData;
1233       Data.wLen = sizeof(bRdData);
1234       UserCb = pNotify;
1235       UserCtxt = pContext;
1236 
1237       wStatus = phDnldNfc_Read(&Data, wRdAddr,
1238                                (pphDnldNfc_RspCb_t)phDnldNfc_ReadComplete,
1239                                gpphDnldContext);
1240     } else {
1241       Data.pBuff = NULL;
1242       Data.wLen = 0;
1243       wStatus = NFCSTATUS_FAILED;
1244     }
1245 
1246     if (NFCSTATUS_PENDING == wStatus) {
1247       NXPLOG_FWDNLD_D("Read Request submitted successfully..");
1248     } else {
1249       NXPLOG_FWDNLD_E("Read Request submission failed!!");
1250     }
1251   }
1252 
1253   return wStatus;
1254 }
1255 
1256 /*******************************************************************************
1257 **
1258 ** Function         phDnldNfc_ReadComplete
1259 **
1260 ** Description      Read complete
1261 **
1262 ** Parameters       pContext - caller layer context
1263 **                  status   - status of the transaction
1264 **                  pInfo    - transaction info
1265 **
1266 ** Returns          None
1267 **
1268 *******************************************************************************/
phDnldNfc_ReadComplete(void * pContext,NFCSTATUS status,void * pInfo)1269 static void phDnldNfc_ReadComplete(void* pContext, NFCSTATUS status,
1270                                    void* pInfo) {
1271   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1272   UNUSED(pContext);
1273 
1274   /* Call Tml Ioctl to enable/restore normal mode */
1275   wStatus = phTmlNfc_IoCtl(phTmlNfc_e_EnableNormalMode);
1276 
1277   if (NFCSTATUS_SUCCESS == wStatus) {
1278     NXPLOG_FWDNLD_D("Read Done!!");
1279   }
1280 
1281   UserCb(&UserCtxt, status, pInfo);
1282 
1283   return;
1284 }
1285 
1286 /*******************************************************************************
1287 **
1288 ** Function         phDnldNfc_Read
1289 **
1290 ** Description      Retrieves requested data of specified length from desired
1291 **                  EEPROM address
1292 **
1293 ** Parameters       pData - response buffer which gets updated with data from
1294 **                          EEPROM
1295 **                  dwRdAddr - EEPROM address for data read
1296 **                  pNotify - notify caller after getting response
1297 **                  pContext - caller context
1298 **
1299 ** Returns          NFC status:
1300 **                  NFCSTATUS_SUCCESS - Read request to NFCC is successful
1301 **                  NFCSTATUS_FAILED - Read request failed due to internal error
1302 **                  NFCSTATUS_NOT_ALLOWED - command not allowed
1303 **                  Other command specific errors
1304 **
1305 *******************************************************************************/
phDnldNfc_Read(pphDnldNfc_Buff_t pData,uint32_t dwRdAddr,pphDnldNfc_RspCb_t pNotify,void * pContext)1306 NFCSTATUS phDnldNfc_Read(pphDnldNfc_Buff_t pData, uint32_t dwRdAddr,
1307                          pphDnldNfc_RspCb_t pNotify, void* pContext) {
1308   NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1309 
1310   if ((NULL == pNotify) || (NULL == pData) || (NULL == pContext)) {
1311     NXPLOG_FWDNLD_E("Invalid Input Parameters!!");
1312     wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
1313   } else {
1314     if (phDnldNfc_TransitionIdle != gpphDnldContext->tDnldInProgress) {
1315       NXPLOG_FWDNLD_E("Dnld Cmd Request in Progress..Cannot Continue!!");
1316       wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_BUSY);
1317     } else {
1318       if ((NULL != pData->pBuff) && (0 != pData->wLen)) {
1319         (gpphDnldContext->tCmdId) = PH_DL_CMD_READ;
1320         (gpphDnldContext->FrameInp.Type) = phDnldNfc_FTRead;
1321         (gpphDnldContext->FrameInp.dwAddr) = dwRdAddr;
1322         (gpphDnldContext->tRspBuffInfo.pBuff) = pData->pBuff;
1323         (gpphDnldContext->tRspBuffInfo.wLen) = pData->wLen;
1324         (gpphDnldContext->tUserData.pBuff) = NULL;
1325         (gpphDnldContext->tUserData.wLen) = 0;
1326         (gpphDnldContext->UserCb) = pNotify;
1327         (gpphDnldContext->UserCtxt) = pContext;
1328 
1329         memset(&(gpphDnldContext->tRWInfo), 0,
1330                sizeof(gpphDnldContext->tRWInfo));
1331 
1332         wStatus = phDnldNfc_CmdHandler(gpphDnldContext, phDnldNfc_EventRead);
1333 
1334         if (NFCSTATUS_PENDING == wStatus) {
1335           NXPLOG_FWDNLD_D("Read Request submitted successfully");
1336         } else {
1337           NXPLOG_FWDNLD_E("Read Request Failed!!");
1338         }
1339       } else {
1340         NXPLOG_FWDNLD_E("Invalid Buff Parameters!!");
1341         wStatus = PHNFCSTVAL(CID_NFC_DNLD, NFCSTATUS_INVALID_PARAMETER);
1342       }
1343     }
1344   }
1345 
1346   return wStatus;
1347 }
1348 #endif
1349