]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxmark3.c
First check in.
[proxmark3-svn] / client / proxmark3.c
CommitLineData
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"
902cb3c0 24#include "ui.h"
759c16b3 25#include "sleep.h"
57c69556
MHS
26#include "cmdparser.h"
27#include "cmdmain.h"
902cb3c0 28
9492e0b0 29// a global mutex to prevent interlaced printing from different threads
30pthread_mutex_t print_lock;
31
902cb3c0 32static serial_port sp;
f0ba6342 33static UsbCommand txcmd;
c3385024 34volatile static bool txcmd_pending = false;
902cb3c0 35
36void SendCommand(UsbCommand *c) {
37#if 0
38 printf("Sending %d bytes\n", sizeof(UsbCommand));
39#endif
f0ba6342 40/*
41 if (txcmd_pending) {
42 ERR("Sending command failed, previous command is still pending");
902cb3c0 43 }
f0ba6342 44*/
da9d456e 45 if(offline)
46 {
47 PrintAndLog("Sending bytes to proxmark failed - offline");
48 return;
49 }
50
f0ba6342 51 while(txcmd_pending);
52 txcmd = *c;
53 txcmd_pending = true;
902cb3c0 54}
6658905f 55
902cb3c0 56struct receiver_arg {
7fe9b0b7 57 int run;
6658905f 58};
59
902cb3c0 60struct main_loop_arg {
7fe9b0b7 61 int usb_present;
1f947c4b 62 char *script_cmds_file;
a60612db 63};
64
902cb3c0 65byte_t rx[0x1000000];
fe7bfa78 66byte_t* prx = rx;
902cb3c0 67
68static void *uart_receiver(void *targ) {
69 struct receiver_arg *arg = (struct receiver_arg*)targ;
70 size_t rxlen;
71 size_t cmd_count;
72
7fe9b0b7 73 while (arg->run) {
af65f5f7 74 rxlen = sizeof(UsbCommand);
fe7bfa78 75 if (uart_receive(sp,prx,&rxlen)) {
76 prx += rxlen;
77 if (((prx-rx) % sizeof(UsbCommand)) != 0) {
902cb3c0 78 continue;
79 }
fe7bfa78 80 cmd_count = (prx-rx) / sizeof(UsbCommand);
81 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
902cb3c0 82 for (size_t i=0; i<cmd_count; i++) {
83 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
84 }
7fe9b0b7 85 }
fe7bfa78 86 prx = rx;
87
f0ba6342 88 if(txcmd_pending) {
89 if (!uart_send(sp,(byte_t*)&txcmd,sizeof(UsbCommand))) {
90 PrintAndLog("Sending bytes to proxmark failed");
91 }
92 txcmd_pending = false;
93 }
7fe9b0b7 94 }
902cb3c0 95
7fe9b0b7 96 pthread_exit(NULL);
4cd41f34 97 return NULL;
6658905f 98}
99
902cb3c0 100static void *main_loop(void *targ) {
101 struct main_loop_arg *arg = (struct main_loop_arg*)targ;
102 struct receiver_arg rarg;
103 char *cmd = NULL;
104 pthread_t reader_thread;
105
106 if (arg->usb_present == 1) {
107 rarg.run=1;
108 // pthread_create(&reader_thread, NULL, &usb_receiver, &rarg);
109 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
110 }
111
112 FILE *script_file = NULL;
113 char script_cmd_buf[256];
114
115 if (arg->script_cmds_file)
116 {
117 script_file = fopen(arg->script_cmds_file, "r");
118 if (script_file)
1f947c4b 119 {
902cb3c0 120 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
1f947c4b 121 }
902cb3c0 122 }
7fe9b0b7 123
8556b852 124 read_history(".history");
1f947c4b 125 while(1)
902cb3c0 126 {
127 // If there is a script file
128 if (script_file)
129 {
130 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file))
131 {
132 fclose(script_file);
133 script_file = NULL;
134 }
135 else
136 {
137 char *nl;
138 nl = strrchr(script_cmd_buf, '\r');
139 if (nl) *nl = '\0';
140 nl = strrchr(script_cmd_buf, '\n');
141 if (nl) *nl = '\0';
142
50d6e4ab 143 if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL)
1f947c4b 144 {
902cb3c0 145 memset(cmd, 0, strlen(script_cmd_buf));
146 strcpy(cmd, script_cmd_buf);
147 printf("%s\n", cmd);
148 }
149 }
150 }
1f947c4b 151
152 if (!script_file)
153 {
902cb3c0 154 cmd = readline(PROXPROMPT);
1f947c4b 155 }
156
8556b852
M
157 if (cmd) {
158 while(cmd[strlen(cmd) - 1] == ' ')
902cb3c0 159 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
160
161 if (cmd[0] != 0x00) {
162 if (strncmp(cmd, "quit", 4) == 0) {
981bd429 163 exit(0);
8556b852
M
164 break;
165 }
166
167 CommandReceived(cmd);
168 add_history(cmd);
169 }
170 free(cmd);
171 } else {
172 printf("\n");
173 break;
174 }
175 }
902cb3c0 176
51969283 177 write_history(".history");
902cb3c0 178
179 if (arg->usb_present == 1) {
180 rarg.run = 0;
181 pthread_join(reader_thread, NULL);
182 }
183
184 if (script_file)
185 {
186 fclose(script_file);
187 script_file = NULL;
188 }
189
190 ExitGraphics();
191 pthread_exit(NULL);
192 return NULL;
6658905f 193}
194
dec8e8bd 195static void dumpAllHelp(int markdown)
ae7aa73d 196{
dec8e8bd
PT
197 printf("\n%sProxmark3 command dump%s\n\n",markdown?"# ":"",markdown?"":"\n======================");
198 printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown?" ":"");
6f5dd601 199 printf("Check column \"offline\" for their availability.\n");
ae7aa73d 200 printf("\n");
57c69556 201 command_t *cmds = getTopLevelCommandTable();
dec8e8bd 202 dumpCommandsRecursive(cmds, markdown);
ae7aa73d
PT
203}
204
902cb3c0 205int main(int argc, char* argv[]) {
9492e0b0 206 srand(time(0));
125a98a1 207
9492e0b0 208 if (argc < 2) {
209 printf("syntax: %s <port>\n\n",argv[0]);
210 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
dec8e8bd
PT
211 printf("help: %s -h\n\n", argv[0]);
212 printf("\tDump all interactive help at once\n");
213 printf("markdown: %s -m\n\n", argv[0]);
214 printf("\tDump all interactive help at once in markdown syntax\n");
9492e0b0 215 return 1;
216 }
dec8e8bd
PT
217 if (strcmp(argv[1], "-h") == 0) {
218 printf("syntax: %s <port>\n\n",argv[0]);
219 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
220 dumpAllHelp(0);
221 return 0;
222 }
223 if (strcmp(argv[1], "-m") == 0) {
224 dumpAllHelp(1);
225 return 0;
226 }
9492e0b0 227 // Make sure to initialize
228 struct main_loop_arg marg = {
229 .usb_present = 0,
230 .script_cmds_file = NULL
231 };
232 pthread_t main_loop_t;
1f947c4b 233
4890730a 234
9492e0b0 235 sp = uart_open(argv[1]);
236 if (sp == INVALID_SERIAL_PORT) {
237 printf("ERROR: invalid serial port\n");
238 marg.usb_present = 0;
239 offline = 1;
240 } else if (sp == CLAIMED_SERIAL_PORT) {
241 printf("ERROR: serial port is claimed by another process\n");
242 marg.usb_present = 0;
243 offline = 1;
244 } else {
245 marg.usb_present = 1;
246 offline = 0;
247 }
7fe9b0b7 248
9492e0b0 249 // If the user passed the filename of the 'script' to execute, get it
250 if (argc > 2 && argv[2]) {
ed77aabe 251 if (argv[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry.
252 argv[2][1] == 'l' &&
253 argv[2][2] == 'u' &&
254 argv[2][3] == 's' &&
255 argv[2][4] == 'h')
256 {
257 printf("Output will be flushed after every print.\n");
258 flushAfterWrite = 1;
259 }
260 else
9492e0b0 261 marg.script_cmds_file = argv[2];
262 }
ed77aabe 263
9492e0b0 264 // create a mutex to avoid interlacing print commands from our different threads
265 pthread_mutex_init(&print_lock, NULL);
7fe9b0b7 266
9492e0b0 267 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
268 InitGraphics(argc, argv);
7fe9b0b7 269
9492e0b0 270 MainGraphics();
271
272 pthread_join(main_loop_t, NULL);
7fe9b0b7 273
9492e0b0 274 // Clean up the port
275 uart_close(sp);
276
277 // clean up mutex
278 pthread_mutex_destroy(&print_lock);
902cb3c0 279
7fe9b0b7 280 return 0;
6658905f 281}
Impressum, Datenschutz