]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
mod hw version: (#631)
authorpwpiwi <pwpiwi@users.noreply.github.com>
Sun, 5 Aug 2018 16:15:03 +0000 (18:15 +0200)
committerGitHub <noreply@github.com>
Sun, 5 Aug 2018 16:15:03 +0000 (18:15 +0200)
* create fpga version info at compile time (by additional functionality in fpgacompress)
* remove hw version caching (prepare USB reconnect)
* fix calculation of available compressed bytes in fpga_loader.c

armsrc/Makefile
armsrc/appmain.c
armsrc/fpgaloader.c
armsrc/fpgaloader.h
bootrom/Makefile
client/cmdhw.c
client/fpga_compress.c
common/Makefile.common
common/fpga.h [new file with mode: 0644]

index 3a7293e574a3ee7dbd44693950a7b2e659f01571..f0a0c0ffda2c54b976aeef0b2fe467e0dbd85894 100644 (file)
@@ -64,6 +64,9 @@ ARMSRC = fpgaloader.c \
        optimized_cipher.c \
        hfsnoop.c
 
        optimized_cipher.c \
        hfsnoop.c
 
+VERSIONSRC = version.c \
+       fpga_version_info.c
+
 # Do not move this inclusion before the definition of {THUMB,ASM,ARM}SRC
 include ../common/Makefile.common
 
 # Do not move this inclusion before the definition of {THUMB,ASM,ARM}SRC
 include ../common/Makefile.common
 
@@ -74,6 +77,14 @@ all: $(OBJS)
 
 .DELETE_ON_ERROR:
 
 
 .DELETE_ON_ERROR:
 
+# version.c should be remade on every compilation
+.PHONY: version.c
+version.c: default_version.c
+       perl ../tools/mkversion.pl .. > $@ || $(COPY) $^ $@ 
+
+fpga_version_info.c: $(FPGA_BITSTREAMS) $(FPGA_COMPRESSOR)
+       $(FPGA_COMPRESSOR) -v $(filter %.bit,$^) $@
+
 $(OBJDIR)/fpga_all.o: $(OBJDIR)/fpga_all.bit.z
        $(OBJCOPY) -O elf32-littlearm -I binary -B arm --prefix-sections=fpga_all_bit $^ $@
 
 $(OBJDIR)/fpga_all.o: $(OBJDIR)/fpga_all.bit.z
        $(OBJCOPY) -O elf32-littlearm -I binary -B arm --prefix-sections=fpga_all_bit $^ $@
 
index e8581216522e189e8da3ab5880275f1c3c0ad208..eabe9fbe627c15c97d8065bdb99b4b0c4939369a 100644 (file)
@@ -16,6 +16,7 @@
 #include "cmd.h"
 #include "proxmark3.h"
 #include "apps.h"
 #include "cmd.h"
 #include "proxmark3.h"
 #include "apps.h"
+#include "fpga.h"
 #include "util.h"
 #include "printf.h"
 #include "string.h"
 #include "util.h"
 #include "printf.h"
 #include "string.h"
@@ -286,6 +287,7 @@ void ReadMem(int addr)
 extern struct version_information version_information;
 /* bootrom version information is pointed to from _bootphase1_version_pointer */
 extern char *_bootphase1_version_pointer, _flash_start, _flash_end, _bootrom_start, _bootrom_end, __data_src_start__;
 extern struct version_information version_information;
 /* bootrom version information is pointed to from _bootphase1_version_pointer */
 extern char *_bootphase1_version_pointer, _flash_start, _flash_end, _bootrom_start, _bootrom_end, __data_src_start__;
+
 void SendVersion(void)
 {
        char temp[USB_CMD_DATA_SIZE]; /* Limited data payload in USB packets */
 void SendVersion(void)
 {
        char temp[USB_CMD_DATA_SIZE]; /* Limited data payload in USB packets */
@@ -306,10 +308,12 @@ void SendVersion(void)
        FormatVersionInformation(temp, sizeof(temp), "os: ", &version_information);
        strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
 
        FormatVersionInformation(temp, sizeof(temp), "os: ", &version_information);
        strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
 
-       FpgaGatherVersion(FPGA_BITSTREAM_LF, temp, sizeof(temp));
-       strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
-       FpgaGatherVersion(FPGA_BITSTREAM_HF, temp, sizeof(temp));
-       strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
+       for (int i = 0; i < fpga_bitstream_num; i++) {
+               strncat(VersionString, fpga_version_information[i], sizeof(VersionString) - strlen(VersionString) - 1);
+               if (i < fpga_bitstream_num - 1) {
+                       strncat(VersionString, "\n", sizeof(VersionString) - strlen(VersionString) - 1);
+               }
+       }
 
        // Send Chip ID and used flash memory
        uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
 
        // Send Chip ID and used flash memory
        uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
index c0b04f3cc99c4aa09526dd5a57f71fbec827b4cb..77223bd06bcf8f9d8485fe5f496020b6d0db730a 100644 (file)
 // mode once it is configured.
 //-----------------------------------------------------------------------------
 
 // mode once it is configured.
 //-----------------------------------------------------------------------------
 
+#include "fpgaloader.h"
+
 #include <stdint.h>
 #include <stddef.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stddef.h>
 #include <stdbool.h>
-#include "fpgaloader.h"
+#include "apps.h"
+#include "fpga.h"
 #include "proxmark3.h"
 #include "util.h"
 #include "string.h"
 #include "BigBuf.h"
 #include "zlib.h"
 
 #include "proxmark3.h"
 #include "util.h"
 #include "string.h"
 #include "BigBuf.h"
 #include "zlib.h"
 
-extern void Dbprintf(const char *fmt, ...);
-
 // remember which version of the bitstream we have already downloaded to the FPGA
 // remember which version of the bitstream we have already downloaded to the FPGA
-static int downloaded_bitstream = FPGA_BITSTREAM_ERR;
+static int downloaded_bitstream = 0;
 
 // this is where the bitstreams are located in memory:
 extern uint8_t _binary_obj_fpga_all_bit_z_start, _binary_obj_fpga_all_bit_z_end;
 
 // this is where the bitstreams are located in memory:
 extern uint8_t _binary_obj_fpga_all_bit_z_start, _binary_obj_fpga_all_bit_z_end;
@@ -31,10 +32,7 @@ extern uint8_t _binary_obj_fpga_all_bit_z_start, _binary_obj_fpga_all_bit_z_end;
 static uint8_t *fpga_image_ptr = NULL;
 static uint32_t uncompressed_bytes_cnt;
 
 static uint8_t *fpga_image_ptr = NULL;
 static uint32_t uncompressed_bytes_cnt;
 
-static const uint8_t _bitparse_fixed_header[] = {0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01};
-#define FPGA_BITSTREAM_FIXED_HEADER_SIZE       sizeof(_bitparse_fixed_header)
 #define OUTPUT_BUFFER_LEN              80
 #define OUTPUT_BUFFER_LEN              80
-#define FPGA_INTERLEAVE_SIZE   288
 
 //-----------------------------------------------------------------------------
 // Set up the Serial Peripheral Interface as master
 
 //-----------------------------------------------------------------------------
 // Set up the Serial Peripheral Interface as master
@@ -201,7 +199,7 @@ static int get_from_fpga_combined_stream(z_streamp compressed_fpga_stream, uint8
 //----------------------------------------------------------------------------
 static int get_from_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
 {
 //----------------------------------------------------------------------------
 static int get_from_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
 {
-       while((uncompressed_bytes_cnt / FPGA_INTERLEAVE_SIZE) % FPGA_BITSTREAM_MAX != (bitstream_version - 1)) {
+       while((uncompressed_bytes_cnt / FPGA_INTERLEAVE_SIZE) % fpga_bitstream_num != (bitstream_version - 1)) {
                // skip undesired data belonging to other bitstream_versions
                get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
        }
                // skip undesired data belonging to other bitstream_versions
                get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
        }
@@ -234,7 +232,7 @@ static bool reset_fpga_stream(int bitstream_version, z_streamp compressed_fpga_s
        
        // initialize z_stream structure for inflate:
        compressed_fpga_stream->next_in = &_binary_obj_fpga_all_bit_z_start;
        
        // initialize z_stream structure for inflate:
        compressed_fpga_stream->next_in = &_binary_obj_fpga_all_bit_z_start;
-       compressed_fpga_stream->avail_in = &_binary_obj_fpga_all_bit_z_start - &_binary_obj_fpga_all_bit_z_end;
+       compressed_fpga_stream->avail_in = &_binary_obj_fpga_all_bit_z_end - &_binary_obj_fpga_all_bit_z_start;
        compressed_fpga_stream->next_out = output_buffer;
        compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
        compressed_fpga_stream->zalloc = &fpga_inflate_malloc;
        compressed_fpga_stream->next_out = output_buffer;
        compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
        compressed_fpga_stream->zalloc = &fpga_inflate_malloc;
@@ -248,8 +246,8 @@ static bool reset_fpga_stream(int bitstream_version, z_streamp compressed_fpga_s
                header[i] = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
        }
        
                header[i] = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
        }
        
-       // Check for a valid .bit file (starts with _bitparse_fixed_header)
-       if(memcmp(_bitparse_fixed_header, header, FPGA_BITSTREAM_FIXED_HEADER_SIZE) == 0) {
+       // Check for a valid .bit file (starts with bitparse_fixed_header)
+       if(memcmp(bitparse_fixed_header, header, FPGA_BITSTREAM_FIXED_HEADER_SIZE) == 0) {
                return true;
        } else {
                return false;
                return true;
        } else {
                return false;
@@ -427,7 +425,7 @@ void FpgaDownloadAndGo(int bitstream_version)
        }
 
        unsigned int bitstream_length;
        }
 
        unsigned int bitstream_length;
-       if(bitparse_find_section(bitstream_version, 'e', &bitstream_length, &compressed_fpga_stream, output_buffer)) {
+       if (bitparse_find_section(bitstream_version, 'e', &bitstream_length, &compressed_fpga_stream, output_buffer)) {
                DownloadFPGA(bitstream_version, bitstream_length, &compressed_fpga_stream, output_buffer);
                downloaded_bitstream = bitstream_version;
        }
                DownloadFPGA(bitstream_version, bitstream_length, &compressed_fpga_stream, output_buffer);
                downloaded_bitstream = bitstream_version;
        }
@@ -442,77 +440,6 @@ void FpgaDownloadAndGo(int bitstream_version)
 }      
 
 
 }      
 
 
-//-----------------------------------------------------------------------------
-// Gather version information from FPGA image. Needs to decompress the begin 
-// of the respective (HF or LF) image.
-// Note: decompression makes use of (i.e. overwrites) BigBuf[]. It is therefore
-// advisable to call this only once and store the results for later use.
-//-----------------------------------------------------------------------------
-void FpgaGatherVersion(int bitstream_version, char *dst, int len)
-{
-       unsigned int fpga_info_len;
-       char tempstr[40] = {0x00};
-       z_stream compressed_fpga_stream;
-       uint8_t output_buffer[OUTPUT_BUFFER_LEN] = {0x00};
-       
-       dst[0] = '\0';
-
-       // ensure that we can allocate enough memory for decompression:
-       BigBuf_free(); BigBuf_Clear_ext(false);
-
-       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++) {
-                       char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
-                       if (i < sizeof(tempstr)) {
-                               tempstr[i] = c;
-                       }
-               }
-               if (!memcmp("fpga_lf", tempstr, 7))
-                       strncat(dst, "LF ", len-1);
-               else if (!memcmp("fpga_hf", tempstr, 7))
-                       strncat(dst, "HF ", len-1);
-       }
-       strncat(dst, "FPGA image built", len-1);
-       if(bitparse_find_section(bitstream_version, 'b', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
-               strncat(dst, " for ", len-1);
-               for (uint16_t i = 0; i < fpga_info_len; i++) {
-                       char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
-                       if (i < sizeof(tempstr)) {
-                               tempstr[i] = c;
-                       }
-               }
-               strncat(dst, tempstr, len-1);
-       }
-       if(bitparse_find_section(bitstream_version, 'c', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
-               strncat(dst, " on ", len-1);
-               for (uint16_t i = 0; i < fpga_info_len; i++) {
-                       char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
-                       if (i < sizeof(tempstr)) {
-                               tempstr[i] = c;
-                       }
-               }
-               strncat(dst, tempstr, len-1);
-       }
-       if(bitparse_find_section(bitstream_version, 'd', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
-               strncat(dst, " at ", len-1);
-               for (uint16_t i = 0; i < fpga_info_len; i++) {
-                       char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
-                       if (i < sizeof(tempstr)) {
-                               tempstr[i] = c;
-                       }
-               }
-               strncat(dst, tempstr, len-1);
-       }
-       
-       strncat(dst, "\n", len-1);
-
-       inflateEnd(&compressed_fpga_stream);
-}
-
-
 //-----------------------------------------------------------------------------
 // Send a 16 bit command/data pair to the FPGA.
 // The bit format is:  C3 C2 C1 C0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
 //-----------------------------------------------------------------------------
 // Send a 16 bit command/data pair to the FPGA.
 // The bit format is:  C3 C2 C1 C0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
@@ -562,12 +489,8 @@ void SetAdcMuxFor(uint32_t whichGpio)
 }
 
 void Fpga_print_status(void) {
 }
 
 void Fpga_print_status(void) {
-       Dbprintf("Fgpa");
-       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;
-       }
+       Dbprintf("Currently loaded FPGA image:");
+       Dbprintf("  %s", fpga_version_information[downloaded_bitstream-1]);
 }
 
 int FpgaGetCurrent() {
 }
 
 int FpgaGetCurrent() {
index 7dfc5c1265e539e8e8e419bb9ad68da857e6a061..fa16771d91cf6fd8deb0f647c47c11bf7b2cabed 100644 (file)
 // mode once it is configured.
 //-----------------------------------------------------------------------------
 
 // mode once it is configured.
 //-----------------------------------------------------------------------------
 
+#ifndef __FPGALOADER_H
+#define __FPGALOADER_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
 void FpgaSendCommand(uint16_t cmd, uint16_t v);
 void FpgaWriteConfWord(uint8_t v);
 void FpgaDownloadAndGo(int bitstream_version);
 void FpgaSendCommand(uint16_t cmd, uint16_t v);
 void FpgaWriteConfWord(uint8_t v);
 void FpgaDownloadAndGo(int bitstream_version);
-void FpgaGatherVersion(int bitstream_version, char *dst, int len);
 void FpgaSetupSsc(void);
 void SetupSpi(int mode);
 bool FpgaSetupSscDma(uint8_t *buf, int len);
 void FpgaSetupSsc(void);
 void SetupSpi(int mode);
 bool FpgaSetupSscDma(uint8_t *buf, int len);
@@ -24,12 +29,9 @@ int FpgaGetCurrent();
 void SetAdcMuxFor(uint32_t whichGpio);
 
 // definitions for multiple FPGA config files support
 void SetAdcMuxFor(uint32_t whichGpio);
 
 // definitions for multiple FPGA config files support
-#define FPGA_BITSTREAM_MAX 2   // the total number of FPGA bitstreams (configs)
-#define FPGA_BITSTREAM_ERR 0
 #define FPGA_BITSTREAM_LF 1
 #define FPGA_BITSTREAM_HF 2
 
 #define FPGA_BITSTREAM_LF 1
 #define FPGA_BITSTREAM_HF 2
 
-
 // Definitions for the FPGA commands.
 #define FPGA_CMD_SET_CONFREG                                           (1<<12)
 #define FPGA_CMD_SET_DIVISOR                                           (2<<12)
 // Definitions for the FPGA commands.
 #define FPGA_CMD_SET_CONFREG                                           (1<<12)
 #define FPGA_CMD_SET_DIVISOR                                           (2<<12)
@@ -72,3 +74,5 @@ void SetAdcMuxFor(uint32_t whichGpio);
 #define FPGA_HF_ISO14443A_TAGSIM_MOD                           (2<<0)
 #define FPGA_HF_ISO14443A_READER_LISTEN                                (3<<0)
 #define FPGA_HF_ISO14443A_READER_MOD                           (4<<0)
 #define FPGA_HF_ISO14443A_TAGSIM_MOD                           (2<<0)
 #define FPGA_HF_ISO14443A_READER_LISTEN                                (3<<0)
 #define FPGA_HF_ISO14443A_READER_MOD                           (4<<0)
+
+#endif
index 9237399566e786335d377b3a88a59f8cbcdd08e9..59c22aa2dc35b03952a3cb7a2f0ee62dec0f0f42 100644 (file)
@@ -10,6 +10,7 @@
 ARMSRC = 
 THUMBSRC = cmd.c usb_cdc.c bootrom.c
 ASMSRC = ram-reset.s flash-reset.s
 ARMSRC = 
 THUMBSRC = cmd.c usb_cdc.c bootrom.c
 ASMSRC = ram-reset.s flash-reset.s
+VERSIONSRC =
 
 ## There is a strange bug with the linker: Sometimes it will not emit the glue to call
 ## BootROM from ARM mode. The symbol is emitted, but the section will be filled with
 
 ## There is a strange bug with the linker: Sometimes it will not emit the glue to call
 ## BootROM from ARM mode. The symbol is emitted, but the section will be filled with
index bdab01eba35bc66f1a45ac656e581d238f9c3fc7..f994e938778cae8dffa590432240a053173e78d0 100644 (file)
@@ -408,21 +408,13 @@ int CmdVersion(const char *Cmd)
 
        clearCommandBuffer();
        UsbCommand c = {CMD_VERSION};
 
        clearCommandBuffer();
        UsbCommand c = {CMD_VERSION};
-       static UsbCommand resp = {0, {0, 0, 0}};
+       UsbCommand resp = {0, {0, 0, 0}};
 
 
-       if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available
-               SendCommand(&c);
-               if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
-                       PrintAndLog("Prox/RFID mark3 RFID instrument");
-                       PrintAndLog((char*)resp.d.asBytes);
-                       lookupChipID(resp.arg[0], resp.arg[1]);
-               }
-       } else {
-               PrintAndLog("[[[ Cached information ]]]\n");
+       SendCommand(&c);
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
                PrintAndLog("Prox/RFID mark3 RFID instrument");
                PrintAndLog((char*)resp.d.asBytes);
                lookupChipID(resp.arg[0], resp.arg[1]);
                PrintAndLog("Prox/RFID mark3 RFID instrument");
                PrintAndLog((char*)resp.d.asBytes);
                lookupChipID(resp.arg[0], resp.arg[1]);
-               PrintAndLog("");
        }
        return 0;
 }
        }
        return 0;
 }
