]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdlfti.c
59f83f0873de3a01e83a96f5c37ff5027af9594f
[proxmark3-svn] / client / cmdlfti.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Low frequency TI commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include "crc16.h"
14 #include "proxusb.h"
15 #include "data.h"
16 #include "ui.h"
17 #include "graph.h"
18 #include "cmdparser.h"
19 #include "cmdlfti.h"
20
21 static int CmdHelp(const char *Cmd);
22
23 int CmdTIDemod(const char *Cmd)
24 {
25 /* MATLAB as follows:
26 f_s = 2000000; % sampling frequency
27 f_l = 123200; % low FSK tone
28 f_h = 134200; % high FSK tone
29
30 T_l = 119e-6; % low bit duration
31 T_h = 130e-6; % high bit duration
32
33 l = 2*pi*ones(1, floor(f_s*T_l))*(f_l/f_s);
34 h = 2*pi*ones(1, floor(f_s*T_h))*(f_h/f_s);
35
36 l = sign(sin(cumsum(l)));
37 h = sign(sin(cumsum(h)));
38 */
39
40 // 2M*16/134.2k = 238
41 static const int LowTone[] = {
42 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
43 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
44 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
45 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
47 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
48 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
49 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
50 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
51 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
52 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
53 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
54 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
55 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
56 1, 1, 1, 1, 1, 1, 1, 1, -1, -1
57 };
58 // 2M*16/123.2k = 260
59 static const int HighTone[] = {
60 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
61 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
62 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
63 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
64 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
65 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
66 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
67 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
68 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
69 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
70 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
71 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
72 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
73 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
74 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
75 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
76 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,
77 1, 1, 1, 1, 1, 1, 1, 1
78 };
79 int lowLen = sizeof(LowTone)/sizeof(int);
80 int highLen = sizeof(HighTone)/sizeof(int);
81 int convLen = (highLen>lowLen)?highLen:lowLen;
82 uint16_t crc;
83 int i, j, TagType;
84 int lowSum = 0, highSum = 0;;
85 int lowTot = 0, highTot = 0;
86
87 for (i = 0; i < GraphTraceLen - convLen; i++) {
88 lowSum = 0;
89 highSum = 0;;
90
91 for (j = 0; j < lowLen; j++) {
92 lowSum += LowTone[j]*GraphBuffer[i+j];
93 }
94 for (j = 0; j < highLen; j++) {
95 highSum += HighTone[j]*GraphBuffer[i+j];
96 }
97 lowSum = abs((100*lowSum) / lowLen);
98 highSum = abs((100*highSum) / highLen);
99 lowSum = (lowSum<0)?-lowSum:lowSum;
100 highSum = (highSum<0)?-highSum:highSum;
101
102 GraphBuffer[i] = (highSum << 16) | lowSum;
103 }
104
105 for (i = 0; i < GraphTraceLen - convLen - 16; i++) {
106 lowTot = 0;
107 highTot = 0;
108 // 16 and 15 are f_s divided by f_l and f_h, rounded
109 for (j = 0; j < 16; j++) {
110 lowTot += (GraphBuffer[i+j] & 0xffff);
111 }
112 for (j = 0; j < 15; j++) {
113 highTot += (GraphBuffer[i+j] >> 16);
114 }
115 GraphBuffer[i] = lowTot - highTot;
116 }
117
118 GraphTraceLen -= (convLen + 16);
119
120 RepaintGraphWindow();
121
122 // TI tag data format is 16 prebits, 8 start bits, 64 data bits,
123 // 16 crc CCITT bits, 8 stop bits, 15 end bits
124
125 // the 16 prebits are always low
126 // the 8 start and stop bits of a tag must match
127 // the start/stop prebits of a ro tag are 01111110
128 // the start/stop prebits of a rw tag are 11111110
129 // the 15 end bits of a ro tag are all low
130 // the 15 end bits of a rw tag match bits 15-1 of the data bits
131
132 // Okay, so now we have unsliced soft decisions;
133 // find bit-sync, and then get some bits.
134 // look for 17 low bits followed by 6 highs (common pattern for ro and rw tags)
135 int max = 0, maxPos = 0;
136 for (i = 0; i < 6000; i++) {
137 int j;
138 int dec = 0;
139 // searching 17 consecutive lows
140 for (j = 0; j < 17*lowLen; j++) {
141 dec -= GraphBuffer[i+j];
142 }
143 // searching 7 consecutive highs
144 for (; j < 17*lowLen + 6*highLen; j++) {
145 dec += GraphBuffer[i+j];
146 }
147 if (dec > max) {
148 max = dec;
149 maxPos = i;
150 }
151 }
152
153 // place a marker in the buffer to visually aid location
154 // of the start of sync
155 GraphBuffer[maxPos] = 800;
156 GraphBuffer[maxPos+1] = -800;
157
158 // advance pointer to start of actual data stream (after 16 pre and 8 start bits)
159 maxPos += 17*lowLen;
160 maxPos += 6*highLen;
161
162 // place a marker in the buffer to visually aid location
163 // of the end of sync
164 GraphBuffer[maxPos] = 800;
165 GraphBuffer[maxPos+1] = -800;
166
167 PrintAndLog("actual data bits start at sample %d", maxPos);
168
169 PrintAndLog("length %d/%d", highLen, lowLen);
170
171 uint8_t bits[1+64+16+8+16];
172 bits[sizeof(bits)-1] = '\0';
173
174 uint32_t shift3 = 0x7e000000, shift2 = 0, shift1 = 0, shift0 = 0;
175
176 for (i = 0; i < arraylen(bits)-1; i++) {
177 int high = 0;
178 int low = 0;
179 int j;
180 for (j = 0; j < lowLen; j++) {
181 low -= GraphBuffer[maxPos+j];
182 }
183 for (j = 0; j < highLen; j++) {
184 high += GraphBuffer[maxPos+j];
185 }
186
187 if (high > low) {
188 bits[i] = '1';
189 maxPos += highLen;
190 // bitstream arrives lsb first so shift right
191 shift3 |= (1<<31);
192 } else {
193 bits[i] = '.';
194 maxPos += lowLen;
195 }
196
197 // 128 bit right shift register
198 shift0 = (shift0>>1) | (shift1 << 31);
199 shift1 = (shift1>>1) | (shift2 << 31);
200 shift2 = (shift2>>1) | (shift3 << 31);
201 shift3 >>= 1;
202
203 // place a marker in the buffer between bits to visually aid location
204 GraphBuffer[maxPos] = 800;
205 GraphBuffer[maxPos+1] = -800;
206 }
207 PrintAndLog("Info: raw tag bits = %s", bits);
208
209 TagType = (shift3>>8)&0xff;
210 if ( TagType != ((shift0>>16)&0xff) ) {
211 PrintAndLog("Error: start and stop bits do not match!");
212 return 0;
213 }
214 else if (TagType == 0x7e) {
215 PrintAndLog("Info: Readonly TI tag detected.");
216 return 0;
217 }
218 else if (TagType == 0xfe) {
219 PrintAndLog("Info: Rewriteable TI tag detected.");
220
221 // put 64 bit data into shift1 and shift0
222 shift0 = (shift0>>24) | (shift1 << 8);
223 shift1 = (shift1>>24) | (shift2 << 8);
224
225 // align 16 bit crc into lower half of shift2
226 shift2 = ((shift2>>24) | (shift3 << 8)) & 0x0ffff;
227
228 // align 16 bit "end bits" or "ident" into lower half of shift3
229 shift3 >>= 16;
230
231 // only 15 bits compare, last bit of ident is not valid
232 if ( (shift3^shift0)&0x7fff ) {
233 PrintAndLog("Error: Ident mismatch!");
234 }
235 // WARNING the order of the bytes in which we calc crc below needs checking
236 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
237 // bytes in reverse or something
238 // calculate CRC
239 crc=0;
240 crc = update_crc16(crc, (shift0)&0xff);
241 crc = update_crc16(crc, (shift0>>8)&0xff);
242 crc = update_crc16(crc, (shift0>>16)&0xff);
243 crc = update_crc16(crc, (shift0>>24)&0xff);
244 crc = update_crc16(crc, (shift1)&0xff);
245 crc = update_crc16(crc, (shift1>>8)&0xff);
246 crc = update_crc16(crc, (shift1>>16)&0xff);
247 crc = update_crc16(crc, (shift1>>24)&0xff);
248 PrintAndLog("Info: Tag data = %08X%08X", shift1, shift0);
249 if (crc != (shift2&0xffff)) {
250 PrintAndLog("Error: CRC mismatch, calculated %04X, got ^04X", crc, shift2&0xffff);
251 } else {
252 PrintAndLog("Info: CRC %04X is good", crc);
253 }
254 }
255 else {
256 PrintAndLog("Unknown tag type.");
257 return 0;
258 }
259 return 0;
260 }
261
262 // read a TI tag and return its ID
263 int CmdTIRead(const char *Cmd)
264 {
265 UsbCommand c = {CMD_READ_TI_TYPE};
266 SendCommand(&c);
267 return 0;
268 }
269
270 // write new data to a r/w TI tag
271 int CmdTIWrite(const char *Cmd)
272 {
273 UsbCommand c = {CMD_WRITE_TI_TYPE};
274 int res = 0;
275
276 res = sscanf(Cmd, "0x%x 0x%x 0x%x ", &c.arg[0], &c.arg[1], &c.arg[2]);
277 if (res == 2) c.arg[2]=0;
278 if (res < 2)
279 PrintAndLog("Please specify the data as two hex strings, optionally the CRC as a third");
280 else
281 SendCommand(&c);
282 return 0;
283 }
284
285 static command_t CommandTable[] =
286 {
287 {"help", CmdHelp, 1, "This help"},
288 {"demod", CmdTIDemod, 1, "Demodulate raw bits for TI-type LF tag"},
289 {"read", CmdTIRead, 0, "Read and decode a TI 134 kHz tag"},
290 {"write", CmdTIWrite, 0, "Write new data to a r/w TI 134 kHz tag"},
291 {NULL, NULL, 0, NULL}
292 };
293
294 int CmdLFTI(const char *Cmd)
295 {
296 CmdsParse(CommandTable, Cmd);
297 return 0;
298 }
299
300 int CmdHelp(const char *Cmd)
301 {
302 CmdsHelp(CommandTable);
303 return 0;
304 }
Impressum, Datenschutz