]>
Commit | Line | Data |
---|---|---|
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 | //#include "proxusb.h" | |
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 | ||
27 | // a global mutex to prevent interlaced printing from different threads | |
28 | pthread_mutex_t print_lock; | |
29 | ||
30 | static serial_port sp; | |
31 | static UsbCommand txcmd; | |
32 | volatile static bool txcmd_pending = false; | |
33 | ||
34 | ||
35 | void SendCommand(UsbCommand *c) { | |
36 | #if 0 | |
37 | printf("Sending %d bytes\n", sizeof(UsbCommand)); | |
38 | #endif | |
39 | /* | |
40 | if (txcmd_pending) { | |
41 | ERR("Sending command failed, previous command is still pending"); | |
42 | } | |
43 | */ | |
44 | if(offline) | |
45 | { | |
46 | PrintAndLog("Sending bytes to proxmark failed - offline"); | |
47 | return; | |
48 | } | |
49 | ||
50 | while(txcmd_pending); | |
51 | txcmd = *c; | |
52 | txcmd_pending = true; | |
53 | } | |
54 | ||
55 | struct receiver_arg { | |
56 | int run; | |
57 | }; | |
58 | ||
59 | struct main_loop_arg { | |
60 | int usb_present; | |
61 | char *script_cmds_file; | |
62 | }; | |
63 | ||
64 | //static void *usb_receiver(void *targ) { | |
65 | // struct receiver_arg *arg = (struct receiver_arg*)targ; | |
66 | // UsbCommand cmdbuf; | |
67 | // | |
68 | // while (arg->run) { | |
69 | // if (ReceiveCommandPoll(&cmdbuf)) { | |
70 | // UsbCommandReceived(&cmdbuf); | |
71 | // fflush(NULL); | |
72 | // } | |
73 | // } | |
74 | // | |
75 | // pthread_exit(NULL); | |
76 | // return NULL; | |
77 | //} | |
78 | ||
79 | byte_t rx[0x1000000]; | |
80 | byte_t* prx = rx; | |
81 | ||
82 | static void *uart_receiver(void *targ) { | |
83 | struct receiver_arg *arg = (struct receiver_arg*)targ; | |
84 | size_t rxlen; | |
85 | size_t cmd_count; | |
86 | ||
87 | while (arg->run) { | |
88 | rxlen = sizeof(UsbCommand); | |
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 | // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count); | |
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 | if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) { | |
104 | PrintAndLog("Sending bytes to proxmark failed"); | |
105 | } | |
106 | txcmd_pending = false; | |
107 | } | |
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, &usb_receiver, &rarg); | |
123 | pthread_create(&reader_thread, NULL, &uart_receiver, &rarg); | |
124 | } | |
125 | ||
126 | FILE *script_file = NULL; | |
127 | char script_cmd_buf[256]; | |
128 | ||
129 | if (arg->script_cmds_file) | |
130 | { | |
131 | script_file = fopen(arg->script_cmds_file, "r"); | |
132 | if (script_file) | |
133 | { | |
134 | printf("using 'scripting' commands file %s\n", arg->script_cmds_file); | |
135 | } | |
136 | } | |
137 | ||
138 | read_history(".history"); | |
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 | { | |
146 | fclose(script_file); | |
147 | script_file = NULL; | |
148 | } | |
149 | else | |
150 | { | |
151 | char *nl; | |
152 | nl = strrchr(script_cmd_buf, '\r'); | |
153 | if (nl) *nl = '\0'; | |
154 | nl = strrchr(script_cmd_buf, '\n'); | |
155 | if (nl) *nl = '\0'; | |
156 | ||
157 | if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) | |
158 | { | |
159 | memset(cmd, 0, strlen(script_cmd_buf)); | |
160 | strcpy(cmd, script_cmd_buf); | |
161 | printf("%s\n", cmd); | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | if (!script_file) | |
167 | { | |
168 | cmd = readline(PROXPROMPT); | |
169 | } | |
170 | ||
171 | if (cmd) { | |
172 | while(cmd[strlen(cmd) - 1] == ' ') | |
173 | cmd[strlen(cmd) - 1] = 0x00; | |
174 | ||
175 | if (cmd[0] != 0x00) { | |
176 | if (strncmp(cmd, "quit", 4) == 0) { | |
177 | exit(0); | |
178 | break; | |
179 | } | |
180 | ||
181 | CommandReceived(cmd); | |
182 | add_history(cmd); | |
183 | } | |
184 | free(cmd); | |
185 | } else { | |
186 | printf("\n"); | |
187 | break; | |
188 | } | |
189 | } | |
190 | ||
191 | write_history(".history"); | |
192 | ||
193 | if (arg->usb_present == 1) { | |
194 | rarg.run = 0; | |
195 | pthread_join(reader_thread, NULL); | |
196 | } | |
197 | ||
198 | if (script_file) | |
199 | { | |
200 | fclose(script_file); | |
201 | script_file = NULL; | |
202 | } | |
203 | ||
204 | ExitGraphics(); | |
205 | pthread_exit(NULL); | |
206 | return NULL; | |
207 | } | |
208 | ||
209 | int main(int argc, char* argv[]) { | |
210 | srand(time(0)); | |
211 | ||
212 | if (argc < 2) { | |
213 | printf("syntax: %s <port>\n\n",argv[0]); | |
214 | printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]); | |
215 | return 1; | |
216 | } | |
217 | ||
218 | // Make sure to initialize | |
219 | struct main_loop_arg marg = { | |
220 | .usb_present = 0, | |
221 | .script_cmds_file = NULL | |
222 | }; | |
223 | pthread_t main_loop_t; | |
224 | ||
225 | /* | |
226 | usb_init(); | |
227 | if (!OpenProxmark(1)) { | |
228 | fprintf(stderr,"PROXMARK3: NOT FOUND!\n"); | |
229 | marg.usb_present = 0; | |
230 | offline = 1; | |
231 | } else { | |
232 | marg.usb_present = 1; | |
233 | offline = 0; | |
234 | } | |
235 | */ | |
236 | ||
237 | sp = uart_open(argv[1]); | |
238 | if (sp == INVALID_SERIAL_PORT) { | |
239 | printf("ERROR: invalid serial port\n"); | |
240 | marg.usb_present = 0; | |
241 | offline = 1; | |
242 | } else if (sp == CLAIMED_SERIAL_PORT) { | |
243 | printf("ERROR: serial port is claimed by another process\n"); | |
244 | marg.usb_present = 0; | |
245 | offline = 1; | |
246 | } else { | |
247 | marg.usb_present = 1; | |
248 | offline = 0; | |
249 | } | |
250 | ||
251 | // If the user passed the filename of the 'script' to execute, get it | |
252 | if (argc > 2 && argv[2]) { | |
253 | if (argv[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry. | |
254 | argv[2][1] == 'l' && | |
255 | argv[2][2] == 'u' && | |
256 | argv[2][3] == 's' && | |
257 | argv[2][4] == 'h') | |
258 | { | |
259 | printf("Output will be flushed after every print.\n"); | |
260 | flushAfterWrite = 1; | |
261 | } | |
262 | else | |
263 | marg.script_cmds_file = argv[2]; | |
264 | } | |
265 | ||
266 | // create a mutex to avoid interlacing print commands from our different threads | |
267 | pthread_mutex_init(&print_lock, NULL); | |
268 | ||
269 | pthread_create(&main_loop_t, NULL, &main_loop, &marg); | |
270 | InitGraphics(argc, argv); | |
271 | ||
272 | MainGraphics(); | |
273 | ||
274 | pthread_join(main_loop_t, NULL); | |
275 | ||
276 | // if (marg.usb_present == 1) { | |
277 | // CloseProxmark(); | |
278 | // } | |
279 | ||
280 | // Clean up the port | |
281 | uart_close(sp); | |
282 | ||
283 | // clean up mutex | |
284 | pthread_mutex_destroy(&print_lock); | |
285 | ||
286 | return 0; | |
287 | } |