]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
Merge pull request #440 from merlokk/c3
authorIceman <iceman@iuse.se>
Thu, 26 Oct 2017 09:46:20 +0000 (11:46 +0200)
committerGitHub <noreply@github.com>
Thu, 26 Oct 2017 09:46:20 +0000 (11:46 +0200)
improve appveyor.yml

armsrc/mifarecmd.c
client/cmdmain.c
client/mifarehost.c
client/proxmark3.c
common/usb_cdc.c
fpga/fpga_hf.bit
fpga/hi_read_rx_xcorr.v
fpga/hi_read_tx.v
uart/uart_win32.c

index edafe0a34129d922397e7deb40fc7bc4f09d4e9e..39029d4e3429dcc2e4202aaf80e21477a99d4c84 100644 (file)
@@ -1510,6 +1510,16 @@ void MifareCIdent(){
 \r
        uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
        uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
+       \r
+       LED_A_ON();\r
+       LED_B_OFF();\r
+       LED_C_OFF();\r
+//     FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+//     SpinDelay(100);\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
+\r
+       clear_trace();\r
+       set_tracing(true);      \r
 \r
        ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
        if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {\r
@@ -1523,8 +1533,13 @@ void MifareCIdent(){
 \r
        // From iceman1001: removed the if,  since some magic tags misbehavies and send an answer to it.\r
        mifare_classic_halt(NULL, 0);\r
-\r
+       \r
+       LED_B_ON();\r
        cmd_send(CMD_ACK,isOK,0,0,0,0);\r
+       LED_B_OFF();\r
+\r
+       FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
+       LEDsoff();      \r
 }\r
 \r
 //\r
index db88b3ac8ec90ccf653db04e938cb7857878aa8c..739d68e1cbc0beb609f0d858dae78a3ad1438b95 100644 (file)
@@ -8,6 +8,9 @@
 // Main command parser entry point
 //-----------------------------------------------------------------------------
 
+#include "cmdmain.h"
+
+#include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -21,7 +24,6 @@
 #include "cmddata.h"
 #include "cmdhw.h"
 #include "cmdlf.h"
-#include "cmdmain.h"
 #include "util.h"
 #include "util_posix.h"
 #include "cmdscript.h"
@@ -41,6 +43,8 @@ static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
 static int cmd_head;//Starts as 0
 //Points to the position of the last unread command
 static int cmd_tail;//Starts as 0
+// to lock cmdBuffer operations from different threads
+static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER;
 
 static command_t CommandTable[] = 
 {
@@ -86,7 +90,9 @@ int CmdRev(const char *Cmd)
 void clearCommandBuffer()
 {
     //This is a very simple operation
+       pthread_mutex_lock(&cmdBufferMutex);
     cmd_tail = cmd_head;
+       pthread_mutex_unlock(&cmdBufferMutex);
 }
 
 /**
@@ -95,6 +101,7 @@ void clearCommandBuffer()
  */
 void storeCommand(UsbCommand *command)
 {
+       pthread_mutex_lock(&cmdBufferMutex);
     if( ( cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
     {
         //If these two are equal, we're about to overwrite in the
@@ -106,6 +113,7 @@ void storeCommand(UsbCommand *command)
     memcpy(destination, command, sizeof(UsbCommand));
 
     cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
+       pthread_mutex_unlock(&cmdBufferMutex);
 }
 
 
@@ -116,8 +124,10 @@ void storeCommand(UsbCommand *command)
  */
 int getCommand(UsbCommand* response)
 {
+       pthread_mutex_lock(&cmdBufferMutex);
     //If head == tail, there's nothing to read, or if we just got initialized
     if(cmd_head == cmd_tail){
+               pthread_mutex_unlock(&cmdBufferMutex);
         return 0;
     }
     //Pick out the next unread command
@@ -125,7 +135,7 @@ int getCommand(UsbCommand* response)
     memcpy(response, last_unread, sizeof(UsbCommand));
     //Increment tail - this is a circular buffer, so modulo buffer size
     cmd_tail = (cmd_tail +1 ) % CMD_BUFFER_SIZE;
-
+       pthread_mutex_unlock(&cmdBufferMutex);
     return 1;
 }
 
@@ -147,22 +157,28 @@ bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeo
                response = &resp;
        }
 
+       uint64_t start_time = msclock();
+       
        // Wait until the command is received
-       for(size_t dm_seconds=0; dm_seconds < ms_timeout/10; dm_seconds++) {
+       while (true) {
                while(getCommand(response)) {
                        if(response->cmd == cmd){
                                return true;
                        }
                }
-               msleep(10); // XXX ugh
-               if (dm_seconds == 200 && show_warning) { // Two seconds elapsed
+               if (msclock() - start_time > ms_timeout) {
+                       break;
+               }
+               if (msclock() - start_time > 2000 && show_warning) {
                        PrintAndLog("Waiting for a response from the proxmark...");
                        PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
+                       break;
                }
        }
        return false;
 }
 
+
 bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
        return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
 }
index a02019a316cbc52f6cbbe4cad8cea733c409f8f8..ca5d97e13ac1419cbfcac1598ab35d643a9166c7 100644 (file)
@@ -542,11 +542,12 @@ int mfCSetUID(uint8_t *uid, uint8_t *atqa, uint8_t *sak, uint8_t *oldUID) {
 \r
 int mfCIdentify()\r
 {\r
-       UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};\r
-       SendCommand(&c);\r
+  UsbCommand c;\r
+//     UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};\r
+//     SendCommand(&c);\r
 \r
-       UsbCommand resp;\r
-       WaitForResponse(CMD_ACK,&resp);\r
+  UsbCommand resp;\r
+//     WaitForResponse(CMD_ACK,&resp);\r
 \r
        // iso14a_card_select_t card;\r
        // memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));\r
@@ -578,11 +579,11 @@ int mfCIdentify()
        }\r
 \r
        // disconnect\r
-       c.cmd = CMD_READER_ISO_14443a;\r
-       c.arg[0] = 0;\r
-       c.arg[1] = 0;\r
-       c.arg[2] = 0;\r
-       SendCommand(&c);\r
+//     c.cmd = CMD_READER_ISO_14443a;\r
+//     c.arg[0] = 0;\r
+//     c.arg[1] = 0;\r
+//     c.arg[2] = 0;\r
+//     SendCommand(&c);\r
 \r
        return (int) isGeneration;\r
 }\r
index d0e68b6121a32086d8109badf8f768a28c154eee..99ba9fbad77ec9dbcea40bc0b36074ea7914b0e5 100644 (file)
@@ -73,12 +73,11 @@ static void *uart_receiver(void *targ) {
 
        while (arg->run) {
                rxlen = 0;
-               if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen)) {
+               if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
                        prx += rxlen;
                        if (prx-rx < sizeof(UsbCommand)) {
                                continue;
                        }
-                       
                        UsbCommandReceived((UsbCommand*)rx);
                }
                prx = rx;
@@ -356,11 +355,13 @@ int main(int argc, char* argv[]) {
                sp = uart_open(argv[1]);
        } else {
                printf("Waiting for Proxmark to appear on %s ", argv[1]);
+               fflush(stdout);
                int openCount = 0;
                do {
                        sp = uart_open(argv[1]);
                        msleep(1000);
                        printf(".");
+                       fflush(stdout);
                } while(++openCount < 20 && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
                printf("\n");
        }
index 56690ad87f69c42a095a2f515341dbf44304f858..91a63cf3c8cfd8984d8f055411273bf085d85b70 100644 (file)
 #define AT91C_EP_OUT_SIZE 0x40\r
 #define AT91C_EP_IN_SIZE  0x40\r
 \r
+// Language must always be 0.\r
+#define STR_LANGUAGE_CODES 0x00\r
+#define STR_MANUFACTURER   0x01\r
+#define STR_PRODUCT        0x02\r
+\r
 static const char devDescriptor[] = {\r
        /* Device descriptor */\r
        0x12,      // bLength\r
@@ -56,8 +61,8 @@ static const char devDescriptor[] = {
        0xc4,0x9a, // Vendor ID (0x9ac4 = J. Westhues)\r
        0x8f,0x4b, // Product ID (0x4b8f = Proxmark-3 RFID Instrument)\r
        0x01,0x00, // Device release number (0001)\r
-       0x01,      // iManufacturer\r
-       0x02,      // iProduct\r
+       STR_MANUFACTURER,  // iManufacturer\r
+       STR_PRODUCT,       // iProduct\r
        0x00,      // iSerialNumber\r
        0x01       // bNumConfigs\r
 };\r
@@ -157,7 +162,9 @@ static const char StrDescLanguageCodes[] = {
   0x03,                        // Type is string\r
   0x09, 0x04   // supported language Code 0 = 0x0409 (English)\r
 };\r
-       \r
+\r
+// Note: ModemManager (Linux) ignores Proxmark3 devices by matching the\r
+// manufacturer string "proxmark.org". Don't change this.\r
 static const char StrDescManufacturer[] = {\r
   26,                  // Length\r
   0x03,                        // Type is string\r
@@ -182,20 +189,18 @@ static const char StrDescProduct[] = {
   'M', 0x00,\r
   '3', 0x00\r
 };\r
-       \r
-static const char* const pStrings[] =\r
-{\r
-    StrDescLanguageCodes,\r
-    StrDescManufacturer,\r
-       StrDescProduct\r
-};\r
 \r
 const char* getStringDescriptor(uint8_t idx)\r
 {\r
-    if(idx >= (sizeof(pStrings) / sizeof(pStrings[0]))) {\r
-        return(NULL);\r
-       } else {\r
-               return(pStrings[idx]);\r
+       switch (idx) {\r
+               case STR_LANGUAGE_CODES:\r
+                       return StrDescLanguageCodes;\r
+               case STR_MANUFACTURER:\r
+                       return StrDescManufacturer;\r
+               case STR_PRODUCT:\r
+                       return StrDescProduct;\r
+               default:\r
+                       return NULL;\r
        }\r
 }\r
 \r
index b9f2e6292e42801a73b57aa3e43dff00191ec8b9..bd8cf8e7dcf7ae80756e29f2417a94da7e9dd290 100644 (file)
Binary files a/fpga/fpga_hf.bit and b/fpga/fpga_hf.bit differ
index afa46c4437a2870f3065c03d4ed0cd7759f7ef4d..433d6736f1814c0f3cc529e807f8cfb70ed3c1eb 100644 (file)
@@ -34,13 +34,13 @@ always @(negedge ck_1356megb)
 
 (* clock_signal = "yes" *) reg adc_clk;                                // sample frequency, always 16 * fc
 always @(ck_1356megb, xcorr_is_848, xcorr_quarter_freq, fc_div)
-       if (xcorr_is_848 & ~xcorr_quarter_freq)                 // fc = 847.5 kHz
+       if (xcorr_is_848 & ~xcorr_quarter_freq)                 // fc = 847.5 kHz, standard ISO14443B
                adc_clk <= ck_1356megb;
-       else if (~xcorr_is_848 & ~xcorr_quarter_freq)   // fc = 424.25 kHz 
+       else if (~xcorr_is_848 & ~xcorr_quarter_freq)   // fc = 423.75 kHz 
                adc_clk <= fc_div[0];
-       else if (xcorr_is_848 & xcorr_quarter_freq)             // fc = 212.125 kHz
+       else if (xcorr_is_848 & xcorr_quarter_freq)             // fc = 211.875 kHz
                adc_clk <= fc_div[1];
-       else                                                                                    // fc = 106.0625 kHz
+       else                                                                                    // fc = 105.9375 kHz
                adc_clk <= fc_div[2];
                
 // When we're a reader, we just need to do the BPSK demod; but when we're an
@@ -69,13 +69,16 @@ begin
     end
 end
 
-// Let us report a correlation every 4 subcarrier cycles, or 4*16 samples,
+// Let us report a correlation every 4 subcarrier cycles, or 4*16=64 samples,
 // so we need a 6-bit counter.
 reg [5:0] corr_i_cnt;
 // And a couple of registers in which to accumulate the correlations.
-// we would add/sub at most 32 times adc_d, the signed result can be held in 14 bits. 
-reg signed [13:0] corr_i_accum;
-reg signed [13:0] corr_q_accum;
+// We would add at most 32 times the difference between unmodulated and modulated signal. It should
+// be safe to assume that a tag will not be able to modulate the carrier signal by more than 25%.
+// 32 * 255 * 0,25 = 2040, which can be held in 11 bits. Add 1 bit for sign.
+reg signed [11:0] corr_i_accum;
+reg signed [11:0] corr_q_accum;
+// we will report maximum 8 significant bits
 reg signed [7:0] corr_i_out;
 reg signed [7:0] corr_q_out;
 // clock and frame signal for communication to ARM
@@ -99,16 +102,16 @@ begin
     begin
         if(snoop)
         begin
-                       // Send only 7 most significant bits of tag signal (signed), LSB is reader signal:
-            corr_i_out <= {corr_i_accum[13:7], after_hysteresis_prev_prev};
-            corr_q_out <= {corr_q_accum[13:7], after_hysteresis_prev};
+                       // Send 7 most significant bits of tag signal (signed), plus 1 bit reader signal
+            corr_i_out <= {corr_i_accum[11:5], after_hysteresis_prev_prev};
+            corr_q_out <= {corr_q_accum[11:5], after_hysteresis_prev};
                        after_hysteresis_prev_prev <= after_hysteresis;
         end
         else
         begin
-            // 8 most significant bits of tag signal
-            corr_i_out <= corr_i_accum[13:6];
-            corr_q_out <= corr_q_accum[13:6];
+            // 8 bits of tag signal
+            corr_i_out <= corr_i_accum[11:4];
+            corr_q_out <= corr_q_accum[11:4];
         end
 
         corr_i_accum <= adc_d;
index f12e64eb65c2f8fea1adf6eba8dd211a8ff33a61..fc309cde6cedb60fd86f12a72aaeb44110f832e2 100644 (file)
@@ -24,33 +24,36 @@ module hi_read_tx(
     output dbg;
     input shallow_modulation;
 
+// low frequency outputs, not relevant
+assign pwr_lo = 1'b0;
+assign pwr_oe2 = 1'b0;
+       
 // The high-frequency stuff. For now, for testing, just bring out the carrier,
 // and allow the ARM to modulate it over the SSP.
 reg pwr_hi;
 reg pwr_oe1;
-reg pwr_oe2;
 reg pwr_oe3;
 reg pwr_oe4;
+
 always @(ck_1356megb or ssp_dout or shallow_modulation)
 begin
     if(shallow_modulation)
     begin
         pwr_hi <= ck_1356megb;
-        pwr_oe1 <= ~ssp_dout;
-        pwr_oe2 <= ~ssp_dout;
-        pwr_oe3 <= ~ssp_dout;
-        pwr_oe4 <= 1'b0;
+        pwr_oe1 <= 1'b0;
+        pwr_oe3 <= 1'b0;
+        pwr_oe4 <= ~ssp_dout;
     end
     else
     begin
         pwr_hi <= ck_1356megb & ssp_dout;
         pwr_oe1 <= 1'b0;
-        pwr_oe2 <= 1'b0;
         pwr_oe3 <= 1'b0;
         pwr_oe4 <= 1'b0;
     end
 end
 
+
 // Then just divide the 13.56 MHz clock down to produce appropriate clocks
 // for the synchronous serial port.
 
@@ -83,7 +86,6 @@ end
 
 assign ssp_din = after_hysteresis;
 
-assign pwr_lo = 1'b0;
 assign dbg = ssp_din;
 
 endmodule
index af521ead183741637b9a56fb203c8607bd878971..121b2b51f433c417c217886ec5f606c6a51a00b2 100644 (file)
@@ -107,15 +107,13 @@ void uart_close(const serial_port sp) {
   free(sp);
 }
 
-bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
-  ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,pszMaxRxLen,(LPDWORD)pszRxLen,NULL);
-  return (*pszRxLen != 0);
+bool uart_receive(const serial_port sp, byte_t *pbtRx, size_t pszMaxRxLen, size_t *pszRxLen) {
+  return ReadFile(((serial_port_windows*)sp)->hPort, pbtRx, pszMaxRxLen, (LPDWORD)pszRxLen, NULL);
 }
 
 bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) {
   DWORD dwTxLen = 0;
-  return WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,szTxLen,&dwTxLen,NULL);
-  return (dwTxLen != 0);
+  return WriteFile(((serial_port_windows*)sp)->hPort, pbtTx, szTxLen, &dwTxLen, NULL);
 }
 
 bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
Impressum, Datenschutz