Home | History | Annotate | Download | only in mifare
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*
     18  * NFC Ndef Mapping For Remote Devices.
     19  */
     20 
     21 #ifndef PHFRINFC_MIFARESTDMAP_H
     22 #define PHFRINFC_MIFARESTDMAP_H
     23 
     24 #include <phFriNfc.h>
     25 #include <phFriNfc_NdefMap.h>
     26 #include <phNfcStatus.h>
     27 #include <phNfcTypes.h>
     28 
     29 /* NFC Device Major and Minor Version numbers */
     30 /* !!CAUTION!! these needs to be updated periodically.Major and Minor version
     31    numbers should be compatible to the version number of currently implemented
     32    mapping document. Example : NFC Device version Number : 1.0 , specifies Major
     33    VNo is 1, Minor VNo is 0 */
     34 #define PH_NFCFRI_NDEFMAP_NFCDEV_MAJOR_VER_NUM 0x01
     35 #define PH_NFCFRI_NDEFMAP_NFCDEV_MINOR_VER_NUM 0x00
     36 #define PH_NFCFRI_MFSTDMAP_NFCDEV_MAJOR_VER_NUM 0x40
     37 #define PH_NFCFRI_MFSTDMAP_NFCDEV_MINOR_VER_NUM 0x00
     38 
     39 /* Macros to find major and minor TAG : Ex:Type1/Type2/Type3/Type4 version
     40  * numbers */
     41 #define PH_NFCFRI_MFSTDMAP_GET_MAJOR_TAG_VERNO(a) \
     42   ((a) & (0x40)) /* must be 0xC0 */
     43 #define PH_NFCFRI_MFSTDMAP_GET_MINOR_TAG_VERNO(a) ((a) & (0x30))
     44 
     45 /* Macros to find major and minor TAG : Ex:Type1/Type2/Type3/Type4 version
     46  * numbers */
     47 #define PH_NFCFRI_NDEFMAP_GET_MAJOR_TAG_VERNO(a) (((a) & (0xf0)) >> (4))
     48 #define PH_NFCFRI_NDEFMAP_GET_MINOR_TAG_VERNO(a) ((a) & (0x0f))
     49 
     50 /* NDEF Mapping - states of the Finite State machine */
     51 #define PH_FRINFC_NDEFMAP_STATE_INIT 0  /* Init state. The start-up state */
     52 #define PH_FRINFC_NDEFMAP_STATE_READ 1  /* Read State */
     53 #define PH_FRINFC_NDEFMAP_STATE_WRITE 2 /* Write is going on*/
     54 #define PH_FRINFC_NDEFMAP_STATE_AUTH 3  /* Authenticate is going on*/
     55 #define PH_FRINFC_NDEFMAP_STATE_CHK_NDEF_COMP 4 /* Check Ndef is going on */
     56 #define PH_FRINFC_NDEFMAP_STATE_RD_ACS_BIT \
     57   5 /* Read access bit is in progress */
     58 #define PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN 6 /* Write NDEF TLV LEngth*/
     59 #define PH_FRINFC_NDEFMAP_STATE_RD_TO_WR_NDEF_LEN \
     60   7 /* read to write the Ndef TLV*/
     61 #define PH_FRINFC_NDEFMAP_STATE_GET_ACT_CARDSIZE 8 /* Get the card size */
     62 #define PH_FRINFC_NDEFMAP_STATE_RD_BEF_WR \
     63   9 /* Read the NDEF TLV block before starting write */
     64 #define PH_FRINFC_NDEFMAP_STATE_WR_TLV \
     65   10 /* Read the NDEF TLV block before starting write */
     66 #define PH_FRINFC_NDEFMAP_STATE_RD_TLV 11     /* Read the NDEF TLV block */
     67 #define PH_FRINFC_NDEFMAP_STATE_TERM_TLV 12   /* Write terminator TLV block */
     68 #define PH_FRINFC_NDEFMAP_STATE_POLL 13       /* Poll in progress */
     69 #define PH_FRINFC_NDEFMAP_STATE_DISCONNECT 14 /* Disconnect in progress */
     70 #define PH_FRINFC_NDEFMAP_STATE_CONNECT 15    /* Connect in progress */
     71 
     72 #define PH_FRINFC_NDEFMAP_STATE_RD_SEC_ACS_BIT \
     73   16 /* Convert to ReadOnly in progress */
     74 #define PH_FRINFC_NDEFMAP_STATE_WRITE_SEC \
     75   17 /* Convert to ReadOnly in progress */
     76 
     77 /* Mifare Standard - NDEF Compliant Flags */
     78 #define PH_FRINFC_MIFARESTD_NDEF_COMP 0     /* Sector is NDEF Compliant */
     79 #define PH_FRINFC_MIFARESTD_NON_NDEF_COMP 1 /* Sector is not NDEF Compliant */
     80 
     81 /*  Mifare Standard - NDEF Compliant Flag */
     82 #define PH_FRINFC_MIFARESTD_PROP_1ST_CONFIG \
     83   0 /* No proprietary forum sector found */
     84 #define PH_FRINFC_MIFARESTD_PROP_2ND_CONFIG \
     85   1 /* Here the proprietary                 \
     86        forum sector exists after NFC forum  \
     87        sector */
     88 #define PH_FRINFC_MIFARESTD_PROP_3RD_CONFIG            \
     89   2 /* Here the proprietary forum sector exists before \
     90        NFC forum sector */
     91 
     92 /* Mifare Standard - NDEF Compliant Flags */
     93 #define PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE6 \
     94   0x78 /* Access Bit for Byte 6 in            \
     95           MAD sector trailer */
     96 #define PH_FRINFC_MIFARESTD_MADSECT_ACS_BYTE7 \
     97   0x77 /* Access Bit for Byte 7 in            \
     98           MAD sector trailer */
     99 #define PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE6 \
    100   0x7F /* Access Bit for Byte 6 in            \
    101           NFC forum sector trailer */
    102 #define PH_FRINFC_MIFARESTD_NFCSECT_ACS_BYTE7 \
    103   0x07 /* Access Bit for Byte 7 in            \
    104           NFC forum sector trailer */
    105 #define PH_FRINFC_MIFARESTD_ACS_BYTE8 \
    106   0x88 /* Access Bit for Byte 8 in    \
    107           all sector trailer */
    108 #define PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE6 \
    109   0x0F /* Access Bit for Byte 6 in              \
    110           NFC forum sector trailer for          \
    111           Read Only State */
    112 #define PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE7 \
    113   0x07 /* Access Bit for Byte 7 in              \
    114           NFC forum sector trailer              \
    115           Read Only State */
    116 #define PH_FRINFC_MIFARESTD_NFCSECT_RDACS_BYTE8                     \
    117   0x8F                                  /* Access Bit for Byte 8 in \
    118                                            NFC forum sector trailer \
    119                                            Read Only State */
    120                                         /* Mifare Standard constants */
    121 #define MIFARE_MAX_SEND_BUF_TO_READ 1   /* Send Length for Reading a Block */
    122 #define MIFARE_MAX_SEND_BUF_TO_WRITE 17 /* Send Length for writing a Block */
    123 #define MIFARE_AUTHENTICATE_CMD_LENGTH \
    124   7 /* Send Length for authenticating a Block */
    125 
    126 /* Mifare standard - Constants */
    127 #define PH_FRINFC_MIFARESTD_MAD_BLK0 0     /* Block number 0 */
    128 #define PH_FRINFC_MIFARESTD_MAD_BLK1 1     /* Block number 1 */
    129 #define PH_FRINFC_MIFARESTD_MAD_BLK2 2     /* Block number 2 */
    130 #define PH_FRINFC_MIFARESTD_MAD_BLK3 3     /* Block number 3 */
    131 #define PH_FRINFC_MIFARESTD_BLK4 4         /* Block number 4 */
    132 #define PH_FRINFC_MIFARESTD_BLK5 5         /* Block number 5 */
    133 #define PH_FRINFC_MIFARESTD_BLK6 6         /* Block number 6 */
    134 #define PH_FRINFC_MIFARESTD_BLK7 7         /* Block number 7 */
    135 #define PH_FRINFC_MIFARESTD_BLK8 8         /* Block number 8 */
    136 #define PH_FRINFC_MIFARESTD_BLK9 9         /* Block number 9 */
    137 #define PH_FRINFC_MIFARESTD_BLK10 10       /* Block number 10 */
    138 #define PH_FRINFC_MIFARESTD_BLK11 11       /* Block number 11 */
    139 #define PH_FRINFC_MIFARESTD_BLK12 12       /* Block number 12 */
    140 #define PH_FRINFC_MIFARESTD_BLK13 13       /* Block number 13 */
    141 #define PH_FRINFC_MIFARESTD_BLK14 14       /* Block number 14 */
    142 #define PH_FRINFC_MIFARESTD_BLK15 15       /* Block number 15 */
    143 #define PH_FRINFC_MIFARESTD_MAD_BLK16 16   /* Block number 16 */
    144 #define PH_FRINFC_MIFARESTD_MAD_BLK63 63   /* Block number 63 */
    145 #define PH_FRINFC_MIFARESTD_MAD_BLK64 64   /* Block number 64 */
    146 #define PH_FRINFC_MIFARESTD_MAD_BLK65 65   /* Block number 65 */
    147 #define PH_FRINFC_MIFARESTD_MAD_BLK66 66   /* Block number 66 */
    148 #define PH_FRINFC_MIFARESTD_MAD_BLK67 67   /* Block number 67 */
    149 #define PH_FRINFC_MIFARESTD4K_BLK128 128   /* Block number 128 for Mifare 4k */
    150 #define PH_FRINFC_MIFARESTD_SECTOR_NO0 0   /* Sector 0 */
    151 #define PH_FRINFC_MIFARESTD_SECTOR_NO1 1   /* Sector 1 */
    152 #define PH_FRINFC_MIFARESTD_SECTOR_NO16 16 /* Sector 16 */
    153 #define PH_FRINFC_MIFARESTD_SECTOR_NO31 31 /* Sector 31 */
    154 #define PH_FRINFC_MIFARESTD_SECTOR_NO39 39 /* Sector 39 */
    155 #define PH_FRINFC_MIFARESTD_SECTOR_NO32 32 /* Sector 32 */
    156 #define PH_FRINFC_MIFARESTD4K_TOTAL_SECTOR 40 /* Sector 40 */
    157 #define PH_FRINFC_MIFARESTD1K_TOTAL_SECTOR 16 /* Sector 16 */
    158 #define PH_FRINFC_MIFARESTD2K_TOTAL_SECTOR 32 /* Sector 32 */
    159 #define PH_FRINFC_MIFARESTD_BYTES_READ 16     /* Bytes read */
    160 #define PH_FRINFC_MIFARESTD_BLOCK_BYTES 16    /* Bytes per block */
    161 #define PH_FRINFC_MIFARESTD_SECTOR_BLOCKS 16  /* Blocks per sector */
    162 #define PH_FRINFC_MIFARESTD_WR_A_BLK       \
    163   17 /* 17 bytes (including current block) \
    164         are given to transfer */
    165 #define PH_FRINFC_MIFARESTD4K_MAX_BLOCKS    \
    166   210 /* Maximum number of Mifare 4k Blocks \
    167         excluding sector trailer */
    168 #define PH_FRINFC_MIFARESTD1K_MAX_BLK      \
    169   63 /* Maximum number of Mifare 1k blocks \
    170         including the sector trailer*/
    171 #define PH_FRINFC_MIFARESTD2K_MAX_BLK       \
    172   127 /* Maximum number of Mifare 2k blocks \
    173         including the sector trailer*/
    174 #define PH_FRINFC_MIFARESTD4K_MAX_BLK                                      \
    175   254                                /* Maximum number of Mifare 4k blocks \
    176                                        including the sector trailer*/
    177 #define PH_FRINFC_MIFARESTD_FLAG1 1  /* Flag to set 1 */
    178 #define PH_FRINFC_MIFARESTD_FLAG0 0  /* Flag to set 0 */
    179 #define PH_FRINFC_MIFARESTD_INC_1 1  /* increment by 1 */
    180 #define PH_FRINFC_MIFARESTD_INC_2 2  /* increment by 2 */
    181 #define PH_FRINFC_MIFARESTD_INC_3 3  /* increment by 3 */
    182 #define PH_FRINFC_MIFARESTD_INC_4 4  /* increment by 4 */
    183 #define PH_FRINFC_MIFARESTD_VAL0 0   /* Value initialised to 0 */
    184 #define PH_FRINFC_MIFARESTD_VAL1 1   /* Value initialised to 1 */
    185 #define PH_FRINFC_MIFARESTD_VAL2 2   /* Value initialised to 2 */
    186 #define PH_FRINFC_MIFARESTD_VAL3 3   /* Value initialised to 3 */
    187 #define PH_FRINFC_MIFARESTD_VAL4 4   /* Value initialised to 4 */
    188 #define PH_FRINFC_MIFARESTD_VAL5 5   /* Value initialised to 5 */
    189 #define PH_FRINFC_MIFARESTD_VAL6 6   /* Value initialised to 6 */
    190 #define PH_FRINFC_MIFARESTD_VAL7 7   /* Value initialised to 7 */
    191 #define PH_FRINFC_MIFARESTD_VAL8 8   /* Value initialised to 8 */
    192 #define PH_FRINFC_MIFARESTD_VAL9 9   /* Value initialised to 9 */
    193 #define PH_FRINFC_MIFARESTD_VAL10 10 /* Value initialised to 10 */
    194 #define PH_FRINFC_MIFARESTD_VAL11 11 /* Value initialised to 11 */
    195 #define PH_FRINFC_MIFARESTD_VAL12 12 /* Value initialised to 12 */
    196 #define PH_FRINFC_MIFARESTD_VAL13 13 /* Value initialised to 13 */
    197 #define PH_FRINFC_MIFARESTD_VAL14 14 /* Value initialised to 14 */
    198 #define PH_FRINFC_MIFARESTD_VAL15 15 /* Value initialised to 15 */
    199 #define PH_FRINFC_MIFARESTD_VAL16 16 /* Value initialised to 16 */
    200 #define PH_FRINFC_MIFARESTD_VAL31 31 /* Value initialised to 31 */
    201 #define PH_FRINFC_MIFARESTD_VAL32 32 /* Value initialised to 32 */
    202 #define PH_FRINFC_MIFARESTD_NDEFTLV_L 0xFF  /* Length of the TLV */
    203 #define PH_FRINFC_MIFARESTD_NDEFTLV_T 0x03  /* Length of the TLV */
    204 #define PH_FRINFC_MIFARESTD_NDEFTLV_L0 0x00 /* Length of the TLV */
    205 #define PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES0 \
    206   0 /* Number of bytes taken by length (L) of the TLV */
    207 #define PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES1 \
    208   1 /* Number of bytes taken by length (L) of the TLV */
    209 #define PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES2 \
    210   2 /* Number of bytes taken by length (L) of the TLV */
    211 #define PH_FRINFC_MIFARESTD_NDEFTLV_LBYTES3 \
    212   3 /* Number of bytes taken by length (L) of the TLV */
    213 #define PH_FRINFC_MIFARESTD_PROPTLV_T 0xFD     /* Type of Proprietary TLV */
    214 #define PH_FRINFC_MIFARESTD_TERMTLV_T 0xFE     /* Type of Terminator TLV */
    215 #define PH_FRINFC_MIFARESTD_NULLTLV_T 0x00     /* Type of NULL TLV */
    216 #define PH_FRINFC_MIFARESTD_LEFTSHIFT8 8       /* Left shift by 8 */
    217 #define PH_FRINFC_MIFARESTD_RIGHTSHIFT8 8      /* Right shift by 8 */
    218 #define PH_FRINFC_MIFARESTD_MASK_FF 0xFF       /* Mask 0xFF */
    219 #define PH_FRINFC_MIFARESTD_MASK_GPB_WR 0x03   /* Mask 0x03 for GPB byte */
    220 #define PH_FRINFC_MIFARESTD_MASK_GPB_RD 0x0C   /* Mask 0xOC for GPB byte */
    221 #define PH_FRINFC_MIFARESTD_GPB_RD_WR_VAL 0x00 /* GPB Read Write value */
    222 #define PH_FRINFC_MIFARESTD_KEY_LEN 0x06       /* MIFARE Std key length */
    223 #define PH_FRINFC_MIFARESTD_DEFAULT_KEY 0xFF   /* MIFARE Std Default Key */
    224 
    225 NFCSTATUS phFriNfc_MifareStdMap_H_Reset(phFriNfc_NdefMap_t* NdefMap);
    226 NFCSTATUS phFriNfc_MifareStdMap_RdNdef(phFriNfc_NdefMap_t* NdefMap,
    227                                        uint8_t* PacketData,
    228                                        uint32_t* PacketDataLength,
    229                                        uint8_t Offset);
    230 NFCSTATUS phFriNfc_MifareStdMap_WrNdef(phFriNfc_NdefMap_t* NdefMap,
    231                                        uint8_t* PacketData,
    232                                        uint32_t* PacketDataLength,
    233                                        uint8_t Offset);
    234 NFCSTATUS phFriNfc_MifareStdMap_ChkNdef(phFriNfc_NdefMap_t* NdefMap);
    235 
    236 void phFriNfc_MifareStdMap_Process(void* Context, NFCSTATUS Status);
    237 extern NFCSTATUS phFrinfc_MifareClassic_GetContainerSize(
    238     const phFriNfc_NdefMap_t* NdefMap, uint32_t* maxSize, uint32_t* actualSize);
    239 
    240 NFCSTATUS
    241 phFriNfc_MifareStdMap_ConvertToReadOnly(phFriNfc_NdefMap_t* NdefMap,
    242                                         const uint8_t* ScrtKeyB);
    243 
    244 #endif /* PHFRINFC_MIFARESTDMAP_H */
    245