PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/nci/jni/extns/pn54x/src/mifare/phFriNfc_MifStdFormat.h

https://gitlab.com/Atomic-ROM/android_vendor_nxp-nfc_opensource_Nfc
C Header | 340 lines | 230 code | 32 blank | 78 comment | 19 complexity | 9aa3ba999b3858cc28473e2894d54e84 MD5 | raw file
  1. /*
  2. * Copyright (C) 2015 NXP Semiconductors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Mifare Standard Format implementation
  18. */
  19. #ifndef PHFRINFC_MIFSTDFORMAT_H
  20. #define PHFRINFC_MIFSTDFORMAT_H
  21. #include <phFriNfc.h>
  22. #include <phNfcStatus.h>
  23. #include <phNfcTypes.h>
  24. #include <phFriNfc_SmtCrdFmt.h>
  25. /********************* Definitions and structures *****************************/
  26. /*
  27. * Mifare standard -progress states
  28. */
  29. #define PH_FRINFC_MFSTD_FMT_RESET_INIT 0 /* Reset state */
  30. #define PH_FRINFC_MFSTD_FMT_AUTH_SECT 1 /* Sector authentication is in progress */
  31. #define PH_FRINFC_MFSTD_FMT_DIS_CON 2 /* Disconnect is in progress */
  32. #define PH_FRINFC_MFSTD_FMT_CON 3 /* Connect is in progress */
  33. #define PH_FRINFC_MFSTD_FMT_POLL 4 /* Poll is in progress */
  34. #define PH_FRINFC_MFSTD_FMT_RD_SECT_TR 5 /* Read sector trailer is in progress */
  35. #define PH_FRINFC_MFSTD_FMT_WR_SECT_TR 6 /* Write sector trailer is in progress */
  36. #define PH_FRINFC_MFSTD_FMT_WR_TLV 7 /* Write sector trailer is in progress */
  37. #define PH_FRINFC_MFSTD_FMT_WR_MAD_BLK 8 /* Write MAD is in progress */
  38. #define PH_FRINFC_MFSTD_FMT_UPD_MAD_BLK 9 /* Write MAD is in progress */
  39. /*
  40. * Mifare standard -Authenticate states
  41. */
  42. #define PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY 0 /* Trying to authenticate with the default key */
  43. #define PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY 1 /* Trying to authenticate with the MAD key */
  44. #define PH_FRINFC_MFSTD_FMT_AUTH_MAD_KEY 2 /* Trying to authenticate with the NFC forum key */
  45. #define PH_FRINFC_MFSTD_FMT_AUTH_KEYB 3 /* Trying to authenticate with key B */
  46. #define PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB 4 /* Trying to authenticate with secret key B */
  47. /*
  48. * Mifare standard - Update MAD block flag
  49. */
  50. #define PH_FRINFC_MFSTD_FMT_NOT_A_MAD_BLK 0 /* Not a MAD block */
  51. #define PH_FRINFC_MFSTD_FMT_MAD_BLK_1 1 /* MAD block number 1 */
  52. #define PH_FRINFC_MFSTD_FMT_MAD_BLK_2 2 /* MAD block number 2 */
  53. #define PH_FRINFC_MFSTD_FMT_MAD_BLK_64 64 /* MAD block number 64 (only used for Mifare 4k card) */
  54. #define PH_FRINFC_MFSTD_FMT_MAD_BLK_65 65 /* MAD block number 65 (only used for Mifare 4k card) */
  55. #define PH_FRINFC_MFSTD_FMT_MAD_BLK_66 66 /* MAD block number 66 (only used for Mifare 4k card) */
  56. /*
  57. * Mifare standard - Update MAD block flag
  58. */
  59. #define PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_1K {0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0x78,0x77,0x88,0xC1}
  60. #define PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_2K {0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0x78,0x77,0x88,0xC1}
  61. #define PH_FRINFC_SMTCRDFMT_MSTD_MADSECT_KEYA_ACS_BIT_4K {0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0x78,0x77,0x88,0xC2}
  62. #define PH_FRINFC_SMTCRDFMT_NFCFORUMSECT_KEYA_ACS_BIT {0xD3,0xF7,0xD3,0xF7,0xD3,0xF7,0x7F,0x07,0x88,0x40}
  63. /*
  64. * Mifare standard - Key and access bit constants
  65. */
  66. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_KEYA0 0xD3 /* NFC forum sector key A */
  67. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_KEYA1 0xF7 /* NFC forum sector key A */
  68. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_KEYA0 0xA0 /* MAD sector key A */
  69. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_KEYA1 0xA1 /* MAD sector key A */
  70. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_KEYA2 0xA2 /* MAD sector key A */
  71. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_KEYA3 0xA3 /* MAD sector key A */
  72. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_KEYA4 0xA4 /* MAD sector key A */
  73. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_KEYA5 0xA5 /* MAD sector key A */
  74. #define PH_FRINFC_MFSTD_FMT_DEFAULT_KEY 0xFF /* Default key A or B */
  75. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_ACS6 0x78 /* MAD sector access bits 6 */
  76. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_ACS7 0x77 /* MAD sector access bits 7 */
  77. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_ACS8 0x88 /* MAD sector access bits 8 */
  78. #define PH_FRINFC_MFSTD_FMT_MAD_SECT_GPB 0xC1 /* MAD sector GPB */
  79. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_ACS_RW6 0x7F /* NFC forum sector access bits 6 for read write */
  80. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_ACS_RW7 0x07 /* NFC forum sector access bits 7 for read write */
  81. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_ACS_RW8 0x88 /* NFC forum sector access bits 8 for read write */
  82. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_GPB_RW 0x40 /* NFC forum sector GPB for read write */
  83. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_ACS_RO6 0x07 /* NFC forum sector access bits 6 for read only */
  84. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_ACS_RO7 0x8F /* NFC forum sector access bits 7 for read only */
  85. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_ACS_RO8 0x0F /* NFC forum sector access bits 8 for read only */
  86. #define PH_FRINFC_MFSTD_FMT_NFC_SECT_GPB_R0 0x43 /* NFC forum sector GPB for read only */
  87. /*
  88. * Enum definition contains Mifare standard values
  89. */
  90. typedef enum{
  91. PH_FRINFC_MFSTD_FMT_VAL_0,
  92. PH_FRINFC_MFSTD_FMT_VAL_1,
  93. PH_FRINFC_MFSTD_FMT_VAL_2,
  94. PH_FRINFC_MFSTD_FMT_VAL_3,
  95. PH_FRINFC_MFSTD_FMT_VAL_4,
  96. PH_FRINFC_MFSTD_FMT_VAL_5,
  97. PH_FRINFC_MFSTD_FMT_VAL_6,
  98. PH_FRINFC_MFSTD_FMT_VAL_7,
  99. PH_FRINFC_MFSTD_FMT_VAL_8,
  100. PH_FRINFC_MFSTD_FMT_VAL_9,
  101. PH_FRINFC_MFSTD_FMT_VAL_10,
  102. PH_FRINFC_MFSTD_FMT_VAL_11
  103. }phFriNfc_MfStdVal;
  104. /*
  105. * Mifare standard - NDEF information constants
  106. */
  107. #define PH_FRINFC_MFSTD_FMT_NON_NDEF_COMPL 0 /* Sector is not ndef compliant */
  108. #define PH_FRINFC_MFSTD_FMT_NDEF_COMPL 1 /* Sector is ndef compliant */
  109. #define PH_FRINFC_MFSTD_FMT_NDEF_INFO1 0x03 /* If sector is ndef compliant, then one of the MAD
  110. sector byte is 0x03 */
  111. #define PH_FRINFC_MFSTD_FMT_NDEF_INFO2 0xE1 /* If sector is ndef compliant, then one of the MAD
  112. sector byte is 0xE1 */
  113. /*
  114. * Mifare standard - constants
  115. */
  116. #define PH_FRINFC_MFSTD_FMT_MAX_RECV_LENGTH 252 /* Maximum receive length */
  117. #define PH_FRINFC_MFSTD_FMT_WR_SEND_LENGTH 17 /* Send length for write */
  118. #define PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K 16 /* Maximum sector index for Mifare 1k = 16 */
  119. #define PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_2K 32 /* Maximum sector index for Mifare 2k = 32 */
  120. #define PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K 40 /* Maximum sector index for Mifare 4k = 40 */
  121. #define PH_FRINFC_MFSTD_FMT_MAX_BLOCKS_1K 64 /* Maximum Number of Blocks for Mifare 1k = 64 */
  122. #define PH_FRINFC_MFSTD_FMT_MAX_BLOCKS_2K 128 /* Maximum Number of Blocks for Mifare 2k = 128 */
  123. #define PH_FRINFC_MFSTD_FMT_MAX_BLOCKS_4K 256 /* Maximum Number of Blocks for Mifare 4k = 256*/
  124. /*
  125. * Copy default keyA to send buffer
  126. */
  127. #define PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_DEF(mem)\
  128. do\
  129. {\
  130. memset(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],\
  131. PH_FRINFC_MFSTD_FMT_DEFAULT_KEY,\
  132. PH_FRINFC_MFSTD_FMT_VAL_6);\
  133. NdefSmtCrdFmt->Cmd.MfCmd = ((NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState == \
  134. PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY)? \
  135. phHal_eMifareAuthentA: \
  136. phHal_eMifareAuthentB); \
  137. NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_VAL_7; \
  138. }while(0)
  139. /*
  140. * NFC forum sector keyA to send buffer
  141. */
  142. #define PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_NFCSECT_KEYA(mem)\
  143. do \
  144. {\
  145. memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],\
  146. NdefSmtCrdFmt->AddInfo.MfStdInfo.NFCForumSect_KeyA,\
  147. PH_FRINFC_MFSTD_FMT_VAL_6);\
  148. NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareAuthentA;\
  149. NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_VAL_7;\
  150. } while(0)
  151. /*
  152. * Copy MAD sector keyA to send buffer
  153. */
  154. #define PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_MADSECT_KEYA(mem)\
  155. do \
  156. {\
  157. memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],\
  158. NdefSmtCrdFmt->AddInfo.MfStdInfo.MADSect_KeyA,\
  159. PH_FRINFC_MFSTD_FMT_VAL_6);\
  160. NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareAuthentA;\
  161. NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_VAL_7;\
  162. } while(0)
  163. /*
  164. * Copy MAD sector keyB to send buffer
  165. */
  166. #define PH_FRINFC_MFSTD_FMT_AUTH_SEND_BUF_SCRT_KEY(mem) \
  167. do \
  168. {\
  169. (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[PH_FRINFC_MFSTD_FMT_VAL_1],\
  170. NdefSmtCrdFmt->AddInfo.MfStdInfo.ScrtKeyB,\
  171. PH_FRINFC_MFSTD_FMT_VAL_6);\
  172. NdefSmtCrdFmt->Cmd.MfCmd = phHal_eMifareAuthentB;\
  173. NdefSmtCrdFmt->SendLength = PH_FRINFC_MFSTD_FMT_VAL_7;\
  174. } while(0)
  175. /*
  176. * Get the next block
  177. */
  178. #define PH_FRINFC_MFSTD_FMT_CUR_BLK_INC() \
  179. NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock += \
  180. ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock >= 127)?\
  181. 16:4)
  182. /*
  183. * Get the sector index
  184. */
  185. #define PH_FRINFC_MFSTD_FMT_SECT_INDEX_CALC \
  186. ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock >= 128)?\
  187. (32 + ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock - 128)/16)):\
  188. (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock/4))
  189. /*
  190. * Check the sector block
  191. */
  192. #define PH_FRINFC_MFSTD_FMT_CUR_BLK_CHK\
  193. (((NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD) && \
  194. (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock >= \
  195. PH_FRINFC_MFSTD_FMT_MAX_BLOCKS_1K)) || \
  196. ((NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD) && \
  197. (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock >= \
  198. PH_FRINFC_MFSTD_FMT_MAX_BLOCKS_4K)) || \
  199. ((NdefSmtCrdFmt->CardType == PH_FRINFC_SMTCRDFMT_MFSTD_2K_CRD) && \
  200. (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock >= \
  201. PH_FRINFC_MFSTD_FMT_MAX_BLOCKS_2K)))
  202. /*
  203. * Get the next authenticate state
  204. */
  205. #define PH_FRINFC_MFSTD_FMT_NXT_AUTH_STATE() \
  206. do \
  207. {\
  208. switch(NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState)\
  209. {\
  210. case PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY:\
  211. {\
  212. NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = (uint8_t) \
  213. ((((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock <= 3) || \
  214. ((NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock > 63) && \
  215. (NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock < 67))))? \
  216. PH_FRINFC_MFSTD_FMT_AUTH_MAD_KEY: \
  217. PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY);\
  218. }\
  219. break;\
  220. case PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY:\
  221. {\
  222. NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = \
  223. PH_FRINFC_MFSTD_FMT_AUTH_KEYB;\
  224. }\
  225. break;\
  226. case PH_FRINFC_MFSTD_FMT_AUTH_MAD_KEY:\
  227. {\
  228. NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = \
  229. PH_FRINFC_MFSTD_FMT_AUTH_NFC_KEY;\
  230. }\
  231. break;\
  232. case PH_FRINFC_MFSTD_FMT_AUTH_KEYB:\
  233. { \
  234. NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = \
  235. PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB;\
  236. } \
  237. break;\
  238. case PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB:\
  239. default:\
  240. { \
  241. NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = \
  242. PH_FRINFC_MFSTD_FMT_AUTH_DEF_KEY;\
  243. }\
  244. break;\
  245. }\
  246. } while(0)
  247. /*
  248. * Increment the sector index
  249. */
  250. #define PH_FRINFC_MFSTD_FMT_INCR_SECT \
  251. do \
  252. {\
  253. SectIndex++;\
  254. SectIndex = (uint8_t)((SectIndex == 16)?\
  255. (SectIndex + PH_FRINFC_MFSTD_FMT_VAL_1):\
  256. SectIndex);\
  257. } while(0)
  258. /*
  259. * Increment the sector index
  260. */
  261. #define PH_FRINFC_MFSTD_FMT_CHK_SECT_ARRAY \
  262. do \
  263. {\
  264. while ((index < PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_4K) && \
  265. (memcompare != PH_FRINFC_MFSTD_FMT_VAL_0))\
  266. {\
  267. /* Compare any one among the sectors is NDEF COMPLIANT */\
  268. memcompare = (uint32_t)phFriNfc_MfStd_MemCompare(&Buffer[PH_FRINFC_MFSTD_FMT_VAL_0], \
  269. &NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl[index],\
  270. PH_FRINFC_MFSTD_FMT_VAL_1);\
  271. /* increment the index */\
  272. index += (uint8_t)((index == (PH_FRINFC_MFSTD_FMT_MAX_SECT_IND_1K - \
  273. PH_FRINFC_MFSTD_FMT_VAL_1))?\
  274. PH_FRINFC_MFSTD_FMT_VAL_2:\
  275. PH_FRINFC_MFSTD_FMT_VAL_1);\
  276. }\
  277. } while(0)
  278. /*
  279. * Complete the sector
  280. */
  281. #define PH_FRINFC_MFSTD_FMT_CHK_END_OF_CARD() \
  282. do \
  283. { \
  284. phFriNfc_MfStd_H_NdefComplSect(NdefSmtCrdFmt->CardType, \
  285. NdefSmtCrdFmt->AddInfo.MfStdInfo.SectCompl); \
  286. PH_FRINFC_MFSTD_FMT_CHK_SECT_ARRAY; \
  287. if(memcompare == PH_FRINFC_MFSTD_FMT_VAL_0) \
  288. { \
  289. phFriNfc_MfStd_H_StrNdefData(NdefSmtCrdFmt); \
  290. NdefSmtCrdFmt->AddInfo.MfStdInfo.CurrentBlock = \
  291. PH_FRINFC_MFSTD_FMT_VAL_1; \
  292. NdefSmtCrdFmt->AddInfo.MfStdInfo.UpdMADBlk = \
  293. PH_FRINFC_MFSTD_FMT_MAD_BLK_1; \
  294. NdefSmtCrdFmt->AddInfo.MfStdInfo.AuthState = \
  295. PH_FRINFC_MFSTD_FMT_AUTH_SCRT_KEYB; \
  296. NdefSmtCrdFmt->State = PH_FRINFC_MFSTD_FMT_AUTH_SECT; \
  297. Result = phFriNfc_MfStd_H_WrRdAuth(NdefSmtCrdFmt); \
  298. } \
  299. else \
  300. { \
  301. Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT, \
  302. NFCSTATUS_FORMAT_ERROR); \
  303. } \
  304. } while(0)
  305. void phFriNfc_MfStd_Reset(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
  306. NFCSTATUS phFriNfc_MfStd_Format(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt, const uint8_t *ScrtKeyB);
  307. void phFriNfc_MfStd_Process(void *Context, NFCSTATUS Status);
  308. #endif /* PHFRINFC_MIFSTDFMT_H */