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
)
163 // we went low, so this could be the beginning
165 Uart
.state
= STATE_GOT_FALLING_EDGE_OF_SOF
;
171 case STATE_GOT_FALLING_EDGE_OF_SOF
:
173 if(Uart
.posCnt
== 2) {
175 if(Uart
.bitCnt
>= 10) {
176 // we've seen enough consecutive
177 // zeros that it's a valid SOF
180 Uart
.state
= STATE_AWAITING_START_BIT
;
181 LED_A_ON(); // Indicate we got a valid SOF
183 // didn't stay down long enough
184 // before going high, error
185 Uart
.state
= STATE_ERROR_WAIT
;
188 // do nothing, keep waiting
192 if(Uart
.posCnt
>= 4) Uart
.posCnt
= 0;
193 if(Uart
.bitCnt
> 14) {
194 // Give up if we see too many zeros without
196 Uart
.state
= STATE_ERROR_WAIT
;
200 case STATE_AWAITING_START_BIT
:
203 if(Uart
.posCnt
> 25) {
204 // stayed high for too long between
206 Uart
.state
= STATE_ERROR_WAIT
;
209 // falling edge, this starts the data byte
213 Uart
.state
= STATE_RECEIVING_DATA
;
214 LED_A_ON(); // Indicate we're receiving
218 case STATE_RECEIVING_DATA
:
220 if(Uart
.posCnt
== 2) {
221 // time to sample a bit
224 Uart
.shiftReg
|= 0x200;
228 if(Uart
.posCnt
>= 4) {
231 if(Uart
.bitCnt
== 10) {
232 if((Uart
.shiftReg
& 0x200) && !(Uart
.shiftReg
& 0x001))
234 // this is a data byte, with correct
235 // start and stop bits
236 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
>> 1) & 0xff;
239 if(Uart
.byteCnt
>= Uart
.byteCntMax
) {
240 // Buffer overflowed, give up
242 Uart
.state
= STATE_ERROR_WAIT
;
244 // so get the next byte now
246 Uart
.state
= STATE_AWAITING_START_BIT
;
248 } else if(Uart
.shiftReg
== 0x000) {
249 // this is an EOF byte
250 LED_A_OFF(); // Finished receiving
255 Uart
.state
= STATE_ERROR_WAIT
;
260 case STATE_ERROR_WAIT
:
261 // We're all screwed up, so wait a little while
262 // for whatever went wrong to finish, and then
265 if(Uart
.posCnt
> 10) {
266 Uart
.state
= STATE_UNSYNCD
;
271 Uart
.state
= STATE_UNSYNCD
;
275 // This row make the error blew circular buffer in hf 14b snoop
276 //if (Uart.state == STATE_ERROR_WAIT) LED_A_OFF(); // Error
281 //-----------------------------------------------------------------------------
282 // Receive a command (from the reader to us, where we are the simulated tag),
283 // and store it in the given buffer, up to the given maximum length. Keeps
284 // spinning, waiting for a well-framed command, until either we get one
285 // (returns TRUE) or someone presses the pushbutton on the board (FALSE).
287 // Assume that we're called with the SSC (to the FPGA) and ADC path set
289 //-----------------------------------------------------------------------------
290 static int GetIso14443CommandFromReader(uint8_t *received
, int *len
, int maxLen
)
295 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
296 // only, since we are receiving, not transmitting).
297 // Signal field is off with the appropriate LED
299 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
302 // Now run a `software UART' on the stream of incoming samples.
303 Uart
.output
= received
;
304 Uart
.byteCntMax
= maxLen
;
305 Uart
.state
= STATE_UNSYNCD
;
310 if(BUTTON_PRESS()) return FALSE
;
312 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
313 AT91C_BASE_SSC
->SSC_THR
= 0x00;
315 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
316 uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
319 for(i
= 0; i
< 8; i
++, mask
>>= 1) {
321 if(Handle14443UartBit(bit
)) {
330 //-----------------------------------------------------------------------------
331 // Main loop of simulated tag: receive commands from reader, decide what
332 // response to send, and send it.
333 //-----------------------------------------------------------------------------
334 void SimulateIso14443Tag(void)
336 static const uint8_t cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
337 static const uint8_t response1
[] = {
338 0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, 0x22,
339 0x00, 0x21, 0x85, 0x5e, 0xd7
345 uint8_t *resp1
= BigBuf_get_addr() + 800;
348 uint8_t *receivedCmd
= BigBuf_get_addr();
355 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
356 memset(receivedCmd
, 0x44, 400);
358 CodeIso14443bAsTag(response1
, sizeof(response1
));
359 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
361 // We need to listen to the high-frequency, peak-detected path.
362 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
370 if(!GetIso14443CommandFromReader(receivedCmd
, &len
, 100)) {
371 Dbprintf("button pressed, received %d commands", cmdsRecvd
);
375 // Good, look at the command now.
377 if(len
== sizeof(cmd1
) && memcmp(receivedCmd
, cmd1
, len
)==0) {
378 resp
= resp1
; respLen
= resp1Len
;
380 Dbprintf("new cmd from reader: len=%d, cmdsRecvd=%d", len
, cmdsRecvd
);
381 // And print whether the CRC fails, just for good measure
382 ComputeCrc14443(CRC_14443_B
, receivedCmd
, len
-2, &b1
, &b2
);
383 if(b1
!= receivedCmd
[len
-2] || b2
!= receivedCmd
[len
-1]) {
384 // Not so good, try again.
385 DbpString("+++CRC fail");
387 DbpString("CRC passes");
392 memset(receivedCmd
, 0x44, 32);
396 if(cmdsRecvd
> 0x30) {
397 DbpString("many commands later...");
401 if(respLen
<= 0) continue;
404 // Signal field is off with the appropriate LED
406 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_BPSK
);
407 AT91C_BASE_SSC
->SSC_THR
= 0xff;
410 // Transmit the response.
413 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
416 AT91C_BASE_SSC
->SSC_THR
= b
;
423 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
424 volatile uint8_t b
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
431 //=============================================================================
432 // An ISO 14443 Type B reader. We take layer two commands, code them
433 // appropriately, and then send them to the tag. We then listen for the
434 // tag's response, which we leave in the buffer to be demodulated on the
436 //=============================================================================
441 DEMOD_PHASE_REF_TRAINING
,
442 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
443 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
444 DEMOD_AWAITING_START_BIT
,
445 DEMOD_RECEIVING_DATA
,
461 * Handles reception of a bit from the tag
464 * LED C -> ON once we have received the SOF and are expecting the rest.
465 * LED C -> OFF once we have received EOF or are unsynced
467 * Returns: true if we received a EOF
468 * false if we are still waiting for some more
471 static RAMFUNC
int Handle14443SamplesDemod(int ci
, int cq
)
475 // The soft decision on the bit uses an estimate of just the
476 // quadrant of the reference angle, not the exact angle.
477 #define MAKE_SOFT_DECISION() { \
478 if(Demod.sumI > 0) { \
483 if(Demod.sumQ > 0) { \
490 switch(Demod
.state
) {
501 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
507 case DEMOD_PHASE_REF_TRAINING
:
508 if(Demod
.posCount
< 8) {
511 } else if(Demod
.posCount
> 100) {
512 // error, waited too long
513 Demod
.state
= DEMOD_UNSYNCD
;
515 MAKE_SOFT_DECISION();
517 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
524 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
525 MAKE_SOFT_DECISION();
527 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
530 if(Demod
.posCount
> 100) {
531 Demod
.state
= DEMOD_UNSYNCD
;
537 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
538 MAKE_SOFT_DECISION();
540 if(Demod
.posCount
< 12) {
541 Demod
.state
= DEMOD_UNSYNCD
;
543 LED_C_ON(); // Got SOF
544 Demod
.state
= DEMOD_AWAITING_START_BIT
;
551 if(Demod
.posCount
> 100) {
552 Demod
.state
= DEMOD_UNSYNCD
;
558 case DEMOD_AWAITING_START_BIT
:
559 MAKE_SOFT_DECISION();
561 if(Demod
.posCount
> 10) {
562 Demod
.state
= DEMOD_UNSYNCD
;
569 Demod
.state
= DEMOD_RECEIVING_DATA
;
573 case DEMOD_RECEIVING_DATA
:
574 MAKE_SOFT_DECISION();
575 if(Demod
.posCount
== 0) {
581 if(Demod
.thisBit
> 0) {
582 Demod
.metric
+= Demod
.thisBit
;
584 Demod
.metric
-= Demod
.thisBit
;
588 Demod
.shiftReg
>>= 1;
589 if(Demod
.thisBit
> 0) {
590 Demod
.shiftReg
|= 0x200;
594 if(Demod
.bitCount
== 10) {
595 uint16_t s
= Demod
.shiftReg
;
596 if((s
& 0x200) && !(s
& 0x001)) {
597 uint8_t b
= (s
>> 1);
598 Demod
.output
[Demod
.len
] = b
;
600 Demod
.state
= DEMOD_AWAITING_START_BIT
;
601 } else if(s
== 0x000) {
604 Demod
.state
= DEMOD_UNSYNCD
;
607 Demod
.state
= DEMOD_UNSYNCD
;
615 Demod
.state
= DEMOD_UNSYNCD
;
619 if (Demod
.state
== DEMOD_UNSYNCD
) LED_C_OFF(); // Not synchronized...
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
);
629 static void DemodInit(uint8_t *data
)
635 static void UartReset()
637 Uart
.byteCntMax
= MAX_FRAME_SIZE
;
638 Uart
.state
= STATE_UNSYNCD
;
642 static void UartInit(uint8_t *data
)
649 * Demodulate the samples we received from the tag, also log to tracebuffer
650 * weTx: set to 'TRUE' if we behave like a reader
651 * set to 'FALSE' if we behave like a snooper
652 * quiet: set to 'TRUE' to disable debug output
654 static void GetSamplesFor14443Demod(int weTx
, int n
, int quiet
)
657 int gotFrame
= FALSE
;
658 int lastRxCounter
, ci
, cq
, samples
= 0;
660 // Allocate memory from BigBuf for some buffers
661 // free all previous allocations first
664 // The response (tag -> reader) that we're receiving.
665 uint8_t *receivedResponse
= BigBuf_malloc(MAX_FRAME_SIZE
);
667 // The DMA buffer, used to stream samples from the FPGA
668 uint8_t *dmaBuf
= BigBuf_malloc(DMA_BUFFER_SIZE
);
670 // Set up the demodulator for tag -> reader responses.
671 DemodInit(receivedResponse
);
673 // Setup and start DMA.
674 FpgaSetupSscDma(dmaBuf
, DMA_BUFFER_SIZE
);
676 uint8_t *upTo
= dmaBuf
;
677 lastRxCounter
= DMA_BUFFER_SIZE
;
679 // Signal field is ON with the appropriate LED:
680 if (weTx
) LED_D_ON(); else LED_D_OFF();
681 // And put the FPGA in the appropriate mode
683 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
684 (weTx
? 0 : FPGA_HF_READER_RX_XCORR_SNOOP
));
687 int behindBy
= lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
688 if(behindBy
> max
) max
= behindBy
;
690 while(((lastRxCounter
-AT91C_BASE_PDC_SSC
->PDC_RCR
) & (DMA_BUFFER_SIZE
-1))
696 if(upTo
>= dmaBuf
+ DMA_BUFFER_SIZE
) {
698 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
699 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
702 if(lastRxCounter
<= 0) {
703 lastRxCounter
+= DMA_BUFFER_SIZE
;
708 if(Handle14443SamplesDemod(ci
, cq
)) {
717 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
718 if (!quiet
) Dbprintf("%x %x %x", max
, gotFrame
, Demod
.len
);
720 if (tracing
&& Demod
.len
> 0) {
721 uint8_t parity
[MAX_PARITY_SIZE
];
722 GetParity(Demod
.output
, Demod
.len
, parity
);
723 LogTrace(Demod
.output
, Demod
.len
, 0, 0, parity
, FALSE
);
727 //-----------------------------------------------------------------------------
728 // Read the tag's response. We just receive a stream of slightly-processed
729 // samples from the FPGA, which we will later do some signal processing on,
731 //-----------------------------------------------------------------------------
732 /*static void GetSamplesFor14443(int weTx, int n)
734 uint8_t *dest = (uint8_t *)BigBuf;
738 FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |
739 (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));
743 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
744 AT91C_BASE_SSC->SSC_THR = 0x43;
746 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
748 b = (int8_t)AT91C_BASE_SSC->SSC_RHR;
750 dest[c++] = (uint8_t)b;
759 //-----------------------------------------------------------------------------
760 // Transmit the command (to the tag) that was placed in ToSend[].
761 //-----------------------------------------------------------------------------
762 static void TransmitFor14443(void)
768 while(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
769 AT91C_BASE_SSC
->SSC_THR
= 0xff;
772 // Signal field is ON with the appropriate Red LED
774 // Signal we are transmitting with the Green LED
777 FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
779 for(c
= 0; c
< 10;) {
780 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
781 AT91C_BASE_SSC
->SSC_THR
= 0xff;
784 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
785 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
793 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
794 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
800 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
801 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
806 LED_B_OFF(); // Finished sending
809 //-----------------------------------------------------------------------------
810 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
811 // so that it is ready to transmit to the tag using TransmitFor14443().
812 //-----------------------------------------------------------------------------
813 static void CodeIso14443bAsReader(const uint8_t *cmd
, int len
)
820 // Establish initial reference level
821 for(i
= 0; i
< 40; i
++) {
825 for(i
= 0; i
< 10; i
++) {
829 for(i
= 0; i
< len
; i
++) {
837 for(j
= 0; j
< 8; j
++) {
848 for(i
= 0; i
< 10; i
++) {
851 for(i
= 0; i
< 8; i
++) {
855 // And then a little more, to make sure that the last character makes
856 // it out before we switch to rx mode.
857 for(i
= 0; i
< 24; i
++) {
861 // Convert from last character reference to length
865 //-----------------------------------------------------------------------------
866 // Read an ISO 14443 tag. We send it some set of commands, and record the
868 // The command name is misleading, it actually decodes the reponse in HEX
869 // into the output buffer (read the result using hexsamples, not hisamples)
871 // obsolete function only for test
872 //-----------------------------------------------------------------------------
873 void AcquireRawAdcSamplesIso14443(uint32_t parameter
)
875 uint8_t cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
877 SendRawCommand14443B(sizeof(cmd1
),1,1,cmd1
);
881 Convenience function to encode, transmit and trace iso 14443b comms
883 static void CodeAndTransmit14443bAsReader(const uint8_t *cmd
, int len
)
885 CodeIso14443bAsReader(cmd
, len
);
888 uint8_t parity
[MAX_PARITY_SIZE
];
889 GetParity(cmd
, len
, parity
);
890 LogTrace(cmd
,len
, 0, 0, parity
, TRUE
);
894 //-----------------------------------------------------------------------------
895 // Read a SRI512 ISO 14443 tag.
897 // SRI512 tags are just simple memory tags, here we're looking at making a dump
898 // of the contents of the memory. No anticollision algorithm is done, we assume
899 // we have a single tag in the field.
901 // I tried to be systematic and check every answer of the tag, every CRC, etc...
902 //-----------------------------------------------------------------------------
903 void ReadSTMemoryIso14443(uint32_t dwLast
)
910 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
911 // Make sure that we start from off, since the tags are stateful;
912 // confusing things will happen if we don't reset them between reads.
914 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
917 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
920 // Now give it time to spin up.
921 // Signal field is on with the appropriate LED
924 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
927 // First command: wake up the tag using the INITIATE command
928 uint8_t cmd1
[] = { 0x06, 0x00, 0x97, 0x5b};
930 CodeAndTransmit14443bAsReader(cmd1
, sizeof(cmd1
));
932 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
935 if (Demod
.len
== 0) {
936 DbpString("No response from tag");
939 Dbprintf("Randomly generated UID from tag (+ 2 byte CRC): %x %x %x",
940 Demod
.output
[0], Demod
.output
[1],Demod
.output
[2]);
942 // There is a response, SELECT the uid
943 DbpString("Now SELECT tag:");
944 cmd1
[0] = 0x0E; // 0x0E is SELECT
945 cmd1
[1] = Demod
.output
[0];
946 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
947 CodeAndTransmit14443bAsReader(cmd1
, sizeof(cmd1
));
950 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
952 if (Demod
.len
!= 3) {
953 Dbprintf("Expected 3 bytes from tag, got %d", Demod
.len
);
956 // Check the CRC of the answer:
957 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 1 , &cmd1
[2], &cmd1
[3]);
958 if(cmd1
[2] != Demod
.output
[1] || cmd1
[3] != Demod
.output
[2]) {
959 DbpString("CRC Error reading select response.");
962 // Check response from the tag: should be the same UID as the command we just sent:
963 if (cmd1
[1] != Demod
.output
[0]) {
964 Dbprintf("Bad response to SELECT from Tag, aborting: %x %x", cmd1
[1], Demod
.output
[0]);
967 // Tag is now selected,
968 // First get the tag's UID:
970 ComputeCrc14443(CRC_14443_B
, cmd1
, 1 , &cmd1
[1], &cmd1
[2]);
971 CodeAndTransmit14443bAsReader(cmd1
, 3); // Only first three bytes for this one
974 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
976 if (Demod
.len
!= 10) {
977 Dbprintf("Expected 10 bytes from tag, got %d", Demod
.len
);
980 // The check the CRC of the answer (use cmd1 as temporary variable):
981 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 8, &cmd1
[2], &cmd1
[3]);
982 if(cmd1
[2] != Demod
.output
[8] || cmd1
[3] != Demod
.output
[9]) {
983 Dbprintf("CRC Error reading block! - Below: expected, got %x %x",
984 (cmd1
[2]<<8)+cmd1
[3], (Demod
.output
[8]<<8)+Demod
.output
[9]);
985 // Do not return;, let's go on... (we should retry, maybe ?)
987 Dbprintf("Tag UID (64 bits): %08x %08x",
988 (Demod
.output
[7]<<24) + (Demod
.output
[6]<<16) + (Demod
.output
[5]<<8) + Demod
.output
[4],
989 (Demod
.output
[3]<<24) + (Demod
.output
[2]<<16) + (Demod
.output
[1]<<8) + Demod
.output
[0]);
991 // Now loop to read all 16 blocks, address from 0 to last block
992 Dbprintf("Tag memory dump, block 0 to %d",dwLast
);
998 DbpString("System area block (0xff):");
1002 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
1003 CodeAndTransmit14443bAsReader(cmd1
, sizeof(cmd1
));
1006 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
1008 if (Demod
.len
!= 6) { // Check if we got an answer from the tag
1009 DbpString("Expected 6 bytes from tag, got less...");
1012 // The check the CRC of the answer (use cmd1 as temporary variable):
1013 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 4, &cmd1
[2], &cmd1
[3]);
1014 if(cmd1
[2] != Demod
.output
[4] || cmd1
[3] != Demod
.output
[5]) {
1015 Dbprintf("CRC Error reading block! - Below: expected, got %x %x",
1016 (cmd1
[2]<<8)+cmd1
[3], (Demod
.output
[4]<<8)+Demod
.output
[5]);
1017 // Do not return;, let's go on... (we should retry, maybe ?)
1019 // Now print out the memory location:
1020 Dbprintf("Address=%x, Contents=%x, CRC=%x", i
,
1021 (Demod
.output
[3]<<24) + (Demod
.output
[2]<<16) + (Demod
.output
[1]<<8) + Demod
.output
[0],
1022 (Demod
.output
[4]<<8)+Demod
.output
[5]);
1031 //=============================================================================
1032 // Finally, the `sniffer' combines elements from both the reader and
1033 // simulated tag, to show both sides of the conversation.
1034 //=============================================================================
1036 //-----------------------------------------------------------------------------
1037 // Record the sequence of commands sent by the reader to the tag, with
1038 // triggering so that we start recording at the point that the tag is moved
1040 //-----------------------------------------------------------------------------
1042 * Memory usage for this function, (within BigBuf)
1043 * 0-4095 : Demodulated samples receive (4096 bytes) - DEMOD_TRACE_SIZE
1044 * 4096-6143 : Last Received command, 2048 bytes (reader->tag) - READER_TAG_BUFFER_SIZE
1045 * 6144-8191 : Last Received command, 2048 bytes(tag->reader) - TAG_READER_BUFFER_SIZE
1046 * 8192-9215 : DMA Buffer, 1024 bytes (samples) - DEMOD_DMA_BUFFER_SIZE
1048 void RAMFUNC
SnoopIso14443(void)
1050 // We won't start recording the frames that we acquire until we trigger;
1051 // a good trigger condition to get started is probably when we see a
1052 // response from the tag.
1053 int triggered
= TRUE
;
1055 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1061 // The DMA buffer, used to stream samples from the FPGA
1062 uint8_t *dmaBuf
= BigBuf_malloc(DMA_BUFFER_SIZE
);
1066 int maxBehindBy
= 0;
1068 // Count of samples received so far, so that we can include timing
1069 // information in the trace buffer.
1072 DemodInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1073 UartInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1075 // Print some debug information about the buffer sizes
1076 Dbprintf("Snooping buffers initialized:");
1077 Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen());
1078 Dbprintf(" Reader -> tag: %i bytes", MAX_FRAME_SIZE
);
1079 Dbprintf(" tag -> Reader: %i bytes", MAX_FRAME_SIZE
);
1080 Dbprintf(" DMA: %i bytes", DMA_BUFFER_SIZE
);
1082 // Signal field is off with the appropriate LED
1085 // And put the FPGA in the appropriate mode
1087 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
1088 FPGA_HF_READER_RX_XCORR_SNOOP
);
1089 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1091 // Setup for the DMA.
1094 lastRxCounter
= DMA_BUFFER_SIZE
;
1095 FpgaSetupSscDma((uint8_t *)dmaBuf
, DMA_BUFFER_SIZE
);
1096 uint8_t parity
[MAX_PARITY_SIZE
];
1099 // And now we loop, receiving samples.
1101 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
1102 (DMA_BUFFER_SIZE
-1);
1103 if(behindBy
> maxBehindBy
) {
1104 maxBehindBy
= behindBy
;
1105 if(behindBy
> (9*DMA_BUFFER_SIZE
/10)) { // TODO: understand whether we can increase/decrease as we want or not?
1106 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy
);
1110 if(behindBy
< 2) continue;
1116 if(upTo
>= dmaBuf
+ DMA_BUFFER_SIZE
) {
1118 lastRxCounter
+= DMA_BUFFER_SIZE
;
1119 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
;
1120 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
1125 if(Handle14443UartBit(ci
& 1)) {
1126 if(triggered
&& tracing
) {
1127 GetParity(Uart
.output
, Uart
.byteCnt
, parity
);
1128 LogTrace(Uart
.output
,Uart
.byteCnt
,samples
, samples
,parity
,TRUE
);
1130 if(Uart
.byteCnt
==0) Dbprintf("[1] Error, Uart.byteCnt==0, Uart.bitCnt=%d", Uart
.bitCnt
);
1132 /* And ready to receive another command. */
1134 /* And also reset the demod code, which might have been */
1135 /* false-triggered by the commands from the reader. */
1138 if(Handle14443UartBit(cq
& 1)) {
1139 if(triggered
&& tracing
) {
1140 GetParity(Uart
.output
, Uart
.byteCnt
, parity
);
1141 LogTrace(Uart
.output
,Uart
.byteCnt
,samples
, samples
,parity
,TRUE
);
1143 if(Uart
.byteCnt
==0) Dbprintf("[2] Error, Uart.byteCnt==0, Uart.bitCnt=%d", Uart
.bitCnt
);
1145 /* And ready to receive another command. */
1147 /* And also reset the demod code, which might have been */
1148 /* false-triggered by the commands from the reader. */
1152 if(Handle14443SamplesDemod(ci
, cq
)) {
1154 //Use samples as a time measurement
1157 uint8_t parity
[MAX_PARITY_SIZE
];
1158 GetParity(Demod
.output
, Demod
.len
, parity
);
1159 LogTrace(Demod
.output
,Demod
.len
,samples
, samples
,parity
,FALSE
);
1165 // And ready to receive another response.
1171 DbpString("Reached trace limit");
1175 if(BUTTON_PRESS()) {
1176 DbpString("cancelled");
1180 FpgaDisableSscDma();
1184 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
1185 DbpString("Snoop statistics:");
1186 Dbprintf(" Max behind by: %i", maxBehindBy
);
1187 Dbprintf(" Uart State: %x", Uart
.state
);
1188 Dbprintf(" Uart ByteCnt: %i", Uart
.byteCnt
);
1189 Dbprintf(" Uart ByteCntMax: %i", Uart
.byteCntMax
);
1190 Dbprintf(" Trace length: %i", BigBuf_get_traceLen());
1194 * Send raw command to tag ISO14443B
1196 * datalen len of buffer data
1197 * recv bool when true wait for data from tag and send to client
1198 * powerfield bool leave the field on when true
1199 * data buffer with byte to send
1206 void SendRawCommand14443B(uint32_t datalen
, uint32_t recv
,uint8_t powerfield
, uint8_t data
[])
1208 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1211 // Make sure that we start from off, since the tags are stateful;
1212 // confusing things will happen if we don't reset them between reads.
1213 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1218 if(!GETBIT(GPIO_LED_D
))
1220 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1223 // Now give it time to spin up.
1224 // Signal field is on with the appropriate LED
1227 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
1231 CodeAndTransmit14443bAsReader(data
, datalen
);
1235 GetSamplesFor14443Demod(TRUE
, RECEIVE_SAMPLES_TIMEOUT
, TRUE
);
1236 uint16_t iLen
= MIN(Demod
.len
,USB_CMD_DATA_SIZE
);
1237 cmd_send(CMD_ACK
,iLen
,0,0,Demod
.output
,iLen
);
1241 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);