summaryrefslogtreecommitdiffstats
path: root/src/phFriNfc_MifStdFormat.c
blob: e53c0861e207b3a2583169aa6e096a69dad526e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
/*
 * Copyright (C) 2010 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * \file  phFriNfc_MifStdFormat.c
 * \brief NFC Ndef Formatting For Mifare standard card.
 *
 * Project: NFC-FRI
 *
 * $Date: Tue Oct 20 20:13:03 2009 $
 * $Author: ing02260 $
 * $Revision: 1.9 $
 * $Aliases: NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $
 *
 */

#include <phFriNfc_MifStdFormat.h>
#include <phFriNfc_OvrHal.h>

/*! \ingroup grp_file_attributes
 *  \name NDEF Mapping
 *
 * File: \ref phFriNfc_MifStdFormat.c
 *
 */
/*@{*/
#define PHFRINFCMIFSTDFMT_FILEREVISION "$Revision: 1.9 $"
#define PHFRINFCMIFSTDFMT_FILEALIASES  "$Aliases: NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $"
/*@}*/

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function fills the
 * send buffer for transceive function
 */
static void phFriNfc_MfStd_H_FillSendBuf(phFriNfc_sNdefSmtCrdFmt_t      *NdefSmtCrdFmt,
                                        uint16_t                         BlockNo);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function authenticates
 *  a block or a sector from the card.
 */
static NFCSTATUS phFriNfc_MfStd_H_Transceive(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function calls
 *  disconnect.
 */
static NFCSTATUS phFriNfc_MfStd_H_CallDisCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt,
                                             NFCSTATUS                    Status);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function calls
 *  disconnect.
 */
static NFCSTATUS phFriNfc_MfStd_H_CallCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

#ifndef PH_HAL4_ENABLE
/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function calls
 *  disconnect.
 */
static NFCSTATUS phFriNfc_MfStd_H_CallPoll(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
#endif /* #ifndef PH_HAL4_ENABLE */

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
 * poll call.
 */
static NFCSTATUS phFriNfc_MfStd_H_ProCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
 * authenticate call.
 */
static NFCSTATUS phFriNfc_MfStd_H_ProAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
 * read access bit call.
 */
static NFCSTATUS phFriNfc_MfStd_H_ProRdSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
 * write access bit call.
 */
static NFCSTATUS phFriNfc_MfStd_H_ProWrSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function writes the
 * sector trailer using the block number.
 */
static NFCSTATUS phFriNfc_MfStd_H_WrRdAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function checks the
 * access bits of each sector trailer.
 */
static uint32_t phFriNfc_MfStd_H_ChkAcsBit(uint16_t                 BlockNo,
                                           const uint8_t                    *RecvBuf,
                                           const uint8_t            AcsBits1[],
                                           const uint8_t            AcsBits2[]);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function change the
 * authentication state and change the block number if required
 */
static void phFriNfc_MfStd_H_ChangeAuthSt(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function finds the
 * contiguous ndef compliant blocks.
 */
static void phFriNfc_MfStd_H_NdefComplSect(uint8_t      CardTypes,
                                           uint8_t      Sector[]);

/*!
 * \brief \copydoc page_ovr Helper function for Mifare standard. This function writes the
 * MAD block values.
 */
static NFCSTATUS phFriNfc_MfStd_H_ProWrMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
* the error status of the authentication
*/
static NFCSTATUS phFriNfc_MfStd_H_ProErrAuth(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);

/*!
* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
* the error status of the writing sector trailer
*/
static NFCSTATUS phFriNfc_MfStd_H_ErrWrSectTr(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);

/*!
* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
* the error status of the reading sector trailer
*/
static NFCSTATUS phFriNfc_MfStd_H_ErrRdSectTr(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);

/*!
* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
* the error status of the writing sector trailer
*/
static NFCSTATUS phFriNfc_MfStd_H_ProUpdMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall store the
* ndef compliant in the MAD array which will be later used for updating the MAD sector
*/
static void phFriNfc_MfStd_H_StrNdefData(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

/*!
* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall find the ndef compliant
* and calculate the block number to write the NDEF TLV
*/
static void phFriNfc_MfStd_H_BlkNoToWrTLV(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);

static int phFriNfc_MfStd_MemCompare ( void *s1, void *s2, unsigned int n );


void phFriNfc_MfStd_Reset(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    uint8_t NfcForSectArray[] = PH_FRINFC_SMTCRDFMT_NFCFORUMSECT_KEYA_ACS_BIT,
            MADSectArray[] = PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_1K;

    /* Authentication state */
    NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_VAL_1;

    /* Set default key for A or B */
    (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.Default_KeyA_OR_B,
                PH_FRINFC_MFSTD_FMT_DEFAULT_KEY, /* 0xFF */
                PH_FRINFC_MFSTD_FMT_VAL_6);

    /* MAD sector key A */
    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSect_KeyA,
                MADSectArray, //PH_FRINFC_MFSTD_FMT_VAL_0,
                PH_FRINFC_MFSTD_FMT_VAL_6);

    /* Copy access bits for MAD sectors */
    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSect_AccessBits,
                &MADSectArray[PH_FRINFC_MFSTD_FMT_VAL_6],
                PH_FRINFC_MFSTD_FMT_VAL_3);

    /* NFC forum sector key A */
    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.NFCForumSect_KeyA,
                NfcForSectArray, //PH_FRINFC_MFSTD_FMT_VAL_0,
                PH_FRINFC_MFSTD_FMT_VAL_6);

    /* Copy access bits for NFC forum sectors */
    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.NFCForumSect_AccessBits,
                &NfcForSectArray[PH_FRINFC_MFSTD_FMT_VAL_6],
                PH_FRINFC_MFSTD_FMT_VAL_3);

    /* Sector compliant array initialised to 0 */
    (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl,
                PH_FRINFC_MFSTD_FMT_VAL_0, /* 0x00 */
                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);

    NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag = (uint8_t)PH_FRINFC_MFSTD_FMT_VAL_0;
    NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;

}

