]>
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"
18 // BigBuf is the large multi-purpose buffer, typically used to hold A/D samples or traces.
19 // Also used to hold various smaller buffers and the Mifare Emulator Memory.
21 /* BigBuf memory layout:
22 Pointer to highest available memory: BigBuf_hi
25 reserved = BigBuf_malloc() subtracts amount from BigBuf_hi,
29 // declare it as uint32_t to achieve alignment to 4 Byte boundary
30 static uint32_t BigBuf
[BIGBUF_SIZE
/sizeof(uint32_t)];
33 static uint16_t BigBuf_hi
= BIGBUF_SIZE
;
35 // pointer to the emulator memory.
36 static uint8_t *emulator_memory
= NULL
;
38 // trace related variables
39 static uint32_t traceLen
= 0;
40 static bool tracing
= true;
43 // get the address of BigBuf
44 uint8_t *BigBuf_get_addr(void)
46 return (uint8_t *)BigBuf
;
50 // get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
51 uint8_t *BigBuf_get_EM_addr(void)
54 if (emulator_memory
== NULL
) {
55 emulator_memory
= BigBuf_malloc(CARD_MEMORY_SIZE
);
58 return emulator_memory
;
62 // clear ALL of BigBuf
63 void BigBuf_Clear(void)
65 BigBuf_Clear_ext(true);
69 // clear ALL of BigBuf
70 void BigBuf_Clear_ext(bool verbose
)
72 memset(BigBuf
, 0, BIGBUF_SIZE
);
74 Dbprintf("Buffer cleared (%i bytes)", BIGBUF_SIZE
);
78 void BigBuf_Clear_EM(void){
79 memset(BigBuf_get_EM_addr(), 0, CARD_MEMORY_SIZE
);
83 void BigBuf_Clear_keep_EM(void)
85 memset(BigBuf
, 0, BigBuf_hi
);
88 // allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory
89 // at the beginning of BigBuf is always for traces/samples
90 uint8_t *BigBuf_malloc(uint16_t chunksize
)
92 if (BigBuf_hi
- chunksize
< 0) {
93 return NULL
; // no memory left
95 chunksize
= (chunksize
+ 3) & 0xfffc; // round to next multiple of 4
96 BigBuf_hi
-= chunksize
; // aligned to 4 Byte boundary
97 return (uint8_t *)BigBuf
+ BigBuf_hi
;
102 // free ALL allocated chunks. The whole BigBuf is available for traces or samples again.
103 void BigBuf_free(void)
105 BigBuf_hi
= BIGBUF_SIZE
;
106 emulator_memory
= NULL
;
110 // free allocated chunks EXCEPT the emulator memory
111 void BigBuf_free_keep_EM(void)
113 if (emulator_memory
!= NULL
) {
114 BigBuf_hi
= emulator_memory
- (uint8_t *)BigBuf
;
116 BigBuf_hi
= BIGBUF_SIZE
;
120 void BigBuf_print_status(void)
123 Dbprintf(" BIGBUF_SIZE.............%d", BIGBUF_SIZE
);
124 Dbprintf(" Available memory........%d", BigBuf_hi
);
126 Dbprintf(" tracing ................%d", tracing
);
127 Dbprintf(" traceLen ...............%d", traceLen
);
131 // return the maximum trace length (i.e. the unallocated size of BigBuf)
132 uint16_t BigBuf_max_traceLen(void)
143 void set_tracing(bool enable
) {
148 bool get_tracing(void) {
154 * Get the number of bytes traced
157 uint16_t BigBuf_get_traceLen(void)
164 This is a function to store traces. All protocols can use this generic tracer-function.
165 The traces produced by calling this function can be fetched on the client-side
166 by 'hf list raw', alternatively 'hf list <proto>' for protocol-specific
167 annotation of commands/responses.
170 bool RAMFUNC
LogTrace(const uint8_t *btBytes
, uint16_t iLen
, uint32_t timestamp_start
, uint32_t timestamp_end
, uint8_t *parity
, bool readerToTag
)
172 if (!tracing
) return false;
174 uint8_t *trace
= BigBuf_get_addr();
176 uint32_t num_paritybytes
= (iLen
-1)/8 + 1; // number of valid paritybytes in *parity
177 uint32_t duration
= timestamp_end
- timestamp_start
;
179 // Return when trace is full
180 uint16_t max_traceLen
= BigBuf_max_traceLen();
182 if (traceLen
+ sizeof(iLen
) + sizeof(timestamp_start
) + sizeof(duration
) + num_paritybytes
+ iLen
>= max_traceLen
) {
183 tracing
= false; // don't trace any more
187 // 32 bits timestamp (little endian)
188 // 16 bits duration (little endian)
189 // 16 bits data length (little endian, Highest Bit used as readerToTag flag)
191 // x Bytes parity (one byte per 8 bytes data)
194 trace
[traceLen
++] = ((timestamp_start
>> 0) & 0xff);
195 trace
[traceLen
++] = ((timestamp_start
>> 8) & 0xff);
196 trace
[traceLen
++] = ((timestamp_start
>> 16) & 0xff);
197 trace
[traceLen
++] = ((timestamp_start
>> 24) & 0xff);
200 trace
[traceLen
++] = ((duration
>> 0) & 0xff);
201 trace
[traceLen
++] = ((duration
>> 8) & 0xff);
204 trace
[traceLen
++] = ((iLen
>> 0) & 0xff);
205 trace
[traceLen
++] = ((iLen
>> 8) & 0xff);
209 trace
[traceLen
- 1] |= 0x80;
213 if (btBytes
!= NULL
&& iLen
!= 0) {
214 for (int i
= 0; i
< iLen
; i
++) {
215 trace
[traceLen
++] = *btBytes
++;
220 if (num_paritybytes
!= 0) {
221 if (parity
!= NULL
) {
222 for (int i
= 0; i
< num_paritybytes
; i
++) {
223 trace
[traceLen
++] = *parity
++;
226 for (int i
= 0; i
< num_paritybytes
; i
++) {
227 trace
[traceLen
++] = 0x00;
236 int LogTraceHitag(const uint8_t * btBytes
, int iBits
, int iSamples
, uint32_t dwParity
, int readerToTag
)
239 Todo, rewrite the logger to use the generic functionality instead. It should be noted, however,
240 that this logger takes number of bits as argument, not number of bytes.
243 if (!tracing
) return false;
245 uint8_t *trace
= BigBuf_get_addr();
246 uint16_t iLen
= nbytes(iBits
);
248 // Return when trace is full
249 if (traceLen
+ sizeof(rsamples
) + sizeof(dwParity
) + sizeof(iBits
) + iLen
> BigBuf_max_traceLen()) {
253 //Hitag traces appear to use this traceformat:
254 // 32 bits timestamp (little endian,Highest Bit used as readerToTag flag)
256 // 8 bits size (number of bits in the trace entry, not number of bytes)
261 rsamples
+= iSamples
;
262 trace
[traceLen
++] = ((rsamples
>> 0) & 0xff);
263 trace
[traceLen
++] = ((rsamples
>> 8) & 0xff);
264 trace
[traceLen
++] = ((rsamples
>> 16) & 0xff);
265 trace
[traceLen
++] = ((rsamples
>> 24) & 0xff);
268 trace
[traceLen
- 1] |= 0x80;
271 trace
[traceLen
++] = ((dwParity
>> 0) & 0xff);
272 trace
[traceLen
++] = ((dwParity
>> 8) & 0xff);
273 trace
[traceLen
++] = ((dwParity
>> 16) & 0xff);
274 trace
[traceLen
++] = ((dwParity
>> 24) & 0xff);
275 trace
[traceLen
++] = iBits
;
277 for (int i
= 0; i
< iLen
; i
++) {
278 trace
[traceLen
++] = *btBytes
++;
286 uint8_t emlSet(uint8_t *data
, uint32_t offset
, uint32_t length
){
287 uint8_t* mem
= BigBuf_get_EM_addr();
288 if (offset
+length
< CARD_MEMORY_SIZE
) {
289 memcpy(mem
+offset
, data
, length
);
292 Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset
+length
), CARD_MEMORY_SIZE
);