diff --git a/armsrc/appmain.c b/armsrc/appmain.c index db5843ece..0e10529e0 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1116,7 +1116,19 @@ static void SendCapabilities(void) { #ifdef WITH_SMARTCARD capabilities.compiled_with_smartcard = true; uint8_t maj, min; - capabilities.hw_available_smartcard = I2C_get_version(&maj, &min) == PM3_SUCCESS; + + // The module may still be finishing a card exchange from a previous + // session when this runs, and a single missed answer used to report the + // hardware as absent for the whole of the next one - every smartcard + // command then said it was not available in this mode. + capabilities.hw_available_smartcard = false; + for (uint8_t i = 0; i < 3; i++) { + if (I2C_get_version(&maj, &min) == PM3_SUCCESS) { + capabilities.hw_available_smartcard = true; + break; + } + SpinDelay(50); + } #else capabilities.compiled_with_smartcard = false; capabilities.hw_available_smartcard = false; diff --git a/client/src/cmdsmartcard.c b/client/src/cmdsmartcard.c index 410b25362..5b5bd2634 100644 --- a/client/src/cmdsmartcard.c +++ b/client/src/cmdsmartcard.c @@ -622,7 +622,7 @@ static int CmdSmartUpgrade(const char *Cmd) { CLIParserContext *ctx; CLIParserInit(&ctx, "smart upgrade", "Upgrade RDV4 sim module firmware", - "smart upgrade -f sim014.bin" + "smart upgrade -f sim017.bin" ); void *argtable[] = { diff --git a/client/src/comms.c b/client/src/comms.c index a45bf3962..a4052471e 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -138,6 +138,10 @@ void SendCommandOLD(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, c pthread_mutex_unlock(&txBufferMutex); + // and interrupt its receive, which it would otherwise sit out in full + // before looking at the send buffer at all + uart_wakeup(); + //__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST); } @@ -202,6 +206,10 @@ static void SendCommandNG_internal(uint16_t cmd, uint8_t *data, size_t len, bool pthread_mutex_unlock(&txBufferMutex); + // and interrupt its receive, which it would otherwise sit out in full + // before looking at the send buffer at all + uart_wakeup(); + //__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST); } diff --git a/client/src/emv/cmdemv.c b/client/src/emv/cmdemv.c index 7309d30fe..36a7eec74 100644 --- a/client/src/emv/cmdemv.c +++ b/client/src/emv/cmdemv.c @@ -799,6 +799,16 @@ static int CmdEMVSearch(const char *Cmd) { struct tlvdb *t = tlvdb_fixed(1, strlen(al), (const unsigned char *)al); if (EMVSearch(channel, activateField, leaveSignalON, decodeTLV, t, false)) { + + PrintAndLogEx(FAILED, "Search failed, no answer from the card"); + + // Without activation the ARM never reads the ATR, so it does not know + // which protocols the card offers and cannot redirect a T=0 request to + // a T=1 only card. That used to end here without a word. + if ((channel == CC_CONTACT) && (activateField == false)) { + PrintAndLogEx(HINT, "Hint: the card is activated by `" _YELLOW_("-s") "`, try `" _YELLOW_("emv search -w -s") "`"); + } + tlvdb_free(t); SetAPDULogging(false); return PM3_ERFTRANS; diff --git a/client/src/emv/emvcore.c b/client/src/emv/emvcore.c index 985c3b769..24671a068 100644 --- a/client/src/emv/emvcore.c +++ b/client/src/emv/emvcore.c @@ -266,6 +266,11 @@ void TLVPrintAIDlistFromSelectTLV(struct tlvdb *tlv) { ttmp = tlvdb_find_next(ttmp, 0x6f); } + if (n == 0) { + PrintAndLogEx(INFO, "No applications found"); + return; + } + size_t w_aid = strlen("AID"); size_t w_prio = strlen("Priority"); size_t w_name = strlen("Name"); @@ -289,10 +294,6 @@ void TLVPrintAIDlistFromSelectTLV(struct tlvdb *tlv) { (int)w_aid, "AID", (int)w_prio, "Priority", (int)w_name, "Name"); PrintAndLogEx(INFO, "%s", rule); - if (n == 0) { - PrintAndLogEx(INFO, "| %-*s |", (int)(w_aid + w_prio + w_name + 6), "none"); - } - for (size_t i = 0; i < n; i++) { PrintAndLogEx(INFO, "| " _YELLOW_("%-*s") " | %-*s | " _CYAN_("%-*s") " |", (int)w_aid, rows[i].aid, diff --git a/client/src/uart/uart.h b/client/src/uart/uart.h index 22aab5556..902fa4a87 100644 --- a/client/src/uart/uart.h +++ b/client/src/uart/uart.h @@ -76,6 +76,13 @@ bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed); */ uint32_t uart_get_speed(const serial_port sp); +/* Wake a thread blocked in uart_receive() so it can get on and service a + * queued send. The comm thread receives before it transmits, so without this a + * command sits in the buffer until the receive phase times out. + * Safe to call from another thread; a no-op where it is not implemented. + */ +void uart_wakeup(void); + /* Reconfigure timeouts (ms) */ int uart_reconfigure_timeouts(uint32_t value); diff --git a/client/src/uart/uart_posix.c b/client/src/uart/uart_posix.c index 3e8f63706..3111eb466 100644 --- a/client/src/uart/uart_posix.c +++ b/client/src/uart/uart_posix.c @@ -36,6 +36,7 @@ #include #include #include +#include #ifdef HAVE_BLUEZ #include @@ -74,6 +75,36 @@ static uint32_t newtimeout_value = 0; static bool newtimeout_pending = false; static uint8_t rx_empty_counter = 0; +// Self pipe used to interrupt the select() in uart_receive(). Created once, on +// first use, and never closed - two descriptors for the life of the process, +// and both ends have to stay valid for whichever thread touches them. +static int wakeup_pipe[2] = { -1, -1 }; +static pthread_once_t wakeup_once = PTHREAD_ONCE_INIT; + +static void uart_wakeup_create(void) { + if (pipe(wakeup_pipe) != 0) { + wakeup_pipe[0] = -1; + wakeup_pipe[1] = -1; + return; + } + // Neither end may ever block: a full pipe already means a wakeup is + // pending, which is all the reader needs to know. + fcntl(wakeup_pipe[0], F_SETFL, O_NONBLOCK); + fcntl(wakeup_pipe[1], F_SETFL, O_NONBLOCK); +} + +void uart_wakeup(void) { + pthread_once(&wakeup_once, uart_wakeup_create); + + if (wakeup_pipe[1] < 0) { + return; + } + + const uint8_t b = 1; + ssize_t res = write(wakeup_pipe[1], &b, 1); + (void)res; // EAGAIN just means a wakeup is already queued +} + int uart_reconfigure_timeouts(uint32_t value) { newtimeout_value = value; newtimeout_pending = true; @@ -616,18 +647,22 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin // Reset file descriptor FD_ZERO(&rfds); FD_SET(spu->fd, &rfds); - tv = timeout; - if ((*pszRxLen == 0) && - (spu->udpBuffer == NULL) && - (timeout.tv_sec == 0) && - (timeout.tv_usec <= UART_FPC_CLIENT_RX_TIMEOUT_MS * 1000)) { - // Nothing of a frame in hand yet - see the header note. Local links - // only: a network link keeps its own longer timeout, where polling - // this fast would cost more than it saves. - tv.tv_sec = 0; - tv.tv_usec = UART_USB_CLIENT_RX_IDLE_TIMEOUT_MS * 1000; + int maxfd = spu->fd; + + // Only watch the wakeup while no part of a frame is in hand. Cutting a + // frame short would hand the caller a partial packet, and leaving the + // pipe readable while mid frame would spin. + pthread_once(&wakeup_once, uart_wakeup_create); + bool watch_wakeup = ((*pszRxLen == 0) && (wakeup_pipe[0] >= 0)); + if (watch_wakeup) { + FD_SET(wakeup_pipe[0], &rfds); + if (wakeup_pipe[0] > maxfd) { + maxfd = wakeup_pipe[0]; + } } - res = select(spu->fd + 1, &rfds, NULL, NULL, &tv); + + tv = timeout; + res = select(maxfd + 1, &rfds, NULL, NULL, &tv); // Read error if (res < 0) { @@ -645,6 +680,19 @@ int uart_receive(const serial_port sp, uint8_t *pbtRx, uint32_t pszMaxRxLen, uin } } + if (watch_wakeup && FD_ISSET(wakeup_pipe[0], &rfds)) { + + uint8_t drain[64]; + while (read(wakeup_pipe[0], drain, sizeof(drain)) > 0) { }; + + // Nothing on the wire, only a send waiting. Return before the + // FIONREAD below, which counts empty reads towards declaring the + // device gone. + if (FD_ISSET(spu->fd, &rfds) == false) { + return PM3_ENODATA; + } + } + // Retrieve the count of the incoming bytes res = ioctl(spu->fd, FIONREAD, &byteCount); // PrintAndLogEx(ERR, "UART:: RX ioctl res %d byteCount %u", res, byteCount); diff --git a/client/src/uart/uart_win32.c b/client/src/uart/uart_win32.c index 75bd156c7..7613f785f 100644 --- a/client/src/uart/uart_win32.c +++ b/client/src/uart/uart_win32.c @@ -51,6 +51,10 @@ uint32_t newtimeout_value = 0; bool newtimeout_pending = false; uint8_t rx_empty_counter = 0; +// Not implemented here: the win32 receive path would need an event object in +// its wait instead of a pipe. Leaving it a no-op keeps that platform as it was. +void uart_wakeup(void) { } + int uart_reconfigure_timeouts(uint32_t value) { newtimeout_value = value; newtimeout_pending = true; diff --git a/client/src/util.c b/client/src/util.c index f6a556fd3..8151dcfe9 100644 --- a/client/src/util.c +++ b/client/src/util.c @@ -272,7 +272,10 @@ void hex_to_buffer(uint8_t *buf, const uint8_t *hex_data, const size_t hex_len, m = hex_max_len; } - while (m--) { + // Pad up to m, the way ascii_to_buffer() above does. This used to write m + // spaces from wherever it had got to, so a padded field came out i + // characters too wide - a 7 byte AID asked to fill 16 produced 30. + for (; i < m; i++) { *(tmp++) = ' '; } diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 86a3d3001..c4f23c75f 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -1151,13 +1151,6 @@ typedef struct { // uart_win32.c & uart_posix.c #define UART_FPC_CLIENT_RX_TIMEOUT_MS 200 #define UART_USB_CLIENT_RX_TIMEOUT_MS 20 -// Idle poll, used only while no part of a frame is in hand. The comm thread -// transmits a queued command only after its receive phase returns, so a long -// idle wait here delays every single command by up to one full timeout. -// Returning "no data" promptly costs nothing; once part of a frame IS in hand -// the full timeout above applies, so a frame split across USB frames is never -// truncated. -#define UART_USB_CLIENT_RX_IDLE_TIMEOUT_MS 1 #define UART_NET_CLIENT_RX_TIMEOUT_MS 500 #define UART_TCP_LOCAL_CLIENT_RX_TIMEOUT_MS 40 #define UART_UDP_LOCAL_CLIENT_RX_TIMEOUT_MS 20 diff --git a/tools/pm3_tests.sh b/tools/pm3_tests.sh index 6bbe32e19..f12d81f2c 100755 --- a/tools/pm3_tests.sh +++ b/tools/pm3_tests.sh @@ -358,7 +358,7 @@ while true; do if $TESTALL || $TESTMFDAESBRUTE; then echo -e "\n${C_BLUE}Testing mfd_aes_brute:${C_NC} ${MFDASEBRUTEBIN:=./tools/mfd_aes_brute/mfd_aes_brute}" if ! CheckFileExist "mfd_aes_brute exists" "$MFDASEBRUTEBIN"; then break; fi - if ! CheckExecute "mfd_aes_brute test 1/2" "$MFDASEBRUTEBIN 1629394800 bb6aea729414a5b1eff7b16328ce37fd 82f5f498dbc29f7570102397a2e5ef2b6dc14a864f665b3c54d11765af81e95c" "key.................... .*261C07A23F2BC8262F69F10A5BDF3764"; then break; fi + if ! CheckExecute slow "mfd_aes_brute test 1/2" "$MFDASEBRUTEBIN 1629394800 bb6aea729414a5b1eff7b16328ce37fd 82f5f498dbc29f7570102397a2e5ef2b6dc14a864f665b3c54d11765af81e95c" "key.................... .*261C07A23F2BC8262F69F10A5BDF3764"; then break; fi if ! CheckExecute slow "mfd_aes_brute test 2/2" "$MFDASEBRUTEBIN 1546300800 3fda933e2953ca5e6cfbbf95d1b51ddf 97fe4b5de24188458d102959b888938c988e96fb98469ce7426f50f108eaa583" "key.................... .*E757178E13516A4F3171BC6EA85E165A"; then break; fi fi if $TESTALL || $TESTMFULCDESBRUTE; then