Add option -h to dump complete set of supported commands
[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"
902cb3c0 26
9492e0b0 27// a global mutex to prevent interlaced printing from different threads
28pthread_mutex_t print_lock;
29
902cb3c0 30static serial_port sp;
f0ba6342 31static UsbCommand txcmd;
c3385024 32volatile static bool txcmd_pending = false;
902cb3c0 33
9492e0b0 34
902cb3c0 35void SendCommand(UsbCommand *c) {
36#if 0
37 printf("Sending %d bytes\n", sizeof(UsbCommand));
38#endif
f0ba6342 39/*
40 if (txcmd_pending) {
41 ERR("Sending command failed, previous command is still pending");
902cb3c0 42 }
f0ba6342 43*/
da9d456e 44 if(offline)
45 {
46 PrintAndLog("Sending bytes to proxmark failed - offline");
47 return;
48 }
49
f0ba6342 50 while(txcmd_pending);
51 txcmd = *c;
52 txcmd_pending = true;
902cb3c0 53}
6658905f 54
902cb3c0 55struct receiver_arg {
7fe9b0b7 56 int run;
6658905f 57};
58
902cb3c0 59struct main_loop_arg {
7fe9b0b7 60 int usb_present;
1f947c4b 61 char *script_cmds_file;
a60612db 62};
63
902cb3c0 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//}
7fe9b0b7 78
902cb3c0 79byte_t rx[0x1000000];
fe7bfa78 80byte_t* prx = rx;
902cb3c0 81
82static void *uart_receiver(void *targ) {
83 struct receiver_arg *arg = (struct receiver_arg*)targ;
84 size_t rxlen;
85 size_t cmd_count;
86
7fe9b0b7 87 while (arg->run) {
af65f5f7 88 rxlen = sizeof(UsbCommand);
fe7bfa78 89 if (uart_receive(sp,prx,&rxlen)) {
90 prx += rxlen;
91 if (((prx-rx) % sizeof(UsbCommand)) != 0) {
902cb3c0 92 continue;
93 }
fe7bfa78 94 cmd_count = (prx-rx) / sizeof(UsbCommand);
95 // printf("received %d bytes, which represents %d commands\n",(prx-rx), cmd_count);
902cb3c0 96 for (size_t i=0; i<cmd_count; i++) {
97 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
98 }
7fe9b0b7 99 }
fe7bfa78 100 prx = rx;
101
f0ba6342 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 }
7fe9b0b7 108 }
902cb3c0 109
7fe9b0b7 110 pthread_exit(NULL);
4cd41f34 111 return NULL;
6658905f 112}
113
902cb3c0 114static 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)
1f947c4b 133 {
902cb3c0 134 printf("using 'scripting' commands file %s\n", arg->script_cmds_file);
1f947c4b 135 }
902cb3c0 136 }
7fe9b0b7 137
8556b852 138 read_history(".history");
1f947c4b 139 while(1)
902cb3c0 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
50d6e4ab 157 if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL)
1f947c4b 158 {
902cb3c0 159 memset(cmd, 0, strlen(script_cmd_buf));
160 strcpy(cmd, script_cmd_buf);
161 printf("%s\n", cmd);
162 }
163 }
164 }
1f947c4b 165
166 if (!script_file)
167 {
902cb3c0 168 cmd = readline(PROXPROMPT);
1f947c4b 169 }
170
8556b852
M
171 if (cmd) {
172 while(cmd[strlen(cmd) - 1] == ' ')
902cb3c0 173 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
174
175 if (cmd[0] != 0x00) {
176 if (strncmp(cmd, "quit", 4) == 0) {
981bd429 177 exit(0);
8556b852
M
178 break;
179 }
180
181 CommandReceived(cmd);
182 add_history(cmd);
183 }
184 free(cmd);
185 } else {
186 printf("\n");
187 break;
188 }
189 }
902cb3c0 190
51969283 191 write_history(".history");
902cb3c0 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;
6658905f 207}
208
ae7aa73d
PT
209#define DUMPHELP(cmd) \
210 do { \
211 printf("%s\n", cmd); \
212 printf("---------------------------------------------\n"); \
213 CommandReceived(cmd); \
214 printf("\n"); \
215 } while (0)
216
217static void dumphelp()
218{
219 offline=2;
220 printf("\n------------PROXMARK3 HELP DUMP--------------\n");
221 printf("Some commands are available only if a Proxmark is actually connected,\n");
222 printf("Those commands are flagged with \"@\" in front of their description.\n");
223 printf("\n");
224 DUMPHELP("help");
225 DUMPHELP("data help");
226 DUMPHELP("hf help");
227 DUMPHELP("hf 14a help");
228 DUMPHELP("hf 14b help");
229 DUMPHELP("hf 15 help");
230 DUMPHELP("hf epa help");
231 DUMPHELP("hf legic help");
232 DUMPHELP("hf iclass help");
233 DUMPHELP("hf mf help");
234 DUMPHELP("hw help");
235 DUMPHELP("lf help");
236 DUMPHELP("lf em4x help");
237 DUMPHELP("lf hid help");
238 DUMPHELP("lf ti help");
239 DUMPHELP("lf hitag help");
240 DUMPHELP("lf pcf7931 help");
241 DUMPHELP("lf t55xx help");
242}
243
902cb3c0 244int main(int argc, char* argv[]) {
9492e0b0 245 srand(time(0));
125a98a1 246
9492e0b0 247 if (argc < 2) {
248 printf("syntax: %s <port>\n\n",argv[0]);
249 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
ae7aa73d
PT
250 printf("help: %s -h\n\n", argv[0]);
251 printf("\tDump all interactive help at once\n");
9492e0b0 252 return 1;
253 }
902cb3c0 254
ae7aa73d
PT
255 if (strcmp(argv[1], "-h") == 0) {
256 printf("syntax: %s <port>\n\n",argv[0]);
257 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
258 offline = 2;
259 dumphelp();
260 return 0;
261 }
9492e0b0 262 // Make sure to initialize
263 struct main_loop_arg marg = {
264 .usb_present = 0,
265 .script_cmds_file = NULL
266 };
267 pthread_t main_loop_t;
1f947c4b 268
902cb3c0 269/*
270 usb_init();
7fe9b0b7 271 if (!OpenProxmark(1)) {
272 fprintf(stderr,"PROXMARK3: NOT FOUND!\n");
273 marg.usb_present = 0;
274 offline = 1;
275 } else {
276 marg.usb_present = 1;
277 offline = 0;
278 }
902cb3c0 279*/
4890730a 280
9492e0b0 281 sp = uart_open(argv[1]);
282 if (sp == INVALID_SERIAL_PORT) {
283 printf("ERROR: invalid serial port\n");
284 marg.usb_present = 0;
285 offline = 1;
286 } else if (sp == CLAIMED_SERIAL_PORT) {
287 printf("ERROR: serial port is claimed by another process\n");
288 marg.usb_present = 0;
289 offline = 1;
290 } else {
291 marg.usb_present = 1;
292 offline = 0;
293 }
7fe9b0b7 294
9492e0b0 295 // If the user passed the filename of the 'script' to execute, get it
296 if (argc > 2 && argv[2]) {
ed77aabe 297 if (argv[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry.
298 argv[2][1] == 'l' &&
299 argv[2][2] == 'u' &&
300 argv[2][3] == 's' &&
301 argv[2][4] == 'h')
302 {
303 printf("Output will be flushed after every print.\n");
304 flushAfterWrite = 1;
305 }
306 else
9492e0b0 307 marg.script_cmds_file = argv[2];
308 }
ed77aabe 309
9492e0b0 310 // create a mutex to avoid interlacing print commands from our different threads
311 pthread_mutex_init(&print_lock, NULL);
7fe9b0b7 312
9492e0b0 313 pthread_create(&main_loop_t, NULL, &main_loop, &marg);
314 InitGraphics(argc, argv);
7fe9b0b7 315
9492e0b0 316 MainGraphics();
317
318 pthread_join(main_loop_t, NULL);
7fe9b0b7 319
902cb3c0 320// if (marg.usb_present == 1) {
321// CloseProxmark();
322// }
323
9492e0b0 324 // Clean up the port
325 uart_close(sp);
326
327 // clean up mutex
328 pthread_mutex_destroy(&print_lock);
902cb3c0 329
7fe9b0b7 330 return 0;
6658905f 331}
Impressum, Datenschutz