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[PHLIBNFC_MFC_AUTHKEYLEN] = {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[PHLIBNFC_MFC_AUTHKEYLEN] = {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 if (len != PHLIBNFC_MFC_AUTHKEYLEN) return NFCSTATUS_FAILED;
863
864 memcpy(mif_std_key, secretkey, len);
865 memcpy(current_key, secretkey, len);
866
867 if (NULL == NdefSmtCrdFmt || NULL == NdefMap ||
868 NULL == NdefMap->SendRecvBuf) {
869 goto Mfc_FormatEnd;
870 }
871 NdefSmtCrdFmt->pTransceiveInfo = NdefMap->pTransceiveInfo;
872
873 gphNxpExtns_Context.CallBackMifare = phFriNfc_MfStd_Process;
874 gphNxpExtns_Context.CallBackCtxt = NdefSmtCrdFmt;
875
876 NdefInfo.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
877 phFriNfc_NdefSmtCrd_Reset__(NdefSmtCrdFmt, NdefMap->SendRecvBuf,
878 &(NdefInfo.NdefSendRecvLen));
879
880 /* Register Callbacks */
881 NdefSmtCrdFmt->CompletionRoutine[0].CompletionRoutine =
882 Mfc_FormatNdef_Completion_Routine;
883 NdefSmtCrdFmt->CompletionRoutine[1].CompletionRoutine =
884 Mfc_FormatNdef_Completion_Routine;
885 NdefSmtCrdFmt->psRemoteDevInfo = NdefMap->psRemoteDevInfo;
886 sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
887
888 if ((0x08 == (sak & 0x18)) || (0x18 == (sak & 0x18)) || (0x01 == sak)) {
889 NdefSmtCrdFmt->CardType = (uint8_t)(
890 ((sak & 0x18) == 0x08)
891 ? PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD
892 : (((sak & 0x19) == 0x19) ? PH_FRINFC_SMTCRDFMT_MFSTD_2K_CRD
893 : PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD));
894 status = phFriNfc_MfStd_Format(NdefSmtCrdFmt, mif_std_key);
895 }
896
897 if (NFCSTATUS_PENDING == status) {
898 status = NFCSTATUS_SUCCESS;
899 }
900
901 Mfc_FormatEnd:
902 if (status != NFCSTATUS_SUCCESS) {
903 status = NFCSTATUS_FAILED;
904 }
905
906 return status;
907 }
908
909 /*******************************************************************************
910 **
911 ** Function phNxNciExtns_MifareStd_Reconnect
912 **
913 ** Description This function sends the deactivate command to NFCC for
914 *Mifare
915 **
916 **
917 ** Returns:
918 ** NFCSTATUS_PENDING - if successfully initiated
919 ** NFCSTATUS_FAILED - otherwise
920 **
921 *******************************************************************************/
phNxNciExtns_MifareStd_Reconnect(void)922 NFCSTATUS phNxNciExtns_MifareStd_Reconnect(void) {
923 tNFA_STATUS status;
924
925 EXTNS_SetDeactivateFlag(true);
926 if (NFA_STATUS_OK !=
927 (status = NFA_Deactivate(true))) /* deactivate to sleep state */
928 {
929 LOG(ERROR) << StringPrintf("%s: deactivate failed, status = %d", __func__,
930 status);
931 return NFCSTATUS_FAILED;
932 }
933
934 return NFCSTATUS_PENDING;
935 }
936
937 /*******************************************************************************
938 **
939 ** Function Mfc_DeactivateCbackSelect
940 **
941 ** Description This function select the Mifare tag
942 **
943 **
944 ** Returns: void
945 **
946 *******************************************************************************/
Mfc_DeactivateCbackSelect(void)947 void Mfc_DeactivateCbackSelect(void) {
948 tNFA_STATUS status;
949
950 EXTNS_SetConnectFlag(true);
951 if (NFA_STATUS_OK !=
952 (status = NFA_Select(0x01, phNciNfc_e_RfProtocolsMifCProtocol,
953 phNciNfc_e_RfInterfacesTagCmd_RF))) {
954 LOG(ERROR) << StringPrintf("%s: NFA_Select failed, status = %d", __func__,
955 status);
956 }
957
958 return;
959 }
960
961 /*******************************************************************************
962 **
963 ** Function Mfc_ActivateCback
964 **
965 ** Description This function invoke the callback when receive the response
966 **
967 **
968 ** Returns: void
969 **
970 **
971 *******************************************************************************/
Mfc_ActivateCback(void)972 void Mfc_ActivateCback(void) {
973 gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
974 NFCSTATUS_SUCCESS);
975 return;
976 }
977
978 /*******************************************************************************
979 **
980 ** Function Mfc_Transceive
981 **
982 ** Description Sends raw frame to Mifare Classic Tag.
983 **
984 ** Returns NFCSTATUS_SUCCESS - if successfully initiated
985 ** NFCSTATUS_FAILED - otherwise
986 **
987 *******************************************************************************/
Mfc_Transceive(uint8_t * p_data,uint32_t len)988 NFCSTATUS Mfc_Transceive(uint8_t* p_data, uint32_t len) {
989 NFCSTATUS status = NFCSTATUS_FAILED;
990 uint8_t i = 0x00;
991
992 if (len == 0) {
993 android_errorWriteLog(0x534e4554, "132082342");
994 return status;
995 }
996
997 gphNxpExtns_Context.RawWriteCallBack = false;
998 gphNxpExtns_Context.CallBackMifare = NULL;
999 gphNxpExtns_Context.CallBackCtxt = NdefMap;
1000
1001 EXTNS_SetCallBackFlag(true);
1002 if (p_data[0] == 0x60 || p_data[0] == 0x61) {
1003 if (len < 12) {
1004 android_errorWriteLog(0x534e4554, "125900276");
1005 return status;
1006 }
1007 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1008
1009 NdefMap->SendRecvBuf[i++] = p_data[1];
1010
1011 NdefMap->SendRecvBuf[i++] = p_data[6]; /* TODO, handle 7 byte UID */
1012 NdefMap->SendRecvBuf[i++] = p_data[7];
1013 NdefMap->SendRecvBuf[i++] = p_data[8];
1014 NdefMap->SendRecvBuf[i++] = p_data[9];
1015 NdefMap->SendRecvBuf[i++] = p_data[10];
1016 NdefMap->SendRecvBuf[i++] = p_data[11];
1017
1018 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1019 NdefMap->SendRecvBuf, NdefMap->SendLength,
1020 NdefMap->SendRecvLength);
1021 } else if (p_data[0] == 0xA0) {
1022 EXTNS_SetCallBackFlag(false);
1023 NdefMap->Cmd.MfCmd = phNfc_eMifareWrite16;
1024 gphNxpExtns_Context.RawWriteCallBack = true;
1025
1026 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1027 NdefMap->SendLength = len - 1;
1028 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1029 NdefMap->SendRecvBuf, NdefMap->SendLength,
1030 NdefMap->SendRecvLength);
1031 } else if ((p_data[0] == phNfc_eMifareInc) ||
1032 (p_data[0] == phNfc_eMifareDec)) {
1033 EXTNS_SetCallBackFlag(false);
1034 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1035 gphNxpExtns_Context.RawWriteCallBack = true;
1036
1037 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1038 NdefMap->SendLength = len - 1;
1039 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1040 NdefMap->SendRecvBuf, NdefMap->SendLength,
1041 NdefMap->SendRecvLength);
1042 } else if (((p_data[0] == phNfc_eMifareTransfer) ||
1043 (p_data[0] == phNfc_eMifareRestore)) &&
1044 (len == 2)) {
1045 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)p_data[0];
1046 if (p_data[0] == phNfc_eMifareRestore) {
1047 EXTNS_SetCallBackFlag(false);
1048 gphNxpExtns_Context.RawWriteCallBack = true;
1049 memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1);
1050 NdefMap->SendLength = len - 1;
1051 } else {
1052 memcpy(NdefMap->SendRecvBuf, p_data, len);
1053 NdefMap->SendLength = len;
1054 }
1055 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1056 NdefMap->SendRecvBuf, NdefMap->SendLength,
1057 NdefMap->SendRecvLength);
1058
1059 } else {
1060 NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t)phNfc_eMifareRaw;
1061
1062 memcpy(NdefMap->SendRecvBuf, p_data, len);
1063 NdefMap->SendLength = len;
1064 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd,
1065 NdefMap->SendRecvBuf, NdefMap->SendLength,
1066 NdefMap->SendRecvLength);
1067 }
1068 if (NFCSTATUS_PENDING == status) {
1069 status = NFCSTATUS_SUCCESS;
1070 } else {
1071 LOG(ERROR) << StringPrintf("ERROR: Mfc_Transceive = 0x%x", status);
1072 }
1073
1074 return status;
1075 }
1076
1077 /*******************************************************************************
1078 **
1079 ** Function nativeNfcExtns_doTransceive
1080 **
1081 ** Description Sends raw frame to BCM stack.
1082 **
1083 ** Returns NFCSTATUS_PENDING - if successfully initiated
1084 ** NFCSTATUS_FAILED - otherwise
1085 **
1086 *******************************************************************************/
nativeNfcExtns_doTransceive(uint8_t * buff,uint16_t buffSz)1087 static NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz) {
1088 NFCSTATUS wStatus = NFCSTATUS_PENDING;
1089 tNFA_STATUS status =
1090 NFA_SendRawFrame(buff, buffSz, NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY);
1091
1092 if (status != NFA_STATUS_OK) {
1093 LOG(ERROR) << StringPrintf("%s: fail send; error=%d", __func__, status);
1094 wStatus = NFCSTATUS_FAILED;
1095 }
1096
1097 return wStatus;
1098 }
1099
1100 /*******************************************************************************
1101 **
1102 ** Function phNciNfc_RecvMfResp
1103 **
1104 ** Description This function shall be invoked as part of ReaderMgmt data
1105 ** exchange sequence handler on receiving response/data from
1106 *NFCC
1107 **
1108 ** Returns NFCSTATUS_SUCCESS - Data Reception is successful
1109 ** NFCSTATUS_FAILED - Data Reception failed
1110 **
1111 *******************************************************************************/
phNciNfc_RecvMfResp(phNciNfc_Buff_t * RspBuffInfo,NFCSTATUS wStatus)1112 static NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo,
1113 NFCSTATUS wStatus) {
1114 NFCSTATUS status = NFCSTATUS_SUCCESS;
1115 uint16_t wPldDataSize = 0;
1116 phNciNfc_ExtnRespId_t RecvdExtnRspId = phNciNfc_e_InvalidRsp;
1117 if (NULL == RspBuffInfo) {
1118 status = NFCSTATUS_FAILED;
1119 } else {
1120 if ((0 == (RspBuffInfo->wLen)) || (PH_NCINFC_STATUS_OK != wStatus) ||
1121 (NULL == (RspBuffInfo->pBuff))) {
1122 status = NFCSTATUS_FAILED;
1123 } else {
1124 RecvdExtnRspId = (phNciNfc_ExtnRespId_t)RspBuffInfo->pBuff[0];
1125
1126 switch (RecvdExtnRspId) {
1127 case phNciNfc_e_MfXchgDataRsp: {
1128 NFCSTATUS writeResponse = NFCSTATUS_SUCCESS;
1129 /* check the status byte */
1130 if (NFC_GetNCIVersion() == NCI_VERSION_2_0 &&
1131 (NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_TLV ||
1132 NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WRITE ||
1133 NdefMap->State == PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN ||
1134 NdefMap->State == PH_FRINFC_NDEFMAP_STATE_INIT)) {
1135 uint8_t rspAck = RspBuffInfo->pBuff[RspBuffInfo->wLen - 2];
1136 uint8_t rspAckMask = ((RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]) &
1137 MAX_NUM_VALID_BITS_FOR_ACK);
1138 NCI_CALCULATE_ACK(rspAck, rspAckMask);
1139 writeResponse =
1140 (rspAck == T2T_RSP_ACK) ? NFCSTATUS_SUCCESS : NFC_STATUS_FAILED;
1141 } else {
1142 writeResponse = RspBuffInfo->pBuff[RspBuffInfo->wLen - 1];
1143 }
1144 if (PH_NCINFC_STATUS_OK == writeResponse) {
1145 status = NFCSTATUS_SUCCESS;
1146 uint16_t wRecvDataSz = 0;
1147
1148 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1149 wPldDataSize = ((RspBuffInfo->wLen) -
1150 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1151 wRecvDataSz = NCI_MAX_DATA_LEN;
1152
1153 /* wPldDataSize = wPldDataSize-1; ==> ignoring the last status byte
1154 * appended with data */
1155 if ((wPldDataSize) <= wRecvDataSz) {
1156 /* Extract the data part from pBuff[2] & fill it to be sent to
1157 * upper layer */
1158 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[1]),
1159 (wPldDataSize));
1160 /* update the number of bytes received from lower layer,excluding
1161 * the status byte */
1162 *(NdefMap->SendRecvLength) = wPldDataSize;
1163 } else {
1164 // TODO:- Map some status for remaining extra data received to be
1165 // sent back to caller??
1166 status = NFCSTATUS_FAILED;
1167 }
1168 } else {
1169 status = NFCSTATUS_FAILED;
1170 }
1171 } break;
1172
1173 case phNciNfc_e_MfcAuthRsp: {
1174 /* check the status byte */
1175 if (PH_NCINFC_STATUS_OK == RspBuffInfo->pBuff[1]) {
1176 if (gAuthCmdBuf.auth_sent == true) {
1177 MfcPresenceCheckResult(NFCSTATUS_SUCCESS);
1178 return NFCSTATUS_SUCCESS;
1179 }
1180 gAuthCmdBuf.auth_status = true;
1181 status = NFCSTATUS_SUCCESS;
1182 if ((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) >
1183 RspBuffInfo->wLen) {
1184 android_errorWriteLog(0x534e4554, "126204073");
1185 return NFCSTATUS_FAILED;
1186 }
1187 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1188 wPldDataSize = ((RspBuffInfo->wLen) -
1189 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1190
1191 /* Extract the data part from pBuff[2] & fill it to be sent to upper
1192 * layer */
1193 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[2]),
1194 wPldDataSize);
1195 /* update the number of bytes received from lower layer,excluding
1196 * the status byte */
1197 *(NdefMap->SendRecvLength) = wPldDataSize;
1198 } else {
1199 if (gAuthCmdBuf.auth_sent == true) {
1200 gAuthCmdBuf.auth_status = false;
1201 MfcPresenceCheckResult(NFCSTATUS_FAILED);
1202 return NFCSTATUS_SUCCESS;
1203 } else {
1204 /* Reset the stored auth command buffer */
1205 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1206 gAuthCmdBuf.pauth_cmd->length = 0;
1207 gAuthCmdBuf.auth_status = false;
1208 }
1209 status = NFCSTATUS_FAILED;
1210 }
1211 } break;
1212
1213 default: { status = NFCSTATUS_FAILED; } break;
1214 }
1215 }
1216 }
1217
1218 return status;
1219 }
1220
1221 /*******************************************************************************
1222 **
1223 ** Function phLibNfc_SendWrt16CmdPayload
1224 **
1225 ** Description This function map the raw write cmd
1226 **
1227 ** Returns NFCSTATUS_SUCCESS - Command framing done
1228 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1229 **
1230 *******************************************************************************/
phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1231 static NFCSTATUS phLibNfc_SendWrt16CmdPayload(
1232 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1233 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1234 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1235
1236 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1237 (0 != (pTransceiveInfo->sSendData.length))) {
1238 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1239 (pTransceiveInfo->sSendData.length));
1240 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1241 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1242 } else {
1243 wStatus = NFCSTATUS_INVALID_PARAMETER;
1244 }
1245
1246 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1247 EXTNS_SetCallBackFlag(true);
1248 gphNxpExtns_Context.RawWriteCallBack = false;
1249 }
1250
1251 return wStatus;
1252 }
1253
1254 /*******************************************************************************
1255 **
1256 ** Function phLibNfc_SendIncDecCmdPayload
1257 **
1258 ** Description This function prepares the Increment/Decrement Value to be
1259 ** sent. This is called after sending the Increment/Decrement
1260 ** command is already sent and successfull
1261 **
1262 ** Returns NFCSTATUS_SUCCESS - Payload framing done
1263 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1264 **
1265 *******************************************************************************/
phLibNfc_SendIncDecCmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1266 static NFCSTATUS phLibNfc_SendIncDecCmdPayload(
1267 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1268 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1269 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1270
1271 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1272 (0 != (pTransceiveInfo->sSendData.length))) {
1273 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1274 (pTransceiveInfo->sSendData.length));
1275 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1276 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1277 } else {
1278 wStatus = NFCSTATUS_INVALID_PARAMETER;
1279 }
1280
1281 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1282 EXTNS_SetCallBackFlag(true);
1283 gphNxpExtns_Context.RawWriteCallBack = false;
1284 }
1285
1286 return wStatus;
1287 }
1288
1289 /*******************************************************************************
1290 **
1291 ** Function Mfc_RecvPacket
1292 **
1293 ** Description Decodes Mifare Classic Tag Response
1294 ** This is called from NFA_SendRaw Callback
1295 **
1296 ** Returns:
1297 ** NFCSTATUS_SUCCESS - if successfully initiated
1298 ** NFCSTATUS_FAILED - otherwise
1299 **
1300 *******************************************************************************/
Mfc_RecvPacket(uint8_t * buff,uint8_t buffSz)1301 NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz) {
1302 NFCSTATUS status = NFCSTATUS_SUCCESS;
1303 phNciNfc_Buff_t RspBuff;
1304 uint8_t* pcmd_buff;
1305 uint16_t buffSize;
1306
1307 RspBuff.pBuff = buff;
1308 RspBuff.wLen = buffSz;
1309 status = phNciNfc_RecvMfResp(&RspBuff, status);
1310 if (true == gAuthCmdBuf.auth_sent) {
1311 DLOG_IF(INFO, nfc_debug_enabled)
1312 << StringPrintf("%s Mfc Check Presence in progress", __func__);
1313 gAuthCmdBuf.auth_sent = false;
1314 return status;
1315 }
1316 if (true == gphNxpExtns_Context.writecmdFlag &&
1317 (NFCSTATUS_SUCCESS == status)) {
1318 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1319 if (NULL == pcmd_buff) {
1320 return NFCSTATUS_FAILED;
1321 }
1322 buffSize = MAX_BUFF_SIZE;
1323 gphNxpExtns_Context.writecmdFlag = false;
1324 phLibNfc_SendWrt16CmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1325 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1326 if (NFCSTATUS_PENDING != status) {
1327 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1328 } else {
1329 status = NFCSTATUS_SUCCESS;
1330 }
1331 if (pcmd_buff != NULL) {
1332 free(pcmd_buff);
1333 pcmd_buff = NULL;
1334 }
1335 } else if (true == gphNxpExtns_Context.incrdecflag &&
1336 (NFCSTATUS_SUCCESS == status)) {
1337 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1338 if (NULL == pcmd_buff) {
1339 return NFCSTATUS_FAILED;
1340 }
1341 buffSize = MAX_BUFF_SIZE;
1342 gphNxpExtns_Context.incrdecflag = false;
1343 phLibNfc_SendIncDecCmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1344 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1345 if (NFCSTATUS_PENDING != status) {
1346 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1347 } else {
1348 status = NFCSTATUS_SUCCESS;
1349 }
1350 gphNxpExtns_Context.incrdecstatusflag = true;
1351 if (pcmd_buff != NULL) {
1352 free(pcmd_buff);
1353 pcmd_buff = NULL;
1354 }
1355
1356 } else {
1357 if (gphNxpExtns_Context.CallBackMifare != NULL) {
1358 if ((gphNxpExtns_Context.incrdecstatusflag == true) && status == 0xB2) {
1359 gphNxpExtns_Context.incrdecstatusflag = false;
1360 status = NFCSTATUS_SUCCESS;
1361 }
1362 gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
1363 status);
1364 }
1365 }
1366
1367 return status;
1368 }
1369
1370 /*******************************************************************************
1371 **
1372 ** Function phNciNfc_MfCreateXchgDataHdr
1373 **
1374 ** Description This function builds the payload header for mifare XchgData
1375 ** request to be sent to NFCC.
1376 **
1377 ** Returns NFCSTATUS_PENDING - Command framing done
1378 ** NFCSTATUS_FAILED - Otherwise
1379 **
1380 *******************************************************************************/
phNciNfc_MfCreateXchgDataHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1381 static NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
1382 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz)
1383
1384 {
1385 NFCSTATUS status = NFCSTATUS_SUCCESS;
1386 uint8_t i = 0;
1387
1388 buff[i++] = phNciNfc_e_MfRawDataXchgHdr;
1389 memcpy(&buff[i], tTranscvInfo.tSendData.pBuff, tTranscvInfo.tSendData.wLen);
1390 *buffSz = i + tTranscvInfo.tSendData.wLen;
1391
1392 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1393
1394 return status;
1395 }
1396
1397 /*******************************************************************************
1398 **
1399 ** Function phNciNfc_MfCreateAuthCmdHdr
1400 **
1401 ** Description This function builds the payload header for mifare
1402 ** classic Authenticate command to be sent to NFCC.
1403 **
1404 ** Returns NFCSTATUS_PENDING - Command framing done
1405 ** NFCSTATUS_FAILED - Otherwise
1406 **
1407 *******************************************************************************/
phNciNfc_MfCreateAuthCmdHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t bBlockAddr,uint8_t * buff,uint16_t * buffSz)1408 static NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
1409 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
1410 uint16_t* buffSz) {
1411 NFCSTATUS status = NFCSTATUS_SUCCESS;
1412 // pphNciNfc_RemoteDevInformation_t pActivDev = NULL;
1413 // /*commented to eliminate unused variable warning*/
1414 uint8_t bKey = 0x00;
1415
1416 /*No need to check range of block address*/
1417 /*To check for Authenticate A or Authenticate B type command*/
1418 if (PHNCINFC_AUTHENTICATION_KEYB == tTranscvInfo.tSendData.pBuff[0]) {
1419 bKey = bKey | PHNCINFC_ENABLE_KEY_B;
1420 }
1421
1422 /*TO Do last 4 bits of Key to be set based of firmware implementation*/
1423 /*this value is hardcoded but based on firmware implementation change this
1424 * value*/
1425 bKey = (bKey | PHNCINFC_AUTHENTICATION_KEY);
1426
1427 bKey |= tTranscvInfo.tSendData.pBuff[2];
1428
1429 /*For authentication extension no need to copy tSendData buffer of
1430 * tTranscvInfo */
1431 tTranscvInfo.tSendData.wLen = 0x00;
1432
1433 buff[0] = phNciNfc_e_MfcAuthReq;
1434 buff[1] = bBlockAddr;
1435 buff[2] = bKey;
1436
1437 *buffSz = 0x03;
1438 if (bKey & PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY) {
1439 memcpy(&buff[3], &tTranscvInfo.tSendData.pBuff[3], PHLIBNFC_MFC_AUTHKEYLEN);
1440 *buffSz += PHLIBNFC_MFC_AUTHKEYLEN;
1441 }
1442 /* Store the auth command buffer to use further for presence check */
1443 if (gAuthCmdBuf.pauth_cmd != NULL) {
1444 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1445 gAuthCmdBuf.pauth_cmd->length = *buffSz;
1446 memcpy(gAuthCmdBuf.pauth_cmd->buffer, buff, *buffSz);
1447 }
1448 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1449
1450 return status;
1451 }
1452
1453 /*******************************************************************************
1454 **
1455 ** Function phNciNfc_SendMfReq
1456 **
1457 ** Description This function shall be invoked as part of ReaderMgmt data
1458 ** exchange sequence handler.
1459 ** It shall send the request packet to NFCC.
1460 **
1461 ** Returns NFCSTATUS_PENDING - Send request is Pending
1462 ** NFCSTATUS_FAILED - otherwise
1463 **
1464 *******************************************************************************/
phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1465 static NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
1466 uint8_t* buff, uint16_t* buffSz) {
1467 NFCSTATUS status = NFCSTATUS_SUCCESS;
1468
1469 switch (tTranscvInfo.uCmd.T2TCmd) {
1470 case phNciNfc_eT2TRaw: {
1471 status = phNciNfc_MfCreateXchgDataHdr(tTranscvInfo, buff, buffSz);
1472 } break;
1473 case phNciNfc_eT2TAuth: {
1474 status = phNciNfc_MfCreateAuthCmdHdr(tTranscvInfo, (tTranscvInfo.bAddr),
1475 buff, buffSz);
1476 } break;
1477 default: {
1478 status = NFCSTATUS_FAILED;
1479 break;
1480 }
1481 }
1482
1483 return status;
1484 }
1485
1486 /*******************************************************************************
1487 **
1488 ** Function phLibNfc_CalSectorAddress
1489 **
1490 ** Description This function update the sector address for Mifare classic
1491 **
1492 ** Returns none
1493 **
1494 *******************************************************************************/
phLibNfc_CalSectorAddress(uint8_t * Sector_Address)1495 static void phLibNfc_CalSectorAddress(uint8_t* Sector_Address) {
1496 uint8_t BlockNumber = 0x00;
1497
1498 if (NULL != Sector_Address) {
1499 BlockNumber = *Sector_Address;
1500 if (BlockNumber >= PHLIBNFC_MIFARESTD4K_BLK128) {
1501 *Sector_Address = (uint8_t)(PHLIBNFC_MIFARESTD_SECTOR_NO32 +
1502 ((BlockNumber - PHLIBNFC_MIFARESTD4K_BLK128) /
1503 PHLIBNFC_MIFARESTD_BLOCK_BYTES));
1504 } else {
1505 *Sector_Address = BlockNumber / PHLIBNFC_NO_OF_BLKPERSECTOR;
1506 }
1507 } else {
1508 }
1509
1510 return;
1511 }
1512
1513 /*******************************************************************************
1514 **
1515 ** Function phLibNfc_GetKeyNumberMFC
1516 **
1517 ** Description This function find key number based on authentication
1518 *command
1519 **
1520 ** Returns NFCSTATUS_SUCCESS - If found the key number
1521 ** NFCSTATUS_FAILED - otherwise
1522 **
1523 *******************************************************************************/
phLibNfc_GetKeyNumberMFC(uint8_t * buffer,uint8_t * bKey)1524 static NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey) {
1525 int32_t sdwStat = 0X00;
1526 NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;
1527
1528 uint8_t bIndex = 0x00;
1529 uint8_t bNoOfKeys = 0x00;
1530
1531 #if PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS
1532 uint8_t aMfc_keys[NXP_NUMBER_OF_MFC_KEYS][NXP_MFC_KEY_SIZE] = NXP_MFC_KEYS;
1533 #else
1534 uint8_t aMfc_keys[1][1] = {{0x00}};
1535 #endif
1536
1537 if (NULL != bKey && NULL != buffer) {
1538 bNoOfKeys = sizeof(aMfc_keys) / NXP_MFC_KEY_SIZE;
1539 /* Traverse through the keys stored to determine whether keys is preloaded
1540 * key */
1541 for (bIndex = 0; bIndex < bNoOfKeys; bIndex++) {
1542 /* Check passed key is NDEF key */
1543 sdwStat = memcmp(&buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], aMfc_keys[bIndex],
1544 PHLIBNFC_MFC_AUTHKEYLEN);
1545 if (!sdwStat) {
1546 LOG(ERROR) << StringPrintf(
1547 "Mifare : phLibNfc_GetKeyNumberMFC Key found");
1548 *bKey = bIndex;
1549 wStatus = NFCSTATUS_SUCCESS;
1550 break;
1551 }
1552 }
1553 LOG(ERROR) << StringPrintf(
1554 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x Key = 0x%x",
1555 wStatus, *bKey);
1556 } else {
1557 wStatus = NFCSTATUS_FAILED;
1558 LOG(ERROR) << StringPrintf(
1559 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x", wStatus);
1560 }
1561
1562 return wStatus;
1563 }
1564
1565 /*******************************************************************************
1566 **
1567 ** Function phLibNfc_ChkAuthCmdMFC
1568 **
1569 ** Description This function Check Authentication command send is proper or
1570 *not
1571 **
1572 ** Returns NFCSTATUS_SUCCESS - Authenticate command is proper
1573 ** NFCSTATUS_FAILED - otherwise
1574 **
1575 *******************************************************************************/
phLibNfc_ChkAuthCmdMFC(phNfc_sTransceiveInfo_t * pTransceiveInfo,uint8_t * bKey)1576 static NFCSTATUS phLibNfc_ChkAuthCmdMFC(
1577 phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey) {
1578 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1579
1580 if (NULL != pTransceiveInfo && NULL != pTransceiveInfo->sSendData.buffer &&
1581 0 != pTransceiveInfo->sSendData.length && NULL != bKey) {
1582 if ((pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentA ||
1583 pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentB)) {
1584 wStatus =
1585 phLibNfc_GetKeyNumberMFC(pTransceiveInfo->sSendData.buffer, bKey);
1586 } else {
1587 wStatus = NFCSTATUS_FAILED;
1588 }
1589 } else {
1590 wStatus = NFCSTATUS_FAILED;
1591 }
1592 return wStatus;
1593 }
1594
1595 /*******************************************************************************
1596 **
1597 ** Function phLibNfc_MifareMap
1598 **
1599 ** Description Mifare Mapping Utility function
1600 **
1601 ** Returns NFCSTATUS_SUCCESS - Mapping is proper
1602 ** NFCSTATUS_INVALID_PARAMETER - otherwise
1603 **
1604 *******************************************************************************/
phLibNfc_MifareMap(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1605 static NFCSTATUS phLibNfc_MifareMap(
1606 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1607 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1608 NFCSTATUS status = NFCSTATUS_SUCCESS;
1609 uint8_t bBuffIdx = 0;
1610 uint8_t bSectorNumber;
1611 uint8_t bKey = 0;
1612
1613 switch (pTransceiveInfo->cmd.MfCmd) {
1614 case phNfc_eMifareRead16: {
1615 if ((NULL != pTransceiveInfo->sRecvData.buffer) &&
1616 (0 != (pTransceiveInfo->sRecvData.length))) {
1617 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareRead16;
1618 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1619 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1620 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1621 } else {
1622 status = NFCSTATUS_INVALID_PARAMETER;
1623 }
1624 } break;
1625
1626 case phNfc_eMifareWrite16: {
1627 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1628 (0 != (pTransceiveInfo->sSendData.length))) {
1629 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1630 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1631 memcpy(&(pMappedTranscvIf->tSendData.pBuff[bBuffIdx]),
1632 pTransceiveInfo->sSendData.buffer,
1633 (pTransceiveInfo->sSendData.length));
1634 pMappedTranscvIf->tSendData.wLen =
1635 bBuffIdx + pTransceiveInfo->sSendData.length;
1636 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1637 } else {
1638 status = NFCSTATUS_INVALID_PARAMETER;
1639 }
1640 } break;
1641
1642 case phNfc_eMifareAuthentA:
1643 case phNfc_eMifareAuthentB: {
1644 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1645 (0 != (pTransceiveInfo->sSendData.length)) &&
1646 (NULL != pTransceiveInfo->sRecvData.buffer) &&
1647 (0 != (pTransceiveInfo->sRecvData.length))) {
1648 status = phLibNfc_ChkAuthCmdMFC(pTransceiveInfo, &bKey);
1649 if (NFCSTATUS_FAILED != status) {
1650 bSectorNumber = pTransceiveInfo->addr;
1651 phLibNfc_CalSectorAddress(&bSectorNumber);
1652 /*For creating extension command header pTransceiveInfo's MfCmd get
1653 * used*/
1654 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] =
1655 pTransceiveInfo->cmd.MfCmd;
1656 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bSectorNumber;
1657 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TAuth;
1658 pMappedTranscvIf->bAddr = bSectorNumber;
1659 pMappedTranscvIf->bNumBlock = pTransceiveInfo->NumBlock;
1660 if (NFCSTATUS_SUCCESS == status) {
1661 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1662 (pMappedTranscvIf->tSendData.wLen) = (uint16_t)(bBuffIdx);
1663
1664 } else if (NFCSTATUS_INVALID_PARAMETER == status) {
1665 bKey = bKey | PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY;
1666 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1667 memcpy(&pMappedTranscvIf->tSendData.pBuff[bBuffIdx],
1668 &pTransceiveInfo->sSendData
1669 .buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD],
1670 PHLIBNFC_MFC_AUTHKEYLEN);
1671
1672 (pMappedTranscvIf->tSendData.wLen) =
1673 (uint16_t)(bBuffIdx + PHLIBNFC_MFC_AUTHKEYLEN);
1674 status = NFCSTATUS_SUCCESS;
1675 } else {
1676 /* do nothing */
1677 }
1678 }
1679 } else {
1680 status = NFCSTATUS_INVALID_PARAMETER;
1681 }
1682 } break;
1683
1684 case phNfc_eMifareRaw: {
1685 } break;
1686
1687 default: {
1688 status = NFCSTATUS_INVALID_PARAMETER;
1689 break;
1690 }
1691 }
1692
1693 return status;
1694 }
1695
1696 /*******************************************************************************
1697 **
1698 ** Function phLibNfc_MapCmds
1699 **
1700 ** Description This function maps the command request from libnfc level to
1701 *nci level
1702 **
1703 ** Returns NFCSTATUS_SUCCESS - Mapping of command is
1704 *successful
1705 ** NFCSTATUS_INVALID_PARAMETER - One or more of the supplied
1706 ** parameters could not be interpreted properly
1707 **
1708 *******************************************************************************/
phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1709 static NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
1710 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1711 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1712 NFCSTATUS status = NFCSTATUS_SUCCESS;
1713
1714 if ((NULL == pTransceiveInfo) || (NULL == pMappedTranscvIf)) {
1715 return NFCSTATUS_FAILED;
1716 }
1717 switch (RemDevType) {
1718 case phNciNfc_eMifare1k_PICC:
1719 case phNciNfc_eMifare4k_PICC: {
1720 status = phLibNfc_MifareMap(pTransceiveInfo, pMappedTranscvIf);
1721 break;
1722 }
1723 default: { break; }
1724 }
1725
1726 return status;
1727 }
1728
1729 /*******************************************************************************
1730 **
1731 ** Function phLibNfc_SendAuthCmd
1732 **
1733 ** Description This function Send authentication command to NFCC
1734 **
1735 ** Returns NFCSTATUS_SUCCESS - Parameters are proper
1736 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1737 **
1738 *******************************************************************************/
phLibNfc_SendAuthCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNciNfc_TransceiveInfo_t * tNciTranscvInfo)1739 static NFCSTATUS phLibNfc_SendAuthCmd(
1740 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1741 phNciNfc_TransceiveInfo_t* tNciTranscvInfo) {
1742 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1743
1744 wStatus = phLibNfc_MapCmds(phNciNfc_eMifare1k_PICC, pTransceiveInfo,
1745 tNciTranscvInfo);
1746
1747 return wStatus;
1748 }
1749
1750 /*******************************************************************************
1751 **
1752 ** Function phLibNfc_SendWrt16Cmd
1753 **
1754 ** Description This function maps Mifarewirte16 commands
1755 **
1756 ** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1757 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1758 **
1759 *******************************************************************************/
phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1760 static NFCSTATUS phLibNfc_SendWrt16Cmd(
1761 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1762 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1763 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1764 uint8_t bBuffIdx = 0x00;
1765
1766 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1767 (0 != (pTransceiveInfo->sSendData.length))) {
1768 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1769 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1770 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1771 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1772 } else {
1773 wStatus = NFCSTATUS_INVALID_PARAMETER;
1774 }
1775
1776 return wStatus;
1777 }
1778
1779 /*******************************************************************************
1780 **
1781 ** Function phLibNfc_SendIncDecCmd
1782 **
1783 ** Description This function prepares the Increment/Decrement command
1784 ** to be sent, increment/decrement value is sent separately
1785 **
1786 ** Returns NFCSTATUS_SUCCESS - Params are mapped
1787 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1788 **
1789 *******************************************************************************/
phLibNfc_SendIncDecCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf,uint8_t IncDecCmd)1790 static NFCSTATUS phLibNfc_SendIncDecCmd(
1791 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1792 pphNciNfc_TransceiveInfo_t pMappedTranscvIf, uint8_t IncDecCmd) {
1793 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1794 uint8_t bBuffIdx = 0x00;
1795
1796 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1797 (0 != (pTransceiveInfo->sSendData.length))) {
1798 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = IncDecCmd;
1799 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1800 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1801 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1802 } else {
1803 wStatus = NFCSTATUS_INVALID_PARAMETER;
1804 }
1805
1806 return wStatus;
1807 }
1808
1809 /*******************************************************************************
1810 **
1811 ** Function phLibNfc_SendRawCmd
1812 **
1813 ** Description This function maps Mifare raw command
1814 **
1815 ** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1816 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1817 **
1818 *******************************************************************************/
phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1819 static NFCSTATUS phLibNfc_SendRawCmd(
1820 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1821 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1822 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1823 // uint8_t bBuffIdx = 0x00; /*commented to
1824 // eliminate unused variable warning*/
1825
1826 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1827 (0 != (pTransceiveInfo->sSendData.length))) {
1828 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1829 (pTransceiveInfo->sSendData.length));
1830 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1831 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1832 } else {
1833 wStatus = NFCSTATUS_INVALID_PARAMETER;
1834 }
1835
1836 return wStatus;
1837 }
1838
1839 /*******************************************************************************
1840 **
1841 ** Function phFriNfc_ExtnsTransceive
1842 **
1843 ** Description This function maps Mifare raw command and send it to NFCC
1844 **
1845 ** Returns NFCSTATUS_PENDING - Operation successful
1846 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1847 **
1848 *******************************************************************************/
phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNfc_uCmdList_t Cmd,uint8_t * SendRecvBuf,uint16_t SendLength,uint16_t * SendRecvLength)1849 NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1850 phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf,
1851 uint16_t SendLength,
1852 uint16_t* SendRecvLength) {
1853 (void)SendRecvLength;
1854 NFCSTATUS status = NFCSTATUS_FAILED;
1855 uint8_t* buff = NULL;
1856 uint16_t buffSz = 0;
1857 uint8_t i = 0;
1858 uint32_t length = SendLength;
1859 uint8_t restore_payload[] = {
1860 0x00, 0x00, 0x00, 0x00,
1861 };
1862
1863 if (SendLength == 0) {
1864 android_errorWriteLog(0x534e4554, "132083376");
1865 return status;
1866 }
1867
1868 buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1869 if (NULL == buff) {
1870 return status;
1871 }
1872
1873 pTransceiveInfo->cmd = Cmd;
1874
1875 if ((Cmd.MfCmd == phNfc_eMifareAuthentA) ||
1876 (Cmd.MfCmd == phNfc_eMifareAuthentB)) {
1877 pTransceiveInfo->addr = SendRecvBuf[i++];
1878 pTransceiveInfo->sSendData.buffer[4] = SendRecvBuf[i++];
1879 pTransceiveInfo->sSendData.buffer[5] = SendRecvBuf[i++];
1880 pTransceiveInfo->sSendData.buffer[6] = SendRecvBuf[i++];
1881 pTransceiveInfo->sSendData.buffer[7] = SendRecvBuf[i++];
1882 pTransceiveInfo->sSendData.buffer[8] = SendRecvBuf[i++];
1883 pTransceiveInfo->sSendData.buffer[9] = SendRecvBuf[i++];
1884
1885 pTransceiveInfo->cmd.MfCmd = Cmd.MfCmd;
1886
1887 pTransceiveInfo->sSendData.length = length;
1888 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1889 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1890 } else if (Cmd.MfCmd == phNfc_eMifareWrite16) {
1891 pTransceiveInfo->addr = SendRecvBuf[i++];
1892 length = SendLength - i;
1893 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1894 pTransceiveInfo->sSendData.length = length;
1895 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1896
1897 gphNxpExtns_Context.writecmdFlag = true;
1898
1899 status = phLibNfc_SendWrt16Cmd(pTransceiveInfo, &tNciTranscvInfo);
1900 } else if ((Cmd.MfCmd == phNfc_eMifareInc) ||
1901 (Cmd.MfCmd == phNfc_eMifareDec)) {
1902 pTransceiveInfo->addr = SendRecvBuf[i++];
1903 length = SendLength - i;
1904 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1905 pTransceiveInfo->sSendData.length = length;
1906 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1907
1908 gphNxpExtns_Context.incrdecflag = true;
1909
1910 status =
1911 phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1912
1913 } else if (Cmd.MfCmd == phNfc_eMifareRestore) {
1914 pTransceiveInfo->addr = SendRecvBuf[i++];
1915 length = SendLength - i;
1916 memcpy(pTransceiveInfo->sSendData.buffer, &restore_payload[0],
1917 sizeof(restore_payload));
1918 pTransceiveInfo->sSendData.length = length + sizeof(restore_payload);
1919 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1920
1921 gphNxpExtns_Context.incrdecflag = true;
1922
1923 status =
1924 phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1925
1926 } else if ((Cmd.MfCmd == phNfc_eMifareRaw) ||
1927 (Cmd.MfCmd == phNfc_eMifareTransfer)) {
1928 pTransceiveInfo->cmd.MfCmd = (phNfc_eMifareCmdList_t)phNciNfc_eT2TRaw;
1929 memcpy(pTransceiveInfo->sSendData.buffer, SendRecvBuf, length);
1930 pTransceiveInfo->sSendData.length = length;
1931 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1932 status = phLibNfc_SendRawCmd(pTransceiveInfo, &tNciTranscvInfo);
1933 } else {
1934 pTransceiveInfo->addr = SendRecvBuf[i++];
1935 length = SendLength - i;
1936 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1937 pTransceiveInfo->sSendData.length = length;
1938 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1939 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1940 }
1941
1942 if (NFCSTATUS_SUCCESS == status) {
1943 status = phNciNfc_SendMfReq(tNciTranscvInfo, buff, &buffSz);
1944 if (NFCSTATUS_PENDING != status) {
1945 LOG(ERROR) << StringPrintf("ERROR : phNciNfc_SendMfReq()");
1946 }
1947 } else {
1948 LOG(ERROR) << StringPrintf(" ERROR : Sending phNciNfc_SendMfReq");
1949 }
1950 if (buff != NULL) {
1951 free(buff);
1952 buff = NULL;
1953 }
1954
1955 return status;
1956 }
1957