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"
26 #include "cmdparser.h"
31 // a global mutex to prevent interlaced printing from different threads
32 pthread_mutex_t print_lock
;
34 static serial_port sp
;
35 static UsbCommand txcmd
;
36 volatile static bool txcmd_pending
= false;
38 void SendCommand(UsbCommand
*c
) {
40 printf("Sending %d bytes\n", sizeof(UsbCommand
));
44 PrintAndLog("Sending bytes to proxmark failed - offline");
48 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
49 or disconnected. The main console thread is alive, but comm thread just spins here.
62 struct main_loop_arg
{
64 char *script_cmds_file
;
70 // static void showBanner(void){
71 // printf("██████╗ ███╗ ███╗ ████╗ ...Iceman fork\n");
72 // printf("██╔══██╗████╗ ████║ ══█║\n");
73 // printf("██████╔╝██╔████╔██║ ████╔╝\n");
74 // printf("██╔═══╝ ██║╚██╔╝██║ ══█║ iceman@icesql.net\n");
75 // printf("██║ ██║ ╚═╝ ██║ ████╔╝ https://github.com/iceman1001/proxmark3\n");
76 // printf("╚═╝ ╚═╝ ╚═╝ ╚═══╝v1.7.0\n");
80 static void *uart_receiver(void *targ
) {
81 struct receiver_arg
*arg
= (struct receiver_arg
*)targ
;
87 rxlen
= sizeof(UsbCommand
);
89 if (uart_receive(sp
, prx
, &rxlen
)) {
91 if (((prx
-rx
) % sizeof(UsbCommand
)) != 0)
94 cmd_count
= (prx
-rx
) / sizeof(UsbCommand
);
96 for (size_t i
= 0; i
< cmd_count
; i
++)
97 UsbCommandReceived((UsbCommand
*)( rx
+ ( i
* sizeof(UsbCommand
))));
103 bool res
= uart_send(sp
, (byte_t
*) &txcmd
, sizeof(UsbCommand
));
105 PrintAndLog("Sending bytes to proxmark failed");
107 txcmd_pending
= false;
114 static void *main_loop(void *targ
) {
115 struct main_loop_arg
*arg
= (struct main_loop_arg
*)targ
;
116 struct receiver_arg rarg
;
118 pthread_t reader_thread
;
120 if (arg
->usb_present
== 1) {
122 pthread_create(&reader_thread
, NULL
, &uart_receiver
, &rarg
);
123 // cache Version information now:
127 FILE *script_file
= NULL
;
128 char script_cmd_buf
[256] = {0x00}; // iceman, needs lua script the same file_path_buffer as the rest
130 if (arg
->script_cmds_file
) {
131 script_file
= fopen(arg
->script_cmds_file
, "r");
134 printf("using 'scripting' commands file %s\n", arg
->script_cmds_file
);
137 read_history(".history");
141 // If there is a script file
144 if (!fgets(script_cmd_buf
, sizeof(script_cmd_buf
), script_file
)) {
149 nl
= strrchr(script_cmd_buf
, '\r');
153 nl
= strrchr(script_cmd_buf
, '\n');
158 int newlen
= strlen(script_cmd_buf
);
159 if ((cmd
= (char*) malloc( newlen
+ 1)) != NULL
) {
160 memset(cmd
, 0x00, newlen
);
161 strcpy(cmd
, script_cmd_buf
);
166 cmd
= readline(PROXPROMPT
);
169 // this one should pick up all non-null cmd...
172 if (strlen(cmd
) > 0) {
173 while(cmd
[strlen(cmd
) - 1] == ' ')
174 cmd
[strlen(cmd
) - 1] = 0x00;
177 if (cmd
[0] != 0x00) {
178 int ret
= CommandReceived(cmd
);
197 write_history(".history");
202 if (arg
->usb_present
== 1) {
204 pthread_join(reader_thread
, NULL
);
212 static void dumpAllHelp(int markdown
)
214 printf("\n%sProxmark3 command dump%s\n\n",markdown
?"# ":"",markdown
?"":"\n======================");
215 printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown
?" ":"");
216 printf("Check column \"offline\" for their availability.\n");
218 command_t
*cmds
= getTopLevelCommandTable();
219 dumpCommandsRecursive(cmds
, markdown
);
222 static char *my_executable_path
= NULL
;
223 static char *my_executable_directory
= NULL
;
225 const char *get_my_executable_path(void)
227 return my_executable_path
;
230 const char *get_my_executable_directory(void)
232 return my_executable_directory
;
235 static void set_my_executable_path(void)
237 int path_length
= wai_getExecutablePath(NULL
, 0, NULL
);
238 if (path_length
!= -1) {
239 my_executable_path
= (char*)malloc(path_length
+ 1);
240 int dirname_length
= 0;
241 if (wai_getExecutablePath(my_executable_path
, path_length
, &dirname_length
) != -1) {
242 my_executable_path
[path_length
] = '\0';
243 my_executable_directory
= (char *)malloc(dirname_length
+ 2);
244 strncpy(my_executable_directory
, my_executable_path
, dirname_length
+1);
249 int main(int argc
, char* argv
[]) {
253 printf("syntax: %s <port>\n\n",argv
[0]);
254 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv
[0]);
255 printf("help: %s -h\n\n", argv
[0]);
256 printf("\tDump all interactive help at once\n");
257 printf("markdown: %s -m\n\n", argv
[0]);
258 printf("\tDump all interactive help at once in markdown syntax\n");
261 if (strcmp(argv
[1], "-h") == 0) {
262 printf("syntax: %s <port>\n\n",argv
[0]);
263 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv
[0]);
267 if (strcmp(argv
[1], "-m") == 0) {
272 set_my_executable_path();
274 // Make sure to initialize
275 struct main_loop_arg marg
= {
277 .script_cmds_file
= NULL
280 pthread_t main_loop_threat
;
282 sp
= uart_open(argv
[1]);
283 if (sp
== INVALID_SERIAL_PORT
) {
284 printf("ERROR: invalid serial port\n");
285 marg
.usb_present
= 0;
287 } else if (sp
== CLAIMED_SERIAL_PORT
) {
288 printf("ERROR: serial port is claimed by another process\n");
289 marg
.usb_present
= 0;
292 marg
.usb_present
= 1;
296 // If the user passed the filename of the 'script' to execute, get it
297 if (argc
> 2 && argv
[2]) {
298 if (argv
[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry.
304 printf("Output will be flushed after every print.\n");
308 marg
.script_cmds_file
= argv
[2];
312 // create a mutex to avoid interlacing print commands from our different threads
313 pthread_mutex_init(&print_lock
, NULL
);
315 pthread_create(&main_loop_threat
, NULL
, &main_loop
, &marg
);
316 InitGraphics(argc
, argv
);
320 pthread_join(main_loop_threat
, NULL
);
327 pthread_mutex_destroy(&print_lock
);