proxmark3/armsrc/BigBuf.c

341 lines
9.3 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// Jonathan Westhues, Aug 2005
// Gerhard de Koning Gans, April 2008, May 2011
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// BigBuf and functions to allocate/free parts of it.
//-----------------------------------------------------------------------------
2016-07-07 16:01:50 +08:00
#include "BigBuf.h"
#include "string.h"
#include "dbprint.h"
2020-05-19 08:39:39 +08:00
#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.
// We know that bss is aligned to 4 bytes.
2020-06-20 00:34:47 +08:00
static uint8_t *BigBuf = &__bss_end__;
/* BigBuf memory layout:
2020-07-10 22:37:56 +08:00
Pointer to highest available memory: s_bigbuf_hi
high s_bigbuf_size
reserved = BigBuf_malloc() subtracts amount from s_bigbuf_hi,
2019-03-10 03:34:41 +08:00
low 0x00
*/
2020-07-13 18:26:26 +08:00
static uint32_t s_bigbuf_size = 0;
// High memory mark
2020-07-10 22:37:56 +08:00
static uint32_t s_bigbuf_hi = 0;
// pointer to the emulator memory.
static uint8_t *emulator_memory = NULL;
//=============================================================================
// The ToSend buffer.
// A buffer where we can queue things up to be sent through the FPGA, for
// any purpose (fake tag, as reader, whatever). We go MSB first, since that
// is the order in which they go out on the wire.
//=============================================================================
static tosend_t toSend = {
.max = -1,
.bit = 8,
.buf = NULL
};
//=============================================================================
// The dmaBuf 16bit buffer.
// A buffer where we recive IQ samples sent from the FPGA, for demodulating
//=============================================================================
static dmabuf16_t dma_16 = {
.size = DMA_BUFFER_SIZE,
.buf = NULL
};
// dmaBuf 8bit buffer
static dmabuf8_t dma_8 = {
.size = DMA_BUFFER_SIZE,
.buf = NULL
};
// trace related variables
2020-07-10 22:37:56 +08:00
static uint32_t trace_len = 0;
static bool tracing = true;
// compute the available size for BigBuf
void BigBuf_initialize(void) {
2020-07-10 22:37:56 +08:00
s_bigbuf_size = (uint32_t)&_stack_start - (uint32_t)&__bss_end__;
s_bigbuf_hi = s_bigbuf_size;
trace_len = 0;
}
// get the address of BigBuf
uint8_t *BigBuf_get_addr(void) {
2019-03-10 03:34:41 +08:00
return (uint8_t *)BigBuf;
}
uint32_t BigBuf_get_size(void) {
2020-07-10 22:37:56 +08:00
return s_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) {
2019-03-10 03:34:41 +08:00
// not yet allocated
if (emulator_memory == NULL)
emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
2019-03-10 03:34:41 +08:00
return emulator_memory;
}
2020-07-10 22:37:56 +08:00
/*
uint32_t BigBuf_get_EM_size(void) {
return CARD_MEMORY_SIZE;
}
*/
// clear ALL of BigBuf
void BigBuf_Clear(void) {
2019-03-10 03:34:41 +08:00
BigBuf_Clear_ext(true);
}
// clear ALL of BigBuf
void BigBuf_Clear_ext(bool verbose) {
2020-07-10 22:37:56 +08:00
memset(BigBuf, 0, s_bigbuf_size);
clear_trace();
2019-03-10 03:34:41 +08:00
if (verbose)
2020-07-10 22:37:56 +08:00
Dbprintf("Buffer cleared (%i bytes)", s_bigbuf_size);
}
void BigBuf_Clear_EM(void) {
2019-03-10 03:34:41 +08:00
memset(BigBuf_get_EM_addr(), 0, CARD_MEMORY_SIZE);
2017-07-07 18:52:51 +08:00
}
void BigBuf_Clear_keep_EM(void) {
2020-07-10 22:37:56 +08:00
memset(BigBuf, 0, s_bigbuf_hi);
}
// 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) {
2020-07-10 22:37:56 +08:00
if (s_bigbuf_hi < chunksize)
2019-03-10 03:34:41 +08:00
return NULL; // no memory left
2018-09-05 02:36:25 +08:00
2019-03-10 03:34:41 +08:00
chunksize = (chunksize + 3) & 0xfffc; // round to next multiple of 4
2020-07-10 22:37:56 +08:00
s_bigbuf_hi -= chunksize; // aligned to 4 Byte boundary
return (uint8_t *)BigBuf + s_bigbuf_hi;
}
2020-11-07 08:32:43 +08:00
// allocate a chunk of memory from BigBuf, and returns a pointer to it.
// sets the memory to zero
uint8_t *BigBuf_calloc(uint16_t chunksize) {
uint8_t *mem = BigBuf_malloc(chunksize);
if (mem != NULL) {
memset(mem, 0x00, chunksize);
}
2020-11-07 08:32:43 +08:00
return mem;
}
// free ALL allocated chunks. The whole BigBuf is available for traces or samples again.
void BigBuf_free(void) {
2020-07-10 22:37:56 +08:00
s_bigbuf_hi = s_bigbuf_size;
2019-03-10 03:34:41 +08:00
emulator_memory = NULL;
// shouldn't this empty BigBuf also?
toSend.buf = NULL;
dma_16.buf = NULL;
dma_8.buf = NULL;
}
// free allocated chunks EXCEPT the emulator memory
void BigBuf_free_keep_EM(void) {
2019-03-10 03:34:41 +08:00
if (emulator_memory != NULL)
2020-07-10 22:37:56 +08:00
s_bigbuf_hi = emulator_memory - (uint8_t *)BigBuf;
2019-03-10 03:34:41 +08:00
else
2020-07-10 22:37:56 +08:00
s_bigbuf_hi = s_bigbuf_size;
toSend.buf = NULL;
dma_16.buf = NULL;
dma_8.buf = NULL;
}
void BigBuf_print_status(void) {
2020-06-12 01:20:59 +08:00
DbpString(_CYAN_("Memory"));
2020-07-10 22:37:56 +08:00
Dbprintf(" BigBuf_size.............%d", s_bigbuf_size);
Dbprintf(" Available memory........%d", s_bigbuf_hi);
2020-06-12 01:20:59 +08:00
DbpString(_CYAN_("Tracing"));
2019-03-10 03:34:41 +08:00
Dbprintf(" tracing ................%d", tracing);
2020-07-10 22:37:56 +08:00
Dbprintf(" traceLen ...............%d", trace_len);
Dbprintf(" dma8 memory.............%d", dma_8.buf - BigBuf_get_addr());
2020-08-13 18:25:04 +08:00
Dbprintf(" dma16 memory............%d", (uint8_t *)dma_16.buf - BigBuf_get_addr());
Dbprintf(" toSend memory...........%d", toSend.buf - BigBuf_get_addr());
}
// return the maximum trace length (i.e. the unallocated size of BigBuf)
uint16_t BigBuf_max_traceLen(void) {
2020-07-10 22:37:56 +08:00
return s_bigbuf_hi;
}
void clear_trace(void) {
2020-07-10 22:37:56 +08:00
trace_len = 0;
}
2020-01-02 03:47:39 +08:00
void set_tracelen(uint32_t value) {
2020-07-10 22:37:56 +08:00
trace_len = value;
}
2020-01-02 03:47:39 +08:00
void set_tracing(bool enable) {
2019-03-10 03:34:41 +08:00
tracing = enable;
}
bool get_tracing(void) {
2019-03-10 03:34:41 +08:00
return tracing;
2017-10-24 03:17:59 +08:00
}
/**
* Get the number of bytes traced
* @return
*/
uint32_t BigBuf_get_traceLen(void) {
2020-07-10 22:37:56 +08:00
return trace_len;
}
/**
This is a function to store traces. All protocols can use this generic tracer-function.
The traces produced by calling this function can be fetched on the client-side
2021-04-16 07:53:07 +08:00
by 'hf list -t raw', alternatively 'hf list -t <proto>' for protocol-specific
annotation of commands/responses.
**/
bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag) {
2020-07-10 01:40:54 +08:00
if (tracing == false) {
return false;
}
2019-03-10 03:34:41 +08:00
uint8_t *trace = BigBuf_get_addr();
2020-07-10 22:37:56 +08:00
tracelog_hdr_t *hdr = (tracelog_hdr_t *)(trace + trace_len);
2019-03-10 03:34:41 +08:00
2019-03-10 07:00:59 +08:00
uint32_t num_paritybytes = (iLen - 1) / 8 + 1; // number of valid paritybytes in *parity
2019-03-10 03:34:41 +08:00
// Return when trace is full
2020-07-10 22:37:56 +08:00
if (TRACELOG_HDR_LEN + iLen + num_paritybytes >= BigBuf_max_traceLen() - trace_len) {
tracing = false;
2019-03-10 03:34:41 +08:00
return false;
}
uint32_t duration;
if (timestamp_end > timestamp_start) {
duration = timestamp_end - timestamp_start;
} else {
duration = (UINT32_MAX - timestamp_start) + timestamp_end;
2019-03-10 03:34:41 +08:00
}
if (duration > 0xFFFF) {
2020-07-14 04:05:11 +08:00
/*
if (DBGLEVEL >= DBG_DEBUG) {
Dbprintf("Error in LogTrace: duration too long for 16 bits encoding: 0x%08x start: 0x%08x end: 0x%08x", duration, timestamp_start, timestamp_end);
}
2020-07-14 04:05:11 +08:00
*/
duration = 0;
}
2020-08-13 18:25:04 +08:00
2020-05-19 08:39:39 +08:00
hdr->timestamp = timestamp_start;
hdr->duration = duration & 0xFFFF;
2020-05-19 08:39:39 +08:00
hdr->data_len = iLen;
hdr->isResponse = !readerToTag;
2020-07-10 22:37:56 +08:00
trace_len += TRACELOG_HDR_LEN;
2019-03-10 03:34:41 +08:00
// data bytes
if (btBytes != NULL && iLen != 0) {
memcpy(hdr->frame, btBytes, iLen);
trace_len += iLen;
2019-03-10 03:34:41 +08:00
}
// parity bytes
if (num_paritybytes != 0) {
if (parity != NULL) {
2020-07-10 22:37:56 +08:00
memcpy(trace + trace_len, parity, num_paritybytes);
2019-03-10 03:34:41 +08:00
} else {
2020-07-10 22:37:56 +08:00
memset(trace + trace_len, 0x00, num_paritybytes);
2019-03-10 03:34:41 +08:00
}
trace_len += num_paritybytes;
2019-03-10 03:34:41 +08:00
}
return true;
}
// specific LogTrace function for ISO15693: the duration needs to be scaled because otherwise it won't fit into a uint16_t
bool LogTrace_ISO15693(const uint8_t *bytes, uint16_t len, uint32_t ts_start, uint32_t ts_end, uint8_t *parity, bool reader2tag) {
uint32_t duration = ts_end - ts_start;
duration /= 32;
ts_end = ts_start + duration;
return LogTrace(bytes, len, ts_start, ts_end, parity, reader2tag);
}
// Emulator memory
uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length) {
2019-03-10 07:00:59 +08:00
uint8_t *mem = BigBuf_get_EM_addr();
2019-03-10 03:34:41 +08:00
if (offset + length < CARD_MEMORY_SIZE) {
2019-03-10 07:00:59 +08:00
memcpy(mem + offset, data, length);
2019-03-10 03:34:41 +08:00
return 0;
}
Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset + length), CARD_MEMORY_SIZE);
return 1;
}
2020-07-13 23:56:19 +08:00
// get the address of the ToSend buffer. Allocate part of Bigbuf for it, if not yet done
tosend_t *get_tosend(void) {
if (toSend.buf == NULL)
toSend.buf = BigBuf_malloc(TOSEND_BUFFER_SIZE);
return &toSend;
}
void tosend_reset(void) {
toSend.max = -1;
toSend.bit = 8;
}
void tosend_stuffbit(int b) {
2020-09-28 23:46:12 +08:00
if (toSend.max >= TOSEND_BUFFER_SIZE - 1) {
Dbprintf(_RED_("toSend overflow"));
return;
}
2020-09-30 20:27:19 +08:00
2020-07-13 23:56:19 +08:00
if (toSend.bit >= 8) {
toSend.max++;
toSend.buf[toSend.max] = 0;
toSend.bit = 0;
}
if (b)
2020-09-28 23:46:12 +08:00
toSend.buf[toSend.max] |= (1 << (7 - toSend.bit));
2020-07-13 23:56:19 +08:00
toSend.bit++;
if (toSend.max >= TOSEND_BUFFER_SIZE) {
toSend.bit = 0;
}
}
dmabuf16_t *get_dma16(void) {
if (dma_16.buf == NULL)
2020-08-13 18:25:04 +08:00
dma_16.buf = (uint16_t *)BigBuf_malloc(DMA_BUFFER_SIZE * sizeof(uint16_t));
return &dma_16;
}
dmabuf8_t *get_dma8(void) {
if (dma_8.buf == NULL)
dma_8.buf = BigBuf_malloc(DMA_BUFFER_SIZE);
return &dma_8;
}