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.c
19 * \brief This component encapsulates different format functinalities ,
20 * for the Type4/Desfire card.
21 *
22 * Project: NFC-FRI
23 *
24 * $Date: Thu Oct 28 17:44:00 2010 $
25 * $Author: ing02260 $
26 * $Revision: 1.8 $
27 * $Aliases: $
28 *
29 */
30 #include <phNfcTypes.h>
31 #include <phFriNfc_OvrHal.h>
32 #include <phFriNfc_SmtCrdFmt.h>
33 #include <phFriNfc_DesfireFormat.h>
34
35
36 /* Following section details, how to wrap the native DESFire commands in to ISO commands
37 Following are different native commands are wrapped under the ISO commands :
38 1. Crate Application
39 2. Select File
40 3. Get version
41 4. Create CC/NDEF File
42 5. Write data to STD File
43 In this File above commands are sent using the ISO Wrapper.
44
45 Wrapping the native DESFire APDU's procedure
46 --------------------------------------------------------------------------------
47 CLA INS P1 P2 Lc Data Le
48 0x90 Cmd 0x00 0x00 Data Len Cmd. Par's 0x00
49 -----------------------------------------------------------------------------------*/
50
51 /****************************** Macro definitions start ********************************/
52 /* This settings can be changed, depending on the requirement*/
53 #define PH_FRINFC_DESF_PICC_NFC_KEY_SETTING 0x0FU
54
55 #ifdef FRINFC_READONLY_NDEF
56
57 #define READ_ONLY_NDEF_DESFIRE 0xFFU
58 #define CC_BYTES_SIZE 0x0FU
59 #define PH_FRINFC_DESF_READ_DATA_CMD 0xBDU
60 #define NATIVE_WRAPPER_READ_DATA_LC_VALUE 0x07U
61
62 #endif /* #ifdef FRINFC_READONLY_NDEF */
63
64 #ifdef DESFIRE_FMT_EV1
65
66 #define DESFIRE_CARD_TYPE_EV1 0x01U
67
68 #define DESFIRE_EV1_MAPPING_VERSION 0x20U
69
70 #define DESFIRE_EV1_HW_MAJOR_VERSION 0x01U
71 #define DESFIRE_EV1_HW_MINOR_VERSION 0x00U
72 #define DESFIRE_EV1_SW_MAJOR_VERSION 0x01U
73 #define DESFIRE_EV1_SW_MINOR_VERSION 0x03U
74
75 /* The below values are received for the command GET VERSION */
76 #define DESFIRE_TAG_SIZE_IDENTIFIER_2K 0x16U
77 #define DESFIRE_TAG_SIZE_IDENTIFIER_4K 0x18U
78 #define DESFIRE_TAG_SIZE_IDENTIFIER_8K 0x1AU
79
80 #define DESFIRE_2K_CARD 2048U
81 #define DESFIRE_4K_CARD 4096U
82 #define DESFIRE_8K_CARD 7680U
83
84 #define DESFIRE_EV1_KEY_SETTINGS_2 0x21U
85
86 #define DESFIRE_EV1_FIRST_AID_BYTE 0x01U
87 #define DESFIRE_EV1_SECOND_AID_BYTE 0x00U
88 #define DESFIRE_EV1_THIRD_AID_BYTE 0x00U
89
90 #define DESFIRE_EV1_FIRST_ISO_FILE_ID 0x05U
91 #define DESFIRE_EV1_SECOND_ISO_FILE_ID 0xE1U
92
93 #define DESFIRE_EV1_ISO_APP_DF_NAME {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01}
94
95 #define DESFIRE_EV1_CC_FILE_ID 0x01U
96 #define DESFIRE_EV1_FIRST_CC_FILE_ID_BYTE 0x03U
97 #define DESFIRE_EV1_SECOND_CC_FILE_ID_BYTE 0xE1U
98
99 #define DESFIRE_EV1_NDEF_FILE_ID 0x02U
100 #define DESFIRE_EV1_FIRST_NDEF_FILE_ID_BYTE 0x04U
101 #define DESFIRE_EV1_SECOND_NDEF_FILE_ID_BYTE 0xE1U
102
103
104 #define PH_FRINFC_DESF_STATE_REACTIVATE 0x0FU
105
106 #endif /* #ifdef DESFIRE_FMT_EV1 */
107 /****************************** Macro definitions end ********************************/
108 /* Helper functions to create app/select app/create data file/read /write from the
109 CC file and NDEF files */
110 static void phFriNfc_Desf_HWrapISONativeCmds(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt,uint8_t CmdType);
111
112 /* Gets H/W version details*/
113 static NFCSTATUS phFriNfc_Desf_HGetHWVersion(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
114
115 /* Gets S/W version details*/
116 static NFCSTATUS phFriNfc_Desf_HGetSWVersion(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
117
118 /* Updates the version details to context structure*/
119 static NFCSTATUS phFriNfc_Desf_HUpdateVersionDetails(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
120
121 /*Gets UID details*/
122 static NFCSTATUS phFriNfc_Desf_HGetUIDDetails(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt);
123
124 /*Creates Application*/
125 static NFCSTATUS phFriNfc_Desf_HCreateApp(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
126
127 /* Selects Application*/
128 static NFCSTATUS phFriNfc_Desf_HSelectApp(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
129
130 /*Creates Capability Container File*/
131 static NFCSTATUS phFriNfc_Desf_HCreatCCFile(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
132
133 /* Create NDEF File*/
134 static NFCSTATUS phFriNfc_Desf_HCreatNDEFFile(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
135
136 /* Writes CC Bytes to CC File*/
137 static NFCSTATUS phFriNfc_Desf_HWrCCBytes(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
138
139 /* Writes NDEF data into NDEF File*/
140 static NFCSTATUS phFriNfc_Desf_HWrNDEFData(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
141
142 /* Transceive Cmd initiation*/
143 static NFCSTATUS phFriNfc_Desf_HSendTransCmd(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
144
145 #ifdef FRINFC_READONLY_NDEF
146
147 #if 0
148 static
149 NFCSTATUS
150 phFriNfc_Desf_HReadOnlySelectCCFile (
151 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
152 #endif /* #if 0 */
153
154 static
155 NFCSTATUS
156 phFriNfc_Desf_HReadOnlyReadCCFile (
157 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
158
159 static
160 NFCSTATUS
161 phFriNfc_Desf_HReadOnlyWriteCCFile (
162 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
163
164 static
165 NFCSTATUS
166 phFriNfc_Desf_HReadOnlySelectApp (
167 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
168
169 #ifdef DESFIRE_FMT_EV1
170
171 static
172 NFCSTATUS
173 phFriNfc_Desf_HReadOnlySelectAppEV1 (
174 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt);
175
176 #endif /* #ifdef DESFIRE_FMT_EV1 */
177
178 #endif /* #ifdef FRINFC_READONLY_NDEF */
179
phFriNfc_Desfire_Reset(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)180 void phFriNfc_Desfire_Reset( phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
181 {
182 /* This piece of code may not be useful for the current supported DESFire formatting feature*/
183 /* Currently, s/w doesn't support authenticating either PICC Master key nor NFC Forum
184 Application Master key*/
185
186 /*NdefSmtCrdFmt->AddInfo.Type4Info.PICCMasterKey[] = PH_FRINFC_SMTCRDFMT_DESF_PICC_MASTER_KEY;
187 NdefSmtCrdFmt->AddInfo.Type4Info.NFCForumMasterkey[] = PH_FRINFC_SMTCRDFMT_DESF_NFCFORUM_APP_KEY;*/
188
189 /* reset to zero PICC and NFC FORUM master keys*/
190 (void)memset((void *)NdefSmtCrdFmt->AddInfo.Type4Info.PICCMasterKey,
191 0x00,
192 16);
193
194 (void)memset((void *)NdefSmtCrdFmt->AddInfo.Type4Info.NFCForumMasterkey,
195 0x00,
196 16);
197 NdefSmtCrdFmt->AddInfo.Type4Info.PrevState = 0;
198
199 }
200
201
phFriNfc_Desf_HWrapISONativeCmds(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt,uint8_t CmdType)202 static void phFriNfc_Desf_HWrapISONativeCmds(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt,uint8_t CmdType)
203 {
204
205 uint16_t i=0, CmdByte=1;
206 uint8_t NdefFileBytes[] = PH_FRINFC_DESF_NDEFFILE_BYTES;
207 uint8_t CCFileBytes[] = PH_FRINFC_DESF_CCFILE_BYTES;
208
209
210 /* common elements for all the native commands*/
211
212 /* Class Byte */
213 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_CLASS_BYTE;
214
215 /* let the place to store the cmd byte type, point to next index*/
216 i += 2;
217
218
219 /* P1/P2 offsets always set to zero */
220 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_OFFSET_P1;
221 i++;
222 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_OFFSET_P2;
223 i++;
224
225 switch(CmdType)
226 {
227 case PH_FRINFC_DESF_GET_HW_VERSION_CMD :
228 case PH_FRINFC_DESF_GET_SW_VERSION_CMD :
229 case PH_FRINFC_DESF_GET_UID_CMD :
230 {
231 if (CmdType == PH_FRINFC_DESF_GET_HW_VERSION_CMD )
232 {
233 /* Instruction Cmd code */
234 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_GET_VER_CMD;
235 }
236 else
237 {
238 /* Instruction Cmd code */
239 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_PICC_ADDI_FRAME_RESP;
240 }
241
242 /* Lc: Length of wrapped data */
243 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
244 i++;
245
246 /* NO Data to send in this cmd*/
247 /* we are not suppose to set Le*/
248 /* set the length of the buffer*/
249 NdefSmtCrdFmt->SendLength = i;
250
251 break;
252 }
253
254 case PH_FRINFC_DESF_CREATEAPP_CMD:
255 {
256 #ifdef DESFIRE_FMT_EV1
257 uint8_t df_name[] = DESFIRE_EV1_ISO_APP_DF_NAME;
258 #endif /* #ifdef DESFIRE_FMT_EV1 */
259
260 /* Instruction Cmd code */
261 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_CREATE_AID_CMD;
262
263 #ifdef DESFIRE_FMT_EV1
264 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
265 {
266 /* Lc: Length of wrapped data,
267 here the magic number 2 is for the ISO File ID for the application */
268 NdefSmtCrdFmt->SendRecvBuf[i] = (uint8_t)(PH_FRINFC_DESF_NATIVE_CRAPP_WRDT_LEN +
269 sizeof (df_name) + 2);
270 i++;
271 /* NFC FORUM APPLICATION ID*/
272 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_FIRST_AID_BYTE;
273 i++;
274 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_SECOND_AID_BYTE;
275 i++;
276 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_THIRD_AID_BYTE;
277 i++;
278 }
279 else
280 #endif /* #ifdef DESFIRE_FMT_EV1 */
281 {
282 /* Lc: Length of wrapped data */
283 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_CRAPP_WRDT_LEN;
284 i++;
285 /* NFC FORUM APPLICATION ID*/
286 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_FIRST_AID_BYTE;
287 i++;
288 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_SEC_AID_BYTE;
289 i++;
290 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_THIRD_AID_BYTE;
291 i++;
292 }
293 /* set key settings and number of keys*/
294 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_PICC_NFC_KEY_SETTING;
295 i++;
296 #ifdef DESFIRE_FMT_EV1
297 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
298 {
299 /* set key settings and number of keys*/
300 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_KEY_SETTINGS_2;
301 i++;
302 }
303 else
304 #endif /* #ifdef DESFIRE_FMT_EV1 */
305 {
306 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NFCFORUM_APP_NO_OF_KEYS;
307 i++;
308 }
309
310 #ifdef DESFIRE_FMT_EV1
311 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
312 {
313 /* ISO File ID */
314 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_FIRST_ISO_FILE_ID;
315 i++;
316 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_SECOND_ISO_FILE_ID;
317 i++;
318 /* DF file name */
319 (void)memcpy ((void *)&NdefSmtCrdFmt->SendRecvBuf[i],
320 (void *)df_name, sizeof (df_name));
321 i = (uint16_t)(i + sizeof (df_name));
322 }
323 #endif /* #ifdef DESFIRE_FMT_EV1 */
324
325 /* Le bytes*/
326 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
327 i++;
328
329 #ifdef DESFIRE_FMT_EV1
330 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
331 {
332 /* set the length of the buffer*/
333 NdefSmtCrdFmt->SendLength = i;
334 }
335 else
336 #endif /* #ifdef DESFIRE_FMT_EV1 */
337 {
338 NdefSmtCrdFmt->SendLength = PH_FRINFC_DESF_CREATEAPP_CMD_SNLEN;
339 }
340 break;
341 }
342
343 case PH_FRINFC_DESF_SELECTAPP_CMD:
344 {
345 /* Instruction Cmd code */
346 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_SLECT_APP_CMD;
347
348 /* Lc: Length of wrapped data */
349 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_SLAPP_WRDT_LEN;
350 i++;
351
352 #ifdef DESFIRE_FMT_EV1
353 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
354 {
355 /* Data*/
356 /* set the send buffer to create the application identifier*/
357 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_FIRST_AID_BYTE;
358 i++;
359
360 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_SECOND_AID_BYTE;
361 i++;
362
363 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_THIRD_AID_BYTE;
364 i++;
365 }
366 else
367 #endif /* #ifdef DESFIRE_FMT_EV1 */
368 {
369 /* Data*/
370 /* set the send buffer to create the application identifier*/
371 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_FIRST_AID_BYTE;
372 i++;
373
374 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_SEC_AID_BYTE;
375 i++;
376
377 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_THIRD_AID_BYTE;
378 i++;
379 }
380
381 /* Le bytes*/
382 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
383 i++;
384
385 /* set the length of the buffer*/
386 NdefSmtCrdFmt->SendLength = PH_FRINFC_DESF_SELECTAPP_CMD_SNLEN;
387 break;
388 }
389
390 case PH_FRINFC_DESF_CREATECC_CMD:
391 {
392 /* Instruction Cmd code */
393 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_CREATE_DATA_FILE_CMD;
394
395 #ifdef DESFIRE_FMT_EV1
396 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
397 {
398 /* Lc: Length of wrapped data,
399 here the magic number 2 is added as part of the ISO File ID in the packet */
400 NdefSmtCrdFmt->SendRecvBuf[i] = (uint8_t)
401 (PH_FRINFC_DESF_NATIVE_CRCCNDEF_WRDT_LEN + 2);
402 i++;
403 /* set cc file id* */
404 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_CC_FILE_ID;
405 i++;
406 /* ISO File ID */
407 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_FIRST_CC_FILE_ID_BYTE;
408 i++;
409 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_SECOND_CC_FILE_ID_BYTE;
410 i++;
411 }
412 else
413 #endif /* #ifdef DESFIRE_FMT_EV1 */
414 {
415 /* Lc: Length of wrapped data */
416 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_CRCCNDEF_WRDT_LEN;
417 i++;
418 /* set cc file id*/
419 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_ID;
420 i++;
421 }
422
423 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_COMM_SETTINGS;
424 i++;
425
426 /* set the Access Rights are set to full read/write, full cntrl*/
427 NdefSmtCrdFmt->SendRecvBuf[i] = 0xEE;
428 i++;
429 NdefSmtCrdFmt->SendRecvBuf[i] = 0xEE;
430 i++;
431
432 /* set the file size*/
433 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_SIZE;
434 i++;
435 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
436 i++;
437 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
438 i++;
439
440 /* Le bytes*/
441 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
442 i++;
443 #ifdef DESFIRE_FMT_EV1
444 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
445 {
446 /* set the length of the buffer*/
447 NdefSmtCrdFmt->SendLength = i;
448 }
449 else
450 #endif /* #ifdef DESFIRE_FMT_EV1 */
451 {
452 /* set the length of the buffer*/
453 NdefSmtCrdFmt->SendLength = PH_FRINFC_DESF_CREATECCNDEF_CMD_SNLEN;
454 }
455 break;
456 }
457
458 case PH_FRINFC_DESF_CREATENDEF_CMD:
459 {
460 /* Instruction Cmd code */
461 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_CREATE_DATA_FILE_CMD;
462
463 #ifdef DESFIRE_FMT_EV1
464 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
465 {
466 /* Lc: Length of wrapped data,
467 here the magic number 2 is added as part of the ISO File ID in the packet */
468 NdefSmtCrdFmt->SendRecvBuf[i] = (uint8_t)
469 (PH_FRINFC_DESF_NATIVE_CRCCNDEF_WRDT_LEN + 2);
470 i++;
471 /* set NDEF file id* */
472 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_NDEF_FILE_ID;
473 i++;
474 /* ISO File ID */
475 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_FIRST_NDEF_FILE_ID_BYTE;
476 i++;
477 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_SECOND_NDEF_FILE_ID_BYTE;
478 i++;
479 }
480 else
481 #endif /* #ifdef DESFIRE_FMT_EV1 */
482 {
483 /* Lc: Length of wrapped data */
484 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_CRCCNDEF_WRDT_LEN;
485 i++;
486
487 /* set NDEF file id*/
488 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NDEF_FILE_ID;
489 i++;
490 }
491 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_COMM_SETTINGS;
492 i++;
493
494 /* set the r/w access rights.Once Authentication part is fixed,
495 we will use the constants*/
496 NdefSmtCrdFmt->SendRecvBuf[i] = 0xEE;
497 i++;
498 NdefSmtCrdFmt->SendRecvBuf[i] = 0xEE;
499 i++;
500
501 NdefSmtCrdFmt->SendRecvBuf[i]= (uint8_t)NdefSmtCrdFmt->AddInfo.Type4Info.CardSize;
502 i++;
503 NdefSmtCrdFmt->SendRecvBuf[i]= (uint8_t)
504 (NdefSmtCrdFmt->AddInfo.Type4Info.CardSize >> 8);
505 i++;
506 NdefSmtCrdFmt->SendRecvBuf[i]= (uint8_t)
507 (NdefSmtCrdFmt->AddInfo.Type4Info.CardSize >> 16);
508 i++;
509
510 /* Le bytes*/
511 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
512 i++;
513
514 #ifdef DESFIRE_FMT_EV1
515 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
516 {
517 /* set the length of the buffer*/
518 NdefSmtCrdFmt->SendLength = i;
519 }
520 else
521 #endif /* #ifdef DESFIRE_FMT_EV1 */
522 {
523 /* set the length of the buffer*/
524 NdefSmtCrdFmt->SendLength = PH_FRINFC_DESF_CREATECCNDEF_CMD_SNLEN;
525 }
526 break;
527 }
528
529 case PH_FRINFC_DESF_WRITECC_CMD:
530 {
531 /* Instruction Cmd code */
532 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_WRITE_CMD;
533
534 /* Lc: Length of wrapped data */
535 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_WRCC_WRDT_LEN;
536 i++;
537
538 #ifdef DESFIRE_FMT_EV1
539 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
540 {
541 /* set the file id*/
542 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_CC_FILE_ID;
543 i++;
544 }
545 else
546 #endif /* #ifdef DESFIRE_FMT_EV1 */
547 {
548 /* set the file id*/
549 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_ID;
550 i++;
551 }
552
553 /* set the offset to zero*/
554 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
555 i++;
556 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
557 i++;
558 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
559 i++;
560
561 /* Set the length of data available to write*/
562 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_SIZE;
563 i++;
564 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
565 i++;
566 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
567 i++;
568 #ifdef DESFIRE_FMT_EV1
569 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
570 {
571 CCFileBytes[2] = (uint8_t)DESFIRE_EV1_MAPPING_VERSION;
572
573 /* Length value is updated in the CC as per the card size received from
574 the GetVersion command */
575 CCFileBytes[11] = (uint8_t)
576 (NdefSmtCrdFmt->AddInfo.Type4Info.CardSize >> 8);
577 CCFileBytes[12] = (uint8_t)
578 (NdefSmtCrdFmt->AddInfo.Type4Info.CardSize);
579 }
580 #endif /* #ifdef DESFIRE_FMT_EV1 */
581 /*set the data to be written to the CC file*/
582 (void)memcpy ((void *)&NdefSmtCrdFmt->SendRecvBuf[i],
583 (void *)CCFileBytes, sizeof (CCFileBytes));
584 #ifdef DESFIRE_FMT_EV1
585 #else
586 i++;
587 #endif /* #ifdef DESFIRE_FMT_EV1 */
588
589 i = (uint16_t)(i + sizeof (CCFileBytes));
590
591 /* Le bytes*/
592 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
593 i++;
594 #ifdef DESFIRE_FMT_EV1
595 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
596 {
597 NdefSmtCrdFmt->SendLength = i;
598 }
599 else
600 #endif /* #ifdef DESFIRE_FMT_EV1 */
601 {
602 NdefSmtCrdFmt->SendLength = PH_FRINFC_DESF_WRITECC_CMD_SNLEN;
603 }
604 break;
605 }
606
607 case PH_FRINFC_DESF_WRITENDEF_CMD:
608 {
609 /* Instruction Cmd code */
610 NdefSmtCrdFmt->SendRecvBuf[CmdByte] = PH_FRINFC_DESF_WRITE_CMD;
611
612 /* Lc: Length of wrapped data */
613 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_WRNDEF_WRDT_LEN;
614 i++;
615
616 #ifdef DESFIRE_FMT_EV1
617 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
618 {
619 /* set the file id*/
620 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_NDEF_FILE_ID;
621 i++;
622 }
623 else
624 #endif /* #ifdef DESFIRE_FMT_EV1 */
625 {
626 /* set the file id*/
627 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NDEF_FILE_ID;
628 i++;
629 }
630
631 /* set the offset to zero*/
632 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
633 i++;
634 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
635 i++;
636 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
637 i++;
638
639 /* Set the length of data available to write*/
640 NdefSmtCrdFmt->SendRecvBuf[i] = 0x02;
641 i++;
642 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
643 i++;
644 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
645 i++;
646
647 /*set the data to be written to the CC file*/
648
649 (void)memcpy(&NdefSmtCrdFmt->SendRecvBuf[i],
650 NdefFileBytes, sizeof (NdefFileBytes));
651 i = (uint16_t)(i + sizeof (NdefFileBytes));
652
653 /* Le bytes*/
654 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
655 i++;
656
657 NdefSmtCrdFmt->SendLength = PH_FRINFC_DESF_WRITENDEF_CMD_SNLEN;
658 break;
659 }
660
661 default:
662 {
663 break;
664 }
665 }
666 }
667
phFriNfc_Desf_HGetHWVersion(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)668 static NFCSTATUS phFriNfc_Desf_HGetHWVersion(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
669 {
670 NFCSTATUS status = NFCSTATUS_SUCCESS;
671 #ifdef PH_HAL4_ENABLE
672 /* Removed uint8_t i=0; */
673 #else
674 uint8_t i=0;
675 #endif /* #ifdef PH_HAL4_ENABLE */
676
677 /*set the state*/
678 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_GET_HW_VERSION;
679
680 /* Helper routine to wrap the native DESFire cmds*/
681 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_GET_HW_VERSION_CMD);
682
683 status = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
684
685 return ( status);
686 }
687
phFriNfc_Desf_HGetSWVersion(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)688 static NFCSTATUS phFriNfc_Desf_HGetSWVersion(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
689 {
690
691 NFCSTATUS status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
692 NFCSTATUS_FORMAT_ERROR);
693
694 if( ( NdefSmtCrdFmt->SendRecvBuf[*(NdefSmtCrdFmt->SendRecvLength)- 1] ==
695 PH_FRINFC_DESF_PICC_ADDI_FRAME_RESP) )
696 {
697 /*set the state*/
698 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_GET_SW_VERSION;
699
700 /* Helper routine to wrap the native DESFire cmds*/
701 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_GET_SW_VERSION_CMD);
702
703 status = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
704 }
705 return status;
706 }
707
phFriNfc_Desf_HUpdateVersionDetails(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)708 static NFCSTATUS phFriNfc_Desf_HUpdateVersionDetails(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
709 {
710 NFCSTATUS status = PHNFCSTVAL(CID_NFC_NONE,
711 NFCSTATUS_SUCCESS);
712
713 if( ( NdefSmtCrdFmt->SendRecvBuf[*(NdefSmtCrdFmt->SendRecvLength)-
714 PH_SMTCRDFMT_DESF_VAL1] == PH_FRINFC_DESF_PICC_ADDI_FRAME_RESP ) )
715 {
716 NdefSmtCrdFmt->AddInfo.Type4Info.MajorVersion = NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL3];
717 NdefSmtCrdFmt->AddInfo.Type4Info.MinorVersion = NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL4];
718
719 if ((PH_FRINFC_DESF4_MAJOR_VERSION == NdefSmtCrdFmt->AddInfo.Type4Info.MajorVersion) &&
720 (PH_FRINFC_DESF4_MINOR_VERSION == NdefSmtCrdFmt->AddInfo.Type4Info.MinorVersion))
721 {
722 /* card size of DESFire4 type */
723 NdefSmtCrdFmt->AddInfo.Type4Info.CardSize = PH_FRINFC_DESF4_MEMORY_SIZE;
724
725 }
726 #ifdef DESFIRE_FMT_EV1
727 else if ((DESFIRE_EV1_SW_MAJOR_VERSION == NdefSmtCrdFmt->AddInfo.Type4Info.MajorVersion) &&
728 (DESFIRE_EV1_SW_MINOR_VERSION == NdefSmtCrdFmt->AddInfo.Type4Info.MinorVersion))
729 {
730 NdefSmtCrdFmt->CardType = DESFIRE_CARD_TYPE_EV1;
731 }
732 #endif /* #ifdef DESFIRE_FMT_EV1 */
733 else
734 {
735 // need to handle the Desfire8 type cards
736 // need to use get free memory
737 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_SMTCRDFMT,
738 NFCSTATUS_INVALID_REMOTE_DEVICE);
739
740 }
741 #ifdef DESFIRE_FMT_EV1
742 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
743 {
744 switch (NdefSmtCrdFmt->SendRecvBuf[5])
745 {
746 case DESFIRE_TAG_SIZE_IDENTIFIER_2K:
747 {
748 NdefSmtCrdFmt->AddInfo.Type4Info.CardSize = DESFIRE_2K_CARD;
749 break;
750 }
751
752 case DESFIRE_TAG_SIZE_IDENTIFIER_4K:
753 {
754 NdefSmtCrdFmt->AddInfo.Type4Info.CardSize = DESFIRE_4K_CARD;
755 break;
756 }
757
758 case DESFIRE_TAG_SIZE_IDENTIFIER_8K:
759 {
760 NdefSmtCrdFmt->AddInfo.Type4Info.CardSize = DESFIRE_8K_CARD;
761 break;
762 }
763
764 default:
765 {
766 status = PHNFCSTVAL (CID_FRI_NFC_NDEF_SMTCRDFMT,
767 NFCSTATUS_INVALID_REMOTE_DEVICE);
768 break;
769 }
770 }
771 }
772 #endif /* #ifdef DESFIRE_FMT_EV1 */
773 }
774
775 return status;
776 }
777
phFriNfc_Desf_HGetUIDDetails(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)778 static NFCSTATUS phFriNfc_Desf_HGetUIDDetails(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)
779 {
780
781 NFCSTATUS status = NFCSTATUS_PENDING;
782 if( ( NdefSmtCrdFmt->SendRecvBuf[*(NdefSmtCrdFmt->SendRecvLength)-
783 PH_SMTCRDFMT_DESF_VAL1] == PH_FRINFC_DESF_PICC_ADDI_FRAME_RESP) )
784 {
785 /*set the state*/
786 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_GET_UID;
787
788 /* Helper routine to wrap the native desfire cmds*/
789 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_GET_UID_CMD);
790
791 status = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
792 }
793
794 return status;
795
796 }
797
798
phFriNfc_Desf_HCreateApp(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)799 static NFCSTATUS phFriNfc_Desf_HCreateApp(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
800 {
801 NFCSTATUS status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
802 NFCSTATUS_FORMAT_ERROR);
803
804 if ( (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL14] == PH_FRINFC_DESF_NAT_WRAP_FIRST_RESP_BYTE)
805 && (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL15] == PH_FRINFC_DESF_NAT_WRAP_SEC_RESP_BYTE ))
806 {
807 /*set the state*/
808 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_CREATE_AID;
809
810 /* Helper routine to wrap the native DESFire cmds*/
811 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_CREATEAPP_CMD);
812
813 status = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
814 }
815 return ( status);
816 }
817
818
phFriNfc_Desf_HSelectApp(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)819 static NFCSTATUS phFriNfc_Desf_HSelectApp(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
820 {
821
822 NFCSTATUS status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
823 NFCSTATUS_FORMAT_ERROR);
824
825 /* check for the response of previous operation, before
826 issuing the next command*/
827
828 if ( (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL0] == PH_FRINFC_DESF_NAT_WRAP_FIRST_RESP_BYTE) &&
829 (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL1] == PH_FRINFC_DESF_NAT_WRAP_SEC_RESP_BYTE ))
830 {
831 /*set the state*/
832 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_SELECT_APP;
833
834 /* Helper routine to wrap the native DESFire cmds*/
835 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_SELECTAPP_CMD);
836
837 status = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
838 }
839 return ( status);
840
841 }
842
phFriNfc_Desf_HCreatCCFile(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)843 static NFCSTATUS phFriNfc_Desf_HCreatCCFile(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
844 {
845 NFCSTATUS status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
846 NFCSTATUS_FORMAT_ERROR);
847
848 if ( (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL0] == PH_FRINFC_DESF_NATIVE_RESP_BYTE1) &&
849 (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL1] == PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ))
850 {
851 /*set the state*/
852 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_CREATE_CCFILE;
853
854 /* Helper routine to wrap the native DESFire cmds*/
855 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_CREATECC_CMD);
856
857 status = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
858 }
859 return ( status);
860 }
861
phFriNfc_Desf_HCreatNDEFFile(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)862 static NFCSTATUS phFriNfc_Desf_HCreatNDEFFile(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
863 {
864
865 NFCSTATUS status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
866 NFCSTATUS_FORMAT_ERROR);
867
868 if ( (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL0] == PH_FRINFC_DESF_NATIVE_RESP_BYTE1) &&
869 (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL1] == PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ))
870 {
871 /*set the state*/
872 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_CREATE_NDEFFILE;
873
874 /* Helper routine to wrap the native desfire cmds*/
875 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_CREATENDEF_CMD);
876
877 status = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
878
879 }
880
881 return ( status);
882
883 }
884
phFriNfc_Desf_HWrCCBytes(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)885 static NFCSTATUS phFriNfc_Desf_HWrCCBytes(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
886 {
887
888 NFCSTATUS result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
889 NFCSTATUS_FORMAT_ERROR);
890 if ( (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL0] == PH_FRINFC_DESF_NATIVE_RESP_BYTE1) &&
891 (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL1] == PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ))
892 {
893
894 /*set the state*/
895 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_WRITE_CC_FILE;
896
897 /* Helper routine to wrap the native DESFire cmds*/
898 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_WRITECC_CMD);
899
900 result = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
901 }
902 return (result);
903 }
904
phFriNfc_Desf_HWrNDEFData(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)905 static NFCSTATUS phFriNfc_Desf_HWrNDEFData(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
906 {
907
908 NFCSTATUS Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
909 NFCSTATUS_FORMAT_ERROR);
910
911
912 if ( (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL0] == PH_FRINFC_DESF_NATIVE_RESP_BYTE1) &&
913 (NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL1] == PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ))
914 {
915 /*set the state*/
916 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_WRITE_NDEF_FILE;
917
918 /* Helper routine to wrap the native DESFire cmds*/
919 phFriNfc_Desf_HWrapISONativeCmds(NdefSmtCrdFmt,PH_FRINFC_DESF_WRITENDEF_CMD);
920
921 Result = phFriNfc_Desf_HSendTransCmd(NdefSmtCrdFmt);
922 }
923 return (Result);
924 }
925
phFriNfc_Desf_HSendTransCmd(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)926 static NFCSTATUS phFriNfc_Desf_HSendTransCmd(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
927 {
928
929 NFCSTATUS status = NFCSTATUS_SUCCESS;
930
931 /* set the command type*/
932 #ifdef PH_HAL4_ENABLE
933 NdefSmtCrdFmt->Cmd.Iso144434Cmd = phHal_eIso14443_4_Raw;
934 #else
935 NdefSmtCrdFmt->Cmd.Iso144434Cmd = phHal_eIso14443_4_CmdListTClCmd;
936 #endif /* #ifdef PH_HAL4_ENABLE */
937
938 /* set the Additional Info*/
939 NdefSmtCrdFmt->psDepAdditionalInfo.DepFlags.MetaChaining = 0;
940 NdefSmtCrdFmt->psDepAdditionalInfo.DepFlags.NADPresent = 0;
941
942 /*set the completion routines for the desfire card operations*/
943 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = phFriNfc_NdefSmtCrd_Process;
944 NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NdefSmtCrdFmt;
945
946 /* set the receive length */
947 *NdefSmtCrdFmt->SendRecvLength = PH_FRINFC_SMTCRDFMT_MAX_SEND_RECV_BUF_SIZE;
948
949
950 /*Call the Overlapped HAL Transceive function */
951 status = phFriNfc_OvrHal_Transceive(NdefSmtCrdFmt->LowerDevice,
952 &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
953 NdefSmtCrdFmt->psRemoteDevInfo,
954 NdefSmtCrdFmt->Cmd,
955 &NdefSmtCrdFmt->psDepAdditionalInfo,
956 NdefSmtCrdFmt->SendRecvBuf,
957 NdefSmtCrdFmt->SendLength,
958 NdefSmtCrdFmt->SendRecvBuf,
959 NdefSmtCrdFmt->SendRecvLength);
960
961 return (status);
962
963
964 }
965
phFriNfc_Desfire_Format(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)966 NFCSTATUS phFriNfc_Desfire_Format(phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
967 {
968
969 NFCSTATUS status = NFCSTATUS_SUCCESS;
970 #ifdef DESFIRE_FMT_EV1
971 NdefSmtCrdFmt->CardType = 0;
972 #endif /* #ifdef DESFIRE_FMT_EV1 */
973 status = phFriNfc_Desf_HGetHWVersion(NdefSmtCrdFmt);
974 return (status);
975 }
976
977 #ifdef FRINFC_READONLY_NDEF
978
979 #if 0
980 static
981 NFCSTATUS
982 phFriNfc_Desf_HReadOnlySelectCCFile (
983 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
984 {
985 NFCSTATUS result = NFCSTATUS_SUCCESS;
986 return result;
987 }
988 #endif /* #if 0 */
989
990 static
991 NFCSTATUS
phFriNfc_Desf_HReadOnlyReadCCFile(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)992 phFriNfc_Desf_HReadOnlyReadCCFile (
993 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
994 {
995 NFCSTATUS result = NFCSTATUS_SUCCESS;
996 uint16_t i = 0;
997
998 if ((PH_FRINFC_DESF_NATIVE_RESP_BYTE1 ==
999 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 2)])
1000 && (PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ==
1001 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 1)]))
1002 {
1003 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_RO_READ_CC_FILE;
1004
1005 /* Class Byte */
1006 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_CLASS_BYTE;
1007 i++;
1008
1009 /* let the place to store the cmd byte type, point to next index
1010 Instruction Cmd code */
1011 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_READ_DATA_CMD;
1012 i++;
1013
1014
1015 /* P1/P2 offsets always set to zero */
1016 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_OFFSET_P1;
1017 i++;
1018 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_OFFSET_P2;
1019 i++;
1020
1021 /* Lc: Length of wrapped data */
1022 NdefSmtCrdFmt->SendRecvBuf[i] = NATIVE_WRAPPER_READ_DATA_LC_VALUE;
1023 i++;
1024
1025 #ifdef DESFIRE_FMT_EV1
1026 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
1027 {
1028 /* set the file id*/
1029 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_CC_FILE_ID;
1030 i++;
1031 }
1032 else
1033 #endif /* #ifdef DESFIRE_FMT_EV1 */
1034 {
1035 /* set the file id*/
1036 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_ID;
1037 i++;
1038 }
1039
1040 /* set the offset to zero*/
1041 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1042 i++;
1043 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1044 i++;
1045 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1046 i++;
1047
1048 /* Set the length of data available to read */
1049 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_SIZE;
1050 i++;
1051 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1052 i++;
1053 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1054 i++;
1055
1056 /* Le Value is set 0 */
1057 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
1058 i++;
1059
1060 NdefSmtCrdFmt->SendLength = i;
1061
1062 result = phFriNfc_Desf_HSendTransCmd (NdefSmtCrdFmt);
1063 }
1064 else
1065 {
1066 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
1067 NFCSTATUS_FORMAT_ERROR);
1068 }
1069
1070 return result;
1071 }
1072
1073 static
1074 NFCSTATUS
phFriNfc_Desf_HReadOnlyWriteCCFile(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)1075 phFriNfc_Desf_HReadOnlyWriteCCFile (
1076 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
1077 {
1078 NFCSTATUS result = NFCSTATUS_SUCCESS;
1079 uint8_t read_cc_btyes[CC_BYTES_SIZE] = {0};
1080 uint16_t i = 0;
1081
1082 if ((PH_FRINFC_DESF_NATIVE_RESP_BYTE1 ==
1083 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 2)])
1084 && (PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ==
1085 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 1)]))
1086 {
1087 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_RO_UPDATE_CC_FILE;
1088
1089 memcpy ((void *)read_cc_btyes, (void *)NdefSmtCrdFmt->SendRecvBuf,
1090 sizeof (read_cc_btyes));
1091 read_cc_btyes[(sizeof (read_cc_btyes) - 1)] = READ_ONLY_NDEF_DESFIRE;
1092
1093 /* Class Byte */
1094 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_CLASS_BYTE;
1095 i++;
1096
1097 /* let the place to store the cmd byte type, point to next index
1098 Instruction Cmd code */
1099 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_WRITE_CMD;
1100 i++;
1101
1102
1103 /* P1/P2 offsets always set to zero */
1104 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_OFFSET_P1;
1105 i++;
1106 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_OFFSET_P2;
1107 i++;
1108
1109 /* Lc: Length of wrapped data */
1110 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_WRCC_WRDT_LEN;
1111 i++;
1112
1113 #ifdef DESFIRE_FMT_EV1
1114 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
1115 {
1116 /* set the file id*/
1117 NdefSmtCrdFmt->SendRecvBuf[i] = DESFIRE_EV1_CC_FILE_ID;
1118 i++;
1119 }
1120 else
1121 #endif /* #ifdef DESFIRE_FMT_EV1 */
1122 {
1123 /* set the file id*/
1124 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_ID;
1125 i++;
1126 }
1127
1128 /* set the offset to zero*/
1129 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1130 i++;
1131 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1132 i++;
1133 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1134 i++;
1135
1136 /* Set the length of data available to write*/
1137 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_CC_FILE_SIZE;
1138 i++;
1139 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1140 i++;
1141 NdefSmtCrdFmt->SendRecvBuf[i] = 0x00;
1142 i++;
1143
1144 /*set the data to be written to the CC file*/
1145 (void)memcpy ((void *)&NdefSmtCrdFmt->SendRecvBuf[i],
1146 (void *)read_cc_btyes, sizeof (read_cc_btyes));
1147 #ifdef DESFIRE_FMT_EV1
1148 #else
1149 i++;
1150 #endif /* #ifdef DESFIRE_FMT_EV1 */
1151
1152 i = (uint16_t)(i + sizeof (read_cc_btyes));
1153
1154 /* Le bytes*/
1155 NdefSmtCrdFmt->SendRecvBuf[i] = PH_FRINFC_DESF_NATIVE_LE_BYTE;
1156 i++;
1157 #ifdef DESFIRE_FMT_EV1
1158 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
1159 {
1160 NdefSmtCrdFmt->SendLength = i;
1161 }
1162 else
1163 #endif /* #ifdef DESFIRE_FMT_EV1 */
1164 {
1165 NdefSmtCrdFmt->SendLength = PH_FRINFC_DESF_WRITECC_CMD_SNLEN;
1166 }
1167
1168 result = phFriNfc_Desf_HSendTransCmd (NdefSmtCrdFmt);
1169 }
1170 else
1171 {
1172 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
1173 NFCSTATUS_FORMAT_ERROR);
1174 }
1175
1176 return result;
1177 }
1178
1179 static
1180 NFCSTATUS
phFriNfc_Desf_HReadOnlySelectApp(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)1181 phFriNfc_Desf_HReadOnlySelectApp (
1182 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
1183 {
1184 NFCSTATUS result = NFCSTATUS_SUCCESS;
1185
1186 NdefSmtCrdFmt->CardType = 0;
1187
1188 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_RO_SELECT_APP;
1189
1190 /* Helper routine to wrap the native DESFire cmds */
1191 phFriNfc_Desf_HWrapISONativeCmds (NdefSmtCrdFmt, PH_FRINFC_DESF_SELECTAPP_CMD);
1192
1193 result = phFriNfc_Desf_HSendTransCmd (NdefSmtCrdFmt);
1194
1195 return result;
1196 }
1197
1198 #ifdef DESFIRE_FMT_EV1
1199 static
1200 NFCSTATUS
phFriNfc_Desf_HReadOnlySelectAppEV1(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)1201 phFriNfc_Desf_HReadOnlySelectAppEV1 (
1202 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
1203 {
1204 NFCSTATUS result = NFCSTATUS_SUCCESS;
1205
1206 NdefSmtCrdFmt->CardType = DESFIRE_CARD_TYPE_EV1;
1207
1208 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_RO_SELECT_APP_EV1;
1209
1210 /* Helper routine to wrap the native DESFire cmds */
1211 phFriNfc_Desf_HWrapISONativeCmds (NdefSmtCrdFmt, PH_FRINFC_DESF_SELECTAPP_CMD);
1212
1213 result = phFriNfc_Desf_HSendTransCmd (NdefSmtCrdFmt);
1214
1215 return result;
1216 }
1217 #endif /* #ifdef DESFIRE_FMT_EV1 */
1218
1219 NFCSTATUS
phFriNfc_Desfire_ConvertToReadOnly(phFriNfc_sNdefSmtCrdFmt_t * NdefSmtCrdFmt)1220 phFriNfc_Desfire_ConvertToReadOnly (
1221 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt)
1222 {
1223 NFCSTATUS result = NFCSTATUS_SUCCESS;
1224
1225 #ifdef DESFIRE_FMT_EV1
1226 result = phFriNfc_Desf_HReadOnlySelectAppEV1 (NdefSmtCrdFmt);
1227 #else
1228 result = phFriNfc_Desf_HReadOnlySelectApp (NdefSmtCrdFmt);
1229 #endif /* #ifdef DESFIRE_FMT_EV1 */
1230
1231 return result;
1232 }
1233
1234 #endif /* #ifdef FRINFC_READONLY_NDEF */
1235
phFriNfc_Desf_Process(void * Context,NFCSTATUS Status)1236 void phFriNfc_Desf_Process( void *Context,
1237 NFCSTATUS Status)
1238 {
1239
1240 phFriNfc_sNdefSmtCrdFmt_t *NdefSmtCrdFmt;
1241
1242 NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)Context;
1243
1244 if((NFCSTATUS_SUCCESS & PHNFCSTBLOWER) == (Status & PHNFCSTBLOWER))
1245 {
1246 switch(NdefSmtCrdFmt->State)
1247 {
1248 #ifdef FRINFC_READONLY_NDEF
1249 #ifdef DESFIRE_FMT_EV1
1250 case PH_FRINFC_DESF_STATE_RO_SELECT_APP_EV1:
1251 {
1252 if ((PH_FRINFC_DESF_NATIVE_RESP_BYTE1 ==
1253 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 2)])
1254 && (PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ==
1255 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 1)]))
1256 {
1257 Status = phFriNfc_Desf_HReadOnlyReadCCFile (NdefSmtCrdFmt);
1258 }
1259 else
1260 {
1261 Status = phFriNfc_Desf_HReadOnlySelectApp (NdefSmtCrdFmt);
1262 }
1263 break;
1264 }
1265 #endif /* #ifdef DESFIRE_FMT_EV1 */
1266
1267 case PH_FRINFC_DESF_STATE_RO_SELECT_APP:
1268 {
1269 Status = phFriNfc_Desf_HReadOnlyReadCCFile (NdefSmtCrdFmt);
1270 break;
1271 }
1272
1273 case PH_FRINFC_DESF_STATE_RO_READ_CC_FILE:
1274 {
1275 Status = phFriNfc_Desf_HReadOnlyWriteCCFile (NdefSmtCrdFmt);
1276 break;
1277 }
1278
1279 case PH_FRINFC_DESF_STATE_RO_UPDATE_CC_FILE:
1280 {
1281 if ((PH_FRINFC_DESF_NATIVE_RESP_BYTE1 ==
1282 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 2)])
1283 && (PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ==
1284 NdefSmtCrdFmt->SendRecvBuf[(*NdefSmtCrdFmt->SendRecvLength - 1)]))
1285 {
1286 /* SUCCESSFULL Formatting */
1287 #ifdef DESFIRE_FMT_EV1
1288 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
1289 {
1290 Status = phFriNfc_OvrHal_Reconnect (
1291 NdefSmtCrdFmt->LowerDevice,
1292 &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
1293 NdefSmtCrdFmt->psRemoteDevInfo);
1294
1295 if (NFCSTATUS_PENDING == Status)
1296 {
1297 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_REACTIVATE;
1298 }
1299 }
1300 #endif /* #ifdef DESFIRE_FMT_EV1 */
1301 }
1302 else
1303 {
1304 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_SMTCRDFMT,
1305 NFCSTATUS_FORMAT_ERROR);
1306 }
1307 break;
1308 }
1309
1310 #endif /* #ifdef FRINFC_READONLY_NDEF */
1311 case PH_FRINFC_DESF_STATE_GET_HW_VERSION:
1312 {
1313 /* Check and store the h/w and s/w specific details.
1314 Ex: Major/Minor version, memory storage info. */
1315
1316 Status = phFriNfc_Desf_HGetSWVersion (NdefSmtCrdFmt);
1317
1318 break;
1319 }
1320
1321 case PH_FRINFC_DESF_STATE_GET_SW_VERSION:
1322 {
1323 /* Check and store the h/w and s/w specific details.
1324 Ex: Major/Minor version, memory storage info. */
1325
1326 Status = phFriNfc_Desf_HUpdateVersionDetails (NdefSmtCrdFmt);
1327 if ( Status == NFCSTATUS_SUCCESS )
1328 {
1329 Status = phFriNfc_Desf_HGetUIDDetails (NdefSmtCrdFmt);
1330 }
1331 break;
1332 }
1333
1334 case PH_FRINFC_DESF_STATE_GET_UID:
1335 {
1336 Status = phFriNfc_Desf_HCreateApp (NdefSmtCrdFmt);
1337 break;
1338 }
1339
1340 case PH_FRINFC_DESF_STATE_CREATE_AID:
1341 {
1342 Status = phFriNfc_Desf_HSelectApp (NdefSmtCrdFmt);
1343 break;
1344 }
1345
1346 case PH_FRINFC_DESF_STATE_SELECT_APP:
1347 {
1348 Status = phFriNfc_Desf_HCreatCCFile (NdefSmtCrdFmt);
1349 break;
1350 }
1351
1352 case PH_FRINFC_DESF_STATE_CREATE_CCFILE:
1353 {
1354 Status = phFriNfc_Desf_HCreatNDEFFile (NdefSmtCrdFmt);
1355 break;
1356 }
1357
1358 case PH_FRINFC_DESF_STATE_CREATE_NDEFFILE:
1359 {
1360 Status = phFriNfc_Desf_HWrCCBytes (NdefSmtCrdFmt);
1361 break;
1362 }
1363
1364 case PH_FRINFC_DESF_STATE_WRITE_CC_FILE:
1365 {
1366 Status = phFriNfc_Desf_HWrNDEFData (NdefSmtCrdFmt);
1367 break;
1368 }
1369
1370 case PH_FRINFC_DESF_STATE_WRITE_NDEF_FILE:
1371 {
1372 if ((PH_FRINFC_DESF_NATIVE_RESP_BYTE1 ==
1373 NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL0]) &&
1374 (PH_FRINFC_DESF_NATIVE_RESP_BYTE2 ==
1375 NdefSmtCrdFmt->SendRecvBuf[PH_SMTCRDFMT_DESF_VAL1]))
1376 {
1377 NdefSmtCrdFmt->CardState = 0;
1378 #ifdef DESFIRE_FMT_EV1
1379 if (DESFIRE_CARD_TYPE_EV1 == NdefSmtCrdFmt->CardType)
1380 {
1381 Status = phFriNfc_OvrHal_Reconnect (
1382 NdefSmtCrdFmt->LowerDevice,
1383 &NdefSmtCrdFmt->SmtCrdFmtCompletionInfo,
1384 NdefSmtCrdFmt->psRemoteDevInfo);
1385
1386 if (NFCSTATUS_PENDING == Status)
1387 {
1388 NdefSmtCrdFmt->State = PH_FRINFC_DESF_STATE_REACTIVATE;
1389 }
1390 }
1391 else
1392 #endif /* #ifdef DESFIRE_FMT_EV1 */
1393 {
1394 Status = PHNFCSTVAL (CID_NFC_NONE, NFCSTATUS_SUCCESS);
1395 }
1396 }
1397 break;
1398 }
1399
1400 #ifdef DESFIRE_FMT_EV1
1401 case PH_FRINFC_DESF_STATE_REACTIVATE:
1402 {
1403 /* Do nothing */
1404 break;
1405 }
1406 #endif /* #ifdef DESFIRE_FMT_EV1 */
1407
1408 default:
1409 {
1410 /*set the invalid state*/
1411 Status = PHNFCSTVAL (CID_FRI_NFC_NDEF_SMTCRDFMT,
1412 NFCSTATUS_INVALID_DEVICE_REQUEST);
1413 break;
1414 }
1415 }
1416 }
1417 /* Handle the all the error cases*/
1418 if ((NFCSTATUS_PENDING & PHNFCSTBLOWER) != (Status & PHNFCSTBLOWER))
1419 {
1420 /* call respective CR */
1421 phFriNfc_SmtCrdFmt_HCrHandler(NdefSmtCrdFmt,Status);
1422 }
1423
1424 }
1425
1426