Add files via upload

Signed-off-by: Niel Nielsen <nieldk@gmail.com>
This commit is contained in:
Niel Nielsen authored and Philippe Teuwen committed 2026-08-26 16:07:37 +02:00
1 parent 3553836b7c
commit a82c6e4fe9
4 files changed
+26 -106

No files matched your search

+13 -38
View File
@@ -14,15 +14,13 @@
#include "bwm_forward.h"
#include "bwm_uart_at32.h"
#include "pm3_cmd.h" // PM3_CMD_DATA_SIZE, PM3_* return codes
#include "pm3_cmd.h"
#include "string.h"
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
// CRC-16/CCITT-FALSE, byte-identical to the BWM firmware's crc16_ccitt()
// (poly 0x1021, init 0xFFFF, MSB-first, no reflection, no xorout).
static uint16_t bwm_crc16(const uint8_t *data, size_t len, uint16_t crc) {
for (size_t i = 0; i < len; i++) {
crc ^= (uint16_t)data[i] << 8;
@@ -37,20 +35,15 @@ static uint16_t bwm_crc16(const uint8_t *data, size_t len, uint16_t crc) {
return crc;
}
// ---------------------------------------------------------------------------
// TX: wrap one reply frame into a SEND_FORWARD_DATA app_com frame.
// A full NG/OLD frame is <= PM3_CMD_DATA_SIZE + a small header/postamble, well
// under the BWM 4096-byte payload cap, so a single frame always suffices.
// ---------------------------------------------------------------------------
#define BWM_TX_OVERHEAD (2 + 2 + 2 + 2) // hdr + cmd + len + crc
#define BWM_TX_MAX_PAYLOAD (PM3_CMD_DATA_SIZE + 64) // NG/OLD frame ceiling
#define BWM_TX_OVERHEAD (2 + 2 + 2 + 2)
#define BWM_TX_MAX_PAYLOAD (PM3_CMD_DATA_SIZE + 64)
#define BWM_TX_BUFSZ (BWM_TX_OVERHEAD + BWM_TX_MAX_PAYLOAD)
int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) {
static uint8_t frame[BWM_TX_BUFSZ]; // single-threaded bare-metal: static OK
static uint8_t frame[BWM_TX_BUFSZ];
if (len > BWM_TX_MAX_PAYLOAD) {
len = BWM_TX_MAX_PAYLOAD; // defensive; should never trigger
len = BWM_TX_MAX_PAYLOAD;
}
size_t idx = 0;
@@ -71,13 +64,7 @@ int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) {
return bwm_uart_write(frame, idx);
}
// ---------------------------------------------------------------------------
// RX: persistent app_com de-framer. Feeds raw FPC bytes through a state machine
// and pushes the payloads of valid DATA_FORWARD (0xD2 0xD3 / cmd 8089) frames
// into a byte FIFO that bwm_read_ng() drains. Non-DATA_FORWARD frames (slave
// responses, forwarded logs, cmd-error reports) are validated and discarded.
// ---------------------------------------------------------------------------
#define BWM_DEFIFO_SZ 2048 // >= one full NG frame's payload
#define BWM_DEFIFO_SZ 2048
#define BWM_RXFRAME_MAX (PM3_CMD_DATA_SIZE + 64)
typedef enum {
@@ -87,28 +74,27 @@ typedef enum {
typedef struct {
bwm_state_t state;
uint8_t hdr1;
bool is_bcast; // header pair is 0xD2 0xD3
bool is_bcast;
uint16_t cmd;
uint16_t len;
uint16_t got; // payload bytes received
uint16_t crc_calc; // running CRC over hdr..payload
uint16_t got;
uint16_t crc_calc;
uint16_t crc_recv;
uint8_t payload[BWM_RXFRAME_MAX];
} bwm_parser_t;
static bwm_parser_t s_p = { .state = S_IDLE };
// De-framed payload ring
static uint8_t s_fifo[BWM_DEFIFO_SZ];
static volatile uint16_t s_fifo_head = 0; // write
static volatile uint16_t s_fifo_tail = 0; // read
static volatile uint16_t s_fifo_head = 0;
static volatile uint16_t s_fifo_tail = 0;
static inline uint16_t fifo_count(void) {
return (uint16_t)((s_fifo_head - s_fifo_tail) & (BWM_DEFIFO_SZ - 1));
}
static inline void fifo_push(uint8_t b) {
uint16_t next = (uint16_t)((s_fifo_head + 1) & (BWM_DEFIFO_SZ - 1));
if (next != s_fifo_tail) { // drop on overflow rather than corrupt
if (next != s_fifo_tail) {
s_fifo[s_fifo_head] = b;
s_fifo_head = next;
}
@@ -123,8 +109,6 @@ static void bwm_reset_frame(bwm_parser_t *p) {
p->state = S_IDLE;
}
// Update running CRC one byte at a time (mirrors the streaming update in the
// BWM firmware parser).
static inline void crc_step(bwm_parser_t *p, uint8_t byte) {
p->crc_calc = bwm_crc16(&byte, 1, p->crc_calc);
}
@@ -137,14 +121,12 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) {
} else if (byte == BWM_HDR_SLAVE_RESP_1) {
p->hdr1 = byte; p->is_bcast = false; p->state = S_HDR2;
}
// any other byte: stay idle (resync)
break;
case S_HDR2: {
bool ok = (p->is_bcast && byte == BWM_HDR_SLAVE_BCAST_2) ||
(!p->is_bcast && byte == BWM_HDR_SLAVE_RESP_2);
if (!ok) {
// header mismatch: reset and re-examine this byte as a potential SOF
p->state = S_IDLE;
bwm_feed_byte(p, byte);
return;
@@ -162,7 +144,7 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) {
p->len |= (uint16_t)byte << 8;
crc_step(p, byte);
p->got = 0;
if (p->len > BWM_RXFRAME_MAX) { // oversized -> drop frame
if (p->len > BWM_RXFRAME_MAX) {
bwm_reset_frame(p);
} else {
p->state = (p->len == 0) ? S_CRC_LO : S_PAYLOAD;
@@ -186,7 +168,6 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) {
fifo_push(p->payload[i]);
}
}
// valid non-DATA_FORWARD frames and CRC failures alike: just resync
bwm_reset_frame(p);
break;
@@ -196,7 +177,6 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) {
}
}
// Pull whatever raw framed bytes are waiting and run them through the parser.
static void bwm_pump(void) {
uint8_t scratch[64];
uint16_t avail = bwm_uart_rx_available();
@@ -216,8 +196,6 @@ uint16_t bwm_fwd_rxdata_available(void) {
if (fifo_count() > 0) {
return fifo_count();
}
// No de-framed payload yet, but raw frame bytes may be waiting; pump once so
// receive_ng()'s gate reflects real forward data.
bwm_pump();
return fifo_count();
}
@@ -226,9 +204,6 @@ uint32_t bwm_read_ng(uint8_t *data, size_t len) {
if (len == 0) {
return 0;
}
// Same bounded-retry budget shape as bwm_uart_read(); USART_SLOW_LINK (set
// for the BWM/BLE link) widens it so a slow round-trip doesn't time out.
uint32_t tryconstant = 0;
#ifdef USART_SLOW_LINK
tryconstant = 50000;
+6 -38
View File
@@ -8,61 +8,29 @@
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// Proxmark5 Battery Wireless Module (BWM) transport shim.
//
// The BWM (ESP32-C2, RfidResearchGroup/Proxmark5_BWM_esp32) bridges the AT32
// host <-> BLE/WiFi. Its ESP<->AT32 UART link does NOT carry raw PacketCommandNG;
// it uses a framed "app_com" protocol. Transparent host<->wireless traffic rides
// inside that framing:
//
// AT32 -> ESP (our reply, toward wireless host):
// [0x7C 0xC7] cmd=APP_CMD_SEND_FORWARD_DATA(5000) len(LE) payload CRC16(LE)
// ESP -> AT32 (command from wireless host):
// [0xD2 0xD3] cmd=APP_BROADCAST_DATA_FORWARD(8089) len(LE) payload CRC16(LE)
//
// Frame = HDR1 HDR2 | CMD(LE16) | LEN(LE16) | PAYLOAD[LEN] | CRC(LE16)
// CRC = CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF, MSB-first, no xorout)
// over HDR..PAYLOAD. (NOT compute_crc(CRC_14443_A) - different CRC.)
//
// This shim wraps outgoing NG/OLD reply bytes into a SEND_FORWARD_DATA frame and
// de-frames incoming DATA_FORWARD frames back into a raw NG byte stream, so the
// stock reply_ng()/receive_ng() paths work unchanged over the BWM link.
//
// Enabled by -DWITH_BWM_FORWARD (implies WITH_FPC_USART_HOST).
//-----------------------------------------------------------------------------
#ifndef __BWM_FORWARD_H
#define __BWM_FORWARD_H
#include "common.h"
// app_com framing constants (verified against BWM firmware app_cmd_uart.[ch] /
// app_com_defs.h).
#define BWM_HDR_HOST_CMD_1 0x7C // AT32 -> ESP (host command)
#define BWM_HDR_HOST_CMD_1 0x7C
#define BWM_HDR_HOST_CMD_2 0xC7
#define BWM_HDR_SLAVE_BCAST_1 0xD2 // ESP -> AT32 (slave broadcast)
#define BWM_HDR_SLAVE_BCAST_1 0xD2
#define BWM_HDR_SLAVE_BCAST_2 0xD3
#define BWM_HDR_SLAVE_RESP_1 0x2D // ESP -> AT32 (slave response, skipped here)
#define BWM_HDR_SLAVE_RESP_1 0x2D
#define BWM_HDR_SLAVE_RESP_2 0x3D
#define BWM_CMD_SEND_FORWARD_DATA 5000 // host cmd: payload -> BLE/WiFi endpoint
#define BWM_CMD_DATA_FORWARD 8089 // slave bcast: payload came from endpoint
#define BWM_CMD_SEND_FORWARD_DATA 5000
#define BWM_CMD_DATA_FORWARD 8089
#define BWM_CRC16_POLY 0x1021
#define BWM_CRC16_INIT 0xFFFF
// Wrap `len` raw reply bytes (a whole PacketResponseNG/OLD frame) into one
// SEND_FORWARD_DATA app_com frame and write it synchronously to the FPC USART.
// Returns PM3_SUCCESS or the underlying usart error. Drop-in for the FPC
// usart_writebuffer_sync() call in reply_ng_internal()/reply_old().
int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len);
// De-framed read: returns up to `len` raw NG bytes recovered from inbound
// DATA_FORWARD frames, blocking-with-timeout exactly like usart_read_ng().
// Drop-in for usart_read_ng() as the receive_ng() read callback.
uint32_t bwm_read_ng(uint8_t *data, size_t len);
// >0 when raw bytes are waiting on the FPC USART (gate for receive_ng()).
uint16_t bwm_fwd_rxdata_available(void);
#endif // __BWM_FORWARD_H
#endif
+5 -13
View File
@@ -13,14 +13,13 @@
#include "bwm_uart_at32.h"
#include "pm3_cmd.h" // PM3_SUCCESS
#include "pm3_cmd.h"
#include "at32f435_437.h"
#include "at32f435_437_crm.h"
#include "at32f435_437_gpio.h"
#include "at32f435_437_usart.h"
#include "at32f435_437_misc.h" // nvic_irq_enable
#include "at32f435_437_misc.h"
// PA0 = UART4_TX, PA1 = UART4_RX, alternate function MUX8 (per at32_unit_test.c)
#define BWM_UART UART4
#define BWM_UART_IRQn UART4_IRQn
#define BWM_UART_GPIO GPIOA
@@ -30,12 +29,10 @@
#define BWM_UART_RX_SRC GPIO_PINS_SOURCE1
#define BWM_UART_MUX GPIO_MUX_8
// RX ring buffer (power-of-two so masking wraps). Sized to hold a full command
// burst between main-loop polls.
#define BWM_RX_RING_SZ 4096
static volatile uint8_t s_rx_ring[BWM_RX_RING_SZ];
static volatile uint16_t s_rx_head = 0; // written by ISR
static volatile uint16_t s_rx_tail = 0; // read by main loop
static volatile uint16_t s_rx_head = 0;
static volatile uint16_t s_rx_tail = 0;
static volatile bool s_inited = false;
@@ -66,7 +63,6 @@ void bwm_uart_init(void) {
s_rx_head = 0;
s_rx_tail = 0;
// RX-data-buffer-full interrupt. Priority below USB OTG (which is 0,0).
usart_interrupt_enable(BWM_UART, USART_RDBF_INT, TRUE);
nvic_irq_enable(BWM_UART_IRQn, 2, 0);
@@ -74,13 +70,11 @@ void bwm_uart_init(void) {
s_inited = true;
}
// UART4 global interrupt: drain the RX data register into the ring. Overrun is
// cleared by reading the data register; clear the flag defensively too.
void UART4_IRQHandler(void) {
if (usart_flag_get(BWM_UART, USART_RDBF_FLAG) != RESET) {
uint8_t b = (uint8_t)usart_data_receive(BWM_UART);
uint16_t next = (uint16_t)((s_rx_head + 1) & (BWM_RX_RING_SZ - 1));
if (next != s_rx_tail) { // drop on overflow rather than corrupt
if (next != s_rx_tail) {
s_rx_ring[s_rx_head] = b;
s_rx_head = next;
}
@@ -94,11 +88,9 @@ void UART4_IRQHandler(void) {
int bwm_uart_write(const uint8_t *data, size_t len) {
for (size_t i = 0; i < len; i++) {
while (usart_flag_get(BWM_UART, USART_TDBE_FLAG) == RESET) {
// wait for TX data buffer empty
}
usart_data_transmit(BWM_UART, data[i]);
}
// ensure the last byte has left the shift register
while (usart_flag_get(BWM_UART, USART_TDC_FLAG) == RESET) {
}
return PM3_SUCCESS;
+2 -17
View File
@@ -8,35 +8,20 @@
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// AT32F435 UART4 driver for the Proxmark5 BWM link.
//
// PM5 talks to the ESP32-C2 BWM over UART4 (PA0=TX, PA1=RX, MUX8) at 460800 8N1
// - values taken from the firmware's own at32_unit_test.c. This is the AT32
// equivalent of the AT91 armsrc/usart.c (which does NOT run on this chip).
// IRQ-driven RX into a ring buffer; polled TX. Init runs AFTER usb_enable() so
// a UART fault can never block USB enumeration.
//
// Only compiled/used when -DWITH_BWM_FORWARD (PM5 only).
//-----------------------------------------------------------------------------
#ifndef __BWM_UART_AT32_H
#define __BWM_UART_AT32_H
#include "common.h"
#define BWM_UART_BAUD 460800 // must match BWM UART_BAUD_RATE_DEFAULT
#define BWM_UART_BAUD 460800
// Configure GPIO/clock/UART4 + RX interrupt and enable the port.
// Safe to call once, after usb_enable().
void bwm_uart_init(void);
// Blocking (polled) transmit of len bytes. Returns PM3_SUCCESS.
int bwm_uart_write(const uint8_t *data, size_t len);
// Non-blocking: number of bytes currently buffered from the RX ISR.
uint16_t bwm_uart_rx_available(void);
// Non-blocking: copy up to len buffered RX bytes into data; returns the count.
uint32_t bwm_uart_read(uint8_t *data, size_t len);
#endif // __BWM_UART_AT32_H
#endif