NFCSTATUS phFriNfc_MfStd_Format( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt, const uint8_t *ScrtKeyB )
{
    NFCSTATUS               Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                                NFCSTATUS_INVALID_PARAMETER);
    uint8_t                 index = PH_FRINFC_MFSTD_FMT_VAL_0;

    if(ScrtKeyB != NULL)
    {
        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
            PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
        /* Store Key B in the context */
        while(index < PH_FRINFC_MFSTD_FMT_VAL_6)
        {
            NdefSmtCrdFmt->AddInfo.MfStdInfo.ScrtKeyB[index] = ScrtKeyB[index];
            index++;
        }
        /* Set the state */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
        /* Initialise current block to the first sector trailer */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = PH_FRINFC_MFSTD_FMT_VAL_3;
        /* Set the authenticate state */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
        /* Start authentication */
        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    }
    return Result;
}

void phFriNfc_MfStd_Process(void        *Context,
                            NFCSTATUS   Status)
{
    phFriNfc_sNdefSmtCrdFmt_t  *NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)Context;
    /* Copy the formatting status */
    NdefSmtCrdFmt->FmtProcStatus = Status;
    if(Status == NFCSTATUS_SUCCESS)
    {
        switch(NdefSmtCrdFmt->State)
        {
        case PH_FRINFC_MFSTD_FMT_AUTH_SECT:
            Status = phFriNfc_MfStd_H_ProAuth(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_DIS_CON:
#ifndef PH_HAL4_ENABLE
            Status = phFriNfc_MfStd_H_CallPoll(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_POLL:
#endif /* #ifndef PH_HAL4_ENABLE */
            Status = phFriNfc_MfStd_H_CallCon(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_CON:
            Status = phFriNfc_MfStd_H_ProCon(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_RD_SECT_TR:
            Status = phFriNfc_MfStd_H_ProRdSectTr(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_WR_SECT_TR:
            Status = phFriNfc_MfStd_H_ProWrSectTr(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_WR_MAD_BLK:
            Status = phFriNfc_MfStd_H_ProWrMADBlk(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_WR_TLV:
            break;

        case PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK:
            Status = phFriNfc_MfStd_H_ProUpdMADBlk(NdefSmtCrdFmt);
            break;

        default:
            Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
                                NFCSTATUS_INVALID_DEVICE_REQUEST);
            break;
        }
    }
    else
    {
        switch(NdefSmtCrdFmt->State)
        {
        case PH_FRINFC_MFSTD_FMT_AUTH_SECT:
            Status = phFriNfc_MfStd_H_ProErrAuth(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_WR_SECT_TR:
            Status = phFriNfc_MfStd_H_ErrWrSectTr(NdefSmtCrdFmt);
            break;

        case PH_FRINFC_MFSTD_FMT_RD_SECT_TR:
            Status = phFriNfc_MfStd_H_ErrRdSectTr(NdefSmtCrdFmt);
            break;

        default:
            Status = NdefSmtCrdFmt->FmtProcStatus;
            break;
        }
    }

    /* Status is not success then call completion routine */
    if(Status != NFCSTATUS_PENDING)
    {
        phFriNfc_SmtCrdFmt_HCrHandler(NdefSmtCrdFmt, Status);
    }
}

static void phFriNfc_MfStd_H_FillSendBuf(phFriNfc_sNdefSmtCrdFmt_t      *NdefSmtCrdFmt,
                                        uint16_t                         BlockNo)
{
    void        *mem = NULL;
    uint8_t     MADSectTr1k[] = PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_1K, /* MAD key A,
                                                                            Access bits and GPB of MAD sector */
                MADSectTr4k[] = PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_4K, /* MAD key A,
                                                                                    Access bits and GPB of MAD sector */
                NFCSectTr[] = PH_FRINFC_SMTCRDFMT_NFCFORUMSECT_KEYA_ACS_BIT, /* NFC forum key A,
                                                                             Access bits and GPB of NFC sector */
                NDEFMsgTLV[16] = {0x03, 0x00, 0xFE, 0x00, 0x00, 0x00, /* NDEF message TLV (INITIALISED state) */
                                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                  0x00, 0x00, 0x00, 0x00},
                MADBlk[16] = {0x0F, 0x00, 0x03, 0xE1, 0x03, 0xE1,
                              0x03, 0xE1, 0x03, 0xE1,
                              0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
    /* Block number in send buffer */
    NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_0] = (uint8_t)BlockNo;
    /* Initialise send receive length */
    *NdefSmtCrdFmt->SendRecvLength = PH_FRINFC_MFSTD_FMT_MAX_RECV_LENGTH;

    /* Depending on the different state, fill the send buffer */
    switch(NdefSmtCrdFmt->State)
    {
        case PH_FRINFC_MFSTD_FMT_AUTH_SECT:
            /* Depending on the authentication state, fill the send buffer */
            switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState)
            {
                case PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY:
                case PH_FRINFC_MFSTD_FMT_AUTH_KEYB:
                    /* Fill send buffer with the default key */
                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_DEF(mem);
                break;

                case PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY:
                    /* Fill send buffer with NFC forum sector key */
                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_NFCSECT_KEYA(mem);
                break;

                case PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB:
                    /* Fill send buffer with NFC forum sector key */
                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_SCRT_KEY(mem);
                    break;

                case PH_FRINFC_MFSTD_FMT_AUTH_MAD_KEY:
                default:
                    /* Fill send buffer with MAD sector key */
                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_MADSECT_KEYA(mem);
                break;
            }
        break;

        case PH_FRINFC_MFSTD_FMT_RD_SECT_TR:
#ifdef PH_HAL4_ENABLE
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareRead;
#else
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareRead;
#endif /* #ifdef PH_HAL4_ENABLE */

            /* Send length is always one for read operation */
            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_VAL_1;
        break;

        case PH_FRINFC_MFSTD_FMT_WR_SECT_TR:
            /* Fill send buffer for writing sector trailer */
#ifdef PH_HAL4_ENABLE
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
#else
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
#endif /* #ifdef PH_HAL4_ENABLE */
            /* Copy the relevant sector trailer value in the buffer */
            switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock)
            {
            case PH_FRINFC_MFSTD_FMT_VAL_3:
                if (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)
                {
                    (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                                MADSectTr1k,
                                sizeof(MADSectTr1k));
                }
                else
                {
                    (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                                MADSectTr4k,
                                sizeof(MADSectTr4k));
                }
                break;
            case 67:
                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                            MADSectTr4k,
                            sizeof(MADSectTr4k));
                break;
            default:
                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                            NFCSectTr,
                            sizeof(NFCSectTr));
                break;
            }
            (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_11],
                NdefSmtCrdFmt->AddInfo.MfStdInfo.ScrtKeyB,
                sizeof(NdefSmtCrdFmt->AddInfo.MfStdInfo.ScrtKeyB));

            /* Send length is always 17 for write operation */
            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
        break;

        case PH_FRINFC_MFSTD_FMT_WR_TLV:
            /* Fill send buffer for writing TLV */
#ifdef PH_HAL4_ENABLE
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
#else
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
#endif /* #ifdef PH_HAL4_ENABLE */
            /* Copy the NDEF message TLV */
            (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                        NDEFMsgTLV, sizeof(NDEFMsgTLV));
            /* Send length is always 17 for write operation */
            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
        break;

        case PH_FRINFC_MFSTD_FMT_WR_MAD_BLK:
            /* Fill send buffer for writing MAD block */
#ifdef PH_HAL4_ENABLE
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
#else
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
#endif /* #ifdef PH_HAL4_ENABLE */

            if((BlockNo == PH_FRINFC_MFSTD_FMT_VAL_2) ||
                (BlockNo == 65) || (BlockNo == 66))
            {
                /* MAD block number 2, 65 and 66 has 0x03, 0xE1 in the
                    first two bytes */
                MADBlk[PH_FRINFC_MFSTD_FMT_VAL_0] = 0x03;
                MADBlk[PH_FRINFC_MFSTD_FMT_VAL_1] = 0xE1;
            }
            /* Copy the MAD Block values */
            (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                            MADBlk, sizeof(MADBlk));
            /* Send length is always 17 for write operation */
            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
        break;

        case PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK:
        default:
            /* Fill send buffer for writing MAD block */
#ifdef PH_HAL4_ENABLE
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
#else
            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
#endif /* #ifdef PH_HAL4_ENABLE */
            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
            switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk)
            {
            case PH_FRINFC_MFSTD_FMT_MAD_BLK_1:
                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk,
                            (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
                break;

            case PH_FRINFC_MFSTD_FMT_MAD_BLK_2:
                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[16],
                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
                break;

            case PH_FRINFC_MFSTD_FMT_MAD_BLK_64:
                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[32],
                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
                break;

            case PH_FRINFC_MFSTD_FMT_MAD_BLK_65:
                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[48],
                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
                break;

            case PH_FRINFC_MFSTD_FMT_MAD_BLK_66:
            default:
                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[64],
                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
                break;
            }
            break;
    }
    PHNFC_UNUSED_VARIABLE(mem);
}

static NFCSTATUS phFriNfc_MfStd_H_Transceive(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;

    /* set the data for additional data exchange*/
    NdefSmtCrdFmt->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
    NdefSmtCrdFmt->psDepAdditionalInfo.DepFlags.NADPresent = 0;
    NdefSmtCrdFmt->psDepAdditionalInfo.NAD = 0;

    /*set the completion routines for the card operations*/
    NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = phFriNfc_NdefSmtCrd_Process;
    NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NdefSmtCrdFmt;

    *NdefSmtCrdFmt->SendRecvLength = PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE;

    /* Call the Overlapped HAL Transceive function */
    Result = phFriNfc_OvrHal_Transceive(    NdefSmtCrdFmt->LowerDevice,
                                            &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
                                            NdefSmtCrdFmt->psRemoteDevInfo,
                                            NdefSmtCrdFmt->Cmd,
                                            &NdefSmtCrdFmt->psDepAdditionalInfo,
                                            NdefSmtCrdFmt->SendRecvBuf,
                                            NdefSmtCrdFmt->SendLength,
                                            NdefSmtCrdFmt->SendRecvBuf,
                                            NdefSmtCrdFmt->SendRecvLength);
    return Result;
}

static NFCSTATUS phFriNfc_MfStd_H_CallDisCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt,
                                             NFCSTATUS                    Status)
{
    NFCSTATUS   Result = Status;

    /*Set Ndef State*/
    NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_DIS_CON;

#ifdef PH_HAL4_ENABLE

    /*Call the Overlapped HAL POLL function */
    Result =  phFriNfc_OvrHal_Reconnect( NdefSmtCrdFmt->LowerDevice,
                                    &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
                                    NdefSmtCrdFmt->psRemoteDevInfo);
#else
    /*Call the Overlapped HAL POLL function */
    Result =  phFriNfc_OvrHal_Disconnect( NdefSmtCrdFmt->LowerDevice,
                                        &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
                                        NdefSmtCrdFmt->psRemoteDevInfo);
#endif /* #ifdef PH_HAL4_ENABLE */

    return Result;
}

