1 //-----------------------------------------------------------------------------
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
6 //-----------------------------------------------------------------------------
7 // Low frequency Presco tag commands
8 //-----------------------------------------------------------------------------
11 #include "cmdlfpresco.h"
12 static int CmdHelp(const char *Cmd
);
14 int usage_lf_presco_clone(void){
15 PrintAndLog("clone a Presco tag to a T55x7 tag.");
16 PrintAndLog("Usage: lf presco clone d <Card-ID> H <hex-ID> <Q5>");
17 PrintAndLog("Options :");
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)");
22 PrintAndLog("Sample : lf presco clone d 123456789");
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.");
31 PrintAndLog("Usage: lf presco sim d <Card-ID> or H <hex-ID>");
32 PrintAndLog("Options :");
33 PrintAndLog(" d <Card-ID> : 9 digit presco card number");
34 PrintAndLog(" H <hex-ID> : 8 digit hex card number");
36 PrintAndLog("Sample : lf presco sim d 123456789");
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
) {
44 bool hex
= false, errors
= false;
48 memset(id
, 0x00, sizeof(id
));
50 while(param_getchar(Cmd
, cmdp
) != 0x00) {
51 switch(param_getchar(Cmd
, cmdp
)) {
57 *fullcode
= param_get32ex(Cmd
, cmdp
+1, 0, 10);
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;
73 PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd
, cmdp
));
80 if(cmdp
== 0) errors
= 1;
86 for (int index
=0; index
< strlen(id
); ++index
) {
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;
96 // last digit is only added, not multipled.
97 if ( index
< strlen(id
)-1 )
102 *usercode
= *fullcode
& 0x0000FFFF; //% 65566
103 *sitecode
= (*fullcode
>> 24) & 0x000000FF; // /= 16777216;
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);
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");
122 size_t size
= DemodBufferLen
;
123 //call lfdemod.c demod for Viking
124 int ans
= PrescoDemod(DemodBuffer
, &size
);
126 if (g_debugMode
) PrintAndLog("Error Presco_Demod %d", ans
);
130 uint32_t raw1
= bytebits_to_byte(DemodBuffer
+ans
, 32);
131 uint32_t raw2
= bytebits_to_byte(DemodBuffer
+ans
+32, 32);
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
;
135 PrintAndLog("Presco Tag Found: Card ID %08X", cardid
);
136 PrintAndLog("Raw: %08X%08X%08X%08X", raw1
,raw2
,raw3
,raw4
);
137 setDemodBuf(DemodBuffer
+ans
, 128, 0);
139 uint32_t sitecode
= 0, usercode
= 0, fullcode
= 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
);
149 //see ASKDemod for what args are accepted
150 int CmdPrescoRead(const char *Cmd
) {
151 // Presco Number: 123456789 --> Sitecode 30 | usercode 8665
155 // get samples silently
156 getSamples("20000", TRUE
);
157 // demod and output Presco ID
158 return CmdPrescoDemod(Cmd
);
161 // takes base 12 ID converts to hex
162 // Or takes 8 digit hex ID
163 int CmdPrescoClone(const char *Cmd
) {
166 uint32_t sitecode
=0, usercode
=0, fullcode
=0;
167 uint32_t blocks
[5] = {T55x7_MODULATION_MANCHESTER
| T55x7_BITRATE_RF_32
| 4<<T55x7_MAXBLOCK_SHIFT
| T55x7_ST_TERMINATOR
, 0, 0, 0, 5};
169 // get wiegand from printed number.
170 if (GetWiegandFromPresco(Cmd
, &sitecode
, &usercode
, &fullcode
, &Q5
) == -1) return usage_lf_presco_clone();
173 //t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
174 blocks
[0] = T5555_MODULATION_MANCHESTER
| 32<<T5555_BITRATE_SHIFT
| 4<<T5555_MAXBLOCK_SHIFT
| T5555_ST_TERMINATOR
;
176 if ((sitecode
& 0xFF) != sitecode
) {
178 PrintAndLog("Facility-Code Truncated to 8-bits (Presco): %u", sitecode
);
181 if ((usercode
& 0xFFFF) != usercode
) {
183 PrintAndLog("Card Number Truncated to 16-bits (Presco): %u", usercode
);
186 blocks
[1] = 0x10D00000; //preamble
187 blocks
[2] = 0x00000000;
188 blocks
[3] = 0x00000000;
189 blocks
[4] = fullcode
;
191 PrintAndLog("Preparing to clone Presco to T55x7 with SiteCode: %u, UserCode: %u, FullCode: %08x", sitecode
, usercode
, fullcode
);
192 PrintAndLog("Blk | Data ");
193 PrintAndLog("----+------------");
194 PrintAndLog(" 00 | 0x%08x", blocks
[0]);
195 PrintAndLog(" 01 | 0x%08x", blocks
[1]);
196 PrintAndLog(" 02 | 0x%08x", blocks
[2]);
197 PrintAndLog(" 03 | 0x%08x", blocks
[3]);
198 PrintAndLog(" 04 | 0x%08x", blocks
[4]);
201 UsbCommand c
= {CMD_T55XX_WRITE_BLOCK
, {0,0,0}};
203 for (int i
=4; i
>=0; i
--) {
204 c
.arg
[0] = blocks
[i
];
206 clearCommandBuffer();
208 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1000)){
209 PrintAndLog("Error occurred, device did not respond during write operation.");
216 // takes base 12 ID converts to hex
217 // Or takes 8 digit hex ID
218 int CmdPrescoSim(const char *Cmd
) {
219 uint32_t sitecode
=0, usercode
=0, fullcode
=0;
221 // get wiegand from printed number.
222 if (GetWiegandFromPresco(Cmd
, &sitecode
, &usercode
, &fullcode
, &Q5
) == -1) return usage_lf_presco_sim();
224 uint8_t clk
= 32, encoding
= 1, separator
= 1, invert
= 0;
227 arg1
= clk
<< 8 | encoding
;
228 arg2
= invert
<< 8 | separator
;
230 PrintAndLog("Simulating Presco - SiteCode: %u, UserCode: %u, FullCode: %08X",sitecode
, usercode
, fullcode
);
232 UsbCommand c
= {CMD_ASK_SIM_TAG
, {arg1
, arg2
, size
}};
233 GetPrescoBits(fullcode
, c
.d
.asBytes
);
234 clearCommandBuffer();
239 static command_t CommandTable
[] = {
240 {"help", CmdHelp
, 1, "This help"},
241 {"read", CmdPrescoRead
, 0, "Attempt to read and Extract tag data"},
242 {"clone", CmdPrescoClone
, 0, "d <9 digit ID> or h <hex> [Q5] clone presco tag"},
243 {"sim", CmdPrescoSim
, 0, "d <9 digit ID> or h <hex> simulate presco tag"},
244 {NULL
, NULL
, 0, NULL
}
247 int CmdLFPresco(const char *Cmd
) {
248 clearCommandBuffer();
249 CmdsParse(CommandTable
, Cmd
);
253 int CmdHelp(const char *Cmd
) {
254 CmdsHelp(CommandTable
);