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