From f2a954b422b372cb43b79481e4bac2f9119aac95 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 7 May 2019 15:40:01 +0200 Subject: [PATCH] chg: save serial port name as copy. --- client/cmdhw.c | 31 +++++++++++++++++++++---------- client/comms.c | 22 +++++++--------------- client/comms.h | 3 +-- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/client/cmdhw.c b/client/cmdhw.c index 4041e793f..51ab755b8 100644 --- a/client/cmdhw.c +++ b/client/cmdhw.c @@ -503,22 +503,29 @@ static int CmdConnect(const char *Cmd) { return usage_hw_connect(); char *port = NULL; - port = (char *)Cmd; - + bool shall_free = false; + // default back to previous used serial port - if (strlen(port) == 0 ) - GetSavedSerialPortName( &port ); + if (strlen(Cmd) == 0 ) { + int len = strlen((char *)conn.serial_port_name); + if ( len == 0 ) { + return usage_hw_connect(); + } + port = calloc(len + 1, sizeof(uint8_t)); + memcpy(port, conn.serial_port_name, len); + shall_free = true; + } else { + port = (char *)Cmd; + } if ( port == NULL ) { return usage_hw_connect(); } - // if we were already connected, disconnect first. - //if (session.pm3_present) { - PrintAndLogEx(INFO, "Disconnecting from current serial port"); - CloseProxmark(); - session.pm3_present = false; - //} + // always disconnect first. + PrintAndLogEx(INFO, "Disconnecting from current serial port"); + CloseProxmark(); + session.pm3_present = false; // try to open serial port session.pm3_present = OpenProxmark(port, false, 20, false, USART_BAUD_RATE); @@ -528,6 +535,10 @@ static int CmdConnect(const char *Cmd) { CloseProxmark(); session.pm3_present = false; } + + if ( shall_free ) + free(port); + return PM3_SUCCESS; } diff --git a/client/comms.c b/client/comms.c index 7f6691390..53315c160 100644 --- a/client/comms.c +++ b/client/comms.c @@ -57,10 +57,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); -void GetSavedSerialPortName( char **dest ) { - *dest = conn.serial_port_name; -} - void SendCommand(PacketCommandOLD *c) { #ifdef COMMS_DEBUG @@ -560,7 +556,9 @@ bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode, return false; } else { // start the communication thread - conn.serial_port_name = portname; + uint16_t len = MIN( strlen(portname), FILE_PATH_SIZE - 1); + memset(conn.serial_port_name, 0, FILE_PATH_SIZE); + memcpy(conn.serial_port_name, portname, len); conn.run = true; conn.block_after_ACK = flash_mode; // Flags to tell where to add CRC on sent replies @@ -655,12 +653,6 @@ void CloseProxmark(void) { uart_close(sp); } -#if defined(__linux__) && !defined(NO_UNLINK) - // Fix for linux, it seems that it is extremely slow to release the serial port file descriptor /dev/* - // - // This may be disabled at compile-time with -DNO_UNLINK (used for a JNI-based serial port on Android). -#endif - // Clean up our state sp = NULL; memset(&communication_thread, 0, sizeof(pthread_t)); @@ -763,19 +755,19 @@ bool GetFromDevice(DeviceMemType_t memtype, uint8_t *dest, uint32_t bytes, uint3 switch (memtype) { case BIG_BUF: { - SendCommandOLD(CMD_DOWNLOAD_BIGBUF, start_index, bytes, 0, NULL, 0); + SendCommandMIX(CMD_DOWNLOAD_BIGBUF, start_index, bytes, 0, NULL, 0); return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_BIGBUF); } case BIG_BUF_EML: { - SendCommandOLD(CMD_DOWNLOAD_EML_BIGBUF, start_index, bytes, 0, NULL, 0); + SendCommandMIX(CMD_DOWNLOAD_EML_BIGBUF, start_index, bytes, 0, NULL, 0); return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_EML_BIGBUF); } case FLASH_MEM: { - SendCommandOLD(CMD_FLASHMEM_DOWNLOAD, start_index, bytes, 0, NULL, 0); + SendCommandMIX(CMD_FLASHMEM_DOWNLOAD, start_index, bytes, 0, NULL, 0); return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_FLASHMEM_DOWNLOADED); } case SIM_MEM: { - //SendCommandOLD(CMD_DOWNLOAD_SIM_MEM, start_index, bytes, 0, NULL, 0); + //SendCommandMIX(CMD_DOWNLOAD_SIM_MEM, start_index, bytes, 0, NULL, 0); //return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_SIMMEM); return false; } diff --git a/client/comms.h b/client/comms.h index 58387fc66..547a46af6 100644 --- a/client/comms.h +++ b/client/comms.h @@ -50,7 +50,7 @@ typedef struct { // To memorise baudrate uint32_t uart_speed; uint16_t last_command; - uint8_t *serial_port_name; + uint8_t serial_port_name[FILE_PATH_SIZE]; } communication_arg_t; extern communication_arg_t conn; @@ -63,7 +63,6 @@ void SendCommandMIX(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, v void clearCommandBuffer(void); #define FLASHMODE_SPEED 460800 -void GetSavedSerialPortName( char **dest ); bool IsCommunicationThreadDead(void); bool OpenProxmark(void *port, bool wait_for_port, int timeout, bool flash_mode, uint32_t speed); int TestProxmark(void);