]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxmark3.c
Merge pull request #600 from marshmellow42/master
[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"
aa757f71 21#include "util_posix.h"
6658905f 22#include "proxgui.h"
7fe9b0b7 23#include "cmdmain.h"
902cb3c0 24#include "ui.h"
aa757f71 25#include "util.h"
57c69556 26#include "cmdparser.h"
8e074056 27#include "cmdhw.h"
4197a3f6 28#include "whereami.h"
29
afdcb8c1 30
f921c113 31void
32#ifdef __has_attribute
33#if __has_attribute(force_align_arg_pointer)
34__attribute__((force_align_arg_pointer))
35#endif
36#endif
37main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
f5ecd97b 38 receiver_arg conn;
3851172d 39 char *cmd = NULL;
40 pthread_t reader_thread;
41 bool execCommand = (script_cmd != NULL);
42 bool stdinOnPipe = !isatty(STDIN_FILENO);
61aaee35 43
f5ecd97b 44 memset(&conn, 0, sizeof(receiver_arg));
45
5acd195d 46 if (usb_present) {
f5ecd97b 47 conn.run = true;
61aaee35 48 SetOffline(false);
f5ecd97b 49 pthread_create(&reader_thread, NULL, &uart_receiver, &conn);
8e074056 50 // cache Version information now:
51 CmdVersion(NULL);
61aaee35 52 } else {
53 SetOffline(true);
9484ff3d 54 }
55
aa757f71 56 // file with script
9484ff3d 57 FILE *script_file = NULL;
aa757f71 58 char script_cmd_buf[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest
9484ff3d 59
5acd195d 60 if (script_cmds_file) {
61 script_file = fopen(script_cmds_file, "r");
9484ff3d 62 if (script_file) {
aa757f71 63 printf("executing commands from file: %s\n", script_cmds_file);
9484ff3d 64 }
65 }
a5a83016 66
8556b852 67 read_history(".history");
9484ff3d 68
61aaee35 69 while (1) {
9484ff3d 70 // If there is a script file
71 if (script_file)
1f947c4b 72 {
aa757f71 73 memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
9484ff3d 74 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), script_file)) {
75 fclose(script_file);
76 script_file = NULL;
77 } else {
aa757f71 78 strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
9484ff3d 79
aa757f71
OM
80 if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
81 printf(PROXPROMPT"%s\n", cmd);
82 }
83 }
84 } else {
85 // If there is a script command
86 if (execCommand){
87 if ((cmd = strmcopy(script_cmd)) != NULL) {
88 printf(PROXPROMPT"%s\n", cmd);
89 }
90
91 execCommand = false;
92 } else {
93 // exit after exec command
94 if (script_cmd)
95 break;
96
97 // if there is a pipe from stdin
98 if (stdinOnPipe) {
99 memset(script_cmd_buf, 0, sizeof(script_cmd_buf));
100 if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), stdin)) {
101 printf("\nStdin end. Exit...\n");
102 break;
103 }
104 strcleanrn(script_cmd_buf, sizeof(script_cmd_buf));
105
106 if ((cmd = strmcopy(script_cmd_buf)) != NULL) {
107 printf(PROXPROMPT"%s\n", cmd);
108 }
109
110 } else {
111 // read command from command prompt
112 cmd = readline(PROXPROMPT);
9484ff3d 113 }
114 }
1f947c4b 115 }
116
aa757f71 117 // execute command
8556b852 118 if (cmd) {
9484ff3d 119
8556b852 120 while(cmd[strlen(cmd) - 1] == ' ')
9484ff3d 121 cmd[strlen(cmd) - 1] = 0x00;
8556b852
M
122
123 if (cmd[0] != 0x00) {
2487dfeb 124 int ret = CommandReceived(cmd);
125 add_history(cmd);
126 if (ret == 99) { // exit or quit
8556b852
M
127 break;
128 }
8556b852
M
129 }
130 free(cmd);
aa757f71 131 cmd = NULL;
8556b852
M
132 } else {
133 printf("\n");
134 break;
135 }
136 }
aa757f71 137
51969283 138 write_history(".history");
818efbeb 139
5acd195d 140 if (usb_present) {
f5ecd97b 141 conn.run = false;
9484ff3d 142 pthread_join(reader_thread, NULL);
143 }
1a3c0064 144
9484ff3d 145 if (script_file) {
146 fclose(script_file);
147 script_file = NULL;
148 }
6658905f 149}
150
dec8e8bd 151static void dumpAllHelp(int markdown)
ae7aa73d 152{
dec8e8bd
PT
153 printf("\n%sProxmark3 command dump%s\n\n",markdown?"# ":"",markdown?"":"\n======================");
154 printf("Some commands are available only if a Proxmark is actually connected.%s\n",markdown?" ":"");
6f5dd601 155 printf("Check column \"offline\" for their availability.\n");
ae7aa73d 156 printf("\n");
57c69556 157 command_t *cmds = getTopLevelCommandTable();
dec8e8bd 158 dumpCommandsRecursive(cmds, markdown);
ae7aa73d
PT
159}
160
4197a3f6 161static char *my_executable_path = NULL;
162static char *my_executable_directory = NULL;
163
4a6bc37e 164const char *get_my_executable_path(void)
4197a3f6 165{
166 return my_executable_path;
167}
168
4a6bc37e 169const char *get_my_executable_directory(void)
4197a3f6 170{
171 return my_executable_directory;
172}
173
174static void set_my_executable_path(void)
175{
176 int path_length = wai_getExecutablePath(NULL, 0, NULL);
177 if (path_length != -1) {
178 my_executable_path = (char*)malloc(path_length + 1);
179 int dirname_length = 0;
180 if (wai_getExecutablePath(my_executable_path, path_length, &dirname_length) != -1) {
181 my_executable_path[path_length] = '\0';
182 my_executable_directory = (char *)malloc(dirname_length + 2);
183 strncpy(my_executable_directory, my_executable_path, dirname_length+1);
4a6bc37e 184 my_executable_directory[dirname_length+1] = '\0';
4197a3f6 185 }
186 }
187}
188
aa757f71
OM
189static void show_help(bool showFullHelp, char *command_line){
190 printf("syntax: %s <port> [-h|-help|-m|-f|-flush|-w|-wait|-c|-command|-l|-lua] [cmd_script_file_name] [command][lua_script_name]\n", command_line);
f5ecd97b 191 printf("\texample: %s "SERIAL_PORT_H"\n\n", command_line);
192
aa757f71
OM
193 if (showFullHelp){
194 printf("help: <-h|-help> Dump all interactive command's help at once.\n");
195 printf("\t%s -h\n\n", command_line);
196 printf("markdown: <-m> Dump all interactive help at once in markdown syntax\n");
197 printf("\t%s -m\n\n", command_line);
198 printf("flush: <-f|-flush> Output will be flushed after every print.\n");
199 printf("\t%s -f\n\n", command_line);
200 printf("wait: <-w|-wait> 20sec waiting the serial port to appear in the OS\n");
201 printf("\t%s "SERIAL_PORT_H" -w\n\n", command_line);
202 printf("script: A script file with one proxmark3 command per line.\n\n");
203 printf("command: <-c|-command> Execute one proxmark3 command.\n");
204 printf("\t%s "SERIAL_PORT_H" -c \"hf mf chk 1* ?\"\n", command_line);
205 printf("\t%s "SERIAL_PORT_H" -command \"hf mf nested 1 *\"\n\n", command_line);
206 printf("lua: <-l|-lua> Execute lua script.\n");
207 printf("\t%s "SERIAL_PORT_H" -l hf_read\n\n", command_line);
208 }
209}
5acd195d 210
902cb3c0 211int main(int argc, char* argv[]) {
9492e0b0 212 srand(time(0));
125a98a1 213
aa757f71
OM
214 bool usb_present = false;
215 bool waitCOMPort = false;
216 bool executeCommand = false;
217 bool addLuaExec = false;
218 char *script_cmds_file = NULL;
219 char *script_cmd = NULL;
f5ecd97b 220
9492e0b0 221 if (argc < 2) {
aa757f71 222 show_help(true, argv[0]);
9492e0b0 223 return 1;
224 }
aa757f71
OM
225
226 for (int i = 1; i < argc; i++) {
227 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i],"-help") == 0) {
228 show_help(false, argv[0]);
229 dumpAllHelp(0);
230 return 0;
231 }
232
233 if (strcmp(argv[i], "-m") == 0) {
234 dumpAllHelp(1);
235 return 0;
236 }
237
238 if(strcmp(argv[i],"-f") == 0 || strcmp(argv[i],"-flush") == 0){
239 printf("Output will be flushed after every print.\n");
61aaee35 240 SetFlushAfterWrite(true);
aa757f71
OM
241 }
242
243 if(strcmp(argv[i],"-w") == 0 || strcmp(argv[i],"-wait") == 0){
244 waitCOMPort = true;
245 }
246
247 if(strcmp(argv[i],"-c") == 0 || strcmp(argv[i],"-command") == 0){
248 executeCommand = true;
249 }
250
251 if(strcmp(argv[i],"-l") == 0 || strcmp(argv[i],"-lua") == 0){
252 executeCommand = true;
253 addLuaExec = true;
254 }
dec8e8bd 255 }
aa757f71
OM
256
257 // If the user passed the filename of the 'script' to execute, get it from last parameter
258 if (argc > 2 && argv[argc - 1] && argv[argc - 1][0] != '-') {
259 if (executeCommand){
260 script_cmd = argv[argc - 1];
261
262 while(script_cmd[strlen(script_cmd) - 1] == ' ')
263 script_cmd[strlen(script_cmd) - 1] = 0x00;
264
265 if (strlen(script_cmd) == 0) {
266 script_cmd = NULL;
267 } else {
268 if (addLuaExec){
269 // add "script run " to command
270 char *ctmp = NULL;
271 int len = strlen(script_cmd) + 11 + 1;
272 if ((ctmp = (char*) malloc(len)) != NULL) {
273 memset(ctmp, 0, len);
274 strcpy(ctmp, "script run ");
275 strcpy(&ctmp[11], script_cmd);
276 script_cmd = ctmp;
277 }
278 }
279
280 printf("Execute command from commandline: %s\n", script_cmd);
281 }
282 } else {
283 script_cmds_file = argv[argc - 1];
284 }
dec8e8bd 285 }
4197a3f6 286
aa757f71
OM
287 // check command
288 if (executeCommand && (!script_cmd || strlen(script_cmd) == 0)){
289 printf("ERROR: execute command: command not found.\n");
290 return 2;
291 }
292
293 // set global variables
4197a3f6 294 set_my_executable_path();
aa757f71 295
818efbeb 296 // try to open USB connection to Proxmark
297 usb_present = OpenProxmark(argv[1], waitCOMPort, 20);
7fe9b0b7 298
5acd195d 299#ifdef HAVE_GUI
c6c04491 300#ifdef _WIN32
3851172d 301 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
9492e0b0 302 MainGraphics();
c6c04491 303#else
304 char* display = getenv("DISPLAY");
305
306 if (display && strlen(display) > 1)
307 {
3851172d 308 InitGraphics(argc, argv, script_cmds_file, script_cmd, usb_present);
c6c04491 309 MainGraphics();
310 }
311 else
312 {
3851172d 313 main_loop(script_cmds_file, script_cmd, usb_present);
c6c04491 314 }
315#endif
5acd195d 316#else
3851172d 317 main_loop(script_cmds_file, script_cmd, usb_present);
5acd195d 318#endif
7fe9b0b7 319
9492e0b0 320 // Clean up the port
5acd195d 321 if (usb_present) {
818efbeb 322 CloseProxmark();
2487dfeb 323 }
324
2487dfeb 325 exit(0);
6658905f 326}
Impressum, Datenschutz