]>
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."); | |
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)"); | |
20 | PrintAndLog(""); | |
21 | PrintAndLog("Sample : lf presco clone 123456789"); | |
22 | return 0; | |
23 | } | |
24 | ||
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."); | |
29 | PrintAndLog(""); | |
30 | PrintAndLog("Usage: lf presco sim <Card-Number>"); | |
31 | PrintAndLog("Options :"); | |
32 | PrintAndLog(" <Card Number> : 9 digit presco card number"); | |
33 | PrintAndLog(""); | |
34 | PrintAndLog("Sample : lf presco sim 123456789"); | |
35 | return 0; | |
36 | } | |
37 | ||
38 | // calc checksum | |
39 | int GetWiegandFromPresco(const char *id, uint32_t *sitecode, uint32_t *usercode) { | |
40 | ||
41 | uint8_t val = 0; | |
42 | for (int index =0; index < strlen(id); ++index) { | |
43 | ||
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; | |
49 | ||
50 | *sitecode += val; | |
51 | ||
52 | // last digit is only added, not multipled. | |
53 | if ( index < strlen(id)-1 ) | |
54 | *sitecode *= 12; | |
55 | } | |
56 | *usercode = *sitecode % 65536; | |
57 | *sitecode /= 16777216; | |
58 | return 0; | |
59 | } | |
60 | ||
61 | int GetPrescoBits(uint32_t sitecode, uint32_t usercode, uint8_t *prescoBits) { | |
62 | uint8_t pre[66]; | |
63 | memset(pre, 0, sizeof(pre)); | |
64 | prescoBits[7]=1; | |
65 | num_to_bytebits(26, 8, pre); | |
66 | ||
67 | uint8_t wiegand[24]; | |
68 | num_to_bytebits(sitecode, 8, wiegand); | |
69 | num_to_bytebits(usercode, 16, wiegand+8); | |
70 | ||
71 | wiegand_add_parity(pre+8, wiegand, 24); | |
72 | size_t bitLen = addParity(pre, prescoBits+8, 66, 4, 1); | |
73 | ||
74 | if (bitLen != 88) return 0; | |
75 | return 1; | |
76 | } | |
77 | ||
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"); | |
82 | ||
83 | // read lf silently | |
84 | //CmdLFRead("s"); | |
85 | // get samples silently | |
86 | //getSamples("30000",false); | |
87 | // demod and output viking ID | |
88 | //return CmdVikingDemod(Cmd); | |
89 | return 0; | |
90 | } | |
91 | ||
92 | int CmdPrescoClone(const char *Cmd) { | |
93 | ||
94 | char cmdp = param_getchar(Cmd, 0); | |
95 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_presco_clone(); | |
96 | ||
97 | uint32_t sitecode=0, usercode=0; | |
98 | uint8_t bits[96]; | |
99 | uint8_t *bs = bits; | |
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}; | |
102 | ||
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; | |
105 | ||
106 | // get wiegand from printed number. | |
107 | GetWiegandFromPresco(Cmd, &sitecode, &usercode); | |
108 | ||
109 | if ((sitecode & 0xFF) != sitecode) { | |
110 | sitecode &= 0xFF; | |
111 | PrintAndLog("Facility-Code Truncated to 8-bits (Presco): %u", sitecode); | |
112 | } | |
113 | ||
114 | if ((usercode & 0xFFFF) != usercode) { | |
115 | usercode &= 0xFFFF; | |
116 | PrintAndLog("Card Number Truncated to 16-bits (Presco): %u", usercode); | |
117 | } | |
118 | ||
119 | if ( !GetPrescoBits(sitecode, usercode, bs)) { | |
120 | PrintAndLog("Error with tag bitstream generation."); | |
121 | return 1; | |
122 | } | |
123 | ||
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); | |
128 | ||
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]); | |
137 | ||
138 | // UsbCommand resp; | |
139 | // UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; | |
140 | ||
141 | // for (uint8_t i=0; i<5; i++) { | |
142 | // c.arg[0] = blocks[i]; | |
143 | // c.arg[1] = i; | |
144 | // clearCommandBuffer(); | |
145 | // SendCommand(&c); | |
146 | // if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){ | |
147 | // PrintAndLog("Error occurred, device did not respond during write operation."); | |
148 | // return -1; | |
149 | // } | |
150 | // } | |
151 | return 0; | |
152 | } | |
153 | ||
154 | int CmdPrescoSim(const char *Cmd) { | |
155 | // uint32_t id = 0; | |
156 | // uint64_t rawID = 0; | |
157 | // uint8_t clk = 32, encoding = 1, separator = 0, invert = 0; | |
158 | ||
159 | // char cmdp = param_getchar(Cmd, 0); | |
160 | // if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_presco_sim(); | |
161 | ||
162 | // id = param_get32ex(Cmd, 0, 0, 16); | |
163 | // if (id == 0) return usage_lf_presco_sim(); | |
164 | ||
165 | //rawID = getVikingBits(id); | |
166 | ||
167 | // uint16_t arg1, arg2; | |
168 | // size_t size = 64; | |
169 | // arg1 = clk << 8 | encoding; | |
170 | // arg2 = invert << 8 | separator; | |
171 | ||
172 | // PrintAndLog("Simulating - ID: %08X, Raw: %08X%08X",id,(uint32_t)(rawID >> 32),(uint32_t) (rawID & 0xFFFFFFFF)); | |
173 | ||
174 | // UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; | |
175 | // num_to_bytebits(rawID, size, c.d.asBytes); | |
176 | // clearCommandBuffer(); | |
177 | // SendCommand(&c); | |
178 | return 0; | |
179 | } | |
180 | ||
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} | |
187 | }; | |
188 | ||
189 | int CmdLFPresco(const char *Cmd) { | |
190 | CmdsParse(CommandTable, Cmd); | |
191 | return 0; | |
192 | } | |
193 | ||
194 | int CmdHelp(const char *Cmd) { | |
195 | CmdsHelp(CommandTable); | |
196 | return 0; | |
197 | } |