1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, split Nov 2006
3 // Modified by Greg Jones, Jan 2009
4 // Modified by Adrian Dabrowski "atrox", Mar-Sept 2010,Oct 2011
5 // Modified by piwi, Oct 2018
7 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
8 // at your option, any later version. See the LICENSE.txt file for the text of
10 //-----------------------------------------------------------------------------
11 // Routines to support ISO 15693. This includes both the reader software and
12 // the `fake tag' modes.
13 //-----------------------------------------------------------------------------
15 // The ISO 15693 describes two transmission modes from reader to tag, and four
16 // transmission modes from tag to reader. As of Oct 2018 this code supports
17 // both reader modes and the high speed variant with one subcarrier from card to reader.
18 // As long as the card fully support ISO 15693 this is no problem, since the
19 // reader chooses both data rates, but some non-standard tags do not.
20 // For card simulation, the code supports both high and low speed modes with one subcarrier.
22 // VCD (reader) -> VICC (tag)
24 // data rate: 1,66 kbit/s (fc/8192)
25 // used for long range
27 // data rate: 26,48 kbit/s (fc/512)
28 // used for short range, high speed
30 // VICC (tag) -> VCD (reader)
32 // ASK / one subcarrier (423,75 khz)
33 // FSK / two subcarriers (423,75 khz && 484,28 khz)
34 // Data Rates / Modes:
35 // low ASK: 6,62 kbit/s
36 // low FSK: 6.67 kbit/s
37 // high ASK: 26,48 kbit/s
38 // high FSK: 26,69 kbit/s
39 //-----------------------------------------------------------------------------
43 // *) UID is always used "transmission order" (LSB), which is reverse to display order
45 // TODO / BUGS / ISSUES:
46 // *) signal decoding is unable to detect collisions.
47 // *) add anti-collision support for inventory-commands
48 // *) read security status of a block
49 // *) sniffing and simulation do not support two subcarrier modes.
50 // *) remove or refactor code under "deprecated"
51 // *) document all the functions
55 #include "proxmark3.h"
59 #include "iso15693tools.h"
60 #include "protocols.h"
64 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
68 ///////////////////////////////////////////////////////////////////////
69 // ISO 15693 Part 2 - Air Interface
70 // This section basicly contains transmission and receiving of bits
71 ///////////////////////////////////////////////////////////////////////
73 #define Crc(data,datalen) Iso15693Crc(data,datalen)
74 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
75 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
78 #define ISO15693_DMA_BUFFER_SIZE 2048 // must be a power of 2
79 #define ISO15693_MAX_RESPONSE_LENGTH 36 // allows read single block with the maximum block size of 256bits. Read multiple blocks not supported yet
80 #define ISO15693_MAX_COMMAND_LENGTH 45 // allows write single block with the maximum block size of 256bits. Write multiple blocks not supported yet
82 // timing. Delays in SSP_CLK ticks.
83 #define DELAY_READER_TO_ARM 8
84 #define DELAY_ARM_TO_READER 1
85 #define DELAY_ISO15693_VCD_TO_VICC 132 // 132/423.75kHz = 311.5us from end of EOF to start of tag response
86 #define DELAY_ISO15693_VICC_TO_VCD 1017 // 1017/3.39MHz = 300us between end of tag response and next reader command
88 // ---------------------------
90 // ---------------------------
92 // prepare data using "1 out of 4" code for later transmission
93 // resulting data rate is 26.48 kbit/s (fc/512)
95 // n ... length of data
96 static void CodeIso15693AsReader(uint8_t *cmd
, int n
)
102 // Give it a bit of slack at the beginning
103 for(i
= 0; i
< 24; i
++) {
116 for(i
= 0; i
< n
; i
++) {
117 for(j
= 0; j
< 8; j
+= 2) {
118 int these
= (cmd
[i
] >> j
) & 3;
169 // Fill remainder of last byte with 1
170 for(i
= 0; i
< 4; i
++) {
177 // encode data using "1 out of 256" scheme
178 // data rate is 1,66 kbit/s (fc/8192)
179 // is designed for more robust communication over longer distances
180 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
)
186 // Give it a bit of slack at the beginning
187 for(i
= 0; i
< 24; i
++) {
201 for(i
= 0; i
< n
; i
++) {
202 for (j
= 0; j
<=255; j
++) {
218 // Fill remainder of last byte with 1
219 for(i
= 0; i
< 4; i
++) {
227 static void CodeIso15693AsTag(uint8_t *cmd
, int n
)
242 for(int i
= 0; i
< n
; i
++) {
243 for(int j
= 0; j
< 8; j
++) {
244 if ((cmd
[i
] >> j
) & 0x01) {
268 // Transmit the command (to the tag) that was placed in cmd[].
269 static void TransmitTo15693Tag(const uint8_t *cmd
, int len
, uint32_t start_time
)
271 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX
);
272 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
274 while (GetCountSspClk() < start_time
);
277 for(int c
= 0; c
< len
; ) {
278 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
279 AT91C_BASE_SSC
->SSC_THR
= ~cmd
[c
];
287 //-----------------------------------------------------------------------------
288 // Transmit the tag response (to the reader) that was placed in cmd[].
289 //-----------------------------------------------------------------------------
290 static void TransmitTo15693Reader(const uint8_t *cmd
, size_t len
, uint32_t start_time
, bool slow
)
292 // don't use the FPGA_HF_SIMULATOR_MODULATE_424K_8BIT minor mode. It would spoil GetCountSspClk()
293 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_424K
);
295 uint8_t shift_delay
= start_time
& 0x00000007;
296 uint8_t bitmask
= 0x00;
297 for (int i
= 0; i
< shift_delay
; i
++) {
298 bitmask
|= (0x01 << i
);
301 while (GetCountSspClk() < (start_time
& 0xfffffff8)) ;
303 AT91C_BASE_SSC
->SSC_THR
= 0x00; // clear TXRDY
306 uint8_t bits_to_shift
= 0x00;
307 for(size_t c
= 0; c
<= len
; c
++) {
308 uint8_t bits_to_send
= bits_to_shift
<< (8 - shift_delay
) | (c
==len
?0x00:cmd
[c
]) >> shift_delay
;
309 bits_to_shift
= cmd
[c
] & bitmask
;
310 for (int i
= 7; i
>= 0; i
--) {
311 for (int j
= 0; j
< (slow
?4:1); ) {
312 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
313 if (bits_to_send
>> i
& 0x01) {
314 AT91C_BASE_SSC
->SSC_THR
= 0xff;
316 AT91C_BASE_SSC
->SSC_THR
= 0x00;
328 //=============================================================================
329 // An ISO 15693 decoder for tag responses (one subcarrier only).
330 // Uses cross correlation to identify each bit and EOF.
331 // This function is called 8 times per bit (every 2 subcarrier cycles).
332 // Subcarrier frequency fs is 424kHz, 1/fs = 2,36us,
333 // i.e. function is called every 4,72us
335 // LED C -> ON once we have received the SOF and are expecting the rest.
336 // LED C -> OFF once we have received EOF or are unsynced
338 // Returns: true if we received a EOF
339 // false if we are still waiting for some more
340 //=============================================================================
342 #define NOISE_THRESHOLD 160 // don't try to correlate noise
344 typedef struct DecodeTag
{
348 STATE_TAG_SOF_HIGH_END
,
349 STATE_TAG_RECEIVING_DATA
,
368 static int inline __attribute__((always_inline
)) Handle15693SamplesFromTag(uint16_t amplitude
, DecodeTag_t
*DecodeTag
)
370 switch(DecodeTag
->state
) {
371 case STATE_TAG_SOF_LOW
:
372 // waiting for 12 times low (11 times low is accepted as well)
373 if (amplitude
< NOISE_THRESHOLD
) {
374 DecodeTag
->posCount
++;
376 if (DecodeTag
->posCount
> 10) {
377 DecodeTag
->posCount
= 1;
379 DecodeTag
->state
= STATE_TAG_SOF_HIGH
;
381 DecodeTag
->posCount
= 0;
386 case STATE_TAG_SOF_HIGH
:
387 // waiting for 10 times high. Take average over the last 8
388 if (amplitude
> NOISE_THRESHOLD
) {
389 DecodeTag
->posCount
++;
390 if (DecodeTag
->posCount
> 2) {
391 DecodeTag
->sum1
+= amplitude
; // keep track of average high value
393 if (DecodeTag
->posCount
== 10) {
394 DecodeTag
->sum1
>>= 4; // calculate half of average high value (8 samples)
395 DecodeTag
->state
= STATE_TAG_SOF_HIGH_END
;
397 } else { // high phase was too short
398 DecodeTag
->posCount
= 1;
399 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
403 case STATE_TAG_SOF_HIGH_END
:
404 // waiting for a falling edge
405 if (amplitude
< DecodeTag
->sum1
) { // signal drops below 50% average high: a falling edge
406 DecodeTag
->lastBit
= SOF_PART1
; // detected 1st part of SOF (12 samples low and 12 samples high)
407 DecodeTag
->shiftReg
= 0;
408 DecodeTag
->bitCount
= 0;
410 DecodeTag
->sum1
= amplitude
;
412 DecodeTag
->posCount
= 2;
413 DecodeTag
->state
= STATE_TAG_RECEIVING_DATA
;
416 DecodeTag
->posCount
++;
417 if (DecodeTag
->posCount
> 13) { // high phase too long
418 DecodeTag
->posCount
= 0;
419 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
425 case STATE_TAG_RECEIVING_DATA
:
426 if (DecodeTag
->posCount
== 1) {
430 if (DecodeTag
->posCount
<= 4) {
431 DecodeTag
->sum1
+= amplitude
;
433 DecodeTag
->sum2
+= amplitude
;
435 if (DecodeTag
->posCount
== 8) {
436 int32_t corr_1
= DecodeTag
->sum2
- DecodeTag
->sum1
;
437 int32_t corr_0
= -corr_1
;
438 int32_t corr_EOF
= (DecodeTag
->sum1
+ DecodeTag
->sum2
) / 2;
439 if (corr_EOF
> corr_0
&& corr_EOF
> corr_1
) {
440 if (DecodeTag
->lastBit
== LOGIC0
) { // this was already part of EOF
441 DecodeTag
->state
= STATE_TAG_EOF
;
443 DecodeTag
->posCount
= 0;
444 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
447 } else if (corr_1
> corr_0
) {
449 if (DecodeTag
->lastBit
== SOF_PART1
) { // still part of SOF
450 DecodeTag
->lastBit
= SOF_PART2
; // SOF completed
452 DecodeTag
->lastBit
= LOGIC1
;
453 DecodeTag
->shiftReg
>>= 1;
454 DecodeTag
->shiftReg
|= 0x80;
455 DecodeTag
->bitCount
++;
456 if (DecodeTag
->bitCount
== 8) {
457 DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
;
459 if (DecodeTag
->len
> DecodeTag
->max_len
) {
460 // buffer overflow, give up
461 DecodeTag
->posCount
= 0;
462 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
465 DecodeTag
->bitCount
= 0;
466 DecodeTag
->shiftReg
= 0;
471 if (DecodeTag
->lastBit
== SOF_PART1
) { // incomplete SOF
472 DecodeTag
->posCount
= 0;
473 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
476 DecodeTag
->lastBit
= LOGIC0
;
477 DecodeTag
->shiftReg
>>= 1;
478 DecodeTag
->bitCount
++;
479 if (DecodeTag
->bitCount
== 8) {
480 DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
;
482 if (DecodeTag
->len
> DecodeTag
->max_len
) {
483 // buffer overflow, give up
484 DecodeTag
->posCount
= 0;
485 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
488 DecodeTag
->bitCount
= 0;
489 DecodeTag
->shiftReg
= 0;
493 DecodeTag
->posCount
= 0;
495 DecodeTag
->posCount
++;
499 if (DecodeTag
->posCount
== 1) {
503 if (DecodeTag
->posCount
<= 4) {
504 DecodeTag
->sum1
+= amplitude
;
506 DecodeTag
->sum2
+= amplitude
;
508 if (DecodeTag
->posCount
== 8) {
509 int32_t corr_1
= DecodeTag
->sum2
- DecodeTag
->sum1
;
510 int32_t corr_0
= -corr_1
;
511 int32_t corr_EOF
= (DecodeTag
->sum1
+ DecodeTag
->sum2
) / 2;
512 if (corr_EOF
> corr_0
|| corr_1
> corr_0
) {
513 DecodeTag
->posCount
= 0;
514 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
521 DecodeTag
->posCount
++;
530 static void DecodeTagInit(DecodeTag_t
*DecodeTag
, uint8_t *data
, uint16_t max_len
)
532 DecodeTag
->posCount
= 0;
533 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
534 DecodeTag
->output
= data
;
535 DecodeTag
->max_len
= max_len
;
539 static void DecodeTagReset(DecodeTag_t
*DecodeTag
)
541 DecodeTag
->posCount
= 0;
542 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
547 * Receive and decode the tag response, also log to tracebuffer
549 static int GetIso15693AnswerFromTag(uint8_t* response
, uint16_t max_len
, int timeout
)
552 bool gotFrame
= false;
554 uint16_t *dmaBuf
= (uint16_t*)BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
*sizeof(uint16_t));
556 // the Decoder data structure
557 DecodeTag_t DecodeTag
= { 0 };
558 DecodeTagInit(&DecodeTag
, response
, max_len
);
560 // wait for last transfer to complete
561 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
563 // And put the FPGA in the appropriate mode
564 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_AMPLITUDE
);
566 // Setup and start DMA.
567 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
568 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
569 uint16_t *upTo
= dmaBuf
;
572 uint16_t behindBy
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
574 if (behindBy
== 0) continue;
576 uint16_t tagdata
= *upTo
++;
578 if(upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
579 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
580 if(behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
581 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
);
585 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
586 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
587 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
592 if (Handle15693SamplesFromTag(tagdata
, &DecodeTag
)) {
597 if (samples
> timeout
&& DecodeTag
.state
< STATE_TAG_RECEIVING_DATA
) {
607 if (DEBUG
) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
608 samples
, gotFrame
, DecodeTag
.state
, DecodeTag
.len
, DecodeTag
.bitCount
, DecodeTag
.posCount
);
610 if (DecodeTag
.len
> 0) {
611 LogTrace(DecodeTag
.output
, DecodeTag
.len
, 0, 0, NULL
, false);
614 return DecodeTag
.len
;
618 //=============================================================================
619 // An ISO15693 decoder for reader commands.
621 // This function is called 4 times per bit (every 2 subcarrier cycles).
622 // Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 2,36us
624 // LED B -> ON once we have received the SOF and are expecting the rest.
625 // LED B -> OFF once we have received EOF or are in error state or unsynced
627 // Returns: true if we received a EOF
628 // false if we are still waiting for some more
629 //=============================================================================
631 typedef struct DecodeReader
{
633 STATE_READER_UNSYNCD
,
634 STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
,
635 STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
,
636 STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
,
637 STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
,
638 STATE_READER_RECEIVE_DATA_1_OUT_OF_4
,
639 STATE_READER_RECEIVE_DATA_1_OUT_OF_256
655 static void DecodeReaderInit(DecodeReader_t
* DecodeReader
, uint8_t *data
, uint16_t max_len
)
657 DecodeReader
->output
= data
;
658 DecodeReader
->byteCountMax
= max_len
;
659 DecodeReader
->state
= STATE_READER_UNSYNCD
;
660 DecodeReader
->byteCount
= 0;
661 DecodeReader
->bitCount
= 0;
662 DecodeReader
->posCount
= 1;
663 DecodeReader
->shiftReg
= 0;
667 static void DecodeReaderReset(DecodeReader_t
* DecodeReader
)
669 DecodeReader
->state
= STATE_READER_UNSYNCD
;
673 static int inline __attribute__((always_inline
)) Handle15693SampleFromReader(uint8_t bit
, DecodeReader_t
*restrict DecodeReader
)
675 switch(DecodeReader
->state
) {
676 case STATE_READER_UNSYNCD
:
678 // we went low, so this could be the beginning of a SOF
679 DecodeReader
->posCount
= 1;
680 DecodeReader
->state
= STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
;
684 case STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
:
685 DecodeReader
->posCount
++;
686 if(bit
) { // detected rising edge
687 if(DecodeReader
->posCount
< 4) { // rising edge too early (nominally expected at 5)
688 DecodeReaderReset(DecodeReader
);
690 DecodeReader
->state
= STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
;
693 if(DecodeReader
->posCount
> 5) { // stayed low for too long
694 DecodeReaderReset(DecodeReader
);
696 // do nothing, keep waiting
701 case STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
:
702 DecodeReader
->posCount
++;
703 if(!bit
) { // detected a falling edge
704 if (DecodeReader
->posCount
< 20) { // falling edge too early (nominally expected at 21 earliest)
705 DecodeReaderReset(DecodeReader
);
706 } else if (DecodeReader
->posCount
< 23) { // SOF for 1 out of 4 coding
707 DecodeReader
->Coding
= CODING_1_OUT_OF_4
;
708 DecodeReader
->state
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
;
709 } else if (DecodeReader
->posCount
< 28) { // falling edge too early (nominally expected at 29 latest)
710 DecodeReaderReset(DecodeReader
);
711 } else { // SOF for 1 out of 4 coding
712 DecodeReader
->Coding
= CODING_1_OUT_OF_256
;
713 DecodeReader
->state
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
;
716 if(DecodeReader
->posCount
> 29) { // stayed high for too long
717 DecodeReaderReset(DecodeReader
);
719 // do nothing, keep waiting
724 case STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
:
725 DecodeReader
->posCount
++;
726 if (bit
) { // detected rising edge
727 if (DecodeReader
->Coding
== CODING_1_OUT_OF_256
) {
728 if (DecodeReader
->posCount
< 32) { // rising edge too early (nominally expected at 33)
729 DecodeReaderReset(DecodeReader
);
731 DecodeReader
->posCount
= 1;
732 DecodeReader
->bitCount
= 0;
733 DecodeReader
->byteCount
= 0;
734 DecodeReader
->sum1
= 1;
735 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_256
;
738 } else { // CODING_1_OUT_OF_4
739 if (DecodeReader
->posCount
< 24) { // rising edge too early (nominally expected at 25)
740 DecodeReaderReset(DecodeReader
);
742 DecodeReader
->state
= STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
;
746 if (DecodeReader
->Coding
== CODING_1_OUT_OF_256
) {
747 if (DecodeReader
->posCount
> 34) { // signal stayed low for too long
748 DecodeReaderReset(DecodeReader
);
750 // do nothing, keep waiting
752 } else { // CODING_1_OUT_OF_4
753 if (DecodeReader
->posCount
> 26) { // signal stayed low for too long
754 DecodeReaderReset(DecodeReader
);
756 // do nothing, keep waiting
762 case STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
:
763 DecodeReader
->posCount
++;
765 if (DecodeReader
->posCount
== 33) {
766 DecodeReader
->posCount
= 1;
767 DecodeReader
->bitCount
= 0;
768 DecodeReader
->byteCount
= 0;
769 DecodeReader
->sum1
= 1;
770 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_4
;
773 // do nothing, keep waiting
775 } else { // unexpected falling edge
776 DecodeReaderReset(DecodeReader
);
780 case STATE_READER_RECEIVE_DATA_1_OUT_OF_4
:
781 DecodeReader
->posCount
++;
782 if (DecodeReader
->posCount
== 1) {
783 DecodeReader
->sum1
= bit
;
784 } else if (DecodeReader
->posCount
<= 4) {
785 DecodeReader
->sum1
+= bit
;
786 } else if (DecodeReader
->posCount
== 5) {
787 DecodeReader
->sum2
= bit
;
789 DecodeReader
->sum2
+= bit
;
791 if (DecodeReader
->posCount
== 8) {
792 DecodeReader
->posCount
= 0;
793 int corr10
= DecodeReader
->sum1
- DecodeReader
->sum2
;
794 int corr01
= DecodeReader
->sum2
- DecodeReader
->sum1
;
795 int corr11
= (DecodeReader
->sum1
+ DecodeReader
->sum2
) / 2;
796 if (corr01
> corr11
&& corr01
> corr10
) { // EOF
797 LED_B_OFF(); // Finished receiving
798 DecodeReaderReset(DecodeReader
);
799 if (DecodeReader
->byteCount
!= 0) {
803 if (corr10
> corr11
) { // detected a 2bit position
804 DecodeReader
->shiftReg
>>= 2;
805 DecodeReader
->shiftReg
|= (DecodeReader
->bitCount
<< 6);
807 if (DecodeReader
->bitCount
== 15) { // we have a full byte
808 DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
;
809 if (DecodeReader
->byteCount
> DecodeReader
->byteCountMax
) {
810 // buffer overflow, give up
812 DecodeReaderReset(DecodeReader
);
814 DecodeReader
->bitCount
= 0;
815 DecodeReader
->shiftReg
= 0;
817 DecodeReader
->bitCount
++;
822 case STATE_READER_RECEIVE_DATA_1_OUT_OF_256
:
823 DecodeReader
->posCount
++;
824 if (DecodeReader
->posCount
== 1) {
825 DecodeReader
->sum1
= bit
;
826 } else if (DecodeReader
->posCount
<= 4) {
827 DecodeReader
->sum1
+= bit
;
828 } else if (DecodeReader
->posCount
== 5) {
829 DecodeReader
->sum2
= bit
;
831 DecodeReader
->sum2
+= bit
;
833 if (DecodeReader
->posCount
== 8) {
834 DecodeReader
->posCount
= 0;
835 int corr10
= DecodeReader
->sum1
- DecodeReader
->sum2
;
836 int corr01
= DecodeReader
->sum2
- DecodeReader
->sum1
;
837 int corr11
= (DecodeReader
->sum1
+ DecodeReader
->sum2
) / 2;
838 if (corr01
> corr11
&& corr01
> corr10
) { // EOF
839 LED_B_OFF(); // Finished receiving
840 DecodeReaderReset(DecodeReader
);
841 if (DecodeReader
->byteCount
!= 0) {
845 if (corr10
> corr11
) { // detected the bit position
846 DecodeReader
->shiftReg
= DecodeReader
->bitCount
;
848 if (DecodeReader
->bitCount
== 255) { // we have a full byte
849 DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
;
850 if (DecodeReader
->byteCount
> DecodeReader
->byteCountMax
) {
851 // buffer overflow, give up
853 DecodeReaderReset(DecodeReader
);
856 DecodeReader
->bitCount
++;
862 DecodeReaderReset(DecodeReader
);
870 //-----------------------------------------------------------------------------
871 // Receive a command (from the reader to us, where we are the simulated tag),
872 // and store it in the given buffer, up to the given maximum length. Keeps
873 // spinning, waiting for a well-framed command, until either we get one
874 // (returns true) or someone presses the pushbutton on the board (false).
876 // Assume that we're called with the SSC (to the FPGA) and ADC path set
878 //-----------------------------------------------------------------------------
880 static int GetIso15693CommandFromReader(uint8_t *received
, size_t max_len
, uint32_t *eof_time
)
883 bool gotFrame
= false;
886 uint8_t *dmaBuf
= BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
);
888 // the decoder data structure
889 DecodeReader_t DecodeReader
= {0};
890 DecodeReaderInit(&DecodeReader
, received
, max_len
);
892 // wait for last transfer to complete
893 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
896 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
898 // clear receive register and wait for next transfer
899 uint32_t temp
= AT91C_BASE_SSC
->SSC_RHR
;
901 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)) ;
903 uint32_t bit_time
= GetCountSspClk() & 0xfffffff8;
905 // Setup and start DMA.
906 FpgaSetupSscDma(dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
907 uint8_t *upTo
= dmaBuf
;
910 uint16_t behindBy
= ((uint8_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
912 if (behindBy
== 0) continue;
915 if(upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
916 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
917 if(behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
918 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
);
922 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
923 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
924 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
927 for (int i
= 7; i
>= 0; i
--) {
928 if (Handle15693SampleFromReader((b
>> i
) & 0x01, &DecodeReader
)) {
929 *eof_time
= bit_time
+ samples
- DELAY_READER_TO_ARM
; // end of EOF
940 if (BUTTON_PRESS()) {
941 DecodeReader
.byteCount
= 0;
950 BigBuf_free_keep_EM();
952 if (DEBUG
) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
953 samples
, gotFrame
, DecodeReader
.state
, DecodeReader
.byteCount
, DecodeReader
.bitCount
, DecodeReader
.posCount
);
955 if (DecodeReader
.byteCount
> 0) {
956 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, 0, 0, NULL
, true);
959 return DecodeReader
.byteCount
;
963 // Encode (into the ToSend buffers) an identify request, which is the first
964 // thing that you must send to a tag to get a response.
965 static void BuildIdentifyRequest(void)
970 // one sub-carrier, inventory, 1 slot, fast rate
971 // AFI is at bit 5 (1<<4) when doing an INVENTORY
972 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
973 // inventory command code
982 CodeIso15693AsReader(cmd
, sizeof(cmd
));
986 //-----------------------------------------------------------------------------
987 // Start to read an ISO 15693 tag. We send an identify request, then wait
988 // for the response. The response is not demodulated, just left in the buffer
989 // so that it can be downloaded to a PC and processed there.
990 //-----------------------------------------------------------------------------
991 void AcquireRawAdcSamplesIso15693(void)
996 uint8_t *dest
= BigBuf_get_addr();
998 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
999 BuildIdentifyRequest();
1001 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1003 // Give the tags time to energize
1005 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1008 // Now send the command
1009 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX
);
1010 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
1013 for(int c
= 0; c
< ToSendMax
; ) {
1014 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1015 AT91C_BASE_SSC
->SSC_THR
= ~ToSend
[c
];
1022 // wait for last transfer to complete
1023 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
1025 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1026 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_AMPLITUDE
);
1028 for(int c
= 0; c
< 4000; ) {
1029 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1030 uint16_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1035 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1040 void SnoopIso15693(void)
1042 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1049 // The DMA buffer, used to stream samples from the FPGA
1050 uint16_t* dmaBuf
= (uint16_t*)BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
*sizeof(uint16_t));
1053 // Count of samples received so far, so that we can include timing
1054 // information in the trace buffer.
1057 DecodeTag_t DecodeTag
= {0};
1058 uint8_t response
[ISO15693_MAX_RESPONSE_LENGTH
];
1059 DecodeTagInit(&DecodeTag
, response
, sizeof(response
));
1061 DecodeReader_t DecodeReader
= {0};;
1062 uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
];
1063 DecodeReaderInit(&DecodeReader
, cmd
, sizeof(cmd
));
1065 // Print some debug information about the buffer sizes
1067 Dbprintf("Snooping buffers initialized:");
1068 Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen());
1069 Dbprintf(" Reader -> tag: %i bytes", ISO15693_MAX_COMMAND_LENGTH
);
1070 Dbprintf(" tag -> Reader: %i bytes", ISO15693_MAX_RESPONSE_LENGTH
);
1071 Dbprintf(" DMA: %i bytes", ISO15693_DMA_BUFFER_SIZE
* sizeof(uint16_t));
1073 Dbprintf("Snoop started. Press button to stop.");
1075 // Signal field is off, no reader signal, no tag signal
1077 // And put the FPGA in the appropriate mode
1078 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_SNOOP
| FPGA_HF_READER_RX_XCORR_AMPLITUDE
);
1079 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1081 // Setup for the DMA.
1082 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1084 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1086 bool TagIsActive
= false;
1087 bool ReaderIsActive
= false;
1088 bool ExpectTagAnswer
= false;
1090 // And now we loop, receiving samples.
1092 uint16_t behindBy
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
1094 if (behindBy
== 0) continue;
1096 uint16_t snoopdata
= *upTo
++;
1098 if(upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
1099 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
1100 if(behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
1101 Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy
, samples
);
1104 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
1105 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
1106 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
1108 if(BUTTON_PRESS()) {
1109 DbpString("Snoop stopped.");
1116 if (!TagIsActive
) { // no need to try decoding reader data if the tag is sending
1117 if (Handle15693SampleFromReader(snoopdata
& 0x02, &DecodeReader
)) {
1118 FpgaDisableSscDma();
1119 ExpectTagAnswer
= true;
1120 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, samples
, samples
, NULL
, true);
1121 /* And ready to receive another command. */
1122 DecodeReaderReset(&DecodeReader
);
1123 /* And also reset the demod code, which might have been */
1124 /* false-triggered by the commands from the reader. */
1125 DecodeTagReset(&DecodeTag
);
1127 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1129 if (Handle15693SampleFromReader(snoopdata
& 0x01, &DecodeReader
)) {
1130 FpgaDisableSscDma();
1131 ExpectTagAnswer
= true;
1132 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, samples
, samples
, NULL
, true);
1133 /* And ready to receive another command. */
1134 DecodeReaderReset(&DecodeReader
);
1135 /* And also reset the demod code, which might have been */
1136 /* false-triggered by the commands from the reader. */
1137 DecodeTagReset(&DecodeTag
);
1139 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1141 ReaderIsActive
= (DecodeReader
.state
>= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
);
1144 if (!ReaderIsActive
&& ExpectTagAnswer
) { // no need to try decoding tag data if the reader is currently sending or no answer expected yet
1145 if (Handle15693SamplesFromTag(snoopdata
>> 2, &DecodeTag
)) {
1146 FpgaDisableSscDma();
1147 //Use samples as a time measurement
1148 LogTrace(DecodeTag
.output
, DecodeTag
.len
, samples
, samples
, NULL
, false);
1149 // And ready to receive another response.
1150 DecodeTagReset(&DecodeTag
);
1151 DecodeReaderReset(&DecodeReader
);
1152 ExpectTagAnswer
= false;
1154 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1156 TagIsActive
= (DecodeTag
.state
>= STATE_TAG_RECEIVING_DATA
);
1161 FpgaDisableSscDma();
1166 DbpString("Snoop statistics:");
1167 Dbprintf(" ExpectTagAnswer: %d", ExpectTagAnswer
);
1168 Dbprintf(" DecodeTag State: %d", DecodeTag
.state
);
1169 Dbprintf(" DecodeTag byteCnt: %d", DecodeTag
.len
);
1170 Dbprintf(" DecodeReader State: %d", DecodeReader
.state
);
1171 Dbprintf(" DecodeReader byteCnt: %d", DecodeReader
.byteCount
);
1172 Dbprintf(" Trace length: %d", BigBuf_get_traceLen());
1176 // Initialize the proxmark as iso15k reader
1177 // (this might produces glitches that confuse some tags
1178 static void Iso15693InitReader() {
1179 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1183 // Start from off (no field generated)
1185 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1188 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1189 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1191 // Give the tags time to energize
1193 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1197 ///////////////////////////////////////////////////////////////////////
1198 // ISO 15693 Part 3 - Air Interface
1199 // This section basically contains transmission and receiving of bits
1200 ///////////////////////////////////////////////////////////////////////
1203 // uid is in transmission order (which is reverse of display order)
1204 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
1209 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1210 // followed by the block data
1211 cmd
[0] = ISO15693_REQ_OPTION
| ISO15693_REQ_ADDRESS
| ISO15693_REQ_DATARATE_HIGH
;
1212 // READ BLOCK command code
1213 cmd
[1] = ISO15693_READBLOCK
;
1214 // UID may be optionally specified here
1223 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
1224 // Block number to read
1225 cmd
[10] = blockNumber
;
1227 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 11 bytes
1228 cmd
[11] = crc
& 0xff;
1231 CodeIso15693AsReader(cmd
, sizeof(cmd
));
1235 // Now the VICC>VCD responses when we are simulating a tag
1236 static void BuildInventoryResponse(uint8_t *uid
)
1242 cmd
[0] = 0; // No error, no protocol format extension
1243 cmd
[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
1245 cmd
[2] = uid
[7]; //0x32;
1246 cmd
[3] = uid
[6]; //0x4b;
1247 cmd
[4] = uid
[5]; //0x03;
1248 cmd
[5] = uid
[4]; //0x01;
1249 cmd
[6] = uid
[3]; //0x00;
1250 cmd
[7] = uid
[2]; //0x10;
1251 cmd
[8] = uid
[1]; //0x05;
1252 cmd
[9] = uid
[0]; //0xe0;
1255 cmd
[10] = crc
& 0xff;
1258 CodeIso15693AsTag(cmd
, sizeof(cmd
));
1261 // Universal Method for sending to and recv bytes from a tag
1262 // init ... should we initialize the reader?
1263 // speed ... 0 low speed, 1 hi speed
1264 // *recv will contain the tag's answer
1265 // return: lenght of received data
1266 int SendDataTag(uint8_t *send
, int sendlen
, bool init
, int speed
, uint8_t *recv
, uint16_t max_recv_len
, uint32_t start_time
) {
1272 if (init
) Iso15693InitReader();
1277 // low speed (1 out of 256)
1278 CodeIso15693AsReader256(send
, sendlen
);
1280 // high speed (1 out of 4)
1281 CodeIso15693AsReader(send
, sendlen
);
1284 TransmitTo15693Tag(ToSend
, ToSendMax
, start_time
);
1286 // Now wait for a response
1288 answerLen
= GetIso15693AnswerFromTag(recv
, max_recv_len
, DELAY_ISO15693_VCD_TO_VICC
* 2);
1297 // --------------------------------------------------------------------
1299 // --------------------------------------------------------------------
1301 // Decodes a message from a tag and displays its metadata and content
1302 #define DBD15STATLEN 48
1303 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
1304 char status
[DBD15STATLEN
+1]={0};
1308 if (d
[0] & ISO15693_RES_EXT
)
1309 strncat(status
,"ProtExt ", DBD15STATLEN
);
1310 if (d
[0] & ISO15693_RES_ERROR
) {
1312 strncat(status
,"Error ", DBD15STATLEN
);
1315 strncat(status
,"01:notSupp", DBD15STATLEN
);
1318 strncat(status
,"02:notRecog", DBD15STATLEN
);
1321 strncat(status
,"03:optNotSupp", DBD15STATLEN
);
1324 strncat(status
,"0f:noInfo", DBD15STATLEN
);
1327 strncat(status
,"10:doesn'tExist", DBD15STATLEN
);
1330 strncat(status
,"11:lockAgain", DBD15STATLEN
);
1333 strncat(status
,"12:locked", DBD15STATLEN
);
1336 strncat(status
,"13:progErr", DBD15STATLEN
);
1339 strncat(status
,"14:lockErr", DBD15STATLEN
);
1342 strncat(status
,"unknownErr", DBD15STATLEN
);
1344 strncat(status
," ", DBD15STATLEN
);
1346 strncat(status
,"NoErr ", DBD15STATLEN
);
1350 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
1351 strncat(status
,"CrcOK",DBD15STATLEN
);
1353 strncat(status
,"CrcFail!",DBD15STATLEN
);
1355 Dbprintf("%s",status
);
1361 ///////////////////////////////////////////////////////////////////////
1362 // Functions called via USB/Client
1363 ///////////////////////////////////////////////////////////////////////
1365 void SetDebugIso15693(uint32_t debug
) {
1367 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
1372 //-----------------------------------------------------------------------------
1373 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
1374 // all demodulation performed in arm rather than host. - greg
1375 //-----------------------------------------------------------------------------
1376 void ReaderIso15693(uint32_t parameter
)
1384 uint8_t TagUID
[8] = {0x00};
1386 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1388 uint8_t answer
[ISO15693_MAX_RESPONSE_LENGTH
];
1390 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1392 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1394 // Start from off (no field generated)
1395 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1398 // Give the tags time to energize
1400 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1405 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1406 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1408 // Now send the IDENTIFY command
1409 BuildIdentifyRequest();
1410 TransmitTo15693Tag(ToSend
, ToSendMax
, 0);
1412 // Now wait for a response
1413 answerLen
= GetIso15693AnswerFromTag(answer
, sizeof(answer
), DELAY_ISO15693_VCD_TO_VICC
* 2) ;
1414 uint32_t start_time
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD
;
1416 if (answerLen
>=12) // we should do a better check than this
1418 TagUID
[0] = answer
[2];
1419 TagUID
[1] = answer
[3];
1420 TagUID
[2] = answer
[4];
1421 TagUID
[3] = answer
[5];
1422 TagUID
[4] = answer
[6];
1423 TagUID
[5] = answer
[7];
1424 TagUID
[6] = answer
[8]; // IC Manufacturer code
1425 TagUID
[7] = answer
[9]; // always E0
1429 Dbprintf("%d octets read from IDENTIFY request:", answerLen
);
1430 DbdecodeIso15693Answer(answerLen
, answer
);
1431 Dbhexdump(answerLen
, answer
, false);
1434 if (answerLen
>= 12)
1435 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
1436 TagUID
[7],TagUID
[6],TagUID
[5],TagUID
[4],
1437 TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1440 // Dbprintf("%d octets read from SELECT request:", answerLen2);
1441 // DbdecodeIso15693Answer(answerLen2,answer2);
1442 // Dbhexdump(answerLen2,answer2,true);
1444 // Dbprintf("%d octets read from XXX request:", answerLen3);
1445 // DbdecodeIso15693Answer(answerLen3,answer3);
1446 // Dbhexdump(answerLen3,answer3,true);
1449 if (answerLen
>= 12 && DEBUG
) {
1451 // debugptr = BigBuf_get_addr();
1454 while (i
< 32) { // sanity check, assume max 32 pages
1455 BuildReadBlockRequest(TagUID
, i
);
1456 TransmitTo15693Tag(ToSend
, ToSendMax
, start_time
);
1457 int answerLen
= GetIso15693AnswerFromTag(answer
, sizeof(answer
), DELAY_ISO15693_VCD_TO_VICC
* 2);
1458 start_time
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD
;
1459 if (answerLen
> 0) {
1460 Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i
, answerLen
);
1461 DbdecodeIso15693Answer(answerLen
, answer
);
1462 Dbhexdump(answerLen
, answer
, false);
1463 if ( *((uint32_t*) answer
) == 0x07160101 ) break; // exit on NoPageErr
1469 // for the time being, switch field off to protect rdv4.0
1470 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1471 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1478 // Simulate an ISO15693 TAG.
1479 // For Inventory command: print command and send Inventory Response with given UID
1480 // TODO: interpret other reader commands and send appropriate response
1481 void SimTagIso15693(uint32_t parameter
, uint8_t *uid
)
1486 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1487 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1488 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
1489 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR
);
1493 uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
];
1495 // Build a suitable response to the reader INVENTORY command
1496 BuildInventoryResponse(uid
);
1499 while (!BUTTON_PRESS()) {
1500 uint32_t eof_time
= 0, start_time
= 0;
1501 int cmd_len
= GetIso15693CommandFromReader(cmd
, sizeof(cmd
), &eof_time
);
1503 if ((cmd_len
>= 5) && (cmd
[0] & ISO15693_REQ_INVENTORY
) && (cmd
[1] == ISO15693_INVENTORY
)) { // TODO: check more flags
1504 bool slow
= !(cmd
[0] & ISO15693_REQ_DATARATE_HIGH
);
1505 start_time
= eof_time
+ DELAY_ISO15693_VCD_TO_VICC
- DELAY_ARM_TO_READER
;
1506 TransmitTo15693Reader(ToSend
, ToSendMax
, start_time
, slow
);
1509 Dbprintf("%d bytes read from reader:", cmd_len
);
1510 Dbhexdump(cmd_len
, cmd
, false);
1517 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1518 // (some manufactures offer a way to read the AFI, though)
1519 void BruteforceIso15693Afi(uint32_t speed
)
1525 uint8_t recv
[ISO15693_MAX_RESPONSE_LENGTH
];
1527 int datalen
=0, recvlen
=0;
1529 Iso15693InitReader();
1532 // first without AFI
1533 // Tags should respond without AFI and with AFI=0 even when AFI is active
1535 data
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
;
1536 data
[1] = ISO15693_INVENTORY
;
1537 data
[2] = 0; // mask length
1538 datalen
= AddCrc(data
,3);
1539 recvlen
= SendDataTag(data
, datalen
, false, speed
, recv
, sizeof(recv
), 0);
1540 uint32_t start_time
= GetCountSspClk() + DELAY_ISO15693_VCD_TO_VICC
;
1543 Dbprintf("NoAFI UID=%s", sprintUID(NULL
, &recv
[2]));
1548 data
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_AFI
| ISO15693_REQINV_SLOT1
;
1549 data
[1] = ISO15693_INVENTORY
;
1551 data
[3] = 0; // mask length
1553 for (int i
= 0; i
< 256; i
++) {
1555 datalen
= AddCrc(data
,4);
1556 recvlen
= SendDataTag(data
, datalen
, false, speed
, recv
, sizeof(recv
), start_time
);
1557 start_time
= GetCountSspClk() + DELAY_ISO15693_VCD_TO_VICC
;
1559 if (recvlen
>= 12) {
1560 Dbprintf("AFI=%i UID=%s", i
, sprintUID(NULL
, &recv
[2]));
1563 Dbprintf("AFI Bruteforcing done.");
1565 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1569 // Allows to directly send commands to the tag via the client
1570 void DirectTag15693Command(uint32_t datalen
, uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1573 uint8_t recvbuf
[ISO15693_MAX_RESPONSE_LENGTH
];
1579 Dbhexdump(datalen
, data
, false);
1582 recvlen
= SendDataTag(data
, datalen
, true, speed
, (recv
?recvbuf
:NULL
), sizeof(recvbuf
), 0);
1587 Dbhexdump(recvlen
, recvbuf
, false);
1588 DbdecodeIso15693Answer(recvlen
, recvbuf
);
1591 cmd_send(CMD_ACK
, recvlen
>ISO15693_MAX_RESPONSE_LENGTH
?ISO15693_MAX_RESPONSE_LENGTH
:recvlen
, 0, 0, recvbuf
, ISO15693_MAX_RESPONSE_LENGTH
);
1595 // for the time being, switch field off to protect rdv4.0
1596 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1597 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1606 // --------------------------------------------------------------------
1607 // -- Misc & deprecated functions
1608 // --------------------------------------------------------------------
1612 // do not use; has a fix UID
1613 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1618 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1619 // followed by teh block data
1620 // one sub-carrier, inventory, 1 slot, fast rate
1621 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1622 // System Information command code
1624 // UID may be optionally specified here
1633 cmd[9]= 0xe0; // always e0 (not exactly unique)
1635 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1636 cmd[10] = crc & 0xff;
1639 CodeIso15693AsReader(cmd, sizeof(cmd));
1643 // do not use; has a fix UID
1644 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1649 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1650 // followed by teh block data
1651 // one sub-carrier, inventory, 1 slot, fast rate
1652 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1653 // READ Multi BLOCK command code
1655 // UID may be optionally specified here
1664 cmd[9]= 0xe0; // always e0 (not exactly unique)
1665 // First Block number to read
1667 // Number of Blocks to read
1668 cmd[11] = 0x2f; // read quite a few
1670 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1671 cmd[12] = crc & 0xff;
1674 CodeIso15693AsReader(cmd, sizeof(cmd));
1677 // do not use; has a fix UID
1678 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1683 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1684 // followed by teh block data
1685 // one sub-carrier, inventory, 1 slot, fast rate
1686 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1687 // READ BLOCK command code
1689 // UID may be optionally specified here
1698 cmd[9]= 0xe0; // always e0 (not exactly unique)
1704 // cmd[13] = 0x00; //Now the CRC
1705 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1706 cmd[12] = crc & 0xff;
1709 CodeIso15693AsReader(cmd, sizeof(cmd));
1712 // do not use; has a fix UID
1713 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1718 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1719 // followed by teh block data
1720 // one sub-carrier, inventory, 1 slot, fast rate
1721 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1722 // READ BLOCK command code
1724 // UID may be optionally specified here
1733 cmd[9]= 0xe0; // always e0 (not exactly unique)
1735 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1739 // cmd[13] = 0x00; //Now the CRC
1740 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1741 cmd[12] = crc & 0xff;
1744 CodeIso15693AsReader(cmd, sizeof(cmd));