1 /* 2 * Copyright (C) 2010 NXP Semiconductors 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 /*! 18 * \file phFriNfc_MifareUL.c 19 * \brief This component encapsulates read/write/check ndef/process functionalities, 20 * for the Mifare UL card. 21 * 22 * Project: NFC-FRI 23 * 24 * $Date: Wed Feb 17 15:18:08 2010 $ 25 * $Author: ing07385 $ 26 * $Revision: 1.36 $ 27 * $Aliases: NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $ 28 * 29 */ 30 31 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED 32 33 #include <phFriNfc_NdefMap.h> 34 #include <phFriNfc_MifareULMap.h> 35 #include <phFriNfc_MapTools.h> 36 #include <phFriNfc_OvrHal.h> 37 #include <phFriNfc.h> 38 39 40 /*! \ingroup grp_file_attributes 41 * \name NDEF Mapping 42 * 43 * File: \ref phFriNfc_MifareUL.c 44 * 45 */ 46 /*@{*/ 47 #define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.35 $" 48 #define PHFRINFCNDEFMAP_FILEALIASES "$Aliases: NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $" 49 /*@}*/ 50 /* Completion Helper */ 51 static void phFriNfc_MifareUL_H_Complete(phFriNfc_NdefMap_t *NdefMap, 52 NFCSTATUS Status); 53 54 /*! 55 * \brief \copydoc page_ovr Helper function for Mifare UL. This function reads 56 * a 16 bytes from the card. 57 */ 58 static NFCSTATUS phFriNfc_MfUL_H_Rd16Bytes( phFriNfc_NdefMap_t *NdefMap); 59 60 /*! 61 * \brief \copydoc page_ovr Helper function for Mifare UL. This function is 62 * to find NDEF TLV 63 */ 64 static NFCSTATUS phFriNfc_MfUL_H_findNDEFTLV(phFriNfc_NdefMap_t *NdefMap, 65 uint8_t *CRFlag); 66 67 /*! 68 * \brief \copydoc page_ovr Helper function for Mifare UL. This function is 69 * to check the completing the reading 16 bytes 70 */ 71 static NFCSTATUS phFriNfc_MfUL_H_Chk16Bytes(phFriNfc_NdefMap_t *NdefMap, 72 uint16_t TempLength); 73 74 75 /*! 76 * \brief \copydoc page_ovr Helper function for Mifare UL. This function is 77 * to read 16 bytes for the finding the ndef TLV 78 */ 79 static NFCSTATUS phFriNfc_MfUL_H_RdCardfindNdefTLV( phFriNfc_NdefMap_t *NdefMap, 80 uint8_t BlockNo); 81 82 /*! 83 * \brief \copydoc page_ovr Helper function for Mifare UL. This function is 84 * to check the remaining size of the 3 byte of length field in TLV 85 */ 86 static NFCSTATUS phFriNfc_MfUL_H_ChkRemainTLV(phFriNfc_NdefMap_t *NdefMap, 87 uint8_t *CRFlag); 88 89 /*! 90 * \brief \copydoc page_ovr Helper function for Mifare UL. This function is 91 * to byte and block number of the next TLV in the card and updating the 92 * remaining free space in the card 93 */ 94 static void phFriNfc_MfUL_H_UpdateLen(phFriNfc_NdefMap_t *NdefMap, 95 uint16_t DataLen); 96 97 /*! 98 * \brief \copydoc page_ovr Helper function for Mifare UL. Depending on the 99 * operation (read/write/check ndef), the next function is called 100 */ 101 static NFCSTATUS phFriNfc_MfUL_H_NxtOp(phFriNfc_NdefMap_t *NdefMap, 102 uint8_t *CRFlag); 103 104 /*! 105 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 106 * function is to copy the read bytes to the internal "ReadBuf" buffer 107 */ 108 static NFCSTATUS phFriNfc_MfUL_H_CopyRdBytes(phFriNfc_NdefMap_t *NdefMap); 109 110 /*! 111 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 112 * function is to copy the read bytes to the user buffer 113 */ 114 static NFCSTATUS phFriNfc_MfUL_H_CpDataToUserBuf(phFriNfc_NdefMap_t *NdefMap); 115 116 /*! 117 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 118 * function is to write 4 bytes to 1 block in the card 119 */ 120 static NFCSTATUS phFriNfc_MfUL_H_Wr4bytes(phFriNfc_NdefMap_t *NdefMap); 121 122 /*! 123 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 124 * function is to check the CC bytes in block 3 card 125 */ 126 static NFCSTATUS phFriNfc_MfUL_H_ChkCCBytes(phFriNfc_NdefMap_t *NdefMap); 127 128 /*! 129 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 130 * function is to read the TLVs and then start writing 131 */ 132 static NFCSTATUS phFriNfc_MfUL_H_RdBeforeWrite(phFriNfc_NdefMap_t *NdefMap); 133 134 /*! 135 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 136 * function is to call write operation after reading the NDEF TLV block 137 */ 138 static NFCSTATUS phFriNfc_MfUL_H_CallWrOp(phFriNfc_NdefMap_t *NdefMap); 139 140 /*! 141 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 142 * function is to process the written data 143 */ 144 static NFCSTATUS phFriNfc_MfUL_H_ProWrittenBytes(phFriNfc_NdefMap_t *NdefMap); 145 146 /*! 147 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 148 * function is to fill the send buffer before write 149 */ 150 static NFCSTATUS phFriNfc_MfUL_H_fillSendBufToWr(phFriNfc_NdefMap_t *NdefMap); 151 152 /*! 153 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 154 * function is to update the length L of the TLV 155 */ 156 static NFCSTATUS phFriNfc_MfUL_H_UpdateWrLen(phFriNfc_NdefMap_t *NdefMap); 157 158 /*! 159 * \brief \copydoc page_ovr Helper function for Mifare UL function. This 160 * function is to write the terminator TLV after writing all the bytes 161 */ 162 static NFCSTATUS phFriNfc_MfUL_H_WrTermTLV(phFriNfc_NdefMap_t *NdefMap); 163 164 #ifdef LOCK_BITS_CHECK_ENABLE 165 static 166 void 167 phFriNfc_MfUL_H_ChkLockBits ( 168 phFriNfc_NdefMap_t *NdefMap); 169 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 170 171 /*! 172 * \brief \copydoc select sector function for Mifare UL function. This 173 * function is to write the terminator TLV after writing all the bytes 174 */ 175 static NFCSTATUS phFriNfc_MfUL_H_SelectSector(phFriNfc_NdefMap_t *NdefMap, 176 uint8_t SectorNo, 177 uint8_t CmdNo, 178 uint8_t NextState); 179 180 181 182 static void phFriNfc_MfUL_H_UpdateCrc( uint8_t ch, 183 uint16_t *lpwCrc ); 184 185 static void phFriNfc_MfUL_H_ComputeCrc( int CRCType, 186 uint8_t *Data, 187 int Length, 188 uint8_t *TransmitFirst, 189 uint8_t *TransmitSecond 190 ); 191 192 static void 193 phFriNfc_MfUL_CalcByteNum(phFriNfc_NdefMap_t *NdefMap); 194 195 196 #define CRC_A 0 197 #define CRC_B 1 198 199 200 NFCSTATUS phFriNfc_MifareUL_H_Reset(phFriNfc_NdefMap_t *NdefMap) 201 { 202 NFCSTATUS Result = NFCSTATUS_SUCCESS; 203 204 if ( NdefMap == NULL) 205 { 206 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 207 } 208 else 209 { 210 /* TLV structure initialisation */ 211 NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_NDEFMAP_MFUL_VAL4; 212 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_NDEFMAP_MFUL_VAL0; 213 NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_NDEFMAP_MFUL_VAL0; 214 NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_NDEFMAP_MFUL_VAL0; 215 NdefMap->TLVStruct.prevLenByteValue = PH_FRINFC_NDEFMAP_MFUL_VAL0; 216 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 217 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 218 NdefMap->TLVStruct.ActualSize = PH_FRINFC_NDEFMAP_MFUL_VAL0; 219 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 220 NdefMap->TLVStruct.WrLenFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 221 222 /* Mifare UL container initialisation */ 223 NdefMap->MifareULContainer.ByteNumber = PH_FRINFC_NDEFMAP_MFUL_VAL0; 224 NdefMap->MifareULContainer.CRindex = PH_FRINFC_NDEFMAP_MFUL_VAL0; 225 NdefMap->MifareULContainer.CurrentSector = PH_FRINFC_NDEFMAP_MFUL_VAL0; 226 NdefMap->MifareULContainer.CurrentBlock = PH_FRINFC_NDEFMAP_MFUL_VAL0; 227 NdefMap->MifareULContainer.InternalLength = PH_FRINFC_NDEFMAP_MFUL_VAL0; 228 NdefMap->MifareULContainer.ReadBufIndex = PH_FRINFC_NDEFMAP_MFUL_VAL0; 229 NdefMap->MifareULContainer.ReadWriteCompleteFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 230 NdefMap->MifareULContainer.RemainingSize = PH_FRINFC_NDEFMAP_MFUL_VAL0; 231 232 /* Fill all the structure related buffer to ZERO */ 233 (void)memset(NdefMap->TLVStruct.NdefTLVBuffer, 234 PH_FRINFC_NDEFMAP_MFUL_VAL0, 235 PH_FRINFC_NDEFMAP_MFUL_VAL4); 236 (void)memset(NdefMap->MifareULContainer.Buffer, 237 PH_FRINFC_NDEFMAP_MFUL_VAL0, 238 PH_FRINFC_NDEFMAP_MFUL_VAL4); 239 (void)memset(NdefMap->MifareULContainer.InternalBuf, 240 PH_FRINFC_NDEFMAP_MFUL_VAL0, 241 PH_FRINFC_NDEFMAP_MFUL_VAL4); 242 (void)memset(NdefMap->MifareULContainer.ReadBuf, 243 PH_FRINFC_NDEFMAP_MFUL_VAL0, 244 PH_FRINFC_NDEFMAP_MFUL_VAL64); 245 } 246 return Result; 247 } 248 249 /*! 250 * \brief Initiates Reading of NDEF information from the Mifare UL. 251 * 252 * It performs a reset of the state and starts the action (state machine). 253 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action 254 * has been triggered. 255 */ 256 257 NFCSTATUS phFriNfc_MifareUL_RdNdef( phFriNfc_NdefMap_t *NdefMap, 258 uint8_t *PacketData, 259 uint32_t *PacketDataLength, 260 uint8_t Offset) 261 { 262 NFCSTATUS Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 263 NFCSTATUS_INVALID_PARAMETER); 264 265 if((NdefMap != NULL) && (PacketData != NULL) && (PacketDataLength != NULL) && 266 (*PacketDataLength != PH_FRINFC_NDEFMAP_MFUL_VAL0) && 267 (Offset <= PH_FRINFC_NDEFMAP_SEEK_BEGIN) && 268 (NdefMap->CompletionRoutine->CompletionRoutine != NULL) && 269 (NdefMap->CompletionRoutine->Context != NULL ) && 270 ((NdefMap->CardState != PH_NDEFMAP_CARD_STATE_INITIALIZED) && 271 (NdefMap->CardState != PH_NDEFMAP_CARD_STATE_INVALID))) 272 { 273 /*Register PacketData to Data Buffer of NdefMap */ 274 NdefMap->ApduBuffer = PacketData; 275 /*Register PacketDataLength to Data Length of NdefMap */ 276 NdefMap->ApduBufferSize = *PacketDataLength ; 277 /* To return actual number of bytes read to the caller */ 278 NdefMap->NumOfBytesRead = PacketDataLength ; 279 *NdefMap->NumOfBytesRead = 0; 280 281 if( (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || ( NdefMap->PrevOperation == 282 PH_FRINFC_NDEFMAP_WRITE_OPE)) 283 { 284 NdefMap->MifareULContainer.CurrentBlock = PH_FRINFC_NDEFMAP_MFUL_BLOCK4; 285 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 286 NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 287 NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_NDEFMAP_MFUL_BLOCK4; 288 NdefMap->MifareULContainer.RemainingSize = NdefMap->CardMemSize; 289 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 290 NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_NDEFMAP_MFUL_VAL0; 291 NdefMap->MifareULContainer.ReadBufIndex = PH_FRINFC_NDEFMAP_MFUL_VAL0; 292 NdefMap->MifareULContainer.ReadWriteCompleteFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 293 } 294 295 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE; 296 NdefMap->MifareULContainer.CRindex = PH_FRINFC_NDEFMAP_CR_RD_NDEF; 297 298 if( (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && 299 (NdefMap->MifareULContainer.ReadWriteCompleteFlag == 300 PH_FRINFC_NDEFMAP_MFUL_FLAG1)) 301 { 302 /* No space on card for reading : we have already 303 reached the end of file ! 304 Offset is set to Continue Operation */ 305 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 306 NFCSTATUS_EOF_NDEF_CONTAINER_REACHED); 307 } 308 else 309 { 310 NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) && 311 ( NdefMap->PrevOperation != PH_FRINFC_NDEFMAP_READ_OPE))? 312 PH_FRINFC_NDEFMAP_SEEK_BEGIN: 313 Offset); 314 315 Result = ((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN)? 316 phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, NdefMap->MifareULContainer.CurrentBlock): 317 phFriNfc_MfUL_H_CpDataToUserBuf(NdefMap)); 318 } 319 } 320 return Result; 321 } 322 323 324 /*! 325 * \brief Initiates writing of NDEF information to the Mifare UL. 326 * 327 * The function initiates the writing of NDEF information to a Mifare UL. 328 * It performs a reset of the state and starts the action (state machine). 329 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action 330 * has been triggered. 331 */ 332 333 NFCSTATUS phFriNfc_MifareUL_WrNdef( phFriNfc_NdefMap_t *NdefMap, 334 uint8_t *PacketData, 335 uint32_t *PacketDataLength, 336 uint8_t Offset) 337 { 338 NFCSTATUS Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 339 NFCSTATUS_INVALID_PARAMETER); 340 341 342 343 if((NdefMap != NULL) && (PacketData != NULL) && (PacketDataLength != NULL) && 344 (*PacketDataLength != PH_FRINFC_NDEFMAP_MFUL_VAL0) && 345 (Offset <= PH_FRINFC_NDEFMAP_SEEK_BEGIN) && 346 (NdefMap->CompletionRoutine->CompletionRoutine != NULL) && 347 (NdefMap->CompletionRoutine->Context != NULL ) && 348 ((NdefMap->CardState != PH_NDEFMAP_CARD_STATE_READ_ONLY) && 349 (NdefMap->CardState != PH_NDEFMAP_CARD_STATE_INVALID))) 350 { 351 NdefMap->MifareULContainer.CRindex = PH_FRINFC_NDEFMAP_CR_WR_NDEF; 352 /*Register PacketData to Data Buffer of NdefMap */ 353 NdefMap->ApduBuffer = PacketData; 354 /*Register PacketDataLength to Data Length of NdefMap */ 355 NdefMap->ApduBufferSize = *PacketDataLength ; 356 /* To return actual number of bytes read to the caller */ 357 NdefMap->WrNdefPacketLength = PacketDataLength ; 358 *NdefMap->WrNdefPacketLength = 0; 359 360 if( (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || ( NdefMap->PrevOperation == 361 PH_FRINFC_NDEFMAP_READ_OPE)) 362 { 363 NdefMap->MifareULContainer.CurrentSector = NdefMap->TLVStruct.NdefTLVSector; 364 NdefMap->MifareULContainer.CurrentBlock = NdefMap->TLVStruct.NdefTLVBlock; 365 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 366 NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_NDEFMAP_MFUL_VAL0; 367 NdefMap->MifareULContainer.ReadBufIndex = PH_FRINFC_NDEFMAP_MFUL_VAL0; 368 NdefMap->MifareULContainer.ReadWriteCompleteFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 369 NdefMap->TLVStruct.WrLenFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 370 NdefMap->MifareULContainer.InternalLength = PH_FRINFC_NDEFMAP_MFUL_VAL0; 371 NdefMap->MifareULContainer.RemainingSize = 372 (NdefMap->CardMemSize - 373 (((NdefMap->TLVStruct.NdefTLVBlock - 374 PH_FRINFC_NDEFMAP_MFUL_BYTE4) * 375 PH_FRINFC_NDEFMAP_MFUL_BYTE4) + 376 NdefMap->TLVStruct.NdefTLVByte + 377 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 378 } 379 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE; 380 NdefMap->MifareULContainer.CRindex = PH_FRINFC_NDEFMAP_CR_WR_NDEF; 381 382 if( (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && 383 (NdefMap->MifareULContainer.ReadWriteCompleteFlag == 384 PH_FRINFC_NDEFMAP_MFUL_FLAG1)) 385 { 386 /* No space on card for reading : we have already 387 reached the end of file ! 388 Offset is set to Continue Operation */ 389 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 390 NFCSTATUS_EOF_NDEF_CONTAINER_REACHED); 391 } 392 else 393 { 394 NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) && 395 ( NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE))? 396 PH_FRINFC_NDEFMAP_SEEK_BEGIN: 397 Offset); 398 399 if (NdefMap->TLVStruct.NdefTLVSector == 1) 400 { 401 NdefMap->MifareULContainer.CurrentSector = 1; 402 403 /* Change to sector 1 */ 404 Result = phFriNfc_MfUL_H_SelectSector(NdefMap, 405 NdefMap->MifareULContainer.CurrentSector, 1, 406 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_INIT_1); 407 408 } 409 else 410 { 411 Result = ((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN)? 412 phFriNfc_MfUL_H_RdBeforeWrite(NdefMap): 413 phFriNfc_MfUL_H_fillSendBufToWr(NdefMap)); 414 } 415 } 416 } 417 418 419 return Result; 420 } 421 422 423 424 /*! 425 * \brief Check whether a particular Mifare UL is NDEF compliant. 426 * 427 * The function checks whether the peer device is NDEF compliant. 428 * 429 */ 430 431 NFCSTATUS phFriNfc_MifareUL_ChkNdef( phFriNfc_NdefMap_t *NdefMap) 432 { 433 NFCSTATUS status = NFCSTATUS_PENDING; 434 uint8_t index=0, 435 pSensRes[2] = {0}; 436 437 /* set the data for additional data exchange*/ 438 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0; 439 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0; 440 NdefMap->psDepAdditionalInfo.NAD = 0; 441 442 /* 443 * Changed 444 * Description: CardInfo106 replase 445 */ 446 447 /* retrive remote card information */ 448 pSensRes[0] = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA[0]; 449 450 NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_NDEFMAP_MFUL_VAL4; 451 #ifdef LOCK_BITS_CHECK_ENABLE 452 NdefMap->MifareULContainer.CurrentBlock = PH_FRINFC_NDEFMAP_MFUL_BLOCK2; 453 #else 454 NdefMap->MifareULContainer.CurrentBlock = PH_FRINFC_NDEFMAP_MFUL_BLOCK3; 455 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 456 NdefMap->MifareULContainer.CRindex = PH_FRINFC_NDEFMAP_CR_CHK_NDEF; 457 458 /* Check for Mifare Bit information */ 459 if (((pSensRes[0] & PH_FRINFC_NDEFMAP_MFUL_CHECK_RESP) == PH_FRINFC_NDEFMAP_MFUL_CHECK_RESP)) 460 { 461 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_CHECK_OPE; 462 /* set the offset*/ 463 NdefMap->SendRecvBuf[index] = NdefMap->MifareULContainer.CurrentBlock; 464 465 /*set the send length*/ 466 NdefMap->SendLength = PH_FRINFC_NDEFMAP_MFUL_MAX_SEND_BUF_TO_READ; 467 468 /* Change the state to check ndef compliancy */ 469 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_CHK_NDEF_COMP; 470 471 /* Set the card type as Mifare UL */ 472 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_UL_CARD; 473 474 /* set the cmd to mifare read*/ 475 /* 476 * Changed 477 * Description: phHal_eMifareCmdListMifareRead replace with phHal_eMifareRead 478 */ 479 NdefMap->Cmd.MfCmd = phHal_eMifareRead; 480 481 /* Set the CR and context for Mifare operations*/ 482 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareUL_Process; 483 NdefMap->MapCompletionInfo.Context = NdefMap; 484 485 /*Set the Length*/ 486 *NdefMap->SendRecvLength = PH_FRINFC_NDEFMAP_MF_READ_BLOCK_SIZE; 487 488 489 /*Call the Overlapped HAL Transceive function */ 490 status = phFriNfc_OvrHal_Transceive(NdefMap->LowerDevice, 491 &NdefMap->MapCompletionInfo, 492 NdefMap->psRemoteDevInfo, 493 NdefMap->Cmd, 494 &NdefMap->psDepAdditionalInfo, 495 NdefMap->SendRecvBuf, 496 NdefMap->SendLength, 497 NdefMap->SendRecvBuf, 498 NdefMap->SendRecvLength); 499 } 500 else 501 { 502 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_REMOTE_DEVICE); 503 } 504 return status; 505 } 506 507 static void 508 phFriNfc_MfUL_CalcByteNum(phFriNfc_NdefMap_t *NdefMap) 509 { 510 uint8_t i = PH_FRINFC_NDEFMAP_MFUL_VAL0; 511 uint16_t TemLength = PH_FRINFC_NDEFMAP_MFUL_VAL0; 512 513 514 for (i = 0; i < 16; i++) 515 { 516 if ((NdefMap->MifareULContainer.ReadBuf[i] == 517 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_T) && 518 ((NdefMap->MifareULContainer.ReadBuf[i + 1] == 519 NdefMap->TLVStruct.ActualSize) || 520 (NdefMap->MifareULContainer.ReadBuf[i + 1] == 0xFF))) 521 { 522 if (NdefMap->MifareULContainer.ReadBuf[i + 1] == 0xFF) 523 { 524 TemLength = NdefMap->MifareULContainer.ReadBuf[i + 2] | 525 ((uint16_t)NdefMap->MifareULContainer.ReadBuf[i + 3] << 8); 526 527 if (TemLength == NdefMap->TLVStruct.ActualSize) 528 { 529 NdefMap->MifareULContainer.ByteNumber = i + 1; 530 break; 531 } 532 } 533 else 534 { 535 NdefMap->MifareULContainer.ByteNumber = i + 1; 536 break; 537 } 538 } 539 } 540 541 return; 542 } 543 544 545 #ifdef LOCK_BITS_CHECK_ENABLE 546 547 #define MIF_UL_LOCK_BIT_CHECK 0xFF 548 #define MIF_UL_LOCK_BIT_0_VALUE 0x0F 549 #define MIF_UL_LOCK_BIT_1_VALUE 0x00 550 551 static 552 void 553 phFriNfc_MfUL_H_ChkLockBits ( 554 phFriNfc_NdefMap_t *NdefMap) 555 { 556 uint8_t index = 2; 557 558 if (((NdefMap->SendRecvBuf[index] & 559 MIF_UL_LOCK_BIT_CHECK) > MIF_UL_LOCK_BIT_0_VALUE) || 560 (MIF_UL_LOCK_BIT_1_VALUE != 561 (NdefMap->SendRecvBuf[(index + 1)] & MIF_UL_LOCK_BIT_CHECK))) 562 { 563 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_ONLY; 564 } 565 } 566 567 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 568 569 /*! 570 * \brief Completion Routine, Processing function, needed to avoid long 571 * blocking. 572 * \note The lower (Overlapped HAL) layer must register a pointer to 573 * this function as a Completion 574 * Routine in order to be able to notify the component that an I/O 575 * has finished and data are ready to be processed. 576 * 577 */ 578 579 void phFriNfc_MifareUL_Process( void *Context, 580 NFCSTATUS Status) 581 582 { 583 584 uint8_t index = PH_FRINFC_NDEFMAP_MFUL_VAL0, 585 i = PH_FRINFC_NDEFMAP_MFUL_VAL0; 586 phFriNfc_NdefMap_t *NdefMap; 587 uint16_t TemLength = PH_FRINFC_NDEFMAP_MFUL_VAL0; 588 /*uint16_t TempByte = PH_FRINFC_NDEFMAP_MFUL_VAL0; */ 589 static uint8_t CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 590 591 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 592 593 /* set the context to Map module */ 594 595 NdefMap = (phFriNfc_NdefMap_t *)Context; 596 597 if ( Status == NFCSTATUS_SUCCESS ) 598 { 599 switch (NdefMap->State) 600 { 601 case PH_FRINFC_NDEFMAP_MFUL_STATE_CHK_NDEF_COMP: 602 if (*NdefMap->SendRecvLength == 603 PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16) 604 { 605 /* Checks for the Ndef Compliency and validy of the memory size*/ 606 Status = phFriNfc_MfUL_H_ChkCCBytes(NdefMap); 607 CRFlag = (uint8_t)((Status != NFCSTATUS_SUCCESS)? 608 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 609 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 610 611 #ifdef LOCK_BITS_CHECK_ENABLE 612 613 /* Check for lock bits */ 614 if (NFCSTATUS_SUCCESS == Status) 615 { 616 phFriNfc_MfUL_H_ChkLockBits(NdefMap); 617 } 618 619 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 620 621 /* Find the NDEF TLV */ 622 NdefMap->MifareULContainer.CurrentBlock = PH_FRINFC_NDEFMAP_MFUL_BLOCK4; 623 Status = ((Status != NFCSTATUS_SUCCESS)? 624 Status: 625 phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 626 NdefMap->MifareULContainer.CurrentBlock)); 627 CRFlag = (uint8_t)(((Status != NFCSTATUS_PENDING ) || 628 (CRFlag == PH_FRINFC_NDEFMAP_MFUL_FLAG1))? 629 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 630 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 631 632 #ifdef PH_HAL4_ENABLE 633 if ((Status != NFCSTATUS_PENDING ) && 634 (Status != NFCSTATUS_SUCCESS)) 635 { 636 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 637 } 638 #endif /* #ifdef PH_HAL4_ENABLE */ 639 } 640 break; 641 642 643 case PH_FRINFC_NDEFMAP_MFUL_STATE_READ: 644 /* check the received bytes size equals 16 bytes*/ 645 if (*NdefMap->SendRecvLength == 646 PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16) 647 { 648 if(NdefMap->MifareULContainer.ReadBufIndex < 649 (NdefMap->TLVStruct.ActualSize + (((NdefMap->TLVStruct.NdefTLVBlock - 650 PH_FRINFC_NDEFMAP_MFUL_BLOCK4) 651 * PH_FRINFC_NDEFMAP_MFUL_VAL4) + (NdefMap->TLVStruct.NdefTLVByte - 1) + 4))) 652 653 { 654 Status = phFriNfc_MfUL_H_CopyRdBytes(NdefMap); 655 } 656 657 if (Status == NFCSTATUS_SUCCESS) 658 { 659 if(NdefMap->MifareULContainer.ReadBufIndex >= 660 (NdefMap->TLVStruct.ActualSize + (((NdefMap->TLVStruct.NdefTLVBlock - 661 PH_FRINFC_NDEFMAP_MFUL_BLOCK4) 662 * PH_FRINFC_NDEFMAP_MFUL_VAL4) + (NdefMap->TLVStruct.NdefTLVByte - 1) + 4))) 663 { 664 665 phFriNfc_MfUL_CalcByteNum(NdefMap); 666 #if 0 667 for (i = 0; i < 16; i++) 668 { 669 if ((NdefMap->MifareULContainer.ReadBuf[i] == 670 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_T) && 671 ((NdefMap->MifareULContainer.ReadBuf[i + 1] == 672 NdefMap->TLVStruct.ActualSize) || 673 (NdefMap->MifareULContainer.ReadBuf[i + 1] == 0xFF))) 674 { 675 if (NdefMap->MifareULContainer.ReadBuf[i + 1] == 0xFF) 676 { 677 TemLength = NdefMap->MifareULContainer.ReadBuf[i + 2] | 678 ((uint16_t)NdefMap->MifareULContainer.ReadBuf[i + 3] << 8); 679 680 if (TemLength == NdefMap->TLVStruct.ActualSize) 681 { 682 NdefMap->MifareULContainer.ByteNumber = i + 1; 683 break; 684 } 685 } 686 else 687 { 688 NdefMap->MifareULContainer.ByteNumber = i + 1; 689 break; 690 } 691 } 692 } 693 #endif 694 695 if (NdefMap->MifareULContainer.ReadBuf 696 [NdefMap->MifareULContainer.ByteNumber] == 0xFF) 697 { 698 NdefMap->MifareULContainer.ByteNumber = 699 NdefMap->MifareULContainer.ByteNumber + 3; 700 } 701 else 702 { 703 NdefMap->MifareULContainer.ByteNumber = 704 NdefMap->MifareULContainer.ByteNumber + 1; 705 } 706 707 Status = phFriNfc_MfUL_H_CpDataToUserBuf(NdefMap); 708 if (NdefMap->MifareULContainer.CurrentSector > 0) 709 { 710 NdefMap->MifareULContainer.CurrentSector = 0; 711 NdefMap->PrevState = PH_FRINFC_NDEFMAP_MFUL_STATE_READ; 712 713 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 714 NdefMap->MifareULContainer.CurrentSector, 1, 715 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RESET_1); 716 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING )? 717 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 718 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 719 } 720 else 721 { 722 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 723 } 724 } 725 else 726 { 727 Status = phFriNfc_MfUL_H_Rd16Bytes(NdefMap); 728 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 729 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 730 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 731 } 732 } 733 else 734 { 735 736 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 737 } 738 } 739 else 740 { 741 /* read error */ 742 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 743 NFCSTATUS_INVALID_RECEIVE_LENGTH); 744 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 745 } 746 break; 747 748 case PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE: 749 Status = phFriNfc_MfUL_H_ProWrittenBytes(NdefMap); 750 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 751 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 752 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 753 break; 754 755 case PH_FRINFC_NDEFMAP_MFUL_STATE_FND_NDEF_COMP: 756 if (*NdefMap->SendRecvLength == 757 PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16) 758 { 759 switch(NdefMap->PrevOperation) 760 { 761 case PH_FRINFC_NDEFMAP_CHECK_OPE: 762 case PH_FRINFC_NDEFMAP_READ_OPE: 763 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 764 if(NdefMap->TLVStruct.NoLbytesinTLV > 765 PH_FRINFC_NDEFMAP_MFUL_VAL0) 766 { 767 Status = phFriNfc_MfUL_H_ChkRemainTLV(NdefMap, &CRFlag); 768 } 769 else 770 { 771 if(NdefMap->TLVStruct.NdefTLVFoundFlag != 772 PH_FRINFC_NDEFMAP_MFUL_FLAG1) 773 { 774 /* Find the NDEF TLV */ 775 Status = phFriNfc_MfUL_H_findNDEFTLV(NdefMap, &CRFlag); 776 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING )? 777 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 778 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 779 } 780 } 781 if((NdefMap->TLVStruct.NdefTLVFoundFlag == 782 PH_FRINFC_NDEFMAP_MFUL_FLAG1) && 783 (NdefMap->TLVStruct.NoLbytesinTLV == 784 PH_FRINFC_NDEFMAP_MFUL_VAL0)) 785 { 786 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 787 /* Ndef TLV found, so call the next function depending on the 788 check/read/write ndef operation */ 789 790 if (NdefMap->MifareULContainer.CurrentSector > 0) 791 { 792 NdefMap->MifareULContainer.CurrentSector = 0; 793 NdefMap->PrevState = PH_FRINFC_NDEFMAP_MFUL_STATE_FND_NDEF_COMP; 794 795 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 796 NdefMap->MifareULContainer.CurrentSector, 1, 797 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RESET_1); 798 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING )? 799 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 800 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 801 } 802 else 803 { 804 /* Sector is 0 no need to send sector select */ 805 Status = phFriNfc_MfUL_H_NxtOp(NdefMap, &CRFlag); 806 } 807 } 808 809 #ifdef PH_HAL4_ENABLE 810 if ((Status != NFCSTATUS_PENDING ) && 811 (Status != NFCSTATUS_SUCCESS) && 812 (PH_FRINFC_NDEFMAP_CHECK_OPE == 813 NdefMap->PrevOperation)) 814 { 815 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 816 } 817 #endif /* #ifdef PH_HAL4_ENABLE */ 818 break; 819 820 case PH_FRINFC_NDEFMAP_WRITE_OPE: 821 /* Remove UpdateWrLen */ 822 Status = ((NdefMap->TLVStruct.WrLenFlag == 823 PH_FRINFC_NDEFMAP_MFUL_FLAG1)? 824 phFriNfc_MfUL_H_UpdateWrLen(NdefMap): 825 phFriNfc_MfUL_H_CallWrOp(NdefMap)); 826 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 827 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 828 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 829 break; 830 831 default: 832 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 833 NFCSTATUS_INVALID_REMOTE_DEVICE); 834 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 835 break; 836 837 } 838 } 839 break; 840 841 case PH_FRINFC_NDEFMAP_MFUL_STATE_TERM_TLV: 842 Status = phFriNfc_MfUL_H_UpdateWrLen(NdefMap); 843 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 844 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 845 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 846 break; 847 848 case PH_FRINFC_NDEFMAP_MFUL_STATE_WR_LEN_TLV: 849 if(((((NdefMap->TLVStruct.NdefTLVByte - 850 PH_FRINFC_NDEFMAP_MFUL_VAL1) == 851 PH_FRINFC_NDEFMAP_MFUL_VAL3) && 852 (NdefMap->MifareULContainer.CurrentBlock == 853 (NdefMap->TLVStruct.NdefTLVBlock + 854 PH_FRINFC_NDEFMAP_MFUL_VAL1))) || 855 (((NdefMap->TLVStruct.NdefTLVByte - 856 PH_FRINFC_NDEFMAP_MFUL_VAL1) < 857 PH_FRINFC_NDEFMAP_MFUL_VAL3) && ( 858 NdefMap->MifareULContainer.CurrentBlock == 859 NdefMap->TLVStruct.NdefTLVBlock)))) 860 { 861 (void)memcpy(NdefMap->MifareULContainer.InternalBuf, 862 NdefMap->MifareULContainer.Buffer, 863 NdefMap->MifareULContainer.InternalLength); 864 } 865 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer, 866 NdefMap->MifareULContainer.Buffer, 867 PH_FRINFC_NDEFMAP_MFUL_VAL4); 868 869 NdefMap->CardState =(uint8_t) ((NdefMap->CardState == 870 PH_NDEFMAP_CARD_STATE_INITIALIZED)? 871 PH_NDEFMAP_CARD_STATE_READ_WRITE: 872 NdefMap->CardState); 873 NdefMap->ApduBuffIndex = PH_FRINFC_NDEFMAP_MFUL_VAL0; 874 NdefMap->NumOfBytesWritten = PH_FRINFC_NDEFMAP_MFUL_VAL0; 875 876 if (NdefMap->MifareULContainer.CurrentSector > 0) 877 { 878 /* Reset sector */ 879 NdefMap->MifareULContainer.CurrentSector = 0; 880 NdefMap->PrevState = PH_FRINFC_NDEFMAP_MFUL_STATE_WR_LEN_TLV; 881 882 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 883 NdefMap->MifareULContainer.CurrentSector, 1, 884 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RESET_1); 885 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 886 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 887 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 888 } 889 else 890 { 891 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 892 } 893 894 break; 895 896 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_CHK_1: 897 /* check the received bytes size equals 1 byte*/ 898 if (*NdefMap->SendRecvLength == 899 PH_FRINFC_NDEFMAP_MFUL_VAL1) 900 { 901 if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] == 0x0A) 902 { 903 /* Send second command */ 904 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 905 NdefMap->MifareULContainer.CurrentSector, 2, 906 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_CHK_2); 907 } 908 else 909 { 910 /* read error */ 911 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 912 NFCSTATUS_INVALID_FORMAT); 913 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 914 } 915 } 916 else 917 { 918 /* read error */ 919 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 920 NFCSTATUS_INVALID_RECEIVE_LENGTH); 921 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 922 923 } 924 break; 925 926 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_CHK_2: 927 { 928 NdefMap->MifareULContainer.CurrentBlock += 929 PH_FRINFC_NDEFMAP_MFUL_BLOCK4; 930 931 Status = phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 932 NdefMap->MifareULContainer.CurrentBlock); 933 } 934 break; 935 936 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_READ_1: 937 /* check the received bytes size equals 1 byte*/ 938 if (*NdefMap->SendRecvLength == 939 PH_FRINFC_NDEFMAP_MFUL_VAL1) 940 { 941 if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] == 0x0A) 942 { 943 /* Send second command */ 944 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 945 NdefMap->MifareULContainer.CurrentSector, 2, 946 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_READ_2); 947 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG0; 948 } 949 else 950 { 951 /* read error */ 952 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 953 NFCSTATUS_INVALID_FORMAT); 954 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 955 } 956 } 957 else 958 { 959 /* read error */ 960 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 961 NFCSTATUS_INVALID_RECEIVE_LENGTH); 962 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 963 964 } 965 break; 966 967 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_READ_2: 968 { 969 if (NdefMap->MifareULContainer.CurrentBlock == 0xFF) 970 { 971 NdefMap->MifareULContainer.CurrentBlock = 0; 972 } 973 else 974 { 975 NdefMap->MifareULContainer.CurrentBlock = 976 (NdefMap->TLVStruct.NdefTLVBlock / 4) * 4; 977 } 978 979 Status = phFriNfc_MfUL_H_Rd16Bytes(NdefMap); 980 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 981 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 982 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 983 } 984 break; 985 986 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_1: 987 /* check the received bytes size equals 1 byte*/ 988 if (*NdefMap->SendRecvLength == 989 PH_FRINFC_NDEFMAP_MFUL_VAL1) 990 { 991 if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] == 0x0A) 992 { 993 /* Send second command */ 994 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 995 NdefMap->MifareULContainer.CurrentSector, 2, 996 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_2); 997 } 998 else 999 { 1000 /* read error */ 1001 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1002 NFCSTATUS_INVALID_FORMAT); 1003 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1004 } 1005 } 1006 else 1007 { 1008 /* read error */ 1009 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1010 NFCSTATUS_INVALID_RECEIVE_LENGTH); 1011 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1012 1013 } 1014 break; 1015 1016 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_2: 1017 { 1018 NdefMap->MifareULContainer.CurrentBlock = 0; 1019 Status = phFriNfc_MfUL_H_fillSendBufToWr(NdefMap); 1020 1021 if((Status == NFCSTATUS_SUCCESS) && 1022 (NdefMap->TLVStruct.SetTermTLVFlag != 1023 PH_FRINFC_NDEFMAP_MFUL_FLAG1) && 1024 (NdefMap->MifareULContainer.RemainingSize > 1025 PH_FRINFC_NDEFMAP_MFUL_VAL0)) 1026 { 1027 Status = phFriNfc_MfUL_H_WrTermTLV(NdefMap); 1028 } 1029 else 1030 { 1031 if((Status == NFCSTATUS_SUCCESS) && 1032 (NdefMap->TLVStruct.SetTermTLVFlag == 1033 PH_FRINFC_NDEFMAP_MFUL_FLAG1)) 1034 { 1035 Status = phFriNfc_MfUL_H_UpdateWrLen(NdefMap); 1036 } 1037 } 1038 1039 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 1040 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 1041 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 1042 } 1043 break; 1044 1045 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_INIT_1: 1046 /* check the received bytes size equals 1 byte*/ 1047 if (*NdefMap->SendRecvLength == 1048 PH_FRINFC_NDEFMAP_MFUL_VAL1) 1049 { 1050 if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] == 0x0A) 1051 { 1052 /* Send second command */ 1053 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 1054 NdefMap->MifareULContainer.CurrentSector, 2, 1055 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_INIT_2); 1056 } 1057 else 1058 { 1059 /* read error */ 1060 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1061 NFCSTATUS_INVALID_FORMAT); 1062 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1063 } 1064 } 1065 else 1066 { 1067 /* read error */ 1068 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1069 NFCSTATUS_INVALID_RECEIVE_LENGTH); 1070 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1071 1072 } 1073 break; 1074 1075 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_INIT_2: 1076 { 1077 Status = ((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN)? 1078 phFriNfc_MfUL_H_RdBeforeWrite(NdefMap): 1079 phFriNfc_MfUL_H_fillSendBufToWr(NdefMap)); 1080 1081 1082 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 1083 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 1084 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 1085 } 1086 break; 1087 1088 1089 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RW_1: 1090 /* check the received bytes size equals 1 byte*/ 1091 if (*NdefMap->SendRecvLength == 1092 PH_FRINFC_NDEFMAP_MFUL_VAL1) 1093 { 1094 if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] == 0x0A) 1095 { 1096 /* Send second command */ 1097 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 1098 NdefMap->MifareULContainer.CurrentSector, 2, 1099 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RW_2); 1100 } 1101 else 1102 { 1103 /* read error */ 1104 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1105 NFCSTATUS_INVALID_FORMAT); 1106 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1107 } 1108 } 1109 else 1110 { 1111 /* read error */ 1112 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1113 NFCSTATUS_INVALID_RECEIVE_LENGTH); 1114 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1115 1116 } 1117 break; 1118 1119 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RW_2: 1120 { 1121 NdefMap->MifareULContainer.CurrentBlock = 0; 1122 1123 NdefMap->SendRecvBuf[index] = 1124 NdefMap->MifareULContainer.CurrentBlock; 1125 index++; 1126 NdefMap->SendRecvBuf[index] = 1127 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 1128 index++; 1129 1130 if((((NdefMap->TLVStruct.NdefTLVByte - 1131 PH_FRINFC_NDEFMAP_MFUL_VAL1) == PH_FRINFC_NDEFMAP_MFUL_VAL0) || 1132 ((NdefMap->TLVStruct.NdefTLVByte - PH_FRINFC_NDEFMAP_MFUL_VAL1) 1133 == PH_FRINFC_NDEFMAP_MFUL_VAL3))) 1134 { 1135 /* Length to know how many bytes has to be written to the card */ 1136 TemLength = (((NdefMap->TLVStruct.NdefTLVByte - PH_FRINFC_NDEFMAP_MFUL_VAL1) == 1137 PH_FRINFC_NDEFMAP_MFUL_VAL0)? 1138 PH_FRINFC_NDEFMAP_MFUL_VAL2: 1139 PH_FRINFC_NDEFMAP_MFUL_VAL3); 1140 1141 if(NdefMap->ApduBufferSize >= TemLength) 1142 { 1143 /* Prepare the receive buffer */ 1144 (void)memcpy(&(NdefMap->SendRecvBuf[ 1145 index]), 1146 &(NdefMap->ApduBuffer[ 1147 NdefMap->ApduBuffIndex]), 1148 TemLength); 1149 1150 /* Number of bytes written to the card from user buffer */ 1151 NdefMap->NumOfBytesWritten = TemLength; 1152 1153 index = index+(uint8_t)TemLength; 1154 /* Exact number of bytes written in the card including TLV */ 1155 *NdefMap->DataCount = (index - PH_FRINFC_NDEFMAP_MFUL_VAL1); 1156 } 1157 else 1158 { 1159 /* Prepare the receive buffer */ 1160 (void)memcpy(&(NdefMap->SendRecvBuf[ 1161 index]), 1162 &(NdefMap->ApduBuffer[ 1163 NdefMap->ApduBuffIndex]), 1164 (uint16_t)NdefMap->ApduBufferSize); 1165 1166 /* Number of bytes written to the card from user buffer */ 1167 NdefMap->NumOfBytesWritten = (uint16_t)NdefMap->ApduBufferSize; 1168 1169 index= index +(uint8_t)NdefMap->ApduBufferSize; 1170 /* Exact number of bytes written in the card including TLV */ 1171 *NdefMap->DataCount = (index - PH_FRINFC_NDEFMAP_MFUL_VAL1); 1172 1173 for(i = index; i < PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK; i++) 1174 { 1175 NdefMap->SendRecvBuf[i] = (uint8_t)((i == index)? 1176 PH_FRINFC_NDEFMAP_MFUL_TERMTLV: 1177 PH_FRINFC_NDEFMAP_MFUL_NULLTLV); 1178 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1179 } 1180 } 1181 1182 /* store the bytes in buffer till the bytes are 1183 written in a block */ 1184 (void)memcpy(NdefMap->MifareULContainer.Buffer, 1185 &(NdefMap->SendRecvBuf[ 1186 PH_FRINFC_NDEFMAP_MFUL_VAL1]), 1187 (PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK - 1188 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 1189 1190 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer, 1191 NdefMap->MifareULContainer.Buffer, 1192 (PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK - 1193 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 1194 1195 /* Change the state to check ndef compliancy */ 1196 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE; 1197 1198 Status = phFriNfc_MfUL_H_Wr4bytes(NdefMap); 1199 } 1200 1201 1202 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 1203 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 1204 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 1205 } 1206 break; 1207 1208 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RESET_1: 1209 /* check the received bytes size equals 1 byte*/ 1210 if (*NdefMap->SendRecvLength == 1211 PH_FRINFC_NDEFMAP_MFUL_VAL1) 1212 { 1213 if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] == 0x0A) 1214 { 1215 /* Send second command */ 1216 Status = phFriNfc_MfUL_H_SelectSector(NdefMap, 1217 NdefMap->MifareULContainer.CurrentSector, 2, 1218 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RESET_2); 1219 } 1220 else 1221 { 1222 /* read error */ 1223 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1224 NFCSTATUS_INVALID_FORMAT); 1225 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1226 } 1227 } 1228 else 1229 { 1230 /* read error */ 1231 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1232 NFCSTATUS_INVALID_RECEIVE_LENGTH); 1233 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1234 1235 } 1236 break; 1237 1238 case PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RESET_2: 1239 { 1240 if (NdefMap->PrevState == PH_FRINFC_NDEFMAP_MFUL_STATE_FND_NDEF_COMP) 1241 { 1242 Status = phFriNfc_MfUL_H_NxtOp(NdefMap, &CRFlag); 1243 } 1244 else if (NdefMap->PrevState == PH_FRINFC_NDEFMAP_MFUL_STATE_READ) 1245 { 1246 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1247 } 1248 else if (NdefMap->PrevState == PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE) 1249 { 1250 Status = phFriNfc_MfUL_H_UpdateWrLen(NdefMap); 1251 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING)? 1252 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 1253 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 1254 } 1255 else if (NdefMap->PrevState == PH_FRINFC_NDEFMAP_MFUL_STATE_WR_LEN_TLV) 1256 { 1257 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1258 } 1259 else 1260 { 1261 /* read error */ 1262 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1263 NFCSTATUS_READ_FAILED); 1264 CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1265 1266 } 1267 } 1268 break; 1269 1270 default: 1271 /*set the invalid state*/ 1272 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST); 1273 phFriNfc_MifareUL_H_Complete(NdefMap, Status); 1274 break; 1275 } 1276 if(CRFlag == PH_FRINFC_NDEFMAP_MFUL_FLAG1) 1277 { 1278 /* call the CR routine*/ 1279 phFriNfc_MifareUL_H_Complete(NdefMap, Status); 1280 } 1281 } 1282 else 1283 { 1284 phFriNfc_MifareUL_H_Complete(NdefMap,Status); 1285 } 1286 } 1287 1288 static NFCSTATUS phFriNfc_MfUL_H_ChkCCBytes(phFriNfc_NdefMap_t *NdefMap ) 1289 { 1290 NFCSTATUS Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 1291 NFCSTATUS_NO_NDEF_SUPPORT); 1292 1293 #ifdef LOCK_BITS_CHECK_ENABLE 1294 switch(NdefMap->SendRecvBuf[7]) 1295 #else 1296 switch(NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_BYTE3]) 1297 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1298 { 1299 case PH_FRINFC_NDEFMAP_MFUL_CC_BYTE3_RW: 1300 /* This state can be either INITIALISED or READWRITE. but default 1301 is INITIALISED */ 1302 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INITIALIZED; 1303 break; 1304 1305 case PH_FRINFC_NDEFMAP_MFUL_CC_BYTE3_RO: 1306 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_ONLY; 1307 break; 1308 1309 default : 1310 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 1311 } 1312 1313 1314 1315 /* Check for Ndef compliancy : 0 and 1 byte spcifies the ndef compliancy 1316 2 byte specifies the version of the MF UL tag*/ 1317 #ifdef LOCK_BITS_CHECK_ENABLE 1318 if(( NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_BYTE4] == 1319 #else 1320 if(( NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_BYTE0] == 1321 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1322 PH_FRINFC_NDEFMAP_MFUL_CC_BYTE0) && ( 1323 (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED) || 1324 (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY))) 1325 { 1326 /* Check the version number */ 1327 Result =phFriNfc_MapTool_ChkSpcVer( NdefMap, 1328 #ifdef LOCK_BITS_CHECK_ENABLE 1329 5); 1330 1331 #else 1332 PH_FRINFC_NDEFMAP_MFUL_BYTE1); 1333 1334 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1335 #if 0 1336 #ifdef PH_NDEF_MIFARE_ULC 1337 if (Result == NFCSTATUS_SUCCESS) 1338 { 1339 #ifdef LOCK_BITS_CHECK_ENABLE 1340 1341 if (NdefMap->SendRecvBuf[6] == 0x06) 1342 { 1343 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_UL_CARD; 1344 } 1345 else if (NdefMap->SendRecvBuf[6] == 0x12) 1346 { 1347 // NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_ULC_CARD; 1348 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_UL_CARD; 1349 } 1350 1351 #else /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1352 1353 if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_BYTE2] == 0x06) 1354 { 1355 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_UL_CARD; 1356 } 1357 else if (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_BYTE2] == 0x12) 1358 { 1359 // NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_ULC_CARD; 1360 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_UL_CARD; 1361 } 1362 1363 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1364 else 1365 { 1366 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 1367 NFCSTATUS_INVALID_REMOTE_DEVICE); 1368 } 1369 } 1370 #else 1371 1372 /* Check the CC header size: Only valid ones are 1373 0x06 for 48 bytes. */ 1374 #ifdef LOCK_BITS_CHECK_ENABLE 1375 Result = ((( NdefMap->SendRecvBuf[6] != 1376 #else /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1377 Result = ((( NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_BYTE2] != 1378 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1379 PH_FRINFC_NDEFMAP_MFUL_CC_BYTE2) || (Result != 1380 NFCSTATUS_SUCCESS))? 1381 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1382 NFCSTATUS_INVALID_REMOTE_DEVICE)): 1383 Result); 1384 #endif /* #ifdef PH_NDEF_MIFARE_ULC */ 1385 #endif 1386 NdefMap->MifareULContainer.RemainingSize = 1387 NdefMap->CardMemSize = ((Result == NFCSTATUS_SUCCESS)? 1388 #ifdef LOCK_BITS_CHECK_ENABLE 1389 (NdefMap->SendRecvBuf[6] * 1390 #else /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1391 (NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_BYTE2] * 1392 #endif /* #ifdef LOCK_BITS_CHECK_ENABLE */ 1393 PH_FRINFC_NDEFMAP_MFUL_MUL8): 1394 NdefMap->CardMemSize); 1395 1396 if (NdefMap->CardMemSize > 256) 1397 { 1398 NdefMap->CardMemSize = NdefMap->CardMemSize - 2; 1399 NdefMap->MifareULContainer.RemainingSize = NdefMap->CardMemSize; 1400 } 1401 1402 } 1403 else 1404 { 1405 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 1406 } 1407 1408 1409 return Result; 1410 } 1411 1412 /*! 1413 * \brief this shall notify the integration software with respective 1414 * success/error status along with the completion routines. 1415 * 1416 * This routine is called from the mifareul process function. 1417 * 1418 */ 1419 1420 static void phFriNfc_MifareUL_H_Complete(phFriNfc_NdefMap_t *NdefMap, 1421 NFCSTATUS Status) 1422 { 1423 if(NdefMap!=NULL) 1424 { 1425 if((PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE == NdefMap->State) 1426 && (NFCSTATUS_SUCCESS != Status)) 1427 { 1428 *NdefMap->WrNdefPacketLength = 0; 1429 } 1430 /* set the state back to the Reset_Init state*/ 1431 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT; 1432 1433 /* set the completion routine*/ 1434 NdefMap->CompletionRoutine[NdefMap->MifareULContainer.CRindex]. 1435 CompletionRoutine(NdefMap->CompletionRoutine->Context, Status); 1436 } 1437 } 1438 1439 static NFCSTATUS phFriNfc_MfUL_H_Rd16Bytes( phFriNfc_NdefMap_t *NdefMap) 1440 { 1441 NFCSTATUS Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1442 NFCSTATUS_INVALID_PARAMETER); 1443 1444 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_READ; 1445 1446 /* Set the previous operation flag to read. */ 1447 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE; 1448 1449 /* Have we already read the entire file? */ 1450 if(NdefMap->ApduBuffIndex < NdefMap->ApduBufferSize) 1451 { 1452 /* set the data for additional data exchange */ 1453 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = PH_FRINFC_NDEFMAP_MFUL_VAL0; 1454 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = PH_FRINFC_NDEFMAP_MFUL_VAL0; 1455 NdefMap->psDepAdditionalInfo.NAD = PH_FRINFC_NDEFMAP_MFUL_VAL0; 1456 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] = 1457 NdefMap->MifareULContainer.CurrentBlock; 1458 NdefMap->SendLength = PH_FRINFC_NDEFMAP_MFUL_VAL1; 1459 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1460 /* 1461 * Changed 1462 * Description: replace with phHal_eMifareRead 1463 */ 1464 1465 NdefMap->Cmd.MfCmd = phHal_eMifareRead; 1466 1467 /* Call the overlapped HAL Transceive function */ 1468 Result = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice, 1469 &NdefMap->MapCompletionInfo, 1470 NdefMap->psRemoteDevInfo, 1471 NdefMap->Cmd, 1472 &NdefMap->psDepAdditionalInfo, 1473 NdefMap->SendRecvBuf, 1474 NdefMap->SendLength, 1475 NdefMap->SendRecvBuf, 1476 NdefMap->SendRecvLength); 1477 } 1478 return Result; 1479 } 1480 1481 static NFCSTATUS phFriNfc_MfUL_H_Wr4bytes( phFriNfc_NdefMap_t *NdefMap) 1482 { 1483 NFCSTATUS Result = NFCSTATUS_SUCCESS; 1484 1485 /* set the receive length*/ 1486 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1487 1488 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE; 1489 1490 /* 1491 * Changed 1492 * Description: phHal_eMifareCmdListMifareWrite4 replace with phHal_eMifareWrite4 1493 */ 1494 /* set the cmd to mifare read*/ 1495 NdefMap->Cmd.MfCmd = phHal_eMifareWrite4; 1496 1497 /* Set the CR and context for Mifare operations*/ 1498 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareUL_Process; 1499 NdefMap->MapCompletionInfo.Context = NdefMap; 1500 1501 NdefMap->SendLength = PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK; 1502 /*Call the Overlapped HAL Transceive function */ 1503 Result = phFriNfc_OvrHal_Transceive(NdefMap->LowerDevice, 1504 &NdefMap->MapCompletionInfo, 1505 NdefMap->psRemoteDevInfo, 1506 NdefMap->Cmd, 1507 &NdefMap->psDepAdditionalInfo, 1508 NdefMap->SendRecvBuf, 1509 NdefMap->SendLength, 1510 NdefMap->SendRecvBuf, 1511 NdefMap->SendRecvLength); 1512 return Result; 1513 } 1514 1515 static NFCSTATUS phFriNfc_MfUL_H_findNDEFTLV(phFriNfc_NdefMap_t *NdefMap, 1516 uint8_t *CRFlag) 1517 { 1518 NFCSTATUS Result = NFCSTATUS_SUCCESS; 1519 uint16_t ShiftLength = PH_FRINFC_NDEFMAP_MFUL_VAL0, 1520 TemLength = PH_FRINFC_NDEFMAP_MFUL_VAL0, 1521 Temp16Bytes = PH_FRINFC_NDEFMAP_MFUL_VAL0; 1522 Temp16Bytes = ((NdefMap->TLVStruct.NdefTLVByte > PH_FRINFC_NDEFMAP_MFUL_VAL0)? 1523 (NdefMap->TLVStruct.NdefTLVByte - PH_FRINFC_NDEFMAP_MFUL_VAL1): 1524 NdefMap->TLVStruct.NdefTLVByte); 1525 for(;;) 1526 { 1527 if(NdefMap->SendRecvBuf[Temp16Bytes] == 1528 PH_FRINFC_NDEFMAP_MFUL_NULLTLV) 1529 { 1530 NdefMap->MifareULContainer.RemainingSize -= 1531 PH_FRINFC_NDEFMAP_MFUL_VAL1; 1532 #ifdef PH_HAL4_ENABLE 1533 /* This check is added to know the remaining size in 1534 the card is not 0, if this is 0, then complete card has 1535 been read */ 1536 if (NdefMap->MifareULContainer.RemainingSize == 1537 PH_FRINFC_NDEFMAP_MFUL_VAL0) 1538 { 1539 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1540 NFCSTATUS_NO_NDEF_SUPPORT); 1541 break; 1542 } 1543 else 1544 { 1545 Result = NFCSTATUS_SUCCESS; 1546 } 1547 #else 1548 Result = ((NdefMap->MifareULContainer.RemainingSize == 1549 PH_FRINFC_NDEFMAP_MFUL_VAL0)? 1550 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1551 NFCSTATUS_NO_NDEF_SUPPORT)): 1552 NFCSTATUS_SUCCESS); 1553 #endif /* #ifdef PH_HAL4_ENABLE */ 1554 Temp16Bytes++; 1555 #ifdef PH_HAL4_ENABLE 1556 /* This code is added to read next 16 bytes. This means previous 1557 16 bytes read contains only NULL TLV, so read further to get the 1558 NDEF TLV */ 1559 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1560 Temp16Bytes); 1561 if(NFCSTATUS_SUCCESS != Result) 1562 { 1563 NdefMap->TLVStruct.NdefTLVBlock = 1564 NdefMap->TLVStruct.NdefTLVBlock + PH_FRINFC_NDEFMAP_MFUL_VAL4; 1565 break; 1566 } 1567 #endif /* #ifdef PH_HAL4_ENABLE */ 1568 } 1569 else 1570 { 1571 Result = ((NdefMap->SendRecvBuf[Temp16Bytes] == 1572 PH_FRINFC_NDEFMAP_MFUL_TERMTLV)? 1573 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1574 NFCSTATUS_NO_NDEF_SUPPORT)): 1575 NFCSTATUS_SUCCESS); 1576 1577 if(Result != NFCSTATUS_SUCCESS) 1578 { 1579 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1580 break; 1581 } 1582 1583 #ifdef PH_NDEF_MIFARE_ULC 1584 if ((NdefMap->SendRecvBuf[Temp16Bytes] == 1585 PH_FRINFC_NDEFMAP_MFUL_LOCK_CTRL_TLV) || 1586 (NdefMap->SendRecvBuf[Temp16Bytes] == 1587 PH_FRINFC_NDEFMAP_MFUL_MEM_CTRL_TLV) || 1588 (NdefMap->SendRecvBuf[Temp16Bytes] == 1589 PH_FRINFC_NDEFMAP_MFUL_PROPRIETRY_TLV)) 1590 { 1591 1592 NdefMap->TLVStruct.NdefTLVByte = 1593 ((Temp16Bytes % 1594 PH_FRINFC_NDEFMAP_MFUL_VAL4) + 1595 PH_FRINFC_NDEFMAP_MFUL_VAL1); 1596 1597 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1598 Temp16Bytes); 1599 if(Result != NFCSTATUS_SUCCESS) 1600 { 1601 NdefMap->TLVStruct.TcheckedinTLVFlag = 1602 PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1603 NdefMap->TLVStruct.NoLbytesinTLV = 1604 PH_FRINFC_NDEFMAP_MFUL_VAL3; 1605 break; 1606 } 1607 Temp16Bytes++; 1608 NdefMap->MifareULContainer.RemainingSize -= 1609 PH_FRINFC_NDEFMAP_MFUL_VAL1; 1610 1611 if(NdefMap->MifareULContainer.RemainingSize == 1612 PH_FRINFC_NDEFMAP_MFUL_VAL0) 1613 { 1614 Result = (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1615 NFCSTATUS_NO_NDEF_SUPPORT)); 1616 break; 1617 } 1618 1619 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1620 Temp16Bytes); 1621 if(Result != NFCSTATUS_SUCCESS) 1622 { 1623 NdefMap->TLVStruct.TcheckedinTLVFlag = 1624 PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1625 NdefMap->TLVStruct.NoLbytesinTLV = 1626 PH_FRINFC_NDEFMAP_MFUL_VAL3; 1627 break; 1628 } 1629 1630 1631 /* If the value of the Length(L) in TLV is FF then enter else 1632 check for the card memory */ 1633 if((NdefMap->SendRecvBuf[Temp16Bytes] == 1634 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_LFF) || 1635 ((NdefMap->SendRecvBuf[Temp16Bytes] == 1636 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L) && 1637 (NdefMap->TLVStruct.NdefTLVFoundFlag != 1638 PH_FRINFC_NDEFMAP_MFUL_FLAG1))) 1639 { 1640 /* In the present case, the card space is not greater 1641 than 0xFF */ 1642 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1643 NFCSTATUS_NO_NDEF_SUPPORT); 1644 1645 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1646 break; 1647 } 1648 else 1649 { 1650 NdefMap->TLVStruct.BytesRemainLinTLV = 1651 NdefMap->SendRecvBuf[Temp16Bytes]; 1652 1653 NdefMap->TLVStruct.ActualSize = 1654 NdefMap->SendRecvBuf[Temp16Bytes]; 1655 1656 if((NdefMap->MifareULContainer.RemainingSize < 1657 NdefMap->SendRecvBuf[Temp16Bytes]) || 1658 (NdefMap->MifareULContainer.RemainingSize < 1659 PH_FRINFC_NDEFMAP_MFUL_VAL2) || 1660 (NdefMap->TLVStruct.BytesRemainLinTLV > 1661 (NdefMap->MifareULContainer.RemainingSize)) || 1662 ((NdefMap->TLVStruct.BytesRemainLinTLV == 1663 PH_FRINFC_NDEFMAP_MFUL_VAL0) && 1664 (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE))) 1665 { 1666 /* No NDEF TLV found */ 1667 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1668 NFCSTATUS_NO_NDEF_SUPPORT); 1669 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1670 break; 1671 } 1672 1673 if(NdefMap->TLVStruct.NdefTLVFoundFlag != 1674 PH_FRINFC_NDEFMAP_MFUL_FLAG1) 1675 { 1676 NdefMap->TLVStruct.NdefTLVByte = 1677 (((Temp16Bytes + PH_FRINFC_NDEFMAP_MFUL_VAL1 + 1678 NdefMap->SendRecvBuf[Temp16Bytes]) % 1679 PH_FRINFC_NDEFMAP_MFUL_VAL4) + 1680 PH_FRINFC_NDEFMAP_MFUL_VAL1); 1681 #if 0 1682 NdefMap->TLVStruct.NdefTLVBlock = 1683 (uint8_t)(NdefMap->TLVStruct.NdefTLVBlock 1684 + ((Temp16Bytes + 1685 NdefMap->SendRecvBuf[Temp16Bytes] + 1)/ 1686 PH_FRINFC_NDEFMAP_MFUL_VAL4)); 1687 #endif 1688 NdefMap->TLVStruct.NdefTLVBlock = 1689 (uint8_t)(((NdefMap->TLVStruct.NdefTLVBlock / PH_FRINFC_NDEFMAP_MFUL_VAL4) * 1690 PH_FRINFC_NDEFMAP_MFUL_VAL4) 1691 + ((Temp16Bytes + NdefMap->SendRecvBuf[Temp16Bytes] + 1)/ 1692 PH_FRINFC_NDEFMAP_MFUL_VAL4)); 1693 1694 1695 TemLength = (Temp16Bytes + 1696 NdefMap->SendRecvBuf[Temp16Bytes]); 1697 1698 NdefMap->MifareULContainer.RemainingSize = 1699 (NdefMap->MifareULContainer.RemainingSize - 1700 (NdefMap->SendRecvBuf[Temp16Bytes] 1701 + PH_FRINFC_NDEFMAP_MFUL_VAL1)); 1702 1703 /* If the Length (L) in TLV < 16 bytes */ 1704 Temp16Bytes = ((TemLength >= 1705 PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16)? 1706 PH_FRINFC_NDEFMAP_MFUL_VAL0: 1707 (TemLength + 1708 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 1709 1710 Result = ((TemLength >= 1711 PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16)? 1712 phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 1713 NdefMap->TLVStruct.NdefTLVBlock): 1714 NFCSTATUS_SUCCESS); 1715 1716 if(TemLength >= PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16) 1717 { 1718 break; 1719 } 1720 TemLength = Temp16Bytes; 1721 } 1722 } 1723 1724 1725 1726 1727 #if 0 1728 1729 NdefMap->MifareULContainer.RemainingSize = 1730 (NdefMap->MifareULContainer.RemainingSize - 1731 (NdefMap->SendRecvBuf[Temp16Bytes + 1] 1732 + PH_FRINFC_NDEFMAP_MFUL_VAL2)); 1733 1734 NdefMap->TLVStruct.NdefTLVBlock = 1735 (uint8_t)(NdefMap->TLVStruct.NdefTLVBlock 1736 + ((Temp16Bytes + 1737 NdefMap->SendRecvBuf[Temp16Bytes + 1] + 2)/ 1738 PH_FRINFC_NDEFMAP_MFUL_VAL4)); 1739 1740 1741 Temp16Bytes = Temp16Bytes + 1742 NdefMap->SendRecvBuf[Temp16Bytes + 1] + 2; 1743 #endif 1744 } 1745 #endif /* #ifdef PH_NDEF_MIFARE_ULC */ 1746 else { 1747 1748 /* Check the byte for 0x03 Type of NDEF TLV */ 1749 NdefMap->TLVStruct.NdefTLVFoundFlag = 1750 ((NdefMap->SendRecvBuf[Temp16Bytes] == 1751 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_T)? 1752 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 1753 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 1754 1755 if(NdefMap->TLVStruct.NdefTLVFoundFlag == 1756 PH_FRINFC_NDEFMAP_MFUL_FLAG1) 1757 { 1758 ShiftLength = (Temp16Bytes + 1759 NdefMap->SendRecvBuf[Temp16Bytes]); 1760 1761 NdefMap->TLVStruct.NdefTLVByte = 1762 ((Temp16Bytes % 1763 PH_FRINFC_NDEFMAP_MFUL_VAL4) + 1764 PH_FRINFC_NDEFMAP_MFUL_VAL1); 1765 1766 NdefMap->TLVStruct.NdefTLVBlock = 1767 (uint8_t)(((NdefMap->TLVStruct.NdefTLVBlock /4) * 4) 1768 + (Temp16Bytes)/ 1769 PH_FRINFC_NDEFMAP_MFUL_VAL4); 1770 1771 NdefMap->TLVStruct.NdefTLVSector = NdefMap->MifareULContainer.CurrentSector; 1772 1773 } 1774 #ifdef PH_HAL4_ENABLE 1775 else 1776 { 1777 /* if the Type of the NDEF TLV is not found, then return 1778 error saying no ndef TLV found*/ 1779 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1780 NFCSTATUS_NO_NDEF_SUPPORT); 1781 break; 1782 #if 0 1783 /* This change is added to continue the loop, if the Type of the 1784 NDEF TLV is not found 1785 16 bytes are read, so for each byte, there is a check for the 1786 Type (T) of the TLV, if T != 0x03, then increment the byte 1787 count and restart the loop, till the T = 0x03 is found or all 1788 the bytes in the card is completely read. 1789 */ 1790 Temp16Bytes = (uint16_t)(Temp16Bytes + 1); 1791 NdefMap->MifareULContainer.RemainingSize -= 1792 PH_FRINFC_NDEFMAP_MFUL_VAL1; 1793 if (NdefMap->MifareULContainer.RemainingSize == 1794 PH_FRINFC_NDEFMAP_MFUL_VAL0) 1795 { 1796 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1797 NFCSTATUS_NO_NDEF_SUPPORT); 1798 break; 1799 } 1800 else 1801 { 1802 Result = NFCSTATUS_SUCCESS; 1803 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1804 Temp16Bytes); 1805 if(NFCSTATUS_PENDING == Result) 1806 { 1807 break; 1808 } 1809 continue; 1810 } 1811 #endif /* #if 0 */ 1812 } 1813 #endif /* #ifdef PH_HAL4_ENABLE */ 1814 1815 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1816 Temp16Bytes); 1817 if(Result != NFCSTATUS_SUCCESS) 1818 { 1819 NdefMap->TLVStruct.TcheckedinTLVFlag = 1820 PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1821 NdefMap->TLVStruct.NoLbytesinTLV = 1822 PH_FRINFC_NDEFMAP_MFUL_VAL3; 1823 break; 1824 } 1825 Temp16Bytes++; 1826 NdefMap->MifareULContainer.RemainingSize -= 1827 PH_FRINFC_NDEFMAP_MFUL_VAL1; 1828 1829 if(NdefMap->MifareULContainer.RemainingSize == 1830 PH_FRINFC_NDEFMAP_MFUL_VAL0) 1831 { 1832 Result = (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1833 NFCSTATUS_NO_NDEF_SUPPORT)); 1834 break; 1835 } 1836 1837 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1838 Temp16Bytes); 1839 if(Result != NFCSTATUS_SUCCESS) 1840 { 1841 NdefMap->TLVStruct.TcheckedinTLVFlag = 1842 PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1843 NdefMap->TLVStruct.NoLbytesinTLV = 1844 PH_FRINFC_NDEFMAP_MFUL_VAL3; 1845 break; 1846 } 1847 1848 /* If the value of the Length(L) in TLV is FF then enter else 1849 check for the card memory */ 1850 if((NdefMap->SendRecvBuf[Temp16Bytes] == 1851 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_LFF) || 1852 ((NdefMap->SendRecvBuf[Temp16Bytes] == 1853 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L) && 1854 (NdefMap->TLVStruct.NdefTLVFoundFlag != 1855 PH_FRINFC_NDEFMAP_MFUL_FLAG1))) 1856 { 1857 /* In the present case, the card space is not greater 1858 than 0xFF */ 1859 /* 1860 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1861 NFCSTATUS_NO_NDEF_SUPPORT); 1862 1863 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1864 break; 1865 */ 1866 1867 Temp16Bytes++; 1868 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1869 Temp16Bytes); 1870 if(Result != NFCSTATUS_SUCCESS) 1871 { 1872 NdefMap->TLVStruct.TcheckedinTLVFlag = 1873 PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1874 NdefMap->TLVStruct.NoLbytesinTLV = 1875 PH_FRINFC_NDEFMAP_MFUL_VAL2; 1876 1877 break; 1878 } 1879 1880 ShiftLength = (uint16_t) NdefMap->SendRecvBuf[Temp16Bytes]; 1881 NdefMap->MifareULContainer.RemainingSize--; 1882 1883 Temp16Bytes++; 1884 Result = phFriNfc_MfUL_H_Chk16Bytes(NdefMap, 1885 Temp16Bytes); 1886 if(Result != NFCSTATUS_SUCCESS) 1887 { 1888 NdefMap->TLVStruct.TcheckedinTLVFlag = 1889 PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1890 NdefMap->TLVStruct.NoLbytesinTLV = 1891 PH_FRINFC_NDEFMAP_MFUL_VAL1; 1892 NdefMap->TLVStruct.prevLenByteValue = 1893 NdefMap->SendRecvBuf[Temp16Bytes - 1]; 1894 break; 1895 } 1896 1897 1898 ShiftLength = 1899 (uint16_t) (NdefMap->SendRecvBuf[Temp16Bytes] 1900 | (ShiftLength << PH_FRINFC_NDEFMAP_MFUL_SHIFT8)); 1901 1902 // NdefMap->MifareULContainer.RemainingSize--; 1903 1904 if(ShiftLength > (NdefMap->MifareULContainer.RemainingSize)) 1905 { 1906 // Size in the Length(L) of TLV is greater 1907 //than the actual size of the card 1908 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1909 NFCSTATUS_INVALID_PARAMETER); 1910 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1911 break; 1912 } 1913 1914 // NdefMap->MifareULContainer.RemainingSize--; 1915 /* 1916 NdefMap->TLVStruct.NdefTLVByte = 1917 (NdefMap->SendRecvBuf[Temp16Bytes] % 1918 PH_FRINFC_NDEFMAP_MFUL_VAL4); 1919 1920 NdefMap->TLVStruct.NdefTLVBlock = 1921 (uint8_t)(NdefMap->MifareULContainer.CurrentBlock 1922 + (Temp16Bytes/PH_FRINFC_NDEFMAP_MFUL_VAL4)); 1923 */ 1924 1925 NdefMap->TLVStruct.ActualSize = 1926 NdefMap->TLVStruct.BytesRemainLinTLV = ShiftLength; 1927 1928 NdefMap->TLVStruct.NdefTLVFoundFlag = 1; 1929 1930 NdefMap->TLVStruct.NdefTLVSector = NdefMap->MifareULContainer.CurrentSector; 1931 1932 1933 Result = ((NdefMap->TLVStruct.NoLbytesinTLV == 1934 PH_FRINFC_NDEFMAP_MFUL_VAL0)? 1935 phFriNfc_MapTool_SetCardState( NdefMap, ShiftLength): 1936 Result); 1937 /* 1938 Result = phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 1939 NdefMap->TLVStruct.NdefTLVBlock); 1940 */ 1941 break; 1942 } 1943 else 1944 { 1945 NdefMap->TLVStruct.BytesRemainLinTLV = 1946 NdefMap->SendRecvBuf[Temp16Bytes]; 1947 1948 NdefMap->TLVStruct.ActualSize = 1949 NdefMap->SendRecvBuf[Temp16Bytes]; 1950 1951 if((NdefMap->MifareULContainer.RemainingSize < 1952 NdefMap->SendRecvBuf[Temp16Bytes]) || 1953 (NdefMap->MifareULContainer.RemainingSize < 1954 PH_FRINFC_NDEFMAP_MFUL_VAL2) || 1955 (NdefMap->TLVStruct.BytesRemainLinTLV > 1956 (NdefMap->MifareULContainer.RemainingSize)) || 1957 ((NdefMap->TLVStruct.BytesRemainLinTLV == 1958 PH_FRINFC_NDEFMAP_MFUL_VAL0) && 1959 (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE))) 1960 { 1961 /* No NDEF TLV found */ 1962 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1963 NFCSTATUS_NO_NDEF_SUPPORT); 1964 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 1965 break; 1966 } 1967 1968 if(NdefMap->TLVStruct.NdefTLVFoundFlag != 1969 PH_FRINFC_NDEFMAP_MFUL_FLAG1) 1970 { 1971 NdefMap->TLVStruct.NdefTLVByte = 1972 (((Temp16Bytes + PH_FRINFC_NDEFMAP_MFUL_VAL1 + 1973 NdefMap->SendRecvBuf[Temp16Bytes]) % 1974 PH_FRINFC_NDEFMAP_MFUL_VAL4) + 1975 PH_FRINFC_NDEFMAP_MFUL_VAL1); 1976 NdefMap->TLVStruct.NdefTLVBlock = 1977 (uint8_t)(NdefMap->TLVStruct.NdefTLVBlock 1978 + ((Temp16Bytes + 1979 NdefMap->SendRecvBuf[Temp16Bytes] + 1)/ 1980 PH_FRINFC_NDEFMAP_MFUL_VAL4)); 1981 1982 TemLength = (Temp16Bytes + 1983 NdefMap->SendRecvBuf[Temp16Bytes]); 1984 1985 NdefMap->MifareULContainer.RemainingSize = 1986 (NdefMap->MifareULContainer.RemainingSize - 1987 (NdefMap->SendRecvBuf[Temp16Bytes] 1988 + PH_FRINFC_NDEFMAP_MFUL_VAL1)); 1989 1990 /* If the Length (L) in TLV < 16 bytes */ 1991 Temp16Bytes = ((TemLength >= 1992 PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16)? 1993 PH_FRINFC_NDEFMAP_MFUL_VAL0: 1994 (TemLength + 1995 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 1996 1997 Result = ((TemLength >= 1998 PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16)? 1999 phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 2000 NdefMap->TLVStruct.NdefTLVBlock): 2001 NFCSTATUS_SUCCESS); 2002 2003 if(TemLength >= PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16) 2004 { 2005 break; 2006 } 2007 TemLength = Temp16Bytes; 2008 } 2009 } 2010 if(NdefMap->TLVStruct.NdefTLVFoundFlag == 2011 PH_FRINFC_NDEFMAP_MFUL_FLAG1) 2012 { 2013 #if 0 2014 NdefMap->TLVStruct.NdefTLVBlock = 2015 (uint8_t)(NdefMap->TLVStruct.NdefTLVBlock 2016 + ((Temp16Bytes + 1)/ 2017 PH_FRINFC_NDEFMAP_MFUL_VAL4)) - 1; 2018 #endif 2019 NdefMap->MifareULContainer.RemainingSize = 2020 (NdefMap->MifareULContainer.RemainingSize - 2021 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2022 ShiftLength = NdefMap->SendRecvBuf[Temp16Bytes]; 2023 Result = ((NdefMap->TLVStruct.NoLbytesinTLV == 2024 PH_FRINFC_NDEFMAP_MFUL_VAL0)? 2025 phFriNfc_MapTool_SetCardState( NdefMap, ShiftLength): 2026 Result); 2027 2028 break; 2029 } 2030 } 2031 } 2032 } 2033 2034 return Result; 2035 } 2036 2037 2038 static NFCSTATUS phFriNfc_MfUL_H_Chk16Bytes(phFriNfc_NdefMap_t *NdefMap, 2039 uint16_t TempLength) 2040 { 2041 uint16_t localCurrentBlock; 2042 2043 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2044 if(TempLength == PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16) 2045 { 2046 localCurrentBlock = NdefMap->MifareULContainer.CurrentBlock + 2047 PH_FRINFC_NDEFMAP_MFUL_BLOCK4; 2048 2049 if (localCurrentBlock < 256) 2050 { 2051 NdefMap->MifareULContainer.CurrentBlock += 2052 PH_FRINFC_NDEFMAP_MFUL_BLOCK4; 2053 2054 Result = phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 2055 NdefMap->MifareULContainer.CurrentBlock); 2056 } 2057 else 2058 { 2059 /* Go to next sector */ 2060 NdefMap->MifareULContainer.CurrentSector++; 2061 2062 Result = phFriNfc_MfUL_H_SelectSector(NdefMap, 2063 NdefMap->MifareULContainer.CurrentSector, 1, 2064 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_CHK_1); 2065 } 2066 } 2067 2068 return Result; 2069 } 2070 2071 2072 static void phFriNfc_MfUL_H_UpdateCrc( uint8_t ch, 2073 uint16_t *lpwCrc ) 2074 { 2075 ch = (ch^(uint8_t)((*lpwCrc) & 0x00FF)); 2076 ch = (ch^(ch<<4)); 2077 *lpwCrc = (*lpwCrc >> 8)^((uint16_t)ch << 8)^ \ 2078 ((uint16_t)ch<<3)^((uint16_t)ch>>4); 2079 2080 return; 2081 } 2082 2083 static void phFriNfc_MfUL_H_ComputeCrc( int CRCType, 2084 uint8_t *Data, 2085 int Length, 2086 uint8_t *TransmitFirst, 2087 uint8_t *TransmitSecond 2088 ) 2089 { 2090 uint8_t chBlock; 2091 uint16_t wCrc; 2092 switch(CRCType) 2093 { 2094 case CRC_A: 2095 wCrc = 0x6363; /* ITU-V.41 */ 2096 break; 2097 case CRC_B: 2098 wCrc = 0xFFFF; /* ISO/IEC 13239 (formerly ISO/IEC 3309) */ 2099 break; 2100 default: 2101 return; 2102 } 2103 2104 do 2105 { 2106 chBlock = *Data++; 2107 phFriNfc_MfUL_H_UpdateCrc(chBlock, &wCrc); 2108 } while (--Length); 2109 *TransmitFirst = (uint8_t) (wCrc & 0xFF); 2110 *TransmitSecond = (uint8_t) ((wCrc >> 8) & 0xFF); 2111 return; 2112 } 2113 2114 2115 2116 static NFCSTATUS phFriNfc_MfUL_H_SelectSector(phFriNfc_NdefMap_t *NdefMap, 2117 uint8_t SectorNo, 2118 uint8_t CmdNo, 2119 uint8_t NextState) 2120 { 2121 2122 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2123 2124 /* set the data for additional data exchange */ 2125 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2126 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2127 NdefMap->psDepAdditionalInfo.NAD = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2128 2129 NdefMap->State = NextState; 2130 2131 if (CmdNo == 1) 2132 { 2133 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] = 0x00; 2134 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL1] = 0x00; 2135 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL2] = 0xC2; 2136 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL3] = 0xFF; 2137 NdefMap->SendLength = PH_FRINFC_NDEFMAP_MFUL_VAL4; 2138 } 2139 else 2140 { 2141 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] = 0x00; 2142 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL1] = 0x00; 2143 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL2] = SectorNo; 2144 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL3] = 0; 2145 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL4] = 0; 2146 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL5] = 0; 2147 NdefMap->SendLength = PH_FRINFC_NDEFMAP_MFUL_VAL5 + 1; 2148 } 2149 2150 /* Calculate CRC */ 2151 2152 phFriNfc_MfUL_H_ComputeCrc(CRC_A, &NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL2], 2153 NdefMap->SendLength - 2, 2154 &NdefMap->SendRecvBuf[NdefMap->SendLength], 2155 &NdefMap->SendRecvBuf[NdefMap->SendLength + 1]); 2156 2157 NdefMap->SendLength += PH_FRINFC_NDEFMAP_MFUL_VAL2; 2158 2159 2160 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 2161 2162 NdefMap->Cmd.MfCmd = phHal_eMifareRaw; 2163 2164 /* Call the overlapped HAL Transceive function */ 2165 Result = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice, 2166 &NdefMap->MapCompletionInfo, 2167 NdefMap->psRemoteDevInfo, 2168 NdefMap->Cmd, 2169 &NdefMap->psDepAdditionalInfo, 2170 NdefMap->SendRecvBuf, 2171 NdefMap->SendLength, 2172 NdefMap->SendRecvBuf, 2173 NdefMap->SendRecvLength); 2174 return Result; 2175 } 2176 2177 2178 static NFCSTATUS phFriNfc_MfUL_H_RdCardfindNdefTLV( phFriNfc_NdefMap_t *NdefMap, 2179 uint8_t BlockNo) 2180 { 2181 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2182 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_FND_NDEF_COMP; 2183 /* set the data for additional data exchange */ 2184 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2185 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2186 NdefMap->psDepAdditionalInfo.NAD = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2187 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] = 2188 BlockNo; 2189 NdefMap->SendLength = PH_FRINFC_NDEFMAP_MFUL_VAL1; 2190 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 2191 2192 /* 2193 * Changed 2194 * Description: phHal_eMifareCmdListMifareRead replace with phHal_eMifareRead 2195 */ 2196 NdefMap->Cmd.MfCmd = phHal_eMifareRead; 2197 2198 /* Call the overlapped HAL Transceive function */ 2199 Result = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice, 2200 &NdefMap->MapCompletionInfo, 2201 NdefMap->psRemoteDevInfo, 2202 NdefMap->Cmd, 2203 &NdefMap->psDepAdditionalInfo, 2204 NdefMap->SendRecvBuf, 2205 NdefMap->SendLength, 2206 NdefMap->SendRecvBuf, 2207 NdefMap->SendRecvLength); 2208 return Result; 2209 } 2210 2211 static NFCSTATUS phFriNfc_MfUL_H_ChkRemainTLV(phFriNfc_NdefMap_t *NdefMap, 2212 uint8_t *CRFlag) 2213 { 2214 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2215 uint16_t TempLength = PH_FRINFC_NDEFMAP_MFUL_VAL0, 2216 ShiftLength = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2217 2218 switch(NdefMap->TLVStruct.NoLbytesinTLV) 2219 { 2220 case PH_FRINFC_NDEFMAP_MFUL_VAL1: 2221 case PH_FRINFC_NDEFMAP_MFUL_VAL2: 2222 ShiftLength = ((NdefMap->TLVStruct.NoLbytesinTLV == 2223 PH_FRINFC_NDEFMAP_MFUL_VAL1)? 2224 NdefMap->TLVStruct.prevLenByteValue: 2225 NdefMap->SendRecvBuf[TempLength]); 2226 ShiftLength = ((NdefMap->TLVStruct.NoLbytesinTLV == 2227 PH_FRINFC_NDEFMAP_MFUL_VAL1)? 2228 (((uint16_t)(NdefMap->SendRecvBuf[TempLength]) << 2229 PH_FRINFC_NDEFMAP_MFUL_SHIFT8) | 2230 ShiftLength): 2231 (((uint16_t)(NdefMap->SendRecvBuf[(TempLength + 2232 PH_FRINFC_NDEFMAP_MFUL_VAL1)]) << 2233 PH_FRINFC_NDEFMAP_MFUL_SHIFT8) | 2234 ShiftLength)); 2235 2236 NdefMap->MifareULContainer.RemainingSize -= 2237 PH_FRINFC_NDEFMAP_MFUL_VAL1; 2238 2239 NdefMap->TLVStruct.ActualSize = 2240 NdefMap->TLVStruct.BytesRemainLinTLV = ShiftLength; 2241 2242 /* Check for remaining free space in the card with the 2243 length (L) of TLV OR length(L) of TLV is less than 2244 255 bytes (The length (L) of TLV for 3 byte should not 2245 be less than 255) */ 2246 Result = ((((NdefMap->MifareULContainer.RemainingSize)<= 2247 ShiftLength) || (ShiftLength < 2248 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_LFF))? 2249 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2250 NFCSTATUS_INVALID_PARAMETER)): 2251 Result); 2252 2253 2254 Result = ((Result == NFCSTATUS_SUCCESS)? 2255 phFriNfc_MapTool_SetCardState( NdefMap, ShiftLength): 2256 Result); 2257 2258 *CRFlag = (uint8_t)((Result == (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2259 NFCSTATUS_INVALID_PARAMETER)))? 2260 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 2261 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 2262 2263 2264 if(Result == NFCSTATUS_SUCCESS) 2265 { 2266 2267 NdefMap->MifareULContainer.RemainingSize= NdefMap->MifareULContainer.RemainingSize- 2268 NdefMap->TLVStruct.NoLbytesinTLV; 2269 /* 2270 NdefMap->TLVStruct.NdefTLVByte = ((ShiftLength% 2271 PH_FRINFC_NDEFMAP_MFUL_VAL4) + 2272 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2273 2274 NdefMap->TLVStruct.NdefTLVBlock = 2275 (uint8_t)(NdefMap->MifareULContainer.CurrentBlock 2276 + (ShiftLength/PH_FRINFC_NDEFMAP_MFUL_VAL4)); 2277 NdefMap->MifareULContainer.CurrentBlock = 2278 NdefMap->TLVStruct.NdefTLVBlock; 2279 2280 Result = phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 2281 NdefMap->TLVStruct.NdefTLVBlock); 2282 */ 2283 } 2284 break; 2285 2286 default: 2287 if((NdefMap->SendRecvBuf[TempLength] == 2288 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_LFF) || 2289 ((NdefMap->SendRecvBuf[TempLength] == 2290 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L) && 2291 (NdefMap->TLVStruct.NdefTLVFoundFlag != 2292 PH_FRINFC_NDEFMAP_MFUL_FLAG1))) 2293 { 2294 /* In the present case, the card space is not greater 2295 than 0xFF */ 2296 /* 2297 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2298 NFCSTATUS_NO_NDEF_SUPPORT); 2299 2300 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 2301 2302 */ 2303 2304 ShiftLength = NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_NDEFMAP_MFUL_VAL1)]; 2305 ShiftLength = (((uint16_t)(NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_NDEFMAP_MFUL_VAL2)]) 2306 << PH_FRINFC_NDEFMAP_MFUL_SHIFT8) | 2307 ShiftLength); 2308 Result = ((ShiftLength > (NdefMap->MifareULContainer.RemainingSize))? 2309 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2310 NFCSTATUS_INVALID_PARAMETER)): 2311 Result); 2312 2313 2314 Result = ((Result == NFCSTATUS_SUCCESS)? 2315 phFriNfc_MapTool_SetCardState( NdefMap, ShiftLength): 2316 Result); 2317 2318 NdefMap->TLVStruct.ActualSize = 2319 NdefMap->TLVStruct.BytesRemainLinTLV = ShiftLength; 2320 2321 if(Result == NFCSTATUS_SUCCESS) 2322 { 2323 2324 NdefMap->MifareULContainer.RemainingSize= NdefMap->MifareULContainer.RemainingSize- 2325 NdefMap->TLVStruct.NoLbytesinTLV; 2326 /* 2327 NdefMap->TLVStruct.NdefTLVByte = ((ShiftLength% 2328 PH_FRINFC_NDEFMAP_MFUL_VAL4) + 2329 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2330 2331 NdefMap->TLVStruct.NdefTLVBlock = 2332 (uint8_t)(NdefMap->MifareULContainer.CurrentBlock 2333 + (ShiftLength/PH_FRINFC_NDEFMAP_MFUL_VAL4)); 2334 2335 NdefMap->MifareULContainer.CurrentBlock = 2336 NdefMap->TLVStruct.NdefTLVBlock; 2337 2338 Result = phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 2339 NdefMap->TLVStruct.NdefTLVBlock); 2340 */ 2341 } 2342 } 2343 else 2344 { 2345 /* length (L) value in TLV shall not be greater than 2346 remaining free space in the card */ 2347 Result = ((NdefMap->SendRecvBuf[TempLength] > 2348 NdefMap->MifareULContainer.RemainingSize)? 2349 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2350 NFCSTATUS_INVALID_PARAMETER)): 2351 Result); 2352 2353 NdefMap->TLVStruct.ActualSize = 2354 NdefMap->TLVStruct.BytesRemainLinTLV = 2355 NdefMap->SendRecvBuf[TempLength]; 2356 NdefMap->MifareULContainer.RemainingSize--; 2357 2358 if((Result == NFCSTATUS_SUCCESS) && 2359 (NdefMap->TLVStruct.NdefTLVFoundFlag != 2360 PH_FRINFC_NDEFMAP_MFUL_FLAG1)) 2361 { 2362 phFriNfc_MfUL_H_UpdateLen(NdefMap, 2363 (uint16_t)NdefMap->SendRecvBuf[TempLength]); 2364 2365 NdefMap->MifareULContainer.CurrentBlock = 2366 NdefMap->TLVStruct.NdefTLVBlock; 2367 TempLength=TempLength+(NdefMap->SendRecvBuf[TempLength]); 2368 Result =((TempLength < PH_FRINFC_NDEFMAP_MFUL_RDBYTES_16)? 2369 phFriNfc_MfUL_H_findNDEFTLV(NdefMap, CRFlag): 2370 phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 2371 NdefMap->TLVStruct.NdefTLVBlock)); 2372 } 2373 } 2374 break; 2375 } 2376 NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2377 2378 Result = phFriNfc_MapTool_SetCardState( NdefMap, NdefMap->TLVStruct.ActualSize); 2379 2380 return Result; 2381 } 2382 2383 static void phFriNfc_MfUL_H_UpdateLen(phFriNfc_NdefMap_t *NdefMap, 2384 uint16_t DataLen) 2385 { 2386 NdefMap->MifareULContainer.RemainingSize= NdefMap->MifareULContainer.RemainingSize-DataLen; 2387 NdefMap->TLVStruct.NdefTLVByte = ((DataLen % 2388 PH_FRINFC_NDEFMAP_MFUL_VAL4) + 2389 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2390 NdefMap->TLVStruct.NdefTLVBlock = 2391 (uint8_t)(NdefMap->MifareULContainer.CurrentBlock 2392 + (DataLen/PH_FRINFC_NDEFMAP_MFUL_VAL4)); 2393 } 2394 2395 static NFCSTATUS phFriNfc_MfUL_H_NxtOp(phFriNfc_NdefMap_t *NdefMap, 2396 uint8_t *CRFlag) 2397 { 2398 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2399 2400 switch(NdefMap->PrevOperation) 2401 { 2402 case PH_FRINFC_NDEFMAP_CHECK_OPE: 2403 *CRFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 2404 /* Fix to check if the actual size in the TLV is greater than card */ 2405 if (NdefMap->TLVStruct.ActualSize > (NdefMap->CardMemSize - 2)) 2406 { 2407 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2408 NFCSTATUS_NO_NDEF_SUPPORT); 2409 } 2410 break; 2411 2412 case PH_FRINFC_NDEFMAP_READ_OPE: 2413 if (NdefMap->TLVStruct.NdefTLVSector == 1) 2414 { 2415 /* Goto sector 1 */ 2416 NdefMap->MifareULContainer.CurrentSector = 1; 2417 NdefMap->MifareULContainer.CurrentBlock = 0; 2418 2419 Result = phFriNfc_MfUL_H_SelectSector(NdefMap, 2420 NdefMap->MifareULContainer.CurrentSector, 1, 2421 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_READ_1); 2422 } 2423 else 2424 { 2425 NdefMap->MifareULContainer.CurrentBlock = (NdefMap->TLVStruct.NdefTLVBlock / 4) * 4; 2426 2427 Result = phFriNfc_MfUL_H_Rd16Bytes(NdefMap); 2428 } 2429 #if 0 2430 NdefMap->MifareULContainer.CurrentBlock = 2431 PH_FRINFC_NDEFMAP_MFUL_VAL4; 2432 2433 Result = phFriNfc_MfUL_H_Rd16Bytes(NdefMap); 2434 #endif 2435 2436 2437 *CRFlag = (uint8_t)((Result != NFCSTATUS_PENDING)? 2438 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 2439 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 2440 break; 2441 2442 case PH_FRINFC_NDEFMAP_WRITE_OPE: 2443 break; 2444 2445 default: 2446 break; 2447 } 2448 return Result; 2449 } 2450 2451 2452 2453 static NFCSTATUS phFriNfc_MfUL_H_CopyRdBytes(phFriNfc_NdefMap_t *NdefMap) 2454 { 2455 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2456 uint16_t localCurrentBlock; 2457 2458 #ifndef NDEF_READ_CHANGE 2459 uint16_t v_field_byte = 0; 2460 2461 if (NdefMap->MifareULContainer.CurrentBlock 2462 == NdefMap->TLVStruct.NdefTLVBlock) 2463 { 2464 if (NdefMap->CardMemSize > (0x12 * PH_FRINFC_NDEFMAP_MFUL_MUL8)) 2465 { 2466 v_field_byte = NdefMap->TLVStruct.NdefTLVByte; 2467 } 2468 2469 /* Calculate the Value field of the TLV to read */ 2470 if (NdefMap->TLVStruct.ActualSize >= 0xFF) 2471 { 2472 2473 /* here 2474 3 is the 3 LENGTH bytes to skip 2475 4 is the block size 2476 1 is to increment the byte number 2477 */ 2478 v_field_byte = (uint16_t) 2479 (((v_field_byte + 3) % 4) + 1); 2480 } 2481 else 2482 { 2483 /* less than 0xFF */ 2484 #if 0 2485 if ((0x03 == v_field_byte) 2486 || (0x04 == v_field_byte)) 2487 { 2488 /* 2489 here 2490 1 is the 1 LENGTH byte to skip 2491 4 is the block size 2492 1 is to increment the byte number 2493 */ 2494 v_field_byte = (uint16_t) 2495 (((v_field_byte + 1) % 4) + 1); 2496 } 2497 else 2498 { 2499 v_field_byte = (uint16_t) 2500 (v_field_byte + 1); 2501 } 2502 #endif /* #if 0 */ 2503 } 2504 } 2505 #endif /* #ifndef NDEF_READ_CHANGE */ 2506 2507 #ifndef NDEF_READ_CHANGE 2508 (void)memcpy(&(NdefMap->MifareULContainer.ReadBuf[ 2509 NdefMap->MifareULContainer.ReadBufIndex]), 2510 (void *)(NdefMap->SendRecvBuf + v_field_byte), 2511 (*NdefMap->SendRecvLength - v_field_byte)); 2512 2513 NdefMap->MifareULContainer.ReadBufIndex = (uint16_t) 2514 (NdefMap->MifareULContainer.ReadBufIndex + 2515 (*NdefMap->SendRecvLength - v_field_byte)); 2516 #else /* #ifndef NDEF_READ_CHANGE */ 2517 2518 (void)memcpy(&(NdefMap->MifareULContainer.ReadBuf[ 2519 NdefMap->MifareULContainer.ReadBufIndex]), 2520 NdefMap->SendRecvBuf, 2521 *NdefMap->SendRecvLength); 2522 2523 NdefMap->MifareULContainer.ReadBufIndex=NdefMap->MifareULContainer.ReadBufIndex +*NdefMap->SendRecvLength; 2524 #endif /* #ifndef NDEF_READ_CHANGE */ 2525 2526 localCurrentBlock = NdefMap->MifareULContainer.CurrentBlock+ 2527 (uint8_t)((NdefMap->MifareULContainer.ReadBufIndex != 2528 NdefMap->CardMemSize)? 2529 PH_FRINFC_NDEFMAP_MFUL_BLOCK4: 2530 PH_FRINFC_NDEFMAP_MFUL_VAL0); 2531 if (localCurrentBlock < 256) 2532 { 2533 NdefMap->MifareULContainer.CurrentBlock = NdefMap->MifareULContainer.CurrentBlock+ 2534 (uint8_t)((NdefMap->MifareULContainer.ReadBufIndex != 2535 NdefMap->CardMemSize)? 2536 PH_FRINFC_NDEFMAP_MFUL_BLOCK4: 2537 PH_FRINFC_NDEFMAP_MFUL_VAL0); 2538 } 2539 else 2540 { 2541 /* Go to next sector */ 2542 if (NdefMap->MifareULContainer.CurrentSector == 0) 2543 { 2544 NdefMap->MifareULContainer.CurrentSector++; 2545 NdefMap->MifareULContainer.CurrentBlock = 0xff; 2546 2547 Result = phFriNfc_MfUL_H_SelectSector(NdefMap, 2548 NdefMap->MifareULContainer.CurrentSector, 1, 2549 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_READ_1); 2550 } 2551 } 2552 2553 return Result; 2554 } 2555 2556 static NFCSTATUS phFriNfc_MfUL_H_CpDataToUserBuf(phFriNfc_NdefMap_t *NdefMap) 2557 { 2558 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2559 2560 /* Check the user buffer size with the 2561 L value of TLV */ 2562 if(NdefMap->ApduBufferSize >= 2563 NdefMap->TLVStruct.BytesRemainLinTLV) 2564 { 2565 (void)memcpy(NdefMap->ApduBuffer, 2566 &(NdefMap->MifareULContainer.ReadBuf[ 2567 NdefMap->MifareULContainer.ByteNumber]), 2568 NdefMap->TLVStruct.BytesRemainLinTLV); 2569 2570 *(NdefMap->NumOfBytesRead) = 2571 NdefMap->TLVStruct.BytesRemainLinTLV; 2572 NdefMap->MifareULContainer.ByteNumber = 2573 PH_FRINFC_NDEFMAP_MFUL_VAL0; 2574 NdefMap->MifareULContainer.ReadWriteCompleteFlag = 2575 PH_FRINFC_NDEFMAP_MFUL_FLAG1; 2576 NdefMap->MifareULContainer.RemainingSize = NdefMap->MifareULContainer.RemainingSize- 2577 NdefMap->TLVStruct.BytesRemainLinTLV; 2578 NdefMap->TLVStruct.BytesRemainLinTLV = 2579 PH_FRINFC_NDEFMAP_MFUL_VAL0; 2580 } 2581 else 2582 { 2583 (void)memcpy(NdefMap->ApduBuffer, 2584 &(NdefMap->MifareULContainer.ReadBuf[ 2585 NdefMap->MifareULContainer.ByteNumber]), 2586 NdefMap->ApduBufferSize); 2587 2588 *(NdefMap->NumOfBytesRead) = 2589 NdefMap->ApduBufferSize; 2590 NdefMap->MifareULContainer.ByteNumber = NdefMap->MifareULContainer.ByteNumber+ 2591 (uint16_t)NdefMap->ApduBufferSize; 2592 NdefMap->MifareULContainer.RemainingSize=NdefMap->MifareULContainer.RemainingSize- 2593 (uint16_t)NdefMap->ApduBufferSize; 2594 NdefMap->TLVStruct.BytesRemainLinTLV = NdefMap->TLVStruct.BytesRemainLinTLV- 2595 (uint16_t)NdefMap->ApduBufferSize; 2596 } 2597 return Result; 2598 } 2599 2600 static NFCSTATUS phFriNfc_MfUL_H_RdBeforeWrite(phFriNfc_NdefMap_t *NdefMap) 2601 { 2602 uint16_t localCurrentBlock = NdefMap->TLVStruct.NdefTLVBlock; 2603 2604 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2605 uint8_t index = PH_FRINFC_NDEFMAP_MFUL_VAL0, 2606 i = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2607 /* BytesToWrite = PH_FRINFC_NDEFMAP_MFUL_VAL0;*/ 2608 uint16_t TemLength = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2609 2610 switch((NdefMap->TLVStruct.NdefTLVByte - PH_FRINFC_NDEFMAP_MFUL_VAL1)) 2611 { 2612 case PH_FRINFC_NDEFMAP_MFUL_VAL0: 2613 /* go the NDEF TLV block to start write */ 2614 NdefMap->MifareULContainer.CurrentBlock = 2615 NdefMap->TLVStruct.NdefTLVBlock; 2616 /* fill send buffer for write */ 2617 NdefMap->SendRecvBuf[index] = 2618 NdefMap->MifareULContainer.CurrentBlock; 2619 index++; 2620 NdefMap->SendRecvBuf[index] = 2621 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_T; 2622 index++; 2623 if (NdefMap->ApduBufferSize > 254) 2624 { 2625 NdefMap->SendRecvBuf[index] = 0xFF; 2626 index++; 2627 NdefMap->SendRecvBuf[index] = 2628 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 2629 index++; 2630 NdefMap->SendRecvBuf[index] = 2631 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 2632 index++; 2633 } 2634 else 2635 { 2636 NdefMap->SendRecvBuf[index] = 2637 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 2638 index++; 2639 } 2640 2641 2642 break; 2643 2644 case PH_FRINFC_NDEFMAP_MFUL_VAL1: 2645 case PH_FRINFC_NDEFMAP_MFUL_VAL2: 2646 /* read to get the previous bytes */ 2647 Result = phFriNfc_MfUL_H_RdCardfindNdefTLV(NdefMap, 2648 NdefMap->TLVStruct.NdefTLVBlock); 2649 break; 2650 2651 case PH_FRINFC_NDEFMAP_MFUL_VAL3: 2652 2653 localCurrentBlock = (NdefMap->MifareULContainer.CurrentBlock + 2654 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2655 2656 if (localCurrentBlock < 256) 2657 { 2658 2659 NdefMap->MifareULContainer.CurrentBlock = 2660 (NdefMap->MifareULContainer.CurrentBlock + 2661 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2662 NdefMap->SendRecvBuf[index] = 2663 NdefMap->MifareULContainer.CurrentBlock; 2664 index++; 2665 2666 if (NdefMap->ApduBufferSize > 254) 2667 { 2668 NdefMap->SendRecvBuf[index] = 0xFF; 2669 index++; 2670 NdefMap->SendRecvBuf[index] = 2671 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 2672 index++; 2673 NdefMap->SendRecvBuf[index] = 2674 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 2675 index++; 2676 } 2677 else 2678 { 2679 NdefMap->SendRecvBuf[index] = 2680 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 2681 index++; 2682 } 2683 } 2684 else 2685 { 2686 /* Go to next sector */ 2687 NdefMap->MifareULContainer.CurrentSector++; 2688 2689 Result = phFriNfc_MfUL_H_SelectSector(NdefMap, 2690 NdefMap->MifareULContainer.CurrentSector, 1, 2691 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RW_1); 2692 } 2693 break; 2694 2695 default: 2696 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2697 NFCSTATUS_INVALID_REMOTE_DEVICE); 2698 break; 2699 } 2700 2701 if((((NdefMap->TLVStruct.NdefTLVByte - 2702 PH_FRINFC_NDEFMAP_MFUL_VAL1) == PH_FRINFC_NDEFMAP_MFUL_VAL0) || 2703 ((NdefMap->TLVStruct.NdefTLVByte - PH_FRINFC_NDEFMAP_MFUL_VAL1) 2704 == PH_FRINFC_NDEFMAP_MFUL_VAL3)) && localCurrentBlock < 256) 2705 { 2706 /* Length to know how many bytes has to be written to the card */ 2707 TemLength = (((NdefMap->TLVStruct.NdefTLVByte - PH_FRINFC_NDEFMAP_MFUL_VAL1) == 2708 PH_FRINFC_NDEFMAP_MFUL_VAL0)? 2709 PH_FRINFC_NDEFMAP_MFUL_VAL2: 2710 PH_FRINFC_NDEFMAP_MFUL_VAL3); 2711 2712 if (NdefMap->ApduBufferSize > 254) 2713 { 2714 TemLength -= 2; 2715 } 2716 2717 if(NdefMap->ApduBufferSize >= TemLength) 2718 { 2719 /* Prepare the receive buffer */ 2720 (void)memcpy(&(NdefMap->SendRecvBuf[ 2721 index]), 2722 &(NdefMap->ApduBuffer[ 2723 NdefMap->ApduBuffIndex]), 2724 TemLength); 2725 2726 /* Number of bytes written to the card from user buffer */ 2727 NdefMap->NumOfBytesWritten = TemLength; 2728 2729 index = index+(uint8_t)TemLength; 2730 /* Exact number of bytes written in the card including TLV */ 2731 if (index >= 1) 2732 { 2733 *NdefMap->DataCount = (index - PH_FRINFC_NDEFMAP_MFUL_VAL1); 2734 } 2735 else 2736 { 2737 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2738 NFCSTATUS_INVALID_REMOTE_DEVICE); 2739 } 2740 } 2741 else 2742 { 2743 /* Prepare the receive buffer */ 2744 (void)memcpy(&(NdefMap->SendRecvBuf[ 2745 index]), 2746 &(NdefMap->ApduBuffer[ 2747 NdefMap->ApduBuffIndex]), 2748 (uint16_t)NdefMap->ApduBufferSize); 2749 2750 /* Number of bytes written to the card from user buffer */ 2751 NdefMap->NumOfBytesWritten = (uint16_t)NdefMap->ApduBufferSize; 2752 2753 index= index +(uint8_t)NdefMap->ApduBufferSize; 2754 /* Exact number of bytes written in the card including TLV */ 2755 *NdefMap->DataCount = (index - PH_FRINFC_NDEFMAP_MFUL_VAL1); 2756 2757 for(i = index; i < PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK; i++) 2758 { 2759 NdefMap->SendRecvBuf[i] = (uint8_t)((i == index)? 2760 PH_FRINFC_NDEFMAP_MFUL_TERMTLV: 2761 PH_FRINFC_NDEFMAP_MFUL_NULLTLV); 2762 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 2763 } 2764 } 2765 2766 /* store the bytes in buffer till the bytes are 2767 written in a block */ 2768 (void)memcpy(NdefMap->MifareULContainer.Buffer, 2769 &(NdefMap->SendRecvBuf[ 2770 PH_FRINFC_NDEFMAP_MFUL_VAL1]), 2771 (PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK - 2772 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 2773 2774 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer, 2775 NdefMap->MifareULContainer.Buffer, 2776 (PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK - 2777 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 2778 2779 /* Change the state to check ndef compliancy */ 2780 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE; 2781 2782 Result = phFriNfc_MfUL_H_Wr4bytes(NdefMap); 2783 } 2784 return Result; 2785 } 2786 2787 static NFCSTATUS phFriNfc_MfUL_H_CallWrOp(phFriNfc_NdefMap_t *NdefMap) 2788 { 2789 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2790 /* uint8_t index = PH_FRINFC_NDEFMAP_MFUL_VAL1;*/ 2791 2792 2793 NdefMap->MifareULContainer.CurrentBlock = 2794 NdefMap->TLVStruct.NdefTLVBlock; 2795 2796 (void)memcpy(&(NdefMap->SendRecvBuf[ 2797 PH_FRINFC_NDEFMAP_MFUL_VAL1]), 2798 NdefMap->SendRecvBuf, 2799 PH_FRINFC_NDEFMAP_MFUL_VAL4); 2800 2801 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] = 2802 NdefMap->MifareULContainer.CurrentBlock; 2803 2804 if (NdefMap->ApduBufferSize > 254) 2805 { 2806 NdefMap->SendRecvBuf[(NdefMap->TLVStruct.NdefTLVByte + 2807 PH_FRINFC_NDEFMAP_MFUL_VAL1)] = 0xFF; 2808 2809 2810 if((NdefMap->TLVStruct.NdefTLVByte + PH_FRINFC_NDEFMAP_MFUL_VAL1) < 2811 PH_FRINFC_NDEFMAP_MFUL_VAL4) 2812 { 2813 NdefMap->SendRecvBuf[(NdefMap->TLVStruct.NdefTLVByte + 2814 PH_FRINFC_NDEFMAP_MFUL_VAL2 )] = 0x00; 2815 2816 NdefMap->NumOfLReminWrite = 1; 2817 2818 } 2819 else 2820 { 2821 NdefMap->NumOfLReminWrite = 2; 2822 } 2823 NdefMap->NumOfBytesWritten = 0; 2824 } 2825 else 2826 { 2827 /* Write the length value = 0 */ 2828 NdefMap->SendRecvBuf[(NdefMap->TLVStruct.NdefTLVByte + 2829 PH_FRINFC_NDEFMAP_MFUL_VAL1)] = 2830 PH_FRINFC_NDEFMAP_MFUL_NDEFTLV_L; 2831 2832 if((NdefMap->TLVStruct.NdefTLVByte + PH_FRINFC_NDEFMAP_MFUL_VAL1) < 2833 PH_FRINFC_NDEFMAP_MFUL_VAL4) 2834 { 2835 /* Only one byte */ 2836 (void)memcpy(&(NdefMap->SendRecvBuf[ 2837 PH_FRINFC_NDEFMAP_MFUL_VAL4]), 2838 &(NdefMap->ApduBuffer[ 2839 NdefMap->ApduBuffIndex]), 2840 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2841 /* Number of bytes written to the card from user buffer */ 2842 NdefMap->NumOfBytesWritten = PH_FRINFC_NDEFMAP_MFUL_VAL1; 2843 } 2844 } 2845 2846 (void)memcpy(NdefMap->MifareULContainer.Buffer, 2847 &(NdefMap->SendRecvBuf[ 2848 PH_FRINFC_NDEFMAP_MFUL_VAL1]), 2849 PH_FRINFC_NDEFMAP_MFUL_VAL4); 2850 2851 /* Copy the Ndef TLV buffer to send buffer */ 2852 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer, 2853 NdefMap->MifareULContainer.Buffer, 2854 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 2855 2856 /* Exact number of bytes written in the card including TLV */ 2857 *NdefMap->DataCount = PH_FRINFC_NDEFMAP_MFUL_VAL4; 2858 2859 /* Change the state to check ndef compliancy */ 2860 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE; 2861 2862 Result = phFriNfc_MfUL_H_Wr4bytes(NdefMap); 2863 2864 return Result; 2865 } 2866 2867 static NFCSTATUS phFriNfc_MfUL_H_ProWrittenBytes(phFriNfc_NdefMap_t *NdefMap) 2868 { 2869 uint16_t localCurrentBlock; 2870 NFCSTATUS Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2871 NFCSTATUS_INVALID_REMOTE_DEVICE); 2872 2873 if(NdefMap->ApduBuffIndex < NdefMap->ApduBufferSize) 2874 { 2875 NdefMap->ApduBuffIndex = NdefMap->ApduBuffIndex+NdefMap->NumOfBytesWritten; 2876 if(*NdefMap->DataCount < 2877 PH_FRINFC_NDEFMAP_MFUL_VAL4) 2878 { 2879 (void)memcpy(NdefMap->MifareULContainer.InternalBuf, 2880 NdefMap->MifareULContainer.Buffer, 2881 *NdefMap->DataCount); 2882 2883 NdefMap->MifareULContainer.InternalLength = *NdefMap->DataCount; 2884 } 2885 else 2886 { 2887 NdefMap->MifareULContainer.InternalLength = 2888 PH_FRINFC_NDEFMAP_MFUL_VAL0; 2889 } 2890 2891 NdefMap->MifareULContainer.RemainingSize= NdefMap->MifareULContainer.RemainingSize- 2892 NdefMap->NumOfBytesWritten; 2893 if((NdefMap->ApduBuffIndex == NdefMap->ApduBufferSize) || 2894 (NdefMap->MifareULContainer.RemainingSize == 2895 PH_FRINFC_NDEFMAP_MFUL_VAL0)) 2896 { 2897 Result = NFCSTATUS_SUCCESS; 2898 NdefMap->MifareULContainer.ReadWriteCompleteFlag = 2899 (uint8_t)((NdefMap->MifareULContainer.RemainingSize == 2900 PH_FRINFC_NDEFMAP_MFUL_VAL0)? 2901 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 2902 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 2903 2904 NdefMap->TLVStruct.SetTermTLVFlag = 2905 (uint8_t)(((NdefMap->MifareULContainer.RemainingSize == 2906 PH_FRINFC_NDEFMAP_MFUL_VAL0) || 2907 (NdefMap->TLVStruct.SetTermTLVFlag == 2908 PH_FRINFC_NDEFMAP_MFUL_FLAG1))? 2909 PH_FRINFC_NDEFMAP_MFUL_FLAG1: 2910 PH_FRINFC_NDEFMAP_MFUL_FLAG0); 2911 2912 NdefMap->MifareULContainer.CurrentBlock = NdefMap->MifareULContainer.CurrentBlock+ 2913 (uint8_t)((NdefMap->MifareULContainer.InternalLength != 2914 PH_FRINFC_NDEFMAP_MFUL_VAL0)? 2915 PH_FRINFC_NDEFMAP_MFUL_VAL0: 2916 PH_FRINFC_NDEFMAP_MFUL_VAL1); 2917 2918 *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex; 2919 } 2920 else 2921 { 2922 localCurrentBlock = NdefMap->MifareULContainer.CurrentBlock + 1; 2923 if (localCurrentBlock < 256) 2924 { 2925 NdefMap->MifareULContainer.CurrentBlock++; 2926 Result = phFriNfc_MfUL_H_fillSendBufToWr(NdefMap); 2927 } 2928 else 2929 { 2930 /* Go to next sector */ 2931 NdefMap->MifareULContainer.CurrentSector++; 2932 2933 Result = phFriNfc_MfUL_H_SelectSector(NdefMap, 2934 NdefMap->MifareULContainer.CurrentSector, 1, 2935 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_WRITE_1); 2936 } 2937 } 2938 } 2939 2940 if((Result == NFCSTATUS_SUCCESS) && 2941 (NdefMap->TLVStruct.SetTermTLVFlag != 2942 PH_FRINFC_NDEFMAP_MFUL_FLAG1) && 2943 (NdefMap->MifareULContainer.RemainingSize > 2944 PH_FRINFC_NDEFMAP_MFUL_VAL0)) 2945 { 2946 Result = phFriNfc_MfUL_H_WrTermTLV(NdefMap); 2947 } 2948 else 2949 { 2950 if((Result == NFCSTATUS_SUCCESS) && 2951 (NdefMap->TLVStruct.SetTermTLVFlag == 2952 PH_FRINFC_NDEFMAP_MFUL_FLAG1)) 2953 { 2954 Result = phFriNfc_MfUL_H_UpdateWrLen(NdefMap); 2955 } 2956 } 2957 return Result; 2958 } 2959 static NFCSTATUS phFriNfc_MfUL_H_fillSendBufToWr(phFriNfc_NdefMap_t *NdefMap) 2960 { 2961 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2962 uint16_t RemainingBytes = PH_FRINFC_NDEFMAP_MFUL_VAL0, 2963 BytesToWrite = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2964 uint8_t index = PH_FRINFC_NDEFMAP_MFUL_VAL0; 2965 2966 RemainingBytes = (uint16_t)(( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) <= 2967 NdefMap->MifareULContainer.RemainingSize)? 2968 (uint16_t)(NdefMap->ApduBufferSize - 2969 NdefMap->ApduBuffIndex): 2970 NdefMap->MifareULContainer.RemainingSize); 2971 2972 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] = 2973 NdefMap->MifareULContainer.CurrentBlock; 2974 2975 /* Get the number of bytes that can be written after copying 2976 the internal buffer */ 2977 BytesToWrite = ((RemainingBytes < 2978 (PH_FRINFC_NDEFMAP_MFUL_BYTE4 - 2979 NdefMap->MifareULContainer.InternalLength))? 2980 RemainingBytes: 2981 (PH_FRINFC_NDEFMAP_MFUL_BYTE4 - 2982 NdefMap->MifareULContainer.InternalLength)); 2983 2984 if (NdefMap->NumOfBytesWritten == 0 && NdefMap->NumOfLReminWrite > 0) 2985 { 2986 BytesToWrite = BytesToWrite - NdefMap->NumOfLReminWrite; 2987 2988 if (NdefMap->NumOfLReminWrite == 1) 2989 { 2990 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL1] = 0x00; 2991 } 2992 else 2993 { 2994 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL1] = 0x00; 2995 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL2] = 0x00; 2996 } 2997 } 2998 2999 if(NdefMap->MifareULContainer.InternalLength > 3000 PH_FRINFC_NDEFMAP_MFUL_VAL0) 3001 { 3002 /* copy the internal buffer to the send buffer */ 3003 (void)memcpy(&(NdefMap->SendRecvBuf[ 3004 PH_FRINFC_NDEFMAP_MFUL_VAL1]), 3005 NdefMap->MifareULContainer.InternalBuf, 3006 NdefMap->MifareULContainer.InternalLength); 3007 3008 } 3009 3010 /* Copy Bytes to write in the send buffer */ 3011 (void)memcpy(&(NdefMap->SendRecvBuf[ 3012 (PH_FRINFC_NDEFMAP_MFUL_VAL1 + 3013 NdefMap->MifareULContainer.InternalLength) + 3014 NdefMap->NumOfLReminWrite]), 3015 &(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex]), 3016 BytesToWrite); 3017 3018 /* update number of bytes written from the user buffer */ 3019 NdefMap->NumOfBytesWritten = BytesToWrite; 3020 3021 /* check the exact number of bytes written to a block including the 3022 internal length */ 3023 *NdefMap->DataCount = 3024 (BytesToWrite + NdefMap->MifareULContainer.InternalLength + 3025 NdefMap->NumOfLReminWrite); 3026 3027 3028 /* if total bytes to write in the card is less than 4 bytes then 3029 pad zeroes till 4 bytes */ 3030 if((BytesToWrite + NdefMap->MifareULContainer.InternalLength + 3031 NdefMap->NumOfLReminWrite) 3032 < PH_FRINFC_NDEFMAP_MFUL_BYTE4) 3033 { 3034 for(index = (uint8_t)((BytesToWrite + NdefMap->MifareULContainer.InternalLength + NdefMap->NumOfLReminWrite) + 3035 PH_FRINFC_NDEFMAP_MFUL_VAL1); 3036 index < PH_FRINFC_NDEFMAP_MFUL_WR_A_BLK; 3037 index++) 3038 { 3039 NdefMap->SendRecvBuf[index] = (uint8_t)((index == 3040 ((BytesToWrite + 3041 NdefMap->MifareULContainer.InternalLength + NdefMap->NumOfLReminWrite) + 3042 PH_FRINFC_NDEFMAP_MFUL_VAL1))? 3043 PH_FRINFC_NDEFMAP_MFUL_TERMTLV: 3044 PH_FRINFC_NDEFMAP_MFUL_NULLTLV); 3045 3046 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_NDEFMAP_MFUL_FLAG1; 3047 } 3048 } 3049 3050 /* A temporary buffer to hold four bytes of data that is 3051 written to the card */ 3052 (void)memcpy(NdefMap->MifareULContainer.Buffer, 3053 &(NdefMap->SendRecvBuf[ 3054 PH_FRINFC_NDEFMAP_MFUL_VAL1]), 3055 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3056 3057 3058 3059 if((NdefMap->TLVStruct.NdefTLVByte - PH_FRINFC_NDEFMAP_MFUL_VAL1) < 3060 PH_FRINFC_NDEFMAP_MFUL_VAL3) 3061 { 3062 if ((NdefMap->TLVStruct.NdefTLVSector == 3063 NdefMap->MifareULContainer.CurrentSector)) 3064 { 3065 if(NdefMap->MifareULContainer.CurrentBlock == 3066 NdefMap->TLVStruct.NdefTLVBlock) 3067 { 3068 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer, 3069 NdefMap->MifareULContainer.Buffer, 3070 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3071 } 3072 } 3073 3074 if ((NdefMap->TLVStruct.NdefTLVSector == 3075 NdefMap->MifareULContainer.CurrentSector) || 3076 (NdefMap->TLVStruct.NdefTLVBlock == 0xFF)) 3077 { 3078 if(NdefMap->MifareULContainer.CurrentBlock == 3079 (NdefMap->TLVStruct.NdefTLVBlock + PH_FRINFC_NDEFMAP_MFUL_VAL1)) 3080 { 3081 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer1, 3082 NdefMap->MifareULContainer.Buffer, 3083 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3084 } 3085 } 3086 } 3087 else 3088 { 3089 if ((NdefMap->TLVStruct.NdefTLVSector == 3090 NdefMap->MifareULContainer.CurrentSector)) 3091 { 3092 if(NdefMap->MifareULContainer.CurrentBlock == 3093 (NdefMap->TLVStruct.NdefTLVBlock + 3094 PH_FRINFC_NDEFMAP_MFUL_VAL1)) 3095 { 3096 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer, 3097 NdefMap->MifareULContainer.Buffer, 3098 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3099 } 3100 } 3101 3102 if ((NdefMap->TLVStruct.NdefTLVSector == 3103 NdefMap->MifareULContainer.CurrentSector)|| 3104 (NdefMap->TLVStruct.NdefTLVBlock == 0xFF)) 3105 { 3106 if(NdefMap->MifareULContainer.CurrentBlock == 3107 (NdefMap->TLVStruct.NdefTLVBlock + PH_FRINFC_NDEFMAP_MFUL_VAL2)) 3108 { 3109 (void)memcpy(NdefMap->TLVStruct.NdefTLVBuffer1, 3110 NdefMap->MifareULContainer.Buffer, 3111 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3112 } 3113 } 3114 } 3115 3116 3117 /* Change the state to check ndef compliancy */ 3118 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE; 3119 3120 NdefMap->NumOfLReminWrite = 0; 3121 3122 /* Start writing to the current block */ 3123 Result = phFriNfc_MfUL_H_Wr4bytes(NdefMap); 3124 3125 return Result; 3126 } 3127 static NFCSTATUS phFriNfc_MfUL_H_WrTermTLV(phFriNfc_NdefMap_t *NdefMap) 3128 { 3129 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3130 uint8_t index = PH_FRINFC_NDEFMAP_MFUL_VAL0, 3131 i = PH_FRINFC_NDEFMAP_MFUL_VAL0; 3132 3133 /* Change the state to check ndef compliancy */ 3134 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_TERM_TLV; 3135 3136 NdefMap->SendRecvBuf[index] = 3137 (NdefMap->MifareULContainer.CurrentBlock + 3138 PH_FRINFC_NDEFMAP_MFUL_VAL0); 3139 index++; 3140 NdefMap->SendRecvBuf[index] = PH_FRINFC_NDEFMAP_MFUL_TERMTLV; 3141 index++; 3142 3143 for(i = index; i < PH_FRINFC_NDEFMAP_MFUL_VAL4; i++) 3144 { 3145 NdefMap->SendRecvBuf[i] = PH_FRINFC_NDEFMAP_MFUL_NULLTLV; 3146 } 3147 3148 Result = phFriNfc_MfUL_H_Wr4bytes(NdefMap); 3149 return Result; 3150 } 3151 3152 static NFCSTATUS phFriNfc_MfUL_H_UpdateWrLen(phFriNfc_NdefMap_t *NdefMap) 3153 { 3154 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3155 uint16_t BlockNo = PH_FRINFC_NDEFMAP_MFUL_VAL0, 3156 ByteNo = PH_FRINFC_NDEFMAP_MFUL_VAL0; 3157 3158 if ((NdefMap->TLVStruct.NdefTLVSector == 3159 NdefMap->MifareULContainer.CurrentSector) || 3160 ((NdefMap->TLVStruct.NdefTLVBlock == 0xFF) && 3161 (NdefMap->TLVStruct.NdefTLVByte == 4) && 3162 (NdefMap->TLVStruct.NdefTLVSector == 0))) 3163 { 3164 BlockNo = (((NdefMap->TLVStruct.NdefTLVByte - 3165 PH_FRINFC_NDEFMAP_MFUL_VAL1) != 3166 PH_FRINFC_NDEFMAP_MFUL_VAL3)? 3167 NdefMap->TLVStruct.NdefTLVBlock: 3168 (NdefMap->TLVStruct.NdefTLVBlock + 3169 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 3170 3171 ByteNo = (((NdefMap->TLVStruct.NdefTLVByte - 3172 PH_FRINFC_NDEFMAP_MFUL_VAL1) == 3173 PH_FRINFC_NDEFMAP_MFUL_VAL3)? 3174 PH_FRINFC_NDEFMAP_MFUL_VAL1: 3175 (NdefMap->TLVStruct.NdefTLVByte + 3176 PH_FRINFC_NDEFMAP_MFUL_VAL1)); 3177 3178 if (NdefMap->NumOfLReminWrite > 0) 3179 { 3180 BlockNo++; 3181 3182 /* Copy the Ndef TLV buffer to send buffer */ 3183 (void)memcpy(&(NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL1]), 3184 NdefMap->TLVStruct.NdefTLVBuffer1, 3185 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3186 3187 if (NdefMap->NumOfLReminWrite == 1) 3188 { 3189 /* NdefMap->SendRecvBuf[1] = (uint8_t) ((NdefMap->ApduBuffIndex & 0xFF00) >> 8); */ 3190 NdefMap->SendRecvBuf[1] = (uint8_t) NdefMap->ApduBuffIndex; 3191 3192 } 3193 else if (NdefMap->NumOfLReminWrite == 2) 3194 { 3195 NdefMap->SendRecvBuf[1] = (uint8_t) ((NdefMap->ApduBuffIndex & 0xFF00) >> 8); 3196 NdefMap->SendRecvBuf[2]= (uint8_t) (NdefMap->ApduBuffIndex); 3197 3198 } 3199 else 3200 { 3201 3202 } 3203 NdefMap->NumOfLReminWrite = 0; 3204 } 3205 else 3206 { 3207 /* Copy the Ndef TLV buffer to send buffer */ 3208 (void)memcpy(&(NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL1]), 3209 NdefMap->TLVStruct.NdefTLVBuffer, 3210 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3211 3212 3213 if (NdefMap->ApduBuffIndex > 254) 3214 { 3215 ByteNo++; 3216 if ((ByteNo == 3) || (ByteNo == 2)) 3217 { 3218 NdefMap->SendRecvBuf[ByteNo]= (uint8_t) ((NdefMap->ApduBuffIndex & 0xFF00) >> 8); 3219 ByteNo++; 3220 NdefMap->SendRecvBuf[ByteNo] = (uint8_t) (NdefMap->ApduBuffIndex); 3221 ByteNo++; 3222 NdefMap->NumOfLReminWrite = 0; 3223 } 3224 else if (ByteNo == 4) 3225 { 3226 /* NdefMap->SendRecvBuf[ByteNo]= (uint8_t) (NdefMap->ApduBuffIndex); */ 3227 NdefMap->SendRecvBuf[ByteNo]= (uint8_t) ((NdefMap->ApduBuffIndex & 0xFF00) >> 8); 3228 ByteNo++; 3229 NdefMap->NumOfLReminWrite = 1; 3230 } 3231 else 3232 { 3233 NdefMap->NumOfLReminWrite = 2; 3234 } 3235 } 3236 else 3237 { 3238 NdefMap->SendRecvBuf[ByteNo]= 3239 (uint8_t)((NdefMap->Offset == 3240 PH_FRINFC_NDEFMAP_SEEK_BEGIN)? 3241 (uint8_t)NdefMap->ApduBuffIndex: 3242 (NdefMap->ApduBuffIndex + 3243 NdefMap->SendRecvBuf[ByteNo])); 3244 } 3245 } 3246 3247 (void)memcpy(NdefMap->MifareULContainer.Buffer, 3248 &(NdefMap->SendRecvBuf[ 3249 PH_FRINFC_NDEFMAP_MFUL_VAL1]), 3250 PH_FRINFC_NDEFMAP_MFUL_BYTE4); 3251 3252 NdefMap->SendRecvBuf[PH_FRINFC_NDEFMAP_MFUL_VAL0] = (uint8_t)BlockNo; 3253 Result = phFriNfc_MfUL_H_Wr4bytes(NdefMap); 3254 3255 if (NdefMap->NumOfLReminWrite == 0) 3256 { 3257 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_WR_LEN_TLV; 3258 } 3259 else 3260 { 3261 NdefMap->State = PH_FRINFC_NDEFMAP_MFUL_STATE_TERM_TLV; 3262 } 3263 } 3264 else if (NdefMap->TLVStruct.NdefTLVSector == 0) 3265 { 3266 /* Reset sector */ 3267 NdefMap->MifareULContainer.CurrentSector = 0; 3268 NdefMap->PrevState = PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE; 3269 3270 Result = phFriNfc_MfUL_H_SelectSector(NdefMap, 3271 NdefMap->MifareULContainer.CurrentSector, 1, 3272 PH_FRINFC_NDEFMAP_MFUL_STATE_SELECT_SECTOR_RESET_1); 3273 3274 } 3275 else 3276 { 3277 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST); 3278 } 3279 3280 3281 return Result; 3282 } 3283 #ifdef UNIT_TEST 3284 extern void phFriNfc_MifareUL_UnitTest(void *Context,uint32_t Length) 3285 { 3286 uint8_t value=10; 3287 uint8_t* CrFlag=&value; 3288 phFriNfc_NdefMap_t *pNdefMap=(phFriNfc_NdefMap_t*)Context; 3289 phFriNfc_MfUL_H_UpdateLen(pNdefMap,(uint16_t) Length); 3290 phFriNfc_MfUL_H_WrTermTLV(pNdefMap); 3291 phFriNfc_MfUL_H_CallWrOp(pNdefMap); 3292 phFriNfc_MfUL_H_UpdateWrLen(pNdefMap); 3293 phFriNfc_MfUL_H_ChkRemainTLV(pNdefMap,CrFlag); 3294 phFriNfc_MfUL_H_ProWrittenBytes(pNdefMap); 3295 3296 pNdefMap->PrevOperation=PH_FRINFC_NDEFMAP_READ_OPE; 3297 phFriNfc_MfUL_H_NxtOp(pNdefMap,CrFlag); 3298 3299 pNdefMap->PrevOperation=PH_FRINFC_NDEFMAP_WRITE_OPE; 3300 phFriNfc_MfUL_H_NxtOp(pNdefMap,CrFlag); 3301 3302 pNdefMap->TLVStruct.NoLbytesinTLV=PH_FRINFC_NDEFMAP_MFUL_VAL1; 3303 phFriNfc_MfUL_H_ChkRemainTLV(pNdefMap,CrFlag); 3304 3305 pNdefMap->SendRecvBuf[0x00]= PH_FRINFC_NDEFMAP_MFUL_NULLTLV; 3306 phFriNfc_MfUL_H_findNDEFTLV(pNdefMap,CrFlag); 3307 3308 3309 pNdefMap->SendRecvBuf[0x00]= PH_FRINFC_NDEFMAP_MFUL_NULLTLV; 3310 phFriNfc_MfUL_H_findNDEFTLV(pNdefMap,CrFlag); 3311 3312 3313 phFriNfc_MfUL_H_RdBeforeWrite(pNdefMap); 3314 pNdefMap->TLVStruct.NdefTLVByte=1; 3315 phFriNfc_MfUL_H_RdBeforeWrite(pNdefMap); 3316 3317 pNdefMap->TLVStruct.NdefTLVByte=3; 3318 phFriNfc_MfUL_H_RdBeforeWrite(pNdefMap); 3319 3320 pNdefMap->TLVStruct.NdefTLVByte=4; 3321 phFriNfc_MfUL_H_RdBeforeWrite(pNdefMap); 3322 3323 3324 phFriNfc_MifareUL_H_Complete(pNdefMap,NFCSTATUS_SUCCESS); 3325 phFriNfc_MifareUL_H_Complete(NULL,NFCSTATUS_SUCCESS); 3326 phFriNfc_MifareUL_H_Complete(pNdefMap,NFCSTATUS_SUCCESS); 3327 pNdefMap->State=PH_FRINFC_NDEFMAP_MFUL_STATE_WRITE; 3328 phFriNfc_MifareUL_H_Complete(pNdefMap,NFCSTATUS_SUCCESS); 3329 3330 phFriNfc_MifareUL_H_Complete(pNdefMap,NFCSTATUS_FAILED); 3331 phFriNfc_MifareUL_H_Complete(NULL,NFCSTATUS_SUCCESS); 3332 phFriNfc_MifareUL_H_Complete(pNdefMap,NFCSTATUS_FAILED); 3333 3334 *pNdefMap->DataCount=0x3; 3335 phFriNfc_MfUL_H_ProWrittenBytes(pNdefMap); 3336 3337 pNdefMap->ApduBuffIndex=0x31; 3338 phFriNfc_MfUL_H_ProWrittenBytes(pNdefMap); 3339 3340 3341 3342 } 3343 3344 #endif 3345 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */ 3346 3347