// 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.
+/* BigBuf memory layout:
+Pointer to highest available memory: BigBuf_hi
+
+ high BIGBUF_SIZE
+ reserved = BigBuf_malloc() subtracts amount from BigBuf_hi,
+ low 0x00
+*/
+
// declare it as uint32_t to achieve alignment to 4 Byte boundary
static uint32_t BigBuf[BIGBUF_SIZE/sizeof(uint32_t)];
// get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
uint8_t *BigBuf_get_EM_addr(void)
{
- if (emulator_memory == NULL) { // not yet allocated
+ // not yet allocated
+ if (emulator_memory == NULL) {
emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
}
if (verbose)
Dbprintf("Buffer cleared (%i bytes)",BIGBUF_SIZE);
}
+void BigBuf_Clear_EM(void){
+ memset(BigBuf_get_EM_addr(), 0, CARD_MEMORY_SIZE);
+}
void BigBuf_Clear_keep_EM(void)
{
{
Dbprintf("Memory");
Dbprintf(" BIGBUF_SIZE.............%d", BIGBUF_SIZE);
- Dbprintf(" BigBuf_hi .............%d", BigBuf_hi);
+ Dbprintf(" Available memory........%d", BigBuf_hi);
Dbprintf("Tracing");
Dbprintf(" tracing ................%d", tracing);
Dbprintf(" traceLen ...............%d", traceLen);
**/
bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag)
{
- if (!tracing) return FALSE;
+ if (!tracing) return false;
uint8_t *trace = BigBuf_get_addr();
uint16_t max_traceLen = BigBuf_max_traceLen();
if (traceLen + sizeof(iLen) + sizeof(timestamp_start) + sizeof(duration) + num_paritybytes + iLen >= max_traceLen) {
- tracing = FALSE; // don't trace any more
- return FALSE;
+ tracing = false; // don't trace any more
+ return false;
}
// Traceformat:
// 32 bits timestamp (little endian)
}
traceLen += num_paritybytes;
- return TRUE;
+ return true;
}
that this logger takes number of bits as argument, not number of bytes.
**/
- if (!tracing) return FALSE;
+ if (!tracing) return false;
uint8_t *trace = BigBuf_get_addr();
uint16_t iLen = nbytes(iBits);
// Return when trace is full
- if (traceLen + sizeof(rsamples) + sizeof(dwParity) + sizeof(iBits) + iLen > BigBuf_max_traceLen()) return FALSE;
+ if (traceLen + sizeof(rsamples) + sizeof(dwParity) + sizeof(iBits) + iLen > BigBuf_max_traceLen()) return false;
//Hitag traces appear to use this traceformat:
// 32 bits timestamp (little endian,Highest Bit used as readerToTag flag)
memcpy(trace + traceLen, btBytes, iLen);
traceLen += iLen;
- return TRUE;
+ return true;
}
// Emulator memory
uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length){
uint8_t* mem = BigBuf_get_EM_addr();
- if(offset+length < CARD_MEMORY_SIZE)
- {
+ if (offset+length < CARD_MEMORY_SIZE) {
memcpy(mem+offset, data, length);
return 0;
- }else
- {
+ } else {
Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset+length), CARD_MEMORY_SIZE);
return 1;
}
extern void BigBuf_Clear(void);
extern void BigBuf_Clear_ext(bool verbose);
extern void BigBuf_Clear_keep_EM(void);
+extern void BigBuf_Clear_EM(void);
extern uint8_t *BigBuf_malloc(uint16_t);
extern void BigBuf_free(void);
extern void BigBuf_free_keep_EM(void);
extern void BigBuf_print_status(void);
extern uint16_t BigBuf_get_traceLen(void);
-extern void clear_trace();
+extern void clear_trace(void);
extern void set_tracing(bool enable);
extern bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag);
extern int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwParity, int bReader);
//-----------------------------------------------------------------------------
bool FpgaSetupSscDma(uint8_t *buf, int len)
{
- if (buf == NULL) {
- return false;
- }
+ if (buf == NULL) return false;
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; // Disable DMA Transfer
AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf; // transfer to this memory address
AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf; // next transfer to same memory address
AT91C_BASE_PDC_SSC->PDC_RNCR = len; // ... with same number of bytes
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; // go!
-
- return true;
+
+ return true;
}
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
fpga_image_ptr = output_buffer;
int res = inflate(compressed_fpga_stream, Z_SYNC_FLUSH);
- if (res != Z_OK) {
+ if (res != Z_OK)
Dbprintf("inflate returned: %d, %s", res, compressed_fpga_stream->msg);
- }
- if (res < 0) {
+
+ if (res < 0)
return res;
- }
}
uncompressed_bytes_cnt++;
static void fpga_inflate_free(voidpf opaque, voidpf address)
{
- BigBuf_free();
+ BigBuf_free(); BigBuf_Clear_ext(false);
}
static void DownloadFPGA(int bitstream_version, int FpgaImageLen, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{
- Dbprintf("DownloadFPGA(len: %d)", FpgaImageLen);
+ //Dbprintf("DownloadFPGA(len: %d)", FpgaImageLen);
int i=0;
void FpgaDownloadAndGo(int bitstream_version)
{
z_stream compressed_fpga_stream;
- uint8_t output_buffer[OUTPUT_BUFFER_LEN];
+ uint8_t output_buffer[OUTPUT_BUFFER_LEN] = {0x00};
// check whether or not the bitstream is already loaded
if (downloaded_bitstream == bitstream_version)
return;
// make sure that we have enough memory to decompress
- BigBuf_free();
+ BigBuf_free(); BigBuf_Clear_ext(false);
if (!reset_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer)) {
return;
}
inflateEnd(&compressed_fpga_stream);
+
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
}
void FpgaGatherVersion(int bitstream_version, char *dst, int len)
{
unsigned int fpga_info_len;
- char tempstr[40];
+ char tempstr[40] = {0x00};
z_stream compressed_fpga_stream;
- uint8_t output_buffer[OUTPUT_BUFFER_LEN];
+ uint8_t output_buffer[OUTPUT_BUFFER_LEN] = {0x00};
dst[0] = '\0';
// ensure that we can allocate enough memory for decompression:
- BigBuf_free();
+ BigBuf_free(); BigBuf_Clear_ext(false);
- if (!reset_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer)) {
+ if (!reset_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer))
return;
- }
if(bitparse_find_section(bitstream_version, 'a', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
for (uint16_t i = 0; i < fpga_info_len; i++) {
HIGH(whichGpio);
}
-void Fpga_print_status(void)
-{
+void Fpga_print_status(void) {
Dbprintf("Fgpa");
- if(downloaded_bitstream == FPGA_BITSTREAM_HF) Dbprintf(" mode.............HF");
- else if(downloaded_bitstream == FPGA_BITSTREAM_LF) Dbprintf(" mode.............LF");
- else Dbprintf(" mode.............%d", downloaded_bitstream);
+ switch(downloaded_bitstream) {
+ case FPGA_BITSTREAM_HF: Dbprintf(" mode....................HF"); break;
+ case FPGA_BITSTREAM_LF: Dbprintf(" mode....................LF"); break;
+ default: Dbprintf(" mode....................%d", downloaded_bitstream); break;
+ }
}
int FpgaGetCurrent() {
if(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)
{
*dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR);
- dest = dest + 1;
+ dest++;
}
}
//Resetting Frame mode (First set in fpgaloader.c)
void HfSnoop(int samplesToSkip, int triggersToSkip)
{
- Dbprintf("Skipping first %d sample pairs, Skipping %d triggers.", samplesToSkip, triggersToSkip);
+ BigBuf_free(); BigBuf_Clear();
+
+ Dbprintf("Skipping first %d sample pairs, Skipping %d triggers.\n", samplesToSkip, triggersToSkip);
int trigger_cnt;
LED_D_ON();
// Select correct configs
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SNOOP);
SpinDelay(100);
- BigBuf_free();
- BigBuf_Clear();
-
AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(16); // Setting Frame Mode For better performance on high speed data transfer.
trigger_cnt = 0;
uint16_t r = 0;
- while(!BUTTON_PRESS()) {
+ while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
WDT_HIT();
if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
r = (uint16_t)AT91C_BASE_SSC->SSC_RHR;
r = MAX(r & 0xff, r >> 8);
- if (r >= 240)
- {
- if (++trigger_cnt > triggersToSkip) {
+ if (r >= 240) {
+ if (++trigger_cnt > triggersToSkip)
break;
- }
}
}
}
if(!BUTTON_PRESS()) {
int waitcount = samplesToSkip; // lets wait 40000 ticks of pck0
while(waitcount != 0) {
- if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+ if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY))
waitcount--;
- }
}
optimizedSnoop();
Dbprintf("Trigger kicked! Value: %d, Dumping Samples Hispeed now.", r);