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