]>
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> | |
902cb3c0 | 19 | //#include "proxusb.h" |
6658905f | 20 | #include "proxmark3.h" |
21 | #include "proxgui.h" | |
7fe9b0b7 | 22 | #include "cmdmain.h" |
902cb3c0 | 23 | #include "uart.h" |
24 | #include "messages.h" | |
25 | #include "ui.h" | |
759c16b3 | 26 | #include "sleep.h" |
902cb3c0 | 27 | |
28 | static serial_port sp; | |
f0ba6342 | 29 | static UsbCommand txcmd; |
30 | static bool txcmd_pending = false; | |
902cb3c0 | 31 | |
32 | void SendCommand(UsbCommand *c) { | |
33 | #if 0 | |
34 | printf("Sending %d bytes\n", sizeof(UsbCommand)); | |
35 | #endif | |
f0ba6342 | 36 | /* |
37 | if (txcmd_pending) { | |
38 | ERR("Sending command failed, previous command is still pending"); | |
902cb3c0 | 39 | } |
f0ba6342 | 40 | */ |
41 | while(txcmd_pending); | |
42 | txcmd = *c; | |
43 | txcmd_pending = true; | |
902cb3c0 | 44 | } |
6658905f | 45 | |
902cb3c0 | 46 | struct receiver_arg { |
7fe9b0b7 | 47 | int run; |
6658905f | 48 | }; |
49 | ||
902cb3c0 | 50 | struct main_loop_arg { |
7fe9b0b7 | 51 | int usb_present; |
1f947c4b | 52 | char *script_cmds_file; |
a60612db | 53 | }; |
54 | ||
902cb3c0 | 55 | //static void *usb_receiver(void *targ) { |
56 | // struct receiver_arg *arg = (struct receiver_arg*)targ; | |
57 | // UsbCommand cmdbuf; | |
58 | // | |
59 | // while (arg->run) { | |
60 | // if (ReceiveCommandPoll(&cmdbuf)) { | |
61 | // UsbCommandReceived(&cmdbuf); | |
62 | // fflush(NULL); | |
63 | // } | |
64 | // } | |
65 | // | |
66 | // pthread_exit(NULL); | |
67 | // return NULL; | |
68 | //} | |
7fe9b0b7 | 69 | |
902cb3c0 | 70 | byte_t rx[0x1000000]; |
fe7bfa78 | 71 | byte_t* prx = rx; |
902cb3c0 | 72 | |
73 | static void *uart_receiver(void *targ) { | |
74 | struct receiver_arg *arg = (struct receiver_arg*)targ; | |
75 | size_t rxlen; | |
76 | size_t cmd_count; | |
77 | ||
7fe9b0b7 | 78 | while (arg->run) { |
af65f5f7 | 79 | rxlen = sizeof(UsbCommand); |
fe7bfa78 | 80 | if (uart_receive(sp,prx,&rxlen)) { |
81 | prx += rxlen; | |
82 | if (((prx-rx) % sizeof(UsbCommand)) != 0) { | |
902cb3c0 | 83 | continue; |
84 | } | |
fe7bfa78 | 85 | cmd_count = (prx-rx) / sizeof(UsbCommand); |
86 | // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count); | |
902cb3c0 | 87 | for (size_t i=0; i<cmd_count; i++) { |
88 | UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand)))); | |
89 | } | |
7fe9b0b7 | 90 | } |
fe7bfa78 | 91 | prx = rx; |
92 | ||
f0ba6342 | 93 | if(txcmd_pending) { |
94 | if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) { | |
95 | PrintAndLog("Sending bytes to proxmark failed"); | |
96 | } | |
97 | txcmd_pending = false; | |
98 | } | |
7fe9b0b7 | 99 | } |
902cb3c0 | 100 | |
7fe9b0b7 | 101 | pthread_exit(NULL); |
4cd41f34 | 102 | return NULL; |
6658905f | 103 | } |
104 | ||
902cb3c0 | 105 | static void *main_loop(void *targ) { |
106 | struct main_loop_arg *arg = (struct main_loop_arg*)targ; | |
107 | struct receiver_arg rarg; | |
108 | char *cmd = NULL; | |
109 | pthread_t reader_thread; | |
110 | ||
111 | if (arg->usb_present == 1) { | |
112 | rarg.run=1; | |
113 | // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg); | |
114 | pthread_create(&reader_thread, NULL, &uart_receiver, &rarg); | |
115 | } | |
116 | ||
117 | FILE *script_file = NULL; | |
118 | char script_cmd_buf[256]; | |
119 | ||
120 | if (arg->script_cmds_file) | |
121 | { | |
122 | script_file = fopen(arg->script_cmds_file, "r"); | |
123 | if (script_file) | |
1f947c4b | 124 | { |
902cb3c0 | 125 | printf("using 'scripting' commands file %s\n", arg->script_cmds_file); |
1f947c4b | 126 | } |
902cb3c0 | 127 | } |
7fe9b0b7 | 128 | |
8556b852 | 129 | read_history(".history"); |
1f947c4b | 130 | while(1) |
902cb3c0 | 131 | { |
132 | // If there is a script file | |
133 | if (script_file) | |
134 | { | |
135 | if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) | |
136 | { | |
137 | fclose(script_file); | |
138 | script_file = NULL; | |
139 | } | |
140 | else | |
141 | { | |
142 | char *nl; | |
143 | nl = strrchr(script_cmd_buf, '\r'); | |
144 | if (nl) *nl = '\0'; | |
145 | nl = strrchr(script_cmd_buf, '\n'); | |
146 | if (nl) *nl = '\0'; | |
147 | ||
50d6e4ab | 148 | if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) |
1f947c4b | 149 | { |
902cb3c0 | 150 | memset(cmd, 0, strlen(script_cmd_buf)); |
151 | strcpy(cmd, script_cmd_buf); | |
152 | printf("%s\n", cmd); | |
153 | } | |
154 | } | |
155 | } | |
1f947c4b | 156 | |
157 | if (!script_file) | |
158 | { | |
902cb3c0 | 159 | cmd = readline(PROXPROMPT); |
1f947c4b | 160 | } |
161 | ||
8556b852 M |
162 | if (cmd) { |
163 | while(cmd[strlen(cmd) - 1] == ' ') | |
902cb3c0 | 164 | cmd[strlen(cmd) - 1] = 0x00; |
8556b852 M |
165 | |
166 | if (cmd[0] != 0x00) { | |
167 | if (strncmp(cmd, "quit", 4) == 0) { | |
8556b852 M |
168 | break; |
169 | } | |
170 | ||
171 | CommandReceived(cmd); | |
172 | add_history(cmd); | |
173 | } | |
174 | free(cmd); | |
175 | } else { | |
176 | printf("\n"); | |
177 | break; | |
178 | } | |
179 | } | |
902cb3c0 | 180 | |
51969283 | 181 | write_history(".history"); |
902cb3c0 | 182 | |
183 | if (arg->usb_present == 1) { | |
184 | rarg.run = 0; | |
185 | pthread_join(reader_thread, NULL); | |
186 | } | |
187 | ||
188 | if (script_file) | |
189 | { | |
190 | fclose(script_file); | |
191 | script_file = NULL; | |
192 | } | |
193 | ||
194 | ExitGraphics(); | |
195 | pthread_exit(NULL); | |
196 | return NULL; | |
6658905f | 197 | } |
198 | ||
902cb3c0 | 199 | int main(int argc, char* argv[]) { |
ab4da50d | 200 | srand(time(0)); |
125a98a1 | 201 | |
902cb3c0 | 202 | if (argc < 2) { |
203 | printf("syntax: %s <port>\n\n",argv[0]); | |
204 | return 1; | |
205 | } | |
206 | ||
1f947c4b | 207 | // Make sure to initialize |
208 | struct main_loop_arg marg = { | |
209 | .usb_present = 0, | |
210 | .script_cmds_file = NULL | |
211 | }; | |
7fe9b0b7 | 212 | pthread_t main_loop_t; |
1f947c4b | 213 | |
902cb3c0 | 214 | /* |
215 | usb_init(); | |
7fe9b0b7 | 216 | if (!OpenProxmark(1)) { |
217 | fprintf(stderr,"PROXMARK3: NOT FOUND!\n"); | |
218 | marg.usb_present = 0; | |
219 | offline = 1; | |
220 | } else { | |
221 | marg.usb_present = 1; | |
222 | offline = 0; | |
223 | } | |
902cb3c0 | 224 | */ |
225 | sp = uart_open(argv[1]); | |
226 | if (sp == INVALID_SERIAL_PORT) { | |
227 | printf("ERROR: invalid serial port\n"); | |
228 | marg.usb_present = 0; | |
229 | offline = 1; | |
230 | } else { | |
231 | marg.usb_present = 1; | |
232 | offline = 0; | |
233 | } | |
7fe9b0b7 | 234 | |
902cb3c0 | 235 | // If the user passed the filename of the 'script' to execute, get it |
236 | if (argc > 2 && argv[2]) { | |
237 | marg.script_cmds_file = argv[2]; | |
238 | } | |
239 | ||
7fe9b0b7 | 240 | pthread_create(&main_loop_t, NULL, &main_loop, &marg); |
241 | InitGraphics(argc, argv); | |
242 | ||
243 | MainGraphics(); | |
244 | ||
245 | pthread_join(main_loop_t, NULL); | |
246 | ||
902cb3c0 | 247 | // if (marg.usb_present == 1) { |
248 | // CloseProxmark(); | |
249 | // } | |
250 | ||
251 | // Clean up the port | |
252 | uart_close(sp); | |
253 | ||
7fe9b0b7 | 254 | return 0; |
6658905f | 255 | } |