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 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
17 #include <readline/readline.h>
18 #include <readline/history.h>
19 //#include "proxusb.h"
20 #include "proxmark3.h"
27 // a global mutex to prevent interlaced printing from different threads
28 pthread_mutex_t print_lock
;
30 static serial_port sp
;
31 static UsbCommand txcmd
;
32 volatile static bool txcmd_pending
= false;
35 void SendCommand(UsbCommand
*c
) {
37 printf("Sending %d bytes\n", sizeof(UsbCommand
));
41 ERR("Sending command failed, previous command is still pending");
46 PrintAndLog("Sending bytes to proxmark failed - offline");
59 struct main_loop_arg
{
61 char *script_cmds_file
;
64 //static void *usb_receiver(void *targ) {
65 // struct receiver_arg *arg = (struct receiver_arg*)targ;
69 // if (ReceiveCommandPoll(&cmdbuf)) {
70 // UsbCommandReceived(&cmdbuf);
75 // pthread_exit(NULL);
82 static void *uart_receiver(void *targ
) {
83 struct receiver_arg
*arg
= (struct receiver_arg
*)targ
;
88 rxlen
= sizeof(UsbCommand
);
89 if (uart_receive(sp
,prx
,&rxlen
)) {
91 if (((prx
-rx
) % sizeof(UsbCommand
)) != 0) {
94 cmd_count
= (prx
-rx
) / sizeof(UsbCommand
);
95 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
96 for (size_t i
=0; i
<cmd_count
; i
++) {
97 UsbCommandReceived((UsbCommand
*)(rx
+(i
*sizeof(UsbCommand
))));
103 if (!uart_send(sp
,(byte_t
*)&txcmd
,sizeof(UsbCommand
))) {
104 PrintAndLog("Sending bytes to proxmark failed");
106 txcmd_pending
= false;
114 static void *main_loop(void *targ
) {
115 struct main_loop_arg
*arg
= (struct main_loop_arg
*)targ
;
116 struct receiver_arg rarg
;
118 pthread_t reader_thread
;
120 if (arg
->usb_present
== 1) {
122 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
123 pthread_create(&reader_thread
, NULL
, &uart_receiver
, &rarg
);
126 FILE *script_file
= NULL
;
127 char script_cmd_buf
[256];
129 if (arg
->script_cmds_file
)
131 script_file
= fopen(arg
->script_cmds_file
, "r");
134 printf("using 'scripting' commands file %s\n", arg
->script_cmds_file
);
138 read_history(".history");
141 // If there is a script file
144 if (!fgets(script_cmd_buf
, sizeof(script_cmd_buf
), script_file
))
152 nl
= strrchr(script_cmd_buf
, '\r');
154 nl
= strrchr(script_cmd_buf
, '\n');
157 if ((cmd
= (char*) malloc(strlen(script_cmd_buf
) + 1)) != NULL
)
159 memset(cmd
, 0, strlen(script_cmd_buf
));
160 strcpy(cmd
, script_cmd_buf
);
168 cmd
= readline(PROXPROMPT
);
172 while(cmd
[strlen(cmd
) - 1] == ' ')
173 cmd
[strlen(cmd
) - 1] = 0x00;
175 if (cmd
[0] != 0x00) {
176 if (strncmp(cmd
, "quit", 4) == 0) {
181 CommandReceived(cmd
);
191 write_history(".history");
193 if (arg
->usb_present
== 1) {
195 pthread_join(reader_thread
, NULL
);
209 #define DUMPHELP(cmd) \
211 printf("%s\n", cmd); \
212 printf("---------------------------------------------\n"); \
213 CommandReceived(cmd); \
217 static void dumphelp()
220 printf("\n------------PROXMARK3 HELP DUMP--------------\n");
221 printf("Some commands are available only if a Proxmark is actually connected,\n");
222 printf("Those commands are flagged with \"@\" in front of their description.\n");
225 DUMPHELP("data help");
227 DUMPHELP("hf 14a help");
228 DUMPHELP("hf 14b help");
229 DUMPHELP("hf 15 help");
230 DUMPHELP("hf epa help");
231 DUMPHELP("hf legic help");
232 DUMPHELP("hf iclass help");
233 DUMPHELP("hf mf help");
236 DUMPHELP("lf em4x help");
237 DUMPHELP("lf hid help");
238 DUMPHELP("lf ti help");
239 DUMPHELP("lf hitag help");
240 DUMPHELP("lf pcf7931 help");
241 DUMPHELP("lf t55xx help");
244 int main(int argc
, char* argv
[]) {
248 printf("syntax: %s <port>\n\n",argv
[0]);
249 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv
[0]);
250 printf("help: %s -h\n\n", argv
[0]);
251 printf("\tDump all interactive help at once\n");
255 if (strcmp(argv
[1], "-h") == 0) {
256 printf("syntax: %s <port>\n\n",argv
[0]);
257 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv
[0]);
262 // Make sure to initialize
263 struct main_loop_arg marg
= {
265 .script_cmds_file
= NULL
267 pthread_t main_loop_t
;
271 if (!OpenProxmark(1)) {
272 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
273 marg.usb_present = 0;
276 marg.usb_present = 1;
281 sp
= uart_open(argv
[1]);
282 if (sp
== INVALID_SERIAL_PORT
) {
283 printf("ERROR: invalid serial port\n");
284 marg
.usb_present
= 0;
286 } else if (sp
== CLAIMED_SERIAL_PORT
) {
287 printf("ERROR: serial port is claimed by another process\n");
288 marg
.usb_present
= 0;
291 marg
.usb_present
= 1;
295 // If the user passed the filename of the 'script' to execute, get it
296 if (argc
> 2 && argv
[2]) {
297 if (argv
[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry.
303 printf("Output will be flushed after every print.\n");
307 marg
.script_cmds_file
= argv
[2];
310 // create a mutex to avoid interlacing print commands from our different threads
311 pthread_mutex_init(&print_lock
, NULL
);
313 pthread_create(&main_loop_t
, NULL
, &main_loop
, &marg
);
314 InitGraphics(argc
, argv
);
318 pthread_join(main_loop_t
, NULL
);
320 // if (marg.usb_present == 1) {
328 pthread_mutex_destroy(&print_lock
);