1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // Code for communicating with the proxmark3 hardware.
10 //-----------------------------------------------------------------------------
19 #include "util_posix.h"
23 // Serial port that we are communicating with the PM3 on.
24 static serial_port
* port
;
26 // If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
30 // TODO: Use locks and execute this on the main thread, rather than the receiver
31 // thread. Running on the main thread means we need to be careful in the
32 // flasher, as it means SendCommand is no longer async, and can't be used as a
33 // buffer for a pending command when the connection is re-established.
34 static UsbCommand txcmd
;
35 static bool txcmd_pending
;
37 // Used by UsbReceiveCommand as a ring buffer for messages that are yet to be
38 // processed by a command handler (WaitForResponse{,Timeout})
39 static UsbCommand cmdBuffer
[CMD_BUFFER_SIZE
];
41 // Points to the next empty position to write to
42 static int cmd_head
= 0;
44 // Points to the position of the last unread command
45 static int cmd_tail
= 0;
47 // to lock cmdBuffer operations from different threads
48 static pthread_mutex_t cmdBufferMutex
= PTHREAD_MUTEX_INITIALIZER
;
50 // These wrappers are required because it is not possible to access a static
51 // global variable outside of the context of a single file.
53 void SetSerialPort(serial_port
* new_port
) {
57 serial_port
* GetSerialPort() {
61 void SetOffline(bool new_offline
) {
62 offline
= new_offline
;
69 void SendCommand(UsbCommand
*c
) {
71 printf("Sending %04x cmd\n", c
->cmd
);
75 PrintAndLog("Sending bytes to proxmark failed - offline");
79 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
80 or disconnected. The main console thread is alive, but comm thread just spins here.
89 * @brief This method should be called when sending a new command to the pm3. In case any old
90 * responses from previous commands are stored in the buffer, a call to this method should clear them.
91 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
92 * operation. Right now we'll just have to live with this.
94 void clearCommandBuffer()
96 //This is a very simple operation
97 pthread_mutex_lock(&cmdBufferMutex
);
99 pthread_mutex_unlock(&cmdBufferMutex
);
103 * @brief storeCommand stores a USB command in a circular buffer
106 void storeCommand(UsbCommand
*command
)
108 pthread_mutex_lock(&cmdBufferMutex
);
109 if( (cmd_head
+1) % CMD_BUFFER_SIZE
== cmd_tail
)
111 // If these two are equal, we're about to overwrite in the
113 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
116 // Store the command at the 'head' location
117 UsbCommand
* destination
= &cmdBuffer
[cmd_head
];
118 memcpy(destination
, command
, sizeof(UsbCommand
));
120 cmd_head
= (cmd_head
+1) % CMD_BUFFER_SIZE
; //increment head and wrap
121 pthread_mutex_unlock(&cmdBufferMutex
);
126 * @brief getCommand gets a command from an internal circular buffer.
127 * @param response location to write command
128 * @return 1 if response was returned, 0 if nothing has been received
130 int getCommand(UsbCommand
* response
)
132 pthread_mutex_lock(&cmdBufferMutex
);
133 //If head == tail, there's nothing to read, or if we just got initialized
134 if (cmd_head
== cmd_tail
){
135 pthread_mutex_unlock(&cmdBufferMutex
);
139 //Pick out the next unread command
140 UsbCommand
* last_unread
= &cmdBuffer
[cmd_tail
];
141 memcpy(response
, last_unread
, sizeof(UsbCommand
));
142 //Increment tail - this is a circular buffer, so modulo buffer size
143 cmd_tail
= (cmd_tail
+ 1) % CMD_BUFFER_SIZE
;
145 pthread_mutex_unlock(&cmdBufferMutex
);
149 //-----------------------------------------------------------------------------
150 // Entry point into our code: called whenever we received a packet over USB
151 // that we weren't necessarily expecting, for example a debug print.
152 //-----------------------------------------------------------------------------
153 void UsbCommandReceived(UsbCommand
*UC
)
157 // First check if we are handling a debug message
158 case CMD_DEBUG_PRINT_STRING
: {
159 char s
[USB_CMD_DATA_SIZE
+1];
160 memset(s
, 0x00, sizeof(s
));
161 size_t len
= MIN(UC
->arg
[0],USB_CMD_DATA_SIZE
);
162 memcpy(s
,UC
->d
.asBytes
,len
);
163 PrintAndLog("#db# %s", s
);
167 case CMD_DEBUG_PRINT_INTEGERS
: {
168 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
172 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
173 // FIXME: This does unsanitised copies into memory when we don't know
174 // the size of the buffer.
176 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
188 // Gets a single command from a proxmark3 device. This should never be used
189 // with the full client.
191 // @param conn A receiver_arg structure.
192 // @param command A buffer to store the received command.
193 bool ReceiveCommand(receiver_arg
* conn
, UsbCommand
* command
) {
194 // Local recieve buffer
196 byte_t rx
[sizeof(UsbCommand
)];
201 if (uart_receive(port
, prx
, sizeof(UsbCommand
) - (prx
-rx
), &rxlen
) && rxlen
) {
203 if (prx
-rx
< sizeof(UsbCommand
)) {
204 // Keep reading until we have a completed response.
208 // We have a completed response.
209 memcpy(command
, rx
, sizeof(UsbCommand
));
214 // We got no complete command while waiting, give up control
219 // did not get a complete command before being cancelled.
223 // Worker thread for processing incoming events from the PM3
224 void *uart_receiver(void *targ
) {
225 receiver_arg
*conn
= (receiver_arg
*)targ
;
230 printf("uart_receiver: get lock\n");
232 // Lock up receives, in case they try to take it away from us.
233 pthread_mutex_lock(&conn
->recv_lock
);
235 printf("uart_receiver: lock acquired\n");
240 printf("uart_receiver: port disappeared\n");
242 // Our port disappeared, stall. This code path matters for the flasher,
243 // where it is fiddling with the serial port under us.
244 pthread_mutex_unlock(&conn
->recv_lock
);
249 bool got_command
= ReceiveCommand(conn
, &rx
);
251 printf("uart_receiver: got command\n");
253 pthread_mutex_unlock(&conn
->recv_lock
);
256 UsbCommandReceived(&rx
);
259 // We aren't normally trying to transmit in the flasher when the port would
260 // be reset, so we can just keep going at this point.
262 if (!uart_send(port
, (byte_t
*) &txcmd
, sizeof(UsbCommand
))) {
263 PrintAndLog("Sending bytes to proxmark failed");
265 txcmd_pending
= false;
274 * Waits for a certain response type. This method waits for a maximum of
275 * ms_timeout milliseconds for a specified response command.
276 *@brief WaitForResponseTimeout
277 * @param cmd command to wait for, or CMD_ANY to take any command.
278 * @param response struct to copy received command into.
280 * @param show_warning
281 * @return true if command was returned, otherwise false
283 bool WaitForResponseTimeoutW(uint64_t cmd
, UsbCommand
* response
, size_t ms_timeout
, bool show_warning
) {
287 printf("Waiting for %04x cmd\n", cmd
);
290 if (response
== NULL
) {
294 uint64_t start_time
= msclock();
296 // Wait until the command is received
298 while(getCommand(response
)) {
299 if (cmd
== CMD_ANY
|| response
->cmd
== cmd
) {
304 if (msclock() - start_time
> ms_timeout
) {
309 if (msclock() - start_time
> 2000 && show_warning
) {
310 // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
311 PrintAndLog("Waiting for a response from the proxmark...");
312 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
319 bool WaitForResponseTimeout(uint64_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
320 return WaitForResponseTimeoutW(cmd
, response
, ms_timeout
, true);
323 bool WaitForResponse(uint64_t cmd
, UsbCommand
* response
) {
324 return WaitForResponseTimeout(cmd
, response
, -1);