371535d5 |
1 | //----------------------------------------------------------------------------- |
2 | // |
3 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, |
4 | // at your option, any later version. See the LICENSE.txt file for the text of |
5 | // the license. |
6 | //----------------------------------------------------------------------------- |
7 | // Low frequency Presco tag commands |
8 | //----------------------------------------------------------------------------- |
9 | |
10 | #include "cmdlfvisa2000.h" |
11 | |
0a7e86db |
12 | #define BL0CK1 0x56495332 |
13 | |
371535d5 |
14 | static int CmdHelp(const char *Cmd); |
15 | |
16 | int usage_lf_visa2k_clone(void){ |
17 | PrintAndLog("clone a Visa2000 tag to a T55x7 tag."); |
18 | PrintAndLog("Usage: lf visa2k clone [h] <card ID> <Q5>"); |
19 | PrintAndLog("Options:"); |
20 | PrintAndLog(" h : This help"); |
21 | PrintAndLog(" <card ID> : Visa2k card ID"); |
22 | PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)"); |
23 | PrintAndLog(""); |
24 | PrintAndLog("Sample: lf visa2k clone 112233"); |
25 | return 0; |
26 | } |
27 | |
28 | int usage_lf_visa2k_sim(void) { |
29 | PrintAndLog("Enables simulation of visa2k card with specified card number."); |
30 | PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); |
31 | PrintAndLog(""); |
32 | PrintAndLog("Usage: lf visa2k sim [h] <card ID>"); |
33 | PrintAndLog("Options:"); |
34 | PrintAndLog(" h : This help"); |
35 | PrintAndLog(" <card ID> : Visa2k card ID"); |
36 | PrintAndLog(""); |
37 | PrintAndLog("Sample: lf visa2k sim 112233"); |
38 | return 0; |
39 | } |
40 | |
0a7e86db |
41 | static uint8_t visa_chksum( uint32_t id ) { |
42 | uint8_t sum = 0; |
43 | for (uint8_t i = 0; i < 32; i += 4) |
44 | sum ^= (id >> i) & 0xF; |
45 | return sum & 0xF; |
46 | } |
47 | |
371535d5 |
48 | //see ASKDemod for what args are accepted |
49 | int CmdVisa2kDemod(const char *Cmd) { |
50 | |
92014925 |
51 | // save GraphBuffer - to restore it later |
52 | save_restoreGB(1); |
53 | |
54 | CmdAskEdgeDetect(""); |
55 | |
371535d5 |
56 | //ASK / Manchester |
0a7e86db |
57 | bool st = TRUE; |
371535d5 |
58 | if (!ASKDemod_ext("64 0 0", FALSE, FALSE, 1, &st)) { |
59 | if (g_debugMode) PrintAndLog("DEBUG: Error - Visa2k: ASK/Manchester Demod failed"); |
92014925 |
60 | save_restoreGB(0); |
371535d5 |
61 | return 0; |
62 | } |
63 | size_t size = DemodBufferLen; |
64 | int ans = Visa2kDemod_AM(DemodBuffer, &size); |
65 | if (ans < 0){ |
66 | if (g_debugMode){ |
67 | if (ans == -1) |
68 | PrintAndLog("DEBUG: Error - Visa2k: too few bits found"); |
69 | else if (ans == -2) |
70 | PrintAndLog("DEBUG: Error - Visa2k: preamble not found"); |
71 | else if (ans == -3) |
72 | PrintAndLog("DEBUG: Error - Visa2k: Size not correct: %d", size); |
73 | else |
74 | PrintAndLog("DEBUG: Error - Visa2k: ans: %d", ans); |
75 | } |
92014925 |
76 | save_restoreGB(0); |
371535d5 |
77 | return 0; |
78 | } |
79 | setDemodBuf(DemodBuffer, 96, ans); |
80 | |
81 | //got a good demod |
82 | uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32); |
83 | uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32); |
84 | uint32_t raw3 = bytebits_to_byte(DemodBuffer+64, 32); |
85 | |
0a7e86db |
86 | // chksum |
87 | uint8_t calc = visa_chksum(raw2); |
88 | uint8_t chk = raw3 & 0xF; |
89 | // test checksums |
90 | if ( chk != calc ) { |
91 | printf("DEBUG: error: Visa2000 checksum failed %x - %x\n", chk, calc); |
92014925 |
92 | save_restoreGB(0); |
0a7e86db |
93 | return 0; |
94 | } |
371535d5 |
95 | PrintAndLog("Visa2000 Tag Found: Card ID %u, Raw: %08X%08X%08X", raw2, raw1 ,raw2, raw3); |
92014925 |
96 | save_restoreGB(0); |
371535d5 |
97 | return 1; |
98 | } |
99 | |
100 | int CmdVisa2kRead(const char *Cmd) { |
101 | CmdLFRead("s"); |
102 | getSamples("20000",TRUE); |
103 | return CmdVisa2kDemod(Cmd); |
104 | } |
105 | |
106 | int CmdVisa2kClone(const char *Cmd) { |
107 | |
108 | uint64_t id = 0; |
0a7e86db |
109 | uint32_t blocks[4] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_64 | T55x7_ST_TERMINATOR |3<<T55x7_MAXBLOCK_SHIFT, BL0CK1, 0}; |
371535d5 |
110 | |
371535d5 |
111 | char cmdp = param_getchar(Cmd, 0); |
112 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_visa2k_clone(); |
113 | |
114 | id = param_get32ex(Cmd, 0, 0, 10); |
115 | |
116 | //Q5 |
117 | if (param_getchar(Cmd, 1) == 'Q' || param_getchar(Cmd, 1) == 'q') { |
118 | //t5555 (Q5) BITRATE = (RF-2)/2 (iceman) |
119 | blocks[0] = T5555_MODULATION_MANCHESTER | 64<<T5555_BITRATE_SHIFT | T5555_ST_TERMINATOR | 3<<T5555_MAXBLOCK_SHIFT; |
120 | } |
121 | |
371535d5 |
122 | // |
371535d5 |
123 | blocks[2] = id; |
3acac886 |
124 | blocks[3] = visa_chksum(id); |
371535d5 |
125 | |
126 | PrintAndLog("Preparing to clone Visa2000 to T55x7 with CardId: %u", id); |
127 | PrintAndLog("Blk | Data "); |
128 | PrintAndLog("----+------------"); |
3375daee |
129 | for(int i = 0; i<4; ++i) |
3acac886 |
130 | PrintAndLog(" %02d | 0x%08x", i , blocks[i]); |
371535d5 |
131 | |
132 | UsbCommand resp; |
133 | UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; |
134 | |
135 | for (int i = 3; i >= 0; --i) { |
136 | c.arg[0] = blocks[i]; |
137 | c.arg[1] = i; |
138 | clearCommandBuffer(); |
139 | SendCommand(&c); |
42c235e7 |
140 | if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){ |
371535d5 |
141 | PrintAndLog("Error occurred, device did not respond during write operation."); |
142 | return -1; |
143 | } |
144 | } |
145 | return 0; |
146 | } |
147 | |
148 | int CmdVisa2kSim(const char *Cmd) { |
149 | |
150 | uint32_t id = 0; |
151 | char cmdp = param_getchar(Cmd, 0); |
152 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_visa2k_sim(); |
153 | |
154 | id = param_get32ex(Cmd, 0, 0, 10); |
155 | |
156 | uint8_t clk = 64, encoding = 1, separator = 1, invert = 0; |
157 | uint16_t arg1, arg2; |
158 | size_t size = 96; |
159 | arg1 = clk << 8 | encoding; |
160 | arg2 = invert << 8 | separator; |
161 | |
162 | PrintAndLog("Simulating Visa2000 - CardId: %u", id); |
163 | |
164 | UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; |
371535d5 |
165 | |
0a7e86db |
166 | uint32_t blocks[3] = { BL0CK1, id, visa_chksum(id) }; |
167 | |
168 | for(int i=0; i<3; ++i) |
371535d5 |
169 | num_to_bytebits(blocks[i], 32, c.d.asBytes + i*32); |
0a7e86db |
170 | |
371535d5 |
171 | clearCommandBuffer(); |
172 | SendCommand(&c); |
173 | return 0; |
174 | } |
175 | |
176 | static command_t CommandTable[] = { |
177 | {"help", CmdHelp, 1, "This help"}, |
178 | {"read", CmdVisa2kRead, 0, "Attempt to read and extract tag data"}, |
179 | {"clone", CmdVisa2kClone, 0, "clone Visa2000 tag"}, |
180 | {"sim", CmdVisa2kSim, 0, "simulate Visa2000 tag"}, |
181 | {NULL, NULL, 0, NULL} |
182 | }; |
183 | |
184 | int CmdLFVisa2k(const char *Cmd) { |
185 | clearCommandBuffer(); |
186 | CmdsParse(CommandTable, Cmd); |
187 | return 0; |
188 | } |
189 | |
190 | int CmdHelp(const char *Cmd) { |
191 | CmdsHelp(CommandTable); |
192 | return 0; |
193 | } |