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