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