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