]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
dad2d86bfdfec512c2c8d4084bc4aa31bf4a068a
[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 <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <pthread.h>
16 #include <unistd.h>
17 #include <readline/readline.h>
18 #include <readline/history.h>
19
20 #include "proxmark3.h"
21 #include "proxgui.h"
22 #include "cmdmain.h"
23 #include "uart.h"
24 #include "ui.h"
25 #include "sleep.h"
26 #include "cmdparser.h"
27 #include "cmdhw.h"
28 #include "whereami.h"
29
30
31 // a global mutex to prevent interlaced printing from different threads
32 pthread_mutex_t print_lock;
33
34 static serial_port sp;
35 static UsbCommand txcmd;
36 volatile static bool txcmd_pending = false;
37
38 void SendCommand(UsbCommand *c) {
39 #if 0
40 printf("Sending %d bytes\n", sizeof(UsbCommand));
41 #endif
42
43 if (offline) {
44 PrintAndLog("Sending bytes to proxmark failed - offline");
45 return;
46 }
47 /**
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.
50 Not good.../holiman
51 **/
52 while(txcmd_pending);
53
54 txcmd = *c;
55 txcmd_pending = true;
56 }
57
58 struct receiver_arg {
59 int run;
60 };
61
62 struct main_loop_arg {
63 int usb_present;
64 char *script_cmds_file;
65 };
66
67 byte_t rx[0x1000000];
68 byte_t* prx = rx;
69
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");
77 // }
78
79
80 static void *uart_receiver(void *targ) {
81 struct receiver_arg *arg = (struct receiver_arg*)targ;
82 size_t rxlen;
83 size_t cmd_count;
84
85 while (arg->run) {
86
87 rxlen = sizeof(UsbCommand);
88
89 if (uart_receive(sp, prx, &rxlen)) {
90 prx += rxlen;
91 if (((prx-rx) % sizeof(UsbCommand)) != 0)
92 continue;
93
94 cmd_count = (prx-rx) / sizeof(UsbCommand);
95
96 for (size_t i = 0; i < cmd_count; i++)
97 UsbCommandReceived((UsbCommand*)( rx + ( i * sizeof(UsbCommand))));
98
99 }
100 prx = rx;
101
102 if (txcmd_pending) {
103 bool res = uart_send(sp, (byte_t*) &txcmd, sizeof(UsbCommand));
104 if (!res) {
105 PrintAndLog("Sending bytes to proxmark failed");
106 }
107 txcmd_pending = false;
108 }
109 }
110 pthread_exit(NULL);
111 return NULL;
112 }
113
114 static void *main_loop(void *targ) {
115 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
116 struct receiver_arg rarg;
117 char *cmd = NULL;
118 pthread_t reader_thread;
119
120 if (arg->usb_present == 1) {
121 rarg.run = 1;
122 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
123 // cache Version information now:
124 CmdVersion(NULL);
125 }
126
127 FILE *script_file = NULL;
128 char script_cmd_buf[256] = {0x00}; // iceman, needs lua script the same file_path_buffer as the rest
129
130 if (arg->script_cmds_file) {
131 script_file = fopen(arg->script_cmds_file, "r");
132
133 if (script_file)
134 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
135 }
136
137 read_history(".history");
138
139 while(1) {
140
141 // If there is a script file
142 if (script_file) {
143
144 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) {
145 fclose(script_file);
146 script_file = NULL;
147 } else {
148 char *nl;
149 nl = strrchr(script_cmd_buf, '\r');
150 if (nl)
151 *nl = '\0';
152
153 nl = strrchr(script_cmd_buf, '\n');
154
155 if (nl)
156 *nl = '\0';
157
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);
162 printf("%s\n", cmd);
163 }
164 }
165 } else {
166 cmd = readline(PROXPROMPT);
167 }
168
169 // this one should pick up all non-null cmd...
170 // why is there a
171 if (cmd) {
172 if (strlen(cmd) > 0) {
173 while(cmd[strlen(cmd) - 1] == ' ')
174 cmd[strlen(cmd) - 1] = 0x00;
175 }
176
177 if (cmd[0] != 0x00) {
178 int ret = CommandReceived(cmd);
179 add_history(cmd);
180
181 // exit or quit
182 if (ret == 99)
183 break;
184 }
185 free(cmd);
186 cmd = 0;
187 } else {
188 printf("\n");
189 break;
190 }
191 }
192
193 if (script_file) {
194 fclose(script_file);
195 }
196
197 write_history(".history");
198
199 free(cmd);
200 cmd = 0;
201
202 if (arg->usb_present == 1) {
203 rarg.run = 0;
204 pthread_join(reader_thread, NULL);
205 }
206
207 ExitGraphics();
208 pthread_exit(NULL);
209 return NULL;
210 }
211
212 static void dumpAllHelp(int markdown)
213 {
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");
217 printf("\n");
218 command_t *cmds = getTopLevelCommandTable();
219 dumpCommandsRecursive(cmds, markdown);
220 }
221
222 static char *my_executable_path = NULL;
223 static char *my_executable_directory = NULL;
224
225 const char const *get_my_executable_path(void)
226 {
227 return my_executable_path;
228 }
229
230 const char const *get_my_executable_directory(void)
231 {
232 return my_executable_directory;
233 }
234
235 static void set_my_executable_path(void)
236 {
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);
245 }
246 }
247 }
248
249 int main(int argc, char* argv[]) {
250 srand(time(0));
251
252 if (argc < 2) {
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");
259 return 1;
260 }
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]);
264 dumpAllHelp(0);
265 return 0;
266 }
267 if (strcmp(argv[1], "-m") == 0) {
268 dumpAllHelp(1);
269 return 0;
270 }
271
272 set_my_executable_path();
273
274 // Make sure to initialize
275 struct main_loop_arg marg = {
276 .usb_present = 0,
277 .script_cmds_file = NULL
278 };
279
280 pthread_t main_loop_threat;
281
282 sp = uart_open(argv[1]);
283 if (sp == INVALID_SERIAL_PORT) {
284 printf("ERROR: invalid serial port\n");
285 marg.usb_present = 0;
286 offline = 1;
287 } else if (sp == CLAIMED_SERIAL_PORT) {
288 printf("ERROR: serial port is claimed by another process\n");
289 marg.usb_present = 0;
290 offline = 1;
291 } else {
292 marg.usb_present = 1;
293 offline = 0;
294 }
295
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.
299 argv[2][1] == 'l' &&
300 argv[2][2] == 'u' &&
301 argv[2][3] == 's' &&
302 argv[2][4] == 'h')
303 {
304 printf("Output will be flushed after every print.\n");
305 flushAfterWrite = 1;
306 }
307 else {
308 marg.script_cmds_file = argv[2];
309 }
310 }
311
312 // create a mutex to avoid interlacing print commands from our different threads
313 pthread_mutex_init(&print_lock, NULL);
314
315 pthread_create(&main_loop_threat, NULL, &main_loop, &marg);
316 InitGraphics(argc, argv);
317
318 MainGraphics();
319
320 pthread_join(main_loop_threat, NULL);
321
322 // Clean up the port
323 if (offline == 0)
324 uart_close(sp);
325
326 // clean up mutex
327 pthread_mutex_destroy(&print_lock);
328
329 exit(0);
330 }
Impressum, Datenschutz