static NFCSTATUS phFriNfc_MfStd_H_CallCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
    /*Set Ndef State*/
    NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_CON;

    /*Call the Overlapped HAL POLL function */
#ifdef PH_HAL4_ENABLE
    Result =  phFriNfc_OvrHal_Connect(  NdefSmtCrdFmt->LowerDevice,
                                        &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
                                        NdefSmtCrdFmt->psRemoteDevInfo,
                                        NdefSmtCrdFmt->AddInfo.MfStdInfo.DevInputParam);
#else
    Result =  phFriNfc_OvrHal_Connect(  NdefSmtCrdFmt->LowerDevice,
                                        &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
                                        phHal_eOpModesMifare,
                                        NdefSmtCrdFmt->psRemoteDevInfo,
                                        NdefSmtCrdFmt->AddInfo.MfStdInfo.DevInputParam);
#endif /* #ifdef PH_HAL4_ENABLE */

    return Result;
}

#ifndef PH_HAL4_ENABLE

static NFCSTATUS phFriNfc_MfStd_H_CallPoll(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
    /*Set ndef State*/
    NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_POLL;
    /* Opmodes */
    NdefSmtCrdFmt->OpModeType[PH_FRINFC_MFSTD_FMT_VAL_0] = phHal_eOpModesMifare;
    NdefSmtCrdFmt->OpModeType[PH_FRINFC_MFSTD_FMT_VAL_1] = phHal_eOpModesArrayTerminator;

    /* Number of devices to poll */
    NdefSmtCrdFmt->AddInfo.MfStdInfo.NoOfDevices = PH_FRINFC_MFSTD_FMT_VAL_1;

    /*Call the Overlapped HAL POLL function */
    Result =  phFriNfc_OvrHal_Poll( NdefSmtCrdFmt->LowerDevice,
                                    &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
                                    NdefSmtCrdFmt->OpModeType,
                                    NdefSmtCrdFmt->psRemoteDevInfo,
                                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.NoOfDevices,
                                    NdefSmtCrdFmt->AddInfo.MfStdInfo.DevInputParam);
    return Result;
}

