]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
modify USB communications
authorpwpiwi <pwpiwi@users.noreply.github.com>
Sun, 12 Jan 2020 16:29:07 +0000 (17:29 +0100)
committerpwpiwi <pwpiwi@users.noreply.github.com>
Fri, 17 Jan 2020 08:31:14 +0000 (09:31 +0100)
* use different data types for commands and responses
* use variable length responses
* maintain client/flasher compatibility with old format (e.g. when using old bootloader)
* maintain bootloader compatibility with old format (e.g. when using old or RRG flasher.exe)
* fix length of version string in appmain.c

armsrc/appmain.c
bootrom/bootrom.c
client/cmdsmartcard.c
client/comms.c
client/comms.h
client/flasher.c
client/proxmark3.c
client/scripting.c
common/usb_cdc.c
common/usb_cdc.h
include/usb_cmd.h

index 38965c32ccf78305ac2886ea679cbda434a11e5b..2eb54d27449ef2838ed8e9e93505bd2450b39896 100644 (file)
@@ -346,7 +346,7 @@ void SendVersion(void) {
        // Send Chip ID and used flash memory
        uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
        uint32_t compressed_data_section_size = common_area.arg1;
-       cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), text_and_rodata_section_size + compressed_data_section_size, hw_capabilities, VersionString, strlen(VersionString));
+       cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), text_and_rodata_section_size + compressed_data_section_size, hw_capabilities, VersionString, strlen(VersionString) + 1);
 }
 
 // measure the USB Speed by sending SpeedTestBufferSize bytes to client and measuring the elapsed time.
