From 820a6d99a9b32afb7a7285dae394ac62c505159e Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 30 Apr 2019 23:52:40 +0200 Subject: [PATCH] better offline mode --- client/cmdlf.c | 6 +++--- client/cmdlfawid.c | 2 +- client/cmdlfhid.c | 2 +- client/cmdlft55xx.c | 4 ++-- client/cmdparser.c | 15 ++++++++++++--- client/comms.c | 23 +++++------------------ client/comms.h | 3 --- client/proxgui.cpp | 8 ++++---- client/proxgui.h | 2 +- client/proxguiqt.h | 3 +-- client/proxmark3.c | 33 +++++++++++++++------------------ client/proxmark3.h | 2 +- client/ui.h | 1 + 13 files changed, 47 insertions(+), 57 deletions(-) diff --git a/client/cmdlf.c b/client/cmdlf.c index 1e5878cf2..593c59d12 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -333,7 +333,7 @@ int CmdLFSetConfig(const char *Cmd) { } bool lf_read(bool silent, uint32_t samples) { - if (IsOffline()) return false; + if (!session.pm3_present) return false; clearCommandBuffer(); SendCommandOLD(CMD_ACQUIRE_RAW_ADC_SAMPLES_125K, silent, samples, 0, NULL, 0); @@ -354,7 +354,7 @@ bool lf_read(bool silent, uint32_t samples) { int CmdLFRead(const char *Cmd) { - if (IsOffline()) return 0; + if (!session.pm3_present) return 0; bool errors = false; bool silent = false; @@ -843,7 +843,7 @@ int CmdLFfind(const char *Cmd) { if (cmdp == 'u') testRaw = 'u'; - bool isOnline = (!IsOffline() && (cmdp != '1')); + bool isOnline = (session.pm3_present && (cmdp != '1')); if (isOnline) lf_read(true, 30000); diff --git a/client/cmdlfawid.c b/client/cmdlfawid.c index a65abc909..fc57e9597 100644 --- a/client/cmdlfawid.c +++ b/client/cmdlfawid.c @@ -467,7 +467,7 @@ static int CmdAWIDBrute(const char *Cmd) { // main loop for (;;) { - if (IsOffline()) { + if (!session.pm3_present) { PrintAndLogEx(WARNING, "Device offline\n"); return 2; } diff --git a/client/cmdlfhid.c b/client/cmdlfhid.c index 8acf9037e..857d79adb 100644 --- a/client/cmdlfhid.c +++ b/client/cmdlfhid.c @@ -569,7 +569,7 @@ static int CmdHIDBrute(const char *Cmd) { // main loop for (;;) { - if (IsOffline()) { + if (!session.pm3_present) { PrintAndLogEx(WARNING, "Device offline\n"); return 2; } diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c index 8057c7469..661d4bdbb 100644 --- a/client/cmdlft55xx.c +++ b/client/cmdlft55xx.c @@ -505,7 +505,7 @@ static bool DecodeT5555TraceBlock(void) { // sanity check. Don't use proxmark if it is offline and you didn't specify useGraphbuf static int SanityOfflineCheck(bool useGraphBuffer) { - if (!useGraphBuffer && IsOffline()) { + if (!useGraphBuffer && !session.pm3_present) { PrintAndLogEx(WARNING, "Your proxmark3 device is offline. Specify [1] to use graphbuffer data instead"); return 0; } @@ -1914,7 +1914,7 @@ static int CmdT55xxChkPwds(const char *Cmd) { uint64_t curr_password = 0x00; for (uint16_t c = 0; c < keycount; ++c) { - if (IsOffline()) { + if (!session.pm3_present) { PrintAndLogEx(WARNING, "Device offline\n"); free(keyBlock); return 2; diff --git a/client/cmdparser.c b/client/cmdparser.c index 75a117045..bb24e532b 100644 --- a/client/cmdparser.c +++ b/client/cmdparser.c @@ -21,7 +21,7 @@ void CmdsHelp(const command_t Commands[]) { if (Commands[0].Name == NULL) return; int i = 0; while (Commands[i].Name) { - if (!IsOffline() || Commands[i].Offline) + if (session.pm3_present || Commands[i].Offline) PrintAndLogEx(NORMAL, "%-16s %s", Commands[i].Name, Commands[i].Help); ++i; } @@ -44,8 +44,17 @@ int CmdsParse(const command_t Commands[], const char *Cmd) { sscanf(Cmd, "%127s%n", cmd_name, &len); str_lower(cmd_name); int i = 0; - while (Commands[i].Name && strcmp(Commands[i].Name, cmd_name)) + while (Commands[i].Name) { + if (0 == strcmp(Commands[i].Name, cmd_name)) { + if (session.pm3_present || Commands[i].Offline) { + break; + } else { + PrintAndLogEx(WARNING, "This command is only available in " _YELLOW_("online") "mode"); + return PM3_ENOTIMPL; + } + } ++i; + } /* try to find exactly one prefix-match */ if (!Commands[i].Name) { @@ -53,7 +62,7 @@ int CmdsParse(const command_t Commands[], const char *Cmd) { int matches = 0; for (i = 0; Commands[i].Name; i++) { - if (!strncmp(Commands[i].Name, cmd_name, strlen(cmd_name))) { + if (!strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) && (session.pm3_present || Commands[i].Offline)) { last_match = i; matches++; } diff --git a/client/comms.c b/client/comms.c index 5bff3c13b..d1f42a8a4 100644 --- a/client/comms.c +++ b/client/comms.c @@ -23,9 +23,6 @@ static serial_port sp = NULL; static char *serial_port_name = NULL; -// If TRUE, then there is no active connection to the PM3, and we will drop commands sent. -static bool offline; - communication_arg_t conn; capabilities_t pm3_capabilities; @@ -59,16 +56,6 @@ static uint64_t timeout_start_time; static bool dl_it(uint8_t *dest, uint32_t bytes, uint32_t start_index, PacketResponseNG *response, size_t ms_timeout, bool show_warning, uint32_t rec_cmd); -// These wrappers are required because it is not possible to access a static -// global variable outside of the context of a single file. -void SetOffline(bool value) { - offline = value; -} - -bool IsOffline() { - return offline; -} - void SendCommand(PacketCommandOLD *c) { #ifdef COMMS_DEBUG @@ -80,7 +67,7 @@ void SendCommand(PacketCommandOLD *c) { print_hex_break((uint8_t *)&c->d, sizeof(c->d), 32); #endif - if (offline) { + if (!session.pm3_present) { PrintAndLogEx(WARNING, "Sending bytes to Proxmark3 failed." _YELLOW_("offline")); return; } @@ -123,7 +110,7 @@ static void SendCommandNG_internal(uint16_t cmd, uint8_t *data, size_t len, bool PrintAndLogEx(NORMAL, "Sending %s", ng ? "NG" : "MIX"); #endif - if (offline) { + if (!session.pm3_present) { PrintAndLogEx(NORMAL, "Sending bytes to proxmark failed - offline"); return; } @@ -326,18 +313,18 @@ bool hookUpPM3() { sp = NULL; serial_port_name = NULL; ret = false; - offline = 1; + session.pm3_present = false; } else if (sp == CLAIMED_SERIAL_PORT) { PrintAndLogEx(WARNING, "Reconnect failed, retrying... (reason: serial port is claimed by another process)\n"); sp = NULL; serial_port_name = NULL; ret = false; - offline = 1; + session.pm3_present = false; } else { PrintAndLogEx(SUCCESS, "Proxmark3 reconnected\n"); serial_port_name = ; ret = true; - offline = 0; + session.pm3_present = true; } return ret; } diff --git a/client/comms.h b/client/comms.h index c53b75bee..9edf030cd 100644 --- a/client/comms.h +++ b/client/comms.h @@ -53,9 +53,6 @@ typedef struct { extern communication_arg_t conn; -void SetOffline(bool value); -bool IsOffline(void); - void *uart_receiver(void *targ); void SendCommand(PacketCommandOLD *c); void SendCommandOLD(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len); diff --git a/client/proxgui.cpp b/client/proxgui.cpp index bc382d1c2..120f2b26d 100644 --- a/client/proxgui.cpp +++ b/client/proxgui.cpp @@ -15,14 +15,14 @@ static ProxGuiQT *gui = NULL; static WorkerThread *main_loop_thread = NULL; -WorkerThread::WorkerThread(char *script_cmds_file, char *script_cmd, bool pm3_present) : script_cmds_file(script_cmds_file), script_cmd(script_cmd), pm3_present(pm3_present) { +WorkerThread::WorkerThread(char *script_cmds_file, char *script_cmd) : script_cmds_file(script_cmds_file), script_cmd(script_cmd) { } WorkerThread::~WorkerThread() { } void WorkerThread::run() { - main_loop(script_cmds_file, script_cmd, pm3_present); + main_loop(script_cmds_file, script_cmd); } extern "C" void ShowGraphWindow(void) { @@ -53,12 +53,12 @@ extern "C" void MainGraphics(void) { gui->MainLoop(); } -extern "C" void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd, bool pm3_present) { +extern "C" void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd) { #ifdef Q_WS_X11 if (getenv("DISPLAY") == NULL) return; #endif - main_loop_thread = new WorkerThread(script_cmds_file, script_cmd, pm3_present); + main_loop_thread = new WorkerThread(script_cmds_file, script_cmd); gui = new ProxGuiQT(argc, argv, main_loop_thread); } diff --git a/client/proxgui.h b/client/proxgui.h index 40fb1ce2d..dfbddbc0c 100644 --- a/client/proxgui.h +++ b/client/proxgui.h @@ -22,7 +22,7 @@ void ShowGraphWindow(void); void HideGraphWindow(void); void RepaintGraphWindow(void); void MainGraphics(void); -void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd, bool pm3_present); +void InitGraphics(int argc, char **argv, char *script_cmds_file, char *script_cmd); void ExitGraphics(void); #ifndef MAX_GRAPH_TRACE_LEN #define MAX_GRAPH_TRACE_LEN (40000 * 8) diff --git a/client/proxguiqt.h b/client/proxguiqt.h index 2685aecde..f8f919307 100644 --- a/client/proxguiqt.h +++ b/client/proxguiqt.h @@ -93,13 +93,12 @@ class ProxWidget : public QWidget { class WorkerThread : public QThread { Q_OBJECT; public: - WorkerThread(char *, char *, bool); + WorkerThread(char *, char *); ~WorkerThread(); void run(); private: char *script_cmds_file = NULL; char *script_cmd = NULL; - bool pm3_present; }; class ProxGuiQT : public QObject { diff --git a/client/proxmark3.c b/client/proxmark3.c index 87e4ee814..2a6ff7aa4 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -58,7 +58,7 @@ void __attribute__((force_align_arg_pointer)) #endif #endif -main_loop(char *script_cmds_file, char *script_cmd, bool pm3_present) { +main_loop(char *script_cmds_file, char *script_cmd) { char *cmd = NULL; bool execCommand = (script_cmd != NULL); @@ -73,15 +73,12 @@ main_loop(char *script_cmds_file, char *script_cmd, bool pm3_present) { PrintAndLogEx(DEBUG, "ISATTY/STDIN_FILENO == %s\n", (stdinOnPipe) ? "true" : "false"); - if (pm3_present) { - SetOffline(false); + if (session.pm3_present) { // cache Version information now: if (execCommand || script_cmds_file || stdinOnPipe) pm3_version(false); else pm3_version(true); - } else { - SetOffline(true); } if (script_cmds_file) { @@ -100,13 +97,13 @@ main_loop(char *script_cmds_file, char *script_cmd, bool pm3_present) { bool printprompt = false; // this should hook up the PM3 again. /* - if ( IsOffline() ) { + if ( !session.pm3_present ) { // sets the global variable, SP and offline) - pm3_present = hookUpPM3(); + session.pm3_present = hookUpPM3(); // usb and the reader_thread is NULL, create a new reader thread. - if (pm3_present && !IsOffline() ) { + if (session.pm3_present) { rarg.run = 1; pthread_create(&reader_thread, NULL, &uart_receiver, &rarg); // cache Version information now: @@ -293,7 +290,7 @@ static void show_help(bool showFullHelp, char *exec_name) { int main(int argc, char *argv[]) { srand(time(0)); - bool pm3_present = false; + session.pm3_present = false; bool waitCOMPort = false; bool addLuaExec = false; char *script_cmds_file = NULL; @@ -489,38 +486,38 @@ int main(int argc, char *argv[]) { // try to open USB connection to Proxmark if (port != NULL) - pm3_present = OpenProxmark(port, waitCOMPort, 20, false, speed); + session.pm3_present = OpenProxmark(port, waitCOMPort, 20, false, speed); - if (pm3_present && (TestProxmark() != PM3_SUCCESS)) { + if (session.pm3_present && (TestProxmark() != PM3_SUCCESS)) { PrintAndLogEx(ERR, _RED_("ERROR:") "cannot communicate with the Proxmark\n"); CloseProxmark(); - pm3_present = false; + session.pm3_present = false; } - if (!pm3_present) + if (!session.pm3_present) PrintAndLogEx(INFO, "Running in " _YELLOW_("OFFLINE") "mode. Check \"%s -h\" if it's not what you want.\n", exec_name); #ifdef HAVE_GUI # ifdef _WIN32 - InitGraphics(argc, argv, script_cmds_file, script_cmd, pm3_present); + InitGraphics(argc, argv, script_cmds_file, script_cmd); MainGraphics(); # else // for *nix distro's, check enviroment variable to verify a display char *display = getenv("DISPLAY"); if (display && strlen(display) > 1) { - InitGraphics(argc, argv, script_cmds_file, script_cmd, pm3_present); + InitGraphics(argc, argv, script_cmds_file, script_cmd); MainGraphics(); } else { - main_loop(script_cmds_file, script_cmd, pm3_present); + main_loop(script_cmds_file, script_cmd); } # endif #else - main_loop(script_cmds_file, script_cmd, pm3_present); + main_loop(script_cmds_file, script_cmd); #endif // Clean up the port - if (pm3_present) { + if (session.pm3_present) { CloseProxmark(); } diff --git a/client/proxmark3.h b/client/proxmark3.h index 20664ab81..afebc1c0a 100644 --- a/client/proxmark3.h +++ b/client/proxmark3.h @@ -23,7 +23,7 @@ extern "C" { const char *get_my_executable_path(void); const char *get_my_executable_directory(void); -void main_loop(char *script_cmds_file, char *script_cmd, bool pm3_present); +void main_loop(char *script_cmds_file, char *script_cmd); #ifdef __cplusplus } diff --git a/client/ui.h b/client/ui.h index 0ffe02aa5..5ab30b3e1 100644 --- a/client/ui.h +++ b/client/ui.h @@ -28,6 +28,7 @@ typedef struct { bool stdinOnTTY; bool stdoutOnTTY; bool supports_colors; + bool pm3_present; } session_arg_t; extern session_arg_t session;