#endif /* #ifndef PH_HAL4_ENABLE */

static NFCSTATUS phFriNfc_MfStd_H_ProCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
                index = PH_FRINFC_MFSTD_FMT_VAL_1;
    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;

    phFriNfc_MfStd_H_ChangeAuthSt(NdefSmtCrdFmt);
    if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
    {
        PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
    }
    else
    {
        /* Set the state */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
        /* Start authentication */
        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    }
    return Result;
}

static NFCSTATUS phFriNfc_MfStd_H_ProAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;

    /* Depending on the authentication key check the  */
    switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState)
    {
        case PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY:
            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
                PH_FRINFC_MFSTD_FMT_VAL_3) &&
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag ==
                PH_FRINFC_MFSTD_FMT_VAL_0))
            {
                /* Authenticate with default key for block 3 is successful,
                    so fill the MAD block of sector 0 */
                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                                    PH_FRINFC_MFSTD_FMT_VAL_1;
                /* Write the MAD block */
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
            }
            else if((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock == 67)
                && (NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag ==
                PH_FRINFC_MFSTD_FMT_VAL_0))
            {
                /* Authenticate with default key for block 3 is successful,
                so fill the MAD block of sector 64 */
                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 64;
                /* Write the MAD block */
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
            }
            else
            {
                /* Not a MAD sector */
                NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag =
                                        PH_FRINFC_MFSTD_FMT_VAL_0;
                /* Write the MAD block */
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
            }
        break;

        case PH_FRINFC_MFSTD_FMT_AUTH_KEYB:
            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_1) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_2) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_64) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_65) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_66))
            {
                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                            NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk;
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK;
            }
            else
            {
                NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
                                PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
            }

        break;

        case PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB:
            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_1) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_2) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_64) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_65) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_66))
            {
                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                    NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk;
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK;
            }
            else
            {
                NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
                    PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
            }
            break;

        case PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY:
        case PH_FRINFC_MFSTD_FMT_AUTH_MAD_KEY:
        default:
            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_66) ||
                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
                PH_FRINFC_MFSTD_FMT_MAD_BLK_2))
            {
                /* Updating the MAD block is complete */
                NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
                                PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
                /* If Mifare 4k card, write the TLV */
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_TLV;
            }
            else
            {
                /* Depending on the sector trailer, check the access bit */
                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_RD_SECT_TR;
            }
        break;
    }
    /* Call read, write or authenticate */
    Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    return Result;
}

