diff --git a/Makefile.platform.sample b/Makefile.platform.sample index ee68c7a3e..d912ce9d6 100644 --- a/Makefile.platform.sample +++ b/Makefile.platform.sample @@ -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 diff --git a/armsrc/CMakeLists.txt b/armsrc/CMakeLists.txt index 2603e7acb..3960c5710 100644 --- a/armsrc/CMakeLists.txt +++ b/armsrc/CMakeLists.txt @@ -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 diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 4df71969e..7b5eabc7b 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -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: { diff --git a/armsrc/desfiresim.c b/armsrc/desfiresim.c index 3614b7a86..bbe3304aa 100644 --- a/armsrc/desfiresim.c +++ b/armsrc/desfiresim.c @@ -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 diff --git a/armsrc/desfiresim.h b/armsrc/desfiresim.h index eefef3c6a..704f719e4 100644 --- a/armsrc/desfiresim.h +++ b/armsrc/desfiresim.h @@ -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 diff --git a/common_arm/Makefile.hal b/common_arm/Makefile.hal index ca575e6d4..30d764ae7 100644 --- a/common_arm/Makefile.hal +++ b/common_arm/Makefile.hal @@ -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)))