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.
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 void SendCommand(UsbCommand
*c
) {
63 printf("Sending %04x cmd\n", c
->cmd
);
67 PrintAndLog("Sending bytes to proxmark failed - offline");
71 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
72 or disconnected. The main console thread is alive, but comm thread just spins here.
82 * @brief This method should be called when sending a new command to the pm3. In case any old
83 * responses from previous commands are stored in the buffer, a call to this method should clear them.
84 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
85 * operation. Right now we'll just have to live with this.
87 void clearCommandBuffer()
89 //This is a very simple operation
90 pthread_mutex_lock(&cmdBufferMutex
);
92 pthread_mutex_unlock(&cmdBufferMutex
);
96 * @brief storeCommand stores a USB command in a circular buffer
99 void storeCommand(UsbCommand
*command
)
101 pthread_mutex_lock(&cmdBufferMutex
);
102 if( (cmd_head
+1) % CMD_BUFFER_SIZE
== cmd_tail
)
104 // If these two are equal, we're about to overwrite in the
106 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
109 // Store the command at the 'head' location
110 UsbCommand
* destination
= &cmdBuffer
[cmd_head
];
111 memcpy(destination
, command
, sizeof(UsbCommand
));
113 cmd_head
= (cmd_head
+1) % CMD_BUFFER_SIZE
; //increment head and wrap
114 pthread_mutex_unlock(&cmdBufferMutex
);
119 * @brief getCommand gets a command from an internal circular buffer.
120 * @param response location to write command
121 * @return 1 if response was returned, 0 if nothing has been received
123 int getCommand(UsbCommand
* response
)
125 pthread_mutex_lock(&cmdBufferMutex
);
126 //If head == tail, there's nothing to read, or if we just got initialized
127 if (cmd_head
== cmd_tail
){
128 pthread_mutex_unlock(&cmdBufferMutex
);
132 //Pick out the next unread command
133 UsbCommand
* last_unread
= &cmdBuffer
[cmd_tail
];
134 memcpy(response
, last_unread
, sizeof(UsbCommand
));
135 //Increment tail - this is a circular buffer, so modulo buffer size
136 cmd_tail
= (cmd_tail
+ 1) % CMD_BUFFER_SIZE
;
138 pthread_mutex_unlock(&cmdBufferMutex
);
143 //-----------------------------------------------------------------------------
144 // Entry point into our code: called whenever we received a packet over USB
145 // that we weren't necessarily expecting, for example a debug print.
146 //-----------------------------------------------------------------------------
147 void UsbCommandReceived(UsbCommand
*UC
)
150 // First check if we are handling a debug message
151 case CMD_DEBUG_PRINT_STRING
: {
152 char s
[USB_CMD_DATA_SIZE
+1];
153 memset(s
, 0x00, sizeof(s
));
154 size_t len
= MIN(UC
->arg
[0],USB_CMD_DATA_SIZE
);
155 memcpy(s
,UC
->d
.asBytes
,len
);
156 PrintAndLog("#db# %s", s
);
160 case CMD_DEBUG_PRINT_INTEGERS
: {
161 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
165 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
166 // FIXME: This does unsanitised copies into memory when we don't know
167 // the size of the buffer.
168 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
181 #ifdef __has_attribute
182 #if __has_attribute(force_align_arg_pointer)
183 __attribute__((force_align_arg_pointer
))
186 *uart_receiver(void *targ
) {
187 receiver_arg
*arg
= (receiver_arg
*)targ
;
189 uint8_t rx
[sizeof(UsbCommand
)];
194 if (uart_receive(sp
, prx
, sizeof(UsbCommand
) - (prx
-rx
), &rxlen
) && rxlen
) {
196 if (prx
-rx
< sizeof(UsbCommand
)) {
199 UsbCommandReceived((UsbCommand
*)rx
);
204 if (!uart_send(sp
, (uint8_t*) &txcmd
, sizeof(UsbCommand
))) {
205 PrintAndLog("Sending bytes to proxmark failed");
207 txcmd_pending
= false;
217 * Waits for a certain response type. This method waits for a maximum of
218 * ms_timeout milliseconds for a specified response command.
219 *@brief WaitForResponseTimeout
220 * @param cmd command to wait for, or CMD_UNKNOWN to take any command.
221 * @param response struct to copy received command into.
223 * @param show_warning
224 * @return true if command was returned, otherwise false
226 bool WaitForResponseTimeoutW(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
, bool show_warning
) {
231 printf("Waiting for %04x cmd\n", cmd
);
234 if (response
== NULL
) {
238 uint64_t start_time
= msclock();
240 // Wait until the command is received
242 while(getCommand(response
)) {
243 if (cmd
== CMD_UNKNOWN
|| response
->cmd
== cmd
) {
248 if (msclock() - start_time
> ms_timeout
) {
252 if (msclock() - start_time
> 2000 && show_warning
) {
253 // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
254 PrintAndLog("Waiting for a response from the proxmark...");
255 PrintAndLog("You can cancel this operation by pressing the pm3 button");
256 show_warning
= false;
263 bool WaitForResponseTimeout(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
264 return WaitForResponseTimeoutW(cmd
, response
, ms_timeout
, true);
267 bool WaitForResponse(uint32_t cmd
, UsbCommand
* response
) {
268 return WaitForResponseTimeoutW(cmd
, response
, -1, true);