From aaa1a9a2dcb8b9f2b6acd937f84464daa50ecad6 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 12 Mar 2016 09:03:28 +0100 Subject: [PATCH] CHG: Added calling clear bigbuff to zero out it also, instead of just "free" it. ADD: downloading the EML part from BigBuffer specially. --- armsrc/BigBuf.c | 33 ++++++++++++++++++++------------- armsrc/appmain.c | 22 +++++++++++++++++----- armsrc/fpgaloader.c | 10 ++++++---- armsrc/hfsnoop.c | 6 +++--- armsrc/hitag2.c | 11 ++++++++--- armsrc/hitagS.c | 7 ++++--- armsrc/iso14443a.c | 14 +++++++------- armsrc/mifarecmd.c | 6 +++--- armsrc/mifareutil.c | 1 - armsrc/util.c | 4 ++-- client/cmdmain.c | 7 ++++--- client/data.c | 7 +++++++ client/data.h | 2 +- client/hid-flasher/usb_cmd.h | 3 +++ client/lualibs/commands.lua | 3 +++ include/usb_cmd.h | 3 +++ 16 files changed, 91 insertions(+), 48 deletions(-) diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index 851cf390..407133fd 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -16,10 +16,17 @@ // 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; @@ -40,9 +47,9 @@ uint8_t *BigBuf_get_addr(void) // 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; } @@ -56,14 +63,14 @@ 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); } 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 @@ -85,30 +92,32 @@ void BigBuf_free(void) { 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) { @@ -149,9 +158,7 @@ bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_ 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; } diff --git a/armsrc/appmain.c b/armsrc/appmain.c index e2a1888e..2ba3d75c 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1274,8 +1274,7 @@ void UsbPacketReceived(uint8_t *packet, int len) 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; @@ -1287,13 +1286,26 @@ void UsbPacketReceived(uint8_t *packet, int len) 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; diff --git a/armsrc/fpgaloader.c b/armsrc/fpgaloader.c index da85c66c..64ddc608 100644 --- a/armsrc/fpgaloader.c +++ b/armsrc/fpgaloader.c @@ -220,7 +220,8 @@ static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size) static void fpga_inflate_free(voidpf opaque, voidpf address) { - BigBuf_free(); + // free eventually allocated BigBuf memory + BigBuf_free(); BigBuf_Clear_ext(false); } @@ -416,7 +417,7 @@ void FpgaDownloadAndGo(int 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; @@ -430,7 +431,8 @@ void FpgaDownloadAndGo(int bitstream_version) inflateEnd(&compressed_fpga_stream); - BigBuf_free(); + // free eventually allocated BigBuf memory + BigBuf_free(); BigBuf_Clear_ext(false); } @@ -450,7 +452,7 @@ void FpgaGatherVersion(int bitstream_version, char *dst, int len) 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; diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index cfded302..945809ce 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -28,8 +28,11 @@ static void RAMFUNC optimizedSnoop(void) 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); @@ -39,9 +42,6 @@ void HfSnoop(int samplesToSkip, int triggersToSkip) 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. diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index 1b82c269..4596d3f1 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -712,7 +712,10 @@ void SnoopHitag(uint32_t type) { 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); @@ -720,7 +723,6 @@ void SnoopHitag(uint32_t type) { 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); @@ -927,6 +929,9 @@ void SimulateHitagTag(bool tag_mem_supplied, byte_t* data) { 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); @@ -934,7 +939,7 @@ void SimulateHitagTag(bool tag_mem_supplied, byte_t* data) { 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); diff --git a/armsrc/hitagS.c b/armsrc/hitagS.c index 1b247d19..a5bce4b9 100644 --- a/armsrc/hitagS.c +++ b/armsrc/hitagS.c @@ -949,14 +949,15 @@ void SimulateHitagSTag(bool tag_mem_supplied, byte_t* data) { 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(); diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index dfd167f0..be16447c 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -546,7 +546,7 @@ void RAMFUNC SniffIso14443a(uint8_t param) { // 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(); @@ -2303,6 +2303,9 @@ void ReaderMifare(bool first_try, uint8_t block ) #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); @@ -2310,9 +2313,6 @@ void ReaderMifare(bool first_try, uint8_t block ) 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; @@ -3068,6 +3068,9 @@ void RAMFUNC SniffMifare(uint8_t param) { // 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); @@ -3084,9 +3087,6 @@ void RAMFUNC SniffMifare(uint8_t param) { 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; diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index c68a50e5..c0e357b8 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -260,7 +260,7 @@ void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain) iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); // free eventually allocated BigBuf memory - BigBuf_free(); + BigBuf_free(); BigBuf_Clear_ext(false); clear_trace(); set_tracing(true); @@ -778,8 +778,8 @@ void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *dat iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); // free eventually allocated BigBuf memory - BigBuf_free(); - + BigBuf_free(); BigBuf_Clear_ext(false); + if (calibrate) clear_trace(); set_tracing(true); diff --git a/armsrc/mifareutil.c b/armsrc/mifareutil.c index 1956e88a..0a057d65 100644 --- a/armsrc/mifareutil.c +++ b/armsrc/mifareutil.c @@ -541,7 +541,6 @@ uint8_t FirstBlockOfSector(uint8_t sectorNo) } - // work with emulator memory void emlSetMem(uint8_t *data, int blockNum, int blocksCount) { emlSetMem_xt(data, blockNum, blocksCount, 16); diff --git a/armsrc/util.c b/armsrc/util.c index c00fa011..4e74b88d 100644 --- a/armsrc/util.c +++ b/armsrc/util.c @@ -331,8 +331,8 @@ void StartCountUS() // 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 diff --git a/client/cmdmain.c b/client/cmdmain.c index 583bafca..04d8ad7a 100644 --- a/client/cmdmain.c +++ b/client/cmdmain.c @@ -197,14 +197,15 @@ void UsbCommandReceived(UsbCommand *UC) 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; diff --git a/client/data.c b/client/data.c index 1725944e..49034769 100644 --- a/client/data.c +++ b/client/data.c @@ -23,3 +23,10 @@ void GetFromBigBuf(uint8_t *dest, int bytes, int start_index) { 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); +} + diff --git a/client/data.h b/client/data.h index 7d85e1f1..201aba04 100644 --- a/client/data.h +++ b/client/data.h @@ -19,5 +19,5 @@ extern uint8_t* sample_buf; #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 diff --git a/client/hid-flasher/usb_cmd.h b/client/hid-flasher/usb_cmd.h index ad694ad9..983842a5 100644 --- a/client/hid-flasher/usb_cmd.h +++ b/client/hid-flasher/usb_cmd.h @@ -52,6 +52,9 @@ typedef struct { #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 diff --git a/client/lualibs/commands.lua b/client/lualibs/commands.lua index e9675e79..c9488e31 100644 --- a/client/lualibs/commands.lua +++ b/client/lualibs/commands.lua @@ -22,6 +22,9 @@ local _commands = { 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, diff --git a/include/usb_cmd.h b/include/usb_cmd.h index cd23055c..8d6dd952 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -63,6 +63,9 @@ typedef struct{ #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 -- 2.39.2