//-----------------------------------------------------------------------------
#include "iso14443b.h"
-#define RECEIVE_SAMPLES_TIMEOUT 50000
-#define ISO14443B_DMA_BUFFER_SIZE 256
+#ifndef FWT_TIMEOUT_14B
+# define FWT_TIMEOUT_14B 60000
+#endif
+#ifndef ISO14443B_DMA_BUFFER_SIZE
+# define ISO14443B_DMA_BUFFER_SIZE 256
+#endif
+#ifndef RECEIVE_MASK
+# define RECEIVE_MASK (ISO14443B_DMA_BUFFER_SIZE-1)
+#endif
// Guard Time (per 14443-2)
-#define TR0 0
+#ifndef TR0
+# define TR0 0
+#endif
+
// Synchronization time (per 14443-2)
-#define TR1 0
+#ifndef TR1
+# define TR1 0
+#endif
// Frame Delay Time PICC to PCD (per 14443-3 Amendment 1)
-#define TR2 0
+#ifndef TR2
+# define TR2 0
+#endif
// 4sample
#define SEND4STUFFBIT(x) ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x);
// the block number for the ISO14443-4 PCB (used with APDUs)
static uint8_t pcb_blocknum = 0;
-static uint32_t iso14b_timeout = RECEIVE_SAMPLES_TIMEOUT;
+static uint32_t iso14b_timeout = FWT_TIMEOUT_14B;
// param timeout is in ftw_
void iso14b_set_timeout(uint32_t timeout) {
// 9.4395us = 1etu.
// clock is about 1.5 us
iso14b_timeout = timeout;
- if(MF_DBGLEVEL >= 2) Dbprintf("ISO14443B Timeout set to %ld fwt", iso14b_timeout);
+ if(MF_DBGLEVEL >= 3) Dbprintf("ISO14443B Timeout set to %ld fwt", iso14b_timeout);
}
static void switch_off(void){
static void TransmitFor14443b_AsTag( uint8_t *response, uint16_t len) {
- // Signal field is off with the appropriate LED
- LED_D_OFF();
- //uint16_t fpgasendQueueDelay = 0;
-
- // Modulate BPSK
- FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_BPSK);
-
- ClearFpgaShiftingRegisters();
-
- FpgaSetupSsc();
- volatile uint32_t b;
+ volatile uint32_t b;
+
+ // Signal field is off with the appropriate LED
+ LED_D_OFF();
+ //uint16_t fpgasendQueueDelay = 0;
+
+ // Modulate BPSK
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_BPSK);
+ SpinDelay(40);
+
+ ClearFpgaShiftingRegisters();
+
+ FpgaSetupSsc();
- // Transmit the response.
- for(uint16_t i = 0; i < len;) {
- if(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
- AT91C_BASE_SSC->SSC_THR = response[++i];
- }
- if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
- b = AT91C_BASE_SSC->SSC_RHR;
- (void)b;
- }
+ // Transmit the response.
+ for(uint16_t i = 0; i < len;) {
+ if(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
+ AT91C_BASE_SSC->SSC_THR = response[++i];
}
-
- //WaitForFpgaDelayQueueIsEmpty(fpgasendQueueDelay);
- AT91C_BASE_SSC->SSC_THR = 0xFF;
+ if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+ b = AT91C_BASE_SSC->SSC_RHR;
+ (void)b;
+ }
+ }
+
+ //WaitForFpgaDelayQueueIsEmpty(fpgasendQueueDelay);
+ AT91C_BASE_SSC->SSC_THR = 0xFF;
}
//-----------------------------------------------------------------------------
// Main loop of simulated tag: receive commands from reader, decide what
send halt response ( waiting for wupb )
*/
- switch(cardSTATE){
+ switch (cardSTATE) {
case SIM_NOFIELD:
case SIM_HALTED:
- case SIM_IDLE:{
+ case SIM_IDLE: {
LogTrace(receivedCmd, len, 0, 0, NULL, TRUE);
break;
}
cardSTATE = SIM_HALTED;
break;
}
- case SIM_ACKNOWLEDGE:{
+ case SIM_ACKNOWLEDGE: {
TransmitFor14443b_AsTag( encodedOK, encodedOKLen );
LogTrace(respOK, sizeof(respOK), 0, 0, NULL, FALSE);
cardSTATE = SIM_IDLE;
break;
}
- case SIM_WORK:{
+ case SIM_WORK: {
if ( len == 7 && receivedCmd[0] == ISO14443B_HALT ) {
cardSTATE = SIM_HALTED;
} else if ( len == 11 && receivedCmd[0] == ISO14443B_ATTRIB ) {
}
++cmdsReceived;
+ // iceman, could add a switch to turn this on/off (if off, no logging?)
if(cmdsReceived > 1000) {
DbpString("14B Simulate, 1000 commands later...");
break;
* false if we are still waiting for some more
*
*/
+ // iceman, this threshold value, what makes 8 a good amplituted for this IQ values?
#ifndef SUBCARRIER_DETECT_THRESHOLD
-# define SUBCARRIER_DETECT_THRESHOLD 8
+# define SUBCARRIER_DETECT_THRESHOLD 6
#endif
static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) {
- int v=0;// , myI, myQ = 0;
+ int v = 0, myI = 0, myQ = 0;
// The soft decision on the bit uses an estimate of just the
// quadrant of the reference angle, not the exact angle.
#define MAKE_SOFT_DECISION() { \
// Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by abs(ci) + abs(cq)
// Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
-#define CHECK_FOR_SUBCARRIER() { \
+#define CHECK_FOR_SUBCARRIER_old() { \
if(ci < 0) { \
if(cq < 0) { /* ci < 0, cq < 0 */ \
if (cq < ci) { \
}
//note: couldn't we just use MAX(ABS(ci),ABS(cq)) + (MIN(ABS(ci),ABS(cq))/2) from common.h - marshmellow
-#define CHECK_FOR_SUBCARRIER_un() { \
+#define CHECK_FOR_SUBCARRIER() { \
myI = ABS(ci); \
myQ = ABS(cq); \
- v = MAX(myI,myQ) + (MIN(myI,myQ) >> 1); \
+ v = MAX(myI, myQ) + (MIN(myI, myQ) >> 1); \
}
switch(Demod.state) {
Demod.shiftReg >>= 1;
// logic '1'
- if(Demod.thisBit > 0) Demod.shiftReg |= 0x200;
+ if (Demod.thisBit > 0) Demod.shiftReg |= 0x200;
++Demod.bitCount;
- if(Demod.bitCount == 10) {
+ // 1 start 8 data 1 stop = 10
+ if (Demod.bitCount == 10) {
uint16_t s = Demod.shiftReg;
* quiet: set to 'TRUE' to disable debug output
*/
static void GetTagSamplesFor14443bDemod() {
- bool gotFrame = FALSE;
+ bool gotFrame = FALSE, finished = FALSE;
int lastRxCounter = ISO14443B_DMA_BUFFER_SIZE;
- int max = 0, ci = 0, cq = 0, samples = 0;
+ int ci = 0, cq = 0, samples = 0;
uint32_t time_0 = 0, time_stop = 0;
BigBuf_free();
if (MF_DBGLEVEL > 1) Dbprintf("FpgaSetupSscDma failed. Exiting");
return;
}
-
- time_0 = GetCountSspClk();
-
+
// And put the FPGA in the appropriate mode
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ);
-
- while( !BUTTON_PRESS() ) {
- WDT_HIT();
- int behindBy = lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR;
- if(behindBy > max) max = behindBy;
+ // get current clock
+ time_0 = GetCountSspClk();
+
+ // rx counter - dma counter? (how much?) & (mod) mask > 2. (since 2bytes at the time is read)
+ while ( !finished ) {
- // rx counter - dma counter? (how much?) & (mod) dma buff / 2. (since 2bytes at the time is read)
- while(((lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (ISO14443B_DMA_BUFFER_SIZE-1)) > 2) {
+ LED_A_INV();
+ WDT_HIT();
- ci = upTo[0];
- cq = upTo[1];
- upTo += 2;
- samples += 2;
-
- // restart DMA buffer to receive again.
- if(upTo >= dmaBuf + ISO14443B_DMA_BUFFER_SIZE) {
- upTo = dmaBuf;
- AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
- AT91C_BASE_PDC_SSC->PDC_RNCR = ISO14443B_DMA_BUFFER_SIZE;
- }
-
- lastRxCounter -= 2;
- if(lastRxCounter <= 0)
- lastRxCounter += ISO14443B_DMA_BUFFER_SIZE;
+ // LSB is a fpga signal bit.
+ ci = upTo[0] >> 1;
+ cq = upTo[1] >> 1;
+ upTo += 2;
+ samples += 2;
- // is this | 0x01 the error? & 0xfe in https://github.com/Proxmark/proxmark3/issues/103
- //gotFrame = Handle14443bTagSamplesDemod(ci & 0xfe, cq & 0xfe);
- gotFrame = Handle14443bTagSamplesDemod(ci, cq);
- if ( gotFrame ) break;
- LED_A_INV();
+ lastRxCounter -= 2;
+
+ // restart DMA buffer to receive again.
+ if(upTo >= dmaBuf + ISO14443B_DMA_BUFFER_SIZE) {
+ upTo = dmaBuf;
+ lastRxCounter = ISO14443B_DMA_BUFFER_SIZE;
+ AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
+ AT91C_BASE_PDC_SSC->PDC_RNCR = ISO14443B_DMA_BUFFER_SIZE;
}
+ // https://github.com/Proxmark/proxmark3/issues/103
+ //gotFrame = Handle14443bTagSamplesDemod(ci & 0xfe, cq & 0xfe);
+ gotFrame = Handle14443bTagSamplesDemod(ci, cq);
time_stop = GetCountSspClk() - time_0;
-
- if(time_stop > iso14b_timeout || gotFrame) break;
+
+ finished = (time_stop > iso14b_timeout || gotFrame);
}
FpgaDisableSscDma();
-
+
+ if ( upTo ) upTo = NULL;
+
if (MF_DBGLEVEL >= 3) {
- Dbprintf("max behindby = %d, samples = %d, gotFrame = %s, Demod.state = %d, Demod.len = %u",
- max,
- samples,
- (gotFrame) ? "true" : "false",
+ Dbprintf("Demod.state = %d, Demod.len = %u, PDC_RCR = %u",
Demod.state,
- Demod.len
+ Demod.len,
+ AT91C_BASE_PDC_SSC->PDC_RCR
);
}
+
+ // print the last batch of IQ values from FPGA
+ if (MF_DBGLEVEL == 4)
+ Dbhexdump(ISO14443B_DMA_BUFFER_SIZE, (uint8_t *)dmaBuf, FALSE);
+
if ( Demod.len > 0 )
LogTrace(Demod.output, Demod.len, Demod.startTime, Demod.endTime, NULL, FALSE);
}
void RAMFUNC SnoopIso14443b(void) {
uint32_t time_0 = 0, time_start = 0, time_stop = 0;
-
+ int ci = 0, cq = 0;
+ int lastRxCounter = ISO14443B_DMA_BUFFER_SIZE;
+
// We won't start recording the frames that we acquire until we trigger;
// a good trigger condition to get started is probably when we see a
// response from the tag.
- int triggered = TRUE; // TODO: set and evaluate trigger condition
- int ci, cq;
- int maxBehindBy = 0;
- //int behindBy = 0;
- int lastRxCounter = ISO14443B_DMA_BUFFER_SIZE;
-
+ bool triggered = TRUE; // TODO: set and evaluate trigger condition
bool TagIsActive = FALSE;
bool ReaderIsActive = FALSE;
for(;;) {
WDT_HIT();
-
- int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (ISO14443B_DMA_BUFFER_SIZE-1);
- if ( behindBy > maxBehindBy )
- maxBehindBy = behindBy;
-
- if ( behindBy < 2 ) continue;
-
ci = upTo[0];
cq = upTo[1];
- upTo += 2;
-
+ upTo += 2;
lastRxCounter -= 2;
if (upTo >= dmaBuf + ISO14443B_DMA_BUFFER_SIZE) {
upTo = dmaBuf;
- lastRxCounter += ISO14443B_DMA_BUFFER_SIZE;
+ lastRxCounter = ISO14443B_DMA_BUFFER_SIZE;
AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf;
AT91C_BASE_PDC_SSC->PDC_RNCR = ISO14443B_DMA_BUFFER_SIZE;
- WDT_HIT();
-
- // TODO: understand whether we can increase/decrease as we want or not?
- if ( behindBy > ( 9 * ISO14443B_DMA_BUFFER_SIZE/10) ) {
- Dbprintf("blew circular buffer! behindBy=%d", behindBy);
- break;
- }
-
- if(!tracing) {
- DbpString("Trace full");
+
+ if (!tracing) {
+ if (MF_DBGLEVEL >= 2) DbpString("Trace full");
break;
}
- if(BUTTON_PRESS()) {
- DbpString("cancelled");
+ if (BUTTON_PRESS()) {
+ if (MF_DBGLEVEL >= 2) DbpString("cancelled");
break;
}
}
// no need to try decoding reader data if the tag is sending
if (Handle14443bReaderUartBit(ci & 0x01)) {
- time_stop = (GetCountSspClk()-time_0);
+ time_stop = GetCountSspClk() - time_0;
if (triggered)
LogTrace(Uart.output, Uart.byteCnt, time_start, time_stop, NULL, TRUE);
/* false-triggered by the commands from the reader. */
DemodReset();
} else {
- time_start = (GetCountSspClk()-time_0);
+ time_start = GetCountSspClk() - time_0;
}
if (Handle14443bReaderUartBit(cq & 0x01)) {
- time_stop = (GetCountSspClk()-time_0);
+ time_stop = GetCountSspClk() - time_0;
if (triggered)
LogTrace(Uart.output, Uart.byteCnt, time_start, time_stop, NULL, TRUE);
/* false-triggered by the commands from the reader. */
DemodReset();
} else {
- time_start = (GetCountSspClk()-time_0);
+ time_start = GetCountSspClk() - time_0;
}
ReaderIsActive = (Uart.state > STATE_GOT_FALLING_EDGE_OF_SOF);
LED_A_OFF();
}
- if(!ReaderIsActive) {
+ if (!ReaderIsActive) {
// no need to try decoding tag data if the reader is sending - and we cannot afford the time
// is this | 0x01 the error? & 0xfe in https://github.com/Proxmark/proxmark3/issues/103
- if(Handle14443bTagSamplesDemod(ci & 0xFE, cq & 0xFE)) {
+ // LSB is a fpga signal bit.
+ if (Handle14443bTagSamplesDemod(ci >> 1, cq >> 1)) {
- time_stop = (GetCountSspClk()-time_0);
+ time_stop = GetCountSspClk() - time_0;
LogTrace(Demod.output, Demod.len, time_start, time_stop, NULL, FALSE);
// And ready to receive another response.
DemodReset();
} else {
- time_start = (GetCountSspClk()-time_0);
+ time_start = GetCountSspClk() - time_0;
}
TagIsActive = (Demod.state > DEMOD_GOT_FALLING_EDGE_OF_SOF);
}
switch_off(); // Snoop
DbpString("Snoop statistics:");
- Dbprintf(" Max behind by: %i", maxBehindBy);
Dbprintf(" Uart State: %x ByteCount: %i ByteCountMax: %i", Uart.state, Uart.byteCnt, Uart.byteCntMax);
Dbprintf(" Trace length: %i", BigBuf_get_traceLen());
// free mem refs.
- if ( upTo ) upTo = NULL;
+ if ( upTo ) upTo = NULL;
// Uart.byteCntMax should be set with ATQB value..
}