ADD: downloading the EML part from BigBuffer specially.
// 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)];
+/* BigBuf memory layout:
+Pointer to highest available memory: BigBuf_hi
+
+ high BIGBUF_SIZE
+ reserved = BigBuf_malloc() subtracts amount from BigBuf_hi,
+ low 0x00
+*/
+
// High memory mark
static uint16_t BigBuf_hi = 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)
{
- if (emulator_memory == NULL) { // not yet allocated
+ // not yet allocated
+ if (emulator_memory == NULL)
emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
- }
return emulator_memory;
}
// 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);
}
void BigBuf_Clear_keep_EM(void)
{
- memset(BigBuf,0,BigBuf_hi);
+ memset(BigBuf, 0, BigBuf_hi);
}
// allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory
{
BigBuf_hi = BIGBUF_SIZE;
emulator_memory = NULL;
+
+ // shouldn't this empty BigBuf also?
}
// free allocated chunks EXCEPT the emulator memory
void BigBuf_free_keep_EM(void)
{
- if (emulator_memory != NULL) {
+ if (emulator_memory != NULL)
BigBuf_hi = emulator_memory - (uint8_t *)BigBuf;
- } else {
+ else
BigBuf_hi = BIGBUF_SIZE;
- }
+
+ // shouldn't this empty BigBuf also?
}
void BigBuf_print_status(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);
}
-
// return the maximum trace length (i.e. the unallocated size of BigBuf)
uint16_t BigBuf_max_traceLen(void)
{
uint16_t duration = timestamp_end - timestamp_start;
// Return when trace is full
- uint16_t max_traceLen = BigBuf_max_traceLen();
-
- if (traceLen + sizeof(iLen) + sizeof(timestamp_start) + sizeof(duration) + num_paritybytes + iLen >= max_traceLen) {
+ if (traceLen + sizeof(iLen) + sizeof(timestamp_start) + sizeof(duration) + num_paritybytes + iLen >= BigBuf_max_traceLen()) {
tracing = FALSE; // don't trace any more
return FALSE;
}
LED_D_OFF(); // LED D indicates field ON or OFF
break;
- case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K:
-
+ case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {
LED_B_ON();
uint8_t *BigBuf = BigBuf_get_addr();
size_t len = 0;
cmd_send(CMD_ACK,1,0,BigBuf_get_traceLen(),getSamplingConfig(),sizeof(sample_config));
LED_B_OFF();
break;
-
+ }
case CMD_DOWNLOADED_SIM_SAMPLES_125K: {
uint8_t *b = BigBuf_get_addr();
- memcpy(b+c->arg[0], c->d.asBytes, USB_CMD_DATA_SIZE);
+ memcpy( b + c->arg[0], c->d.asBytes, USB_CMD_DATA_SIZE);
cmd_send(CMD_ACK,0,0,0,0,0);
break;
- }
+ }
+ case CMD_DOWNLOAD_EML_BIGBUF: {
+ LED_B_ON();
+ uint8_t *cardmem = BigBuf_get_EM_addr();
+ size_t len = 0;
+ for(size_t i=0; i < c->arg[1]; i += USB_CMD_DATA_SIZE) {
+ len = MIN((c->arg[1] - i), USB_CMD_DATA_SIZE);
+ cmd_send(CMD_DOWNLOADED_EML_BIGBUF, i, len, CARD_MEMORY_SIZE, cardmem + c->arg[0] + i, len);
+ }
+ // Trigger a finish downloading signal with an ACK frame
+ cmd_send(CMD_ACK, 1, 0, CARD_MEMORY_SIZE, 0, 0);
+ LED_B_OFF();
+ break;
+ }
case CMD_READ_MEM:
ReadMem(c->arg[0]);
break;
static void fpga_inflate_free(voidpf opaque, voidpf address)
{
- BigBuf_free();
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
}
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);
- BigBuf_free();
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
}
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))
return;
void HfSnoop(int samplesToSkip, int triggersToSkip)
{
+ BigBuf_free(); BigBuf_Clear();
+
Dbprintf("Skipping first %d sample pairs, Skipping %d triggers.\n", samplesToSkip, triggersToSkip);
bool trigger_cnt;
+
LED_D_ON();
// Select correct configs
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
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.
size_t rxlen=0;
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
-
+
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
+
// Clean up trace and prepare it for storing frames
clear_trace();
set_tracing(TRUE);
auth_table_len = 0;
auth_table_pos = 0;
- BigBuf_free();
auth_table = (byte_t *)BigBuf_malloc(AUTH_TABLE_LENGTH);
memset(auth_table, 0x00, AUTH_TABLE_LENGTH);
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
+
// Clean up trace and prepare it for storing frames
clear_trace();
set_tracing(TRUE);
auth_table_len = 0;
auth_table_pos = 0;
byte_t* auth_table;
- BigBuf_free();
+
auth_table = (byte_t *)BigBuf_malloc(AUTH_TABLE_LENGTH);
memset(auth_table, 0x00, AUTH_TABLE_LENGTH);
int i, j;
byte_t rx[HITAG_FRAME_LEN];
size_t rxlen = 0;
-//bool bQuitTraceFull = false;
+ //bool bQuitTraceFull = false;
bQuiet = false;
byte_t txbuf[HITAG_FRAME_LEN];
byte_t* tx = txbuf;
size_t txlen = 0;
- BigBuf_free();
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
-// Clean up trace and prepare it for storing frames
+ // Clean up trace and prepare it for storing frames
set_tracing(TRUE);
clear_trace();
// Allocate memory from BigBuf for some buffers
// free all previous allocations first
- BigBuf_free();
+ BigBuf_free(); BigBuf_Clear_ext(false);
// init trace buffer
clear_trace();
#define MAX_SYNC_TRIES 32
#define MAX_STRATEGY 3
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
+
clear_trace();
set_tracing(TRUE);
if (first_try)
iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD);
-
- // free eventually allocated BigBuf memory. We want all for tracing.
- BigBuf_free();
if (first_try) {
sync_time = GetCountSspClk() & 0xfffffff8;
// bit 1 - trigger from first reader 7-bit request
LEDsoff();
+ // free eventually allocated BigBuf memory
+ BigBuf_free(); BigBuf_Clear_ext(false);
+
// init trace buffer
clear_trace();
set_tracing(TRUE);
iso14443a_setup(FPGA_HF_ISO14443A_SNIFFER);
- // free eventually allocated BigBuf memory
- BigBuf_free();
-
// allocate the DMA buffer, used to stream samples from the FPGA
uint8_t *dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE);
uint8_t *data = dmaBuf;
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
\r
// free eventually allocated BigBuf memory\r
- BigBuf_free();\r
+ BigBuf_free(); BigBuf_Clear_ext(false);\r
clear_trace();\r
set_tracing(true);\r
\r
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
\r
// free eventually allocated BigBuf memory\r
- BigBuf_free();\r
-\r
+ BigBuf_free(); BigBuf_Clear_ext(false);\r
+ \r
if (calibrate) clear_trace();\r
set_tracing(true);\r
\r
\r
}\r
\r
-\r
// work with emulator memory\r
void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
emlSetMem_xt(data, blockNum, blocksCount, 16);\r
// fast clock
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz)/32 -- tick=1.5mks
- AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO | AT91C_TC_ACPA_CLEAR |
- AT91C_TC_ACPC_SET | AT91C_TC_ASWTRG_SET;
+ AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO | AT91C_TC_ACPA_CLEAR |
+ AT91C_TC_ACPC_SET | AT91C_TC_ASWTRG_SET;
AT91C_BASE_TC0->TC_RA = 1;
AT91C_BASE_TC0->TC_RC = 0xBFFF + 1; // 0xC000
return;
} break;
- case CMD_DEBUG_PRINT_INTEGERS:
+ case CMD_DEBUG_PRINT_INTEGERS: {
PrintAndLog("#db# %08x, %08x, %08x", UC->arg[0], UC->arg[1], UC->arg[2]);
break;
-
+ }
case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K:
+ case CMD_DOWNLOADED_EML_BIGBUF: {
memcpy( sample_buf + (UC->arg[0]), UC->d.asBytes, UC->arg[1]);
break;
-
+ }
default: {
storeCommand(UC);
break;
clearCommandBuffer();
SendCommand(&c);
}
+void GetEMLFromBigBuf(uint8_t *dest, int bytes, int start_index) {
+ sample_buf = dest;
+ UsbCommand c = {CMD_DOWNLOAD_EML_BIGBUF, {start_index, bytes, 0}};
+ clearCommandBuffer();
+ SendCommand(&c);
+}
+
#define arraylen(x) (sizeof(x)/sizeof((x)[0]))
void GetFromBigBuf(uint8_t *dest, int bytes, int start_index);
-
+void GetEMLFromBigBuf(uint8_t *dest, int bytes, int start_index);
#endif
#define CMD_VERSION 0x0107
#define CMD_STATUS 0x0108
#define CMD_PING 0x0109
+
+#define CMD_DOWNLOAD_EML_BIGBUF 0x0110
+#define CMD_DOWNLOADED_EML_BIGBUF 0x0111
// For low-frequency tags
#define CMD_READ_TI_TYPE 0x0202
CMD_VERSION = 0x0107,
CMD_STATUS = 0x0108,
CMD_PING = 0x0109,
+ CMD_DOWNLOAD_EML_BIGBUF = 0x0110,
+ CMD_DOWNLOADED_EML_BIGBUF = 0x0111,
+
--// For low-frequency tags
CMD_READ_TI_TYPE = 0x0202,
CMD_WRITE_TI_TYPE = 0x0203,
#define CMD_STATUS 0x0108
#define CMD_PING 0x0109
+#define CMD_DOWNLOAD_EML_BIGBUF 0x0110
+#define CMD_DOWNLOADED_EML_BIGBUF 0x0111
+
// For low-frequency tags
#define CMD_READ_TI_TYPE 0x0202
#define CMD_WRITE_TI_TYPE 0x0203