]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/proxmark3.c
Comms refactor (prerequisite of libproxmark work) (#371)
[proxmark3-svn] / client / proxmark3.c
... / ...
CommitLineData
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#include "comms.h"
31
32#ifdef _WIN32
33#define SERIAL_PORT_H "com3"
34#elif __APPLE__
35#define SERIAL_PORT_H "/dev/tty.usbmodem*"
36#else
37#define SERIAL_PORT_H "/dev/ttyACM0"
38#endif
39
40void main_loop(char *script_cmds_file, char* script_cmd, bool usb_present, serial_port* sp) {
41 receiver_arg conn;
42 char *cmd = NULL;
43 pthread_t reader_thread;
44 bool execCommand = (script_cmd != NULL);
45 bool stdinOnPipe = !isatty(STDIN_FILENO);
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
57 if (usb_present) {
58 conn.run = true;
59 SetSerialPort(sp);
60 SetOffline(false);
61 pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
62 // cache Version information now:
63 CmdVersion(NULL);
64 } else {
65 SetOffline(true);
66 }
67
68 // file with script
69 FILE *script_file = NULL;
70 char script_cmd_buf[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest
71
72 if (script_cmds_file) {
73 script_file = fopen(script_cmds_file, "r");
74 if (script_file) {
75 printf("executing commands from file: %s\n", script_cmds_file);
76 }
77 }
78
79 read_history(".history");
80
81 while (1) {
82 // If there is a script file
83 if (script_file)
84 {
85 memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
86 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) {
87 fclose(script_file);
88 script_file = NULL;
89 } else {
90 strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
91
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);
125 }
126 }
127 }
128
129 // execute command
130 if (cmd) {
131
132 while(cmd[strlen(cmd) - 1] == ' ')
133 cmd[strlen(cmd) - 1] = 0x00;
134
135 if (cmd[0] != 0x00) {
136 int ret = CommandReceived(cmd);
137 add_history(cmd);
138 if (ret == 99) { // exit or quit
139 break;
140 }
141 }
142 free(cmd);
143 cmd = NULL;
144 } else {
145 printf("\n");
146 break;
147 }
148 }
149
150 write_history(".history");
151
152 if (usb_present) {
153 conn.run = false;
154 pthread_join(reader_thread, NULL);
155 }
156
157 if (script_file) {
158 fclose(script_file);
159 script_file = NULL;
160 }
161
162 pthread_mutex_destroy(&conn.recv_lock);
163}
164
165static void dumpAllHelp(int markdown)
166{
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?" ":"");
169 printf("Check column \"offline\" for their availability.\n");
170 printf("\n");
171 command_t *cmds = getTopLevelCommandTable();
172 dumpCommandsRecursive(cmds, markdown);
173}
174
175static char *my_executable_path = NULL;
176static char *my_executable_directory = NULL;
177
178const char *get_my_executable_path(void)
179{
180 return my_executable_path;
181}
182
183const char *get_my_executable_directory(void)
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);
198 my_executable_directory[dirname_length+1] = '\0';
199 }
200 }
201}
202
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}
225
226int main(int argc, char* argv[]) {
227 srand(time(0));
228
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;
235 serial_port *sp = NULL;
236 g_debugMode = 0;
237
238 if (argc < 2) {
239 show_help(true, argv[0]);
240 return 1;
241 }
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 }
272 }
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 }
302 }
303
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
311 set_my_executable_path();
312
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]);
318 fflush(stdout);
319 int openCount = 0;
320 do {
321 sp = uart_open(argv[1]);
322 msleep(1000);
323 printf(".");
324 fflush(stdout);
325 } while(++openCount < 20 && (sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT));
326 printf("\n");
327 }
328
329 // check result of uart opening
330 if (sp == INVALID_SERIAL_PORT) {
331 printf("ERROR: invalid serial port\n");
332 usb_present = false;
333 } else if (sp == CLAIMED_SERIAL_PORT) {
334 printf("ERROR: serial port is claimed by another process\n");
335 usb_present = false;
336 } else {
337 usb_present = true;
338 }
339
340 // create a mutex to avoid interlacing print commands from our different threads
341 pthread_mutex_init(&print_lock, NULL);
342
343#ifdef HAVE_GUI
344#ifdef _WIN32
345 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present, sp);
346 MainGraphics();
347#else
348 char* display = getenv("DISPLAY");
349
350 if (display && strlen(display) > 1)
351 {
352 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present, sp);
353 MainGraphics();
354 }
355 else
356 {
357 main_loop(script_cmds_file, script_cmd, usb_present, sp);
358 }
359#endif
360#else
361 main_loop(script_cmds_file, script_cmd, usb_present, sp);
362#endif
363
364 // Clean up the port
365 if (usb_present) {
366 uart_close(sp);
367 }
368
369 // clean up mutex
370 pthread_mutex_destroy(&print_lock);
371
372 exit(0);
373}
Impressum, Datenschutz