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 <Card ID - 9 digits> <Q5>");
17 PrintAndLog("Options :");
18 PrintAndLog(" <Card Number> : 9 digit presco card number");
19 //PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)");
21 PrintAndLog("Sample : lf presco clone 123456789");
25 int usage_lf_presco_sim(void) {
26 PrintAndLog("Enables simulation of presco card with specified card number.");
27 PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
28 PrintAndLog("Per presco format, the card number is 9 digit number and can contain *# chars. Larger values are truncated.");
30 PrintAndLog("Usage: lf presco sim <Card-Number>");
31 PrintAndLog("Options :");
32 PrintAndLog(" <Card Number> : 9 digit presco card number");
34 PrintAndLog("Sample : lf presco sim 123456789");
39 int GetWiegandFromPresco(const char *id
, uint32_t *sitecode
, uint32_t *usercode
) {
42 for (int index
=0; index
< strlen(id
); ++index
) {
44 // Get value from number string.
45 if ( id
[index
] == '*' ) val
= 10;
46 if ( id
[index
] == '#') val
= 11;
47 if ( id
[index
] >= 0x30 && id
[index
] <= 0x39 )
48 val
= id
[index
] - 0x30;
52 // last digit is only added, not multipled.
53 if ( index
< strlen(id
)-1 )
56 *usercode
= *sitecode
% 65536;
57 *sitecode
/= 16777216;
61 int GetPrescoBits(uint32_t sitecode
, uint32_t usercode
, uint8_t *prescoBits
) {
63 memset(pre
, 0, sizeof(pre
));
65 num_to_bytebits(26, 8, pre
);
68 num_to_bytebits(sitecode
, 8, wiegand
);
69 num_to_bytebits(usercode
, 16, wiegand
+8);
71 wiegand_add_parity(pre
+8, wiegand
, 24);
72 size_t bitLen
= addParity(pre
, prescoBits
+8, 66, 4, 1);
74 if (bitLen
!= 88) return 0;
78 //see ASKDemod for what args are accepted
79 int CmdPrescoRead(const char *Cmd
) {
80 PrintAndLog("Number: 123456789 --> Sitecode 30 | usercode 8665");
81 // GetWiegandFromPresco("123456789");
85 // get samples silently
86 //getSamples("30000",false);
87 // demod and output viking ID
88 //return CmdVikingDemod(Cmd);
92 int CmdPrescoClone(const char *Cmd
) {
94 char cmdp
= param_getchar(Cmd
, 0);
95 if (strlen(Cmd
) == 0 || cmdp
== 'h' || cmdp
== 'H') return usage_lf_presco_clone();
97 uint32_t sitecode
=0, usercode
=0;
100 memset(bs
,0,sizeof(bits
));
101 uint32_t blocks
[5] = {T55x7_MODULATION_MANCHESTER
| T55x7_BITRATE_RF_32
| 4<<T55x7_MAXBLOCK_SHIFT
| T55x7_ST_TERMINATOR
, 0, 0, 0, 5};
103 if (param_getchar(Cmd
, 3) == 'Q' || param_getchar(Cmd
, 3) == 'q')
104 blocks
[0] = T5555_MODULATION_MANCHESTER
| 32<<T5555_BITRATE_SHIFT
| 4<<T5555_MAXBLOCK_SHIFT
| T5555_ST_TERMINATOR
;
106 // get wiegand from printed number.
107 GetWiegandFromPresco(Cmd
, &sitecode
, &usercode
);
109 if ((sitecode
& 0xFF) != sitecode
) {
111 PrintAndLog("Facility-Code Truncated to 8-bits (Presco): %u", sitecode
);
114 if ((usercode
& 0xFFFF) != usercode
) {
116 PrintAndLog("Card Number Truncated to 16-bits (Presco): %u", usercode
);
119 if ( !GetPrescoBits(sitecode
, usercode
, bs
)) {
120 PrintAndLog("Error with tag bitstream generation.");
124 blocks
[1] = bytebits_to_byte(bs
,32);
125 blocks
[2] = bytebits_to_byte(bs
+32,32);
126 blocks
[3] = bytebits_to_byte(bs
+64,32);
127 blocks
[4] = bytebits_to_byte(bs
+96,32);
129 PrintAndLog("Preparing to clone Presco to T55x7 with SiteCode: %u, UserCode: %u", sitecode
, usercode
);
130 PrintAndLog("Blk | Data ");
131 PrintAndLog("----+------------");
132 PrintAndLog(" 00 | 0x%08x", blocks
[0]);
133 PrintAndLog(" 01 | 0x%08x", blocks
[1]);
134 PrintAndLog(" 02 | 0x%08x", blocks
[2]);
135 PrintAndLog(" 03 | 0x%08x", blocks
[3]);
136 PrintAndLog(" 04 | 0x%08x", blocks
[4]);
139 // UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
141 // for (uint8_t i=0; i<5; i++) {
142 // c.arg[0] = blocks[i];
144 // clearCommandBuffer();
146 // if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){
147 // PrintAndLog("Error occurred, device did not respond during write operation.");
154 int CmdPrescoSim(const char *Cmd
) {
156 // uint64_t rawID = 0;
157 // uint8_t clk = 32, encoding = 1, separator = 0, invert = 0;
159 // char cmdp = param_getchar(Cmd, 0);
160 // if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_presco_sim();
162 // id = param_get32ex(Cmd, 0, 0, 16);
163 // if (id == 0) return usage_lf_presco_sim();
165 //rawID = getVikingBits(id);
167 // uint16_t arg1, arg2;
169 // arg1 = clk << 8 | encoding;
170 // arg2 = invert << 8 | separator;
172 // PrintAndLog("Simulating - ID: %08X, Raw: %08X%08X",id,(uint32_t)(rawID >> 32),(uint32_t) (rawID & 0xFFFFFFFF));
174 // UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
175 // num_to_bytebits(rawID, size, c.d.asBytes);
176 // clearCommandBuffer();
181 static command_t CommandTable
[] = {
182 {"help", CmdHelp
, 1, "This help"},
183 {"read", CmdPrescoRead
, 0, "Attempt to read and Extract tag data"},
184 {"clone", CmdPrescoClone
, 0, "<8 digit ID number> clone presco tag"},
185 // {"sim", CmdPrescoSim, 0, "<8 digit ID number> simulate presco tag"},
186 {NULL
, NULL
, 0, NULL
}
189 int CmdLFPresco(const char *Cmd
) {
190 CmdsParse(CommandTable
, Cmd
);
194 int CmdHelp(const char *Cmd
) {
195 CmdsHelp(CommandTable
);