index bd1e8be2d374ce1912ea14dde76ea87ef51e5de0..418a02b8abfbd197c9598424a28c783b7d6e9929 100644 (file)
@@ -1,4 +1,6 @@
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
+// piwi, 2017, 2018
+//
 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
 // at your option, any later version. See the LICENSE.txt file for the text of
 // the license.
 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
 // at your option, any later version. See the LICENSE.txt file for the text of
 // the license.
 
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <libgen.h>
 #include <string.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <string.h>
 #include <stdint.h>
 #include <stdbool.h>
+#include "fpga.h"
 #include "zlib.h"
 
 #define MAX(a,b) ((a)>(b)?(a):(b))
 #include "zlib.h"
 
 #define MAX(a,b) ((a)>(b)?(a):(b))
 #define COMPRESS_MAX_NICE_LENGTH       258
 #define COMPRESS_MAX_CHAIN            8192
 
 #define COMPRESS_MAX_NICE_LENGTH       258
 #define COMPRESS_MAX_CHAIN            8192
 
-#define FPGA_INTERLEAVE_SIZE           288  // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
-#define FPGA_CONFIG_SIZE            42336L  // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
 #define HARDNESTED_TABLE_SIZE          (sizeof(uint32_t) * ((1L<<19)+1))
 
 static void usage(void)
 {
        fprintf(stdout, "Usage: fpga_compress <infile1> <infile2> ... <infile_n> <outfile>\n");
        fprintf(stdout, "          Combine n FPGA bitstream files and compress them into one.\n\n");
 #define HARDNESTED_TABLE_SIZE          (sizeof(uint32_t) * ((1L<<19)+1))
 
 static void usage(void)
 {
        fprintf(stdout, "Usage: fpga_compress <infile1> <infile2> ... <infile_n> <outfile>\n");
        fprintf(stdout, "          Combine n FPGA bitstream files and compress them into one.\n\n");
-       fprintf(stdout, "       fpga_compress -d <infile> <outfile>");
-       fprintf(stdout, "          Decompress <infile>. Write result to <outfile>");
-       fprintf(stdout, "       fpga_compress -t <infile> <outfile>");
-       fprintf(stdout, "          Compress hardnested table <infile>. Write result to <outfile>");
+       fprintf(stdout, "       fpga_compress -v <infile1> <infile2> ... <infile_n> <outfile>\n");
+       fprintf(stdout, "          Extract Version Information from FPGA bitstream files and write it to <outfile>\n\n");
+       fprintf(stdout, "       fpga_compress -d <infile> <outfile>\n");
+       fprintf(stdout, "          Decompress <infile>. Write result to <outfile>\n\n");
+       fprintf(stdout, "       fpga_compress -t <infile> <outfile>\n");
+       fprintf(stdout, "          Compress hardnested table <infile>. Write result to <outfile>\n\n");
 }
 
 
 }
 
 
