Skip to content

Commit

Permalink
That was harder that I would like to admit
Browse files Browse the repository at this point in the history
Probably heavily broke things as well, but that's a future me problem
  • Loading branch information
jlitewski committed May 15, 2024
1 parent 02e7b9c commit 67efeae
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 216 deletions.
10 changes: 3 additions & 7 deletions armsrc/Standalone/lf_em4100emul.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
// Predefined IDs must be stored in predefined_ids[].
static uint64_t predefined_ids[] = {0x565A1140BE, 0x365A398149, 0x5555555555, 0xFFFFFFFFFF};
static uint8_t predefined_slots;
static uint16_t *memory_addr = nullptr;
static memptr_t *memory_addr = nullptr;
static uint16_t buffer_len = 0;

void ModInfo(void) {
Expand Down Expand Up @@ -113,14 +113,10 @@ void RunMod(void) {
SpinUp(100);
LED_Slot(selected);
construct_EM410x_emul(rev_quads(predefined_ids[selected]));
SimulateTagLowFrequency(buffer_len, 0, true);
SimulateTagLowFrequency(buffer_len, 0, true, (uint8_t*)memory_addr);

selected = (selected + 1) % predefined_slots;
}

memory_addr = palloc(1, (MAX_BLOCK_SIZE / 4)); //8k bytes should be enough?
if(memory_addr == nullptr) {
Dbprintf(_RED_("Unable to allocate memory for the EM4100 Emulator!"));
return;
}
palloc_free(memory_addr);
}
6 changes: 3 additions & 3 deletions armsrc/Standalone/lf_em4100rswb.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static uint64_t em4100rswb_low[] = {0, 0, 0, 0};
// In em4100rswb_high[] must be nulls
static uint32_t em4100rswb_high[] = {0, 0, 0, 0};

static uint16_t *memory_addr = nullptr;
static memptr_t *memory_addr = nullptr;
static uint16_t buffer_len;

