proxmark3/armsrc/Standalone/lf_em4100emul.c

116 lines
3.5 KiB
C
Raw Normal View History

2020-03-08 01:07:38 +08:00
//-----------------------------------------------------------------------------
2022-01-06 09:19:46 +08:00
// Copyright (C) Artyom Gnatyuk, 2020
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
2020-03-08 01:07:38 +08:00
//
2022-01-06 09:19:46 +08:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// See LICENSE.txt for the text of the license.
2020-03-08 01:07:38 +08:00
//-----------------------------------------------------------------------------
// LF emul - Very simple mode. Simulate only predefined in low[] IDs
// Short click - select next slot and start simulation
2020-03-08 01:07:38 +08:00
//-----------------------------------------------------------------------------
#include "standalone.h"
#include "proxmark3_arm.h"
#include "appmain.h"
#include "fpgaloader.h"
#include "lfops.h"
#include "util.h"
#include "dbprint.h"
#include "ticks.h"
#include "string.h"
#include "BigBuf.h"
2020-05-11 00:04:50 +08:00
#include "commonutil.h"
2020-03-08 01:07:38 +08:00
#define MAX_IND 16 // 4 LEDs - 2^4 combinations
2021-01-28 06:30:37 +08:00
#define LF_CLOCK 64 // for 125kHz
2020-03-08 01:07:38 +08:00
// Predefined IDs must be stored in em4100emul_low[].
static uint64_t em4100emul_low[] = {0x565A1140BE, 0x365A398149, 0x5555555555, 0xFFFFFFFFFF};
static uint8_t em4100emul_slots_count;
static int em4100emul_buflen;
2020-03-08 01:07:38 +08:00
void ModInfo(void) {
DbpString(" LF EM4100 simulator standalone mode");
2020-03-08 01:07:38 +08:00
}
2021-01-28 06:30:37 +08:00
static uint64_t rev_quads(uint64_t bits) {
2020-03-21 22:39:30 +08:00
uint64_t result = 0;
for (int i = 0; i < 16; i++) {
result += ((bits >> (60 - 4 * i)) & 0xf) << (4 * i);
}
return result >> 24;
2020-03-08 01:07:38 +08:00
}
2021-01-28 06:30:37 +08:00
static void fill_buff(uint8_t bit) {
uint8_t *bba = BigBuf_get_addr();
memset(bba + em4100emul_buflen, bit, LF_CLOCK / 2);
em4100emul_buflen += (LF_CLOCK / 2);
memset(bba + em4100emul_buflen, bit ^ 1, LF_CLOCK / 2);
em4100emul_buflen += (LF_CLOCK / 2);
2020-03-08 01:07:38 +08:00
}
2021-01-28 18:49:25 +08:00
static void construct_EM410x_emul(uint64_t id) {
2020-03-21 22:39:30 +08:00
2021-01-28 06:30:37 +08:00
int i, j;
2021-02-09 23:56:55 +08:00
int binary[4] = {0, 0, 0, 0};
int parity[4] = {0, 0, 0, 0};
em4100emul_buflen = 0;
2021-01-28 06:30:37 +08:00
2020-03-08 01:07:38 +08:00
for (i = 0; i < 9; i++)
2021-01-28 06:30:37 +08:00
fill_buff(1);
2020-03-08 01:07:38 +08:00
for (i = 0; i < 10; i++) {
for (j = 3; j >= 0; j--, id /= 2)
binary[j] = id % 2;
2021-01-28 06:30:37 +08:00
2020-03-08 01:07:38 +08:00
for (j = 0; j < 4; j++)
2021-01-28 06:30:37 +08:00
fill_buff(binary[j]);
fill_buff(binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
2020-03-08 01:07:38 +08:00
for (j = 0; j < 4; j++)
2020-03-21 22:39:30 +08:00
parity[j] ^= binary[j];
2020-03-08 01:07:38 +08:00
}
2021-01-28 06:30:37 +08:00
2020-03-21 22:39:30 +08:00
for (j = 0; j < 4; j++)
2021-01-28 06:30:37 +08:00
fill_buff(parity[j]);
fill_buff(0);
2020-03-08 01:07:38 +08:00
}
2020-05-11 00:04:50 +08:00
static void LED_Slot(int i) {
2020-03-21 22:39:30 +08:00
LEDsoff();
if (em4100emul_slots_count > 4) {
LED(i % MAX_IND, 0); //binary indication for em4100emul_slots_count > 4
2020-03-21 22:39:30 +08:00
} else {
LED(1 << i, 0); //simple indication for em4100emul_slots_count <=4
2020-03-21 22:39:30 +08:00
}
2020-03-08 01:07:38 +08:00
}
2020-05-10 22:59:38 +08:00
void RunMod(void) {
2020-03-08 01:07:38 +08:00
StandAloneMode();
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
2020-03-30 21:16:11 +08:00
Dbprintf("[=] >> LF EM4100 simulator started <<");
2020-04-16 15:01:14 +08:00
2020-03-21 22:39:30 +08:00
int selected = 0; //selected slot after start
em4100emul_slots_count = ARRAYLEN(em4100emul_low);
2020-03-21 22:39:30 +08:00
for (;;) {
WDT_HIT();
2020-03-08 01:07:38 +08:00
if (data_available()) break;
2021-01-28 18:49:25 +08:00
2020-03-21 22:39:30 +08:00
SpinDelay(100);
SpinUp(100);
LED_Slot(selected);
construct_EM410x_emul(rev_quads(em4100emul_low[selected]));
SimulateTagLowFrequency(em4100emul_buflen, 0, true);
selected = (selected + 1) % em4100emul_slots_count;
2020-03-21 22:39:30 +08:00
}
2020-03-08 01:07:38 +08:00
}