diff --git a/CHANGELOG.md b/CHANGELOG.md index f48b9a860..28267ebf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 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] +- Change NG: convert CMD_LF_EM4X50_ESET to em4x50_eset_t (@iceman1001) - Change NG: convert CMD_LF_T55XX_SET_CONFIG to t55xx_setconfig_t (@iceman1001) - Added a new ISo15693 v5 dump file format to handle 0x100 number of pages and 8 byte pages better (@iceman1001) - Fixed ISO15693 v4 file handling and converting to v5 (@iceman1001) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 25116a35a..4225d1bfe 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1697,7 +1697,20 @@ static void PacketReceived(PacketCommandNG *packet) { // destroy the Emulator Memory. //----------------------------------------------------------------------------- FpgaDownloadAndGo(FPGA_BITSTREAM_LF); - emlSet(packet->data.asBytes, packet->oldarg[0], packet->oldarg[1]); + + if (packet->length < sizeof(em4x50_eset_t)) { + reply_ng(CMD_LF_EM4X50_ESET, PM3_EINVARG, NULL, 0); + break; + } + + em4x50_eset_t *payload = (em4x50_eset_t *)packet->data.asBytes; + if (payload->len > packet->length - sizeof(em4x50_eset_t)) { + reply_ng(CMD_LF_EM4X50_ESET, PM3_EINVARG, NULL, 0); + break; + } + + int res = emlSet(payload->data, payload->offset, payload->len); + reply_ng(CMD_LF_EM4X50_ESET, res, NULL, 0); break; } case CMD_LF_EM4X50_CHK: { diff --git a/client/src/cmdlfem4x50.c b/client/src/cmdlfem4x50.c index 81e456328..d5d9e6ac4 100644 --- a/client/src/cmdlfem4x50.c +++ b/client/src/cmdlfem4x50.c @@ -163,26 +163,54 @@ static int em4x50_load_file(const char *filename, uint8_t *data, size_t data_len return PM3_SUCCESS; } -static void em4x50_seteml(uint8_t *src, uint32_t offset, uint32_t numofbytes) { +static int em4x50_seteml(const uint8_t *src, uint32_t offset, uint32_t numofbytes) { + + const size_t max_chunk = PM3_CMD_DATA_SIZE - sizeof(em4x50_eset_t); + + uint8_t buf[PM3_CMD_DATA_SIZE] = {0}; + em4x50_eset_t *payload = (em4x50_eset_t *)buf; PrintAndLogEx(INFO, "uploading to emulator memory"); PrintAndLogEx(INFO, "." NOLF); + // fast push mode g_conn.block_after_ACK = true; - for (size_t i = offset; i < numofbytes; i += PM3_CMD_DATA_SIZE_MIX) { + for (size_t i = offset; i < numofbytes; i += max_chunk) { - size_t len = MIN((numofbytes - i), PM3_CMD_DATA_SIZE_MIX); + size_t len = MIN((numofbytes - i), max_chunk); if (len == numofbytes - i) { // Disable fast mode on last packet g_conn.block_after_ACK = false; } + + payload->offset = i; + payload->len = len; + memcpy(payload->data, src + i, len); + clearCommandBuffer(); - SendCommandMIX(CMD_LF_EM4X50_ESET, i, len, 0, src + i, len); + SendCommandNG(CMD_LF_EM4X50_ESET, buf, sizeof(em4x50_eset_t) + len); + + PacketResponseNG resp; + if (WaitForResponseTimeout(CMD_LF_EM4X50_ESET, &resp, 2000) == false) { + g_conn.block_after_ACK = false; + PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(WARNING, "timeout while waiting for reply"); + return PM3_ETIMEOUT; + } + + if (resp.status != PM3_SUCCESS) { + g_conn.block_after_ACK = false; + PrintAndLogEx(NORMAL, ""); + PrintAndLogEx(FAILED, "uploading to emulator memory ( " _RED_("fail") " )"); + return resp.status; + } + PrintAndLogEx(NORMAL, "." NOLF); fflush(stdout); } PrintAndLogEx(NORMAL, ""); PrintAndLogEx(SUCCESS, "uploaded " _YELLOW_("%d") " bytes to emulator memory", numofbytes); + return PM3_SUCCESS; } static int CmdEM4x50ELoad(const char *Cmd) { @@ -214,7 +242,11 @@ static int CmdEM4x50ELoad(const char *Cmd) { } // upload to emulator memory - em4x50_seteml(data, 0, EM4X50_DUMP_FILESIZE); + int res = em4x50_seteml(data, 0, EM4X50_DUMP_FILESIZE); + if (res != PM3_SUCCESS) { + return res; + } + PrintAndLogEx(HINT, "Hint: Use `" _YELLOW_("lf em 4x50 sim -h") "` to simulate"); PrintAndLogEx(INFO, "Done!"); return PM3_SUCCESS; diff --git a/include/em4x50.h b/include/em4x50.h index 1b4d7fa7f..c8f7ace15 100644 --- a/include/em4x50.h +++ b/include/em4x50.h @@ -72,6 +72,13 @@ typedef struct { uint8_t byte[4]; } PACKED em4x50_word_t; +// CMD_LF_EM4X50_ESET payload +typedef struct { + uint16_t offset; + uint16_t len; + uint8_t data[]; +} PACKED em4x50_eset_t; + typedef struct { uint8_t count; uint32_t *words;