diff --git a/armsrc/Makefile b/armsrc/Makefile index 4be839947..498be215f 100644 --- a/armsrc/Makefile +++ b/armsrc/Makefile @@ -107,6 +107,12 @@ else SRC_FPC = endif +ifneq (,$(findstring WITH_BWM_FORWARD,$(APP_CFLAGS))) + SRC_BWM = bwm_uart_at32.c bwm_forward.c +else + SRC_BWM = +endif + ifneq (,$(findstring WITH_HITAG,$(APP_CFLAGS))) SRC_HITAG = hitag2_crypto.c hitag_common.c hitag2.c hitagS.c hitagu.c hitag2_crack.c APP_CFLAGS += -I../common/hitag2 @@ -163,6 +169,7 @@ THUMBSRC = start.c \ $(SRC_FLASH) \ $(SRC_SMARTCARD) \ $(SRC_FPC) \ + $(SRC_BWM) \ $(SRC_HITAG) \ $(SRC_EM4x50) \ $(SRC_EM4x70) \ diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 07bac542f..8f9c74a05 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -22,6 +22,9 @@ #include "sys_apis.h" #include "usb_cdc_apis.h" +#ifdef WITH_BWM_FORWARD +#include "bwm_uart_at32.h" +#endif #include "proxmark3_arm.h" #include "dbprint.h" #include "pmflash.h" @@ -1077,6 +1080,12 @@ static void SendCapabilities(void) { if (g_reply_via_fpc) capabilities.baudrate = g_usart_baudrate; #endif +#ifdef WITH_BWM_FORWARD + // BWM link runs at a fixed baud; report it so the client's timeout math + // (communication_delay: 12000000 / uart_speed) doesn't divide by zero. + if (g_reply_via_fpc) + capabilities.baudrate = BWM_UART_BAUD; +#endif #ifdef RDV4 capabilities.is_rdv4 = true; @@ -4254,6 +4263,10 @@ void __attribute__((noreturn)) AppMain(void) { bwm_detect_and_init(); // probe BWM + apply charge config, off the pre-USB path #endif +#ifdef WITH_BWM_FORWARD + bwm_uart_init(); // AT32 UART4 <-> BWM app_com link; AFTER usb_enable() +#endif + #ifdef WITH_BWM_CHARGERKICK BWM_ChargerKick(); #endif diff --git a/armsrc/cmd.c b/armsrc/cmd.c index 492b4aa26..336334b8f 100644 --- a/armsrc/cmd.c +++ b/armsrc/cmd.c @@ -17,6 +17,9 @@ #include "usb_cdc_apis.h" #include "usb_read_ng.h" #include "usart.h" +#ifdef WITH_BWM_FORWARD +#include "bwm_forward.h" +#endif #include "crc16.h" #include "string.h" #include "BigBuf.h" @@ -49,7 +52,7 @@ int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v } } -#ifdef WITH_FPC_USART_HOST +#if defined(WITH_FPC_USART_HOST) || defined(WITH_BWM_FORWARD) int resultfpc = PM3_EUNDEF; #endif int resultusb = PM3_EUNDEF; @@ -60,7 +63,9 @@ int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v } if (g_reply_via_fpc) { -#ifdef WITH_FPC_USART_HOST +#if defined(WITH_BWM_FORWARD) + resultfpc = bwm_fwd_writebuffer_sync((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); +#elif defined(WITH_FPC_USART_HOST) resultfpc = usart_writebuffer_sync((uint8_t *)&txcmd, sizeof(PacketResponseOLD)); #else return PM3_EDEVNOTSUPP; @@ -68,7 +73,7 @@ int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v } // we got two results, let's prioritize the faulty one and USB over FPC. if (g_reply_via_usb && (resultusb != PM3_SUCCESS)) return resultusb; -#ifdef WITH_FPC_USART_HOST +#if defined(WITH_FPC_USART_HOST) || defined(WITH_BWM_FORWARD) if (g_reply_via_fpc && (resultfpc != PM3_SUCCESS)) return resultfpc; #endif return PM3_SUCCESS; @@ -113,7 +118,7 @@ static int reply_ng_internal(uint16_t cmd, int8_t status, uint8_t reason, const } txBufferNGLen = sizeof(PacketResponseNGPreamble) + len + sizeof(PacketResponseNGPostamble); -#ifdef WITH_FPC_USART_HOST +#if defined(WITH_FPC_USART_HOST) || defined(WITH_BWM_FORWARD) int resultfpc = PM3_EUNDEF; #endif int resultusb = PM3_EUNDEF; @@ -127,7 +132,9 @@ static int reply_ng_internal(uint16_t cmd, int8_t status, uint8_t reason, const // TODO DXL 测试阶段,暂时通过SPI应答 // resultusb = cep_spi_write_sync((uint8_t *)&txBufferNG, txBufferNGLen); -#ifdef WITH_FPC_USART_HOST +#if defined(WITH_BWM_FORWARD) + resultfpc = bwm_fwd_writebuffer_sync((uint8_t *)&txBufferNG, txBufferNGLen); +#elif defined(WITH_FPC_USART_HOST) resultfpc = usart_writebuffer_sync((uint8_t *)&txBufferNG, txBufferNGLen); #else return PM3_EDEVNOTSUPP; @@ -138,7 +145,7 @@ static int reply_ng_internal(uint16_t cmd, int8_t status, uint8_t reason, const return resultusb; } -#ifdef WITH_FPC_USART_HOST +#if defined(WITH_FPC_USART_HOST) || defined(WITH_BWM_FORWARD) if (g_reply_via_fpc && (resultfpc != PM3_SUCCESS)) { return resultfpc; } @@ -274,7 +281,11 @@ int receive_ng(PacketCommandNG *rx) { // return receive_ng_internal(rx, cep_spi_read_ng, false, true); // TODO DXL 临时用fpc这种标志 // } -#ifdef WITH_FPC_USART_HOST +#if defined(WITH_BWM_FORWARD) + // De-frame inbound BWM app_com DATA_FORWARD packets into a raw NG stream. + if (bwm_fwd_rxdata_available() > 0) + return receive_ng_internal(rx, bwm_read_ng, false, true); +#elif defined(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, false, true);