reworked "lf em 4x50 chk" to use dynamic memory for dictionary
[RRG-proxmark3.git] / armsrc / Standalone / lf_em4100emul.c
blobb77f5e2e1261d35e158516bdccea22fa5d95f25f
1 //-----------------------------------------------------------------------------
2 // Artyom Gnatyuk, 2020
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // LF emul - Very simple mode. Simulate only predefined in low[] IDs
9 // Short click - select next slot and start simulation
10 //-----------------------------------------------------------------------------
11 #include "standalone.h"
12 #include "proxmark3_arm.h"
13 #include "appmain.h"
14 #include "fpgaloader.h"
15 #include "lfops.h"
16 #include "util.h"
17 #include "dbprint.h"
18 #include "ticks.h"
19 #include "string.h"
20 #include "BigBuf.h"
21 #include "commonutil.h"
23 #define MAX_IND 16 // 4 LEDs - 2^4 combinations
24 #define LF_CLOCK 64 // for 125kHz
26 // low & high - array for storage IDs. Its length must be equal.
27 // Predefined IDs must be stored in low[].
28 static uint64_t low[] = {0x565A1140BE, 0x365A398149, 0x5555555555, 0xFFFFFFFFFF};
29 static uint8_t slots_count;
30 static int buflen;
32 void ModInfo(void) {
33 DbpString(" LF EM4100 simulator standalone mode");
36 static uint64_t rev_quads(uint64_t bits) {
37 uint64_t result = 0;
38 for (int i = 0; i < 16; i++) {
39 result += ((bits >> (60 - 4 * i)) & 0xf) << (4 * i);
41 return result >> 24;
44 static void fill_buff(uint8_t bit) {
45 uint8_t *bba = BigBuf_get_addr();
46 memset(bba + buflen, bit, LF_CLOCK / 2);
47 buflen += (LF_CLOCK / 2);
48 memset(bba + buflen, bit ^ 1, LF_CLOCK / 2);
49 buflen += (LF_CLOCK / 2);
52 static void construct_EM410x_emul(uint64_t id) {
54 int i, j;
55 int binary[4] = {0, 0, 0, 0};
56 int parity[4] = {0, 0, 0, 0};
57 buflen = 0;
59 for (i = 0; i < 9; i++)
60 fill_buff(1);
62 for (i = 0; i < 10; i++) {
63 for (j = 3; j >= 0; j--, id /= 2)
64 binary[j] = id % 2;
66 for (j = 0; j < 4; j++)
67 fill_buff(binary[j]);
69 fill_buff(binary[0] ^ binary[1] ^ binary[2] ^ binary[3]);
70 for (j = 0; j < 4; j++)
71 parity[j] ^= binary[j];
74 for (j = 0; j < 4; j++)
75 fill_buff(parity[j]);
77 fill_buff(0);
80 static void LED_Slot(int i) {
81 LEDsoff();
82 if (slots_count > 4) {
83 LED(i % MAX_IND, 0); //binary indication for slots_count > 4
84 } else {
85 LED(1 << i, 0); //simple indication for slots_count <=4
89 void RunMod(void) {
90 StandAloneMode();
91 FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
92 Dbprintf("[=] >> LF EM4100 simulator started <<");
94 int selected = 0; //selected slot after start
95 slots_count = ARRAYLEN(low);
96 for (;;) {
97 WDT_HIT();
98 if (data_available()) break;
100 SpinDelay(100);
101 SpinUp(100);
102 LED_Slot(selected);
103 construct_EM410x_emul(rev_quads(low[selected]));
104 SimulateTagLowFrequency(buflen, 0, true);
105 selected = (selected + 1) % slots_count;