@@ -1425,7 +1425,7 @@ void UsbPacketReceived(UsbCommand *c) {
                case CMD_DEVICE_INFO: {
                        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;
-                       cmd_send(CMD_DEVICE_INFO,dev_info,0,0,0,0);
+                       cmd_send_old(CMD_DEVICE_INFO,dev_info,0,0,0,0);
                        break;
                }
                default:
@@ -1479,13 +1479,9 @@ void  __attribute__((noreturn)) AppMain(void) {
        UsbCommand rx;
   
        for(;;) {
+               WDT_HIT();
                if (cmd_receive(&rx)) {
                        UsbPacketReceived(&rx);
-               }
-
-               WDT_HIT();
-               if (usb_poll() && (rx_len = usb_read(rx, sizeof(rx)))) {
-                       UsbPacketReceived(rx, rx_len);
                } else {
 #if defined(WITH_LF_StandAlone) && !defined(WITH_ISO14443a_StandAlone)
                        if (BUTTON_HELD(1000) > 0)
index b2500821728daf00d5ddcad767f41867c1ce81db..817421763182776e9669590c6ca7aa4fe56870c3 100644 (file)
@@ -14,7 +14,7 @@ void DbpString(char *str) {
   while (str[len] != 0x00) {
     len++;
   }
-  cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(uint8_t*)str,len);
+  cmd_send_old(CMD_DEBUG_PRINT_STRING,len,0,0,(uint8_t*)str,len);
 }
 
 struct common_area common_area __attribute__((section(".commonarea")));
@@ -96,12 +96,13 @@ void UsbPacketReceived(UsbCommand *c) {
   switch(c->cmd) {
     case CMD_DEVICE_INFO: {
       dont_ack = 1;
-      arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
-      DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
+      arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT 
+               | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
+               | DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
       if(common_area.flags.osimage_present) {
         arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
       }
-      cmd_send(CMD_DEVICE_INFO,arg0,1,2,0,0);
+      cmd_send_old(CMD_DEVICE_INFO,arg0,1,2,0,0);
     } break;
       
     case CMD_SETUP_WRITE: {
@@ -127,7 +128,7 @@ void UsbPacketReceived(UsbCommand *c) {
         if( ((flash_address+AT91C_IFLASH_PAGE_SIZE-1) >= end_addr) || (flash_address < start_addr) ) {
           /* Disallow write */
           dont_ack = 1;
-          cmd_send(CMD_NACK,0,0,0,0,0);
+          cmd_send_old(CMD_NACK,0,0,0,0,0);
         } else {
           uint32_t page_n = (flash_address - ((uint32_t)flash_mem)) / AT91C_IFLASH_PAGE_SIZE;
           /* Translate address to flash page and do flash, update here for the 512k part */
@@ -141,7 +142,7 @@ void UsbPacketReceived(UsbCommand *c) {
         while(!((sr = AT91C_BASE_EFC0->EFC_FSR) & AT91C_MC_FRDY));
         if(sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
           dont_ack = 1;
-          cmd_send(CMD_NACK,0,0,0,0,0);
+          cmd_send_old(CMD_NACK,0,0,0,0,0);
         }
       }
     } break;
@@ -172,7 +173,7 @@ void UsbPacketReceived(UsbCommand *c) {
         } else {
           start_addr = end_addr = 0;
           dont_ack = 1;
-          cmd_send(CMD_NACK,0,0,0,0,0);
+          cmd_send_old(CMD_NACK,0,0,0,0,0);
         }
       }
     } break;
@@ -183,7 +184,7 @@ void UsbPacketReceived(UsbCommand *c) {
   }
   
   if(!dont_ack) {
-    cmd_send(CMD_ACK,arg0,0,0,0,0);
+    cmd_send_old(CMD_ACK,arg0,0,0,0,0);
   }
 }
 
index b0d96f1320f422848b48c74c7d0ba5b55ef188f6..caf33db6a886f76103ae1318f333423d3114486e 100644 (file)
@@ -10,6 +10,7 @@
 #include "cmdsmartcard.h"
 
 #include <ctype.h>
+#include <string.h>
 
 #include "ui.h"
 #include "cmdparser.h"
index 5af53715e9fa27470ca934dae87e968553ae0a2a..d7f4265f9b76dd6f129ad35bbfce6b07d5659c2d 100644 (file)
@@ -12,6 +12,8 @@
 #include "comms.h"
 
 #include <pthread.h>
+#include <inttypes.h>
+
 #if defined(__linux__) && !defined(NO_UNLINK)
 #include <unistd.h>            // for unlink()
 #endif
@@ -45,6 +47,7 @@ static pthread_cond_t txBufferSig = PTHREAD_COND_INITIALIZER;
 
 // Used by UsbReceiveCommand as a ring buffer for messages that are yet to be
 // processed by a command handler (WaitForResponse{,Timeout})
+#define CMD_BUFFER_SIZE 50
 static UsbCommand rxBuffer[CMD_BUFFER_SIZE];
 
 // Points to the next empty position to write to
@@ -187,6 +190,22 @@ static void UsbCommandReceived(UsbCommand *UC)
 }
 
 
+static bool receive_from_serial(serial_port sp, uint8_t *rx_buf, size_t len, size_t *received_len) {
+       size_t bytes_read = 0;
+       *received_len = 0;
+       // we eventually need to call uart_receive several times if it times out in the middle of a transfer
+       while (uart_receive(sp, rx_buf + *received_len, len - *received_len, &bytes_read) && bytes_read && *received_len < len) {
+               if (bytes_read != len - *received_len) {
+                       printf("uart_receive() returned true but not enough bytes could be received. received: %d, wanted to receive: %d, already received before: %d\n",
+                               bytes_read, len - *received_len, *received_len);
+               }
+               *received_len += bytes_read;
+               bytes_read = 0;
+       }
+       return (*received_len == len);
+}
+       
+
 static void
 #ifdef __has_attribute
 #if __has_attribute(force_align_arg_pointer)
@@ -195,29 +214,49 @@ __attribute__((force_align_arg_pointer))
 #endif
 *uart_communication(void *targ) {
        communication_arg_t *conn = (communication_arg_t*)targ;
-       size_t rxlen;
-       UsbCommand rx;
-       UsbCommand *prx = &rx;
+       uint8_t rx[sizeof(UsbCommand)];
+       size_t rxlen = 0;
+       uint8_t *prx = rx;
+       UsbCommand *command = (UsbCommand*)rx;
+       UsbResponse *response = (UsbResponse*)rx;
 
 #if defined(__MACH__) && defined(__APPLE__)
        disableAppNap("Proxmark3 polling UART");
 #endif
 
        while (conn->run) {
-               rxlen = 0;
                bool ACK_received = false;
-               if (uart_receive(sp, (uint8_t *)prx, sizeof(UsbCommand) - (prx-&rx), &rxlen) && rxlen) {
+               prx = rx;
+               size_t bytes_to_read = offsetof(UsbResponse, d);  // the fixed part of a new style UsbResponse. Otherwise this will be cmd and arg[0] (64 bit each)
+               if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
                        prx += rxlen;
-                       if (prx-&rx < sizeof(UsbCommand)) {
-                               continue;
-                       }
-                       UsbCommandReceived(&rx);
-                       if (rx.cmd == CMD_ACK) {
-                               ACK_received = true;
+                       if (response->cmd & CMD_VARIABLE_SIZE_FLAG) { // new style response with variable size
+                               // printf("received new style response %04" PRIx16 ", datalen = %d, arg[0] = %08" PRIx32 ", arg[1] = %08" PRIx32 ", arg[2] = %08" PRIx32 "\n",
+                                       // response->cmd, response->datalen, response->arg[0], response->arg[1], response->arg[2]);
+                               bytes_to_read = response->datalen;
+                               if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
+                                       UsbCommand resp;
+                                       resp.cmd = response->cmd & ~CMD_VARIABLE_SIZE_FLAG;
+                                       resp.arg[0] = response->arg[0];
+                                       resp.arg[1] = response->arg[1];
+                                       resp.arg[2] = response->arg[2];
+                                       memcpy(&resp.d.asBytes, &response->d.asBytes, response->datalen);
+                                       UsbCommandReceived(&resp);
+                                       if (resp.cmd == CMD_ACK) {
+                                               ACK_received = true;
+                                       }
+                               }
+                       } else { // old style response uses same data structure as commands. Fixed size.
+                               // printf("received old style response %016" PRIx64 ", arg[0] = %016" PRIx64 "\n", command->cmd, command->arg[0]);
+                               bytes_to_read = sizeof(UsbCommand) - bytes_to_read;
+                               if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) { 
+                                       UsbCommandReceived(command);
+                                       if (command->cmd == CMD_ACK) {
+                                               ACK_received = true;
+                                       }
+                               }
                        }
                }
-               prx = &rx;
-
                
                pthread_mutex_lock(&txBufferMutex);
 
index 65294695bf500a95a2ec2602ccb1b6585adf472e..c35c4f254ae1ee9393f93c184c5dd8fcd9671a77 100644 (file)
@@ -9,32 +9,22 @@
 // Code for communicating with the proxmark3 hardware.
 //-----------------------------------------------------------------------------
 
-#ifndef COMMS_H_
-#define COMMS_H_
+#ifndef COMMS_H__
+#define COMMS_H__
 
 #include <stdbool.h>
-#include <pthread.h>
-
 #include "usb_cmd.h"
-#include "uart.h"
-
-#ifndef CMD_BUFFER_SIZE
-#define CMD_BUFFER_SIZE 50
-#endif
-
-void SetOffline(bool new_offline);
-bool IsOffline();
-
-bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode);
-void CloseProxmark(void);
-
-void SendCommand(UsbCommand *c);
-
-void clearCommandBuffer();
-bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning);
-bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout);
-bool WaitForResponse(uint32_t cmd, UsbCommand* response);
-bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning);
-bool GetFromFpgaRAM(uint8_t *dest, int bytes);
 
