]>
Commit | Line | Data |
---|---|---|
5a6e19e6 | 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 | #include <string.h> | |
10 | #include <inttypes.h> | |
11 | #include "cmdlfpresco.h" | |
12 | static int CmdHelp(const char *Cmd); | |
13 | ||
14 | int usage_lf_presco_clone(void){ | |
15 | PrintAndLog("clone a Presco tag to a T55x7 tag."); | |
6c68b84a | 16 | PrintAndLog("Usage: lf presco clone d <Card-ID> H <hex-ID> <Q5>"); |
5a6e19e6 | 17 | PrintAndLog("Options :"); |
6c68b84a | 18 | PrintAndLog(" d <Card-ID> : 9 digit presco card ID"); |
19 | PrintAndLog(" H <hex-ID> : 8 digit hex card number"); | |
20 | PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)"); | |
5a6e19e6 | 21 | PrintAndLog(""); |
6c68b84a | 22 | PrintAndLog("Sample : lf presco clone d 123456789"); |
5a6e19e6 | 23 | return 0; |
24 | } | |
25 | ||
26 | int usage_lf_presco_sim(void) { | |
27 | PrintAndLog("Enables simulation of presco card with specified card number."); | |
28 | PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); | |
29 | PrintAndLog("Per presco format, the card number is 9 digit number and can contain *# chars. Larger values are truncated."); | |
30 | PrintAndLog(""); | |
6c68b84a | 31 | PrintAndLog("Usage: lf presco sim d <Card-ID> or H <hex-ID>"); |
5a6e19e6 | 32 | PrintAndLog("Options :"); |
6c68b84a | 33 | PrintAndLog(" d <Card-ID> : 9 digit presco card number"); |
34 | PrintAndLog(" H <hex-ID> : 8 digit hex card number"); | |
5a6e19e6 | 35 | PrintAndLog(""); |
6c68b84a | 36 | PrintAndLog("Sample : lf presco sim d 123456789"); |
5a6e19e6 | 37 | return 0; |
38 | } | |
39 | ||
6c68b84a | 40 | // convert base 12 ID to sitecode & usercode & 8 bit other unknown code |
41 | int GetWiegandFromPresco(const char *Cmd, uint32_t *sitecode, uint32_t *usercode, uint32_t *fullcode, bool *Q5) { | |
5a6e19e6 | 42 | |
43 | uint8_t val = 0; | |
6c68b84a | 44 | bool hex = false, errors = false; |
45 | uint8_t cmdp = 0; | |
46 | char id[11]; | |
47 | int stringlen = 0; | |
06eb3b1a | 48 | memset(id, 0x00, sizeof(id)); |
49 | ||
6c68b84a | 50 | while(param_getchar(Cmd, cmdp) != 0x00) { |
51 | switch(param_getchar(Cmd, cmdp)) { | |
52 | case 'h': | |
53 | return -1; | |
54 | case 'H': | |
55 | hex = true; | |
56 | //get hex | |
57 | *fullcode = param_get32ex(Cmd, cmdp+1, 0, 10); | |
58 | cmdp+=2; | |
59 | break; | |
60 | case 'P': | |
61 | case 'p': | |
62 | //param get string int param_getstr(const char *line, int paramnum, char * str) | |
63 | stringlen = param_getstr(Cmd, cmdp+1, id); | |
64 | if (stringlen < 2) return -1; | |
65 | cmdp+=2; | |
66 | break; | |
67 | case 'Q': | |
68 | case 'q': | |
69 | *Q5 = true; | |
70 | cmdp++; | |
71 | break; | |
72 | default: | |
73 | PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp)); | |
74 | errors = 1; | |
75 | break; | |
76 | } | |
77 | if(errors) break; | |
78 | } | |
79 | // No args | |
80 | if(cmdp == 0) errors = 1; | |
81 | ||
82 | //Validations | |
83 | if(errors) return -1; | |
84 | ||
85 | if (!hex) { | |
06eb3b1a | 86 | for (int index =0; index < strlen(id); ++index) { |
5a6e19e6 | 87 | |
06eb3b1a | 88 | // Get value from number string. |
89 | if ( id[index] == '*' ) val = 10; | |
90 | if ( id[index] == '#') val = 11; | |
91 | if ( id[index] >= 0x30 && id[index] <= 0x39 ) | |
92 | val = id[index] - 0x30; | |
93 | ||
94 | *fullcode += val; | |
95 | ||
96 | // last digit is only added, not multipled. | |
97 | if ( index < strlen(id)-1 ) | |
98 | *fullcode *= 12; | |
6c68b84a | 99 | } |
5a6e19e6 | 100 | } |
6c68b84a | 101 | |
102 | *usercode = *fullcode & 0x0000FFFF; //% 65566 | |
103 | *sitecode = (*fullcode >> 24) & 0x000000FF; // /= 16777216; | |
5a6e19e6 | 104 | return 0; |
105 | } | |
106 | ||
6c68b84a | 107 | // calc not certain - intended to get bitstream for programming / sim |
108 | int GetPrescoBits(uint32_t fullcode, uint8_t *prescoBits) { | |
109 | num_to_bytebits(0x10D00000, 32, prescoBits); | |
110 | num_to_bytebits(0x00000000, 32, prescoBits+32); | |
111 | num_to_bytebits(0x00000000, 32, prescoBits+64); | |
112 | num_to_bytebits(fullcode , 32, prescoBits+96); | |
5a6e19e6 | 113 | return 1; |
114 | } | |
6c68b84a | 115 | |
4469412e | 116 | //see ASKDemod for what args are accepted |
117 | int CmdPrescoDemod(const char *Cmd) { | |
118 | if (!ASKDemod(Cmd, false, false, 1)) { | |
119 | if (g_debugMode) PrintAndLog("ASKDemod failed"); | |
120 | return 0; | |
121 | } | |
122 | size_t size = DemodBufferLen; | |
123 | //call lfdemod.c demod for Viking | |
124 | int ans = PrescoDemod(DemodBuffer, &size); | |
125 | if (ans < 0) { | |
126 | if (g_debugMode) PrintAndLog("Error Presco_Demod %d", ans); | |
127 | return 0; | |
128 | } | |
129 | //got a good demod | |
130 | uint32_t raw1 = bytebits_to_byte(DemodBuffer+ans, 32); | |
131 | uint32_t raw2 = bytebits_to_byte(DemodBuffer+ans+32, 32); | |
6c68b84a | 132 | uint32_t raw3 = bytebits_to_byte(DemodBuffer+ans+64, 32); |
133 | uint32_t raw4 = bytebits_to_byte(DemodBuffer+ans+96, 32); | |
134 | uint32_t cardid = raw4; | |
4469412e | 135 | PrintAndLog("Presco Tag Found: Card ID %08X", cardid); |
6c68b84a | 136 | PrintAndLog("Raw: %08X%08X%08X%08X", raw1,raw2,raw3,raw4); |
137 | setDemodBuf(DemodBuffer+ans, 128, 0); | |
4469412e | 138 | |
6c68b84a | 139 | uint32_t sitecode = 0, usercode = 0, fullcode = 0; |
140 | bool Q5=false; | |
141 | char cmd[12] = {0}; | |
142 | sprintf(cmd, "H %08X", cardid); | |
143 | GetWiegandFromPresco(cmd, &sitecode, &usercode, &fullcode, &Q5); | |
144 | PrintAndLog("SiteCode %u, UserCode %u, FullCode, %08X", sitecode, usercode, fullcode); | |
4469412e | 145 | |
146 | return 1; | |
147 | } | |
5a6e19e6 | 148 | |
149 | //see ASKDemod for what args are accepted | |
150 | int CmdPrescoRead(const char *Cmd) { | |
4469412e | 151 | // Presco Number: 123456789 --> Sitecode 30 | usercode 8665 |
5a6e19e6 | 152 | |
153 | // read lf silently | |
4469412e | 154 | CmdLFRead("s"); |
5a6e19e6 | 155 | // get samples silently |
4469412e | 156 | getSamples("30000",false); |
157 | // demod and output Presco ID | |
158 | return CmdPrescoDemod(Cmd); | |
5a6e19e6 | 159 | } |
160 | ||
6c68b84a | 161 | // takes base 12 ID converts to hex |
162 | // Or takes 8 digit hex ID | |
5a6e19e6 | 163 | int CmdPrescoClone(const char *Cmd) { |
164 | ||
6c68b84a | 165 | bool Q5 = false; |
166 | uint32_t sitecode=0, usercode=0, fullcode=0; | |
5a6e19e6 | 167 | uint32_t blocks[5] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_32 | 4<<T55x7_MAXBLOCK_SHIFT | T55x7_ST_TERMINATOR, 0, 0, 0, 5}; |
168 | ||
6c68b84a | 169 | // get wiegand from printed number. |
170 | if (GetWiegandFromPresco(Cmd, &sitecode, &usercode, &fullcode, &Q5) == -1) return usage_lf_presco_clone(); | |
171 | ||
172 | if (Q5) | |
5a6e19e6 | 173 | blocks[0] = T5555_MODULATION_MANCHESTER | 32<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT | T5555_ST_TERMINATOR; |
174 | ||
5a6e19e6 | 175 | if ((sitecode & 0xFF) != sitecode) { |
176 | sitecode &= 0xFF; | |
177 | PrintAndLog("Facility-Code Truncated to 8-bits (Presco): %u", sitecode); | |
178 | } | |
179 | ||
180 | if ((usercode & 0xFFFF) != usercode) { | |
181 | usercode &= 0xFFFF; | |
182 | PrintAndLog("Card Number Truncated to 16-bits (Presco): %u", usercode); | |
183 | } | |
184 | ||
6c68b84a | 185 | blocks[1] = 0x10D00000; //preamble |
186 | blocks[2] = 0x00000000; | |
187 | blocks[3] = 0x00000000; | |
188 | blocks[4] = fullcode; | |
5a6e19e6 | 189 | |
6c68b84a | 190 | PrintAndLog("Preparing to clone Presco to T55x7 with SiteCode: %u, UserCode: %u, FullCode: %08x", sitecode, usercode, fullcode); |
5a6e19e6 | 191 | PrintAndLog("Blk | Data "); |
192 | PrintAndLog("----+------------"); | |
193 | PrintAndLog(" 00 | 0x%08x", blocks[0]); | |
194 | PrintAndLog(" 01 | 0x%08x", blocks[1]); | |
195 | PrintAndLog(" 02 | 0x%08x", blocks[2]); | |
196 | PrintAndLog(" 03 | 0x%08x", blocks[3]); | |
197 | PrintAndLog(" 04 | 0x%08x", blocks[4]); | |
198 | ||
6c68b84a | 199 | UsbCommand resp; |
200 | UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; | |
201 | ||
202 | for (int i=4; i>=0; i--) { | |
203 | c.arg[0] = blocks[i]; | |
204 | c.arg[1] = i; | |
205 | clearCommandBuffer(); | |
206 | SendCommand(&c); | |
207 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){ | |
208 | PrintAndLog("Error occurred, device did not respond during write operation."); | |
209 | return -1; | |
210 | } | |
211 | } | |
5a6e19e6 | 212 | return 0; |
213 | } | |
214 | ||
6c68b84a | 215 | // takes base 12 ID converts to hex |
216 | // Or takes 8 digit hex ID | |
5a6e19e6 | 217 | int CmdPrescoSim(const char *Cmd) { |
6c68b84a | 218 | uint32_t sitecode=0, usercode=0, fullcode=0; |
219 | bool Q5=false; | |
220 | // get wiegand from printed number. | |
221 | if (GetWiegandFromPresco(Cmd, &sitecode, &usercode, &fullcode, &Q5) == -1) return usage_lf_presco_sim(); | |
5a6e19e6 | 222 | |
6c68b84a | 223 | uint8_t clk = 32, encoding = 1, separator = 1, invert = 0; |
224 | uint16_t arg1, arg2; | |
225 | size_t size = 128; | |
226 | arg1 = clk << 8 | encoding; | |
227 | arg2 = invert << 8 | separator; | |
5a6e19e6 | 228 | |
6c68b84a | 229 | PrintAndLog("Simulating Presco - SiteCode: %u, UserCode: %u, FullCode: %08X",sitecode, usercode, fullcode); |
5a6e19e6 | 230 | |
6c68b84a | 231 | UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; |
232 | GetPrescoBits(fullcode, c.d.asBytes); | |
233 | clearCommandBuffer(); | |
234 | SendCommand(&c); | |
5a6e19e6 | 235 | return 0; |
236 | } | |
237 | ||
238 | static command_t CommandTable[] = { | |
239 | {"help", CmdHelp, 1, "This help"}, | |
240 | {"read", CmdPrescoRead, 0, "Attempt to read and Extract tag data"}, | |
6c68b84a | 241 | {"clone", CmdPrescoClone, 0, "d <9 digit ID> or h <hex> [Q5] clone presco tag"}, |
242 | {"sim", CmdPrescoSim, 0, "d <9 digit ID> or h <hex> simulate presco tag"}, | |
5a6e19e6 | 243 | {NULL, NULL, 0, NULL} |
244 | }; | |
245 | ||
246 | int CmdLFPresco(const char *Cmd) { | |
4c36581b | 247 | clearCommandBuffer(); |
5a6e19e6 | 248 | CmdsParse(CommandTable, Cmd); |
249 | return 0; | |
250 | } | |
251 | ||
252 | int CmdHelp(const char *Cmd) { | |
253 | CmdsHelp(CommandTable); | |
254 | return 0; | |
255 | } |