#include "comms.h"
#include <pthread.h>
+#include <inttypes.h>
+
#if defined(__linux__) && !defined(NO_UNLINK)
#include <unistd.h> // for unlink()
#endif
#include "uart.h"
#include "ui.h"
#include "common.h"
+#include "util_darwin.h"
#include "util_posix.h"
// 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);
pthread_mutex_unlock(&txBufferMutex);
}
+#if defined(__MACH__) && defined(__APPLE__)
+ enableAppNap();
+#endif
+
pthread_exit(NULL);
return NULL;
}
}
+bool GetFromFpgaRAM(uint8_t *dest, int bytes)
+{
+ UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
+ SendCommand(&c);
+
+ uint64_t start_time = msclock();
+
+ UsbCommand response;
+
+ int bytes_completed = 0;
+ bool show_warning = true;
+ while(true) {
+ if (getCommand(&response)) {
+ if (response.cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
+ int copy_bytes = MIN(bytes - bytes_completed, response.arg[1]);
+ memcpy(dest + response.arg[0], response.d.asBytes, copy_bytes);
+ bytes_completed += copy_bytes;
+ } else if (response.cmd == CMD_ACK) {
+ return true;
+ }
+ }
+
+ if (msclock() - start_time > 2000 && show_warning) {
+ PrintAndLog("Waiting for a response from the proxmark...");
+ PrintAndLog("You can cancel this operation by pressing the pm3 button");
+ show_warning = false;
+ }
+ }
+
+ return false;
+}
+
+
bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode) {
char *portname = (char *)port;
if (!wait_for_port) {
// Clean up our state
sp = NULL;
serial_port_name = NULL;
+#ifdef __BIONIC__
memset(&USB_communication_thread, 0, sizeof(pthread_t));
+#endif
}