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