@@ -60,7 +64,7 @@ static voidpf fpga_deflate_malloc(voidpf opaque, uInt items, uInt size)
 
 static void fpga_deflate_free(voidpf opaque, voidpf address)
 {
 
 static void fpga_deflate_free(voidpf opaque, voidpf address)
 {
-       return free(address);
+       free(address);
 }
 
 
 }
 
 
@@ -252,9 +256,162 @@ int zlib_decompress(FILE *infile, FILE *outfile)
 }
 
 
 }
 
 
+/* Simple Xilinx .bit parser. The file starts with the fixed opaque byte sequence
+ * 00 09 0f f0 0f f0 0f f0 0f f0 00 00 01
+ * After that the format is 1 byte section type (ASCII character), 2 byte length
+ * (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
+ * length.
+ */
+static int bitparse_find_section(FILE *infile, char section_name, unsigned int *section_length)
+{
+       int result = 0;
+       #define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100  // maximum number of bytes to search for the requested section
+       uint16_t numbytes = 0;
+       while(numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) {
+               char current_name = (char)fgetc(infile);
+               numbytes++;
+               if(current_name < 'a' || current_name > 'e') {
+                       /* Strange section name, abort */
+                       break;
+               }
+               unsigned int current_length = 0;
+               switch(current_name) {
+               case 'e':
+                       /* Four byte length field */
+                       current_length += fgetc(infile) << 24;
+                       current_length += fgetc(infile) << 16;
+                       numbytes += 2;
+               default: /* Fall through, two byte length field */
+                       current_length += fgetc(infile) << 8;
+                       current_length += fgetc(infile) << 0;
+                       numbytes += 2;
+               }
+
+               if(current_name != 'e' && current_length > 255) {
+                       /* Maybe a parse error */
+                       break;
+               }
+
+               if(current_name == section_name) {
+                       /* Found it */
+                       *section_length = current_length;
+                       result = 1;
+                       break;
+               }
+
+               for (uint16_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) {
+                       (void)fgetc(infile);
+                       numbytes++;
+               }
+       }
+
+       return result;
+}
+
+
+static int FpgaGatherVersion(FILE *infile, char* infile_name, char *dst, int len)
+{
+       unsigned int fpga_info_len;
+       char tempstr[40] = {0x00};
+       
+       dst[0] = '\0';
+
+       for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++) {
+               if (fgetc(infile) != bitparse_fixed_header[i]) {
+                       fprintf(stderr, "Invalid FPGA file. Aborting...\n\n");
+                       return(EXIT_FAILURE);
+               }
+       }
+
+       strncat(dst, basename(infile_name), len-1);
+       // if (bitparse_find_section(infile, 'a', &fpga_info_len)) {
+               // for (uint16_t i = 0; i < fpga_info_len; i++) {
+                       // char c = (char)fgetc(infile);
+                       // if (i < sizeof(tempstr)) {
+                               // tempstr[i] = c;
+                       // }
+               // }
+               // strncat(dst, tempstr, len-1);
+       // }
+       strncat(dst, " built", len-1);
+       if (bitparse_find_section(infile, 'b', &fpga_info_len)) {
+               strncat(dst, " for ", len-1);
+               for (uint16_t i = 0; i < fpga_info_len; i++) {
+                       char c = (char)fgetc(infile);
+                       if (i < sizeof(tempstr)) {
+                               tempstr[i] = c;
+                       }
+               }
+               strncat(dst, tempstr, len-1);
+       }
+       if (bitparse_find_section(infile, 'c', &fpga_info_len)) {
+               strncat(dst, " on ", len-1);
+               for (uint16_t i = 0; i < fpga_info_len; i++) {
+                       char c = (char)fgetc(infile);
+                       if (i < sizeof(tempstr)) {
+                               tempstr[i] = c;
+                       }
+               }
+               strncat(dst, tempstr, len-1);
+       }
+       if (bitparse_find_section(infile, 'd', &fpga_info_len)) {
+               strncat(dst, " at ", len-1);
+               for (uint16_t i = 0; i < fpga_info_len; i++) {
+                       char c = (char)fgetc(infile);
+                       if (i < sizeof(tempstr)) {
+                               tempstr[i] = c;
+                       }
+               }
+               strncat(dst, tempstr, len-1);
+       }
+       return 0;
+}
+
+
+static void print_version_info_preamble(FILE *outfile, int num_infiles) {
+       fprintf(outfile, "//-----------------------------------------------------------------------------\n");
+       fprintf(outfile, "// piwi, 2018\n");
+       fprintf(outfile, "//\n");
+       fprintf(outfile, "// This code is licensed to you under the terms of the GNU GPL, version 2 or,\n");
+       fprintf(outfile, "// at your option, any later version. See the LICENSE.txt file for the text of\n");
+       fprintf(outfile, "// the license.\n");
+       fprintf(outfile, "//-----------------------------------------------------------------------------\n");
+       fprintf(outfile, "// Version information on fpga images\n");
+       fprintf(outfile, "//\n");
+       fprintf(outfile, "// This file is generated by fpga_compress. Don't edit!\n");
+       fprintf(outfile, "//-----------------------------------------------------------------------------\n");
+       fprintf(outfile, "\n");
+       fprintf(outfile, "\n");
+       fprintf(outfile, "const int fpga_bitstream_num = %d;\n", num_infiles);
+       fprintf(outfile, "const char* const fpga_version_information[%d] = {\n", num_infiles);
+}
+
+
+static int generate_fpga_version_info(FILE *infile[], char *infile_names[], int num_infiles, FILE *outfile) {
+       
+       char version_string[80] = "";
+       
+       print_version_info_preamble(outfile, num_infiles);
+       
+       for (int i = 0; i < num_infiles; i++) {
+               FpgaGatherVersion(infile[i], infile_names[i], version_string, sizeof(version_string));
+               fprintf(outfile, "\t\"%s\"", version_string);
+               if (i != num_infiles-1) {
+                       fprintf(outfile, ",");
+               }
+               fprintf(outfile,"\n");
+       }
+       
+       fprintf(outfile, "};\n");
+
+       return 0;
+}      
+
+
 int main(int argc, char **argv)
 {
        FILE **infiles;
 int main(int argc, char **argv)
 {
        FILE **infiles;
+       char **infile_names;
        FILE *outfile;
        
        if (argc == 1 || argc == 2) {
        FILE *outfile;
        
        if (argc == 1 || argc == 2) {
@@ -271,43 +428,56 @@ int main(int argc, char **argv)
                } 
                infiles[0] = fopen(argv[2], "rb");
                if (infiles[0] == NULL) {
                } 
                infiles[0] = fopen(argv[2], "rb");
                if (infiles[0] == NULL) {
-                       fprintf(stderr, "Error. Cannot open input file %s", argv[2]);
+                       fprintf(stderr, "Error. Cannot open input file %s\n\n", argv[2]);
                        return(EXIT_FAILURE);
                }
                outfile = fopen(argv[3], "wb");
                if (outfile == NULL) {
                        return(EXIT_FAILURE);
                }
                outfile = fopen(argv[3], "wb");
                if (outfile == NULL) {
-                       fprintf(stderr, "Error. Cannot open output file %s", argv[3]);
+                       fprintf(stderr, "Error. Cannot open output file %s\n\n", argv[3]);
                        return(EXIT_FAILURE);
                }
                return zlib_decompress(infiles[0], outfile);
 
                        return(EXIT_FAILURE);
                }
                return zlib_decompress(infiles[0], outfile);
 
-       } else { // Compress
+       } else { // Compress or gemerate version info
 
                bool hardnested_mode = false;
 
                bool hardnested_mode = false;
+               bool generate_version_file = false;
                int num_input_files = 0;
                int num_input_files = 0;
-               if (!strcmp(argv[1], "-t")) { // hardnested table
+               if (!strcmp(argv[1], "-t")) {                   // compress one hardnested table
                        if (argc != 4) {
                                usage();
                                return(EXIT_FAILURE);
                        }
                        hardnested_mode = true;
                        num_input_files = 1;
                        if (argc != 4) {
                                usage();
                                return(EXIT_FAILURE);
                        }
                        hardnested_mode = true;
                        num_input_files = 1;
-               } else {
+               } else if (!strcmp(argv[1], "-v")) {    // generate version info
+                       generate_version_file = true;
+                       num_input_files = argc-3;
+               } else {                                                                // compress 1..n fpga files
                        num_input_files = argc-2;
                }
                        num_input_files = argc-2;
                }
+                       
                infiles = calloc(num_input_files, sizeof(FILE*));
                infiles = calloc(num_input_files, sizeof(FILE*));
-               for (uint16_t i = 0; i < num_input_files; i++) { 
-                       infiles[i] = fopen(argv[i+(hardnested_mode?2:1)], "rb");
+               infile_names = calloc(num_input_files, sizeof(char*));
+               for (uint16_t i = 0; i < num_input_files; i++) {
+                       infile_names[i] = argv[i+((hardnested_mode || generate_version_file)?2:1)];
+                       infiles[i] = fopen(infile_names[i], "rb");
                        if (infiles[i] == NULL) {
                        if (infiles[i] == NULL) {
-                               fprintf(stderr, "Error. Cannot open input file %s", argv[i+(hardnested_mode?2:1)]);
+                               fprintf(stderr, "Error. Cannot open input file %s\n\n", infile_names[i]);
                                return(EXIT_FAILURE);
                        }
                }
                outfile = fopen(argv[argc-1], "wb");
                if (outfile == NULL) {
                                return(EXIT_FAILURE);
                        }
                }
                outfile = fopen(argv[argc-1], "wb");
                if (outfile == NULL) {
-                       fprintf(stderr, "Error. Cannot open output file %s", argv[argc-1]);
+                       fprintf(stderr, "Error. Cannot open output file %s\n\n", argv[argc-1]);
                        return(EXIT_FAILURE);
                }
                        return(EXIT_FAILURE);
                }
-               return zlib_compress(infiles, num_input_files, outfile, hardnested_mode);
+               if (generate_version_file) {
+                       if (generate_fpga_version_info(infiles, infile_names, num_input_files, outfile)) {
+                               return(EXIT_FAILURE);
+                       }
+               } else {
+                       return zlib_compress(infiles, num_input_files, outfile, hardnested_mode);
+               }
        }
 }
        }
 }
