]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
major USB update
authorroel@libnfc.org <roel@libnfc.org@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Tue, 4 Dec 2012 23:39:18 +0000 (23:39 +0000)
committerroel@libnfc.org <roel@libnfc.org@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Tue, 4 Dec 2012 23:39:18 +0000 (23:39 +0000)
44 files changed:
armsrc/LCD.c
armsrc/Makefile
armsrc/appmain.c
armsrc/apps.h
armsrc/epa.c
armsrc/fpgaloader.c
armsrc/iso14443a.c
armsrc/iso14443a.h
armsrc/iso15693.c
armsrc/mifaresniff.c
armsrc/util.h
bootrom/bootrom.c
client/Makefile
client/cmddata.c
client/cmdhf.c
client/cmdhf14a.c
client/cmdhf14b.c
client/cmdhf15.c
client/cmdhfepa.c
client/cmdhficlass.c
client/cmdhflegic.c
client/cmdhfmf.c
client/cmdhw.c
client/cmdlf.c
client/cmdlfem4x.c
client/cmdlfhid.c
client/cmdlfhitag.c
client/cmdlfti.c
client/cmdmain.c
client/cmdmain.h
client/cmdparser.c
client/data.c
client/data.h
client/flash.c
client/hitag2.ht2
client/mifarehost.c
client/proxmark3.c
client/proxmark3.h
client/proxusb.c
client/proxusb.h
client/util.h
common/Makefile.common
include/common.h
include/usb_cmd.h

index f37aa894401dda7021b5fc99bfd4412806962b6a..65d64ac90ed9d87c831a568020669d824d98e98f 100644 (file)
@@ -9,6 +9,7 @@
 #include "proxmark3.h"
 #include "apps.h"
 #include "LCD.h"
+#include "fonts.h"
 
 void LCDSend(unsigned int data)
 {
@@ -128,4 +129,25 @@ void LCDInit(void)
        LCDSend(PRAMWR);                        // Write to display
        i=LCD_XRES*LCD_YRES;
        while(i--) LCDSend(WHITE);
+  
+  // test text on different colored backgrounds
+       LCDString(" The quick brown fox  ",     (char *)&FONT6x8,1,1+8*0,WHITE  ,BLACK );
+       LCDString("  jumped over the     ",     (char *)&FONT6x8,1,1+8*1,BLACK  ,WHITE );
+       LCDString("     lazy dog.        ",     (char *)&FONT6x8,1,1+8*2,YELLOW ,RED   );
+       LCDString(" AaBbCcDdEeFfGgHhIiJj ",     (char *)&FONT6x8,1,1+8*3,RED    ,GREEN );
+       LCDString(" KkLlMmNnOoPpQqRrSsTt ",     (char *)&FONT6x8,1,1+8*4,MAGENTA,BLUE  );
+       LCDString("UuVvWwXxYyZz0123456789",     (char *)&FONT6x8,1,1+8*5,BLUE   ,YELLOW);
+       LCDString("`-=[]_;',./~!@#$%^&*()",     (char *)&FONT6x8,1,1+8*6,BLACK  ,CYAN  );
+       LCDString("     _+{}|:\\\"<>?     ",(char *)&FONT6x8,1,1+8*7,BLUE  ,MAGENTA);
+  
+       // color bands
+       LCDFill(0, 1+8* 8, 132, 8, BLACK);
+       LCDFill(0, 1+8* 9, 132, 8, WHITE);
+       LCDFill(0, 1+8*10, 132, 8, RED);
+       LCDFill(0, 1+8*11, 132, 8, GREEN);
+       LCDFill(0, 1+8*12, 132, 8, BLUE);
+       LCDFill(0, 1+8*13, 132, 8, YELLOW);
+       LCDFill(0, 1+8*14, 132, 8, CYAN);
+       LCDFill(0, 1+8*15, 132, 8, MAGENTA);
+
 }
index 715eba0a05ccfaf5e2e8cbe5c2869b00de0f77ea..813db17fc511528081f491d1bbdf579f02c5b505 100644 (file)
@@ -27,7 +27,9 @@ THUMBSRC = start.c \
        appmain.c printf.c \
        util.c \
        string.c \
-       usb.c
+       usb.c \
+       usb_cdc.c \
+       cmd.c
 
 # These are to be compiled in ARM mode
 ARMSRC = fpgaloader.c \
index dac87677c60c6e8a8b0ba337e2118fd199cec84c..e501dfdd9f236162c75ced0d4260b54aadc83708 100644 (file)
@@ -10,6 +10,9 @@
 // executes.
 //-----------------------------------------------------------------------------
 
+#include "usb_cdc.h"
+#include "cmd.h"
+
 #include "proxmark3.h"
 #include "apps.h"
 #include "util.h"
@@ -22,8 +25,7 @@
 #include <hitag2.h>
 
 #ifdef WITH_LCD
-# include "fonts.h"
-# include "LCD.h"
+ #include "LCD.h"
 #endif
 
 #define abs(x) ( ((x)<0) ? -(x) : (x) )
@@ -77,39 +79,41 @@ void ToSendStuffBit(int b)
 
 void DbpString(char *str)
 {
-       /* this holds up stuff unless we're connected to usb */
-       if (!UsbConnected())
-               return;
-
-       UsbCommand c;
-       c.cmd = CMD_DEBUG_PRINT_STRING;
-       c.arg[0] = strlen(str);
-       if(c.arg[0] > sizeof(c.d.asBytes)) {
-               c.arg[0] = sizeof(c.d.asBytes);
-       }
-       memcpy(c.d.asBytes, str, c.arg[0]);
-
-       UsbSendPacket((uint8_t *)&c, sizeof(c));
-       // TODO fix USB so stupid things like this aren't req'd
-       SpinDelay(50);
+  cmd_send(CMD_DEBUG_PRINT_STRING,strlen(str),0,0,(byte_t*)str,strlen(str));
+//     /* this holds up stuff unless we're connected to usb */
+//     if (!UsbConnected())
+//             return;
+//
+//     UsbCommand c;
+//     c.cmd = CMD_DEBUG_PRINT_STRING;
+//     c.arg[0] = strlen(str);
+//     if(c.arg[0] > sizeof(c.d.asBytes)) {
+//             c.arg[0] = sizeof(c.d.asBytes);
+//     }
+//     memcpy(c.d.asBytes, str, c.arg[0]);
+//
+//     UsbSendPacket((uint8_t *)&c, sizeof(c));
+//     // TODO fix USB so stupid things like this aren't req'd
+//     SpinDelay(50);
 }
 
 #if 0
 void DbpIntegers(int x1, int x2, int x3)
 {
-       /* this holds up stuff unless we're connected to usb */
-       if (!UsbConnected())
-               return;
-
-       UsbCommand c;
-       c.cmd = CMD_DEBUG_PRINT_INTEGERS;
-       c.arg[0] = x1;
-       c.arg[1] = x2;
-       c.arg[2] = x3;
-
-       UsbSendPacket((uint8_t *)&c, sizeof(c));
-       // XXX
-       SpinDelay(50);
+  cmd_send(CMD_DEBUG_PRINT_INTEGERS,x1,x2,x3,0,0);
+//     /* this holds up stuff unless we're connected to usb */
+//     if (!UsbConnected())
+//             return;
+//
+//     UsbCommand c;
+//     c.cmd = CMD_DEBUG_PRINT_INTEGERS;
+//     c.arg[0] = x1;
+//     c.arg[1] = x2;
+//     c.arg[2] = x3;
+//
+//     UsbSendPacket((uint8_t *)&c, sizeof(c));
+//     // XXX
+//     SpinDelay(50);
 }
 #endif
 
@@ -194,7 +198,7 @@ void MeasureAntennaTuning(void)
        int i, adcval = 0, peak = 0, peakv = 0, peakf = 0; //ptr = 0 
        int vLf125 = 0, vLf134 = 0, vHf = 0;    // in mV
 
-       UsbCommand c;
+//     UsbCommand c;
 
   LED_B_ON();
        DbpString("Measuring antenna characteristics, please wait...");
@@ -237,14 +241,14 @@ void MeasureAntennaTuning(void)
        // can measure voltages up to 33000 mV
        vHf = (33000 * AvgAdc(ADC_CHAN_HF)) >> 10;
 
-       c.cmd = CMD_MEASURED_ANTENNA_TUNING;
-       c.arg[0] = (vLf125 << 0) | (vLf134 << 16);
-       c.arg[1] = vHf;
-       c.arg[2] = peakf | (peakv << 16);
+//     c.cmd = CMD_MEASURED_ANTENNA_TUNING;
+//     c.arg[0] = (vLf125 << 0) | (vLf134 << 16);
+//     c.arg[1] = vHf;
+//     c.arg[2] = peakf | (peakv << 16);
 
   DbpString("Measuring complete, sending report back to host");
-
-       UsbSendPacket((uint8_t *)&c, sizeof(c));
+  cmd_send(CMD_MEASURED_ANTENNA_TUNING,vLf125|(vLf134<<16),vHf,peakf|(peakv<<16),0,0);
+//     UsbSendPacket((uint8_t *)&c, sizeof(c));
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
   LED_A_OFF();
   LED_B_OFF();
