BWM: keep app_com de-framer in armsrc

This commit is contained in:
Will committed 2026-09-26 12:23:47 -04:00
1 parent 8150c2af77
commit d364c8fb5d
6 files changed
+8 -6

No files matched your search

-2
View File
@@ -44,13 +44,11 @@ OBJDIR = obj
INCLUDE = -I../include -I../common_arm -I../common_fpga -I../common -I.
INCLUDE += -I../common_arm/flash_code -I../common_arm/flash_data -I../common_arm/fpga -I../common_arm/gpio
INCLUDE += -I../common_arm/rssi -I../common_arm/rgb -I../common_arm/sys -I../common_arm/ticks -I../common_arm/usb -I../common_arm/wdt
INCLUDE += -I../common_arm/bwm
# Also search prerequisites in the common directory (for usb.c), the fpga directory (for fpga.bit), and the lz4 directory
VPATH = . ../common_arm ../common ../common/crapto1 ../common/mbedtls ../common/lz4 ../fpga ../armsrc/Standalone ../common/hitag2
VPATH += ../common_arm/flash_code ../common_arm/flash_data ../common_arm/fpga ../common_arm/gpio
VPATH += ../common_arm/rssi ../common_arm/rgb ../common_arm/sys ../common_arm/ticks ../common_arm/usb ../common_arm/wdt
VPATH += ../common_arm/bwm
INCLUDES = ../include/proxmark3_arm.h ../include/at91sam7s512.h ../include/config_gpio.h ../include/pm3_cmd.h
-301
View File
@@ -1,301 +0,0 @@
//-----------------------------------------------------------------------------
// 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// BWM app_com de-framer. See bwm_frame.h.
//-----------------------------------------------------------------------------
#include "bwm_frame.h"
// CRC-16/CCITT-FALSE, byte-identical to the BWM firmware's crc16_ccitt()
// (poly 0x1021, init 0xFFFF, MSB-first, no reflection, no xorout).
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;
}
// ---------------------------------------------------------------------------
// 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.
// ---------------------------------------------------------------------------
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_FRAME_RX_MAX];
} bwm_parser_t;
static bwm_parser_t s_p = { .state = S_IDLE };
// De-framed payload ring
static uint8_t s_fifo[BWM_FIFO_SZ];
static volatile uint16_t s_fifo_head = 0; // write
static volatile uint16_t s_fifo_tail = 0; // read
// --- Flow control (ack window) ---------------------------------------------
// s_fwd_inflight: forward frames sent but not yet acked by the ESP. Bumped on
// send, decremented when a SLAVE_RESP echoing cmd=SEND_FORWARD_DATA arrives.
// We may send while it is below BWM_FC_WINDOW; at the cap we wait for an ack.
static volatile int16_t s_fwd_inflight = 0;
// Bootrom write path waits for this; OS only uses the inflight count.
static volatile bool s_fwd_ack = false;
// Set true by the parser when a SLAVE_RESP echoing BWM_CMD_SET_UART_BAUD arrives
// (the ESP's ack for a baud-set request). Consumed by bwm_fwd_negotiate_baud().
static volatile bool s_baud_ack = false;
// Set true by the parser on a SLAVE_RESP for BWM_CMD_GET_UART_BAUD - proof the
// ESP is alive and responding at the baud we just switched to (verify step).
static volatile bool s_getbaud_ack = false;
static uint16_t fifo_count(void) {
return (uint16_t)((s_fifo_head - s_fifo_tail) & (BWM_FIFO_SZ - 1));
}
static void fifo_push(uint8_t b) {
uint16_t next = (uint16_t)((s_fifo_head + 1) & (BWM_FIFO_SZ - 1));
if (next != s_fifo_tail) { // drop on overflow rather than corrupt
s_fifo[s_fifo_head] = b;
s_fifo_head = next;
}
}
static uint8_t fifo_pop(void) {
uint8_t b = s_fifo[s_fifo_tail];
s_fifo_tail = (uint16_t)((s_fifo_tail + 1) & (BWM_FIFO_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 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_FRAME_RX_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) {
if (p->is_bcast && p->cmd == BWM_CMD_DATA_FORWARD) {
for (uint16_t i = 0; i < p->len; i++) {
fifo_push(p->payload[i]);
}
} else if ((p->is_bcast == false) && p->cmd == BWM_CMD_SEND_FORWARD_DATA) {
// SLAVE_RESP ack for a forward frame -> one slot freed
if (s_fwd_inflight > 0) {
s_fwd_inflight--;
}
s_fwd_ack = true;
#ifndef AS_BOOTROM
bwm_fwd_on_frame_ack();
#endif
} else if (p->is_bcast && p->cmd == BWM_CMD_CMD_ERROR && p->len >= 2 &&
(((uint16_t)p->payload[0] | ((uint16_t)p->payload[1] << 8)) == BWM_CMD_SEND_FORWARD_DATA)) {
// the ESP could not deliver a forward frame: this is its answer instead of the ack
#ifndef AS_BOOTROM
bwm_fwd_on_frame_error();
#endif
} else if ((p->is_bcast == false) && p->cmd == BWM_CMD_SET_UART_BAUD) {
// SLAVE_RESP ack for a baud-set request (see negotiate below)
s_baud_ack = true;
} else if ((p->is_bcast == false) && p->cmd == BWM_CMD_GET_UART_BAUD) {
// SLAVE_RESP for our GET_BAUD verify probe
s_getbaud_ack = true;
} else if (p->is_bcast && p->cmd == BWM_CMD_LINK_STATE && p->len >= 2) {
#ifndef AS_BOOTROM
bwm_fwd_on_link_state(p->payload[0], p->payload[1]);
#endif
}
}
// valid non-DATA_FORWARD frames and CRC failures alike: just resync
bwm_reset_frame(p);
break;
default:
bwm_reset_frame(p);
break;
}
}
void bwm_frame_feed(uint8_t byte) {
bwm_feed_byte(&s_p, byte);
}
uint16_t bwm_fifo_count(void) {
return fifo_count();
}
uint8_t bwm_fifo_pop(void) {
return fifo_pop();
}
int16_t bwm_frame_inflight(void) {
return s_fwd_inflight;
}
void bwm_frame_inflight_inc(void) {
s_fwd_inflight++;
}
void bwm_frame_inflight_zero(void) {
s_fwd_inflight = 0;
}
bool bwm_frame_take_fwd_ack(void) {
if (s_fwd_ack == false) {
return false;
}
s_fwd_ack = false;
return true;
}
bool bwm_frame_baud_ack(void) {
return s_baud_ack;
}
void bwm_frame_clear_baud_ack(void) {
s_baud_ack = false;
s_p.state = S_IDLE; // drop any half-frame before we listen
}
bool bwm_frame_getbaud_ack(void) {
return s_getbaud_ack;
}
void bwm_frame_clear_getbaud_ack(void) {
s_getbaud_ack = false;
s_p.state = S_IDLE;
}
void bwm_frame_reset(void) {
s_p.state = S_IDLE;
s_fifo_head = 0;
s_fifo_tail = 0;
s_fwd_inflight = 0;
s_fwd_ack = false;
s_baud_ack = false;
s_getbaud_ack = false;
}
size_t bwm_frame_build(uint16_t cmd, const uint8_t *payload, uint16_t len,
uint8_t *out, size_t outsz) {
size_t idx = 0;
if (out == NULL || outsz < (size_t)(8 + len)) {
return 0;
}
out[idx++] = BWM_HDR_HOST_CMD_1;
out[idx++] = BWM_HDR_HOST_CMD_2;
out[idx++] = (uint8_t)(cmd & 0xFF);
out[idx++] = (uint8_t)((cmd >> 8) & 0xFF);
out[idx++] = (uint8_t)(len & 0xFF);
out[idx++] = (uint8_t)((len >> 8) & 0xFF);
if (len && payload) {
for (uint16_t i = 0; i < len; i++) {
out[idx++] = payload[i];
}
}
uint16_t crc = bwm_crc16(out, idx, BWM_CRC16_INIT);
out[idx++] = (uint8_t)(crc & 0xFF);
out[idx++] = (uint8_t)((crc >> 8) & 0xFF);
return idx;
}
-103
View File
@@ -1,103 +0,0 @@
//-----------------------------------------------------------------------------
// 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See LICENSE.txt for the text of the license.
//-----------------------------------------------------------------------------
// BWM app_com de-framer shared by the PM5 OS and bootrom.
//
// 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.)
//-----------------------------------------------------------------------------
#ifndef BWM_FRAME_H
#define BWM_FRAME_H
#include "common.h"
#include "pm3_cmd.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; forward-frame ack)
#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_CMD_ERROR 8091 // slave bcast: command error report
#define BWM_CMD_LINK_STATE 8092 // slave bcast: [ble u8][wifi u8], 1 = a client is connected
// System command: set the ESP<->AT32 UART baud (app_com_defs.h, enum @1000).
// SET is a HOST_CMD carrying u32 LE baud; the ESP replies with a SLAVE_RESP
// echoing this cmd (len 0) at the OLD baud, then commits to the new baud.
#define BWM_CMD_SET_UART_BAUD 1011
#define BWM_CMD_GET_UART_BAUD 1009 // read back the ESP's live baud (negotiation verify)
#define BWM_CRC16_POLY 0x1021
#define BWM_CRC16_INIT 0xFFFF
#ifdef AS_BOOTROM
#ifndef BWM_FRAME_RX_MAX
#define BWM_FRAME_RX_MAX 576
#endif
#ifndef BWM_FIFO_SZ
#define BWM_FIFO_SZ 1024
#endif
#else
#ifndef BWM_FRAME_RX_MAX
#define BWM_FRAME_RX_MAX (PM3_CMD_DATA_SIZE + 64) // NG/OLD frame ceiling
#endif
#ifndef BWM_FIFO_SZ
#define BWM_FIFO_SZ 2048 // >= one full NG frame's payload
#endif
#endif
uint16_t bwm_crc16(const uint8_t *data, size_t len, uint16_t crc);
void bwm_frame_reset(void);
void bwm_frame_feed(uint8_t byte);
uint16_t bwm_fifo_count(void);
uint8_t bwm_fifo_pop(void);
int16_t bwm_frame_inflight(void);
void bwm_frame_inflight_inc(void);
void bwm_frame_inflight_zero(void);
bool bwm_frame_take_fwd_ack(void);
bool bwm_frame_baud_ack(void);
void bwm_frame_clear_baud_ack(void);
bool bwm_frame_getbaud_ack(void);
void bwm_frame_clear_getbaud_ack(void);
size_t bwm_frame_build(uint16_t cmd, const uint8_t *payload, uint16_t len,
uint8_t *out, size_t outsz);
#ifndef AS_BOOTROM
void bwm_fwd_on_frame_ack(void);
void bwm_fwd_on_frame_error(void);
void bwm_fwd_on_link_state(uint8_t ble, uint8_t wifi);
#endif
#endif