static NFCSTATUS phFriNfc_MfStd_H_ErrWrSectTr( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
{
    NFCSTATUS   Result = NdefSmtCrdFmt->FmtProcStatus;
    /* If default key A is used for authentication and if write fails, then try to
    authenticate using key B*/
    if(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
        PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY)
    {
        /* Change the state to authentication */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
        /* internal authenticate state = key B */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_AUTH_KEYB;
        /* Now call authenticate */
        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    }
    else
    {
        Result = phFriNfc_MfStd_H_ProWrSectTr(NdefSmtCrdFmt);
    }
    return Result;
}
static NFCSTATUS phFriNfc_MfStd_H_ProRdSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
                index = PH_FRINFC_MFSTD_FMT_VAL_1,
                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;
    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;

    /* Calculate sector index */
    SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;

    /* Depending on the sector trailer, check the access bit */
    memcompare = phFriNfc_MfStd_H_ChkAcsBit(NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock,
                                            NdefSmtCrdFmt->SendRecvBuf,
                                            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSect_AccessBits,
                                            NdefSmtCrdFmt->AddInfo.MfStdInfo.NFCForumSect_AccessBits);

    /* Check the sector for ndef compliance */
    NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] = (uint8_t)
                ((memcompare != PH_FRINFC_MFSTD_FMT_VAL_0)?
                PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL:
                PH_FRINFC_MFSTD_FMT_NDEF_COMPL);

    /* Increment the current block */
    PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
    SectIndex++;
    if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
    {
       PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
    }
    else
    {
        /* Set the state */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
        /* Set the authenticate state */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
                            PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
        /* Start authentication */
        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    }
    return Result;
}

