]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
Change copyright to allow GPLV3, for https://github.com/Proxmark/proxmark3/issues/527
[proxmark3-svn] / client / proxmark3.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 // Main binary
10 //-----------------------------------------------------------------------------
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <pthread.h>
16 #include <unistd.h>
17 #include <readline/readline.h>
18 #include <readline/history.h>
19
20 #include "proxmark3.h"
21 #include "util_posix.h"
22 #include "proxgui.h"
23 #include "cmdmain.h"
24 #include "uart.h"
25 #include "ui.h"
26 #include "util.h"
27 #include "cmdparser.h"
28 #include "cmdhw.h"
29 #include "whereami.h"
30
31 #ifdef _WIN32
32 #define SERIAL_PORT_H "com3"
33 #else
34 #define SERIAL_PORT_H "/dev/ttyACM0"
35 #endif
36
37 // a global mutex to prevent interlaced printing from different threads
38 pthread_mutex_t print_lock;
39
40 static serial_port sp;
41 static UsbCommand txcmd;
42 volatile static bool txcmd_pending = false;
43
44 void SendCommand(UsbCommand *c) {
45 #if 0
46 printf("Sending %d bytes\n", sizeof(UsbCommand));
47 #endif
48
49 if (offline) {
50 PrintAndLog("Sending bytes to proxmark failed - offline");
51 return;
52 }
53 /**
54 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
55 or disconnected. The main console thread is alive, but comm thread just spins here.
56 Not good.../holiman
57 **/
58 while(txcmd_pending);
59 txcmd = *c;
60 txcmd_pending = true;
61 }
62
63 struct receiver_arg {
64 int run;
65 };
66
67 byte_t rx[sizeof(UsbCommand)];
68 byte_t* prx = rx;
69
70 static void *uart_receiver(void *targ) {
71 struct receiver_arg *arg = (struct receiver_arg*)targ;
72 size_t rxlen;
73
74 while (arg->run) {
75 rxlen = 0;
76 if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
77 prx += rxlen;
78 if (prx-rx < sizeof(UsbCommand)) {
79 continue;
80 }
81 UsbCommandReceived((UsbCommand*)rx);
82 }
83 prx = rx;
84
85 if(txcmd_pending) {
86 if (!uart_send(sp, (byte_t*) &txcmd, sizeof(UsbCommand))) {
87 PrintAndLog("Sending bytes to proxmark failed");
88 }
89 txcmd_pending = false;
90 }
91 }
92
93 pthread_exit(NULL);
94 return NULL;
95 }
96
97
98 void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
99 struct receiver_arg rarg;
100 char *cmd = NULL;
101 pthread_t reader_thread;
102 bool execCommand = (script_cmd != NULL);
103 bool stdinOnPipe = !isatty(STDIN_FILENO);
104
105 if (usb_present) {
106 rarg.run = 1;
107 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
108 // cache Version information now:
109 CmdVersion(NULL);
110 }
111
112 // file with script
113 FILE *script_file = NULL;
114 char script_cmd_buf[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest
115
116 if (script_cmds_file) {
117 script_file = fopen(script_cmds_file, "r");
118 if (script_file) {
119 printf("executing commands from file: %s\n", script_cmds_file);
120 }
121 }
122
123 read_history(".history");
124
125 while(1) {
126 // If there is a script file
127 if (script_file)
128 {
129 memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
130 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) {
131 fclose(script_file);
132 script_file = NULL;
133 } else {
134 strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
135
136 if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
137 printf(PROXPROMPT"%s\n", cmd);
138 }
139 }
140 } else {
141 // If there is a script command
142 if (execCommand){
143 if ((cmd = strmcopy(script_cmd)) != NULL) {
144 printf(PROXPROMPT"%s\n", cmd);
145 }
146
147 execCommand = false;
148 } else {
149 // exit after exec command
150 if (script_cmd)
151 break;
152
153 // if there is a pipe from stdin
154 if (stdinOnPipe) {
155 memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
156 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), stdin)) {
157 printf("\nStdin end. Exit...\n");
158 break;
159 }
160 strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
161
162 if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
163 printf(PROXPROMPT"%s\n", cmd);
164 }
165
166 } else {
167 // read command from command prompt
168 cmd = readline(PROXPROMPT);
169 }
170 }
171 }
172
173 // execute command
174 if (cmd) {
175
176 while(cmd[strlen(cmd) - 1] == ' ')
177 cmd[strlen(cmd) - 1] = 0x00;
178
179 if (cmd[0] != 0x00) {
180 int ret = CommandReceived(cmd);
181 add_history(cmd);
182 if (ret == 99) { // exit or quit
183 break;
184 }
185 }
186 free(cmd);
187 cmd = NULL;
188 } else {
189 printf("\n");
190 break;
191 }
192 }
193
194 write_history(".history");
195
196 if (usb_present) {
197 rarg.run = 0;
198 pthread_join(reader_thread, NULL);
199 }
200
201 if (script_file) {
202 fclose(script_file);
203 script_file = NULL;
204 }
205 }
206
207 static void dumpAllHelp(int markdown)
208 {
209 printf("\n%sProxmark3 command dump%s\n\n",markdown?"# ":"",markdown?"":"\n======================");
210 printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown?" ":"");
211 printf("Check column \"offline\" for their availability.\n");
212 printf("\n");
213 command_t *cmds = getTopLevelCommandTable();
214 dumpCommandsRecursive(cmds, markdown);
215 }
216
217 static char *my_executable_path = NULL;
218 static char *my_executable_directory = NULL;
219
220 const char *get_my_executable_path(void)
221 {
222 return my_executable_path;
223 }
224
225 const char *get_my_executable_directory(void)
226 {
227 return my_executable_directory;
228 }
229
230 static void set_my_executable_path(void)
231 {
232 int path_length = wai_getExecutablePath(NULL, 0, NULL);
233 if (path_length != -1) {
234 my_executable_path = (char*)malloc(path_length + 1);
235 int dirname_length = 0;
236 if (wai_getExecutablePath(my_executable_path, path_length, &dirname_length) != -1) {
237 my_executable_path[path_length] = '\0';
238 my_executable_directory = (char *)malloc(dirname_length + 2);
239 strncpy(my_executable_directory, my_executable_path, dirname_length+1);
240 my_executable_directory[dirname_length+1] = '\0';
241 }
242 }
243 }
244
245 static void show_help(bool showFullHelp, char *command_line){
246 printf("syntax: %s <port> [-h|-help|-m|-f|-flush|-w|-wait|-c|-command|-l|-lua] [cmd_script_file_name] [command][lua_script_name]\n", command_line);
247 printf("\tLinux example:'%s /dev/ttyACM0'\n", command_line);
248 printf("\tWindows example:'%s com3'\n\n", command_line);
249
250 if (showFullHelp){
251 printf("help: <-h|-help> Dump all interactive command's help at once.\n");
252 printf("\t%s -h\n\n", command_line);
253 printf("markdown: <-m> Dump all interactive help at once in markdown syntax\n");
254 printf("\t%s -m\n\n", command_line);
255 printf("flush: <-f|-flush> Output will be flushed after every print.\n");
256 printf("\t%s -f\n\n", command_line);
257 printf("wait: <-w|-wait> 20sec waiting the serial port to appear in the OS\n");
258 printf("\t%s "SERIAL_PORT_H" -w\n\n", command_line);
259 printf("script: A script file with one proxmark3 command per line.\n\n");
260 printf("command: <-c|-command> Execute one proxmark3 command.\n");
261 printf("\t%s "SERIAL_PORT_H" -c \"hf mf chk 1* ?\"\n", command_line);
262 printf("\t%s "SERIAL_PORT_H" -command \"hf mf nested 1 *\"\n\n", command_line);
263 printf("lua: <-l|-lua> Execute lua script.\n");
264 printf("\t%s "SERIAL_PORT_H" -l hf_read\n\n", command_line);
265 }
266 }
267
268 int main(int argc, char* argv[]) {
269 srand(time(0));
270
271 bool usb_present = false;
272 bool waitCOMPort = false;
273 bool executeCommand = false;
274 bool addLuaExec = false;
275 char *script_cmds_file = NULL;
276 char *script_cmd = NULL;
277
278 if (argc < 2) {
279 show_help(true, argv[0]);
280 return 1;
281 }
282
283 for (int i = 1; i < argc; i++) {
284 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i],"-help") == 0) {
285 show_help(false, argv[0]);
286 dumpAllHelp(0);
287 return 0;
288 }
289
290 if (strcmp(argv[i], "-m") == 0) {
291 dumpAllHelp(1);
292 return 0;
293 }
294
295 if(strcmp(argv[i],"-f") == 0 || strcmp(argv[i],"-flush") == 0){
296 printf("Output will be flushed after every print.\n");
297 flushAfterWrite = 1;
298 }
299
300 if(strcmp(argv[i],"-w") == 0 || strcmp(argv[i],"-wait") == 0){
301 waitCOMPort = true;
302 }
303
304 if(strcmp(argv[i],"-c") == 0 || strcmp(argv[i],"-command") == 0){
305 executeCommand = true;
306 }
307
308 if(strcmp(argv[i],"-l") == 0 || strcmp(argv[i],"-lua") == 0){
309 executeCommand = true;
310 addLuaExec = true;
311 }
312 }
313
314 // If the user passed the filename of the 'script' to execute, get it from last parameter
315 if (argc > 2 && argv[argc - 1] && argv[argc - 1][0] != '-') {
316 if (executeCommand){
317 script_cmd = argv[argc - 1];
318
319 while(script_cmd[strlen(script_cmd) - 1] == ' ')
320 script_cmd[strlen(script_cmd) - 1] = 0x00;
321
322 if (strlen(script_cmd) == 0) {
323 script_cmd = NULL;
324 } else {
325 if (addLuaExec){
326 // add "script run " to command
327 char *ctmp = NULL;
328 int len = strlen(script_cmd) + 11 + 1;
329 if ((ctmp = (char*) malloc(len)) != NULL) {
330 memset(ctmp, 0, len);
331 strcpy(ctmp, "script run ");
332 strcpy(&ctmp[11], script_cmd);
333 script_cmd = ctmp;
334 }
335 }
336
337 printf("Execute command from commandline: %s\n", script_cmd);
338 }
339 } else {
340 script_cmds_file = argv[argc - 1];
341 }
342 }
343
344 // check command
345 if (executeCommand && (!script_cmd || strlen(script_cmd) == 0)){
346 printf("ERROR: execute command: command not found.\n");
347 return 2;
348 }
349
350 // set global variables
351 set_my_executable_path();
352
353 // open uart
354 if (!waitCOMPort) {
355 sp = uart_open(argv[1]);
356 } else {
357 printf("Waiting for Proxmark to appear on %s ", argv[1]);
358 fflush(stdout);
359 int openCount = 0;
360 do {
361 sp = uart_open(argv[1]);
362 msleep(1000);
363 printf(".");
364 fflush(stdout);
365 } while(++openCount < 20 && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
366 printf("\n");
367 }
368
369 // check result of uart opening
370 if (sp == INVALID_SERIAL_PORT) {
371 printf("ERROR: invalid serial port\n");
372 usb_present = false;
373 offline = 1;
374 } else if (sp == CLAIMED_SERIAL_PORT) {
375 printf("ERROR: serial port is claimed by another process\n");
376 usb_present = false;
377 offline = 1;
378 } else {
379 usb_present = true;
380 offline = 0;
381 }
382
383 // create a mutex to avoid interlacing print commands from our different threads
384 pthread_mutex_init(&print_lock, NULL);
385
386 #ifdef HAVE_GUI
387 #ifdef _WIN32
388 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
389 MainGraphics();
390 #else
391 char* display = getenv("DISPLAY");
392
393 if (display && strlen(display) > 1)
394 {
395 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
396 MainGraphics();
397 }
398 else
399 {
400 main_loop(script_cmds_file, script_cmd, usb_present);
401 }
402 #endif
403 #else
404 main_loop(script_cmds_file, script_cmd, usb_present);
405 #endif
406
407 // Clean up the port
408 if (usb_present) {
409 uart_close(sp);
410 }
411
412 // clean up mutex
413 pthread_mutex_destroy(&print_lock);
414
415 exit(0);
416 }
Impressum, Datenschutz