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 if (2 > RspBuffInfo->wLen) {
1136 android_errorWriteLog(0x534e4554, "181346550");
1137 return NFCSTATUS_FAILED;
1138 }
1139 uint8_t rspAck = RspBuffInfo->pBuff[RspBuffInfo->wLen - 2];
1140 uint8_t rspAckMask = ((RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]) &
1141 MAX_NUM_VALID_BITS_FOR_ACK);
1142 NCI_CALCULATE_ACK(rspAck, rspAckMask);
1143 writeResponse =
1144 (rspAck == T2T_RSP_ACK) ? NFCSTATUS_SUCCESS : NFC_STATUS_FAILED;
1145 } else {
1146 writeResponse = RspBuffInfo->pBuff[RspBuffInfo->wLen - 1];
1147 }
1148 if (PH_NCINFC_STATUS_OK == writeResponse) {
1149 status = NFCSTATUS_SUCCESS;
1150 uint16_t wRecvDataSz = 0;
1151
1152 if ((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) >
1153 RspBuffInfo->wLen) {
1154 android_errorWriteLog(0x534e4554, "181346550");
1155 return NFCSTATUS_FAILED;
1156 }
1157 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1158 wPldDataSize = ((RspBuffInfo->wLen) -
1159 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1160 wRecvDataSz = NCI_MAX_DATA_LEN;
1161
1162 /* wPldDataSize = wPldDataSize-1; ==> ignoring the last status byte
1163 * appended with data */
1164 if ((wPldDataSize) <= wRecvDataSz) {
1165 /* Extract the data part from pBuff[2] & fill it to be sent to
1166 * upper layer */
1167 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[1]),
1168 (wPldDataSize));
1169 /* update the number of bytes received from lower layer,excluding
1170 * the status byte */
1171 *(NdefMap->SendRecvLength) = wPldDataSize;
1172 } else {
1173 // TODO:- Map some status for remaining extra data received to be
1174 // sent back to caller??
1175 status = NFCSTATUS_FAILED;
1176 }
1177 } else {
1178 status = NFCSTATUS_FAILED;
1179 }
1180 } break;
1181
1182 case phNciNfc_e_MfcAuthRsp: {
1183 /* check the status byte */
1184 if (PH_NCINFC_STATUS_OK == RspBuffInfo->pBuff[1]) {
1185 if (gAuthCmdBuf.auth_sent == true) {
1186 MfcPresenceCheckResult(NFCSTATUS_SUCCESS);
1187 return NFCSTATUS_SUCCESS;
1188 }
1189 gAuthCmdBuf.auth_status = true;
1190 status = NFCSTATUS_SUCCESS;
1191 if ((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) >
1192 RspBuffInfo->wLen) {
1193 android_errorWriteLog(0x534e4554, "126204073");
1194 return NFCSTATUS_FAILED;
1195 }
1196 /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */
1197 wPldDataSize = ((RspBuffInfo->wLen) -
1198 (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE));
1199
1200 /* Extract the data part from pBuff[2] & fill it to be sent to upper
1201 * layer */
1202 memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[2]),
1203 wPldDataSize);
1204 /* update the number of bytes received from lower layer,excluding
1205 * the status byte */
1206 *(NdefMap->SendRecvLength) = wPldDataSize;
1207 } else {
1208 if (gAuthCmdBuf.auth_sent == true) {
1209 gAuthCmdBuf.auth_status = false;
1210 MfcPresenceCheckResult(NFCSTATUS_FAILED);
1211 return NFCSTATUS_SUCCESS;
1212 } else {
1213 /* Reset the stored auth command buffer */
1214 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1215 gAuthCmdBuf.pauth_cmd->length = 0;
1216 gAuthCmdBuf.auth_status = false;
1217 }
1218 status = NFCSTATUS_FAILED;
1219 }
1220 } break;
1221
1222 default: { status = NFCSTATUS_FAILED; } break;
1223 }
1224 }
1225 }
1226
1227 return status;
1228 }
1229
1230 /*******************************************************************************
1231 **
1232 ** Function phLibNfc_SendWrt16CmdPayload
1233 **
1234 ** Description This function map the raw write cmd
1235 **
1236 ** Returns NFCSTATUS_SUCCESS - Command framing done
1237 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1238 **
1239 *******************************************************************************/
phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1240 static NFCSTATUS phLibNfc_SendWrt16CmdPayload(
1241 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1242 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1243 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1244
1245 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1246 (0 != (pTransceiveInfo->sSendData.length))) {
1247 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1248 (pTransceiveInfo->sSendData.length));
1249 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1250 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1251 } else {
1252 wStatus = NFCSTATUS_INVALID_PARAMETER;
1253 }
1254
1255 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1256 EXTNS_SetCallBackFlag(true);
1257 gphNxpExtns_Context.RawWriteCallBack = false;
1258 }
1259
1260 return wStatus;
1261 }
1262
1263 /*******************************************************************************
1264 **
1265 ** Function phLibNfc_SendIncDecCmdPayload
1266 **
1267 ** Description This function prepares the Increment/Decrement Value to be
1268 ** sent. This is called after sending the Increment/Decrement
1269 ** command is already sent and successfull
1270 **
1271 ** Returns NFCSTATUS_SUCCESS - Payload framing done
1272 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1273 **
1274 *******************************************************************************/
phLibNfc_SendIncDecCmdPayload(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1275 static NFCSTATUS phLibNfc_SendIncDecCmdPayload(
1276 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1277 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1278 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1279
1280 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1281 (0 != (pTransceiveInfo->sSendData.length))) {
1282 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1283 (pTransceiveInfo->sSendData.length));
1284 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1285 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1286 } else {
1287 wStatus = NFCSTATUS_INVALID_PARAMETER;
1288 }
1289
1290 if (gphNxpExtns_Context.RawWriteCallBack == true) {
1291 EXTNS_SetCallBackFlag(true);
1292 gphNxpExtns_Context.RawWriteCallBack = false;
1293 }
1294
1295 return wStatus;
1296 }
1297
1298 /*******************************************************************************
1299 **
1300 ** Function Mfc_RecvPacket
1301 **
1302 ** Description Decodes Mifare Classic Tag Response
1303 ** This is called from NFA_SendRaw Callback
1304 **
1305 ** Returns:
1306 ** NFCSTATUS_SUCCESS - if successfully initiated
1307 ** NFCSTATUS_FAILED - otherwise
1308 **
1309 *******************************************************************************/
Mfc_RecvPacket(uint8_t * buff,uint8_t buffSz)1310 NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz) {
1311 NFCSTATUS status = NFCSTATUS_SUCCESS;
1312 phNciNfc_Buff_t RspBuff;
1313 uint8_t* pcmd_buff;
1314 uint16_t buffSize;
1315
1316 RspBuff.pBuff = buff;
1317 RspBuff.wLen = buffSz;
1318 status = phNciNfc_RecvMfResp(&RspBuff, status);
1319 if (true == gAuthCmdBuf.auth_sent) {
1320 DLOG_IF(INFO, nfc_debug_enabled)
1321 << StringPrintf("%s Mfc Check Presence in progress", __func__);
1322 gAuthCmdBuf.auth_sent = false;
1323 return status;
1324 }
1325 if (true == gphNxpExtns_Context.writecmdFlag &&
1326 (NFCSTATUS_SUCCESS == status)) {
1327 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1328 if (NULL == pcmd_buff) {
1329 return NFCSTATUS_FAILED;
1330 }
1331 buffSize = MAX_BUFF_SIZE;
1332 gphNxpExtns_Context.writecmdFlag = false;
1333 phLibNfc_SendWrt16CmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1334 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1335 if (NFCSTATUS_PENDING != status) {
1336 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1337 } else {
1338 status = NFCSTATUS_SUCCESS;
1339 }
1340 if (pcmd_buff != NULL) {
1341 free(pcmd_buff);
1342 pcmd_buff = NULL;
1343 }
1344 } else if (true == gphNxpExtns_Context.incrdecflag &&
1345 (NFCSTATUS_SUCCESS == status)) {
1346 pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1347 if (NULL == pcmd_buff) {
1348 return NFCSTATUS_FAILED;
1349 }
1350 buffSize = MAX_BUFF_SIZE;
1351 gphNxpExtns_Context.incrdecflag = false;
1352 phLibNfc_SendIncDecCmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo);
1353 status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize);
1354 if (NFCSTATUS_PENDING != status) {
1355 LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status);
1356 } else {
1357 status = NFCSTATUS_SUCCESS;
1358 }
1359 gphNxpExtns_Context.incrdecstatusflag = true;
1360 if (pcmd_buff != NULL) {
1361 free(pcmd_buff);
1362 pcmd_buff = NULL;
1363 }
1364
1365 } else {
1366 if (gphNxpExtns_Context.CallBackMifare != NULL) {
1367 if ((gphNxpExtns_Context.incrdecstatusflag == true) && status == 0xB2) {
1368 gphNxpExtns_Context.incrdecstatusflag = false;
1369 status = NFCSTATUS_SUCCESS;
1370 }
1371 gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt,
1372 status);
1373 }
1374 }
1375
1376 return status;
1377 }
1378
1379 /*******************************************************************************
1380 **
1381 ** Function phNciNfc_MfCreateXchgDataHdr
1382 **
1383 ** Description This function builds the payload header for mifare XchgData
1384 ** request to be sent to NFCC.
1385 **
1386 ** Returns NFCSTATUS_PENDING - Command framing done
1387 ** NFCSTATUS_FAILED - Otherwise
1388 **
1389 *******************************************************************************/
phNciNfc_MfCreateXchgDataHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1390 static NFCSTATUS phNciNfc_MfCreateXchgDataHdr(
1391 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz)
1392
1393 {
1394 NFCSTATUS status = NFCSTATUS_SUCCESS;
1395 uint8_t i = 0;
1396
1397 buff[i++] = phNciNfc_e_MfRawDataXchgHdr;
1398 memcpy(&buff[i], tTranscvInfo.tSendData.pBuff, tTranscvInfo.tSendData.wLen);
1399 *buffSz = i + tTranscvInfo.tSendData.wLen;
1400
1401 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1402
1403 return status;
1404 }
1405
1406 /*******************************************************************************
1407 **
1408 ** Function phNciNfc_MfCreateAuthCmdHdr
1409 **
1410 ** Description This function builds the payload header for mifare
1411 ** classic Authenticate command to be sent to NFCC.
1412 **
1413 ** Returns NFCSTATUS_PENDING - Command framing done
1414 ** NFCSTATUS_FAILED - Otherwise
1415 **
1416 *******************************************************************************/
phNciNfc_MfCreateAuthCmdHdr(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t bBlockAddr,uint8_t * buff,uint16_t * buffSz)1417 static NFCSTATUS phNciNfc_MfCreateAuthCmdHdr(
1418 phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff,
1419 uint16_t* buffSz) {
1420 NFCSTATUS status = NFCSTATUS_SUCCESS;
1421 // pphNciNfc_RemoteDevInformation_t pActivDev = NULL;
1422 // /*commented to eliminate unused variable warning*/
1423 uint8_t bKey = 0x00;
1424
1425 /*No need to check range of block address*/
1426 /*To check for Authenticate A or Authenticate B type command*/
1427 if (PHNCINFC_AUTHENTICATION_KEYB == tTranscvInfo.tSendData.pBuff[0]) {
1428 bKey = bKey | PHNCINFC_ENABLE_KEY_B;
1429 }
1430
1431 /*TO Do last 4 bits of Key to be set based of firmware implementation*/
1432 /*this value is hardcoded but based on firmware implementation change this
1433 * value*/
1434 bKey = (bKey | PHNCINFC_AUTHENTICATION_KEY);
1435
1436 bKey |= tTranscvInfo.tSendData.pBuff[2];
1437
1438 /*For authentication extension no need to copy tSendData buffer of
1439 * tTranscvInfo */
1440 tTranscvInfo.tSendData.wLen = 0x00;
1441
1442 buff[0] = phNciNfc_e_MfcAuthReq;
1443 buff[1] = bBlockAddr;
1444 buff[2] = bKey;
1445
1446 *buffSz = 0x03;
1447 if (bKey & PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY) {
1448 memcpy(&buff[3], &tTranscvInfo.tSendData.pBuff[3], PHLIBNFC_MFC_AUTHKEYLEN);
1449 *buffSz += PHLIBNFC_MFC_AUTHKEYLEN;
1450 }
1451 /* Store the auth command buffer to use further for presence check */
1452 if (gAuthCmdBuf.pauth_cmd != NULL) {
1453 memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN);
1454 gAuthCmdBuf.pauth_cmd->length = *buffSz;
1455 memcpy(gAuthCmdBuf.pauth_cmd->buffer, buff, *buffSz);
1456 }
1457 status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz);
1458
1459 return status;
1460 }
1461
1462 /*******************************************************************************
1463 **
1464 ** Function phNciNfc_SendMfReq
1465 **
1466 ** Description This function shall be invoked as part of ReaderMgmt data
1467 ** exchange sequence handler.
1468 ** It shall send the request packet to NFCC.
1469 **
1470 ** Returns NFCSTATUS_PENDING - Send request is Pending
1471 ** NFCSTATUS_FAILED - otherwise
1472 **
1473 *******************************************************************************/
phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,uint8_t * buff,uint16_t * buffSz)1474 static NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo,
1475 uint8_t* buff, uint16_t* buffSz) {
1476 NFCSTATUS status = NFCSTATUS_SUCCESS;
1477
1478 switch (tTranscvInfo.uCmd.T2TCmd) {
1479 case phNciNfc_eT2TRaw: {
1480 status = phNciNfc_MfCreateXchgDataHdr(tTranscvInfo, buff, buffSz);
1481 } break;
1482 case phNciNfc_eT2TAuth: {
1483 status = phNciNfc_MfCreateAuthCmdHdr(tTranscvInfo, (tTranscvInfo.bAddr),
1484 buff, buffSz);
1485 } break;
1486 default: {
1487 status = NFCSTATUS_FAILED;
1488 break;
1489 }
1490 }
1491
1492 return status;
1493 }
1494
1495 /*******************************************************************************
1496 **
1497 ** Function phLibNfc_CalSectorAddress
1498 **
1499 ** Description This function update the sector address for Mifare classic
1500 **
1501 ** Returns none
1502 **
1503 *******************************************************************************/
phLibNfc_CalSectorAddress(uint8_t * Sector_Address)1504 static void phLibNfc_CalSectorAddress(uint8_t* Sector_Address) {
1505 uint8_t BlockNumber = 0x00;
1506
1507 if (NULL != Sector_Address) {
1508 BlockNumber = *Sector_Address;
1509 if (BlockNumber >= PHLIBNFC_MIFARESTD4K_BLK128) {
1510 *Sector_Address = (uint8_t)(PHLIBNFC_MIFARESTD_SECTOR_NO32 +
1511 ((BlockNumber - PHLIBNFC_MIFARESTD4K_BLK128) /
1512 PHLIBNFC_MIFARESTD_BLOCK_BYTES));
1513 } else {
1514 *Sector_Address = BlockNumber / PHLIBNFC_NO_OF_BLKPERSECTOR;
1515 }
1516 } else {
1517 }
1518
1519 return;
1520 }
1521
1522 /*******************************************************************************
1523 **
1524 ** Function phLibNfc_GetKeyNumberMFC
1525 **
1526 ** Description This function find key number based on authentication
1527 *command
1528 **
1529 ** Returns NFCSTATUS_SUCCESS - If found the key number
1530 ** NFCSTATUS_FAILED - otherwise
1531 **
1532 *******************************************************************************/
phLibNfc_GetKeyNumberMFC(uint8_t * buffer,uint8_t * bKey)1533 static NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey) {
1534 int32_t sdwStat = 0X00;
1535 NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER;
1536
1537 uint8_t bIndex = 0x00;
1538 uint8_t bNoOfKeys = 0x00;
1539
1540 #if PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS
1541 uint8_t aMfc_keys[NXP_NUMBER_OF_MFC_KEYS][NXP_MFC_KEY_SIZE] = NXP_MFC_KEYS;
1542 #else
1543 uint8_t aMfc_keys[1][1] = {{0x00}};
1544 #endif
1545
1546 if (NULL != bKey && NULL != buffer) {
1547 bNoOfKeys = sizeof(aMfc_keys) / NXP_MFC_KEY_SIZE;
1548 /* Traverse through the keys stored to determine whether keys is preloaded
1549 * key */
1550 for (bIndex = 0; bIndex < bNoOfKeys; bIndex++) {
1551 /* Check passed key is NDEF key */
1552 sdwStat = memcmp(&buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], aMfc_keys[bIndex],
1553 PHLIBNFC_MFC_AUTHKEYLEN);
1554 if (!sdwStat) {
1555 LOG(ERROR) << StringPrintf(
1556 "Mifare : phLibNfc_GetKeyNumberMFC Key found");
1557 *bKey = bIndex;
1558 wStatus = NFCSTATUS_SUCCESS;
1559 break;
1560 }
1561 }
1562 LOG(ERROR) << StringPrintf(
1563 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x Key = 0x%x",
1564 wStatus, *bKey);
1565 } else {
1566 wStatus = NFCSTATUS_FAILED;
1567 LOG(ERROR) << StringPrintf(
1568 "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x", wStatus);
1569 }
1570
1571 return wStatus;
1572 }
1573
1574 /*******************************************************************************
1575 **
1576 ** Function phLibNfc_ChkAuthCmdMFC
1577 **
1578 ** Description This function Check Authentication command send is proper or
1579 *not
1580 **
1581 ** Returns NFCSTATUS_SUCCESS - Authenticate command is proper
1582 ** NFCSTATUS_FAILED - otherwise
1583 **
1584 *******************************************************************************/
phLibNfc_ChkAuthCmdMFC(phNfc_sTransceiveInfo_t * pTransceiveInfo,uint8_t * bKey)1585 static NFCSTATUS phLibNfc_ChkAuthCmdMFC(
1586 phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey) {
1587 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1588
1589 if (NULL != pTransceiveInfo && NULL != pTransceiveInfo->sSendData.buffer &&
1590 0 != pTransceiveInfo->sSendData.length && NULL != bKey) {
1591 if ((pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentA ||
1592 pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentB)) {
1593 wStatus =
1594 phLibNfc_GetKeyNumberMFC(pTransceiveInfo->sSendData.buffer, bKey);
1595 } else {
1596 wStatus = NFCSTATUS_FAILED;
1597 }
1598 } else {
1599 wStatus = NFCSTATUS_FAILED;
1600 }
1601 return wStatus;
1602 }
1603
1604 /*******************************************************************************
1605 **
1606 ** Function phLibNfc_MifareMap
1607 **
1608 ** Description Mifare Mapping Utility function
1609 **
1610 ** Returns NFCSTATUS_SUCCESS - Mapping is proper
1611 ** NFCSTATUS_INVALID_PARAMETER - otherwise
1612 **
1613 *******************************************************************************/
phLibNfc_MifareMap(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1614 static NFCSTATUS phLibNfc_MifareMap(
1615 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1616 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1617 NFCSTATUS status = NFCSTATUS_SUCCESS;
1618 uint8_t bBuffIdx = 0;
1619 uint8_t bSectorNumber;
1620 uint8_t bKey = 0;
1621
1622 switch (pTransceiveInfo->cmd.MfCmd) {
1623 case phNfc_eMifareRead16: {
1624 if ((NULL != pTransceiveInfo->sRecvData.buffer) &&
1625 (0 != (pTransceiveInfo->sRecvData.length))) {
1626 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareRead16;
1627 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1628 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1629 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1630 } else {
1631 status = NFCSTATUS_INVALID_PARAMETER;
1632 }
1633 } break;
1634
1635 case phNfc_eMifareWrite16: {
1636 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1637 (0 != (pTransceiveInfo->sSendData.length))) {
1638 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1639 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1640 memcpy(&(pMappedTranscvIf->tSendData.pBuff[bBuffIdx]),
1641 pTransceiveInfo->sSendData.buffer,
1642 (pTransceiveInfo->sSendData.length));
1643 pMappedTranscvIf->tSendData.wLen =
1644 bBuffIdx + pTransceiveInfo->sSendData.length;
1645 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1646 } else {
1647 status = NFCSTATUS_INVALID_PARAMETER;
1648 }
1649 } break;
1650
1651 case phNfc_eMifareAuthentA:
1652 case phNfc_eMifareAuthentB: {
1653 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1654 (0 != (pTransceiveInfo->sSendData.length)) &&
1655 (NULL != pTransceiveInfo->sRecvData.buffer) &&
1656 (0 != (pTransceiveInfo->sRecvData.length))) {
1657 status = phLibNfc_ChkAuthCmdMFC(pTransceiveInfo, &bKey);
1658 if (NFCSTATUS_FAILED != status) {
1659 bSectorNumber = pTransceiveInfo->addr;
1660 phLibNfc_CalSectorAddress(&bSectorNumber);
1661 /*For creating extension command header pTransceiveInfo's MfCmd get
1662 * used*/
1663 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] =
1664 pTransceiveInfo->cmd.MfCmd;
1665 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bSectorNumber;
1666 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TAuth;
1667 pMappedTranscvIf->bAddr = bSectorNumber;
1668 pMappedTranscvIf->bNumBlock = pTransceiveInfo->NumBlock;
1669 if (NFCSTATUS_SUCCESS == status) {
1670 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1671 (pMappedTranscvIf->tSendData.wLen) = (uint16_t)(bBuffIdx);
1672
1673 } else if (NFCSTATUS_INVALID_PARAMETER == status) {
1674 bKey = bKey | PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY;
1675 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey;
1676 memcpy(&pMappedTranscvIf->tSendData.pBuff[bBuffIdx],
1677 &pTransceiveInfo->sSendData
1678 .buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD],
1679 PHLIBNFC_MFC_AUTHKEYLEN);
1680
1681 (pMappedTranscvIf->tSendData.wLen) =
1682 (uint16_t)(bBuffIdx + PHLIBNFC_MFC_AUTHKEYLEN);
1683 status = NFCSTATUS_SUCCESS;
1684 } else {
1685 /* do nothing */
1686 }
1687 }
1688 } else {
1689 status = NFCSTATUS_INVALID_PARAMETER;
1690 }
1691 } break;
1692
1693 case phNfc_eMifareRaw: {
1694 } break;
1695
1696 default: {
1697 status = NFCSTATUS_INVALID_PARAMETER;
1698 break;
1699 }
1700 }
1701
1702 return status;
1703 }
1704
1705 /*******************************************************************************
1706 **
1707 ** Function phLibNfc_MapCmds
1708 **
1709 ** Description This function maps the command request from libnfc level to
1710 *nci level
1711 **
1712 ** Returns NFCSTATUS_SUCCESS - Mapping of command is
1713 *successful
1714 ** NFCSTATUS_INVALID_PARAMETER - One or more of the supplied
1715 ** parameters could not be interpreted properly
1716 **
1717 *******************************************************************************/
phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1718 static NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType,
1719 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1720 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1721 NFCSTATUS status = NFCSTATUS_SUCCESS;
1722
1723 if ((NULL == pTransceiveInfo) || (NULL == pMappedTranscvIf)) {
1724 return NFCSTATUS_FAILED;
1725 }
1726 switch (RemDevType) {
1727 case phNciNfc_eMifare1k_PICC:
1728 case phNciNfc_eMifare4k_PICC: {
1729 status = phLibNfc_MifareMap(pTransceiveInfo, pMappedTranscvIf);
1730 break;
1731 }
1732 default: { break; }
1733 }
1734
1735 return status;
1736 }
1737
1738 /*******************************************************************************
1739 **
1740 ** Function phLibNfc_SendAuthCmd
1741 **
1742 ** Description This function Send authentication command to NFCC
1743 **
1744 ** Returns NFCSTATUS_SUCCESS - Parameters are proper
1745 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1746 **
1747 *******************************************************************************/
phLibNfc_SendAuthCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNciNfc_TransceiveInfo_t * tNciTranscvInfo)1748 static NFCSTATUS phLibNfc_SendAuthCmd(
1749 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1750 phNciNfc_TransceiveInfo_t* tNciTranscvInfo) {
1751 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1752
1753 wStatus = phLibNfc_MapCmds(phNciNfc_eMifare1k_PICC, pTransceiveInfo,
1754 tNciTranscvInfo);
1755
1756 return wStatus;
1757 }
1758
1759 /*******************************************************************************
1760 **
1761 ** Function phLibNfc_SendWrt16Cmd
1762 **
1763 ** Description This function maps Mifarewirte16 commands
1764 **
1765 ** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1766 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1767 **
1768 *******************************************************************************/
phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1769 static NFCSTATUS phLibNfc_SendWrt16Cmd(
1770 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1771 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1772 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1773 uint8_t bBuffIdx = 0x00;
1774
1775 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1776 (0 != (pTransceiveInfo->sSendData.length))) {
1777 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16;
1778 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1779 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1780 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1781 } else {
1782 wStatus = NFCSTATUS_INVALID_PARAMETER;
1783 }
1784
1785 return wStatus;
1786 }
1787
1788 /*******************************************************************************
1789 **
1790 ** Function phLibNfc_SendIncDecCmd
1791 **
1792 ** Description This function prepares the Increment/Decrement command
1793 ** to be sent, increment/decrement value is sent separately
1794 **
1795 ** Returns NFCSTATUS_SUCCESS - Params are mapped
1796 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1797 **
1798 *******************************************************************************/
phLibNfc_SendIncDecCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf,uint8_t IncDecCmd)1799 static NFCSTATUS phLibNfc_SendIncDecCmd(
1800 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1801 pphNciNfc_TransceiveInfo_t pMappedTranscvIf, uint8_t IncDecCmd) {
1802 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1803 uint8_t bBuffIdx = 0x00;
1804
1805 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1806 (0 != (pTransceiveInfo->sSendData.length))) {
1807 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = IncDecCmd;
1808 pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr;
1809 pMappedTranscvIf->tSendData.wLen = bBuffIdx;
1810 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1811 } else {
1812 wStatus = NFCSTATUS_INVALID_PARAMETER;
1813 }
1814
1815 return wStatus;
1816 }
1817
1818 /*******************************************************************************
1819 **
1820 ** Function phLibNfc_SendRawCmd
1821 **
1822 ** Description This function maps Mifare raw command
1823 **
1824 ** Returns NFCSTATUS_SUCCESS - Parameters are mapped
1825 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1826 **
1827 *******************************************************************************/
phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t * pTransceiveInfo,pphNciNfc_TransceiveInfo_t pMappedTranscvIf)1828 static NFCSTATUS phLibNfc_SendRawCmd(
1829 phNfc_sTransceiveInfo_t* pTransceiveInfo,
1830 pphNciNfc_TransceiveInfo_t pMappedTranscvIf) {
1831 NFCSTATUS wStatus = NFCSTATUS_SUCCESS;
1832 // uint8_t bBuffIdx = 0x00; /*commented to
1833 // eliminate unused variable warning*/
1834
1835 if ((NULL != pTransceiveInfo->sSendData.buffer) &&
1836 (0 != (pTransceiveInfo->sSendData.length))) {
1837 memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer,
1838 (pTransceiveInfo->sSendData.length));
1839 pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length;
1840 pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw;
1841 } else {
1842 wStatus = NFCSTATUS_INVALID_PARAMETER;
1843 }
1844
1845 return wStatus;
1846 }
1847
1848 /*******************************************************************************
1849 **
1850 ** Function phFriNfc_ExtnsTransceive
1851 **
1852 ** Description This function maps Mifare raw command and send it to NFCC
1853 **
1854 ** Returns NFCSTATUS_PENDING - Operation successful
1855 ** NFCSTATUS_INVALID_PARAMETER - Otherwise
1856 **
1857 *******************************************************************************/
phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t * pTransceiveInfo,phNfc_uCmdList_t Cmd,uint8_t * SendRecvBuf,uint16_t SendLength,uint16_t * SendRecvLength)1858 NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo,
1859 phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf,
1860 uint16_t SendLength,
1861 uint16_t* SendRecvLength) {
1862 (void)SendRecvLength;
1863 NFCSTATUS status = NFCSTATUS_FAILED;
1864 uint8_t* buff = NULL;
1865 uint16_t buffSz = 0;
1866 uint8_t i = 0;
1867 uint32_t length = SendLength;
1868 uint8_t restore_payload[] = {
1869 0x00, 0x00, 0x00, 0x00,
1870 };
1871
1872 if (SendLength == 0) {
1873 android_errorWriteLog(0x534e4554, "132083376");
1874 return status;
1875 }
1876
1877 buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE);
1878 if (NULL == buff) {
1879 return status;
1880 }
1881
1882 pTransceiveInfo->cmd = Cmd;
1883
1884 if ((Cmd.MfCmd == phNfc_eMifareAuthentA) ||
1885 (Cmd.MfCmd == phNfc_eMifareAuthentB)) {
1886 pTransceiveInfo->addr = SendRecvBuf[i++];
1887 pTransceiveInfo->sSendData.buffer[4] = SendRecvBuf[i++];
1888 pTransceiveInfo->sSendData.buffer[5] = SendRecvBuf[i++];
1889 pTransceiveInfo->sSendData.buffer[6] = SendRecvBuf[i++];
1890 pTransceiveInfo->sSendData.buffer[7] = SendRecvBuf[i++];
1891 pTransceiveInfo->sSendData.buffer[8] = SendRecvBuf[i++];
1892 pTransceiveInfo->sSendData.buffer[9] = SendRecvBuf[i++];
1893
1894 pTransceiveInfo->cmd.MfCmd = Cmd.MfCmd;
1895
1896 pTransceiveInfo->sSendData.length = length;
1897 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1898 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1899 } else if (Cmd.MfCmd == phNfc_eMifareWrite16) {
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.writecmdFlag = true;
1907
1908 status = phLibNfc_SendWrt16Cmd(pTransceiveInfo, &tNciTranscvInfo);
1909 } else if ((Cmd.MfCmd == phNfc_eMifareInc) ||
1910 (Cmd.MfCmd == phNfc_eMifareDec)) {
1911 pTransceiveInfo->addr = SendRecvBuf[i++];
1912 length = SendLength - i;
1913 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1914 pTransceiveInfo->sSendData.length = length;
1915 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1916
1917 gphNxpExtns_Context.incrdecflag = true;
1918
1919 status =
1920 phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1921
1922 } else if (Cmd.MfCmd == phNfc_eMifareRestore) {
1923 pTransceiveInfo->addr = SendRecvBuf[i++];
1924 length = SendLength - i;
1925 memcpy(pTransceiveInfo->sSendData.buffer, &restore_payload[0],
1926 sizeof(restore_payload));
1927 pTransceiveInfo->sSendData.length = length + sizeof(restore_payload);
1928 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1929
1930 gphNxpExtns_Context.incrdecflag = true;
1931
1932 status =
1933 phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd);
1934
1935 } else if ((Cmd.MfCmd == phNfc_eMifareRaw) ||
1936 (Cmd.MfCmd == phNfc_eMifareTransfer)) {
1937 pTransceiveInfo->cmd.MfCmd = (phNfc_eMifareCmdList_t)phNciNfc_eT2TRaw;
1938 memcpy(pTransceiveInfo->sSendData.buffer, SendRecvBuf, length);
1939 pTransceiveInfo->sSendData.length = length;
1940 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1941 status = phLibNfc_SendRawCmd(pTransceiveInfo, &tNciTranscvInfo);
1942 } else {
1943 pTransceiveInfo->addr = SendRecvBuf[i++];
1944 length = SendLength - i;
1945 memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length);
1946 pTransceiveInfo->sSendData.length = length;
1947 pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE;
1948 status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo);
1949 }
1950
1951 if (NFCSTATUS_SUCCESS == status) {
1952 status = phNciNfc_SendMfReq(tNciTranscvInfo, buff, &buffSz);
1953 if (NFCSTATUS_PENDING != status) {
1954 LOG(ERROR) << StringPrintf("ERROR : phNciNfc_SendMfReq()");
1955 }
1956 } else {
1957 LOG(ERROR) << StringPrintf(" ERROR : Sending phNciNfc_SendMfReq");
1958 }
1959 if (buff != NULL) {
1960 free(buff);
1961 buff = NULL;
1962 }
1963
1964 return status;
1965 }
1966