static NFCSTATUS phFriNfc_MfStd_H_ProWrSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
                index = PH_FRINFC_MFSTD_FMT_VAL_1,
                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;
    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;

    /* Calculate sector index */
    SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;

    /* Sector is ndef compliance */
    NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] = (uint8_t)
                    ((NdefSmtCrdFmt->FmtProcStatus != NFCSTATUS_SUCCESS)?
                        PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL:
                        PH_FRINFC_MFSTD_FMT_NDEF_COMPL);

    /* Increment the current block */
    PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
    SectIndex++;
    if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
    {
        PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
    }
    else
    {
        /* Set the state */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
        /* Set the authenticate state */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
                            PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
        /* Start authentication */
        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    }
    return Result;
}

static uint32_t phFriNfc_MfStd_H_ChkAcsBit(uint16_t                 BlockNo,
                                           const uint8_t            *RecvBuf,
                                           const uint8_t            AcsBits1[],
                                           const uint8_t            AcsBits2[])
{
    uint32_t    mem = PH_FRINFC_MFSTD_FMT_VAL_0;

    /* Compare the access bits read from the sector trailer */
    mem = (uint32_t)(((BlockNo == PH_FRINFC_MFSTD_FMT_VAL_3) ||
                    (BlockNo == 67))?
                    phFriNfc_MfStd_MemCompare((void*)&RecvBuf[PH_FRINFC_MFSTD_FMT_VAL_6],
                            (void*)AcsBits1,
                            PH_FRINFC_MFSTD_FMT_VAL_3):
                    phFriNfc_MfStd_MemCompare((void*)&RecvBuf[PH_FRINFC_MFSTD_FMT_VAL_6],
                            (void*)AcsBits2,
                            PH_FRINFC_MFSTD_FMT_VAL_3));

    return mem;
}

static NFCSTATUS phFriNfc_MfStd_H_WrRdAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
    /* Fill send buffer and send length */
    phFriNfc_MfStd_H_FillSendBuf(NdefSmtCrdFmt,
                                 NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock);
    /* Call ovrhal transceive */
    Result = phFriNfc_MfStd_H_Transceive(NdefSmtCrdFmt);

    return Result;
}

static void phFriNfc_MfStd_H_ChangeAuthSt(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    uint8_t     SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;

    if( NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
        PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB)
    {
        /* Calculate sector index */
        SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;

        /* Check the sector for ndef compliance */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] =
                    PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL;

        PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
    }
    PH_FRINFC_MFSTD_FMT_NXT_AUTH_STATE();
}

static void phFriNfc_MfStd_H_NdefComplSect(uint8_t      CardTypes,
                                           uint8_t      Sector[])
{
    uint8_t     count = PH_FRINFC_MFSTD_FMT_VAL_0,
                NdefComplSectMax = PH_FRINFC_MFSTD_FMT_VAL_0,
                NdefComplSectTemp = PH_FRINFC_MFSTD_FMT_VAL_1,
                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0,
                MaxCont = PH_FRINFC_MFSTD_FMT_VAL_0,
                MaxSect = PH_FRINFC_MFSTD_FMT_VAL_0;

    /* Get the maximum sector depending on the sector */
    MaxSect = ((CardTypes == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)?
                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K:
                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);
    /* Sector index */
    NdefComplSectTemp = SectIndex = PH_FRINFC_MFSTD_FMT_VAL_1;
    /* Check the sector index depending on the card type */
    while(((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K) &&
        (CardTypes == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)) ||
        ((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K) &&
        (CardTypes == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)))
    {
        if (Sector[SectIndex] ==
            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL)
        {
            if (MaxCont > count)
            {
                /* Store the maximum contiguous */
                NdefComplSectMax = NdefComplSectTemp;
                count = MaxCont;
            }
            MaxCont = PH_FRINFC_MFSTD_FMT_VAL_0;
            /* Increment the sector index */
            PH_FRINFC_MFSTD_FMT_INCR_SECT;
            /* Get the next compliant sector */
            NdefComplSectTemp = SectIndex;
        }
        else
        {
            /* Increment the sector index */
            PH_FRINFC_MFSTD_FMT_INCR_SECT;
        }
        MaxCont ++;

    }
    if (MaxCont > count)
    {
        /* Store the maximum contiguous */
        NdefComplSectMax = NdefComplSectTemp;
        count = MaxCont;
    }
    /* Set the sector value has non ndef compliant which are not present with
    contiguous ndef compliant sectors */
    if((((count < (MaxSect - PH_FRINFC_MFSTD_FMT_VAL_1)) && (CardTypes
        == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)) ||
        ((count < (MaxSect - PH_FRINFC_MFSTD_FMT_VAL_2)) && (CardTypes
        == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD))) &&
        ((NdefComplSectMax > PH_FRINFC_MFSTD_FMT_VAL_0) &&
        (NdefComplSectMax < (MaxSect - PH_FRINFC_MFSTD_FMT_VAL_2))))
    {
        (void)memset(&Sector[PH_FRINFC_MFSTD_FMT_VAL_1],
            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
            (NdefComplSectMax - PH_FRINFC_MFSTD_FMT_VAL_1));

        (void)memset(&Sector[(NdefComplSectMax + count)],
            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
            (MaxSect - (NdefComplSectMax + count)));
    }
}


