]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxmark3.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[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
20 #include "proxmark3.h"
21 #include "proxgui.h"
22 #include "cmdmain.h"
23 #include "uart.h"
24 #include "ui.h"
25 #include "cmdparser.h"
26 #include "cmdhw.h"
27 #include "whereami.h"
28
29
30 // a global mutex to prevent interlaced printing from different threads
31 pthread_mutex_t print_lock;
32
33 static serial_port sp;
34 static UsbCommand txcmd;
35 volatile static bool txcmd_pending = false;
36
37 void SendCommand(UsbCommand *c) {
38 #if 0
39 printf("Sending %d bytes\n", sizeof(UsbCommand));
40 #endif
41
42 if (offline) {
43 PrintAndLog("Sending bytes to proxmark failed - offline");
44 return;
45 }
46 /**
47 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
48 or disconnected. The main console thread is alive, but comm thread just spins here.
49 Not good.../holiman
50 **/
51 while(txcmd_pending);
52 txcmd = *c;
53 txcmd_pending = true;
54 }
55
56 struct receiver_arg {
57 int run;
58 };
59
60 byte_t rx[0x1000000];
61 byte_t* prx = rx;
62
63 static void *uart_receiver(void *targ) {
64 struct receiver_arg *arg = (struct receiver_arg*)targ;
65 size_t rxlen;
66 size_t cmd_count;
67
68 while (arg->run) {
69 rxlen = sizeof(UsbCommand);
70 if (uart_receive(sp, prx, &rxlen)) {
71 prx += rxlen;
72 if (((prx-rx) % sizeof(UsbCommand)) != 0) {
73 continue;
74 }
75 cmd_count = (prx-rx) / sizeof(UsbCommand);
76
77 for (size_t i = 0; i < cmd_count; i++) {
78 UsbCommandReceived((UsbCommand*)(rx+(i*sizeof(UsbCommand))));
79 }
80 }
81 prx = rx;
82
83 if(txcmd_pending) {
84 if (!uart_send(sp, (byte_t*) &txcmd, sizeof(UsbCommand))) {
85 PrintAndLog("Sending bytes to proxmark failed");
86 }
87 txcmd_pending = false;
88 }
89 }
90
91 pthread_exit(NULL);
92 return NULL;
93 }
94
95
96 void main_loop(char *script_cmds_file, bool usb_present) {
97 struct receiver_arg rarg;
98 char *cmd = NULL;
99 pthread_t reader_thread;
100
101 if (usb_present) {
102 rarg.run = 1;
103 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
104 // cache Version information now:
105 CmdVersion(NULL);
106 }
107
108 FILE *script_file = NULL;
109 char script_cmd_buf[256]; // iceman, needs lua script the same file_path_buffer as the rest
110
111 if (script_cmds_file) {
112 script_file = fopen(script_cmds_file, "r");
113 if (script_file) {
114 printf("using 'scripting' commands file %s\n", script_cmds_file);
115 }
116 }
117
118 read_history(".history");
119
120 while(1) {
121
122 // If there is a script file
123 if (script_file)
124 {
125 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) {
126 fclose(script_file);
127 script_file = NULL;
128 } else {
129 char *nl;
130 nl = strrchr(script_cmd_buf, '\r');
131 if (nl) *nl = '\0';
132
133 nl = strrchr(script_cmd_buf, '\n');
134 if (nl) *nl = '\0';
135
136 if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) {
137 memset(cmd, 0, strlen(script_cmd_buf));
138 strcpy(cmd, script_cmd_buf);
139 printf("%s\n", cmd);
140 }
141 }
142 }
143
144 if (!script_file) {
145 cmd = readline(PROXPROMPT);
146 }
147
148 if (cmd) {
149
150 while(cmd[strlen(cmd) - 1] == ' ')
151 cmd[strlen(cmd) - 1] = 0x00;
152
153 if (cmd[0] != 0x00) {
154 int ret = CommandReceived(cmd);
155 add_history(cmd);
156 if (ret == 99) { // exit or quit
157 break;
158 }
159 }
160 free(cmd);
161 } else {
162 printf("\n");
163 break;
164 }
165 }
166
167 write_history(".history");
168
169 if (usb_present) {
170 rarg.run = 0;
171 pthread_join(reader_thread, NULL);
172 }
173
174 if (script_file) {
175 fclose(script_file);
176 script_file = NULL;
177 }
178
179 }
180
181 static void dumpAllHelp(int markdown)
182 {
183 printf("\n%sProxmark3 command dump%s\n\n",markdown?"# ":"",markdown?"":"\n======================");
184 printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown?" ":"");
185 printf("Check column \"offline\" for their availability.\n");
186 printf("\n");
187 command_t *cmds = getTopLevelCommandTable();
188 dumpCommandsRecursive(cmds, markdown);
189 }
190
191 static char *my_executable_path = NULL;
192 static char *my_executable_directory = NULL;
193
194 const char *get_my_executable_path(void)
195 {
196 return my_executable_path;
197 }
198
199 const char *get_my_executable_directory(void)
200 {
201 return my_executable_directory;
202 }
203
204 static void set_my_executable_path(void)
205 {
206 int path_length = wai_getExecutablePath(NULL, 0, NULL);
207 if (path_length != -1) {
208 my_executable_path = (char*)malloc(path_length + 1);
209 int dirname_length = 0;
210 if (wai_getExecutablePath(my_executable_path, path_length, &dirname_length) != -1) {
211 my_executable_path[path_length] = '\0';
212 my_executable_directory = (char *)malloc(dirname_length + 2);
213 strncpy(my_executable_directory, my_executable_path, dirname_length+1);
214 my_executable_directory[dirname_length+1] = '\0';
215 }
216 }
217 }
218
219
220 int main(int argc, char* argv[]) {
221 srand(time(0));
222
223 if (argc < 2) {
224 printf("syntax: %s <port>\n\n",argv[0]);
225 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
226 printf("help: %s -h\n\n", argv[0]);
227 printf("\tDump all interactive help at once\n");
228 printf("markdown: %s -m\n\n", argv[0]);
229 printf("\tDump all interactive help at once in markdown syntax\n");
230 return 1;
231 }
232 if (strcmp(argv[1], "-h") == 0) {
233 printf("syntax: %s <port>\n\n",argv[0]);
234 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
235 dumpAllHelp(0);
236 return 0;
237 }
238 if (strcmp(argv[1], "-m") == 0) {
239 dumpAllHelp(1);
240 return 0;
241 }
242
243 set_my_executable_path();
244
245 bool usb_present = false;
246 char *script_cmds_file = NULL;
247
248 sp = uart_open(argv[1]);
249 if (sp == INVALID_SERIAL_PORT) {
250 printf("ERROR: invalid serial port\n");
251 usb_present = false;
252 offline = 1;
253 } else if (sp == CLAIMED_SERIAL_PORT) {
254 printf("ERROR: serial port is claimed by another process\n");
255 usb_present = false;
256 offline = 1;
257 } else {
258 usb_present = true;
259 offline = 0;
260 }
261
262 // If the user passed the filename of the 'script' to execute, get it
263 if (argc > 2 && argv[2]) {
264 if (argv[2][0] == 'f' && //buzzy, if a word 'flush' passed, flush the output after every log entry.
265 argv[2][1] == 'l' &&
266 argv[2][2] == 'u' &&
267 argv[2][3] == 's' &&
268 argv[2][4] == 'h')
269 {
270 printf("Output will be flushed after every print.\n");
271 flushAfterWrite = 1;
272 }
273 else
274 script_cmds_file = argv[2];
275 }
276
277 // create a mutex to avoid interlacing print commands from our different threads
278 pthread_mutex_init(&print_lock, NULL);
279
280 #ifdef HAVE_GUI
281 InitGraphics(argc, argv, script_cmds_file, usb_present);
282 MainGraphics();
283 #else
284 main_loop(script_cmds_file, usb_present);
285 #endif
286
287 // Clean up the port
288 if (usb_present) {
289 uart_close(sp);
290 }
291
292 // clean up mutex
293 pthread_mutex_destroy(&print_lock);
294
295 exit(0);
296 }
Impressum, Datenschutz