]>
Commit | Line | Data |
---|---|---|
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 Farpoint / Pyramid tag commands | |
8 | //----------------------------------------------------------------------------- | |
9 | #include <string.h> | |
10 | #include <inttypes.h> | |
11 | #include "cmdlfpyramid.h" | |
12 | static int CmdHelp(const char *Cmd); | |
13 | ||
14 | int usage_lf_pyramid_clone(void){ | |
15 | PrintAndLog("clone a Farpointe/Pyramid tag to a T55x7 tag."); | |
16 | PrintAndLog("The facility-code is 8-bit and the card number is 16-bit. Larger values are truncated. "); | |
17 | PrintAndLog("Currently work only on 26bit"); | |
18 | PrintAndLog(""); | |
19 | PrintAndLog("Usage: lf pyramid clone <Facility-Code> <Card-Number>"); | |
20 | PrintAndLog("Options :"); | |
21 | PrintAndLog(" <Facility-Code> : 8-bit value facility code"); | |
22 | PrintAndLog(" <Card Number> : 16-bit value card number"); | |
23 | PrintAndLog(""); | |
24 | PrintAndLog("Sample : lf pyramid clone 123 11223"); | |
25 | return 0; | |
26 | } | |
27 | ||
28 | int usage_lf_pyramid_sim(void) { | |
29 | PrintAndLog("Enables simulation of Farpointe/Pyramid card with specified card number."); | |
30 | PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); | |
31 | PrintAndLog("The facility-code is 8-bit and the card number is 16-bit. Larger values are truncated."); | |
32 | PrintAndLog("Currently work only on 26bit"); | |
33 | PrintAndLog(""); | |
34 | PrintAndLog("Usage: lf pyramid sim <Card-Number>"); | |
35 | PrintAndLog("Options :"); | |
36 | PrintAndLog(" <Facility-Code> : 8-bit value facility code"); | |
37 | PrintAndLog(" <Card Number> : 16-bit value card number"); | |
38 | PrintAndLog(""); | |
39 | PrintAndLog("Sample : lf pyramid sim 123 11223"); | |
40 | return 0; | |
41 | } | |
42 | ||
43 | // Works for 26bits. | |
44 | int GetPyramidBits(uint32_t fc, uint32_t cn, uint8_t *pyramidBits) { | |
45 | ||
46 | uint8_t pre[128]; | |
47 | memset(pre, 0x00, sizeof(pre)); | |
48 | ||
49 | // format start bit | |
50 | pre[79] = 1; | |
51 | ||
52 | // Get 26 wiegand from FacilityCode, CardNumber | |
53 | uint8_t wiegand[24]; | |
54 | memset(wiegand, 0x00, sizeof(wiegand)); | |
55 | num_to_bytebits(fc, 8, wiegand); | |
56 | num_to_bytebits(cn, 16, wiegand+8); | |
57 | ||
58 | // add wiegand parity bits (dest, source, len) | |
59 | wiegand_add_parity(pre+80, wiegand, 24); | |
60 | ||
61 | // add paritybits (bitsource, dest, sourcelen, paritylen, parityType (odd, even,) | |
62 | addParity(pre+8, pyramidBits+8, 102, 8, 1); | |
63 | ||
64 | // add checksum | |
65 | uint8_t csBuff[13]; | |
66 | for (uint8_t i = 0; i < 13; i++) | |
67 | csBuff[i] = bytebits_to_byte(pyramidBits + 16 + (i*8), 8); | |
68 | ||
69 | uint32_t crc = CRC8Maxim(csBuff, 13); | |
70 | num_to_bytebits(crc, 8, pyramidBits+120); | |
71 | return 1; | |
72 | } | |
73 | ||
74 | int CmdPyramidRead(const char *Cmd) { | |
75 | CmdLFRead("s"); | |
76 | getSamples("30000",false); | |
77 | return CmdFSKdemodPyramid(""); | |
78 | } | |
79 | ||
80 | int CmdPyramidClone(const char *Cmd) { | |
81 | ||
82 | char cmdp = param_getchar(Cmd, 0); | |
83 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_pyramid_clone(); | |
84 | ||
85 | uint32_t facilitycode=0, cardnumber=0, fc = 0, cn = 0; | |
86 | uint32_t blocks[5]; | |
87 | uint8_t i; | |
88 | uint8_t bs[128]; | |
89 | memset(bs, 0x00, sizeof(bs)); | |
90 | ||
91 | if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_pyramid_clone(); | |
92 | ||
93 | facilitycode = (fc & 0x000000FF); | |
94 | cardnumber = (cn & 0x0000FFFF); | |
95 | ||
96 | if ( !GetPyramidBits(facilitycode, cardnumber, bs)) { | |
97 | PrintAndLog("Error with tag bitstream generation."); | |
98 | return 1; | |
99 | } | |
100 | ||
101 | // if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q') | |
102 | // blocks[0] = T5555_MODULATION_FSK2 | 50<<T5555_BITRATE_SHIFT | 4<<T5555_MAXBLOCK_SHIFT; | |
103 | ||
104 | //Pyramid - compat mode, FSK2a, data rate 50, 4 data blocks | |
105 | blocks[0] = T55x7_MODULATION_FSK2a | T55x7_BITRATE_RF_50 | 4<<T55x7_MAXBLOCK_SHIFT; | |
106 | blocks[1] = bytebits_to_byte(bs,32); | |
107 | blocks[2] = bytebits_to_byte(bs+32,32); | |
108 | blocks[3] = bytebits_to_byte(bs+64,32); | |
109 | blocks[4] = bytebits_to_byte(bs+96,32); | |
110 | ||
111 | PrintAndLog("Preparing to clone Farpointe/Pyramid to T55x7 with Facility Code: %u, Card Number: %u", facilitycode, cardnumber); | |
112 | PrintAndLog("Blk | Data "); | |
113 | PrintAndLog("----+------------"); | |
114 | for ( i = 0; i<5; ++i ) | |
115 | PrintAndLog(" %02d | %08" PRIx32, i, blocks[i]); | |
116 | ||
117 | UsbCommand resp; | |
118 | UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; | |
119 | ||
120 | for ( i = 0; i<5; ++i ) { | |
121 | c.arg[0] = blocks[i]; | |
122 | c.arg[1] = i; | |
123 | clearCommandBuffer(); | |
124 | SendCommand(&c); | |
125 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){ | |
126 | PrintAndLog("Error occurred, device did not respond during write operation."); | |
127 | return -1; | |
128 | } | |
129 | } | |
130 | return 0; | |
131 | } | |
132 | ||
133 | int CmdPyramidSim(const char *Cmd) { | |
134 | ||
135 | char cmdp = param_getchar(Cmd, 0); | |
136 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_pyramid_sim(); | |
137 | ||
138 | uint32_t facilitycode = 0, cardnumber = 0, fc = 0, cn = 0; | |
139 | ||
140 | uint8_t bs[128]; | |
141 | size_t size = sizeof(bs); | |
142 | memset(bs, 0x00, size); | |
143 | ||
144 | // Pyramid uses: fcHigh: 10, fcLow: 8, clk: 50, invert: 0 | |
145 | uint64_t arg1, arg2; | |
146 | arg1 = (10 << 8) + 8; | |
147 | arg2 = 50 | 0; | |
148 | ||
149 | if (sscanf(Cmd, "%u %u", &fc, &cn ) != 2) return usage_lf_pyramid_sim(); | |
150 | ||
151 | facilitycode = (fc & 0x000000FF); | |
152 | cardnumber = (cn & 0x0000FFFF); | |
153 | ||
154 | if ( !GetPyramidBits(facilitycode, cardnumber, bs)) { | |
155 | PrintAndLog("Error with tag bitstream generation."); | |
156 | return 1; | |
157 | } | |
158 | ||
159 | PrintAndLog("Simulating Farpointe/Pyramid - Facility Code: %u, CardNumber: %u", facilitycode, cardnumber ); | |
160 | ||
161 | UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, size}}; | |
162 | memcpy(c.d.asBytes, bs, size); | |
163 | clearCommandBuffer(); | |
164 | SendCommand(&c); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | static command_t CommandTable[] = { | |
169 | {"help", CmdHelp, 1, "This help"}, | |
170 | {"read", CmdPyramidRead, 0, "Attempt to read and extract tag data"}, | |
171 | {"clone", CmdPyramidClone, 0, "<Facility-Code> <Card Number> clone pyramid tag"}, | |
172 | {"sim", CmdPyramidSim, 0, "<Facility-Code> <Card Number> simulate pyramid tag"}, | |
173 | {NULL, NULL, 0, NULL} | |
174 | }; | |
175 | ||
176 | int CmdLFPyramid(const char *Cmd) { | |
177 | clearCommandBuffer(); | |
178 | CmdsParse(CommandTable, Cmd); | |
179 | return 0; | |
180 | } | |
181 | ||
182 | int CmdHelp(const char *Cmd) { | |
183 | CmdsHelp(CommandTable); | |
184 | return 0; | |
185 | } |