build(desfire): make emulator test hook optional

Exclude the USB test dispatcher and queued-random support from normal firmware builds. Enable them with ENABLE_HFMFDESETEST=1 when running emulator vectors.

Co-Authored-By: GPT-Daybreak Blue <[email protected]>
This commit is contained in:
Mistial Developer
2026-09-17 00:08:06 +02:00
committed by Philippe Teuwen
co-authored by GPT-Daybreak Blue
parent 50f6b76469
commit 957896a926
6 changed files with 26 additions and 0 deletions
+1
View File
@@ -30,6 +30,7 @@ PLATFORM=PM3RDV4
#PLATFORM_EXTRAS=FLASH
#PLATFORM_EXTRAS=SMARTCARD
#PLATFORM_EXTRAS=BTADDON FPC_USART_DEV FLASH
#ENABLE_HFMFDESETEST=1
#STANDALONE=HF_UNISNIFF
+5
View File
@@ -31,6 +31,11 @@ include(${CMAKE_CURRENT_LIST_DIR}/../tools/FpgaCompress.cmake)
# ---------------------------------------------------------------------------------
option(ENABLE_HFMFDESETEST "Build the host-driven DESFire emulator test hook" OFF)
if (ENABLE_HFMFDESETEST)
list(APPEND PLATFORM_DEFS -DENABLE_HFMFDESETEST)
endif ()
set(APP_CFLAGS ${PLATFORM_DEFS} -ffunction-sections -fdata-sections)
set(INC_DIRS
+2
View File
@@ -2556,10 +2556,12 @@ static void PacketReceived(PacketCommandNG *packet) {
SimulateDesfireTag();
break;
}
#ifdef ENABLE_HFMFDESETEST
case CMD_HF_DESFIRE_SIM_TEST: {
DesfireSimTest(packet);
break;
}
#endif
#endif
case CMD_HF_MIFARE_EML_MEMCLR: {
+12
View File
@@ -228,6 +228,7 @@ static const uint8_t s_sim_rndb[16] = {
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10
};
#ifdef ENABLE_HFMFDESETEST
// Random bytes queued by the host, see `hf mfdes etest --random`. A byte
// stream, not blocks: RndB draws 8 or 16, a random id 3. Outside s_st because
// desfire_sim_init() wipes that at every activation.
@@ -257,10 +258,12 @@ static bool desfire_sim_random_push(const uint8_t *src, uint16_t len) {
s_rnd.len += len;
return true;
}
#endif
// A short queue gives nothing, so a half value never desynchronises the rest.
static bool desfire_sim_random(uint8_t *dst, uint8_t len) {
#ifdef ENABLE_HFMFDESETEST
if (s_rnd.len < len) {
s_rnd.underflow = true;
return false;
@@ -270,6 +273,11 @@ static bool desfire_sim_random(uint8_t *dst, uint8_t len) {
s_rnd.head += len;
s_rnd.len -= len;
return true;
#else
(void)dst;
(void)len;
return false;
#endif
}
// rotate left by one byte, the RndA/RndB' transform of the handshake
@@ -3662,8 +3670,10 @@ void SimulateDesfireTag(void) {
// keep emulator memory, that is where eload put the card image
BigBuf_free_keep_EM();
#ifdef ENABLE_HFMFDESETEST
// bytes a host queued for etest must not reach a reader
desfire_sim_random_clear();
#endif
if (desfire_sim_init() == false) {
Dbprintf("No DESFire card image in emulator memory");
@@ -4051,6 +4061,7 @@ void SimulateDesfireTag(void) {
//------------------------------------------------------- host driven simulation
#ifdef ENABLE_HFMFDESETEST
// SimulateDesfireTag() with the antenna replaced by USB: one operation per
// packet, the answer in the reply. Nothing here touches the FPGA.
void DesfireSimTest(PacketCommandNG *packet) {
@@ -4148,3 +4159,4 @@ void DesfireSimTest(PacketCommandNG *packet) {
reply_ng(CMD_HF_DESFIRE_SIM_TEST, status, data, n);
}
#endif
+2
View File
@@ -38,6 +38,8 @@ void SimulateDesfireTag(void);
// Drive the same simulation from the host, one operation per packet and no
// RF: the command bytes arrive over USB and the answer goes back the same way.
// Answers on CMD_HF_DESFIRE_SIM_TEST, see desfire_sim_test_op_t.
#ifdef ENABLE_HFMFDESETEST
void DesfireSimTest(PacketCommandNG *packet);
#endif
#endif // __DESFIRESIM_H
+4
View File
@@ -196,6 +196,10 @@ else
$(error Invalid or empty PLATFORM: $(PLATFORM). $(KNOWN_DEFINITIONS))
endif
ifeq ($(ENABLE_HFMFDESETEST),1)
PLATFORM_DEFS += -DENABLE_HFMFDESETEST
endif
# parsing additional PLATFORM_EXTRAS tokens
PLATFORM_EXTRAS_TMP:=$(PLATFORM_EXTRAS)
ifneq (,$(findstring SMARTCARD,$(PLATFORM_EXTRAS_TMP)))