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