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