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