From 6ba002725f90e5e6a3dadadf07175e018932682f Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 27 Jul 2023 18:55:41 +0200 Subject: [PATCH] rmeoved some fct devices side regarding eml get/ set --- armsrc/BigBuf.c | 13 +++++++--- armsrc/BigBuf.h | 1 + armsrc/Standalone/hf_colin.c | 8 +++--- armsrc/Standalone/hf_mattyrun.c | 6 ++--- armsrc/iso15693.c | 20 +++++---------- armsrc/iso15693.h | 2 -- armsrc/mifarecmd.c | 13 ++-------- armsrc/mifarecmd.h | 1 - armsrc/mifaresim.c | 2 +- armsrc/mifareutil.c | 43 ++++++++++++++------------------- 10 files changed, 45 insertions(+), 64 deletions(-) diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index 6cd3a29f7..29f270b80 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -308,15 +308,22 @@ bool RAMFUNC LogTraceBits(const uint8_t *btBytes, uint16_t bitLen, uint32_t time // Emulator memory uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length) { uint8_t *mem = BigBuf_get_EM_addr(); - if (offset + length < CARD_MEMORY_SIZE) { + if (offset + length <= CARD_MEMORY_SIZE) { memcpy(mem + offset, data, length); return 0; } Dbprintf("Error, trying to set memory outside of bounds! " _RED_("%d") " > %d", (offset + length), CARD_MEMORY_SIZE); return 1; } - - +uint8_t emlGet(uint8_t *out, uint32_t offset, uint32_t length) { + uint8_t *mem = BigBuf_get_EM_addr(); + if (offset + length <= CARD_MEMORY_SIZE) { + memcpy(out, mem + offset, length); + return 0; + } + Dbprintf("Error, trying to read memory outside of bounds! " _RED_("%d") " > %d", (offset + length), CARD_MEMORY_SIZE); + return 1; +} // get the address of the ToSend buffer. Allocate part of Bigbuf for it, if not yet done tosend_t *get_tosend(void) { diff --git a/armsrc/BigBuf.h b/armsrc/BigBuf.h index b1003a4c1..81c2fbdfc 100644 --- a/armsrc/BigBuf.h +++ b/armsrc/BigBuf.h @@ -56,6 +56,7 @@ bool RAMFUNC LogTraceBits(const uint8_t *btBytes, uint16_t bitLen, uint32_t time bool LogTrace_ISO15693(const uint8_t *bytes, uint16_t len, uint32_t ts_start, uint32_t ts_end, uint8_t *parity, bool reader2tag); uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length); +uint8_t emlGet(uint8_t *out, uint32_t offset, uint32_t length); typedef struct { int max; diff --git a/armsrc/Standalone/hf_colin.c b/armsrc/Standalone/hf_colin.c index 91fa971eb..044d3aba3 100644 --- a/armsrc/Standalone/hf_colin.c +++ b/armsrc/Standalone/hf_colin.c @@ -293,7 +293,7 @@ static void ReadLastTagFromFlash(void) { rdv40_spiffs_read_as_filetype((char *)HFCOLIN_LASTTAG_SYMLINK, (uint8_t *)mem, len, RDV40_SPIFFS_SAFETY_SAFE); // copy 64blocks (16bytes) starting w block0, to emulator mem. - emlSetMem(mem, 0, 64); + emlSetMem_xt(mem, 0, 64, 16); DbprintfEx(FLAG_NEWLINE, "[OK] Last tag recovered from FLASHMEM set to emulator"); cjSetCursLeft(); @@ -650,7 +650,7 @@ failtag: for (uint8_t t = 0; t < 2; t++) { memcpy(mblock + t * 10, foundKey[t][sectorNo], 6); } - emlSetMem(mblock, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1); + emlSetMem_xt(mblock, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1, 16); } cjSetCursLeft(); @@ -827,12 +827,12 @@ int e_MifareECardLoad(uint32_t numofsectors, uint8_t keytype) { }; if (isOK) { if (blockNo < NumBlocksPerSector(s) - 1) { - emlSetMem(dataoutbuf, FirstBlockOfSector(s) + blockNo, 1); + emlSetMem_xt(dataoutbuf, FirstBlockOfSector(s) + blockNo, 1, 16); } else { // sector trailer, keep the keys, set only the AC emlGetMem(dataoutbuf2, FirstBlockOfSector(s) + blockNo, 1); memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4); - emlSetMem(dataoutbuf2, FirstBlockOfSector(s) + blockNo, 1); + emlSetMem_xt(dataoutbuf2, FirstBlockOfSector(s) + blockNo, 1, 16); } } } diff --git a/armsrc/Standalone/hf_mattyrun.c b/armsrc/Standalone/hf_mattyrun.c index 9b41f18c4..ed6f93a3b 100644 --- a/armsrc/Standalone/hf_mattyrun.c +++ b/armsrc/Standalone/hf_mattyrun.c @@ -273,12 +273,12 @@ static int saMifareECardLoad(uint32_t numofsectors, uint8_t keytype) { }; if (blockNo < NumBlocksPerSector(s) - 1) { - emlSetMem(dataoutbuf, FirstBlockOfSector(s) + blockNo, 1); + emlSetMem_xt(dataoutbuf, FirstBlockOfSector(s) + blockNo, 1, 16); } else { // sector trailer, keep the keys, set only the AC emlGetMem(dataoutbuf2, FirstBlockOfSector(s) + blockNo, 1); memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4); - emlSetMem(dataoutbuf2, FirstBlockOfSector(s) + blockNo, 1); + emlSetMem_xt(dataoutbuf2, FirstBlockOfSector(s) + blockNo, 1, 16); } } } @@ -505,7 +505,7 @@ void RunMod(void) { memcpy(mblock + t * 10, foundKey[t][sectorNo], 6); } } - emlSetMem(mblock, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1); + emlSetMem_xt(mblock, FirstBlockOfSector(sectorNo) + NumBlocksPerSector(sectorNo) - 1, 1, 16); } } diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c index a52b953ff..e0c3e4436 100644 --- a/armsrc/iso15693.c +++ b/armsrc/iso15693.c @@ -2119,7 +2119,6 @@ void Iso15693InitTag(void) { StartCountSspClk(); } - void EmlClearIso15693(void) { // Resetting the bitstream also frees the BigBuf memory, so we do this here to prevent // an inconvenient reset in the future by Iso15693InitTag @@ -2128,16 +2127,6 @@ void EmlClearIso15693(void) { reply_ng(CMD_HF_ISO15693_EML_CLEAR, PM3_SUCCESS, NULL, 0); } -void EmlSetMemIso15693(uint8_t count, uint8_t *data, uint32_t offset) { - uint8_t *emCARD = BigBuf_get_EM_addr(); - memcpy(emCARD + offset, data, count); -} - -void EmlGetMemIso15693(uint8_t count, uint8_t *output, uint32_t offset) { - uint8_t *emCARD = BigBuf_get_EM_addr(); - memcpy(output, emCARD + offset, count); -} - // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands // all demodulation performed in arm rather than host. - greg void SimTagIso15693(uint8_t *uid, uint8_t block_size) { @@ -2296,8 +2285,11 @@ void SimTagIso15693(uint8_t *uid, uint8_t block_size) { } // Block data if (block_size * (block_idx + j + 1) <= CARD_MEMORY_SIZE) { - EmlGetMemIso15693(block_size, resp_readblock + (work_offset + security_offset), - block_size * (block_idx + j)); + emlGet( + resp_readblock + (work_offset + security_offset), + block_size * (block_idx + j), + block_size + ); } else { memset(resp_readblock + work_offset + security_offset, 0, block_size); } @@ -2334,7 +2326,7 @@ void SimTagIso15693(uint8_t *uid, uint8_t block_size) { uint8_t *data = cmd + 3 + address_offset + multi_offset; // write data - EmlSetMemIso15693(block_count * block_size, data, block_idx * block_size); + emlSet(data, (block_idx * block_size), (block_count * block_size)); // Build WRITE_(MULTI_)BLOCK response int response_length = 3; diff --git a/armsrc/iso15693.h b/armsrc/iso15693.h index b087edd2e..4644adea9 100644 --- a/armsrc/iso15693.h +++ b/armsrc/iso15693.h @@ -47,8 +47,6 @@ int GetIso15693AnswerFromTag(uint8_t *response, uint16_t max_len, uint16_t timeo void AcquireRawAdcSamplesIso15693(void); void ReaderIso15693(iso15_card_select_t *p_card); // ISO15693 reader void EmlClearIso15693(void); -void EmlSetMemIso15693(uint8_t count, uint8_t *data, uint32_t offset); -void EmlGetMemIso15693(uint8_t count, uint8_t *output, uint32_t offset); void SimTagIso15693(uint8_t *uid, uint8_t block_size); // simulate an ISO15693 tag void BruteforceIso15693Afi(uint32_t speed); // find an AFI of a tag void DirectTag15693Command(uint32_t datalen, uint32_t speed, uint32_t recv, uint8_t *data); // send arbitrary commands from CLI diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index 162b8e902..f7f05a3eb 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -2019,15 +2019,6 @@ void MifareEMemClr(void) { emlClearMem(); } -void MifareEMemSet(uint8_t blockno, uint8_t blockcnt, uint8_t blockwidth, uint8_t *datain) { - FpgaDownloadAndGo(FPGA_BITSTREAM_HF); - - if (blockwidth == 0) - blockwidth = 16; // backwards compat... default bytewidth - - emlSetMem_xt(datain, blockno, blockcnt, blockwidth); // data, block num, blocks count, block byte width -} - void MifareEMemGet(uint8_t blockno, uint8_t blockcnt) { FpgaDownloadAndGo(FPGA_BITSTREAM_HF); @@ -2167,9 +2158,9 @@ int MifareECardLoad(uint8_t sectorcnt, uint8_t keytype) { uint8_t st[16] = {0x00}; emlGetMem(st, tb, 1); memcpy(st + 6, data + 6, 4); - emlSetMem(st, tb, 1); + emlSetMem_xt(st, tb, 1, 16); } else { - emlSetMem(data, tb, 1); + emlSetMem_xt(data, tb, 1, 16); } break; } diff --git a/armsrc/mifarecmd.h b/armsrc/mifarecmd.h index b7f7d6379..52bc37f35 100644 --- a/armsrc/mifarecmd.h +++ b/armsrc/mifarecmd.h @@ -40,7 +40,6 @@ void MifareChkKeys_fast(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *da void MifareChkKeys_file(uint8_t *fn); void MifareEMemClr(void); -void MifareEMemSet(uint8_t blockno, uint8_t blockcnt, uint8_t blockwidth, uint8_t *datain); void MifareEMemGet(uint8_t blockno, uint8_t blockcnt); int MifareECardLoad(uint8_t sectorcnt, uint8_t keytype); int MifareECardLoadExt(uint8_t sectorcnt, uint8_t keytype); diff --git a/armsrc/mifaresim.c b/armsrc/mifaresim.c index 5aa1a054a..a44220209 100644 --- a/armsrc/mifaresim.c +++ b/armsrc/mifaresim.c @@ -1272,7 +1272,7 @@ void Mifare1ksim(uint16_t flags, uint8_t exitAfterNReads, uint8_t *datain, uint1 memcpy(receivedCmd_dec, response, 16); // don't change anything } } - emlSetMem(receivedCmd_dec, cardWRBL, 1); + emlSetMem_xt(receivedCmd_dec, cardWRBL, 1, 16); EmSend4bit(mf_crypto1_encrypt4bit(pcs, CARD_ACK)); // always ACK? FpgaDisableTracing(); diff --git a/armsrc/mifareutil.c b/armsrc/mifareutil.c index c2d6f8246..7b3d27ee1 100644 --- a/armsrc/mifareutil.c +++ b/armsrc/mifareutil.c @@ -617,16 +617,9 @@ uint8_t FirstBlockOfSector(uint8_t sectorNo) { return sectorNo * 4; else return 32 * 4 + (sectorNo - 32) * 16; - } // work with emulator memory -void emlSetMem(uint8_t *data, int blockNum, int blocksCount) { - uint32_t offset = blockNum * 16; - uint32_t len = blocksCount * 16; - emlSet(data, offset, len); -} - void emlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int block_width) { uint32_t offset = blockNum * block_width; uint32_t len = blocksCount * block_width; @@ -634,18 +627,18 @@ void emlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int block_width) } void emlGetMem(uint8_t *data, int blockNum, int blocksCount) { - uint8_t *emCARD = BigBuf_get_EM_addr(); - memcpy(data, emCARD + blockNum * 16, blocksCount * 16); + uint8_t *mem = BigBuf_get_EM_addr(); + memcpy(data, mem + blockNum * 16, blocksCount * 16); } void emlGetMemBt(uint8_t *data, int offset, int byteCount) { - uint8_t *emCARD = BigBuf_get_EM_addr(); - memcpy(data, emCARD + offset, byteCount); + uint8_t *mem = BigBuf_get_EM_addr(); + memcpy(data, mem + offset, byteCount); } int emlCheckValBl(int blockNum) { - uint8_t *emCARD = BigBuf_get_EM_addr(); - uint8_t *data = emCARD + blockNum * 16; + uint8_t *mem = BigBuf_get_EM_addr(); + uint8_t *data = mem + blockNum * 16; if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) || (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) || @@ -659,8 +652,8 @@ int emlCheckValBl(int blockNum) { } int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) { - uint8_t *emCARD = BigBuf_get_EM_addr(); - uint8_t *data = emCARD + blockNum * 16; + uint8_t *mem = BigBuf_get_EM_addr(); + uint8_t *data = mem + blockNum * 16; if (emlCheckValBl(blockNum)) return 1; @@ -671,8 +664,8 @@ int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) { } int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) { - uint8_t *emCARD = BigBuf_get_EM_addr(); - uint8_t *data = emCARD + blockNum * 16; + uint8_t *mem = BigBuf_get_EM_addr(); + uint8_t *data = mem + blockNum * 16; memcpy(data + 0, &blReg, 4); memcpy(data + 8, &blReg, 4); @@ -683,29 +676,29 @@ int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) { data[13] = blBlock ^ 0xff; data[14] = blBlock; data[15] = blBlock ^ 0xff; - return 0; } uint64_t emlGetKey(int sectorNum, int keyType) { uint8_t key[6] = {0x00}; - uint8_t *em = BigBuf_get_EM_addr(); - memcpy(key, em + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6); + uint8_t *mem = BigBuf_get_EM_addr(); + memcpy(key, mem + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6); return bytes_to_num(key, 6); } void emlClearMem(void) { const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04}; - uint8_t *emCARD = BigBuf_get_EM_addr(); - memset(emCARD, 0, CARD_MEMORY_SIZE); + uint8_t *mem = BigBuf_get_EM_addr(); + memset(mem, 0, CARD_MEMORY_SIZE); // fill sectors trailer data - for (uint16_t b = 3; b < MIFARE_4K_MAXBLOCK; ((b < MIFARE_2K_MAXBLOCK - 4) ? (b += 4) : (b += 16))) - emlSetMem((uint8_t *)trailer, b, 1); + for (uint16_t b = 3; b < MIFARE_4K_MAXBLOCK; ((b < MIFARE_2K_MAXBLOCK - 4) ? (b += 4) : (b += 16))) { + emlSetMem_xt((uint8_t *)trailer, b, 1, 16); + } // uid - emlSetMem((uint8_t *)uid, 0, 1); + emlSetMem_xt((uint8_t *)uid, 0, 1, 16); return; }