static NFCSTATUS phFriNfc_MfStd_H_ProWrMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;

    switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock)
    {
    case PH_FRINFC_MFSTD_FMT_VAL_1:
        /* MAD blocks, still not completed */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
        /* MAD block number 2 */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                            PH_FRINFC_MFSTD_FMT_VAL_2;
        break;

    case PH_FRINFC_MFSTD_FMT_VAL_2:
        /* Now write to MAD block is completed */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag =
                                        PH_FRINFC_MFSTD_FMT_VAL_1;
        /* Now write the sector trailer, so change the state */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
        /* MAD block number 3 = Sector trailer */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                            PH_FRINFC_MFSTD_FMT_VAL_3;
        break;

    case 64:
        /* MAD blocks, still not completed */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 65;
        break;

    case 65:
        /* MAD blocks, still not completed */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 66;
        break;

    case 66:
    default:
        /* Now write to MAD block is completed */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag =
                                        PH_FRINFC_MFSTD_FMT_VAL_1;
        /* Now write the sector trailer, so change the state */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 67;
        break;

    }
    /* Write the block */
    Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);

    return Result;
}

static NFCSTATUS phFriNfc_MfStd_H_ProErrAuth( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
{
    NFCSTATUS   Result = NdefSmtCrdFmt->FmtProcStatus;
    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
                index = PH_FRINFC_MFSTD_FMT_VAL_1;
    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;

    if ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock == 67) &&
        (NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
                    PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB))
    {
        /* Error in the MAD sector 16, so the remaining sector
        information cant be updated */
        (void)memset(&NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[16],
                    PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
                    (PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K - 16));
        PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
    }
    else if(((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock >
        PH_FRINFC_MFSTD_FMT_VAL_3) &&
        (NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState !=
        PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB)) ||
        ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
        PH_FRINFC_MFSTD_FMT_VAL_3) &&
        (NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState <
        PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB)))
    {
        /* Authenticate failed, so disconnect, poll and connect */
        Result = phFriNfc_MfStd_H_CallDisCon(NdefSmtCrdFmt,
                                             Result);
    }
    else
    {
        if (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
            PH_FRINFC_MFSTD_FMT_VAL_3)
        {
            (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl,
                        PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
                        PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);
        }
    }

    return Result;
}

static NFCSTATUS phFriNfc_MfStd_H_ProUpdMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
{
    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
    switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk)
    {
    case PH_FRINFC_MFSTD_FMT_MAD_BLK_1:
        /* Write the next MAD Block */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
                                PH_FRINFC_MFSTD_FMT_MAD_BLK_2;
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                                PH_FRINFC_MFSTD_FMT_MAD_BLK_2;
        break;

    case PH_FRINFC_MFSTD_FMT_MAD_BLK_2:
    case PH_FRINFC_MFSTD_FMT_MAD_BLK_66:
        if((NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD) ||
           (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
           PH_FRINFC_MFSTD_FMT_MAD_BLK_66))
        {
            /* Get the block from where the TLV has to be written */
            phFriNfc_MfStd_H_BlkNoToWrTLV(NdefSmtCrdFmt);

            NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
            NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
                                    PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY;
        }
        else
        {
            /* Write the next MAD Block */
            NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
                            PH_FRINFC_MFSTD_FMT_MAD_BLK_64;
                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                            PH_FRINFC_MFSTD_FMT_MAD_BLK_64;
            NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
            NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
                                PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB;
        }
        break;

    case PH_FRINFC_MFSTD_FMT_MAD_BLK_64:
        /* Write the next MAD Block */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
                                PH_FRINFC_MFSTD_FMT_MAD_BLK_65;
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                                PH_FRINFC_MFSTD_FMT_MAD_BLK_65;
        break;

    case PH_FRINFC_MFSTD_FMT_MAD_BLK_65:
    default:
        /* Write the next MAD Block */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
                                    PH_FRINFC_MFSTD_FMT_MAD_BLK_66;
        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
                                    PH_FRINFC_MFSTD_FMT_MAD_BLK_66;
        break;
    }
    Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    return Result;
}

