From 4d7d49e089b21a4cc1c736c7ed33cac79bf111d8 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 12 Sep 2026 18:49:24 +0200 Subject: [PATCH] stop an FPGA bitstream download from eating the emulator memory FpgaDownloadAndGoEx() with keep_em false does BigBuf_free() plus BigBuf_Clear_ext(): the emulator memory pointer is nulled and all of BigBuf is zeroed. iso14443a_setup() calls that variant, and Mifare1ksim(), SimulateIso14443aTagEx() and SimulateIso14443aTagAID() build their precompiled anticollision responses in BigBuf first. CMD_HF_MIFARE_EML_MEMGET had it the other way round - it downloaded, then read the memory it had just wiped, so 'hf mf eview' and 'hf mf esave' returned their own zeros after any lf hitag, hf iclass or hf 15 command. A download already cached early-returns, so which image the FPGA held decided whether any of this happened. All five now call FpgaDownloadAndGo_keep_EM() before any BigBuf allocation. Free-BigBuf floor at download time goes 16384 -> 20480 (ring plus the 4096 byte emulator block); BigBuf measures 29084..31420 across all standalone configs on RDV4 and 40120 on a PM3 Easy. The sim paths also clear the trace before taking their modulation buffer - BigBuf_malloc() refuses memory a stale trace holds, and MifareSimInit() ignored the NULL, leaving prepare_tag_modulation() to memcpy 571 bytes over the vector table at address 0. 'hf mf eload' now zeroes emulator memory device side: CMD_HF_MIFARE_EML_MEMSET takes a flags byte, set on the first chunk only, so 'hf mf esetblk' and every other partial write still touch only their own blocks. That byte is why CAPABILITIES_VERSION goes 9 -> 10; mismatched client and firmware refuse to connect rather than write everything one byte offset. Reported in #2836, whose USB drop is separately addressed by bc289cf43. Builds clean for PM3RDV4, PM5 and the client; astyle clean. Not yet verified on hardware. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 5 ++++- armsrc/appmain.c | 23 +++++++++++++++++++++-- armsrc/iso14443a.c | 26 ++++++++++++++++++++++++-- armsrc/mifaresim.c | 17 +++++++++++++++-- client/src/cmdhfjooki.c | 2 +- client/src/cmdhfmf.c | 8 ++++++-- client/src/cmdhfmfu.c | 2 +- client/src/cmdhfst25ta.c | 2 +- client/src/mifare/mifarehost.c | 6 ++++-- client/src/mifare/mifarehost.h | 2 +- include/pm3_cmd.h | 5 ++++- 11 files changed, 82 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 074cd9b2c..44261caae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,10 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] -- - Changed `mem spiffs upload` - the file is held open across packets instead of being reopened by name for each one (@iceman1001) +- Changed `hf mf eload` - the device now zeroes the emulator memory before the upload (@iceman1001) +- Fixed `hf mf sim` / `hf 14a sim` - the anticollision responses were prepared in BigBuf before `iso14443a_setup()` (@iceman1001) +- Changed `CAPABILITIES_VERSION` to 10 - `hf mf eload` sends a flags byte the device did not have before, so client and firmware must be flashed as a pair (@iceman1001) +- Changed `mem spiffs upload` - the file is held open across packets instead of being reopened by name for each one (@iceman1001) - Added `hf thinfilm sniff` - passively records the frames a Thinfilm / NFC Barcode tag beams at a reader (@iceman1001) - Fixed `hf thinfilm sim` - it now holds the genuine tag's measured 65536 carrier period frame rate instead of a fixed delay (@iceman1001) - Changed `mem spiffs upload`/`mem spiffs dump` - both now print inline progress, and a refused write is reported with the byte it stopped at instead of claiming success (@iceman1001) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 849eb62f7..27dc36962 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -2548,13 +2548,21 @@ static void PacketReceived(PacketCommandNG *packet) { break; } case CMD_HF_MIFARE_EML_MEMSET: { - FpgaDownloadAndGo(FPGA_BITSTREAM_HF); + // keep the Emulator Memory, a partial update must not wipe the rest of it + FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF); struct p { uint16_t blockno; uint8_t blockcnt; uint8_t blockwidth; + uint8_t flags; uint8_t data[]; } PACKED; + + if (packet->length < sizeof(struct p)) { + reply_ng(CMD_HF_MIFARE_EML_MEMSET, PM3_EINVARG, NULL, 0); + break; + } + struct p *payload = (struct p *) packet->data.asBytes; // backwards compat... default bytewidth @@ -2562,12 +2570,19 @@ static void PacketReceived(PacketCommandNG *packet) { payload->blockwidth = MIFARE_BLOCK_SIZE; } + // a whole-dump upload zeroes what was there first, so a smaller dump can't + // leave a tail behind that reads back as card data. esetblk never sets this. + if (payload->flags & MFEMUL_MEMSET_CLEAR) { + BigBuf_Clear_EM(); + } + emlSetMem_xt(payload->data, payload->blockno, payload->blockcnt, payload->blockwidth); break; } case CMD_HF_MIFARE_EML_MEMGET: { - FpgaDownloadAndGo(FPGA_BITSTREAM_HF); + // keep the Emulator Memory, we are about to read it back + FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF); struct p { uint16_t blockno; uint8_t blockcnt; @@ -3564,6 +3579,10 @@ static void PacketReceived(PacketCommandNG *packet) { break; } + // a dump smaller than the emulator memory must not leave the tail of a + // previous one behind + BigBuf_Clear_EM(); + rdv40_spiffs_read_as_filetype(fn, em, size, RDV40_SPIFFS_SAFETY_SAFE); reply_ng(CMD_SPIFFS_ELOAD, PM3_SUCCESS, NULL, 0); LED_B_OFF(); diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index f3e1c0fa8..5c94e8e5b 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -1776,9 +1776,21 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u uint8_t receivedCmd[MAX_FRAME_SIZE] = { 0x00 }; uint8_t receivedCmdPar[MAX_PARITY_SIZE] = { 0x00 }; + //----------------------------------------------------------------------------- + // Note: we call FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF) here although the + // FPGA isn't needed yet. iso14443a_setup() below does it otherwise, and a + // bitstream download frees and clears BigBuf - which by then holds the + // Emulator Memory and the precompiled anticollision responses. + //----------------------------------------------------------------------------- + FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF); + // free eventually allocated BigBuf memory but keep Emulator Memory BigBuf_free_keep_EM(); + // clear trace before allocating. The response buffers come out of the same + // pool and BigBuf_malloc() won't hand out memory the trace still occupies. + clear_trace(); + // Allocate 512 bytes for the dynamic modulation, created when the reader queries for it // Such a response is less time critical, so we can prepare them on the fly #define DYNAMIC_RESPONSE_BUFFER_SIZE 64 @@ -1880,7 +1892,6 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u bool odd_reply = true; - clear_trace(); set_tracing(true); LED_A_ON(); @@ -4782,9 +4793,21 @@ void SimulateIso14443aTagAID(uint8_t tagType, uint16_t flags, uint8_t *uid, return; } + //----------------------------------------------------------------------------- + // Note: we call FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF) here although the + // FPGA isn't needed yet. iso14443a_setup() below does it otherwise, and a + // bitstream download frees and clears BigBuf - which by then holds the + // Emulator Memory and the precompiled anticollision responses. + //----------------------------------------------------------------------------- + FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF); + // free eventually allocated BigBuf memory but keep Emulator Memory BigBuf_free_keep_EM(); + // clear trace before allocating. The response buffers come out of the same + // pool and BigBuf_malloc() won't hand out memory the trace still occupies. + clear_trace(); + // Response payloads must fit within the dynamic response buffer, // accounting for 1-byte IBlock header + 1-byte optional CID (offset 0 or 1) if (selectaid_response_len + 2 > DYNAMIC_RESPONSE_BUFFER2_SIZE || @@ -4830,7 +4853,6 @@ void SimulateIso14443aTagAID(uint8_t tagType, uint16_t flags, uint8_t *uid, int sentCount = 0; bool odd_reply = true; - clear_trace(); set_tracing(true); LED_A_ON(); diff --git a/armsrc/mifaresim.c b/armsrc/mifaresim.c index 29b69689a..88bc14529 100644 --- a/armsrc/mifaresim.c +++ b/armsrc/mifaresim.c @@ -33,6 +33,7 @@ #include "iso14443a.h" #include "BigBuf.h" +#include "fpga_loader.h" #include "string.h" #include "mifareutil.h" #include "fpga_apis.h" @@ -460,6 +461,11 @@ bool MifareSimInit(uint16_t flags, uint8_t *uid, uint16_t atqa, uint8_t sak, tag #define ALLOCATED_TAG_MODULATION_BUFFER_SIZE 571 uint8_t *free_buffer = BigBuf_calloc(ALLOCATED_TAG_MODULATION_BUFFER_SIZE); + if (free_buffer == NULL) { + Dbprintf("ERROR: " _RED_("Failed to allocate modulation buffer")); + return false; + } + // modulation buffer pointer and current buffer free space size uint8_t *free_buffer_pointer = free_buffer; size_t free_buffer_size = ALLOCATED_TAG_MODULATION_BUFFER_SIZE; @@ -541,8 +547,17 @@ void Mifare1ksim(uint16_t flags, uint8_t exitAfterNReads, uint8_t *uid, uint16_t const tUart14a *uart = GetUart14a(); + //----------------------------------------------------------------------------- + // Note: we call FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF) here although the + // FPGA isn't needed yet. iso14443a_setup() below does it otherwise, and a + // bitstream download frees and clears BigBuf - which by then holds the + // Emulator Memory and the precompiled anticollision responses. + //----------------------------------------------------------------------------- + FpgaDownloadAndGo_keep_EM(FPGA_BITSTREAM_HF); + // free eventually allocated BigBuf memory but keep Emulator Memory BigBuf_free_keep_EM(); + clear_trace(); if (MifareSimInit(flags, uid, atqa, sak, &responses, &cuid, &uid_len, &rats, &rats_len) == false) { BigBuf_free_keep_EM(); @@ -552,8 +567,6 @@ void Mifare1ksim(uint16_t flags, uint8_t exitAfterNReads, uint8_t *uid, uint16_t // We need to listen to the high-frequency, peak-detected path. iso14443a_setup(FPGA_HF_ISO14443A_TAGSIM_LISTEN); - // clear trace - clear_trace(); set_tracing(true); LED_D_ON(); ResetSspClk(); diff --git a/client/src/cmdhfjooki.c b/client/src/cmdhfjooki.c index 1956c606f..56fe623b9 100644 --- a/client/src/cmdhfjooki.c +++ b/client/src/cmdhfjooki.c @@ -557,7 +557,7 @@ static int CmdHF14AJookiSim(const char *Cmd) { uint16_t chunk_size = MIN(max_avail_blocks, datalen); uint16_t blocks_to_send = chunk_size / blockwidth; - if (mf_eml_set_mem_xt(data + counter, blockno, blocks_to_send, blockwidth) != PM3_SUCCESS) { + if (mf_eml_set_mem_xt(data + counter, blockno, blocks_to_send, blockwidth, 0) != PM3_SUCCESS) { PrintAndLogEx(FAILED, "Cant set emul block: %3d", blockno); free(data); return PM3_ESOFT; diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index c59cfc144..a72a6b5c6 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -5448,7 +5448,11 @@ int CmdHF14AMfELoad(const char *Cmd) { uint16_t chunk_size = MIN(max_avail_blocks, bytes_read); uint16_t blocks_to_send = chunk_size / block_width; - if (mf_eml_set_mem_xt(data + offset, cnt, blocks_to_send, block_width) != PM3_SUCCESS) { + // the first chunk zeroes the emulator memory device side, so a dump smaller + // than the previous one can't leave a tail behind that reads back as card data + uint8_t flags = (cnt == 0) ? MFEMUL_MEMSET_CLEAR : 0; + + if (mf_eml_set_mem_xt(data + offset, cnt, blocks_to_send, block_width, flags) != PM3_SUCCESS) { PrintAndLogEx(FAILED, "Can't set emulator mem at block: %3d", cnt); free(data); return PM3_ESOFT; @@ -9423,7 +9427,7 @@ static int CmdHF14AGen4Save(const char *Cmd) { uint16_t chunk_size = MIN(max_avail_blocks, bytes_left); uint16_t blocks_to_send = chunk_size / MFBLOCK_SIZE; - if (mf_eml_set_mem_xt(dump + offset, cnt, blocks_to_send, MFBLOCK_SIZE) != PM3_SUCCESS) { + if (mf_eml_set_mem_xt(dump + offset, cnt, blocks_to_send, MFBLOCK_SIZE, 0) != PM3_SUCCESS) { PrintAndLogEx(FAILED, "Can't set emulator mem at block: %3d", cnt); free(dump); return PM3_ESOFT; diff --git a/client/src/cmdhfmfu.c b/client/src/cmdhfmfu.c index 5ad11c440..e9b5c3fe8 100644 --- a/client/src/cmdhfmfu.c +++ b/client/src/cmdhfmfu.c @@ -9059,7 +9059,7 @@ static int CmdHF14AMfUeSetBlk(const char *Cmd) { // MFU emulator page data starts after the 56-byte mfu_dump_t prefix, so shift the // page index by MFU_DUMP_PREFIX_LENGTH/MFU_BLOCK_SIZE (=14), width = MFU_BLOCK_SIZE (4). - res = mf_eml_set_mem_xt(data, blk + (MFU_DUMP_PREFIX_LENGTH / MFU_BLOCK_SIZE), count, MFU_BLOCK_SIZE); + res = mf_eml_set_mem_xt(data, blk + (MFU_DUMP_PREFIX_LENGTH / MFU_BLOCK_SIZE), count, MFU_BLOCK_SIZE, 0); if (res != PM3_SUCCESS) { PrintAndLogEx(FAILED, "Failed to set emulator memory"); return res; diff --git a/client/src/cmdhfst25ta.c b/client/src/cmdhfst25ta.c index a34d8d086..0f299d5d3 100644 --- a/client/src/cmdhfst25ta.c +++ b/client/src/cmdhfst25ta.c @@ -462,7 +462,7 @@ static int st25ta_upload_sim_data(const uint8_t *uid, uint8_t uid_len, const uin for (uint16_t offset = 0; offset < data_len;) { uint8_t chunk_len = MIN(data_len - offset, UINT8_MAX); - int res = mf_eml_set_mem_xt(data + offset, offset, chunk_len, 1); + int res = mf_eml_set_mem_xt(data + offset, offset, chunk_len, 1, 0); if (res != PM3_SUCCESS) { return res; } diff --git a/client/src/mifare/mifarehost.c b/client/src/mifare/mifarehost.c index d06a666dd..99a369c81 100644 --- a/client/src/mifare/mifarehost.c +++ b/client/src/mifare/mifarehost.c @@ -1152,15 +1152,16 @@ int mf_eml_get_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtW } int mf_elm_set_mem(uint8_t *data, int blockNum, int blocksCount) { - return mf_eml_set_mem_xt(data, blockNum, blocksCount, MFBLOCK_SIZE); + return mf_eml_set_mem_xt(data, blockNum, blocksCount, MFBLOCK_SIZE, 0); } -int mf_eml_set_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) { +int mf_eml_set_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth, uint8_t flags) { struct p { uint16_t blockno; uint8_t blockcnt; uint8_t blockwidth; + uint8_t flags; uint8_t data[]; } PACKED; @@ -1179,6 +1180,7 @@ int mf_eml_set_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtW payload->blockno = blockNum; payload->blockcnt = blocksCount; payload->blockwidth = blockBtWidth; + payload->flags = flags; memcpy(payload->data, data, size); clearCommandBuffer(); diff --git a/client/src/mifare/mifarehost.h b/client/src/mifare/mifarehost.h index 3a801769f..91ad5db07 100644 --- a/client/src/mifare/mifarehost.h +++ b/client/src/mifare/mifarehost.h @@ -94,7 +94,7 @@ int mf_write_sector(uint8_t sectorNo, uint8_t keyType, const uint8_t *key, uint8 int mf_eml_get_mem(uint8_t *data, int blockNum, int blocksCount); int mf_eml_get_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth); int mf_elm_set_mem(uint8_t *data, int blockNum, int blocksCount); -int mf_eml_set_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth); +int mf_eml_set_mem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth, uint8_t flags); int mf_chinese_set_uid(uint8_t *uid, uint8_t uidlen, const uint8_t *atqa, const uint8_t *sak, uint8_t *old_uid, uint8_t *verifed_uid, uint8_t wipecard, uint8_t gdm); int mf_chinese_wipe(uint8_t *uid, const uint8_t *atqa, const uint8_t *sak, uint8_t gdm); diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 3e35a6d7c..4c87dc52f 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -277,7 +277,10 @@ typedef struct { // PM5 can use this to inform which size they support oo. uint16_t max_cmd_data_size; // device side PM3_CMD_DATA_SIZE } PACKED capabilities_t; -#define CAPABILITIES_VERSION 9 +// Bump this whenever a command payload changes shape, so a mismatched client and +// firmware refuse to talk instead of misparsing each other. +// 10: CMD_HF_MIFARE_EML_MEMSET gained a flags byte +#define CAPABILITIES_VERSION 10 // what a pre-v9 device would have used, it could not tell us #define CAPABILITIES_LEGACY_CMD_DATA_SIZE 512 extern capabilities_t g_pm3_capabilities;