From d7614684f859ae089c8ab6b5bb882d9ecf3c8362 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 2 Jun 2019 23:20:02 +0200 Subject: [PATCH] Make sure standalone modes can be launched when connected on USB without client --- armsrc/appmain.c | 6 +++++- client/proxmark3.c | 4 ++++ common/cmd.c | 53 +++++++++++++++++++++++++--------------------- common/cmd.h | 31 ++++++++++++++++++++++++++- common/usart.c | 9 +------- include/pm3_cmd.h | 4 +++- 6 files changed, 72 insertions(+), 35 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 556721385..c54fb6626 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -436,7 +436,7 @@ void SendCapabilities(void) { capabilities_t capabilities; capabilities.version = CAPABILITIES_VERSION; capabilities.via_fpc = reply_via_fpc; - + capabilities.via_usb = reply_via_usb; capabilities.baudrate = 0; // no real baudrate for USB-CDC #ifdef WITH_FPC_USART if (reply_via_fpc) @@ -742,6 +742,10 @@ static void PacketReceived(PacketCommandNG *packet) { */ switch (packet->cmd) { + case CMD_QUIT_SESSION: + reply_via_fpc = false; + reply_via_usb = false; + break; #ifdef WITH_LF case CMD_SET_LF_T55XX_CONFIG: setT55xxConfig(packet->oldarg[0], (t55xx_config *) packet->data.asBytes); diff --git a/client/proxmark3.c b/client/proxmark3.c index d45ddb3e8..a59f507c1 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -211,6 +211,10 @@ main_loop(char *script_cmds_file, char *script_cmd) { } } // end while + clearCommandBuffer(); + SendCommandNG(CMD_QUIT_SESSION, NULL, 0); + msleep(100); // Make sure command is sent before killing client + if (sf) fclose(sf); diff --git a/common/cmd.c b/common/cmd.c index b7abf16ee..6af0437d1 100644 --- a/common/cmd.c +++ b/common/cmd.c @@ -37,15 +37,7 @@ bool reply_with_crc_on_usb = false; bool reply_with_crc_on_fpc = true; // "Session" flag, to tell via which interface next msgs should be sent: USB or FPC USART bool reply_via_fpc = false; - -#ifdef WITH_FPC_USART_HOST -extern void Dbprintf(const char *fmt, ...); -#define Dbprintf_usb(...) {\ - bool tmp = reply_via_fpc;\ - reply_via_fpc = false;\ - Dbprintf(__VA_ARGS__);\ - reply_via_fpc = tmp;} -#endif +bool reply_via_usb = false; int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len) { PacketResponseOLD txcmd; @@ -67,20 +59,25 @@ int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *d } } - int result = PM3_EUNDEF; + int resultfpc = PM3_EUNDEF; + int resultusb = PM3_EUNDEF; // Send frame and make sure all bytes are transmitted + if (reply_via_usb) { + resultusb = usb_write((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); + } + if (reply_via_fpc) { #ifdef WITH_FPC_USART_HOST - result = usart_writebuffer_sync((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); + resultfpc = usart_writebuffer_sync((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); #else return PM3_EDEVNOTSUPP; #endif - } else { - result = usb_write((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); } - - return result; + // we got two results, let's prioritize the faulty one and USB over FPC. + if (reply_via_usb && (resultusb != PM3_SUCCESS)) return resultusb; + if (reply_via_fpc && (resultfpc != PM3_SUCCESS)) return resultfpc; + return PM3_SUCCESS; } static int reply_ng_internal(uint16_t cmd, int16_t status, uint8_t *data, size_t len, bool ng) { @@ -107,7 +104,8 @@ static int reply_ng_internal(uint16_t cmd, int16_t status, uint8_t *data, size_t } PacketResponseNGPostamble *tx_post = (PacketResponseNGPostamble *)((uint8_t *)&txBufferNG + sizeof(PacketResponseNGPreamble) + len); - if ((reply_via_fpc && reply_with_crc_on_fpc) || ((!reply_via_fpc) && reply_with_crc_on_usb)) { + // Note: if we send to both FPC & USB, we'll set CRC for both if any of them require CRC + if ((reply_via_fpc && reply_with_crc_on_fpc) || ((reply_via_usb) && reply_with_crc_on_usb)) { uint8_t first, second; compute_crc(CRC_14443_A, (uint8_t *)&txBufferNG, sizeof(PacketResponseNGPreamble) + len, &first, &second); tx_post->crc = (first << 8) + second; @@ -116,19 +114,24 @@ static int reply_ng_internal(uint16_t cmd, int16_t status, uint8_t *data, size_t } txBufferNGLen = sizeof(PacketResponseNGPreamble) + len + sizeof(PacketResponseNGPostamble); - int result = PM3_EUNDEF; + int resultfpc = PM3_EUNDEF; + int resultusb = PM3_EUNDEF; // Send frame and make sure all bytes are transmitted + if (reply_via_usb) { + resultusb = usb_write((uint8_t *)&txBufferNG, txBufferNGLen); + } if (reply_via_fpc) { #ifdef WITH_FPC_USART_HOST - result = usart_writebuffer_sync((uint8_t *)&txBufferNG, txBufferNGLen); + resultfpc = usart_writebuffer_sync((uint8_t *)&txBufferNG, txBufferNGLen); #else return PM3_EDEVNOTSUPP; #endif - } else { - result = usb_write((uint8_t *)&txBufferNG, txBufferNGLen); } - return result; + // we got two results, let's prioritize the faulty one and USB over FPC. + if (reply_via_usb && (resultusb != PM3_SUCCESS)) return resultusb; + if (reply_via_fpc && (resultfpc != PM3_SUCCESS)) return resultfpc; + return PM3_SUCCESS; } int reply_ng(uint16_t cmd, int16_t status, uint8_t *data, size_t len) { @@ -150,7 +153,7 @@ int reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *d return reply_ng_internal(cmd, status, cmddata, len + sizeof(arg), false); } -static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool fpc) { +static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool usb, bool fpc) { PacketCommandNGRaw rx_raw; size_t bytes = read_ng((uint8_t *)&rx_raw.pre, sizeof(PacketCommandNGPreamble)); @@ -202,6 +205,7 @@ static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *da if ((first << 8) + second != rx->crc) return PM3_EIO; } + reply_via_usb = usb; reply_via_fpc = fpc; } else { // Old style command PacketCommandOLD rx_old; @@ -210,6 +214,7 @@ static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *da if (bytes != sizeof(PacketCommandOLD) - sizeof(PacketCommandNGPreamble)) return PM3_EIO; + reply_via_usb = usb; reply_via_fpc = fpc; rx->ng = false; rx->magic = 0; @@ -228,12 +233,12 @@ int receive_ng(PacketCommandNG *rx) { // Check if there is a packet available if (usb_poll_validate_length()) - return receive_ng_internal(rx, usb_read_ng, false); + return receive_ng_internal(rx, usb_read_ng, true, false); #ifdef WITH_FPC_USART_HOST // Check if there is a FPC packet available if (usart_rxdata_available() > 0) - return receive_ng_internal(rx, usart_read_ng, true); + return receive_ng_internal(rx, usart_read_ng, false, true); #endif return PM3_ENODATA; } diff --git a/common/cmd.h b/common/cmd.h index 352da88d4..261a1e1a0 100644 --- a/common/cmd.h +++ b/common/cmd.h @@ -47,8 +47,37 @@ int receive_ng(PacketCommandNG *rx); // Flags to tell where to add CRC on sent replies extern bool reply_with_crc_on_usb; extern bool reply_with_crc_on_fpc; -// "Session" flag, to tell via which interface next msgs should be sent: USB or FPC USART +// "Session" flag, to tell via which interface next msgs should be sent: USB and/or FPC USART extern bool reply_via_fpc; +extern bool reply_via_usb; + +extern void Dbprintf(const char *fmt, ...); +#define Dbprintf_usb(...) {\ + bool tmpfpc = reply_via_fpc;\ + bool tmpusb = reply_via_usb;\ + reply_via_fpc = false;\ + reply_via_usb = true;\ + Dbprintf(__VA_ARGS__);\ + reply_via_fpc = tmpfpc;\ + reply_via_usb = tmpusb;} + +#define Dbprintf_fpc(...) {\ + bool tmpfpc = reply_via_fpc;\ + bool tmpusb = reply_via_usb;\ + reply_via_fpc = true;\ + reply_via_usb = false;\ + Dbprintf(__VA_ARGS__);\ + reply_via_fpc = tmpfpc;\ + reply_via_usb = tmpusb;} + +#define Dbprintf_all(...) {\ + bool tmpfpc = reply_via_fpc;\ + bool tmpusb = reply_via_usb;\ + reply_via_fpc = true;\ + reply_via_usb = true;\ + Dbprintf(__VA_ARGS__);\ + reply_via_fpc = tmpfpc;\ + reply_via_usb = tmpusb;} #endif // _PROXMARK_CMD_H_ diff --git a/common/usart.c b/common/usart.c index 734896e42..3b1587f40 100644 --- a/common/usart.c +++ b/common/usart.c @@ -8,6 +8,7 @@ //----------------------------------------------------------------------------- // The main USART code, for serial communications over FPC connector //----------------------------------------------------------------------------- +#include "cmd.h" #include "usart.h" #include "string.h" #include "../armsrc/ticks.h" // startcountus @@ -109,14 +110,6 @@ uint16_t usart_rxdata_available(void) { return sizeof(us_rxfifo) - us_rxfifo_low + us_rxfifo_high; } -extern bool reply_via_fpc; -extern void Dbprintf(const char *fmt, ...); -#define Dbprintf_usb(...) {\ - bool tmp = reply_via_fpc;\ - reply_via_fpc = false;\ - Dbprintf(__VA_ARGS__);\ - reply_via_fpc = tmp;} - uint32_t usart_read_ng(uint8_t *data, size_t len) { if (len == 0) return 0; uint32_t packetSize, nbBytesRcv = 0; diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 4ff2add80..9f05f71f3 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -141,6 +141,7 @@ typedef struct { uint8_t version; uint32_t baudrate; bool via_fpc : 1; + bool via_usb : 1; // rdv4 bool compiled_with_flash : 1; bool compiled_with_smartcard : 1; @@ -165,7 +166,7 @@ typedef struct { bool hw_available_flash : 1; bool hw_available_smartcard : 1; } PACKED capabilities_t; -#define CAPABILITIES_VERSION 1 +#define CAPABILITIES_VERSION 2 extern capabilities_t pm3_capabilities; // For CMD_T55XX_WRITE_BLOCK @@ -232,6 +233,7 @@ typedef struct { #define CMD_DOWNLOAD_EML_BIGBUF 0x0110 #define CMD_DOWNLOADED_EML_BIGBUF 0x0111 #define CMD_CAPABILITIES 0x0112 +#define CMD_QUIT_SESSION 0x0113 // RDV40, Flash memory operations #define CMD_FLASHMEM_READ 0x0120