Merge pull request #337 from Proxmark/iceman1001-patch-4

Iceman1001 patch 4
This commit is contained in:
pwpiwi 2017-06-26 07:50:57 +02:00 committed by GitHub
commit 893d0fcc49
4 changed files with 56 additions and 49 deletions

View file

@ -18,6 +18,14 @@
// BigBuf is the large multi-purpose buffer, typically used to hold A/D samples or traces. // 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. // 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 // declare it as uint32_t to achieve alignment to 4 Byte boundary
static uint32_t BigBuf[BIGBUF_SIZE/sizeof(uint32_t)]; static uint32_t BigBuf[BIGBUF_SIZE/sizeof(uint32_t)];
@ -41,7 +49,8 @@ uint8_t *BigBuf_get_addr(void)
// get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done // get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
uint8_t *BigBuf_get_EM_addr(void) 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); emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
} }
@ -61,6 +70,9 @@ void BigBuf_Clear_ext(bool verbose)
if (verbose) if (verbose)
Dbprintf("Buffer cleared (%i bytes)",BIGBUF_SIZE); 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) void BigBuf_Clear_keep_EM(void)
{ {
@ -103,7 +115,7 @@ void BigBuf_print_status(void)
{ {
Dbprintf("Memory"); Dbprintf("Memory");
Dbprintf(" BIGBUF_SIZE.............%d", BIGBUF_SIZE); Dbprintf(" BIGBUF_SIZE.............%d", BIGBUF_SIZE);
Dbprintf(" BigBuf_hi .............%d", BigBuf_hi); Dbprintf(" Available memory........%d", BigBuf_hi);
Dbprintf("Tracing"); Dbprintf("Tracing");
Dbprintf(" tracing ................%d", tracing); Dbprintf(" tracing ................%d", tracing);
Dbprintf(" traceLen ...............%d", traceLen); Dbprintf(" traceLen ...............%d", traceLen);
@ -142,7 +154,7 @@ uint16_t BigBuf_get_traceLen(void)
**/ **/
bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag) 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(); uint8_t *trace = BigBuf_get_addr();
@ -153,8 +165,8 @@ bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_
uint16_t max_traceLen = BigBuf_max_traceLen(); 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 >= max_traceLen) {
tracing = FALSE; // don't trace any more tracing = false; // don't trace any more
return FALSE; return false;
} }
// Traceformat: // Traceformat:
// 32 bits timestamp (little endian) // 32 bits timestamp (little endian)
@ -198,7 +210,7 @@ bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_
} }
traceLen += num_paritybytes; traceLen += num_paritybytes;
return TRUE; return true;
} }
@ -209,12 +221,12 @@ int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwP
that this logger takes number of bits as argument, not number of bytes. 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(); uint8_t *trace = BigBuf_get_addr();
uint16_t iLen = nbytes(iBits); uint16_t iLen = nbytes(iBits);
// Return when trace is full // 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: //Hitag traces appear to use this traceformat:
// 32 bits timestamp (little endian,Highest Bit used as readerToTag flag) // 32 bits timestamp (little endian,Highest Bit used as readerToTag flag)
@ -241,19 +253,17 @@ int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwP
memcpy(trace + traceLen, btBytes, iLen); memcpy(trace + traceLen, btBytes, iLen);
traceLen += iLen; traceLen += iLen;
return TRUE; return true;
} }
// Emulator memory // Emulator memory
uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length){ uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length){
uint8_t* mem = BigBuf_get_EM_addr(); uint8_t* mem = BigBuf_get_EM_addr();
if(offset+length < CARD_MEMORY_SIZE) if (offset+length < CARD_MEMORY_SIZE) {
{
memcpy(mem+offset, data, length); memcpy(mem+offset, data, length);
return 0; return 0;
}else } else {
{
Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset+length), CARD_MEMORY_SIZE); Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset+length), CARD_MEMORY_SIZE);
return 1; return 1;
} }

