1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * NFC Ndef Mapping For Remote Devices. 19 * 20 */ 21 22 #include <phFriNfc_MifStdFormat.h> 23 #include <phNfcCompId.h> 24 #include <phNxpExtns_MifareStd.h> 25 #include <phFriNfc_MifareStdMap.h> 26 27 /**************** local methods used in this file only ************************/ 28 static NFCSTATUS phFriNfc_MifStd_H_RdABlock (phFriNfc_NdefMap_t *NdefMap); 29 static NFCSTATUS phFriNfc_MifStd_H_WrABlock (phFriNfc_NdefMap_t *NdefMap); 30 static NFCSTATUS phFriNfc_MifStd_H_AuthSector (phFriNfc_NdefMap_t *NdefMap); 31 static NFCSTATUS phFriNfc_MifStd4k_H_CheckNdef (phFriNfc_NdefMap_t *NdefMap); 32 static void phFriNfc_MifStd_H_fillAIDarray (phFriNfc_NdefMap_t *NdefMap); 33 static uint8_t phFriNfc_MifStd_H_GetSect (uint8_t BlockNumber); 34 static NFCSTATUS phFriNfc_MifStd_H_BlkChk (phFriNfc_NdefMap_t *NdefMap); 35 static NFCSTATUS phFriNfc_MifStd_H_RdAcsBit (phFriNfc_NdefMap_t *NdefMap); 36 static NFCSTATUS phFriNfc_MifStd_H_ChkAcsBit (phFriNfc_NdefMap_t *NdefMap); 37 static NFCSTATUS phFriNfc_MifStd_H_ChkRdWr (phFriNfc_NdefMap_t *NdefMap); 38 static void phFriNfc_MifStd_H_ChkNdefCmpltSects (phFriNfc_NdefMap_t *NdefMap); 39 static NFCSTATUS phFriNfc_MifStd_H_RemainTLV (phFriNfc_NdefMap_t *NdefMap, 40 uint8_t *Flag, 41 uint8_t *Temp16Bytes); 42 static NFCSTATUS phFriNfc_MifStd_H_ChkIntLen (phFriNfc_NdefMap_t *NdefMap); 43 static NFCSTATUS phFriNfc_MifStd_H_IntLenWioutNdef (phFriNfc_NdefMap_t *NdefMap, 44 uint8_t *Flag, 45 uint8_t *TempintBytes); 46 static uint8_t phFriNfc_MifStd_H_UpdateTLV (phFriNfc_NdefMap_t *NdefMap); 47 static NFCSTATUS phFriNfc_MifStd_H_WriteNdefLen (phFriNfc_NdefMap_t *NdefMap); 48 static void phFriNfc_MifStd_H_SetNdefBlkAuth (phFriNfc_NdefMap_t *NdefMap); 49 static void phFriNfc_MifStd_H_RdWrReset (phFriNfc_NdefMap_t *NdefMap); 50 static NFCSTATUS phFriNfc_MifStd_H_RdtoWrNdefLen (phFriNfc_NdefMap_t *NdefMap); 51 static NFCSTATUS phFriNfc_MifStd_H_GetActCardLen (phFriNfc_NdefMap_t *NdefMap); 52 static NFCSTATUS phFriNfc_MifStd_H_ChkTLVs (phFriNfc_NdefMap_t *NdefMap, 53 uint8_t *CRFlag); 54 static NFCSTATUS phFriNfc_MifStd_H_GetNxtTLV (phFriNfc_NdefMap_t *NdefMap, 55 uint16_t *TempLength, 56 uint8_t *TL4bytesFlag); 57 static NFCSTATUS phFriNfc_MifStd_H_Chk16Bytes (phFriNfc_NdefMap_t *NdefMap, 58 uint16_t TempLength); 59 static NFCSTATUS phFriNfc_MifStd_H_ChkRemainTLVs (phFriNfc_NdefMap_t *NdefMap, 60 uint8_t *CRFlag, 61 uint8_t *NDEFFlag); 62 static void phFriNfc_MifStd_H_Complete (phFriNfc_NdefMap_t *NdefMap, 63 NFCSTATUS Result); 64 static void phFriNfc_MifStd_H_Get1kStTrail (phFriNfc_NdefMap_t *NdefMap); 65 static void phFriNfc_MifStd_H_Get4kStTrail (phFriNfc_NdefMap_t *NdefMap); 66 static NFCSTATUS phFriNfc_MifStd_H_ProChkNdef (phFriNfc_NdefMap_t *NdefMap); 67 static NFCSTATUS phFriNfc_MifStd_H_ProAuth (phFriNfc_NdefMap_t *NdefMap); 68 static NFCSTATUS phFriNfc_MifStd_H_Rd16Bytes (phFriNfc_NdefMap_t *NdefMap, 69 uint8_t BlockNo); 70 static NFCSTATUS phFriNfc_MifStd_H_ProAcsBits (phFriNfc_NdefMap_t *NdefMap); 71 static NFCSTATUS phFriNfc_MifStd_H_GPBChk (phFriNfc_NdefMap_t *NdefMap); 72 static NFCSTATUS phFriNfc_MifStd_H_ProStatNotValid (phFriNfc_NdefMap_t *NdefMap, 73 NFCSTATUS status); 74 static NFCSTATUS phFriNfc_MifStd_H_RdBeforeWr (phFriNfc_NdefMap_t *NdefMap); 75 static NFCSTATUS phFriNfc_MifStd_H_ProBytesToWr (phFriNfc_NdefMap_t *NdefMap); 76 static NFCSTATUS phFriNfc_MifStd_H_fillSendBuf (phFriNfc_NdefMap_t *NdefMap, 77 uint8_t Length); 78 static NFCSTATUS phFriNfc_MifStd_H_WrTLV (phFriNfc_NdefMap_t *NdefMap); 79 static NFCSTATUS phFriNfc_MifStd_H_ProWrTLV (phFriNfc_NdefMap_t *NdefMap); 80 static uint8_t phFriNfc_MifStd_H_UpdRemTLV (phFriNfc_NdefMap_t *NdefMap); 81 static void phFriNfc_MifStd_H_fillTLV1 (phFriNfc_NdefMap_t *NdefMap); 82 static void phFriNfc_MifStd_H_fillTLV2 (phFriNfc_NdefMap_t *NdefMap); 83 static NFCSTATUS phFriNfc_MifStd_H_CallWrNdefLen (phFriNfc_NdefMap_t *NdefMap); 84 static NFCSTATUS phFriNfc_MifStd_H_BlkChk_1 (phFriNfc_NdefMap_t *NdefMap); 85 static void phFriNfc_MifStd_H_fillTLV1_1 (phFriNfc_NdefMap_t *NdefMap); 86 static void phFriNfc_MifStd_H_fillTLV2_1 (phFriNfc_NdefMap_t *NdefMap); 87 static NFCSTATUS phFriNfc_MifStd_H_RdTLV (phFriNfc_NdefMap_t *NdefMap); 88 static NFCSTATUS phFriNfc_MifStd_H_ProRdTLV (phFriNfc_NdefMap_t *NdefMap); 89 static NFCSTATUS phFriNfc_MifStd_H_WrTermTLV (phFriNfc_NdefMap_t *NdefMap); 90 static NFCSTATUS phFriNfc_MifStd_H_ProWrABlock (phFriNfc_NdefMap_t *NdefMap); 91 static NFCSTATUS phFriNfc_MifStd_H_CallConnect (phFriNfc_NdefMap_t *NdefMap); 92 static NFCSTATUS phFriNfc_MifStd_H_CallDisCon (phFriNfc_NdefMap_t *NdefMap); 93 static void phFriNfc_MifStd1k_H_BlkChk (phFriNfc_NdefMap_t *NdefMap, 94 uint8_t SectorID, 95 uint8_t *callbreak); 96 static uint8_t phFriNfc_MifStd_H_GetSectorTrailerBlkNo (uint8_t SectorID); 97 static NFCSTATUS phFriNfc_MifStd_H_ProSectorTrailorAcsBits (phFriNfc_NdefMap_t *NdefMap); 98 static NFCSTATUS phFriNfc_MifStd_H_WrSectorTrailorBlock (phFriNfc_NdefMap_t *NdefMap); 99 static NFCSTATUS phFriNfc_MifStd_H_ProWrSectorTrailor (phFriNfc_NdefMap_t *NdefMap); 100 static NFCSTATUS phFriNfc_MapTool_ChkSpcVer (const phFriNfc_NdefMap_t *NdefMap, 101 uint8_t VersionIndex) __attribute__((unused)); 102 103 /* Mifare Standard Mapping - Constants */ 104 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT1 0xA0 /* internal Authenticate Command for MAD Sector */ 105 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT2 0xA1 /* internal Authenticate Command for MAD Sector */ 106 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT3 0xA2 /* internal Authenticate Command for MAD Sector */ 107 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT4 0xA3 /* internal Authenticate Command for MAD Sector */ 108 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT5 0xA4 /* internal Authenticate Command for MAD Sector */ 109 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT6 0xA5 /* internal Authenticate Command for MAD Sector */ 110 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1 0xD3 /* internal Authenticate Command for NDEF Sectors 1 */ 111 #define PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2 0xF7 /* internal Authenticate Command for NDEF Sectors 2 */ 112 #define PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL2 0x03 /* internal Ndef Compliant command 1 */ 113 #define PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL1 0xE1 /* internal Ndef Compliant command 2 */ 114 115 /* Enable access bits check for the MAD sector 116 #define ENABLE_ACS_BIT_CHK_FOR_MAD */ 117 118 #define PH_FRINFC_NDEFMAP_MFUL_VAL0 0 119 120 /****************************************************************************** 121 * Function phFriNfc_MapTool_SetCardState 122 * 123 * Description This function sets the appropriate card state. 124 * 125 * Returns This function return NFCSTATUS_SUCCESS in case of success 126 * In case of failure returns other failure value. 127 * 128 ******************************************************************************/ 129 NFCSTATUS phFriNfc_MapTool_SetCardState (phFriNfc_NdefMap_t *NdefMap, 130 uint32_t Length) 131 { 132 NFCSTATUS Result = NFCSTATUS_SUCCESS; 133 134 if (Length == PH_FRINFC_NDEFMAP_MFUL_VAL0) 135 { 136 /* As the NDEF LEN / TLV Len is Zero, irrespective of any state the card 137 shall be set to INITIALIZED STATE*/ 138 NdefMap->CardState =(uint8_t) (((NdefMap->CardState == 139 PH_NDEFMAP_CARD_STATE_READ_ONLY) || 140 (NdefMap->CardState == 141 PH_NDEFMAP_CARD_STATE_INVALID))? 142 PH_NDEFMAP_CARD_STATE_INVALID: 143 PH_NDEFMAP_CARD_STATE_INITIALIZED); 144 } 145 else 146 { 147 switch (NdefMap->CardState) 148 { 149 case PH_NDEFMAP_CARD_STATE_INITIALIZED: 150 NdefMap->CardState =(uint8_t) ((NdefMap->CardState == 151 PH_NDEFMAP_CARD_STATE_INVALID)? 152 NdefMap->CardState: 153 PH_NDEFMAP_CARD_STATE_READ_WRITE); 154 break; 155 156 case PH_NDEFMAP_CARD_STATE_READ_ONLY: 157 NdefMap->CardState = (uint8_t) ((NdefMap->CardState == 158 PH_NDEFMAP_CARD_STATE_INVALID)? 159 NdefMap->CardState: 160 PH_NDEFMAP_CARD_STATE_READ_ONLY); 161 break; 162 163 case PH_NDEFMAP_CARD_STATE_READ_WRITE: 164 NdefMap->CardState = (uint8_t) ((NdefMap->CardState == 165 PH_NDEFMAP_CARD_STATE_INVALID)? 166 NdefMap->CardState: 167 PH_NDEFMAP_CARD_STATE_READ_WRITE); 168 if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD || 169 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD || 170 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD) 171 { 172 if(NdefMap->StdMifareContainer.ReadOnlySectorIndex && 173 NdefMap->StdMifareContainer.SectorTrailerBlockNo == NdefMap->StdMifareContainer.currentBlock ) 174 { 175 NdefMap->CardState = (uint8_t) ((NdefMap->CardState == 176 PH_NDEFMAP_CARD_STATE_INVALID)? 177 NdefMap->CardState: 178 PH_NDEFMAP_CARD_STATE_READ_ONLY); 179 } 180 } 181 break; 182 183 default: 184 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 185 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 186 NFCSTATUS_NO_NDEF_SUPPORT); 187 break; 188 } 189 } 190 Result = ((NdefMap->CardState == 191 PH_NDEFMAP_CARD_STATE_INVALID)? 192 PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 193 NFCSTATUS_NO_NDEF_SUPPORT): 194 Result); 195 196 return Result; 197 } 198 199 /****************************************************************************** 200 * Function phFriNfc_MifareStdMap_H_Reset 201 * 202 * Description This function resets the component instance to the initial 203 * state and lets the component forget about the list of 204 * registered items. Moreover, the lower device is set. 205 * 206 * Returns This function return NFCSTATUS_SUCCESS in case of success 207 * In case of failure returns other failure value. 208 * 209 ******************************************************************************/ 210 NFCSTATUS phFriNfc_MifareStdMap_H_Reset (phFriNfc_NdefMap_t *NdefMap) 211 { 212 NFCSTATUS status = NFCSTATUS_SUCCESS; 213 uint8_t index = PH_FRINFC_MIFARESTD_VAL0; 214 215 if (NdefMap == NULL) 216 { 217 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 218 } 219 else 220 { 221 /* Current Block stores the present block accessed in the card */ 222 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_VAL0; 223 224 for (index = PH_FRINFC_MIFARESTD_VAL0; index < PH_FRINFC_NDEFMAP_MIFARESTD_ST15_BYTES; index++) 225 { 226 /* internal buffer to store the odd bytes of length < 15 */ 227 NdefMap->StdMifareContainer.internalBuf[index] = PH_FRINFC_MIFARESTD_VAL0; 228 } 229 230 for (index = 0; index < PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK; index++) 231 { 232 /* aid buffer reset to non ndef compliant */ 233 NdefMap->StdMifareContainer.aid[index] = PH_FRINFC_MIFARESTD_NON_NDEF_COMP; 234 } 235 236 /* odd bytes length stored in the internal buffer */ 237 NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0; 238 239 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INITIALIZED; 240 241 /* Flag to get that last few bytes are taken from the user buffer */ 242 NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_FLAG0; 243 244 /* Flag to find that the read/write operation has reached the end of the card. 245 Further reading/writing is not possible */ 246 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0; 247 248 /* Flag to get that last few bytes are taken from the internal buffer */ 249 NdefMap->StdMifareContainer.internalBufFlag = PH_FRINFC_MIFARESTD_FLAG0; 250 251 /* Authentication Flag for every sector */ 252 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0; 253 254 /* Used in Check Ndef for storing the sector ID */ 255 NdefMap->StdMifareContainer.SectorIndex = PH_FRINFC_MIFARESTD_VAL0; 256 257 NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL0; 258 259 NdefMap->StdMifareContainer.NoOfNdefCompBlocks = PH_FRINFC_MIFARESTD_VAL0; 260 261 NdefMap->StdMifareContainer.ReadAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG0; 262 263 NdefMap->StdMifareContainer.remSizeUpdFlag = PH_FRINFC_MIFARESTD_FLAG0; 264 265 NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL0; 266 267 NdefMap->TLVStruct.prevLenByteValue = PH_FRINFC_MIFARESTD_VAL0; 268 269 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0; 270 271 NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_MIFARESTD_VAL0; 272 273 NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0; 274 275 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 276 277 NdefMap->StdMifareContainer.remainingSize = PH_FRINFC_MIFARESTD_VAL0; 278 279 NdefMap->StdMifareContainer.ReadNdefFlag = PH_FRINFC_MIFARESTD_FLAG0; 280 281 NdefMap->StdMifareContainer.WrNdefFlag = PH_FRINFC_MIFARESTD_FLAG0; 282 283 NdefMap->StdMifareContainer.ChkNdefFlag = PH_FRINFC_MIFARESTD_FLAG0; 284 285 NdefMap->StdMifareContainer.aidCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0; 286 287 NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0; 288 289 NdefMap->StdMifareContainer.ProprforumSectFlag = PH_FRINFC_MIFARESTD_PROP_1ST_CONFIG; 290 291 NdefMap->StdMifareContainer.ReadCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0; 292 293 NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0; 294 295 NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL1; 296 297 NdefMap->StdMifareContainer.ChkNdefCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0; 298 299 NdefMap->StdMifareContainer.ReadOnlySectorIndex = PH_FRINFC_MIFARESTD_FLAG0; 300 301 NdefMap->StdMifareContainer.TotalNoSectors = PH_FRINFC_MIFARESTD_FLAG0; 302 303 NdefMap->StdMifareContainer.SectorTrailerBlockNo = PH_FRINFC_MIFARESTD_FLAG0; 304 } 305 306 return status; 307 } 308 309 /****************************************************************************** 310 * Function phFriNfc_MifareStdMap_ChkNdef 311 * 312 * Description The function checks whether the peer device is NDEF compliant. 313 * 314 * Returns This function return NFCSTATUS_PENDING in case of success 315 * In case of failure returns other failure value. 316 * 317 ******************************************************************************/ 318 NFCSTATUS phFriNfc_MifareStdMap_ChkNdef ( phFriNfc_NdefMap_t *NdefMap) 319 { 320 NFCSTATUS status = NFCSTATUS_PENDING; 321 uint8_t atq, sak; 322 323 if (NdefMap == NULL) 324 { 325 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 326 } 327 else 328 { 329 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_CHECK_OPE; 330 NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_CHK_NDEF; 331 332 /* Get the Select Response and Sense Response to get 333 the exact Card Type either Mifare 1k or 4k */ 334 sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak; 335 atq = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA[0]; 336 337 if (0x08 == (sak & 0x18)) 338 { 339 /* Total Number of Blocks in Mifare 1k Card */ 340 NdefMap->StdMifareContainer.NoOfNdefCompBlocks = 341 PH_FRINFC_NDEFMAP_MIFARESTD_1KNDEF_COMPBLOCK; 342 NdefMap->StdMifareContainer.remainingSize = 343 ((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0) ? 344 (PH_FRINFC_NDEFMAP_MIFARESTD_1KNDEF_COMPBLOCK * 345 PH_FRINFC_MIFARESTD_BLOCK_BYTES) : 346 NdefMap->StdMifareContainer.remainingSize); 347 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD; 348 } 349 else if (0x19 == (sak & 0x19)) 350 { 351 /* Total Number of Blocks in Mifare 2k Card */ 352 NdefMap->StdMifareContainer.NoOfNdefCompBlocks = 353 PH_FRINFC_NDEFMAP_MIFARESTD_2KNDEF_COMPBLOCK; 354 NdefMap->StdMifareContainer.remainingSize = 355 ((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0) ? 356 (PH_FRINFC_NDEFMAP_MIFARESTD_2KNDEF_COMPBLOCK * 357 PH_FRINFC_MIFARESTD_BLOCK_BYTES) : 358 NdefMap->StdMifareContainer.remainingSize); 359 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD; 360 } 361 else 362 { 363 /* Total Number of Blocks in Mifare 4k Card */ 364 NdefMap->StdMifareContainer.NoOfNdefCompBlocks = 365 PH_FRINFC_NDEFMAP_MIFARESTD_4KNDEF_COMPBLOCK; 366 NdefMap->StdMifareContainer.remainingSize = 367 ((NdefMap->CardType == PH_FRINFC_MIFARESTD_VAL0) ? 368 (PH_FRINFC_NDEFMAP_MIFARESTD_4KNDEF_COMPBLOCK * 369 PH_FRINFC_MIFARESTD_BLOCK_BYTES) : 370 NdefMap->StdMifareContainer.remainingSize); 371 NdefMap->CardType = PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD; 372 } 373 374 375 /* phFriNfc_MifareStdMap_ChkNdef should be called only 376 when currentBlock is 0 OR 64,65 and 66 (for Mifare 4k). 377 Otherwise return error */ 378 /* and also Check the Authentication Flag */ 379 if ((NdefMap->StdMifareContainer.currentBlock != 0) && 380 (NdefMap->StdMifareContainer.currentBlock != 1) && 381 (NdefMap->StdMifareContainer.currentBlock != 2) && 382 (NdefMap->StdMifareContainer.currentBlock != 64) && 383 (NdefMap->StdMifareContainer.currentBlock != 65) && 384 (NdefMap->StdMifareContainer.currentBlock != 66)) 385 { 386 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 387 } 388 else if ( NdefMap->StdMifareContainer.AuthDone == 0) 389 { 390 /* Block 0 contains Manufacturer information and 391 also other informaton. So go for block 1 which 392 contains AIDs. Authenticating any of the block 393 in a sector, Authenticates the whole sector */ 394 if (NdefMap->StdMifareContainer.currentBlock == 0) 395 { 396 NdefMap->StdMifareContainer.currentBlock = 1; 397 } 398 status = phFriNfc_MifStd_H_AuthSector (NdefMap); 399 } 400 else 401 { 402 /** 403 * Mifare 1k, sak = 0x08 atq = 0x04 404 * Mifare 2k, sak = 0x19 atq = 0x02 405 * Mifare 4k, sak = 0x18 atq = 0x02 406 */ 407 if ((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD) || 408 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) || 409 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)) 410 { 411 /* Change the state to Check Ndef Compliant */ 412 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_CHK_NDEF_COMP; 413 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_CHECK_OPE; 414 NdefMap->StdMifareContainer.ChkNdefFlag = PH_FRINFC_MIFARESTD_FLAG1; 415 416 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 417 NdefMap->MapCompletionInfo.Context = NdefMap; 418 419 NdefMap->Cmd.MfCmd = phHal_eMifareRead; 420 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 421 NdefMap->SendRecvBuf [0] = NdefMap->StdMifareContainer.currentBlock; 422 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ; 423 424 /* Call the Overlapped HAL Transceive function */ 425 status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo, 426 NdefMap->Cmd, 427 NdefMap->SendRecvBuf, 428 NdefMap->SendLength, 429 NdefMap->SendRecvLength); 430 } 431 else 432 { 433 /* Since we have decided temporarily not to go 434 for any new error codes we are using 435 NFCSTATUS_INVALID_PARAMETER even though it is not 436 the relevant error code here TBD */ 437 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 438 } 439 } 440 } 441 442 return status; 443 } 444 445 /****************************************************************************** 446 * Function phFriNfc_MifareStdMap_RdNdef 447 * 448 * Description The function initiates the reading of NDEF information from 449 * a Remote Device. It performs a reset of the state and starts 450 * the action (state machine). A periodic call of the 451 * phFriNfcNdefMap_Process has to be done once the action 452 * has been triggered. 453 * 454 * Returns This function return NFCSTATUS_PENDING in case of success 455 * In case of failure returns other failure value. 456 * 457 ******************************************************************************/ 458 NFCSTATUS phFriNfc_MifareStdMap_RdNdef (phFriNfc_NdefMap_t *NdefMap, 459 uint8_t *PacketData, 460 uint32_t *PacketDataLength, 461 uint8_t Offset) 462 { 463 NFCSTATUS status = NFCSTATUS_PENDING; 464 465 NdefMap->ApduBufferSize = *PacketDataLength; 466 NdefMap->NumOfBytesRead = PacketDataLength; 467 *NdefMap->NumOfBytesRead = 0; 468 NdefMap->ApduBuffIndex = 0; 469 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE; 470 NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_RD_NDEF; 471 472 if ((NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) 473 || (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED)) 474 { 475 /* Card state is not correct */ 476 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 477 NFCSTATUS_INVALID_PARAMETER); 478 } 479 else 480 { 481 if ((Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || ( NdefMap->PrevOperation == 482 PH_FRINFC_NDEFMAP_WRITE_OPE)) 483 { 484 phFriNfc_MifStd_H_RdWrReset (NdefMap); 485 NdefMap->StdMifareContainer.ReadNdefFlag = PH_FRINFC_MIFARESTD_FLAG1; 486 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0; 487 } 488 /* Offset = Current, but the read has reached the End of Card */ 489 if ((Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && 490 (NdefMap->StdMifareContainer.ReadWriteCompleteFlag == 491 PH_FRINFC_MIFARESTD_FLAG1)) 492 { 493 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED); 494 } 495 else 496 { 497 NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) && 498 (NdefMap->PrevOperation == 499 PH_FRINFC_NDEFMAP_WRITE_OPE)) ? 500 PH_FRINFC_NDEFMAP_SEEK_BEGIN : 501 Offset); 502 status = phFriNfc_MifStd_H_BlkChk (NdefMap); 503 if (status == NFCSTATUS_SUCCESS) 504 { 505 NdefMap->ApduBuffer = PacketData; 506 507 /* Read Operation in Progress */ 508 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0; 509 510 /* Check Authentication Flag */ 511 status = 512 ((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1) ? 513 phFriNfc_MifStd_H_RdABlock(NdefMap) : 514 phFriNfc_MifStd_H_AuthSector(NdefMap)); 515 } 516 } 517 } 518 519 return status; 520 } 521 522 /****************************************************************************** 523 * Function phFriNfc_MifareStdMap_WrNdef 524 * 525 * Description The function initiates the writing of NDEF information to 526 * a Remote Device. It performs a reset of the state and starts 527 * the action (state machine). A periodic call of the 528 * phFriNfcNdefMap_Process has to be done once the action 529 * has been triggered. 530 * 531 * Returns This function return NFCSTATUS_PENDING in case of success 532 * In case of failure returns other failure value. 533 * 534 ******************************************************************************/ 535 NFCSTATUS phFriNfc_MifareStdMap_WrNdef (phFriNfc_NdefMap_t *NdefMap, 536 uint8_t *PacketData, 537 uint32_t *PacketDataLength, 538 uint8_t Offset) 539 { 540 NFCSTATUS status = NFCSTATUS_PENDING; 541 542 NdefMap->ApduBuffer = PacketData; 543 NdefMap->ApduBufferSize = *PacketDataLength; 544 NdefMap->ApduBuffIndex = PH_FRINFC_MIFARESTD_VAL0; 545 NdefMap->WrNdefPacketLength = PacketDataLength; 546 *NdefMap->WrNdefPacketLength = PH_FRINFC_MIFARESTD_VAL0; 547 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE; 548 NdefMap->StdMifareContainer.CRIndex = PH_FRINFC_NDEFMAP_CR_WR_NDEF; 549 550 if ((NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) 551 || (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY)) 552 { 553 /* Card state is not correct */ 554 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 555 NFCSTATUS_INVALID_PARAMETER); 556 } 557 else 558 { 559 if ((Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || 560 (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE)) 561 { 562 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0; 563 NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG1; 564 NdefMap->StdMifareContainer.WrNdefFlag = PH_FRINFC_MIFARESTD_FLAG1; 565 NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0; 566 NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0; 567 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0; 568 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 569 NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0; 570 NdefMap->TLVStruct.NdefTLVAuthFlag = PH_FRINFC_MIFARESTD_FLAG0; 571 NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0; 572 NdefMap->StdMifareContainer.remainingSize = 573 (NdefMap->StdMifareContainer.NoOfNdefCompBlocks * 574 PH_FRINFC_MIFARESTD_BLOCK_BYTES); 575 NdefMap->StdMifareContainer.currentBlock = 576 PH_FRINFC_MIFARESTD_BLK4; 577 NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL1; 578 NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0; 579 /* This macro is added, to be compliant with the previous HAL 2.0 580 For HAL 2.0, polling is done before writing data to the mifare 581 std (if the offset is BEGIN), because if an error is reported 582 during read or write and again write is called, the PN531 state is 583 unchanged (so write will fail), to bring the PN531 to the correct 584 state, polling is done. 585 Changed on 13th Jan 2009 586 */ 587 NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG0; 588 NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL0; 589 NdefMap->StdMifareContainer.FirstWriteFlag = PH_FRINFC_MIFARESTD_FLAG1; 590 } 591 592 if (((Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && 593 (NdefMap->StdMifareContainer.ReadWriteCompleteFlag == 594 PH_FRINFC_MIFARESTD_FLAG1)) || ((NdefMap->StdMifareContainer.PollFlag == 595 PH_FRINFC_MIFARESTD_FLAG1) && (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR))) 596 { 597 /* Offset = Current, but the read has reached the End of Card */ 598 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 599 NFCSTATUS_EOF_NDEF_CONTAINER_REACHED); 600 } 601 else 602 { 603 NdefMap->Offset = (((Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN) && 604 (NdefMap->PrevOperation == 605 PH_FRINFC_NDEFMAP_READ_OPE)) ? 606 PH_FRINFC_NDEFMAP_SEEK_BEGIN : 607 Offset); 608 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0; 609 status = phFriNfc_MifStd_H_BlkChk (NdefMap); 610 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0; 611 if (status == NFCSTATUS_SUCCESS) 612 { 613 if (NdefMap->StdMifareContainer.PollFlag == 614 PH_FRINFC_MIFARESTD_FLAG1) 615 { 616 /* if poll flag is set then call disconnect because the authentication 617 has failed so reactivation of card is required */ 618 status = phFriNfc_MifStd_H_CallDisCon (NdefMap); 619 } 620 /* Check Authentication Flag */ 621 else if (NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1) 622 { 623 status = ((NdefMap->Offset == 624 PH_FRINFC_NDEFMAP_SEEK_BEGIN) ? 625 phFriNfc_MifStd_H_RdBeforeWr (NdefMap) : 626 phFriNfc_MifStd_H_WrABlock (NdefMap)); 627 } 628 else 629 { 630 status = phFriNfc_MifStd_H_AuthSector (NdefMap); 631 } 632 } 633 } 634 } 635 636 return status; 637 } 638 639 640 /****************************************************************************** 641 * Function phFriNfc_MifareStdMap_Process 642 * 643 * Description This function is a Completion Routine, Processing function, 644 * needed to avoid long blocking. 645 * This function as a Completion Routine in order to be able 646 * to notify the component that an I/O has finished and data 647 * are ready to be processed. 648 649 * Returns void 650 * 651 ******************************************************************************/ 652 void phFriNfc_MifareStdMap_Process (void *Context, 653 NFCSTATUS Status) 654 { 655 phFriNfc_NdefMap_t *NdefMap; 656 uint8_t NDEFFlag = 0, 657 CRFlag = 0, 658 Temp16Bytes = 0, 659 i = 0; 660 661 NdefMap = (phFriNfc_NdefMap_t *)Context; 662 663 if ((Status & PHNFCSTBLOWER) == (NFCSTATUS_SUCCESS & PHNFCSTBLOWER)) 664 { 665 switch (NdefMap->State) 666 { 667 case PH_FRINFC_NDEFMAP_STATE_CHK_NDEF_COMP: 668 Status = phFriNfc_MifStd_H_ProChkNdef (NdefMap); 669 CRFlag = (uint8_t)((Status != NFCSTATUS_PENDING) ? 670 PH_FRINFC_MIFARESTD_FLAG1 : 671 PH_FRINFC_MIFARESTD_FLAG0); 672 break; 673 674 case PH_FRINFC_NDEFMAP_STATE_READ: 675 /* Receive Length for read shall always be equal to 16 */ 676 if ((*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) && 677 (NdefMap->ApduBuffIndex < (uint16_t) NdefMap->ApduBufferSize)) 678 { 679 Temp16Bytes = PH_FRINFC_MIFARESTD_VAL0; 680 NDEFFlag = (uint8_t) PH_FRINFC_MIFARESTD_FLAG1; 681 if (NdefMap->TLVStruct.BytesRemainLinTLV != 0) 682 { 683 NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 684 CRFlag = PH_FRINFC_MIFARESTD_FLAG0; 685 /* To read the remaining length (L) in TLV */ 686 Status = phFriNfc_MifStd_H_RemainTLV (NdefMap, &NDEFFlag, &Temp16Bytes); 687 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 688 PH_FRINFC_MIFARESTD_FLAG1 : 689 PH_FRINFC_MIFARESTD_FLAG0); 690 } 691 692 /* check the NDEFFlag is set. if this is not set, then 693 in the above RemainTLV function all the 16 bytes has been 694 read */ 695 } 696 else 697 { 698 Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_RECEIVE_LENGTH); 699 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 700 } 701 break; 702 703 case PH_FRINFC_NDEFMAP_STATE_WRITE: 704 Status = phFriNfc_MifStd_H_ProWrABlock (NdefMap); 705 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 706 PH_FRINFC_MIFARESTD_FLAG1 : 707 PH_FRINFC_MIFARESTD_FLAG0); 708 709 /* Call Completion Routine if CR Flag is Set to 1 */ 710 if (CRFlag == PH_FRINFC_MIFARESTD_FLAG1) 711 { 712 *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex; 713 } 714 break; 715 716 case PH_FRINFC_NDEFMAP_STATE_AUTH: 717 NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0; 718 Status = phFriNfc_MifStd_H_ProAuth (NdefMap); 719 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 720 PH_FRINFC_MIFARESTD_FLAG1 : 721 PH_FRINFC_MIFARESTD_FLAG0); 722 break; 723 724 case PH_FRINFC_NDEFMAP_STATE_RD_ACS_BIT: 725 Status = phFriNfc_MifStd_H_ProAcsBits (NdefMap); 726 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 727 PH_FRINFC_MIFARESTD_FLAG1 : 728 PH_FRINFC_MIFARESTD_FLAG0); 729 break; 730 731 case PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN: 732 if (NdefMap->StdMifareContainer.RdAfterWrFlag == 733 PH_FRINFC_MIFARESTD_FLAG1) 734 { 735 Status = phFriNfc_MifStd_H_CallWrNdefLen (NdefMap); 736 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 737 PH_FRINFC_MIFARESTD_FLAG1 : 738 PH_FRINFC_MIFARESTD_FLAG0); 739 } 740 else 741 { 742 /* Check this */ 743 if (NdefMap->StdMifareContainer.TempBlockNo == 744 NdefMap->StdMifareContainer.currentBlock) 745 { 746 memcpy (NdefMap->StdMifareContainer.internalBuf, 747 NdefMap->StdMifareContainer.Buffer, 748 NdefMap->StdMifareContainer.internalLength); 749 750 } 751 *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex; 752 NdefMap->StdMifareContainer.currentBlock = 753 NdefMap->StdMifareContainer.TempBlockNo; 754 NdefMap->CardState = (uint8_t) ((NdefMap->CardState == 755 PH_NDEFMAP_CARD_STATE_INITIALIZED) ? 756 PH_NDEFMAP_CARD_STATE_READ_WRITE : 757 NdefMap->CardState); 758 CRFlag = (uint8_t) PH_FRINFC_MIFARESTD_FLAG1; 759 } 760 break; 761 762 case PH_FRINFC_NDEFMAP_STATE_RD_TO_WR_NDEF_LEN: 763 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 764 Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 765 NFCSTATUS_INVALID_DEVICE_REQUEST); 766 if (*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) 767 { 768 /* Size of NdefMap->SendRecvBuf is set by phLibNfc_Gen_NdefMapReset to PH_LIBNFC_GEN_MAX_BUFFER */ 769 /* We don't have to check memory here */ 770 for (i = PH_FRINFC_MIFARESTD_BYTES_READ; i > 0; i--) 771 { 772 NdefMap->SendRecvBuf [i] = NdefMap->SendRecvBuf [i-1]; 773 } 774 NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] = 775 NdefMap->StdMifareContainer.currentBlock; 776 Status = phFriNfc_MifStd_H_WriteNdefLen (NdefMap); 777 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 778 PH_FRINFC_MIFARESTD_FLAG1 : 779 PH_FRINFC_MIFARESTD_FLAG0); 780 } 781 break; 782 783 case PH_FRINFC_NDEFMAP_STATE_GET_ACT_CARDSIZE: 784 NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1; 785 if (NdefMap->TLVStruct.NoLbytesinTLV > PH_FRINFC_MIFARESTD_VAL0) 786 { 787 NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 788 Status = phFriNfc_MifStd_H_ChkRemainTLVs (NdefMap, &CRFlag, &NDEFFlag); 789 NdefMap->TLVStruct.NoLbytesinTLV = 790 PH_FRINFC_MIFARESTD_VAL0; 791 } 792 if ((NDEFFlag == PH_FRINFC_MIFARESTD_FLAG1) && 793 (CRFlag != PH_FRINFC_MIFARESTD_FLAG1)) 794 { 795 Status = phFriNfc_MifStd_H_ChkTLVs (NdefMap, &CRFlag); 796 } 797 if (((NdefMap->StdMifareContainer.ReadNdefFlag == 798 PH_FRINFC_MIFARESTD_FLAG1) || 799 (NdefMap->StdMifareContainer.WrNdefFlag == 800 PH_FRINFC_MIFARESTD_FLAG1)) && 801 (Status != NFCSTATUS_PENDING)) 802 { 803 NdefMap->StdMifareContainer.NFCforumSectFlag = 804 PH_FRINFC_MIFARESTD_FLAG1; 805 CRFlag = PH_FRINFC_MIFARESTD_FLAG0; 806 /* if the card state has changed to initialised and 807 read ndef is called then error is returned */ 808 if (((NdefMap->StdMifareContainer.WrNdefFlag == 809 PH_FRINFC_MIFARESTD_FLAG1) && 810 (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY)) || 811 ((NdefMap->StdMifareContainer.ReadNdefFlag == 812 PH_FRINFC_MIFARESTD_FLAG1) && 813 (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED))) 814 { 815 Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 816 NFCSTATUS_NO_NDEF_SUPPORT); 817 } 818 if (NdefMap->StdMifareContainer.AuthDone == 819 PH_FRINFC_MIFARESTD_FLAG0) 820 { 821 Status = phFriNfc_MifStd_H_AuthSector (NdefMap); 822 } 823 else 824 { 825 Status = ((NdefMap->StdMifareContainer.ReadNdefFlag == 826 PH_FRINFC_MIFARESTD_FLAG1) ? 827 phFriNfc_MifStd_H_RdTLV (NdefMap) : 828 phFriNfc_MifStd_H_RdBeforeWr (NdefMap)); 829 } 830 NdefMap->StdMifareContainer.ReadNdefFlag = 831 PH_FRINFC_MIFARESTD_FLAG0; 832 NdefMap->StdMifareContainer.WrNdefFlag = 833 PH_FRINFC_MIFARESTD_FLAG0; 834 } 835 836 if (NdefMap->StdMifareContainer.ChkNdefFlag == 837 PH_FRINFC_MIFARESTD_FLAG1) 838 { 839 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 840 PH_FRINFC_MIFARESTD_FLAG1 : 841 PH_FRINFC_MIFARESTD_FLAG0); 842 } 843 break; 844 845 case PH_FRINFC_NDEFMAP_STATE_RD_BEF_WR: 846 /* Read flag says that already part of TLV has been written */ 847 Status = phFriNfc_MifStd_H_ProBytesToWr (NdefMap); 848 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 849 PH_FRINFC_MIFARESTD_FLAG1 : 850 PH_FRINFC_MIFARESTD_FLAG0); 851 break; 852 853 case PH_FRINFC_NDEFMAP_STATE_WR_TLV: 854 Status = phFriNfc_MifStd_H_ProWrTLV (NdefMap); 855 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 856 PH_FRINFC_MIFARESTD_FLAG1 : 857 PH_FRINFC_MIFARESTD_FLAG0); 858 break; 859 860 case PH_FRINFC_NDEFMAP_STATE_RD_TLV: 861 Status = phFriNfc_MifStd_H_ProRdTLV (NdefMap); 862 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 863 PH_FRINFC_MIFARESTD_FLAG1 : 864 PH_FRINFC_MIFARESTD_FLAG0); 865 break; 866 867 case PH_FRINFC_NDEFMAP_STATE_TERM_TLV: 868 phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap); 869 NdefMap->StdMifareContainer.currentBlock = 870 NdefMap->TLVStruct.NdefTLVBlock; 871 Status = phFriNfc_MifStd_H_RdtoWrNdefLen (NdefMap); 872 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 873 PH_FRINFC_MIFARESTD_FLAG1 : 874 PH_FRINFC_MIFARESTD_FLAG0); 875 break; 876 877 case PH_FRINFC_NDEFMAP_STATE_DISCONNECT: 878 NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG0; 879 880 Status = phFriNfc_MifStd_H_CallConnect (NdefMap); 881 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 882 PH_FRINFC_MIFARESTD_FLAG1 : 883 PH_FRINFC_MIFARESTD_FLAG0); 884 break; 885 886 case PH_FRINFC_NDEFMAP_STATE_CONNECT: 887 if (NdefMap->StdMifareContainer.FirstReadFlag == PH_FRINFC_MIFARESTD_FLAG1) 888 { 889 NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG0; 890 Status = phFriNfc_MifStd_H_AuthSector (NdefMap); 891 } 892 else if ((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD || 893 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || 894 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD) && 895 (NdefMap->StdMifareContainer.ReadOnlySectorIndex && 896 NdefMap->StdMifareContainer.SectorTrailerBlockNo == NdefMap->StdMifareContainer.currentBlock)) 897 { 898 NdefMap->StdMifareContainer.ReadOnlySectorIndex = 899 PH_FRINFC_MIFARESTD_FLAG0; 900 NdefMap->StdMifareContainer.SectorTrailerBlockNo = 901 PH_FRINFC_MIFARESTD_FLAG0; 902 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_FLAG0; 903 Status = NFCSTATUS_FAILED; 904 } 905 else 906 { 907 Status = ((((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && 908 (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE)) || 909 (NdefMap->StdMifareContainer.WrLength > 910 PH_FRINFC_MIFARESTD_VAL0)) ? 911 phFriNfc_MifStd_H_ProStatNotValid (NdefMap, Status) : 912 phFriNfc_MifStd_H_AuthSector (NdefMap)); 913 } 914 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 915 PH_FRINFC_MIFARESTD_FLAG1 : 916 PH_FRINFC_MIFARESTD_FLAG0); 917 break; 918 919 case PH_FRINFC_NDEFMAP_STATE_RD_SEC_ACS_BIT: 920 Status = phFriNfc_MifStd_H_ProSectorTrailorAcsBits (NdefMap); 921 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING)? 922 PH_FRINFC_MIFARESTD_FLAG1 : 923 PH_FRINFC_MIFARESTD_FLAG0); 924 if ((CRFlag == PH_FRINFC_MIFARESTD_FLAG1) && 925 (NdefMap->StdMifareContainer.WriteAcsBitFlag == PH_FRINFC_MIFARESTD_FLAG0)) 926 { 927 Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 928 NFCSTATUS_INVALID_DEVICE_REQUEST); 929 } 930 break; 931 932 case PH_FRINFC_NDEFMAP_STATE_WRITE_SEC: 933 /* Set flag for writing of Acs bit */ 934 NdefMap->StdMifareContainer.WriteAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG1; 935 936 /* The first NDEF sector is already made read only, 937 set card state to read only and proceed*/ 938 if (NdefMap->CardState != PH_NDEFMAP_CARD_STATE_READ_ONLY) 939 { 940 Status = phFriNfc_MapTool_SetCardState (NdefMap, NdefMap->TLVStruct.BytesRemainLinTLV); 941 if (Status != NFCSTATUS_SUCCESS) 942 { 943 CRFlag = (uint8_t) PH_FRINFC_MIFARESTD_FLAG1; 944 } 945 } 946 947 if (CRFlag != PH_FRINFC_MIFARESTD_FLAG1) 948 { 949 Status = phFriNfc_MifStd_H_ProWrSectorTrailor (NdefMap); 950 CRFlag = (uint8_t) ((Status != NFCSTATUS_PENDING) ? 951 PH_FRINFC_MIFARESTD_FLAG1 : 952 PH_FRINFC_MIFARESTD_FLAG0); 953 } 954 break; 955 956 default: 957 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 958 NFCSTATUS_INVALID_DEVICE_REQUEST); 959 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 960 break; 961 } 962 } 963 else if (NdefMap->State == PH_FRINFC_NDEFMAP_STATE_AUTH) 964 { 965 NdefMap->StdMifareContainer.PollFlag = PH_FRINFC_MIFARESTD_FLAG1; 966 if(NdefMap->StdMifareContainer.FirstWriteFlag == 967 PH_FRINFC_MIFARESTD_FLAG1) 968 { 969 NdefMap->StdMifareContainer.FirstWriteFlag = 970 PH_FRINFC_MIFARESTD_FLAG0; 971 NdefMap->StdMifareContainer.WrLength = 972 ((NdefMap->StdMifareContainer.NFCforumSectFlag == 973 PH_FRINFC_MIFARESTD_FLAG0) ? 974 PH_FRINFC_MIFARESTD_VAL1 : 975 NdefMap->StdMifareContainer.WrLength); 976 } 977 if (NdefMap->StdMifareContainer.WrLength == PH_FRINFC_MIFARESTD_VAL0) 978 { 979 Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 980 NFCSTATUS_EOF_NDEF_CONTAINER_REACHED); 981 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 982 } 983 else 984 { 985 /* Authentication has failed */ 986 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; /* Call Completion Routine */ 987 Status = NFCSTATUS_FAILED;/* Update Status Flag */ 988 } 989 } 990 else 991 { 992 Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 993 NFCSTATUS_INVALID_DEVICE_REQUEST); 994 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 995 } 996 /* Call Completion Routine if CR Flag is Set to 1 */ 997 if (CRFlag == PH_FRINFC_MIFARESTD_FLAG1) 998 { 999 phFriNfc_MifStd_H_Complete (NdefMap, Status); 1000 } 1001 1002 return; 1003 } 1004 1005 /****************************************************************************** 1006 * Function phFriNfc_MifStd_H_RdABlock 1007 * 1008 * Description This function is a Helper function for Mifare Std. It Reads 1009 * a block from the card. 1010 * 1011 * Returns This function return NFCSTATUS_PENDING in case of success 1012 * In case of failure returns other failure value. 1013 * 1014 ******************************************************************************/ 1015 static NFCSTATUS phFriNfc_MifStd_H_RdABlock (phFriNfc_NdefMap_t *NdefMap) 1016 { 1017 NFCSTATUS status = NFCSTATUS_PENDING; 1018 1019 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_READ; 1020 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE; 1021 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 1022 NdefMap->MapCompletionInfo.Context = NdefMap; 1023 1024 if (NdefMap->ApduBuffIndex < (uint16_t) NdefMap->ApduBufferSize) 1025 { 1026 1027 if (NdefMap->StdMifareContainer.internalLength > PH_FRINFC_MIFARESTD_VAL0) 1028 { 1029 status = phFriNfc_MifStd_H_ChkIntLen (NdefMap); 1030 } /* internal Length Check */ 1031 else 1032 { 1033 NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] = 1034 NdefMap->StdMifareContainer.currentBlock; 1035 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ; 1036 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1037 1038 NdefMap->Cmd.MfCmd = phHal_eMifareRead; 1039 1040 /* Call the Overlapped HAL Transceive function */ 1041 status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo, 1042 NdefMap->Cmd, 1043 NdefMap->SendRecvBuf, 1044 NdefMap->SendLength, 1045 NdefMap->SendRecvLength); 1046 } 1047 } 1048 else 1049 { 1050 /* Check for the Card Size */ 1051 if((((NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 1052 NdefMap->StdMifareContainer.NdefBlocks) * 1053 PH_FRINFC_MIFARESTD_BYTES_READ) == 0) || 1054 (NdefMap->ApduBufferSize == NdefMap->ApduBuffIndex)) 1055 { 1056 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 1057 (uint8_t) ((((NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 1058 NdefMap->StdMifareContainer.NdefBlocks) * 1059 PH_FRINFC_MIFARESTD_BYTES_READ) == 0) ? 1060 PH_FRINFC_MIFARESTD_FLAG1 : 1061 PH_FRINFC_MIFARESTD_FLAG0); 1062 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 1063 status = PHNFCSTVAL (CID_NFC_NONE, NFCSTATUS_SUCCESS); 1064 } 1065 else 1066 { 1067 /* Error: The control should not ideally come here. Return Error. */ 1068 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_FAILED); 1069 } 1070 } 1071 1072 return status; 1073 } 1074 1075 /****************************************************************************** 1076 * Function phFriNfc_MifStd_H_WrABlock 1077 * 1078 * Description This function writes into a block of the card. 1079 * 1080 * Returns This function return NFCSTATUS_PENDING in case of success 1081 * In case of failure returns other failure value. 1082 * 1083 ******************************************************************************/ 1084 static NFCSTATUS phFriNfc_MifStd_H_WrABlock (phFriNfc_NdefMap_t *NdefMap) 1085 { 1086 NFCSTATUS status = NFCSTATUS_PENDING; 1087 1088 uint16_t RemainingBytes = 0, 1089 BytesRemained = 0, 1090 index = 0; 1091 uint8_t Temp16Bytes = 0; 1092 1093 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 1094 NdefMap->MapCompletionInfo.Context = NdefMap; 1095 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE; 1096 1097 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WRITE; 1098 1099 /* User Buffer Check */ 1100 if( NdefMap->ApduBuffIndex < (uint16_t) NdefMap->ApduBufferSize) 1101 { 1102 RemainingBytes = (((uint16_t) (NdefMap->ApduBufferSize - 1103 NdefMap->ApduBuffIndex) < 1104 NdefMap->StdMifareContainer.remainingSize) ? 1105 (uint16_t)(NdefMap->ApduBufferSize - 1106 NdefMap->ApduBuffIndex) : 1107 NdefMap->StdMifareContainer.remainingSize); 1108 1109 NdefMap->SendRecvBuf[0] = NdefMap->StdMifareContainer.currentBlock; 1110 Temp16Bytes += PH_FRINFC_MIFARESTD_INC_1; 1111 1112 /* Check for internal bytes */ 1113 if (NdefMap->StdMifareContainer.internalLength > 0) 1114 { 1115 /* copy the bytes previously written in the internal Buffer */ 1116 memcpy (&(NdefMap->SendRecvBuf [Temp16Bytes]), 1117 NdefMap->StdMifareContainer.internalBuf, 1118 NdefMap->StdMifareContainer.internalLength); 1119 1120 Temp16Bytes += (uint8_t) (NdefMap->StdMifareContainer.internalLength); 1121 if (RemainingBytes >= (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes)) 1122 { 1123 /* Copy the Remaining bytes from the user buffer to make the send 1124 data and length = 16 */ 1125 memcpy (&(NdefMap->SendRecvBuf [Temp16Bytes]), 1126 NdefMap->ApduBuffer, 1127 (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes)); 1128 1129 NdefMap->NumOfBytesWritten = 1130 (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes); 1131 Temp16Bytes += (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes); 1132 *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1); 1133 } 1134 else 1135 { 1136 memcpy (&(NdefMap->SendRecvBuf [Temp16Bytes]), 1137 NdefMap->ApduBuffer, 1138 RemainingBytes); 1139 1140 NdefMap->StdMifareContainer.internalBufFlag = PH_FRINFC_MIFARESTD_FLAG1; 1141 NdefMap->NumOfBytesWritten = RemainingBytes; 1142 Temp16Bytes += (uint8_t) (RemainingBytes); 1143 *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1); 1144 1145 BytesRemained = (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes); 1146 /* Pad empty bytes with Zeroes to complete 16 bytes*/ 1147 for (index = 0; index < BytesRemained; index++) 1148 { 1149 NdefMap->SendRecvBuf [(Temp16Bytes + index)] = 1150 (uint8_t) ((index == PH_FRINFC_MIFARESTD_VAL0) ? 1151 PH_FRINFC_MIFARESTD_TERMTLV_T : 1152 PH_FRINFC_MIFARESTD_NULLTLV_T); 1153 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1; 1154 } 1155 Temp16Bytes += (uint8_t) (BytesRemained); 1156 } 1157 } 1158 else 1159 { 1160 if (RemainingBytes >= (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes)) 1161 { 1162 /* Bytes left to write < 16, copy remaining bytes */ 1163 memcpy (&(NdefMap->SendRecvBuf [ 1164 Temp16Bytes]), 1165 &(NdefMap->ApduBuffer [ 1166 NdefMap->ApduBuffIndex]), 1167 (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes)); 1168 1169 NdefMap->NumOfBytesWritten = (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes); 1170 Temp16Bytes += (MIFARE_MAX_SEND_BUF_TO_WRITE - Temp16Bytes); 1171 *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1); 1172 } 1173 else 1174 { 1175 /* Bytes left to write < 16, copy remaining bytes */ 1176 memcpy (&(NdefMap->SendRecvBuf [ 1177 Temp16Bytes]), 1178 &(NdefMap->ApduBuffer [ 1179 NdefMap->ApduBuffIndex]), 1180 RemainingBytes); 1181 1182 NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_FLAG1; 1183 NdefMap->NumOfBytesWritten = RemainingBytes; 1184 Temp16Bytes += (uint8_t) (RemainingBytes); 1185 *NdefMap->DataCount = (Temp16Bytes - PH_FRINFC_MIFARESTD_VAL1); 1186 1187 1188 /* Pad empty bytes with Zeroes to complete 16 bytes */ 1189 for (index = Temp16Bytes; index < MIFARE_MAX_SEND_BUF_TO_WRITE; index++) 1190 { 1191 NdefMap->SendRecvBuf [index] = (uint8_t) ((index == 1192 Temp16Bytes) ? 1193 PH_FRINFC_MIFARESTD_TERMTLV_T : 1194 PH_FRINFC_MIFARESTD_NULLTLV_T); 1195 1196 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1; 1197 } 1198 } 1199 } 1200 /* Buffer to store 16 bytes which is writing to the present block */ 1201 memcpy (NdefMap->StdMifareContainer.Buffer, 1202 &(NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_INC_1]), 1203 PH_FRINFC_MIFARESTD_BLOCK_BYTES); 1204 1205 /* Write from here */ 1206 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE; 1207 NdefMap->Cmd.MfCmd = phHal_eMifareWrite16; 1208 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1209 /* Call the Overlapped HAL Transceive function */ 1210 status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo, 1211 NdefMap->Cmd, 1212 NdefMap->SendRecvBuf, 1213 NdefMap->SendLength, 1214 NdefMap->SendRecvLength); 1215 } 1216 else /* Check User Buffer */ 1217 { 1218 if (NdefMap->StdMifareContainer.NdefBlocks > 1219 NdefMap->StdMifareContainer.NoOfNdefCompBlocks) 1220 { 1221 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 1222 PH_FRINFC_MIFARESTD_FLAG1; 1223 status = PHNFCSTVAL (CID_NFC_NONE, 1224 NFCSTATUS_SUCCESS); 1225 } 1226 else if (NdefMap->ApduBuffIndex == (uint16_t) NdefMap->ApduBufferSize) 1227 { 1228 status = PHNFCSTVAL (CID_NFC_NONE, NFCSTATUS_SUCCESS); 1229 } 1230 else 1231 { 1232 /* Error: The control should not ideally come here. Return Error. */ 1233 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_FAILED); 1234 } 1235 } 1236 1237 return status; 1238 } 1239 1240 /****************************************************************************** 1241 * Function phFriNfc_MifStd_H_AuthSector 1242 * 1243 * Description This function authenticates one sector at a time. 1244 * 1245 * Returns This function return NFCSTATUS_PENDING in case of success 1246 * In case of failure returns other failure value. 1247 * 1248 ******************************************************************************/ 1249 static NFCSTATUS phFriNfc_MifStd_H_AuthSector (phFriNfc_NdefMap_t *NdefMap) 1250 { 1251 NFCSTATUS status = NFCSTATUS_PENDING; 1252 1253 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 1254 NdefMap->MapCompletionInfo.Context = NdefMap; 1255 1256 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1257 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_AUTH; 1258 1259 /* Authenticate */ 1260 NdefMap->Cmd.MfCmd = phHal_eMifareAuthentA; 1261 1262 NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] = 1263 ((NdefMap->TLVStruct.NdefTLVAuthFlag == 1264 PH_FRINFC_MIFARESTD_FLAG1) ? 1265 NdefMap->TLVStruct.NdefTLVBlock : 1266 NdefMap->StdMifareContainer.currentBlock); 1267 1268 /* if MAD blocks then authentication key is 1269 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 else 1270 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 */ 1271 if (((NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK0) && 1272 (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK1) && 1273 (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK2) && 1274 (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK64) && 1275 (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK65) && 1276 (NdefMap->StdMifareContainer.currentBlock != PH_FRINFC_MIFARESTD_MAD_BLK66)) || 1277 (NdefMap->TLVStruct.NdefTLVAuthFlag == 1278 (uint8_t) PH_FRINFC_MIFARESTD_FLAG1)) 1279 { 1280 NdefMap->SendRecvBuf [1] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */ 1281 NdefMap->SendRecvBuf [2] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */ 1282 NdefMap->SendRecvBuf [3] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */ 1283 NdefMap->SendRecvBuf [4] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */ 1284 NdefMap->SendRecvBuf [5] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */ 1285 NdefMap->SendRecvBuf [6] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */ 1286 } 1287 else 1288 { 1289 NdefMap->SendRecvBuf [1] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT1; /* 0xA0 */ 1290 NdefMap->SendRecvBuf [2] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT2; /* 0xA1 */ 1291 NdefMap->SendRecvBuf [3] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT3; /* 0xA2 */ 1292 NdefMap->SendRecvBuf [4] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT4; /* 0xA3 */ 1293 NdefMap->SendRecvBuf [5] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT5; /* 0xA4 */ 1294 NdefMap->SendRecvBuf [6] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_MADSECT6; /* 0xA5 */ 1295 } 1296 1297 if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD || 1298 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || 1299 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD) 1300 { 1301 if (NdefMap->StdMifareContainer.ReadOnlySectorIndex && 1302 NdefMap->StdMifareContainer.SectorTrailerBlockNo == NdefMap->StdMifareContainer.currentBlock) 1303 { 1304 memcpy (&NdefMap->SendRecvBuf [1], &NdefMap->StdMifareContainer.UserScrtKeyB [0], PH_FRINFC_MIFARESTD_KEY_LEN); 1305 1306 /* Authenticate with KeyB */ 1307 NdefMap->Cmd.MfCmd = phHal_eMifareAuthentB; 1308 } 1309 } 1310 1311 NdefMap->SendLength = MIFARE_AUTHENTICATE_CMD_LENGTH; 1312 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1313 /* Call the Overlapped HAL Transceive function */ 1314 status = phFriNfc_ExtnsTransceive (NdefMap->pTransceiveInfo, 1315 NdefMap->Cmd, 1316 NdefMap->SendRecvBuf, 1317 NdefMap->SendLength, 1318 NdefMap->SendRecvLength); 1319 1320 return status; 1321 } 1322 1323 /****************************************************************************** 1324 * Function phFriNfc_MifStd_H_Complete 1325 * 1326 * Description It is used to call the Completion Routine 1327 * 1328 * Returns void 1329 * 1330 ******************************************************************************/ 1331 static void phFriNfc_MifStd_H_Complete (phFriNfc_NdefMap_t *NdefMap, 1332 NFCSTATUS Result) 1333 { 1334 /* set the state back to the Reset_Init state */ 1335 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT; 1336 1337 /* set the completion routine */ 1338 NdefMap->CompletionRoutine [NdefMap->StdMifareContainer.CRIndex]. 1339 CompletionRoutine (NdefMap->CompletionRoutine->Context, Result); 1340 1341 return; 1342 } 1343 1344 /****************************************************************************** 1345 * Function phFriNfc_MifStd4k_H_CheckNdef 1346 * 1347 * Description This function is used for Mifare 4k Check Ndef to 1348 * get the next AID blocks. 1349 * 1350 * Returns This function return NFCSTATUS_SUCCESS in case of success 1351 * In case of failure returns other failure value. 1352 * 1353 ******************************************************************************/ 1354 static NFCSTATUS phFriNfc_MifStd4k_H_CheckNdef (phFriNfc_NdefMap_t *NdefMap) 1355 { 1356 NFCSTATUS Result = NFCSTATUS_SUCCESS; 1357 1358 /* Get the AID Block */ 1359 if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK2) 1360 { 1361 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK64; 1362 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0; 1363 } 1364 else if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64) 1365 { 1366 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK65; 1367 } 1368 else 1369 { 1370 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_MAD_BLK66; 1371 } 1372 1373 Result = phFriNfc_MifareStdMap_ChkNdef (NdefMap); 1374 1375 return Result; 1376 } 1377 1378 /****************************************************************************** 1379 * Function phFriNfc_MifStd_H_fillAIDarray 1380 * 1381 * Description This function storew the AIDs for check ndef. 1382 * 1383 * Returns void 1384 * 1385 ******************************************************************************/ 1386 static void phFriNfc_MifStd_H_fillAIDarray (phFriNfc_NdefMap_t *NdefMap) 1387 { 1388 uint8_t byteindex = 0; 1389 1390 if ((NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK1) || 1391 (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64)) 1392 { 1393 /* The First Two Bytes in Receive Buffer 1394 are CRC bytes so it is not copied 1395 instead, 0 is copied in AID[0] & AID[1] */ 1396 NdefMap->StdMifareContainer.aid [NdefMap->StdMifareContainer.SectorIndex] = 1397 PH_FRINFC_MIFARESTD_NON_NDEF_COMP; 1398 NdefMap->StdMifareContainer.SectorIndex++; 1399 byteindex = 2; 1400 } 1401 1402 while (byteindex < PH_FRINFC_MIFARESTD_BYTES_READ) 1403 { 1404 if ((NdefMap->SendRecvBuf [byteindex] == PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL2) && 1405 (NdefMap->SendRecvBuf [(byteindex + 1)] == PH_FRINFC_NDEFMAP_MIFARESTD_NDEF_COMPVAL1)) 1406 { 1407 /* This flag is set when a NFC forum sector is found in a 1408 MAD block for the first time */ 1409 NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG1; 1410 NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.SectorIndex] = 1411 PH_FRINFC_MIFARESTD_NDEF_COMP; 1412 NdefMap->StdMifareContainer.SectorIndex++; 1413 } 1414 else 1415 { 1416 NdefMap->StdMifareContainer.aid [NdefMap->StdMifareContainer.SectorIndex] = 1417 PH_FRINFC_MIFARESTD_NON_NDEF_COMP; 1418 NdefMap->StdMifareContainer.SectorIndex++; 1419 /* AID complete flag is set when a non NFC forum sector is found in a 1420 MAD block after the NFC forum sector. After setting this, all other 1421 values are ignored and are NOT NDEF compliant */ 1422 NdefMap->StdMifareContainer.aidCompleteFlag = 1423 ((NdefMap->StdMifareContainer.NFCforumSectFlag == 1424 PH_FRINFC_MIFARESTD_FLAG1) ? 1425 PH_FRINFC_MIFARESTD_FLAG1 : 1426 PH_FRINFC_MIFARESTD_FLAG0); 1427 1428 NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0; 1429 if (NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) 1430 { 1431 break; 1432 } 1433 } 1434 byteindex += 2; 1435 } 1436 1437 /* If "aidCompleteFlag" is set then the remaining sectors are made NOT 1438 NDEF compliant */ 1439 if ((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) && 1440 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) 1441 { 1442 /* for Mifare 1k there are 16 sectors, till this number all sectors 1443 are made NOT NDEF compliant */ 1444 for (byteindex = NdefMap->StdMifareContainer.SectorIndex; 1445 byteindex < PH_FRINFC_MIFARESTD1K_TOTAL_SECTOR; 1446 byteindex++) 1447 { 1448 NdefMap->StdMifareContainer.aid [byteindex] = 1449 PH_FRINFC_MIFARESTD_NON_NDEF_COMP; 1450 } 1451 } 1452 else if ((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) && 1453 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)) 1454 { 1455 /* for Mifare 2k there are 32 sectors, till this number all sectors 1456 are made NOT NDEF compliant */ 1457 for (byteindex = NdefMap->StdMifareContainer.SectorIndex; 1458 byteindex < PH_FRINFC_MIFARESTD2K_TOTAL_SECTOR; 1459 byteindex++) 1460 { 1461 NdefMap->StdMifareContainer.aid[byteindex] = 1462 PH_FRINFC_MIFARESTD_NON_NDEF_COMP; 1463 } 1464 } 1465 else 1466 { 1467 /* for Mifare 4k there are 40 sectors, till this number all sectors 1468 are made NOT NDEF compliant */ 1469 if ((NdefMap->StdMifareContainer.aidCompleteFlag == PH_FRINFC_MIFARESTD_FLAG1) && 1470 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)) 1471 { 1472 for(byteindex = NdefMap->StdMifareContainer.SectorIndex; 1473 byteindex < PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR; 1474 byteindex++) 1475 { 1476 NdefMap->StdMifareContainer.aid [byteindex] = 1477 PH_FRINFC_MIFARESTD_NON_NDEF_COMP; 1478 } 1479 } 1480 } 1481 1482 return; 1483 } 1484 1485 /****************************************************************************** 1486 * Function phFriNfc_MifStd_H_BlkChk 1487 * 1488 * Description This function is to check the Ndef compliance of the 1489 * current block, if the block is not Ndef Compliant, 1490 * increment the block till the next Ndef compliant block 1491 * using the Get Sector Helper function 1492 * 1493 * Returns This function return NFCSTATUS_SUCCESS in case of success 1494 * In case of failure returns other failure value. 1495 * 1496 ******************************************************************************/ 1497 static NFCSTATUS phFriNfc_MifStd_H_BlkChk (phFriNfc_NdefMap_t *NdefMap) 1498 { 1499 NFCSTATUS Result = NFCSTATUS_SUCCESS; 1500 uint8_t SectorID = 0, callbreak = 0; 1501 1502 for (;;) 1503 { 1504 /* Get a Sector ID for the Current Block */ 1505 SectorID = phFriNfc_MifStd_H_GetSect (NdefMap->StdMifareContainer.currentBlock); 1506 /* Check the card Type 1k or 4k */ 1507 /* enter if Mifare 1k card. For Mifare 4k go to else */ 1508 if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) 1509 { 1510 /* if Sector Id > 15 No Sectors to write */ 1511 if (SectorID > 15) 1512 { 1513 SectorID = phFriNfc_MifStd_H_GetSect (NdefMap->StdMifareContainer.currentBlock); 1514 /*Error: No Ndef Compliant Sectors present */ 1515 Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 1516 NFCSTATUS_INVALID_PARAMETER); 1517 callbreak = 1; 1518 } 1519 else 1520 { 1521 phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak); 1522 } 1523 } /* End of if */ /* End of Mifare 1k check */ 1524 else if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD) /* Mifare 2k check starts here */ 1525 { 1526 /* Sector > 39 no ndef compliant sectors found*/ 1527 if (SectorID > PH_FRINFC_MIFARESTD_SECTOR_NO31) 1528 { 1529 /*Error: No Ndef Compliant Sectors present */ 1530 Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 1531 NFCSTATUS_INVALID_PARAMETER); 1532 callbreak = 1; 1533 } 1534 else if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64) 1535 { 1536 NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4; 1537 } 1538 else if (SectorID < PH_FRINFC_MIFARESTD_SECTOR_NO32) /* sector < 32 contains 4 blocks in each sector */ 1539 { 1540 /* If the block checked is 63, the 3 blocks after this 1541 are AID(MAD) blocks so its need to be skipped */ 1542 if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK63) 1543 { 1544 NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4; 1545 } 1546 else 1547 { 1548 phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak); 1549 } 1550 } 1551 else 1552 { 1553 phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak); 1554 } 1555 }/* End of if*/ /* End of Mifare 2k check*/ 1556 else /* Mifare 4k check starts here */ 1557 { 1558 /* Sector > 39 no ndef compliant sectors found*/ 1559 if (SectorID > PH_FRINFC_MIFARESTD_SECTOR_NO39) 1560 { 1561 /*Error: No Ndef Compliant Sectors present */ 1562 Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 1563 callbreak = 1; 1564 } 1565 else if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64) 1566 { 1567 NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4; 1568 } 1569 else if (SectorID < PH_FRINFC_MIFARESTD_SECTOR_NO32) /* sector < 32 contains 4 blocks in each sector */ 1570 { 1571 /* If the block checked is 63, the 3 blocks after this 1572 are AID(MAD) blocks so its need to be skipped */ 1573 if (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK63) 1574 { 1575 NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4; 1576 } 1577 else 1578 { 1579 phFriNfc_MifStd1k_H_BlkChk (NdefMap, SectorID, &callbreak); 1580 } 1581 } 1582 else 1583 { 1584 /* every last block of a sector needs to be skipped */ 1585 if (((NdefMap->StdMifareContainer.currentBlock + 1) % 1586 PH_FRINFC_MIFARESTD_BLOCK_BYTES) == 0) 1587 { 1588 NdefMap->StdMifareContainer.currentBlock++; 1589 } 1590 else 1591 { 1592 if (NdefMap->StdMifareContainer.aid [SectorID] == 1593 PH_FRINFC_MIFARESTD_NDEF_COMP) 1594 { 1595 /* Check whether the block is first block of a (next)new sector and 1596 also check if it is first block then internal length is zero 1597 or not. Because once Authentication is done for the sector again 1598 we should not authenticate it again */ 1599 /* In this case 32 sectors contains 4 blocks and next remaining 8 sectors 1600 contains 16 blocks that is why (32 * 4) + (sectorID - 32) *16*/ 1601 if ((NdefMap->StdMifareContainer.currentBlock == 1602 ((PH_FRINFC_MIFARESTD_SECTOR_NO32 * PH_FRINFC_MIFARESTD_BLK4) + 1603 ((SectorID - PH_FRINFC_MIFARESTD_SECTOR_NO32) * PH_FRINFC_MIFARESTD_BLOCK_BYTES))) && 1604 (NdefMap->StdMifareContainer.internalLength == 0)) 1605 { 1606 NdefMap->StdMifareContainer.AuthDone = 0; 1607 } 1608 callbreak = 1; 1609 } 1610 else 1611 { 1612 NdefMap->StdMifareContainer.currentBlock += 16; 1613 } 1614 } 1615 } 1616 } 1617 if (callbreak == 1) 1618 { 1619 break; 1620 } 1621 } 1622 1623 return Result; 1624 } 1625 1626 /****************************************************************************** 1627 * Function phFriNfc_MifStd_H_GetSect 1628 * 1629 * Description This function to get the Sector from the current block 1630 * 1631 * Returns uint8_t SectorID 1632 * 1633 ******************************************************************************/ 1634 static uint8_t phFriNfc_MifStd_H_GetSect (uint8_t BlockNumber) 1635 { 1636 uint8_t SectorID = 0; 1637 1638 if (BlockNumber >= PH_FRINFC_MIFARESTD4K_BLK128) 1639 { 1640 SectorID = (uint8_t) (PH_FRINFC_MIFARESTD_SECTOR_NO32 + 1641 ((BlockNumber - PH_FRINFC_MIFARESTD4K_BLK128)/ 1642 PH_FRINFC_MIFARESTD_BLOCK_BYTES)); 1643 } 1644 else 1645 { 1646 SectorID = (BlockNumber/PH_FRINFC_MIFARESTD_BLK4); 1647 } 1648 1649 return SectorID; 1650 } 1651 1652 /****************************************************************************** 1653 * Function phFriNfc_MifStd_H_RdAcsBit 1654 * 1655 * Description It read the access bits of each sector. 1656 * NCI messages. 1657 * 1658 * Returns This function return NFCSTATUS_SUCCESS in case of success 1659 * In case of failure returns other failure value. 1660 * 1661 ******************************************************************************/ 1662 static NFCSTATUS phFriNfc_MifStd_H_RdAcsBit (phFriNfc_NdefMap_t *NdefMap) 1663 { 1664 NFCSTATUS Result = NFCSTATUS_SUCCESS; 1665 1666 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_ACS_BIT; 1667 1668 if (NdefMap->StdMifareContainer.ReadOnlySectorIndex && 1669 NdefMap->StdMifareContainer.currentBlock == NdefMap->StdMifareContainer.SectorTrailerBlockNo) 1670 { 1671 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_SEC_ACS_BIT; 1672 } 1673 1674 if (NdefMap->StdMifareContainer.ReadAcsBitFlag == PH_FRINFC_MIFARESTD_FLAG1) 1675 { 1676 /* Get the sector trailer */ 1677 ((NdefMap->StdMifareContainer.currentBlock > 127) ? 1678 phFriNfc_MifStd_H_Get4kStTrail(NdefMap) : 1679 phFriNfc_MifStd_H_Get1kStTrail(NdefMap)); 1680 } 1681 else 1682 { 1683 /* Give the current block to read */ 1684 NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0] = 1685 NdefMap->StdMifareContainer.currentBlock; 1686 } 1687 1688 Result = phFriNfc_MifStd_H_Rd16Bytes(NdefMap, 1689 NdefMap->SendRecvBuf [PH_FRINFC_MIFARESTD_VAL0]); 1690 1691 return Result; 1692 } 1693 1694 /****************************************************************************** 1695 * Function phFriNfc_MifStd_H_ChkAcsBit 1696 * 1697 * Description This function check the access bits of each sector. 1698 * 1699 * Returns This function return NFCSTATUS_SUCCESS in case of success 1700 * In case of failure returns other failure value. 1701 * 1702 ******************************************************************************/ 1703 static NFCSTATUS phFriNfc_MifStd_H_ChkAcsBit (phFriNfc_NdefMap_t *NdefMap) 1704 { 1705 NFCSTATUS Result = NFCSTATUS_SUCCESS; 1706 1707 /* Blocks from 0 to 3 and from 64 to 67(MAD blocks) */ 1708 if ((NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK0) || 1709 (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK1) || 1710 (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK2) || 1711 (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK3) || 1712 (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK64) || 1713 (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK65) || 1714 (NdefMap->StdMifareContainer.currentBlock == PH_FRINFC_MIFARESTD_MAD_BLK66) ) 1715 { 1716 /* Access bits check removed for the MAD blocks */ 1717 #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD 1718 1719 if (((NdefMap->SendRecvBuf [ 1720 PH_FRINFC_MIFARESTD_VAL6] & 1721 PH_FRINFC_MIFARESTD_MASK_FF) == 1722 PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE6) && 1723 ((NdefMap->SendRecvBuf [ 1724 PH_FRINFC_MIFARESTD_VAL7] & 1725 PH_FRINFC_MIFARESTD_MASK_FF) == 1726 PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE7) && 1727 ((NdefMap->SendRecvBuf [ 1728 PH_FRINFC_MIFARESTD_VAL8] & 1729 PH_FRINFC_MIFARESTD_MASK_FF) == 1730 PH_FRINFC_MIFARESTD_ACS_BYTE8)) 1731 { 1732 NdefMap->StdMifareContainer.WriteFlag = 1733 PH_FRINFC_MIFARESTD_FLAG0; 1734 NdefMap->StdMifareContainer.ReadFlag = 1735 PH_FRINFC_MIFARESTD_FLAG1; 1736 } 1737 else 1738 { 1739 NdefMap->StdMifareContainer.WriteFlag = 1740 PH_FRINFC_MIFARESTD_FLAG0; 1741 NdefMap->StdMifareContainer.ReadFlag = 1742 PH_FRINFC_MIFARESTD_FLAG0; 1743 } 1744 1745 #else /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */ 1746 1747 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INITIALIZED; 1748 1749 #endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */ 1750 } 1751 else 1752 { 1753 /* Check for Access bytes 6, 7 and 8 value = 1754 0x7F, 0x07, 0x88 NFC forum sectors*/ 1755 if (((NdefMap->SendRecvBuf [ 1756 PH_FRINFC_MIFARESTD_VAL6] & 1757 PH_FRINFC_MIFARESTD_MASK_FF) == 1758 PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE6) && 1759 ((NdefMap->SendRecvBuf [ 1760 PH_FRINFC_MIFARESTD_VAL7] & 1761 PH_FRINFC_MIFARESTD_MASK_FF) == 1762 PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE7) && 1763 ((NdefMap->SendRecvBuf [ 1764 PH_FRINFC_MIFARESTD_VAL8] & 1765 PH_FRINFC_MIFARESTD_MASK_FF) == 1766 PH_FRINFC_MIFARESTD_ACS_BYTE8)) 1767 { 1768 NdefMap->StdMifareContainer.WriteFlag = 1769 PH_FRINFC_MIFARESTD_FLAG1; 1770 NdefMap->StdMifareContainer.ReadFlag = 1771 PH_FRINFC_MIFARESTD_FLAG1; 1772 } 1773 else if (((NdefMap->SendRecvBuf [ 1774 PH_FRINFC_MIFARESTD_VAL6] & 1775 PH_FRINFC_MIFARESTD_MASK_FF) == 1776 PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE6) && 1777 ((NdefMap->SendRecvBuf [ 1778 PH_FRINFC_MIFARESTD_VAL7] & 1779 PH_FRINFC_MIFARESTD_MASK_FF) == 1780 PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE7) && 1781 ((NdefMap->SendRecvBuf [ 1782 PH_FRINFC_MIFARESTD_VAL8] & 1783 PH_FRINFC_MIFARESTD_MASK_FF) == 1784 PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE8)) 1785 { 1786 /* Read Only state */ 1787 /* Check for Access bytes 6, 7 and 8 value = 1788 0x55, 0xAD, 0x2A NFC forum Sectors */ 1789 NdefMap->StdMifareContainer.WriteFlag = 1790 PH_FRINFC_MIFARESTD_FLAG0; 1791 NdefMap->StdMifareContainer.ReadFlag = 1792 PH_FRINFC_MIFARESTD_FLAG1; 1793 } 1794 else 1795 { 1796 NdefMap->StdMifareContainer.WriteFlag = 1797 PH_FRINFC_MIFARESTD_FLAG0; 1798 NdefMap->StdMifareContainer.ReadFlag = 1799 PH_FRINFC_MIFARESTD_FLAG0; 1800 } 1801 1802 #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD 1803 /* Do nothing */ 1804 #else /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */ 1805 Result = phFriNfc_MifStd_H_GPBChk (NdefMap); 1806 #endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */ 1807 } 1808 1809 #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD 1810 Result = phFriNfc_MifStd_H_GPBChk (NdefMap); 1811 #endif /* #ifdef ENABLE_ACS_BIT_CHK_FOR_MAD */ 1812 1813 return Result; 1814 } 1815 1816 /****************************************************************************** 1817 * Function phFriNfc_MifStd_H_ChkRdWr 1818 * 1819 * Description This function is for read access bits, depending 1820 * on the read/write/check ndef function called. 1821 * 1822 * Returns This function return NFCSTATUS_SUCCESS in case of success 1823 * In case of failure returns other failure value. 1824 * 1825 ******************************************************************************/ 1826 static NFCSTATUS phFriNfc_MifStd_H_ChkRdWr (phFriNfc_NdefMap_t *NdefMap) 1827 { 1828 NFCSTATUS Result = NFCSTATUS_SUCCESS; 1829 1830 switch (NdefMap->PrevOperation) 1831 { 1832 case PH_FRINFC_NDEFMAP_CHECK_OPE: 1833 if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) 1834 { 1835 /* No permission to read */ 1836 Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 1837 NFCSTATUS_READ_FAILED); 1838 } 1839 else if ((NdefMap->StdMifareContainer.currentBlock > 3) && 1840 (NdefMap->StdMifareContainer.ChkNdefCompleteFlag == 1841 PH_FRINFC_MIFARESTD_FLAG1) && 1842 (NdefMap->StdMifareContainer.currentBlock != 1843 PH_FRINFC_MIFARESTD_MAD_BLK65) && 1844 (NdefMap->StdMifareContainer.currentBlock != 1845 PH_FRINFC_MIFARESTD_MAD_BLK66)) 1846 { 1847 Result = ((NdefMap->StdMifareContainer.ReadAcsBitFlag == 1848 PH_FRINFC_MIFARESTD_FLAG0) ? 1849 phFriNfc_MifStd_H_RdAcsBit(NdefMap) : 1850 phFriNfc_MifStd_H_AuthSector(NdefMap)); 1851 } 1852 else 1853 { 1854 Result = phFriNfc_MifareStdMap_ChkNdef (NdefMap); 1855 } 1856 break; 1857 1858 case PH_FRINFC_NDEFMAP_READ_OPE: 1859 if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) 1860 { 1861 /* No permission to Read */ 1862 Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_READ_FAILED); 1863 } 1864 else if (NdefMap->StdMifareContainer.ReadNdefFlag == PH_FRINFC_MIFARESTD_FLAG1) 1865 { 1866 Result = phFriNfc_MifStd_H_GetActCardLen (NdefMap); 1867 } 1868 else 1869 { 1870 Result = phFriNfc_MifStd_H_RdABlock (NdefMap); 1871 } 1872 break; 1873 1874 case PH_FRINFC_NDEFMAP_WRITE_OPE: 1875 if ((NdefMap->CardState == 1876 PH_NDEFMAP_CARD_STATE_INVALID) || 1877 (NdefMap->CardState == 1878 PH_NDEFMAP_CARD_STATE_READ_ONLY)) 1879 { 1880 /* No permission to Read */ 1881 Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 1882 NFCSTATUS_WRITE_FAILED); 1883 } 1884 else if (NdefMap->StdMifareContainer.WrNdefFlag == 1885 PH_FRINFC_MIFARESTD_FLAG1) 1886 { 1887 Result = phFriNfc_MifStd_H_GetActCardLen (NdefMap); 1888 } 1889 else if (NdefMap->StdMifareContainer.RdBeforeWrFlag == 1890 PH_FRINFC_MIFARESTD_FLAG1) 1891 { 1892 /*NdefMap->StdMifareContainer.ReadFlag = 1893 PH_FRINFC_MIFARESTD_FLAG0;*/ 1894 Result = phFriNfc_MifStd_H_RdBeforeWr (NdefMap); 1895 } 1896 else if (NdefMap->StdMifareContainer.RdAfterWrFlag == 1897 PH_FRINFC_MIFARESTD_FLAG1) 1898 { 1899 Result = phFriNfc_MifStd_H_RdtoWrNdefLen (NdefMap); 1900 } 1901 else 1902 { 1903 Result = (((NdefMap->TLVStruct.NdefTLVBlock == 1904 NdefMap->StdMifareContainer.currentBlock) && 1905 (NdefMap->Offset == 1906 PH_FRINFC_NDEFMAP_SEEK_BEGIN)) ? 1907 phFriNfc_MifStd_H_RdBeforeWr (NdefMap) : 1908 phFriNfc_MifStd_H_WrABlock (NdefMap)); 1909 } 1910 break; 1911 1912 case PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE: 1913 Result = ((NdefMap->StdMifareContainer.ReadFlag == 1914 PH_FRINFC_MIFARESTD_FLAG0) ? 1915 (PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 1916 NFCSTATUS_READ_FAILED)) : 1917 phFriNfc_MifStd_H_GetActCardLen (NdefMap)); 1918 break; 1919 1920 default: 1921 /* Operation is not correct */ 1922 Result = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, 1923 NFCSTATUS_INVALID_PARAMETER); 1924 1925 break; 1926 } 1927 1928 return Result; 1929 } 1930 1931 /****************************************************************************** 1932 * Function phFriNfc_MifStd_H_ChkNdefCmpltSects 1933 * 1934 * Description This function is used to check ndef to check the 1935 * ndef compliant sectors. 1936 * 1937 * Returns void 1938 * 1939 ******************************************************************************/ 1940 static void phFriNfc_MifStd_H_ChkNdefCmpltSects (phFriNfc_NdefMap_t *NdefMap) 1941 { 1942 uint8_t index = 0; 1943 uint8_t index_max_4k_2k= 0; 1944 if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD) 1945 { 1946 index_max_4k_2k = PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR; 1947 } 1948 else 1949 { 1950 index_max_4k_2k = PH_FRINFC_MIFARESTD2K_TOTAL_SECTOR; 1951 } 1952 1953 if (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD) 1954 { 1955 for (index = PH_FRINFC_MIFARESTD_SECTOR_NO1; index < index_max_4k_2k; index++)/*Block 0 is MAD block, so it should start with 1*/ 1956 { 1957 /* For Mifare 4k, Block 0 to 31 contains 4 blocks */ 1958 /* sector 0 and 15 are aid blocks */ 1959 if (index != PH_FRINFC_MIFARESTD_SECTOR_NO16) 1960 { 1961 if (((index < 32) && (index != PH_FRINFC_MIFARESTD_SECTOR_NO0)) 1962 && (NdefMap->StdMifareContainer.aid [index] == 1963 PH_FRINFC_MIFARESTD_NON_NDEF_COMP)) 1964 { 1965 /* Only 3 blocks can be read/written till sector 31 */ 1966 NdefMap->StdMifareContainer.NoOfNdefCompBlocks -= 1967 PH_FRINFC_MIFARESTD_MAD_BLK3; 1968 1969 } 1970 else 1971 { 1972 /* For Mifare 4k, Block 32 to 39 contains 16 blocks */ 1973 if(NdefMap->StdMifareContainer.aid [index] == 1974 PH_FRINFC_MIFARESTD_NON_NDEF_COMP) 1975 { 1976 /* Only 15 blocks can be read/written from sector 31 */ 1977 NdefMap->StdMifareContainer.NoOfNdefCompBlocks -= 1978 PH_FRINFC_MIFARESTD_BLK15; 1979 } 1980 } 1981 } 1982 } /* For index > 40 */ 1983 } 1984 else 1985 { 1986 for (index = PH_FRINFC_MIFARESTD_SECTOR_NO1; 1987 index < PH_FRINFC_MIFARESTD_SECTOR_NO16; index++) 1988 { 1989 if (NdefMap->StdMifareContainer.aid [index] == 1990 PH_FRINFC_MIFARESTD_NON_NDEF_COMP) 1991 { 1992 /* Only three blocks can be read/written in 1993 a sector. So if a sector is non-ndef 1994 compliant, decrement 3 */ 1995 NdefMap->StdMifareContainer.NoOfNdefCompBlocks -= 1996 PH_FRINFC_MIFARESTD_MAD_BLK3; 1997 } 1998 } 1999 } 2000 2001 return; 2002 } 2003 2004 /****************************************************************************** 2005 * Function phFriNfc_MifStd_H_RemainTLV 2006 * 2007 * Description This function is used for read ndef to process the 2008 * remaining bytes of length (L) in the TLV. 2009 * 2010 * Returns This function return NFCSTATUS_PENDING in case of success 2011 * In case of failure returns other failure value. 2012 * 2013 ******************************************************************************/ 2014 static NFCSTATUS phFriNfc_MifStd_H_RemainTLV (phFriNfc_NdefMap_t *NdefMap, 2015 uint8_t *Flag, 2016 uint8_t *Temp16Bytes) 2017 { 2018 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2019 uint8_t CRFlag = 0; 2020 uint16_t RemainingBytes = 0; 2021 2022 RemainingBytes = ((uint16_t) NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 2023 2024 if (NdefMap->StdMifareContainer.remainingSize < RemainingBytes) 2025 { 2026 /* If the user Buffer is greater than the Card Size 2027 set LastBlockFlag = 1. This Flag is used to read bytes 2028 till the end of the card only */ 2029 RemainingBytes = NdefMap->StdMifareContainer.remainingSize; 2030 } 2031 2032 /* Remaining Bytes of length (L) in TLV <= 16 */ 2033 if ((NdefMap->TLVStruct.BytesRemainLinTLV <= 2034 (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) && 2035 (RemainingBytes <= NdefMap->TLVStruct.BytesRemainLinTLV)) 2036 { 2037 /* Copy data to user buffer */ 2038 memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2039 &(NdefMap->SendRecvBuf [(*Temp16Bytes)]), 2040 RemainingBytes); 2041 2042 NdefMap->ApduBuffIndex += RemainingBytes; 2043 NdefMap->StdMifareContainer.remainingSize -= RemainingBytes; 2044 2045 /* copy the bytes to internal buffer, that are read, 2046 but not used for the user buffer */ 2047 if (RemainingBytes != NdefMap->TLVStruct.BytesRemainLinTLV) 2048 { 2049 memcpy (NdefMap->StdMifareContainer.internalBuf, 2050 &(NdefMap->SendRecvBuf [((*Temp16Bytes) + RemainingBytes)]), 2051 ((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)) - RemainingBytes)); 2052 2053 /* internal buffer length */ 2054 NdefMap->StdMifareContainer.internalLength = 2055 ((PH_FRINFC_MIFARESTD_BYTES_READ - 2056 (*Temp16Bytes)) - RemainingBytes); 2057 } 2058 *Temp16Bytes += ((uint8_t)RemainingBytes); 2059 /* Remaining Bytes of length value in TLV */ 2060 NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes; 2061 2062 if (NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0) 2063 { 2064 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t) 2065 (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) || 2066 (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ? 2067 PH_FRINFC_MIFARESTD_FLAG1 : 2068 PH_FRINFC_MIFARESTD_FLAG0); 2069 2070 /* internal length bytes completed */ 2071 NdefMap->StdMifareContainer.currentBlock++; 2072 NdefMap->StdMifareContainer.NdefBlocks++; 2073 } 2074 2075 if (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0) 2076 { 2077 /* Remaining Bytes of length (L) in TLV is Zero means that the next 2078 coming bytes are containing type (T), length (L) in TLV */ 2079 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1; 2080 NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2081 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2082 } 2083 /* call completion routine */ 2084 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2085 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2086 } 2087 else if ((NdefMap->TLVStruct.BytesRemainLinTLV <= 2088 (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) && 2089 (RemainingBytes > NdefMap->TLVStruct.BytesRemainLinTLV)) 2090 { 2091 /* Copy data to user buffer */ 2092 memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2093 &(NdefMap->SendRecvBuf [(*Temp16Bytes)]), 2094 NdefMap->TLVStruct.BytesRemainLinTLV); 2095 2096 NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV; 2097 NdefMap->StdMifareContainer.remainingSize -= NdefMap->TLVStruct.BytesRemainLinTLV; 2098 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1; 2099 *Temp16Bytes += ((uint8_t) NdefMap->TLVStruct.BytesRemainLinTLV); 2100 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0; 2101 2102 *Flag = PH_FRINFC_MIFARESTD_FLAG1; 2103 2104 NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2105 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2106 /* 16 bytes completed */ 2107 if (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_BYTES_READ) 2108 { 2109 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2110 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0; 2111 NdefMap->StdMifareContainer.currentBlock++; 2112 NdefMap->StdMifareContainer.NdefBlocks++; 2113 Result = phFriNfc_MifStd_H_BlkChk (NdefMap); 2114 if (Result == NFCSTATUS_SUCCESS) 2115 { 2116 if (NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1) 2117 { 2118 Result = phFriNfc_MifStd_H_RdABlock (NdefMap); 2119 } 2120 else 2121 { 2122 Result = phFriNfc_MifStd_H_AuthSector (NdefMap); 2123 } 2124 } 2125 } 2126 else 2127 { 2128 NdefMap->TLVStruct.BytesRemainLinTLV = 2129 PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0; 2130 /* The read operation has finished. so, completion routine 2131 can be called. set the Completion routine(CR) flag */ 2132 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2133 } 2134 } 2135 else if ((NdefMap->TLVStruct.BytesRemainLinTLV > 2136 (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) && 2137 (RemainingBytes <= (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)))) 2138 { 2139 /* Copy data to user buffer */ 2140 memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2141 &(NdefMap->SendRecvBuf [(*Temp16Bytes)]), 2142 RemainingBytes); 2143 NdefMap->ApduBuffIndex += RemainingBytes; 2144 NdefMap->StdMifareContainer.remainingSize -= RemainingBytes; 2145 2146 /* Remaining Bytes of length (L) in TLV */ 2147 NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes; 2148 /* copy the bytes to internal buffer, that are read, 2149 but not used for the user buffer */ 2150 memcpy (NdefMap->StdMifareContainer.internalBuf, 2151 &(NdefMap->SendRecvBuf[(RemainingBytes + (*Temp16Bytes))]), 2152 ((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)) 2153 - RemainingBytes)); 2154 2155 /* internal buffer length */ 2156 NdefMap->StdMifareContainer.internalLength = 2157 ((PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)) - RemainingBytes); 2158 2159 if (RemainingBytes == (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) 2160 { 2161 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t) 2162 (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) || 2163 (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ? 2164 PH_FRINFC_MIFARESTD_FLAG1 : 2165 PH_FRINFC_MIFARESTD_FLAG0); 2166 2167 /* internal length bytes completed */ 2168 NdefMap->StdMifareContainer.currentBlock++; 2169 NdefMap->StdMifareContainer.NdefBlocks++; 2170 } 2171 *Temp16Bytes += ((uint8_t) RemainingBytes); 2172 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0; 2173 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2174 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2175 } 2176 else 2177 { 2178 if ((NdefMap->TLVStruct.BytesRemainLinTLV > 2179 (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))) && 2180 (RemainingBytes > (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)))) 2181 { 2182 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2183 /* Copy data to user buffer */ 2184 memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2185 &(NdefMap->SendRecvBuf [(*Temp16Bytes)]), 2186 (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes))); 2187 NdefMap->ApduBuffIndex += (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)); 2188 NdefMap->StdMifareContainer.remainingSize -= 2189 (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)); 2190 NdefMap->TLVStruct.BytesRemainLinTLV -= (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)); 2191 *Temp16Bytes += (PH_FRINFC_MIFARESTD_BYTES_READ - (*Temp16Bytes)); 2192 if (NdefMap->TLVStruct.BytesRemainLinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0) 2193 { 2194 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0; 2195 } 2196 /* 16 bytes completed */ 2197 NdefMap->StdMifareContainer.currentBlock++; 2198 NdefMap->StdMifareContainer.NdefBlocks++; 2199 Result = phFriNfc_MifStd_H_BlkChk (NdefMap); 2200 if (Result == NFCSTATUS_SUCCESS) 2201 { 2202 Result = ((NdefMap->StdMifareContainer.AuthDone == 2203 PH_FRINFC_MIFARESTD_FLAG1) ? 2204 phFriNfc_MifStd_H_RdABlock(NdefMap) : 2205 phFriNfc_MifStd_H_AuthSector(NdefMap)); 2206 } 2207 } 2208 } 2209 2210 if (CRFlag == PH_FRINFC_MIFARESTD_FLAG1) 2211 { 2212 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 2213 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t) 2214 (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) || 2215 (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ? 2216 PH_FRINFC_MIFARESTD_FLAG1 : 2217 PH_FRINFC_MIFARESTD_FLAG0); 2218 } 2219 2220 return Result; 2221 } 2222 2223 /****************************************************************************** 2224 * Function phFriNfc_MifStd_H_ChkIntLen 2225 * 2226 * Description This function reads ndef to process the internal bytes. 2227 * 2228 * Returns This function return NFCSTATUS_SUCCESS in case of success, 2229 * In case of failure returns other failure value. 2230 * 2231 ******************************************************************************/ 2232 static NFCSTATUS phFriNfc_MifStd_H_ChkIntLen(phFriNfc_NdefMap_t *NdefMap) 2233 { 2234 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2235 uint8_t NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1; 2236 uint8_t TempintBytes = 0; 2237 2238 if(NdefMap->TLVStruct.BytesRemainLinTLV != 0) 2239 { 2240 NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 2241 /* To read the remaining length (L) in TLV */ 2242 Result = phFriNfc_MifStd_H_IntLenWioutNdef(NdefMap, &NDEFFlag, &TempintBytes); 2243 } 2244 NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 2245 /* check the NDEFFlag is set. if this is not set, then 2246 in the above RemainTLV function all the 16 bytes has been 2247 read */ 2248 2249 return Result; 2250 } 2251 2252 /****************************************************************************** 2253 * Function phFriNfc_MifStd_H_IntLenWioutNdef 2254 * 2255 * Description This function reads ndef to check the internal bytes 2256 * without ndef tlv flag. 2257 * 2258 * Returns This function return NFCSTATUS_SUCCESS in case of success, 2259 * In case of failure returns other failure value. 2260 * 2261 ******************************************************************************/ 2262 static NFCSTATUS phFriNfc_MifStd_H_IntLenWioutNdef (phFriNfc_NdefMap_t *NdefMap, 2263 uint8_t *Flag, 2264 uint8_t *TempintBytes) 2265 { 2266 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2267 uint8_t CRFlag = 0; 2268 uint16_t RemainingBytes = 0; 2269 2270 RemainingBytes = ((uint16_t)NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 2271 2272 if (NdefMap->StdMifareContainer.remainingSize < RemainingBytes) 2273 { 2274 /* If the user Buffer is greater than the Card Size 2275 set LastBlockFlag = 1. This Flag is used to read bytes 2276 till the end of the card only */ 2277 RemainingBytes = NdefMap->StdMifareContainer.remainingSize; 2278 } 2279 2280 /* Remaining Bytes of length (L) in TLV <= internal length */ 2281 if ((NdefMap->TLVStruct.BytesRemainLinTLV <= 2282 NdefMap->StdMifareContainer.internalLength) && 2283 (RemainingBytes <= NdefMap->TLVStruct.BytesRemainLinTLV)) 2284 { 2285 memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2286 &(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]), 2287 RemainingBytes); 2288 NdefMap->ApduBuffIndex += RemainingBytes; 2289 NdefMap->StdMifareContainer.remainingSize -= RemainingBytes; 2290 *TempintBytes += ((uint8_t) RemainingBytes); 2291 2292 /* copy the bytes to internal buffer, that are read, 2293 but not used for the user buffer */ 2294 memcpy (NdefMap->StdMifareContainer.internalBuf, 2295 &(NdefMap->StdMifareContainer.internalBuf [RemainingBytes]), 2296 (NdefMap->StdMifareContainer.internalLength - RemainingBytes)); 2297 2298 /* internal buffer length */ 2299 NdefMap->StdMifareContainer.internalLength -= RemainingBytes; 2300 2301 NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes; 2302 if (NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0) 2303 { 2304 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t) 2305 (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) || 2306 (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ? 2307 PH_FRINFC_MIFARESTD_FLAG1 : 2308 PH_FRINFC_MIFARESTD_FLAG0); 2309 2310 /* internal length bytes completed */ 2311 NdefMap->StdMifareContainer.currentBlock++; 2312 NdefMap->StdMifareContainer.NdefBlocks++; 2313 } 2314 2315 /* Remaining Bytes of length value in TLV */ 2316 if (NdefMap->TLVStruct.BytesRemainLinTLV == 0) 2317 { 2318 /* Remaining Bytes of length (L) in TLV is Zero means that the next 2319 coming bytes are containing type (T), length (L) in TLV */ 2320 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1; 2321 NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2322 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2323 } 2324 /* call completion routine */ 2325 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2326 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2327 } 2328 else if ((NdefMap->TLVStruct.BytesRemainLinTLV <= 2329 NdefMap->StdMifareContainer.internalLength) && 2330 (RemainingBytes > NdefMap->TLVStruct.BytesRemainLinTLV)) 2331 { 2332 memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2333 &(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]), 2334 NdefMap->TLVStruct.BytesRemainLinTLV); 2335 2336 NdefMap->ApduBuffIndex += NdefMap->TLVStruct.BytesRemainLinTLV; 2337 NdefMap->StdMifareContainer.remainingSize -= NdefMap->TLVStruct.BytesRemainLinTLV; 2338 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG1; 2339 2340 *TempintBytes += ((uint8_t) NdefMap->TLVStruct.BytesRemainLinTLV); 2341 *Flag = PH_FRINFC_MIFARESTD_FLAG1; 2342 2343 NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2344 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2345 2346 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0; 2347 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t) 2348 (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) || 2349 (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ? 2350 PH_FRINFC_MIFARESTD_FLAG1 : 2351 PH_FRINFC_MIFARESTD_FLAG0); 2352 2353 if (PH_FRINFC_MIFARESTD_FLAG1 == NdefMap->StdMifareContainer.ReadWriteCompleteFlag) 2354 { 2355 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2356 } 2357 2358 if (NdefMap->TLVStruct.BytesRemainLinTLV == NdefMap->StdMifareContainer.internalLength) 2359 { 2360 /* Remaining Bytes in Length (L) field of TLV is 0 */ 2361 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0; 2362 NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0; 2363 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2364 /* internal length bytes completed */ 2365 NdefMap->StdMifareContainer.currentBlock++; 2366 NdefMap->StdMifareContainer.NdefBlocks++; 2367 Result = phFriNfc_MifStd_H_BlkChk (NdefMap); 2368 if (Result == NFCSTATUS_SUCCESS) 2369 { 2370 Result = 2371 ((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1) ? 2372 phFriNfc_MifStd_H_RdABlock (NdefMap) : 2373 phFriNfc_MifStd_H_AuthSector (NdefMap)); 2374 } 2375 } 2376 else 2377 { 2378 /* Remaining Bytes in Length (L) field of TLV is 0 */ 2379 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0; 2380 *Flag = PH_FRINFC_MIFARESTD_FLAG1; 2381 } 2382 } 2383 else if ((NdefMap->TLVStruct.BytesRemainLinTLV > 2384 NdefMap->StdMifareContainer.internalLength) && 2385 (RemainingBytes <= NdefMap->StdMifareContainer.internalLength)) 2386 { 2387 memcpy(&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2388 &(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]), 2389 RemainingBytes); 2390 2391 NdefMap->ApduBuffIndex += RemainingBytes; 2392 NdefMap->StdMifareContainer.remainingSize -= RemainingBytes; 2393 *TempintBytes += ((uint8_t) RemainingBytes); 2394 /* Remaining Bytes of length (L) in TLV */ 2395 NdefMap->TLVStruct.BytesRemainLinTLV -= RemainingBytes; 2396 2397 /* copy the bytes to internal buffer, that are read, 2398 but not used for the user buffer */ 2399 memcpy (NdefMap->StdMifareContainer.internalBuf, 2400 &(NdefMap->StdMifareContainer.internalBuf [RemainingBytes]), 2401 (NdefMap->StdMifareContainer.internalLength - RemainingBytes)); 2402 2403 /* internal buffer length */ 2404 NdefMap->StdMifareContainer.internalLength -= RemainingBytes; 2405 if (NdefMap->StdMifareContainer.internalLength == PH_FRINFC_MIFARESTD_VAL0) 2406 { 2407 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t) 2408 (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) || 2409 (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0)) ? 2410 PH_FRINFC_MIFARESTD_FLAG1 : 2411 PH_FRINFC_MIFARESTD_FLAG0); 2412 2413 /* internal length bytes completed */ 2414 NdefMap->StdMifareContainer.currentBlock++; 2415 NdefMap->StdMifareContainer.NdefBlocks++; 2416 } 2417 2418 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0; 2419 CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2420 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2421 } 2422 else 2423 { 2424 if ((NdefMap->TLVStruct.BytesRemainLinTLV > 2425 NdefMap->StdMifareContainer.internalLength) && 2426 (RemainingBytes > NdefMap->StdMifareContainer.internalLength)) 2427 { 2428 memcpy (&(NdefMap->ApduBuffer [NdefMap->ApduBuffIndex]), 2429 &(NdefMap->StdMifareContainer.internalBuf [(*TempintBytes)]), 2430 NdefMap->StdMifareContainer.internalLength); 2431 *Flag = PH_FRINFC_MIFARESTD_FLAG0; 2432 NdefMap->ApduBuffIndex += NdefMap->StdMifareContainer.internalLength; 2433 NdefMap->StdMifareContainer.remainingSize -= 2434 NdefMap->StdMifareContainer.internalLength; 2435 NdefMap->TLVStruct.BytesRemainLinTLV -= NdefMap->StdMifareContainer.internalLength; 2436 2437 if(NdefMap->TLVStruct.BytesRemainLinTLV != PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0) 2438 { 2439 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0; 2440 } 2441 2442 NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0; 2443 /* internal length bytes completed */ 2444 NdefMap->StdMifareContainer.currentBlock++; 2445 NdefMap->StdMifareContainer.NdefBlocks++; 2446 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 2447 if(Result == NFCSTATUS_SUCCESS) 2448 { 2449 Result = ((NdefMap->StdMifareContainer.AuthDone == 2450 PH_FRINFC_MIFARESTD_FLAG1)? 2451 phFriNfc_MifStd_H_RdABlock(NdefMap): 2452 phFriNfc_MifStd_H_AuthSector(NdefMap)); 2453 } 2454 } 2455 } 2456 2457 if(CRFlag == PH_FRINFC_MIFARESTD_FLAG1) 2458 { 2459 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = (uint8_t) 2460 (((NdefMap->StdMifareContainer.remainingSize == PH_FRINFC_MIFARESTD_VAL0) || 2461 (NdefMap->TLVStruct.BytesRemainLinTLV == PH_FRINFC_MIFARESTD_VAL0))? 2462 PH_FRINFC_MIFARESTD_FLAG1: 2463 PH_FRINFC_MIFARESTD_FLAG0); 2464 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 2465 } 2466 2467 return Result; 2468 } 2469 2470 /****************************************************************************** 2471 * Function phFriNfc_MifStd_H_WriteNdefLen 2472 * 2473 * Description This function is Helper function for write ndef 2474 * to write the Length TLV. 2475 * 2476 * Returns This function return NFCSTATUS_PENDING in case of success 2477 * In case of failure returns other failure value. 2478 * 2479 ******************************************************************************/ 2480 static NFCSTATUS phFriNfc_MifStd_H_WriteNdefLen(phFriNfc_NdefMap_t *NdefMap) 2481 { 2482 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2483 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN; 2484 2485 /* If Current block = Ndef TLV block then the starting point 2486 is writing from type of TLV 2487 Else */ 2488 2489 if(NdefMap->StdMifareContainer.currentBlock == 2490 NdefMap->TLVStruct.NdefTLVBlock) 2491 { 2492 2493 if(NdefMap->TLVStruct.NULLTLVCount >= 2494 PH_FRINFC_MIFARESTD_VAL2) 2495 { 2496 phFriNfc_MifStd_H_fillTLV1(NdefMap); 2497 } 2498 else 2499 { 2500 phFriNfc_MifStd_H_fillTLV2(NdefMap); 2501 } 2502 } 2503 else 2504 { 2505 if(NdefMap->TLVStruct.NULLTLVCount >= 2506 PH_FRINFC_MIFARESTD_VAL2) 2507 { 2508 phFriNfc_MifStd_H_fillTLV1_1(NdefMap); 2509 } 2510 else 2511 { 2512 phFriNfc_MifStd_H_fillTLV2_1(NdefMap); 2513 } 2514 } 2515 2516 memcpy( NdefMap->StdMifareContainer.Buffer, 2517 &(NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1]), 2518 PH_FRINFC_MIFARESTD_BYTES_READ); 2519 2520 2521 /* Write from here */ 2522 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE; 2523 2524 NdefMap->Cmd.MfCmd = phHal_eMifareWrite16; 2525 2526 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 2527 /* Call the Overlapped HAL Transceive function */ 2528 Result = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, 2529 NdefMap->Cmd, 2530 NdefMap->SendRecvBuf, 2531 NdefMap->SendLength, 2532 NdefMap->SendRecvLength); 2533 2534 return Result; 2535 } 2536 2537 /****************************************************************************** 2538 * Function phFriNfc_MifStd_H_RdWrReset 2539 * 2540 * Description It resets ndef TLV values. This is used when the offset 2541 * is BEGIN. 2542 * 2543 * Returns void 2544 * 2545 ******************************************************************************/ 2546 static void phFriNfc_MifStd_H_RdWrReset(phFriNfc_NdefMap_t *NdefMap) 2547 { 2548 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_BLK4; 2549 NdefMap->StdMifareContainer.NdefBlocks = PH_FRINFC_MIFARESTD_VAL1; 2550 NdefMap->TLVStruct.BytesRemainLinTLV = PH_FRINFC_MIFARESTD_VAL0; 2551 NdefMap->TLVStruct.LcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2552 NdefMap->TLVStruct.TcheckedinTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 2553 NdefMap->TLVStruct.NdefTLVAuthFlag = PH_FRINFC_MIFARESTD_FLAG0; 2554 NdefMap->TLVStruct.NdefTLVBlock = PH_FRINFC_MIFARESTD_MAD_BLK0; 2555 NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0; 2556 NdefMap->TLVStruct.NoLbytesinTLV = PH_FRINFC_MIFARESTD_VAL0; 2557 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 2558 NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0; 2559 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0; 2560 NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0; 2561 NdefMap->StdMifareContainer.FirstReadFlag = PH_FRINFC_MIFARESTD_FLAG1; 2562 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = PH_FRINFC_MIFARESTD_FLAG0; 2563 NdefMap->StdMifareContainer.remainingSize = (uint16_t) 2564 (NdefMap->StdMifareContainer.NoOfNdefCompBlocks * 2565 PH_FRINFC_MIFARESTD_BLOCK_BYTES); 2566 NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL1; 2567 2568 return; 2569 } 2570 2571 /****************************************************************************** 2572 * Function phFriNfc_MifStd_H_RdtoWrNdefLen 2573 * 2574 * Description This function is used to read the first ndef compliant 2575 * block to change the length. 2576 * 2577 * Returns This function return NFCSTATUS_PENDING in case of success 2578 * In case of failure returns other failure value. 2579 * 2580 ******************************************************************************/ 2581 static NFCSTATUS phFriNfc_MifStd_H_RdtoWrNdefLen(phFriNfc_NdefMap_t *NdefMap) 2582 { 2583 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2584 2585 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_TO_WR_NDEF_LEN; 2586 2587 if(NdefMap->TLVStruct.NdefTLVAuthFlag == PH_FRINFC_MIFARESTD_FLAG1) 2588 { 2589 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0; 2590 Result = phFriNfc_MifStd_H_AuthSector(NdefMap); 2591 } 2592 else 2593 { 2594 NdefMap->SendRecvBuf[0] = NdefMap->StdMifareContainer.currentBlock; 2595 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ; 2596 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 2597 2598 NdefMap->Cmd.MfCmd = phHal_eMifareRead; 2599 2600 /* Call the Overlapped HAL Transceive function */ 2601 Result = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, 2602 NdefMap->Cmd, 2603 NdefMap->SendRecvBuf, 2604 NdefMap->SendLength, 2605 NdefMap->SendRecvLength); 2606 } 2607 2608 return Result; 2609 } 2610 2611 /****************************************************************************** 2612 * Function phFriNfc_MifStd_H_SetNdefBlkAuth 2613 * 2614 * Description This function is used to set the authentication flag 2615 * for the ndef TLV block. 2616 * 2617 * Returns void 2618 * 2619 ******************************************************************************/ 2620 static void phFriNfc_MifStd_H_SetNdefBlkAuth(phFriNfc_NdefMap_t *NdefMap) 2621 { 2622 NdefMap->TLVStruct.NdefTLVAuthFlag = 2623 ((phFriNfc_MifStd_H_GetSect(NdefMap->TLVStruct.NdefTLVBlock) 2624 == phFriNfc_MifStd_H_GetSect(NdefMap->StdMifareContainer.currentBlock))? 2625 PH_FRINFC_MIFARESTD_FLAG0: 2626 PH_FRINFC_MIFARESTD_FLAG1); 2627 2628 return; 2629 } 2630 2631 /****************************************************************************** 2632 * Function phFriNfc_MifStd_H_GetActCardLen 2633 * 2634 * Description Helper function to get the actual length of card. 2635 * 2636 * Returns This function return NFCSTATUS_SUCCESS in case of success 2637 * In case of failure returns other failure value. 2638 * 2639 ******************************************************************************/ 2640 static NFCSTATUS phFriNfc_MifStd_H_GetActCardLen(phFriNfc_NdefMap_t *NdefMap) 2641 { 2642 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2643 2644 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_GET_ACT_CARDSIZE; 2645 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE; 2646 2647 Result = ((NdefMap->StdMifareContainer.AuthDone == 2648 PH_FRINFC_MIFARESTD_FLAG0)? 2649 phFriNfc_MifStd_H_AuthSector(NdefMap): 2650 phFriNfc_MifStd_H_Rd16Bytes(NdefMap, 2651 NdefMap->StdMifareContainer.currentBlock)); 2652 2653 return Result; 2654 } 2655 2656 /****************************************************************************** 2657 * Function phFriNfc_MifStd_H_ChkTLVs 2658 * 2659 * Description Helper function to check all the TLVs. 2660 * 2661 * Returns This function return NFCSTATUS_SUCCESS in case of success 2662 * In case of failure returns other failure value. 2663 * 2664 ******************************************************************************/ 2665 static NFCSTATUS phFriNfc_MifStd_H_ChkTLVs(phFriNfc_NdefMap_t *NdefMap, 2666 uint8_t *CRFlag) 2667 { 2668 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2669 uint16_t TempLength = PH_FRINFC_MIFARESTD_VAL0, 2670 ShiftLength = PH_FRINFC_MIFARESTD_VAL0; 2671 uint8_t TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0; 2672 2673 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE; 2674 TempLength = NdefMap->TLVStruct.NdefTLVByte; 2675 2676 for(;;) 2677 { 2678 if((NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_TERMTLV_T) && 2679 (NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_NULLTLV_T) && 2680 (NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_NDEFTLV_T) && 2681 (false == NdefMap->TLVStruct.NdefTLVFoundFlag)) 2682 { 2683 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2684 NFCSTATUS_NO_NDEF_SUPPORT); 2685 NdefMap->TLVStruct.BytesRemainLinTLV = 0; 2686 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 2687 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2688 break; 2689 2690 } 2691 else 2692 if((NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_TERMTLV_T) && 2693 (NdefMap->SendRecvBuf[TempLength] != PH_FRINFC_MIFARESTD_NULLTLV_T)) 2694 { 2695 if(NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_NDEFTLV_T) 2696 { 2697 NdefMap->TLVStruct.NdefTLVBlock = 2698 NdefMap->StdMifareContainer.currentBlock; 2699 NdefMap->TLVStruct.NdefTLVByte = (uint8_t)TempLength; 2700 NdefMap->TLVStruct.NdefTLVFoundFlag = 2701 ((NdefMap->SendRecvBuf[TempLength] == 2702 PH_FRINFC_MIFARESTD_NDEFTLV_T)? 2703 PH_FRINFC_MIFARESTD_FLAG1: 2704 PH_FRINFC_MIFARESTD_FLAG0); 2705 2706 NdefMap->TLVStruct.NULLTLVCount = ((NdefMap->TLVStruct.NULLTLVCount 2707 == PH_FRINFC_MIFARESTD_VAL1)? 2708 PH_FRINFC_MIFARESTD_VAL0: 2709 NdefMap->TLVStruct.NULLTLVCount); 2710 } 2711 else 2712 { 2713 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 2714 } 2715 2716 TempLength++; 2717 if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ) 2718 { 2719 NdefMap->TLVStruct.TcheckedinTLVFlag = 2720 PH_FRINFC_MIFARESTD_FLAG1; 2721 NdefMap->TLVStruct.NoLbytesinTLV = 2722 PH_FRINFC_MIFARESTD_VAL3; 2723 } 2724 Result = phFriNfc_MifStd_H_Chk16Bytes( NdefMap, 2725 TempLength); 2726 if(Result != NFCSTATUS_SUCCESS) 2727 { 2728 *CRFlag = (uint8_t)((Result == NFCSTATUS_PENDING)? 2729 PH_FRINFC_MIFARESTD_FLAG0: 2730 PH_FRINFC_MIFARESTD_FLAG1); 2731 break; 2732 } 2733 2734 if((((( NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 2735 NdefMap->StdMifareContainer.NdefBlocks) * 2736 PH_FRINFC_MIFARESTD_BLOCK_BYTES) + 2737 (PH_FRINFC_MIFARESTD_BLOCK_BYTES - 2738 TempLength)) < 2739 NdefMap->SendRecvBuf[TempLength]) && 2740 ((NdefMap->SendRecvBuf[TempLength] < 2741 PH_FRINFC_MIFARESTD_NDEFTLV_L) && 2742 (NdefMap->TLVStruct.NdefTLVFoundFlag != 2743 PH_FRINFC_MIFARESTD_VAL1))) 2744 { 2745 /* Result = Error */ 2746 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2747 NFCSTATUS_INVALID_PARAMETER); 2748 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2749 break; 2750 } 2751 2752 if((((( NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 2753 NdefMap->StdMifareContainer.NdefBlocks) * 2754 PH_FRINFC_MIFARESTD_BLOCK_BYTES) + 2755 (PH_FRINFC_MIFARESTD_BLOCK_BYTES - 2756 TempLength)) < 2757 NdefMap->SendRecvBuf[TempLength]) && 2758 ((NdefMap->SendRecvBuf[TempLength] == 2759 PH_FRINFC_MIFARESTD_VAL0) && 2760 (NdefMap->TLVStruct.NdefTLVFoundFlag == 2761 PH_FRINFC_MIFARESTD_VAL1))) 2762 { 2763 /* Result = Error */ 2764 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2765 NFCSTATUS_INVALID_PARAMETER); 2766 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2767 break; 2768 } 2769 2770 if((NdefMap->TLVStruct.NdefTLVFoundFlag == 2771 PH_FRINFC_MIFARESTD_FLAG1) && 2772 (NdefMap->SendRecvBuf[TempLength] < 2773 PH_FRINFC_MIFARESTD_NDEFTLV_L)) 2774 { 2775 Result = phFriNfc_MapTool_SetCardState(NdefMap, 2776 NdefMap->SendRecvBuf[TempLength]); 2777 NdefMap->TLVStruct.BytesRemainLinTLV = 2778 NdefMap->SendRecvBuf[TempLength]; 2779 NdefMap->StdMifareContainer.remainingSize -= 2780 PH_FRINFC_MIFARESTD_VAL2; 2781 /* This flag is set */ 2782 NdefMap->StdMifareContainer.remSizeUpdFlag = 2783 (uint8_t)((NdefMap->TLVStruct.NULLTLVCount >= 2784 PH_FRINFC_MIFARESTD_VAL2)? 2785 PH_FRINFC_MIFARESTD_FLAG0: 2786 PH_FRINFC_MIFARESTD_FLAG1); 2787 2788 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2789 break; 2790 } 2791 2792 NdefMap->StdMifareContainer.remainingSize -= 2793 (( NdefMap->SendRecvBuf[TempLength] < 2794 PH_FRINFC_MIFARESTD_NDEFTLV_L)? 2795 (NdefMap->SendRecvBuf[TempLength] 2796 + PH_FRINFC_MIFARESTD_VAL2): 2797 PH_FRINFC_MIFARESTD_VAL0); 2798 2799 if(NdefMap->SendRecvBuf[TempLength] == 2800 PH_FRINFC_MIFARESTD_VAL0) 2801 { 2802 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2803 NFCSTATUS_INVALID_PARAMETER); 2804 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2805 break; 2806 } 2807 2808 TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0; 2809 /* get the next TLV after the proprietary TLV */ 2810 Result = 2811 ((NdefMap->SendRecvBuf[TempLength] < 2812 PH_FRINFC_MIFARESTD_NDEFTLV_L)? 2813 phFriNfc_MifStd_H_GetNxtTLV(NdefMap, &TempLength, &TL4bytesFlag): 2814 NFCSTATUS_PENDING); 2815 2816 if((TempLength >= PH_FRINFC_MIFARESTD_BYTES_READ) && 2817 (Result == NFCSTATUS_SUCCESS)) 2818 { 2819 NdefMap->TLVStruct.TcheckedinTLVFlag = 2820 PH_FRINFC_MIFARESTD_FLAG0; 2821 NdefMap->TLVStruct.NoLbytesinTLV = 2822 PH_FRINFC_MIFARESTD_VAL0; 2823 2824 Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap); 2825 *CRFlag = (uint8_t)((Result != NFCSTATUS_PENDING)? 2826 PH_FRINFC_MIFARESTD_FLAG1: 2827 PH_FRINFC_MIFARESTD_FLAG0); 2828 break; 2829 } 2830 else 2831 { 2832 if(Result == NFCSTATUS_PENDING) 2833 { 2834 TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG1; 2835 Result = ((NdefMap->SendRecvBuf[TempLength] == 2836 PH_FRINFC_MIFARESTD_NDEFTLV_L) ? 2837 NFCSTATUS_SUCCESS: 2838 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2839 NFCSTATUS_INVALID_PARAMETER))); 2840 2841 if(Result != NFCSTATUS_SUCCESS) 2842 { 2843 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2844 break; 2845 } 2846 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 2847 TempLength++; 2848 /* Check 0xFF */ 2849 if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ) 2850 { 2851 NdefMap->TLVStruct.TcheckedinTLVFlag = 2852 PH_FRINFC_MIFARESTD_FLAG1; 2853 NdefMap->TLVStruct.NoLbytesinTLV = 2854 PH_FRINFC_MIFARESTD_VAL2; 2855 } 2856 Result = phFriNfc_MifStd_H_Chk16Bytes( NdefMap, 2857 TempLength); 2858 if(Result != NFCSTATUS_SUCCESS) 2859 { 2860 break; 2861 } 2862 2863 ShiftLength = NdefMap->SendRecvBuf[TempLength]; 2864 TempLength++; 2865 if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ) 2866 { 2867 NdefMap->TLVStruct.TcheckedinTLVFlag = 2868 PH_FRINFC_MIFARESTD_FLAG1; 2869 NdefMap->TLVStruct.NoLbytesinTLV = 2870 PH_FRINFC_MIFARESTD_VAL1; 2871 NdefMap->TLVStruct.prevLenByteValue = 2872 NdefMap->SendRecvBuf[(TempLength - 2873 PH_FRINFC_MIFARESTD_VAL1)]; 2874 } 2875 Result = phFriNfc_MifStd_H_Chk16Bytes( NdefMap, 2876 TempLength); 2877 if(Result != NFCSTATUS_SUCCESS) 2878 { 2879 break; 2880 } 2881 2882 2883 if(((( NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 2884 NdefMap->StdMifareContainer.NdefBlocks) * 2885 PH_FRINFC_MIFARESTD_BLOCK_BYTES) + 2886 (PH_FRINFC_MIFARESTD_BLOCK_BYTES - 2887 TempLength)) < 2888 (( ShiftLength 2889 << 8) + NdefMap->SendRecvBuf[TempLength])) 2890 { 2891 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2892 NFCSTATUS_INVALID_REMOTE_DEVICE); 2893 2894 break; 2895 } 2896 2897 if(NdefMap->TLVStruct.NdefTLVFoundFlag == 2898 PH_FRINFC_MIFARESTD_FLAG1) 2899 { 2900 ShiftLength = (( ShiftLength<< 8) + 2901 NdefMap->SendRecvBuf[TempLength]); 2902 NdefMap->TLVStruct.BytesRemainLinTLV = ShiftLength; 2903 Result = phFriNfc_MapTool_SetCardState(NdefMap, 2904 ShiftLength); 2905 NdefMap->StdMifareContainer.remainingSize -= 2906 PH_FRINFC_MIFARESTD_VAL4; 2907 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 2908 break; 2909 } 2910 2911 NdefMap->StdMifareContainer.remainingSize -= 2912 ((ShiftLength<< 8) + 2913 NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)]); 2914 TempLength++; 2915 2916 /* get the next TLV after the proprietary TLV */ 2917 Result = phFriNfc_MifStd_H_GetNxtTLV(NdefMap, &TempLength, &TL4bytesFlag); 2918 2919 if((TempLength >= PH_FRINFC_MIFARESTD_BYTES_READ) && 2920 (Result == NFCSTATUS_SUCCESS)) 2921 { 2922 NdefMap->TLVStruct.TcheckedinTLVFlag = 2923 PH_FRINFC_MIFARESTD_FLAG0; 2924 NdefMap->TLVStruct.NoLbytesinTLV = 2925 PH_FRINFC_MIFARESTD_VAL0; 2926 Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap); 2927 2928 break; 2929 } 2930 break; 2931 } 2932 } 2933 } 2934 else if((NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_TERMTLV_T) && 2935 (NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG0)) 2936 { 2937 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2938 NFCSTATUS_INVALID_PARAMETER); 2939 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 2940 PH_FRINFC_MIFARESTD_FLAG1; 2941 break; 2942 2943 } 2944 else if(NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_NULLTLV_T) 2945 { 2946 TempLength++; 2947 NdefMap->TLVStruct.NULLTLVCount += PH_FRINFC_MIFARESTD_VAL1; 2948 ShiftLength = NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL1)]; 2949 NdefMap->StdMifareContainer.remainingSize -= PH_FRINFC_MIFARESTD_VAL1; 2950 if(NdefMap->StdMifareContainer.remainingSize < 2951 (( ShiftLength << 8) + NdefMap->SendRecvBuf[TempLength])) 2952 { 2953 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2954 NFCSTATUS_INVALID_REMOTE_DEVICE); 2955 break; 2956 } 2957 Result = phFriNfc_MifStd_H_Chk16Bytes( NdefMap, 2958 TempLength); 2959 if(Result != NFCSTATUS_SUCCESS) 2960 { 2961 NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL0; 2962 break; 2963 } 2964 } 2965 else 2966 { 2967 if((NdefMap->SendRecvBuf[TempLength] == PH_FRINFC_MIFARESTD_TERMTLV_T) && 2968 (NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG1)) 2969 { 2970 TempLength++; 2971 Result = NFCSTATUS_SUCCESS; 2972 NdefMap->StdMifareContainer.remainingSize -= 2973 PH_FRINFC_MIFARESTD_VAL1; 2974 } 2975 } 2976 } 2977 2978 if(NdefMap->TLVStruct.BytesRemainLinTLV > 2979 NdefMap->StdMifareContainer.remainingSize) 2980 { 2981 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2982 NFCSTATUS_INVALID_FORMAT); 2983 } 2984 else 2985 { 2986 if(NdefMap->StdMifareContainer.remainingSize == 2987 PH_FRINFC_MIFARESTD_VAL0) 2988 { 2989 Result = ((NdefMap->TLVStruct.NdefTLVFoundFlag == 2990 PH_FRINFC_MIFARESTD_FLAG1)? 2991 NFCSTATUS_SUCCESS: 2992 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2993 NFCSTATUS_INVALID_PARAMETER))); 2994 } 2995 } 2996 2997 return Result; 2998 } 2999 3000 /****************************************************************************** 3001 * Function phFriNfc_MifStd_H_GetNxtTLV 3002 * 3003 * Description This is a Helper function to get the next TLV. 3004 * 3005 * Returns This function return NFCSTATUS_SUCCESS in case of success 3006 * In case of failure returns other failure value. 3007 * 3008 ******************************************************************************/ 3009 static NFCSTATUS phFriNfc_MifStd_H_GetNxtTLV(phFriNfc_NdefMap_t *NdefMap, 3010 uint16_t *TempLength, 3011 uint8_t *TL4bytesFlag) 3012 { 3013 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3014 uint16_t LengthRemaining = PH_FRINFC_MIFARESTD_VAL0, 3015 TempLen = PH_FRINFC_MIFARESTD_VAL0, 3016 ShiftLength = PH_FRINFC_MIFARESTD_VAL0; 3017 3018 TempLen = (*TempLength); 3019 LengthRemaining = (PH_FRINFC_MIFARESTD_BYTES_READ - 3020 (TempLen + PH_FRINFC_MIFARESTD_VAL1)); 3021 3022 if(*TL4bytesFlag == PH_FRINFC_MIFARESTD_FLAG0) 3023 { 3024 (*TempLength) += (NdefMap->SendRecvBuf[TempLen] + 3025 PH_FRINFC_MIFARESTD_VAL1); 3026 3027 if(NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG0) 3028 { 3029 LengthRemaining = 3030 (((*TempLength) < PH_FRINFC_MIFARESTD_BYTES_READ)? 3031 PH_FRINFC_MIFARESTD_VAL0: 3032 (NdefMap->SendRecvBuf[TempLen] - 3033 LengthRemaining)); 3034 } 3035 else 3036 { 3037 LengthRemaining = 3038 (((*TempLength) < PH_FRINFC_MIFARESTD_BYTES_READ)? 3039 PH_FRINFC_MIFARESTD_VAL0: 3040 (NdefMap->SendRecvBuf[TempLen] - 3041 LengthRemaining)); 3042 } 3043 } 3044 else 3045 { 3046 *TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0; 3047 if(NdefMap->TLVStruct.NoLbytesinTLV == 3048 PH_FRINFC_MIFARESTD_VAL1) 3049 { 3050 ShiftLength = NdefMap->TLVStruct.prevLenByteValue; 3051 (*TempLength) += ((ShiftLength 3052 << 8) + NdefMap->SendRecvBuf[TempLen] + 3053 PH_FRINFC_MIFARESTD_VAL1); 3054 3055 LengthRemaining = 3056 (((ShiftLength 3057 << 8) + NdefMap->SendRecvBuf[TempLen]) - 3058 LengthRemaining); 3059 } 3060 else 3061 { 3062 ShiftLength = NdefMap->SendRecvBuf[(TempLen - PH_FRINFC_MIFARESTD_VAL1)]; 3063 (*TempLength) += ((ShiftLength 3064 << 8) + NdefMap->SendRecvBuf[TempLen] + 3065 PH_FRINFC_MIFARESTD_VAL1); 3066 3067 LengthRemaining = 3068 (((ShiftLength 3069 << 8) + NdefMap->SendRecvBuf[TempLen]) - 3070 LengthRemaining); 3071 } 3072 } 3073 3074 NdefMap->TLVStruct.NdefTLVByte = 3075 (uint8_t)(((*TempLength) < PH_FRINFC_MIFARESTD_BYTES_READ)? 3076 (*TempLength): 3077 (LengthRemaining % PH_FRINFC_MIFARESTD_BYTES_READ)); 3078 3079 while(LengthRemaining != PH_FRINFC_MIFARESTD_VAL0) 3080 { 3081 NdefMap->StdMifareContainer.currentBlock++; 3082 NdefMap->StdMifareContainer.NdefBlocks++; 3083 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 3084 LengthRemaining -= 3085 ((LengthRemaining <= PH_FRINFC_MIFARESTD_BYTES_READ)? 3086 LengthRemaining: 3087 PH_FRINFC_MIFARESTD_BYTES_READ); 3088 } 3089 3090 if(NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL0) 3091 { 3092 NdefMap->StdMifareContainer.currentBlock++; 3093 NdefMap->StdMifareContainer.NdefBlocks++; 3094 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 3095 } 3096 3097 return Result; 3098 } 3099 3100 /****************************************************************************** 3101 * Function phFriNfc_MifStd_H_Chk16Bytes 3102 * 3103 * Description This Helper function is used to know whether the read 3104 * 16 bytes are parsed completely. 3105 * 3106 * Returns This function return NFCSTATUS_SUCCESS in case of success 3107 * In case of failure returns other failure value. 3108 * 3109 ******************************************************************************/ 3110 static NFCSTATUS phFriNfc_MifStd_H_Chk16Bytes(phFriNfc_NdefMap_t *NdefMap, 3111 uint16_t TempLength) 3112 { 3113 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3114 3115 if(TempLength == PH_FRINFC_MIFARESTD_BYTES_READ) 3116 { 3117 NdefMap->StdMifareContainer.currentBlock++; 3118 NdefMap->StdMifareContainer.NdefBlocks++; 3119 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 3120 3121 Result = 3122 ((NdefMap->StdMifareContainer.AuthDone == PH_FRINFC_MIFARESTD_FLAG1)? 3123 phFriNfc_MifStd_H_GetActCardLen(NdefMap): 3124 phFriNfc_MifStd_H_AuthSector(NdefMap)); 3125 } 3126 3127 return Result; 3128 } 3129 3130 /****************************************************************************** 3131 * Function phFriNfc_MifStd_H_ChkRemainTLVs 3132 * 3133 * Description This function is used to know whether the read 3134 * 16 bytes are parsed completely. 3135 * 3136 * Returns This function return NFCSTATUS_SUCCESS in case of success 3137 * In case of failure returns other failure value. 3138 * 3139 ******************************************************************************/ 3140 static NFCSTATUS phFriNfc_MifStd_H_ChkRemainTLVs(phFriNfc_NdefMap_t *NdefMap, 3141 uint8_t *CRFlag, 3142 uint8_t *NDEFFlag) 3143 { 3144 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3145 uint16_t TempLength = PH_FRINFC_MIFARESTD_VAL0, 3146 ShiftLength = PH_FRINFC_MIFARESTD_VAL0; 3147 uint8_t TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0; 3148 3149 switch(NdefMap->TLVStruct.NoLbytesinTLV) 3150 { 3151 case PH_FRINFC_MIFARESTD_VAL3: 3152 /* if TLV is found then set card state */ 3153 Result = ((NdefMap->TLVStruct.NdefTLVFoundFlag == 3154 PH_FRINFC_MIFARESTD_FLAG1)? 3155 phFriNfc_MapTool_SetCardState(NdefMap, 3156 NdefMap->SendRecvBuf[TempLength]): 3157 Result); 3158 3159 Result = ((NdefMap->TLVStruct.NdefTLVFoundFlag == 3160 PH_FRINFC_MIFARESTD_FLAG1)? 1 : Result); 3161 3162 3163 /* Check the length field is less than or 3164 equal to 0xFF if yes enter below statement 3165 else enter else if*/ 3166 if((NdefMap->SendRecvBuf[TempLength] < 3167 PH_FRINFC_MIFARESTD_NDEFTLV_L) && 3168 (Result == NFCSTATUS_SUCCESS)) 3169 { 3170 NdefMap->StdMifareContainer.remainingSize -= 3171 PH_FRINFC_MIFARESTD_VAL2; 3172 3173 Result = ((NdefMap->SendRecvBuf[TempLength] > 3174 NdefMap->StdMifareContainer.remainingSize)? 3175 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 3176 NFCSTATUS_INVALID_FORMAT)): 3177 Result); 3178 TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0; 3179 if((NdefMap->TLVStruct.NdefTLVFoundFlag == 3180 PH_FRINFC_MIFARESTD_FLAG1) && 3181 (Result == NFCSTATUS_SUCCESS)) 3182 { 3183 NdefMap->TLVStruct.BytesRemainLinTLV = 3184 NdefMap->SendRecvBuf[TempLength]; 3185 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 3186 3187 } 3188 else if(Result == NFCSTATUS_SUCCESS) 3189 { 3190 TempLength++; 3191 Result = phFriNfc_MifStd_H_GetNxtTLV(NdefMap, 3192 &TempLength, &TL4bytesFlag); 3193 3194 NdefMap->StdMifareContainer.remainingSize -= 3195 NdefMap->SendRecvBuf[TempLength]; 3196 if((TempLength >= PH_FRINFC_MIFARESTD_BYTES_READ) && 3197 (*CRFlag == PH_FRINFC_MIFARESTD_FLAG0)) 3198 { 3199 *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 3200 Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap); 3201 } 3202 } 3203 3204 else 3205 { 3206 /* do nothing */ 3207 } 3208 } 3209 else if((NdefMap->SendRecvBuf[TempLength] == 3210 PH_FRINFC_MIFARESTD_NDEFTLV_L) && 3211 (Result == NFCSTATUS_SUCCESS)) 3212 { 3213 TempLength++; 3214 NdefMap->StdMifareContainer.remainingSize -= 3215 PH_FRINFC_MIFARESTD_VAL4; 3216 TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG0; 3217 Result = (((((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) + 3218 NdefMap->SendRecvBuf[(TempLength + 3219 PH_FRINFC_MIFARESTD_VAL1)]) > 3220 NdefMap->StdMifareContainer.remainingSize)? 3221 (PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 3222 NFCSTATUS_INVALID_FORMAT)): 3223 Result); 3224 if((NdefMap->TLVStruct.NdefTLVFoundFlag == 3225 PH_FRINFC_MIFARESTD_FLAG1) && 3226 (Result == NFCSTATUS_SUCCESS)) 3227 { 3228 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 3229 NdefMap->TLVStruct.BytesRemainLinTLV = 3230 (((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) + 3231 NdefMap->SendRecvBuf[(TempLength + 3232 PH_FRINFC_MIFARESTD_VAL1)]); 3233 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 3234 } 3235 else if(Result == NFCSTATUS_SUCCESS) 3236 { 3237 TempLength++; 3238 3239 Result = phFriNfc_MifStd_H_GetNxtTLV(NdefMap, 3240 &TempLength, &TL4bytesFlag); 3241 NdefMap->StdMifareContainer.remainingSize -= 3242 (((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) + 3243 NdefMap->SendRecvBuf[(TempLength + 3244 PH_FRINFC_MIFARESTD_VAL1)]); 3245 3246 *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 3247 Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap); 3248 } 3249 else 3250 { 3251 /* do nothing */ 3252 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 3253 } 3254 } 3255 else 3256 { 3257 /* Result = Error */ 3258 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 3259 NFCSTATUS_INVALID_FORMAT); 3260 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 3261 } 3262 break; 3263 3264 case PH_FRINFC_MIFARESTD_VAL2: 3265 case PH_FRINFC_MIFARESTD_VAL1: 3266 ShiftLength = ((NdefMap->TLVStruct.NoLbytesinTLV == 3267 PH_FRINFC_MIFARESTD_VAL1)? 3268 ((NdefMap->TLVStruct.prevLenByteValue << 8) + 3269 NdefMap->SendRecvBuf[TempLength]): 3270 (((uint16_t)NdefMap->SendRecvBuf[TempLength] << 8) + 3271 NdefMap->SendRecvBuf[(TempLength + 3272 PH_FRINFC_MIFARESTD_VAL1)])); 3273 if(((( NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 3274 NdefMap->StdMifareContainer.NdefBlocks) * 3275 PH_FRINFC_MIFARESTD_BLOCK_BYTES) + 3276 (PH_FRINFC_MIFARESTD_BLOCK_BYTES - 3277 TempLength)) < 3278 ShiftLength) 3279 { 3280 /* Result = Error */ 3281 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 3282 NFCSTATUS_INVALID_PARAMETER); 3283 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 3284 } 3285 else 3286 { 3287 NdefMap->StdMifareContainer.remainingSize -= 3288 PH_FRINFC_MIFARESTD_VAL2; 3289 if(NdefMap->TLVStruct.NdefTLVFoundFlag == 3290 PH_FRINFC_MIFARESTD_FLAG1) 3291 { 3292 NdefMap->TLVStruct.BytesRemainLinTLV = ShiftLength; 3293 if(NdefMap->TLVStruct.BytesRemainLinTLV > 3294 NdefMap->StdMifareContainer.remainingSize) 3295 { 3296 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 3297 NFCSTATUS_INVALID_FORMAT); 3298 } 3299 *CRFlag = PH_FRINFC_MIFARESTD_FLAG1; 3300 *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 3301 } 3302 else 3303 { 3304 NdefMap->StdMifareContainer.remainingSize -= 3305 ShiftLength; 3306 *CRFlag = PH_FRINFC_MIFARESTD_FLAG0; 3307 TempLength += PH_FRINFC_MIFARESTD_VAL2; 3308 TL4bytesFlag = PH_FRINFC_MIFARESTD_FLAG1; 3309 Result = ((NdefMap->TLVStruct.NdefTLVFoundFlag == PH_FRINFC_MIFARESTD_FLAG1)? 3310 NFCSTATUS_SUCCESS: 3311 phFriNfc_MifStd_H_GetNxtTLV(NdefMap, &TempLength, &TL4bytesFlag)); 3312 3313 *NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 3314 Result = phFriNfc_MifStd_H_GetActCardLen(NdefMap); 3315 } 3316 } 3317 break; 3318 3319 default: 3320 break; 3321 } 3322 3323 return Result; 3324 } 3325 3326 /****************************************************************************** 3327 * Function phFriNfc_MifStd_H_Get1kStTrail 3328 * 3329 * Description This function is used to get the Mifare 1k Sector Trailer. 3330 * 3331 * Returns void 3332 * 3333 ******************************************************************************/ 3334 static void phFriNfc_MifStd_H_Get1kStTrail(phFriNfc_NdefMap_t *NdefMap) 3335 { 3336 switch((NdefMap->StdMifareContainer.currentBlock % 4)) 3337 { 3338 case PH_FRINFC_MIFARESTD_VAL0: 3339 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3340 (NdefMap->StdMifareContainer.currentBlock + 3341 PH_FRINFC_MIFARESTD_MAD_BLK3); 3342 break; 3343 3344 case PH_FRINFC_MIFARESTD_VAL1: 3345 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3346 (NdefMap->StdMifareContainer.currentBlock + 3347 PH_FRINFC_MIFARESTD_MAD_BLK2); 3348 break; 3349 3350 case PH_FRINFC_MIFARESTD_VAL2: 3351 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3352 (NdefMap->StdMifareContainer.currentBlock + 3353 PH_FRINFC_MIFARESTD_MAD_BLK1); 3354 break; 3355 3356 default: 3357 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3358 NdefMap->StdMifareContainer.currentBlock; 3359 break; 3360 } 3361 3362 return; 3363 } 3364 3365 /****************************************************************************** 3366 * Function phFriNfc_MifStd_H_Get4kStTrail 3367 * 3368 * Description This function gets the Mifare 4k Sector Trailer. 3369 * 3370 * Returns void 3371 * 3372 ******************************************************************************/ 3373 static void phFriNfc_MifStd_H_Get4kStTrail(phFriNfc_NdefMap_t *NdefMap) 3374 { 3375 switch((NdefMap->StdMifareContainer.currentBlock % 16)) 3376 { 3377 case PH_FRINFC_MIFARESTD_MAD_BLK0: 3378 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3379 (NdefMap->StdMifareContainer.currentBlock + 3380 PH_FRINFC_MIFARESTD_BLK15); 3381 break; 3382 3383 case PH_FRINFC_MIFARESTD_MAD_BLK1: 3384 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3385 (NdefMap->StdMifareContainer.currentBlock + 3386 PH_FRINFC_MIFARESTD_BLK14); 3387 break; 3388 3389 case PH_FRINFC_MIFARESTD_MAD_BLK2: 3390 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3391 (NdefMap->StdMifareContainer.currentBlock + 3392 PH_FRINFC_MIFARESTD_BLK13); 3393 break; 3394 3395 case PH_FRINFC_MIFARESTD_MAD_BLK3: 3396 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3397 (NdefMap->StdMifareContainer.currentBlock + 3398 PH_FRINFC_MIFARESTD_BLK12); 3399 break; 3400 3401 case PH_FRINFC_MIFARESTD_BLK4: 3402 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3403 (NdefMap->StdMifareContainer.currentBlock + 3404 PH_FRINFC_MIFARESTD_BLK11); 3405 break; 3406 3407 case PH_FRINFC_MIFARESTD_BLK5: 3408 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3409 (NdefMap->StdMifareContainer.currentBlock + 3410 PH_FRINFC_MIFARESTD_BLK10); 3411 break; 3412 3413 case PH_FRINFC_MIFARESTD_BLK6: 3414 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3415 (NdefMap->StdMifareContainer.currentBlock + 3416 PH_FRINFC_MIFARESTD_BLK9); 3417 break; 3418 3419 case PH_FRINFC_MIFARESTD_BLK7: 3420 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3421 (NdefMap->StdMifareContainer.currentBlock + 3422 PH_FRINFC_MIFARESTD_BLK8); 3423 break; 3424 3425 case PH_FRINFC_MIFARESTD_BLK8: 3426 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3427 (NdefMap->StdMifareContainer.currentBlock + 3428 PH_FRINFC_MIFARESTD_BLK7); 3429 break; 3430 3431 case PH_FRINFC_MIFARESTD_BLK9: 3432 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3433 (NdefMap->StdMifareContainer.currentBlock + 3434 PH_FRINFC_MIFARESTD_BLK6); 3435 break; 3436 3437 case PH_FRINFC_MIFARESTD_BLK10: 3438 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3439 (NdefMap->StdMifareContainer.currentBlock + 3440 PH_FRINFC_MIFARESTD_BLK5); 3441 break; 3442 3443 case PH_FRINFC_MIFARESTD_BLK11: 3444 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3445 (NdefMap->StdMifareContainer.currentBlock + 3446 PH_FRINFC_MIFARESTD_BLK4); 3447 break; 3448 3449 case PH_FRINFC_MIFARESTD_BLK12: 3450 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3451 (NdefMap->StdMifareContainer.currentBlock + 3452 PH_FRINFC_MIFARESTD_MAD_BLK3); 3453 break; 3454 3455 case PH_FRINFC_MIFARESTD_BLK13: 3456 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3457 (NdefMap->StdMifareContainer.currentBlock + 3458 PH_FRINFC_MIFARESTD_MAD_BLK2); 3459 break; 3460 3461 case PH_FRINFC_MIFARESTD_BLK14: 3462 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3463 (NdefMap->StdMifareContainer.currentBlock + 3464 PH_FRINFC_MIFARESTD_MAD_BLK1); 3465 break; 3466 3467 default: 3468 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 3469 NdefMap->StdMifareContainer.currentBlock; 3470 break; 3471 } 3472 3473 return; 3474 } 3475 3476 /****************************************************************************** 3477 * Function phFriNfc_MifStd_H_ProChkNdef 3478 * 3479 * Description This function processes the check ndef call. 3480 * 3481 * Returns This function return NFCSTATUS_PENDING in case of success 3482 * In case of failure returns other failure value. 3483 * 3484 ******************************************************************************/ 3485 static NFCSTATUS phFriNfc_MifStd_H_ProChkNdef(phFriNfc_NdefMap_t *NdefMap) 3486 { 3487 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3488 3489 /* Copy remaining bytes into the AID array 3490 from Receive Buffer till array number 7 in aid */ 3491 if(NdefMap->StdMifareContainer.currentBlock == 3492 PH_FRINFC_MIFARESTD_VAL1) 3493 { 3494 /* Helper Function to Store AID Information */ 3495 phFriNfc_MifStd_H_fillAIDarray(NdefMap); 3496 3497 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_VAL2; 3498 /* read remaining AIDs from block number 2 */ 3499 Result = ((NdefMap->StdMifareContainer.aidCompleteFlag == 3500 PH_FRINFC_MIFARESTD_FLAG1)? 3501 Result: 3502 phFriNfc_MifareStdMap_ChkNdef( NdefMap)); 3503 } 3504 else if(((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) && 3505 (NdefMap->StdMifareContainer.currentBlock == 3506 PH_FRINFC_MIFARESTD_MAD_BLK2)) || ( 3507 (NdefMap->StdMifareContainer.currentBlock == 3508 PH_FRINFC_MIFARESTD_MAD_BLK66) && 3509 (NdefMap->CardType == 3510 PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || 3511 NdefMap->CardType == 3512 PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD))) 3513 { 3514 /* Helper Function to Store AID Information */ 3515 phFriNfc_MifStd_H_fillAIDarray(NdefMap); 3516 3517 NdefMap->StdMifareContainer.aidCompleteFlag = 3518 PH_FRINFC_MIFARESTD_FLAG1; 3519 } /* Mifare 1k and Mifare 4k end Check */ 3520 else if((NdefMap->StdMifareContainer.currentBlock > 3521 PH_FRINFC_MIFARESTD_VAL1) && 3522 (NdefMap->CardType == 3523 PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || 3524 NdefMap->CardType == 3525 PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)) 3526 { 3527 phFriNfc_MifStd_H_fillAIDarray(NdefMap); 3528 /* read remaining AIDs from block number 2 */ 3529 /* Mifare 4k Helper Function */ 3530 Result = ((NdefMap->StdMifareContainer.aidCompleteFlag == 3531 PH_FRINFC_MIFARESTD_FLAG1)? 3532 Result: 3533 phFriNfc_MifStd4k_H_CheckNdef(NdefMap)); 3534 } /* Card Type 4k Check */ 3535 else 3536 { 3537 /* Since we have decided temporarily not to go 3538 for any new error codes we are using 3539 NFCSTATUS_INVALID_PARAMETER even though it is not 3540 the relevant error code here TBD */ 3541 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3542 NFCSTATUS_INVALID_PARAMETER); 3543 } 3544 3545 if(NdefMap->StdMifareContainer.aidCompleteFlag == 3546 PH_FRINFC_MIFARESTD_FLAG1) 3547 { 3548 NdefMap->StdMifareContainer.ChkNdefCompleteFlag = 3549 PH_FRINFC_MIFARESTD_FLAG1; 3550 /* The check for NDEF compliant information is now over for 3551 the Mifare 1K card. 3552 Update(decrement) the NoOfNdefCompBlocks as much required, 3553 depending on the NDEF compliant information found */ 3554 /* Check the Sectors are Ndef Compliant */ 3555 phFriNfc_MifStd_H_ChkNdefCmpltSects(NdefMap); 3556 if((NdefMap->StdMifareContainer.NoOfNdefCompBlocks == 0) || 3557 (NdefMap->StdMifareContainer.NoOfNdefCompBlocks > 255)) 3558 { 3559 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3560 NFCSTATUS_NO_NDEF_SUPPORT); 3561 } 3562 else 3563 { 3564 NdefMap->StdMifareContainer.aidCompleteFlag = 3565 PH_FRINFC_MIFARESTD_FLAG0; 3566 NdefMap->StdMifareContainer.NFCforumSectFlag = 3567 PH_FRINFC_MIFARESTD_FLAG0; 3568 NdefMap->StdMifareContainer.currentBlock = PH_FRINFC_MIFARESTD_BLK4; 3569 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 3570 Result = ((Result != NFCSTATUS_SUCCESS)? 3571 Result:phFriNfc_MifStd_H_AuthSector(NdefMap)); 3572 } 3573 3574 } 3575 3576 return Result; 3577 } 3578 3579 /****************************************************************************** 3580 * Function phFriNfc_MifStd_H_ProAuth 3581 * 3582 * Description This function process the authentication of a sector. 3583 * 3584 * Returns This function return NFCSTATUS_SUCCESS in case of success 3585 * In case of failure returns other failure value. 3586 * 3587 ******************************************************************************/ 3588 static NFCSTATUS phFriNfc_MifStd_H_ProAuth(phFriNfc_NdefMap_t *NdefMap) 3589 { 3590 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3591 3592 if(NdefMap->TLVStruct.NdefTLVAuthFlag == 3593 PH_FRINFC_MIFARESTD_FLAG1) 3594 { 3595 NdefMap->TLVStruct.NdefTLVAuthFlag = 3596 PH_FRINFC_MIFARESTD_FLAG0; 3597 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG1; 3598 Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap); 3599 } 3600 else 3601 { 3602 NdefMap->StdMifareContainer.AuthDone = 1; 3603 NdefMap->StdMifareContainer.ReadAcsBitFlag = 1; 3604 Result = phFriNfc_MifStd_H_RdAcsBit(NdefMap); 3605 } 3606 3607 return Result; 3608 } 3609 3610 /****************************************************************************** 3611 * Function phFriNfc_MifStd_H_Rd16Bytes 3612 * 3613 * Description This function reads 16 bytes from a specifed block no. 3614 * 3615 * Returns This function return NFCSTATUS_PENDING in case of success 3616 * In case of failure returns other failure value. 3617 * 3618 ******************************************************************************/ 3619 static NFCSTATUS phFriNfc_MifStd_H_Rd16Bytes(phFriNfc_NdefMap_t *NdefMap, 3620 uint8_t BlockNo) 3621 { 3622 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3623 3624 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = BlockNo; 3625 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_READ; 3626 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 3627 NdefMap->Cmd.MfCmd = phHal_eMifareRead; 3628 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 3629 NdefMap->MapCompletionInfo.Context = NdefMap; 3630 3631 /* Call the Overlapped HAL Transceive function */ 3632 Result = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, 3633 NdefMap->Cmd, 3634 NdefMap->SendRecvBuf, 3635 NdefMap->SendLength, 3636 NdefMap->SendRecvLength); 3637 3638 return Result; 3639 } 3640 3641 /****************************************************************************** 3642 * Function phFriNfc_MifStd_H_ProAcsBits 3643 * 3644 * Description It processes access bits of the sector trailer. 3645 * 3646 * Returns This function return NFCSTATUS_SUCCESS in case of success 3647 * In case of failure returns other failure value. 3648 * 3649 ******************************************************************************/ 3650 static NFCSTATUS phFriNfc_MifStd_H_ProAcsBits(phFriNfc_NdefMap_t *NdefMap) 3651 { 3652 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3653 uint8_t CRFlag = PH_FRINFC_MIFARESTD_FLAG0; 3654 3655 if(*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) 3656 { 3657 if(NdefMap->StdMifareContainer.ReadAcsBitFlag == 3658 PH_FRINFC_MIFARESTD_FLAG1) 3659 { 3660 /* check for the correct access bits */ 3661 Result = phFriNfc_MifStd_H_ChkAcsBit(NdefMap); 3662 3663 if((NdefMap->StdMifareContainer.ChkNdefFlag == 3664 PH_FRINFC_MIFARESTD_FLAG1) && 3665 (Result == NFCSTATUS_SUCCESS)) 3666 { 3667 if(NdefMap->CardState == 3668 PH_NDEFMAP_CARD_STATE_INVALID) 3669 { 3670 NdefMap->StdMifareContainer.NoOfNdefCompBlocks = 3671 ((NdefMap->StdMifareContainer.currentBlock >= 3672 PH_FRINFC_MIFARESTD4K_BLK128)? 3673 (NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 3674 PH_FRINFC_MIFARESTD_BLK15): 3675 (NdefMap->StdMifareContainer.NoOfNdefCompBlocks - 3676 PH_FRINFC_MIFARESTD_MAD_BLK3)); 3677 3678 NdefMap->StdMifareContainer.ProprforumSectFlag = 3679 ((NdefMap->StdMifareContainer.NFCforumSectFlag == 3680 PH_FRINFC_MIFARESTD_FLAG1)? 3681 PH_FRINFC_MIFARESTD_PROP_2ND_CONFIG: 3682 PH_FRINFC_MIFARESTD_PROP_3RD_CONFIG); 3683 3684 Result = phFriNfc_MifStd_H_ProStatNotValid(NdefMap, Result); 3685 } 3686 else 3687 { 3688 NdefMap->StdMifareContainer.NFCforumSectFlag = 3689 (((NdefMap->StdMifareContainer.currentBlock == 64) && 3690 ((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD) || 3691 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD))) ? 3692 NdefMap->StdMifareContainer.NFCforumSectFlag: 3693 PH_FRINFC_MIFARESTD_FLAG1); 3694 } 3695 3696 if(NdefMap->StdMifareContainer.ProprforumSectFlag != 3697 PH_FRINFC_MIFARESTD_PROP_2ND_CONFIG) 3698 { 3699 NdefMap->StdMifareContainer.ReadAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG0; 3700 /* ((NdefMap->StdMifareContainer.ReadCompleteFlag == 3701 PH_FRINFC_MIFARESTD_FLAG1)? 3702 PH_FRINFC_MIFARESTD_FLAG0: 3703 PH_FRINFC_MIFARESTD_FLAG1);*/ 3704 3705 NdefMap->StdMifareContainer.ReadCompleteFlag = 3706 (uint8_t)((((((NdefMap->StdMifareContainer.currentBlock + 3707 PH_FRINFC_MIFARESTD_VAL4) >= 3708 PH_FRINFC_MIFARESTD1K_MAX_BLK) && 3709 (NdefMap->CardType == 3710 PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) && 3711 (NdefMap->StdMifareContainer.ReadCompleteFlag == 3712 PH_FRINFC_MIFARESTD_FLAG0)) || 3713 (NdefMap->StdMifareContainer.ReadCompleteFlag == 3714 PH_FRINFC_MIFARESTD_FLAG1))? 3715 PH_FRINFC_MIFARESTD_FLAG1: 3716 PH_FRINFC_MIFARESTD_FLAG0); 3717 3718 NdefMap->StdMifareContainer.ReadCompleteFlag = 3719 (uint8_t)((((((uint16_t)(NdefMap->StdMifareContainer.currentBlock + 3720 PH_FRINFC_MIFARESTD_VAL4) >= 3721 PH_FRINFC_MIFARESTD4K_MAX_BLK) && 3722 (NdefMap->CardType == 3723 PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)) && 3724 (NdefMap->StdMifareContainer.ReadCompleteFlag == 3725 PH_FRINFC_MIFARESTD_FLAG0)) || 3726 (NdefMap->StdMifareContainer.ReadCompleteFlag == 3727 PH_FRINFC_MIFARESTD_FLAG1))? 3728 PH_FRINFC_MIFARESTD_FLAG1: 3729 PH_FRINFC_MIFARESTD_FLAG0); 3730 3731 NdefMap->StdMifareContainer.ReadCompleteFlag = 3732 (uint8_t)((((((uint16_t)(NdefMap->StdMifareContainer.currentBlock + 3733 PH_FRINFC_MIFARESTD_VAL4) >= 3734 PH_FRINFC_MIFARESTD4K_MAX_BLK) && 3735 (NdefMap->CardType == 3736 PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)) && 3737 (NdefMap->StdMifareContainer.ReadCompleteFlag == 3738 PH_FRINFC_MIFARESTD_FLAG0)) || 3739 (NdefMap->StdMifareContainer.ReadCompleteFlag == 3740 PH_FRINFC_MIFARESTD_FLAG1))? 3741 PH_FRINFC_MIFARESTD_FLAG1: 3742 PH_FRINFC_MIFARESTD_FLAG0); 3743 3744 NdefMap->StdMifareContainer.currentBlock = 3745 ((NdefMap->StdMifareContainer.ReadCompleteFlag == 3746 PH_FRINFC_MIFARESTD_FLAG1)? 3747 PH_FRINFC_MIFARESTD_BLK4: 3748 NdefMap->StdMifareContainer.currentBlock); 3749 3750 Result = 3751 ((NdefMap->StdMifareContainer.ReadCompleteFlag == 3752 PH_FRINFC_MIFARESTD_FLAG1)? 3753 phFriNfc_MifStd_H_BlkChk(NdefMap): 3754 Result); 3755 } 3756 } 3757 3758 Result = ((Result != NFCSTATUS_SUCCESS)? 3759 Result: 3760 phFriNfc_MifStd_H_ChkRdWr(NdefMap)); 3761 } 3762 else 3763 { 3764 NdefMap->StdMifareContainer.ChkNdefFlag = 3765 PH_FRINFC_MIFARESTD_FLAG0; 3766 /* Here its required to read the entire card to know the */ 3767 /* Get exact ndef size of the card */ 3768 Result = phFriNfc_MifStd_H_ChkTLVs(NdefMap, &CRFlag); 3769 } 3770 } 3771 else 3772 { 3773 /* Since we have decided temporarily not to go 3774 for any new error codes we are using 3775 NFCSTATUS_INVALID_PARAMETER even though it is not 3776 the relevant error code here TBD */ 3777 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3778 NFCSTATUS_INVALID_PARAMETER); 3779 } 3780 3781 return Result; 3782 } 3783 3784 /****************************************************************************** 3785 * Function phFriNfc_MifStd_H_GPBChk 3786 * 3787 * Description This function is checks the GPB bytes. 3788 * 3789 * Returns This function return NFCSTATUS_SUCCESS in case of success 3790 * In case of failure returns other failure value. 3791 * 3792 ******************************************************************************/ 3793 static NFCSTATUS phFriNfc_MifStd_H_GPBChk(phFriNfc_NdefMap_t *NdefMap) 3794 { 3795 NFCSTATUS Result = NFCSTATUS_SUCCESS; 3796 3797 /* Spec version needs to be checked every time (Version check is not enabled) */ 3798 /* Result = phFriNfc_MapTool_ChkSpcVer(NdefMap, PH_FRINFC_MIFARESTD_VAL9); */ 3799 3800 /* Check rhe read and write access field 3801 in GPB is 00b 3802 bit 0 and 1 for write access check 3803 bit 2 and 3 for read access check */ 3804 if(Result == NFCSTATUS_SUCCESS) 3805 { 3806 if(((NdefMap->SendRecvBuf[ 3807 PH_FRINFC_MIFARESTD_VAL9] & 3808 PH_FRINFC_MIFARESTD_MASK_GPB_WR) == 3809 PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL) && 3810 ((NdefMap->SendRecvBuf[ 3811 PH_FRINFC_MIFARESTD_VAL9] & 3812 PH_FRINFC_MIFARESTD_MASK_GPB_RD) == 3813 PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL)) 3814 { 3815 NdefMap->CardState = (((NdefMap->StdMifareContainer.ChkNdefFlag == 3816 PH_FRINFC_MIFARESTD_FLAG1) || 3817 (NdefMap->StdMifareContainer.ReadNdefFlag == 3818 PH_FRINFC_MIFARESTD_FLAG1) || 3819 (NdefMap->StdMifareContainer.WrNdefFlag == 3820 PH_FRINFC_MIFARESTD_FLAG1))? 3821 PH_NDEFMAP_CARD_STATE_INITIALIZED: 3822 PH_NDEFMAP_CARD_STATE_READ_WRITE); 3823 } 3824 else if(((NdefMap->SendRecvBuf[ 3825 PH_FRINFC_MIFARESTD_VAL9] & 3826 PH_FRINFC_MIFARESTD_MASK_GPB_WR) != 3827 PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL) && 3828 ((NdefMap->SendRecvBuf[ 3829 PH_FRINFC_MIFARESTD_VAL9] & 3830 PH_FRINFC_MIFARESTD_MASK_GPB_RD) == 3831 PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL)) 3832 { 3833 /* write access not given 3834 only read access check */ 3835 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_ONLY; 3836 } 3837 else 3838 { 3839 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 3840 } 3841 } 3842 3843 return Result; 3844 } 3845 3846 /****************************************************************************** 3847 * Function phFriNfc_MifStd_H_ProStatNotValid 3848 * 3849 * Description This function checks for the different status value in the 3850 * process because of proprietary forum sector. 3851 * 3852 * Returns This function return NFCSTATUS_SUCCESS in case of success 3853 * In case of failure returns other failure value. 3854 * 3855 ******************************************************************************/ 3856 static NFCSTATUS phFriNfc_MifStd_H_ProStatNotValid(phFriNfc_NdefMap_t *NdefMap, 3857 NFCSTATUS status) 3858 { 3859 NFCSTATUS Result = status; 3860 3861 /* if NFC forum sector is not found before the proprietary one then 3862 authenticate the next sector 3863 Else it is a error*/ 3864 if(NdefMap->StdMifareContainer.NFCforumSectFlag == 3865 PH_FRINFC_MIFARESTD_FLAG0) 3866 { 3867 NdefMap->StdMifareContainer.ProprforumSectFlag = 3868 PH_FRINFC_MIFARESTD_PROP_3RD_CONFIG; 3869 if(NdefMap->StdMifareContainer.currentBlock < 3870 PH_FRINFC_MIFARESTD4K_BLK128) 3871 { 3872 /* Fix for the disovery problem, 3873 if 1st sector is invalid then ignore the remaining sectors and 3874 send an error if the card is mifare 1k, 3875 if the card is mifare 4k, then update the block number to 67 and 3876 continue. 3877 Even if the authentication of that block fails then send error */ 3878 if(((NdefMap->StdMifareContainer.currentBlock < 3879 PH_FRINFC_MIFARESTD_BLK4) && 3880 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) || 3881 ((NdefMap->StdMifareContainer.currentBlock <= 3882 PH_FRINFC_MIFARESTD_MAD_BLK67) && 3883 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || 3884 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD))) 3885 { 3886 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3887 NFCSTATUS_NO_NDEF_SUPPORT); 3888 } 3889 else if((NdefMap->StdMifareContainer.currentBlock < 3890 PH_FRINFC_MIFARESTD_BLK4) && 3891 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD || 3892 NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)) 3893 { 3894 Result = NFCSTATUS_SUCCESS; 3895 NdefMap->StdMifareContainer.currentBlock = 3896 PH_FRINFC_MIFARESTD_MAD_BLK67; 3897 } 3898 else if(((NdefMap->StdMifareContainer.currentBlock + 3899 PH_FRINFC_MIFARESTD_BLK4) > 3900 PH_FRINFC_MIFARESTD1K_MAX_BLK) && 3901 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) 3902 { 3903 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3904 NFCSTATUS_NO_NDEF_SUPPORT); 3905 } 3906 else 3907 { 3908 NdefMap->StdMifareContainer.remainingSize -= 3909 (PH_FRINFC_MIFARESTD_MAD_BLK3 * PH_FRINFC_MIFARESTD_BLOCK_BYTES); 3910 NdefMap->StdMifareContainer.currentBlock += 3911 PH_FRINFC_MIFARESTD_BLK4; 3912 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 3913 } 3914 } 3915 else if((NdefMap->StdMifareContainer.currentBlock + 3916 PH_FRINFC_MIFARESTD_BLK15) > 3917 PH_FRINFC_MIFARESTD4K_MAX_BLK) 3918 { 3919 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3920 NFCSTATUS_NO_NDEF_SUPPORT); 3921 } 3922 else 3923 { 3924 NdefMap->StdMifareContainer.remainingSize -= 3925 (PH_FRINFC_MIFARESTD_BLK15 * PH_FRINFC_MIFARESTD_BLOCK_BYTES); 3926 NdefMap->StdMifareContainer.currentBlock += 3927 PH_FRINFC_MIFARESTD_BLOCK_BYTES; 3928 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 3929 } 3930 Result = ((Result != NFCSTATUS_SUCCESS)? 3931 (PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3932 NFCSTATUS_NO_NDEF_SUPPORT)): 3933 phFriNfc_MifStd_H_AuthSector(NdefMap)); 3934 } 3935 else if((NdefMap->StdMifareContainer.ProprforumSectFlag == 3936 PH_FRINFC_MIFARESTD_PROP_3RD_CONFIG) && 3937 (NdefMap->StdMifareContainer.NFCforumSectFlag == 3938 PH_FRINFC_MIFARESTD_FLAG1)) 3939 { 3940 /* if the proprietary forum sector are found before 3941 NFC forum sector then again a proprietary 3942 forum sector are found after the NFC forum 3943 sector */ 3944 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3945 NFCSTATUS_NO_NDEF_SUPPORT); 3946 } 3947 else 3948 { 3949 NdefMap->StdMifareContainer.ProprforumSectFlag = 3950 PH_FRINFC_MIFARESTD_PROP_2ND_CONFIG; 3951 switch(NdefMap->PrevOperation) 3952 { 3953 case PH_FRINFC_NDEFMAP_CHECK_OPE: 3954 case PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE: 3955 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3956 NFCSTATUS_NO_NDEF_SUPPORT); 3957 break; 3958 3959 case PH_FRINFC_NDEFMAP_READ_OPE: 3960 if((NdefMap->TLVStruct.NdefTLVFoundFlag == 3961 PH_FRINFC_MIFARESTD_FLAG1) && 3962 (NdefMap->TLVStruct.NoLbytesinTLV == 3963 PH_FRINFC_MIFARESTD_VAL0)) 3964 { 3965 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 3966 Result = NFCSTATUS_SUCCESS; 3967 } 3968 else 3969 { 3970 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 3971 NFCSTATUS_NO_NDEF_SUPPORT); 3972 } 3973 break; 3974 3975 case PH_FRINFC_NDEFMAP_WRITE_OPE: 3976 default: 3977 /* This means the further write is not possible, 3978 EOF_NDEF_CONTAINER_REACHED */ 3979 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 3980 PH_FRINFC_MIFARESTD_FLAG1; 3981 /* Write the length to the L field in the TLV */ 3982 NdefMap->StdMifareContainer.TempBlockNo = 3983 NdefMap->StdMifareContainer.currentBlock; 3984 phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap); 3985 NdefMap->StdMifareContainer.currentBlock = 3986 NdefMap->TLVStruct.NdefTLVBlock; 3987 Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap); 3988 break; 3989 } 3990 } 3991 3992 return Result; 3993 } 3994 3995 /****************************************************************************** 3996 * Function phFriNfc_MifStd_H_RdBeforeWr 3997 * 3998 * Description This function is used to read the NDEF TLV block. 3999 * 4000 * Returns This function return NFCSTATUS_SUCCESS in case of success 4001 * In case of failure returns other failure value. 4002 * 4003 ******************************************************************************/ 4004 static NFCSTATUS phFriNfc_MifStd_H_RdBeforeWr(phFriNfc_NdefMap_t *NdefMap) 4005 { 4006 NFCSTATUS Result = NFCSTATUS_SUCCESS; 4007 4008 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_BEF_WR; 4009 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE; 4010 4011 Result = phFriNfc_MifStd_H_Rd16Bytes(NdefMap, 4012 NdefMap->StdMifareContainer.currentBlock); 4013 4014 return Result; 4015 } 4016 4017 /****************************************************************************** 4018 * Function phFriNfc_MifStd_H_ProBytesToWr 4019 * 4020 * Description This function processes the NDEF TLV block read bytes to 4021 * start write from the NDEF TLV. 4022 * 4023 * Returns This function return NFCSTATUS_SUCCESS in case of success 4024 * In case of failure returns other failure value. 4025 * 4026 ******************************************************************************/ 4027 static NFCSTATUS phFriNfc_MifStd_H_ProBytesToWr(phFriNfc_NdefMap_t *NdefMap) 4028 { 4029 NFCSTATUS Result = NFCSTATUS_SUCCESS; 4030 uint8_t TempLength = PH_FRINFC_MIFARESTD_VAL0; 4031 4032 if(*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) 4033 { 4034 memcpy(&NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1], 4035 NdefMap->SendRecvBuf, 4036 PH_FRINFC_MIFARESTD_BLOCK_BYTES); 4037 4038 /* Write to Ndef TLV Block */ 4039 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 4040 NdefMap->StdMifareContainer.currentBlock; 4041 4042 TempLength = ((NdefMap->StdMifareContainer.currentBlock == 4043 NdefMap->TLVStruct.NdefTLVBlock)? 4044 phFriNfc_MifStd_H_UpdateTLV(NdefMap): 4045 phFriNfc_MifStd_H_UpdRemTLV(NdefMap)); 4046 4047 NdefMap->StdMifareContainer.remainingSize -= 4048 ((NdefMap->StdMifareContainer.remSizeUpdFlag == 4049 PH_FRINFC_MIFARESTD_FLAG1)? 4050 PH_FRINFC_MIFARESTD_VAL2: 4051 PH_FRINFC_MIFARESTD_VAL0); 4052 4053 NdefMap->StdMifareContainer.remSizeUpdFlag = PH_FRINFC_MIFARESTD_FLAG0; 4054 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WR_TLV; 4055 Result = ((TempLength == PH_FRINFC_MIFARESTD_BLOCK_BYTES)? 4056 phFriNfc_MifStd_H_WrTLV(NdefMap): 4057 phFriNfc_MifStd_H_fillSendBuf(NdefMap, TempLength)); 4058 } 4059 else 4060 { 4061 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 4062 NFCSTATUS_READ_FAILED); 4063 } 4064 4065 return Result; 4066 } 4067 4068 /****************************************************************************** 4069 * Function phFriNfc_MifStd_H_UpdateTLV 4070 * 4071 * Description This function writes ndef to add the TLV structure. 4072 * 4073 * Returns uint8_t TempLength 4074 * 4075 ******************************************************************************/ 4076 static uint8_t phFriNfc_MifStd_H_UpdateTLV(phFriNfc_NdefMap_t *NdefMap) 4077 { 4078 uint8_t TempLength = PH_FRINFC_MIFARESTD_VAL0; 4079 4080 TempLength = (uint8_t)(NdefMap->TLVStruct.NdefTLVByte + PH_FRINFC_MIFARESTD_VAL1); 4081 /* Creating TLV */ 4082 if(NdefMap->TLVStruct.NULLTLVCount >= 2) 4083 { 4084 if((PH_FRINFC_MIFARESTD_BYTES_READ - TempLength) == 4085 PH_FRINFC_MIFARESTD_VAL0) 4086 { 4087 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4088 } 4089 else 4090 { 4091 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4092 TempLength++; 4093 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0; 4094 } 4095 } 4096 else 4097 { 4098 switch((PH_FRINFC_MIFARESTD_BYTES_READ - 4099 TempLength)) 4100 { 4101 case PH_FRINFC_MIFARESTD_VAL0: 4102 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4103 break; 4104 4105 case PH_FRINFC_MIFARESTD_VAL1: 4106 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4107 TempLength++; 4108 NdefMap->TLVStruct.prevLenByteValue = 4109 (uint16_t)((NdefMap->SendRecvBuf[TempLength] >= 4110 PH_FRINFC_MIFARESTD_NDEFTLV_L)? 4111 PH_FRINFC_MIFARESTD_VAL0: 4112 NdefMap->SendRecvBuf[TempLength]); 4113 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4114 break; 4115 4116 case PH_FRINFC_MIFARESTD_VAL2: 4117 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4118 TempLength++; 4119 NdefMap->TLVStruct.prevLenByteValue = 4120 (uint16_t)((NdefMap->SendRecvBuf[TempLength] >= 4121 PH_FRINFC_MIFARESTD_NDEFTLV_L)? 4122 NdefMap->SendRecvBuf[(TempLength + 4123 PH_FRINFC_MIFARESTD_VAL1)]: 4124 NdefMap->SendRecvBuf[TempLength]); 4125 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4126 TempLength++; 4127 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4128 break; 4129 4130 default: 4131 NdefMap->TLVStruct.prevLenByteValue = 4132 NdefMap->SendRecvBuf[TempLength]; 4133 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4134 TempLength++; 4135 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4136 TempLength++; 4137 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4138 TempLength++; 4139 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0; 4140 break; 4141 } 4142 } 4143 4144 return TempLength; 4145 } 4146 4147 /****************************************************************************** 4148 * Function phFriNfc_MifStd_H_fillSendBuf 4149 * 4150 * Description It fill the send buffer to write. 4151 * 4152 * Returns This function return NFCSTATUS_SUCCESS in case of success 4153 * In case of failure returns other failure value. 4154 * 4155 ******************************************************************************/ 4156 static NFCSTATUS phFriNfc_MifStd_H_fillSendBuf(phFriNfc_NdefMap_t *NdefMap, 4157 uint8_t Length) 4158 { 4159 NFCSTATUS Result = NFCSTATUS_SUCCESS; 4160 uint16_t RemainingBytes = PH_FRINFC_MIFARESTD_VAL0, 4161 BytesToWrite = PH_FRINFC_MIFARESTD_VAL0; 4162 uint8_t index = PH_FRINFC_MIFARESTD_VAL0; 4163 4164 Length = (Length + PH_FRINFC_MIFARESTD_VAL1); 4165 4166 RemainingBytes = (uint16_t)((NdefMap->StdMifareContainer.remainingSize 4167 < (uint16_t)(NdefMap->ApduBufferSize - 4168 NdefMap->ApduBuffIndex))? 4169 NdefMap->StdMifareContainer.remainingSize: 4170 (NdefMap->ApduBufferSize - 4171 NdefMap->ApduBuffIndex)); 4172 4173 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = 4174 NdefMap->StdMifareContainer.currentBlock; 4175 /* Get the number of bytes that can be written after copying 4176 the internal buffer */ 4177 BytesToWrite = ((RemainingBytes < 4178 ((PH_FRINFC_MIFARESTD_WR_A_BLK - Length) - 4179 NdefMap->StdMifareContainer.internalLength))? 4180 RemainingBytes: 4181 ((PH_FRINFC_MIFARESTD_WR_A_BLK - Length) - 4182 NdefMap->StdMifareContainer.internalLength)); 4183 4184 if(NdefMap->StdMifareContainer.internalLength > 4185 PH_FRINFC_MIFARESTD_VAL0) 4186 { 4187 /* copy the internal buffer to the send buffer */ 4188 memcpy(&(NdefMap->SendRecvBuf[ 4189 Length]), 4190 NdefMap->StdMifareContainer.internalBuf, 4191 NdefMap->StdMifareContainer.internalLength); 4192 } 4193 4194 /* Copy Bytes to write in the send buffer */ 4195 memcpy(&(NdefMap->SendRecvBuf[ 4196 (Length + 4197 NdefMap->StdMifareContainer.internalLength)]), 4198 &(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex]), 4199 BytesToWrite); 4200 4201 /* update number of bytes written from the user buffer */ 4202 NdefMap->NumOfBytesWritten = BytesToWrite; 4203 4204 /* check the exact number of bytes written to a block including the 4205 internal length */ 4206 *NdefMap->DataCount = 4207 ((BytesToWrite + NdefMap->StdMifareContainer.internalLength 4208 + Length) - PH_FRINFC_MIFARESTD_VAL1); 4209 4210 /* if total bytes to write in the card is less than 4 bytes then 4211 pad zeroes till 4 bytes */ 4212 if((BytesToWrite + NdefMap->StdMifareContainer.internalLength + 4213 Length) < PH_FRINFC_MIFARESTD_WR_A_BLK) 4214 { 4215 for(index = (uint8_t)(BytesToWrite + 4216 NdefMap->StdMifareContainer.internalLength + 4217 Length); 4218 index < PH_FRINFC_MIFARESTD_WR_A_BLK; 4219 index++) 4220 { 4221 NdefMap->SendRecvBuf[index] = (uint8_t)((index == 4222 (BytesToWrite + Length + 4223 NdefMap->StdMifareContainer.internalLength))? 4224 PH_FRINFC_MIFARESTD_TERMTLV_T: 4225 PH_FRINFC_MIFARESTD_NULLTLV_T); 4226 4227 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1; 4228 } 4229 } 4230 4231 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG1; 4232 4233 /* A temporary buffer to hold four bytes of data that is 4234 written to the card */ 4235 memcpy(NdefMap->StdMifareContainer.Buffer, 4236 &(NdefMap->SendRecvBuf[ 4237 PH_FRINFC_MIFARESTD_VAL1]), 4238 PH_FRINFC_MIFARESTD_BLOCK_BYTES); 4239 4240 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WR_TLV; 4241 Result = phFriNfc_MifStd_H_WrTLV(NdefMap); 4242 4243 return Result; 4244 } 4245 4246 /****************************************************************************** 4247 * Function phFriNfc_MifStd_H_WrTLV 4248 * 4249 * Description This function writes 16 bytes in a block. 4250 * 4251 * Returns This function return NFCSTATUS_SUCCESS in case of success 4252 * In case of failure returns other failure value. 4253 * 4254 ******************************************************************************/ 4255 static NFCSTATUS phFriNfc_MifStd_H_WrTLV(phFriNfc_NdefMap_t *NdefMap) 4256 { 4257 NFCSTATUS Result = NFCSTATUS_SUCCESS; 4258 4259 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 4260 NdefMap->MapCompletionInfo.Context = NdefMap; 4261 /* Write from here */ 4262 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE; 4263 4264 NdefMap->Cmd.MfCmd = phHal_eMifareWrite16; 4265 4266 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 4267 4268 /* Call the Overlapped HAL Transceive function */ 4269 Result = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, 4270 NdefMap->Cmd, 4271 NdefMap->SendRecvBuf, 4272 NdefMap->SendLength, 4273 NdefMap->SendRecvLength); 4274 4275 4276 return Result; 4277 } 4278 4279 /****************************************************************************** 4280 * Function phFriNfc_MifStd_H_ProWrTLV 4281 * 4282 * Description This function processes the write TLV bytes in a block. 4283 * 4284 * Returns This function return NFCSTATUS_SUCESS in case of success 4285 * In case of failure returns other failure value. 4286 * 4287 ******************************************************************************/ 4288 static NFCSTATUS phFriNfc_MifStd_H_ProWrTLV(phFriNfc_NdefMap_t *NdefMap) 4289 { 4290 NFCSTATUS Result = NFCSTATUS_SUCCESS; 4291 4292 /* Check that if complete TLV has been written in the 4293 card if yes enter the below check or go to else*/ 4294 if(((((PH_FRINFC_MIFARESTD_BLOCK_BYTES - 4295 NdefMap->TLVStruct.NdefTLVByte) == 4296 PH_FRINFC_MIFARESTD_VAL1) && 4297 (NdefMap->TLVStruct.NULLTLVCount >= 4298 PH_FRINFC_MIFARESTD_VAL2)) || 4299 (((PH_FRINFC_MIFARESTD_BLOCK_BYTES - 4300 NdefMap->TLVStruct.NdefTLVByte) <= 4301 PH_FRINFC_MIFARESTD_VAL3) && 4302 (NdefMap->TLVStruct.NULLTLVCount == 4303 PH_FRINFC_MIFARESTD_VAL0))) && 4304 (NdefMap->StdMifareContainer.currentBlock == 4305 NdefMap->TLVStruct.NdefTLVBlock)) 4306 { 4307 /* increment the block and chekc the block is in the same sector 4308 using the block check function */ 4309 NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG1; 4310 NdefMap->StdMifareContainer.currentBlock++; 4311 NdefMap->StdMifareContainer.NdefBlocks++; 4312 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 4313 if(Result == NFCSTATUS_SUCCESS) 4314 { 4315 Result = ((NdefMap->StdMifareContainer.AuthDone == 4316 PH_FRINFC_MIFARESTD_FLAG0)? 4317 phFriNfc_MifStd_H_AuthSector(NdefMap): 4318 phFriNfc_MifStd_H_RdBeforeWr(NdefMap)); 4319 } 4320 } 4321 else 4322 { 4323 NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG0; 4324 if(NdefMap->ApduBuffIndex < 4325 (uint16_t)NdefMap->ApduBufferSize) 4326 { 4327 if(*NdefMap->DataCount < PH_FRINFC_MIFARESTD_BLOCK_BYTES) 4328 { 4329 /* Write complete, so next byte shall be */ 4330 NdefMap->StdMifareContainer.internalLength = 4331 *NdefMap->DataCount; 4332 4333 /* Copy bytes less than 16 to internal buffer 4334 for the next write this can be used */ 4335 memcpy( NdefMap->StdMifareContainer.internalBuf, 4336 NdefMap->StdMifareContainer.Buffer, 4337 NdefMap->StdMifareContainer.internalLength); 4338 } 4339 4340 /* Increment the Send Buffer index */ 4341 NdefMap->ApduBuffIndex += NdefMap->NumOfBytesWritten; 4342 4343 NdefMap->StdMifareContainer.remainingSize -= 4344 NdefMap->NumOfBytesWritten; 4345 4346 /* Check for the End of Card */ 4347 if((NdefMap->StdMifareContainer.remainingSize == 4348 PH_FRINFC_MIFARESTD_VAL0) || 4349 (NdefMap->ApduBuffIndex == NdefMap->ApduBufferSize)) 4350 { 4351 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 4352 (uint8_t)((NdefMap->StdMifareContainer.remainingSize == 0)? 4353 PH_FRINFC_MIFARESTD_FLAG1:PH_FRINFC_MIFARESTD_FLAG0); 4354 4355 if(NdefMap->StdMifareContainer.internalLength == 4356 PH_FRINFC_MIFARESTD_VAL0) 4357 { 4358 NdefMap->StdMifareContainer.currentBlock++; 4359 /* Mifare 4k Card, After 128th Block 4360 each sector = 16 blocks in Mifare 4k */ 4361 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 4362 NdefMap->StdMifareContainer.NdefBlocks++; 4363 } 4364 4365 NdefMap->TLVStruct.SetTermTLVFlag = 4366 (uint8_t)(((NdefMap->StdMifareContainer.remainingSize == 4367 PH_FRINFC_MIFARESTD_VAL0) || 4368 (NdefMap->TLVStruct.SetTermTLVFlag == 4369 PH_FRINFC_MIFARESTD_FLAG1))? 4370 PH_FRINFC_MIFARESTD_FLAG1: 4371 PH_FRINFC_MIFARESTD_FLAG0); 4372 4373 } 4374 else 4375 { 4376 NdefMap->StdMifareContainer.currentBlock++; 4377 /* Mifare 4k Card, After 128th Block 4378 each sector = 16 blocks in Mifare 4k */ 4379 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 4380 if(Result == NFCSTATUS_SUCCESS) 4381 { 4382 NdefMap->StdMifareContainer.NdefBlocks++; 4383 Result = ((NdefMap->StdMifareContainer.AuthDone == 4384 PH_FRINFC_MIFARESTD_FLAG1)? 4385 phFriNfc_MifStd_H_WrABlock(NdefMap): 4386 phFriNfc_MifStd_H_AuthSector(NdefMap)); 4387 } 4388 } 4389 } 4390 } 4391 4392 if((Result == NFCSTATUS_SUCCESS) && 4393 (NdefMap->TLVStruct.SetTermTLVFlag != 4394 PH_FRINFC_MIFARESTD_FLAG1) && 4395 (NdefMap->StdMifareContainer.remainingSize > 4396 PH_FRINFC_MIFARESTD_VAL0)) 4397 { 4398 Result = phFriNfc_MifStd_H_WrTermTLV(NdefMap); 4399 } 4400 else 4401 { 4402 if((Result == NFCSTATUS_SUCCESS) && 4403 (NdefMap->TLVStruct.SetTermTLVFlag == 4404 PH_FRINFC_MIFARESTD_FLAG1)) 4405 { 4406 /* Write the length to the L field in the TLV */ 4407 NdefMap->StdMifareContainer.TempBlockNo = 4408 NdefMap->StdMifareContainer.currentBlock; 4409 phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap); 4410 NdefMap->StdMifareContainer.currentBlock = 4411 NdefMap->TLVStruct.NdefTLVBlock; 4412 Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap); 4413 } 4414 } 4415 4416 return Result; 4417 } 4418 4419 /****************************************************************************** 4420 * Function phFriNfc_MifStd_H_UpdRemTLV 4421 * 4422 * Description This function updates the remaining TLV. 4423 * 4424 * Returns uint8_t TempLength : length value 4425 * 4426 ******************************************************************************/ 4427 static uint8_t phFriNfc_MifStd_H_UpdRemTLV(phFriNfc_NdefMap_t *NdefMap) 4428 { 4429 uint8_t TempLength = PH_FRINFC_MIFARESTD_VAL1; 4430 4431 if(NdefMap->TLVStruct.NULLTLVCount >= 4432 PH_FRINFC_MIFARESTD_VAL2) 4433 { 4434 NdefMap->TLVStruct.prevLenByteValue = NdefMap->SendRecvBuf[TempLength]; 4435 NdefMap->SendRecvBuf[TempLength] = 4436 PH_FRINFC_MIFARESTD_NDEFTLV_L0; 4437 } 4438 else 4439 { 4440 switch((PH_FRINFC_MIFARESTD_BLOCK_BYTES - 4441 NdefMap->TLVStruct.NdefTLVByte)) 4442 { 4443 case PH_FRINFC_MIFARESTD_VAL1: 4444 NdefMap->TLVStruct.prevLenByteValue = 4445 (((NdefMap->SendRecvBuf[TempLength] == 4446 PH_FRINFC_MIFARESTD_NDEFTLV_L))? 4447 (((uint16_t)NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)] 4448 << PH_FRINFC_MIFARESTD_LEFTSHIFT8) + 4449 NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL2)]): 4450 NdefMap->SendRecvBuf[TempLength]); 4451 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4452 TempLength++; 4453 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4454 TempLength++; 4455 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0; 4456 break; 4457 4458 case PH_FRINFC_MIFARESTD_VAL2: 4459 NdefMap->TLVStruct.prevLenByteValue = 4460 (((NdefMap->SendRecvBuf[TempLength] == 4461 PH_FRINFC_MIFARESTD_NDEFTLV_L))? 4462 (((uint16_t)NdefMap->SendRecvBuf[TempLength] << 4463 PH_FRINFC_MIFARESTD_LEFTSHIFT8) + 4464 NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)]): 4465 NdefMap->SendRecvBuf[TempLength]); 4466 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4467 TempLength++; 4468 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0; 4469 break; 4470 4471 case PH_FRINFC_MIFARESTD_VAL3: 4472 default: 4473 NdefMap->TLVStruct.prevLenByteValue = 4474 ((NdefMap->TLVStruct.prevLenByteValue << 4475 PH_FRINFC_MIFARESTD_LEFTSHIFT8) 4476 + NdefMap->SendRecvBuf[TempLength]); 4477 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L0; 4478 break; 4479 } 4480 } 4481 4482 return TempLength; 4483 } 4484 4485 /****************************************************************************** 4486 * Function phFriNfc_MifStd_H_fillTLV1 4487 * 4488 * Description This function updates the length field if more than one 4489 * NULL TLVs exists before of the NDEF TLV. 4490 * 4491 * Returns void 4492 * 4493 ******************************************************************************/ 4494 static void phFriNfc_MifStd_H_fillTLV1(phFriNfc_NdefMap_t *NdefMap) 4495 { 4496 uint8_t TempLength = (uint8_t)(NdefMap->TLVStruct.NdefTLVByte + 4497 PH_FRINFC_MIFARESTD_VAL1); 4498 4499 NdefMap->TLVStruct.prevLenByteValue = 4500 ((NdefMap->Offset == PH_FRINFC_NDEFMAP_SEEK_CUR)? 4501 (NdefMap->TLVStruct.prevLenByteValue + 4502 NdefMap->ApduBuffIndex): 4503 NdefMap->ApduBuffIndex); 4504 4505 NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG1; 4506 switch(NdefMap->TLVStruct.NdefTLVByte) 4507 { 4508 case PH_FRINFC_MIFARESTD_VAL0: 4509 if(NdefMap->TLVStruct.prevLenByteValue >= 4510 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4511 { 4512 NdefMap->SendRecvBuf[TempLength] = 4513 (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4514 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4515 NdefMap->SendRecvBuf[(TempLength + 4516 PH_FRINFC_MIFARESTD_VAL1)] = 4517 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4518 } 4519 else 4520 { 4521 NdefMap->SendRecvBuf[TempLength] = 4522 PH_FRINFC_MIFARESTD_NDEFTLV_T; 4523 NdefMap->SendRecvBuf[(TempLength + 4524 PH_FRINFC_MIFARESTD_VAL1)] = 4525 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4526 4527 NdefMap->StdMifareContainer.RdAfterWrFlag = 4528 PH_FRINFC_MIFARESTD_FLAG0; 4529 } 4530 break; 4531 4532 case PH_FRINFC_MIFARESTD_VAL1: 4533 if(NdefMap->TLVStruct.prevLenByteValue >= 4534 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4535 { 4536 NdefMap->SendRecvBuf[TempLength - PH_FRINFC_MIFARESTD_VAL1] = 4537 PH_FRINFC_MIFARESTD_NDEFTLV_L; 4538 NdefMap->SendRecvBuf[TempLength] = 4539 (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4540 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4541 NdefMap->SendRecvBuf[(TempLength + 4542 PH_FRINFC_MIFARESTD_VAL1)] = 4543 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4544 } 4545 else 4546 { 4547 NdefMap->SendRecvBuf[TempLength] = 4548 PH_FRINFC_MIFARESTD_NDEFTLV_T; 4549 NdefMap->SendRecvBuf[(TempLength + 4550 PH_FRINFC_MIFARESTD_VAL1)] = 4551 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4552 NdefMap->StdMifareContainer.RdAfterWrFlag = 4553 PH_FRINFC_MIFARESTD_FLAG0; 4554 } 4555 break; 4556 4557 case PH_FRINFC_MIFARESTD_VAL15: 4558 /* if "Type" of TLV present at byte 15 */ 4559 if(NdefMap->TLVStruct.prevLenByteValue >= 4560 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4561 { 4562 /* Update the null TLV, ndef TLV block and ndef TLV byte */ 4563 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 4564 NdefMap->TLVStruct.NdefTLVBlock = 4565 NdefMap->StdMifareContainer.currentBlock; 4566 NdefMap->TLVStruct.NdefTLVByte = 4567 (TempLength - PH_FRINFC_MIFARESTD_VAL3); 4568 4569 NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL2)] = 4570 PH_FRINFC_MIFARESTD_NDEFTLV_T; 4571 NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL1)] = 4572 PH_FRINFC_MIFARESTD_NDEFTLV_L; 4573 NdefMap->SendRecvBuf[TempLength] = 4574 (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4575 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4576 } 4577 else 4578 { 4579 NdefMap->SendRecvBuf[TempLength] = 4580 PH_FRINFC_MIFARESTD_NDEFTLV_T; 4581 } 4582 break; 4583 4584 default: 4585 /* Already the TLV is present so just append the length field */ 4586 if(NdefMap->TLVStruct.prevLenByteValue >= 4587 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4588 { 4589 /* Update the null TLV, ndef TLV block and ndef TLV byte */ 4590 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 4591 NdefMap->TLVStruct.NdefTLVBlock = 4592 NdefMap->StdMifareContainer.currentBlock; 4593 NdefMap->TLVStruct.NdefTLVByte = 4594 (TempLength - PH_FRINFC_MIFARESTD_VAL3); 4595 4596 NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL2)] = 4597 (uint8_t)PH_FRINFC_MIFARESTD_NDEFTLV_T; 4598 NdefMap->SendRecvBuf[(TempLength - PH_FRINFC_MIFARESTD_VAL1)] = 4599 (uint8_t)PH_FRINFC_MIFARESTD_NDEFTLV_L; 4600 NdefMap->SendRecvBuf[TempLength] = 4601 (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4602 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4603 NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)] = 4604 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4605 } 4606 else 4607 { 4608 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4609 NdefMap->SendRecvBuf[(TempLength + PH_FRINFC_MIFARESTD_VAL1)] = 4610 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4611 } 4612 NdefMap->StdMifareContainer.RdAfterWrFlag = 4613 PH_FRINFC_MIFARESTD_FLAG0; 4614 break; 4615 } 4616 4617 return; 4618 } 4619 4620 /****************************************************************************** 4621 * Function phFriNfc_MifStd_H_fillTLV2 4622 * 4623 * Description This function is updates the length field if more than one 4624 * NULL TLVs does not exists before the TLV. 4625 * 4626 * Returns void 4627 * 4628 ******************************************************************************/ 4629 static void phFriNfc_MifStd_H_fillTLV2(phFriNfc_NdefMap_t *NdefMap) 4630 { 4631 uint8_t TempLength = (uint8_t)(NdefMap->TLVStruct.NdefTLVByte + 4632 PH_FRINFC_MIFARESTD_VAL1); 4633 4634 NdefMap->TLVStruct.prevLenByteValue = ((NdefMap->Offset == 4635 PH_FRINFC_NDEFMAP_SEEK_CUR)? 4636 (NdefMap->TLVStruct.prevLenByteValue + 4637 NdefMap->ApduBuffIndex): 4638 NdefMap->ApduBuffIndex); 4639 NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG1; 4640 switch(NdefMap->TLVStruct.NdefTLVByte) 4641 { 4642 case PH_FRINFC_MIFARESTD_VAL13: 4643 if(NdefMap->TLVStruct.prevLenByteValue >= 4644 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4645 { 4646 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4647 TempLength++; 4648 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L; 4649 TempLength++; 4650 NdefMap->SendRecvBuf[TempLength] = (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4651 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4652 } 4653 else 4654 { 4655 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4656 TempLength++; 4657 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4658 TempLength++; 4659 4660 /* Update the null TLV, ndef TLV block and ndef TLV byte */ 4661 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2; 4662 NdefMap->TLVStruct.NdefTLVBlock = 4663 NdefMap->StdMifareContainer.currentBlock; 4664 NdefMap->TLVStruct.NdefTLVByte = 4665 (TempLength - PH_FRINFC_MIFARESTD_VAL1); 4666 4667 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4668 } 4669 break; 4670 4671 case PH_FRINFC_MIFARESTD_VAL14: 4672 if(NdefMap->TLVStruct.prevLenByteValue >= 4673 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4674 { 4675 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4676 TempLength++; 4677 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L; 4678 } 4679 else 4680 { 4681 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4682 TempLength++; 4683 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4684 } 4685 break; 4686 4687 case PH_FRINFC_MIFARESTD_VAL15: 4688 if(NdefMap->TLVStruct.prevLenByteValue >= 4689 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4690 { 4691 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4692 } 4693 else 4694 { 4695 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4696 } 4697 break; 4698 4699 default: 4700 if(NdefMap->TLVStruct.prevLenByteValue >= 4701 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4702 { 4703 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4704 TempLength++; 4705 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_L; 4706 TempLength++; 4707 NdefMap->SendRecvBuf[TempLength] = (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4708 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4709 TempLength++; 4710 NdefMap->SendRecvBuf[TempLength] = 4711 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4712 } 4713 else 4714 { 4715 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4716 TempLength++; 4717 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NULLTLV_T; 4718 TempLength++; 4719 4720 /* Update the null TLV, ndef TLV block and ndef TLV byte */ 4721 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2; 4722 NdefMap->TLVStruct.NdefTLVBlock = 4723 NdefMap->StdMifareContainer.currentBlock; 4724 NdefMap->TLVStruct.NdefTLVByte = 4725 (TempLength - PH_FRINFC_MIFARESTD_VAL1); 4726 4727 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4728 TempLength++; 4729 NdefMap->SendRecvBuf[TempLength] = 4730 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4731 } 4732 NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0; 4733 break; 4734 } 4735 4736 return; 4737 } 4738 4739 /****************************************************************************** 4740 * Function phFriNfc_MifStd_H_CallWrNdefLen 4741 * 4742 * Description This function is used to increment/decrement the ndef tlv block 4743 * and read the block. 4744 * 4745 * Returns This function return NFCSTATUS_SUCCESS in case of success 4746 * In case of failure returns other failure value. 4747 * 4748 ******************************************************************************/ 4749 static NFCSTATUS phFriNfc_MifStd_H_CallWrNdefLen(phFriNfc_NdefMap_t *NdefMap) 4750 { 4751 NFCSTATUS Result = NFCSTATUS_SUCCESS; 4752 4753 if(NdefMap->TLVStruct.NULLTLVCount >= PH_FRINFC_MIFARESTD_VAL2) 4754 { 4755 if((NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL0) || 4756 (NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL1) ) 4757 { 4758 /* In this case, current block is decremented because the 4759 NULL TLVs are in the previous block */ 4760 NdefMap->StdMifareContainer.currentBlock--; 4761 Result = phFriNfc_MifStd_H_BlkChk_1(NdefMap); 4762 } 4763 else 4764 { 4765 /* case NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL15: 4766 Current block is incremented to update the remaining TLV 4767 structure */ 4768 NdefMap->StdMifareContainer.currentBlock++; 4769 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 4770 } 4771 } 4772 else 4773 { 4774 if((NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL13) || 4775 (NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL14) || 4776 (NdefMap->TLVStruct.NdefTLVByte == PH_FRINFC_MIFARESTD_VAL15)) 4777 { 4778 /* Current block is incremented to update the remaining TLV 4779 structure */ 4780 NdefMap->StdMifareContainer.currentBlock++; 4781 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 4782 } 4783 } 4784 4785 Result = ((Result == NFCSTATUS_SUCCESS)? 4786 phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap): 4787 Result); 4788 4789 return Result; 4790 } 4791 4792 /****************************************************************************** 4793 * Function phFriNfc_MifStd_H_BlkChk_1 4794 * 4795 * Description This function check the current block is valid or not 4796 * if not valid decrement the current block till the valid block. 4797 * 4798 * Returns This function return NFCSTATUS_SUCCESS in case of success 4799 * In case of failure returns other failure value. 4800 * 4801 ******************************************************************************/ 4802 static NFCSTATUS phFriNfc_MifStd_H_BlkChk_1(phFriNfc_NdefMap_t *NdefMap) 4803 { 4804 NFCSTATUS Result = NFCSTATUS_SUCCESS; 4805 uint8_t SectorID = PH_FRINFC_MIFARESTD_VAL0; 4806 4807 /* Get a Sector ID for the Current Block */ 4808 SectorID = phFriNfc_MifStd_H_GetSect(NdefMap->StdMifareContainer.currentBlock); 4809 4810 /* Check the sector id is valid or not and if valid then check the 4811 current block is greater than 128 */ 4812 if((NdefMap->StdMifareContainer.aid[SectorID] == 4813 PH_FRINFC_MIFARESTD_NDEF_COMP) && 4814 (((SectorID <= PH_FRINFC_MIFARESTD_VAL15) && 4815 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD)) || 4816 ((SectorID <= PH_FRINFC_MIFARESTD_SECTOR_NO31) && 4817 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD)) || 4818 ((SectorID <= PH_FRINFC_MIFARESTD_SECTOR_NO39) && 4819 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD)))) 4820 { 4821 if(NdefMap->StdMifareContainer.currentBlock > 128) 4822 { 4823 NdefMap->TLVStruct.NdefTLVAuthFlag = 4824 ((((NdefMap->StdMifareContainer.currentBlock + 4825 PH_FRINFC_MIFARESTD_VAL1) % 4826 PH_FRINFC_MIFARESTD_MAD_BLK16) == 4827 PH_FRINFC_MIFARESTD_VAL0)? 4828 PH_FRINFC_MIFARESTD_FLAG1: 4829 PH_FRINFC_MIFARESTD_FLAG0); 4830 4831 NdefMap->StdMifareContainer.currentBlock -= 4832 ((((NdefMap->StdMifareContainer.currentBlock + 4833 PH_FRINFC_MIFARESTD_VAL1) % 4834 PH_FRINFC_MIFARESTD_MAD_BLK16) == 4835 PH_FRINFC_MIFARESTD_VAL0)? 4836 PH_FRINFC_MIFARESTD_VAL1: 4837 PH_FRINFC_MIFARESTD_VAL0); 4838 4839 } 4840 else 4841 { 4842 NdefMap->TLVStruct.NdefTLVAuthFlag = 4843 ((((NdefMap->StdMifareContainer.currentBlock + 4844 PH_FRINFC_MIFARESTD_VAL1) % 4845 PH_FRINFC_MIFARESTD_BLK4) == 4846 PH_FRINFC_MIFARESTD_VAL0)? 4847 PH_FRINFC_MIFARESTD_FLAG1: 4848 PH_FRINFC_MIFARESTD_FLAG0); 4849 4850 NdefMap->StdMifareContainer.currentBlock -= 4851 ((((NdefMap->StdMifareContainer.currentBlock + 4852 PH_FRINFC_MIFARESTD_VAL1) % 4853 PH_FRINFC_MIFARESTD_BLK4) == 4854 PH_FRINFC_MIFARESTD_VAL1)? 4855 PH_FRINFC_MIFARESTD_VAL1: 4856 PH_FRINFC_MIFARESTD_VAL0); 4857 4858 } 4859 } 4860 else 4861 { 4862 /*Error: No Ndef Compliant Sectors present.*/ 4863 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 4864 NFCSTATUS_INVALID_PARAMETER); 4865 } 4866 4867 return Result; 4868 } 4869 4870 /****************************************************************************** 4871 * Function phFriNfc_MifStd_H_fillTLV1_1 4872 * 4873 * Description This function updates the length of the TLV if NULL TLVs 4874 * greater than or equal to 2. 4875 * 4876 * Returns void 4877 * 4878 ******************************************************************************/ 4879 static void phFriNfc_MifStd_H_fillTLV1_1(phFriNfc_NdefMap_t *NdefMap) 4880 { 4881 switch(NdefMap->TLVStruct.NdefTLVByte) 4882 { 4883 case PH_FRINFC_MIFARESTD_VAL0: 4884 /* In the first write ndef length procedure, the 4885 length is updated, in this case T and L = 0xFF of TLV are 4886 updated */ 4887 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 4888 NdefMap->TLVStruct.NdefTLVBlock = 4889 NdefMap->StdMifareContainer.currentBlock; 4890 NdefMap->TLVStruct.NdefTLVByte = PH_FRINFC_MIFARESTD_VAL14; 4891 4892 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL15] = 4893 PH_FRINFC_MIFARESTD_NDEFTLV_T; 4894 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL16] = 4895 PH_FRINFC_MIFARESTD_NDEFTLV_L; 4896 break; 4897 4898 case PH_FRINFC_MIFARESTD_VAL1: 4899 /* In the first write ndef length procedure, the 4900 length is updated, in this case T of TLV is 4901 updated */ 4902 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL0; 4903 NdefMap->TLVStruct.NdefTLVBlock = 4904 NdefMap->StdMifareContainer.currentBlock; 4905 NdefMap->TLVStruct.NdefTLVByte = 4906 PH_FRINFC_MIFARESTD_VAL15; 4907 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL16] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4908 break; 4909 4910 case PH_FRINFC_MIFARESTD_VAL15: 4911 default: 4912 /* In the first ndef write length, part of the L field or only T 4913 (if update length is less than 255) is updated */ 4914 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1] = 4915 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4916 break; 4917 } 4918 NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0; 4919 4920 return; 4921 } 4922 4923 /****************************************************************************** 4924 * Function phFriNfc_MifStd_H_fillTLV2_1 4925 * 4926 * Description This function updates the length of the TLV if NULL TLVs 4927 * less than 2. 4928 * 4929 * Returns void 4930 * 4931 ******************************************************************************/ 4932 static void phFriNfc_MifStd_H_fillTLV2_1(phFriNfc_NdefMap_t *NdefMap) 4933 { 4934 uint8_t TempLength = PH_FRINFC_MIFARESTD_VAL1; 4935 switch(NdefMap->TLVStruct.NdefTLVByte) 4936 { 4937 case PH_FRINFC_MIFARESTD_VAL13: 4938 /* In last write ndef length, part of length (L) field of TLV 4939 is updated now */ 4940 NdefMap->SendRecvBuf[TempLength] = 4941 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4942 break; 4943 4944 case PH_FRINFC_MIFARESTD_VAL14: 4945 /* In last write ndef length, part of length (L) field of TLV 4946 is updated now */ 4947 if(NdefMap->TLVStruct.prevLenByteValue >= 4948 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4949 { 4950 NdefMap->SendRecvBuf[TempLength] = 4951 (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4952 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4953 TempLength++; 4954 NdefMap->SendRecvBuf[TempLength] = 4955 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4956 } 4957 else 4958 { 4959 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2; 4960 NdefMap->TLVStruct.NdefTLVBlock = 4961 NdefMap->StdMifareContainer.currentBlock; 4962 NdefMap->TLVStruct.NdefTLVByte = 4963 (TempLength - PH_FRINFC_MIFARESTD_VAL1); 4964 NdefMap->SendRecvBuf[TempLength] = PH_FRINFC_MIFARESTD_NDEFTLV_T; 4965 TempLength++; 4966 NdefMap->SendRecvBuf[TempLength] = 4967 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4968 } 4969 break; 4970 4971 case PH_FRINFC_MIFARESTD_VAL15: 4972 default: 4973 if(NdefMap->TLVStruct.prevLenByteValue >= 4974 PH_FRINFC_MIFARESTD_NDEFTLV_L) 4975 { 4976 /* In last write ndef length, only T of TLV is updated and 4977 length (L) field of TLV is updated now */ 4978 NdefMap->SendRecvBuf[TempLength] = 4979 PH_FRINFC_MIFARESTD_NDEFTLV_L; 4980 TempLength++; 4981 NdefMap->SendRecvBuf[TempLength] = 4982 (uint8_t)(NdefMap->TLVStruct.prevLenByteValue >> 4983 PH_FRINFC_MIFARESTD_RIGHTSHIFT8); 4984 TempLength++; 4985 NdefMap->SendRecvBuf[TempLength] = 4986 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 4987 } 4988 else 4989 { 4990 NdefMap->SendRecvBuf[TempLength] = 4991 PH_FRINFC_MIFARESTD_NULLTLV_T; 4992 TempLength++; 4993 NdefMap->TLVStruct.NULLTLVCount = PH_FRINFC_MIFARESTD_VAL2; 4994 NdefMap->TLVStruct.NdefTLVBlock = 4995 NdefMap->StdMifareContainer.currentBlock; 4996 NdefMap->TLVStruct.NdefTLVByte = 4997 (TempLength - PH_FRINFC_MIFARESTD_VAL1); 4998 NdefMap->SendRecvBuf[TempLength] = 4999 PH_FRINFC_MIFARESTD_NDEFTLV_T; 5000 TempLength++; 5001 NdefMap->SendRecvBuf[TempLength] = 5002 (uint8_t)NdefMap->TLVStruct.prevLenByteValue; 5003 } 5004 break; 5005 } 5006 NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0; 5007 5008 return; 5009 } 5010 5011 /****************************************************************************** 5012 * Function phFriNfc_MifStd_H_RdTLV 5013 * 5014 * Description This function reads the TLV block. 5015 * 5016 * Returns This function return NFCSTATUS_SUCCESS in case of success 5017 * In case of failure returns other failure value. 5018 * 5019 ******************************************************************************/ 5020 static NFCSTATUS phFriNfc_MifStd_H_RdTLV(phFriNfc_NdefMap_t *NdefMap) 5021 { 5022 NFCSTATUS Result = NFCSTATUS_SUCCESS; 5023 5024 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RD_TLV; 5025 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE; 5026 5027 Result = phFriNfc_MifStd_H_Rd16Bytes(NdefMap, 5028 NdefMap->StdMifareContainer.currentBlock); 5029 5030 return Result; 5031 } 5032 5033 /****************************************************************************** 5034 * Function phFriNfc_MifStd_H_ProRdTLV 5035 * 5036 * Description This function processes the read TLV block. 5037 * 5038 * Returns This function return NFCSTATUS_SUCCESS in case of success 5039 * In case of failure returns other failure value. 5040 * 5041 ******************************************************************************/ 5042 static NFCSTATUS phFriNfc_MifStd_H_ProRdTLV(phFriNfc_NdefMap_t *NdefMap) 5043 { 5044 NFCSTATUS Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 5045 NFCSTATUS_INVALID_DEVICE_REQUEST); 5046 uint8_t TempLength = PH_FRINFC_MIFARESTD_VAL0, 5047 NDEFFlag = PH_FRINFC_MIFARESTD_FLAG1; 5048 5049 /*TempLength = (uint8_t)(((NdefMap->TLVStruct.NULLTLVCount >= 5050 PH_FRINFC_MIFARESTD_VAL2) && 5051 (NdefMap->TLVStruct.BytesRemainLinTLV > 0xFE))? 5052 ((NdefMap->TLVStruct.NdefTLVByte + 5053 PH_FRINFC_MIFARESTD_VAL2)% 5054 PH_FRINFC_MIFARESTD_VAL16): 5055 ((NdefMap->TLVStruct.NdefTLVByte + 5056 PH_FRINFC_MIFARESTD_VAL4)% 5057 PH_FRINFC_MIFARESTD_VAL16));*/ 5058 5059 TempLength = (uint8_t)((NdefMap->TLVStruct.BytesRemainLinTLV <= 0xFE)? 5060 ((NdefMap->TLVStruct.NdefTLVByte + 5061 PH_FRINFC_MIFARESTD_VAL2)% 5062 PH_FRINFC_MIFARESTD_VAL16): 5063 ((NdefMap->TLVStruct.NdefTLVByte + 5064 PH_FRINFC_MIFARESTD_VAL4)% 5065 PH_FRINFC_MIFARESTD_VAL16)); 5066 5067 if((*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) && 5068 (NdefMap->ApduBuffIndex < NdefMap->ApduBufferSize)) 5069 { 5070 if(NdefMap->TLVStruct.BytesRemainLinTLV != 0) 5071 { 5072 NDEFFlag = PH_FRINFC_MIFARESTD_FLAG0; 5073 /* To read the remaining length (L) in TLV */ 5074 Result = phFriNfc_MifStd_H_RemainTLV(NdefMap, &NDEFFlag, &TempLength); 5075 } 5076 } 5077 5078 return Result; 5079 } 5080 5081 /****************************************************************************** 5082 * Function phFriNfc_MifStd_H_WrTermTLV 5083 * 5084 * Description This function is used to write the terminator TLV. 5085 * 5086 * Returns This function return NFCSTATUS_SUCCESS in case of success 5087 * In case of failure returns other failure value. 5088 * 5089 ******************************************************************************/ 5090 static NFCSTATUS phFriNfc_MifStd_H_WrTermTLV(phFriNfc_NdefMap_t *NdefMap) 5091 { 5092 NFCSTATUS Result = NFCSTATUS_SUCCESS; 5093 uint8_t index = PH_FRINFC_MIFARESTD_VAL0; 5094 5095 /* Change the state to check ndef compliancy */ 5096 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_TERM_TLV; 5097 5098 NdefMap->SendRecvBuf[index] = 5099 NdefMap->StdMifareContainer.currentBlock; 5100 index++; 5101 NdefMap->SendRecvBuf[index] = PH_FRINFC_MIFARESTD_TERMTLV_T; 5102 index++; 5103 5104 while(index < PH_FRINFC_MIFARESTD_WR_A_BLK) 5105 { 5106 NdefMap->SendRecvBuf[index] = PH_FRINFC_MIFARESTD_NULLTLV_T; 5107 index++; 5108 } 5109 5110 NdefMap->TLVStruct.SetTermTLVFlag = PH_FRINFC_MIFARESTD_FLAG0; 5111 5112 Result = phFriNfc_MifStd_H_WrTLV(NdefMap); 5113 5114 return Result; 5115 } 5116 5117 5118 /****************************************************************************** 5119 * Function phFriNfc_MifStd_H_ProWrABlock 5120 * 5121 * Description This function processes the write a block. 5122 * 5123 * Returns This function return NFCSTATUS_SUCCESS in case of success 5124 * In case of failure returns other failure value. 5125 * 5126 ******************************************************************************/ 5127 static NFCSTATUS phFriNfc_MifStd_H_ProWrABlock(phFriNfc_NdefMap_t *NdefMap) 5128 { 5129 NFCSTATUS Result = NFCSTATUS_SUCCESS; 5130 5131 NdefMap->StdMifareContainer.WrLength = PH_FRINFC_MIFARESTD_VAL0; 5132 if(NdefMap->ApduBuffIndex < 5133 (uint16_t)NdefMap->ApduBufferSize) 5134 { 5135 /* Remaining bytes to write < 16 */ 5136 if(NdefMap->StdMifareContainer.RemainingBufFlag == 5137 PH_FRINFC_MIFARESTD_FLAG1) 5138 { 5139 /* Write complete, so next byte shall be */ 5140 NdefMap->StdMifareContainer.internalLength = 5141 *NdefMap->DataCount; 5142 5143 /* Copy bytes less than 16 to internal buffer 5144 for the next write this can be used */ 5145 memcpy( NdefMap->StdMifareContainer.internalBuf, 5146 NdefMap->StdMifareContainer.Buffer, 5147 NdefMap->StdMifareContainer.internalLength); 5148 5149 /* Increment the Send Buffer index */ 5150 NdefMap->ApduBuffIndex += NdefMap->NumOfBytesWritten; 5151 5152 NdefMap->StdMifareContainer.remainingSize -= 5153 NdefMap->NumOfBytesWritten; 5154 5155 NdefMap->StdMifareContainer.RemainingBufFlag = PH_FRINFC_MIFARESTD_VAL0; 5156 /* Check for the End of Card */ 5157 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 5158 (uint8_t)((NdefMap->StdMifareContainer.remainingSize == 5159 PH_FRINFC_MIFARESTD_VAL0)? 5160 PH_FRINFC_MIFARESTD_FLAG1: 5161 PH_FRINFC_MIFARESTD_FLAG0); 5162 5163 NdefMap->TLVStruct.SetTermTLVFlag = 5164 (uint8_t)(((NdefMap->StdMifareContainer.remainingSize == 5165 PH_FRINFC_MIFARESTD_VAL0) || 5166 (NdefMap->TLVStruct.SetTermTLVFlag == 5167 PH_FRINFC_MIFARESTD_FLAG1))? 5168 PH_FRINFC_MIFARESTD_FLAG1: 5169 PH_FRINFC_MIFARESTD_FLAG0); 5170 5171 } /* internal Buffer > Send Buffer */ 5172 else if(NdefMap->StdMifareContainer.internalBufFlag == 5173 PH_FRINFC_MIFARESTD_FLAG1) 5174 { 5175 memcpy(NdefMap->StdMifareContainer.internalBuf, 5176 NdefMap->StdMifareContainer.Buffer, 5177 *NdefMap->DataCount); 5178 5179 NdefMap->StdMifareContainer.internalLength = 5180 *NdefMap->DataCount; 5181 5182 /* Increment the Send Buffer index */ 5183 NdefMap->ApduBuffIndex += 5184 NdefMap->NumOfBytesWritten; 5185 5186 NdefMap->StdMifareContainer.remainingSize -= 5187 NdefMap->NumOfBytesWritten; 5188 5189 NdefMap->StdMifareContainer.internalBufFlag = 5190 PH_FRINFC_MIFARESTD_FLAG0; 5191 /* Check for the End of Card */ 5192 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 5193 (uint8_t)(((NdefMap->StdMifareContainer.remainingSize == 5194 PH_FRINFC_MIFARESTD_VAL0) && 5195 (NdefMap->StdMifareContainer.internalLength == 5196 PH_FRINFC_MIFARESTD_VAL0))? 5197 PH_FRINFC_MIFARESTD_FLAG1: 5198 PH_FRINFC_MIFARESTD_FLAG0); 5199 5200 NdefMap->TLVStruct.SetTermTLVFlag = 5201 (uint8_t)(((NdefMap->StdMifareContainer.remainingSize == 5202 PH_FRINFC_MIFARESTD_VAL0) || 5203 (NdefMap->TLVStruct.SetTermTLVFlag == 5204 PH_FRINFC_MIFARESTD_FLAG1))? 5205 PH_FRINFC_MIFARESTD_FLAG1: 5206 PH_FRINFC_MIFARESTD_FLAG0); 5207 } 5208 else 5209 { 5210 NdefMap->StdMifareContainer.internalLength = 0; 5211 /* Increment the Send Buffer index */ 5212 NdefMap->ApduBuffIndex += 5213 NdefMap->NumOfBytesWritten; 5214 NdefMap->StdMifareContainer.remainingSize -= 5215 NdefMap->NumOfBytesWritten; 5216 5217 /* Check for the End of Card */ 5218 if((NdefMap->StdMifareContainer.remainingSize == 5219 PH_FRINFC_MIFARESTD_VAL0) || 5220 (NdefMap->ApduBuffIndex == NdefMap->ApduBufferSize)) 5221 { 5222 NdefMap->StdMifareContainer.ReadWriteCompleteFlag = 5223 (uint8_t)((NdefMap->StdMifareContainer.remainingSize == 0)? 5224 PH_FRINFC_MIFARESTD_FLAG1:PH_FRINFC_MIFARESTD_FLAG0); 5225 5226 if(NdefMap->StdMifareContainer.internalLength == 5227 PH_FRINFC_MIFARESTD_VAL0) 5228 { 5229 NdefMap->StdMifareContainer.currentBlock++; 5230 /* Mifare 4k Card, After 128th Block 5231 each sector = 16 blocks in Mifare 4k */ 5232 Result = ((NdefMap->StdMifareContainer.remainingSize == 0)? 5233 Result: 5234 phFriNfc_MifStd_H_BlkChk(NdefMap)); 5235 NdefMap->StdMifareContainer.NdefBlocks++; 5236 } 5237 NdefMap->TLVStruct.SetTermTLVFlag = 5238 (uint8_t)(((NdefMap->StdMifareContainer.remainingSize == 5239 PH_FRINFC_MIFARESTD_VAL0) || 5240 (NdefMap->TLVStruct.SetTermTLVFlag == 5241 PH_FRINFC_MIFARESTD_FLAG1))? 5242 PH_FRINFC_MIFARESTD_FLAG1: 5243 PH_FRINFC_MIFARESTD_FLAG0); 5244 } 5245 else 5246 { 5247 NdefMap->StdMifareContainer.currentBlock++; 5248 NdefMap->StdMifareContainer.WrLength = 5249 (uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 5250 /* Mifare 4k Card, After 128th Block 5251 each sector = 16 blocks in Mifare 4k */ 5252 Result = phFriNfc_MifStd_H_BlkChk(NdefMap); 5253 if(Result == NFCSTATUS_SUCCESS) 5254 { 5255 NdefMap->StdMifareContainer.NdefBlocks++; 5256 Result = ((NdefMap->StdMifareContainer.AuthDone == 5257 PH_FRINFC_MIFARESTD_FLAG1)? 5258 phFriNfc_MifStd_H_WrABlock(NdefMap): 5259 phFriNfc_MifStd_H_AuthSector(NdefMap)); 5260 } 5261 } 5262 } 5263 } 5264 else 5265 { 5266 Result = PHNFCSTVAL( CID_FRI_NFC_NDEF_MAP, 5267 NFCSTATUS_INVALID_DEVICE_REQUEST); 5268 } 5269 5270 if((Result == NFCSTATUS_SUCCESS) && 5271 (NdefMap->TLVStruct.SetTermTLVFlag != 5272 PH_FRINFC_MIFARESTD_FLAG1) && 5273 (NdefMap->StdMifareContainer.remainingSize > 5274 PH_FRINFC_MIFARESTD_VAL0)) 5275 { 5276 Result = phFriNfc_MifStd_H_WrTermTLV(NdefMap); 5277 } 5278 else 5279 { 5280 if((Result == NFCSTATUS_SUCCESS) && 5281 (NdefMap->TLVStruct.SetTermTLVFlag == 5282 PH_FRINFC_MIFARESTD_FLAG1)) 5283 { 5284 /* Write the length to the L field in the TLV */ 5285 NdefMap->StdMifareContainer.TempBlockNo = 5286 NdefMap->StdMifareContainer.currentBlock; 5287 phFriNfc_MifStd_H_SetNdefBlkAuth(NdefMap); 5288 NdefMap->StdMifareContainer.currentBlock = 5289 NdefMap->TLVStruct.NdefTLVBlock; 5290 Result = phFriNfc_MifStd_H_RdtoWrNdefLen(NdefMap); 5291 } 5292 } 5293 5294 return Result; 5295 } 5296 5297 /****************************************************************************** 5298 * Function phFriNfc_MifStd_H_CallDisCon 5299 * 5300 * Description This function trigger disconnect after the authentication 5301 * has failed. 5302 * 5303 * Returns This function return NFCSTATUS_PENDING in case of success 5304 * In case of failure returns other failure value. 5305 * 5306 ******************************************************************************/ 5307 static NFCSTATUS phFriNfc_MifStd_H_CallDisCon(phFriNfc_NdefMap_t *NdefMap) 5308 { 5309 NFCSTATUS Result = NFCSTATUS_SUCCESS; 5310 5311 /* Set Ndef State */ 5312 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_DISCONNECT; 5313 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 5314 NdefMap->MapCompletionInfo.Context = NdefMap; 5315 5316 Result = phNxNciExtns_MifareStd_Reconnect(); 5317 5318 return Result; 5319 } 5320 5321 /****************************************************************************** 5322 * Function phFriNfc_MifStd_H_CallConnect 5323 * 5324 * Description This function sets card state to connect after the 5325 * authentication has failed. 5326 * 5327 * Returns NFCSTATUS_SUCCESS 5328 * 5329 ******************************************************************************/ 5330 static NFCSTATUS phFriNfc_MifStd_H_CallConnect(phFriNfc_NdefMap_t *NdefMap) 5331 { 5332 NFCSTATUS Result = NFCSTATUS_SUCCESS; 5333 5334 /* Set Ndef State */ 5335 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_CONNECT; 5336 5337 return Result; 5338 } 5339 5340 /****************************************************************************** 5341 * Function phFriNfc_MifStd1k_H_BlkChk 5342 * 5343 * Description This function used to update the current block. 5344 * 5345 * Returns void 5346 * 5347 ******************************************************************************/ 5348 static void phFriNfc_MifStd1k_H_BlkChk(phFriNfc_NdefMap_t *NdefMap, 5349 uint8_t SectorID, 5350 uint8_t *callbreak) 5351 { 5352 /* every last block of a sector needs to be skipped */ 5353 if(((NdefMap->StdMifareContainer.currentBlock + PH_FRINFC_MIFARESTD_INC_1) % 5354 PH_FRINFC_MIFARESTD_BLK4) == 0) 5355 { 5356 NdefMap->StdMifareContainer.currentBlock++; 5357 } 5358 else 5359 { 5360 if(NdefMap->StdMifareContainer.aid[SectorID] == 5361 PH_FRINFC_MIFARESTD_NDEF_COMP) 5362 { 5363 /* Check whether the block is first block of a (next)new sector and 5364 also check if it is first block then internal length is zero 5365 or not. Because once Authentication is done for the sector again 5366 we should not authenticate it again */ 5367 if((NdefMap->StdMifareContainer.currentBlock == 5368 (SectorID * PH_FRINFC_MIFARESTD_BLK4)) && 5369 (NdefMap->StdMifareContainer.internalLength == 0)) 5370 { 5371 NdefMap->StdMifareContainer.AuthDone = 0; 5372 } 5373 *callbreak = 1; 5374 } 5375 else 5376 { 5377 NdefMap->StdMifareContainer.currentBlock += PH_FRINFC_MIFARESTD_BLK4; 5378 } 5379 } 5380 5381 return; 5382 } 5383 5384 /****************************************************************************** 5385 * Function phFrinfc_MifareClassic_GetContainerSize 5386 * 5387 * Description This function calculate the card size. 5388 * 5389 * Returns This function return NFCSTATUS_SUCCESS in case of success 5390 * In case of failure returns other failure value. 5391 * 5392 ******************************************************************************/ 5393 NFCSTATUS phFrinfc_MifareClassic_GetContainerSize(const phFriNfc_NdefMap_t *NdefMap, 5394 uint32_t *maxSize, uint32_t *actualSize) 5395 { 5396 NFCSTATUS result = NFCSTATUS_SUCCESS; 5397 uint16_t valid_no_of_bytes = 0; 5398 uint8_t sect_aid_index = 0; 5399 /* Mifare std card */ 5400 5401 /* Max size is the number of NDEF compliant blocks in the card 5402 multiplied by 16 bytes */ 5403 5404 /* Skip all the non ndef sectors */ 5405 while ((sect_aid_index < PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK) && 5406 (PH_FRINFC_MIFARESTD_NON_NDEF_COMP == NdefMap->StdMifareContainer.aid[sect_aid_index])) 5407 { 5408 sect_aid_index++; 5409 } 5410 5411 /* Parse only the contiguous NDEF sectors for the max size calculation */ 5412 while ((sect_aid_index <PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK)&& 5413 (PH_FRINFC_MIFARESTD_NDEF_COMP ==NdefMap->StdMifareContainer.aid[sect_aid_index])) 5414 { 5415 if (((PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD == NdefMap->CardType)|| 5416 (PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD == NdefMap->CardType)) && (sect_aid_index >= 32)) 5417 { 5418 /* Mifare classic card of 4k size, sector >= 32 has 5419 16 blocks per sector and in that 15 blocks are valid data blocks 5420 16 is the block number in a sector 5421 15 is the number of valid data blocks in a sector 5422 */ 5423 valid_no_of_bytes += (uint16_t)(16 * 15); 5424 } 5425 else 5426 { 5427 valid_no_of_bytes += (uint16_t)(16 * 3); 5428 } 5429 5430 sect_aid_index++; 5431 if (16 == sect_aid_index) 5432 { 5433 /* Because sector index is 16, that is "MAD 2" block 5434 For calculating size MAD block shall be ignored 5435 */ 5436 sect_aid_index++; 5437 } 5438 } 5439 /* The below check is for the 3 byte length format of the NDEF TLV 5440 If the length field > 255, Max size will less by 4 5441 else Max size will less by 2 (Type and Length of the NDEF TLV 5442 has to be skipped to provide the maximum size in the card */ 5443 *maxSize = (valid_no_of_bytes > 0xFF) ? (valid_no_of_bytes - 4) : (valid_no_of_bytes - 2); 5444 5445 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV; 5446 5447 return result; 5448 } 5449 5450 /****************************************************************************** 5451 * Function phFriNfc_MifareStdMap_ConvertToReadOnly 5452 * 5453 * Description This function converts the Mifare card to read-only. 5454 * It check preconditions before converting to read only. 5455 * 5456 * Returns This function return NFCSTATUS_PENDING in case of success 5457 * In case of failure returns other failure value. 5458 * 5459 ******************************************************************************/ 5460 NFCSTATUS 5461 phFriNfc_MifareStdMap_ConvertToReadOnly ( 5462 phFriNfc_NdefMap_t *NdefMap, 5463 const uint8_t *ScrtKeyB) 5464 { 5465 NFCSTATUS result = NFCSTATUS_SUCCESS; 5466 uint8_t totalNoSectors = 0 , sectorTrailerBlockNo = 0; 5467 5468 if ( NdefMap == NULL) 5469 { 5470 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 5471 } 5472 else if ( PH_NDEFMAP_CARD_STATE_INVALID == NdefMap->CardState ) 5473 { 5474 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_STATE); 5475 } 5476 else 5477 { 5478 /* card state is PH_NDEFMAP_CARD_STATE_READ_WRITE now */ 5479 /* get AID array and parse */ 5480 if( PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD == NdefMap->CardType ) 5481 { 5482 totalNoSectors = PH_FRINFC_MIFARESTD1K_TOTAL_SECTOR; 5483 } 5484 else if ( PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD == NdefMap->CardType ) 5485 { 5486 totalNoSectors = PH_FRINFC_MIFARESTD2K_TOTAL_SECTOR; 5487 } 5488 else if ( PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD == NdefMap->CardType ) 5489 { 5490 totalNoSectors = PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR; 5491 } 5492 5493 /* Store Key B in the context */ 5494 if(ScrtKeyB ==NULL) 5495 { 5496 memset (NdefMap->StdMifareContainer.UserScrtKeyB, PH_FRINFC_MIFARESTD_DEFAULT_KEY, 5497 PH_FRINFC_MIFARESTD_KEY_LEN); 5498 } 5499 else 5500 { 5501 memcpy (NdefMap->StdMifareContainer.UserScrtKeyB, ScrtKeyB, PH_FRINFC_MIFARESTD_KEY_LEN); 5502 } 5503 5504 NdefMap->StdMifareContainer.TotalNoSectors = totalNoSectors; 5505 if(totalNoSectors == 0) 5506 { 5507 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER); 5508 } 5509 else 5510 { 5511 NdefMap->TLVStruct.NdefTLVFoundFlag = PH_FRINFC_MIFARESTD_FLAG0; 5512 NdefMap->StdMifareContainer.RdBeforeWrFlag = PH_FRINFC_MIFARESTD_FLAG0; 5513 NdefMap->StdMifareContainer.WrNdefFlag = PH_FRINFC_MIFARESTD_FLAG0; 5514 NdefMap->StdMifareContainer.internalLength = PH_FRINFC_MIFARESTD_VAL0; 5515 NdefMap->StdMifareContainer.RdAfterWrFlag = PH_FRINFC_MIFARESTD_FLAG0; 5516 NdefMap->StdMifareContainer.AuthDone = PH_FRINFC_MIFARESTD_FLAG0; 5517 NdefMap->StdMifareContainer.NFCforumSectFlag = PH_FRINFC_MIFARESTD_FLAG0; 5518 NdefMap->StdMifareContainer.WriteAcsBitFlag = PH_FRINFC_MIFARESTD_FLAG0; 5519 5520 /* Sector 0 is MAD sector .Start from Sector 1 */ 5521 for(NdefMap->StdMifareContainer.ReadOnlySectorIndex = PH_FRINFC_MIFARESTD_FLAG1; 5522 NdefMap->StdMifareContainer.ReadOnlySectorIndex < totalNoSectors; 5523 NdefMap->StdMifareContainer.ReadOnlySectorIndex++) 5524 { 5525 /* skip MAD sectors */ 5526 if( PH_FRINFC_MIFARESTD_SECTOR_NO16 == NdefMap->StdMifareContainer.ReadOnlySectorIndex ) 5527 { 5528 continue; 5529 } 5530 5531 /* if not NDEF compliant skip */ 5532 if( PH_FRINFC_MIFARESTD_NON_NDEF_COMP == 5533 NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.ReadOnlySectorIndex]) 5534 { 5535 continue; 5536 } 5537 5538 if (PH_FRINFC_MIFARESTD_NDEF_COMP == 5539 NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.ReadOnlySectorIndex]) 5540 { 5541 /*get the sector trailer block number */ 5542 sectorTrailerBlockNo = 5543 phFriNfc_MifStd_H_GetSectorTrailerBlkNo(NdefMap->StdMifareContainer.ReadOnlySectorIndex); 5544 NdefMap->StdMifareContainer.currentBlock = sectorTrailerBlockNo; 5545 NdefMap->StdMifareContainer.SectorTrailerBlockNo = sectorTrailerBlockNo; 5546 5547 /* Proceed to authenticate the sector with Key B 5548 and modify the sector trailor bits to make it read only*/ 5549 result = phFriNfc_MifStd_H_AuthSector(NdefMap); 5550 5551 if (result == NFCSTATUS_PENDING ) 5552 { 5553 break; 5554 } 5555 } 5556 } /* end for */ 5557 5558 /* There are no NDEF sectors in this card , return */ 5559 if(NdefMap->StdMifareContainer.ReadOnlySectorIndex == totalNoSectors && 5560 NFCSTATUS_PENDING!= result ) 5561 { 5562 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_NO_NDEF_SUPPORT); 5563 } 5564 } /* end else */ 5565 } 5566 5567 return result; 5568 } 5569 5570 /****************************************************************************** 5571 * Function phFriNfc_MifStd_H_GetSectorTrailerBlkNo 5572 * 5573 * Description This function returns the block number of the sector 5574 * trailor for the given sector trailer Id. 5575 * 5576 * Returns uint8_t sectorTrailerblockNumber : sector trailor 5577 * 5578 ******************************************************************************/ 5579 static uint8_t phFriNfc_MifStd_H_GetSectorTrailerBlkNo (uint8_t SectorID) 5580 { 5581 uint8_t sectorTrailerblockNumber = 0; 5582 5583 /* every last block of a sector needs to be skipped */ 5584 if (SectorID < PH_FRINFC_MIFARESTD_SECTOR_NO32) 5585 { 5586 sectorTrailerblockNumber = (SectorID * PH_FRINFC_MIFARESTD_BLK4 ) + 3; 5587 } 5588 else 5589 { 5590 sectorTrailerblockNumber = ((PH_FRINFC_MIFARESTD_SECTOR_NO32 * PH_FRINFC_MIFARESTD_BLK4) + 5591 ((SectorID - PH_FRINFC_MIFARESTD_SECTOR_NO32) * PH_FRINFC_MIFARESTD_SECTOR_BLOCKS)) + 15; 5592 } 5593 5594 return sectorTrailerblockNumber; 5595 } 5596 5597 /****************************************************************************** 5598 * Function phFriNfc_MifStd_H_ProSectorTrailorAcsBits 5599 * 5600 * Description This function is called during ConvertToReadonly process to 5601 * Authenticate NDEF compliant Sector. 5602 * 5603 * Returns This function return NFCSTATUS_SUCCESS in case of success 5604 * In case of failure returns other failure value. 5605 * 5606 ******************************************************************************/ 5607 static NFCSTATUS phFriNfc_MifStd_H_ProSectorTrailorAcsBits(phFriNfc_NdefMap_t *NdefMap) 5608 { 5609 NFCSTATUS Result = NFCSTATUS_SUCCESS; 5610 5611 if(*NdefMap->SendRecvLength == PH_FRINFC_MIFARESTD_BYTES_READ) 5612 { 5613 if(NdefMap->StdMifareContainer.ReadAcsBitFlag == 5614 PH_FRINFC_MIFARESTD_FLAG1) 5615 { 5616 /* check for the correct access bits */ 5617 Result = phFriNfc_MifStd_H_ChkAcsBit(NdefMap); 5618 if(Result == NFCSTATUS_SUCCESS) 5619 { 5620 5621 if(NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY) 5622 { 5623 /* Go to next sector */ 5624 Result = phFriNfc_MifStd_H_ProWrSectorTrailor(NdefMap); 5625 } 5626 else 5627 { 5628 /* tranceive to write the data into SendRecvBuff */ 5629 Result = phFriNfc_MifStd_H_WrSectorTrailorBlock(NdefMap); 5630 } 5631 } 5632 } 5633 } 5634 else 5635 { 5636 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 5637 NFCSTATUS_INVALID_PARAMETER); 5638 } 5639 5640 return Result; 5641 } 5642 5643 /****************************************************************************** 5644 * Function phFriNfc_MifStd_H_WrSectorTrailorBlock 5645 * 5646 * Description This function makes current NDEF compliant Sector ReadOnly 5647 * modify the sector trailor bits and write it to the card. 5648 * 5649 * Returns This function return NFCSTATUS_PENDING in case of success 5650 * In case of failure returns other failure value. 5651 * 5652 ******************************************************************************/ 5653 static NFCSTATUS phFriNfc_MifStd_H_WrSectorTrailorBlock(phFriNfc_NdefMap_t *NdefMap) 5654 { 5655 NFCSTATUS status = NFCSTATUS_PENDING; 5656 5657 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_MifareStdMap_Process; 5658 NdefMap->MapCompletionInfo.Context = NdefMap; 5659 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE; 5660 5661 /* next state (update sector index) */ 5662 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_WRITE_SEC; 5663 5664 /* Buffer Check */ 5665 if(NdefMap->SendRecvBuf != NULL) 5666 { 5667 NdefMap->SendRecvBuf[10] = 0x00; 5668 NdefMap->SendRecvBuf[10] = NdefMap->SendRecvBuf[9] | PH_FRINFC_MIFARESTD_MASK_GPB_WR; /* WR bits 11*/ 5669 5670 /*The NdefMap->SendRecvBuf already has the sector trailor. 5671 modify the bits to make Read Only */ 5672 NdefMap->SendRecvBuf[1] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */ 5673 NdefMap->SendRecvBuf[2] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */ 5674 NdefMap->SendRecvBuf[3] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */ 5675 NdefMap->SendRecvBuf[4] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */ 5676 NdefMap->SendRecvBuf[5] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT1; /* 0xD3 */ 5677 NdefMap->SendRecvBuf[6] = PH_FRINFC_NDEFMAP_MIFARESTD_AUTH_NDEFSECT2; /* 0xF7 */ 5678 5679 NdefMap->SendRecvBuf[7] = PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE6;/* 0x0F */ 5680 NdefMap->SendRecvBuf[8] = PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE7;/* 0x07 */ 5681 NdefMap->SendRecvBuf[9] = PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE8;/* 0x8F */ 5682 5683 NdefMap->SendRecvBuf[11] = NdefMap->StdMifareContainer.UserScrtKeyB[0]; 5684 NdefMap->SendRecvBuf[12] = NdefMap->StdMifareContainer.UserScrtKeyB[1]; 5685 NdefMap->SendRecvBuf[13] = NdefMap->StdMifareContainer.UserScrtKeyB[2]; 5686 NdefMap->SendRecvBuf[14] = NdefMap->StdMifareContainer.UserScrtKeyB[3]; 5687 NdefMap->SendRecvBuf[15] = NdefMap->StdMifareContainer.UserScrtKeyB[4]; 5688 NdefMap->SendRecvBuf[16] = NdefMap->StdMifareContainer.UserScrtKeyB[5]; 5689 5690 /* Write to Ndef Sector Block */ 5691 NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL0] = NdefMap->StdMifareContainer.currentBlock; 5692 5693 /* Copy Ndef Sector Block into buffer */ 5694 memcpy(NdefMap->StdMifareContainer.Buffer, 5695 &(NdefMap->SendRecvBuf[PH_FRINFC_MIFARESTD_VAL1]), 5696 PH_FRINFC_MIFARESTD_BLOCK_BYTES); 5697 5698 /* Write from here */ 5699 NdefMap->SendLength = MIFARE_MAX_SEND_BUF_TO_WRITE; 5700 NdefMap->Cmd.MfCmd = phHal_eMifareWrite16; 5701 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 5702 5703 /* Call the Overlapped HAL Transceive function */ 5704 status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, 5705 NdefMap->Cmd, 5706 NdefMap->SendRecvBuf, 5707 NdefMap->SendLength, 5708 NdefMap->SendRecvLength); 5709 } 5710 else 5711 { 5712 /* Error: The control should not ideally come here. 5713 Return Error.*/ 5714 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_FAILED); 5715 } 5716 5717 return status; 5718 } 5719 5720 /****************************************************************************** 5721 * Function phFriNfc_MifStd_H_ProWrSectorTrailor 5722 * 5723 * Description This function makes next NDEF compliant Sector ReadOnly. 5724 * 5725 * Returns This function return NFCSTATUS_SUCCESS in case of success 5726 * In case of failure returns other failure value. 5727 * 5728 ******************************************************************************/ 5729 static NFCSTATUS phFriNfc_MifStd_H_ProWrSectorTrailor(phFriNfc_NdefMap_t *NdefMap) 5730 { 5731 NFCSTATUS status = NFCSTATUS_FAILED; 5732 uint8_t sectorTrailerBlockNo = 0; 5733 5734 /*Increment Sector Index */ 5735 NdefMap->StdMifareContainer.ReadOnlySectorIndex++; 5736 5737 /* skip if MAD2 */ 5738 if(PH_FRINFC_MIFARESTD_SECTOR_NO16 == NdefMap->StdMifareContainer.ReadOnlySectorIndex ) 5739 { 5740 NdefMap->StdMifareContainer.ReadOnlySectorIndex++; 5741 } 5742 5743 /* if current sector index exceeds total sector index then 5744 all ndef sectors are made readonly then return success 5745 If a NON def sector is encountered return success*/ 5746 if (NdefMap->StdMifareContainer.ReadOnlySectorIndex >= NdefMap->StdMifareContainer.TotalNoSectors || 5747 PH_FRINFC_MIFARESTD_NON_NDEF_COMP == 5748 NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.ReadOnlySectorIndex]) 5749 { 5750 status = NFCSTATUS_SUCCESS; 5751 } 5752 else if(PH_FRINFC_MIFARESTD_NDEF_COMP == NdefMap->StdMifareContainer.aid[NdefMap->StdMifareContainer.ReadOnlySectorIndex]) 5753 { 5754 /* Convert next NDEF sector to read only */ 5755 sectorTrailerBlockNo = phFriNfc_MifStd_H_GetSectorTrailerBlkNo(NdefMap->StdMifareContainer.ReadOnlySectorIndex); 5756 NdefMap->StdMifareContainer.currentBlock = sectorTrailerBlockNo; 5757 NdefMap->StdMifareContainer.SectorTrailerBlockNo = sectorTrailerBlockNo; 5758 5759 status = phFriNfc_MifStd_H_AuthSector(NdefMap); 5760 } 5761 5762 return status; 5763 } 5764 5765 /****************************************************************************** 5766 * Function phFriNfc_MifStd_H_ProWrSectorTrailor 5767 * 5768 * Description This function checks mapping spec version. 5769 * 5770 * Returns This function return NFCSTATUS_SUCCESS in case of success 5771 * In case of failure returns other failure value. 5772 * 5773 ******************************************************************************/ 5774 static NFCSTATUS phFriNfc_MapTool_ChkSpcVer( const phFriNfc_NdefMap_t *NdefMap, 5775 uint8_t VersionIndex) 5776 { 5777 NFCSTATUS status = NFCSTATUS_SUCCESS; 5778 5779 uint8_t TagVerNo = NdefMap->SendRecvBuf[VersionIndex]; 5780 5781 if ( TagVerNo == 0 ) 5782 { 5783 /* Return Status Error invalid format */ 5784 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT); 5785 } 5786 else 5787 { 5788 switch (NdefMap->CardType) 5789 { 5790 case PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD: 5791 case PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD: 5792 case PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD: 5793 { 5794 /* calculate the major and minor version number of Mifare std version number */ 5795 status = (( (( PH_NFCFRI_MFSTDMAP_NFCDEV_MAJOR_VER_NUM == 5796 PH_NFCFRI_MFSTDMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&& 5797 ( PH_NFCFRI_MFSTDMAP_NFCDEV_MINOR_VER_NUM == 5798 PH_NFCFRI_MFSTDMAP_GET_MINOR_TAG_VERNO(TagVerNo))) || 5799 (( PH_NFCFRI_MFSTDMAP_NFCDEV_MAJOR_VER_NUM == 5800 PH_NFCFRI_MFSTDMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&& 5801 ( PH_NFCFRI_MFSTDMAP_NFCDEV_MINOR_VER_NUM < 5802 PH_NFCFRI_MFSTDMAP_GET_MINOR_TAG_VERNO(TagVerNo) )))? 5803 NFCSTATUS_SUCCESS: 5804 PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 5805 NFCSTATUS_INVALID_FORMAT)); 5806 break; 5807 } 5808 5809 default: 5810 { 5811 /* calculate the major and minor version number of T3VerNo */ 5812 if( (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM == 5813 PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&& 5814 ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM == 5815 PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo))) || 5816 (( PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM == 5817 PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo ) )&& 5818 ( PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM < 5819 PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(TagVerNo) ))) 5820 { 5821 status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS); 5822 } 5823 else 5824 { 5825 if ((PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM < 5826 PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo)) || 5827 (PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM > 5828 PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(TagVerNo))) 5829 { 5830 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_FORMAT); 5831 } 5832 } 5833 break; 5834 } 5835 } 5836 } 5837 5838 return (status); 5839 } 5840 5841