]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
integrated MIFARE ultralight features, contributed by 'midnitesnake'
[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 //#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 while(txcmd_pending);
45 txcmd = *c;
46 txcmd_pending = true;
47 }
48
49 struct receiver_arg {
50 int run;
51 };
52
53 struct main_loop_arg {
54 int usb_present;
55 char *script_cmds_file;
56 };
57
58 //static void *usb_receiver(void *targ) {
59 // struct receiver_arg *arg = (struct receiver_arg*)targ;
60 // UsbCommand cmdbuf;
61 //
62 // while (arg->run) {
63 // if (ReceiveCommandPoll(&cmdbuf)) {
64 // UsbCommandReceived(&cmdbuf);
65 // fflush(NULL);
66 // }
67 // }
68 //
69 // pthread_exit(NULL);
70 // return NULL;
71 //}
72
73 byte_t rx[0x1000000];
74 byte_t* prx = rx;
75
76 static void *uart_receiver(void *targ) {
77 struct receiver_arg *arg = (struct receiver_arg*)targ;
78 size_t rxlen;
79 size_t cmd_count;
80
81 while (arg->run) {
82 rxlen = sizeof(UsbCommand);
83 if (uart_receive(sp,prx,&rxlen)) {
84 prx += rxlen;
85 if (((prx-rx) % sizeof(UsbCommand)) != 0) {
86 continue;
87 }
88 cmd_count = (prx-rx) / sizeof(UsbCommand);
89 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
90 for (size_t i=0; i<cmd_count; i++) {
91 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
92 }
93 }
94 prx = rx;
95
96 if(txcmd_pending) {
97 if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) {
98 PrintAndLog("Sending bytes to proxmark failed");
99 }
100 txcmd_pending = false;
101 }
102 }
103
104 pthread_exit(NULL);
105 return NULL;
106 }
107
108 static void *main_loop(void *targ) {
109 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
110 struct receiver_arg rarg;
111 char *cmd = NULL;
112 pthread_t reader_thread;
113
114 if (arg->usb_present == 1) {
115 rarg.run=1;
116 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
117 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
118 }
119
120 FILE *script_file = NULL;
121 char script_cmd_buf[256];
122
123 if (arg->script_cmds_file)
124 {
125 script_file = fopen(arg->script_cmds_file, "r");
126 if (script_file)
127 {
128 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
129 }
130 }
131
132 read_history(".history");
133 while(1)
134 {
135 // If there is a script file
136 if (script_file)
137 {
138 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
139 {
140 fclose(script_file);
141 script_file = NULL;
142 }
143 else
144 {
145 char *nl;
146 nl = strrchr(script_cmd_buf, '\r');
147 if (nl) *nl = '\0';
148 nl = strrchr(script_cmd_buf, '\n');
149 if (nl) *nl = '\0';
150
151 if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL)
152 {
153 memset(cmd, 0, strlen(script_cmd_buf));
154 strcpy(cmd, script_cmd_buf);
155 printf("%s\n", cmd);
156 }
157 }
158 }
159
160 if (!script_file)
161 {
162 cmd = readline(PROXPROMPT);
163 }
164
165 if (cmd) {
166 while(cmd[strlen(cmd) - 1] == ' ')
167 cmd[strlen(cmd) - 1] = 0x00;
168
169 if (cmd[0] != 0x00) {
170 if (strncmp(cmd, "quit", 4) == 0) {
171 exit(0);
172 break;
173 }
174
175 CommandReceived(cmd);
176 add_history(cmd);
177 }
178 free(cmd);
179 } else {
180 printf("\n");
181 break;
182 }
183 }
184
185 write_history(".history");
186
187 if (arg->usb_present == 1) {
188 rarg.run = 0;
189 pthread_join(reader_thread, NULL);
190 }
191
192 if (script_file)
193 {
194 fclose(script_file);
195 script_file = NULL;
196 }
197
198 ExitGraphics();
199 pthread_exit(NULL);
200 return NULL;
201 }
202
203 int main(int argc, char* argv[]) {
204 srand(time(0));
205
206 if (argc < 2) {
207 printf("syntax: %s <port>\n\n",argv[0]);
208 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
209 return 1;
210 }
211
212 // Make sure to initialize
213 struct main_loop_arg marg = {
214 .usb_present = 0,
215 .script_cmds_file = NULL
216 };
217 pthread_t main_loop_t;
218
219 /*
220 usb_init();
221 if (!OpenProxmark(1)) {
222 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
223 marg.usb_present = 0;
224 offline = 1;
225 } else {
226 marg.usb_present = 1;
227 offline = 0;
228 }
229 */
230
231 sp = uart_open(argv[1]);
232 if (sp == INVALID_SERIAL_PORT) {
233 printf("ERROR: invalid serial port\n");
234 marg.usb_present = 0;
235 offline = 1;
236 } else if (sp == CLAIMED_SERIAL_PORT) {
237 printf("ERROR: serial port is claimed by another process\n");
238 marg.usb_present = 0;
239 offline = 1;
240 } else {
241 marg.usb_present = 1;
242 offline = 0;
243 }
244
245 // If the user passed the filename of the 'script' to execute, get it
246 if (argc > 2 && argv[2]) {
247 if (argv[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry.
248 argv[2][1] == 'l' &&
249 argv[2][2] == 'u' &&
250 argv[2][3] == 's' &&
251 argv[2][4] == 'h')
252 {
253 printf("Output will be flushed after every print.\n");
254 flushAfterWrite = 1;
255 }
256 else
257 marg.script_cmds_file = argv[2];
258 }
259
260 // create a mutex to avoid interlacing print commands from our different threads
261 pthread_mutex_init(&print_lock, NULL);
262
263 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
264 InitGraphics(argc, argv);
265
266 MainGraphics();
267
268 pthread_join(main_loop_t, NULL);
269
270 // if (marg.usb_present == 1) {
271 // CloseProxmark();
272 // }
273
274 // Clean up the port
275 uart_close(sp);
276
277 // clean up mutex
278 pthread_mutex_destroy(&print_lock);
279
280 return 0;
281 }
Impressum, Datenschutz