View file

@ -29,12 +29,13 @@ extern uint16_t BigBuf_max_traceLen(void);
extern void BigBuf_Clear(void); extern void BigBuf_Clear(void);
extern void BigBuf_Clear_ext(bool verbose); extern void BigBuf_Clear_ext(bool verbose);
extern void BigBuf_Clear_keep_EM(void); extern void BigBuf_Clear_keep_EM(void);
extern void BigBuf_Clear_EM(void);
extern uint8_t *BigBuf_malloc(uint16_t); extern uint8_t *BigBuf_malloc(uint16_t);
extern void BigBuf_free(void); extern void BigBuf_free(void);
extern void BigBuf_free_keep_EM(void); extern void BigBuf_free_keep_EM(void);
extern void BigBuf_print_status(void); extern void BigBuf_print_status(void);
extern uint16_t BigBuf_get_traceLen(void); extern uint16_t BigBuf_get_traceLen(void);
extern void clear_trace(); extern void clear_trace(void);
extern void set_tracing(bool enable); 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 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); extern int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwParity, int bReader);

View file

@ -158,9 +158,7 @@ void FpgaSetupSsc(void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool FpgaSetupSscDma(uint8_t *buf, int len) bool FpgaSetupSscDma(uint8_t *buf, int len)
{ {
if (buf == NULL) { if (buf == NULL) return false;
return false;
}
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; // Disable DMA Transfer 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_RPR = (uint32_t) buf; // transfer to this memory address
@ -168,8 +166,8 @@ bool FpgaSetupSscDma(uint8_t *buf, int len)
AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf; // next transfer to same 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_RNCR = len; // ... with same number of bytes
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; // go! AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; // go!
return true; return true;
} }
@ -184,12 +182,11 @@ static int get_from_fpga_combined_stream(z_streamp compressed_fpga_stream, uint8
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN; compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
fpga_image_ptr = output_buffer; fpga_image_ptr = output_buffer;
int res = inflate(compressed_fpga_stream, Z_SYNC_FLUSH); 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); Dbprintf("inflate returned: %d, %s", res, compressed_fpga_stream->msg);
}
if (res < 0) { if (res < 0)
return res; return res;
}
} }
uncompressed_bytes_cnt++; uncompressed_bytes_cnt++;
@ -222,7 +219,7 @@ static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size)
static void fpga_inflate_free(voidpf opaque, voidpf address) static void fpga_inflate_free(voidpf opaque, voidpf address)
{ {
BigBuf_free(); BigBuf_free(); BigBuf_Clear_ext(false);
} }
@ -277,7 +274,7 @@ static void DownloadFPGA_byte(unsigned char w)
static void DownloadFPGA(int bitstream_version, int FpgaImageLen, z_streamp compressed_fpga_stream, uint8_t *output_buffer) 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; int i=0;
@ -416,14 +413,14 @@ static int bitparse_find_section(int bitstream_version, char section_name, unsig
void FpgaDownloadAndGo(int bitstream_version) void FpgaDownloadAndGo(int bitstream_version)
{ {
z_stream compressed_fpga_stream; 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 // check whether or not the bitstream is already loaded
if (downloaded_bitstream == bitstream_version) if (downloaded_bitstream == bitstream_version)
return; return;
// make sure that we have enough memory to decompress // 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)) { if (!reset_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer)) {
return; return;
@ -436,6 +433,9 @@ void FpgaDownloadAndGo(int bitstream_version)
} }
inflateEnd(&compressed_fpga_stream); inflateEnd(&compressed_fpga_stream);
// free eventually allocated BigBuf memory
BigBuf_free(); BigBuf_Clear_ext(false);
} }
@ -448,18 +448,17 @@ void FpgaDownloadAndGo(int bitstream_version)
void FpgaGatherVersion(int bitstream_version, char *dst, int len) void FpgaGatherVersion(int bitstream_version, char *dst, int len)
{ {
unsigned int fpga_info_len; unsigned int fpga_info_len;
char tempstr[40]; char tempstr[40] = {0x00};
z_stream compressed_fpga_stream; z_stream compressed_fpga_stream;
uint8_t output_buffer[OUTPUT_BUFFER_LEN]; uint8_t output_buffer[OUTPUT_BUFFER_LEN] = {0x00};
dst[0] = '\0'; dst[0] = '\0';
// ensure that we can allocate enough memory for decompression: // 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; return;
}
if(bitparse_find_section(bitstream_version, 'a', &fpga_info_len, &compressed_fpga_stream, output_buffer)) { 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++) { for (uint16_t i = 0; i < fpga_info_len; i++) {
@ -559,12 +558,13 @@ void SetAdcMuxFor(uint32_t whichGpio)
HIGH(whichGpio); HIGH(whichGpio);
} }
void Fpga_print_status(void) void Fpga_print_status(void) {
{
Dbprintf("Fgpa"); Dbprintf("Fgpa");
if(downloaded_bitstream == FPGA_BITSTREAM_HF) Dbprintf(" mode.............HF"); switch(downloaded_bitstream) {
else if(downloaded_bitstream == FPGA_BITSTREAM_LF) Dbprintf(" mode.............LF"); case FPGA_BITSTREAM_HF: Dbprintf(" mode....................HF"); break;
else Dbprintf(" mode.............%d", downloaded_bitstream); case FPGA_BITSTREAM_LF: Dbprintf(" mode....................LF"); break;
default: Dbprintf(" mode....................%d", downloaded_bitstream); break;
}
} }
int FpgaGetCurrent() { int FpgaGetCurrent() {

View file

@ -19,7 +19,7 @@ static void RAMFUNC optimizedSnoop(void)
if(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) if(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)
{ {
*dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR); *dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR);
dest = dest + 1; dest++;
} }
} }
//Resetting Frame mode (First set in fpgaloader.c) //Resetting Frame mode (First set in fpgaloader.c)
@ -28,7 +28,9 @@ static void RAMFUNC optimizedSnoop(void)
void HfSnoop(int samplesToSkip, int triggersToSkip) 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; int trigger_cnt;
LED_D_ON(); LED_D_ON();
// Select correct configs // Select correct configs
@ -40,23 +42,18 @@ void HfSnoop(int samplesToSkip, int triggersToSkip)
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SNOOP); FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SNOOP);
SpinDelay(100); 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. 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; trigger_cnt = 0;
uint16_t r = 0; uint16_t r = 0;
while(!BUTTON_PRESS()) { while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
WDT_HIT(); WDT_HIT();
if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
r = (uint16_t)AT91C_BASE_SSC->SSC_RHR; r = (uint16_t)AT91C_BASE_SSC->SSC_RHR;
r = MAX(r & 0xff, r >> 8); r = MAX(r & 0xff, r >> 8);
if (r >= 240) if (r >= 240) {
{ if (++trigger_cnt > triggersToSkip)
if (++trigger_cnt > triggersToSkip) {
break; break;
}
} }
} }
} }
@ -64,9 +61,8 @@ void HfSnoop(int samplesToSkip, int triggersToSkip)
if(!BUTTON_PRESS()) { if(!BUTTON_PRESS()) {
int waitcount = samplesToSkip; // lets wait 40000 ticks of pck0 int waitcount = samplesToSkip; // lets wait 40000 ticks of pck0
while(waitcount != 0) { while(waitcount != 0) {
if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY))
waitcount--; waitcount--;
}
} }
optimizedSnoop(); optimizedSnoop();
Dbprintf("Trigger kicked! Value: %d, Dumping Samples Hispeed now.", r); Dbprintf("Trigger kicked! Value: %d, Dumping Samples Hispeed now.", r);