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