-#endif // COMMS_H_
+extern void SetOffline(bool new_offline);
+extern bool IsOffline();
+extern bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode);
+extern void CloseProxmark(void);
+extern void SendCommand(UsbCommand *c);
+extern void clearCommandBuffer();
+extern bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning);
+extern bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout);
+extern bool WaitForResponse(uint32_t cmd, UsbCommand* response);
+extern bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning);
+extern bool GetFromFpgaRAM(uint8_t *dest, int bytes);
+
+#endif // COMMS_H__
index a008f7bc9d85c82cb3dac55a723e6dbac83e2a04..7691fb3fa7a602433ea3d934d9bac724c8fd75de 100644 (file)
@@ -17,7 +17,7 @@
 #include "flash.h"
 #include "comms.h"
 #include "usb_cmd.h"
-
+#include "uart.h"
 
 void cmd_debug(UsbCommand* UC) {
   //  Debug
index fda9f313b3f6876f5c2fe19a8ccbbcf73af651ce..6b1e23933464901a4d508cb60f24685b26b6b648 100644 (file)
@@ -28,7 +28,7 @@
 #include "cmdhw.h"
 #include "whereami.h"
 #include "comms.h"
-
+#include "uart.h"
 
 void
 #ifdef __has_attribute
index 85a788e97a964233382c193e3f5948113e6e6d0f..1ad5d214dace12714c2108ebfb7aebd7ce0a87d8 100644 (file)
@@ -14,6 +14,7 @@
 #include <lua.h>
 #include <lualib.h>
 #include <lauxlib.h>
+#include <string.h>
 #include "proxmark3.h"
 #include "comms.h"
 #include "usb_cmd.h"
index 66b02e78870b6670b6c125fcc9e0cab625b75187..fa26ea7d8e85323620f8d9ecd16726af0b18ff08 100644 (file)
@@ -705,29 +705,57 @@ bool cmd_receive(UsbCommand* cmd) {
 \r
 \r
 // The function to send a response to the client via USB\r
-bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {\r
-       UsbCommand txcmd;\r
+bool cmd_send(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {\r
+\r
+       UsbResponse txcmd;\r
+\r
+       // Compose the outgoing response frame\r
+       txcmd.cmd = cmd | CMD_VARIABLE_SIZE_FLAG;\r
+       txcmd.arg[0] = arg0;\r
+       txcmd.arg[1] = arg1;\r
+       txcmd.arg[2] = arg2;\r
 \r
-       for (size_t i = 0; i < sizeof(UsbCommand); i++) {\r
-               ((uint8_t*)&txcmd)[i] = 0x00;\r
+       // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
+       if (data) {\r
+               datalen = MIN(datalen, USB_CMD_DATA_SIZE);\r
+               for (uint16_t i = 0; i < datalen; i++) {\r
+                       txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
+               }\r
+               txcmd.datalen = datalen;\r
+       } else {\r
+               txcmd.datalen = 0;\r
        }\r
 \r
-       // Compose the outgoing command frame\r
+       // Send frame and make sure all bytes are transmitted\r
+       size_t tx_size = offsetof(UsbResponse, d) + datalen;\r
+       if (usb_write((uint8_t*)&txcmd, tx_size) != 0) return false;\r
+\r
+       return true;\r
+}\r
+\r
+\r
+// For compatibility only: legacy function to send a response with fixed size to the client via USB\r
+bool cmd_send_old(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {\r
+\r
+       UsbCommand txcmd;\r
+\r
+       // Compose the outgoing response frame\r
        txcmd.cmd = cmd;\r
        txcmd.arg[0] = arg0;\r
        txcmd.arg[1] = arg1;\r
        txcmd.arg[2] = arg2;\r
 \r
        // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
-       if (data && len) {\r
-               len = MIN(len, USB_CMD_DATA_SIZE);\r
-               for (size_t i = 0; i < len; i++) {\r
+       if (data) {\r
+               datalen = MIN(datalen, USB_CMD_DATA_SIZE);\r
+               for (uint16_t i = 0; i < datalen; i++) {\r
                        txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
                }\r
        }\r
-\r
+       \r
        // Send frame and make sure all bytes are transmitted\r
        if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false;\r
 \r
        return true;\r
 }\r
+\r
index 31399222e93e66dbd3e33f28d29469c70a821a16..3fff154dbad54bf2528663a2b03787f8ee8c0bd7 100644 (file)
@@ -45,6 +45,7 @@ extern void usb_enable();
 extern bool usb_poll();\r
 extern bool usb_poll_validate_length();\r
 extern bool cmd_receive(UsbCommand* cmd);\r
-extern bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);\r
+extern bool cmd_send(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen); // new variable sized response\r
+extern bool cmd_send_old(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen); // old fixed size response\r
 \r
 #endif // USB_CDC_H__\r
index 082b7d50175eaff5a98c19c1a0d947d003520c46..a4d164f039ce6c3f0a1b725f472f51b69f1269b6 100644 (file)
@@ -25,6 +25,7 @@ typedef BYTE uint8_t;
 
 #define USB_CMD_DATA_SIZE 512
 
+// the packets sent from client to PM3
 typedef struct {
        uint64_t cmd;
        uint64_t arg[3];
@@ -34,6 +35,16 @@ typedef struct {
        } d;
 } PACKED UsbCommand;
 
+// the packets sent from PM3 to client (a smaller version of UsbCommand)
+typedef struct {
+       uint16_t cmd;
+       uint16_t datalen;
+       uint32_t arg[3];
+       union {
+               uint8_t  asBytes[USB_CMD_DATA_SIZE];
+               uint32_t asDwords[USB_CMD_DATA_SIZE/4];
+       } d;
+} PACKED UsbResponse;
 
 // A struct used to send sample-configs over USB
 typedef struct {
@@ -226,6 +237,7 @@ typedef struct {
 #define CMD_HF_SNIFFER                                                    0x0800
 #define CMD_HF_PLOT                                                       0x0801
 
+#define CMD_VARIABLE_SIZE_FLAG                                            0x8000
 #define CMD_UNKNOWN                                                       0xFFFF
 
 
Impressum, Datenschutz