mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-19 21:03:23 -07:00
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
This commit is contained in:
parent
867e10a5fd
commit
b8ed9975e5
11 changed files with 133 additions and 64 deletions
|
@ -10,6 +10,7 @@
|
|||
#include "cmdsmartcard.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ui.h"
|
||||
#include "cmdparser.h"
|
||||
|
|
|
@ -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 = ℞
|
||||
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);
|
||||
|
||||
|
|
|
@ -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
|
||||
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);
|
||||
|
||||
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_
|
||||
#endif // COMMS_H__
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include "flash.h"
|
||||
#include "comms.h"
|
||||
#include "usb_cmd.h"
|
||||
|
||||
#include "uart.h"
|
||||
|
||||
void cmd_debug(UsbCommand* UC) {
|
||||
// Debug
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
#include "cmdhw.h"
|
||||
#include "whereami.h"
|
||||
#include "comms.h"
|
||||
|
||||
#include "uart.h"
|
||||
|
||||
void
|
||||
#ifdef __has_attribute
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue