]>
Commit | Line | Data |
---|---|---|
6c283951 | 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 "cmdlfjablotron.h" | |
10 | ||
11 | static int CmdHelp(const char *Cmd); | |
12 | ||
13 | int usage_lf_jablotron_clone(void){ | |
14 | PrintAndLog("clone a Jablotron tag to a T55x7 tag."); | |
8ae9b358 | 15 | PrintAndLog("Usage: lf jablotron clone [h] <card ID> <Q5>"); |
16 | PrintAndLog("Options:"); | |
17 | PrintAndLog(" h : This help"); | |
18 | PrintAndLog(" <card ID> : jablotron card ID"); | |
19 | PrintAndLog(" <Q5> : specify write to Q5 (t5555 instead of t55x7)"); | |
6c283951 | 20 | PrintAndLog(""); |
514ddaa2 | 21 | PrintAndLog("Sample: lf jablotron clone 112233"); |
6c283951 | 22 | return 0; |
23 | } | |
24 | ||
25 | int usage_lf_jablotron_sim(void) { | |
26 | PrintAndLog("Enables simulation of jablotron card with specified card number."); | |
27 | PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); | |
6c283951 | 28 | PrintAndLog(""); |
8ae9b358 | 29 | PrintAndLog("Usage: lf jablotron sim [h] <card ID>"); |
30 | PrintAndLog("Options:"); | |
31 | PrintAndLog(" h : This help"); | |
32 | PrintAndLog(" <card ID> : jablotron card ID"); | |
6c283951 | 33 | PrintAndLog(""); |
514ddaa2 | 34 | PrintAndLog("Sample: lf jablotron sim 112233"); |
6c283951 | 35 | return 0; |
36 | } | |
37 | ||
29ce214c | 38 | static uint8_t jablontron_chksum(uint8_t *bits){ |
39 | uint8_t chksum = 0; | |
40 | for (int i=16; i < 56; i += 8) { | |
41 | chksum += bytebits_to_byte(bits+i,8); | |
42 | } | |
43 | chksum ^= 0x3A; | |
44 | return chksum; | |
45 | } | |
46 | ||
6c283951 | 47 | int getJablotronBits(uint64_t fullcode, uint8_t *bits) { |
48 | //preamp | |
49 | num_to_bytebits(0xFFFF, 16, bits); | |
50 | ||
51 | //fullcode | |
52 | num_to_bytebits(fullcode, 40, bits+16); | |
53 | ||
54 | //chksum byte | |
29ce214c | 55 | uint8_t chksum = jablontron_chksum(bits); |
56 | num_to_bytebits(chksum, 8, bits+56); | |
6c283951 | 57 | return 1; |
58 | } | |
59 | ||
60 | //see ASKDemod for what args are accepted | |
61 | int CmdJablotronDemod(const char *Cmd) { | |
62 | ||
63 | //Differential Biphase / di-phase (inverted biphase) | |
64 | //get binary from ask wave | |
65 | if (!ASKbiphaseDemod("0 64 1 0", FALSE)) { | |
66 | if (g_debugMode) PrintAndLog("Error Jablotron: ASKbiphaseDemod failed"); | |
67 | return 0; | |
68 | } | |
69 | size_t size = DemodBufferLen; | |
70 | int ans = JablotronDemod(DemodBuffer, &size); | |
71 | if (ans < 0){ | |
72 | if (g_debugMode){ | |
73 | // if (ans == -5) | |
74 | // PrintAndLog("DEBUG: Error - not enough samples"); | |
29ce214c | 75 | if (ans == -1) |
76 | PrintAndLog("DEBUG: Error - Jablotron too few bits found"); | |
6c283951 | 77 | // else if (ans == -2) |
78 | // PrintAndLog("DEBUG: Error - problem during ASK/Biphase demod"); | |
29ce214c | 79 | else if (ans == -3) |
80 | PrintAndLog("DEBUG: Error - Jablotron Size not correct: %d", size); | |
6c283951 | 81 | else if (ans == -4) |
82 | PrintAndLog("DEBUG: Error - Jablotron preamble not found"); | |
29ce214c | 83 | else if (ans == -5) |
84 | PrintAndLog("DEBUG: Error - Jablotron checksum failed"); | |
6c283951 | 85 | else |
86 | PrintAndLog("DEBUG: Error - ans: %d", ans); | |
87 | } | |
88 | return 0; | |
89 | } | |
29ce214c | 90 | |
91 | setDemodBuf(DemodBuffer+ans, 64, 0); | |
92 | ||
6c283951 | 93 | //got a good demod |
29ce214c | 94 | uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32); |
95 | uint32_t raw2 = bytebits_to_byte(DemodBuffer+32, 32); | |
96 | uint64_t cardid = (raw1 & 0xFFFF); | |
97 | cardid <<= 24; | |
6c283951 | 98 | cardid |= (raw2 >> 8); |
99 | ||
29ce214c | 100 | PrintAndLog("Jablotron Tag Found: Card ID %"PRIx64, cardid); |
6c283951 | 101 | PrintAndLog("Raw: %08X%08X", raw1 ,raw2); |
102 | ||
29ce214c | 103 | uint8_t chksum = raw2 & 0xFF; |
104 | PrintAndLog("Checksum: %02X [%s]", | |
105 | chksum, | |
106 | (chksum == jablontron_chksum(DemodBuffer)) ? "OK":"FAIL" | |
107 | ); | |
108 | ||
109 | // Printed format: 1410-nn-nnnn-nnnn | |
110 | PrintAndLog("Printed: 1410-%02X-%04X-%04X", | |
111 | (raw1 & 0x0000FF00) >> 8, | |
112 | (raw1 & 0xFF) << 8 | ((raw2 >> 24) & 0xFF), | |
113 | (raw2 & 0x00FFFF00) >> 8 | |
114 | ); | |
6c283951 | 115 | return 1; |
116 | } | |
117 | ||
118 | int CmdJablotronRead(const char *Cmd) { | |
6c283951 | 119 | CmdLFRead("s"); |
6c283951 | 120 | getSamples("30000",false); |
6c283951 | 121 | return CmdJablotronDemod(Cmd); |
122 | } | |
123 | ||
124 | int CmdJablotronClone(const char *Cmd) { | |
125 | ||
126 | uint64_t fullcode = 0; | |
127 | uint32_t blocks[3] = {T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_64 | 2<<T55x7_MAXBLOCK_SHIFT, 0, 0}; | |
128 | ||
129 | uint8_t bits[64]; | |
130 | uint8_t *bs = bits; | |
131 | memset(bs, 0, sizeof(bits)); | |
132 | ||
133 | char cmdp = param_getchar(Cmd, 0); | |
134 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_clone(); | |
135 | ||
8ae9b358 | 136 | fullcode = param_get64ex(Cmd, 0, 0, 16); |
6c283951 | 137 | |
138 | //Q5 | |
8ae9b358 | 139 | if (param_getchar(Cmd, 1) == 'Q' || param_getchar(Cmd, 1) == 'q') { |
6c283951 | 140 | //t5555 (Q5) BITRATE = (RF-2)/2 (iceman) |
141 | blocks[0] = T5555_MODULATION_BIPHASE | T5555_INVERT_OUTPUT | 64<<T5555_BITRATE_SHIFT | 2<<T5555_MAXBLOCK_SHIFT; | |
142 | } | |
143 | ||
29ce214c | 144 | // clearing the topbit needed for the preambl detection. |
145 | if ((fullcode & 0x7FFFFFFFFF) != fullcode) { | |
146 | fullcode &= 0x7FFFFFFFFF; | |
f5538c1c | 147 | PrintAndLog("Card Number Truncated to 39bits: %"PRIx64, fullcode); |
6c283951 | 148 | } |
29ce214c | 149 | |
6c283951 | 150 | if ( !getJablotronBits(fullcode, bs)) { |
151 | PrintAndLog("Error with tag bitstream generation."); | |
152 | return 1; | |
153 | } | |
154 | ||
155 | // | |
156 | blocks[1] = bytebits_to_byte(bs,32); | |
157 | blocks[2] = bytebits_to_byte(bs+32,32); | |
158 | ||
29ce214c | 159 | PrintAndLog("Preparing to clone Jablotron to T55x7 with FullCode: %"PRIx64, fullcode); |
6c283951 | 160 | PrintAndLog("Blk | Data "); |
161 | PrintAndLog("----+------------"); | |
162 | PrintAndLog(" 00 | 0x%08x", blocks[0]); | |
163 | PrintAndLog(" 01 | 0x%08x", blocks[1]); | |
164 | PrintAndLog(" 02 | 0x%08x", blocks[2]); | |
165 | ||
166 | UsbCommand resp; | |
167 | UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}}; | |
168 | ||
169 | for (int i=4; i>=0; i--) { | |
170 | c.arg[0] = blocks[i]; | |
171 | c.arg[1] = i; | |
172 | clearCommandBuffer(); | |
173 | SendCommand(&c); | |
174 | if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){ | |
175 | PrintAndLog("Error occurred, device did not respond during write operation."); | |
176 | return -1; | |
177 | } | |
178 | } | |
179 | return 0; | |
180 | } | |
181 | ||
182 | int CmdJablotronSim(const char *Cmd) { | |
183 | uint64_t fullcode = 0; | |
184 | ||
185 | char cmdp = param_getchar(Cmd, 0); | |
186 | if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_jablotron_sim(); | |
187 | ||
8ae9b358 | 188 | fullcode = param_get64ex(Cmd, 0, 0, 16); |
29ce214c | 189 | |
190 | // clearing the topbit needed for the preambl detection. | |
191 | if ((fullcode & 0x7FFFFFFFFF) != fullcode) { | |
192 | fullcode &= 0x7FFFFFFFFF; | |
f5538c1c | 193 | PrintAndLog("Card Number Truncated to 39bits: %"PRIx64, fullcode); |
29ce214c | 194 | } |
6c283951 | 195 | |
196 | uint8_t clk = 64, encoding = 2, separator = 0, invert = 1; | |
197 | uint16_t arg1, arg2; | |
198 | size_t size = 64; | |
199 | arg1 = clk << 8 | encoding; | |
200 | arg2 = invert << 8 | separator; | |
201 | ||
29ce214c | 202 | PrintAndLog("Simulating Jablotron - FullCode: %"PRIx64, fullcode); |
6c283951 | 203 | |
204 | UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; | |
205 | getJablotronBits(fullcode, c.d.asBytes); | |
206 | clearCommandBuffer(); | |
207 | SendCommand(&c); | |
208 | return 0; | |
209 | } | |
210 | ||
211 | static command_t CommandTable[] = { | |
212 | {"help", CmdHelp, 1, "This help"}, | |
8ae9b358 | 213 | {"read", CmdJablotronRead, 0, "Attempt to read and extract tag data"}, |
214 | {"clone", CmdJablotronClone, 0, "clone jablotron tag"}, | |
215 | {"sim", CmdJablotronSim, 0, "simulate jablotron tag"}, | |
6c283951 | 216 | {NULL, NULL, 0, NULL} |
217 | }; | |
218 | ||
219 | int CmdLFJablotron(const char *Cmd) { | |
220 | clearCommandBuffer(); | |
221 | CmdsParse(CommandTable, Cmd); | |
222 | return 0; | |
223 | } | |
224 | ||
225 | int CmdHelp(const char *Cmd) { | |
226 | CmdsHelp(CommandTable); | |
227 | return 0; | |
228 | } |