]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/BigBuf.c
1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Aug 2005
3 // Gerhard de Koning Gans, April 2008, May 2011
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // BigBuf and functions to allocate/free parts of it.
10 //-----------------------------------------------------------------------------
13 #include "proxmark3.h"
17 // BigBuf is the large multi-purpose buffer, typically used to hold A/D samples or traces.
18 // Also used to hold various smaller buffers and the Mifare Emulator Memory.
20 // declare it as uint32_t to achieve alignment to 4 Byte boundary
21 static uint32_t BigBuf
[BIGBUF_SIZE
/sizeof(uint32_t)];
24 static uint16_t BigBuf_hi
= BIGBUF_SIZE
;
26 // pointer to the emulator memory.
27 static uint8_t *emulator_memory
= NULL
;
29 // trace related variables
30 static uint16_t traceLen
= 0;
31 int tracing
= 1; //Last global one.. todo static?
33 // get the address of BigBuf
34 uint8_t *BigBuf_get_addr(void)
36 return (uint8_t *)BigBuf
;
40 // get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
41 uint8_t *BigBuf_get_EM_addr(void)
43 if (emulator_memory
== NULL
) { // not yet allocated
44 emulator_memory
= BigBuf_malloc(CARD_MEMORY_SIZE
);
47 return emulator_memory
;
51 // clear ALL of BigBuf
52 void BigBuf_Clear(void)
54 memset(BigBuf
,0,BIGBUF_SIZE
);
55 Dbprintf("Buffer cleared (%i bytes)",BIGBUF_SIZE
);
59 // allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory
60 // at the beginning of BigBuf is always for traces/samples
61 uint8_t *BigBuf_malloc(uint16_t chunksize
)
63 if (BigBuf_hi
- chunksize
< 0) {
64 return NULL
; // no memory left
66 chunksize
= (chunksize
+ 3) & 0xfffc; // round to next multiple of 4
67 BigBuf_hi
-= chunksize
; // aligned to 4 Byte boundary
68 return (uint8_t *)BigBuf
+ BigBuf_hi
;
73 // free ALL allocated chunks. The whole BigBuf is available for traces or samples again.
74 void BigBuf_free(void)
76 BigBuf_hi
= BIGBUF_SIZE
;
77 emulator_memory
= NULL
;
81 // free allocated chunks EXCEPT the emulator memory
82 void BigBuf_free_keep_EM(void)
84 if (emulator_memory
!= NULL
) {
85 BigBuf_hi
= emulator_memory
- (uint8_t *)BigBuf
;
87 BigBuf_hi
= BIGBUF_SIZE
;
92 // return the maximum trace length (i.e. the unallocated size of BigBuf)
93 uint16_t BigBuf_max_traceLen(void)
102 void set_tracing(bool enable
) {
107 * Get the number of bytes traced
110 uint16_t BigBuf_get_traceLen(void)
116 This is a function to store traces. All protocols can use this generic tracer-function.
117 The traces produced by calling this function can be fetched on the client-side
118 by 'hf list raw', alternatively 'hf list <proto>' for protocol-specific
119 annotation of commands/responses.
122 bool RAMFUNC
LogTrace(const uint8_t *btBytes
, uint16_t iLen
, uint32_t timestamp_start
, uint32_t timestamp_end
, uint8_t *parity
, bool readerToTag
)
124 if (!tracing
) return FALSE
;
126 uint8_t *trace
= BigBuf_get_addr();
128 uint16_t num_paritybytes
= (iLen
-1)/8 + 1; // number of valid paritybytes in *parity
129 uint16_t duration
= timestamp_end
- timestamp_start
;
131 // Return when trace is full
132 uint16_t max_traceLen
= BigBuf_max_traceLen();
134 if (traceLen
+ sizeof(iLen
) + sizeof(timestamp_start
) + sizeof(duration
) + num_paritybytes
+ iLen
>= max_traceLen
) {
135 tracing
= FALSE
; // don't trace any more
139 // 32 bits timestamp (little endian)
140 // 16 bits duration (little endian)
141 // 16 bits data length (little endian, Highest Bit used as readerToTag flag)
143 // x Bytes parity (one byte per 8 bytes data)
146 trace
[traceLen
++] = ((timestamp_start
>> 0) & 0xff);
147 trace
[traceLen
++] = ((timestamp_start
>> 8) & 0xff);
148 trace
[traceLen
++] = ((timestamp_start
>> 16) & 0xff);
149 trace
[traceLen
++] = ((timestamp_start
>> 24) & 0xff);
152 trace
[traceLen
++] = ((duration
>> 0) & 0xff);
153 trace
[traceLen
++] = ((duration
>> 8) & 0xff);
156 trace
[traceLen
++] = ((iLen
>> 0) & 0xff);
157 trace
[traceLen
++] = ((iLen
>> 8) & 0xff);
161 trace
[traceLen
- 1] |= 0x80;
165 if (btBytes
!= NULL
&& iLen
!= 0) {
166 memcpy(trace
+ traceLen
, btBytes
, iLen
);
171 if (parity
!= NULL
&& iLen
!= 0) {
172 memcpy(trace
+ traceLen
, parity
, num_paritybytes
);
174 traceLen
+= num_paritybytes
;
180 int LogTraceHitag(const uint8_t * btBytes
, int iBits
, int iSamples
, uint32_t dwParity
, int readerToTag
)
183 Todo, rewrite the logger to use the generic functionality instead. It should be noted, however,
184 that this logger takes number of bits as argument, not number of bytes.
187 if (!tracing
) return FALSE
;
189 uint8_t *trace
= BigBuf_get_addr();
190 uint16_t iLen
= nbytes(iBits
);
191 // Return when trace is full
192 if (traceLen
+ sizeof(rsamples
) + sizeof(dwParity
) + sizeof(iBits
) + iLen
> BigBuf_max_traceLen()) return FALSE
;
194 //Hitag traces appear to use this traceformat:
195 // 32 bits timestamp (little endian,Highest Bit used as readerToTag flag)
197 // 8 bits size (number of bits in the trace entry, not number of bytes)
200 rsamples
+= iSamples
;
201 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
202 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
203 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
204 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
207 trace
[traceLen
- 1] |= 0x80;
210 trace
[traceLen
++] = ((dwParity
>> 0) & 0xff);
211 trace
[traceLen
++] = ((dwParity
>> 8) & 0xff);
212 trace
[traceLen
++] = ((dwParity
>> 16) & 0xff);
213 trace
[traceLen
++] = ((dwParity
>> 24) & 0xff);
214 trace
[traceLen
++] = iBits
;
216 memcpy(trace
+ traceLen
, btBytes
, iLen
);
222 uint8_t emlSet(uint8_t *data
, uint32_t offset
, uint32_t length
){
223 uint8_t* mem
= BigBuf_get_EM_addr();
224 if(offset
+length
< CARD_MEMORY_SIZE
)
226 memcpy(mem
+offset
, data
, length
);
230 Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset
+length
), CARD_MEMORY_SIZE
);