index f31ff7bbdb1387acea808d317ff91572e41f454b..0ab89b3d399ae368bdb5fe834c1ddd45127c3c66 100644 (file)
@@ -75,7 +75,7 @@ LIBS = -lgcc
 THUMBOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(THUMBSRC)))
 ARMOBJ   = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(ARMSRC)))
 ASMOBJ   = $(patsubst %.s,$(OBJDIR)/%.o,$(notdir $(ASMSRC)))
 THUMBOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(THUMBSRC)))
 ARMOBJ   = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(ARMSRC)))
 ASMOBJ   = $(patsubst %.s,$(OBJDIR)/%.o,$(notdir $(ASMSRC)))
-VERSIONOBJ = $(OBJDIR)/version.o
+VERSIONOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(VERSIONSRC)))
 
 $(THUMBOBJ): $(OBJDIR)/%.o: %.c $(INCLUDES)
        $(CC) $(CFLAGS) -mthumb -mthumb-interwork -o $@ $< 
 
 $(THUMBOBJ): $(OBJDIR)/%.o: %.c $(INCLUDES)
        $(CC) $(CFLAGS) -mthumb -mthumb-interwork -o $@ $< 
@@ -99,11 +99,6 @@ OBJCOPY_TRANSLATIONS = --no-change-warnings \
 $(OBJDIR)/%.s19: $(OBJDIR)/%.elf
        $(OBJCOPY) -Osrec --srec-forceS3 --strip-debug $(OBJCOPY_TRANSLATIONS) $^ $@
 
 $(OBJDIR)/%.s19: $(OBJDIR)/%.elf
        $(OBJCOPY) -Osrec --srec-forceS3 --strip-debug $(OBJCOPY_TRANSLATIONS) $^ $@
 
