15d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
25d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Copyright (C) 2010 NXP Semiconductors
35d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
45d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
55d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * you may not use this file except in compliance with the License.
65d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * You may obtain a copy of the License at
75d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
85d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
95d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Unless required by applicable law or agreed to in writing, software
115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * See the License for the specific language governing permissions and
145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * limitations under the License.
155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \file  phFriNfc_MifStdFormat.c
195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief NFC Ndef Formatting For Mifare standard card.
205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Project: NFC-FRI
225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Date: Tue Oct 20 20:13:03 2009 $
245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Author: ing02260 $
255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $Revision: 1.9 $
265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * $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 $
275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_MifStdFormat.h>
315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_OvrHal.h>
325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*! \ingroup grp_file_attributes
345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *  \name NDEF Mapping
355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * File: \ref phFriNfc_MifStdFormat.c
375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*@{*/
405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#define PHFRINFCMIFSTDFMT_FILEREVISION "$Revision: 1.9 $"
415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#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 $"
425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*@}*/
435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function fills the
465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * send buffer for transceive function
475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_FillSendBuf(phFriNfc_sNdefSmtCrdFmt_t      *NdefSmtCrdFmt,
495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        uint16_t                         BlockNo);
505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function authenticates
535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *  a block or a sector from the card.
545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_Transceive(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function calls
595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *  disconnect.
605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_CallDisCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt,
625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                             NFCSTATUS                    Status);
635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function calls
665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *  disconnect.
675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_CallCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifndef PH_HAL4_ENABLE
715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function calls
735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *  disconnect.
745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_CallPoll(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifndef PH_HAL4_ENABLE */
775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * poll call.
815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * authenticate call.
875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * read access bit call.
935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProRdSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process the
985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * write access bit call.
995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
1005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProWrSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
1015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function writes the
1045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * sector trailer using the block number.
1055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
1065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_WrRdAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
1075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function checks the
1105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * access bits of each sector trailer.
1115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
1125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic uint32_t phFriNfc_MfStd_H_ChkAcsBit(uint16_t                 BlockNo,
1135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           const uint8_t                    *RecvBuf,
1145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           const uint8_t            AcsBits1[],
1155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           const uint8_t            AcsBits2[]);
1165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function change the
1195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * authentication state and change the block number if required
1205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
1215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_ChangeAuthSt(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
1225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function finds the
1255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * contiguous ndef compliant blocks.
1265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
1275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_NdefComplSect(uint8_t      CardTypes,
1285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           uint8_t      Sector[]);
1295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief \copydoc page_ovr Helper function for Mifare standard. This function writes the
1325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * MAD block values.
1335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
1345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProWrMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
1355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
1385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* the error status of the authentication
1395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProErrAuth(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
1415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
1445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* the error status of the writing sector trailer
1455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ErrWrSectTr(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
1475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
1505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* the error status of the reading sector trailer
1515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ErrRdSectTr(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
1535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall process
1565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* the error status of the writing sector trailer
1575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProUpdMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
1595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall store the
1625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* ndef compliant in the MAD array which will be later used for updating the MAD sector
1635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_StrNdefData(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
1655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*!
1675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief \copydoc page_ovr Helper function for Mifare standard. This function shall find the ndef compliant
1685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* and calculate the block number to write the NDEF TLV
1695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
1705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_BlkNoToWrTLV(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt);
1715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic int phFriNfc_MfStd_MemCompare ( void *s1, void *s2, unsigned int n );
1735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phFriNfc_MfStd_Reset(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
1765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
1775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t NfcForSectArray[] = PH_FRINFC_SMTCRDFMT_NFCFORUMSECT_KEYA_ACS_BIT,
1785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            MADSectArray[] = PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_1K;
1795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Authentication state */
1815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_VAL_1;
1825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Set default key for A or B */
1845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.Default_KeyA_OR_B,
1855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_DEFAULT_KEY, /* 0xFF */
1865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_6);
1875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* MAD sector key A */
1895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSect_KeyA,
1905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                MADSectArray, //PH_FRINFC_MFSTD_FMT_VAL_0,
1915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_6);
1925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Copy access bits for MAD sectors */
1945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSect_AccessBits,
1955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                &MADSectArray[PH_FRINFC_MFSTD_FMT_VAL_6],
1965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_3);
1975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* NFC forum sector key A */
1995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.NFCForumSect_KeyA,
2005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NfcForSectArray, //PH_FRINFC_MFSTD_FMT_VAL_0,
2015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_6);
2025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Copy access bits for NFC forum sectors */
2045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    (void)memcpy(NdefSmtCrdFmt->AddInfo.MfStdInfo.NFCForumSect_AccessBits,
2055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                &NfcForSectArray[PH_FRINFC_MFSTD_FMT_VAL_6],
2065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_3);
2075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Sector compliant array initialised to 0 */
2095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl,
2105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_0, /* 0x00 */
2115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);
2125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag = (uint8_t)PH_FRINFC_MFSTD_FMT_VAL_0;
2145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
2155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
2175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_MfStd_Format( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt, const uint8_t *ScrtKeyB )
2195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
2205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS               Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
2215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                NFCSTATUS_INVALID_PARAMETER);
2225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t                 index = PH_FRINFC_MFSTD_FMT_VAL_0;
2235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(ScrtKeyB != NULL)
2255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
2275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
2285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Store Key B in the context */
2295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        while(index < PH_FRINFC_MFSTD_FMT_VAL_6)
2305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.ScrtKeyB[index] = ScrtKeyB[index];
2325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index++;
2335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Set the state */
2355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
2365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Initialise current block to the first sector trailer */
2375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = PH_FRINFC_MFSTD_FMT_VAL_3;
2385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Set the authenticate state */
2395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
2405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Start authentication */
2415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
2425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
2445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
2455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellyvoid phFriNfc_MfStd_Process(void        *Context,
2475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NFCSTATUS   Status)
2485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
2495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phFriNfc_sNdefSmtCrdFmt_t  *NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)Context;
2505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Copy the formatting status */
2515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->FmtProcStatus = Status;
2525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(Status == NFCSTATUS_SUCCESS)
2535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
2545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        switch(NdefSmtCrdFmt->State)
2555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
2565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_SECT:
2575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ProAuth(NdefSmtCrdFmt);
2585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_DIS_CON:
2615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifndef PH_HAL4_ENABLE
2625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_CallPoll(NdefSmtCrdFmt);
2635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_POLL:
2665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifndef PH_HAL4_ENABLE */
2675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_CallCon(NdefSmtCrdFmt);
2685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_CON:
2715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ProCon(NdefSmtCrdFmt);
2725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_RD_SECT_TR:
2755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ProRdSectTr(NdefSmtCrdFmt);
2765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_WR_SECT_TR:
2795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ProWrSectTr(NdefSmtCrdFmt);
2805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_WR_MAD_BLK:
2835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ProWrMADBlk(NdefSmtCrdFmt);
2845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_WR_TLV:
2875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK:
2905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ProUpdMADBlk(NdefSmtCrdFmt);
2915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        default:
2945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
2955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                NFCSTATUS_INVALID_DEVICE_REQUEST);
2965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
2975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
2985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
2995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
3005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        switch(NdefSmtCrdFmt->State)
3025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
3035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_SECT:
3045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ProErrAuth(NdefSmtCrdFmt);
3055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
3065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_WR_SECT_TR:
3085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ErrWrSectTr(NdefSmtCrdFmt);
3095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
3105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_RD_SECT_TR:
3125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = phFriNfc_MfStd_H_ErrRdSectTr(NdefSmtCrdFmt);
3135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
3145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        default:
3165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Status = NdefSmtCrdFmt->FmtProcStatus;
3175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
3185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
3195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Status is not success then call completion routine */
3225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(Status != NFCSTATUS_PENDING)
3235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        phFriNfc_SmtCrdFmt_HCrHandler(NdefSmtCrdFmt, Status);
3255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
3265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
3275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_FillSendBuf(phFriNfc_sNdefSmtCrdFmt_t      *NdefSmtCrdFmt,
3295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        uint16_t                         BlockNo)
3305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
3315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    void        *mem = NULL;
3325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     MADSectTr1k[] = PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_1K, /* MAD key A,
3335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                            Access bits and GPB of MAD sector */
3345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                MADSectTr4k[] = PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_4K, /* MAD key A,
3355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                                    Access bits and GPB of MAD sector */
3365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NFCSectTr[] = PH_FRINFC_SMTCRDFMT_NFCFORUMSECT_KEYA_ACS_BIT, /* NFC forum key A,
3375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                             Access bits and GPB of NFC sector */
3385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NDEFMsgTLV[16] = {0x03, 0x00, 0xFE, 0x00, 0x00, 0x00, /* NDEF message TLV (INITIALISED state) */
3395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                  0x00, 0x00, 0x00, 0x00},
3415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                MADBlk[16] = {0x0F, 0x00, 0x03, 0xE1, 0x03, 0xE1,
3425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                              0x03, 0xE1, 0x03, 0xE1,
3435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                              0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
3445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Block number in send buffer */
3455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_0] = (uint8_t)BlockNo;
3465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Initialise send receive length */
3475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    *NdefSmtCrdFmt->SendRecvLength = PH_FRINFC_MFSTD_FMT_MAX_RECV_LENGTH;
3485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Depending on the different state, fill the send buffer */
3505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    switch(NdefSmtCrdFmt->State)
3515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
3525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_SECT:
3535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Depending on the authentication state, fill the send buffer */
3545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState)
3555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
3565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                case PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY:
3575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                case PH_FRINFC_MFSTD_FMT_AUTH_KEYB:
3585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* Fill send buffer with the default key */
3595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_DEF(mem);
3605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
3615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                case PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY:
3635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* Fill send buffer with NFC forum sector key */
3645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_NFCSECT_KEYA(mem);
3655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
3665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                case PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB:
3685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* Fill send buffer with NFC forum sector key */
3695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_SCRT_KEY(mem);
3705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    break;
3715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                case PH_FRINFC_MFSTD_FMT_AUTH_MAD_KEY:
3735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                default:
3745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    /* Fill send buffer with MAD sector key */
3755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_MADSECT_KEYA(mem);
3765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
3775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
3785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
3795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_RD_SECT_TR:
3815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef PH_HAL4_ENABLE
3825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareRead;
3835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
3845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareRead;
3855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef PH_HAL4_ENABLE */
3865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Send length is always one for read operation */
3885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_VAL_1;
3895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
3905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_WR_SECT_TR:
3925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Fill send buffer for writing sector trailer */
3935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef PH_HAL4_ENABLE
3945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
3955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
3965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
3975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef PH_HAL4_ENABLE */
3985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Copy the relevant sector trailer value in the buffer */
3995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock)
4005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            case PH_FRINFC_MFSTD_FMT_VAL_3:
4025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                if (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)
4035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
4045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                MADSectTr1k,
4065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                sizeof(MADSectTr1k));
4075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
4085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                else
4095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                {
4105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                MADSectTr4k,
4125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                sizeof(MADSectTr4k));
4135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                }
4145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
4155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            case 67:
4165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            MADSectTr4k,
4185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            sizeof(MADSectTr4k));
4195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
4205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            default:
4215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NFCSectTr,
4235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            sizeof(NFCSectTr));
4245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
4255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
4265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_11],
4275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.ScrtKeyB,
4285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                sizeof(NdefSmtCrdFmt->AddInfo.MfStdInfo.ScrtKeyB));
4295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Send length is always 17 for write operation */
4315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
4325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
4335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_WR_TLV:
4355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Fill send buffer for writing TLV */
4365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef PH_HAL4_ENABLE
4375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
4385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
4395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
4405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef PH_HAL4_ENABLE */
4415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Copy the NDEF message TLV */
4425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        NDEFMsgTLV, sizeof(NDEFMsgTLV));
4445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Send length is always 17 for write operation */
4455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
4465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
4475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_WR_MAD_BLK:
4495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Fill send buffer for writing MAD block */
4505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef PH_HAL4_ENABLE
4515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
4525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
4535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
4545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef PH_HAL4_ENABLE */
4555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if((BlockNo == PH_FRINFC_MFSTD_FMT_VAL_2) ||
4575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (BlockNo == 65) || (BlockNo == 66))
4585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* MAD block number 2, 65 and 66 has 0x03, 0xE1 in the
4605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    first two bytes */
4615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                MADBlk[PH_FRINFC_MFSTD_FMT_VAL_0] = 0x03;
4625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                MADBlk[PH_FRINFC_MFSTD_FMT_VAL_1] = 0xE1;
4635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
4645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Copy the MAD Block values */
4655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            MADBlk, sizeof(MADBlk));
4675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Send length is always 17 for write operation */
4685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
4695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
4705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK:
4725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        default:
4735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Fill send buffer for writing MAD block */
4745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef PH_HAL4_ENABLE
4755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareWrite16;
4765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
4775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareCmdListMifareWrite16;
4785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef PH_HAL4_ENABLE */
4795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH;
4805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk)
4815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            case PH_FRINFC_MFSTD_FMT_MAD_BLK_1:
4835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk,
4855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
4865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
4875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            case PH_FRINFC_MFSTD_FMT_MAD_BLK_2:
4895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[16],
4915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
4925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
4935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            case PH_FRINFC_MFSTD_FMT_MAD_BLK_64:
4955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
4965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[32],
4975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
4985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
4995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            case PH_FRINFC_MFSTD_FMT_MAD_BLK_65:
5015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
5025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[48],
5035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
5045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
5055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            case PH_FRINFC_MFSTD_FMT_MAD_BLK_66:
5075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            default:
5085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],
5095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[64],
5105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH - PH_FRINFC_MFSTD_FMT_VAL_1));
5115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                break;
5125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
5135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
5145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
5155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    PHNFC_UNUSED_VARIABLE(mem);
5165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_Transceive(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
5195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
5205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* set the data for additional data exchange*/
5235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
5245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->psDepAdditionalInfo.DepFlags.NADPresent = 0;
5255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->psDepAdditionalInfo.NAD = 0;
5265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*set the completion routines for the card operations*/
5285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = phFriNfc_NdefSmtCrd_Process;
5295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NdefSmtCrdFmt;
5305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    *NdefSmtCrdFmt->SendRecvLength = PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE;
5325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Call the Overlapped HAL Transceive function */
5345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result = phFriNfc_OvrHal_Transceive(    NdefSmtCrdFmt->LowerDevice,
5355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
5365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->psRemoteDevInfo,
5375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->Cmd,
5385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            &NdefSmtCrdFmt->psDepAdditionalInfo,
5395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->SendRecvBuf,
5405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->SendLength,
5415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->SendRecvBuf,
5425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->SendRecvLength);
5435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
5445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_CallDisCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt,
5475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                             NFCSTATUS                    Status)
5485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
5495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = Status;
5505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Set Ndef State*/
5525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_DIS_CON;
5535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef PH_HAL4_ENABLE
5555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Call the Overlapped HAL POLL function */
5575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result =  phFriNfc_OvrHal_Reconnect( NdefSmtCrdFmt->LowerDevice,
5585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
5595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    NdefSmtCrdFmt->psRemoteDevInfo);
5605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
5615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Call the Overlapped HAL POLL function */
5625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result =  phFriNfc_OvrHal_Disconnect( NdefSmtCrdFmt->LowerDevice,
5635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
5645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        NdefSmtCrdFmt->psRemoteDevInfo);
5655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef PH_HAL4_ENABLE */
5665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
5685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_CallCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
5715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
5725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Set Ndef State*/
5745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_CON;
5755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Call the Overlapped HAL POLL function */
5775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef PH_HAL4_ENABLE
5785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result =  phFriNfc_OvrHal_Connect(  NdefSmtCrdFmt->LowerDevice,
5795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
5805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        NdefSmtCrdFmt->psRemoteDevInfo,
5815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        NdefSmtCrdFmt->AddInfo.MfStdInfo.DevInputParam);
5825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#else
5835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result =  phFriNfc_OvrHal_Connect(  NdefSmtCrdFmt->LowerDevice,
5845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
5855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phHal_eOpModesMifare,
5865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        NdefSmtCrdFmt->psRemoteDevInfo,
5875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        NdefSmtCrdFmt->AddInfo.MfStdInfo.DevInputParam);
5885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifdef PH_HAL4_ENABLE */
5895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
5915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifndef PH_HAL4_ENABLE
5945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_CallPoll(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
5965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
5975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
5985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Set ndef State*/
5995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_POLL;
6005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Opmodes */
6015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->OpModeType[PH_FRINFC_MFSTD_FMT_VAL_0] = phHal_eOpModesMifare;
6025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->OpModeType[PH_FRINFC_MFSTD_FMT_VAL_1] = phHal_eOpModesArrayTerminator;
6035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Number of devices to poll */
6055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.NoOfDevices = PH_FRINFC_MFSTD_FMT_VAL_1;
6065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /*Call the Overlapped HAL POLL function */
6085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result =  phFriNfc_OvrHal_Poll( NdefSmtCrdFmt->LowerDevice,
6095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
6105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    NdefSmtCrdFmt->OpModeType,
6115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    NdefSmtCrdFmt->psRemoteDevInfo,
6125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    &NdefSmtCrdFmt->AddInfo.MfStdInfo.NoOfDevices,
6135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    NdefSmtCrdFmt->AddInfo.MfStdInfo.DevInputParam);
6145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
6155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
6165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif /* #ifndef PH_HAL4_ENABLE */
6185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProCon(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
6205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
6215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
6225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
6235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                index = PH_FRINFC_MFSTD_FMT_VAL_1;
6245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;
6255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phFriNfc_MfStd_H_ChangeAuthSt(NdefSmtCrdFmt);
6275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
6285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
6305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
6325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Set the state */
6345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
6355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Start authentication */
6365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
6375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
6385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
6395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
6405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
6425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
6435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
6445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Depending on the authentication key check the  */
6465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState)
6475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
6485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY:
6495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
6505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_3) &&
6515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag ==
6525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_0))
6535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
6545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Authenticate with default key for block 3 is successful,
6555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    so fill the MAD block of sector 0 */
6565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
6575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    PH_FRINFC_MFSTD_FMT_VAL_1;
6585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Write the MAD block */
6595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
6605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
6615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else if((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock == 67)
6625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                && (NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag ==
6635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_VAL_0))
6645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
6655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Authenticate with default key for block 3 is successful,
6665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                so fill the MAD block of sector 64 */
6675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 64;
6685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Write the MAD block */
6695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
6705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
6715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
6725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
6735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Not a MAD sector */
6745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag =
6755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        PH_FRINFC_MFSTD_FMT_VAL_0;
6765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Write the MAD block */
6775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
6785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
6795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
6805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_KEYB:
6825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
6835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_1) ||
6845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
6855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_2) ||
6865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
6875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_64) ||
6885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
6895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_65) ||
6905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
6915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_66))
6925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
6935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
6945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk;
6955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK;
6965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
6975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
6985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
6995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
7005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
7015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
7025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
7035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
7055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB:
7075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
7085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_1) ||
7095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
7105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_2) ||
7115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
7125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_64) ||
7135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
7145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_65) ||
7155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
7165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_66))
7175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
7185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
7195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk;
7205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK;
7215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
7225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
7235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
7245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
7255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
7265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
7275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
7285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            break;
7295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY:
7315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        case PH_FRINFC_MFSTD_FMT_AUTH_MAD_KEY:
7325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        default:
7335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if((NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
7345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_66) ||
7355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                (NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk ==
7365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAD_BLK_2))
7375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
7385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Updating the MAD block is complete */
7395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk =
7405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK;
7415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* If Mifare 4k card, write the TLV */
7425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_TLV;
7435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
7445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            else
7455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
7465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Depending on the sector trailer, check the access bit */
7475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_RD_SECT_TR;
7485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
7495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
7505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Call read, write or authenticate */
7525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
7535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
7545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
7555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ErrWrSectTr( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
7575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
7585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NdefSmtCrdFmt->FmtProcStatus;
7595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* If default key A is used for authentication and if write fails, then try to
7605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    authenticate using key B*/
7615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
7625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY)
7635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Change the state to authentication */
7655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
7665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* internal authenticate state = key B */
7675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_AUTH_KEYB;
7685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Now call authenticate */
7695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
7705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
7725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
7735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_ProWrSectTr(NdefSmtCrdFmt);
7745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
7755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
7765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
7775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProRdSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
7785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
7795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
7805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
7815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                index = PH_FRINFC_MFSTD_FMT_VAL_1,
7825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;
7835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;
7845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Calculate sector index */
7865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;
7875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Depending on the sector trailer, check the access bit */
7895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    memcompare = phFriNfc_MfStd_H_ChkAcsBit(NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock,
7905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->SendRecvBuf,
7915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSect_AccessBits,
7925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NdefSmtCrdFmt->AddInfo.MfStdInfo.NFCForumSect_AccessBits);
7935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check the sector for ndef compliance */
7955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] = (uint8_t)
7965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                ((memcompare != PH_FRINFC_MFSTD_FMT_VAL_0)?
7975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL:
7985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_NDEF_COMPL);
7995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Increment the current block */
8015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
8025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    SectIndex++;
8035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
8045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
8055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly       PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
8065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
8075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
8085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
8095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Set the state */
8105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
8115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Set the authenticate state */
8125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
8135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
8145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Start authentication */
8155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
8165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
8175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
8185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
8195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProWrSectTr(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
8215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
8225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
8235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
8245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                index = PH_FRINFC_MFSTD_FMT_VAL_1,
8255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;
8265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;
8275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Calculate sector index */
8295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;
8305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Sector is ndef compliance */
8325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] = (uint8_t)
8335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    ((NdefSmtCrdFmt->FmtProcStatus != NFCSTATUS_SUCCESS)?
8345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL:
8355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        PH_FRINFC_MFSTD_FMT_NDEF_COMPL);
8365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Increment the current block */
8385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
8395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    SectIndex++;
8405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
8415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
8425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
8435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
8445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
8455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
8465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Set the state */
8475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
8485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Set the authenticate state */
8495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
8505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
8515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Start authentication */
8525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
8535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
8545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
8555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
8565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic uint32_t phFriNfc_MfStd_H_ChkAcsBit(uint16_t                 BlockNo,
8585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           const uint8_t            *RecvBuf,
8595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           const uint8_t            AcsBits1[],
8605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           const uint8_t            AcsBits2[])
8615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
8625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint32_t    mem = PH_FRINFC_MFSTD_FMT_VAL_0;
8635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Compare the access bits read from the sector trailer */
8655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    mem = (uint32_t)(((BlockNo == PH_FRINFC_MFSTD_FMT_VAL_3) ||
8665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (BlockNo == 67))?
8675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phFriNfc_MfStd_MemCompare((void*)&RecvBuf[PH_FRINFC_MFSTD_FMT_VAL_6],
8685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            (void*)AcsBits1,
8695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_VAL_3):
8705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    phFriNfc_MfStd_MemCompare((void*)&RecvBuf[PH_FRINFC_MFSTD_FMT_VAL_6],
8715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            (void*)AcsBits2,
8725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_VAL_3));
8735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return mem;
8755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
8765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_WrRdAuth(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
8785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
8795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
8805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Fill send buffer and send length */
8815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    phFriNfc_MfStd_H_FillSendBuf(NdefSmtCrdFmt,
8825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                 NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock);
8835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Call ovrhal transceive */
8845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result = phFriNfc_MfStd_H_Transceive(NdefSmtCrdFmt);
8855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
8875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
8885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_ChangeAuthSt(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
8905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
8915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;
8925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if( NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
8945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB)
8955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
8965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Calculate sector index */
8975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;
8985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Check the sector for ndef compliance */
9005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] =
9015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL;
9025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
9045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
9055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    PH_FRINFC_MFSTD_FMT_NXT_AUTH_STATE();
9065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
9075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_NdefComplSect(uint8_t      CardTypes,
9095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           uint8_t      Sector[])
9105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
9115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     count = PH_FRINFC_MFSTD_FMT_VAL_0,
9125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefComplSectMax = PH_FRINFC_MFSTD_FMT_VAL_0,
9135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefComplSectTemp = PH_FRINFC_MFSTD_FMT_VAL_1,
9145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0,
9155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                MaxCont = PH_FRINFC_MFSTD_FMT_VAL_0,
9165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                MaxSect = PH_FRINFC_MFSTD_FMT_VAL_0;
9175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Get the maximum sector depending on the sector */
9195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    MaxSect = ((CardTypes == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)?
9205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K:
9215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);
9225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Sector index */
9235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefComplSectTemp = SectIndex = PH_FRINFC_MFSTD_FMT_VAL_1;
9245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Check the sector index depending on the card type */
9255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    while(((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K) &&
9265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (CardTypes == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)) ||
9275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K) &&
9285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (CardTypes == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)))
9295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
9305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if (Sector[SectIndex] ==
9315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL)
9325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
9335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if (MaxCont > count)
9345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
9355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                /* Store the maximum contiguous */
9365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefComplSectMax = NdefComplSectTemp;
9375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                count = MaxCont;
9385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
9395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            MaxCont = PH_FRINFC_MFSTD_FMT_VAL_0;
9405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Increment the sector index */
9415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_INCR_SECT;
9425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Get the next compliant sector */
9435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefComplSectTemp = SectIndex;
9445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
9455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
9465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
9475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Increment the sector index */
9485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_INCR_SECT;
9495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
9505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        MaxCont ++;
9515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
9535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if (MaxCont > count)
9545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
9555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Store the maximum contiguous */
9565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefComplSectMax = NdefComplSectTemp;
9575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        count = MaxCont;
9585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
9595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Set the sector value has non ndef compliant which are not present with
9605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    contiguous ndef compliant sectors */
9615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if((((count < (MaxSect - PH_FRINFC_MFSTD_FMT_VAL_1)) && (CardTypes
9625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)) ||
9635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ((count < (MaxSect - PH_FRINFC_MFSTD_FMT_VAL_2)) && (CardTypes
9645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD))) &&
9655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ((NdefComplSectMax > PH_FRINFC_MFSTD_FMT_VAL_0) &&
9665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefComplSectMax < (MaxSect - PH_FRINFC_MFSTD_FMT_VAL_2))))
9675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
9685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (void)memset(&Sector[PH_FRINFC_MFSTD_FMT_VAL_1],
9695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
9705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (NdefComplSectMax - PH_FRINFC_MFSTD_FMT_VAL_1));
9715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (void)memset(&Sector[(NdefComplSectMax + count)],
9735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
9745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (MaxSect - (NdefComplSectMax + count)));
9755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
9765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
9775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProWrMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
9805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
9815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
9825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock)
9845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
9855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case PH_FRINFC_MFSTD_FMT_VAL_1:
9865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* MAD blocks, still not completed */
9875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
9885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* MAD block number 2 */
9895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
9905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_VAL_2;
9915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
9925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case PH_FRINFC_MFSTD_FMT_VAL_2:
9945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Now write to MAD block is completed */
9955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag =
9965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        PH_FRINFC_MFSTD_FMT_VAL_1;
9975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Now write the sector trailer, so change the state */
9985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
9995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* MAD block number 3 = Sector trailer */
10005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
10015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_VAL_3;
10025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
10035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case 64:
10055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* MAD blocks, still not completed */
10065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
10075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 65;
10085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
10095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case 65:
10115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* MAD blocks, still not completed */
10125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_MAD_BLK;
10135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 66;
10145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
10155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case 66:
10175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    default:
10185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Now write to MAD block is completed */
10195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.WrMADBlkFlag =
10205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        PH_FRINFC_MFSTD_FMT_VAL_1;
10215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Now write the sector trailer, so change the state */
10225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_WR_SECT_TR;
10235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = 67;
10245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
10255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Write the block */
10285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
10295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
10315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
10325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProErrAuth( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
10345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
10355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NdefSmtCrdFmt->FmtProcStatus;
10365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
10375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                index = PH_FRINFC_MFSTD_FMT_VAL_1;
10385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;
10395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock == 67) &&
10415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
10425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB))
10435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
10445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Error in the MAD sector 16, so the remaining sector
10455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        information cant be updated */
10465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (void)memset(&NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[16],
10475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
10485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                    (PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K - 16));
10495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
10505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else if(((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock >
10525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_VAL_3) &&
10535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState !=
10545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB)) ||
10555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
10565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_VAL_3) &&
10575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState <
10585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB)))
10595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
10605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Authenticate failed, so disconnect, poll and connect */
10615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_CallDisCon(NdefSmtCrdFmt,
10625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                             Result);
10635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
10655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
10665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
10675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_VAL_3)
10685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
10695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl,
10705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL,
10715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                        PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);
10725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
10735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
10745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
10765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
10775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ProUpdMADBlk(phFriNfc_sNdefSmtCrdFmt_t    *NdefSmtCrdFmt)
10795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
10805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NFCSTATUS_SUCCESS;
10815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk)
10825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
10835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case PH_FRINFC_MFSTD_FMT_MAD_BLK_1:
10845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Write the next MAD Block */
10855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
10865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                PH_FRINFC_MFSTD_FMT_MAD_BLK_2;
10875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
10885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                PH_FRINFC_MFSTD_FMT_MAD_BLK_2;
10895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
10905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case PH_FRINFC_MFSTD_FMT_MAD_BLK_2:
10925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case PH_FRINFC_MFSTD_FMT_MAD_BLK_66:
10935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if((NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD) ||
10945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly           (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock ==
10955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly           PH_FRINFC_MFSTD_FMT_MAD_BLK_66))
10965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
10975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Get the block from where the TLV has to be written */
10985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            phFriNfc_MfStd_H_BlkNoToWrTLV(NdefSmtCrdFmt);
10995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
11015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
11025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY;
11035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
11055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Write the next MAD Block */
11075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
11085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_MAD_BLK_64;
11095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
11105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_MAD_BLK_64;
11115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
11125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
11135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB;
11145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
11165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case PH_FRINFC_MFSTD_FMT_MAD_BLK_64:
11185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Write the next MAD Block */
11195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
11205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                PH_FRINFC_MFSTD_FMT_MAD_BLK_65;
11215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
11225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                PH_FRINFC_MFSTD_FMT_MAD_BLK_65;
11235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
11245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    case PH_FRINFC_MFSTD_FMT_MAD_BLK_65:
11265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    default:
11275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Write the next MAD Block */
11285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = (uint8_t)
11295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    PH_FRINFC_MFSTD_FMT_MAD_BLK_66;
11305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock =
11315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    PH_FRINFC_MFSTD_FMT_MAD_BLK_66;
11325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        break;
11335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
11345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
11355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
11365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
11375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_StrNdefData( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
11395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
11405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     SectIndex = PH_FRINFC_MFSTD_FMT_VAL_1,
11415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                index = PH_FRINFC_MFSTD_FMT_VAL_0;
11425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    (void)memset(NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk,
11445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                0x00,
11455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K);
11465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Zeroth sector of the Mifare card is MAD sector, CRC is 0x14 */
11485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[PH_FRINFC_MFSTD_FMT_VAL_0] = 0x14;
11495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Info byte is 0x01, because the NDEF application is written and as per the MAD spec,
11505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    the value for miscellaneous application is 0x01 */
11515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[PH_FRINFC_MFSTD_FMT_VAL_1] = 0x01;
11525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)
11545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
11555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* If 4k card then sector number 16 is MAD sector, CRC is 0xE8 */
11565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[32] = 0xE8;
11575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Info byte is 0x01, because the NDEF application is written and
11585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            as per the MAD spec,
11595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        the value for miscellaneous application is 0x01 */
11605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[33] = 0x01;
11615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
11625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* NDEF information has to be updated from */
11635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    index = PH_FRINFC_MFSTD_FMT_VAL_2;
11645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* Depending on the card type, check the sector index */
11655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    while (((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K) &&
11665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)) ||
11675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ((SectIndex < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K) &&
11685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)))
11695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
11705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Is the sector ndef compliant? */
11715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] ==
11725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_NDEF_COMPL)
11735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Ndef compliant sector, update the MAD sector array
11755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                in the context with values 0x03 and 0xE1
11765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                0x03 and 0xE1 is NDEF information in MAD sector */
11775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] =
11785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        PH_FRINFC_MFSTD_FMT_NDEF_INFO1;
11795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index++;
11805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] =
11815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        PH_FRINFC_MFSTD_FMT_NDEF_INFO2;
11825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index++;
11835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
11855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Not a Ndef compliant sector, update the MAD sector array
11875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                in the context with values 0x00 and 0x00
11885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                0x00 and 0x00 is NDEF information in MAD sector */
11895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] = 0x00;
11905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index++;
11915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSectBlk[index] = 0x00;
11925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index++;
11935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
11945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Go to next sector */
11955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        SectIndex++;
11965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* is the sector, a MAD sector 16? */
11975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(SectIndex == PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K)
11985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
11995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* MAD sector number 16, so skip this sector */
12005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            SectIndex = SectIndex + PH_FRINFC_MFSTD_FMT_VAL_1;
12015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index = index + PH_FRINFC_MFSTD_FMT_VAL_2;
12025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
12055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_MfStd_H_BlkNoToWrTLV( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
12075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
12085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_VAL_1;
12095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    while (((SectIndex < (uint8_t)PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K) &&
12105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefSmtCrdFmt->CardType == (uint8_t)PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)) ||
12115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        ((SectIndex < (uint8_t)PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K) &&
12125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        (NdefSmtCrdFmt->CardType == (uint8_t)PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD)))
12135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if (NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] ==
12155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            (uint8_t)PH_FRINFC_MFSTD_FMT_NDEF_COMPL)
12165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
12175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Get the first NFC forum sector's block */
12185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = (uint16_t)
12195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          (((SectIndex & 0xE0) >= 32)?
12205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        (128 + ((SectIndex % 32) * 16)):
12215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        (SectIndex * (uint8_t)PH_FRINFC_MFSTD_FMT_VAL_4));
12225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Break out of the loop */
12235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            SectIndex += (uint8_t)PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K;
12245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        SectIndex++;
12265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
12285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic NFCSTATUS phFriNfc_MfStd_H_ErrRdSectTr( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt )
12305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
12315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    NFCSTATUS   Result = NdefSmtCrdFmt->FmtProcStatus;
12325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint8_t     Buffer[1] = {PH_FRINFC_MFSTD_FMT_NDEF_COMPL},
12335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                index = PH_FRINFC_MFSTD_FMT_VAL_1,
12345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                SectIndex = PH_FRINFC_MFSTD_FMT_VAL_0;
12355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    uint32_t    memcompare = PH_FRINFC_MFSTD_FMT_VAL_1;
12365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    /* If default key A is used for authentication and if write fails, then try to
12375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    authenticate using key B*/
12385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState ==
12395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY)
12405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Change the state to authentication */
12425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
12435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* internal authenticate state = key B */
12445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = PH_FRINFC_MFSTD_FMT_AUTH_KEYB;
12455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Now call authenticate */
12465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
12475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
12495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Calculate sector index */
12515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        SectIndex = (uint8_t)PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC;
12525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Sector is ndef compliance */
12545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[SectIndex] = (uint8_t)
12555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            ((NdefSmtCrdFmt->FmtProcStatus != NFCSTATUS_SUCCESS)?
12565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL:
12575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                            PH_FRINFC_MFSTD_FMT_NDEF_COMPL);
12585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        /* Increment the current block */
12605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PH_FRINFC_MFSTD_FMT_CUR_BLK_INC();
12615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        SectIndex++;
12625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        if(PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK)
12635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
12645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD();
12655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        else
12675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
12685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the state */
12695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT;
12705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the authenticate state */
12715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState =
12725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;
12735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Start authentication */
12745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt);
12755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return Result;
12785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
12795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic int phFriNfc_MfStd_MemCompare( void *s1, void *s2, unsigned int n )
12815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
12825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    int8_t   diff = 0;
12835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    int8_t *char_1  =(int8_t *)s1;
12845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    int8_t *char_2  =(int8_t *)s2;
12855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    if(NULL == s1 || NULL == s2)
12865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        PHDBG_CRITICAL_ERROR("NULL pointer passed to memcompare");
12885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    else
12905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    {
12915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        for(;((n>0)&&(diff==0));n--,char_1++,char_2++)
12925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        {
12935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            diff = *char_1 - *char_2;
12945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly        }
12955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    }
12965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly    return (int)diff;
12975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
12985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#ifdef UNIT_TEST
13025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phUnitTestNfc_MifStdFormat_static.c>
13035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#endif
1304