1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, split Nov 2006
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // Routines to support ISO 14443. This includes both the reader software and
9 // the `fake tag' modes. At the moment only the Type B modulation is
11 //-----------------------------------------------------------------------------
13 #include "proxmark3.h"
18 #include "iso14443crc.h"
20 //static void GetSamplesFor14443(int weTx, int n);
22 /*#define DEMOD_TRACE_SIZE 4096
23 #define READER_TAG_BUFFER_SIZE 2048
24 #define TAG_READER_BUFFER_SIZE 2048
25 #define DEMOD_DMA_BUFFER_SIZE 1024
28 #define RECEIVE_SAMPLES_TIMEOUT 2000
30 //=============================================================================
31 // An ISO 14443 Type B tag. We listen for commands from the reader, using
32 // a UART kind of thing that's implemented in software. When we get a
33 // frame (i.e., a group of bytes between SOF and EOF), we check the CRC.
34 // If it's good, then we can do something appropriate with it, and send
36 //=============================================================================
38 //-----------------------------------------------------------------------------
39 // Code up a string of octets at layer 2 (including CRC, we don't generate
40 // that here) so that they can be transmitted to the reader. Doesn't transmit
41 // them yet, just leaves them ready to send in ToSend[].
42 //-----------------------------------------------------------------------------
43 static void CodeIso14443bAsTag(const uint8_t *cmd
, int len
)
49 // Transmit a burst of ones, as the initial thing that lets the
50 // reader get phase sync. This (TR1) must be > 80/fs, per spec,
51 // but tag that I've tried (a Paypass) exceeds that by a fair bit,
53 for(i
= 0; i
< 20; i
++) {
61 for(i
= 0; i
< 10; i
++) {
67 for(i
= 0; i
< 2; i
++) {
74 for(i
= 0; i
< len
; i
++) {
85 for(j
= 0; j
< 8; j
++) {
108 for(i
= 0; i
< 10; i
++) {
114 for(i
= 0; i
< 10; i
++) {
121 // Convert from last byte pos to length
124 // Add a few more for slop
128 //-----------------------------------------------------------------------------
129 // The software UART that receives commands from the reader, and its state
131 //-----------------------------------------------------------------------------
135 STATE_GOT_FALLING_EDGE_OF_SOF
,
136 STATE_AWAITING_START_BIT
,
137 STATE_RECEIVING_DATA
,
148 /* Receive & handle a bit coming from the reader.
151 * LED A -> ON once we have received the SOF and are expecting the rest.
152 * LED A -> OFF once we have received EOF or are in error state or unsynced
154 * Returns: true if we received a EOF
155 * false if we are still waiting for some more
157 static int Handle14443UartBit(int bit
)
162 // we went low, so this could be the beginning
164 Uart
.state
= STATE_GOT_FALLING_EDGE_OF_SOF
;
170 case STATE_GOT_FALLING_EDGE_OF_SOF
:
172 if(Uart
.posCnt
== 2) {
174 if(Uart
.bitCnt
>= 10) {
175 // we've seen enough consecutive
176 // zeros that it's a valid SOF
179 Uart
.state
= STATE_AWAITING_START_BIT
;
180 LED_A_ON(); // Indicate we got a valid SOF
182 // didn't stay down long enough
183 // before going high, error
184 Uart
.state
= STATE_ERROR_WAIT
;
187 // do nothing, keep waiting
191 if(Uart
.posCnt
>= 4) Uart
.posCnt
= 0;
192 if(Uart
.bitCnt
> 14) {
193 // Give up if we see too many zeros without
195 Uart
.state
= STATE_ERROR_WAIT
;
199 case STATE_AWAITING_START_BIT
:
202 if(Uart
.posCnt
> 25) {
203 // stayed high for too long between
205 Uart
.state
= STATE_ERROR_WAIT
;
208 // falling edge, this starts the data byte
212 Uart
.state
= STATE_RECEIVING_DATA
;
216 case STATE_RECEIVING_DATA
:
218 if(Uart
.posCnt
== 2) {
219 // time to sample a bit
222 Uart
.shiftReg
|= 0x200;
226 if(Uart
.posCnt
>= 4) {
229 if(Uart
.bitCnt
== 10) {
230 if((Uart
.shiftReg
& 0x200) && !(Uart
.shiftReg
& 0x001))
232 // this is a data byte, with correct
233 // start and stop bits
234 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
>> 1) & 0xff;
237 if(Uart
.byteCnt
>= Uart
.byteCntMax
) {
238 // Buffer overflowed, give up
240 Uart
.state
= STATE_ERROR_WAIT
;
242 // so get the next byte now
244 Uart
.state
= STATE_AWAITING_START_BIT
;
246 } else if(Uart
.shiftReg
== 0x000) {
247 // this is an EOF byte
248 LED_A_OFF(); // Finished receiving
253 Uart
.state
= STATE_ERROR_WAIT
;
258 case STATE_ERROR_WAIT
:
259 // We're all screwed up, so wait a little while
260 // for whatever went wrong to finish, and then
263 if(Uart
.posCnt
> 10) {
264 Uart
.state
= STATE_UNSYNCD
;
270 Uart
.state
= STATE_UNSYNCD
;
277 //-----------------------------------------------------------------------------
278 // Receive a command (from the reader to us, where we are the simulated tag),
279 // and store it in the given buffer, up to the given maximum length. Keeps
280 // spinning, waiting for a well-framed command, until either we get one
281 // (returns TRUE) or someone presses the pushbutton on the board (FALSE).
283 // Assume that we're called with the SSC (to the FPGA) and ADC path set
285 //-----------------------------------------------------------------------------
286 static int GetIso14443CommandFromReader(uint8_t *received
, int *len
, int maxLen
)
291 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
292 // only, since we are receiving, not transmitting).
293 // Signal field is off with the appropriate LED
295 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
298 // Now run a `software UART' on the stream of incoming samples.
299 Uart
.output
= received
;
300 Uart
.byteCntMax
= maxLen
;
301 Uart
.state
= STATE_UNSYNCD
;
306 if(BUTTON_PRESS()) return FALSE
;
308 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
309 AT91C_BASE_SSC
->SSC_THR
= 0x00;
311 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
312 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
315 for(i
= 0; i
< 8; i
++, mask
>>= 1) {
317 if(Handle14443UartBit(bit
)) {
326 //-----------------------------------------------------------------------------
327 // Main loop of simulated tag: receive commands from reader, decide what
328 // response to send, and send it.
329 //-----------------------------------------------------------------------------
330 void SimulateIso14443Tag(void)
332 static const uint8_t cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
333 static const uint8_t response1
[] = {
334 0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, 0x22,
335 0x00, 0x21, 0x85, 0x5e, 0xd7
341 uint8_t *resp1
= BigBuf_get_addr() + 800;
344 uint8_t *receivedCmd
= BigBuf_get_addr();
351 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
352 memset(receivedCmd
, 0x44, 400);
354 CodeIso14443bAsTag(response1
, sizeof(response1
));
355 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
357 // We need to listen to the high-frequency, peak-detected path.
358 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
366 if(!GetIso14443CommandFromReader(receivedCmd
, &len
, 100)) {
367 Dbprintf("button pressed, received %d commands", cmdsRecvd
);
371 // Good, look at the command now.
373 if(len
== sizeof(cmd1
) && memcmp(receivedCmd
, cmd1
, len
)==0) {
374 resp
= resp1
; respLen
= resp1Len
;
376 Dbprintf("new cmd from reader: len=%d, cmdsRecvd=%d", len
, cmdsRecvd
);
377 // And print whether the CRC fails, just for good measure
378 ComputeCrc14443(CRC_14443_B
, receivedCmd
, len
-2, &b1
, &b2
);
379 if(b1
!= receivedCmd
[len
-2] || b2
!= receivedCmd
[len
-1]) {
380 // Not so good, try again.
381 DbpString("+++CRC fail");
383 DbpString("CRC passes");
388 memset(receivedCmd
, 0x44, 32);
392 if(cmdsRecvd
> 0x30) {
393 DbpString("many commands later...");
397 if(respLen
<= 0) continue;
400 // Signal field is off with the appropriate LED
402 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_BPSK
);
403 AT91C_BASE_SSC
->SSC_THR
= 0xff;
406 // Transmit the response.
409 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
412 AT91C_BASE_SSC
->SSC_THR
= b
;
419 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
420 volatile uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
427 //=============================================================================
428 // An ISO 14443 Type B reader. We take layer two commands, code them
429 // appropriately, and then send them to the tag. We then listen for the
430 // tag's response, which we leave in the buffer to be demodulated on the
432 //=============================================================================
437 DEMOD_PHASE_REF_TRAINING
,
438 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
439 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
440 DEMOD_AWAITING_START_BIT
,
441 DEMOD_RECEIVING_DATA
,
457 * Handles reception of a bit from the tag
460 * LED C -> ON once we have received the SOF and are expecting the rest.
461 * LED C -> OFF once we have received EOF or are unsynced
463 * Returns: true if we received a EOF
464 * false if we are still waiting for some more
467 static RAMFUNC
int Handle14443SamplesDemod(int ci
, int cq
)
471 // The soft decision on the bit uses an estimate of just the
472 // quadrant of the reference angle, not the exact angle.
473 #define MAKE_SOFT_DECISION() { \
474 if(Demod.sumI > 0) { \
479 if(Demod.sumQ > 0) { \
486 switch(Demod
.state
) {
497 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
503 case DEMOD_PHASE_REF_TRAINING
:
504 if(Demod
.posCount
< 8) {
507 } else if(Demod
.posCount
> 100) {
508 // error, waited too long
509 Demod
.state
= DEMOD_UNSYNCD
;
511 MAKE_SOFT_DECISION();
513 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
520 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
521 MAKE_SOFT_DECISION();
523 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
526 if(Demod
.posCount
> 100) {
527 Demod
.state
= DEMOD_UNSYNCD
;
533 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
534 MAKE_SOFT_DECISION();
536 if(Demod
.posCount
< 12) {
537 Demod
.state
= DEMOD_UNSYNCD
;
539 LED_C_ON(); // Got SOF
540 Demod
.state
= DEMOD_AWAITING_START_BIT
;
547 if(Demod
.posCount
> 100) {
548 Demod
.state
= DEMOD_UNSYNCD
;
555 case DEMOD_AWAITING_START_BIT
:
556 MAKE_SOFT_DECISION();
558 if(Demod
.posCount
> 10) {
559 Demod
.state
= DEMOD_UNSYNCD
;
567 Demod
.state
= DEMOD_RECEIVING_DATA
;
571 case DEMOD_RECEIVING_DATA
:
572 MAKE_SOFT_DECISION();
573 if(Demod
.posCount
== 0) {
579 if(Demod
.thisBit
> 0) {
580 Demod
.metric
+= Demod
.thisBit
;
582 Demod
.metric
-= Demod
.thisBit
;
586 Demod
.shiftReg
>>= 1;
587 if(Demod
.thisBit
> 0) {
588 Demod
.shiftReg
|= 0x200;
592 if(Demod
.bitCount
== 10) {
593 uint16_t s
= Demod
.shiftReg
;
594 if((s
& 0x200) && !(s
& 0x001)) {
595 uint8_t b
= (s
>> 1);
596 Demod
.output
[Demod
.len
] = b
;
598 Demod
.state
= DEMOD_AWAITING_START_BIT
;
600 Demod
.state
= DEMOD_UNSYNCD
;
613 Demod
.state
= DEMOD_UNSYNCD
;
622 static void DemodReset()
624 // Clear out the state of the "UART" that receives from the tag.
626 Demod
.state
= DEMOD_UNSYNCD
;
627 memset(Demod
.output
, 0x00, MAX_FRAME_SIZE
);
631 static void DemodInit(uint8_t *data
)
638 static void UartReset()
640 Uart
.byteCntMax
= MAX_FRAME_SIZE
;
641 Uart
.state
= STATE_UNSYNCD
;
647 static void UartInit(uint8_t *data
)
655 * Demodulate the samples we received from the tag, also log to tracebuffer
656 * weTx: set to 'TRUE' if we behave like a reader
657 * set to 'FALSE' if we behave like a snooper
658 * quiet: set to 'TRUE' to disable debug output
660 static void GetSamplesFor14443Demod(int weTx
, int n
, int quiet
)
663 int gotFrame
= FALSE
;
664 int lastRxCounter
, ci
, cq
, samples
= 0;
666 // Allocate memory from BigBuf for some buffers
667 // free all previous allocations first
670 // The response (tag -> reader) that we're receiving.
671 uint8_t *receivedResponse
= BigBuf_malloc(MAX_FRAME_SIZE
);
673 // The DMA buffer, used to stream samples from the FPGA
674 int8_t *dmaBuf
= (int8_t*) BigBuf_malloc(DMA_BUFFER_SIZE
);
676 // Set up the demodulator for tag -> reader responses.
677 DemodInit(receivedResponse
);
679 // Setup and start DMA.
680 FpgaSetupSscDma((uint8_t*) dmaBuf
, DMA_BUFFER_SIZE
);
682 int8_t *upTo
= dmaBuf
;
683 lastRxCounter
= DMA_BUFFER_SIZE
;
685 // Signal field is ON with the appropriate LED:
686 if (weTx
) LED_D_ON(); else LED_D_OFF();
687 // And put the FPGA in the appropriate mode
689 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
690 (weTx
? 0 : FPGA_HF_READER_RX_XCORR_SNOOP
));
693 int behindBy
= lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
694 if(behindBy
> max
) max
= behindBy
;
696 while(((lastRxCounter
-AT91C_BASE_PDC_SSC
->PDC_RCR
) & (DMA_BUFFER_SIZE
-1))
702 if(upTo
>= dmaBuf
+ DMA_BUFFER_SIZE
) {
704 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
705 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
708 if(lastRxCounter
<= 0) {
709 lastRxCounter
+= DMA_BUFFER_SIZE
;
714 if(Handle14443SamplesDemod(ci
, cq
)) {
723 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
724 if (!quiet
) Dbprintf("%x %x %x", max
, gotFrame
, Demod
.len
);
726 if (tracing
&& Demod
.len
> 0) {
727 uint8_t parity
[MAX_PARITY_SIZE
];
728 GetParity(Demod
.output
, Demod
.len
, parity
);
729 LogTrace(Demod
.output
, Demod
.len
, 0, 0, parity
, FALSE
);
734 //-----------------------------------------------------------------------------
735 // Read the tag's response. We just receive a stream of slightly-processed
736 // samples from the FPGA, which we will later do some signal processing on,
738 //-----------------------------------------------------------------------------
739 /*static void GetSamplesFor14443(int weTx, int n)
741 uint8_t *dest = (uint8_t *)BigBuf;
745 FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |
746 (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));
750 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
751 AT91C_BASE_SSC->SSC_THR = 0x43;
753 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
755 b = (int8_t)AT91C_BASE_SSC->SSC_RHR;
757 dest[c++] = (uint8_t)b;
767 //-----------------------------------------------------------------------------
768 // Transmit the command (to the tag) that was placed in ToSend[].
769 //-----------------------------------------------------------------------------
770 static void TransmitFor14443(void)
776 while(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
777 AT91C_BASE_SSC
->SSC_THR
= 0xff;
780 // Signal field is ON with the appropriate Red LED
782 // Signal we are transmitting with the Green LED
785 FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
787 for(c
= 0; c
< 10;) {
788 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
789 AT91C_BASE_SSC
->SSC_THR
= 0xff;
792 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
793 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
801 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
802 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
808 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
809 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
814 LED_B_OFF(); // Finished sending
818 //-----------------------------------------------------------------------------
819 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
820 // so that it is ready to transmit to the tag using TransmitFor14443().
821 //-----------------------------------------------------------------------------
822 static void CodeIso14443bAsReader(const uint8_t *cmd
, int len
)
829 // Establish initial reference level
830 for(i
= 0; i
< 40; i
++) {
834 for(i
= 0; i
< 10; i
++) {
838 for(i
= 0; i
< len
; i
++) {
846 for(j
= 0; j
< 8; j
++) {
857 for(i
= 0; i
< 10; i
++) {
860 for(i
= 0; i
< 8; i
++) {
864 // And then a little more, to make sure that the last character makes
865 // it out before we switch to rx mode.
866 for(i
= 0; i
< 24; i
++) {
870 // Convert from last character reference to length
875 //-----------------------------------------------------------------------------
876 // Read an ISO 14443 tag. We send it some set of commands, and record the
878 // The command name is misleading, it actually decodes the reponse in HEX
879 // into the output buffer (read the result using hexsamples, not hisamples)
881 // obsolete function only for test
882 //-----------------------------------------------------------------------------
883 void AcquireRawAdcSamplesIso14443(uint32_t parameter
)
885 uint8_t cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
887 SendRawCommand14443B(sizeof(cmd1
),1,1,cmd1
);
892 Convenience function to encode, transmit and trace iso 14443b comms
894 static void CodeAndTransmit14443bAsReader(const uint8_t *cmd
, int len
)
896 CodeIso14443bAsReader(cmd
, len
);
899 uint8_t parity
[MAX_PARITY_SIZE
];
900 GetParity(cmd
, len
, parity
);
901 LogTrace(cmd
,len
, 0, 0, parity
, TRUE
);
906 //-----------------------------------------------------------------------------
907 // Read a SRI512 ISO 14443 tag.
909 // SRI512 tags are just simple memory tags, here we're looking at making a dump
910 // of the contents of the memory. No anticollision algorithm is done, we assume
911 // we have a single tag in the field.
913 // I tried to be systematic and check every answer of the tag, every CRC, etc...
914 //-----------------------------------------------------------------------------
915 void ReadSTMemoryIso14443(uint32_t dwLast
)
922 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
923 // Make sure that we start from off, since the tags are stateful;
924 // confusing things will happen if we don't reset them between reads.
926 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
929 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
932 // Now give it time to spin up.
933 // Signal field is on with the appropriate LED
936 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
939 // First command: wake up the tag using the INITIATE command
940 uint8_t cmd1
[] = { 0x06, 0x00, 0x97, 0x5b};
942 CodeAndTransmit14443bAsReader(cmd1
, sizeof(cmd1
));
944 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
947 if (Demod
.len
== 0) {
948 DbpString("No response from tag");
951 Dbprintf("Randomly generated UID from tag (+ 2 byte CRC): %x %x %x",
952 Demod
.output
[0], Demod
.output
[1],Demod
.output
[2]);
954 // There is a response, SELECT the uid
955 DbpString("Now SELECT tag:");
956 cmd1
[0] = 0x0E; // 0x0E is SELECT
957 cmd1
[1] = Demod
.output
[0];
958 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
959 CodeAndTransmit14443bAsReader(cmd1
, sizeof(cmd1
));
962 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
964 if (Demod
.len
!= 3) {
965 Dbprintf("Expected 3 bytes from tag, got %d", Demod
.len
);
968 // Check the CRC of the answer:
969 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 1 , &cmd1
[2], &cmd1
[3]);
970 if(cmd1
[2] != Demod
.output
[1] || cmd1
[3] != Demod
.output
[2]) {
971 DbpString("CRC Error reading select response.");
974 // Check response from the tag: should be the same UID as the command we just sent:
975 if (cmd1
[1] != Demod
.output
[0]) {
976 Dbprintf("Bad response to SELECT from Tag, aborting: %x %x", cmd1
[1], Demod
.output
[0]);
979 // Tag is now selected,
980 // First get the tag's UID:
982 ComputeCrc14443(CRC_14443_B
, cmd1
, 1 , &cmd1
[1], &cmd1
[2]);
983 CodeAndTransmit14443bAsReader(cmd1
, 3); // Only first three bytes for this one
986 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
988 if (Demod
.len
!= 10) {
989 Dbprintf("Expected 10 bytes from tag, got %d", Demod
.len
);
992 // The check the CRC of the answer (use cmd1 as temporary variable):
993 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 8, &cmd1
[2], &cmd1
[3]);
994 if(cmd1
[2] != Demod
.output
[8] || cmd1
[3] != Demod
.output
[9]) {
995 Dbprintf("CRC Error reading block! - Below: expected, got %x %x",
996 (cmd1
[2]<<8)+cmd1
[3], (Demod
.output
[8]<<8)+Demod
.output
[9]);
997 // Do not return;, let's go on... (we should retry, maybe ?)
999 Dbprintf("Tag UID (64 bits): %08x %08x",
1000 (Demod
.output
[7]<<24) + (Demod
.output
[6]<<16) + (Demod
.output
[5]<<8) + Demod
.output
[4],
1001 (Demod
.output
[3]<<24) + (Demod
.output
[2]<<16) + (Demod
.output
[1]<<8) + Demod
.output
[0]);
1003 // Now loop to read all 16 blocks, address from 0 to last block
1004 Dbprintf("Tag memory dump, block 0 to %d",dwLast
);
1010 DbpString("System area block (0xff):");
1014 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
1015 CodeAndTransmit14443bAsReader(cmd1
, sizeof(cmd1
));
1018 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
1020 if (Demod
.len
!= 6) { // Check if we got an answer from the tag
1021 DbpString("Expected 6 bytes from tag, got less...");
1024 // The check the CRC of the answer (use cmd1 as temporary variable):
1025 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 4, &cmd1
[2], &cmd1
[3]);
1026 if(cmd1
[2] != Demod
.output
[4] || cmd1
[3] != Demod
.output
[5]) {
1027 Dbprintf("CRC Error reading block! - Below: expected, got %x %x",
1028 (cmd1
[2]<<8)+cmd1
[3], (Demod
.output
[4]<<8)+Demod
.output
[5]);
1029 // Do not return;, let's go on... (we should retry, maybe ?)
1031 // Now print out the memory location:
1032 Dbprintf("Address=%x, Contents=%x, CRC=%x", i
,
1033 (Demod
.output
[3]<<24) + (Demod
.output
[2]<<16) + (Demod
.output
[1]<<8) + Demod
.output
[0],
1034 (Demod
.output
[4]<<8)+Demod
.output
[5]);
1043 //=============================================================================
1044 // Finally, the `sniffer' combines elements from both the reader and
1045 // simulated tag, to show both sides of the conversation.
1046 //=============================================================================
1048 //-----------------------------------------------------------------------------
1049 // Record the sequence of commands sent by the reader to the tag, with
1050 // triggering so that we start recording at the point that the tag is moved
1052 //-----------------------------------------------------------------------------
1054 * Memory usage for this function, (within BigBuf)
1055 * Last Received command (reader->tag) - MAX_FRAME_SIZE
1056 * Last Received command (tag->reader) - MAX_FRAME_SIZE
1057 * DMA Buffer, 1024 bytes (samples) - DMA_BUFFER_SIZE
1058 * Demodulated samples received - all the rest
1060 void RAMFUNC
SnoopIso14443(void)
1062 // We won't start recording the frames that we acquire until we trigger;
1063 // a good trigger condition to get started is probably when we see a
1064 // response from the tag.
1065 int triggered
= TRUE
; // TODO: set and evaluate trigger condition
1067 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1073 // The DMA buffer, used to stream samples from the FPGA
1074 int8_t *dmaBuf
= (int8_t*) BigBuf_malloc(DMA_BUFFER_SIZE
);
1078 int maxBehindBy
= 0;
1080 // Count of samples received so far, so that we can include timing
1081 // information in the trace buffer.
1084 DemodInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1085 UartInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1087 // Print some debug information about the buffer sizes
1088 Dbprintf("Snooping buffers initialized:");
1089 Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen());
1090 Dbprintf(" Reader -> tag: %i bytes", MAX_FRAME_SIZE
);
1091 Dbprintf(" tag -> Reader: %i bytes", MAX_FRAME_SIZE
);
1092 Dbprintf(" DMA: %i bytes", DMA_BUFFER_SIZE
);
1094 // Signal field is off with the appropriate LED
1097 // And put the FPGA in the appropriate mode
1099 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
1100 FPGA_HF_READER_RX_XCORR_SNOOP
);
1101 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1103 // Setup for the DMA.
1106 lastRxCounter
= DMA_BUFFER_SIZE
;
1107 FpgaSetupSscDma((uint8_t*) dmaBuf
, DMA_BUFFER_SIZE
);
1108 uint8_t parity
[MAX_PARITY_SIZE
];
1111 bool TagIsActive
= FALSE
;
1112 bool ReaderIsActive
= FALSE
;
1114 // And now we loop, receiving samples.
1116 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
1117 (DMA_BUFFER_SIZE
-1);
1118 if(behindBy
> maxBehindBy
) {
1119 maxBehindBy
= behindBy
;
1120 if(behindBy
> (9*DMA_BUFFER_SIZE
/10)) { // TODO: understand whether we can increase/decrease as we want or not?
1121 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
1125 if(behindBy
< 2) continue;
1131 if(upTo
>= dmaBuf
+ DMA_BUFFER_SIZE
) {
1133 lastRxCounter
+= DMA_BUFFER_SIZE
;
1134 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
;
1135 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
1140 if (!TagIsActive
) { // no need to try decoding reader data if the tag is sending
1141 if(Handle14443UartBit(ci
& 0x01)) {
1142 if(triggered
&& tracing
) {
1143 GetParity(Uart
.output
, Uart
.byteCnt
, parity
);
1144 LogTrace(Uart
.output
,Uart
.byteCnt
,samples
, samples
,parity
,TRUE
);
1146 if(Uart
.byteCnt
==0) Dbprintf("[1] Error, Uart.byteCnt==0, Uart.bitCnt=%d", Uart
.bitCnt
);
1148 /* And ready to receive another command. */
1150 /* And also reset the demod code, which might have been */
1151 /* false-triggered by the commands from the reader. */
1154 if(Handle14443UartBit(cq
& 0x01)) {
1155 if(triggered
&& tracing
) {
1156 GetParity(Uart
.output
, Uart
.byteCnt
, parity
);
1157 LogTrace(Uart
.output
,Uart
.byteCnt
,samples
, samples
, parity
, TRUE
);
1159 if(Uart
.byteCnt
==0) Dbprintf("[2] Error, Uart.byteCnt==0, Uart.bitCnt=%d", Uart
.bitCnt
);
1161 /* And ready to receive another command. */
1163 /* And also reset the demod code, which might have been */
1164 /* false-triggered by the commands from the reader. */
1167 ReaderIsActive
= (Uart
.state
!= STATE_UNSYNCD
);
1170 if(!ReaderIsActive
) { // no need to try decoding tag data if the reader is sending - and we cannot afford the time
1171 if(Handle14443SamplesDemod(ci
& 0xFE, cq
& 0xFE)) {
1173 //Use samples as a time measurement
1176 uint8_t parity
[MAX_PARITY_SIZE
];
1177 GetParity(Demod
.output
, Demod
.len
, parity
);
1178 LogTrace(Demod
.output
, Demod
.len
,samples
, samples
, parity
, FALSE
);
1184 // And ready to receive another response.
1187 TagIsActive
= (Demod
.state
!= DEMOD_UNSYNCD
);
1193 DbpString("Reached trace limit");
1197 if(BUTTON_PRESS()) {
1198 DbpString("cancelled");
1202 FpgaDisableSscDma();
1206 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
1207 DbpString("Snoop statistics:");
1208 Dbprintf(" Max behind by: %i", maxBehindBy
);
1209 Dbprintf(" Uart State: %x", Uart
.state
);
1210 Dbprintf(" Uart ByteCnt: %i", Uart
.byteCnt
);
1211 Dbprintf(" Uart ByteCntMax: %i", Uart
.byteCntMax
);
1212 Dbprintf(" Trace length: %i", BigBuf_get_traceLen());
1217 * Send raw command to tag ISO14443B
1219 * datalen len of buffer data
1220 * recv bool when true wait for data from tag and send to client
1221 * powerfield bool leave the field on when true
1222 * data buffer with byte to send
1228 void SendRawCommand14443B(uint32_t datalen
, uint32_t recv
, uint8_t powerfield_trace
, uint8_t data
[])
1230 uint8_t powerfield
= powerfield_trace
& 1;
1231 uint8_t trace
= powerfield_trace
& 2;
1236 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1239 // Make sure that we start from off, since the tags are stateful;
1240 // confusing things will happen if we don't reset them between reads.
1241 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1246 if(!GETBIT(GPIO_LED_D
))
1248 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1251 // Now give it time to spin up.
1252 // Signal field is on with the appropriate LED
1255 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
1259 CodeAndTransmit14443bAsReader(data
, datalen
);
1263 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
1264 uint16_t iLen
= MIN(Demod
.len
,USB_CMD_DATA_SIZE
);
1265 cmd_send(CMD_ACK
,iLen
,0,0,Demod
.output
,iLen
);
1269 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);