]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
88cb5fa76db22a705172e9318fa566f9205b9a15
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"
26 #include "cmdparser.h"
32 #ifdef __has_attribute
33 #if __has_attribute(force_align_arg_pointer)
34 __attribute__((force_align_arg_pointer
))
37 main_loop(char *script_cmds_file
, char *script_cmd
, bool usb_present
) {
40 pthread_t reader_thread
;
41 bool execCommand
= (script_cmd
!= NULL
);
42 bool stdinOnPipe
= !isatty(STDIN_FILENO
);
44 memset(&conn
, 0, sizeof(receiver_arg
));
49 pthread_create(&reader_thread
, NULL
, &uart_receiver
, &conn
);
50 // cache Version information now:
57 FILE *script_file
= NULL
;
58 char script_cmd_buf
[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest
60 if (script_cmds_file
) {
61 script_file
= fopen(script_cmds_file
, "r");
63 printf("executing commands from file: %s\n", script_cmds_file
);
67 read_history(".history");
70 // If there is a script file
73 memset(script_cmd_buf
, 0, sizeof(script_cmd_buf
));
74 if (!fgets(script_cmd_buf
, sizeof(script_cmd_buf
), script_file
)) {
78 strcleanrn(script_cmd_buf
, sizeof(script_cmd_buf
));
80 if ((cmd
= strmcopy(script_cmd_buf
)) != NULL
) {
81 printf(PROXPROMPT
"%s\n", cmd
);
85 // If there is a script command
87 if ((cmd
= strmcopy(script_cmd
)) != NULL
) {
88 printf(PROXPROMPT
"%s\n", cmd
);
93 // exit after exec command
97 // if there is a pipe from stdin
99 memset(script_cmd_buf
, 0, sizeof(script_cmd_buf
));
100 if (!fgets(script_cmd_buf
, sizeof(script_cmd_buf
), stdin
)) {
101 printf("\nStdin end. Exit...\n");
104 strcleanrn(script_cmd_buf
, sizeof(script_cmd_buf
));
106 if ((cmd
= strmcopy(script_cmd_buf
)) != NULL
) {
107 printf(PROXPROMPT
"%s\n", cmd
);
111 // read command from command prompt
112 cmd
= readline(PROXPROMPT
);
120 while(cmd
[strlen(cmd
) - 1] == ' ')
121 cmd
[strlen(cmd
) - 1] = 0x00;
123 if (cmd
[0] != 0x00) {
124 int ret
= CommandReceived(cmd
);
126 if (ret
== 99) { // exit or quit
138 write_history(".history");
142 pthread_join(reader_thread
, NULL
);
151 static void dumpAllHelp(int markdown
)
153 printf("\n%sProxmark3 command dump%s\n\n",markdown
?"# ":"",markdown
?"":"\n======================");
154 printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown
?" ":"");
155 printf("Check column \"offline\" for their availability.\n");
157 command_t
*cmds
= getTopLevelCommandTable();
158 dumpCommandsRecursive(cmds
, markdown
);
161 static char *my_executable_path
= NULL
;
162 static char *my_executable_directory
= NULL
;
164 const char *get_my_executable_path(void)
166 return my_executable_path
;
169 const char *get_my_executable_directory(void)
171 return my_executable_directory
;
174 static void set_my_executable_path(void)
176 int path_length
= wai_getExecutablePath(NULL
, 0, NULL
);
177 if (path_length
!= -1) {
178 my_executable_path
= (char*)malloc(path_length
+ 1);
179 int dirname_length
= 0;
180 if (wai_getExecutablePath(my_executable_path
, path_length
, &dirname_length
) != -1) {
181 my_executable_path
[path_length
] = '\0';
182 my_executable_directory
= (char *)malloc(dirname_length
+ 2);
183 strncpy(my_executable_directory
, my_executable_path
, dirname_length
+1);
184 my_executable_directory
[dirname_length
+1] = '\0';
189 static void show_help(bool showFullHelp
, char *command_line
){
190 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
);
191 printf("\texample: %s "SERIAL_PORT_H
"\n\n", command_line
);
194 printf("help: <-h|-help> Dump all interactive command's help at once.\n");
195 printf("\t%s -h\n\n", command_line
);
196 printf("markdown: <-m> Dump all interactive help at once in markdown syntax\n");
197 printf("\t%s -m\n\n", command_line
);
198 printf("flush: <-f|-flush> Output will be flushed after every print.\n");
199 printf("\t%s -f\n\n", command_line
);
200 printf("wait: <-w|-wait> 20sec waiting the serial port to appear in the OS\n");
201 printf("\t%s "SERIAL_PORT_H
" -w\n\n", command_line
);
202 printf("script: A script file with one proxmark3 command per line.\n\n");
203 printf("command: <-c|-command> Execute one proxmark3 command.\n");
204 printf("\t%s "SERIAL_PORT_H
" -c \"hf mf chk 1* ?\"\n", command_line
);
205 printf("\t%s "SERIAL_PORT_H
" -command \"hf mf nested 1 *\"\n\n", command_line
);
206 printf("lua: <-l|-lua> Execute lua script.\n");
207 printf("\t%s "SERIAL_PORT_H
" -l hf_read\n\n", command_line
);
211 int main(int argc
, char* argv
[]) {
214 bool usb_present
= false;
215 bool waitCOMPort
= false;
216 bool executeCommand
= false;
217 bool addLuaExec
= false;
218 char *script_cmds_file
= NULL
;
219 char *script_cmd
= NULL
;
222 show_help(true, argv
[0]);
226 for (int i
= 1; i
< argc
; i
++) {
227 if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
],"-help") == 0) {
228 show_help(false, argv
[0]);
233 if (strcmp(argv
[i
], "-m") == 0) {
238 if(strcmp(argv
[i
],"-f") == 0 || strcmp(argv
[i
],"-flush") == 0){
239 printf("Output will be flushed after every print.\n");
240 SetFlushAfterWrite(true);
243 if(strcmp(argv
[i
],"-w") == 0 || strcmp(argv
[i
],"-wait") == 0){
247 if(strcmp(argv
[i
],"-c") == 0 || strcmp(argv
[i
],"-command") == 0){
248 executeCommand
= true;
251 if(strcmp(argv
[i
],"-l") == 0 || strcmp(argv
[i
],"-lua") == 0){
252 executeCommand
= true;
257 // If the user passed the filename of the 'script' to execute, get it from last parameter
258 if (argc
> 2 && argv
[argc
- 1] && argv
[argc
- 1][0] != '-') {
260 script_cmd
= argv
[argc
- 1];
262 while(script_cmd
[strlen(script_cmd
) - 1] == ' ')
263 script_cmd
[strlen(script_cmd
) - 1] = 0x00;
265 if (strlen(script_cmd
) == 0) {
269 // add "script run " to command
271 int len
= strlen(script_cmd
) + 11 + 1;
272 if ((ctmp
= (char*) malloc(len
)) != NULL
) {
273 memset(ctmp
, 0, len
);
274 strcpy(ctmp
, "script run ");
275 strcpy(&ctmp
[11], script_cmd
);
280 printf("Execute command from commandline: %s\n", script_cmd
);
283 script_cmds_file
= argv
[argc
- 1];
288 if (executeCommand
&& (!script_cmd
|| strlen(script_cmd
) == 0)){
289 printf("ERROR: execute command: command not found.\n");
293 // set global variables
294 set_my_executable_path();
296 // try to open USB connection to Proxmark
297 usb_present
= OpenProxmark(argv
[1], waitCOMPort
, 20);
301 InitGraphics(argc
, argv
, script_cmds_file
, script_cmd
, usb_present
);
304 char* display
= getenv("DISPLAY");
306 if (display
&& strlen(display
) > 1)
308 InitGraphics(argc
, argv
, script_cmds_file
, script_cmd
, usb_present
);
313 main_loop(script_cmds_file
, script_cmd
, usb_present
);
317 main_loop(script_cmds_file
, script_cmd
, usb_present
);