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