@@ -613,14 +617,14 @@ void ListenReaderField(int limit)
 void UsbPacketReceived(uint8_t *packet, int len)
 {
        UsbCommand *c = (UsbCommand *)packet;
-       UsbCommand ack;
-       ack.cmd = CMD_ACK;
 
+//  Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
+  
        switch(c->cmd) {
 #ifdef WITH_LF
                case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:
                        AcquireRawAdcSamples125k(c->arg[0]);
-                       UsbSendPacket((uint8_t*)&ack, sizeof(ack));
+      cmd_send(CMD_ACK,0,0,0,0,0);
                        break;
                case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K:
                        ModThenAcquireRawAdcSamples125k(c->arg[0],c->arg[1],c->arg[2],c->d.asBytes);
@@ -736,13 +740,13 @@ void UsbPacketReceived(uint8_t *packet, int len)
                        SnoopIso14443a(c->arg[0]);
                        break;
                case CMD_READER_ISO_14443a:
-                       ReaderIso14443a(c, &ack);
+                       ReaderIso14443a(c);
                        break;
                case CMD_SIMULATE_TAG_ISO_14443a:
                        SimulateIso14443aTag(c->arg[0], c->arg[1], c->arg[2]);  // ## Simulate iso14443a tag - pass tag type & UID
                        break;
                case CMD_EPA_PACE_COLLECT_NONCE:
-                       EPA_PACE_Collect_Nonce(c, &ack);
+                       EPA_PACE_Collect_Nonce(c);
                        break;
                        
                case CMD_READER_MIFARE:
@@ -838,16 +842,26 @@ void UsbPacketReceived(uint8_t *packet, int len)
                        break;
 
                case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {
-                       UsbCommand n;
-                       if(c->cmd == CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K) {
-                               n.cmd = CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K;
-                       } else {
-                               n.cmd = CMD_DOWNLOADED_RAW_BITS_TI_TYPE;
-                       }
-                       n.arg[0] = c->arg[0];
-                       memcpy(n.d.asDwords, BigBuf+c->arg[0], 12*sizeof(uint32_t));
-                       LED_B_ON();
-                       UsbSendPacket((uint8_t *)&n, sizeof(n));
+//                     UsbCommand n;
+//                     if(c->cmd == CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K) {
+//                             n.cmd = CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K;
+//                     } else {
+//                             n.cmd = CMD_DOWNLOADED_RAW_BITS_TI_TYPE;
+//                     }
+//                     n.arg[0] = c->arg[0];
+      //                       memcpy(n.d.asBytes, BigBuf+c->arg[0], 48); // 12*sizeof(uint32_t)
+      //                       LED_B_ON();
+      //      usb_write((uint8_t *)&n, sizeof(n));
+      //                       UsbSendPacket((uint8_t *)&n, sizeof(n));
+      //                       LED_B_OFF();
+
+      LED_B_ON();
+      for(size_t i=0; i<c->arg[1]; i += USB_CMD_DATA_SIZE) {
+        size_t len = MIN((c->arg[1] - i),USB_CMD_DATA_SIZE);
+        cmd_send(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K,i,len,0,((byte_t*)BigBuf)+c->arg[0]+i,len);
+      }
+      // Trigger a finish downloading signal with an ACK frame
+      cmd_send(CMD_ACK,0,0,0,0,0);
                        LED_B_OFF();
                } break;
 
@@ -855,7 +869,8 @@ void UsbPacketReceived(uint8_t *packet, int len)
                        uint8_t *b = (uint8_t *)BigBuf;
                        memcpy(b+c->arg[0], c->d.asBytes, 48);
                        //Dbprintf("copied 48 bytes to %i",b+c->arg[0]);
-                       UsbSendPacket((uint8_t*)&ack, sizeof(ack));
+//                     UsbSendPacket((uint8_t*)&ack, sizeof(ack));
+      cmd_send(CMD_ACK,0,0,0,0,0);
                } break;
 
                case CMD_READ_MEM:
@@ -909,11 +924,10 @@ void UsbPacketReceived(uint8_t *packet, int len)
         } break;
 
                case CMD_DEVICE_INFO: {
-                       UsbCommand c;
-                       c.cmd = CMD_DEVICE_INFO;
-                       c.arg[0] = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS;
-                       if(common_area.flags.bootrom_present) c.arg[0] |= DEVICE_INFO_FLAG_BOOTROM_PRESENT;
-                       UsbSendPacket((uint8_t*)&c, sizeof(c));
+                       uint32_t dev_info = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS;
+                       if(common_area.flags.bootrom_present) dev_info |= DEVICE_INFO_FLAG_BOOTROM_PRESENT;
+//                     UsbSendPacket((uint8_t*)&c, sizeof(c));
+      cmd_send(CMD_DEVICE_INFO,dev_info,0,0,0,0);
                } break;
             
                default: {
@@ -939,7 +953,10 @@ void  __attribute__((noreturn)) AppMain(void)
        LED_B_OFF();
        LED_A_OFF();
 
-       UsbStart();
+  // Init USB device
+  usb_enable();
+  UsbStart();
+//     UsbStart();
 
        // The FPGA gets its clock from us from PCK0 output, so set that up.
        AT91C_BASE_PIOA->PIO_BSR = GPIO_PCK0;
@@ -959,35 +976,23 @@ void  __attribute__((noreturn)) AppMain(void)
        FpgaDownloadAndGo();
 
        StartTickCount();
-       
+       
 #ifdef WITH_LCD
-
        LCDInit();
-
-       // test text on different colored backgrounds
-       LCDString(" The quick brown fox  ",     (char *)&FONT6x8,1,1+8*0,WHITE  ,BLACK );
-       LCDString("  jumped over the     ",     (char *)&FONT6x8,1,1+8*1,BLACK  ,WHITE );
-       LCDString("     lazy dog.        ",     (char *)&FONT6x8,1,1+8*2,YELLOW ,RED   );
-       LCDString(" AaBbCcDdEeFfGgHhIiJj ",     (char *)&FONT6x8,1,1+8*3,RED    ,GREEN );
-       LCDString(" KkLlMmNnOoPpQqRrSsTt ",     (char *)&FONT6x8,1,1+8*4,MAGENTA,BLUE  );
-       LCDString("UuVvWwXxYyZz0123456789",     (char *)&FONT6x8,1,1+8*5,BLUE   ,YELLOW);
-       LCDString("`-=[]_;',./~!@#$%^&*()",     (char *)&FONT6x8,1,1+8*6,BLACK  ,CYAN  );
-       LCDString("     _+{}|:\\\"<>?     ",(char *)&FONT6x8,1,1+8*7,BLUE  ,MAGENTA);
-
-       // color bands
-       LCDFill(0, 1+8* 8, 132, 8, BLACK);
-       LCDFill(0, 1+8* 9, 132, 8, WHITE);
-       LCDFill(0, 1+8*10, 132, 8, RED);
-       LCDFill(0, 1+8*11, 132, 8, GREEN);
-       LCDFill(0, 1+8*12, 132, 8, BLUE);
-       LCDFill(0, 1+8*13, 132, 8, YELLOW);
-       LCDFill(0, 1+8*14, 132, 8, CYAN);
-       LCDFill(0, 1+8*15, 132, 8, MAGENTA);
-
 #endif
 
+  byte_t rx[sizeof(UsbCommand)];
+       size_t rx_len;
+  
        for(;;) {
-               UsbPoll(FALSE);
+    if (usb_poll()) {
+      rx_len = usb_read(rx,sizeof(UsbCommand));
+      if (rx_len) {
+        UsbPacketReceived(rx,rx_len);
+      }
+    }
+//             UsbPoll(FALSE);
+
                WDT_HIT();
 
 #ifdef WITH_LF
index f3f9023782a4485a782a3128e41b85216b3350e4..c2b24eeadd2d6d3f67d5a6b453c5adc9e8db8a43 100644 (file)
@@ -16,6 +16,7 @@
 #include <stddef.h>
 #include "common.h"
 #include "hitag2.h"
+#include "mifare.h"
 
 // The large multi-purpose buffer, typically used to hold A/D samples,
 // maybe processed in some way.
@@ -133,17 +134,17 @@ void RAMFUNC SnoopIso14443(void);
 /// iso14443a.h
 void RAMFUNC SnoopIso14443a(uint8_t param);
 void SimulateIso14443aTag(int tagType, int uid_1st, int uid_2nd);      // ## simulate iso14443a tag
-void ReaderIso14443a(UsbCommand * c, UsbCommand * ack);
+void ReaderIso14443a(UsbCommand * c);
 // Also used in iclass.c
 int RAMFUNC LogTrace(const uint8_t * btBytes, int iLen, int iSamples, uint32_t dwParity, int bReader);
 uint32_t GetParity(const uint8_t * pbtCmd, int iLen);
-void iso14a_set_trigger(int enable);
-void iso14a_clear_trace(void);
-void iso14a_set_tracing(int enable);
+void iso14a_set_trigger(bool enable);
+void iso14a_clear_trace();
+void iso14a_set_tracing(bool enable);
 void RAMFUNC SniffMifare(uint8_t param);
 
 /// epa.h
-void EPA_PACE_Collect_Nonce(UsbCommand * c, UsbCommand * ack);
+void EPA_PACE_Collect_Nonce(UsbCommand * c);
 
 // mifarecmd.h
 void ReaderMifare(uint32_t parameter);
index 0e93a0561cd408f54c13b1cf8688d4a988ca19db..73c3a7555077c997bf14b50cc336fc4375d5a3cb 100644 (file)
@@ -12,8 +12,8 @@
 //-----------------------------------------------------------------------------
 
 #include "iso14443a.h"
-
 #include "epa.h"
+#include "cmd.h"
 
 // Protocol and Parameter Selection Request
 // use regular (1x) speed in both directions
@@ -211,24 +211,25 @@ int EPA_Read_CardAccess(uint8_t *buffer, size_t max_length)
 // Abort helper function for EPA_PACE_Collect_Nonce
 // sets relevant data in ack, sends the response
 //-----------------------------------------------------------------------------
-static void EPA_PACE_Collect_Nonce_Abort(UsbCommand *ack, uint8_t step, int func_return)
+static void EPA_PACE_Collect_Nonce_Abort(uint8_t step, int func_return)
 {
-       // step in which the failure occured
-       ack->arg[0] = step;
-       // last return code
-       ack->arg[1] = func_return;
+//     // step in which the failure occured
+//     ack->arg[0] = step;
+//     // last return code
+//     ack->arg[1] = func_return;
 
        // power down the field
        EPA_Finish();
        
        // send the USB packet
-       UsbSendPacket((void *)ack, sizeof(UsbCommand));
+  cmd_send(CMD_ACK,step,func_return,0,0,0);
+//UsbSendPacket((void *)ack, sizeof(UsbCommand));
 }
 
 //-----------------------------------------------------------------------------
 // Acquire one encrypted PACE nonce
 //-----------------------------------------------------------------------------
-void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
+void EPA_PACE_Collect_Nonce(UsbCommand *c)
 {
        /*
         * ack layout:
@@ -244,14 +245,14 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
        // return value of a function
        int func_return;
 
-       // initialize ack with 0s
-       memset(ack->arg, 0, 12);
-       memset(ack->d.asBytes, 0, 48);
+//     // initialize ack with 0s
+//     memset(ack->arg, 0, 12);
+//     memset(ack->d.asBytes, 0, 48);
        
        // set up communication
        func_return = EPA_Setup();
        if (func_return != 0) {
-               EPA_PACE_Collect_Nonce_Abort(ack, 1, func_return);
+               EPA_PACE_Collect_Nonce_Abort(1, func_return);
                return;
        }
 
@@ -264,7 +265,7 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
        int card_access_length = EPA_Read_CardAccess(card_access, 256);
        // the response has to be at least this big to hold the OID
        if (card_access_length < 18) {
-               EPA_PACE_Collect_Nonce_Abort(ack, 2, card_access_length);
+               EPA_PACE_Collect_Nonce_Abort(2, card_access_length);
                return;
        }
 
@@ -275,7 +276,7 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
                                           card_access_length,
                                           &pace_version_info);
        if (func_return != 0 || pace_version_info.version == 0) {
-               EPA_PACE_Collect_Nonce_Abort(ack, 3, func_return);
+               EPA_PACE_Collect_Nonce_Abort(3, func_return);
                return;
        }
        
@@ -290,17 +291,18 @@ void EPA_PACE_Collect_Nonce(UsbCommand *c, UsbCommand *ack)
        // check if the command succeeded
        if (func_return < 0)
        {
-               EPA_PACE_Collect_Nonce_Abort(ack, 4, func_return);
+               EPA_PACE_Collect_Nonce_Abort(4, func_return);
                return;
        }
+  
+  // all done, return
+       EPA_Finish();
        
        // save received information
-       ack->arg[1] = func_return;
-       memcpy(ack->d.asBytes, nonce, func_return);
-
-       // all done, return
-       EPA_Finish();
-       UsbSendPacket((void *)ack, sizeof(UsbCommand));
+//     ack->arg[1] = func_return;
+//     memcpy(ack->d.asBytes, nonce, func_return);
+//     UsbSendPacket((void *)ack, sizeof(UsbCommand));
+  cmd_send(CMD_ACK,0,func_return,0,nonce,func_return);
 }
 
 //-----------------------------------------------------------------------------
index a719f5edf2fa5f9906f54a067ac57fcf513cbd1f..e3784bf5b4bade1a8b1d68e34868ada4c6c9660a 100644 (file)
@@ -116,13 +116,11 @@ void FpgaSetupSsc(void)
 
        // 8 bits per transfer, no loopback, MSB first, 1 transfer per sync
        // pulse, no output sync, start on positive-going edge of sync
-       AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(8) |
-               AT91C_SSC_MSBF | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
+       AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(8) |     AT91C_SSC_MSBF | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
 
        // clock comes from TK pin, no clock output, outputs change on falling
        // edge of TK, start on rising edge of TF
-       AT91C_BASE_SSC->SSC_TCMR = SSC_CLOCK_MODE_SELECT(2) |
-               SSC_CLOCK_MODE_START(5);
+       AT91C_BASE_SSC->SSC_TCMR = SSC_CLOCK_MODE_SELECT(2) |   SSC_CLOCK_MODE_START(5);
 
        // tx framing is the same as the rx framing
        AT91C_BASE_SSC->SSC_TFMR = AT91C_BASE_SSC->SSC_RFMR;
index 3757043b30823fdd05805d6948d48a5875f7c2e8..d2ebb0c6c61a47ec1a7be957f0cc3fe196d9b331 100644 (file)
@@ -14,6 +14,7 @@
 #include "apps.h"
 #include "util.h"
 #include "string.h"
+#include "cmd.h"
 
 #include "iso14443crc.h"
 #include "iso14443a.h"
@@ -64,16 +65,16 @@ const uint8_t OddByteParity[256] = {
 };
 
 
-void iso14a_set_trigger(int enable) {
+void iso14a_set_trigger(bool enable) {
        trigger = enable;
 }
 
-void iso14a_clear_trace(void) {
-    memset(trace, 0x44, TRACE_SIZE);
+void iso14a_clear_trace() {
+  memset(trace, 0x44, TRACE_SIZE);
        traceLen = 0;
 }
 
-void iso14a_set_tracing(int enable) {
+void iso14a_set_tracing(bool enable) {
        tracing = enable;
 }
 
@@ -1641,7 +1642,7 @@ int iso14443a_select_card(uint8_t * uid_ptr, iso14a_card_select_t * resp_data, u
        uint8_t sel_uid[]    = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
        uint8_t rats[]       = { 0xE0,0x80,0x00,0x00 }; // FSD=256, FSDI=8, CID=0
 
-       uint8_t* resp = (((uint8_t *)BigBuf) + 3560);   // was 3560 - tied to other size changes
+       uint8_t* resp = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET);     // was 3560 - tied to other size changes
 
        uint8_t sak = 0x04; // cascade uid
        int cascade_level = 0;
@@ -1649,13 +1650,14 @@ int iso14443a_select_card(uint8_t * uid_ptr, iso14a_card_select_t * resp_data, u
        int len;
        
        // clear uid
-       memset(uid_ptr, 0, 8);
+       memset(uid_ptr, 0, 12);
 
        // Broadcast for a card, WUPA (0x52) will force response from all cards in the field
        ReaderTransmitShort(wupa);
        // Receive the ATQA
        if(!ReaderReceive(resp)) return 0;
-
+//  Dbprintf("atqa: %02x %02x",resp[0],resp[1]);
+  
        if(resp_data)
                memcpy(resp_data->atqa, resp, 2);
        
@@ -1670,6 +1672,8 @@ int iso14443a_select_card(uint8_t * uid_ptr, iso14a_card_select_t * resp_data, u
                // SELECT_ALL
                ReaderTransmit(sel_all,sizeof(sel_all));
                if (!ReaderReceive(resp)) return 0;
+//    Dbprintf("uid: %02x %02x %02x %02x",resp[0],resp[1],resp[2],resp[3]);
+
                if(uid_ptr) memcpy(uid_ptr + cascade_level*4, resp, 4);
                
                // calculate crypto UID
@@ -1715,13 +1719,13 @@ int iso14443a_select_card(uint8_t * uid_ptr, iso14a_card_select_t * resp_data, u
 }
 
 void iso14443a_setup() {
-       // Setup SSC
-       FpgaSetupSsc();
+  // Set up the synchronous serial port
+  FpgaSetupSsc();
        // Start from off (no field generated)
        // Signal field is off with the appropriate LED
        LED_D_OFF();
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
-       SpinDelay(200);
+       SpinDelay(50);
 
        SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
 
@@ -1729,7 +1733,7 @@ void iso14443a_setup() {
        // Signal field is on with the appropriate LED
        LED_D_ON();
        FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
-       SpinDelay(200);
+       SpinDelay(50);
 
        iso14a_timeout = 2048; //default
 }
@@ -1765,18 +1769,24 @@ int iso14_apdu(uint8_t * cmd, size_t cmd_len, void * data) {
 // Read an ISO 14443a tag. Send out commands and store answers.
 //
 //-----------------------------------------------------------------------------
-void ReaderIso14443a(UsbCommand * c, UsbCommand * ack)
+void ReaderIso14443a(UsbCommand * c)
 {
        iso14a_command_t param = c->arg[0];
        uint8_t * cmd = c->d.asBytes;
        size_t len = c->arg[1];
+  uint32_t arg0;
+  byte_t buf[48];
+  
+  iso14a_clear_trace();
+  iso14a_set_tracing(true);
 
        if(param & ISO14A_REQUEST_TRIGGER) iso14a_set_trigger(1);
 
        if(param & ISO14A_CONNECT) {
                iso14443a_setup();
-               ack->arg[0] = iso14443a_select_card(ack->d.asBytes, (iso14a_card_select_t *) (ack->d.asBytes+12), NULL);
-               UsbSendPacket((void *)ack, sizeof(UsbCommand));
+               arg0 = iso14443a_select_card(buf, (iso14a_card_select_t *)(buf+12), NULL);
+               cmd_send(CMD_ACK,arg0,0,0,buf,48);
+//    UsbSendPacket((void *)ack, sizeof(UsbCommand));
        }
 
        if(param & ISO14A_SET_TIMEOUT) {
@@ -1788,8 +1798,9 @@ void ReaderIso14443a(UsbCommand * c, UsbCommand * ack)
        }
 
        if(param & ISO14A_APDU) {
-               ack->arg[0] = iso14_apdu(cmd, len, ack->d.asBytes);
-               UsbSendPacket((void *)ack, sizeof(UsbCommand));
+               arg0 = iso14_apdu(cmd, len, buf);
+               cmd_send(CMD_ACK,arg0,0,0,buf,48);
+//             UsbSendPacket((void *)ack, sizeof(UsbCommand));
        }
 
        if(param & ISO14A_RAW) {
@@ -1798,8 +1809,9 @@ void ReaderIso14443a(UsbCommand * c, UsbCommand * ack)
                        len += 2;
                }
                ReaderTransmit(cmd,len);
-               ack->arg[0] = ReaderReceive(ack->d.asBytes);
-               UsbSendPacket((void *)ack, sizeof(UsbCommand));
+               arg0 = ReaderReceive(buf);
+//             UsbSendPacket((void *)ack, sizeof(UsbCommand));
+    cmd_send(CMD_ACK,arg0,0,0,buf,48);
        }
 
        if(param & ISO14A_REQUEST_TRIGGER) iso14a_set_trigger(0);
@@ -1821,7 +1833,7 @@ void ReaderMifare(uint32_t parameter)
        uint8_t mf_auth[]    = { 0x60,0x00,0xf5,0x7b };
        uint8_t mf_nr_ar[]   = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
 
-       uint8_t* receivedAnswer = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes
+       uint8_t* receivedAnswer = (((uint8_t *)BigBuf) + FREE_BUFFER_OFFSET);   // was 3560 - tied to other size changes
        traceLen = 0;
        tracing = false;
 
@@ -1918,14 +1930,16 @@ void ReaderMifare(uint32_t parameter)
        LogTrace(par_list, 8, 0, GetParity(par_list, 8), TRUE);
        LogTrace(ks_list, 8, 0, GetParity(ks_list, 8), TRUE);
 
-       UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
-       memcpy(ack.d.asBytes + 0,  uid, 4);
-       memcpy(ack.d.asBytes + 4,  nt, 4);
-       memcpy(ack.d.asBytes + 8,  par_list, 8);
-       memcpy(ack.d.asBytes + 16, ks_list, 8);
+  byte_t buf[48];
+//     UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
+       memcpy(buf + 0,  uid, 4);
+       memcpy(buf + 4,  nt, 4);
+       memcpy(buf + 8,  par_list, 8);
+       memcpy(buf + 16, ks_list, 8);
                
        LED_B_ON();
-       UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
+  cmd_send(CMD_ACK,isOK,0,0,buf,48);
+//     UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
        LED_B_OFF();    
 
        // Thats it...
index b8f56c153e96589f5438a8bd9110220ec21d5051..8422f6cb1c84e3521261550bc023634c3308a48e 100644 (file)
@@ -91,10 +91,10 @@ extern int ReaderReceivePar(uint8_t* receivedAnswer, uint32_t * parptr);
 extern void iso14443a_setup();
 extern int iso14_apdu(uint8_t * cmd, size_t cmd_len, void * data);
 extern int iso14443a_select_card(uint8_t * uid_ptr, iso14a_card_select_t * resp_data, uint32_t * cuid_ptr);
-extern void iso14a_set_trigger(int enable);
+extern void iso14a_set_trigger(bool enable);
 extern void iso14a_set_timeout(uint32_t timeout);
 
-extern void iso14a_clear_tracelen(void);
-extern void iso14a_set_tracing(int enable);
+extern void iso14a_clear_tracelen();
+extern void iso14a_set_tracing(bool enable);
 
 #endif /* __ISO14443A_H */
index b73ee0147ffc83cd8c3cefa68edd5169584553a7..ace6f87c997a3564ea1e42f551db21ab70a71d68 100644 (file)
@@ -63,7 +63,7 @@
 #include "apps.h"
 #include "string.h"
 #include "iso15693tools.h"
-
+#include "cmd.h"
 
 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
 
@@ -1260,7 +1260,7 @@ void DirectTag15693Command(uint32_t datalen,uint32_t speed, uint32_t recv, uint8
 
        int recvlen=0;
        uint8_t *recvbuf=(uint8_t *)BigBuf;
-       UsbCommand n;
+//     UsbCommand n;
        
        if (DEBUG) {
                Dbprintf("SEND");
@@ -1270,11 +1270,12 @@ void DirectTag15693Command(uint32_t datalen,uint32_t speed, uint32_t recv, uint8
        recvlen=SendDataTag(data,datalen,1,speed,(recv?&recvbuf:NULL));
 
        if (recv) { 
-               n.cmd=/* CMD_ISO_15693_COMMAND_DONE */ CMD_ACK;
-               n.arg[0]=recvlen>48?48:recvlen;
-               memcpy(n.d.asBytes, recvbuf, 48);
+//             n.cmd=/* CMD_ISO_15693_COMMAND_DONE */ CMD_ACK;
+//             n.arg[0]=recvlen>48?48:recvlen;
+//             memcpy(n.d.asBytes, recvbuf, 48);
                LED_B_ON();
-               UsbSendPacket((uint8_t *)&n, sizeof(n));
+    cmd_send(CMD_ACK,recvlen>48?48:recvlen,0,0,recvbuf,48);
+//             UsbSendPacket((uint8_t *)&n, sizeof(n));
                LED_B_OFF();    
                
                if (DEBUG) {
index fc5156fdad27e4cdb4045868e08ca30cb6b25aa8..45878000399eb291e91b2d23ed935924e2e6751b 100644 (file)
@@ -166,7 +166,7 @@ int intMfSniffSend() {
        FpgaDisableSscDma();\r
 \r
        while (pckLen > 0) {\r
-               pckSize = min(32, pckLen);\r
+               pckSize = MIN(32, pckLen);\r
                UsbCommand ack = {CMD_ACK, {1, pckSize, pckNum}};\r
                memcpy(ack.d.asBytes, trace + traceLen - pckLen, pckSize);\r
        \r
index 21a9b7577f2d1f79c0ef3a8d41462bac6e0994a4..b68c511c7c5007a52c8f721bb3c26e2eb355285a 100644 (file)
@@ -17,7 +17,8 @@
 #define RAMFUNC __attribute((long_call, section(".ramfunc")))
 
 #define BYTEx(x, n) (((x) >> (n * 8)) & 0xff )
-#define min(a, b) (((a) > (b)) ? (b) : (a))
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
 
 #define LED_RED 1
 #define LED_ORANGE 2
index 6b0ff532800be0129b364d9413c22f3fdf84001b..0359832cc5f52fdb0062daeab0dcd777980f643f 100644 (file)
@@ -300,6 +300,6 @@ void BootROM(void)
            flash_mode(1);
     } else {
            // jump to Flash address of the osimage entry point (LSBit set for thumb mode)
-           asm("bx %0\n" : : "r" ( ((int)&_osimage_entry) | 0x1 ) );
+           __asm("bx %0\n" : : "r" ( ((int)&_osimage_entry) | 0x1 ) );
     }
 }
index 8ddde4bdb2169dd55031f8a39bf1e744269e5d6a..ef1155ec4b3161fae8f449019a3d90ebde10150d 100644 (file)
@@ -67,12 +67,13 @@ CMDSRCS = \
                        cmdlfhitag.c \
                        cmdlfti.c \
                        cmdparser.c \
-                       cmdmain.c
+                       cmdmain.c \
+                       uart.c
 
 CMDOBJS = $(CMDSRCS:%.c=$(OBJDIR)/%.o)
 
 RM = rm -f
-BINS = proxmark3 snooper cli flasher
+BINS = proxmark3 flasher #snooper cli
 CLEAN = cli cli.exe flasher flasher.exe proxmark3 proxmark3.exe snooper snooper.exe $(CMDOBJS) $(OBJDIR)/*.o *.o *.moc.cpp
 
 all: $(BINS)
index 77640d9d8a15e0ce754e39d59efa4a23e6a56f76..1c58c69b956c5ac9468464bb754572607827653b 100644 (file)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <limits.h>
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "data.h"
 #include "ui.h"
 #include "graph.h"
@@ -158,7 +159,7 @@ int CmdBitsamples(const char *Cmd)
   for (int i = 0; i < n; i += 12) {
     UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
     SendCommand(&c);
-    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
+    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, NULL);
 
     for (int j = 0; j < 48; j++) {
       for (int k = 0; k < 8; k++) {
@@ -418,7 +419,7 @@ int CmdHexsamples(const char *Cmd)
   for (int i = offset; i < n+offset; i += 12) {
     UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
     SendCommand(&c);
-    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
+    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, NULL);
     for (int j = 0; j < 48; j += 8) {
       PrintAndLog("%02x %02x %02x %02x %02x %02x %02x %02x",
         sample_buf[j+0],
@@ -475,7 +476,7 @@ int CmdSamples(const char *Cmd)
   for (int i = 0; i < n; i += 12) {
     UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
     SendCommand(&c);
-    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
+    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, NULL);
     for (int j = 0; j < 48; j++) {
       GraphBuffer[cnt++] = ((int)sample_buf[j]) - 128;
     }
index 0ed3de1a8eea43a4ba11a38ad02a7b56ef1b4691..cfbd9e168b837d241131a4cf5b96de4d438903ff 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <stdio.h>
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "graph.h"
 #include "ui.h"
 #include "cmdparser.h"
index be7afc77f26ca7bfbf5366746d3775f217657b84..a82c7e3528888be91c5f60ed2cdaeda70063156c 100644 (file)
 #include "iso14443crc.h"
 #include "data.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
 #include "cmdhf14a.h"
 #include "common.h"
 #include "cmdmain.h"
+#include "mifare.h"
 
 static int CmdHelp(const char *Cmd);
 
@@ -29,6 +31,7 @@ int CmdHF14AList(const char *Cmd)
 {
   uint8_t got[1920];
   GetFromBigBuf(got,sizeof(got),0);
+  WaitForResponse(CMD_ACK,NULL);
 
   PrintAndLog("recorded activity:");
   PrintAndLog(" ETU     :rssi: who bytes");
@@ -161,18 +164,21 @@ int CmdHF14AReader(const char *Cmd)
 {
        UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
        SendCommand(&c);
-       UsbCommand * resp = WaitForResponse(CMD_ACK);
-       uint8_t              * uid  = resp->d.asBytes;
+
+       UsbCommand resp;
+  WaitForResponse(CMD_ACK,&resp);
+       
+  uint8_t              * uid  = resp.d.asBytes;
        iso14a_card_select_t * card = (iso14a_card_select_t *)(uid + 12);
 
-       if(resp->arg[0] == 0) {
+       if(resp.arg[0] == 0) {
                PrintAndLog("iso14443a card select failed");
                return 0;
        }
 
        PrintAndLog("ATQA : %02x %02x", card->atqa[0], card->atqa[1]);
        PrintAndLog(" UID : %s", sprint_hex(uid, 12));
-       PrintAndLog(" SAK : %02x [%d]", card->sak, resp->arg[0]);
+       PrintAndLog(" SAK : %02x [%d]", card->sak, resp.arg[0]);
 
        switch (card->sak) {
                case 0x00: PrintAndLog(" SAK : NXP MIFARE Ultralight | Ultralight C"); break;
@@ -191,7 +197,7 @@ int CmdHF14AReader(const char *Cmd)
                case 0x98: PrintAndLog(" SAK : Gemplus MPCOS"); break;
                default: ;
        }
-       if(resp->arg[0] == 1) {
+       if(resp.arg[0] == 1) {
                bool ta1 = 0, tb1 = 0, tc1 = 0;
                int pos;
 
@@ -312,7 +318,7 @@ int CmdHF14AReader(const char *Cmd)
        else
                PrintAndLog("proprietary non-iso14443a card found, RATS not supported");
 
-       return resp->arg[0];
+       return resp.arg[0];
 }
 
 // Collect ISO14443 Type A UIDs
@@ -330,12 +336,15 @@ int CmdHF14ACUIDs(const char *Cmd)
                // execute anticollision procedure
                UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
                SendCommand(&c);
-               UsbCommand *resp = WaitForResponse(CMD_ACK);
-               uint8_t *uid  = resp->d.asBytes;
+    
+    UsbCommand resp;
+    WaitForResponse(CMD_ACK,&resp);
+
+               uint8_t *uid  = resp.d.asBytes;
                iso14a_card_select_t *card = (iso14a_card_select_t *)(uid + 12);
 
                // check if command failed
-               if (resp->arg[0] == 0) {
+               if (resp.arg[0] == 0) {
                        PrintAndLog("Card select failed.");
                } else {
                        // check if UID is 4 bytes
@@ -472,10 +481,9 @@ static command_t CommandTable[] =
   {NULL, NULL, 0, NULL}
 };
 
-int CmdHF14A(const char *Cmd)
-{
+int CmdHF14A(const char *Cmd) {
        // flush
-       while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;
+       WaitForResponseTimeout(CMD_ACK,NULL,100);
 
        // parse
   CmdsParse(CommandTable, Cmd);
index 68c7fa68616043d4b68c91df753f409e636ffbea..9b2fa753903fadad3eb999a196e1b655b8a7c1b3 100644 (file)
@@ -15,6 +15,7 @@
 #include <stdint.h>
 #include "iso14443crc.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "data.h"
 #include "graph.h"
 #include "ui.h"
index a13ac4d031a779b249ab4d6ee47cda675abb48f6..e1e5e02a79e988ea397ffe3f10ba735e0360879a 100644 (file)
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <stdint.h>
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "data.h"
 #include "graph.h"
 #include "ui.h"
@@ -94,7 +95,7 @@ const productName uidmapping[] = {
 // returns 1 if suceeded
 int getUID(uint8_t *buf) 
 {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t *recv;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
        uint8_t *req=c.d.asBytes;
@@ -111,11 +112,9 @@ int getUID(uint8_t *buf)
        
                SendCommand(&c);
                
-               r=WaitForResponseTimeout(CMD_ACK,1000);
-               
-               if (r!=NULL) {
-                       recv = r->d.asBytes;
-                       if (r->arg[0]>=12 && ISO15_CRC_CHECK==Crc(recv,12)) {
+               if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
+                       recv = resp.d.asBytes;
+                       if (resp.arg[0]>=12 && ISO15_CRC_CHECK==Crc(recv,12)) {
                           memcpy(buf,&recv[2],8);
                           return 1;
                        } 
@@ -292,7 +291,7 @@ int CmdHF15Afi(const char *Cmd)
 
 // Reads all memory pages
 int CmdHF15DumpMem(const char*Cmd) {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t uid[8]; 
        uint8_t *recv=NULL;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
@@ -321,20 +320,18 @@ int CmdHF15DumpMem(const char*Cmd) {
        
                SendCommand(&c);
                
-               r=WaitForResponseTimeout(CMD_ACK,1000);
-               
-               if (r!=NULL) {
-                       recv = r->d.asBytes;
-                       if (ISO15_CRC_CHECK==Crc(recv,r->arg[0])) {
+               if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
+                       recv = resp.d.asBytes;
+                       if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
                                if (!(recv[0] & ISO15_RES_ERROR)) {
                                        retry=0;
                                        *output=0; // reset outputstring
                                        sprintf(output, "Block %2i   ",blocknum);
-                                       for ( int i=1; i<r->arg[0]-2; i++) { // data in hex
+                                       for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
                                                sprintf(output+strlen(output),"%02hX ",recv[i]);                                        
                                        }                                       
                                        strcat(output,"   "); 
-                                       for ( int i=1; i<r->arg[0]-2; i++) { // data in cleaned ascii
+                                       for ( int i=1; i<resp.arg[0]-2; i++) { // data in cleaned ascii
                                                sprintf(output+strlen(output),"%c",(recv[i]>31 && recv[i]<127)?recv[i]:'.');                                    
                                        }                                       
                                        PrintAndLog("%s",output);       
@@ -346,14 +343,14 @@ int CmdHF15DumpMem(const char*Cmd) {
                                }
                        } // else PrintAndLog("crc");
                } // else PrintAndLog("r null");
-               
        } // retry
-       if (r && r->arg[0]<3) 
-               PrintAndLog("Lost Connection");
-       else if (r && ISO15_CRC_CHECK!=Crc(r->d.asBytes,r->arg[0]))
-               PrintAndLog("CRC Failed");
-       else 
-               PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); 
+  // TODO: need fix
+//     if (resp.arg[0]<3)
+//             PrintAndLog("Lost Connection");
+//     else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
+//             PrintAndLog("CRC Failed");
+//     else 
+//             PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); 
        return 0;
 }
 
@@ -392,7 +389,7 @@ int CmdHF15Help(const char *Cmd)
 
 int CmdHF15CmdInquiry(const char *Cmd) 
 {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t *recv;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
        uint8_t *req=c.d.asBytes;
@@ -407,15 +404,13 @@ int CmdHF15CmdInquiry(const char *Cmd)
 
        SendCommand(&c);
        
-       r=WaitForResponseTimeout(CMD_ACK,1000);
-       
-       if (r!=NULL) {
-               if (r->arg[0]>=12) {
-                  recv = r->d.asBytes;
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
+               if (resp.arg[0]>=12) {
+                  recv = resp.d.asBytes;
                   PrintAndLog("UID=%s",sprintUID(NULL,&recv[2]));
                   PrintAndLog("Tag Info: %s",getTagInfo(&recv[2]));    
                } else {
-                       PrintAndLog("Response to short, just %i bytes. No tag?\n",r->arg[0]);           
+                       PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
                }
        } else {
                PrintAndLog("timeout.");
@@ -440,7 +435,7 @@ int CmdHF15CmdDebug( const char *cmd) {
 
 
 int CmdHF15CmdRaw (const char *cmd) {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t *recv;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
        int reply=1;
@@ -515,14 +510,12 @@ int CmdHF15CmdRaw (const char *cmd) {
        SendCommand(&c);
        
        if (reply) {
-               r=WaitForResponseTimeout(CMD_ACK,1000);
-       
-               if (r!=NULL) {
-                       recv = r->d.asBytes;
-                       PrintAndLog("received %i octets",r->arg[0]);
-                       hexout = (char *)malloc(r->arg[0] * 3 + 1);
+               if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
+                       recv = resp.d.asBytes;
+                       PrintAndLog("received %i octets",resp.arg[0]);
+                       hexout = (char *)malloc(resp.arg[0] * 3 + 1);
                        if (hexout != NULL) {
-                               for (int i = 0; i < r->arg[0]; i++) { // data in hex
+                               for (int i = 0; i < resp.arg[0]; i++) { // data in hex
                                        sprintf(&hexout[i * 3], "%02hX ", recv[i]);
                                }
                                PrintAndLog("%s", hexout);
@@ -635,7 +628,7 @@ int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdle
  * get system information from tag/VICC
  */
 int CmdHF15CmdSysinfo(const char *Cmd) {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t *recv;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
        uint8_t *req=c.d.asBytes;
@@ -670,14 +663,12 @@ int CmdHF15CmdSysinfo(const char *Cmd) {
 
        SendCommand(&c);
 
-       r=WaitForResponseTimeout(CMD_ACK,1000);
-       
-       if (r!=NULL && r->arg[0]>2) {
-               recv = r->d.asBytes;
-               if (ISO15_CRC_CHECK==Crc(recv,r->arg[0])) {
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
+               recv = resp.d.asBytes;
+               if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
                        if (!(recv[0] & ISO15_RES_ERROR)) {
                                *output=0; // reset outputstring
-                               for ( i=1; i<r->arg[0]-2; i++) {
+                               for ( i=1; i<resp.arg[0]-2; i++) {
                                        sprintf(output+strlen(output),"%02hX ",recv[i]);                                        
                                }                                       
                                strcat(output,"\n\r");
@@ -725,7 +716,7 @@ int CmdHF15CmdSysinfo(const char *Cmd) {
  * Read multiple blocks at once (not all tags support this)
  */
 int CmdHF15CmdReadmulti(const char *Cmd) {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t *recv;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
        uint8_t *req=c.d.asBytes;
@@ -773,18 +764,16 @@ int CmdHF15CmdReadmulti(const char *Cmd) {
 
        SendCommand(&c);
 
-       r=WaitForResponseTimeout(CMD_ACK,1000);
-       
-       if (r!=NULL && r->arg[0]>2) {
-               recv = r->d.asBytes;
-               if (ISO15_CRC_CHECK==Crc(recv,r->arg[0])) {
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
+               recv = resp.d.asBytes;
+               if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
                        if (!(recv[0] & ISO15_RES_ERROR)) {
                                *output=0; // reset outputstring
-                               for ( int i=1; i<r->arg[0]-2; i++) {
+                               for ( int i=1; i<resp.arg[0]-2; i++) {
                                        sprintf(output+strlen(output),"%02hX ",recv[i]);                                        
                                }                                       
                                strcat(output,"   ");
-                               for ( int i=1; i<r->arg[0]-2; i++) {
+                               for ( int i=1; i<resp.arg[0]-2; i++) {
                                        sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');                                      
                                }                                       
                                PrintAndLog("%s",output);       
@@ -806,7 +795,7 @@ int CmdHF15CmdReadmulti(const char *Cmd) {
  * Reads a single Block
  */
 int CmdHF15CmdRead(const char *Cmd) {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t *recv;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
        uint8_t *req=c.d.asBytes;
@@ -848,19 +837,17 @@ int CmdHF15CmdRead(const char *Cmd) {
 
        SendCommand(&c);
 
-       r=WaitForResponseTimeout(CMD_ACK,1000);
-       
-       if (r!=NULL && r->arg[0]>2) {
-               recv = r->d.asBytes;
-               if (ISO15_CRC_CHECK==Crc(recv,r->arg[0])) {
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) {
+               recv = resp.d.asBytes;
+               if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
                        if (!(recv[0] & ISO15_RES_ERROR)) {
                                *output=0; // reset outputstring
                                //sprintf(output, "Block %2i   ",blocknum);
-                               for ( int i=1; i<r->arg[0]-2; i++) {
+                               for ( int i=1; i<resp.arg[0]-2; i++) {
                                        sprintf(output+strlen(output),"%02hX ",recv[i]);                                        
                                }                                       
                                strcat(output,"   ");
-                               for ( int i=1; i<r->arg[0]-2; i++) {
+                               for ( int i=1; i<resp.arg[0]-2; i++) {
                                        sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');                                      
                                }                                       
                                PrintAndLog("%s",output);       
@@ -883,7 +870,7 @@ int CmdHF15CmdRead(const char *Cmd) {
  * Writes a single Block - might run into timeout, even when successful
  */
 int CmdHF15CmdWrite(const char *Cmd) {
-       UsbCommand *r;  
+       UsbCommand resp;
        uint8_t *recv;
        UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
        uint8_t *req=c.d.asBytes;
@@ -943,11 +930,9 @@ int CmdHF15CmdWrite(const char *Cmd) {
 
        SendCommand(&c);
 
-       r=WaitForResponseTimeout(CMD_ACK,2000);
-       
-       if (r!=NULL && r->arg[0]>2) {
-               recv = r->d.asBytes;
-               if (ISO15_CRC_CHECK==Crc(recv,r->arg[0])) {
+       if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) {
+               recv = resp.d.asBytes;
+               if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
                        if (!(recv[0] & ISO15_RES_ERROR)) {                                     
                                PrintAndLog("OK");      
                        } else {
index 2787b9695745bf94b491186390eb65d04b30077b..62680eff529c6a71f451e2e09dca6eaef410acdd 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "util.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
 #include "common.h"
@@ -42,22 +43,21 @@ int CmdHFEPACollectPACENonces(const char *Cmd)
                // execute PACE
                UsbCommand c = {CMD_EPA_PACE_COLLECT_NONCE, {(int)m, 0, 0}};
                SendCommand(&c);
-               UsbCommand *resp = WaitForResponse(CMD_ACK);
+               UsbCommand resp;
+    
+    WaitForResponse(CMD_ACK,&resp);
 
                // check if command failed
-               if (resp->arg[0] != 0) {
-                       PrintAndLog("Error in step %d, Return code: %d",
-                                   resp->arg[0],
-                                               (int)resp->arg[1]);
+               if (resp.arg[0] != 0) {
+                       PrintAndLog("Error in step %d, Return code: %d",resp.arg[0],(int)resp.arg[1]);
                } else {
-                       size_t nonce_length = resp->arg[1];
+                       size_t nonce_length = resp.arg[1];
                        char *nonce = (char *) malloc(2 * nonce_length + 1);
                        for(int j = 0; j < nonce_length; j++) {
-                               snprintf(nonce + (2 * j), 3, "%02X", resp->d.asBytes[j]);
+                               snprintf(nonce + (2 * j), 3, "%02X", resp.d.asBytes[j]);
                        }
                        // print nonce
-                       PrintAndLog("Length: %d, Nonce: %s",
-                                   resp->arg[1], nonce);
+                       PrintAndLog("Length: %d, Nonce: %s",resp.arg[1], nonce);
                }
                if (i < n - 1) {
                        sleep(d);
@@ -87,7 +87,7 @@ int CmdHelp(const char *Cmd)
 int CmdHFEPA(const char *Cmd)
 {
        // flush
-       while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;
+       while (!WaitForResponseTimeout(CMD_ACK,NULL,500));
 
        // parse
   CmdsParse(CommandTable, Cmd);
index 7c31dab39974c3577fa085af87b524e202e6b729..944ec498e6103c0b4f25a544343cbf0e4cbdb628 100644 (file)
@@ -15,6 +15,7 @@
 #include "iso14443crc.h" // Can also be used for iClass, using 0xE012 as CRC-type
 #include "data.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
 #include "cmdhficlass.h"
index aad035615173aef5835fb0a7d6bb9498e0111ccf..7a268e9235059d7b5c4bf57a43c8577dfc422ddd 100644 (file)
@@ -11,6 +11,7 @@
 #include <stdio.h>
 #include <string.h>
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "data.h"
 #include "ui.h"
 #include "cmdparser.h"
@@ -69,7 +70,7 @@ int CmdLegicDecode(const char *Cmd)
   for (i = 0; i < 256; i += 12, h += 48) {
     UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
     SendCommand(&c);
-    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
+    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, NULL);
     
     for (j = 0; j < 48; j += 8) {
       for (k = 0; k < 8; k++) {
@@ -253,7 +254,7 @@ int CmdLegicLoad(const char *Cmd)
             c.d.asBytes[j] = data[j];
         }
         SendCommand(&c);
-        WaitForResponse(CMD_ACK);
+        WaitForResponse(CMD_ACK, NULL);
         offset += 8;
     }
     fclose(f);
@@ -292,7 +293,7 @@ int CmdLegicSave(const char *Cmd)
   for (int i = offset; i < n+offset; i += 12) {
     UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
     SendCommand(&c);
-    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
+    WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, NULL);
     for (int j = 0; j < 48; j += 8) {
       fprintf(f, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
         sample_buf[j+0],
@@ -357,7 +358,7 @@ int CmdLegicRfFill(const char *Cmd)
     for(i = 0; i < 22; i++) {
       c.arg[0] = i*48;
       SendCommand(&c);
-      WaitForResponse(CMD_ACK);
+      WaitForResponse(CMD_ACK,NULL);
     }
     SendCommand(&cmd);
     return 0;
index dff5db37e08e526687e816480e66880082ae2ca0..5927616bf0d14367e1ba07d6786b52bda8bf6c25 100644 (file)
@@ -9,7 +9,6 @@
 //-----------------------------------------------------------------------------\r
 \r
 #include "cmdhfmf.h"\r
-#include "proxmark3.h"\r
 \r
 static int CmdHelp(const char *Cmd);\r
 \r
@@ -50,14 +49,14 @@ start:
                        break;\r
                }\r
                \r
-               UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000);\r
-               if (resp != NULL) {\r
-                       isOK  = resp->arg[0] & 0xff;\r
+               UsbCommand resp;\r
+               if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
+                       isOK  = resp.arg[0] & 0xff;\r
        \r
-                       uid = (uint32_t)bytes_to_num(resp->d.asBytes +  0, 4);\r
-                       nt =  (uint32_t)bytes_to_num(resp->d.asBytes +  4, 4);\r
-                       par_list = bytes_to_num(resp->d.asBytes +  8, 8);\r
-                       ks_list = bytes_to_num(resp->d.asBytes +  16, 8);\r
+                       uid = (uint32_t)bytes_to_num(resp.d.asBytes +  0, 4);\r
+                       nt =  (uint32_t)bytes_to_num(resp.d.asBytes +  4, 4);\r
+                       par_list = bytes_to_num(resp.d.asBytes +  8, 8);\r
+                       ks_list = bytes_to_num(resp.d.asBytes +  16, 8);\r
        \r
                        printf("\n\n");\r
                        PrintAndLog("isOk:%02x", isOK);\r
@@ -131,11 +130,10 @@ int CmdHF14AMfWrBl(const char *Cmd)
        memcpy(c.d.asBytes, key, 6);\r
        memcpy(c.d.asBytes + 10, bldata, 16);\r
   SendCommand(&c);\r
-       UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
-\r
-       if (resp != NULL) {\r
-               uint8_t                isOK  = resp->arg[0] & 0xff;\r
 \r
+       UsbCommand resp;\r
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+               uint8_t isOK  = resp.arg[0] & 0xff;\r
                PrintAndLog("isOk:%02x", isOK);\r
        } else {\r
                PrintAndLog("Command execute timeout");\r
@@ -175,11 +173,11 @@ int CmdHF14AMfRdBl(const char *Cmd)
   UsbCommand c = {CMD_MIFARE_READBL, {blockNo, keyType, 0}};\r
        memcpy(c.d.asBytes, key, 6);\r
   SendCommand(&c);\r
-       UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
 \r
-       if (resp != NULL) {\r
-               uint8_t                isOK  = resp->arg[0] & 0xff;\r
-               uint8_t              * data  = resp->d.asBytes;\r
+       UsbCommand resp;\r
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+               uint8_t                isOK  = resp.arg[0] & 0xff;\r
+               uint8_t              * data  = resp.d.asBytes;\r
 \r
                if (isOK)\r
                        PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 16));\r
@@ -230,12 +228,12 @@ int CmdHF14AMfRdSc(const char *Cmd)
   UsbCommand c = {CMD_MIFARE_READSC, {sectorNo, keyType, 0}};\r
        memcpy(c.d.asBytes, key, 6);\r
   SendCommand(&c);\r
-       UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
        PrintAndLog(" ");\r
 \r
-       if (resp != NULL) {\r
-               isOK  = resp->arg[0] & 0xff;\r
-               data  = resp->d.asBytes;\r
+       UsbCommand resp;\r
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+               isOK  = resp.arg[0] & 0xff;\r
+               data  = resp.d.asBytes;\r
 \r
                PrintAndLog("isOk:%02x", isOK);\r
                if (isOK) \r
@@ -246,13 +244,11 @@ int CmdHF14AMfRdSc(const char *Cmd)
                PrintAndLog("Command1 execute timeout");\r
        }\r
 \r
-               // response2\r
-       resp = WaitForResponseTimeout(CMD_ACK, 500);\r
+  // response2\r
        PrintAndLog(" ");\r
-\r
-       if (resp != NULL) {\r
-               isOK  = resp->arg[0] & 0xff;\r
-               data  = resp->d.asBytes;\r
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+               isOK  = resp.arg[0] & 0xff;\r
+               data  = resp.d.asBytes;\r
 \r
                if (isOK) \r
                        for (i = 0; i < 2; i++) {\r
@@ -276,7 +272,7 @@ int CmdHF14AMfDump(const char *Cmd)
        FILE *fin;\r
        FILE *fout;\r
        \r
-       UsbCommand *resp;\r
+       UsbCommand resp;\r
        \r
        if ((fin = fopen("dumpkeys.bin","rb")) == NULL) {\r
                PrintAndLog("Could not find file dumpkeys.bin");\r
@@ -307,11 +303,10 @@ int CmdHF14AMfDump(const char *Cmd)
                UsbCommand c = {CMD_MIFARE_READBL, {4*i + 3, 0, 0}};\r
                memcpy(c.d.asBytes, keyA[i], 6);\r
                SendCommand(&c);\r
-               resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
 \r
-               if (resp != NULL) {\r
-                       uint8_t isOK  = resp->arg[0] & 0xff;\r
-                       uint8_t *data  = resp->d.asBytes;\r
+    if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                       uint8_t isOK  = resp.arg[0] & 0xff;\r
+                       uint8_t *data  = resp.d.asBytes;\r
                        if (isOK){\r
                                rights[i][0] = ((data[7] & 0x10)>>4) | ((data[8] & 0x1)<<1) | ((data[8] & 0x10)>>2);\r
                                rights[i][1] = ((data[7] & 0x20)>>5) | ((data[8] & 0x2)<<0) | ((data[8] & 0x20)>>3);\r
@@ -333,20 +328,23 @@ int CmdHF14AMfDump(const char *Cmd)
        PrintAndLog("|----- Dumping all blocks to file... -----|");\r
        PrintAndLog("|-----------------------------------------|");\r
        \r
+  \r
        for (i=0 ; i<16 ; i++) {\r
                for (j=0 ; j<4 ; j++) {\r
-                       if (j == 3){\r
+      bool received = false;\r
+      \r
+      if (j == 3){\r
                                UsbCommand c = {CMD_MIFARE_READBL, {i*4 + j, 0, 0}};\r
                                memcpy(c.d.asBytes, keyA[i], 6);\r
                                SendCommand(&c);\r
-                               resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
+        received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
                        }\r
                        else{\r
                                if ((rights[i][j] == 6) | (rights[i][j] == 5)) {\r
                                        UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 1, 0}};\r
                                        memcpy(c.d.asBytes, keyB[i], 6);\r
                                        SendCommand(&c);\r
-                                       resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
+          received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
                                }\r
                                else if (rights[i][j] == 7) {\r
                                        PrintAndLog("Access rights do not allow reading of sector %d block %d",i,j);\r
@@ -355,13 +353,13 @@ int CmdHF14AMfDump(const char *Cmd)
                                        UsbCommand c = {CMD_MIFARE_READBL, {i*4+j, 0, 0}};\r
                                        memcpy(c.d.asBytes, keyA[i], 6);\r
                                        SendCommand(&c);\r
-                                       resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
+          received = WaitForResponseTimeout(CMD_ACK,&resp,1500);\r
                                }\r
                        }\r
 \r
-                       if (resp != NULL) {\r
-                               uint8_t isOK  = resp->arg[0] & 0xff;\r
-                               uint8_t *data  = resp->d.asBytes;\r
+                       if (received) {\r
+                               uint8_t isOK  = resp.arg[0] & 0xff;\r
+                               uint8_t *data  = resp.d.asBytes;\r
                                if (j == 3) {\r
                                        data[0]  = (keyA[i][0]);\r
                                        data[1]  = (keyA[i][1]);\r
@@ -462,10 +460,10 @@ int CmdHF14AMfRestore(const char *Cmd)
                        \r
                        memcpy(c.d.asBytes + 10, bldata, 16);\r
                        SendCommand(&c);\r
-                       UsbCommand *resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
 \r
-                       if (resp != NULL) {\r
-                               uint8_t isOK  = resp->arg[0] & 0xff;\r
+                       UsbCommand resp;\r
+      if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                               uint8_t isOK  = resp.arg[0] & 0xff;\r
                                PrintAndLog("isOk:%02x", isOK);\r
                        } else {\r
                                PrintAndLog("Command execute timeout");\r
@@ -1591,11 +1589,11 @@ int CmdHF14AMfSniff(const char *Cmd){
                        break;\r
                }\r
                \r
-               UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 2000);\r
-               if (resp != NULL) {\r
-                       res = resp->arg[0] & 0xff;\r
-                       len = resp->arg[1];\r
-                       num = resp->arg[2];\r
+    UsbCommand resp;\r
+    if (WaitForResponseTimeout(CMD_ACK,&resp,2000)) {\r
+                       res = resp.arg[0] & 0xff;\r
+                       len = resp.arg[1];\r
+                       num = resp.arg[2];\r
                        \r
                        if (res == 0) return 0;\r
                        if (res == 1) {\r
@@ -1603,7 +1601,7 @@ int CmdHF14AMfSniff(const char *Cmd){
                                        bufPtr = buf;\r
                                        memset(buf, 0x00, 3000);\r
                                }\r
-                               memcpy(bufPtr, resp->d.asBytes, len);\r
+                               memcpy(bufPtr, resp.d.asBytes, len);\r
                                bufPtr += len;\r
                                pckNum++;\r
                        }\r
@@ -1678,7 +1676,7 @@ static command_t CommandTable[] =
 int CmdHFMF(const char *Cmd)\r
 {\r
        // flush\r
-       while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;\r
+       while (!WaitForResponseTimeout(CMD_ACK,NULL,500));\r
 \r
   CmdsParse(CommandTable, Cmd);\r
   return 0;\r
index 4dd5823df48e9b36bcedde92cc3cd809a8b98c0e..991cd5324710f0558c1efd57a13e6ed39a3a1cf2 100644 (file)
@@ -14,6 +14,7 @@
 #include <limits.h>
 #include "ui.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "cmdparser.h"
 #include "cmdhw.h"
 
index 78e1f98869b919c97f09ee5cbd0bdd550ebc8d79..98a6c1f05ba6dc8f9384a42c9c63a9d742da9a5b 100644 (file)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <limits.h>
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "data.h"
 #include "graph.h"
 #include "ui.h"
@@ -364,7 +365,7 @@ int CmdLFRead(const char *Cmd)
     return 0;
   }
   SendCommand(&c);
-  WaitForResponse(CMD_ACK);
+  WaitForResponse(CMD_ACK,NULL);
   return 0;
 }
 
@@ -401,7 +402,7 @@ int CmdLFSim(const char *Cmd)
       c.d.asBytes[j] = GraphBuffer[i+j];
     }
     SendCommand(&c);
-    WaitForResponse(CMD_ACK);
+    WaitForResponse(CMD_ACK,NULL);
   }
 
   PrintAndLog("Starting simulator...");
index 537712f441dac5aeec4b068ca4ec85b51f5a67a5..83ed673b357fe86f4f03ab9eb2d06eae7effeada 100644 (file)
@@ -12,6 +12,7 @@
 #include <string.h>
 #include <inttypes.h>
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "ui.h"
 #include "graph.h"
 #include "cmdparser.h"
index 767ea3cc3dec3216489babd45473b83e8656ffbe..93c351f1081774eee6c717c94c0239824d55f59b 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <stdio.h>
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "ui.h"
 #include "graph.h"
 #include "cmdparser.h"
index c852544c5532bb135836bc432a8e28cdacaadf54..2541ce59fd7abea6efc8b61333720e040a4e23f6 100644 (file)
 #include <string.h>
 #include "data.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "ui.h"
 #include "cmdparser.h"
 #include "common.h"
 #include "util.h"
 #include "hitag2.h"
+#include "sleep.h"
+#include "cmdmain.h"
 
 static int CmdHelp(const char *Cmd);
 
@@ -25,16 +28,18 @@ int CmdLFHitagList(const char *Cmd)
 {
   uint8_t got[3000];
   GetFromBigBuf(got,sizeof(got),0);
+  WaitForResponse(CMD_ACK,NULL);
+
   char filename[256];
-  FILE* pf;
+  FILE* pf = NULL;
 
-  param_getstr(Cmd,0,filename);
-  
-  if (strlen(filename) > 0) {
+  if (param_getstr(Cmd,0,filename)) {
+    if (strlen(filename) > 0) {
       if ((pf = fopen(filename,"w")) == NULL) {
-           PrintAndLog("Error: Could not open file [%s]",filename);
-           return 1;
-         }
+        PrintAndLog("Error: Could not open file [%s]",filename);
+        return 1;
+      }
+    }
   }
 
   PrintAndLog("recorded activity:");
@@ -114,7 +119,7 @@ int CmdLFHitagList(const char *Cmd)
       line);
 
 
-       if (strlen(filename) > 0) {
+   if (pf) {
       fprintf(pf," +%7d: %s: %s %s %s",
                                        (prev < 0 ? 0 : (timestamp - prev)),
                                        metricString,
@@ -127,7 +132,7 @@ int CmdLFHitagList(const char *Cmd)
     i += (len + 9);
   }
   
-  if (strlen(filename) > 0) {
+  if (pf) {
          PrintAndLog("Recorded activity succesfully written to file: %s", filename);
     fclose(pf);
   }
index 59f83f0873de3a01e83a96f5c37ff5027af9594f..4e8b1150bfc52527738e0ee3f1d0d3909488a5d1 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdlib.h>
 #include "crc16.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "data.h"
 #include "ui.h"
 #include "graph.h"
index c26f2eb4a1af1d86ce640fc8f9f367cf2fae8bd5..edff9f8e01d641a99d1a534737d0248da04def56 100644 (file)
@@ -14,6 +14,7 @@
 #include <string.h>
 #include "sleep.h"
 #include "cmdparser.h"
+#include "proxmark3.h"
 #include "data.h"
 #include "usb_cmd.h"
 #include "ui.h"
@@ -55,37 +56,40 @@ int CmdQuit(const char *Cmd)
   return 0;
 }
 
-UsbCommand * WaitForResponseTimeout(uint32_t response_type, uint32_t ms_timeout) {
-       UsbCommand * ret =  NULL;
-       int i=0;
+bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
 
-       for(i=0; received_command != response_type && i < ms_timeout / 10; i++) {
-               msleep(10); // XXX ugh
+       // Wait until the command is received
+  for(size_t i=0; received_command != cmd && i < ms_timeout; i++) {
+               msleep(1); // XXX ugh
+    if (i == 2000) {
+      PrintAndLog("Waiting for a response from the proxmark...");
+      PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
+    }
        }
        
-       // There was an evil BUG
-       memcpy(&current_response_user, &current_response, sizeof(UsbCommand));
-       ret = &current_response_user;
+  // Check if timeout occured
+  if(received_command != cmd) return false;
 
-       if(received_command != response_type)
-               ret = NULL;
+       // Copy the received response (if supplied)
+  if (response) {
+    memcpy(response, &current_response, sizeof(UsbCommand));
+  }
 
-       received_command = CMD_UNKNOWN;
+       // Reset the received command
+  received_command = CMD_UNKNOWN;
 
-       return ret;
+       return true;
 }
 
-UsbCommand * WaitForResponse(uint32_t response_type)
-{
-       return WaitForResponseTimeout(response_type, -1);
+bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
+       return WaitForResponseTimeout(cmd,response,-1);
 }
 
 //-----------------------------------------------------------------------------
 // Entry point into our code: called whenever the user types a command and
 // then presses Enter, which the full command line that they typed.
 //-----------------------------------------------------------------------------
-void CommandReceived(char *Cmd)
-{
+void CommandReceived(char *Cmd) {
   CmdsParse(CommandTable, Cmd);
 }
 
@@ -138,6 +142,19 @@ void UsbCommandReceived(UsbCommand *UC)
         PrintAndLog("# Your HF antenna is marginal.");
     } break;
       
+    case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
+//      printf("received samples: ");
+//      print_hex(UC->d.asBytes,512);
+      sample_buf_len += UC->arg[1];
+//      printf("samples: %zd offset: %d\n",sample_buf_len,UC->arg[0]);
+      memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
+    } break;
+
+
+//    case CMD_ACK: {
+//      PrintAndLog("Receive ACK\n");
+//    } break;
+
     default: {
       // Maybe it's a response
       switch(current_command) {
@@ -146,19 +163,24 @@ void UsbCommandReceived(UsbCommand *UC)
             PrintAndLog("unrecognized command %08x\n", UC->cmd);
             break;
           }
-          int i;
-          for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
+//          int i;
+          PrintAndLog("received samples %d\n",UC->arg[0]);
+          memcpy(sample_buf+UC->arg[0],UC->d.asBytes,48);
+          sample_buf_len += 48;
+//          for(i=0; i<48; i++) sample_buf[i] = UC->d.asBytes[i];
           received_command = UC->cmd;
         } break;
 
         default: {
         } break;
       }
-      // Store the last received command
-      received_command = UC->cmd;
-      memcpy(&current_response, UC, sizeof(UsbCommand));
+//      // Store the last received command
+//      memcpy(&current_response, UC, sizeof(UsbCommand));
+//      received_command = UC->cmd;
     } break;
   }
+  // Store the last received command
+  memcpy(&current_response, UC, sizeof(UsbCommand));
   received_command = UC->cmd;
 /*
   // Maybe it's a response:
index 3a4145c1f6dfeb743e1f4eedeafccc4f37414f8a..a745fea7bf9e76c0c9acd1760444b69465daaa5f 100644 (file)
@@ -15,7 +15,7 @@
 
 void UsbCommandReceived(UsbCommand *UC);
 void CommandReceived(char *Cmd);
-UsbCommand * WaitForResponseTimeout(uint32_t response_type, uint32_t ms_timeout);
-UsbCommand * WaitForResponse(uint32_t response_type);
+bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout);
+bool WaitForResponse(uint32_t cmd, UsbCommand* response);
 
 #endif
index 6f17e0bdbe7c00cacd046131eba8073f9342159a..7ea1bf47023d180b31dea6866164cedcfd5fda1c 100644 (file)
@@ -12,6 +12,7 @@
 #include <string.h>
 #include "ui.h"
 #include "cmdparser.h"
+#include "proxmark3.h"
 
 void CmdsHelp(const command_t Commands[])
 {
index eeae8cc58aab6670497433538fa8a77b8c13bd1e..b6639867be6dcc9d04daf3f60436682f382fca5f 100644 (file)
 #include "data.h"
 #include "ui.h"
 #include "proxusb.h"
+#include "proxmark3.h"
 #include "cmdmain.h"
 
-uint8_t sample_buf[SAMPLE_BUFFER_SIZE];
+uint8_t* sample_buf;
+size_t sample_buf_len;
 
 void GetFromBigBuf(uint8_t *dest, int bytes, int start_index)
 {
-       start_index = ((start_index/12)*12);
-    int n = (((bytes/4)/48)*48) + start_index;
+  sample_buf_len = 0;
+  sample_buf = dest;
+//     start_index = ((start_index/12)*12);
+//    int n = start_index + bytes;
     /*
      if (n % 48 != 0) {
      PrintAndLog("bad len in GetFromBigBuf");
      return;
      }
      */
-    for (int i = start_index; i < n; i += 12) {
+  UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}};
+  SendCommand(&c);
+/*
+  for (int i = start_index; i < n; i += 48) {
         UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {i, 0, 0}};
         SendCommand(&c);
-        WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
-        memcpy(dest+(i*4), sample_buf, 48);
+//        WaitForResponse(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K);
+//        memcpy(dest+(i*4), sample_buf, 48);
     }
+*/
 }
index e3a4cbdaad8e5622e1370742b90c5efd0a7ef958..33ee9d04e1c746a4951c8ddd480ed1d01d6e86ec 100644 (file)
@@ -15,7 +15,8 @@
 
 #define SAMPLE_BUFFER_SIZE 64
 
-extern uint8_t sample_buf[SAMPLE_BUFFER_SIZE];
+extern uint8_t* sample_buf;
+extern size_t sample_buf_len;
 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
 
 void GetFromBigBuf(uint8_t *dest, int bytes, int start_index);
index 6670d637dca187bff1c7944961dbe20101198620..8fe874e5cab6d0722def4396280073e88e018aa4 100644 (file)
@@ -267,11 +267,11 @@ fail:
 // Get the state of the proxmark, backwards compatible
 static int get_proxmark_state(uint32_t *state)
 {
-       UsbCommand c;
+       HidCommand c;
        c.cmd = CMD_DEVICE_INFO;
-       SendCommand(&c);
+       SendCommand_(&c);
 
-       UsbCommand resp;
+       HidCommand resp;
        ReceiveCommand(&resp);
 
        // Three outcomes:
@@ -313,7 +313,7 @@ static int enter_bootloader(void)
 
        if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
                fprintf(stderr,"Entering bootloader...\n");
-               UsbCommand c;
+               HidCommand c;
                memset(&c, 0, sizeof (c));
 
                if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT)
@@ -322,12 +322,12 @@ static int enter_bootloader(void)
                        // New style handover: Send CMD_START_FLASH, which will reset the board
                        // and enter the bootrom on the next boot.
                        c.cmd = CMD_START_FLASH;
-                       SendCommand(&c);
+                       SendCommand_(&c);
                        fprintf(stderr,"(Press and release the button only to abort)\n");
                } else {
                        // Old style handover: Ask the user to press the button, then reset the board
                        c.cmd = CMD_HARDWARE_RESET;
-                       SendCommand(&c);
+                       SendCommand_(&c);
                        fprintf(stderr,"Press and hold down button NOW if your bootloader requires it.\n");
                }
                fprintf(stderr,"Waiting for Proxmark to reappear on USB...");
@@ -349,7 +349,7 @@ static int enter_bootloader(void)
 
 static int wait_for_ack(void)
 {
-       UsbCommand ack;
+       HidCommand ack;
        ReceiveCommand(&ack);
        if (ack.cmd != CMD_ACK) {
                printf("Error: Unexpected reply 0x%04x (expected ACK)\n", ack.cmd);
@@ -372,7 +372,7 @@ int flash_start_flashing(int enable_bl_writes)
        if (state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
                // This command is stupid. Why the heck does it care which area we're
                // flashing, as long as it's not the bootloader area? The mind boggles.
-               UsbCommand c = {CMD_START_FLASH};
+               HidCommand c = {CMD_START_FLASH};
 
                if (enable_bl_writes) {
                        c.arg[0] = FLASH_START;
@@ -383,7 +383,7 @@ int flash_start_flashing(int enable_bl_writes)
                        c.arg[1] = FLASH_END;
                        c.arg[2] = 0;
                }
-               SendCommand(&c);
+               SendCommand_(&c);
                return wait_for_ack();
        } else {
                fprintf(stderr, "Note: Your bootloader does not understand the new START_FLASH command\n");
@@ -400,11 +400,11 @@ static int write_block(uint32_t address, uint8_t *data, uint32_t length)
        memset(block_buf, 0xFF, BLOCK_SIZE);
        memcpy(block_buf, data, length);
 
-       UsbCommand c = {CMD_SETUP_WRITE};
+       HidCommand c = {CMD_SETUP_WRITE};
        for (int i = 0; i < 240; i += 48) {
                memcpy(c.d.asBytes, block_buf + i, 48);
                c.arg[0] = i / 4;
-               SendCommand(&c);
+               SendCommand_(&c);
                if (wait_for_ack() < 0)
                        return -1;
        }
@@ -412,7 +412,7 @@ static int write_block(uint32_t address, uint8_t *data, uint32_t length)
        c.cmd = CMD_FINISH_WRITE;
        c.arg[0] = address;
        memcpy(c.d.asBytes, block_buf+240, 16);
-       SendCommand(&c);
+       SendCommand_(&c);
        return wait_for_ack();
 }
 
@@ -472,7 +472,7 @@ void flash_free(flash_file_t *ctx)
 
 // just reset the unit
 int flash_stop_flashing(void) {
-       UsbCommand c = {CMD_HARDWARE_RESET};
-       SendCommand(&c);
+       HidCommand c = {CMD_HARDWARE_RESET};
+       SendCommand_(&c);
        return 0;
 }
index 6aa27962b20bf2cbe5e10c109d500c06d3f13991..2b6d5055b953160a9efe965a18a91088016c3468 100644 (file)
Binary files a/client/hitag2.ht2 and b/client/hitag2.ht2 differ
index f34759df80b6343cd1be1de0cf7dc111a20cdcd4..22b2a328fb476f824e417ffc3c1ebc2ce0e69d35 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdlib.h> \r
 #include <string.h>\r
 #include "mifarehost.h"\r
+#include "proxmark3.h"\r
 \r
 // MIFARE\r
 \r
@@ -59,12 +60,12 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
        fnVector * vector = NULL;\r
        countKeys       *ck;\r
        int lenVector = 0;\r
-       UsbCommand * resp = NULL;\r
+       UsbCommand resp;\r
        \r
        memset(resultKeys, 0x00, 16 * 6);\r
 \r
        // flush queue\r
-       while (WaitForResponseTimeout(CMD_ACK, 500) != NULL) ;\r
+       while (!WaitForResponseTimeout(CMD_ACK,NULL,500));\r
        \r
   UsbCommand c = {CMD_MIFARE_NESTED, {blockNo, keyType, trgBlockNo + trgKeyType * 0x100}};\r
        memcpy(c.d.asBytes, key, 6);\r
@@ -81,18 +82,16 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
                        break;\r
                }\r
 \r
-               resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
-\r
-               if (resp != NULL) {\r
-                       isEOF  = resp->arg[0] & 0xff;\r
+               if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+                       isEOF  = resp.arg[0] & 0xff;\r
 \r
                        if (isEOF) break;\r
                        \r
-                       len = resp->arg[1] & 0xff;\r
+                       len = resp.arg[1] & 0xff;\r
                        if (len == 0) continue;\r
                        \r
-                       memcpy(&uid, resp->d.asBytes, 4); \r
-                       PrintAndLog("uid:%08x len=%d trgbl=%d trgkey=%x", uid, len, resp->arg[2] & 0xff, (resp->arg[2] >> 8) & 0xff);\r
+                       memcpy(&uid, resp.d.asBytes, 4);\r
+                       PrintAndLog("uid:%08x len=%d trgbl=%d trgkey=%x", uid, len, resp.arg[2] & 0xff, (resp.arg[2] >> 8) & 0xff);\r
                        vector = (fnVector *) realloc((void *)vector, (lenVector + len) * sizeof(fnVector) + 200);\r
                        if (vector == NULL) {\r
                                PrintAndLog("Memory allocation error for fnVector. len: %d bytes: %d", lenVector + len, (lenVector + len) * sizeof(fnVector)); \r
@@ -100,12 +99,12 @@ int mfnested(uint8_t blockNo, uint8_t keyType, uint8_t * key, uint8_t trgBlockNo
                        }\r
                        \r
                        for (i = 0; i < len; i++) {\r
-                               vector[lenVector + i].blockNo = resp->arg[2] & 0xff;\r
-                               vector[lenVector + i].keyType = (resp->arg[2] >> 8) & 0xff;\r
+                               vector[lenVector + i].blockNo = resp.arg[2] & 0xff;\r
+                               vector[lenVector + i].keyType = (resp.arg[2] >> 8) & 0xff;\r
                                vector[lenVector + i].uid = uid;\r
 \r
-                               memcpy(&vector[lenVector + i].nt,  (void *)(resp->d.asBytes + 8 + i * 8 + 0), 4);\r
-                               memcpy(&vector[lenVector + i].ks1, (void *)(resp->d.asBytes + 8 + i * 8 + 4), 4);\r
+                               memcpy(&vector[lenVector + i].nt,  (void *)(resp.d.asBytes + 8 + i * 8 + 0), 4);\r
+                               memcpy(&vector[lenVector + i].ks1, (void *)(resp.d.asBytes + 8 + i * 8 + 4), 4);\r
                        }\r
 \r
                        lenVector += len;\r
@@ -187,14 +186,12 @@ int mfCheckKeys (uint8_t blockNo, uint8_t keyType, uint8_t keycnt, uint8_t * key
 \r
   UsbCommand c = {CMD_MIFARE_CHKKEYS, {blockNo, keyType, keycnt}};\r
        memcpy(c.d.asBytes, keyBlock, 6 * keycnt);\r
-\r
   SendCommand(&c);\r
 \r
-       UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 3000);\r
-\r
-       if (resp == NULL) return 1;\r
-       if ((resp->arg[0] & 0xff) != 0x01) return 2;\r
-       *key = bytes_to_num(resp->d.asBytes, 6);\r
+       UsbCommand resp;\r
+       if (!WaitForResponseTimeout(CMD_ACK,&resp,3000)) return 1;\r
+       if ((resp.arg[0] & 0xff) != 0x01) return 2;\r
+       *key = bytes_to_num(resp.d.asBytes, 6);\r
        return 0;\r
 }\r
 \r
@@ -202,13 +199,11 @@ int mfCheckKeys (uint8_t blockNo, uint8_t keyType, uint8_t keycnt, uint8_t * key
 \r
 int mfEmlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
        UsbCommand c = {CMD_MIFARE_EML_MEMGET, {blockNum, blocksCount, 0}};\r
\r
-       SendCommand(&c);\r
+       SendCommand(&c);\r
 \r
-       UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
-\r
-       if (resp == NULL) return 1;\r
-       memcpy(data, resp->d.asBytes, blocksCount * 16); \r
+  UsbCommand resp;\r
+       if (!WaitForResponseTimeout(CMD_ACK,&resp,1500)) return 1;\r
+       memcpy(data, resp.d.asBytes, blocksCount * 16);\r
        return 0;\r
 }\r
 \r
@@ -241,11 +236,10 @@ int mfCSetBlock(uint8_t blockNo, uint8_t *data, uint8_t *uid, int wantWipe, uint
        memcpy(c.d.asBytes, data, 16); \r
        SendCommand(&c);\r
 \r
-       UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
-\r
-       if (resp != NULL) {\r
-               isOK  = resp->arg[0] & 0xff;\r
-               if (uid != NULL) memcpy(uid, resp->d.asBytes, 4); \r
+  UsbCommand resp;\r
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+               isOK  = resp.arg[0] & 0xff;\r
+               if (uid != NULL) memcpy(uid, resp.d.asBytes, 4);\r
                if (!isOK) return 2;\r
        } else {\r
                PrintAndLog("Command execute timeout");\r
@@ -260,11 +254,10 @@ int mfCGetBlock(uint8_t blockNo, uint8_t *data, uint8_t params) {
        UsbCommand c = {CMD_MIFARE_EML_CGETBLOCK, {params, 0, blockNo}};\r
        SendCommand(&c);\r
 \r
-       UsbCommand * resp = WaitForResponseTimeout(CMD_ACK, 1500);\r
-\r
-       if (resp != NULL) {\r
-               isOK  = resp->arg[0] & 0xff;\r
-               memcpy(data, resp->d.asBytes, 16); \r
+  UsbCommand resp;\r
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {\r
+               isOK  = resp.arg[0] & 0xff;\r
+               memcpy(data, resp.d.asBytes, 16);\r
                if (!isOK) return 2;\r
        } else {\r
                PrintAndLog("Command execute timeout");\r
index 4b898d5495dd2b58700698ff05225f364936b078..cff94903d1693e46e585f9d00fedc532fc9b67b5 100644 (file)
 #include <unistd.h>
 #include <readline/readline.h>
 #include <readline/history.h>
-#include "proxusb.h"
+//#include "proxusb.h"
 #include "proxmark3.h"
 #include "proxgui.h"
 #include "cmdmain.h"
+#include "uart.h"
+#include "messages.h"
+#include "ui.h"
+
+static serial_port sp;
+
+void SendCommand(UsbCommand *c) {
+#if 0
+  printf("Sending %d bytes\n", sizeof(UsbCommand));
+#endif
+  if (!uart_send(sp,(byte_t*)c,sizeof(UsbCommand))) {
+    ERR("Sending bytes to proxmark failed");
+  }
+}
 
-struct usb_receiver_arg
-{
+struct receiver_arg {
   int run;
 };
 
-struct main_loop_arg
-{
+struct main_loop_arg {
   int usb_present;
   char *script_cmds_file;
 };
 
-static void *usb_receiver(void *targ)
-{
-  struct usb_receiver_arg *arg = (struct usb_receiver_arg*)targ;
-  UsbCommand cmdbuf;
+//static void *usb_receiver(void *targ) {
+//  struct receiver_arg *arg = (struct receiver_arg*)targ;
+//  UsbCommand cmdbuf;
+//
+//  while (arg->run) {
+//    if (ReceiveCommandPoll(&cmdbuf)) {
+//      UsbCommandReceived(&cmdbuf);
+//      fflush(NULL);
+//    }
+//  }
+//
+//  pthread_exit(NULL);
+//  return NULL;
+//}
 
+byte_t rx[0x1000000];
+
+static void *uart_receiver(void *targ) {
+  struct receiver_arg *arg = (struct receiver_arg*)targ;
+  size_t rxlen;
+  size_t cmd_count;
+  
   while (arg->run) {
-    if (ReceiveCommandPoll(&cmdbuf)) {
-      UsbCommandReceived(&cmdbuf);
-      fflush(NULL);
+    if (uart_receive(sp,rx,&rxlen)) {
+      if ((rxlen % sizeof(UsbCommand)) != 0) {
+        PrintAndLog("ERROR: received %zd bytes, which does not seem to be one or more command(s)\n",rxlen);
+        continue;
+      }
+      cmd_count = rxlen / sizeof(UsbCommand);
+//      printf("received %zd bytes, which represents %zd commands\n",rxlen, cmd_count);
+      for (size_t i=0; i<cmd_count; i++) {
+        UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
+      }
     }
   }
-
+  
   pthread_exit(NULL);
   return NULL;
 }
 
-static void *main_loop(void *targ)
-{
-    struct main_loop_arg *arg = (struct main_loop_arg*)targ;
-    struct usb_receiver_arg rarg;
-    char *cmd = NULL;
-    pthread_t reader_thread;
 
-    if (arg->usb_present == 1) {
-        rarg.run=1;
-        pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
-    }
-    
-    FILE *script_file = NULL;
-    char script_cmd_buf[256];
-    
-    if (arg->script_cmds_file)
+static void *main_loop(void *targ) {
+  struct main_loop_arg *arg = (struct main_loop_arg*)targ;
+  struct receiver_arg rarg;
+  char *cmd = NULL;
+  pthread_t reader_thread;
+  
+  if (arg->usb_present == 1) {
+    rarg.run=1;
+    // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
+    pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
+  }
+  
+  FILE *script_file = NULL;
+  char script_cmd_buf[256];
+  
+  if (arg->script_cmds_file)
+  {
+    script_file = fopen(arg->script_cmds_file, "r");
+    if (script_file)
     {
-        script_file = fopen(arg->script_cmds_file, "r");
-        if (script_file)
-        {
-            printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
-        }
+      printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
     }
+  }
 
        read_history(".history");
        while(1)
+  {
+    // If there is a script file
+    if (script_file)
+    {
+      if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
+      {
+        fclose(script_file);
+        script_file = NULL;
+      }
+      else
+      {
+        char *nl;
+        nl = strrchr(script_cmd_buf, '\r');
+        if (nl) *nl = '\0';
+        nl = strrchr(script_cmd_buf, '\n');
+        if (nl) *nl = '\0';
+        
+        if ((cmd = (char*) malloc(strlen(script_cmd_buf))) != NULL)
         {
-           // If there is a script file
-           if (script_file)
-           {
-               if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
-               {
-                   fclose(script_file);
-                   script_file = NULL;
-               }
-               else
-               {
-                   char *nl;
-                   nl = strrchr(script_cmd_buf, '\r');
-                    if (nl) *nl = '\0';
-                    nl = strrchr(script_cmd_buf, '\n');
-                    if (nl) *nl = '\0';
-                   
-                    if ((cmd = (char*) malloc(strlen(script_cmd_buf))) != NULL)
-                    {
-                        memset(cmd, 0, strlen(script_cmd_buf));
-                        strcpy(cmd, script_cmd_buf);
-                        printf("%s\n", cmd);
-                    }
-               }
-           }
+          memset(cmd, 0, strlen(script_cmd_buf));
+          strcpy(cmd, script_cmd_buf);
+          printf("%s\n", cmd);
+        }
+      }
+    }
                
                if (!script_file)
                {
-                   cmd = readline(PROXPROMPT);
+      cmd = readline(PROXPROMPT);
                }
                
                if (cmd) {
                        while(cmd[strlen(cmd) - 1] == ' ')
-                       cmd[strlen(cmd) - 1] = 0x00;
+        cmd[strlen(cmd) - 1] = 0x00;
                        
                        if (cmd[0] != 0x00) {
                                if (strncmp(cmd, "quit", 4) == 0) {
@@ -123,41 +160,41 @@ static void *main_loop(void *targ)
                        break;
                }
        }
-
+  
        write_history(".history");
-
-    if (arg->usb_present == 1) {
-        rarg.run = 0;
-        pthread_join(reader_thread, NULL);
-    }
-    
-    if (script_file)
-    {
-        fclose(script_file);
-        script_file = NULL;
-    }
-
-    ExitGraphics();
-    pthread_exit(NULL);
-    return NULL;
+  
+  if (arg->usb_present == 1) {
+    rarg.run = 0;
+    pthread_join(reader_thread, NULL);
+  }
+  
+  if (script_file)
+  {
+    fclose(script_file);
+    script_file = NULL;
+  }
+  
+  ExitGraphics();
+  pthread_exit(NULL);
+  return NULL;
 }
 
-int main(int argc, char **argv)
-{
+int main(int argc, char* argv[]) {
+  
+  if (argc < 2) {
+    printf("syntax: %s <port>\n\n",argv[0]);
+    return 1;
+  }
+  
   // Make sure to initialize
   struct main_loop_arg marg = {
     .usb_present = 0,
     .script_cmds_file = NULL
   };
   pthread_t main_loop_t;
-  usb_init();
-
-  // If the user passed the filename of the 'script' to execute, get it
-  if (argc > 1 && argv[1])
-  {
-    marg.script_cmds_file = argv[1];
-  }
 
+/*
+  usb_init();
   if (!OpenProxmark(1)) {
     fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
     marg.usb_present = 0;
@@ -166,7 +203,22 @@ int main(int argc, char **argv)
     marg.usb_present = 1;
     offline = 0;
   }
+*/
+  sp = uart_open(argv[1]);
+  if (sp == INVALID_SERIAL_PORT) {
+    printf("ERROR: invalid serial port\n");
+    marg.usb_present = 0;
+    offline = 1;
+  } else {
+    marg.usb_present = 1;
+    offline = 0;
+  }
 
+  // If the user passed the filename of the 'script' to execute, get it
+  if (argc > 2 && argv[2]) {
+    marg.script_cmds_file = argv[2];
+  }
+  
   pthread_create(&main_loop_t, NULL, &main_loop, &marg);
   InitGraphics(argc, argv);
 
@@ -174,8 +226,12 @@ int main(int argc, char **argv)
 
   pthread_join(main_loop_t, NULL);
 
-  if (marg.usb_present == 1) {
-    CloseProxmark();
-  }
+//  if (marg.usb_present == 1) {
+//    CloseProxmark();
+//  }
+
+  // Clean up the port
+  uart_close(sp);
+  
   return 0;
 }
index f5dd2b7fa761e86bb6c9c9b719d71a1a24ebfdca..2875511b4f3de7ad040a6b0828be5e1d9ecbfe64 100644 (file)
 #ifndef PROXMARK3_H__
 #define PROXMARK3_H__
 
+#include <usb.h>
+#include "usb_cmd.h"
+
 #define PROXPROMPT "proxmark3> "
 
+void SendCommand(UsbCommand *c);
+
 #endif
index 3c2b20b4c13fdf6ffc182ff24a0a155b2dd903f0..2f152ace25dfe5f2f49e48fee3e11ed4b609c3e4 100644 (file)
@@ -34,15 +34,15 @@ unsigned char return_on_error = 0;
 unsigned char error_occured = 0;
 extern unsigned int current_command;
 
-void SendCommand(UsbCommand *c)
+void SendCommand_(HidCommand *c)
 {
   int ret;
 
 #if 0
-  printf("Sending %d bytes\n", sizeof(UsbCommand));
+  printf("Sending %d bytes\n", sizeof(HidCommand));
 #endif
   current_command = c->cmd;
-  ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
+  ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(HidCommand), 1000);
   if (ret<0) {
     error_occured = 1;
     if (return_on_error)
@@ -63,12 +63,12 @@ void SendCommand(UsbCommand *c)
   }
 }
 
-bool ReceiveCommandPoll(UsbCommand *c)
+bool ReceiveCommandPoll(HidCommand *c)
 {
   int ret;
 
-  memset(c, 0, sizeof (UsbCommand));
-  ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500);
+  memset(c, 0, sizeof (HidCommand));
+  ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(HidCommand), 500);
   if (ret<0) {
     if (ret != -ETIMEDOUT) {
       error_occured = 1;
@@ -89,16 +89,16 @@ bool ReceiveCommandPoll(UsbCommand *c)
       return false;
     }
   } else {
-    if (ret && (ret < sizeof(UsbCommand))) {
+    if (ret && (ret < sizeof(HidCommand))) {
       fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
-        ret, (int)sizeof(UsbCommand));
+        ret, (int)sizeof(HidCommand));
     }
   }
 
   return ret > 0;
 }
 
-void ReceiveCommand(UsbCommand *c)
+void ReceiveCommand(HidCommand *c)
 {
 //  printf("%s()\n", __FUNCTION__);
   int retval = 0;
index 5845888a539af7ea01b200f815ffe8b23cc809c5..cc34fd4837b283c2e740478e562c3ed66de35fbc 100644 (file)
@@ -19,9 +19,9 @@
 extern unsigned char return_on_error;
 extern unsigned char error_occured;
 
-void SendCommand(UsbCommand *c);
-bool ReceiveCommandPoll(UsbCommand *c);
-void ReceiveCommand(UsbCommand *c);
+void SendCommand_(HidCommand *c);
+bool ReceiveCommandPoll(HidCommand *c);
+void ReceiveCommand(HidCommand *c);
 struct usb_dev_handle* FindProxmark(int verbose, unsigned int *iface);
 struct usb_dev_handle* OpenProxmark(int verbose);
 void CloseProxmark(void);
index 22e834719b94174883fef4336360cd0864eae49c..1d410592e1d39dfc1032cbfd5d551cafd8845b6f 100644 (file)
@@ -16,6 +16,9 @@
 #include <ctype.h>
 #include <time.h>
 
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
 int ukbhit(void);
 
 void AddLogLine(char *fileName, char *extData, char *c);
index d89683270cd6e2fecf9db4058b1eedd5e48fefd5..9ff05c5005e67e2a840f8081c8d0cf8922d056a5 100644 (file)
@@ -65,7 +65,7 @@ VPATH = . ../common/ ../fpga/
 
 INCLUDES = ../include/proxmark3.h ../include/at91sam7s512.h ../include/config_gpio.h ../include/usb_cmd.h $(APP_INCLUDES)
 
-CFLAGS =  -c $(INCLUDE) -Wall -Werror -pedantic -std=gnu99 $(APP_CFLAGS)
+CFLAGS =  -c $(INCLUDE) -Wall -Werror -pedantic -std=c99 $(APP_CFLAGS)
 LDFLAGS = -nostartfiles -nodefaultlibs -Wl,-gc-sections -n
 LIBS = -lgcc
 
index cfce1b9f0347f6a23b3081c088ed33e08f2461bb..13daa86eb94b35085bb622ce757c5e292255cc61 100644 (file)
 #ifndef __COMMON_H
 #define __COMMON_H
 
+#include <stddef.h>
 #include <stdint.h>
+#include <stdbool.h>
+#include <at91sam7s512.h>
 typedef unsigned char byte_t;
 
-//-----------------------------------------------------------------------------
-// ISO 14443A
-//-----------------------------------------------------------------------------
-typedef struct {
-       uint8_t atqa[2];
-       uint8_t  sak;
-       uint8_t  ats_len;
-       uint8_t  ats[20]; //FIXME: size?
-} __attribute__((__packed__)) iso14a_card_select_t;
-
-typedef enum ISO14A_COMMAND {
-       ISO14A_CONNECT = 1,
-       ISO14A_NO_DISCONNECT = 2,
-       ISO14A_APDU = 4,
-       ISO14A_RAW = 8,
-       ISO14A_REQUEST_TRIGGER = 0x10,
-       ISO14A_APPEND_CRC = 0x20,
-       ISO14A_SET_TIMEOUT = 0x40
-} iso14a_command_t;
-
 #endif
index a7552b3ec4b3deef94f7c9f8ea57b241a84d3623..b3c52d1df086139c8f4e95515ea8a9289aa13147 100644 (file)
@@ -24,12 +24,23 @@ typedef BYTE uint8_t;
 #endif
 
 typedef struct {
-       uint32_t        cmd;
-       uint32_t        arg[3];
-       union {
-               uint8_t         asBytes[48];
-               uint32_t        asDwords[12];
-       } d;
+  uint32_t     cmd;
+  uint32_t     arg[3];
+  union {
+    uint8_t            asBytes[48];
+    uint32_t   asDwords[12];
+  } d;
+} PACKED HidCommand;
+
+#define USB_CMD_DATA_SIZE 512
+
+typedef struct {
+  uint32_t cmd;
+  uint32_t arg[3];
+  union {
+    uint8_t     asBytes[USB_CMD_DATA_SIZE];
+    uint32_t asDwords[USB_CMD_DATA_SIZE/4];
+  } d;
 } PACKED UsbCommand;
 
 // For the bootloader
Impressum, Datenschutz