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