diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index 07f32239f..20e2e3e2d 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -14,34 +14,48 @@ #include "dbprint.h" #include "pm3_cmd.h" +extern uint8_t _stack_start, __bss_end__; + // BigBuf is the large multi-purpose buffer, typically used to hold A/D samples or traces. // Also used to hold various smaller buffers and the Mifare Emulator Memory. -// declare it as uint32_t to achieve alignment to 4 Byte boundary -static uint32_t BigBuf[BIGBUF_SIZE / sizeof(uint32_t)]; +// We know that bss is aligned to 4 bytes. +static uint8_t* BigBuf = &__bss_end__; /* BigBuf memory layout: Pointer to highest available memory: BigBuf_hi - - high BIGBUF_SIZE + high BigBuf_size reserved = BigBuf_malloc() subtracts amount from BigBuf_hi, low 0x00 */ +static uint32_t BigBuf_size = 0; + // High memory mark -static uint16_t BigBuf_hi = BIGBUF_SIZE; +static uint32_t BigBuf_hi = 0; // pointer to the emulator memory. static uint8_t *emulator_memory = NULL; // trace related variables static uint32_t traceLen = 0; -static bool tracing = true; //todo static? +static bool tracing = true; + +// compute the available size for BigBuf +void BigBuf_initialize(void) { + BigBuf_size = (uint32_t)&_stack_start - (uint32_t)&__bss_end__; + BigBuf_hi = BigBuf_size; + traceLen = 0; +} // get the address of BigBuf uint8_t *BigBuf_get_addr(void) { return (uint8_t *)BigBuf; } +uint32_t BigBuf_get_size(void) { + return BigBuf_size; +} + // get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done uint8_t *BigBuf_get_EM_addr(void) { // not yet allocated @@ -58,9 +72,9 @@ void BigBuf_Clear(void) { // clear ALL of BigBuf void BigBuf_Clear_ext(bool verbose) { - memset(BigBuf, 0, BIGBUF_SIZE); + memset(BigBuf, 0, BigBuf_size); if (verbose) - Dbprintf("Buffer cleared (%i bytes)", BIGBUF_SIZE); + Dbprintf("Buffer cleared (%i bytes)", BigBuf_size); } void BigBuf_Clear_EM(void) { @@ -74,7 +88,7 @@ void BigBuf_Clear_keep_EM(void) { // allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory // at the beginning of BigBuf is always for traces/samples uint8_t *BigBuf_malloc(uint16_t chunksize) { - if (BigBuf_hi - chunksize < 0) + if (BigBuf_hi < chunksize) return NULL; // no memory left chunksize = (chunksize + 3) & 0xfffc; // round to next multiple of 4 @@ -84,7 +98,7 @@ uint8_t *BigBuf_malloc(uint16_t chunksize) { // free ALL allocated chunks. The whole BigBuf is available for traces or samples again. void BigBuf_free(void) { - BigBuf_hi = BIGBUF_SIZE; + BigBuf_hi = BigBuf_size; emulator_memory = NULL; // shouldn't this empty BigBuf also? } @@ -94,14 +108,14 @@ void BigBuf_free_keep_EM(void) { if (emulator_memory != NULL) BigBuf_hi = emulator_memory - (uint8_t *)BigBuf; else - BigBuf_hi = BIGBUF_SIZE; + BigBuf_hi = BigBuf_size; // shouldn't this empty BigBuf also? } void BigBuf_print_status(void) { DbpString(_BLUE_("Memory")); - Dbprintf(" BIGBUF_SIZE.............%d", BIGBUF_SIZE); + Dbprintf(" BigBuf_size.............%d", BigBuf_size); Dbprintf(" Available memory........%d", BigBuf_hi); DbpString(_BLUE_("Tracing")); Dbprintf(" tracing ................%d", tracing); diff --git a/armsrc/BigBuf.h b/armsrc/BigBuf.h index ad967c4c7..60857e82a 100644 --- a/armsrc/BigBuf.h +++ b/armsrc/BigBuf.h @@ -14,7 +14,6 @@ #include "common.h" -#define BIGBUF_SIZE 40000 #define MAX_FRAME_SIZE 256 // maximum allowed ISO14443 frame #define MAX_PARITY_SIZE ((MAX_FRAME_SIZE + 7) / 8) #define MAX_MIFARE_FRAME_SIZE 18 // biggest Mifare frame is answer to a read (one block = 16 Bytes) + 2 Bytes CRC @@ -23,8 +22,10 @@ #define DMA_BUFFER_SIZE 256 //128 (how big is the dma?!? uint8_t *BigBuf_get_addr(void); +uint32_t BigBuf_get_size(void); uint8_t *BigBuf_get_EM_addr(void); uint16_t BigBuf_max_traceLen(void); +void BigBuf_initialize(void); void BigBuf_Clear(void); void BigBuf_Clear_ext(bool verbose); void BigBuf_Clear_keep_EM(void); diff --git a/armsrc/appmain.c b/armsrc/appmain.c index fd5b4853c..96f133f49 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1659,12 +1659,12 @@ static void PacketReceived(PacketCommandNG *packet) { } // offset should not be over buffer - if (payload->offset >= BIGBUF_SIZE) { + if (payload->offset >= BigBuf_get_size()) { reply_ng(CMD_LF_UPLOAD_SIM_SAMPLES, PM3_EOVFLOW, NULL, 0); break; } // ensure len bytes copied wont go past end of bigbuf - uint16_t len = MIN(BIGBUF_SIZE - payload->offset, sizeof(payload->data)); + uint16_t len = MIN(BigBuf_get_size() - payload->offset, sizeof(payload->data)); uint8_t *mem = BigBuf_get_addr(); @@ -2054,7 +2054,7 @@ static void PacketReceived(PacketCommandNG *packet) { void __attribute__((noreturn)) AppMain(void) { SpinDelay(100); - clear_trace(); + BigBuf_initialize(); if (common_area.magic != COMMON_AREA_MAGIC || common_area.version != 1) { /* Initialize common area */ diff --git a/common_arm/ldscript.common b/common_arm/ldscript.common index 1be33df95..c94d0dcff 100644 --- a/common_arm/ldscript.common +++ b/common_arm/ldscript.common @@ -9,6 +9,8 @@ ms of the GNU GPL, version 2 or, ----------------------------------------------------------------------------- */ +stacksize = DEFINED(stacksize) ? stacksize : 4K; + /* AT91SAM7S256 has 256k Flash and 64k RAM */ /* AT91SAM7S512 has 512k Flash and 64k RAM */ /* boot space = 8192bytes (0x2000) */ @@ -19,6 +21,7 @@ MEMORY bootphase2 : ORIGIN = 0x00100200, LENGTH = 0x2000 - 0x200 /* Main bootloader code, stored in Flash, executed from RAM */ osimage : ORIGIN = 0x00102000, LENGTH = 512K - 0x2000 /* Place where the main OS will end up */ ram : ORIGIN = 0x00200000, LENGTH = 64K - 0x20 /* RAM, minus small common area */ + stack : ORIGIN = 0x00200000 + 64K - 4K - 0x20, LENGTH = stacksize /* Stack */ commonarea : ORIGIN = 0x00200000 + 64K - 0x20, LENGTH = 0x20 /* Communication between bootloader and main OS */ } @@ -29,4 +32,5 @@ _bootrom_start = ORIGIN(bootphase1); _bootrom_end = ORIGIN(bootphase2) + LENGTH(bootphase2); _flash_start = ORIGIN(bootphase1); _flash_end = ORIGIN(osimage) + LENGTH(osimage); +_stack_start = ORIGIN(stack); _stack_end = ORIGIN(ram) + LENGTH(ram) - 8;