1 /*
2  * Copyright (C) 2010 NXP Semiconductors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*!
18 * \file  phFriNfc_DesfireFormat.h
19 * \brief Type4 Smart card formatting.
20 *
21 * Project: NFC-FRI
22 *
23 * $Date: Tue Jul 27 08:59:52 2010 $
24 * $Author: ing02260 $
25 * $Revision: 1.3 $
26 * $Aliases:  $
27 *
28 */
29 
30 #ifndef PHFRINFC_DESFIREFORMAT_H
31 #define PHFRINFC_DESFIREFORMAT_H
32 
33 
34 /*! \ingroup grp_file_attributes
35 *  \name NDEF Smart Card Foramting
36 *
37 * File: \ref phFriNfc_DesfireFormat.h
38 *
39 */
40 /*@{*/
41 
42 /*@}*/
43 
44 
45 /* Enum to represent the state variables*/
46 enum{
47 
48     PH_FRINFC_DESF_STATE_CREATE_AID = 0,
49     PH_FRINFC_DESF_STATE_SELECT_APP = 1,
50     PH_FRINFC_DESF_STATE_CREATE_CCFILE = 2,
51     PH_FRINFC_DESF_STATE_CREATE_NDEFFILE = 3,
52     PH_FRINFC_DESF_STATE_WRITE_CC_FILE = 4,
53     PH_FRINFC_DESF_STATE_WRITE_NDEF_FILE = 5,
54     PH_FRINFC_DESF_STATE_DISCON = 6,
55     PH_FRINFC_DESF_STATE_CON = 7,
56     PH_FRINFC_DESF_STATE_POLL = 8,
57     PH_FRINFC_DESF_STATE_GET_UID = 9,
58     PH_FRINFC_DESF_STATE_GET_SW_VERSION = 10,
59     PH_FRINFC_DESF_STATE_GET_HW_VERSION = 11,
60 #ifdef FRINFC_READONLY_NDEF
61 
62 #ifdef DESFIRE_FMT_EV1
63     PH_FRINFC_DESF_STATE_RO_SELECT_APP_EV1 = 100,
64 #endif /* #ifdef DESFIRE_FMT_EV1 */
65 
66     PH_FRINFC_DESF_STATE_RO_SELECT_APP = 101,
67     PH_FRINFC_DESF_STATE_RO_SELECT_CC_FILE = 102,
68     PH_FRINFC_DESF_STATE_RO_READ_CC_FILE = 103,
69     PH_FRINFC_DESF_STATE_RO_UPDATE_CC_FILE = 104,
70 
71 #endif /* #ifdef FRINFC_READONLY_NDEF */
72 
73     /* following are used in the ISO wrapper commands*/
74     PH_FRINFC_DESF_CREATEAPP_CMD = 0,
75     PH_FRINFC_DESF_SELECTAPP_CMD = 1,
76     PH_FRINFC_DESF_CREATECC_CMD = 2,
77     PH_FRINFC_DESF_CREATENDEF_CMD = 3,
78     PH_FRINFC_DESF_WRITECC_CMD = 4,
79 #ifdef FRINFC_READONLY_NDEF
80     PH_FRINFC_DESF_WRITECC_CMD_READ_ONLY = 20,
81 #endif /* #ifdef FRINFC_READONLY_NDEF */
82     PH_FRINFC_DESF_WRITENDEF_CMD = 5,
83     PH_FRINFC_DESF_GET_HW_VERSION_CMD = 6,
84     PH_FRINFC_DESF_GET_SW_VERSION_CMD = 7,
85     PH_FRINFC_DESF_GET_UID_CMD = 8,
86     PH_FRINFC_DESF_WRITENDEF_CMD_SNLEN = 15,
87     PH_FRINFC_DESF_WRITECC_CMD_SNLEN = 28,
88     PH_FRINFC_DESF_CREATECCNDEF_CMD_SNLEN = 13,
89     PH_FRINFC_DESF_SELECTAPP_CMD_SNLEN = 9,
90     PH_FRINFC_DESF_CREATEAPP_CMD_SNLEN = 11,
91     PH_FRINFC_DESF_NATIVE_OFFSET_P1 = 0x00,
92     PH_FRINFC_DESF_NATIVE_OFFSET_P2 = 0x00,
93     PH_FRINFC_DESF_NATIVE_LE_BYTE = 0x00,
94     PH_FRINFC_DESF_NATIVE_CRAPP_WRDT_LEN = 5,
95     PH_FRINFC_DESF_NATIVE_SLAPP_WRDT_LEN = 3,
96     PH_FRINFC_DESF_NATIVE_CRCCNDEF_WRDT_LEN = 7,
97     PH_FRINFC_DESF_NATIVE_WRCC_WRDT_LEN = 22,
98     PH_FRINFC_DESF_NATIVE_WRNDEF_WRDT_LEN = 9
99 
100 };
101 
102 
103 /* CC File contents*/
104 
105 #define  PH_FRINFC_DESF_CCFILE_BYTES                    {0x00,0x0f,0x10,0x00,0x3B,0x00,0x34,0x04,0x06,0xE1,0x04,0x04,0x00,0x00,0x00 }
106 #define  PH_FRINFC_DESF_NDEFFILE_BYTES                  {0x00,0x00}
107 #define  PH_FRINFC_DESF_PICC_MASTER_KEY                 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
108 #define  PH_FRINFC_DESF_NFCFORUM_APP_KEY                {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
109 #define  PH_FRINFC_DESF_COMM_SETTINGS                   0x00
110 #define  PH_FRINFC_DESF_CREATE_DATA_FILE_CMD            0xCD
111 #define  PH_FRINFC_DESF_NATIVE_CLASS_BYTE               0x90
112 
113 /* Constant defined to specify the NFC Forum Application ID : 0xEEEE10*/
114 /* This is defined in order to support to N/W Byte order style : LSB : : MSB*/
115 #define PH_FRINFC_DESF_FIRST_AID_BYTE                   0x10
116 #define PH_FRINFC_DESF_SEC_AID_BYTE                     0xEE
117 #define PH_FRINFC_DESF_THIRD_AID_BYTE                   0xEE
118 
119 
120 /* Create File command constants*/
121 #define  PH_FRINFC_DESF_CREATE_AID_CMD                  0xCA
122 
123 /* Specifies the NFC Forum App Number of Keys*/
124 #define  PH_FRINFC_DESF_NFCFORUM_APP_NO_OF_KEYS         0x01
125 
126 #define  PH_FRINFC_DESF_SLECT_APP_CMD                   0x5A
127 
128 #define  PH_FRINFC_DESF_GET_VER_CMD                     0x60
129 
130 
131 #define  PH_FRINFC_DESF_NATIVE_RESP_BYTE1               0x91
132 #define  PH_FRINFC_DESF_NATIVE_RESP_BYTE2               0x00
133 
134 /* Create CC File Commands*/
135 #define  PH_FRINFC_DESF_CC_FILE_ID                      0x03
136 #define  PH_FRINFC_DESF_CC_FILE_SIZE                    0x0F
137 #define  PH_FRINFC_DESF_FIRST_BYTE_CC_ACCESS_RIGHTS     0x00
138 #define  PH_FRINFC_DESF_SEC_BYTE_CC_ACCESS_RIGHTS       0xE0
139 
140 
141 /* Create NDEF File Commands*/
142 #define  PH_FRINFC_DESF_NDEF_FILE_ID                    0x04
143 #define  PH_FRINFC_DESF_NDEF_FILE_SIZE                  0x04
144 #define  PH_FRINFC_DESF_FIRST_BYTE_NDEF_ACCESS_RIGHTS   0xE0
145 #define  PH_FRINFC_DESF_SEC_BYTE_NDEF_ACCESS_RIGHTS     0xEE
146 
147 
148 /* Write/Read Data commands/constants*/
149 #define  PH_FRINFC_DESF_WRITE_CMD                       0x3D
150 
151 /* PICC additional frame response*/
152 #define  PH_FRINFC_DESF_PICC_ADDI_FRAME_RESP            0xAF
153 
154 /* Response for PICC native DESFire wrapper cmd*/
155 #define  PH_FRINFC_DESF_NAT_WRAP_FIRST_RESP_BYTE        0x91
156 #define  PH_FRINFC_DESF_NAT_WRAP_SEC_RESP_BYTE          0x00
157 
158 /* DESFire4 Major/Minor versions*/
159 #define  PH_FRINFC_DESF4_MAJOR_VERSION                  0x00
160 #define  PH_FRINFC_DESF4_MINOR_VERSION                  0x06
161 
162 /* DESFire4 memory size*/
163 #define  PH_FRINFC_DESF4_MEMORY_SIZE                    0xEDE
164 
165 enum{
166     PH_SMTCRDFMT_DESF_VAL0 = 0,
167     PH_SMTCRDFMT_DESF_VAL1 = 1,
168     PH_SMTCRDFMT_DESF_VAL2 = 2,
169     PH_SMTCRDFMT_DESF_VAL3 = 3,
170     PH_SMTCRDFMT_DESF_VAL4 = 4,
171     PH_SMTCRDFMT_DESF_VAL14 = 14,
172     PH_SMTCRDFMT_DESF_VAL15 = 15
173 };
174 
175 
176 
177 /*!
178 * \brief \copydoc page_reg Resets the component instance to the initial state and lets the component forget about
179 *        the list of registered items. Moreover, the lower device is set.
180 *
181 * \param[in] NdefSmtCrdFmt Pointer to a valid or uninitialized instance of \ref phFriNfc_sNdefSmtCrdFmt_t.
182 *
183 * \note  This function has to be called at the beginning, after creating an instance of
184 *        \ref phFriNfc_sNdefSmtCrdFmt_t. Use this function to reset the instance of smart card
185 formatting context variables.
186 */
187 void phFriNfc_Desfire_Reset(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
188 
189 /*!
190 * \ingroup grp_fri_smart_card_formatting
191 *
192 * \brief Initiates the card formatting procedure for Remote Smart Card Type.
193 *
194 * \copydoc page_ovr The function initiates and formats the DESFire Card.After this
195 *                   operation,remote card would be properly initialized and
196 *                   Ndef Compliant.Depending upon the different card type, this
197 *                   function handles formatting procedure.This function also handles
198 *                   the different recovery procedures for different types of the cards.
199 *                   For both Format and Recovery Management same API is used.
200 *
201 * \param[in] phFriNfc_sNdefSmartCardFmt_t Pointer to a valid instance of the \ref phFriNfc_sNdefSmartCardFmt_t
202 *                             structure describing the component context.
203 *
204 * \retval NFCSTATUS_SUCCESS                  Card formatting has been successfully completed.
205 * \retval NFCSTATUS_PENDING                  The action has been successfully triggered.
206 * \retval NFCSTATUS_FORMAT_ERROR             Error occured during the formatting procedure.
207 * \retval NFCSTATUS_INVALID_REMOTE_DEVICE    Card Type is unsupported.
208 * \retval NFCSTATUS_INVALID_DEVICE_REQUEST   Command or Operation types are mismatching.
209 *
210 */
211 NFCSTATUS phFriNfc_Desfire_Format(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
212 
213 /*!
214 * \brief \copydoc page_reg Resets the component instance to the initial state and lets the component forget about
215 *        the list of registered items. Moreover, the lower device is set.
216 *
217 * \param[in] NdefSmtCrdFmt Pointer to a valid or uninitialized instance of \ref phFriNfc_sNdefSmtCrdFmt_t.
218 *
219 * \note  This function has to be called at the beginning, after creating an instance of
220 *        \ref phFriNfc_sNdefSmtCrdFmt_t. Use this function to reset the instance of smart card
221 formatting context variables.
222 */
223 void phFriNfc_Desfire_Reset(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
224 
225 #ifdef FRINFC_READONLY_NDEF
226 /*!
227  * \ingroup grp_fri_smart_card_formatting
228  *
229  * \brief Initiates the conversion of the already NDEF formatted tag to READ ONLY.
230  *
231  * \copydoc page_ovr  The function initiates the conversion of the already NDEF formatted
232  * tag to READ ONLY. After this formation, remote card would be properly Ndef Compliant and READ ONLY.
233  * Depending upon the different card type, this function handles formatting procedure.
234  *
235  * \param[in] phFriNfc_sNdefSmartCardFmt_t Pointer to a valid instance of the \ref phFriNfc_sNdefSmartCardFmt_t
236  *                             structure describing the component context.
237  *
238  * \retval NFCSTATUS_SUCCESS                  Card formatting has been successfully completed.
239  * \retval NFCSTATUS_PENDING                  The action has been successfully triggered.
240  * \retval NFCSTATUS_FORMAT_ERROR             Error occured during the formatting procedure.
241  * \retval NFCSTATUS_INVALID_REMOTE_DEVICE    Card Type is unsupported.
242  * \retval NFCSTATUS_INVALID_DEVICE_REQUEST   Command or Operation types are mismatching.
243  *
244  */
245 NFCSTATUS
246 phFriNfc_Desfire_ConvertToReadOnly (
247     phFriNfc_sNdefSmtCrdFmt_t   *NdefSmtCrdFmt);
248 #endif /* #ifdef FRINFC_READONLY_NDEF */
249 
250 /**
251 *\ingroup grp_fri_smart_card_formatting
252 *
253 * \brief Smart card Formatting \b Completion \b Routine or \b Process function
254 *
255 * \copydoc page_ovr Completion Routine: This function is called by the lower layer (OVR HAL)
256 *                  when an I/O operation has finished. The internal state machine decides
257 *                  whether to call into the lower device again or to complete the process
258 *                  by calling into the upper layer's completion routine, stored within this
259 *                  component's context (\ref phFriNfc_sNdefSmtCrdFmt_t).
260 *
261 * The function call scheme is according to \ref grp_interact. No State reset is performed during
262 * operation.
263 *
264 * \param[in] Context The context of the current (not the lower/upper) instance, as set by the lower,
265 *            calling layer, upon its completion.
266 * \param[in] Status  The completion status of the lower layer (to be handled by the implementation of
267 *                    the state machine of this function like a regular return value of an internally
268 *                    called function).
269 *
270 * \note For general information about the completion routine interface please see \ref pphFriNfc_Cr_t . * The Different Status Values are as follows
271 *
272 */
273 void phFriNfc_Desf_Process(void        *Context,
274                            NFCSTATUS   Status);
275 
276 
277 #endif
278 
279