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