Skip to content

Commit

Permalink
Some cleanup, some more files moved to palloc
Browse files Browse the repository at this point in the history
  • Loading branch information
jlitewski committed May 14, 2024
1 parent aa1d3c8 commit 06aa9c7
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion armsrc/Standalone/hf_legic.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void save_dump_to_file(legic_card_select_t *p_card) {
#ifdef WITH_FLASH

// legic functions puts it memory in Emulator reserved memory.
uint8_t *mem = get_emulator_address();
uint8_t *mem = (uint8_t*)get_emulator_address();

char *preferredName = (char *)palloc(1, 30);
if (preferredName == NULL) {
Expand Down
11 changes: 5 additions & 6 deletions armsrc/Standalone/hf_legicsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <inttypes.h>
#include "ticks.h"
#include "proxmark3_arm.h"
#include "BigBuf.h"
#include "cardemu.h"
#include "commonutil.h"
#include "fpgaloader.h"
#include "util.h"
Expand Down Expand Up @@ -63,16 +63,14 @@ static bool fill_eml_from_file(char *dumpfile) {
uint32_t size = size_in_spiffs(dumpfile);
if (size != DUMP_SIZE) {
Dbprintf(_RED_("File Size: %dB The dump file size is incorrect! Only support Legic Prime MIM1024! Please check it."));
BigBuf_free();
return false;
}
//read and load dump file
BigBuf_Clear();

if (g_dbglevel >= DBG_INFO) {
Dbprintf("Found dump file... `" _YELLOW_("%s") "`", dumpfile);
Dbprintf("Uploading to emulator memory...");
}
uint8_t *emCARD = BigBuf_get_EM_addr();
uint8_t *emCARD = (uint8_t*)get_emulator_address();
rdv40_spiffs_read_as_filetype(dumpfile, emCARD, size, RDV40_SPIFFS_SAFETY_SAFE);
return true;
}
Expand All @@ -82,7 +80,7 @@ static bool write_file_from_eml(char *dumpfile) {
Dbprintf(_RED_("Dump file %s not found!"), dumpfile);
return false;
}
uint8_t *emCARD = BigBuf_get_EM_addr();
uint8_t *emCARD = (uint8_t*)get_emulator_address();
rdv40_spiffs_write(dumpfile, emCARD, DUMP_SIZE, RDV40_SPIFFS_SAFETY_SAFE);
return true;
}
Expand Down Expand Up @@ -148,6 +146,7 @@ void RunMod(void) {
}
if (!flag_has_dumpfile)
Dbprintf("No dump file found!");

Dbprintf("Breaked! Exit standalone mode!");
SpinErr(15, 200, 3);
return;
Expand Down
27 changes: 19 additions & 8 deletions armsrc/Standalone/hf_mattyrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ on a blank card.
#include "iso14443a.h"
#include "mifarecmd.h"
#include "crc16.h"
#include "BigBuf.h"
#include "palloc.h"
#include "tracer.h"
#include "cardemu.h"
#include "mifaresim.h" // mifare1ksim
#include "mifareutil.h"

Expand Down Expand Up @@ -92,13 +94,13 @@ static int saMifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_
uint8_t isOK = 0;
uint8_t d_block[18] = {0x00};

uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
uint8_t receivedAnswer[MAX_FRAME_SIZE];
uint8_t receivedAnswerPar[MAX_PARITY_SIZE];

// reset FPGA and LED
if (workFlags & 0x08) {
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
set_tracing(false);
stop_tracing();
}

while (true) {
Expand Down Expand Up @@ -188,7 +190,7 @@ static int saMifareChkKeys(uint8_t blockNo, uint8_t keyType, bool clearTrace,
uint8_t keyCount, uint8_t *datain, uint64_t *key) {
g_dbglevel = DBG_NONE;
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
set_tracing(false);
stop_tracing();

struct Crypto1State mpcs = {0, 0};
struct Crypto1State *pcs;
Expand Down Expand Up @@ -240,8 +242,8 @@ static int saMifareECardLoad(uint32_t numofsectors, uint8_t keytype) {
uint8_t dataoutbuf2[16];

iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
clear_trace();
set_tracing(false);
release_trace();
stop_tracing();

int retval = PM3_SUCCESS;

Expand Down Expand Up @@ -400,7 +402,14 @@ void RunMod(void) {
This part allocates the byte representation of the
keys in keyBlock's memory space .
*/
keyBlock = BigBuf_malloc(ARRAYLEN(mfKeys) * 6);
keyBlock = palloc(6, ARRAYLEN(mfKeys));

if(keyBlock == nullptr) {
LEDsoff();
Dbprintf("Unable to allocate memory, aborting...");
return;
}

int mfKeysCnt = ARRAYLEN(mfKeys);

for (int mfKeyCounter = 0; mfKeyCounter < mfKeysCnt; mfKeyCounter++) {
Expand Down Expand Up @@ -468,6 +477,8 @@ void RunMod(void) {
}
}

palloc_free(keyBlock);

/*
TODO:
- Get UID from tag and set accordingly in emulator memory and call mifaresim with right flags (iceman)
Expand Down
7 changes: 3 additions & 4 deletions armsrc/Standalone/hf_mfcsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <inttypes.h>
#include "ticks.h"
#include "proxmark3_arm.h"
#include "BigBuf.h"
#include "cardemu.h"
#include "commonutil.h"
#include "fpgaloader.h"
#include "util.h"
Expand Down Expand Up @@ -65,7 +65,6 @@ static bool fill_eml_from_file(char *dumpfile) {
uint32_t size = size_in_spiffs(dumpfile);
if (size != DUMP_SIZE) {
Dbprintf(_RED_("File Size: %dB The dump file size is incorrect! Only support Mifare Classic 1K! Please check it."));
BigBuf_free();
return false;
}

Expand All @@ -76,7 +75,7 @@ static bool fill_eml_from_file(char *dumpfile) {
}

emlClearMem();
uint8_t *emCARD = BigBuf_get_EM_addr();
uint8_t *emCARD = (uint8_t*)get_emulator_address();
rdv40_spiffs_read_as_filetype(dumpfile, emCARD, size, RDV40_SPIFFS_SAFETY_SAFE);
return true;
}
Expand All @@ -86,7 +85,7 @@ static bool write_file_from_eml(char *dumpfile) {
Dbprintf(_RED_("Dump file %s not found!"), dumpfile);
return false;
}
uint8_t *emCARD = BigBuf_get_EM_addr();
uint8_t *emCARD = (uint8_t*)get_emulator_address();
rdv40_spiffs_write(dumpfile, emCARD, DUMP_SIZE, RDV40_SPIFFS_SAFETY_SAFE);
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions armsrc/appmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,7 @@ static void PacketReceived(PacketCommandNG *packet) {
#endif
case CMD_DOWNLOAD_EMULATOR: {
LED_B_ON();
uint16_t *mem = get_emulator_address();
uint8_t *mem = (uint8_t*)get_emulator_address();
uint32_t startidx = packet->oldarg[0];
uint32_t numofbytes = packet->oldarg[1];

Expand Down Expand Up @@ -2869,7 +2869,7 @@ static void PacketReceived(PacketCommandNG *packet) {

struct p *payload = (struct p *) packet->data.asBytes;

uint8_t *bb = get_emulator_address();
uint8_t *bb = (uint8_t*)get_emulator_address();
if (payload->mlen == 0) {
bb[0] = payload->arg;
} else {
Expand Down
2 changes: 1 addition & 1 deletion armsrc/em4x50.c
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ void em4x50_sim(const uint32_t *password, bool ledcontrol) {

int command = PM3_ENODATA;

uint16_t *em4x50_mem = get_emulator_address();
uint8_t *em4x50_mem = (uint8_t*)get_emulator_address();
uint32_t tag[EM4X50_NO_WORDS] = {0x0};

for (int i = 0; i < EM4X50_NO_WORDS; i++)
Expand Down
10 changes: 5 additions & 5 deletions armsrc/iclass.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void iclass_simulate(uint8_t sim_type, uint8_t num_csns, bool send_reply, uint8_
if(send_reply) start_tracing();

//Use the emulator memory for SIM
uint16_t *emulator = get_emulator_address();
uint8_t *emulator = (uint8_t*)get_emulator_address();
uint8_t mac_responses[PM3_CMD_DATA_SIZE] = { 0 };

if (sim_type == ICLASS_SIM_MODE_CSN) {
Expand Down Expand Up @@ -308,8 +308,8 @@ int do_iclass_simulation(int simulationMode, uint8_t *reader_mac_buf) {
cipher_state_t cipher_state_KC[8];
cipher_state_t *cipher_state = &cipher_state_KD[0];

uint16_t *emulator = get_emulator_address();
uint8_t *csn = emulator;
memptr_t *emulator = get_emulator_address();
uint8_t *csn = (uint8_t*)emulator;

// CSN followed by two CRC bytes
uint8_t anticoll_data[10] = { 0 };
Expand Down Expand Up @@ -875,8 +875,8 @@ int do_iclass_simulation_nonsec(void) {
uint16_t page_size = 32 * 8;
uint8_t current_page = 0;

uint16_t *emulator = get_emulator_address();
uint16_t *csn = emulator;
memptr_t *emulator = get_emulator_address();
uint8_t *csn = (uint8_t*)emulator;

// CSN followed by two CRC bytes
uint8_t anticoll_data[10] = { 0 };
Expand Down
2 changes: 1 addition & 1 deletion armsrc/legicrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "util.h"
#include "protocols.h"

static uint16_t *legic_mem; /* card memory, used for read, write */
static memptr_t *legic_mem; /* card memory, used for read, write */
static legic_card_select_t card;/* metadata of currently selected card */
static crc_t legic_crc;

Expand Down
2 changes: 1 addition & 1 deletion armsrc/legicrfsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "dbprint.h"
#include "util.h"

static uint16_t *legic_mem; /* card memory, used for sim */
static memptr_t *legic_mem; /* card memory, used for sim */
static legic_card_select_t card;/* metadata of currently selected card */
static crc_t legic_crc;

Expand Down
2 changes: 1 addition & 1 deletion armsrc/mifaresim.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ void Mifare1ksim(uint16_t flags, uint8_t exitAfterNReads, uint8_t *datain, uint1
LED_D_ON();
ResetSspClk();

uint16_t *p_em = get_emulator_address();
uint8_t *p_em = (uint8_t*)get_emulator_address();
uint8_t cve_flipper = 0;

int counter = 0;
Expand Down
14 changes: 7 additions & 7 deletions armsrc/mifareutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,8 @@ void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
}

bool emlCheckValBl(int blockNum) {
uint16_t *mem = get_emulator_address();
uint16_t *d = mem + (blockNum * 16);
uint8_t *mem = (uint8_t*)get_emulator_address();
uint8_t *d = mem + (blockNum * 16);

if ((d[0] != (d[4] ^ 0xff)) || (d[0] != d[8]) ||
(d[1] != (d[5] ^ 0xff)) || (d[1] != d[9]) ||
Expand All @@ -734,8 +734,8 @@ bool emlCheckValBl(int blockNum) {
}

int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
uint16_t *mem = get_emulator_address();
uint16_t *d = mem + blockNum * 16;
uint8_t *mem = (uint8_t*)get_emulator_address();
uint8_t *d = mem + blockNum * 16;

if (emlCheckValBl(blockNum) == false) {
return PM3_ESOFT;
Expand All @@ -747,7 +747,7 @@ int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
}

void emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
uint8_t *mem = get_emulator_address();
uint8_t *mem = (uint8_t*)get_emulator_address();
uint8_t *d = mem + blockNum * 16;

palloc_copy(d + 0, &blReg, 4);
Expand All @@ -763,15 +763,15 @@ void emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {

uint64_t emlGetKey(int sectorNum, int keyType) {
uint8_t key[6] = {0x00};
uint8_t *mem = get_emulator_address();
uint8_t *mem = (uint8_t*)get_emulator_address();
palloc_copy(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 *mem = get_emulator_address();
uint8_t *mem = (uint8_t*)get_emulator_address();
palloc_set(mem, 0, CARD_MEMORY_SIZE);

// fill sectors trailer data
Expand Down

0 comments on commit 06aa9c7

Please sign in to comment.