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