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)
This commit is contained in:
iceman1001andClaude Opus 5 (1M context) committed 2026-09-12 18:49:24 +02:00
1 parent 054fae4ac1
commit 4d7d49e089
11 files changed
+82 -16

No files matched your search

+1 -1
View File
@@ -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;
+6 -2
View File
@@ -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;
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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;
}
+4 -2
View File
@@ -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();
+1 -1
View File
@@ -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);