]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/BigBuf.c
7f56e9a014290c1a4b60cbfbacfc49bc9b8da02d
[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
12 #include <stdint.h>
13 #include "proxmark3.h"
14 #include "apps.h"
15 #include "string.h"
16
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.
19
20 // declare it as uint32_t to achieve alignment to 4 Byte boundary
21 static uint32_t BigBuf[BIGBUF_SIZE/sizeof(uint32_t)];
22
23 // High memory mark
24 static uint16_t BigBuf_hi = BIGBUF_SIZE;
25
26 // pointer to the emulator memory.
27 static uint8_t *emulator_memory = NULL;
28
29 // trace related global variables
30 // (only one left). ToDo: make this static as well?
31 uint16_t traceLen = 0;
32
33
34 // get the address of BigBuf
35 uint8_t *BigBuf_get_addr(void)
36 {
37 return (uint8_t *)BigBuf;
38 }
39
40
41 // get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
42 uint8_t *BigBuf_get_EM_addr(void)
43 {
44 if (emulator_memory == NULL) { // not yet allocated
45 emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
46 }
47
48 return emulator_memory;
49 }
50
51
52 // clear ALL of BigBuf
53 void BigBuf_Clear(void)
54 {
55 memset(BigBuf,0,BIGBUF_SIZE);
56 Dbprintf("Buffer cleared (%i bytes)",BIGBUF_SIZE);
57 }
58
59
60 // allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory
61 // at the beginning of BigBuf is always for traces/samples
62 uint8_t *BigBuf_malloc(uint16_t chunksize)
63 {
64 if (BigBuf_hi - chunksize < 0) {
65 return NULL; // no memory left
66 } else {
67 chunksize = (chunksize + 3) & 0xfffc; // round to next multiple of 4
68 BigBuf_hi -= chunksize; // aligned to 4 Byte boundary
69 return (uint8_t *)BigBuf + BigBuf_hi;
70 }
71 }
72
73
74 // free ALL allocated chunks. The whole BigBuf is available for traces or samples again.
75 void BigBuf_free(void)
76 {
77 BigBuf_hi = BIGBUF_SIZE;
78 emulator_memory = NULL;
79 }
80
81
82 // free allocated chunks EXCEPT the emulator memory
83 void BigBuf_free_keep_EM(void)
84 {
85 if (emulator_memory != NULL) {
86 BigBuf_hi = emulator_memory - (uint8_t *)BigBuf;
87 } else {
88 BigBuf_hi = BIGBUF_SIZE;
89 }
90 }
91
92
93 // return the maximum trace length (i.e. the unallocated size of BigBuf)
94 uint16_t BigBuf_max_traceLen(void)
95 {
96 return BigBuf_hi;
97 }
Impressum, Datenschutz