-# version.c should be remade on every compilation
-.PHONY: version.c
-version.c: default_version.c
-       perl ../tools/mkversion.pl .. > $@ || $(COPY) $^ $@ 
-
 # Automatic dependency generation
 DEPENDENCY_FILES = $(patsubst %.c,$(OBJDIR)/%.d,$(notdir $(THUMBSRC))) \
        $(patsubst %.c,$(OBJDIR)/%.d,$(notdir $(ARMSRC))) \
 # Automatic dependency generation
 DEPENDENCY_FILES = $(patsubst %.c,$(OBJDIR)/%.d,$(notdir $(THUMBSRC))) \
        $(patsubst %.c,$(OBJDIR)/%.d,$(notdir $(ARMSRC))) \
diff --git a/common/fpga.h b/common/fpga.h
new file mode 100644 (file)
index 0000000..b99a759
--- /dev/null
@@ -0,0 +1,18 @@
+//-----------------------------------------------------------------------------
+// This code is licensed to you under the terms of the GNU GPL, version 2 or,
+// at your option, any later version. See the LICENSE.txt file for the text of
+// the license.
+//-----------------------------------------------------------------------------
+
+#ifndef __FPGA_H
+#define __FPGA_H
+
+#define FPGA_BITSTREAM_FIXED_HEADER_SIZE    sizeof(bitparse_fixed_header)
+#define FPGA_INTERLEAVE_SIZE                288
+#define FPGA_CONFIG_SIZE                    42336L  // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
+
+static const uint8_t bitparse_fixed_header[] = {0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01};
+extern const int fpga_bitstream_num;
+extern const char* const fpga_version_information[];
+
+#endif
Impressum, Datenschutz