]> git.zerfleddert.de Git - proxmark3-svn/blob - client/comms.c
c8d72b8a5cf036eb733231716fbb71324fc3de0e
[proxmark3-svn] / client / comms.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Code for communicating with the proxmark3 hardware.
10 //-----------------------------------------------------------------------------
11
12 #include "comms.h"
13
14 #include <stdio.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <pthread.h>
18 #include <inttypes.h>
19 #include <time.h>
20
21 #include "uart.h"
22 #include "ui.h"
23 #include "common.h"
24 #include "util_darwin.h"
25 #include "util_posix.h"
26
27
28 // Serial port that we are communicating with the PM3 on.
29 static serial_port sp = NULL;
30 static char *serial_port_name = NULL;
31
32 // If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
33 static bool offline;
34
35 typedef struct {
36 bool run; // If TRUE, continue running the uart_communication thread
37 } communication_arg_t;
38
39 static communication_arg_t conn;
40 static pthread_t USB_communication_thread;
41
42 // Transmit buffer.
43 static UsbCommand txBuffer;
44 static bool txBuffer_pending = false;
45 static pthread_mutex_t txBufferMutex = PTHREAD_MUTEX_INITIALIZER;
46 static pthread_cond_t txBufferSig = PTHREAD_COND_INITIALIZER;
47
48 // Used by UsbReceiveCommand as a ring buffer for messages that are yet to be
49 // processed by a command handler (WaitForResponse{,Timeout})
50 #define CMD_BUFFER_SIZE 50
51 static UsbCommand rxBuffer[CMD_BUFFER_SIZE];
52
53 // Points to the next empty position to write to
54 static int cmd_head = 0;
55
56 // Points to the position of the last unread command
57 static int cmd_tail = 0;
58
59 // to lock rxBuffer operations from different threads
60 static pthread_mutex_t rxBufferMutex = PTHREAD_MUTEX_INITIALIZER;
61 static pthread_cond_t rxBufferSig = PTHREAD_COND_INITIALIZER;
62
63 // These wrappers are required because it is not possible to access a static
64 // global variable outside of the context of a single file.
65
66 void SetOffline(bool new_offline) {
67 offline = new_offline;
68 }
69
70 bool IsOffline() {
71 return offline;
72 }
73
74 void SendCommand(UsbCommand *c) {
75 #ifdef COMMS_DEBUG
76 printf("Sending %04" PRIx64 " cmd\n", c->cmd);
77 #endif
78
79 if (offline) {
80 PrintAndLog("Sending bytes to proxmark failed - offline");
81 return;
82 }
83
84 pthread_mutex_lock(&txBufferMutex);
85 /**
86 This causes hangups at times, when the pm3 unit is unresponsive or disconnected. The main console thread is alive,
87 but comm thread just spins here. Not good.../holiman
88 **/
89 while (txBuffer_pending) {
90 pthread_cond_wait(&txBufferSig, &txBufferMutex); // wait for communication thread to complete sending a previous commmand
91 }
92
93 txBuffer = *c;
94 txBuffer_pending = true;
95 pthread_cond_signal(&txBufferSig); // tell communication thread that a new command can be send
96
97 pthread_mutex_unlock(&txBufferMutex);
98
99 }
100
101
102 /**
103 * @brief This method should be called when sending a new command to the pm3. In case any old
104 * responses from previous commands are stored in the buffer, a call to this method should clear them.
105 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
106 * operation. Right now we'll just have to live with this.
107 */
108 void clearCommandBuffer() {
109 //This is a very simple operation
110 pthread_mutex_lock(&rxBufferMutex);
111 cmd_tail = cmd_head;
112 pthread_mutex_unlock(&rxBufferMutex);
113 }
114
115 /**
116 * @brief storeCommand stores a USB command in a circular buffer
117 * @param UC
118 */
119 static void storeCommand(UsbCommand *command) {
120 pthread_mutex_lock(&rxBufferMutex);
121 if ((cmd_head + 1) % CMD_BUFFER_SIZE == cmd_tail) {
122 // If these two are equal, we're about to overwrite in the
123 // circular buffer.
124 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
125 }
126
127 // Store the command at the 'head' location
128 UsbCommand* destination = &rxBuffer[cmd_head];
129 memcpy(destination, command, sizeof(UsbCommand));
130
131 cmd_head = (cmd_head + 1) % CMD_BUFFER_SIZE; //increment head and wrap
132 pthread_cond_signal(&rxBufferSig); // tell main thread that a new command can be retreived
133 pthread_mutex_unlock(&rxBufferMutex);
134 }
135
136
137 /**
138 * @brief getCommand gets a command from an internal circular buffer.
139 * @param response location to write command
140 * @return 1 if response was returned, 0 if nothing has been received in time
141 */
142 static int getCommand(UsbCommand* response, uint32_t ms_timeout) {
143
144 struct timespec end_time;
145 clock_gettime(CLOCK_REALTIME, &end_time);
146 end_time.tv_sec += ms_timeout / 1000;
147 end_time.tv_nsec += (ms_timeout % 1000) * 1000000;
148 if (end_time.tv_nsec > 1000000000) {
149 end_time.tv_nsec -= 1000000000;
150 end_time.tv_sec += 1;
151 }
152 pthread_mutex_lock(&rxBufferMutex);
153 int res = 0;
154 while (cmd_head == cmd_tail && !res) {
155 res = pthread_cond_timedwait(&rxBufferSig, &rxBufferMutex, &end_time);
156 }
157 if (res) { // timeout
158 pthread_mutex_unlock(&rxBufferMutex);
159 return 0;
160 }
161
162 // Pick out the next unread command
163 UsbCommand* last_unread = &rxBuffer[cmd_tail];
164 memcpy(response, last_unread, sizeof(UsbCommand));
165 // Increment tail - this is a circular buffer, so modulo buffer size
166 cmd_tail = (cmd_tail + 1) % CMD_BUFFER_SIZE;
167
168 pthread_mutex_unlock(&rxBufferMutex);
169 return 1;
170 }
171
172
173 //----------------------------------------------------------------------------------
174 // Entry point into our code: called whenever we received a packet over USB.
175 // Handle debug commands directly, store all other commands in circular buffer.
176 //----------------------------------------------------------------------------------
177 static void UsbCommandReceived(UsbCommand *UC) {
178 switch (UC->cmd) {
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);
186 return;
187 } break;
188
189 case CMD_DEBUG_PRINT_INTEGERS: {
190 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
191 return;
192 } break;
193
194 default:
195 storeCommand(UC);
196 break;
197 }
198
199 }
200
201
202 static bool receive_from_serial(serial_port sp, uint8_t *rx_buf, size_t len, size_t *received_len) {
203 size_t bytes_read = 0;
204 *received_len = 0;
205 // we eventually need to call uart_receive several times because it may timeout in the middle of a transfer
206 while (uart_receive(sp, rx_buf + *received_len, len - *received_len, &bytes_read) && bytes_read && *received_len < len) {
207 #ifdef COMMS_DEBUG
208 if (bytes_read != len - *received_len) {
209 printf("uart_receive() returned true but not enough bytes could be received. received: %zd, wanted to receive: %zd, already received before: %zd\n",
210 bytes_read, len - *received_len, *received_len);
211 }
212 #endif
213 *received_len += bytes_read;
214 bytes_read = 0;
215 }
216 return (*received_len == len);
217 }
218
219
220 static void
221 #ifdef __has_attribute
222 #if __has_attribute(force_align_arg_pointer)
223 __attribute__((force_align_arg_pointer))
224 #endif
225 #endif
226 *uart_communication(void *targ) {
227 communication_arg_t *conn = (communication_arg_t*)targ;
228 uint8_t rx[sizeof(UsbCommand)];
229 size_t rxlen = 0;
230 uint8_t *prx = rx;
231 UsbCommand *command = (UsbCommand*)rx;
232 UsbResponse *response = (UsbResponse*)rx;
233
234 #if defined(__MACH__) && defined(__APPLE__)
235 disableAppNap("Proxmark3 polling UART");
236 #endif
237
238 while (conn->run) {
239 bool ACK_received = false;
240 prx = rx;
241 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)
242 if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
243 prx += rxlen;
244 if (response->cmd & CMD_VARIABLE_SIZE_FLAG) { // new style response with variable size
245 #ifdef COMMS_DEBUG
246 PrintAndLog("received new style response %04" PRIx16 ", datalen = %zd, arg[0] = %08" PRIx32 ", arg[1] = %08" PRIx32 ", arg[2] = %08" PRIx32,
247 response->cmd, response->datalen, response->arg[0], response->arg[1], response->arg[2]);
248 #endif
249 bytes_to_read = response->datalen;
250 if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
251 UsbCommand resp;
252 resp.cmd = response->cmd & ~CMD_VARIABLE_SIZE_FLAG; // remove the flag
253 resp.arg[0] = response->arg[0];
254 resp.arg[1] = response->arg[1];
255 resp.arg[2] = response->arg[2];
256 memcpy(&resp.d.asBytes, &response->d.asBytes, response->datalen);
257 UsbCommandReceived(&resp);
258 if (resp.cmd == CMD_ACK) {
259 ACK_received = true;
260 }
261 }
262 } else { // old style response uses same data structure as commands. Fixed size.
263 #ifdef COMMS_DEBUG
264 PrintAndLog("received old style response %016" PRIx64 ", arg[0] = %016" PRIx64, command->cmd, command->arg[0]);
265 #endif
266 bytes_to_read = sizeof(UsbCommand) - bytes_to_read;
267 if (receive_from_serial(sp, prx, bytes_to_read, &rxlen)) {
268 UsbCommandReceived(command);
269 if (command->cmd == CMD_ACK) {
270 ACK_received = true;
271 }
272 }
273 }
274 }
275
276 pthread_mutex_lock(&txBufferMutex);
277 // if we received an ACK the PM has done its job and waits for another command.
278 // We therefore can wait here as well until a new command is to be transmitted.
279 // The advantage is that the next command will be transmitted immediately without the need to wait for a receive timeout
280 if (ACK_received) {
281 while (!txBuffer_pending) {
282 pthread_cond_wait(&txBufferSig, &txBufferMutex);
283 }
284 }
285 if (txBuffer_pending) {
286 if (!uart_send(sp, (uint8_t*) &txBuffer, sizeof(UsbCommand))) {
287 PrintAndLog("Sending bytes to proxmark failed");
288 }
289 txBuffer_pending = false;
290 }
291 pthread_cond_signal(&txBufferSig); // tell main thread that txBuffer is empty
292 pthread_mutex_unlock(&txBufferMutex);
293 }
294
295 #if defined(__MACH__) && defined(__APPLE__)
296 enableAppNap();
297 #endif
298
299 pthread_exit(NULL);
300 return NULL;
301 }
302
303
304 /**
305 * Data transfer from Proxmark to client. This method times out after
306 * ms_timeout milliseconds.
307 * @brief GetFromBigBuf
308 * @param dest Destination address for transfer
309 * @param bytes number of bytes to be transferred
310 * @param start_index offset into Proxmark3 BigBuf[]
311 * @param response struct to copy last command (CMD_ACK) into
312 * @param ms_timeout timeout in milliseconds
313 * @param show_warning display message after 2 seconds
314 * @return true if command was returned, otherwise false
315 */
316 bool GetFromBigBuf(uint8_t *dest, int bytes, int start_index, UsbCommand *response, size_t ms_timeout, bool show_warning) {
317
318 uint64_t start_time = msclock();
319 uint32_t poll_time = 100; // loop every 100ms
320
321 UsbCommand c = {CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K, {start_index, bytes, 0}};
322 SendCommand(&c);
323
324 UsbCommand resp;
325 if (response == NULL) {
326 response = &resp;
327 }
328
329 int bytes_completed = 0;
330 while (true) {
331 if (msclock() - start_time > ms_timeout) {
332 break; // timeout
333 }
334 if (msclock() - start_time > 2000 && show_warning) {
335 // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
336 PrintAndLog("Waiting for a response from the proxmark...");
337 PrintAndLog("You can cancel this operation by pressing the pm3 button");
338 show_warning = false;
339 }
340 if (getCommand(response, poll_time)) {
341 if (response->cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
342 int copy_bytes = MIN(bytes - bytes_completed, response->arg[1]);
343 memcpy(dest + response->arg[0], response->d.asBytes, copy_bytes);
344 bytes_completed += copy_bytes;
345 } else if (response->cmd == CMD_ACK) {
346 return true;
347 }
348 }
349 }
350
351 return false;
352 }
353
354
355 bool GetFromFpgaRAM(uint8_t *dest, int bytes) {
356
357 uint64_t start_time = msclock();
358 uint32_t poll_time = 100; // loop every 100ms
359
360 UsbCommand c = {CMD_HF_PLOT, {0, 0, 0}};
361 SendCommand(&c);
362
363 UsbCommand response;
364
365 int bytes_completed = 0;
366 bool show_warning = true;
367 while (true) {
368 if (msclock() - start_time > 2000 && show_warning) {
369 PrintAndLog("Waiting for a response from the proxmark...");
370 PrintAndLog("You can cancel this operation by pressing the pm3 button");
371 show_warning = false;
372 }
373 if (getCommand(&response, poll_time)) {
374 if (response.cmd == CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K) {
375 int copy_bytes = MIN(bytes - bytes_completed, response.arg[1]);
376 memcpy(dest + response.arg[0], response.d.asBytes, copy_bytes);
377 bytes_completed += copy_bytes;
378 } else if (response.cmd == CMD_ACK) {
379 return true;
380 }
381 }
382 }
383
384 return false;
385 }
386
387
388 bool OpenProxmark(void *port, bool wait_for_port, int timeout) {
389 char *portname = (char *)port;
390 if (!wait_for_port) {
391 sp = uart_open(portname);
392 } else {
393 printf("Waiting for Proxmark to appear on %s ", portname);
394 fflush(stdout);
395 int openCount = 0;
396 do {
397 sp = uart_open(portname);
398 msleep(1000);
399 printf(".");
400 fflush(stdout);
401 } while (++openCount < timeout && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
402 printf("\n");
403 }
404
405 // check result of uart opening
406 if (sp == INVALID_SERIAL_PORT) {
407 printf("ERROR: invalid serial port\n");
408 sp = NULL;
409 serial_port_name = NULL;
410 return false;
411 } else if (sp == CLAIMED_SERIAL_PORT) {
412 printf("ERROR: serial port is claimed by another process\n");
413 sp = NULL;
414 serial_port_name = NULL;
415 return false;
416 } else {
417 // start the USB communication thread
418 serial_port_name = portname;
419 conn.run = true;
420 pthread_create(&USB_communication_thread, NULL, &uart_communication, &conn);
421 return true;
422 }
423 }
424
425
426 void CloseProxmark(void) {
427 conn.run = false;
428
429 #ifdef __BIONIC__
430 // In Android O and later, if an invalid pthread_t is passed to pthread_join, it calls fatal().
431 // https://github.com/aosp-mirror/platform_bionic/blob/ed16b344e75f422fb36fbfd91fb30de339475880/libc/bionic/pthread_internal.cpp#L116-L128
432 //
433 // In Bionic libc, pthread_t is an integer.
434
435 if (USB_communication_thread != 0) {
436 pthread_join(USB_communication_thread, NULL);
437 }
438 #else
439 // pthread_t is a struct on other libc, treat as an opaque memory reference
440 pthread_join(USB_communication_thread, NULL);
441 #endif
442
443 if (sp) {
444 uart_close(sp);
445 }
446
447 // Clean up our state
448 sp = NULL;
449 serial_port_name = NULL;
450 #ifdef __BIONIC__
451 memset(&USB_communication_thread, 0, sizeof(pthread_t));
452 #endif
453 }
454
455
456 /**
457 * Waits for a certain response type. This method waits for a maximum of
458 * ms_timeout milliseconds for a specified response command.
459 *@brief WaitForResponseTimeout
460 * @param cmd command to wait for, or CMD_UNKNOWN to take any command.
461 * @param response struct to copy received command into.
462 * @param ms_timeout
463 * @param show_warning display message after 2 seconds
464 * @return true if command was returned, otherwise false
465 */
466 bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning) {
467
468 UsbCommand resp;
469
470 #ifdef COMMS_DEBUG
471 printf("Waiting for %04x cmd\n", cmd);
472 #endif
473
474 uint64_t start_time = msclock();
475 uint64_t end_time = start_time + ms_timeout;
476
477 if (response == NULL) {
478 response = &resp;
479 }
480
481 // Wait until the command is received
482 while (true) {
483 int32_t remaining_time = end_time - msclock();
484 if (remaining_time <= 0) {
485 break; // timeout
486 }
487 if (msclock() - start_time > 2000 && show_warning) {
488 // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
489 PrintAndLog("Waiting for a response from the proxmark...");
490 PrintAndLog("You can cancel this operation by pressing the pm3 button");
491 show_warning = false;
492 }
493 if (getCommand(response, remaining_time)) {
494 if (cmd == CMD_UNKNOWN || response->cmd == cmd) {
495 return true;
496 }
497 }
498 }
499 return false;
500 }
501
502
503 bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
504 return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
505 }
506
507 bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
508 return WaitForResponseTimeoutW(cmd, response, -1, true);
509 }
510
Impressum, Datenschutz