diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index 8a941811f..1b786630a 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -175,36 +175,66 @@ int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len, uint } void HfPlotDownload(void) { + // Two chunk buffers laid back to back in the ToSend buffer. Each one is a + // download_chunk_t header followed by the samples the DMA writes straight + // into chunk->data, so a filled buffer is already a complete NG payload and + // can be handed to reply_ng without a copy. + // get_tosend() has to come after FpgaDownloadAndGo(): the FPGA loader frees + // BigBuf and reuses this region for its decompression ring buffer. + + FpgaDownloadAndGo(FPGA_BITSTREAM_HF); + + const size_t stride = sizeof(download_chunk_t) + DOWNLOAD_CHUNK_MAX; tosend_t *ts = get_tosend(); - uint8_t *this_buf = ts->buf; + if (ts->buf == NULL || (2 * stride) > TOSEND_BUFFER_SIZE) { + reply_ng(CMD_FPGAMEM_DOWNLOAD, PM3_EMALLOC, NULL, 0); + return; + } - FpgaDownloadAndGo(FPGA_BITSTREAM_HF); + download_chunk_t *chunk[2] = { + (download_chunk_t *)ts->buf, + (download_chunk_t *)(ts->buf + stride) + }; + uint8_t idx = 0; FpgaSetupSsc(FPGA_MAJOR_MODE_HF_GET_TRACE); - FpgaSetupSscRxDmaSingle(this_buf, PM3_CMD_DATA_SIZE); + // Arm each transfer for exactly what is still coming. + // FPGA_TRACE_SIZE is not a multiple of DOWNLOAD_CHUNK_MAX, + // and the FPGA stops after its Block-RAM is out + FpgaSetupSscRxDmaSingle(chunk[idx]->data, MIN(FPGA_TRACE_SIZE, DOWNLOAD_CHUNK_MAX)); - FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_GET_TRACE); // let FPGA transfer its internal Block-RAM + FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_GET_TRACE); LED_B_ON(); - for (size_t i = 0; i < FPGA_TRACE_SIZE; i += PM3_CMD_DATA_SIZE) { - size_t len = MIN(FPGA_TRACE_SIZE - i, PM3_CMD_DATA_SIZE); - // prepare next DMA transfer: - uint8_t *next_buf = ts->buf + ((i + PM3_CMD_DATA_SIZE) % (2 * PM3_CMD_DATA_SIZE)); + for (size_t i = 0; i < FPGA_TRACE_SIZE; i += DOWNLOAD_CHUNK_MAX) { - while (!FPGA_SSC_DMA_RX_Done()) {}; // wait for DMA transfer to complete + size_t len = MIN(FPGA_TRACE_SIZE - i, DOWNLOAD_CHUNK_MAX); + download_chunk_t *this_chunk = chunk[idx]; + idx ^= 1; - // The main buf has stopped receiving, so it needs to be refreshed. - FPGA_SSC_DMA_RX_Refresh_Single(next_buf, PM3_CMD_DATA_SIZE); + while (!FPGA_SSC_DMA_RX_Done()) {}; - reply_old(CMD_FPGAMEM_DOWNLOADED, i, len, FPGA_TRACE_SIZE, this_buf, len); - this_buf = next_buf; + // The main buf has stopped receiving, so arm the other one first + size_t next = i + DOWNLOAD_CHUNK_MAX; + if (next < FPGA_TRACE_SIZE) { + FPGA_SSC_DMA_RX_Refresh_Single( + chunk[idx]->data, + MIN(FPGA_TRACE_SIZE - next, DOWNLOAD_CHUNK_MAX) + ); + } + + this_chunk->offset = i; + reply_ng(CMD_FPGAMEM_DOWNLOADED, PM3_SUCCESS, (uint8_t *)this_chunk, sizeof(download_chunk_t) + len); } FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); - // Trigger a finish downloading signal with an ACK frame - reply_ng(CMD_FPGAMEM_DOWNLOAD, PM3_SUCCESS, NULL, 0); + download_done_t done = { + .bytes_sent = FPGA_TRACE_SIZE, + .extra = 0, + }; + reply_ng(CMD_FPGAMEM_DOWNLOAD, PM3_SUCCESS, (uint8_t *)&done, sizeof(done)); LED_B_OFF(); } diff --git a/doc/new_frame_format.md b/doc/new_frame_format.md index eac140f25..285aef541 100644 --- a/doc/new_frame_format.md +++ b/doc/new_frame_format.md @@ -43,6 +43,8 @@ Previously, frames were, in both directions like this: } d; with PM3_CMD_DATA_SIZE = 512 and there was no API abstraction, everybody was forging/parsing these frames. +These two structs now use `PM3_CMD_DATA_SIZE_OLD`, pinned at 512 independently of `PM3_CMD_DATA_SIZE`: +the bootloader only speaks OLD, so growing them would break flashing against every deployed bootrom. So the frame size was fixed, 544 bytes, even for simple ACKs. When snooping the USB transfers, we can observe the host is sending 544b Bulk USB frames while the Proxmark3 is limited by its internal buffers and is sending 128b, 128b, 128b, 128b, 32b, so in total 5 packets. @@ -108,17 +110,29 @@ But they are abstracted from the developer view with a new API. See below. **Status: the C client and the ARM firmware are converted.** The client has no `SendCommandOLD` or `SendCommandMIX` call sites left. On the -device only four legacy replies remain, and all four are deliberate: +device only three legacy replies remain, and all three are deliberate: | Site | Why it stays | |---|---| | `armsrc/appmain.c` — `CMD_READ_MEM_DOWNLOADED` and its `CMD_ACK` terminator | also served by `bootrom.c`, which only speaks OLD | | `armsrc/appmain.c` — `CMD_DEVICE_INFO` | same, see `bootrom.c` | -| `armsrc/hfsnoop.c` — `CMD_FPGAMEM_DOWNLOADED` | the FPGA trace loop is a DMA double-buffer sized to `PM3_CMD_DATA_SIZE`; an NG header does not fit alongside a full 512 byte DMA buffer, so converting it means reworking the DMA buffering | `SendCommandBL` marks the frames that must stay OLD because the bootloader serves them. Do not "convert" those. +These sites chunk by `PM3_CMD_DATA_SIZE_OLD`, not `PM3_CMD_DATA_SIZE`: `reply_old` +clamps its payload to the OLD size, so a sender chunking by the NG size would build +oversized chunks, have them truncated on the wire, and still announce the full length +in `oldarg[1]`. + +`armsrc/hfsnoop.c` — `CMD_FPGAMEM_DOWNLOADED` used to be on this list because its +FPGA trace loop is a DMA double-buffer and an NG header did not obviously fit +alongside a full DMA buffer. It is converted: the DMA writes straight into +`chunk->data` of a `download_chunk_t`, so a filled buffer is already a complete NG +payload. Note the loop arms each transfer for exactly the bytes still expected - +`FPGA_TRACE_SIZE` is not a multiple of `DOWNLOAD_CHUNK_MAX`, and arming a full +chunk for the short last one waits forever on bytes the FPGA never sends. + `PacketResponseNG.oldarg[]` is likewise almost gone on the client. The only readers left are `client/src/flash.c` and `client/src/proxmark3.c`, both talking to the bootrom over `SendCommandBL`. The Lua binding no longer serialises the