]>
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> | |
10403a6a | 19 | |
6658905f | 20 | #include "proxmark3.h" |
21 | #include "proxgui.h" | |
7fe9b0b7 | 22 | #include "cmdmain.h" |
902cb3c0 | 23 | #include "uart.h" |
902cb3c0 | 24 | #include "ui.h" |
759c16b3 | 25 | #include "sleep.h" |
57c69556 | 26 | #include "cmdparser.h" |
9783989b | 27 | #include "cmdhw.h" |
6ac23014 | 28 | #include "whereami.h" |
29 | ||
902cb3c0 | 30 | |
9492e0b0 | 31 | // a global mutex to prevent interlaced printing from different threads |
32 | pthread_mutex_t print_lock; | |
33 | ||
902cb3c0 | 34 | static serial_port sp; |
f0ba6342 | 35 | static UsbCommand txcmd; |
c3385024 | 36 | volatile static bool txcmd_pending = false; |
902cb3c0 | 37 | |
38 | void SendCommand(UsbCommand *c) { | |
9484ff3d | 39 | #if 0 |
87b28d31 | 40 | printf("Sending %d bytes\n", sizeof(UsbCommand)); |
9484ff3d | 41 | #endif |
14edfd09 | 42 | |
43 | if (offline) { | |
87b28d31 | 44 | PrintAndLog("Sending bytes to proxmark failed - offline"); |
45 | return; | |
46 | } | |
47 | /** | |
72e930ef | 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 | **/ | |
87b28d31 | 52 | while(txcmd_pending); |
53 | ||
54 | txcmd = *c; | |
55 | txcmd_pending = true; | |
902cb3c0 | 56 | } |
6658905f | 57 | |
902cb3c0 | 58 | struct receiver_arg { |
ad5bc8cc | 59 | int run; |
6658905f | 60 | }; |
61 | ||
902cb3c0 | 62 | struct main_loop_arg { |
ad5bc8cc | 63 | int usb_present; |
64 | char *script_cmds_file; | |
a60612db | 65 | }; |
66 | ||
902cb3c0 | 67 | byte_t rx[0x1000000]; |
fe7bfa78 | 68 | byte_t* prx = rx; |
902cb3c0 | 69 | |
ad5bc8cc | 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"); | |
6ac23014 | 76 | // printf("╚═╝ ╚═╝ ╚═╝ ╚═══╝v1.7.0\n"); |
ad5bc8cc | 77 | // } |
78 | ||
79 | ||
902cb3c0 | 80 | static void *uart_receiver(void *targ) { |
87b28d31 | 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); | |
9484ff3d | 88 | |
9484ff3d | 89 | if (uart_receive(sp, prx, &rxlen)) { |
87b28d31 | 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) { | |
3e83ff21 | 103 | bool res = uart_send(sp, (byte_t*) &txcmd, sizeof(UsbCommand)); |
104 | if (!res) { | |
87b28d31 | 105 | PrintAndLog("Sending bytes to proxmark failed"); |
106 | } | |
107 | txcmd_pending = false; | |
108 | } | |
109 | } | |
87b28d31 | 110 | pthread_exit(NULL); |
111 | return NULL; | |
6658905f | 112 | } |
113 | ||
902cb3c0 | 114 | static void *main_loop(void *targ) { |
87b28d31 | 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; | |
902cb3c0 | 119 | |
87b28d31 | 120 | if (arg->usb_present == 1) { |
9484ff3d | 121 | rarg.run = 1; |
87b28d31 | 122 | pthread_create(&reader_thread, NULL, &uart_receiver, &rarg); |
9783989b | 123 | // cache Version information now: |
124 | CmdVersion(NULL); | |
87b28d31 | 125 | } |
9484ff3d | 126 | |
87b28d31 | 127 | FILE *script_file = NULL; |
128 | char script_cmd_buf[256] = {0x00}; // iceman, needs lua script the same file_path_buffer as the rest | |
9484ff3d | 129 | |
14edfd09 | 130 | if (arg->script_cmds_file) { |
87b28d31 | 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 | } | |
7fe9b0b7 | 136 | |
8556b852 | 137 | read_history(".history"); |
14edfd09 | 138 | |
139 | while(1) { | |
140 | ||
87b28d31 | 141 | // If there is a script file |
aacb96d7 | 142 | if (script_file) { |
143 | ||
14edfd09 | 144 | if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) { |
87b28d31 | 145 | fclose(script_file); |
146 | script_file = NULL; | |
14edfd09 | 147 | } else { |
87b28d31 | 148 | char *nl; |
149 | nl = strrchr(script_cmd_buf, '\r'); | |
150 | if (nl) | |
151 | *nl = '\0'; | |
1d660bb9 | 152 | |
87b28d31 | 153 | nl = strrchr(script_cmd_buf, '\n'); |
154 | ||
155 | if (nl) | |
156 | *nl = '\0'; | |
aacb96d7 | 157 | |
158 | int newlen = strlen(script_cmd_buf); | |
159 | if ((cmd = (char*) malloc( newlen + 1)) != NULL) { | |
160 | memset(cmd, 0x00, newlen); | |
87b28d31 | 161 | strcpy(cmd, script_cmd_buf); |
162 | printf("%s\n", cmd); | |
163 | } | |
164 | } | |
165 | } else { | |
166 | cmd = readline(PROXPROMPT); | |
1f947c4b | 167 | } |
168 | ||
95215e87 | 169 | // this one should pick up all non-null cmd... |
170 | // why is there a | |
8556b852 | 171 | if (cmd) { |
0325c12f GG |
172 | if (strlen(cmd) > 0) { |
173 | while(cmd[strlen(cmd) - 1] == ' ') | |
174 | cmd[strlen(cmd) - 1] = 0x00; | |
175 | } | |
87b28d31 | 176 | |
8556b852 | 177 | if (cmd[0] != 0x00) { |
cc3c0a51 | 178 | int ret = CommandReceived(cmd); |
179 | add_history(cmd); | |
87b28d31 | 180 | |
181 | // exit or quit | |
182 | if (ret == 99) | |
8556b852 | 183 | break; |
4b3655e7 | 184 | } |
75cf8623 | 185 | free(cmd); |
95215e87 | 186 | cmd = 0; |
8556b852 M |
187 | } else { |
188 | printf("\n"); | |
189 | break; | |
190 | } | |
191 | } | |
aacb96d7 | 192 | |
193 | if (script_file) { | |
194 | fclose(script_file); | |
aacb96d7 | 195 | } |
196 | ||
51969283 | 197 | write_history(".history"); |
109def22 | 198 | |
199 | free(cmd); | |
95215e87 | 200 | cmd = 0; |
109def22 | 201 | |
87b28d31 | 202 | if (arg->usb_present == 1) { |
203 | rarg.run = 0; | |
204 | pthread_join(reader_thread, NULL); | |
205 | } | |
9484ff3d | 206 | |
87b28d31 | 207 | ExitGraphics(); |
208 | pthread_exit(NULL); | |
209 | return NULL; | |
6658905f | 210 | } |
211 | ||
dec8e8bd | 212 | static void dumpAllHelp(int markdown) |
ae7aa73d | 213 | { |
87b28d31 | 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); | |
ae7aa73d PT |
220 | } |
221 | ||
6ac23014 | 222 | static char *my_executable_path = NULL; |
223 | static char *my_executable_directory = NULL; | |
224 | ||
1b6cc974 | 225 | const char *get_my_executable_path(void) |
6ac23014 | 226 | { |
227 | return my_executable_path; | |
228 | } | |
229 | ||
1b6cc974 | 230 | const char *get_my_executable_directory(void) |
6ac23014 | 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 | ||
902cb3c0 | 249 | int main(int argc, char* argv[]) { |
9492e0b0 | 250 | srand(time(0)); |
125a98a1 | 251 | |
9492e0b0 | 252 | if (argc < 2) { |
253 | printf("syntax: %s <port>\n\n",argv[0]); | |
254 | printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]); | |
dec8e8bd PT |
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"); | |
9492e0b0 | 259 | return 1; |
260 | } | |
dec8e8bd PT |
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 | } | |
6ac23014 | 271 | |
272 | set_my_executable_path(); | |
273 | ||
9492e0b0 | 274 | // Make sure to initialize |
275 | struct main_loop_arg marg = { | |
276 | .usb_present = 0, | |
277 | .script_cmds_file = NULL | |
278 | }; | |
87b28d31 | 279 | |
cc3c0a51 | 280 | pthread_t main_loop_threat; |
1f947c4b | 281 | |
1bf1db84 | 282 | sp = uart_open(argv[1]); |
9492e0b0 | 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 | } | |
7fe9b0b7 | 295 | |
9492e0b0 | 296 | // If the user passed the filename of the 'script' to execute, get it |
297 | if (argc > 2 && argv[2]) { | |
ed77aabe | 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 | } | |
87b28d31 | 307 | else { |
308 | marg.script_cmds_file = argv[2]; | |
309 | } | |
9492e0b0 | 310 | } |
ed77aabe | 311 | |
9492e0b0 | 312 | // create a mutex to avoid interlacing print commands from our different threads |
313 | pthread_mutex_init(&print_lock, NULL); | |
7fe9b0b7 | 314 | |
cc3c0a51 | 315 | pthread_create(&main_loop_threat, NULL, &main_loop, &marg); |
9492e0b0 | 316 | InitGraphics(argc, argv); |
7fe9b0b7 | 317 | |
9492e0b0 | 318 | MainGraphics(); |
319 | ||
cc3c0a51 | 320 | pthread_join(main_loop_threat, NULL); |
7fe9b0b7 | 321 | |
9492e0b0 | 322 | // Clean up the port |
87b28d31 | 323 | if (offline == 0) |
324 | uart_close(sp); | |
9492e0b0 | 325 | |
326 | // clean up mutex | |
327 | pthread_mutex_destroy(&print_lock); | |
46a33c18 | 328 | |
cc3c0a51 | 329 | exit(0); |
6658905f | 330 | } |