From: pwpiwi <pwpiwi@users.noreply.github.com>
Date: Sun, 12 Jan 2020 16:29:07 +0000 (+0100)
Subject: modify USB communications
X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/b8ed9975e55c91abfd274e17d3414a31d53fab08?ds=sidebyside;hp=-c

modify USB communications
* 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
---

b8ed9975e55c91abfd274e17d3414a31d53fab08
diff --git a/armsrc/appmain.c b/armsrc/appmain.c
index 38965c32..2eb54d27 100644
--- a/armsrc/appmain.c
+++ b/armsrc/appmain.c
@@ -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)
diff --git a/bootrom/bootrom.c b/bootrom/bootrom.c
index b2500821..81742176 100644
--- a/bootrom/bootrom.c
+++ b/bootrom/bootrom.c
@@ -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);
   }
 }
 
diff --git a/client/cmdsmartcard.c b/client/cmdsmartcard.c
index b0d96f13..caf33db6 100644
--- a/client/cmdsmartcard.c
+++ b/client/cmdsmartcard.c
@@ -10,6 +10,7 @@
 #include "cmdsmartcard.h"
 
 #include <ctype.h>
+#include <string.h>
 
 #include "ui.h"
 #include "cmdparser.h"
diff --git a/client/comms.c b/client/comms.c
index 5af53715..d7f4265f 100644
--- a/client/comms.c
+++ b/client/comms.c
@@ -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);
 
diff --git a/client/comms.h b/client/comms.h
index 65294695..c35c4f25 100644
--- a/client/comms.h
+++ b/client/comms.h
@@ -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__
diff --git a/client/flasher.c b/client/flasher.c
index a008f7bc..7691fb3f 100644
--- a/client/flasher.c
+++ b/client/flasher.c
@@ -17,7 +17,7 @@
 #include "flash.h"
 #include "comms.h"
 #include "usb_cmd.h"
-
+#include "uart.h"
 
 void cmd_debug(UsbCommand* UC) {
   //  Debug
diff --git a/client/proxmark3.c b/client/proxmark3.c
index fda9f313..6b1e2393 100644
--- a/client/proxmark3.c
+++ b/client/proxmark3.c
@@ -28,7 +28,7 @@
 #include "cmdhw.h"
 #include "whereami.h"
 #include "comms.h"
-
+#include "uart.h"
 
 void
 #ifdef __has_attribute
diff --git a/client/scripting.c b/client/scripting.c
index 85a788e9..1ad5d214 100644
--- a/client/scripting.c
+++ b/client/scripting.c
@@ -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"
diff --git a/common/usb_cdc.c b/common/usb_cdc.c
index 66b02e78..fa26ea7d 100644
--- a/common/usb_cdc.c
+++ b/common/usb_cdc.c
@@ -705,29 +705,57 @@ bool cmd_receive(UsbCommand* cmd) {
 
 
 // The function to send a response to the client via USB
-bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {
-	UsbCommand txcmd;
+bool cmd_send(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {
+
+	UsbResponse txcmd;
+
+	// Compose the outgoing response frame
+	txcmd.cmd = cmd | CMD_VARIABLE_SIZE_FLAG;
+	txcmd.arg[0] = arg0;
+	txcmd.arg[1] = arg1;
+	txcmd.arg[2] = arg2;
 
-	for (size_t i = 0; i < sizeof(UsbCommand); i++) {
-		((uint8_t*)&txcmd)[i] = 0x00;
+	// Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE
+	if (data) {
+		datalen = MIN(datalen, USB_CMD_DATA_SIZE);
+		for (uint16_t i = 0; i < datalen; i++) {
+			txcmd.d.asBytes[i] = ((uint8_t*)data)[i];
+		}
+		txcmd.datalen = datalen;
+	} else {
+		txcmd.datalen = 0;
 	}
 
-	// Compose the outgoing command frame
+	// Send frame and make sure all bytes are transmitted
+	size_t tx_size = offsetof(UsbResponse, d) + datalen;
+	if (usb_write((uint8_t*)&txcmd, tx_size) != 0) return false;
+
+	return true;
+}
+
+
+// For compatibility only: legacy function to send a response with fixed size to the client via USB
+bool cmd_send_old(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {
+
+	UsbCommand txcmd;
+
+	// Compose the outgoing response frame
 	txcmd.cmd = cmd;
 	txcmd.arg[0] = arg0;
 	txcmd.arg[1] = arg1;
 	txcmd.arg[2] = arg2;
 
 	// Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE
-	if (data && len) {
-		len = MIN(len, USB_CMD_DATA_SIZE);
-		for (size_t i = 0; i < len; i++) {
+	if (data) {
+		datalen = MIN(datalen, USB_CMD_DATA_SIZE);
+		for (uint16_t i = 0; i < datalen; i++) {
 			txcmd.d.asBytes[i] = ((uint8_t*)data)[i];
 		}
 	}
-
+	
 	// Send frame and make sure all bytes are transmitted
 	if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false;
 
 	return true;
 }
+
diff --git a/common/usb_cdc.h b/common/usb_cdc.h
index 31399222..3fff154d 100644
--- a/common/usb_cdc.h
+++ b/common/usb_cdc.h
@@ -45,6 +45,7 @@ extern void usb_enable();
 extern bool usb_poll();
 extern bool usb_poll_validate_length();
 extern bool cmd_receive(UsbCommand* cmd);
-extern bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);
+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
+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
 
 #endif // USB_CDC_H__
diff --git a/include/usb_cmd.h b/include/usb_cmd.h
index 082b7d50..a4d164f0 100644
--- a/include/usb_cmd.h
+++ b/include/usb_cmd.h
@@ -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