]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/BigBuf.c
CHG: change the number of calls to prng_successor was done. Fewer should mean faster :)
[proxmark3-svn] / armsrc / BigBuf.c
1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Aug 2005
3 // Gerhard de Koning Gans, April 2008, May 2011
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // BigBuf and functions to allocate/free parts of it.
10 //-----------------------------------------------------------------------------
11 #include "BigBuf.h"
12
13 // BigBuf is the large multi-purpose buffer, typically used to hold A/D samples or traces.
14 // Also used to hold various smaller buffers and the Mifare Emulator Memory.
15 // declare it as uint32_t to achieve alignment to 4 Byte boundary
16 static uint32_t BigBuf[BIGBUF_SIZE/sizeof(uint32_t)];
17
18 /* BigBuf memory layout:
19 Pointer to highest available memory: BigBuf_hi
20
21 high BIGBUF_SIZE
22 reserved = BigBuf_malloc() subtracts amount from BigBuf_hi,
23 low 0x00
24 */
25
26 // High memory mark
27 static uint16_t BigBuf_hi = BIGBUF_SIZE;
28
29 // pointer to the emulator memory.
30 static uint8_t *emulator_memory = NULL;
31
32 // trace related variables
33 static uint16_t traceLen = 0;
34 int tracing = 1; //Last global one.. todo static?
35
36 // get the address of BigBuf
37 uint8_t *BigBuf_get_addr(void)
38 {
39 return (uint8_t *)BigBuf;
40 }
41
42 // get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
43 uint8_t *BigBuf_get_EM_addr(void)
44 {
45 // not yet allocated
46 if (emulator_memory == NULL)
47 emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
48
49 return emulator_memory;
50 }
51
52 // clear ALL of BigBuf
53 void BigBuf_Clear(void)
54 {
55 BigBuf_Clear_ext(true);
56 }
57
58 // clear ALL of BigBuf
59 void BigBuf_Clear_ext(bool verbose)
60 {
61 memset(BigBuf, 0, BIGBUF_SIZE);
62 if (verbose)
63 Dbprintf("Buffer cleared (%i bytes)", BIGBUF_SIZE);
64 }
65
66 void BigBuf_Clear_keep_EM(void)
67 {
68 memset(BigBuf, 0, BigBuf_hi);
69 }
70
71 // allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory
72 // at the beginning of BigBuf is always for traces/samples
73 uint8_t *BigBuf_malloc(uint16_t chunksize)
74 {
75 if (BigBuf_hi - chunksize < 0) {
76 return NULL; // no memory left
77 } else {
78 chunksize = (chunksize + 3) & 0xfffc; // round to next multiple of 4
79 BigBuf_hi -= chunksize; // aligned to 4 Byte boundary
80 return (uint8_t *)BigBuf + BigBuf_hi;
81 }
82 }
83
84 // free ALL allocated chunks. The whole BigBuf is available for traces or samples again.
85 void BigBuf_free(void)
86 {
87 BigBuf_hi = BIGBUF_SIZE;
88 emulator_memory = NULL;
89
90 // shouldn't this empty BigBuf also?
91 }
92
93 // free allocated chunks EXCEPT the emulator memory
94 void BigBuf_free_keep_EM(void)
95 {
96 if (emulator_memory != NULL)
97 BigBuf_hi = emulator_memory - (uint8_t *)BigBuf;
98 else
99 BigBuf_hi = BIGBUF_SIZE;
100
101 // shouldn't this empty BigBuf also?
102 }
103
104 void BigBuf_print_status(void)
105 {
106 Dbprintf("Memory");
107 Dbprintf(" BIGBUF_SIZE.............%d", BIGBUF_SIZE);
108 Dbprintf(" Available memory........%d", BigBuf_hi);
109 Dbprintf("Tracing");
110 Dbprintf(" tracing ................%d", tracing);
111 Dbprintf(" traceLen ...............%d", traceLen);
112 }
113
114 // return the maximum trace length (i.e. the unallocated size of BigBuf)
115 uint16_t BigBuf_max_traceLen(void)
116 {
117 return BigBuf_hi;
118 }
119
120 void clear_trace() {
121 traceLen = 0;
122 }
123
124 void set_tracing(bool enable) {
125 tracing = enable;
126 }
127
128 /**
129 * Get the number of bytes traced
130 * @return
131 */
132 uint16_t BigBuf_get_traceLen(void)
133 {
134 return traceLen;
135 }
136
137 /**
138 This is a function to store traces. All protocols can use this generic tracer-function.
139 The traces produced by calling this function can be fetched on the client-side
140 by 'hf list raw', alternatively 'hf list <proto>' for protocol-specific
141 annotation of commands/responses.
142
143 **/
144 bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag)
145 {
146 if (!tracing) return FALSE;
147
148 uint8_t *trace = BigBuf_get_addr();
149
150 uint16_t num_paritybytes = (iLen-1)/8 + 1; // number of valid paritybytes in *parity
151 uint16_t duration = timestamp_end - timestamp_start;
152
153 // Return when trace is full
154 if (traceLen + sizeof(iLen) + sizeof(timestamp_start) + sizeof(duration) + num_paritybytes + iLen >= BigBuf_max_traceLen()) {
155 tracing = FALSE; // don't trace any more
156 return FALSE;
157 }
158 // Traceformat:
159 // 32 bits timestamp (little endian)
160 // 16 bits duration (little endian)
161 // 16 bits data length (little endian, Highest Bit used as readerToTag flag)
162 // y Bytes data
163 // x Bytes parity (one byte per 8 bytes data)
164
165 // timestamp (start)
166 trace[traceLen++] = ((timestamp_start >> 0) & 0xff);
167 trace[traceLen++] = ((timestamp_start >> 8) & 0xff);
168 trace[traceLen++] = ((timestamp_start >> 16) & 0xff);
169 trace[traceLen++] = ((timestamp_start >> 24) & 0xff);
170
171 // duration
172 trace[traceLen++] = ((duration >> 0) & 0xff);
173 trace[traceLen++] = ((duration >> 8) & 0xff);
174
175 // data length
176 trace[traceLen++] = ((iLen >> 0) & 0xff);
177 trace[traceLen++] = ((iLen >> 8) & 0xff);
178
179 // readerToTag flag
180 if (!readerToTag) {
181 trace[traceLen - 1] |= 0x80;
182 }
183
184 // data bytes
185 if (btBytes != NULL && iLen != 0) {
186 memcpy(trace + traceLen, btBytes, iLen);
187 }
188 traceLen += iLen;
189
190 // parity bytes
191 if (num_paritybytes != 0) {
192 if (parity != NULL) {
193 memcpy(trace + traceLen, parity, num_paritybytes);
194 } else {
195 memset(trace + traceLen, 0x00, num_paritybytes);
196 }
197 }
198 traceLen += num_paritybytes;
199
200 return TRUE;
201 }
202
203
204 int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwParity, int readerToTag)
205 {
206 /**
207 Todo, rewrite the logger to use the generic functionality instead. It should be noted, however,
208 that this logger takes number of bits as argument, not number of bytes.
209 **/
210
211 if (!tracing) return FALSE;
212
213 uint8_t *trace = BigBuf_get_addr();
214 uint16_t iLen = nbytes(iBits);
215 // Return when trace is full
216 if (traceLen + sizeof(rsamples) + sizeof(dwParity) + sizeof(iBits) + iLen > BigBuf_max_traceLen()) return FALSE;
217
218 //Hitag traces appear to use this traceformat:
219 // 32 bits timestamp (little endian,Highest Bit used as readerToTag flag)
220 // 32 bits parity
221 // 8 bits size (number of bits in the trace entry, not number of bytes)
222 // y Bytes data
223
224 rsamples += iSamples;
225 trace[traceLen++] = ((rsamples >> 0) & 0xff);
226 trace[traceLen++] = ((rsamples >> 8) & 0xff);
227 trace[traceLen++] = ((rsamples >> 16) & 0xff);
228 trace[traceLen++] = ((rsamples >> 24) & 0xff);
229
230 if (!readerToTag) {
231 trace[traceLen - 1] |= 0x80;
232 }
233
234 trace[traceLen++] = ((dwParity >> 0) & 0xff);
235 trace[traceLen++] = ((dwParity >> 8) & 0xff);
236 trace[traceLen++] = ((dwParity >> 16) & 0xff);
237 trace[traceLen++] = ((dwParity >> 24) & 0xff);
238 trace[traceLen++] = iBits;
239
240 memcpy(trace + traceLen, btBytes, iLen);
241 traceLen += iLen;
242
243 return TRUE;
244 }
245
246
247 // Emulator memory
248 uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length){
249 uint8_t* mem = BigBuf_get_EM_addr();
250 if(offset+length < CARD_MEMORY_SIZE) {
251 memcpy(mem+offset, data, length);
252 return 0;
253 } else {
254 Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset+length), CARD_MEMORY_SIZE);
255 return 1;
256 }
257 }
Impressum, Datenschutz