]>
Commit | Line | Data |
---|---|---|
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 | ||
fb26bb54 | 48 | static uint8_t visa_parity( uint32_t id) { |
49 | // 4bit parity LUT | |
50 | uint8_t par_lut[] = { | |
51 | 0,1,1,0 | |
52 | ,1,0,0,1 | |
53 | ,1,0,0,1 | |
54 | ,0,1,1,0 | |
55 | }; | |
56 | uint8_t par = 0; | |
dc873169 | 57 | par |= par_lut[ NIBBLE_HIGH( (id >> 24) & 0xFF) ] << 7; |
58 | par |= par_lut[ NIBBLE_LOW( (id >> 24) & 0xFF) ] << 6; | |
59 | par |= par_lut[ NIBBLE_HIGH( (id >> 16) & 0xFF) ] << 5; | |
60 | par |= par_lut[ NIBBLE_LOW( (id >> 16) & 0xFF) ] << 4; | |
fb26bb54 | 61 | par |= par_lut[ NIBBLE_HIGH( (id >> 8) & 0xFF) ] << 3; |
62 | par |= par_lut[ NIBBLE_LOW( (id >> 8) & 0xFF) ] << 2; | |
63 | par |= par_lut[ NIBBLE_HIGH( id & 0xFF ) ] << 1; | |
64 | par |= par_lut[ NIBBLE_LOW( id & 0xFF) ]; | |
65 | return par; | |
66 | } | |
67 | ||
68 | ||
ff9c043d | 69 | /** |
70 | * | |
71 | * 56495332 00096ebd 00000077 —> tag id 618173 | |
72 | * aaaaaaaa iiiiiiii -----..c | |
73 | * | |
74 | * a = fixed value ascii 'VIS2' | |
75 | * i = card id | |
76 | * c = checksum (xor of card id) | |
77 | * . = unknown | |
78 | * | |
79 | **/ | |
371535d5 | 80 | //see ASKDemod for what args are accepted |
81 | int CmdVisa2kDemod(const char *Cmd) { | |
82 | ||
92014925 | 83 | // save GraphBuffer - to restore it later |
84 | save_restoreGB(1); | |
85 | ||
4eabb8ad | 86 | //sCmdAskEdgeDetect(""); |
92014925 | 87 | |
371535d5 | 88 | //ASK / Manchester |
0a7e86db | 89 | bool st = TRUE; |
371535d5 | 90 | if (!ASKDemod_ext("64 0 0", FALSE, FALSE, 1, &st)) { |
91 | if (g_debugMode) PrintAndLog("DEBUG: Error - Visa2k: ASK/Manchester Demod failed"); | |
92014925 | 92 | save_restoreGB(0); |
371535d5 | 93 | return 0; |
94 | } | |
95 | size_t size = DemodBufferLen; | |
96 | int ans = Visa2kDemod_AM(DemodBuffer, &size); | |
97 | if (ans < 0){ | |
98 | if (g_debugMode){ | |
99 | if (ans == -1) | |
100 | PrintAndLog("DEBUG: Error - Visa2k: too few bits found"); | |
101 | else if (ans == -2) | |
102 | PrintAndLog("DEBUG: Error - Visa2k: preamble not found"); | |
103 | else if (ans == -3) | |
104 | PrintAndLog("DEBUG: Error - Visa2k: Size not correct: %d", size); | |
105 | else | |
106 | PrintAndLog("DEBUG: Error - Visa2k: ans: %d", ans); | |
107 | } | |
92014925 | 108 | save_restoreGB(0); |
371535d5 | 109 | return 0; |
110 | } | |
111 | setDemodBuf(DemodBuffer, 96, ans); | |
4eabb8ad | 112 | setGrid_Clock(64); |
113 | ||
371535d5 | 114 | //got a good demod |
115 | uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32); | |
116 | uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32); | |
117 | uint32_t raw3 = bytebits_to_byte(DemodBuffer+64, 32); | |
ff9c043d | 118 | |
0a7e86db | 119 | // chksum |
120 | uint8_t calc = visa_chksum(raw2); | |
121 | uint8_t chk = raw3 & 0xF; | |
fb26bb54 | 122 | |
0a7e86db | 123 | // test checksums |
124 | if ( chk != calc ) { | |
125 | printf("DEBUG: error: Visa2000 checksum failed %x - %x\n", chk, calc); | |
92014925 | 126 | save_restoreGB(0); |
0a7e86db | 127 | return 0; |
128 | } | |
fb26bb54 | 129 | // parity |
130 | uint8_t calc_par = visa_parity(raw2); | |
dc873169 | 131 | uint8_t chk_par = (raw3 & 0xFF0) >> 4; |
fb26bb54 | 132 | if ( calc_par != chk_par) { |
133 | printf("DEBUG: error: Visa2000 parity failed %x - %x\n", chk_par, calc_par); | |
134 | save_restoreGB(0); | |
135 | return 0; | |
136 | } | |
371535d5 | 137 | PrintAndLog("Visa2000 Tag Found: Card ID %u, Raw: %08X%08X%08X", raw2, raw1 ,raw2, raw3); |
92014925 | 138 | save_restoreGB(0); |
371535d5 | 139 | return 1; |
140 | } | |
141 | ||
672d72de | 142 | // 64*96*2=12288 samples just in case we just missed the first preamble we can still catch 2 of them |
371535d5 | 143 | int CmdVisa2kRead(const char *Cmd) { |
144 | CmdLFRead("s"); | |
672d72de | 145 | getSamples("12500",TRUE); |
371535d5 | 146 | return CmdVisa2kDemod(Cmd); |
147 | } | |
148 | ||
149 | int CmdVisa2kClone(const char *Cmd) { | |
150 | ||
151 | uint64_t id = 0; | |
ff9c043d | 152 | uint32_t blocks[4] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_64 | T55x7_ST_TERMINATOR | 3 << T55x7_MAXBLOCK_SHIFT, BL0CK1, 0}; |
371535d5 | 153 | |
371535d5 | 154 | char cmdp = param_getchar(Cmd, 0); |
155 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_visa2k_clone(); | |
156 | ||
157 | id = param_get32ex(Cmd, 0, 0, 10); | |
158 | ||
159 | //Q5 | |
160 | if (param_getchar(Cmd, 1) == 'Q' || param_getchar(Cmd, 1) == 'q') { | |
161 | //t5555 (Q5) BITRATE = (RF-2)/2 (iceman) | |
ff9c043d | 162 | blocks[0] = T5555_MODULATION_MANCHESTER | ((64-2)>>1) << T5555_BITRATE_SHIFT | T5555_ST_TERMINATOR | 3 << T5555_MAXBLOCK_SHIFT; |
371535d5 | 163 | } |
164 | ||
371535d5 | 165 | // |
371535d5 | 166 | blocks[2] = id; |
fb26bb54 | 167 | blocks[3] = (visa_parity(id) << 4) | visa_chksum(id); |
371535d5 | 168 | |
169 | PrintAndLog("Preparing to clone Visa2000 to T55x7 with CardId: %u", id); | |
170 | PrintAndLog("Blk | Data "); | |
171 | PrintAndLog("----+------------"); | |
3375daee | 172 | for(int i = 0; i<4; ++i) |
3acac886 | 173 | PrintAndLog(" %02d | 0x%08x", i , blocks[i]); |
371535d5 | 174 | |
175 | UsbCommand resp; | |
176 | UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; | |
177 | ||
178 | for (int i = 3; i >= 0; --i) { | |
179 | c.arg[0] = blocks[i]; | |
180 | c.arg[1] = i; | |
181 | clearCommandBuffer(); | |
182 | SendCommand(&c); | |
42c235e7 | 183 | if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){ |
371535d5 | 184 | PrintAndLog("Error occurred, device did not respond during write operation."); |
185 | return -1; | |
186 | } | |
187 | } | |
188 | return 0; | |
189 | } | |
190 | ||
191 | int CmdVisa2kSim(const char *Cmd) { | |
192 | ||
193 | uint32_t id = 0; | |
194 | char cmdp = param_getchar(Cmd, 0); | |
195 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_visa2k_sim(); | |
196 | ||
197 | id = param_get32ex(Cmd, 0, 0, 10); | |
198 | ||
199 | uint8_t clk = 64, encoding = 1, separator = 1, invert = 0; | |
200 | uint16_t arg1, arg2; | |
201 | size_t size = 96; | |
202 | arg1 = clk << 8 | encoding; | |
203 | arg2 = invert << 8 | separator; | |
204 | ||
205 | PrintAndLog("Simulating Visa2000 - CardId: %u", id); | |
206 | ||
207 | UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; | |
371535d5 | 208 | |
fb26bb54 | 209 | uint32_t blocks[3] = { BL0CK1, id, (visa_parity(id) << 4) | visa_chksum(id) }; |
0a7e86db | 210 | |
672d72de | 211 | for(int i = 0; i < 3; ++i) |
371535d5 | 212 | num_to_bytebits(blocks[i], 32, c.d.asBytes + i*32); |
0a7e86db | 213 | |
371535d5 | 214 | clearCommandBuffer(); |
215 | SendCommand(&c); | |
216 | return 0; | |
217 | } | |
218 | ||
219 | static command_t CommandTable[] = { | |
220 | {"help", CmdHelp, 1, "This help"}, | |
672d72de | 221 | {"demod", CmdVisa2kDemod, 1, "Attempt to demod from GraphBuffer"}, |
371535d5 | 222 | {"read", CmdVisa2kRead, 0, "Attempt to read and extract tag data"}, |
223 | {"clone", CmdVisa2kClone, 0, "clone Visa2000 tag"}, | |
224 | {"sim", CmdVisa2kSim, 0, "simulate Visa2000 tag"}, | |
225 | {NULL, NULL, 0, NULL} | |
226 | }; | |
227 | ||
228 | int CmdLFVisa2k(const char *Cmd) { | |
229 | clearCommandBuffer(); | |
230 | CmdsParse(CommandTable, Cmd); | |
231 | return 0; | |
232 | } | |
233 | ||
234 | int CmdHelp(const char *Cmd) { | |
235 | CmdsHelp(CommandTable); | |
236 | return 0; | |
237 | } |