@@ -79,6 +79,12 @@ void ISO144434Reset(void) {
79
79
ISO14443ALastIncomingDataFrame [0 ] = 0x00 ;
80
80
}
81
81
82
+ static uint16_t GetACKCommandData (uint8_t * Buffer );
83
+ static uint16_t GetACKCommandData (uint8_t * Buffer ) {
84
+ Buffer [0 ] = ISO14443A_ACK ;
85
+ return ASBITS (1 );
86
+ }
87
+
82
88
static uint16_t GetNAKCommandData (uint8_t * Buffer , bool ResetToHaltState );
83
89
static uint16_t GetNAKCommandData (uint8_t * Buffer , bool ResetToHaltState ) {
84
90
if (ResetToHaltState ) {
@@ -124,17 +130,17 @@ uint16_t ISO144434ProcessBlock(uint8_t *Buffer, uint16_t ByteCount, uint16_t Bit
124
130
return GetNAKCommandData (Buffer , false);
125
131
//return ISO14443A_APP_NO_RESPONSE;
126
132
}
127
- /* Process RATS.
133
+ /* Process RATS:
128
134
* NOTE: ATS bytes are tailored to Chameleon implementation and differ from DESFire spec.
129
135
* NOTE: Some PCD implementations do a memcmp() over ATS bytes, which is completely wrong.
130
136
*/
131
137
Iso144434CardID = Buffer [1 ] & 0x0F ;
132
138
Buffer [0 ] = 0x06 ;
133
139
memcpy (& Buffer [1 ], & Picc .ATSBytes [1 ], 4 );
134
- Buffer [5 ] = 0x80 ; /* T1: dummy value for historical bytes */
135
- ByteCount = 6 ; /* NOT including CRC */
140
+ Buffer [5 ] = 0x80 ; /* T1: dummy value for historical bytes */
141
+ ByteCount = 6 ;
136
142
ISO144434SwitchState (ISO14443_4_STATE_ACTIVE );
137
- return ASBITS ( ByteCount ); /* PM3 expects no CRCA bytes */
143
+ return GetAndSetBufferCRCA ( Buffer , ByteCount ); /* PM3 'hf mfdes list' expects CRCA bytes on the RATS data */
138
144
}
139
145
case ISO14443_4_STATE_ACTIVE : {
140
146
/* See: ISO/IEC 14443-4; 7.1 Block format */
@@ -341,8 +347,16 @@ uint16_t ISO144433APiccProcess(uint8_t *Buffer, uint16_t BitCount) {
341
347
} else if (ISO144433AIsHalt (Buffer , BitCount )) {
342
348
DesfireLogEntry (LOG_INFO_APP_CMD_HALT , NULL , 0 );
343
349
return GetHLTACommandData (Buffer , true);
350
+ } else if (Cmd == ISO14443A_CMD_RATS ) {
351
+ ISO144434SwitchState (ISO14443_4_STATE_EXPECT_RATS );
352
+ uint16_t ReturnBits = ISO144434ProcessBlock (Buffer , ASBYTES (BitCount ), BitCount );
353
+ Iso144433AState = ISO14443_3A_STATE_ACTIVE ;
354
+ StateRetryCount = 0 ;
355
+ return ReturnBits ;
344
356
} else if (IsDeselectCmd (Cmd )) {
345
- return GetHLTACommandData (Buffer , true);
357
+ ISO144433AHalt ();
358
+ return GetACKCommandData (Buffer );
359
+ //return GetHLTACommandData(Buffer, true);
346
360
} else if (IsRIDCmd (Cmd )) {
347
361
Iso144433AState = ISO14443_3A_STATE_ACTIVE ;
348
362
StateRetryCount = 0 ;
@@ -352,14 +366,11 @@ uint16_t ISO144433APiccProcess(uint8_t *Buffer, uint16_t BitCount) {
352
366
*/
353
367
uint16_t respDataSize = (uint16_t ) sizeof (MIFARE_DESFIRE_TAG_AID );
354
368
memcpy (& Buffer [0 ], MIFARE_DESFIRE_TAG_AID , respDataSize );
355
- /* ??? TODO: Do we append CRCA bytes ???
356
- * ISO14443AAppendCRCA(Buffer, respDataSize);
357
- * respDataSize += 2;
358
- */
369
+ /* ??? TODO: Do we append CRCA bytes ??? */
359
370
return ASBITS (respDataSize );
360
371
} else if (IsUnsupportedCmd (Cmd )) {
361
372
return GetNAKCommandData (Buffer , true);
362
- } else if (CheckStateRetryCount (false)) { /* Increment the state retry count to keep track of when to timeout */
373
+ } else if (CheckStateRetryCount (false)) {
363
374
DEBUG_PRINT_P (PSTR ("ISO14443-3: SW-RESET" ));
364
375
return GetHLTACommandData (Buffer , true);
365
376
} else if (BitCount <= BITS_PER_BYTE ) {
@@ -382,31 +393,30 @@ uint16_t ISO144433APiccProcess(uint8_t *Buffer, uint16_t BitCount) {
382
393
Iso144433AIdleState = Iso144433AState ;
383
394
ISO144433ASwitchState (ISO14443_3A_STATE_READY_CL1 );
384
395
/* The LSByte ordering of the ATQA value for ISO14443 tags is
385
- * discussed in section 2.3 of NXP AN10833.
386
- */
396
+ * discussed in section 2.3 of NXP AN10833.
397
+ */
387
398
Buffer [0 ] = Picc .ATQA [1 ];
388
399
Buffer [1 ] = Picc .ATQA [0 ];
389
400
return ASBITS (ISO14443A_ATQA_FRAME_SIZE_BYTES );
390
401
391
402
case ISO14443_3A_STATE_READY_CL1 :
392
- case ISO14443_3A_STATE_READY_CL1_NVB_END :
403
+ case ISO14443_3A_STATE_READY_CL1_NVB_END : {
393
404
if (Cmd == ISO14443A_CMD_SELECT_CL1 ) {
394
- /* Load UID CL1 and perform anticollision: */
405
+ /* NXP AN10927 (section 2, figure 1, page 3) shows the expected
406
+ * data flow to exchange the 7-byte UID for DESFire tags:
407
+ * http://www.nxp.com/docs/en/application-note/AN10927.pdf
408
+ */
395
409
ConfigurationUidType Uid ;
396
410
ApplicationGetUid (& Uid [1 ]);
397
411
Uid [0 ] = ISO14443A_UID0_CT ;
398
- /* NXP AN10927 (section 2, figure 1, page 3) shows the expected
399
- * data flow to exchange the 7-byte UID for DESFire tags:
400
- * http://www.nxp.com/docs/en/application-note/AN10927.pdf
401
- */
412
+ /* Load UID CL1 and perform anticollision: */
402
413
uint8_t cl1SAKValue = SAK_CL1_VALUE ;
403
414
if (Buffer [1 ] == ISO14443A_NVB_AC_START && ISO14443ASelectDesfire (& Buffer [0 ], 0 , & BitCount , & Uid [0 ], 4 , cl1SAKValue )) {
404
415
ISO144433ASwitchState (ISO14443_3A_STATE_READY_CL1_NVB_END );
405
416
return BitCount ;
406
417
} else if (Buffer [1 ] == ISO14443A_NVB_AC_END && ISO14443ASelectDesfire (& Buffer [0 ], 0 , & BitCount , & Uid [0 ], 4 , cl1SAKValue )) {
407
418
ISO144433ASwitchState (ISO14443_3A_STATE_READY_CL2 );
408
419
return BitCount ;
409
-
410
420
} else {
411
421
DEBUG_PRINT_P (PSTR ("ISO14443-4: Select CL1-NOT-OK" ));
412
422
}
@@ -415,19 +425,19 @@ uint16_t ISO144433APiccProcess(uint8_t *Buffer, uint16_t BitCount) {
415
425
}
416
426
CheckStateRetryCount (false);
417
427
return GetNAKCommandData (Buffer , false);
418
- //return ISO14443A_APP_NO_RESPONSE;
419
-
428
+ //return ISO14443A_APP_NO_RESPONSE;
429
+ }
420
430
case ISO14443_3A_STATE_READY_CL2 :
421
- case ISO14443_3A_STATE_READY_CL2_NVB_END :
431
+ case ISO14443_3A_STATE_READY_CL2_NVB_END : {
422
432
if (Cmd == ISO14443A_CMD_SELECT_CL2 && ActiveConfiguration .UidSize >= ISO14443A_UID_SIZE_DOUBLE ) {
423
433
/* Load UID CL2 and perform anticollision: */
424
434
ConfigurationUidType Uid ;
425
435
ApplicationGetUid (& Uid [0 ]);
426
436
uint8_t cl2SAKValue = SAK_CL2_VALUE ;
427
- if (Buffer [1 ] == ISO14443A_NVB_AC_START && ISO14443ASelectDesfire (& Buffer [0 ], 0 , & BitCount , & Uid [4 ], 4 , cl2SAKValue )) {
437
+ if (Buffer [1 ] == ISO14443A_NVB_AC_START && ISO14443ASelectDesfire (& Buffer [0 ], 0 , & BitCount , & Uid [3 ], 4 , cl2SAKValue )) {
428
438
ISO144433ASwitchState (ISO14443_3A_STATE_READY_CL2_NVB_END );
429
439
return BitCount ;
430
- } else if (Buffer [1 ] == ISO14443A_NVB_AC_END && ISO14443ASelectDesfire (& Buffer [0 ], 0 , & BitCount , & Uid [4 ], 4 , cl2SAKValue )) {
440
+ } else if (Buffer [1 ] == ISO14443A_NVB_AC_END && ISO14443ASelectDesfire (& Buffer [0 ], 0 , & BitCount , & Uid [3 ], 4 , cl2SAKValue )) {
431
441
ISO144433ASwitchState (ISO14443_3A_STATE_ACTIVE );
432
442
return BitCount ;
433
443
@@ -439,22 +449,19 @@ uint16_t ISO144433APiccProcess(uint8_t *Buffer, uint16_t BitCount) {
439
449
}
440
450
CheckStateRetryCount (false);
441
451
return GetNAKCommandData (Buffer , false);
442
- //return ISO14443A_APP_NO_RESPONSE;
443
-
444
- case ISO14443_3A_STATE_ACTIVE :
452
+ //return ISO14443A_APP_NO_RESPONSE;
453
+ }
454
+ case ISO14443_3A_STATE_ACTIVE : {
445
455
StateRetryCount = MAX_STATE_RETRY_COUNT ;
446
456
if (Cmd == ISO14443A_CMD_RATS ) {
447
457
ISO144434SwitchState (ISO14443_4_STATE_EXPECT_RATS );
448
458
} else if (Cmd == ISO14443A_CMD_SELECT_CL3 ) {
449
459
/* DESFire UID size is of insufficient size to support this request: */
450
- Buffer [0 ] = ISO14443A_SAK_COMPLETE_NOT_COMPLIANT ;
451
- ISO14443AAppendCRCA (& Buffer [0 ], 1 );
452
- return ISO14443A_SAK_FRAME_SIZE ;
460
+ return GetNAKCommandData (Buffer , false);
453
461
}
454
462
/* Forward to ISO/IEC 14443-4 block processing code */
455
- uint16_t ReturnBits = ISO144434ProcessBlock (Buffer , ASBYTES (BitCount ), BitCount );
456
- return ReturnBits ;
457
-
463
+ return ISO144434ProcessBlock (Buffer , ASBYTES (BitCount ), BitCount );
464
+ }
458
465
default :
459
466
break ;
460
467
0 commit comments