]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxmark3.c
Update cmdhf14a.c
[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>
9484ff3d 19
6658905f 20#include "proxmark3.h"
21#include "proxgui.h"
7fe9b0b7 22#include "cmdmain.h"
902cb3c0 23#include "uart.h"
902cb3c0 24#include "ui.h"
57c69556 25#include "cmdparser.h"
8e074056 26#include "cmdhw.h"
4197a3f6 27#include "whereami.h"
28
902cb3c0 29
9492e0b0 30// a global mutex to prevent interlaced printing from different threads
31pthread_mutex_t print_lock;
32
902cb3c0 33static serial_port sp;
f0ba6342 34static UsbCommand txcmd;
c3385024 35volatile static bool txcmd_pending = false;
902cb3c0 36
37void SendCommand(UsbCommand *c) {
9484ff3d 38 #if 0
39 printf("Sending %d bytes\n", sizeof(UsbCommand));
40 #endif
41
42 if (offline) {
da9d456e 43 PrintAndLog("Sending bytes to proxmark failed - offline");
44 return;
45 }
07976a25
MHS
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 **/
9484ff3d 51 while(txcmd_pending);
52 txcmd = *c;
53 txcmd_pending = true;
902cb3c0 54}
6658905f 55
902cb3c0 56struct receiver_arg {
9484ff3d 57 int run;
6658905f 58};
59
902cb3c0 60byte_t rx[0x1000000];
fe7bfa78 61byte_t* prx = rx;
902cb3c0 62
63static void *uart_receiver(void *targ) {
9484ff3d 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;
6658905f 93}
94
5acd195d 95
96void main_loop(char *script_cmds_file, bool usb_present) {
9484ff3d 97 struct receiver_arg rarg;
98 char *cmd = NULL;
99 pthread_t reader_thread;
1a3c0064 100
5acd195d 101 if (usb_present) {
9484ff3d 102 rarg.run = 1;
103 pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
8e074056 104 // cache Version information now:
105 CmdVersion(NULL);
9484ff3d 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
5acd195d 111 if (script_cmds_file) {
112 script_file = fopen(script_cmds_file, "r");
9484ff3d 113 if (script_file) {
5acd195d 114 printf("using 'scripting' commands file %s\n", script_cmds_file);
9484ff3d 115 }
116 }
7fe9b0b7 117
8556b852 118 read_history(".history");
9484ff3d 119
120 while(1) {
121
122 // If there is a script file
123 if (script_file)
1f947c4b 124 {
9484ff3d 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 }
1f947c4b 142 }
143
9484ff3d 144 if (!script_file) {
9484ff3d 145 cmd = readline(PROXPROMPT);
9484ff3d 146 }
147
8556b852 148 if (cmd) {
9484ff3d 149
8556b852 150 while(cmd[strlen(cmd) - 1] == ' ')
9484ff3d 151 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
152
153 if (cmd[0] != 0x00) {
2487dfeb 154 int ret = CommandReceived(cmd);
155 add_history(cmd);
156 if (ret == 99) { // exit or quit
8556b852
M
157 break;
158 }
8556b852
M
159 }
160 free(cmd);
161 } else {
162 printf("\n");
163 break;
164 }
165 }
902cb3c0 166
51969283 167 write_history(".history");
902cb3c0 168
5acd195d 169 if (usb_present) {
9484ff3d 170 rarg.run = 0;
171 pthread_join(reader_thread, NULL);
172 }
1a3c0064 173
9484ff3d 174 if (script_file) {
175 fclose(script_file);
176 script_file = NULL;
177 }
5acd195d 178
6658905f 179}
180
dec8e8bd 181static void dumpAllHelp(int markdown)
ae7aa73d 182{
dec8e8bd
PT
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?" ":"");
6f5dd601 185 printf("Check column \"offline\" for their availability.\n");
ae7aa73d 186 printf("\n");
57c69556 187 command_t *cmds = getTopLevelCommandTable();
dec8e8bd 188 dumpCommandsRecursive(cmds, markdown);
ae7aa73d
PT
189}
190
4197a3f6 191static char *my_executable_path = NULL;
192static char *my_executable_directory = NULL;
193
4a6bc37e 194const char *get_my_executable_path(void)
4197a3f6 195{
196 return my_executable_path;
197}
198
4a6bc37e 199const char *get_my_executable_directory(void)
4197a3f6 200{
201 return my_executable_directory;
202}
203
204static 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);
4a6bc37e 214 my_executable_directory[dirname_length+1] = '\0';
4197a3f6 215 }
216 }
217}
218
5acd195d 219
902cb3c0 220int main(int argc, char* argv[]) {
9492e0b0 221 srand(time(0));
125a98a1 222
9492e0b0 223 if (argc < 2) {
224 printf("syntax: %s <port>\n\n",argv[0]);
225 printf("\tLinux example:'%s /dev/ttyACM0'\n\n", argv[0]);
dec8e8bd
PT
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");
9492e0b0 230 return 1;
231 }
dec8e8bd
PT
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 }
4197a3f6 242
243 set_my_executable_path();
244
5acd195d 245 bool usb_present = false;
246 char *script_cmds_file = NULL;
4890730a 247
9492e0b0 248 sp = uart_open(argv[1]);
249 if (sp == INVALID_SERIAL_PORT) {
250 printf("ERROR: invalid serial port\n");
5acd195d 251 usb_present = false;
9492e0b0 252 offline = 1;
253 } else if (sp == CLAIMED_SERIAL_PORT) {
254 printf("ERROR: serial port is claimed by another process\n");
5acd195d 255 usb_present = false;
9492e0b0 256 offline = 1;
257 } else {
5acd195d 258 usb_present = true;
9492e0b0 259 offline = 0;
260 }
7fe9b0b7 261
9492e0b0 262 // If the user passed the filename of the 'script' to execute, get it
263 if (argc > 2 && argv[2]) {
ed77aabe 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
5acd195d 274 script_cmds_file = argv[2];
9492e0b0 275 }
ed77aabe 276
9492e0b0 277 // create a mutex to avoid interlacing print commands from our different threads
278 pthread_mutex_init(&print_lock, NULL);
7fe9b0b7 279
5acd195d 280#ifdef HAVE_GUI
281 InitGraphics(argc, argv, script_cmds_file, usb_present);
9492e0b0 282 MainGraphics();
5acd195d 283#else
284 main_loop(script_cmds_file, usb_present);
285#endif
7fe9b0b7 286
9492e0b0 287 // Clean up the port
5acd195d 288 if (usb_present) {
2487dfeb 289 uart_close(sp);
290 }
291
9492e0b0 292 // clean up mutex
293 pthread_mutex_destroy(&print_lock);
2487dfeb 294
295 exit(0);
6658905f 296}
Impressum, Datenschutz