]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/lfsampling.c
added mock of key check
[proxmark3-svn] / armsrc / lfsampling.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------
2// This code is licensed to you under the terms of the GNU GPL, version 2 or,
3// at your option, any later version. See the LICENSE.txt file for the text of
4// the license.
5//-----------------------------------------------------------------------------
6// Miscellaneous routines for low frequency sampling.
7//-----------------------------------------------------------------------------
8
9#include "proxmark3.h"
10#include "apps.h"
11#include "util.h"
12#include "string.h"
13#include "lfsampling.h"
14#include "usb_cdc.h" // for usb_poll_validate_length
15//#include "ticks.h" // for StartTicks
16
17sample_config config = { 1, 8, 1, 95, 0 } ;
18
19void printConfig()
20{
21 Dbprintf("LF Sampling config: ");
22 Dbprintf(" [q] divisor: %d ", config.divisor);
23 Dbprintf(" [b] bps: %d ", config.bits_per_sample);
24 Dbprintf(" [d] decimation: %d ", config.decimation);
25 Dbprintf(" [a] averaging: %d ", config.averaging);
26 Dbprintf(" [t] trigger threshold: %d ", config.trigger_threshold);
27}
28
29
30/**
31 * Called from the USB-handler to set the sampling configuration
32 * The sampling config is used for std reading and snooping.
33 *
34 * Other functions may read samples and ignore the sampling config,
35 * such as functions to read the UID from a prox tag or similar.
36 *
37 * Values set to '0' implies no change (except for averaging)
38 * @brief setSamplingConfig
39 * @param sc
40 */
41void setSamplingConfig(sample_config *sc)
42{
43 if(sc->divisor != 0) config.divisor = sc->divisor;
44 if(sc->bits_per_sample!= 0) config.bits_per_sample= sc->bits_per_sample;
45 if(sc->decimation!= 0) config.decimation= sc->decimation;
46 if(sc->trigger_threshold != -1) config.trigger_threshold= sc->trigger_threshold;
47
48 config.averaging= sc->averaging;
49 if(config.bits_per_sample > 8) config.bits_per_sample = 8;
50 if(config.decimation < 1) config.decimation = 1;
51
52 printConfig();
53}
54
55sample_config* getSamplingConfig()
56{
57 return &config;
58}
59
60typedef struct {
61 uint8_t * buffer;
62 uint32_t numbits;
63 uint32_t position;
64} BitstreamOut;
65
66/**
67 * @brief Pushes bit onto the stream
68 * @param stream
69 * @param bit
70 */
71void pushBit( BitstreamOut* stream, uint8_t bit)
72{
73 int bytepos = stream->position >> 3; // divide by 8
74 int bitpos = stream->position & 7;
75 *(stream->buffer+bytepos) |= (bit > 0) << (7 - bitpos);
76 stream->position++;
77 stream->numbits++;
78}
79
80/**
81* Setup the FPGA to listen for samples. This method downloads the FPGA bitstream
82* if not already loaded, sets divisor and starts up the antenna.
83* @param divisor : 1, 88> 255 or negative ==> 134.8 KHz
84* 0 or 95 ==> 125 KHz
85*
86**/
87void LFSetupFPGAForADC(int divisor, bool lf_field)
88{
89 FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
90 if ( (divisor == 1) || (divisor < 0) || (divisor > 255) )
91 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz
92 else if (divisor == 0)
93 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
94 else
95 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, divisor);
96
97 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | (lf_field ? FPGA_LF_ADC_READER_FIELD : 0));
98
99 // Connect the A/D to the peak-detected low-frequency path.
100 SetAdcMuxFor(GPIO_MUXSEL_LOPKD);
101 // Give it a bit of time for the resonant antenna to settle.
102 SpinDelay(50);
103 // Now set up the SSC to get the ADC samples that are now streaming at us.
104 FpgaSetupSsc();
105}
106
107/**
108 * Does the sample acquisition. If threshold is specified, the actual sampling
109 * is not commenced until the threshold has been reached.
110 * This method implements decimation and quantization in order to
111 * be able to provide longer sample traces.
112 * Uses the following global settings:
113 * @param decimation - how much should the signal be decimated. A decimation of N means we keep 1 in N samples, etc.
114 * @param bits_per_sample - bits per sample. Max 8, min 1 bit per sample.
115 * @param averaging If set to true, decimation will use averaging, so that if e.g. decimation is 3, the sample
116 * value that will be used is the average value of the three samples.
117 * @param trigger_threshold - a threshold. The sampling won't commence until this threshold has been reached. Set
118 * to -1 to ignore threshold.
119 * @param silent - is true, now outputs are made. If false, dbprints the status
120 * @return the number of bits occupied by the samples.
121 */
122uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averaging, int trigger_threshold, bool silent, int bufsize, int cancel_after)
123{
124 //.
125 uint8_t *dest = BigBuf_get_addr();
126 bufsize = (bufsize > 0 && bufsize < BigBuf_max_traceLen()) ? bufsize : BigBuf_max_traceLen();
127
128 //memset(dest, 0, bufsize); //creates issues with cmdread (marshmellow)
129
130 if(bits_per_sample < 1) bits_per_sample = 1;
131 if(bits_per_sample > 8) bits_per_sample = 8;
132
133 if(decimation < 1) decimation = 1;
134
135 // Use a bit stream to handle the output
136 BitstreamOut data = { dest , 0, 0};
137 int sample_counter = 0;
138 uint8_t sample = 0;
139 //If we want to do averaging
140 uint32_t sample_sum =0 ;
141 uint32_t sample_total_numbers =0 ;
142 uint32_t sample_total_saved =0 ;
143 uint32_t cancel_counter = 0;
144
145 while(!BUTTON_PRESS() && !usb_poll_validate_length() ) {
146 WDT_HIT();
147 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
148 AT91C_BASE_SSC->SSC_THR = 0x43;
149 LED_D_ON();
150 }
151 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) {
152 sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
153 LED_D_OFF();
154 // threshold either high or low values 128 = center 0. if trigger = 178
155 if ((trigger_threshold > 0) && (sample < (trigger_threshold+128)) && (sample > (128-trigger_threshold))) { //
156 if (cancel_after > 0) {
157 cancel_counter++;
158 if (cancel_after == cancel_counter) break;
159 }
160 continue;
161 }
162 trigger_threshold = 0;
163 sample_total_numbers++;
164
165 if(averaging)
166 {
167 sample_sum += sample;
168 }
169 //Check decimation
170 if(decimation > 1)
171 {
172 sample_counter++;
173 if(sample_counter < decimation) continue;
174 sample_counter = 0;
175 }
176 //Averaging
177 if(averaging && decimation > 1) {
178 sample = sample_sum / decimation;
179 sample_sum =0;
180 }
181 //Store the sample
182 sample_total_saved ++;
183 if(bits_per_sample == 8){
184 dest[sample_total_saved-1] = sample;
185 data.numbits = sample_total_saved << 3;//Get the return value correct
186 if(sample_total_saved >= bufsize) break;
187 }
188 else{
189 pushBit(&data, sample & 0x80);
190 if(bits_per_sample > 1) pushBit(&data, sample & 0x40);
191 if(bits_per_sample > 2) pushBit(&data, sample & 0x20);
192 if(bits_per_sample > 3) pushBit(&data, sample & 0x10);
193 if(bits_per_sample > 4) pushBit(&data, sample & 0x08);
194 if(bits_per_sample > 5) pushBit(&data, sample & 0x04);
195 if(bits_per_sample > 6) pushBit(&data, sample & 0x02);
196 //Not needed, 8bps is covered above
197 //if(bits_per_sample > 7) pushBit(&data, sample & 0x01);
198 if((data.numbits >> 3) +1 >= bufsize) break;
199 }
200 }
201 }
202
203 if(!silent)
204 {
205 Dbprintf("Done, saved %d out of %d seen samples at %d bits/sample",sample_total_saved, sample_total_numbers,bits_per_sample);
206 Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
207 dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
208 }
209 return data.numbits;
210}
211/**
212 * @brief Does sample acquisition, ignoring the config values set in the sample_config.
213 * This method is typically used by tag-specific readers who just wants to read the samples
214 * the normal way
215 * @param trigger_threshold
216 * @param silent
217 * @return number of bits sampled
218 */
219uint32_t DoAcquisition_default(int trigger_threshold, bool silent)
220{
221 return DoAcquisition(1,8,0,trigger_threshold,silent,0,0);
222}
223uint32_t DoAcquisition_config(bool silent, int sample_size)
224{
225 return DoAcquisition(config.decimation
226 ,config.bits_per_sample
227 ,config.averaging
228 ,config.trigger_threshold
229 ,silent
230 ,sample_size
231 ,0);
232}
233
234uint32_t DoPartialAcquisition(int trigger_threshold, bool silent, int sample_size, int cancel_after) {
235 return DoAcquisition(1,8,0,trigger_threshold,silent,sample_size,cancel_after);
236}
237
238uint32_t ReadLF(bool activeField, bool silent, int sample_size)
239{
240 if (!silent) printConfig();
241 LFSetupFPGAForADC(config.divisor, activeField);
242 // Now call the acquisition routine
243 return DoAcquisition_config(silent, sample_size);
244}
245
246/**
247* Initializes the FPGA for reader-mode (field on), and acquires the samples.
248* @return number of bits sampled
249**/
250uint32_t SampleLF(bool printCfg, int sample_size)
251{
252 uint32_t ret = ReadLF(true, printCfg, sample_size);
253 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
254 return ret;
255}
256/**
257* Initializes the FPGA for snoop-mode (field off), and acquires the samples.
258* @return number of bits sampled
259**/
260
261uint32_t SnoopLF()
262{
263 uint32_t ret = ReadLF(false, true, 0);
264 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
265 return ret;
266}
267
268/**
269* acquisition of Cotag LF signal. Similart to other LF, since the Cotag has such long datarate RF/384
270* and is Manchester?, we directly gather the manchester data into bigbuff
271**/
272#define COTAG_T1 384
273#define COTAG_T2 (COTAG_T1>>1)
274#define COTAG_ONE_THRESHOLD 128+30
275#define COTAG_ZERO_THRESHOLD 128-30
276#ifndef COTAG_BITS
277#define COTAG_BITS 264
278#endif
279void doCotagAcquisition(size_t sample_size) {
280
281 uint8_t *dest = BigBuf_get_addr();
282 uint16_t bufsize = BigBuf_max_traceLen();
283
284 if ( bufsize > sample_size )
285 bufsize = sample_size;
286
287 dest[0] = 0;
288 uint8_t sample = 0, firsthigh = 0, firstlow = 0;
289 uint16_t i = 0;
290
291 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (i < bufsize) ) {
292 WDT_HIT();
293 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
294 AT91C_BASE_SSC->SSC_THR = 0x43;
295 LED_D_ON();
296 }
297
298 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) {
299 sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
300 LED_D_OFF();
301
302 // find first peak
303 if ( !firsthigh ) {
304 if (sample < COTAG_ONE_THRESHOLD)
305 continue;
306 firsthigh = 1;
307 }
308 if ( !firstlow ){
309 if (sample > COTAG_ZERO_THRESHOLD )
310 continue;
311 firstlow = 1;
312 }
313
314 ++i;
315
316 if ( sample > COTAG_ONE_THRESHOLD)
317 dest[i] = 255;
318 else if ( sample < COTAG_ZERO_THRESHOLD)
319 dest[i] = 0;
320 else
321 dest[i] = dest[i-1];
322 }
323 }
324}
325
326uint32_t doCotagAcquisitionManchester() {
327
328 uint8_t *dest = BigBuf_get_addr();
329 uint16_t bufsize = BigBuf_max_traceLen();
330
331 if ( bufsize > COTAG_BITS )
332 bufsize = COTAG_BITS;
333
334 dest[0] = 0;
335 uint8_t sample = 0, firsthigh = 0, firstlow = 0;
336 uint16_t sample_counter = 0, period = 0;
337 uint8_t curr = 0, prev = 0;
338 uint16_t noise_counter = 0;
339 while (!BUTTON_PRESS() && !usb_poll_validate_length() && (sample_counter < bufsize) && (noise_counter < (COTAG_T1<<1)) ) {
340 WDT_HIT();
341 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
342 AT91C_BASE_SSC->SSC_THR = 0x43;
343 LED_D_ON();
344 }
345
346 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) {
347 sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
348 LED_D_OFF();
349
350 // find first peak
351 if ( !firsthigh ) {
352 if (sample < COTAG_ONE_THRESHOLD) {
353 noise_counter++;
354 continue;
355 }
356 noise_counter = 0;
357 firsthigh = 1;
358 }
359
360 if ( !firstlow ){
361 if (sample > COTAG_ZERO_THRESHOLD ) {
362 noise_counter++;
363 continue;
364 }
365 noise_counter=0;
366 firstlow = 1;
367 }
368
369 // set sample 255, 0, or previous
370 if ( sample > COTAG_ONE_THRESHOLD){
371 prev = curr;
372 curr = 1;
373 }
374 else if ( sample < COTAG_ZERO_THRESHOLD) {
375 prev = curr;
376 curr = 0;
377 }
378 else {
379 curr = prev;
380 }
381
382 // full T1 periods,
383 if ( period > 0 ) {
384 --period;
385 continue;
386 }
387
388 dest[sample_counter] = curr;
389 ++sample_counter;
390 period = COTAG_T1;
391 }
392 }
393 return sample_counter;
394}
Impressum, Datenschutz