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