1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Code for communicating with the proxmark3 hardware.
17 //-----------------------------------------------------------------------------
26 #include "uart/uart.h"
29 #include "util.h" // g_pendingPrompt
30 #include "util_posix.h" // msclock
31 #include "util_darwin.h" // en/dis-ableNapp();
32 #include "usart_defs.h"
34 // #define COMMS_DEBUG
35 // #define COMMS_DEBUG_RAW
37 // Serial port that we are communicating with the PM3 on.
38 static serial_port sp
= NULL
;
40 communication_arg_t g_conn
;
41 capabilities_t g_pm3_capabilities
;
43 static pthread_t communication_thread
;
44 static pthread_t reconnect_thread
;
46 static bool reconnect_ok
= false;
48 static bool comm_thread_dead
= false;
49 static bool comm_raw_mode
= false;
50 static uint8_t *comm_raw_data
= NULL
;
51 static size_t comm_raw_len
= 0;
52 static size_t comm_raw_pos
= 0;
55 static PacketCommandOLD txBuffer
;
56 static PacketCommandNGRaw txBufferNG
;
57 static size_t txBufferNGLen
;
58 static bool txBuffer_pending
= false;
59 static pthread_mutex_t txBufferMutex
= PTHREAD_MUTEX_INITIALIZER
;
60 static pthread_cond_t txBufferSig
= PTHREAD_COND_INITIALIZER
;
62 // Used by PacketResponseReceived as a ring buffer for messages that are yet to be
63 // processed by a command handler (WaitForResponse{,Timeout})
64 static PacketResponseNG rxBuffer
[CMD_BUFFER_SIZE
];
66 // Points to the next empty position to write to
67 static int cmd_head
= 0;
69 // Points to the position of the last unread command
70 static int cmd_tail
= 0;
72 // to lock rxBuffer operations from different threads
73 static pthread_mutex_t rxBufferMutex
= PTHREAD_MUTEX_INITIALIZER
;
75 // Global start time for WaitForResponseTimeout & dl_it, so we can reset timeout when we get packets
76 // as sending lot of these packets can slow down things wuite a lot on slow links (e.g. hw status or lf read at 9600)
77 static uint64_t timeout_start_time
;
79 static uint64_t last_packet_time
;
81 static bool dl_it(uint8_t *dest
, uint32_t bytes
, PacketResponseNG
*response
, size_t ms_timeout
, bool show_warning
, uint32_t rec_cmd
);
83 // Simple alias to track usages linked to the Bootloader, these commands must not be migrated.
84 // - commands sent to enter bootloader mode as we might have to talk to old firmwares
85 // - commands sent to the bootloader as it only supports OLD frames (which will always be the case for old BL)
86 void SendCommandBL(uint64_t cmd
, uint64_t arg0
, uint64_t arg1
, uint64_t arg2
, void *data
, size_t len
) {
87 SendCommandOLD(cmd
, arg0
, arg1
, arg2
, data
, len
);
90 void SendCommandOLD(uint64_t cmd
, uint64_t arg0
, uint64_t arg1
, uint64_t arg2
, const void *data
, size_t len
) {
91 PacketCommandOLD c
= {CMD_UNKNOWN
, {0, 0, 0}, {{0}}};
97 memcpy(&c
.d
, data
, len
);
101 PrintAndLogEx(NORMAL
, "Sending %s", "OLD");
103 #ifdef COMMS_DEBUG_RAW
104 print_hex_break((uint8_t *)&c
.cmd
, sizeof(c
.cmd
), 32);
105 print_hex_break((uint8_t *)&c
.arg
, sizeof(c
.arg
), 32);
106 print_hex_break((uint8_t *)&c
.d
, sizeof(c
.d
), 32);
109 if (g_session
.pm3_present
== false) {
110 PrintAndLogEx(WARNING
, "Sending bytes to Proxmark3 failed ( " _RED_("offline") " )");
114 pthread_mutex_lock(&txBufferMutex
);
116 This causes hangups at times, when the pm3 unit is unresponsive or disconnected. The main console thread is alive,
117 but comm thread just spins here. Not good.../holiman
119 while (txBuffer_pending
) {
120 // wait for communication thread to complete sending a previous command
121 pthread_cond_wait(&txBufferSig
, &txBufferMutex
);
125 txBuffer_pending
= true;
127 // tell communication thread that a new command can be send
128 pthread_cond_signal(&txBufferSig
);
130 pthread_mutex_unlock(&txBufferMutex
);
132 //__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST);
135 static void SendCommandNG_internal(uint16_t cmd
, uint8_t *data
, size_t len
, bool ng
) {
137 PrintAndLogEx(INFO
, "Sending %s", ng
? "NG" : "MIX");
140 if (!g_session
.pm3_present
) {
141 PrintAndLogEx(INFO
, "Sending bytes to proxmark failed - offline");
144 if (len
> PM3_CMD_DATA_SIZE
) {
145 PrintAndLogEx(WARNING
, "Sending %zu bytes of payload is too much, abort", len
);
149 PacketCommandNGPostamble
*tx_post
= (PacketCommandNGPostamble
*)((uint8_t *)&txBufferNG
+ sizeof(PacketCommandNGPreamble
) + len
);
151 pthread_mutex_lock(&txBufferMutex
);
153 This causes hangups at times, when the pm3 unit is unresponsive or disconnected. The main console thread is alive,
154 but comm thread just spins here. Not good.../holiman
156 while (txBuffer_pending
) {
157 // wait for communication thread to complete sending a previous command
158 pthread_cond_wait(&txBufferSig
, &txBufferMutex
);
161 txBufferNG
.pre
.magic
= COMMANDNG_PREAMBLE_MAGIC
;
162 txBufferNG
.pre
.ng
= ng
;
163 txBufferNG
.pre
.length
= len
;
164 txBufferNG
.pre
.cmd
= cmd
;
165 if (len
> 0 && data
) {
166 memcpy(&txBufferNG
.data
, data
, len
);
169 if ((g_conn
.send_via_fpc_usart
&& g_conn
.send_with_crc_on_fpc
) || ((!g_conn
.send_via_fpc_usart
) && g_conn
.send_with_crc_on_usb
)) {
170 uint8_t first
= 0, second
= 0;
171 compute_crc(CRC_14443_A
, (uint8_t *)&txBufferNG
, sizeof(PacketCommandNGPreamble
) + len
, &first
, &second
);
172 tx_post
->crc
= (first
<< 8) + second
;
174 tx_post
->crc
= COMMANDNG_POSTAMBLE_MAGIC
;
177 txBufferNGLen
= sizeof(PacketCommandNGPreamble
) + len
+ sizeof(PacketCommandNGPostamble
);
179 #ifdef COMMS_DEBUG_RAW
180 print_hex_break((uint8_t *)&txBufferNG
.pre
, sizeof(PacketCommandNGPreamble
), 32);
182 print_hex_break((uint8_t *)&txBufferNG
.data
, len
, 32);
184 print_hex_break((uint8_t *)&txBufferNG
.data
, 3 * sizeof(uint64_t), 32);
185 print_hex_break((uint8_t *)&txBufferNG
.data
+ 3 * sizeof(uint64_t), len
- 3 * sizeof(uint64_t), 32);
187 print_hex_break((uint8_t *)tx_post
, sizeof(PacketCommandNGPostamble
), 32);
189 txBuffer_pending
= true;
191 // tell communication thread that a new command can be send
192 pthread_cond_signal(&txBufferSig
);
194 pthread_mutex_unlock(&txBufferMutex
);
196 //__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST);
199 void SendCommandNG(uint16_t cmd
, uint8_t *data
, size_t len
) {
200 SendCommandNG_internal(cmd
, data
, len
, true);
203 void SendCommandMIX(uint64_t cmd
, uint64_t arg0
, uint64_t arg1
, uint64_t arg2
, const void *data
, size_t len
) {
204 uint64_t arg
[3] = {arg0
, arg1
, arg2
};
205 if (len
> PM3_CMD_DATA_SIZE_MIX
) {
206 PrintAndLogEx(WARNING
, "Sending %zu bytes of payload is too much for MIX frames, abort", len
);
209 uint8_t cmddata
[PM3_CMD_DATA_SIZE
];
210 memcpy(cmddata
, arg
, sizeof(arg
));
212 memcpy(cmddata
+ sizeof(arg
), data
, len
);
213 SendCommandNG_internal(cmd
, cmddata
, len
+ sizeof(arg
), false);
218 * @brief This method should be called when sending a new command to the pm3. In case any old
219 * responses from previous commands are stored in the buffer, a call to this method should clear them.
220 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
221 * operation. Right now we'll just have to live with this.
223 void clearCommandBuffer(void) {
224 //This is a very simple operation
225 pthread_mutex_lock(&rxBufferMutex
);
227 pthread_mutex_unlock(&rxBufferMutex
);
230 * @brief storeCommand stores a USB command in a circular buffer
233 static void storeReply(const PacketResponseNG
*packet
) {
234 pthread_mutex_lock(&rxBufferMutex
);
235 if ((cmd_head
+ 1) % CMD_BUFFER_SIZE
== cmd_tail
) {
236 //If these two are equal, we're about to overwrite in the
238 PrintAndLogEx(FAILED
, "WARNING: Command buffer about to overwrite command! This needs to be fixed!");
241 //Store the command at the 'head' location
242 PacketResponseNG
*destination
= &rxBuffer
[cmd_head
];
243 memcpy(destination
, packet
, sizeof(PacketResponseNG
));
245 //increment head and wrap
246 cmd_head
= (cmd_head
+ 1) % CMD_BUFFER_SIZE
;
247 pthread_mutex_unlock(&rxBufferMutex
);
250 * @brief getCommand gets a command from an internal circular buffer.
251 * @param response location to write command
252 * @return 1 if response was returned, 0 if nothing has been received
254 static int getReply(PacketResponseNG
*packet
) {
255 pthread_mutex_lock(&rxBufferMutex
);
256 //If head == tail, there's nothing to read, or if we just got initialized
257 if (cmd_head
== cmd_tail
) {
258 pthread_mutex_unlock(&rxBufferMutex
);
262 //Pick out the next unread command
263 memcpy(packet
, &rxBuffer
[cmd_tail
], sizeof(PacketResponseNG
));
265 //Increment tail - this is a circular buffer, so modulo buffer size
266 cmd_tail
= (cmd_tail
+ 1) % CMD_BUFFER_SIZE
;
268 pthread_mutex_unlock(&rxBufferMutex
);
272 //-----------------------------------------------------------------------------
273 // Entry point into our code: called whenever we received a packet over USB
274 // that we weren't necessarily expecting, for example a debug print.
275 //-----------------------------------------------------------------------------
276 static void PacketResponseReceived(PacketResponseNG
*packet
) {
278 // we got a packet, reset WaitForResponseTimeout timeout
279 uint64_t prev_clk
= __atomic_load_n(&last_packet_time
, __ATOMIC_SEQ_CST
);
280 uint64_t clk
= msclock();
281 __atomic_store_n(&timeout_start_time
, clk
, __ATOMIC_SEQ_CST
);
282 __atomic_store_n(&last_packet_time
, clk
, __ATOMIC_SEQ_CST
);
284 // PrintAndLogEx(NORMAL, "[%07"PRIu64"] RECV %s magic %08x length %04x status %04x crc %04x cmd %04x",
285 // clk - prev_clk, packet->ng ? "NG" : "OLD", packet->magic, packet->length, packet->status, packet->crc, packet->cmd);
287 switch (packet
->cmd
) {
288 // First check if we are handling a debug message
289 case CMD_DEBUG_PRINT_STRING
: {
291 char s
[PM3_CMD_DATA_SIZE
+ 1];
292 memset(s
, 0x00, sizeof(s
));
299 uint8_t buf
[PM3_CMD_DATA_SIZE
- sizeof(uint16_t)];
301 const struct d
*data
= (struct d
*)&packet
->data
.asBytes
;
302 len
= packet
->length
- sizeof(data
->flag
);
304 memcpy(s
, data
->buf
, len
);
306 len
= MIN(packet
->oldarg
[0], PM3_CMD_DATA_SIZE
);
307 flag
= packet
->oldarg
[1];
308 memcpy(s
, packet
->data
.asBytes
, len
);
311 if (flag
& FLAG_LOG
) {
312 if (g_pendingPrompt
) {
313 PrintAndLogEx(NORMAL
, "");
314 g_pendingPrompt
= false;
316 //PrintAndLogEx(NORMAL, "[" _MAGENTA_("pm3") "] ["_BLUE_("#")"] " "%s", s);
317 PrintAndLogEx(NORMAL
, "[" _BLUE_("#") "] %s", s
);
319 if (flag
& FLAG_INPLACE
) {
320 PrintAndLogEx(NORMAL
, "\r" NOLF
);
323 PrintAndLogEx(NORMAL
, "%s" NOLF
, s
);
325 if (flag
& FLAG_NEWLINE
) {
326 PrintAndLogEx(NORMAL
, "");
331 case CMD_DEBUG_PRINT_INTEGERS
: {
332 if (packet
->ng
== false) {
333 PrintAndLogEx(NORMAL
, "[" _MAGENTA_("pm3") "] ["_BLUE_("#")"] " "%" PRIx64
", %" PRIx64
", %" PRIx64
""
341 // iceman: hw status - down the path on device, runs printusbspeed which starts sending a lot of
342 // CMD_DOWNLOAD_BIGBUF packages which is not dealt with. I wonder if simply ignoring them will
343 // work. lets try it.
351 // The reconnect device thread.
352 // When communication thread is dead, start up and try to start it again
353 void *uart_reconnect(void *targ
) {
355 const communication_arg_t
*connection
= (communication_arg_t
*)targ
;
357 #if defined(__MACH__) && defined(__APPLE__)
358 disableAppNap("Proxmark3 polling UART");
361 uint32_t speed
= USART_BAUD_RATE
;
362 if (connection
->uart_speed
) {
363 speed
= connection
->uart_speed
;
369 if (OpenProxmarkSilent(&g_session
.current_device
, connection
->serial_port_name
, speed
) == false) {
373 if (g_session
.pm3_present
&& (TestProxmark(g_session
.current_device
) != PM3_SUCCESS
)) {
374 CloseProxmark(g_session
.current_device
);
381 #if defined(__MACH__) && defined(__APPLE__)
385 __atomic_test_and_set(&reconnect_ok
, __ATOMIC_SEQ_CST
);
391 void StartReconnectProxmark(void) {
392 pthread_create(&reconnect_thread
, NULL
, &uart_reconnect
, &g_conn
);
395 bool IsReconnectedOk(void) {
396 bool ret
= __atomic_load_n(&reconnect_ok
, __ATOMIC_SEQ_CST
);
400 // The communications thread.
401 // signals to main thread when a response is ready to process.
404 #ifdef __has_attribute
405 #if __has_attribute(force_align_arg_pointer)
406 __attribute__((force_align_arg_pointer
))
409 *uart_communication(void *targ
) {
410 const communication_arg_t
*connection
= (communication_arg_t
*)targ
;
412 bool commfailed
= false;
414 PacketResponseNGRaw rx_raw
;
415 // Stash the last state of is_receiving_raw, to detect if state changed
416 bool is_receiving_raw_last
= false;
418 #if defined(__MACH__) && defined(__APPLE__)
419 disableAppNap("Proxmark3 polling UART");
422 // is this connection->run a cross thread call?
423 while (connection
->run
) {
425 bool ACK_received
= false;
429 // Signal to main thread that communications seems off.
430 // main thread will kill and restart this thread.
432 if (g_conn
.last_command
!= CMD_HARDWARE_RESET
&&
433 g_conn
.last_command
!= CMD_START_FLASH
) {
434 PrintAndLogEx(WARNING
, "\nCommunicating with Proxmark3 device " _RED_("failed"));
436 __atomic_test_and_set(&comm_thread_dead
, __ATOMIC_SEQ_CST
);
440 bool is_receiving_raw
= __atomic_load_n(&comm_raw_mode
, __ATOMIC_SEQ_CST
);
442 if (is_receiving_raw
) {
443 uint8_t *bufferData
= __atomic_load_n(&comm_raw_data
, __ATOMIC_SEQ_CST
); // read only
444 size_t bufferLen
= __atomic_load_n(&comm_raw_len
, __ATOMIC_SEQ_CST
); // read only
445 size_t bufferPos
= __atomic_load_n(&comm_raw_pos
, __ATOMIC_SEQ_CST
); // read and write
446 if (bufferPos
< bufferLen
) {
447 size_t rxMaxLen
= bufferLen
- bufferPos
;
449 rxMaxLen
= MIN(COMM_RAW_RECEIVE_LEN
, rxMaxLen
);
451 res
= uart_receive(sp
, bufferData
+ bufferPos
, rxMaxLen
, &rxlen
);
452 if (res
== PM3_SUCCESS
) {
453 uint64_t clk
= msclock();
454 __atomic_store_n(&timeout_start_time
, clk
, __ATOMIC_SEQ_CST
);
455 __atomic_store_n(&comm_raw_pos
, bufferPos
+ rxlen
, __ATOMIC_SEQ_CST
);
456 } else if (res
!= PM3_ENODATA
) {
457 PrintAndLogEx(WARNING
, "Error when reading raw data: %zu/%zu, %d", bufferPos
, bufferLen
, res
);
459 if (res
== PM3_ENOTTY
) {
464 // Ignore data when bufferPos >= bufferLen and is_receiving_raw has not been set to false
465 uint8_t dummyData
[64];
467 uart_receive(sp
, dummyData
, sizeof(dummyData
), &dummyLen
);
470 if (is_receiving_raw_last
) {
471 // is_receiving_raw changed from true to false
473 // Set the buffer as undefined
474 // comm_raw_data == NULL is used in SetCommunicationReceiveMode()
475 __atomic_store_n(&comm_raw_data
, NULL
, __ATOMIC_SEQ_CST
);
477 res
= uart_receive(sp
, (uint8_t *)&rx_raw
.pre
, sizeof(PacketResponseNGPreamble
), &rxlen
);
479 if ((res
== PM3_SUCCESS
) && (rxlen
== sizeof(PacketResponseNGPreamble
))) {
481 rx
.magic
= rx_raw
.pre
.magic
;
482 uint16_t length
= rx_raw
.pre
.length
;
483 rx
.ng
= rx_raw
.pre
.ng
;
484 rx
.status
= rx_raw
.pre
.status
;
485 rx
.reason
= rx_raw
.pre
.reason
;
486 rx
.cmd
= rx_raw
.pre
.cmd
;
488 if (rx
.magic
== RESPONSENG_PREAMBLE_MAGIC
) { // New style NG reply
490 if (length
> PM3_CMD_DATA_SIZE
) {
491 PrintAndLogEx(WARNING
, "Received packet frame with incompatible length: 0x%04x", length
);
495 if ((!error
) && (length
> 0)) { // Get the variable length payload
497 res
= uart_receive(sp
, (uint8_t *)&rx_raw
.data
, length
, &rxlen
);
499 if ((res
!= PM3_SUCCESS
) || (rxlen
!= length
)) {
501 PrintAndLogEx(WARNING
, "Received packet frame with variable part too short? %d/%d", rxlen
, length
);
506 if (rx
.ng
) { // Received a valid NG frame
508 memcpy(&rx
.data
, &rx_raw
.data
, length
);
510 if ((rx
.cmd
== g_conn
.last_command
) && (rx
.status
== PM3_SUCCESS
)) {
516 if (length
< sizeof(arg
)) {
517 PrintAndLogEx(WARNING
, "Received MIX packet frame with incompatible length: 0x%04x", length
);
521 if (!error
) { // Received a valid MIX frame
523 memcpy(arg
, &rx_raw
.data
, sizeof(arg
));
524 rx
.oldarg
[0] = arg
[0];
525 rx
.oldarg
[1] = arg
[1];
526 rx
.oldarg
[2] = arg
[2];
527 memcpy(&rx
.data
, ((uint8_t *)&rx_raw
.data
) + sizeof(arg
), length
- sizeof(arg
));
528 rx
.length
= length
- sizeof(arg
);
530 if (rx
.cmd
== CMD_ACK
) {
536 } else if ((!error
) && (length
== 0)) { // we received an empty frame
539 rx
.length
= 0; // set received length to 0
540 } else { // old frames can't be empty
541 PrintAndLogEx(WARNING
, "Received empty MIX packet frame (length: 0x00)");
547 if (!error
) { // Get the postamble
548 res
= uart_receive(sp
, (uint8_t *)&rx_raw
.foopost
, sizeof(PacketResponseNGPostamble
), &rxlen
);
549 if ((res
!= PM3_SUCCESS
) || (rxlen
!= sizeof(PacketResponseNGPostamble
))) {
550 PrintAndLogEx(WARNING
, "Received packet frame without postamble");
555 if (!error
) { // Check CRC, accept MAGIC as placeholder
556 rx
.crc
= rx_raw
.foopost
.crc
;
558 if (rx
.crc
!= RESPONSENG_POSTAMBLE_MAGIC
) {
560 uint8_t first
, second
;
561 compute_crc(CRC_14443_A
, (uint8_t *)&rx_raw
, sizeof(PacketResponseNGPreamble
) + length
, &first
, &second
);
563 if ((first
<< 8) + second
!= rx
.crc
) {
564 PrintAndLogEx(WARNING
, "Received packet frame with invalid CRC %02X%02X <> %04X", first
, second
, rx
.crc
);
569 if (!error
) { // Received a valid OLD frame
571 PrintAndLogEx(NORMAL
, "Receiving %s:", rx
.ng
? "NG" : "MIX");
573 #ifdef COMMS_DEBUG_RAW
574 print_hex_break((uint8_t *)&rx_raw
.pre
, sizeof(PacketResponseNGPreamble
), 32);
575 print_hex_break((uint8_t *)&rx_raw
.data
, rx_raw
.pre
.length
, 32);
576 print_hex_break((uint8_t *)&rx_raw
.foopost
, sizeof(PacketResponseNGPostamble
), 32);
578 PacketResponseReceived(&rx
);
580 } else { // Old style reply
581 PacketResponseOLD rx_old
;
582 memcpy(&rx_old
, &rx_raw
.pre
, sizeof(PacketResponseNGPreamble
));
584 res
= uart_receive(sp
, ((uint8_t *)&rx_old
) + sizeof(PacketResponseNGPreamble
), sizeof(PacketResponseOLD
) - sizeof(PacketResponseNGPreamble
), &rxlen
);
585 if ((res
!= PM3_SUCCESS
) || (rxlen
!= sizeof(PacketResponseOLD
) - sizeof(PacketResponseNGPreamble
))) {
586 PrintAndLogEx(WARNING
, "Received packet OLD frame with payload too short? %d/%zu", rxlen
, sizeof(PacketResponseOLD
) - sizeof(PacketResponseNGPreamble
));
591 PrintAndLogEx(NORMAL
, "Receiving OLD:");
593 #ifdef COMMS_DEBUG_RAW
594 print_hex_break((uint8_t *)&rx_old
.cmd
, sizeof(rx_old
.cmd
), 32);
595 print_hex_break((uint8_t *)&rx_old
.arg
, sizeof(rx_old
.arg
), 32);
596 print_hex_break((uint8_t *)&rx_old
.d
, sizeof(rx_old
.d
), 32);
603 rx
.oldarg
[0] = rx_old
.arg
[0];
604 rx
.oldarg
[1] = rx_old
.arg
[1];
605 rx
.oldarg
[2] = rx_old
.arg
[2];
606 rx
.length
= PM3_CMD_DATA_SIZE
;
607 memcpy(&rx
.data
, &rx_old
.d
, rx
.length
);
608 PacketResponseReceived(&rx
);
609 if (rx
.cmd
== CMD_ACK
) {
616 PrintAndLogEx(WARNING
, "Received packet frame preamble too short: %d/%zu", rxlen
, sizeof(PacketResponseNGPreamble
));
619 if (res
== PM3_ENOTTY
) {
625 is_receiving_raw_last
= is_receiving_raw
;
626 // TODO if error, shall we resync ?
628 pthread_mutex_lock(&txBufferMutex
);
630 if (connection
->block_after_ACK
) {
631 // if we just received an ACK, wait here until a new command is to be transmitted
632 // This is only working on OLD frames, and only used by flasher and flashmem
635 PrintAndLogEx(NORMAL
, "Received ACK, fast TX mode: ignoring other RX till TX");
637 while (!txBuffer_pending
) {
638 pthread_cond_wait(&txBufferSig
, &txBufferMutex
);
643 if (txBuffer_pending
) {
645 if (txBufferNGLen
) { // NG packet
646 res
= uart_send(sp
, (uint8_t *) &txBufferNG
, txBufferNGLen
);
647 if (res
== PM3_EIO
) {
650 g_conn
.last_command
= txBufferNG
.pre
.cmd
;
653 res
= uart_send(sp
, (uint8_t *) &txBuffer
, sizeof(PacketCommandOLD
));
654 if (res
== PM3_EIO
) {
657 g_conn
.last_command
= txBuffer
.cmd
;
660 txBuffer_pending
= false;
662 // main thread doesn't know send failed...
664 // tell main thread that txBuffer is empty
665 pthread_cond_signal(&txBufferSig
);
668 pthread_mutex_unlock(&txBufferMutex
);
671 // when thread dies, we close the serial port.
675 #if defined(__MACH__) && defined(__APPLE__)
683 bool IsCommunicationThreadDead(void) {
684 bool ret
= __atomic_load_n(&comm_thread_dead
, __ATOMIC_SEQ_CST
);
690 // To start raw receive mode:
691 // 1. Call SetCommunicationRawReceiveBuffer(...)
692 // 2. Call SetCommunicationReceiveMode(true)
694 // To stop raw receive mode:
695 // Call SetCommunicationReceiveMode(false)
698 // 1. The receiving thread won't accept any normal packets after calling
699 // SetCommunicationReceiveMode(true). You need to call
700 // SetCommunicationReceiveMode(false) to stop the raw receiving process.
701 // 2. If the received size >= len used in SetCommunicationRawReceiveBuffer(),
702 // The receiving thread will ignore the incoming data to prevent overflow.
703 // 3. Normally you only need WaitForRawDataTimeout() rather than the
704 // low level functions like SetCommunicationReceiveMode(),
705 // SetCommunicationRawReceiveBuffer() and GetCommunicationRawReceiveNum()
707 bool SetCommunicationReceiveMode(bool isRawMode
) {
709 const uint8_t *buffer
= __atomic_load_n(&comm_raw_data
, __ATOMIC_SEQ_CST
);
710 if (buffer
== NULL
) {
711 PrintAndLogEx(ERR
, "Buffer for raw data is not set");
715 __atomic_store_n(&comm_raw_mode
, isRawMode
, __ATOMIC_SEQ_CST
);
719 void SetCommunicationRawReceiveBuffer(uint8_t *buffer
, size_t len
) {
720 __atomic_store_n(&comm_raw_data
, buffer
, __ATOMIC_SEQ_CST
);
721 __atomic_store_n(&comm_raw_len
, len
, __ATOMIC_SEQ_CST
);
722 __atomic_store_n(&comm_raw_pos
, 0, __ATOMIC_SEQ_CST
);
725 size_t GetCommunicationRawReceiveNum(void) {
726 return __atomic_load_n(&comm_raw_pos
, __ATOMIC_SEQ_CST
);
729 bool OpenProxmarkSilent(pm3_device_t
**dev
, const char *port
, uint32_t speed
) {
731 sp
= uart_open(port
, speed
, true);
733 // check result of uart opening
734 if (sp
== INVALID_SERIAL_PORT
) {
737 } else if (sp
== CLAIMED_SERIAL_PORT
) {
741 // start the communication thread
742 if (port
!= g_conn
.serial_port_name
) {
743 uint16_t len
= MIN(strlen(port
), FILE_PATH_SIZE
- 1);
744 memset(g_conn
.serial_port_name
, 0, FILE_PATH_SIZE
);
745 memcpy(g_conn
.serial_port_name
, port
, len
);
748 g_conn
.block_after_ACK
= false;
749 // Flags to tell where to add CRC on sent replies
750 g_conn
.send_with_crc_on_usb
= false;
751 g_conn
.send_with_crc_on_fpc
= true;
752 // "Session" flag, to tell via which interface next msgs should be sent: USB or FPC USART
753 g_conn
.send_via_fpc_usart
= false;
755 pthread_create(&communication_thread
, NULL
, &uart_communication
, &g_conn
);
756 __atomic_clear(&comm_thread_dead
, __ATOMIC_SEQ_CST
);
757 __atomic_clear(&reconnect_ok
, __ATOMIC_SEQ_CST
);
759 g_session
.pm3_present
= true; // TODO support for multiple devices
763 *dev
= calloc(sizeof(pm3_device_t
), sizeof(uint8_t));
765 (*dev
)->g_conn
= &g_conn
; // TODO g_conn shouldn't be global
770 bool OpenProxmark(pm3_device_t
**dev
, const char *port
, bool wait_for_port
, int timeout
, bool flash_mode
, uint32_t speed
) {
772 if (wait_for_port
== false) {
773 PrintAndLogEx(SUCCESS
, "Using UART port " _GREEN_("%s"), port
);
774 sp
= uart_open(port
, speed
, false);
776 PrintAndLogEx(SUCCESS
, "Waiting for Proxmark3 to appear on " _YELLOW_("%s"), port
);
779 PrintAndLogEx(INPLACE
, "% 3i", timeout
);
781 sp
= uart_open(port
, speed
, false);
783 PrintAndLogEx(INPLACE
, "% 3i", timeout
- openCount
- 1);
785 } while (++openCount
< timeout
&& (sp
== INVALID_SERIAL_PORT
|| sp
== CLAIMED_SERIAL_PORT
));
788 // check result of uart opening
789 if (sp
== INVALID_SERIAL_PORT
) {
790 PrintAndLogEx(WARNING
, "\n" _RED_("ERROR:") " invalid serial port " _YELLOW_("%s"), port
);
791 PrintAndLogEx(HINT
, "Try the shell script " _YELLOW_("`./pm3 --list`") " to get a list of possible serial ports");
794 } else if (sp
== CLAIMED_SERIAL_PORT
) {
795 PrintAndLogEx(WARNING
, "\n" _RED_("ERROR:") " serial port " _YELLOW_("%s") " is claimed by another process", port
);
796 PrintAndLogEx(HINT
, "Try the shell script " _YELLOW_("`./pm3 --list`") " to get a list of possible serial ports");
801 // start the communication thread
802 if (port
!= g_conn
.serial_port_name
) {
803 uint16_t len
= MIN(strlen(port
), FILE_PATH_SIZE
- 1);
804 memset(g_conn
.serial_port_name
, 0, FILE_PATH_SIZE
);
805 memcpy(g_conn
.serial_port_name
, port
, len
);
808 g_conn
.block_after_ACK
= flash_mode
;
809 // Flags to tell where to add CRC on sent replies
810 g_conn
.send_with_crc_on_usb
= false;
811 g_conn
.send_with_crc_on_fpc
= true;
812 // "Session" flag, to tell via which interface next msgs should be sent: USB or FPC USART
813 g_conn
.send_via_fpc_usart
= false;
815 pthread_create(&communication_thread
, NULL
, &uart_communication
, &g_conn
);
816 __atomic_clear(&comm_thread_dead
, __ATOMIC_SEQ_CST
);
817 g_session
.pm3_present
= true; // TODO support for multiple devices
821 *dev
= calloc(sizeof(pm3_device_t
), sizeof(uint8_t));
823 (*dev
)->g_conn
= &g_conn
; // TODO g_conn shouldn't be global
828 // check if we can communicate with Pm3
829 int TestProxmark(pm3_device_t
*dev
) {
833 for (uint16_t i
= 0; i
< len
; i
++) {
837 __atomic_store_n(&last_packet_time
, msclock(), __ATOMIC_SEQ_CST
);
838 clearCommandBuffer();
839 SendCommandNG(CMD_PING
, data
, len
);
843 #ifdef USART_SLOW_LINK
844 // 10s timeout for slow FPC, e.g. over BT
845 // as this is the very first command sent to the pm3
846 // that initiates the BT connection
852 PacketResponseNG resp
;
853 if (WaitForResponseTimeoutW(CMD_PING
, &resp
, timeout
, false) == 0) {
857 bool error
= memcmp(data
, resp
.data
.asBytes
, len
) != 0;
862 SendCommandNG(CMD_CAPABILITIES
, NULL
, 0);
863 if (WaitForResponseTimeoutW(CMD_CAPABILITIES
, &resp
, 1000, false) == 0) {
867 if ((resp
.length
!= sizeof(g_pm3_capabilities
)) || (resp
.data
.asBytes
[0] != CAPABILITIES_VERSION
)) {
868 PrintAndLogEx(ERR
, _RED_("Capabilities structure version sent by Proxmark3 is not the same as the one used by the client!"));
869 PrintAndLogEx(ERR
, _RED_("Please flash the Proxmark3 with the same version as the client."));
870 return PM3_EDEVNOTSUPP
;
873 memcpy(&g_pm3_capabilities
, resp
.data
.asBytes
, sizeof(capabilities_t
));
874 g_conn
.send_via_fpc_usart
= g_pm3_capabilities
.via_fpc
;
875 g_conn
.uart_speed
= g_pm3_capabilities
.baudrate
;
877 bool is_tcp_conn
= (g_conn
.send_via_ip
== PM3_TCPv4
|| g_conn
.send_via_ip
== PM3_TCPv6
);
878 bool is_bt_conn
= (memcmp(g_conn
.serial_port_name
, "bt:", 3) == 0);
879 bool is_udp_conn
= (g_conn
.send_via_ip
== PM3_UDPv4
|| g_conn
.send_via_ip
== PM3_UDPv6
);
881 PrintAndLogEx(SUCCESS
, "Communicating with PM3 over %s%s%s%s",
882 (g_conn
.send_via_fpc_usart
) ? _GREEN_("FPC UART") : _GREEN_("USB-CDC"),
883 (is_tcp_conn
) ? " over " _GREEN_("TCP") : "",
884 (is_bt_conn
) ? " over " _GREEN_("BT") : "",
885 (is_udp_conn
) ? " over " _GREEN_("UDP") : ""
887 if (g_conn
.send_via_fpc_usart
) {
888 PrintAndLogEx(SUCCESS
, "PM3 UART serial baudrate: " _GREEN_("%u") "\n", g_conn
.uart_speed
);
891 if (g_conn
.send_via_local_ip
) {
892 // (g_conn.send_via_local_ip == true) -> ((is_tcp_conn || is_udp_conn) == true)
893 res
= uart_reconfigure_timeouts(is_tcp_conn
? UART_TCP_LOCAL_CLIENT_RX_TIMEOUT_MS
: UART_UDP_LOCAL_CLIENT_RX_TIMEOUT_MS
);
894 } else if (is_tcp_conn
|| is_udp_conn
) {
895 res
= uart_reconfigure_timeouts(UART_NET_CLIENT_RX_TIMEOUT_MS
);
897 res
= uart_reconfigure_timeouts(UART_USB_CLIENT_RX_TIMEOUT_MS
);
899 if (res
!= PM3_SUCCESS
) {
906 void CloseProxmark(pm3_device_t
*dev
) {
907 dev
->g_conn
->run
= false;
910 if (communication_thread
!= 0) {
911 pthread_join(communication_thread
, NULL
);
914 pthread_join(communication_thread
, NULL
);
921 // Clean up our state
924 if (communication_thread
!= 0) {
925 memset(&communication_thread
, 0, sizeof(pthread_t
));
928 memset(&communication_thread
, 0, sizeof(pthread_t
));
931 g_session
.pm3_present
= false;
934 // Gives a rough estimate of the communication delay based on channel & baudrate
935 // Max communication delay is when sending largest frame and receiving largest frame
936 // Empirical measures on FTDI with physical cable:
939 // 460800 -> 40..70ms
940 // 9600 -> 1100..1150ms
941 // ~ = 12000000 / USART_BAUD_RATE
942 // Let's take 2x (maybe we need more for BT link?)
943 static size_t communication_delay(void) {
944 // needed also for Windows USB USART??
945 if (g_conn
.send_via_fpc_usart
) {
946 return 2 * (12000000 / g_conn
.uart_speed
);
953 * @brief Wait for receiving a specified amount of bytes
955 * @param buffer The receive buffer
956 * @param len The maximum receive byte size
957 * @param ms_timeout the maximum timeout
958 * @param show_process print how many bytes are received
959 * @return the number of received bytes
961 size_t WaitForRawDataTimeout(uint8_t *buffer
, size_t len
, size_t ms_timeout
, bool show_process
) {
962 uint8_t print_counter
= 0;
965 // Add delay depending on the communication channel & speed
966 if (ms_timeout
!= (size_t) - 1) {
967 ms_timeout
+= communication_delay();
969 __atomic_store_n(&timeout_start_time
, msclock(), __ATOMIC_SEQ_CST
);
971 SetCommunicationRawReceiveBuffer(buffer
, len
);
972 SetCommunicationReceiveMode(true);
977 if (kbd_enter_pressed()) {
978 // Send anything to stop the transfer
979 PrintAndLogEx(INFO
, "Stopping");
980 SendCommandNG(CMD_BREAK_LOOP
, NULL
, 0);
982 // For ms_timeout == -1, pos < len might always be true
983 // so user need a spectial way to break this loop
984 if (ms_timeout
== (size_t) - 1) {
989 pos
= __atomic_load_n(&comm_raw_pos
, __ATOMIC_SEQ_CST
);
991 // Check the timeout if pos is not updated
992 if (last_pos
== pos
) {
993 uint64_t tmp_clk
= __atomic_load_n(&timeout_start_time
, __ATOMIC_SEQ_CST
);
994 // If ms_timeout == -1, the loop can only be breaked by pressing Enter or receiving enough data
995 if ((ms_timeout
!= (size_t) - 1) && (msclock() - tmp_clk
> ms_timeout
)) {
999 // Print process when (print_counter % 64) == 0
1000 if (show_process
&& (print_counter
& 0x3F) == 0) {
1001 PrintAndLogEx(INFO
, "[%zu/%zu]", pos
, len
);
1009 if (pos
== len
&& (ms_timeout
!= (size_t) - 1)) {
1010 // If ms_timeout != -1, when the desired data is received, tell the arm side
1011 // to stop the current process, and wait for some time to make sure the process
1012 // has been stopped.
1013 // If ms_timeout == -1, the user might not want to break the existing process
1015 SendCommandNG(CMD_BREAK_LOOP
, NULL
, 0);
1018 SetCommunicationReceiveMode(false);
1019 pos
= __atomic_load_n(&comm_raw_pos
, __ATOMIC_SEQ_CST
);
1024 * @brief Waits for a certain response type. This method waits for a maximum of
1025 * ms_timeout milliseconds for a specified response command.
1027 * @param cmd command to wait for, or CMD_UNKNOWN to take any command.
1028 * @param response struct to copy received command into.
1029 * @param ms_timeout display message after 3 seconds
1030 * @param show_warning display message after 3 seconds
1031 * @return true if command was returned, otherwise false
1033 bool WaitForResponseTimeoutW(uint32_t cmd
, PacketResponseNG
*response
, size_t ms_timeout
, bool show_warning
) {
1036 PacketResponseNG resp
;
1037 memset(&resp
, 0, sizeof(resp
));
1039 if (response
== NULL
) {
1043 // Add delay depending on the communication channel & speed
1044 if (ms_timeout
!= (size_t) - 1)
1045 ms_timeout
+= communication_delay();
1047 __atomic_store_n(&timeout_start_time
, msclock(), __ATOMIC_SEQ_CST
);
1049 // Wait until the command is received
1052 // if device gets disconnected or resets, break out of this loop
1053 if (IsCommunicationThreadDead()) {
1057 while (getReply(response
)) {
1058 if (cmd
== CMD_UNKNOWN
|| response
->cmd
== cmd
) {
1062 if (response
->cmd
== CMD_WTX
&& response
->length
== sizeof(uint16_t)) {
1063 uint16_t wtx
= response
->data
.asDwords
[0] & 0xFFFF;
1064 PrintAndLogEx(DEBUG
, "Got Waiting Time eXtension request %i ms", wtx
);
1065 if (ms_timeout
!= (size_t) - 1) {
1071 uint64_t tmp_clk
= __atomic_load_n(&timeout_start_time
, __ATOMIC_SEQ_CST
);
1072 if ((ms_timeout
!= (size_t) - 1) && (msclock() - tmp_clk
> ms_timeout
)) {
1076 if (msclock() - tmp_clk
> 3000 && show_warning
) {
1077 // 3 seconds elapsed (but this doesn't mean the timeout was exceeded)
1078 PrintAndLogEx(INFO
, "You can cancel this operation by pressing the pm3 button");
1079 show_warning
= false;
1081 // just to avoid CPU busy loop:
1087 bool WaitForResponseTimeout(uint32_t cmd
, PacketResponseNG
*response
, size_t ms_timeout
) {
1088 return WaitForResponseTimeoutW(cmd
, response
, ms_timeout
, true);
1091 bool WaitForResponse(uint32_t cmd
, PacketResponseNG
*response
) {
1092 return WaitForResponseTimeoutW(cmd
, response
, -1, true);
1096 * Data transfer from Proxmark to client. This method times out after
1097 * ms_timeout milliseconds.
1098 * @brief GetFromDevice
1099 * @param memtype Type of memory to download from proxmark
1100 * @param dest Destination address for transfer
1101 * @param bytes number of bytes to be transferred
1102 * @param start_index offset into Proxmark3 BigBuf[]
1103 * @param data used by SPIFFS to provide filename
1104 * @param datalen used by SPIFFS to provide filename length
1105 * @param response struct to copy last command (CMD_ACK) into
1106 * @param ms_timeout timeout in milliseconds
1107 * @param show_warning display message after 2 seconds
1108 * @return true if command was returned, otherwise false
1110 bool GetFromDevice(DeviceMemType_t memtype
, uint8_t *dest
, uint32_t bytes
, uint32_t start_index
, uint8_t *data
, uint32_t datalen
, PacketResponseNG
*response
, size_t ms_timeout
, bool show_warning
) {
1112 if (dest
== NULL
) return false;
1115 PacketResponseNG resp
;
1116 memset(&resp
, 0, sizeof(resp
));
1118 if (response
== NULL
) {
1122 if (bytes
== 0) return true;
1126 clearCommandBuffer();
1130 SendCommandMIX(CMD_DOWNLOAD_BIGBUF
, start_index
, bytes
, 0, NULL
, 0);
1131 return dl_it(dest
, bytes
, response
, ms_timeout
, show_warning
, CMD_DOWNLOADED_BIGBUF
);
1134 SendCommandMIX(CMD_DOWNLOAD_EML_BIGBUF
, start_index
, bytes
, 0, NULL
, 0);
1135 return dl_it(dest
, bytes
, response
, ms_timeout
, show_warning
, CMD_DOWNLOADED_EML_BIGBUF
);
1138 SendCommandMIX(CMD_SPIFFS_DOWNLOAD
, start_index
, bytes
, 0, data
, datalen
);
1139 return dl_it(dest
, bytes
, response
, ms_timeout
, show_warning
, CMD_SPIFFS_DOWNLOADED
);
1142 SendCommandMIX(CMD_FLASHMEM_DOWNLOAD
, start_index
, bytes
, 0, NULL
, 0);
1143 return dl_it(dest
, bytes
, response
, ms_timeout
, show_warning
, CMD_FLASHMEM_DOWNLOADED
);
1146 //SendCommandMIX(CMD_DOWNLOAD_SIM_MEM, start_index, bytes, 0, NULL, 0);
1147 //return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_DOWNLOADED_SIMMEM);
1151 SendCommandNG(CMD_FPGAMEM_DOWNLOAD
, NULL
, 0);
1152 return dl_it(dest
, bytes
, response
, ms_timeout
, show_warning
, CMD_FPGAMEM_DOWNLOADED
);
1156 uint32_t flags
= (memtype
== MCU_MEM
) ? READ_MEM_DOWNLOAD_FLAG_RAW
: 0;
1157 SendCommandBL(CMD_READ_MEM_DOWNLOAD
, start_index
, bytes
, flags
, NULL
, 0);
1158 return dl_it(dest
, bytes
, response
, ms_timeout
, show_warning
, CMD_READ_MEM_DOWNLOADED
);
1164 static bool dl_it(uint8_t *dest
, uint32_t bytes
, PacketResponseNG
*response
, size_t ms_timeout
, bool show_warning
, uint32_t rec_cmd
) {
1166 uint32_t bytes_completed
= 0;
1167 __atomic_store_n(&timeout_start_time
, msclock(), __ATOMIC_SEQ_CST
);
1169 // Add delay depending on the communication channel & speed
1170 if (ms_timeout
!= (size_t) - 1)
1171 ms_timeout
+= communication_delay();
1175 if (getReply(response
)) {
1177 if (response
->cmd
== CMD_ACK
)
1179 if (response
->cmd
== CMD_SPIFFS_DOWNLOAD
&& response
->status
== PM3_EMALLOC
)
1181 // Spiffs // fpgamem-plot download is converted to NG,
1182 if (response
->cmd
== CMD_SPIFFS_DOWNLOAD
|| response
->cmd
== CMD_FPGAMEM_DOWNLOAD
)
1185 // sample_buf is a array pointer, located in data.c
1186 // arg0 = offset in transfer. Startindex of this chunk
1187 // arg1 = length bytes to transfer
1188 // arg2 = bigbuff tracelength (?)
1189 if (response
->cmd
== rec_cmd
) {
1191 uint32_t offset
= response
->oldarg
[0];
1192 uint32_t copy_bytes
= MIN(bytes
- bytes_completed
, response
->oldarg
[1]);
1193 //uint32_t tracelen = response->oldarg[2];
1195 // extended bounds check1. upper limit is PM3_CMD_DATA_SIZE
1197 copy_bytes
= MIN(copy_bytes
, PM3_CMD_DATA_SIZE
);
1199 // extended bounds check2.
1200 if (offset
+ copy_bytes
> bytes
) {
1201 PrintAndLogEx(FAILED
, "ERROR: Out of bounds when downloading from device, offset %u | len %u | total len %u > buf_size %u", offset
, copy_bytes
, offset
+ copy_bytes
, bytes
);
1205 memcpy(dest
+ offset
, response
->data
.asBytes
, copy_bytes
);
1206 bytes_completed
+= copy_bytes
;
1207 } else if (response
->cmd
== CMD_WTX
&& response
->length
== sizeof(uint16_t)) {
1208 uint16_t wtx
= response
->data
.asDwords
[0] & 0xFFFF;
1209 PrintAndLogEx(DEBUG
, "Got Waiting Time eXtension request %i ms", wtx
);
1210 if (ms_timeout
!= (size_t) - 1)
1215 uint64_t tmp_clk
= __atomic_load_n(&timeout_start_time
, __ATOMIC_SEQ_CST
);
1216 if (msclock() - tmp_clk
> ms_timeout
) {
1217 PrintAndLogEx(FAILED
, "Timed out while trying to download data from device");
1221 if (msclock() - tmp_clk
> 3000 && show_warning
) {
1222 // 3 seconds elapsed (but this doesn't mean the timeout was exceeded)
1223 PrintAndLogEx(INFO
, "Waiting for a response from the Proxmark3...");
1224 PrintAndLogEx(INFO
, "You can cancel this operation by pressing the pm3 button");
1225 show_warning
= false;