// 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.
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:
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)
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")));
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: {
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 */
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;
} 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;
}
if(!dont_ack) {
- cmd_send(CMD_ACK,arg0,0,0,0,0);
+ cmd_send_old(CMD_ACK,arg0,0,0,0,0);
}
}
#include "cmdsmartcard.h"
#include <ctype.h>
+#include <string.h>
#include "ui.h"
#include "cmdparser.h"
#include "comms.h"
#include <pthread.h>
+#include <inttypes.h>
+
#if defined(__linux__) && !defined(NO_UNLINK)
#include <unistd.h> // for unlink()
#endif
// 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
}
+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)
#endif
*uart_communication(void *targ) {
communication_arg_t *conn = (communication_arg_t*)targ;
- size_t rxlen;
- UsbCommand rx;
- UsbCommand *prx = ℞
+ 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 = ℞
-
pthread_mutex_lock(&txBufferMutex);
// 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__
#include "flash.h"
#include "comms.h"
#include "usb_cmd.h"
-
+#include "uart.h"
void cmd_debug(UsbCommand* UC) {
// Debug
#include "cmdhw.h"
#include "whereami.h"
#include "comms.h"
-
+#include "uart.h"
void
#ifdef __has_attribute
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
+#include <string.h>
#include "proxmark3.h"
#include "comms.h"
#include "usb_cmd.h"
\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
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
#define USB_CMD_DATA_SIZE 512
+// the packets sent from client to PM3
typedef struct {
uint64_t cmd;
uint64_t arg[3];
} 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 {
#define CMD_HF_SNIFFER 0x0800
#define CMD_HF_PLOT 0x0801
+#define CMD_VARIABLE_SIZE_FLAG 0x8000
#define CMD_UNKNOWN 0xFFFF