Merge pull request #337 from Proxmark/iceman1001-patch-4
authorpwpiwi <pwpiwi@users.noreply.github.com>
Mon, 26 Jun 2017 05:50:57 +0000 (07:50 +0200)
committerGitHub <noreply@github.com>
Mon, 26 Jun 2017 05:50:57 +0000 (07:50 +0200)
Iceman1001 patch 4

armsrc/BigBuf.c
armsrc/BigBuf.h
armsrc/fpgaloader.c
armsrc/hfsnoop.c

index a5fcea7d288515227fa6af5ca015efef69578459..8870f426fa2b05d46ebe2fa57c374183c7d0a3ab 100644 (file)
 // 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)];
 
@@ -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
 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);
        }
        
@@ -61,6 +70,9 @@ void BigBuf_Clear_ext(bool verbose)
        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)
 {
@@ -103,7 +115,7 @@ 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);
@@ -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)
 {
-       if (!tracing) return FALSE;
+       if (!tracing) return false;
 
        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();
 
        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)
@@ -198,7 +210,7 @@ bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_
        }
        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.
          **/
 
-       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)
@@ -241,19 +253,17 @@ int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwP
        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;
        }
index 928c50c4d8ca9b3a341fb41186bf9ad7eae7d190..6a052dca152462d1ef6718e160c30473e5cd9d46 100644 (file)
@@ -29,12 +29,13 @@ extern uint16_t BigBuf_max_traceLen(void);
 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);
index e211c12fbd5a7e5119b86d6196da954e1b0198ef..1deb5b7dd09c1c19f0c103c580a15623b5c67a4e 100644 (file)
@@ -158,9 +158,7 @@ void FpgaSetupSsc(void)
 //-----------------------------------------------------------------------------
 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
@@ -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_RNCR = len;                                     // ... with same number of bytes
        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;
                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++;
@@ -222,7 +219,7 @@ static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size)
 
 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)
 {
 
-       Dbprintf("DownloadFPGA(len: %d)", FpgaImageLen);
+       //Dbprintf("DownloadFPGA(len: %d)", FpgaImageLen);
        
        int i=0;
 
@@ -416,14 +413,14 @@ static int bitparse_find_section(int bitstream_version, char section_name, unsig
 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;
@@ -436,6 +433,9 @@ void FpgaDownloadAndGo(int bitstream_version)
        }
 
        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)
 {
        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++) {
@@ -559,12 +558,13 @@ void SetAdcMuxFor(uint32_t whichGpio)
        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() {
index c750ab261eab647a4e41e57c9d77b4fb2a82763a..6a58b20b0e7f9fc6319abc7ea6869f5fa6e9cc60 100644 (file)
@@ -19,7 +19,7 @@ static void RAMFUNC optimizedSnoop(void)
                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)
@@ -28,7 +28,9 @@ static void RAMFUNC optimizedSnoop(void)
 
 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
@@ -40,23 +42,18 @@ void HfSnoop(int samplesToSkip, int triggersToSkip)
        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;
-                               }
                        } 
                }
        }
@@ -64,9 +61,8 @@ void HfSnoop(int samplesToSkip, int triggersToSkip)
        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);
Impressum, Datenschutz