static void phFriNfc_MfStd_H_StrNdefData( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
{
    uint8_t     SectIndex = PH_FRINFC_MFSTD_FMT_VAL_1,
                index = PH_FRINFC_MFSTD_FMT_VAL_0;
    
    (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk,
                0x00,
                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);

    /* Zeroth sector of the Mifare card is MAD sector, CRC is 0x14 */
    NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[PH_FRINFC_MFSTD_FMT_VAL_0] = 0x14;
    /* Info byte is 0x01, because the NDEF application is written and as per the MAD spec, 
    the value for miscellaneous application is 0x01 */
    NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[PH_FRINFC_MFSTD_FMT_VAL_1] = 0x01;

    if(NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)
    {
        /* If 4k card then sector number 16 is MAD sector, CRC is 0xE8 */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[32] = 0xE8;
        /* Info byte is 0x01, because the NDEF application is written and 
            as per the MAD spec, 
        the value for miscellaneous application is 0x01 */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[33] = 0x01;
    }
    /* NDEF information has to be updated from */
    index = PH_FRINFC_MFSTD_FMT_VAL_2;
    /* Depending on the card type, check the sector index */
    while (((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K) &&
        (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)) ||
        ((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K) &&
        (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)))
    {
        /* Is the sector ndef compliant? */
        if(NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] ==
            PH_FRINFC_MFSTD_FMT_NDEF_COMPL)
        {
            /* Ndef compliant sector, update the MAD sector array
                in the context with values 0x03 and 0xE1
                0x03 and 0xE1 is NDEF information in MAD sector */
            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] =
                                        PH_FRINFC_MFSTD_FMT_NDEF_INFO1;
            index++;
            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] =
                                        PH_FRINFC_MFSTD_FMT_NDEF_INFO2;
            index++;
        }
        else
        {
            /* Not a Ndef compliant sector, update the MAD sector array
                in the context with values 0x00 and 0x00
                0x00 and 0x00 is NDEF information in MAD sector */
            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] = 0x00;
            index++;
            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] = 0x00;
            index++;
        }
        /* Go to next sector */
        SectIndex++;
        /* is the sector, a MAD sector 16? */
        if(SectIndex == PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K)
        {
            /* MAD sector number 16, so skip this sector */
            SectIndex = SectIndex + PH_FRINFC_MFSTD_FMT_VAL_1;
            index = index + PH_FRINFC_MFSTD_FMT_VAL_2;
        }
    }
}

static void phFriNfc_MfStd_H_BlkNoToWrTLV( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
{
    uint8_t     SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_VAL_1;
    while (((SectIndex < (uint8_t)PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K) &&
        (NdefSmtCrdFmt->CardType == (uint8_t)PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)) ||
        ((SectIndex < (uint8_t)PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K) &&
        (NdefSmtCrdFmt->CardType == (uint8_t)PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)))
    {
        if (NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] ==
            (uint8_t)PH_FRINFC_MFSTD_FMT_NDEF_COMPL)
        {
            /* Get the first NFC forum sector's block */
            NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = (uint16_t)
                                          (((SectIndex & 0xE0) >= 32)?
                                        (128 + ((SectIndex % 32) * 16)):
                                        (SectIndex * (uint8_t)PH_FRINFC_MFSTD_FMT_VAL_4));
            /* Break out of the loop */
            SectIndex += (uint8_t)PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K;
        }
        SectIndex++;
    }
}

static NFCSTATUS phFriNfc_MfStd_H_ErrRdSectTr( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
{
    NFCSTATUS   Result = NdefSmtCrdFmt->FmtProcStatus;
    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
                index = PH_FRINFC_MFSTD_FMT_VAL_1,
                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;
    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;
    /* If default key A is used for authentication and if write fails, then try to
    authenticate using key B*/
    if(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
        PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY)
    {
        /* Change the state to authentication */
        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
        /* internal authenticate state = key B */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_AUTH_KEYB;
        /* Now call authenticate */
        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
    }
    else
    {
        /* Calculate sector index */
        SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;

        /* Sector is ndef compliance */
        NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] = (uint8_t)
                            ((NdefSmtCrdFmt->FmtProcStatus != NFCSTATUS_SUCCESS)?
                            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL:
                            PH_FRINFC_MFSTD_FMT_NDEF_COMPL);

        /* Increment the current block */
        PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
        SectIndex++;
        if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
        {
            PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
        }
        else
        {
            /* Set the state */
            NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
            /* Set the authenticate state */
            NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
                PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
            /* Start authentication */
            Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
        }
    }
    return Result;
}

static int phFriNfc_MfStd_MemCompare( void *s1, void *s2, unsigned int n )
{
    int8_t   diff = 0;
    int8_t *char_1  =(int8_t *)s1;
    int8_t *char_2  =(int8_t *)s2;
    if(NULL == s1 || NULL == s2)
    {
        PHDBG_CRITICAL_ERROR("NULL pointer passed to memcompare");
    }
    else
    {
        for(;((n>0)&&(diff==0));n--,char_1++,char_2++)
        {
            diff = *char_1 - *char_2;
        }
    }
    return (int)diff;
}



#ifdef UNIT_TEST
#include <phUnitTestNfc_MifStdFormat_static.c>
#endif