Merge pull request #1447 from AxisRay/fix-hf_mfcsim

hf_mfcsim refactored
This commit is contained in:
Iceman 2021-08-14 11:03:16 +02:00 committed by GitHub
commit d3d4e72f2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,59 +30,51 @@
* It requires RDV4 hardware (for flash and battery). * It requires RDV4 hardware (for flash and battery).
* *
* On entering stand-alone mode, this module will start simulating. * On entering stand-alone mode, this module will start simulating.
* Data is read from bin dump file uploaded to flash memory (hf_mfcsim_dump.bin). * Data is read from bin dump file uploaded to flash memory (hf_mfcsim_dump_xx.bin).
* Only support mifare classic 1k * Only support mifare classic 1k
* *
* LEDs:
* - LED A: initializing
* - LED B: simulating
* - LED C blinking: data transmiting
*
* To upload input file (eml format) to flash: * To upload input file (eml format) to flash:
* - mem spiffs upload -s <filename> -d hf_mfcsim_dump.bin * - mem spiffs upload -s <filename> -d hf_mfcsim_dump_xx.bin (Notes: xx is form 01 to 15)
* To delete the input file from flash: * To delete the input file from flash:
* - mem spiffs remove -f hf_mfcsim_dump.bin * - mem spiffs remove -f hf_mfcsim_dump_xx.bin (Notes: xx is form 01 to 15)
* *
*/ */
#define HF_MFCSIM_INPUTFILE_SIM "hf_mfcsim_dump.bin" #define HF_MFCSIM_DUMPFILE_SIM "hf_mfcsim_dump_%02d.bin"
#define DUMP_SIZE 1024 #define DUMP_SIZE 1024
static uint8_t uid[10]; static char cur_dump_file[22] = {0};
static bool ecfill_from_file(char *inputfile) { static bool fill_eml_from_file(char *dumpfile) {
// check file exist
if (exists_in_spiffs(inputfile)) { if (!exists_in_spiffs(dumpfile)) {
uint32_t size = size_in_spiffs(inputfile); Dbprintf(_RED_("Dump file %s not found!"), dumpfile);
uint8_t *mem = BigBuf_malloc(size);
if (!mem) {
Dbprintf(_RED_("No memory"));
return false; return false;
} }
//read dumpfile
Dbprintf(_YELLOW_("Found dump file %s"), inputfile);
rdv40_spiffs_read_as_filetype(inputfile, mem, size, RDV40_SPIFFS_SAFETY_SAFE);
//check dumpfile size //check dumpfile size
Dbprintf(_YELLOW_("File size is %d"), size); uint32_t size = size_in_spiffs(dumpfile);
if (size != DUMP_SIZE) { if (size != DUMP_SIZE) {
Dbprintf(_RED_("Only support Mifare Classic 1K! Please check the dumpfile")); Dbprintf(_RED_("File Size: %dB The dump file size is incorrect! Only support Mifare Classic 1K! Please check it."));
BigBuf_free(); BigBuf_free();
return false; return false;
} }
//read and load dump file
//load the dump into emulator memory if (DBGLEVEL >= DBG_INFO)
Dbprintf(_YELLOW_("Read card data from input file")); Dbprintf(_YELLOW_("Found dump file %s. Uploading to emulator memory..."), dumpfile);
emlSetMem(mem, 0, MIFARE_1K_MAXBLOCK); emlClearMem();
Dbprintf(_YELLOW_("Uploaded to emulator memory")); uint8_t *emCARD = BigBuf_get_EM_addr();
BigBuf_free_keep_EM(); rdv40_spiffs_read_as_filetype(dumpfile, emCARD, size, RDV40_SPIFFS_SAFETY_SAFE);
return true; return true;
} else { }
Dbprintf(_RED_("no input file %s"), inputfile);
static bool write_file_from_eml(char *dumpfile) {
if (!exists_in_spiffs(dumpfile)) {
Dbprintf(_RED_("Dump file %s not found!"), dumpfile);
return false; return false;
} }
return false;//Shouldn't be here uint8_t *emCARD = BigBuf_get_EM_addr();
rdv40_spiffs_write(dumpfile, emCARD, DUMP_SIZE, RDV40_SPIFFS_SAFETY_SAFE);
return true;
} }
void ModInfo(void) { void ModInfo(void) {
@ -90,32 +82,63 @@ void ModInfo(void) {
} }
void RunMod(void) { void RunMod(void) {
//initializing
StandAloneMode(); StandAloneMode();
FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
rdv40_spiffs_lazy_mount();
Dbprintf(_YELLOW_("Standalone mode MFCSIM started!")); Dbprintf(_YELLOW_("Standalone mode MFCSIM started!"));
LED_A_ON(); bool flag_has_dumpfile = false;
emlClearMem(); for (int i = 1;; i++) {
Dbprintf(_YELLOW_("Emulator memory initialized")); //Exit! usbcommand break
rdv40_spiffs_lazy_mount(); if (data_available()) break;
if (!ecfill_from_file(HF_MFCSIM_INPUTFILE_SIM)) {
Dbprintf(_RED_("Load data failed!")); //Infinite loop
return; if (i > 15) {
if (!flag_has_dumpfile)
break; //still no dump file found
i = 1; //next loop
} }
Dbprintf(_YELLOW_("Emulator memory filled, simulation ready to start."));
Dbprintf(_YELLOW_("Press button to abort simulation at anytime."));
SpinOff(1000); //Indicate which card will be simulated
LED(i, 0);
LED_B_ON(); //Try to load dump form flash
Dbprintf(_YELLOW_("Simulation start!")); sprintf(cur_dump_file, HF_MFCSIM_DUMPFILE_SIM, i);
uint16_t simflags = FLAG_UID_IN_EMUL | FLAG_MF_1K; Dbprintf(_YELLOW_("[Slot: %d] Try to load dump file: %s"), i, cur_dump_file);
Mifare1ksim(simflags, 0, uid, 0, 0); if (!fill_eml_from_file(cur_dump_file)) {
Dbprintf(_YELLOW_("[Slot: %d] Dump load Failed, Next one!"), i);
Dbprintf(_YELLOW_("Simulation end!"));
LEDsoff(); LEDsoff();
continue;
}
flag_has_dumpfile = true;
//Exit! Button hold break
int button_pressed = BUTTON_HELD(500);
if (button_pressed == BUTTON_HOLD) {
Dbprintf("Button hold, Break!");
break;
}
//Hope there is enough time to see clearly
SpinDelay(500);
//Start to simulate
Dbprintf(_YELLOW_("[Slot: %d] Simulation start, Press button to change next card."), i);
uint16_t simflags = FLAG_UID_IN_EMUL | FLAG_MF_1K;
Mifare1ksim(simflags, 0, NULL, 0, 0);
Dbprintf(_YELLOW_("[Slot: %d] Simulation end, Write Back to dump file!"), i);
//Simulation end, Write Back
if (!write_file_from_eml(cur_dump_file)) {
Dbprintf(_RED_("[Slot: %d] Write Failed! Anyway, Change to next one!"), i);
continue;
}
Dbprintf(_YELLOW_("[Slot: %d] Write Success! Change to next one!"), i);
}
if (!flag_has_dumpfile)
Dbprintf("No dump file found!");
Dbprintf("Breaked! Exit standalone mode!");
SpinErr(15, 200, 3);
return;
} }