void ModInfo(void) {
Expand Down Expand Up @@ -233,7 +233,7 @@ static int BruteEMTag(uint64_t originalCard, int slot) {
uint64_t currentCard = PackEmID(originalCard, cardnum);
Dbprintf("[=] >> Simulating card id %"PRIx64" <<", currentCard);
construct_EM410x_emul(rev_quads(currentCard));
SimulateTagLowFrequencyEx(buffer_len, 0, 1, bruteforce_speeds[current_bruteforce_speed] * 10000);
SimulateTagLowFrequencyEx(buffer_len, 0, 1, bruteforce_speeds[current_bruteforce_speed] * 10000, (uint8_t*)memory_addr);

int button_pressed = BUTTON_CLICKED(1000);
if (button_pressed == BUTTON_SINGLE_CLICK) {
Expand Down Expand Up @@ -278,7 +278,7 @@ static int ExecuteMode(int mode, int slot) {
case LF_RWSB_MODE_SIM:
Dbprintf("[=] >> Sim mode started <<");
construct_EM410x_emul(rev_quads(em4100rswb_low[slot]));
SimulateTagLowFrequency(buffer_len, 0, true);
SimulateTagLowFrequency(buffer_len, 0, true, (uint8_t*)memory_addr);
return LF_RWSB_UNKNOWN_RESULT;
case LF_RWSB_MODE_WRITE:
Dbprintf("[!!] >> Write mode started <<");
Expand Down
6 changes: 4 additions & 2 deletions armsrc/Standalone/lf_em4100rwc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static uint32_t predefined_high[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
static uint8_t predefined_slots;

static uint16_t buffer_len;
static uint16_t *memory_addr = nullptr;
static memptr_t *memory_addr = nullptr;

void ModInfo(void) {
DbpString(" LF EM4100 read/write/clone mode");
Expand Down Expand Up @@ -204,7 +204,7 @@ void RunMod(void) {
construct_EM410x_emul(rev_quads(predefined_ids[selected]));
flash_leds(100, 5);

SimulateTagLowFrequency(buffer_len, 0, true);
SimulateTagLowFrequency(buffer_len, 0, true, (uint8_t*)memory_addr);
led_slot(selected);
state = 0; // Switch to select mode
}
Expand All @@ -231,4 +231,6 @@ void RunMod(void) {
break;
}
}

palloc_free(memory_addr);
}
27 changes: 15 additions & 12 deletions armsrc/Standalone/lf_nedap_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ typedef struct _NEDAP_TAG {

const NEDAP_TAG Tag = {.subType = 0x5, .customerCode = 0x123, .id = 42424, .bIsLong = 1};

static int NedapPrepareBigBuffer(const NEDAP_TAG *pTag);
static void biphaseSimBitInverted(uint8_t c, int *n, uint8_t *phase);
static int NedapPrepareBigBuffer(const NEDAP_TAG *pTag, uint8_t *dest);
static void biphaseSimBitInverted(uint8_t c, int *n, uint8_t *phase, uint8_t *dest);
static void NedapGen(uint8_t subType, uint16_t customerCode, uint32_t id, bool isLong, uint8_t *data);
static uint8_t isEven_64_63(const uint8_t *data);
static inline uint32_t bitcount32(uint32_t a);
Expand All @@ -51,22 +51,27 @@ void ModInfo(void) {
}

void RunMod(void) {
int n;

StandAloneMode();

uint8_t *buf = (uint8_t*)palloc(1, 4098); // 4Kb good?
if(buf == nullptr) {
Dbprintf("Unable to allocate memory, aborting...");
return;
}

int n;
Dbprintf("[=] " MODULE_LONG_NAME " -- started");
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
Dbprintf("[=] NEDAP (%s) - ID: " _GREEN_("%05u") " subtype: " _GREEN_("%1u") " customer code: " _GREEN_("%u / 0x%03X"), Tag.bIsLong ? "128b" : "64b", Tag.id, Tag.subType, Tag.customerCode, Tag.customerCode);

n = NedapPrepareBigBuffer(&Tag);
n = NedapPrepareBigBuffer(&Tag, buf);
do {
WDT_HIT();

if (data_available())
break;

SimulateTagLowFrequency(n, 0, true);
SimulateTagLowFrequency(n, 0, true, buf);

} while (BUTTON_HELD(1000) == BUTTON_NO_CLICK);

Expand All @@ -75,7 +80,7 @@ void RunMod(void) {
LEDsoff();
}

static int NedapPrepareBigBuffer(const NEDAP_TAG *pTag) {
static int NedapPrepareBigBuffer(const NEDAP_TAG *pTag, uint8_t* dest) {
int ret = 0;
uint8_t data[16], bitStream[sizeof(data) * 8], phase = 0;
uint16_t i, size = pTag->bIsLong ? sizeof(data) : (sizeof(data) / 2);
Expand All @@ -85,20 +90,18 @@ static int NedapPrepareBigBuffer(const NEDAP_TAG *pTag) {
size <<= 3;

for (i = 0; i < size; i++) {
biphaseSimBitInverted(!bitStream[i], &ret, &phase);
biphaseSimBitInverted(!bitStream[i], &ret, &phase, dest);
}
if (phase == 1) { //run a second set inverted to keep phase in check
for (i = 0; i < size; i++) {
biphaseSimBitInverted(!bitStream[i], &ret, &phase);
biphaseSimBitInverted(!bitStream[i], &ret, &phase, dest);
}
}

return ret;
}

static void biphaseSimBitInverted(uint8_t c, int *n, uint8_t *phase) {
uint8_t *dest = BigBuf_get_addr();

static void biphaseSimBitInverted(uint8_t c, int *n, uint8_t *phase, uint8_t *dest) {
if (c) {
palloc_set(dest + (*n), c ^ 1 ^ *phase, 32);
palloc_set(dest + (*n) + 32, c ^ *phase, 32);
Expand Down
8 changes: 3 additions & 5 deletions armsrc/hitag2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ void SimulateHitag2(bool ledcontrol) {
while (BUTTON_PRESS() == false) {

// use malloc
initSampleBufferEx(&signal_size, true);
initSampleBuffer(&signal_size);

if (ledcontrol) {
LED_D_ON();
Expand Down Expand Up @@ -1791,8 +1791,7 @@ void ReaderHitag(const lf_hitag_data_t *payload, bool ledcontrol) {

while (bStop == false && BUTTON_PRESS() == false) {

// use malloc
initSampleBufferEx(&signal_size, true);
initSampleBuffer(&signal_size);

WDT_HIT();

Expand Down Expand Up @@ -2154,8 +2153,7 @@ void WriterHitag(const lf_hitag_data_t *payload, bool ledcontrol) {

while (bStop == false && BUTTON_PRESS() == false) {

// use malloc
initSampleBufferEx(&signal_size, true);
initSampleBuffer(&signal_size);

// only every 4000th times, in order to save time when collecting samples.
if (checked == 4000) {
Expand Down
3 changes: 1 addition & 2 deletions armsrc/lfadc.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,8 @@ void lf_init(bool reader, bool simulate, bool ledcontrol) {
// Prepare data trace
uint32_t bufsize = 10000;

// use malloc
if (g_logging) {
initSampleBufferEx(&bufsize, true);
initSampleBuffer(&bufsize);
}

lf_sample_mean();
Expand Down
Loading

0 comments on commit 67efeae

Please sign in to comment.