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 sp
;
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 volatile static bool txcmd_pending
= false;
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 SetOffline(bool new_offline
) {
54 offline
= new_offline
;
61 bool OpenProxmark(char *portname
, bool waitCOMPort
, int timeout
) {
63 sp
= uart_open(portname
);
65 printf("Waiting for Proxmark to appear on %s ", portname
);
69 sp
= uart_open(portname
);
73 } while(++openCount
< timeout
&& (sp
== INVALID_SERIAL_PORT
|| sp
== CLAIMED_SERIAL_PORT
));
77 // check result of uart opening
78 if (sp
== INVALID_SERIAL_PORT
) {
79 printf("ERROR: invalid serial port\n");
81 } else if (sp
== CLAIMED_SERIAL_PORT
) {
82 printf("ERROR: serial port is claimed by another process\n");
89 void CloseProxmark(void) {
93 void SendCommand(UsbCommand
*c
) {
95 printf("Sending %04x cmd\n", c
->cmd
);
99 PrintAndLog("Sending bytes to proxmark failed - offline");
103 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
104 or disconnected. The main console thread is alive, but comm thread just spins here.
107 while(txcmd_pending
);
110 txcmd_pending
= true;
115 * @brief This method should be called when sending a new command to the pm3. In case any old
116 * responses from previous commands are stored in the buffer, a call to this method should clear them.
117 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
118 * operation. Right now we'll just have to live with this.
120 void clearCommandBuffer()
122 //This is a very simple operation
123 pthread_mutex_lock(&cmdBufferMutex
);
125 pthread_mutex_unlock(&cmdBufferMutex
);
129 * @brief storeCommand stores a USB command in a circular buffer
132 void storeCommand(UsbCommand
*command
)
134 pthread_mutex_lock(&cmdBufferMutex
);
135 if( (cmd_head
+1) % CMD_BUFFER_SIZE
== cmd_tail
)
137 // If these two are equal, we're about to overwrite in the
139 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
142 // Store the command at the 'head' location
143 UsbCommand
* destination
= &cmdBuffer
[cmd_head
];
144 memcpy(destination
, command
, sizeof(UsbCommand
));
146 cmd_head
= (cmd_head
+1) % CMD_BUFFER_SIZE
; //increment head and wrap
147 pthread_mutex_unlock(&cmdBufferMutex
);
152 * @brief getCommand gets a command from an internal circular buffer.
153 * @param response location to write command
154 * @return 1 if response was returned, 0 if nothing has been received
156 int getCommand(UsbCommand
* response
)
158 pthread_mutex_lock(&cmdBufferMutex
);
159 //If head == tail, there's nothing to read, or if we just got initialized
160 if (cmd_head
== cmd_tail
){
161 pthread_mutex_unlock(&cmdBufferMutex
);
165 //Pick out the next unread command
166 UsbCommand
* last_unread
= &cmdBuffer
[cmd_tail
];
167 memcpy(response
, last_unread
, sizeof(UsbCommand
));
168 //Increment tail - this is a circular buffer, so modulo buffer size
169 cmd_tail
= (cmd_tail
+ 1) % CMD_BUFFER_SIZE
;
171 pthread_mutex_unlock(&cmdBufferMutex
);
176 static void UsbCommandReceived(UsbCommand
*UC
)
179 // First check if we are handling a debug message
180 case CMD_DEBUG_PRINT_STRING
: {
181 char s
[USB_CMD_DATA_SIZE
+1];
182 memset(s
, 0x00, sizeof(s
));
183 size_t len
= MIN(UC
->arg
[0],USB_CMD_DATA_SIZE
);
184 memcpy(s
,UC
->d
.asBytes
,len
);
185 PrintAndLog("#db# %s", s
);
189 case CMD_DEBUG_PRINT_INTEGERS
: {
190 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
194 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
195 // FIXME: This does unsanitised copies into memory when we don't know
196 // the size of the buffer.
197 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
210 #ifdef __has_attribute
211 #if __has_attribute(force_align_arg_pointer)
212 __attribute__((force_align_arg_pointer
))
215 *uart_receiver(void *targ
) {
216 receiver_arg
*conn
= (receiver_arg
*)targ
;
218 uint8_t rx
[sizeof(UsbCommand
)];
223 if (uart_receive(sp
, prx
, sizeof(UsbCommand
) - (prx
-rx
), &rxlen
) && rxlen
) {
225 if (prx
-rx
< sizeof(UsbCommand
)) {
228 UsbCommandReceived((UsbCommand
*)rx
);
233 if (!uart_send(sp
, (uint8_t*) &txcmd
, sizeof(UsbCommand
))) {
234 PrintAndLog("Sending bytes to proxmark failed");
236 txcmd_pending
= false;
246 * Waits for a certain response type. This method waits for a maximum of
247 * ms_timeout milliseconds for a specified response command.
248 *@brief WaitForResponseTimeout
249 * @param cmd command to wait for, or CMD_UNKNOWN to take any command.
250 * @param response struct to copy received command into.
252 * @param show_warning
253 * @return true if command was returned, otherwise false
255 bool WaitForResponseTimeoutW(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
, bool show_warning
) {
260 printf("Waiting for %04x cmd\n", cmd
);
263 if (response
== NULL
) {
267 uint64_t start_time
= msclock();
269 // Wait until the command is received
271 while(getCommand(response
)) {
272 if (cmd
== CMD_UNKNOWN
|| response
->cmd
== cmd
) {
277 if (msclock() - start_time
> ms_timeout
) {
281 if (msclock() - start_time
> 2000 && show_warning
) {
282 // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
283 PrintAndLog("Waiting for a response from the proxmark...");
284 PrintAndLog("You can cancel this operation by pressing the pm3 button");
285 show_warning
= false;
292 bool WaitForResponseTimeout(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
293 return WaitForResponseTimeoutW(cmd
, response
, ms_timeout
, true);
296 bool WaitForResponse(uint32_t cmd
, UsbCommand
* response
) {
297 return WaitForResponseTimeoutW(cmd
, response
, -1, true);