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