X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/c86cc30801a3ea718df1daa45e989cb40f8c5950..5a956258d3d8416f1cd2259319eae784d008a46b:/armsrc/iso14443.c diff --git a/armsrc/iso14443.c b/armsrc/iso14443.c index fbc3a6d4..b20d1be9 100644 --- a/armsrc/iso14443.c +++ b/armsrc/iso14443.c @@ -6,12 +6,15 @@ //----------------------------------------------------------------------------- #include #include "apps.h" -#include "../common/iso14443_crc.c" +#include "iso14443crc.h" //static void GetSamplesFor14443(BOOL weTx, int n); -#define DMA_BUFFER_SIZE 256 +#define DEMOD_TRACE_SIZE 4096 +#define READER_TAG_BUFFER_SIZE 2048 +#define TAG_READER_BUFFER_SIZE 2048 +#define DMA_BUFFER_SIZE 1024 //============================================================================= // An ISO 14443 Type B tag. We listen for commands from the reader, using @@ -353,9 +356,8 @@ void SimulateIso14443Tag(void) BYTE b1, b2; if(!GetIso14443CommandFromReader(receivedCmd, &len, 100)) { - DbpIntegers(cmdsRecvd, 0, 0); - DbpString("button press"); - break; + Dbprintf("button pressed, received %d commands", cmdsRecvd); + break; } // Good, look at the command now. @@ -363,8 +365,7 @@ void SimulateIso14443Tag(void) if(len == sizeof(cmd1) && memcmp(receivedCmd, cmd1, len)==0) { resp = resp1; respLen = resp1Len; } else { - DbpString("new cmd from reader:"); - DbpIntegers(len, 0x1234, cmdsRecvd); + Dbprintf("new cmd from reader: len=%d, cmdsRecvd=%d", len, cmdsRecvd); // And print whether the CRC fails, just for good measure ComputeCrc14443(CRC_14443_B, receivedCmd, len-2, &b1, &b2); if(b1 != receivedCmd[len-2] || b2 != receivedCmd[len-1]) { @@ -688,7 +689,7 @@ static void GetSamplesFor14443Demod(BOOL weTx, int n, BOOL quiet) } } AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; - if (!quiet) DbpIntegers(max, gotFrame, Demod.len); + if (!quiet) Dbprintf("%x %x %x", max, gotFrame, Demod.len); } //----------------------------------------------------------------------------- @@ -873,15 +874,15 @@ void AcquireRawAdcSamplesIso14443(DWORD parameter) //----------------------------------------------------------------------------- void ReadSRI512Iso14443(DWORD parameter) { - ReadSTMemoryIso14443(parameter,0x0F); -} -void ReadSRIX4KIso14443(DWORD parameter) -{ - ReadSTMemoryIso14443(parameter,0x7F); -} - -void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) -{ + ReadSTMemoryIso14443(parameter,0x0F); +} +void ReadSRIX4KIso14443(DWORD parameter) +{ + ReadSTMemoryIso14443(parameter,0x7F); +} + +void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) +{ BYTE i = 0x00; // Make sure that we start from off, since the tags are stateful; @@ -912,8 +913,8 @@ void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) DbpString("No response from tag"); return; } else { - DbpString("Randomly generated UID from tag (+ 2 byte CRC):"); - DbpIntegers(Demod.output[0], Demod.output[1],Demod.output[2]); + Dbprintf("Randomly generated UID from tag (+ 2 byte CRC): %x %x %x", + Demod.output[0], Demod.output[1],Demod.output[2]); } // There is a response, SELECT the uid DbpString("Now SELECT tag:"); @@ -926,8 +927,7 @@ void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) GetSamplesFor14443Demod(TRUE, 2000,TRUE); // LED_A_OFF(); if (Demod.len != 3) { - DbpString("Expected 3 bytes from tag, got:"); - DbpIntegers(Demod.len,0x0,0x0); + Dbprintf("Expected 3 bytes from tag, got %d", Demod.len); return; } // Check the CRC of the answer: @@ -938,8 +938,7 @@ void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) } // Check response from the tag: should be the same UID as the command we just sent: if (cmd1[1] != Demod.output[0]) { - DbpString("Bad response to SELECT from Tag, aborting:"); - DbpIntegers(cmd1[1],Demod.output[0],0x0); + Dbprintf("Bad response to SELECT from Tag, aborting: %x %x", cmd1[1], Demod.output[0]); return; } // Tag is now selected, @@ -952,27 +951,27 @@ void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) GetSamplesFor14443Demod(TRUE, 2000,TRUE); // LED_A_OFF(); if (Demod.len != 10) { - DbpString("Expected 10 bytes from tag, got:"); - DbpIntegers(Demod.len,0x0,0x0); + Dbprintf("Expected 10 bytes from tag, got %d", Demod.len); return; } // The check the CRC of the answer (use cmd1 as temporary variable): ComputeCrc14443(CRC_14443_B, Demod.output, 8, &cmd1[2], &cmd1[3]); if(cmd1[2] != Demod.output[8] || cmd1[3] != Demod.output[9]) { - DbpString("CRC Error reading block! - Below: expected, got"); - DbpIntegers( (cmd1[2]<<8)+cmd1[3], (Demod.output[8]<<8)+Demod.output[9],0); + Dbprintf("CRC Error reading block! - Below: expected, got %x %x", + (cmd1[2]<<8)+cmd1[3], (Demod.output[8]<<8)+Demod.output[9]); // Do not return;, let's go on... (we should retry, maybe ?) } - DbpString("Tag UID (64 bits):"); - DbpIntegers((Demod.output[7]<<24) + (Demod.output[6]<<16) + (Demod.output[5]<<8) + Demod.output[4], (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0], 0); + Dbprintf("Tag UID (64 bits): %08x %08x", + (Demod.output[7]<<24) + (Demod.output[6]<<16) + (Demod.output[5]<<8) + Demod.output[4], + (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0]); // Now loop to read all 16 blocks, address from 0 to 15 DbpString("Tag memory dump, block 0 to 15"); cmd1[0] = 0x08; i = 0x00; - dwLast++; + dwLast++; for (;;) { - if (i == dwLast) { + if (i == dwLast) { DbpString("System area block (0xff):"); i = 0xff; } @@ -990,13 +989,14 @@ void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) // The check the CRC of the answer (use cmd1 as temporary variable): ComputeCrc14443(CRC_14443_B, Demod.output, 4, &cmd1[2], &cmd1[3]); if(cmd1[2] != Demod.output[4] || cmd1[3] != Demod.output[5]) { - DbpString("CRC Error reading block! - Below: expected, got"); - DbpIntegers( (cmd1[2]<<8)+cmd1[3], (Demod.output[4]<<8)+Demod.output[5],0); + Dbprintf("CRC Error reading block! - Below: expected, got %x %x", + (cmd1[2]<<8)+cmd1[3], (Demod.output[4]<<8)+Demod.output[5]); // Do not return;, let's go on... (we should retry, maybe ?) } // Now print out the memory location: - DbpString("Address , Contents, CRC"); - DbpIntegers(i, (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0], (Demod.output[4]<<8)+Demod.output[5]); + Dbprintf("Address=%x, Contents=%x, CRC=%x", i, + (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0], + (Demod.output[4]<<8)+Demod.output[5]); if (i == 0xff) { break; } @@ -1017,10 +1017,10 @@ void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) //----------------------------------------------------------------------------- /* * Memory usage for this function, (within BigBuf) - * 0-1023 : Demodulated samples receive (1024 bytes) - * 1024-1535 : Last Received command, 512 bytes (reader->tag) - * 1536-2047 : Last Received command, 512 bytes(tag->reader) - * 2048-2304 : DMA Buffer, 256 bytes (samples) + * 0-4095 : Demodulated samples receive (4096 bytes) - DEMOD_TRACE_SIZE + * 4096-6143 : Last Received command, 2048 bytes (reader->tag) - READER_TAG_BUFFER_SIZE + * 6144-8191 : Last Received command, 2048 bytes(tag->reader) - TAG_READER_BUFFER_SIZE + * 8192-9215 : DMA Buffer, 1024 bytes (samples) - DMA_BUFFER_SIZE */ void SnoopIso14443(void) { @@ -1030,9 +1030,9 @@ void SnoopIso14443(void) BOOL triggered = FALSE; // The command (reader -> tag) that we're working on receiving. - BYTE *receivedCmd = (BYTE *)(BigBuf) + 1024; + BYTE *receivedCmd = (BYTE *)(BigBuf) + DEMOD_TRACE_SIZE; // The response (tag -> reader) that we're working on receiving. - BYTE *receivedResponse = (BYTE *)(BigBuf) + 1536; + BYTE *receivedResponse = (BYTE *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE; // As we receive stuff, we copy it from receivedCmd or receivedResponse // into trace, along with its length and other annotations. @@ -1040,7 +1040,7 @@ void SnoopIso14443(void) int traceLen = 0; // The DMA buffer, used to stream samples from the FPGA. - SBYTE *dmaBuf = (SBYTE *)(BigBuf) + 2048; + SBYTE *dmaBuf = (SBYTE *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE + TAG_READER_BUFFER_SIZE; int lastRxCounter; SBYTE *upTo; int ci, cq; @@ -1051,7 +1051,7 @@ void SnoopIso14443(void) int samples = 0; // Initialize the trace buffer - memset(trace, 0x44, 1024); + memset(trace, 0x44, DEMOD_TRACE_SIZE); // Set up the demodulator for tag -> reader responses. Demod.output = receivedResponse; @@ -1064,6 +1064,17 @@ void SnoopIso14443(void) Uart.byteCntMax = 100; Uart.state = STATE_UNSYNCD; + // Print some debug information about the buffer sizes + Dbprintf("Snooping buffers initialized:"); + Dbprintf(" Trace: %i bytes", DEMOD_TRACE_SIZE); + Dbprintf(" Reader -> tag: %i bytes", READER_TAG_BUFFER_SIZE); + Dbprintf(" tag -> Reader: %i bytes", TAG_READER_BUFFER_SIZE); + Dbprintf(" DMA: %i bytes", DMA_BUFFER_SIZE); + + // Use a counter for blinking the LED + long ledCount=0; + long ledFlashAt=200000; + // And put the FPGA in the appropriate mode // Signal field is off with the appropriate LED LED_D_OFF(); @@ -1079,13 +1090,22 @@ void SnoopIso14443(void) FpgaSetupSscDma((BYTE *)dmaBuf, DMA_BUFFER_SIZE); // And now we loop, receiving samples. for(;;) { + // Blink the LED while Snooping + ledCount++; + if (ledCount == ledFlashAt) { + LED_D_ON(); + } + if (ledCount >= 2*ledFlashAt) { + LED_D_OFF(); + ledCount=0; + } + int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (DMA_BUFFER_SIZE-1); if(behindBy > maxBehindBy) { maxBehindBy = behindBy; if(behindBy > (DMA_BUFFER_SIZE-2)) { // TODO: understand whether we can increase/decrease as we want or not? - DbpString("blew circular buffer!"); - DbpIntegers(behindBy,0,0); + Dbprintf("blew circular buffer! behindBy=%x", behindBy); goto done; } } @@ -1106,6 +1126,7 @@ void SnoopIso14443(void) #define HANDLE_BIT_IF_BODY \ if(triggered) { \ + ledFlashAt=30000; \ trace[traceLen++] = ((samples >> 0) & 0xff); \ trace[traceLen++] = ((samples >> 8) & 0xff); \ trace[traceLen++] = ((samples >> 16) & 0xff); \ @@ -1155,7 +1176,10 @@ void SnoopIso14443(void) trace[traceLen++] = Demod.len; memcpy(trace+traceLen, receivedResponse, Demod.len); traceLen += Demod.len; - if(traceLen > 1000) break; + if(traceLen > DEMOD_TRACE_SIZE) { + DbpString("Reached trace limit"); + goto done; + } triggered = TRUE; @@ -1172,12 +1196,13 @@ void SnoopIso14443(void) } } - DbpString("in done pt"); - - DbpIntegers(maxBehindBy, Uart.state, Uart.byteCnt); - DbpIntegers(Uart.byteCntMax, traceLen, 0x23); - done: LED_D_OFF(); AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; + DbpString("Snoop statistics:"); + Dbprintf(" Max behind by: %i", maxBehindBy); + Dbprintf(" Uart State: %x", Uart.state); + Dbprintf(" Uart ByteCnt: %i", Uart.byteCnt); + Dbprintf(" Uart ByteCntMax: %i", Uart.byteCntMax); + Dbprintf(" Trace length: %i", traceLen); }