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