]>
Commit | Line | Data |
---|---|---|
15c4dc5a | 1 | //----------------------------------------------------------------------------- |
bd20f8f4 | 2 | // Jonathan Westhues, split Nov 2006 |
3 | // Modified by Greg Jones, Jan 2009 | |
e6304bca | 4 | // Modified by Adrian Dabrowski "atrox", Mar-Sept 2010,Oct 2011 |
bd20f8f4 | 5 | // |
6 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
7 | // at your option, any later version. See the LICENSE.txt file for the text of | |
8 | // the license. | |
9 | //----------------------------------------------------------------------------- | |
15c4dc5a | 10 | // Routines to support ISO 15693. This includes both the reader software and |
11 | // the `fake tag' modes, but at the moment I've implemented only the reader | |
12 | // stuff, and that barely. | |
bd20f8f4 | 13 | // Modified to perform modulation onboard in arm rather than on PC |
15c4dc5a | 14 | // Also added additional reader commands (SELECT, READ etc.) |
15c4dc5a | 15 | //----------------------------------------------------------------------------- |
9455b51c | 16 | // The ISO 15693 describes two transmission modes from reader to tag, and 4 |
17 | // transmission modes from tag to reader. As of Mar 2010 this code only | |
18 | // supports one of each: "1of4" mode from reader to tag, and the highspeed | |
19 | // variant with one subcarrier from card to reader. | |
20 | // As long, as the card fully support ISO 15693 this is no problem, since the | |
21 | // reader chooses both data rates, but some non-standard tags do not. Further for | |
22 | // the simulation to work, we will need to support all data rates. | |
23 | // | |
24 | // VCD (reader) -> VICC (tag) | |
25 | // 1 out of 256: | |
26 | // data rate: 1,66 kbit/s (fc/8192) | |
27 | // used for long range | |
28 | // 1 out of 4: | |
29 | // data rate: 26,48 kbit/s (fc/512) | |
30 | // used for short range, high speed | |
31 | // | |
32 | // VICC (tag) -> VCD (reader) | |
33 | // Modulation: | |
34 | // ASK / one subcarrier (423,75 khz) | |
35 | // FSK / two subcarriers (423,75 khz && 484,28 khz) | |
36 | // Data Rates / Modes: | |
37 | // low ASK: 6,62 kbit/s | |
38 | // low FSK: 6.67 kbit/s | |
39 | // high ASK: 26,48 kbit/s | |
40 | // high FSK: 26,69 kbit/s | |
41 | //----------------------------------------------------------------------------- | |
42 | // added "1 out of 256" mode (for VCD->PICC) - atrox 20100911 | |
43 | ||
44 | ||
45 | // Random Remarks: | |
46 | // *) UID is always used "transmission order" (LSB), which is reverse to display order | |
47 | ||
48 | // TODO / BUGS / ISSUES: | |
49 | // *) writing to tags takes longer: we miss the answer from the tag in most cases | |
50 | // -> tweak the read-timeout times | |
51 | // *) signal decoding from the card is still a bit shaky. | |
52 | // *) signal decoding is unable to detect collissions. | |
53 | // *) add anti-collission support for inventory-commands | |
e6304bca | 54 | // *) read security status of a block |
9455b51c | 55 | // *) sniffing and simulation do only support one transmission mode. need to support |
56 | // all 8 transmission combinations | |
57 | // *) remove or refactor code under "depricated" | |
58 | // *) document all the functions | |
59 | ||
bd20f8f4 | 60 | |
e30c654b | 61 | #include "proxmark3.h" |
f7e3ed82 | 62 | #include "util.h" |
15c4dc5a | 63 | #include "apps.h" |
9ab7a6c7 | 64 | #include "string.h" |
9455b51c | 65 | #include "iso15693tools.h" |
902cb3c0 | 66 | #include "cmd.h" |
15c4dc5a | 67 | |
15c4dc5a | 68 | #define arraylen(x) (sizeof(x)/sizeof((x)[0])) |
69 | ||
9455b51c | 70 | /////////////////////////////////////////////////////////////////////// |
71 | // ISO 15693 Part 2 - Air Interface | |
72 | // This section basicly contains transmission and receiving of bits | |
73 | /////////////////////////////////////////////////////////////////////// | |
74 | ||
75 | #define FrameSOF Iso15693FrameSOF | |
76 | #define Logic0 Iso15693Logic0 | |
77 | #define Logic1 Iso15693Logic1 | |
78 | #define FrameEOF Iso15693FrameEOF | |
15c4dc5a | 79 | |
9455b51c | 80 | #define Crc(data,datalen) Iso15693Crc(data,datalen) |
81 | #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen) | |
82 | #define sprintUID(target,uid) Iso15693sprintUID(target,uid) | |
15c4dc5a | 83 | |
9455b51c | 84 | int DEBUG=0; |
85 | ||
86 | ||
87 | // --------------------------- | |
88 | // Signal Processing | |
89 | // --------------------------- | |
90 | ||
91 | // prepare data using "1 out of 4" code for later transmission | |
92 | // resulting data rate is 26,48 kbit/s (fc/512) | |
93 | // cmd ... data | |
94 | // n ... length of data | |
f7e3ed82 | 95 | static void CodeIso15693AsReader(uint8_t *cmd, int n) |
15c4dc5a | 96 | { |
97 | int i, j; | |
98 | ||
99 | ToSendReset(); | |
100 | ||
101 | // Give it a bit of slack at the beginning | |
102 | for(i = 0; i < 24; i++) { | |
103 | ToSendStuffBit(1); | |
104 | } | |
105 | ||
9455b51c | 106 | // SOF for 1of4 |
15c4dc5a | 107 | ToSendStuffBit(0); |
108 | ToSendStuffBit(1); | |
109 | ToSendStuffBit(1); | |
110 | ToSendStuffBit(1); | |
111 | ToSendStuffBit(1); | |
112 | ToSendStuffBit(0); | |
113 | ToSendStuffBit(1); | |
114 | ToSendStuffBit(1); | |
115 | for(i = 0; i < n; i++) { | |
116 | for(j = 0; j < 8; j += 2) { | |
117 | int these = (cmd[i] >> j) & 3; | |
118 | switch(these) { | |
119 | case 0: | |
120 | ToSendStuffBit(1); | |
121 | ToSendStuffBit(0); | |
122 | ToSendStuffBit(1); | |
123 | ToSendStuffBit(1); | |
124 | ToSendStuffBit(1); | |
125 | ToSendStuffBit(1); | |
126 | ToSendStuffBit(1); | |
127 | ToSendStuffBit(1); | |
128 | break; | |
129 | case 1: | |
130 | ToSendStuffBit(1); | |
131 | ToSendStuffBit(1); | |
132 | ToSendStuffBit(1); | |
133 | ToSendStuffBit(0); | |
134 | ToSendStuffBit(1); | |
135 | ToSendStuffBit(1); | |
136 | ToSendStuffBit(1); | |
137 | ToSendStuffBit(1); | |
138 | break; | |
139 | case 2: | |
140 | ToSendStuffBit(1); | |
141 | ToSendStuffBit(1); | |
142 | ToSendStuffBit(1); | |
143 | ToSendStuffBit(1); | |
144 | ToSendStuffBit(1); | |
145 | ToSendStuffBit(0); | |
146 | ToSendStuffBit(1); | |
147 | ToSendStuffBit(1); | |
148 | break; | |
149 | case 3: | |
150 | ToSendStuffBit(1); | |
151 | ToSendStuffBit(1); | |
152 | ToSendStuffBit(1); | |
153 | ToSendStuffBit(1); | |
154 | ToSendStuffBit(1); | |
155 | ToSendStuffBit(1); | |
156 | ToSendStuffBit(1); | |
157 | ToSendStuffBit(0); | |
158 | break; | |
159 | } | |
160 | } | |
161 | } | |
9455b51c | 162 | // EOF |
15c4dc5a | 163 | ToSendStuffBit(1); |
164 | ToSendStuffBit(1); | |
165 | ToSendStuffBit(0); | |
166 | ToSendStuffBit(1); | |
167 | ||
168 | // And slack at the end, too. | |
169 | for(i = 0; i < 24; i++) { | |
170 | ToSendStuffBit(1); | |
171 | } | |
172 | } | |
173 | ||
9455b51c | 174 | // encode data using "1 out of 256" sheme |
175 | // data rate is 1,66 kbit/s (fc/8192) | |
176 | // is designed for more robust communication over longer distances | |
177 | static void CodeIso15693AsReader256(uint8_t *cmd, int n) | |
15c4dc5a | 178 | { |
15c4dc5a | 179 | int i, j; |
180 | ||
9455b51c | 181 | ToSendReset(); |
182 | ||
183 | // Give it a bit of slack at the beginning | |
184 | for(i = 0; i < 24; i++) { | |
185 | ToSendStuffBit(1); | |
186 | } | |
187 | ||
188 | // SOF for 1of256 | |
189 | ToSendStuffBit(0); | |
190 | ToSendStuffBit(1); | |
191 | ToSendStuffBit(1); | |
192 | ToSendStuffBit(1); | |
193 | ToSendStuffBit(1); | |
194 | ToSendStuffBit(1); | |
195 | ToSendStuffBit(1); | |
196 | ToSendStuffBit(0); | |
197 | ||
15c4dc5a | 198 | for(i = 0; i < n; i++) { |
9455b51c | 199 | for (j = 0; j<=255; j++) { |
200 | if (cmd[i]==j) { | |
201 | ToSendStuffBit(1); | |
202 | ToSendStuffBit(0); | |
15c4dc5a | 203 | } else { |
9455b51c | 204 | ToSendStuffBit(1); |
205 | ToSendStuffBit(1); | |
206 | } | |
207 | } | |
15c4dc5a | 208 | } |
9455b51c | 209 | // EOF |
210 | ToSendStuffBit(1); | |
211 | ToSendStuffBit(1); | |
212 | ToSendStuffBit(0); | |
213 | ToSendStuffBit(1); | |
15c4dc5a | 214 | |
9455b51c | 215 | // And slack at the end, too. |
216 | for(i = 0; i < 24; i++) { | |
217 | ToSendStuffBit(1); | |
218 | } | |
15c4dc5a | 219 | } |
220 | ||
9455b51c | 221 | |
15c4dc5a | 222 | // Transmit the command (to the tag) that was placed in ToSend[]. |
f7e3ed82 | 223 | static void TransmitTo15693Tag(const uint8_t *cmd, int len, int *samples, int *wait) |
15c4dc5a | 224 | { |
225 | int c; | |
226 | ||
227 | // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD); | |
228 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX); | |
229 | if(*wait < 10) { *wait = 10; } | |
230 | ||
231 | // for(c = 0; c < *wait;) { | |
232 | // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
233 | // AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing! | |
234 | // c++; | |
235 | // } | |
236 | // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
f7e3ed82 | 237 | // volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; |
15c4dc5a | 238 | // (void)r; |
239 | // } | |
240 | // WDT_HIT(); | |
241 | // } | |
242 | ||
243 | c = 0; | |
244 | for(;;) { | |
245 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
246 | AT91C_BASE_SSC->SSC_THR = cmd[c]; | |
247 | c++; | |
248 | if(c >= len) { | |
249 | break; | |
250 | } | |
251 | } | |
252 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
f7e3ed82 | 253 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; |
15c4dc5a | 254 | (void)r; |
255 | } | |
256 | WDT_HIT(); | |
257 | } | |
258 | *samples = (c + *wait) << 3; | |
259 | } | |
260 | ||
261 | //----------------------------------------------------------------------------- | |
262 | // Transmit the command (to the reader) that was placed in ToSend[]. | |
263 | //----------------------------------------------------------------------------- | |
f7e3ed82 | 264 | static void TransmitTo15693Reader(const uint8_t *cmd, int len, int *samples, int *wait) |
15c4dc5a | 265 | { |
3fe4ff4f | 266 | int c = 0; |
267 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR|FPGA_HF_SIMULATOR_MODULATE_424K); | |
15c4dc5a | 268 | if(*wait < 10) { *wait = 10; } |
269 | ||
15c4dc5a | 270 | for(;;) { |
271 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
272 | AT91C_BASE_SSC->SSC_THR = cmd[c]; | |
273 | c++; | |
274 | if(c >= len) { | |
275 | break; | |
276 | } | |
277 | } | |
278 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
f7e3ed82 | 279 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; |
15c4dc5a | 280 | (void)r; |
281 | } | |
282 | WDT_HIT(); | |
283 | } | |
284 | *samples = (c + *wait) << 3; | |
285 | } | |
286 | ||
9455b51c | 287 | |
288 | // Read from Tag | |
289 | // Parameters: | |
290 | // receivedResponse | |
291 | // maxLen | |
292 | // samples | |
293 | // elapsed | |
294 | // returns: | |
295 | // number of decoded bytes | |
f7e3ed82 | 296 | static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) |
15c4dc5a | 297 | { |
117d9ec2 | 298 | uint8_t *dest = BigBuf_get_addr(); |
15c4dc5a | 299 | |
f2c2b174 | 300 | int c = 0; |
301 | int getNext = FALSE; | |
f7e3ed82 | 302 | int8_t prev = 0; |
15c4dc5a | 303 | |
15c4dc5a | 304 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); |
f2c2b174 | 305 | SpinDelay(100); // greg - experiment to get rid of some of the 0 byte/failed reads |
306 | ||
15c4dc5a | 307 | for(;;) { |
f2c2b174 | 308 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) |
15c4dc5a | 309 | AT91C_BASE_SSC->SSC_THR = 0x43; |
f2c2b174 | 310 | |
15c4dc5a | 311 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { |
f7e3ed82 | 312 | int8_t b; |
313 | b = (int8_t)AT91C_BASE_SSC->SSC_RHR; | |
15c4dc5a | 314 | |
315 | // The samples are correlations against I and Q versions of the | |
316 | // tone that the tag AM-modulates, so every other sample is I, | |
317 | // every other is Q. We just want power, so abs(I) + abs(Q) is | |
318 | // close to what we want. | |
319 | if(getNext) { | |
f7e3ed82 | 320 | int8_t r; |
15c4dc5a | 321 | |
f2c2b174 | 322 | r = ABS(b); |
323 | ||
324 | // if(b < 0) { | |
325 | // r = -b; | |
326 | // } else { | |
327 | // r = b; | |
328 | // } | |
329 | // ABS(prev) | |
15c4dc5a | 330 | if(prev < 0) { |
331 | r -= prev; | |
332 | } else { | |
333 | r += prev; | |
334 | } | |
335 | ||
f7e3ed82 | 336 | dest[c++] = (uint8_t)r; |
15c4dc5a | 337 | |
338 | if(c >= 2000) { | |
339 | break; | |
340 | } | |
341 | } else { | |
342 | prev = b; | |
343 | } | |
344 | ||
345 | getNext = !getNext; | |
346 | } | |
347 | } | |
348 | ||
9455b51c | 349 | ////////////////////////////////////////// |
350 | /////////// DEMODULATE /////////////////// | |
351 | ////////////////////////////////////////// | |
15c4dc5a | 352 | |
353 | int i, j; | |
354 | int max = 0, maxPos=0; | |
355 | ||
356 | int skip = 4; | |
357 | ||
9455b51c | 358 | // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL |
15c4dc5a | 359 | |
360 | // First, correlate for SOF | |
361 | for(i = 0; i < 100; i++) { | |
362 | int corr = 0; | |
363 | for(j = 0; j < arraylen(FrameSOF); j += skip) { | |
364 | corr += FrameSOF[j]*dest[i+(j/skip)]; | |
365 | } | |
366 | if(corr > max) { | |
367 | max = corr; | |
368 | maxPos = i; | |
369 | } | |
370 | } | |
9455b51c | 371 | // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip)); |
15c4dc5a | 372 | |
373 | int k = 0; // this will be our return value | |
374 | ||
375 | // greg - If correlation is less than 1 then there's little point in continuing | |
376 | if ((max/(arraylen(FrameSOF)/skip)) >= 1) | |
377 | { | |
378 | ||
9455b51c | 379 | i = maxPos + arraylen(FrameSOF)/skip; |
380 | ||
381 | uint8_t outBuf[20]; | |
382 | memset(outBuf, 0, sizeof(outBuf)); | |
383 | uint8_t mask = 0x01; | |
384 | for(;;) { | |
385 | int corr0 = 0, corr1 = 0, corrEOF = 0; | |
386 | for(j = 0; j < arraylen(Logic0); j += skip) { | |
387 | corr0 += Logic0[j]*dest[i+(j/skip)]; | |
388 | } | |
389 | for(j = 0; j < arraylen(Logic1); j += skip) { | |
390 | corr1 += Logic1[j]*dest[i+(j/skip)]; | |
391 | } | |
392 | for(j = 0; j < arraylen(FrameEOF); j += skip) { | |
393 | corrEOF += FrameEOF[j]*dest[i+(j/skip)]; | |
394 | } | |
395 | // Even things out by the length of the target waveform. | |
396 | corr0 *= 4; | |
397 | corr1 *= 4; | |
398 | ||
399 | if(corrEOF > corr1 && corrEOF > corr0) { | |
400 | // DbpString("EOF at %d", i); | |
401 | break; | |
402 | } else if(corr1 > corr0) { | |
403 | i += arraylen(Logic1)/skip; | |
404 | outBuf[k] |= mask; | |
405 | } else { | |
406 | i += arraylen(Logic0)/skip; | |
407 | } | |
408 | mask <<= 1; | |
409 | if(mask == 0) { | |
410 | k++; | |
411 | mask = 0x01; | |
412 | } | |
413 | if((i+(int)arraylen(FrameEOF)) >= 2000) { | |
414 | DbpString("ran off end!"); | |
415 | break; | |
416 | } | |
15c4dc5a | 417 | } |
9455b51c | 418 | if(mask != 0x01) { // this happens, when we miss the EOF |
419 | // TODO: for some reason this happens quite often | |
420 | if (DEBUG) Dbprintf("error, uneven octet! (extra bits!) mask=%02x", mask); | |
421 | if (mask<0x08) k--; // discard the last uneven octet; | |
422 | // 0x08 is an assumption - but works quite often | |
15c4dc5a | 423 | } |
9455b51c | 424 | // uint8_t str1 [8]; |
425 | // itoa(k,str1); | |
426 | // strncat(str1," octets read",8); | |
427 | ||
428 | // DbpString( str1); // DbpString("%d octets", k); | |
429 | ||
430 | // for(i = 0; i < k; i+=3) { | |
431 | // //DbpString("# %2d: %02x ", i, outBuf[i]); | |
432 | // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]); | |
433 | // } | |
434 | ||
435 | for(i = 0; i < k; i++) { | |
436 | receivedResponse[i] = outBuf[i]; | |
15c4dc5a | 437 | } |
15c4dc5a | 438 | } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip)) |
439 | return k; // return the number of bytes demodulated | |
440 | ||
f2c2b174 | 441 | // DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2)); |
15c4dc5a | 442 | } |
443 | ||
9455b51c | 444 | |
15c4dc5a | 445 | // Now the GetISO15693 message from sniffing command |
f7e3ed82 | 446 | static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) |
15c4dc5a | 447 | { |
117d9ec2 | 448 | uint8_t *dest = BigBuf_get_addr(); |
15c4dc5a | 449 | |
f2c2b174 | 450 | int c = 0; |
451 | int getNext = FALSE; | |
f7e3ed82 | 452 | int8_t prev = 0; |
15c4dc5a | 453 | |
15c4dc5a | 454 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); |
f2c2b174 | 455 | SpinDelay(100); // greg - experiment to get rid of some of the 0 byte/failed reads |
456 | ||
15c4dc5a | 457 | for(;;) { |
f2c2b174 | 458 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) |
15c4dc5a | 459 | AT91C_BASE_SSC->SSC_THR = 0x43; |
f2c2b174 | 460 | |
15c4dc5a | 461 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { |
3fe4ff4f | 462 | int8_t b = (int8_t)AT91C_BASE_SSC->SSC_RHR; |
15c4dc5a | 463 | |
464 | // The samples are correlations against I and Q versions of the | |
465 | // tone that the tag AM-modulates, so every other sample is I, | |
466 | // every other is Q. We just want power, so abs(I) + abs(Q) is | |
467 | // close to what we want. | |
468 | if(getNext) { | |
f7e3ed82 | 469 | int8_t r; |
15c4dc5a | 470 | |
f2c2b174 | 471 | r = ABS(b); |
472 | // if(b < 0) { | |
473 | // r = -b; | |
474 | // } else { | |
475 | // r = b; | |
476 | // } | |
15c4dc5a | 477 | if(prev < 0) { |
478 | r -= prev; | |
479 | } else { | |
480 | r += prev; | |
481 | } | |
482 | ||
f7e3ed82 | 483 | dest[c++] = (uint8_t)r; |
15c4dc5a | 484 | |
485 | if(c >= 20000) { | |
486 | break; | |
487 | } | |
488 | } else { | |
489 | prev = b; | |
490 | } | |
491 | ||
492 | getNext = !getNext; | |
493 | } | |
494 | } | |
495 | ||
9455b51c | 496 | ////////////////////////////////////////// |
497 | /////////// DEMODULATE /////////////////// | |
498 | ////////////////////////////////////////// | |
15c4dc5a | 499 | |
500 | int i, j; | |
501 | int max = 0, maxPos=0; | |
502 | ||
503 | int skip = 4; | |
504 | ||
505 | // if(GraphTraceLen < 1000) return; // THIS CHECKS FOR A BUFFER TO SMALL | |
506 | ||
507 | // First, correlate for SOF | |
508 | for(i = 0; i < 19000; i++) { | |
509 | int corr = 0; | |
510 | for(j = 0; j < arraylen(FrameSOF); j += skip) { | |
511 | corr += FrameSOF[j]*dest[i+(j/skip)]; | |
512 | } | |
513 | if(corr > max) { | |
514 | max = corr; | |
515 | maxPos = i; | |
516 | } | |
517 | } | |
518 | // DbpString("SOF at %d, correlation %d", maxPos,max/(arraylen(FrameSOF)/skip)); | |
519 | ||
520 | int k = 0; // this will be our return value | |
521 | ||
522 | // greg - If correlation is less than 1 then there's little point in continuing | |
523 | if ((max/(arraylen(FrameSOF)/skip)) >= 1) // THIS SHOULD BE 1 | |
524 | { | |
9455b51c | 525 | |
526 | i = maxPos + arraylen(FrameSOF)/skip; | |
527 | ||
528 | uint8_t outBuf[20]; | |
529 | memset(outBuf, 0, sizeof(outBuf)); | |
530 | uint8_t mask = 0x01; | |
531 | for(;;) { | |
532 | int corr0 = 0, corr1 = 0, corrEOF = 0; | |
533 | for(j = 0; j < arraylen(Logic0); j += skip) { | |
534 | corr0 += Logic0[j]*dest[i+(j/skip)]; | |
535 | } | |
536 | for(j = 0; j < arraylen(Logic1); j += skip) { | |
537 | corr1 += Logic1[j]*dest[i+(j/skip)]; | |
538 | } | |
539 | for(j = 0; j < arraylen(FrameEOF); j += skip) { | |
540 | corrEOF += FrameEOF[j]*dest[i+(j/skip)]; | |
541 | } | |
542 | // Even things out by the length of the target waveform. | |
543 | corr0 *= 4; | |
544 | corr1 *= 4; | |
545 | ||
546 | if(corrEOF > corr1 && corrEOF > corr0) { | |
547 | // DbpString("EOF at %d", i); | |
548 | break; | |
549 | } else if(corr1 > corr0) { | |
550 | i += arraylen(Logic1)/skip; | |
551 | outBuf[k] |= mask; | |
552 | } else { | |
553 | i += arraylen(Logic0)/skip; | |
554 | } | |
555 | mask <<= 1; | |
556 | if(mask == 0) { | |
557 | k++; | |
558 | mask = 0x01; | |
559 | } | |
560 | if((i+(int)arraylen(FrameEOF)) >= 2000) { | |
561 | DbpString("ran off end!"); | |
562 | break; | |
563 | } | |
15c4dc5a | 564 | } |
9455b51c | 565 | if(mask != 0x01) { |
566 | DbpString("sniff: error, uneven octet! (discard extra bits!)"); | |
567 | /// DbpString(" mask=%02x", mask); | |
15c4dc5a | 568 | } |
9455b51c | 569 | // uint8_t str1 [8]; |
570 | // itoa(k,str1); | |
571 | // strncat(str1," octets read",8); | |
572 | ||
573 | // DbpString( str1); // DbpString("%d octets", k); | |
574 | ||
575 | // for(i = 0; i < k; i+=3) { | |
576 | // //DbpString("# %2d: %02x ", i, outBuf[i]); | |
577 | // DbpIntegers(outBuf[i],outBuf[i+1],outBuf[i+2]); | |
578 | // } | |
579 | ||
580 | for(i = 0; i < k; i++) { | |
581 | receivedResponse[i] = outBuf[i]; | |
15c4dc5a | 582 | } |
15c4dc5a | 583 | } // "end if correlation > 0" (max/(arraylen(FrameSOF)/skip)) |
584 | return k; // return the number of bytes demodulated | |
585 | ||
586 | /// DbpString("CRC=%04x", Iso15693Crc(outBuf, k-2)); | |
587 | } | |
588 | ||
9455b51c | 589 | |
590 | static void BuildIdentifyRequest(void); | |
15c4dc5a | 591 | //----------------------------------------------------------------------------- |
592 | // Start to read an ISO 15693 tag. We send an identify request, then wait | |
593 | // for the response. The response is not demodulated, just left in the buffer | |
594 | // so that it can be downloaded to a PC and processed there. | |
595 | //----------------------------------------------------------------------------- | |
596 | void AcquireRawAdcSamplesIso15693(void) | |
597 | { | |
117d9ec2 | 598 | uint8_t *dest = BigBuf_get_addr(); |
15c4dc5a | 599 | |
3fe4ff4f | 600 | int c = 0; |
601 | int getNext = 0; | |
f7e3ed82 | 602 | int8_t prev = 0; |
15c4dc5a | 603 | |
7cc204bf | 604 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
15c4dc5a | 605 | BuildIdentifyRequest(); |
606 | ||
607 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
608 | ||
609 | // Give the tags time to energize | |
610 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); | |
611 | SpinDelay(100); | |
612 | ||
613 | // Now send the command | |
614 | FpgaSetupSsc(); | |
615 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX); | |
616 | ||
617 | c = 0; | |
618 | for(;;) { | |
619 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
620 | AT91C_BASE_SSC->SSC_THR = ToSend[c]; | |
621 | c++; | |
622 | if(c == ToSendMax+3) { | |
623 | break; | |
624 | } | |
625 | } | |
626 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
f7e3ed82 | 627 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; |
15c4dc5a | 628 | (void)r; |
629 | } | |
630 | WDT_HIT(); | |
631 | } | |
632 | ||
633 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); | |
634 | ||
635 | c = 0; | |
636 | getNext = FALSE; | |
637 | for(;;) { | |
638 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
639 | AT91C_BASE_SSC->SSC_THR = 0x43; | |
640 | } | |
641 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
f7e3ed82 | 642 | int8_t b; |
643 | b = (int8_t)AT91C_BASE_SSC->SSC_RHR; | |
15c4dc5a | 644 | |
9455b51c | 645 | // The samples are correlations against I and Q versions of the |
646 | // tone that the tag AM-modulates, so every other sample is I, | |
647 | // every other is Q. We just want power, so abs(I) + abs(Q) is | |
648 | // close to what we want. | |
649 | if(getNext) { | |
650 | int8_t r; | |
651 | ||
652 | if(b < 0) { | |
653 | r = -b; | |
654 | } else { | |
655 | r = b; | |
656 | } | |
657 | if(prev < 0) { | |
658 | r -= prev; | |
659 | } else { | |
660 | r += prev; | |
661 | } | |
662 | ||
663 | dest[c++] = (uint8_t)r; | |
664 | ||
665 | if(c >= 2000) { | |
666 | break; | |
667 | } | |
668 | } else { | |
669 | prev = b; | |
670 | } | |
671 | ||
672 | getNext = !getNext; | |
673 | } | |
674 | } | |
675 | } | |
676 | ||
677 | ||
678 | void RecordRawAdcSamplesIso15693(void) | |
679 | { | |
117d9ec2 | 680 | uint8_t *dest = BigBuf_get_addr(); |
3fe4ff4f | 681 | |
9455b51c | 682 | int c = 0; |
9455b51c | 683 | int getNext = 0; |
9455b51c | 684 | int8_t prev = 0; |
685 | ||
7cc204bf | 686 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
9455b51c | 687 | // Setup SSC |
688 | FpgaSetupSsc(); | |
689 | ||
690 | // Start from off (no field generated) | |
5ee53a0e | 691 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
692 | SpinDelay(200); | |
9455b51c | 693 | |
694 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
695 | ||
696 | SpinDelay(100); | |
697 | ||
698 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); | |
699 | ||
700 | c = 0; | |
701 | getNext = FALSE; | |
702 | for(;;) { | |
703 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
704 | AT91C_BASE_SSC->SSC_THR = 0x43; | |
705 | } | |
706 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
707 | int8_t b; | |
708 | b = (int8_t)AT91C_BASE_SSC->SSC_RHR; | |
709 | ||
710 | // The samples are correlations against I and Q versions of the | |
711 | // tone that the tag AM-modulates, so every other sample is I, | |
712 | // every other is Q. We just want power, so abs(I) + abs(Q) is | |
713 | // close to what we want. | |
714 | if(getNext) { | |
715 | int8_t r; | |
716 | ||
717 | if(b < 0) { | |
718 | r = -b; | |
719 | } else { | |
720 | r = b; | |
721 | } | |
722 | if(prev < 0) { | |
723 | r -= prev; | |
724 | } else { | |
725 | r += prev; | |
726 | } | |
727 | ||
728 | dest[c++] = (uint8_t)r; | |
729 | ||
730 | if(c >= 7000) { | |
731 | break; | |
732 | } | |
733 | } else { | |
734 | prev = b; | |
735 | } | |
736 | ||
737 | getNext = !getNext; | |
738 | WDT_HIT(); | |
739 | } | |
740 | } | |
741 | Dbprintf("fin record"); | |
742 | } | |
743 | ||
744 | ||
745 | // Initialize the proxmark as iso15k reader | |
e6304bca | 746 | // (this might produces glitches that confuse some tags |
9455b51c | 747 | void Iso15693InitReader() { |
748 | LED_A_ON(); | |
749 | LED_B_ON(); | |
750 | LED_C_OFF(); | |
751 | LED_D_OFF(); | |
752 | ||
7cc204bf | 753 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
9455b51c | 754 | // Setup SSC |
e6304bca | 755 | // FpgaSetupSsc(); |
9455b51c | 756 | |
757 | // Start from off (no field generated) | |
758 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); | |
e6304bca | 759 | SpinDelay(10); |
9455b51c | 760 | |
761 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
762 | FpgaSetupSsc(); | |
763 | ||
764 | // Give the tags time to energize | |
765 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); | |
e6304bca | 766 | SpinDelay(250); |
9455b51c | 767 | |
768 | LED_A_ON(); | |
769 | LED_B_OFF(); | |
770 | LED_C_OFF(); | |
771 | LED_D_OFF(); | |
772 | } | |
773 | ||
774 | /////////////////////////////////////////////////////////////////////// | |
775 | // ISO 15693 Part 3 - Air Interface | |
776 | // This section basicly contains transmission and receiving of bits | |
777 | /////////////////////////////////////////////////////////////////////// | |
778 | ||
779 | // Encode (into the ToSend buffers) an identify request, which is the first | |
780 | // thing that you must send to a tag to get a response. | |
781 | static void BuildIdentifyRequest(void) | |
782 | { | |
783 | uint8_t cmd[5]; | |
784 | ||
785 | uint16_t crc; | |
786 | // one sub-carrier, inventory, 1 slot, fast rate | |
787 | // AFI is at bit 5 (1<<4) when doing an INVENTORY | |
788 | cmd[0] = (1 << 2) | (1 << 5) | (1 << 1); | |
789 | // inventory command code | |
790 | cmd[1] = 0x01; | |
791 | // no mask | |
792 | cmd[2] = 0x00; | |
793 | //Now the CRC | |
794 | crc = Crc(cmd, 3); | |
795 | cmd[3] = crc & 0xff; | |
796 | cmd[4] = crc >> 8; | |
797 | ||
798 | CodeIso15693AsReader(cmd, sizeof(cmd)); | |
799 | } | |
800 | ||
801 | // uid is in transmission order (which is reverse of display order) | |
802 | static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber ) | |
803 | { | |
804 | uint8_t cmd[13]; | |
805 | ||
806 | uint16_t crc; | |
807 | // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block | |
808 | // followed by teh block data | |
809 | // one sub-carrier, inventory, 1 slot, fast rate | |
810 | cmd[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit | |
811 | // READ BLOCK command code | |
812 | cmd[1] = 0x20; | |
813 | // UID may be optionally specified here | |
814 | // 64-bit UID | |
815 | cmd[2] = uid[0]; | |
816 | cmd[3] = uid[1]; | |
817 | cmd[4] = uid[2]; | |
818 | cmd[5] = uid[3]; | |
819 | cmd[6] = uid[4]; | |
820 | cmd[7] = uid[5]; | |
821 | cmd[8] = uid[6]; | |
822 | cmd[9] = uid[7]; // 0xe0; // always e0 (not exactly unique) | |
823 | // Block number to read | |
824 | cmd[10] = blockNumber;//0x00; | |
825 | //Now the CRC | |
826 | crc = Crc(cmd, 11); // the crc needs to be calculated over 12 bytes | |
827 | cmd[11] = crc & 0xff; | |
828 | cmd[12] = crc >> 8; | |
829 | ||
830 | CodeIso15693AsReader(cmd, sizeof(cmd)); | |
831 | } | |
832 | ||
833 | // Now the VICC>VCD responses when we are simulating a tag | |
3fe4ff4f | 834 | static void BuildInventoryResponse( uint8_t *uid) |
9455b51c | 835 | { |
836 | uint8_t cmd[12]; | |
837 | ||
838 | uint16_t crc; | |
839 | // one sub-carrier, inventory, 1 slot, fast rate | |
840 | // AFI is at bit 5 (1<<4) when doing an INVENTORY | |
3fe4ff4f | 841 | //(1 << 2) | (1 << 5) | (1 << 1); |
842 | cmd[0] = 0; // | |
843 | cmd[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported | |
9455b51c | 844 | // 64-bit UID |
3fe4ff4f | 845 | cmd[2] = uid[7]; //0x32; |
846 | cmd[3] = uid[6]; //0x4b; | |
847 | cmd[4] = uid[5]; //0x03; | |
848 | cmd[5] = uid[4]; //0x01; | |
849 | cmd[6] = uid[3]; //0x00; | |
850 | cmd[7] = uid[2]; //0x10; | |
851 | cmd[8] = uid[1]; //0x05; | |
852 | cmd[9] = uid[0]; //0xe0; | |
9455b51c | 853 | //Now the CRC |
854 | crc = Crc(cmd, 10); | |
855 | cmd[10] = crc & 0xff; | |
856 | cmd[11] = crc >> 8; | |
857 | ||
858 | CodeIso15693AsReader(cmd, sizeof(cmd)); | |
859 | } | |
860 | ||
e6304bca | 861 | // Universal Method for sending to and recv bytes from a tag |
9455b51c | 862 | // init ... should we initialize the reader? |
863 | // speed ... 0 low speed, 1 hi speed | |
864 | // **recv will return you a pointer to the received data | |
865 | // If you do not need the answer use NULL for *recv[] | |
866 | // return: lenght of received data | |
867 | int SendDataTag(uint8_t *send, int sendlen, int init, int speed, uint8_t **recv) { | |
868 | ||
869 | int samples = 0; | |
870 | int tsamples = 0; | |
871 | int wait = 0; | |
872 | int elapsed = 0; | |
873 | ||
874 | LED_A_ON(); | |
875 | LED_B_ON(); | |
876 | LED_C_OFF(); | |
877 | LED_D_OFF(); | |
878 | ||
99cf19d9 | 879 | if (init) Iso15693InitReader(); |
880 | ||
9455b51c | 881 | int answerLen=0; |
117d9ec2 | 882 | uint8_t *answer = BigBuf_get_addr() + 3660; |
883 | if (recv != NULL) memset(answer, 0, 100); | |
9455b51c | 884 | |
9455b51c | 885 | if (!speed) { |
886 | // low speed (1 out of 256) | |
887 | CodeIso15693AsReader256(send, sendlen); | |
888 | } else { | |
889 | // high speed (1 out of 4) | |
890 | CodeIso15693AsReader(send, sendlen); | |
891 | } | |
892 | ||
893 | LED_A_ON(); | |
894 | LED_B_OFF(); | |
895 | ||
896 | TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); | |
897 | // Now wait for a response | |
898 | if (recv!=NULL) { | |
899 | LED_A_OFF(); | |
900 | LED_B_ON(); | |
901 | answerLen = GetIso15693AnswerFromTag(answer, 100, &samples, &elapsed) ; | |
902 | *recv=answer; | |
903 | } | |
904 | ||
4a71da5a | 905 | LEDsoff(); |
9455b51c | 906 | |
907 | return answerLen; | |
908 | } | |
15c4dc5a | 909 | |
15c4dc5a | 910 | |
9455b51c | 911 | // -------------------------------------------------------------------- |
912 | // Debug Functions | |
913 | // -------------------------------------------------------------------- | |
15c4dc5a | 914 | |
9455b51c | 915 | // Decodes a message from a tag and displays its metadata and content |
916 | #define DBD15STATLEN 48 | |
917 | void DbdecodeIso15693Answer(int len, uint8_t *d) { | |
918 | char status[DBD15STATLEN+1]={0}; | |
919 | uint16_t crc; | |
920 | ||
921 | if (len>3) { | |
922 | if (d[0]&(1<<3)) | |
923 | strncat(status,"ProtExt ",DBD15STATLEN); | |
924 | if (d[0]&1) { | |
925 | // error | |
926 | strncat(status,"Error ",DBD15STATLEN); | |
927 | switch (d[1]) { | |
928 | case 0x01: | |
929 | strncat(status,"01:notSupp",DBD15STATLEN); | |
15c4dc5a | 930 | break; |
9455b51c | 931 | case 0x02: |
932 | strncat(status,"02:notRecog",DBD15STATLEN); | |
933 | break; | |
934 | case 0x03: | |
935 | strncat(status,"03:optNotSupp",DBD15STATLEN); | |
936 | break; | |
937 | case 0x0f: | |
938 | strncat(status,"0f:noInfo",DBD15STATLEN); | |
939 | break; | |
940 | case 0x10: | |
941 | strncat(status,"10:dontExist",DBD15STATLEN); | |
942 | break; | |
943 | case 0x11: | |
944 | strncat(status,"11:lockAgain",DBD15STATLEN); | |
945 | break; | |
946 | case 0x12: | |
947 | strncat(status,"12:locked",DBD15STATLEN); | |
948 | break; | |
949 | case 0x13: | |
950 | strncat(status,"13:progErr",DBD15STATLEN); | |
951 | break; | |
952 | case 0x14: | |
953 | strncat(status,"14:lockErr",DBD15STATLEN); | |
954 | break; | |
955 | default: | |
956 | strncat(status,"unknownErr",DBD15STATLEN); | |
15c4dc5a | 957 | } |
9455b51c | 958 | strncat(status," ",DBD15STATLEN); |
959 | } else { | |
960 | strncat(status,"NoErr ",DBD15STATLEN); | |
15c4dc5a | 961 | } |
9455b51c | 962 | |
963 | crc=Crc(d,len-2); | |
964 | if ( (( crc & 0xff ) == d[len-2]) && (( crc >> 8 ) == d[len-1]) ) | |
965 | strncat(status,"CrcOK",DBD15STATLEN); | |
966 | else | |
967 | strncat(status,"CrcFail!",DBD15STATLEN); | |
968 | ||
969 | Dbprintf("%s",status); | |
15c4dc5a | 970 | } |
971 | } | |
972 | ||
9455b51c | 973 | |
974 | ||
975 | /////////////////////////////////////////////////////////////////////// | |
976 | // Functions called via USB/Client | |
977 | /////////////////////////////////////////////////////////////////////// | |
978 | ||
979 | void SetDebugIso15693(uint32_t debug) { | |
980 | DEBUG=debug; | |
981 | Dbprintf("Iso15693 Debug is now %s",DEBUG?"on":"off"); | |
982 | return; | |
983 | } | |
984 | ||
985 | ||
986 | ||
15c4dc5a | 987 | //----------------------------------------------------------------------------- |
988 | // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector | |
989 | // all demodulation performed in arm rather than host. - greg | |
990 | //----------------------------------------------------------------------------- | |
f7e3ed82 | 991 | void ReaderIso15693(uint32_t parameter) |
15c4dc5a | 992 | { |
993 | LED_A_ON(); | |
994 | LED_B_ON(); | |
995 | LED_C_OFF(); | |
996 | LED_D_OFF(); | |
997 | ||
15c4dc5a | 998 | int answerLen1 = 0; |
999 | int answerLen2 = 0; | |
1000 | int answerLen3 = 0; | |
3fe4ff4f | 1001 | int i = 0; |
1002 | int samples = 0; | |
1003 | int tsamples = 0; | |
1004 | int wait = 0; | |
1005 | int elapsed = 0; | |
1006 | uint8_t TagUID[8] = {0x00}; | |
1007 | ||
99cf19d9 | 1008 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
15c4dc5a | 1009 | |
99cf19d9 | 1010 | uint8_t *answer1 = BigBuf_get_addr() + 3660; |
1011 | uint8_t *answer2 = BigBuf_get_addr() + 3760; | |
1012 | uint8_t *answer3 = BigBuf_get_addr() + 3860; | |
15c4dc5a | 1013 | // Blank arrays |
117d9ec2 | 1014 | memset(answer1, 0x00, 300); |
15c4dc5a | 1015 | |
3fe4ff4f | 1016 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); |
15c4dc5a | 1017 | // Setup SSC |
1018 | FpgaSetupSsc(); | |
1019 | ||
1020 | // Start from off (no field generated) | |
5ee53a0e | 1021 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
1022 | SpinDelay(200); | |
15c4dc5a | 1023 | |
15c4dc5a | 1024 | // Give the tags time to energize |
1025 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); | |
1026 | SpinDelay(200); | |
1027 | ||
1028 | LED_A_ON(); | |
1029 | LED_B_OFF(); | |
1030 | LED_C_OFF(); | |
1031 | LED_D_OFF(); | |
1032 | ||
15c4dc5a | 1033 | // FIRST WE RUN AN INVENTORY TO GET THE TAG UID |
1034 | // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME | |
15c4dc5a | 1035 | |
1036 | // Now send the IDENTIFY command | |
1037 | BuildIdentifyRequest(); | |
3fe4ff4f | 1038 | |
1039 | TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); | |
1040 | ||
15c4dc5a | 1041 | // Now wait for a response |
1042 | answerLen1 = GetIso15693AnswerFromTag(answer1, 100, &samples, &elapsed) ; | |
1043 | ||
1044 | if (answerLen1 >=12) // we should do a better check than this | |
1045 | { | |
15c4dc5a | 1046 | TagUID[0] = answer1[2]; |
1047 | TagUID[1] = answer1[3]; | |
1048 | TagUID[2] = answer1[4]; | |
1049 | TagUID[3] = answer1[5]; | |
1050 | TagUID[4] = answer1[6]; | |
1051 | TagUID[5] = answer1[7]; | |
1052 | TagUID[6] = answer1[8]; // IC Manufacturer code | |
9455b51c | 1053 | TagUID[7] = answer1[9]; // always E0 |
15c4dc5a | 1054 | |
15c4dc5a | 1055 | } |
1056 | ||
9455b51c | 1057 | Dbprintf("%d octets read from IDENTIFY request:", answerLen1); |
1058 | DbdecodeIso15693Answer(answerLen1,answer1); | |
d19929cb | 1059 | Dbhexdump(answerLen1,answer1,true); |
9455b51c | 1060 | |
1061 | // UID is reverse | |
1062 | if (answerLen1>=12) | |
3fe4ff4f | 1063 | Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX", |
1064 | TagUID[7],TagUID[6],TagUID[5],TagUID[4], | |
1065 | TagUID[3],TagUID[2],TagUID[1],TagUID[0]); | |
9455b51c | 1066 | |
1067 | ||
1068 | Dbprintf("%d octets read from SELECT request:", answerLen2); | |
1069 | DbdecodeIso15693Answer(answerLen2,answer2); | |
d19929cb | 1070 | Dbhexdump(answerLen2,answer2,true); |
9455b51c | 1071 | |
1072 | Dbprintf("%d octets read from XXX request:", answerLen3); | |
1073 | DbdecodeIso15693Answer(answerLen3,answer3); | |
d19929cb | 1074 | Dbhexdump(answerLen3,answer3,true); |
9455b51c | 1075 | |
9455b51c | 1076 | // read all pages |
1077 | if (answerLen1>=12 && DEBUG) { | |
1078 | i=0; | |
1079 | while (i<32) { // sanity check, assume max 32 pages | |
1080 | BuildReadBlockRequest(TagUID,i); | |
1081 | TransmitTo15693Tag(ToSend,ToSendMax,&tsamples, &wait); | |
1082 | answerLen2 = GetIso15693AnswerFromTag(answer2, 100, &samples, &elapsed); | |
1083 | if (answerLen2>0) { | |
1084 | Dbprintf("READ SINGLE BLOCK %d returned %d octets:",i,answerLen2); | |
1085 | DbdecodeIso15693Answer(answerLen2,answer2); | |
d19929cb | 1086 | Dbhexdump(answerLen2,answer2,true); |
9455b51c | 1087 | if ( *((uint32_t*) answer2) == 0x07160101 ) break; // exit on NoPageErr |
1088 | } | |
1089 | i++; | |
1090 | } | |
1091 | } | |
15c4dc5a | 1092 | |
15c4dc5a | 1093 | LED_A_OFF(); |
1094 | LED_B_OFF(); | |
1095 | LED_C_OFF(); | |
1096 | LED_D_OFF(); | |
1097 | } | |
1098 | ||
15c4dc5a | 1099 | // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands |
1100 | // all demodulation performed in arm rather than host. - greg | |
3fe4ff4f | 1101 | void SimTagIso15693(uint32_t parameter, uint8_t *uid) |
15c4dc5a | 1102 | { |
1103 | LED_A_ON(); | |
1104 | LED_B_ON(); | |
1105 | LED_C_OFF(); | |
1106 | LED_D_OFF(); | |
1107 | ||
15c4dc5a | 1108 | int answerLen1 = 0; |
3fe4ff4f | 1109 | int samples = 0; |
1110 | int tsamples = 0; | |
1111 | int wait = 0; | |
1112 | int elapsed = 0; | |
15c4dc5a | 1113 | |
7cc204bf | 1114 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
15c4dc5a | 1115 | |
99cf19d9 | 1116 | uint8_t *buf = BigBuf_get_addr() + 3660; |
1117 | memset(buf, 0x00, 100); | |
3fe4ff4f | 1118 | |
99cf19d9 | 1119 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); |
15c4dc5a | 1120 | FpgaSetupSsc(); |
1121 | ||
3fe4ff4f | 1122 | // Start from off (no field generated) |
5ee53a0e | 1123 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); |
15c4dc5a | 1124 | SpinDelay(200); |
1125 | ||
1126 | LED_A_OFF(); | |
1127 | LED_B_OFF(); | |
1128 | LED_C_ON(); | |
1129 | LED_D_OFF(); | |
1130 | ||
3fe4ff4f | 1131 | // Listen to reader |
1132 | answerLen1 = GetIso15693AnswerFromSniff(buf, 100, &samples, &elapsed) ; | |
15c4dc5a | 1133 | |
1134 | if (answerLen1 >=1) // we should do a better check than this | |
1135 | { | |
1136 | // Build a suitable reponse to the reader INVENTORY cocmmand | |
3fe4ff4f | 1137 | // not so obsvious, but in the call to BuildInventoryResponse, the command is copied to the global ToSend buffer used below. |
1138 | ||
1139 | BuildInventoryResponse(uid); | |
1140 | ||
15c4dc5a | 1141 | TransmitTo15693Reader(ToSend,ToSendMax, &tsamples, &wait); |
1142 | } | |
1143 | ||
1144 | Dbprintf("%d octets read from reader command: %x %x %x %x %x %x %x %x %x", answerLen1, | |
3fe4ff4f | 1145 | buf[0], buf[1], buf[2], buf[3], |
1146 | buf[4], buf[5], buf[6], buf[7], buf[8]); | |
1147 | ||
1148 | Dbprintf("Simulationg uid: %x %x %x %x %x %x %x %x", | |
1149 | uid[0], uid[1], uid[2], uid[3], | |
1150 | uid[4], uid[5], uid[6], uid[7]); | |
15c4dc5a | 1151 | |
1152 | LED_A_OFF(); | |
1153 | LED_B_OFF(); | |
1154 | LED_C_OFF(); | |
1155 | LED_D_OFF(); | |
1156 | } | |
9455b51c | 1157 | |
1158 | ||
1159 | // Since there is no standardized way of reading the AFI out of a tag, we will brute force it | |
1160 | // (some manufactures offer a way to read the AFI, though) | |
1161 | void BruteforceIso15693Afi(uint32_t speed) | |
1162 | { | |
1163 | uint8_t data[20]; | |
1164 | uint8_t *recv=data; | |
1165 | int datalen=0, recvlen=0; | |
1166 | ||
1167 | Iso15693InitReader(); | |
1168 | ||
1169 | // first without AFI | |
1170 | // Tags should respond wihtout AFI and with AFI=0 even when AFI is active | |
1171 | ||
1172 | data[0]=ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
1173 | ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1; | |
1174 | data[1]=ISO15_CMD_INVENTORY; | |
1175 | data[2]=0; // mask length | |
1176 | datalen=AddCrc(data,3); | |
1177 | recvlen=SendDataTag(data,datalen,0,speed,&recv); | |
1178 | WDT_HIT(); | |
1179 | if (recvlen>=12) { | |
1180 | Dbprintf("NoAFI UID=%s",sprintUID(NULL,&recv[2])); | |
1181 | } | |
1182 | ||
1183 | // now with AFI | |
1184 | ||
1185 | data[0]=ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH | | |
1186 | ISO15_REQ_INVENTORY | ISO15_REQINV_AFI | ISO15_REQINV_SLOT1; | |
1187 | data[1]=ISO15_CMD_INVENTORY; | |
1188 | data[2]=0; // AFI | |
1189 | data[3]=0; // mask length | |
1190 | ||
1191 | for (int i=0;i<256;i++) { | |
1192 | data[2]=i & 0xFF; | |
1193 | datalen=AddCrc(data,4); | |
1194 | recvlen=SendDataTag(data,datalen,0,speed,&recv); | |
1195 | WDT_HIT(); | |
1196 | if (recvlen>=12) { | |
1197 | Dbprintf("AFI=%i UID=%s",i,sprintUID(NULL,&recv[2])); | |
1198 | } | |
1199 | } | |
1200 | Dbprintf("AFI Bruteforcing done."); | |
1201 | ||
1202 | } | |
1203 | ||
1204 | // Allows to directly send commands to the tag via the client | |
1205 | void DirectTag15693Command(uint32_t datalen,uint32_t speed, uint32_t recv, uint8_t data[]) { | |
1206 | ||
1207 | int recvlen=0; | |
117d9ec2 | 1208 | uint8_t *recvbuf = BigBuf_get_addr(); |
902cb3c0 | 1209 | // UsbCommand n; |
9455b51c | 1210 | |
1211 | if (DEBUG) { | |
1212 | Dbprintf("SEND"); | |
d19929cb | 1213 | Dbhexdump(datalen,data,true); |
9455b51c | 1214 | } |
1215 | ||
1216 | recvlen=SendDataTag(data,datalen,1,speed,(recv?&recvbuf:NULL)); | |
1217 | ||
1218 | if (recv) { | |
9455b51c | 1219 | LED_B_ON(); |
902cb3c0 | 1220 | cmd_send(CMD_ACK,recvlen>48?48:recvlen,0,0,recvbuf,48); |
9455b51c | 1221 | LED_B_OFF(); |
1222 | ||
1223 | if (DEBUG) { | |
1224 | Dbprintf("RECV"); | |
1225 | DbdecodeIso15693Answer(recvlen,recvbuf); | |
d19929cb | 1226 | Dbhexdump(recvlen,recvbuf,true); |
9455b51c | 1227 | } |
1228 | } | |
1229 | ||
1230 | } | |
1231 | ||
1232 | ||
1233 | ||
1234 | ||
1235 | // -------------------------------------------------------------------- | |
1236 | // -- Misc & deprecated functions | |
1237 | // -------------------------------------------------------------------- | |
1238 | ||
e6304bca | 1239 | /* |
9455b51c | 1240 | |
1241 | // do not use; has a fix UID | |
1242 | static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid) | |
1243 | { | |
1244 | uint8_t cmd[12]; | |
1245 | ||
1246 | uint16_t crc; | |
1247 | // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block | |
1248 | // followed by teh block data | |
1249 | // one sub-carrier, inventory, 1 slot, fast rate | |
1250 | cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit | |
1251 | // System Information command code | |
1252 | cmd[1] = 0x2B; | |
1253 | // UID may be optionally specified here | |
1254 | // 64-bit UID | |
1255 | cmd[2] = 0x32; | |
1256 | cmd[3]= 0x4b; | |
1257 | cmd[4] = 0x03; | |
1258 | cmd[5] = 0x01; | |
1259 | cmd[6] = 0x00; | |
1260 | cmd[7] = 0x10; | |
1261 | cmd[8] = 0x05; | |
1262 | cmd[9]= 0xe0; // always e0 (not exactly unique) | |
1263 | //Now the CRC | |
1264 | crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes | |
1265 | cmd[10] = crc & 0xff; | |
1266 | cmd[11] = crc >> 8; | |
1267 | ||
1268 | CodeIso15693AsReader(cmd, sizeof(cmd)); | |
1269 | } | |
1270 | ||
9455b51c | 1271 | |
1272 | // do not use; has a fix UID | |
1273 | static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid) | |
1274 | { | |
1275 | uint8_t cmd[14]; | |
1276 | ||
1277 | uint16_t crc; | |
1278 | // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block | |
1279 | // followed by teh block data | |
1280 | // one sub-carrier, inventory, 1 slot, fast rate | |
1281 | cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit | |
1282 | // READ Multi BLOCK command code | |
1283 | cmd[1] = 0x23; | |
1284 | // UID may be optionally specified here | |
1285 | // 64-bit UID | |
1286 | cmd[2] = 0x32; | |
1287 | cmd[3]= 0x4b; | |
1288 | cmd[4] = 0x03; | |
1289 | cmd[5] = 0x01; | |
1290 | cmd[6] = 0x00; | |
1291 | cmd[7] = 0x10; | |
1292 | cmd[8] = 0x05; | |
1293 | cmd[9]= 0xe0; // always e0 (not exactly unique) | |
1294 | // First Block number to read | |
1295 | cmd[10] = 0x00; | |
1296 | // Number of Blocks to read | |
1297 | cmd[11] = 0x2f; // read quite a few | |
1298 | //Now the CRC | |
1299 | crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes | |
1300 | cmd[12] = crc & 0xff; | |
1301 | cmd[13] = crc >> 8; | |
1302 | ||
1303 | CodeIso15693AsReader(cmd, sizeof(cmd)); | |
1304 | } | |
1305 | ||
1306 | // do not use; has a fix UID | |
1307 | static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode) | |
1308 | { | |
1309 | uint8_t cmd[14]; | |
1310 | ||
1311 | uint16_t crc; | |
1312 | // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block | |
1313 | // followed by teh block data | |
1314 | // one sub-carrier, inventory, 1 slot, fast rate | |
1315 | cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit | |
1316 | // READ BLOCK command code | |
1317 | cmd[1] = CmdCode; | |
1318 | // UID may be optionally specified here | |
1319 | // 64-bit UID | |
1320 | cmd[2] = 0x32; | |
1321 | cmd[3]= 0x4b; | |
1322 | cmd[4] = 0x03; | |
1323 | cmd[5] = 0x01; | |
1324 | cmd[6] = 0x00; | |
1325 | cmd[7] = 0x10; | |
1326 | cmd[8] = 0x05; | |
1327 | cmd[9]= 0xe0; // always e0 (not exactly unique) | |
1328 | // Parameter | |
1329 | cmd[10] = 0x00; | |
1330 | cmd[11] = 0x0a; | |
1331 | ||
1332 | // cmd[12] = 0x00; | |
1333 | // cmd[13] = 0x00; //Now the CRC | |
1334 | crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes | |
1335 | cmd[12] = crc & 0xff; | |
1336 | cmd[13] = crc >> 8; | |
1337 | ||
1338 | CodeIso15693AsReader(cmd, sizeof(cmd)); | |
1339 | } | |
1340 | ||
1341 | // do not use; has a fix UID | |
1342 | static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode) | |
1343 | { | |
1344 | uint8_t cmd[14]; | |
1345 | ||
1346 | uint16_t crc; | |
1347 | // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block | |
1348 | // followed by teh block data | |
1349 | // one sub-carrier, inventory, 1 slot, fast rate | |
1350 | cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit | |
1351 | // READ BLOCK command code | |
1352 | cmd[1] = CmdCode; | |
1353 | // UID may be optionally specified here | |
1354 | // 64-bit UID | |
1355 | cmd[2] = 0x32; | |
1356 | cmd[3]= 0x4b; | |
1357 | cmd[4] = 0x03; | |
1358 | cmd[5] = 0x01; | |
1359 | cmd[6] = 0x00; | |
1360 | cmd[7] = 0x10; | |
1361 | cmd[8] = 0x05; | |
1362 | cmd[9]= 0xe0; // always e0 (not exactly unique) | |
1363 | // Parameter | |
1364 | cmd[10] = 0x05; // for custom codes this must be manufcturer code | |
1365 | cmd[11] = 0x00; | |
1366 | ||
1367 | // cmd[12] = 0x00; | |
1368 | // cmd[13] = 0x00; //Now the CRC | |
1369 | crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes | |
1370 | cmd[12] = crc & 0xff; | |
1371 | cmd[13] = crc >> 8; | |
1372 | ||
1373 | CodeIso15693AsReader(cmd, sizeof(cmd)); | |
1374 | } | |
1375 | ||
1376 | ||
1377 | ||
1378 | ||
e6304bca | 1379 | */ |
9455b51c | 1380 | |
1381 |