]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/iclass.c
CHG: Added @icsom 's changes to his legic.lua script.
[proxmark3-svn] / armsrc / iclass.c
index 2dff8a4e7246d4cef8cee4c0ee589ae50c1abd11..dcb672e73515414d750155f215b2b866536b18e5 100644 (file)
@@ -633,8 +633,6 @@ static RAMFUNC int ManchesterDecoding(int v)
 //-----------------------------------------------------------------------------
 void RAMFUNC SnoopIClass(void)
 {
-
-
     // 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.
@@ -655,8 +653,9 @@ void RAMFUNC SnoopIClass(void)
     // The DMA buffer, used to stream samples from the FPGA
     uint8_t *dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE);
  
-       set_tracing(TRUE);
        clear_trace();
+       set_tracing(TRUE);
+       
     iso14a_set_trigger(FALSE);
 
     int lastRxCounter;
@@ -705,22 +704,22 @@ void RAMFUNC SnoopIClass(void)
     for(;;) {
         LED_A_ON();
         WDT_HIT();
-        int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) &
-                                (DMA_BUFFER_SIZE-1);
-        if(behindBy > maxBehindBy) {
+        int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (DMA_BUFFER_SIZE-1);
+
+        if ( behindBy > maxBehindBy) {
             maxBehindBy = behindBy;
-            if(behindBy > (9 * DMA_BUFFER_SIZE / 10)) {
+            if ( behindBy > (9 * DMA_BUFFER_SIZE / 10)) {
                 Dbprintf("blew circular buffer! behindBy=0x%x", behindBy);
                 goto done;
             }
         }
-        if(behindBy < 1) continue;
+        if( behindBy < 1) continue;
 
-       LED_A_OFF();
+               LED_A_OFF();
         smpl = upTo[0];
         upTo++;
         lastRxCounter -= 1;
-        if(upTo - dmaBuf > DMA_BUFFER_SIZE) {
+        if (upTo - dmaBuf > DMA_BUFFER_SIZE) {
             upTo -= DMA_BUFFER_SIZE;
             lastRxCounter += DMA_BUFFER_SIZE;
             AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
@@ -728,77 +727,75 @@ void RAMFUNC SnoopIClass(void)
         }
 
         //samples += 4;
-       samples += 1;
+               samples += 1;
 
-       if(smpl & 0xF) {
-               decbyte ^= (1 << (3 - div));
-       }
+               if(smpl & 0xF)
+                       decbyte ^= (1 << (3 - div));
        
-       // FOR READER SIDE COMMUMICATION...
+       
+               // FOR READER SIDE COMMUMICATION...
 
-       decbyter <<= 2;
-       decbyter ^= (smpl & 0x30);
+               decbyter <<= 2;
+               decbyter ^= (smpl & 0x30);
 
-       div++;
+               ++div;
        
-       if((div + 1) % 2 == 0) {
-               smpl = decbyter;        
-               if(OutOfNDecoding((smpl & 0xF0) >> 4)) {
-                   rsamples = samples - Uart.samples;
-                       time_stop = (GetCountSspClk()-time_0) << 4;
-                   LED_C_ON();
-
-                       //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
-                       //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
-                       if(tracing)     {
-                               uint8_t parity[MAX_PARITY_SIZE];
-                               GetParity(Uart.output, Uart.byteCnt, parity);
-                               LogTrace(Uart.output,Uart.byteCnt, time_start, time_stop, parity, TRUE);
-                       }
-
+               if (( div + 1) % 2 == 0) {
+                       smpl = decbyter;        
+                       if ( OutOfNDecoding((smpl & 0xF0) >> 4)) {
+                               rsamples = samples - Uart.samples;
+                               time_stop = (GetCountSspClk()-time_0) << 4;
+                               LED_C_ON();
+
+                               //if(!LogTrace(Uart.output,Uart.byteCnt, rsamples, Uart.parityBits,TRUE)) break;
+                               //if(!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, TRUE)) break;
+                               if(tracing)     {
+                                       uint8_t parity[MAX_PARITY_SIZE];
+                                       GetParity(Uart.output, Uart.byteCnt, parity);
+                                       LogTrace(Uart.output,Uart.byteCnt, time_start, time_stop, parity, TRUE);
+                               }
 
-                       /* And ready to receive another command. */
-                   Uart.state = STATE_UNSYNCD;
-                   /* And also reset the demod code, which might have been */
-                   /* false-triggered by the commands from the reader. */
-                   Demod.state = DEMOD_UNSYNCD;
-                   LED_B_OFF();
-                   Uart.byteCnt = 0;
-               }else{
-                       time_start = (GetCountSspClk()-time_0) << 4;
+                               /* And ready to receive another command. */
+                               Uart.state = STATE_UNSYNCD;
+                               /* And also reset the demod code, which might have been */
+                               /* false-triggered by the commands from the reader. */
+                               Demod.state = DEMOD_UNSYNCD;
+                               LED_B_OFF();
+                               Uart.byteCnt = 0;
+                       } else {
+                               time_start = (GetCountSspClk()-time_0) << 4;
+                       }
+                       decbyter = 0;
                }
-               decbyter = 0;
-       }
 
-       if(div > 3) {
-               smpl = decbyte;
-               if(ManchesterDecoding(smpl & 0x0F)) {
-                       time_stop = (GetCountSspClk()-time_0) << 4;
+               if(div > 3) {
+                       smpl = decbyte;
+                       if(ManchesterDecoding(smpl & 0x0F)) {
+                               time_stop = (GetCountSspClk()-time_0) << 4;
 
-                   rsamples = samples - Demod.samples;
-                   LED_B_ON();
+                               rsamples = samples - Demod.samples;
+                               LED_B_ON();
 
-                       if(tracing)     {
-                               uint8_t parity[MAX_PARITY_SIZE];
-                               GetParity(Demod.output, Demod.len, parity);
-                               LogTrace(Demod.output, Demod.len, time_start, time_stop, parity, FALSE);
-                       }
+                               if(tracing)     {
+                                       uint8_t parity[MAX_PARITY_SIZE];
+                                       GetParity(Demod.output, Demod.len, parity);
+                                       LogTrace(Demod.output, Demod.len, time_start, time_stop, parity, FALSE);
+                               }
 
-                   // And ready to receive another response.
-                   memset(&Demod, 0, sizeof(Demod));
-                       Demod.output = tagToReaderResponse;
-                   Demod.state = DEMOD_UNSYNCD;
-                   LED_C_OFF();
-               }else{
-                       time_start = (GetCountSspClk()-time_0) << 4;
+                               // And ready to receive another response.
+                               memset(&Demod, 0, sizeof(Demod));
+                               Demod.output = tagToReaderResponse;
+                               Demod.state = DEMOD_UNSYNCD;
+                               LED_C_OFF();
+                       } else {
+                               time_start = (GetCountSspClk()-time_0) << 4;
+                       }
+                       
+                       div = 0;
+                       decbyte = 0x00;
                }
-               
-               div = 0;
-               decbyte = 0x00;
-       }
-       //}
 
-        if(BUTTON_PRESS()) {
+        if (BUTTON_PRESS()) {
             DbpString("cancelled_a");
             goto done;
         }
@@ -813,18 +810,14 @@ done:
     AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
     Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
        Dbprintf("%x %x %x", Uart.byteCntMax, BigBuf_get_traceLen(), (int)Uart.output[0]);
-    LED_A_OFF();
-    LED_B_OFF();
-    LED_C_OFF();
-    LED_D_OFF();
+       LEDsoff();
        set_tracing(FALSE);     
 }
 
 void rotateCSN(uint8_t* originalCSN, uint8_t* rotatedCSN) {
        int i; 
-       for(i = 0; i < 8; i++) {
+       for(i = 0; i < 8; i++)
                rotatedCSN[i] = (originalCSN[i] >> 3) | (originalCSN[(i+1)%8] << 5);
-       }
 }
 
 //-----------------------------------------------------------------------------
@@ -993,8 +986,9 @@ void SimulateIClass(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain
        FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
 
        // Enable and clear the trace
-       set_tracing(TRUE);
        clear_trace();
+       set_tracing(TRUE);
+
        //Use the emulator memory for SIM
        uint8_t *emulator = BigBuf_get_EM_addr();
 
@@ -1331,23 +1325,17 @@ int doIClassSimulation( int simulationMode, uint8_t *reader_mac_buf)
                                GetParity(trace_data, trace_data_size, parity);
                                LogTrace(trace_data, trace_data_size, (t2r_time-time_0) << 4, (t2r_time-time_0) << 4, parity, FALSE);
                        }
-                       if(!tracing) {
+                       if(!tracing)
                                DbpString("Trace full");
-                               //break;
-                       }
 
                }
        }
 
-       //Dbprintf("%x", cmdsRecvd);
-       LED_A_OFF();
-       LED_B_OFF();
-       LED_C_OFF();
-
+       LEDsoff();
+       
        if(buttonPressed)
-       {
                DbpString("Button pressed");
-       }
+       
        return buttonPressed;
 }
 
@@ -1395,33 +1383,31 @@ static int SendIClassAnswer(uint8_t *resp, int respLen, int delay)
 //-----------------------------------------------------------------------------
 static void TransmitIClassCommand(const uint8_t *cmd, int len, int *samples, int *wait)
 {
-  int c;
-  FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
-  AT91C_BASE_SSC->SSC_THR = 0x00;
-  FpgaSetupSsc();
-
-   if (wait)
-   {
-     if(*wait < 10) *wait = 10;
-     
-  for(c = 0; c < *wait;) {
-    if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-      AT91C_BASE_SSC->SSC_THR = 0x00;          // For exact timing!
-      c++;
-    }
-    if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
-      volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
-      (void)r;
-    }
-    WDT_HIT();
-  }
+       int c;
+       FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
+       AT91C_BASE_SSC->SSC_THR = 0x00;
+       FpgaSetupSsc();
+
+       if (wait) {
+               if(*wait < 10) *wait = 10;
 
-   }
+               for(c = 0; c < *wait;) {
+                       if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
+                               AT91C_BASE_SSC->SSC_THR = 0x00;         // For exact timing!
+                               c++;
+                       }
+                       if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+                               volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
+                               (void)r;
+                       }
+                       WDT_HIT();
+               }
+       }
 
 
-  uint8_t sendbyte;
-  bool firstpart = TRUE;
-  c = 0;
+       uint8_t sendbyte;
+       bool firstpart = TRUE;
+       c = 0;
   for(;;) {
     if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
 
@@ -1449,7 +1435,7 @@ static void TransmitIClassCommand(const uint8_t *cmd, int len, int *samples, int
     }
     WDT_HIT();
   }
-  if (samples) *samples = (c + *wait) << 3;
+  if (samples && wait) *samples = (c + *wait) << 3;
 }
 
 
@@ -1582,9 +1568,9 @@ void setupIclassReader()
 {
     FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
     // Reset trace buffer
-       set_tracing(TRUE);
        clear_trace();
-
+       set_tracing(TRUE);
+       
     // Setup SSC
     FpgaSetupSsc();
     // Start from off (no field generated)
@@ -1705,7 +1691,9 @@ void ReaderIClass(uint8_t arg0) {
     while(!BUTTON_PRESS())
     {
                if (try_once && tryCnt > 5) break; 
+               
                tryCnt++;
+               
                if(!tracing) {
                        DbpString("Trace full");
                        break;
Impressum, Datenschutz