blob: a7640e4907711d70ab6a475452cf42e28a442a28 [file] [log] [blame]
nxf24591dc0bc2c2018-02-21 17:33:08 +05301/*
2 * Copyright (C) 2015 The Android Open Source Project
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 */
nxf482936b11c132019-06-06 15:09:37 +053016 /******************************************************************************
17 *
18 * The original Work has been changed by NXP Semiconductors.
19 *
20 * Copyright (C) 2019 NXP Semiconductors
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 *
34 ******************************************************************************/
nxf24591dc0bc2c2018-02-21 17:33:08 +053035
36#include <android-base/stringprintf.h>
37#include <base/logging.h>
Lijo Abraham4e8ed2e2019-09-06 10:55:14 +053038#include <log/log.h>
nxf24591dc0bc2c2018-02-21 17:33:08 +053039#include <nfc_api.h>
40#include <nfc_int.h>
41#include <phNfcCompId.h>
42#include <phNxpExtns_MifareStd.h>
43#include <phNxpLog.h>
44#include <rw_api.h>
45
46using android::base::StringPrintf;
47
48extern bool nfc_debug_enabled;
49
50phNxpExtns_Context_t gphNxpExtns_Context;
51phNciNfc_TransceiveInfo_t tNciTranscvInfo;
52phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt = NULL;
53phFriNfc_NdefMap_t* NdefMap = NULL;
54phLibNfc_NdefInfo_t NdefInfo;
55#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
56pthread_mutex_t SharedDataMutex = PTHREAD_MUTEX_INITIALIZER;
57#endif
58uint8_t current_key[6] = {0};
59phNci_mfc_auth_cmd_t gAuthCmdBuf;
60static NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
61 uint8_t* buff, uint16_t* buffSz);
62static NFCSTATUS phLibNfc_SendRawCmd(
63 phNfc_sTransceiveInfo_t* pTransceiveInfo,
64 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
65static NFCSTATUS phLibNfc_SendWrt16Cmd(
66 phNfc_sTransceiveInfo_t* pTransceiveInfo,
67 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
68static NFCSTATUS phLibNfc_SendAuthCmd(
69 phNfc_sTransceiveInfo_t* pTransceiveInfo,
70 phNciNfc_TransceiveInfo_t* tNciTranscvInfo) __attribute__((unused));
71static NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
72 phNfc_sTransceiveInfo_t* pTransceiveInfo,
73 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
74static NFCSTATUS phLibNfc_MifareMap(
75 phNfc_sTransceiveInfo_t* pTransceiveInfo,
76 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
77static NFCSTATUS phLibNfc_ChkAuthCmdMFC(
78 phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey);
79static NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey);
80static void phLibNfc_CalSectorAddress(uint8_t* Sector_Address);
81static NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
82 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
83 uint16_t* buffSz);
84static NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
85 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz);
86static NFCSTATUS phLibNfc_SendWrt16CmdPayload(
87 phNfc_sTransceiveInfo_t* pTransceiveInfo,
88 pphNciNfc_TransceiveInfo_t pMappedTranscvIf);
89static NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo,
90 NFCSTATUS wStatus);
91static NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz);
92static NFCSTATUS phFriNfc_NdefSmtCrd_Reset__(
93 phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, uint8_t* SendRecvBuffer,
94 uint16_t* SendRecvBuffLen);
95static NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData,
96 uint32_t* PacketDataLength,
97 uint8_t Offset,
98 phFriNfc_NdefMap_t* pNdefMap,
99 uint8_t bNdefReq);
100static void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
101static void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
102static void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
103static void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status);
104
105/*******************************************************************************
106**
107** Function phNxpExtns_MfcModuleDeInit
108**
109** Description It Deinitializes the Mifare module.
110**
111** Frees all the memory occupied by Mifare module
112**
113** Returns:
114** NFCSTATUS_SUCCESS - if successfully deinitialize
115** NFCSTATUS_FAILED - otherwise
116**
117*******************************************************************************/
118NFCSTATUS phNxpExtns_MfcModuleDeInit(void) {
119 NFCSTATUS status = NFCSTATUS_FAILED;
120
121 if (NdefMap != NULL) {
122 if (NdefMap->psRemoteDevInfo != NULL) {
123 free(NdefMap->psRemoteDevInfo);
124 NdefMap->psRemoteDevInfo = NULL;
125 }
126 if (NdefMap->SendRecvBuf != NULL) {
127 free(NdefMap->SendRecvBuf);
128 NdefMap->SendRecvBuf = NULL;
129 }
130 if (NdefMap->SendRecvLength != NULL) {
131 free(NdefMap->SendRecvLength);
132 NdefMap->SendRecvLength = NULL;
133 }
134 if (NdefMap->DataCount != NULL) {
135 free(NdefMap->DataCount);
136 NdefMap->DataCount = NULL;
137 }
138 if (NdefMap->pTransceiveInfo != NULL) {
139 if (NdefMap->pTransceiveInfo->sSendData.buffer != NULL) {
140 free(NdefMap->pTransceiveInfo->sSendData.buffer);
141 NdefMap->pTransceiveInfo->sSendData.buffer = NULL;
142 }
143 if (NdefMap->pTransceiveInfo->sRecvData.buffer != NULL) {
144 free(NdefMap->pTransceiveInfo->sRecvData.buffer);
145 NdefMap->pTransceiveInfo->sRecvData.buffer = NULL;
146 }
147 free(NdefMap->pTransceiveInfo);
148 NdefMap->pTransceiveInfo = NULL;
149 }
150
151 free(NdefMap);
152 NdefMap = NULL;
153 }
154
155 if (tNciTranscvInfo.tSendData.pBuff != NULL) {
156 free(tNciTranscvInfo.tSendData.pBuff);
157 tNciTranscvInfo.tSendData.pBuff = NULL;
158 }
159
160 if (NdefSmtCrdFmt != NULL) {
161 free(NdefSmtCrdFmt);
162 NdefSmtCrdFmt = NULL;
163 }
164#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
165 pthread_mutex_lock(&SharedDataMutex);
166#endif
167 if (NULL != NdefInfo.psUpperNdefMsg) {
168 if (NdefInfo.psUpperNdefMsg->buffer != NULL) {
169 free(NdefInfo.psUpperNdefMsg->buffer);
170 NdefInfo.psUpperNdefMsg->buffer = NULL;
171 }
172 free(NdefInfo.psUpperNdefMsg);
173 NdefInfo.psUpperNdefMsg = NULL;
174 }
175#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
176 pthread_mutex_unlock(&SharedDataMutex);
177#endif
178 if (NULL != gAuthCmdBuf.pauth_cmd) {
179 if (NULL != gAuthCmdBuf.pauth_cmd->buffer) {
180 free(gAuthCmdBuf.pauth_cmd->buffer);
181 gAuthCmdBuf.pauth_cmd->buffer = NULL;
182 }
183 free(gAuthCmdBuf.pauth_cmd);
184 gAuthCmdBuf.pauth_cmd = NULL;
185 }
186 status = NFCSTATUS_SUCCESS;
187 return status;
188}
189
190/*******************************************************************************
191**
192** Function phNxpExtns_MfcModuleInit
193**
194** Description It Initializes the memroy and global variables related
195** to Mifare module.
196**
197** Reset all the global variables and allocate memory for
198*Mifare module
199**
200** Returns:
201** NFCSTATUS_SUCCESS - if successfully deinitialize
202** NFCSTATUS_FAILED - otherwise
203**
204*******************************************************************************/
205NFCSTATUS phNxpExtns_MfcModuleInit(void) {
206 NFCSTATUS status = NFCSTATUS_FAILED;
207 gphNxpExtns_Context.writecmdFlag = false;
208 gphNxpExtns_Context.RawWriteCallBack = false;
209 gphNxpExtns_Context.CallBackCtxt = NULL;
210 gphNxpExtns_Context.CallBackMifare = NULL;
211 gphNxpExtns_Context.ExtnsConnect = false;
212 gphNxpExtns_Context.ExtnsDeactivate = false;
213 gphNxpExtns_Context.ExtnsCallBack = false;
214
215 NdefMap = (phFriNfc_NdefMap_t*)malloc(sizeof(phFriNfc_NdefMap_t));
216 if (NULL == NdefMap) {
217 goto clean_and_return;
218 }
219 memset(NdefMap, 0, sizeof(phFriNfc_NdefMap_t));
220
221 NdefMap->psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)malloc(
222 sizeof(phLibNfc_sRemoteDevInformation_t));
223 if (NULL == NdefMap->psRemoteDevInfo) {
224 goto clean_and_return;
225 }
226 memset(NdefMap->psRemoteDevInfo, 0, sizeof(phLibNfc_sRemoteDevInformation_t));
227
228 NdefMap->SendRecvBuf = (uint8_t*)malloc((uint32_t)(MAX_BUFF_SIZE * 2));
229 if (NULL == NdefMap->SendRecvBuf) {
230 goto clean_and_return;
231 }
232 memset(NdefMap->SendRecvBuf, 0, (MAX_BUFF_SIZE * 2));
233
234 NdefMap->SendRecvLength = (uint16_t*)malloc(sizeof(uint16_t));
235 if (NULL == NdefMap->SendRecvLength) {
236 goto clean_and_return;
237 }
238 memset(NdefMap->SendRecvLength, 0, sizeof(uint16_t));
239
240 NdefMap->DataCount = (uint16_t*)malloc(sizeof(uint16_t));
241 if (NULL == NdefMap->DataCount) {
242 goto clean_and_return;
243 }
244 memset(NdefMap->DataCount, 0, sizeof(uint16_t));
245
246 NdefMap->pTransceiveInfo =
247 (phNfc_sTransceiveInfo_t*)malloc(sizeof(phNfc_sTransceiveInfo_t));
248 if (NULL == NdefMap->pTransceiveInfo) {
249 goto clean_and_return;
250 }
251 memset(NdefMap->pTransceiveInfo, 0, sizeof(phNfc_sTransceiveInfo_t));
252
253 tNciTranscvInfo.tSendData.pBuff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
254 if (NULL == tNciTranscvInfo.tSendData.pBuff) {
255 goto clean_and_return;
256 }
257 memset(tNciTranscvInfo.tSendData.pBuff, 0, MAX_BUFF_SIZE);
258
259 NdefMap->pTransceiveInfo->sSendData.buffer =
260 (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
261 if (NdefMap->pTransceiveInfo->sSendData.buffer == NULL) {
262 goto clean_and_return;
263 }
264 memset(NdefMap->pTransceiveInfo->sSendData.buffer, 0, MAX_BUFF_SIZE);
265 NdefMap->pTransceiveInfo->sSendData.length = MAX_BUFF_SIZE;
266
267 NdefMap->pTransceiveInfo->sRecvData.buffer = (uint8_t*)malloc(
268 (uint32_t)MAX_BUFF_SIZE); /* size should be same as sRecvData */
269 if (NdefMap->pTransceiveInfo->sRecvData.buffer == NULL) {
270 goto clean_and_return;
271 }
272 memset(NdefMap->pTransceiveInfo->sRecvData.buffer, 0, MAX_BUFF_SIZE);
273 NdefMap->pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
274
275 NdefSmtCrdFmt =
276 (phFriNfc_sNdefSmtCrdFmt_t*)malloc(sizeof(phFriNfc_sNdefSmtCrdFmt_t));
277 if (NdefSmtCrdFmt == NULL) {
278 goto clean_and_return;
279 }
280 memset(NdefSmtCrdFmt, 0, sizeof(phFriNfc_sNdefSmtCrdFmt_t));
281#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
282 pthread_mutex_lock(&SharedDataMutex);
283#endif
284 NdefInfo.psUpperNdefMsg = (phNfc_sData_t*)malloc(sizeof(phNfc_sData_t));
285 if (NULL == NdefInfo.psUpperNdefMsg) {
286 goto clean_and_return;
287 }
288 memset(NdefInfo.psUpperNdefMsg, 0, sizeof(phNfc_sData_t));
289 memset(&gAuthCmdBuf, 0, sizeof(phNci_mfc_auth_cmd_t));
290 gAuthCmdBuf.pauth_cmd = (phNfc_sData_t*)malloc(sizeof(phNfc_sData_t));
291 if (NULL == gAuthCmdBuf.pauth_cmd) {
292 goto clean_and_return;
293 }
294 gAuthCmdBuf.pauth_cmd->buffer = (uint8_t*)malloc((uint32_t)NCI_MAX_DATA_LEN);
295 if (NULL == gAuthCmdBuf.pauth_cmd->buffer) {
296 goto clean_and_return;
297 }
298 status = NFCSTATUS_SUCCESS;
299
300clean_and_return:
301#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
302 pthread_mutex_unlock(&SharedDataMutex);
303#endif
304 if (status != NFCSTATUS_SUCCESS) {
305 LOG(ERROR) << StringPrintf("CRIT: Memory Allocation failed for MFC!");
306 phNxpExtns_MfcModuleDeInit();
307 }
308 return status;
309}
310
311/*******************************************************************************
312**
313** Function Mfc_CheckNdef
314**
315** Description It triggers NDEF detection for Mifare Classic Tag.
316**
317**
318** Returns NFCSTATUS_SUCCESS - if successfully initiated
319** NFCSTATUS_FAILED - otherwise
320**
321*******************************************************************************/
322NFCSTATUS Mfc_CheckNdef(void) {
323 NFCSTATUS status = NFCSTATUS_FAILED;
324
325 EXTNS_SetCallBackFlag(false);
326 /* Set Completion Routine for CheckNdef */
327 NdefMap->CompletionRoutine[0].CompletionRoutine =
328 Mfc_CheckNdef_Completion_Routine;
329
330 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
331 gphNxpExtns_Context.CallBackCtxt = NdefMap;
332 status = phFriNfc_MifareStdMap_H_Reset(NdefMap);
333 if (NFCSTATUS_SUCCESS == status) {
334 status = phFriNfc_MifareStdMap_ChkNdef(NdefMap);
335 if (status == NFCSTATUS_PENDING) {
336 status = NFCSTATUS_SUCCESS;
337 }
338 }
339 if (status != NFCSTATUS_SUCCESS) {
340 status = NFCSTATUS_FAILED;
341 }
342
343 return status;
344}
345
346/*******************************************************************************
347**
348** Function Mfc_CheckNdef_Completion_Routine
349**
350** Description Notify NDEF detection for Mifare Classic Tag to JNI
351**
352** Upon completion of NDEF detection, a
353** NFA_NDEF_DETECT_EVT will be sent, to notify the application
354** of the NDEF attributes (NDEF total memory size, current
355** size, etc.).
356**
357** Returns: void
358**
359*******************************************************************************/
360static void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
361 (void)NdefCtxt;
362 tNFA_CONN_EVT_DATA conn_evt_data;
363
364 conn_evt_data.ndef_detect.status = status;
365 if (NFCSTATUS_SUCCESS == status) {
366 /* NDef Tag Detected */
367 conn_evt_data.ndef_detect.protocol = NFC_PROTOCOL_MIFARE;
368 phFrinfc_MifareClassic_GetContainerSize(
369 NdefMap, (uint32_t*)&(conn_evt_data.ndef_detect.max_size),
370 (uint32_t*)&(conn_evt_data.ndef_detect.cur_size));
371 NdefInfo.NdefLength = conn_evt_data.ndef_detect.max_size;
372 /* update local flags */
373 NdefInfo.is_ndef = 1;
374 NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size;
375 if (PH_NDEFMAP_CARD_STATE_READ_ONLY == NdefMap->CardState) {
376 DLOG_IF(INFO, gLog_level.extns_log_level >= NXPLOG_LOG_DEBUG_LOGLEVEL)
377 << StringPrintf("Mfc_CheckNdef_Completion_Routine : READ_ONLY_CARD");
378 conn_evt_data.ndef_detect.flags = RW_NDEF_FL_READ_ONLY;
379 } else {
380 conn_evt_data.ndef_detect.flags =
381 RW_NDEF_FL_SUPPORTED | RW_NDEF_FL_FORMATED;
382 }
383 } else {
384 /* NDEF Detection failed for other reasons */
385 conn_evt_data.ndef_detect.cur_size = 0;
386 conn_evt_data.ndef_detect.max_size = 0;
387 conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN;
388
389 /* update local flags */
390 NdefInfo.is_ndef = 0;
391 NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size;
392 }
393 (*gphNxpExtns_Context.p_conn_cback)(NFA_NDEF_DETECT_EVT, &conn_evt_data);
394
395 return;
396}
397/*******************************************************************************
398**
399** Function Mfc_ReadNdef_Completion_Routine
400**
401** Description Notify NDEF read completion for Mifare Classic Tag to JNI
402**
403** Upon completion of NDEF read, a
404** NFA_READ_CPLT_EVT will be sent, to notify the application
405** with the NDEF data and status
406**
407** Returns: void
408**
409*******************************************************************************/
410static void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
411 (void)NdefCtxt;
412 tNFA_CONN_EVT_DATA conn_evt_data;
413 tNFA_NDEF_EVT_DATA p_data;
414
415 conn_evt_data.status = status;
416#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
417 pthread_mutex_lock(&SharedDataMutex);
418#endif
419 if (NFCSTATUS_SUCCESS == status) {
420 p_data.ndef_data.len = NdefInfo.psUpperNdefMsg->length;
421 p_data.ndef_data.p_data = NdefInfo.psUpperNdefMsg->buffer;
422 (*gphNxpExtns_Context.p_ndef_cback)(NFA_NDEF_DATA_EVT, &p_data);
423 } else {
424 }
425
426 (*gphNxpExtns_Context.p_conn_cback)(NFA_READ_CPLT_EVT, &conn_evt_data);
427
428 if (NdefInfo.psUpperNdefMsg->buffer != NULL) {
429 free(NdefInfo.psUpperNdefMsg->buffer);
430 NdefInfo.psUpperNdefMsg->buffer = NULL;
431 }
432#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
433 pthread_mutex_unlock(&SharedDataMutex);
434#endif
435 return;
436}
437
438/*******************************************************************************
439**
440** Function Mfc_WriteNdef_Completion_Routine
441**
442** Description Notify NDEF write completion for Mifare Classic Tag to JNI
443**
444** Upon completion of NDEF write, a
445** NFA_WRITE_CPLT_EVT will be sent along with status
446**
447** Returns: void
448**
449*******************************************************************************/
450static void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
451 (void)NdefCtxt;
452 tNFA_CONN_EVT_DATA conn_evt_data;
453
nxf24178f1dc4932019-10-07 14:40:45 +0530454#if (NXP_EXTNS == TRUE)
455 NdefInfo.psUpperNdefMsg->buffer = nullptr;
456 NdefInfo.psUpperNdefMsg->length = 0x00;
457#endif
nxf24591dc0bc2c2018-02-21 17:33:08 +0530458 conn_evt_data.status = status;
459 (*gphNxpExtns_Context.p_conn_cback)(NFA_WRITE_CPLT_EVT, &conn_evt_data);
460
461 return;
462}
463
464/*******************************************************************************
465**
466** Function Mfc_FormatNdef_Completion_Routine
467**
468** Description Notify NDEF format completion for Mifare Classic Tag to JNI
469**
470** Upon completion of NDEF format, a
471** NFA_FORMAT_CPLT_EVT will be sent along with status
472**
473** Returns: void
474**
475*******************************************************************************/
476static void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt,
477 NFCSTATUS status) {
478 (void)NdefCtxt;
479 tNFA_CONN_EVT_DATA conn_evt_data;
480
481 conn_evt_data.status = status;
482 (*gphNxpExtns_Context.p_conn_cback)(NFA_FORMAT_CPLT_EVT, &conn_evt_data);
483
484 return;
485}
486
487/*******************************************************************************
488**
489** Function phFriNfc_ValidateParams
490**
491** Description This function is a common function which validates NdefRd
492** and NdefWr parameters.
493**
494** Returns NFCSTATUS_SUCCESS - All the params are valid
495** NFCSTATUS_FAILED - otherwise
496**
497*******************************************************************************/
498static NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData,
499 uint32_t* PacketDataLength,
500 uint8_t Offset,
501 phFriNfc_NdefMap_t* pNdefMap,
502 uint8_t bNdefReq) {
503 if ((pNdefMap == NULL) || (PacketData == NULL) ||
504 (PacketDataLength == NULL)) {
505 return NFCSTATUS_FAILED;
506 }
507
508 if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) {
509 return NFCSTATUS_FAILED;
510 }
511
512 if (bNdefReq == PH_FRINFC_NDEF_READ_REQ) {
513 if ((Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) &&
514 (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) {
515 return NFCSTATUS_FAILED;
516 }
517 if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED) {
518 pNdefMap->NumOfBytesRead = PacketDataLength;
519 *pNdefMap->NumOfBytesRead = 0;
520 return NFCSTATUS_EOF_NDEF_CONTAINER_REACHED;
521 }
522 if ((pNdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) &&
523 (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) {
524 return NFCSTATUS_FAILED; /* return INVALID_DEVICE_REQUEST */
525 }
526 if (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) {
527 pNdefMap->ApduBuffIndex = 0;
528 *pNdefMap->DataCount = 0;
529 } else if ((pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_BEGIN) ||
530 (pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_CUR)) {
531 } else {
532 return NFCSTATUS_FAILED;
533 }
534 } else if (bNdefReq == PH_FRINFC_NDEF_WRITE_REQ) {
535 if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY) {
536 pNdefMap->WrNdefPacketLength = PacketDataLength;
537 *pNdefMap->WrNdefPacketLength = 0x00;
538 return NFCSTATUS_NOT_ALLOWED;
539 }
540 }
541
542 return NFCSTATUS_SUCCESS;
543}
544
545/*******************************************************************************
546**
547** Function Mfc_SetRdOnly_Completion_Routine
548**
549** Description Notify NDEF read only completion for Mifare Classic Tag to
550*JNI
551**
552** Upon completion of NDEF format, a
553** NFA_SET_TAG_RO_EVT will be sent along with status
554**
555** Returns: void
556**
557*******************************************************************************/
558static void Mfc_SetRdOnly_Completion_Routine(void* NdefCtxt, NFCSTATUS status) {
559 (void)NdefCtxt;
560 tNFA_CONN_EVT_DATA conn_evt_data;
561 LOG(ERROR) << StringPrintf("%s status = 0x%x", __func__, status);
562 conn_evt_data.status = status;
563 (*gphNxpExtns_Context.p_conn_cback)(NFA_SET_TAG_RO_EVT, &conn_evt_data);
564
565 return;
566}
567
568/*******************************************************************************
569**
570** Function Mfc_SetReadOnly
571**
572**
573** Description: It triggers ConvertToReadOnly for Mifare Classic Tag.
574**
575** Returns:
576** NFCSTATUS_SUCCESS if successfully initiated
577** NFCSTATUS_FAILED otherwise
578**
579*******************************************************************************/
580NFCSTATUS Mfc_SetReadOnly(uint8_t* secrtkey, uint8_t len) {
581 DLOG_IF(INFO, gLog_level.extns_log_level >= NXPLOG_LOG_DEBUG_LOGLEVEL)
582 << StringPrintf("%s Entering ", __func__);
583 NFCSTATUS status = NFCSTATUS_FAILED;
584 uint8_t mif_secrete_key[6] = {0};
585 uint8_t id = 0;
586 EXTNS_SetCallBackFlag(false);
587 memcpy(mif_secrete_key, secrtkey, len);
588 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
589 gphNxpExtns_Context.CallBackCtxt = NdefMap;
590 for (id = 0; id < len; id++) {
591 DLOG_IF(INFO, nfc_debug_enabled)
592 << StringPrintf("secrtkey[%d] = 0x%x", id, secrtkey[id]);
593 DLOG_IF(INFO, nfc_debug_enabled)
594 << StringPrintf("mif_secrete_key[%d] = 0x%x", id, mif_secrete_key[id]);
595 }
596 /* Set Completion Routine for ReadNdef */
597 NdefMap->CompletionRoutine[0].CompletionRoutine =
598 Mfc_SetRdOnly_Completion_Routine;
599 if (NdefInfo.is_ndef == 0) {
600 status = NFCSTATUS_NON_NDEF_COMPLIANT;
601 goto Mfc_SetRdOnly;
602 } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) {
603#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
604 pthread_mutex_lock(&SharedDataMutex);
605#endif
606 NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
607#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
608 pthread_mutex_unlock(&SharedDataMutex);
609#endif
610 status = NFCSTATUS_SUCCESS;
611 goto Mfc_SetRdOnly;
612 } else {
613 status = phFriNfc_MifareStdMap_ConvertToReadOnly(NdefMap, mif_secrete_key);
614 }
615 if (NFCSTATUS_PENDING == status) {
616 status = NFCSTATUS_SUCCESS;
617 }
618
619Mfc_SetRdOnly:
620 return status;
621}
622
623/*******************************************************************************
624**
625** Function Mfc_ReadNdef
626**
627** Description It triggers receiving of the NDEF message from Mifare
628*Classic Tag.
629**
630**
631** Returns:
632** NFCSTATUS_SUCCESS - if successfully initiated
633** NFCSTATUS_FAILED - otherwise
634**
635*******************************************************************************/
636NFCSTATUS Mfc_ReadNdef(void) {
637 NFCSTATUS status = NFCSTATUS_FAILED;
638 uint8_t* PacketData = NULL;
639 uint32_t* PacketDataLength = NULL;
640 phLibNfc_Ndef_EOffset_t Offset;
641
642 EXTNS_SetCallBackFlag(false);
643
644 Offset = phLibNfc_Ndef_EBegin;
645
646 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
647 gphNxpExtns_Context.CallBackCtxt = NdefMap;
648#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
649 pthread_mutex_lock(&SharedDataMutex);
650#endif
651 if (NdefInfo.is_ndef == 0) {
652 status = NFCSTATUS_NON_NDEF_COMPLIANT;
653 goto Mfc_RdNdefEnd;
654 } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) {
655 NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
656 status = NFCSTATUS_SUCCESS;
657 goto Mfc_RdNdefEnd;
658 } else {
659 NdefInfo.psUpperNdefMsg->buffer = (uint8_t*)malloc(NdefInfo.NdefActualSize);
660 if (NULL == NdefInfo.psUpperNdefMsg->buffer) {
661 goto Mfc_RdNdefEnd;
662 }
663 NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize;
664
665 /* Set Completion Routine for ReadNdef */
666 NdefMap->CompletionRoutine[1].CompletionRoutine =
667 Mfc_ReadNdef_Completion_Routine;
nxf42217f94fbc32019-03-26 11:32:53 +0530668 NdefInfo.NdefContinueRead = (uint8_t)(PH_FRINFC_NDEFMAP_SEEK_BEGIN);
nxf24591dc0bc2c2018-02-21 17:33:08 +0530669 }
670
671 PacketData = NdefInfo.psUpperNdefMsg->buffer;
672 PacketDataLength = (uint32_t*)&(NdefInfo.psUpperNdefMsg->length);
673 NdefMap->bCurrReadMode = Offset;
674 status = phFriNfc_ValidateParams(PacketData, PacketDataLength, Offset,
675 NdefMap, PH_FRINFC_NDEF_READ_REQ);
676 if (status != NFCSTATUS_SUCCESS) {
677 goto Mfc_RdNdefEnd;
678 }
679
680 status = phFriNfc_MifareStdMap_RdNdef(NdefMap, PacketData, PacketDataLength,
681 Offset);
682
683 if (NFCSTATUS_INSUFFICIENT_STORAGE == status) {
684 NdefInfo.psUpperNdefMsg->length = 0x00;
685 status = NFCSTATUS_SUCCESS;
686 }
687
688 if (NFCSTATUS_PENDING == status) {
689 status = NFCSTATUS_SUCCESS;
690 }
691
692Mfc_RdNdefEnd:
693 if (status != NFCSTATUS_SUCCESS) {
694 if (NULL != NdefInfo.psUpperNdefMsg->buffer) {
695 free(NdefInfo.psUpperNdefMsg->buffer);
696 NdefInfo.psUpperNdefMsg->buffer = NULL;
697 }
698 status = NFCSTATUS_FAILED;
699 }
700#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
701 pthread_mutex_unlock(&SharedDataMutex);
702#endif
703 return status;
704}
705/*******************************************************************************
706**
707** Function Mfc_PresenceCheck
708**
709** Description It triggers receiving of the NDEF message from Mifare
710*Classic Tag.
711**
712**
713** Returns:
714** NFCSTATUS_SUCCESS - if successfully initiated
715** NFCSTATUS_FAILED - otherwise
716**
717*******************************************************************************/
718NFCSTATUS Mfc_PresenceCheck(void) {
719 NFCSTATUS status = NFCSTATUS_SUCCESS;
720
721 if (gAuthCmdBuf.auth_status == true) {
722 EXTNS_SetCallBackFlag(false);
723 status = nativeNfcExtns_doTransceive(gAuthCmdBuf.pauth_cmd->buffer,
724 gAuthCmdBuf.pauth_cmd->length);
725 if (status != NFCSTATUS_PENDING) {
726 gAuthCmdBuf.auth_sent = false;
727 status = NFCSTATUS_FAILED;
728 } else {
729 gAuthCmdBuf.auth_sent = true;
730 status = NFCSTATUS_SUCCESS;
731 }
732 } else {
733 status = NFCSTATUS_NOT_ALLOWED;
734 }
735 DLOG_IF(INFO, gLog_level.extns_log_level >= NXPLOG_LOG_DEBUG_LOGLEVEL)
736 << StringPrintf("%s status = 0x%x", __func__, status);
737 return status;
738}
739/*******************************************************************************
740**
741** Function Mfc_WriteNdef
742**
743** Description It triggers the NDEF data write to Mifare Classic Tag.
744**
745**
746** Returns:
747** NFCSTATUS_SUCCESS - if successfully initiated
748** NFCSTATUS_FAILED - otherwise
749**
750*******************************************************************************/
751NFCSTATUS Mfc_WriteNdef(uint8_t* p_data, uint32_t len) {
752 NFCSTATUS status = NFCSTATUS_SUCCESS;
753 uint8_t* PacketData = NULL;
754 uint32_t* PacketDataLength = NULL;
755
756 if (p_data == NULL || len == 0) {
757 LOG(ERROR) << StringPrintf("MFC Error: Invalid Parameters to Ndef Write");
758 status = NFCSTATUS_FAILED;
759 goto Mfc_WrNdefEnd;
760 }
761
762 EXTNS_SetCallBackFlag(false);
763 gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process;
764 gphNxpExtns_Context.CallBackCtxt = NdefMap;
765#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
766 pthread_mutex_lock(&SharedDataMutex);
767#endif
768 if (NdefInfo.is_ndef == PH_LIBNFC_INTERNAL_CHK_NDEF_NOT_DONE) {
769 status = NFCSTATUS_REJECTED;
770 goto Mfc_WrNdefEnd;
771 } else if (NdefInfo.is_ndef == 0) {
772 status = NFCSTATUS_NON_NDEF_COMPLIANT;
773 goto Mfc_WrNdefEnd;
774 } else if (len > NdefInfo.NdefLength) {
775 status = NFCSTATUS_NOT_ENOUGH_MEMORY;
776 goto Mfc_WrNdefEnd;
777 } else {
778 NdefInfo.psUpperNdefMsg->buffer = p_data;
779 NdefInfo.psUpperNdefMsg->length = len;
780
781 NdefInfo.AppWrLength = len;
782 NdefMap->CompletionRoutine[2].CompletionRoutine =
783 Mfc_WriteNdef_Completion_Routine;
784 if (0 == len) {
785 /* TODO: Erase the Tag */
786 } else {
787 NdefMap->ApduBuffIndex = 0x00;
788 *NdefMap->DataCount = 0x00;
789 PacketData = NdefInfo.psUpperNdefMsg->buffer;
790 PacketDataLength = &(NdefInfo.dwWrLength);
791 NdefMap->WrNdefPacketLength = PacketDataLength;
792 NdefInfo.dwWrLength = len;
793
794 status = phFriNfc_ValidateParams(PacketData, PacketDataLength, 0, NdefMap,
795 PH_FRINFC_NDEF_WRITE_REQ);
796 if (status != NFCSTATUS_SUCCESS) {
797 goto Mfc_WrNdefEnd;
798 }
799
800 status = phFriNfc_MifareStdMap_WrNdef(
801 NdefMap, PacketData, PacketDataLength, PH_FRINFC_NDEFMAP_SEEK_BEGIN);
802
803 if (status == NFCSTATUS_PENDING) {
804 status = NFCSTATUS_SUCCESS;
805 }
806 }
807 }
808
809Mfc_WrNdefEnd:
810#if (NFC_NXP_NOT_OPEN_INCLUDED == TRUE)
811 pthread_mutex_unlock(&SharedDataMutex);
812#endif
813 if (status != NFCSTATUS_SUCCESS) {
814 status = NFCSTATUS_FAILED;
815 }
816 return status;
817}
818/*******************************************************************************
819**
820** Function phFriNfc_NdefSmtCrd_Reset__
821**
822** Description This function Resets the component instance to the initial
823** state and initializes the internal variables.
824**
825** Returns NFCSTATUS_SUCCESS
826**
827*******************************************************************************/
828static NFCSTATUS phFriNfc_NdefSmtCrd_Reset__(
829 phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, uint8_t* SendRecvBuffer,
830 uint16_t* SendRecvBuffLen) {
831 // NFCSTATUS status = NFCSTATUS_FAILED; /*commented to
832 // eliminate unused variable warning*/
833 uint8_t index;
834
835 /* Initialize the state to Init */
836 NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT;
837
838 for (index = 0; index < PH_FRINFC_SMTCRDFMT_CR; index++) {
839 /* Initialize the NdefMap Completion Routine to Null */
840 NdefSmtCrdFmt->CompletionRoutine[index].CompletionRoutine = NULL;
841 /* Initialize the NdefMap Completion Routine context to Null */
842 NdefSmtCrdFmt->CompletionRoutine[index].Context = NULL;
843 }
844
845 /* Trx Buffer registered */
846 NdefSmtCrdFmt->SendRecvBuf = SendRecvBuffer;
847
848 /* Trx Buffer Size */
849 NdefSmtCrdFmt->SendRecvLength = SendRecvBuffLen;
850
851 /* Register Transfer Buffer Length */
852 NdefSmtCrdFmt->SendLength = 0;
853
854 /* Initialize the Format status flag*/
855 NdefSmtCrdFmt->FmtProcStatus = 0;
856
857 /* Reset the Card Type */
858 NdefSmtCrdFmt->CardType = 0;
859
860 /* Reset MapCompletion Info*/
861 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = NULL;
862 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NULL;
863
864 phFriNfc_MfStd_Reset(NdefSmtCrdFmt);
865
866 return NFCSTATUS_SUCCESS;
867}
868
869/*******************************************************************************
870**
871** Function Mfc_FormatNdef
872**
873** Description It triggers the NDEF format of Mifare Classic Tag.
874**
875**
876** Returns:
877** NFCSTATUS_SUCCESS - if successfully initiated
878** NFCSTATUS_FAILED - otherwise
879**
880*******************************************************************************/
881NFCSTATUS Mfc_FormatNdef(uint8_t* secretkey, uint8_t len) {
882 NFCSTATUS status = NFCSTATUS_FAILED;
883 uint8_t mif_std_key[6] = {0};
884 // static uint8_t Index;
885 // /*commented to eliminate unused variable warning*/
886 uint8_t sak = 0;
887
888 EXTNS_SetCallBackFlag(false);
889
890 memcpy(mif_std_key, secretkey, len);
891 memcpy(current_key, secretkey, len);
892
893 if (NULL == NdefSmtCrdFmt || NULL == NdefMap ||
894 NULL == NdefMap->SendRecvBuf) {
895 goto Mfc_FormatEnd;
896 }
897 NdefSmtCrdFmt->pTransceiveInfo = NdefMap->pTransceiveInfo;
898
899 gphNxpExtns_Context.CallBackMifare = phFriNfc_MfStd_Process;
900 gphNxpExtns_Context.CallBackCtxt = NdefSmtCrdFmt;
901
902 NdefInfo.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
903 phFriNfc_NdefSmtCrd_Reset__(NdefSmtCrdFmt, NdefMap->SendRecvBuf,
904 &(NdefInfo.NdefSendRecvLen));
905
906 /* Register Callbacks */
907 NdefSmtCrdFmt->CompletionRoutine[0].CompletionRoutine =
908 Mfc_FormatNdef_Completion_Routine;
909 NdefSmtCrdFmt->CompletionRoutine[1].CompletionRoutine =
910 Mfc_FormatNdef_Completion_Routine;
911 NdefSmtCrdFmt->psRemoteDevInfo = NdefMap->psRemoteDevInfo;
912 sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
913
914 if ((0x08 == (sak & 0x18)) || (0x18 == (sak & 0x18)) || (0x01 == sak)) {
915 NdefSmtCrdFmt->CardType = (uint8_t)(
916 ((sak & 0x18) == 0x08)
917 ? PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD
918 : (((sak & 0x19) == 0x19) ? PH_FRINFC_SMTCRDFMT_MFSTD_2K_CRD
919 : PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD));
920 status = phFriNfc_MfStd_Format(NdefSmtCrdFmt, mif_std_key);
921 }
922
923 if (NFCSTATUS_PENDING == status) {
924 status = NFCSTATUS_SUCCESS;
925 }
926
927Mfc_FormatEnd:
928 if (status != NFCSTATUS_SUCCESS) {
929 status = NFCSTATUS_FAILED;
930 }
931
932 return status;
933}
934
935/*******************************************************************************
936**
937** Function phNxNciExtns_MifareStd_Reconnect
938**
939** Description This function sends the deactivate command to NFCC for
940*Mifare
941**
942**
943** Returns:
944** NFCSTATUS_PENDING - if successfully initiated
945** NFCSTATUS_FAILED - otherwise
946**
947*******************************************************************************/
948NFCSTATUS phNxNciExtns_MifareStd_Reconnect(void) {
949 tNFA_STATUS status;
950
951 EXTNS_SetDeactivateFlag(true);
952 if (NFA_STATUS_OK !=
953 (status = NFA_Deactivate(true))) /* deactivate to sleep state */
954 {
955 LOG(ERROR) << StringPrintf("%s: deactivate failed, status = %d", __func__,
956 status);
957 return NFCSTATUS_FAILED;
958 }
959
960 return NFCSTATUS_PENDING;
961}
962
963/*******************************************************************************
964**
965** Function Mfc_DeactivateCbackSelect
966**
967** Description This function select the Mifare tag
968**
969**
970** Returns: void
971**
972*******************************************************************************/
973void Mfc_DeactivateCbackSelect(void) {
974 tNFA_STATUS status;
975
976 EXTNS_SetConnectFlag(true);
977 if (NFA_STATUS_OK !=
978 (status = NFA_Select(0x01, phNciNfc_e_RfProtocolsMifCProtocol,
979 phNciNfc_e_RfInterfacesTagCmd_RF))) {
980 LOG(ERROR) << StringPrintf("%s: NFA_Select failed, status = %d", __func__,
981 status);
982 }
983
984 return;
985}
986
987/*******************************************************************************
988**
989** Function Mfc_ActivateCback
990**
991** Description This function invoke the callback when receive the response
992**
993**
994** Returns: void
995**
996**
997*******************************************************************************/
998void Mfc_ActivateCback(void) {
999 gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
1000 NFCSTATUS_SUCCESS);
1001 return;
1002}
1003
1004/*******************************************************************************
1005**
1006** Function Mfc_Transceive
1007**
1008** Description Sends raw frame to Mifare Classic Tag.
1009**
1010** Returns NFCSTATUS_SUCCESS - if successfully initiated
1011** NFCSTATUS_FAILED - otherwise
1012**
1013*******************************************************************************/
1014NFCSTATUS Mfc_Transceive(uint8_t* p_data, uint32_t len) {
1015 NFCSTATUS status = NFCSTATUS_FAILED;
1016 uint8_t i = 0x00;
1017
Lijo Abrahama051d252019-09-06 11:17:56 +05301018 if (len == 0) {
1019 android_errorWriteLog(0x534e4554, "132082342");
1020 return status;
1021 }
1022
nxf24591dc0bc2c2018-02-21 17:33:08 +05301023 gphNxpExtns_Context.RawWriteCallBack = false;
1024 gphNxpExtns_Context.CallBackMifare = NULL;
1025 gphNxpExtns_Context.CallBackCtxt = NdefMap;
1026
1027 EXTNS_SetCallBackFlag(true);
1028 if (p_data[0] == 0x60 || p_data[0] == 0x61) {
Lijo Abraham002147e2019-09-06 11:44:36 +05301029 if (len < 12) {
1030 android_errorWriteLog(0x534e4554, "125900276");
1031 return status;
1032 }
nxf24591dc0bc2c2018-02-21 17:33:08 +05301033 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1034
1035 NdefMap->SendRecvBuf[i++] = p_data[1];
1036
1037 NdefMap->SendRecvBuf[i++] = p_data[6]; /* TODO, handle 7 byte UID */
1038 NdefMap->SendRecvBuf[i++] = p_data[7];
1039 NdefMap->SendRecvBuf[i++] = p_data[8];
1040 NdefMap->SendRecvBuf[i++] = p_data[9];
1041 NdefMap->SendRecvBuf[i++] = p_data[10];
1042 NdefMap->SendRecvBuf[i++] = p_data[11];
1043
1044 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1045 NdefMap->SendRecvBuf, NdefMap->SendLength,
1046 NdefMap->SendRecvLength);
1047 } else if (p_data[0] == 0xA0) {
1048 EXTNS_SetCallBackFlag(false);
1049 NdefMap->Cmd.MfCmd = phNfc_eMifareWrite16;
1050 gphNxpExtns_Context.RawWriteCallBack = true;
1051
1052 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1053 NdefMap->SendLength = len - 1;
1054 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1055 NdefMap->SendRecvBuf, NdefMap->SendLength,
1056 NdefMap->SendRecvLength);
1057 } else if ((p_data[0] == phNfc_eMifareInc) ||
1058 (p_data[0] == phNfc_eMifareDec)) {
1059 EXTNS_SetCallBackFlag(false);
1060 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1061 gphNxpExtns_Context.RawWriteCallBack = true;
1062
1063 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1064 NdefMap->SendLength = len - 1;
1065 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1066 NdefMap->SendRecvBuf, NdefMap->SendLength,
1067 NdefMap->SendRecvLength);
1068 } else if (((p_data[0] == phNfc_eMifareTransfer) ||
1069 (p_data[0] == phNfc_eMifareRestore)) &&
1070 (len == 2)) {
1071 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1072 if (p_data[0] == phNfc_eMifareRestore) {
1073 EXTNS_SetCallBackFlag(false);
1074 gphNxpExtns_Context.RawWriteCallBack = true;
1075 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1076 NdefMap->SendLength = len - 1;
1077 } else {
1078 memcpy(NdefMap->SendRecvBuf, p_data, len);
1079 NdefMap->SendLength = len;
1080 }
1081 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1082 NdefMap->SendRecvBuf, NdefMap->SendLength,
1083 NdefMap->SendRecvLength);
1084
1085 } else {
1086 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)phNfc_eMifareRaw;
1087
1088 memcpy(NdefMap->SendRecvBuf, p_data, len);
1089 NdefMap->SendLength = len;
1090 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1091 NdefMap->SendRecvBuf, NdefMap->SendLength,
1092 NdefMap->SendRecvLength);
1093 }
1094 if (NFCSTATUS_PENDING == status) {
1095 status = NFCSTATUS_SUCCESS;
1096 } else {
1097 LOG(ERROR) << StringPrintf("ERROR: Mfc_Transceive = 0x%x", status);
1098 }
1099
1100 return status;
1101}
1102
1103/*******************************************************************************
1104**
1105** Function nativeNfcExtns_doTransceive
1106**
1107** Description Sends raw frame to BCM stack.
1108**
1109** Returns NFCSTATUS_PENDING - if successfully initiated
1110** NFCSTATUS_FAILED - otherwise
1111**
1112*******************************************************************************/
1113static NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz) {
1114 NFCSTATUS wStatus = NFCSTATUS_PENDING;
1115 tNFA_STATUS status =
1116 NFA_SendRawFrame(buff, buffSz, NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY);
1117
1118 if (status != NFA_STATUS_OK) {
1119 LOG(ERROR) << StringPrintf("%s: fail send; error=%d", __func__, status);
1120 wStatus = NFCSTATUS_FAILED;
1121 }
1122
1123 return wStatus;
1124}
1125
1126/*******************************************************************************
1127**
1128** Function phNciNfc_RecvMfResp
1129**
1130** Description This function shall be invoked as part of ReaderMgmt data
1131** exchange sequence handler on receiving response/data from
1132*NFCC
1133**
1134** Returns NFCSTATUS_SUCCESS - Data Reception is successful
1135** NFCSTATUS_FAILED - Data Reception failed
1136**
1137*******************************************************************************/
1138static NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo,
1139 NFCSTATUS wStatus) {
1140 NFCSTATUS status = NFCSTATUS_SUCCESS;
1141 uint16_t wPldDataSize = 0;
1142 phNciNfc_ExtnRespId_t RecvdExtnRspId = phNciNfc_e_InvalidRsp;
1143 if (NULL == RspBuffInfo) {
1144 status = NFCSTATUS_FAILED;
1145 } else {
Alisher Alikhodjaev0d7103d2022-03-18 17:13:05 -07001146 if (((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) >
1147 RspBuffInfo->wLen) ||
1148 (PH_NCINFC_STATUS_OK != wStatus) || (NULL == (RspBuffInfo->pBuff))) {
nxf24591dc0bc2c2018-02-21 17:33:08 +05301149 status = NFCSTATUS_FAILED;
1150 } else {
1151 RecvdExtnRspId = (phNciNfc_ExtnRespId_t)RspBuffInfo->pBuff[0];
1152
1153 switch (RecvdExtnRspId) {
1154 case phNciNfc_e_MfXchgDataRsp: {
1155 NFCSTATUS writeResponse = NFCSTATUS_SUCCESS;
1156 /* check the status byte */
1157 if (NFC_GetNCIVersion() == NCI_VERSION_2_0 &&
1158 (NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_TLV ||
1159 NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WRITE ||
1160 NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN ||
1161 NdefMap->State == PH_FRINFC_NDEFMAP_STATE_INIT)) {
1162 uint8_t rspAck = RspBuffInfo->pBuff[RspBuffInfo->wLen - 2];
1163 uint8_t rspAckMask = ((RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]) &
1164 MAX_NUM_VALID_BITS_FOR_ACK);
1165 NCI_CALCULATE_ACK(rspAck, rspAckMask);
1166 writeResponse =
1167 (rspAck == T2T_RSP_ACK) ? NFCSTATUS_SUCCESS : NFC_STATUS_FAILED;
1168 } else {
1169 writeResponse = RspBuffInfo->pBuff[RspBuffInfo->wLen - 1];
1170 }
1171 if (PH_NCINFC_STATUS_OK == writeResponse) {
1172 status = NFCSTATUS_SUCCESS;
1173 uint16_t wRecvDataSz = 0;
1174
1175 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1176 wPldDataSize = ((RspBuffInfo->wLen) -
1177 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1178 wRecvDataSz = NCI_MAX_DATA_LEN;
1179
1180 /* wPldDataSize = wPldDataSize-1; ==> ignoring the last status byte
1181 * appended with data */
1182 if ((wPldDataSize) <= wRecvDataSz) {
1183 /* Extract the data part from pBuff[2] & fill it to be sent to
1184 * upper layer */
1185 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[1]),
1186 (wPldDataSize));
1187 /* update the number of bytes received from lower layer,excluding
1188 * the status byte */
1189 *(NdefMap->SendRecvLength) = wPldDataSize;
1190 } else {
1191 // TODO:- Map some status for remaining extra data received to be
1192 // sent back to caller??
1193 status = NFCSTATUS_FAILED;
1194 }
1195 } else {
1196 status = NFCSTATUS_FAILED;
1197 }
1198 } break;
1199
1200 case phNciNfc_e_MfcAuthRsp: {
1201 /* check the status byte */
1202 if (PH_NCINFC_STATUS_OK == RspBuffInfo->pBuff[1]) {
1203 if (gAuthCmdBuf.auth_sent == true) {
1204 MfcPresenceCheckResult(NFCSTATUS_SUCCESS);
1205 return NFCSTATUS_SUCCESS;
1206 }
1207 gAuthCmdBuf.auth_status = true;
1208 status = NFCSTATUS_SUCCESS;
1209
1210 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1211 wPldDataSize = ((RspBuffInfo->wLen) -
1212 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1213
1214 /* Extract the data part from pBuff[2] & fill it to be sent to upper
1215 * layer */
1216 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[2]),
1217 wPldDataSize);
1218 /* update the number of bytes received from lower layer,excluding
1219 * the status byte */
1220 *(NdefMap->SendRecvLength) = wPldDataSize;
1221 } else {
1222 if (gAuthCmdBuf.auth_sent == true) {
1223 gAuthCmdBuf.auth_status = false;
1224 MfcPresenceCheckResult(NFCSTATUS_FAILED);
1225 return NFCSTATUS_SUCCESS;
1226 } else {
1227 /* Reset the stored auth command buffer */
1228 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1229 gAuthCmdBuf.pauth_cmd->length = 0;
1230 gAuthCmdBuf.auth_status = false;
1231 }
1232 status = NFCSTATUS_FAILED;
1233 }
1234 } break;
1235
1236 default: { status = NFCSTATUS_FAILED; } break;
1237 }
1238 }
1239 }
1240
1241 return status;
1242}
1243
1244/*******************************************************************************
1245**
1246** Function phLibNfc_SendWrt16CmdPayload
1247**
1248** Description This function map the raw write cmd
1249**
1250** Returns NFCSTATUS_SUCCESS - Command framing done
1251** NFCSTATUS_INVALID_PARAMETER - Otherwise
1252**
1253*******************************************************************************/
1254static NFCSTATUS phLibNfc_SendWrt16CmdPayload(
1255 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1256 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1257 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1258
1259 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1260 (0 != (pTransceiveInfo->sSendData.length))) {
1261 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1262 (pTransceiveInfo->sSendData.length));
1263 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1264 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1265 } else {
1266 wStatus = NFCSTATUS_INVALID_PARAMETER;
1267 }
1268
1269 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1270 EXTNS_SetCallBackFlag(true);
1271 gphNxpExtns_Context.RawWriteCallBack = false;
1272 }
1273
1274 return wStatus;
1275}
1276
1277/*******************************************************************************
1278**
1279** Function phLibNfc_SendIncDecCmdPayload
1280**
1281** Description This function prepares the Increment/Decrement Value to be
1282** sent. This is called after sending the Increment/Decrement
1283** command is already sent and successfull
1284**
1285** Returns NFCSTATUS_SUCCESS - Payload framing done
1286** NFCSTATUS_INVALID_PARAMETER - Otherwise
1287**
1288*******************************************************************************/
1289static NFCSTATUS phLibNfc_SendIncDecCmdPayload(
1290 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1291 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1292 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1293
1294 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1295 (0 != (pTransceiveInfo->sSendData.length))) {
1296 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1297 (pTransceiveInfo->sSendData.length));
1298 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1299 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1300 } else {
1301 wStatus = NFCSTATUS_INVALID_PARAMETER;
1302 }
1303
1304 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1305 EXTNS_SetCallBackFlag(true);
1306 gphNxpExtns_Context.RawWriteCallBack = false;
1307 }
1308
1309 return wStatus;
1310}
1311
1312/*******************************************************************************
1313**
1314** Function Mfc_RecvPacket
1315**
1316** Description Decodes Mifare Classic Tag Response
1317** This is called from NFA_SendRaw Callback
1318**
1319** Returns:
1320** NFCSTATUS_SUCCESS - if successfully initiated
1321** NFCSTATUS_FAILED - otherwise
1322**
1323*******************************************************************************/
1324NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz) {
1325 NFCSTATUS status = NFCSTATUS_SUCCESS;
1326 phNciNfc_Buff_t RspBuff;
1327 uint8_t* pcmd_buff;
1328 uint16_t buffSize;
1329
1330 RspBuff.pBuff = buff;
1331 RspBuff.wLen = buffSz;
1332 status = phNciNfc_RecvMfResp(&RspBuff, status);
1333 if (true == gAuthCmdBuf.auth_sent) {
1334 DLOG_IF(INFO, nfc_debug_enabled)
1335 << StringPrintf("%s Mfc Check Presence in progress", __func__);
1336 gAuthCmdBuf.auth_sent = false;
1337 return status;
1338 }
1339 if (true == gphNxpExtns_Context.writecmdFlag &&
1340 (NFCSTATUS_SUCCESS == status)) {
1341 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1342 if (NULL == pcmd_buff) {
1343 return NFCSTATUS_FAILED;
1344 }
1345 buffSize = MAX_BUFF_SIZE;
1346 gphNxpExtns_Context.writecmdFlag = false;
1347 phLibNfc_SendWrt16CmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1348 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1349 if (NFCSTATUS_PENDING != status) {
1350 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1351 } else {
1352 status = NFCSTATUS_SUCCESS;
1353 }
1354 if (pcmd_buff != NULL) {
1355 free(pcmd_buff);
1356 pcmd_buff = NULL;
1357 }
1358 } else if (true == gphNxpExtns_Context.incrdecflag &&
1359 (NFCSTATUS_SUCCESS == status)) {
1360 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1361 if (NULL == pcmd_buff) {
1362 return NFCSTATUS_FAILED;
1363 }
1364 buffSize = MAX_BUFF_SIZE;
1365 gphNxpExtns_Context.incrdecflag = false;
1366 phLibNfc_SendIncDecCmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1367 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1368 if (NFCSTATUS_PENDING != status) {
1369 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1370 } else {
1371 status = NFCSTATUS_SUCCESS;
1372 }
1373 gphNxpExtns_Context.incrdecstatusflag = true;
1374 if (pcmd_buff != NULL) {
1375 free(pcmd_buff);
1376 pcmd_buff = NULL;
1377 }
1378
1379 } else {
1380 if (gphNxpExtns_Context.CallBackMifare != NULL) {
1381 if ((gphNxpExtns_Context.incrdecstatusflag == true) && status == 0xB2) {
1382 gphNxpExtns_Context.incrdecstatusflag = false;
1383 status = NFCSTATUS_SUCCESS;
1384 }
1385 gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
1386 status);
1387 }
1388 }
1389
1390 return status;
1391}
1392
1393/*******************************************************************************
1394**
1395** Function phNciNfc_MfCreateXchgDataHdr
1396**
1397** Description This function builds the payload header for mifare XchgData
1398** request to be sent to NFCC.
1399**
1400** Returns NFCSTATUS_PENDING - Command framing done
1401** NFCSTATUS_FAILED - Otherwise
1402**
1403*******************************************************************************/
1404static NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
1405 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz)
1406
1407{
1408 NFCSTATUS status = NFCSTATUS_SUCCESS;
1409 uint8_t i = 0;
1410
1411 buff[i++] = phNciNfc_e_MfRawDataXchgHdr;
1412 memcpy(&buff[i], tTranscvInfo.tSendData.pBuff, tTranscvInfo.tSendData.wLen);
1413 *buffSz = i + tTranscvInfo.tSendData.wLen;
1414
1415 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1416
1417 return status;
1418}
1419
1420/*******************************************************************************
1421**
1422** Function phNciNfc_MfCreateAuthCmdHdr
1423**
1424** Description This function builds the payload header for mifare
1425** classic Authenticate command to be sent to NFCC.
1426**
1427** Returns NFCSTATUS_PENDING - Command framing done
1428** NFCSTATUS_FAILED - Otherwise
1429**
1430*******************************************************************************/
1431static NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
1432 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
1433 uint16_t* buffSz) {
1434 NFCSTATUS status = NFCSTATUS_SUCCESS;
1435 // pphNciNfc_RemoteDevInformation_t pActivDev = NULL;
1436 // /*commented to eliminate unused variable warning*/
1437 uint8_t bKey = 0x00;
1438
1439 /*No need to check range of block address*/
1440 /*To check for Authenticate A or Authenticate B type command*/
1441 if (PHNCINFC_AUTHENTICATION_KEYB == tTranscvInfo.tSendData.pBuff[0]) {
1442 bKey = bKey | PHNCINFC_ENABLE_KEY_B;
1443 }
1444
1445 /*TO Do last 4 bits of Key to be set based of firmware implementation*/
1446 /*this value is hardcoded but based on firmware implementation change this
1447 * value*/
1448 bKey = (bKey | PHNCINFC_AUTHENTICATION_KEY);
1449
1450 bKey |= tTranscvInfo.tSendData.pBuff[2];
1451
1452 /*For authentication extension no need to copy tSendData buffer of
1453 * tTranscvInfo */
1454 tTranscvInfo.tSendData.wLen = 0x00;
1455
1456 buff[0] = phNciNfc_e_MfcAuthReq;
1457 buff[1] = bBlockAddr;
1458 buff[2] = bKey;
1459
1460 *buffSz = 0x03;
1461 if (bKey & PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY) {
1462 memcpy(&buff[3], &tTranscvInfo.tSendData.pBuff[3], PHLIBNFC_MFC_AUTHKEYLEN);
1463 *buffSz += PHLIBNFC_MFC_AUTHKEYLEN;
1464 }
1465 /* Store the auth command buffer to use further for presence check */
1466 if (gAuthCmdBuf.pauth_cmd != NULL) {
1467 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1468 gAuthCmdBuf.pauth_cmd->length = *buffSz;
1469 memcpy(gAuthCmdBuf.pauth_cmd->buffer, buff, *buffSz);
1470 }
1471 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1472
1473 return status;
1474}
1475
1476/*******************************************************************************
1477**
1478** Function phNciNfc_SendMfReq
1479**
1480** Description This function shall be invoked as part of ReaderMgmt data
1481** exchange sequence handler.
1482** It shall send the request packet to NFCC.
1483**
1484** Returns NFCSTATUS_PENDING - Send request is Pending
1485** NFCSTATUS_FAILED - otherwise
1486**
1487*******************************************************************************/
1488static NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
1489 uint8_t* buff, uint16_t* buffSz) {
1490 NFCSTATUS status = NFCSTATUS_SUCCESS;
1491
1492 switch (tTranscvInfo.uCmd.T2TCmd) {
1493 case phNciNfc_eT2TRaw: {
1494 status = phNciNfc_MfCreateXchgDataHdr(tTranscvInfo, buff, buffSz);
1495 } break;
1496 case phNciNfc_eT2TAuth: {
1497 status = phNciNfc_MfCreateAuthCmdHdr(tTranscvInfo, (tTranscvInfo.bAddr),
1498 buff, buffSz);
1499 } break;
1500 default: {
1501 status = NFCSTATUS_FAILED;
1502 break;
1503 }
1504 }
1505
1506 return status;
1507}
1508
1509/*******************************************************************************
1510**
1511** Function phLibNfc_CalSectorAddress
1512**
1513** Description This function update the sector address for Mifare classic
1514**
1515** Returns none
1516**
1517*******************************************************************************/
1518static void phLibNfc_CalSectorAddress(uint8_t* Sector_Address) {
1519 uint8_t BlockNumber = 0x00;
1520
1521 if (NULL != Sector_Address) {
1522 BlockNumber = *Sector_Address;
1523 if (BlockNumber >= PHLIBNFC_MIFARESTD4K_BLK128) {
1524 *Sector_Address = (uint8_t)(PHLIBNFC_MIFARESTD_SECTOR_NO32 +
1525 ((BlockNumber - PHLIBNFC_MIFARESTD4K_BLK128) /
1526 PHLIBNFC_MIFARESTD_BLOCK_BYTES));
1527 } else {
1528 *Sector_Address = BlockNumber / PHLIBNFC_NO_OF_BLKPERSECTOR;
1529 }
1530 } else {
1531 }
1532
1533 return;
1534}
1535
1536/*******************************************************************************
1537**
1538** Function phLibNfc_GetKeyNumberMFC
1539**
1540** Description This function find key number based on authentication
1541*command
1542**
1543** Returns NFCSTATUS_SUCCESS - If found the key number
1544** NFCSTATUS_FAILED - otherwise
1545**
1546*******************************************************************************/
1547static NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey) {
1548 int32_t sdwStat = 0X00;
1549 NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;
1550
nxf24591dc0bc2c2018-02-21 17:33:08 +05301551 uint8_t bIndex = 0x00;
1552 uint8_t bNoOfKeys = 0x00;
1553
1554#if PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS
1555 uint8_t aMfc_keys[NXP_NUMBER_OF_MFC_KEYS][NXP_MFC_KEY_SIZE] = NXP_MFC_KEYS;
1556#else
1557 uint8_t aMfc_keys[1][1] = {{0x00}};
1558#endif
1559
1560 if (NULL != bKey && NULL != buffer) {
1561 bNoOfKeys = sizeof(aMfc_keys) / NXP_MFC_KEY_SIZE;
1562 /* Traverse through the keys stored to determine whether keys is preloaded
1563 * key */
1564 for (bIndex = 0; bIndex < bNoOfKeys; bIndex++) {
1565 /* Check passed key is NDEF key */
1566 sdwStat = memcmp(&buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], aMfc_keys[bIndex],
1567 PHLIBNFC_MFC_AUTHKEYLEN);
1568 if (!sdwStat) {
1569 LOG(ERROR) << StringPrintf(
1570 "Mifare : phLibNfc_GetKeyNumberMFC Key found");
1571 *bKey = bIndex;
1572 wStatus = NFCSTATUS_SUCCESS;
1573 break;
1574 }
1575 }
1576 LOG(ERROR) << StringPrintf(
1577 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x Key = 0x%x",
1578 wStatus, *bKey);
1579 } else {
1580 wStatus = NFCSTATUS_FAILED;
1581 LOG(ERROR) << StringPrintf(
1582 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x", wStatus);
1583 }
1584
1585 return wStatus;
1586}
1587
1588/*******************************************************************************
1589**
1590** Function phLibNfc_ChkAuthCmdMFC
1591**
1592** Description This function Check Authentication command send is proper or
1593*not
1594**
1595** Returns NFCSTATUS_SUCCESS - Authenticate command is proper
1596** NFCSTATUS_FAILED - otherwise
1597**
1598*******************************************************************************/
1599static NFCSTATUS phLibNfc_ChkAuthCmdMFC(
1600 phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey) {
1601 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1602
1603 if (NULL != pTransceiveInfo && NULL != pTransceiveInfo->sSendData.buffer &&
1604 0 != pTransceiveInfo->sSendData.length && NULL != bKey) {
1605 if ((pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentA ||
1606 pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentB)) {
1607 wStatus =
1608 phLibNfc_GetKeyNumberMFC(pTransceiveInfo->sSendData.buffer, bKey);
1609 } else {
1610 wStatus = NFCSTATUS_FAILED;
1611 }
1612 } else {
1613 wStatus = NFCSTATUS_FAILED;
1614 }
1615 return wStatus;
1616}
1617
1618/*******************************************************************************
1619**
1620** Function phLibNfc_MifareMap
1621**
1622** Description Mifare Mapping Utility function
1623**
1624** Returns NFCSTATUS_SUCCESS - Mapping is proper
1625** NFCSTATUS_INVALID_PARAMETER - otherwise
1626**
1627*******************************************************************************/
1628static NFCSTATUS phLibNfc_MifareMap(
1629 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1630 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1631 NFCSTATUS status = NFCSTATUS_SUCCESS;
1632 uint8_t bBuffIdx = 0;
1633 uint8_t bSectorNumber;
1634 uint8_t bKey = 0;
1635
1636 switch (pTransceiveInfo->cmd.MfCmd) {
1637 case phNfc_eMifareRead16: {
1638 if ((NULL != pTransceiveInfo->sRecvData.buffer) &&
1639 (0 != (pTransceiveInfo->sRecvData.length))) {
1640 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareRead16;
1641 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1642 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1643 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1644 } else {
1645 status = NFCSTATUS_INVALID_PARAMETER;
1646 }
1647 } break;
1648
1649 case phNfc_eMifareWrite16: {
1650 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1651 (0 != (pTransceiveInfo->sSendData.length))) {
1652 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1653 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1654 memcpy(&(pMappedTranscvIf->tSendData.pBuff[bBuffIdx]),
1655 pTransceiveInfo->sSendData.buffer,
1656 (pTransceiveInfo->sSendData.length));
1657 pMappedTranscvIf->tSendData.wLen =
1658 bBuffIdx + pTransceiveInfo->sSendData.length;
1659 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1660 } else {
1661 status = NFCSTATUS_INVALID_PARAMETER;
1662 }
1663 } break;
1664
1665 case phNfc_eMifareAuthentA:
1666 case phNfc_eMifareAuthentB: {
1667 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1668 (0 != (pTransceiveInfo->sSendData.length)) &&
1669 (NULL != pTransceiveInfo->sRecvData.buffer) &&
1670 (0 != (pTransceiveInfo->sRecvData.length))) {
1671 status = phLibNfc_ChkAuthCmdMFC(pTransceiveInfo, &bKey);
1672 if (NFCSTATUS_FAILED != status) {
1673 bSectorNumber = pTransceiveInfo->addr;
1674 phLibNfc_CalSectorAddress(&bSectorNumber);
1675 /*For creating extension command header pTransceiveInfo's MfCmd get
1676 * used*/
1677 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] =
1678 pTransceiveInfo->cmd.MfCmd;
1679 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bSectorNumber;
1680 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TAuth;
1681 pMappedTranscvIf->bAddr = bSectorNumber;
1682 pMappedTranscvIf->bNumBlock = pTransceiveInfo->NumBlock;
1683 if (NFCSTATUS_SUCCESS == status) {
1684 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1685 (pMappedTranscvIf->tSendData.wLen) = (uint16_t)(bBuffIdx);
1686
1687 } else if (NFCSTATUS_INVALID_PARAMETER == status) {
1688 bKey = bKey | PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY;
1689 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1690 memcpy(&pMappedTranscvIf->tSendData.pBuff[bBuffIdx],
1691 &pTransceiveInfo->sSendData
1692 .buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD],
1693 PHLIBNFC_MFC_AUTHKEYLEN);
1694
1695 (pMappedTranscvIf->tSendData.wLen) =
1696 (uint16_t)(bBuffIdx + PHLIBNFC_MFC_AUTHKEYLEN);
1697 status = NFCSTATUS_SUCCESS;
1698 } else {
1699 /* do nothing */
1700 }
1701 }
1702 } else {
1703 status = NFCSTATUS_INVALID_PARAMETER;
1704 }
1705 } break;
1706
1707 case phNfc_eMifareRaw: {
1708 } break;
1709
1710 default: {
1711 status = NFCSTATUS_INVALID_PARAMETER;
1712 break;
1713 }
1714 }
1715
1716 return status;
1717}
1718
1719/*******************************************************************************
1720**
1721** Function phLibNfc_MapCmds
1722**
1723** Description This function maps the command request from libnfc level to
1724*nci level
1725**
1726** Returns NFCSTATUS_SUCCESS - Mapping of command is
1727*successful
1728** NFCSTATUS_INVALID_PARAMETER - One or more of the supplied
1729** parameters could not be interpreted properly
1730**
1731*******************************************************************************/
1732static NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
1733 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1734 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1735 NFCSTATUS status = NFCSTATUS_SUCCESS;
1736
1737 if ((NULL == pTransceiveInfo) || (NULL == pMappedTranscvIf)) {
1738 return NFCSTATUS_FAILED;
1739 }
1740 switch (RemDevType) {
1741 case phNciNfc_eMifare1k_PICC:
1742 case phNciNfc_eMifare4k_PICC: {
1743 status = phLibNfc_MifareMap(pTransceiveInfo, pMappedTranscvIf);
1744 break;
1745 }
1746 default: { break; }
1747 }
1748
1749 return status;
1750}
1751
1752/*******************************************************************************
1753**
1754** Function phLibNfc_SendAuthCmd
1755**
1756** Description This function Send authentication command to NFCC
1757**
1758** Returns NFCSTATUS_SUCCESS - Parameters are proper
1759** NFCSTATUS_INVALID_PARAMETER - Otherwise
1760**
1761*******************************************************************************/
1762static NFCSTATUS phLibNfc_SendAuthCmd(
1763 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1764 phNciNfc_TransceiveInfo_t* tNciTranscvInfo) {
1765 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1766
1767 wStatus = phLibNfc_MapCmds(phNciNfc_eMifare1k_PICC, pTransceiveInfo,
1768 tNciTranscvInfo);
1769
1770 return wStatus;
1771}
1772
1773/*******************************************************************************
1774**
1775** Function phLibNfc_SendWrt16Cmd
1776**
1777** Description This function maps Mifarewirte16 commands
1778**
1779** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1780** NFCSTATUS_INVALID_PARAMETER - Otherwise
1781**
1782*******************************************************************************/
1783static NFCSTATUS phLibNfc_SendWrt16Cmd(
1784 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1785 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1786 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1787 uint8_t bBuffIdx = 0x00;
1788
1789 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1790 (0 != (pTransceiveInfo->sSendData.length))) {
1791 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1792 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1793 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1794 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1795 } else {
1796 wStatus = NFCSTATUS_INVALID_PARAMETER;
1797 }
1798
1799 return wStatus;
1800}
1801
1802/*******************************************************************************
1803**
1804** Function phLibNfc_SendIncDecCmd
1805**
1806** Description This function prepares the Increment/Decrement command
1807** to be sent, increment/decrement value is sent separately
1808**
1809** Returns NFCSTATUS_SUCCESS - Params are mapped
1810** NFCSTATUS_INVALID_PARAMETER - Otherwise
1811**
1812*******************************************************************************/
1813static NFCSTATUS phLibNfc_SendIncDecCmd(
1814 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1815 pphNciNfc_TransceiveInfo_t pMappedTranscvIf, uint8_t IncDecCmd) {
1816 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1817 uint8_t bBuffIdx = 0x00;
1818
1819 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1820 (0 != (pTransceiveInfo->sSendData.length))) {
1821 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = IncDecCmd;
1822 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1823 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1824 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1825 } else {
1826 wStatus = NFCSTATUS_INVALID_PARAMETER;
1827 }
1828
1829 return wStatus;
1830}
1831
1832/*******************************************************************************
1833**
1834** Function phLibNfc_SendRawCmd
1835**
1836** Description This function maps Mifare raw command
1837**
1838** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1839** NFCSTATUS_INVALID_PARAMETER - Otherwise
1840**
1841*******************************************************************************/
1842static NFCSTATUS phLibNfc_SendRawCmd(
1843 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1844 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1845 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1846 // uint8_t bBuffIdx = 0x00; /*commented to
1847 // eliminate unused variable warning*/
1848
1849 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1850 (0 != (pTransceiveInfo->sSendData.length))) {
1851 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1852 (pTransceiveInfo->sSendData.length));
1853 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1854 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1855 } else {
1856 wStatus = NFCSTATUS_INVALID_PARAMETER;
1857 }
1858
1859 return wStatus;
1860}
1861
1862/*******************************************************************************
1863**
1864** Function phFriNfc_ExtnsTransceive
1865**
1866** Description This function maps Mifare raw command and send it to NFCC
1867**
1868** Returns NFCSTATUS_PENDING - Operation successful
1869** NFCSTATUS_INVALID_PARAMETER - Otherwise
1870**
1871*******************************************************************************/
1872NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1873 phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf,
1874 uint16_t SendLength,
1875 uint16_t* SendRecvLength) {
1876 (void)SendRecvLength;
1877 NFCSTATUS status = NFCSTATUS_FAILED;
1878 uint8_t* buff = NULL;
1879 uint16_t buffSz = 0;
1880 uint8_t i = 0;
1881 uint32_t length = SendLength;
1882 uint8_t restore_payload[] = {
1883 0x00, 0x00, 0x00, 0x00,
1884 };
1885
Lijo Abraham4e8ed2e2019-09-06 10:55:14 +05301886 if (SendLength == 0) {
1887 android_errorWriteLog(0x534e4554, "132083376");
1888 return status;
1889 }
1890
nxf24591dc0bc2c2018-02-21 17:33:08 +05301891 buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1892 if (NULL == buff) {
1893 return status;
1894 }
1895
1896 pTransceiveInfo->cmd = Cmd;
1897
1898 if ((Cmd.MfCmd == phNfc_eMifareAuthentA) ||
1899 (Cmd.MfCmd == phNfc_eMifareAuthentB)) {
1900 pTransceiveInfo->addr = SendRecvBuf[i++];
1901 pTransceiveInfo->sSendData.buffer[4] = SendRecvBuf[i++];
1902 pTransceiveInfo->sSendData.buffer[5] = SendRecvBuf[i++];
1903 pTransceiveInfo->sSendData.buffer[6] = SendRecvBuf[i++];
1904 pTransceiveInfo->sSendData.buffer[7] = SendRecvBuf[i++];
1905 pTransceiveInfo->sSendData.buffer[8] = SendRecvBuf[i++];
1906 pTransceiveInfo->sSendData.buffer[9] = SendRecvBuf[i++];
1907
1908 pTransceiveInfo->cmd.MfCmd = Cmd.MfCmd;
1909
1910 pTransceiveInfo->sSendData.length = length;
1911 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1912 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1913 } else if (Cmd.MfCmd == phNfc_eMifareWrite16) {
1914 pTransceiveInfo->addr = SendRecvBuf[i++];
1915 length = SendLength - i;
1916 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1917 pTransceiveInfo->sSendData.length = length;
1918 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1919
1920 gphNxpExtns_Context.writecmdFlag = true;
1921
1922 status = phLibNfc_SendWrt16Cmd(pTransceiveInfo, &tNciTranscvInfo);
1923 } else if ((Cmd.MfCmd == phNfc_eMifareInc) ||
1924 (Cmd.MfCmd == phNfc_eMifareDec)) {
1925 pTransceiveInfo->addr = SendRecvBuf[i++];
1926 length = SendLength - i;
1927 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1928 pTransceiveInfo->sSendData.length = length;
1929 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1930
1931 gphNxpExtns_Context.incrdecflag = true;
1932
1933 status =
1934 phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1935
1936 } else if (Cmd.MfCmd == phNfc_eMifareRestore) {
1937 pTransceiveInfo->addr = SendRecvBuf[i++];
1938 length = SendLength - i;
1939 memcpy(pTransceiveInfo->sSendData.buffer, &restore_payload[0],
1940 sizeof(restore_payload));
1941 pTransceiveInfo->sSendData.length = length + sizeof(restore_payload);
1942 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1943
1944 gphNxpExtns_Context.incrdecflag = true;
1945
1946 status =
1947 phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1948
1949 } else if ((Cmd.MfCmd == phNfc_eMifareRaw) ||
1950 (Cmd.MfCmd == phNfc_eMifareTransfer)) {
1951 pTransceiveInfo->cmd.MfCmd = (phNfc_eMifareCmdList_t)phNciNfc_eT2TRaw;
1952 memcpy(pTransceiveInfo->sSendData.buffer, SendRecvBuf, length);
1953 pTransceiveInfo->sSendData.length = length;
1954 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1955 status = phLibNfc_SendRawCmd(pTransceiveInfo, &tNciTranscvInfo);
1956 } else {
1957 pTransceiveInfo->addr = SendRecvBuf[i++];
1958 length = SendLength - i;
1959 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1960 pTransceiveInfo->sSendData.length = length;
1961 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1962 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1963 }
1964
1965 if (NFCSTATUS_SUCCESS == status) {
1966 status = phNciNfc_SendMfReq(tNciTranscvInfo, buff, &buffSz);
1967 if (NFCSTATUS_PENDING != status) {
1968 LOG(ERROR) << StringPrintf("ERROR : phNciNfc_SendMfReq()");
1969 }
1970 } else {
1971 LOG(ERROR) << StringPrintf(" ERROR : Sending phNciNfc_SendMfReq");
1972 }
1973 if (buff != NULL) {
1974 free(buff);
1975 buff = NULL;
1976 }
1977
1978 return status;
1979}