0acf34f682d23946ab8495b620de943d0af7e996
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
17 #include <readline/readline.h>
18 #include <readline/history.h>
20 #include "proxmark3.h"
21 #include "util_posix.h"
27 #include "cmdparser.h"
33 #define SERIAL_PORT_H "com3"
35 #define SERIAL_PORT_H "/dev/tty.usbmodem*"
37 #define SERIAL_PORT_H "/dev/ttyACM0"
40 void main_loop(char *script_cmds_file
, char* script_cmd
, bool usb_present
, serial_port
* sp
) {
43 pthread_t reader_thread
;
44 bool execCommand
= (script_cmd
!= NULL
);
45 bool stdinOnPipe
= !isatty(STDIN_FILENO
);
47 memset(&conn
, 0, sizeof(receiver_arg
));
48 pthread_mutex_init(&conn
.recv_lock
, NULL
);
51 // TODO: Move this into comms.c
52 PlotGridXdefault
= 64;
53 PlotGridYdefault
= 64;
55 CursorScaleFactor
= 1;
61 pthread_create(&reader_thread
, NULL
, &uart_receiver
, &conn
);
62 // cache Version information now:
69 FILE *script_file
= NULL
;
70 char script_cmd_buf
[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest
72 if (script_cmds_file
) {
73 script_file
= fopen(script_cmds_file
, "r");
75 printf("executing commands from file: %s\n", script_cmds_file
);
79 read_history(".history");
82 // If there is a script file
85 memset(script_cmd_buf
, 0, sizeof(script_cmd_buf
));
86 if (!fgets(script_cmd_buf
, sizeof(script_cmd_buf
), script_file
)) {
90 strcleanrn(script_cmd_buf
, sizeof(script_cmd_buf
));
92 if ((cmd
= strmcopy(script_cmd_buf
)) != NULL
) {
93 printf(PROXPROMPT
"%s\n", cmd
);
97 // If there is a script command
99 if ((cmd
= strmcopy(script_cmd
)) != NULL
) {
100 printf(PROXPROMPT
"%s\n", cmd
);
105 // exit after exec command
109 // if there is a pipe from stdin
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");
116 strcleanrn(script_cmd_buf
, sizeof(script_cmd_buf
));
118 if ((cmd
= strmcopy(script_cmd_buf
)) != NULL
) {
119 printf(PROXPROMPT
"%s\n", cmd
);
123 // read command from command prompt
124 cmd
= readline(PROXPROMPT
);
132 while(cmd
[strlen(cmd
) - 1] == ' ')
133 cmd
[strlen(cmd
) - 1] = 0x00;
135 if (cmd
[0] != 0x00) {
136 int ret
= CommandReceived(cmd
);
138 if (ret
== 99) { // exit or quit
150 write_history(".history");
154 pthread_join(reader_thread
, NULL
);
162 pthread_mutex_destroy(&conn
.recv_lock
);
165 static void dumpAllHelp(int markdown
)
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");
171 command_t
*cmds
= getTopLevelCommandTable();
172 dumpCommandsRecursive(cmds
, markdown
);
175 static char *my_executable_path
= NULL
;
176 static char *my_executable_directory
= NULL
;
178 const char *get_my_executable_path(void)
180 return my_executable_path
;
183 const char *get_my_executable_directory(void)
185 return my_executable_directory
;
188 static void set_my_executable_path(void)
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';
203 static 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
);
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
);
226 int main(int argc
, char* argv
[]) {
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
;
239 show_help(true, argv
[0]);
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]);
250 if (strcmp(argv
[i
], "-m") == 0) {
255 if(strcmp(argv
[i
],"-f") == 0 || strcmp(argv
[i
],"-flush") == 0){
256 printf("Output will be flushed after every print.\n");
260 if(strcmp(argv
[i
],"-w") == 0 || strcmp(argv
[i
],"-wait") == 0){
264 if(strcmp(argv
[i
],"-c") == 0 || strcmp(argv
[i
],"-command") == 0){
265 executeCommand
= true;
268 if(strcmp(argv
[i
],"-l") == 0 || strcmp(argv
[i
],"-lua") == 0){
269 executeCommand
= true;
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] != '-') {
277 script_cmd
= argv
[argc
- 1];
279 while(script_cmd
[strlen(script_cmd
) - 1] == ' ')
280 script_cmd
[strlen(script_cmd
) - 1] = 0x00;
282 if (strlen(script_cmd
) == 0) {
286 // add "script run " to command
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
);
297 printf("Execute command from commandline: %s\n", script_cmd
);
300 script_cmds_file
= argv
[argc
- 1];
305 if (executeCommand
&& (!script_cmd
|| strlen(script_cmd
) == 0)){
306 printf("ERROR: execute command: command not found.\n");
310 // set global variables
311 set_my_executable_path();
315 sp
= uart_open(argv
[1]);
317 printf("Waiting for Proxmark to appear on %s ", argv
[1]);
321 sp
= uart_open(argv
[1]);
325 } while(++openCount
< 20 && (sp
== INVALID_SERIAL_PORT
|| sp
== CLAIMED_SERIAL_PORT
));
329 // check result of uart opening
330 if (sp
== INVALID_SERIAL_PORT
) {
331 printf("ERROR: invalid serial port\n");
333 } else if (sp
== CLAIMED_SERIAL_PORT
) {
334 printf("ERROR: serial port is claimed by another process\n");
340 // create a mutex to avoid interlacing print commands from our different threads
341 pthread_mutex_init(&print_lock
, NULL
);
345 InitGraphics(argc
, argv
, script_cmds_file
, script_cmd
, usb_present
, sp
);
348 char* display
= getenv("DISPLAY");
350 if (display
&& strlen(display
) > 1)
352 InitGraphics(argc
, argv
, script_cmds_file
, script_cmd
, usb_present
, sp
);
357 main_loop(script_cmds_file
, script_cmd
, usb_present
, sp
);
361 main_loop(script_cmds_file
, script_cmd
, usb_present
, sp
);
370 pthread_mutex_destroy(&print_lock
);