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 48458b6ba7
commit 3553836b7c
4 files changed
+487

No files matched your search

+259
View File
@@ -0,0 +1,259 @@
//-----------------------------------------------------------------------------
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// Proxmark5 Battery Wireless Module (BWM) transport shim - see bwm_forward.h.
//-----------------------------------------------------------------------------
#include "bwm_forward.h"
#include "bwm_uart_at32.h"
#include "pm3_cmd.h" // PM3_CMD_DATA_SIZE, PM3_* return codes
#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;
for (uint8_t b = 0; b < 8; b++) {
if (crc & 0x8000) {
crc = (uint16_t)((crc << 1) ^ BWM_CRC16_POLY);
} else {
crc = (uint16_t)(crc << 1);
}
}
}
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_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
if (len > BWM_TX_MAX_PAYLOAD) {
len = BWM_TX_MAX_PAYLOAD; // defensive; should never trigger
}
size_t idx = 0;
frame[idx++] = BWM_HDR_HOST_CMD_1;
frame[idx++] = BWM_HDR_HOST_CMD_2;
frame[idx++] = (uint8_t)(BWM_CMD_SEND_FORWARD_DATA & 0xFF);
frame[idx++] = (uint8_t)((BWM_CMD_SEND_FORWARD_DATA >> 8) & 0xFF);
frame[idx++] = (uint8_t)(len & 0xFF);
frame[idx++] = (uint8_t)((len >> 8) & 0xFF);
if (len) {
memcpy(&frame[idx], data, len);
idx += len;
}
uint16_t crc = bwm_crc16(frame, idx, BWM_CRC16_INIT);
frame[idx++] = (uint8_t)(crc & 0xFF);
frame[idx++] = (uint8_t)((crc >> 8) & 0xFF);
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_RXFRAME_MAX (PM3_CMD_DATA_SIZE + 64)
typedef enum {
S_IDLE = 0, S_HDR2, S_CMD_LO, S_CMD_HI, S_LEN_LO, S_LEN_HI, S_PAYLOAD, S_CRC_LO, S_CRC_HI
} bwm_state_t;
typedef struct {
bwm_state_t state;
uint8_t hdr1;
bool is_bcast; // header pair is 0xD2 0xD3
uint16_t cmd;
uint16_t len;
uint16_t got; // payload bytes received
uint16_t crc_calc; // running CRC over hdr..payload
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 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
s_fifo[s_fifo_head] = b;
s_fifo_head = next;
}
}
static inline uint8_t fifo_pop(void) {
uint8_t b = s_fifo[s_fifo_tail];
s_fifo_tail = (uint16_t)((s_fifo_tail + 1) & (BWM_DEFIFO_SZ - 1));
return b;
}
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);
}
static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) {
switch (p->state) {
case S_IDLE:
if (byte == BWM_HDR_SLAVE_BCAST_1) {
p->hdr1 = byte; p->is_bcast = true; p->state = S_HDR2;
} 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;
}
uint8_t hdr[2] = { p->hdr1, byte };
p->crc_calc = bwm_crc16(hdr, 2, BWM_CRC16_INIT);
p->state = S_CMD_LO;
break;
}
case S_CMD_LO: p->cmd = byte; crc_step(p, byte); p->state = S_CMD_HI; break;
case S_CMD_HI: p->cmd |= (uint16_t)byte << 8; crc_step(p, byte); p->state = S_LEN_LO; break;
case S_LEN_LO: p->len = byte; crc_step(p, byte); p->state = S_LEN_HI; break;
case S_LEN_HI:
p->len |= (uint16_t)byte << 8;
crc_step(p, byte);
p->got = 0;
if (p->len > BWM_RXFRAME_MAX) { // oversized -> drop frame
bwm_reset_frame(p);
} else {
p->state = (p->len == 0) ? S_CRC_LO : S_PAYLOAD;
}
break;
case S_PAYLOAD:
p->payload[p->got++] = byte;
crc_step(p, byte);
if (p->got >= p->len) {
p->state = S_CRC_LO;
}
break;
case S_CRC_LO: p->crc_recv = byte; p->state = S_CRC_HI; break;
case S_CRC_HI:
p->crc_recv |= (uint16_t)byte << 8;
if (p->crc_recv == p->crc_calc &&
p->is_bcast && p->cmd == BWM_CMD_DATA_FORWARD) {
for (uint16_t i = 0; i < p->len; i++) {
fifo_push(p->payload[i]);
}
}
// valid non-DATA_FORWARD frames and CRC failures alike: just resync
bwm_reset_frame(p);
break;
default:
bwm_reset_frame(p);
break;
}
}
// 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();
while (avail) {
uint32_t n = bwm_uart_read(scratch, MIN((uint32_t)avail, (uint32_t)sizeof(scratch)));
if (n == 0) {
break;
}
for (uint32_t i = 0; i < n; i++) {
bwm_feed_byte(&s_p, scratch[i]);
}
avail = bwm_uart_rx_available();
}
}
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();
}
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;
#endif
uint32_t maxtry = 10 * (3000000 / BWM_UART_BAUD) + tryconstant;
uint32_t out = 0;
uint32_t try = 0;
while (out < len) {
while (out < len && fifo_count() > 0) {
data[out++] = fifo_pop();
try = 0;
}
if (out >= len) {
break;
}
uint16_t before = fifo_count();
bwm_pump();
if (fifo_count() != before) {
try = 0;
continue;
}
if (try++ >= maxtry) {
break;
}
}
return out;
}
+68
View File
@@ -0,0 +1,68 @@
//-----------------------------------------------------------------------------
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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_2 0xC7
#define BWM_HDR_SLAVE_BCAST_1 0xD2 // ESP -> AT32 (slave broadcast)
#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_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_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
+118
View File
@@ -0,0 +1,118 @@
//-----------------------------------------------------------------------------
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// AT32F435 UART4 driver for the Proxmark5 BWM link - see bwm_uart_at32.h.
//-----------------------------------------------------------------------------
#include "bwm_uart_at32.h"
#include "pm3_cmd.h" // PM3_SUCCESS
#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
// 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
#define BWM_UART_TX_PIN GPIO_PINS_0
#define BWM_UART_RX_PIN GPIO_PINS_1
#define BWM_UART_TX_SRC GPIO_PINS_SOURCE0
#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 bool s_inited = false;
void bwm_uart_init(void) {
if (s_inited) {
return;
}
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_UART4_PERIPH_CLOCK, TRUE);
gpio_init_type gpio_init_struct;
gpio_default_para_init(&gpio_init_struct);
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_pins = BWM_UART_TX_PIN | BWM_UART_RX_PIN;
gpio_init(BWM_UART_GPIO, &gpio_init_struct);
gpio_pin_mux_config(BWM_UART_GPIO, BWM_UART_TX_SRC, BWM_UART_MUX);
gpio_pin_mux_config(BWM_UART_GPIO, BWM_UART_RX_SRC, BWM_UART_MUX);
usart_init(BWM_UART, BWM_UART_BAUD, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_parity_selection_config(BWM_UART, USART_PARITY_NONE);
usart_transmitter_enable(BWM_UART, TRUE);
usart_receiver_enable(BWM_UART, TRUE);
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);
usart_enable(BWM_UART, TRUE);
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
s_rx_ring[s_rx_head] = b;
s_rx_head = next;
}
}
if (usart_flag_get(BWM_UART, USART_ROERR_FLAG) != RESET) {
usart_flag_clear(BWM_UART, USART_ROERR_FLAG);
(void)usart_data_receive(BWM_UART);
}
}
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;
}
uint16_t bwm_uart_rx_available(void) {
return (uint16_t)((s_rx_head - s_rx_tail) & (BWM_RX_RING_SZ - 1));
}
uint32_t bwm_uart_read(uint8_t *data, size_t len) {
uint32_t n = 0;
while (n < len && s_rx_tail != s_rx_head) {
data[n++] = s_rx_ring[s_rx_tail];
s_rx_tail = (uint16_t)((s_rx_tail + 1) & (BWM_RX_RING_SZ - 1));
}
return n;
}
+42
View File
@@ -0,0 +1,42 @@
//-----------------------------------------------------------------------------
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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
// 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