fpga_compress: interleave (combine) fpga_lf.bit and fpga_hf.bit before compression.

fpga_loader.c: change to unweave fpga_lf and fpga_hf accordingly
prepare fpga_compress, fpga_loader and Makefile to handle more than two FPGA config files
revert removal of fullimage Makefile target. Remove osimage instead.
This commit is contained in:
pwpiwi 2015-04-29 17:59:33 +02:00
parent f39198789b
commit fb22897415
8 changed files with 170 additions and 99 deletions

View file

@ -31,10 +31,10 @@ client: client/all
flash-bootrom: bootrom/obj/bootrom.elf $(FLASH_TOOL) flash-bootrom: bootrom/obj/bootrom.elf $(FLASH_TOOL)
$(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$<) $(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$<)
flash-os: armsrc/obj/osimage.elf $(FLASH_TOOL) flash-os: armsrc/obj/fullimage.elf $(FLASH_TOOL)
$(FLASH_TOOL) $(FLASH_PORT) $(subst /,$(PATHSEP),$<) $(FLASH_TOOL) $(FLASH_PORT) $(subst /,$(PATHSEP),$<)
flash-all: bootrom/obj/bootrom.elf armsrc/obj/osimage.elf $(FLASH_TOOL) flash-all: bootrom/obj/bootrom.elf armsrc/obj/fullimage.elf $(FLASH_TOOL)
$(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$(filter-out $(FLASH_TOOL),$^)) $(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$(filter-out $(FLASH_TOOL),$^))
newtarbin: newtarbin:

View file

@ -22,6 +22,9 @@ SRC_ISO14443b = iso14443.c
SRC_CRAPTO1 = crapto1.c crypto1.c des.c aes.c SRC_CRAPTO1 = crapto1.c crypto1.c des.c aes.c
SRC_CRC = iso14443crc.c crc.c crc16.c crc32.c SRC_CRC = iso14443crc.c crc.c crc16.c crc32.c
#the FPGA bitstream files. Note: order matters!
FPGA_BITSTREAMS = fpga_lf.bit fpga_hf.bit
#the zlib source files required for decompressing the fpga config at run time #the zlib source files required for decompressing the fpga config at run time
SRC_ZLIB = inflate.c inffast.c inftrees.c adler32.c zutil.c SRC_ZLIB = inflate.c inffast.c inftrees.c adler32.c zutil.c
#additional defines required to compile zlib #additional defines required to compile zlib
@ -67,16 +70,13 @@ FPGA_COMPRESSOR = ../client/fpga_compress
all: $(OBJS) all: $(OBJS)
$(OBJDIR)/fpga_lf.o: $(OBJDIR)/fpga_lf.bit.z $(OBJDIR)/fpga_all.o: $(OBJDIR)/fpga_all.bit.z
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --redefine-sym _binary_obj_fpga_lf_bit_z_start=_binary_fpga_lf_bit_start --redefine-sym _binary_obj_fpga_lf_bit_z_end=_binary_fpga_lf_bit_end --prefix-sections=fpga_lf_bit $^ $@ $(OBJCOPY) -O elf32-littlearm -I binary -B arm --prefix-sections=fpga_all_bit $^ $@
$(OBJDIR)/fpga_hf.o: $(OBJDIR)/fpga_hf.bit.z $(OBJDIR)/fpga_all.bit.z: $(FPGA_BITSTREAMS) $(FPGA_COMPRESSOR)
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --redefine-sym _binary_obj_fpga_hf_bit_z_start=_binary_fpga_hf_bit_start --redefine-sym _binary_obj_fpga_hf_bit_z_end=_binary_fpga_hf_bit_end --prefix-sections=fpga_hf_bit $^ $@
$(OBJDIR)/%.bit.z: %.bit $(FPGA_COMPRESSOR)
$(FPGA_COMPRESSOR) $(filter %.bit,$^) $@ $(FPGA_COMPRESSOR) $(filter %.bit,$^) $@
$(OBJDIR)/osimage.elf: $(VERSIONOBJ) $(OBJDIR)/fpga_lf.o $(OBJDIR)/fpga_hf.o $(THUMBOBJ) $(ARMOBJ) $(OBJDIR)/fullimage.elf: $(VERSIONOBJ) $(OBJDIR)/fpga_all.o $(THUMBOBJ) $(ARMOBJ)
$(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^ $(LIBS) $(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^ $(LIBS)
tarbin: $(OBJS) tarbin: $(OBJS)
@ -95,6 +95,6 @@ clean:
help: help:
@echo Multi-OS Makefile, you are running on $(DETECTED_OS) @echo Multi-OS Makefile, you are running on $(DETECTED_OS)
@echo Possible targets: @echo Possible targets:
@echo + all - Build the OS image $(OBJDIR)/osimage.s19 @echo + all - Build the full image $(OBJDIR)/fullimage.s19
@echo + clean - Clean $(OBJDIR) @echo + clean - Clean $(OBJDIR)

View file

@ -26,13 +26,15 @@ extern void Dbprintf(const char *fmt, ...);
static int downloaded_bitstream = FPGA_BITSTREAM_ERR; static int downloaded_bitstream = FPGA_BITSTREAM_ERR;
// this is where the bitstreams are located in memory: // this is where the bitstreams are located in memory:
extern uint8_t _binary_fpga_lf_bit_start, _binary_fpga_lf_bit_end; extern uint8_t _binary_obj_fpga_all_bit_z_start, _binary_obj_fpga_all_bit_z_end;
extern uint8_t _binary_fpga_hf_bit_start, _binary_fpga_hf_bit_end;
static uint8_t *fpga_image_ptr = NULL; 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}; 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 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
@ -171,7 +173,7 @@ bool FpgaSetupSscDma(uint8_t *buf, int len)
} }
static int get_from_fpga_stream(z_streamp compressed_fpga_stream, uint8_t *output_buffer) static int get_from_fpga_combined_stream(z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{ {
if (fpga_image_ptr == compressed_fpga_stream->next_out) { // need more data if (fpga_image_ptr == compressed_fpga_stream->next_out) { // need more data
compressed_fpga_stream->next_out = output_buffer; compressed_fpga_stream->next_out = output_buffer;
@ -186,10 +188,24 @@ static int get_from_fpga_stream(z_streamp compressed_fpga_stream, uint8_t *outpu
} }
} }
uncompressed_bytes_cnt++;
return *fpga_image_ptr++; return *fpga_image_ptr++;
} }
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)) {
// skip undesired data belonging to other bitstream_versions
get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
}
return get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
}
static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size) static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size)
{ {
Dbprintf("zlib requested %d bytes", items*size); Dbprintf("zlib requested %d bytes", items*size);
@ -207,22 +223,12 @@ static void fpga_inflate_free(voidpf opaque, voidpf address)
static bool reset_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer) static bool reset_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{ {
uint8_t header[FPGA_BITSTREAM_FIXED_HEADER_SIZE]; uint8_t header[FPGA_BITSTREAM_FIXED_HEADER_SIZE];
uint8_t *fpga_image_start;
uint32_t fpga_image_size;
if (bitstream_version == FPGA_BITSTREAM_LF) { uncompressed_bytes_cnt = 0;
fpga_image_start = &_binary_fpga_lf_bit_start;
fpga_image_size = (uint32_t)&_binary_fpga_lf_bit_end - (uint32_t)&_binary_fpga_lf_bit_start;
} else if (bitstream_version == FPGA_BITSTREAM_HF) {
fpga_image_start = &_binary_fpga_hf_bit_start;
fpga_image_size = (uint32_t)&_binary_fpga_hf_bit_end - (uint32_t)&_binary_fpga_hf_bit_start;
} else {
return false;
}
// initialize z_stream structure for inflate: // initialize z_stream structure for inflate:
compressed_fpga_stream->next_in = fpga_image_start; compressed_fpga_stream->next_in = &_binary_obj_fpga_all_bit_z_start;
compressed_fpga_stream->avail_in = fpga_image_size; compressed_fpga_stream->avail_in = &_binary_obj_fpga_all_bit_z_start - &_binary_obj_fpga_all_bit_z_end;
compressed_fpga_stream->next_out = output_buffer; compressed_fpga_stream->next_out = output_buffer;
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN; compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
compressed_fpga_stream->zalloc = &fpga_inflate_malloc; compressed_fpga_stream->zalloc = &fpga_inflate_malloc;
@ -233,7 +239,7 @@ static bool reset_fpga_stream(int bitstream_version, z_streamp compressed_fpga_s
fpga_image_ptr = output_buffer; fpga_image_ptr = output_buffer;
for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++) { for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++) {
header[i] = get_from_fpga_stream(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) // Check for a valid .bit file (starts with _bitparse_fixed_header)
@ -259,7 +265,7 @@ static void DownloadFPGA_byte(unsigned char w)
} }
// Download the fpga image starting at current stream position with length FpgaImageLen bytes // Download the fpga image starting at current stream position with length FpgaImageLen bytes
static void DownloadFPGA(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);
@ -316,7 +322,7 @@ static void DownloadFPGA(int FpgaImageLen, z_streamp compressed_fpga_stream, uin
} }
for(i = 0; i < FpgaImageLen; i++) { for(i = 0; i < FpgaImageLen; i++) {
int b = get_from_fpga_stream(compressed_fpga_stream, output_buffer); int b = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
if (b < 0) { if (b < 0) {
Dbprintf("Error %d during FpgaDownload", b); Dbprintf("Error %d during FpgaDownload", b);
break; break;
@ -348,13 +354,13 @@ static void DownloadFPGA(int FpgaImageLen, z_streamp compressed_fpga_stream, uin
* (big endian), <length> bytes content. Except for section 'e' which has 4 bytes * (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
* length. * length.
*/ */
static int bitparse_find_section(char section_name, unsigned int *section_length, z_streamp compressed_fpga_stream, uint8_t *output_buffer) static int bitparse_find_section(int bitstream_version, char section_name, unsigned int *section_length, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{ {
int result = 0; int result = 0;
#define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section #define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section
uint16_t numbytes = 0; uint16_t numbytes = 0;
while(numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) { while(numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) {
char current_name = get_from_fpga_stream(compressed_fpga_stream, output_buffer); char current_name = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
numbytes++; numbytes++;
unsigned int current_length = 0; unsigned int current_length = 0;
if(current_name < 'a' || current_name > 'e') { if(current_name < 'a' || current_name > 'e') {
@ -365,12 +371,12 @@ static int bitparse_find_section(char section_name, unsigned int *section_length
switch(current_name) { switch(current_name) {
case 'e': case 'e':
/* Four byte length field */ /* Four byte length field */
current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 24; current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 24;
current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 16; current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 16;
numbytes += 2; numbytes += 2;
default: /* Fall through, two byte length field */ default: /* Fall through, two byte length field */
current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 8; current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 8;
current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 0; current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 0;
numbytes += 2; numbytes += 2;
} }
@ -387,7 +393,7 @@ static int bitparse_find_section(char section_name, unsigned int *section_length
} }
for (uint16_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) { for (uint16_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) {
get_from_fpga_stream(compressed_fpga_stream, output_buffer); get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
numbytes++; numbytes++;
} }
} }
@ -414,8 +420,8 @@ void FpgaDownloadAndGo(int bitstream_version)
} }
unsigned int bitstream_length; unsigned int bitstream_length;
if(bitparse_find_section('e', &bitstream_length, &compressed_fpga_stream, output_buffer)) { if(bitparse_find_section(bitstream_version, 'e', &bitstream_length, &compressed_fpga_stream, output_buffer)) {
DownloadFPGA(bitstream_length, &compressed_fpga_stream, output_buffer); DownloadFPGA(bitstream_version, bitstream_length, &compressed_fpga_stream, output_buffer);
downloaded_bitstream = bitstream_version; downloaded_bitstream = bitstream_version;
} }
@ -437,9 +443,9 @@ void FpgaGatherVersion(int bitstream_version, char *dst, int len)
return; return;
} }
if(bitparse_find_section('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++) {
char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer); char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) { if (i < sizeof(tempstr)) {
tempstr[i] = c; tempstr[i] = c;
} }
@ -450,30 +456,30 @@ void FpgaGatherVersion(int bitstream_version, char *dst, int len)
strncat(dst, "HF ", len-1); strncat(dst, "HF ", len-1);
} }
strncat(dst, "FPGA image built", len-1); strncat(dst, "FPGA image built", len-1);
if(bitparse_find_section('b', &fpga_info_len, &compressed_fpga_stream, output_buffer)) { if(bitparse_find_section(bitstream_version, 'b', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
strncat(dst, " for ", len-1); strncat(dst, " for ", len-1);
for (uint16_t i = 0; i < fpga_info_len; i++) { for (uint16_t i = 0; i < fpga_info_len; i++) {
char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer); char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) { if (i < sizeof(tempstr)) {
tempstr[i] = c; tempstr[i] = c;
} }
} }
strncat(dst, tempstr, len-1); strncat(dst, tempstr, len-1);
} }
if(bitparse_find_section('c', &fpga_info_len, &compressed_fpga_stream, output_buffer)) { if(bitparse_find_section(bitstream_version, 'c', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
strncat(dst, " on ", len-1); strncat(dst, " on ", len-1);
for (uint16_t i = 0; i < fpga_info_len; i++) { for (uint16_t i = 0; i < fpga_info_len; i++) {
char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer); char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) { if (i < sizeof(tempstr)) {
tempstr[i] = c; tempstr[i] = c;
} }
} }
strncat(dst, tempstr, len-1); strncat(dst, tempstr, len-1);
} }
if(bitparse_find_section('d', &fpga_info_len, &compressed_fpga_stream, output_buffer)) { if(bitparse_find_section(bitstream_version, 'd', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
strncat(dst, " at ", len-1); strncat(dst, " at ", len-1);
for (uint16_t i = 0; i < fpga_info_len; i++) { for (uint16_t i = 0; i < fpga_info_len; i++) {
char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer); char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) { if (i < sizeof(tempstr)) {
tempstr[i] = c; tempstr[i] = c;
} }

View file

@ -21,6 +21,13 @@ bool FpgaSetupSscDma(uint8_t *buf, int len);
#define FpgaEnableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; #define FpgaEnableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN;
void SetAdcMuxFor(uint32_t whichGpio); 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
// Definitions for the FPGA commands. // Definitions for the FPGA commands.
#define FPGA_CMD_SET_CONFREG (1<<12) #define FPGA_CMD_SET_CONFREG (1<<12)
#define FPGA_CMD_SET_DIVISOR (2<<12) #define FPGA_CMD_SET_DIVISOR (2<<12)

View file

@ -34,8 +34,7 @@ SECTIONS
.rodata : { .rodata : {
*(.rodata) *(.rodata)
*(.rodata.*) *(.rodata.*)
*(fpga_lf_bit.data) *(fpga_all_bit.data)
*(fpga_hf_bit.data)
KEEP(*(.version_information)) KEEP(*(.version_information))
} >osimage :text } >osimage :text

View file

@ -10,19 +10,36 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include "zlib.h" #include "zlib.h"
#define MAX(a,b) ((a)>(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
// zlib configuration // zlib configuration
#define COMPRESS_LEVEL 9 // use best possible compression #define COMPRESS_LEVEL 9 // use best possible compression
#define COMPRESS_WINDOW_BITS 15 // default = 15 for a window of 2^15 = 32KBytes
#define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8. Must be < 9
/* COMPRESS_STRATEGY can be
Z_DEFAULT_STRATEGY (the default),
Z_FILTERED (more huffmann, less string matching),
Z_HUFFMAN_ONLY (huffman only, no string matching)
Z_RLE (distances limited to one)
Z_FIXED (prevents the use of dynamic Huffman codes)
*/
#define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
// zlib tuning parameters:
#define COMPRESS_GOOD_LENGTH 258
#define COMPRESS_MAX_LAZY 258
#define COMPRESS_MAX_NICE_LENGTH 258
#define COMPRESS_MAX_CHAIN 8192
#define FPGA_CONFIG_SIZE 42175 #define FPGA_INTERLEAVE_SIZE 288 // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
static uint8_t fpga_config[FPGA_CONFIG_SIZE]; #define FPGA_CONFIG_SIZE 42336 // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
static void usage(char *argv0) static void usage(char *argv0)
{ {
fprintf(stderr, "Usage: %s <infile> <outfile>\n\n", argv0); fprintf(stderr, "Usage: %s <infile1> <infile2> ... <infile_n> <outfile>\n\n", argv0);
fprintf(stderr, "Combines n FPGA bitstream files and compresses them into one.\n\n");
} }
@ -40,40 +57,78 @@ static void fpga_deflate_free(voidpf opaque, voidpf address)
} }
int zlib_compress(FILE *infile, FILE *outfile) static bool all_feof(FILE *infile[], uint8_t num_infiles)
{ {
int i, ret; for (uint16_t i = 0; i < num_infiles; i++) {
z_stream compressed_fpga_stream; if (!feof(infile[i])) {
return false;
// read the input file into fpga_config[] and count occurrences of each symbol:
i = 0;
while(!feof(infile)) {
uint8_t c;
c = fgetc(infile);
fpga_config[i++] = c;
if (i > FPGA_CONFIG_SIZE+1) {
fprintf(stderr, "Input file too big (> %d bytes). This is probably not a PM3 FPGA config file.", FPGA_CONFIG_SIZE);
fclose(infile);
fclose(outfile);
return -1;
} }
} }
return true;
}
int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile)
{
uint8_t *fpga_config;
uint32_t i;
int ret;
uint8_t c;
z_stream compressed_fpga_stream;
fpga_config = malloc(num_infiles * FPGA_CONFIG_SIZE);
// read the input files interleaving into fpga_config[]
i = 0;
do {
for(uint16_t j = 0; j < num_infiles; j++) {
for(uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) {
c = fgetc(infile[j]);
if (!feof(infile[j])) fpga_config[i++] = c; else fpga_config[i++] = '\0';
}
}
if (i > num_infiles * FPGA_CONFIG_SIZE) {
fprintf(stderr, "Input files too big (total of %ld > %d bytes). These are probably not PM3 FPGA config files.", i, num_infiles*FPGA_CONFIG_SIZE);
for(uint16_t j = 0; j < num_infiles; j++) {
fclose(infile[j]);
}
return -1;
}
} while (!all_feof(infile, num_infiles));
fprintf(stderr, "Read a total of %ld bytes from %d files\n", i, num_infiles);
// initialize zlib structures // initialize zlib structures
compressed_fpga_stream.next_in = fpga_config; compressed_fpga_stream.next_in = fpga_config;
compressed_fpga_stream.avail_in = i; compressed_fpga_stream.avail_in = i;
compressed_fpga_stream.zalloc = fpga_deflate_malloc; compressed_fpga_stream.zalloc = fpga_deflate_malloc;
compressed_fpga_stream.zfree = fpga_deflate_free; compressed_fpga_stream.zfree = fpga_deflate_free;
ret = deflateInit2(&compressed_fpga_stream,
COMPRESS_LEVEL,
Z_DEFLATED,
COMPRESS_WINDOW_BITS,
COMPRESS_MEM_LEVEL,
COMPRESS_STRATEGY);
// estimate the size of the compressed output // estimate the size of the compressed output
unsigned int outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in); unsigned int outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in);
fprintf(stderr, "Allocating %ld bytes for output file (estimated upper bound)\n", outsize_max);
uint8_t *outbuf = malloc(outsize_max); uint8_t *outbuf = malloc(outsize_max);
compressed_fpga_stream.next_out = outbuf; compressed_fpga_stream.next_out = outbuf;
compressed_fpga_stream.avail_out = outsize_max; compressed_fpga_stream.avail_out = outsize_max;
fprintf(stderr, "Allocated %d bytes for output file (estimated upper bound)\n", outsize_max);
ret = deflateInit(&compressed_fpga_stream, COMPRESS_LEVEL);
if (ret == Z_OK) {
ret = deflateTune(&compressed_fpga_stream,
COMPRESS_GOOD_LENGTH,
COMPRESS_MAX_LAZY,
COMPRESS_MAX_NICE_LENGTH,
COMPRESS_MAX_CHAIN);
}
if (ret == Z_OK) { if (ret == Z_OK) {
ret = deflate(&compressed_fpga_stream, Z_FINISH); ret = deflate(&compressed_fpga_stream, Z_FINISH);
} }
@ -84,8 +139,12 @@ int zlib_compress(FILE *infile, FILE *outfile)
fprintf(stderr, "Error in deflate(): %d %s\n", ret, compressed_fpga_stream.msg); fprintf(stderr, "Error in deflate(): %d %s\n", ret, compressed_fpga_stream.msg);
free(outbuf); free(outbuf);
deflateEnd(&compressed_fpga_stream); deflateEnd(&compressed_fpga_stream);
fclose(infile); for(uint16_t j = 0; j < num_infiles; j++) {
fclose(infile[j]);
}
fclose(outfile); fclose(outfile);
free(infile);
free(fpga_config);
return -1; return -1;
} }
@ -95,9 +154,13 @@ int zlib_compress(FILE *infile, FILE *outfile)
free(outbuf); free(outbuf);
deflateEnd(&compressed_fpga_stream); deflateEnd(&compressed_fpga_stream);
fclose(infile); for(uint16_t j = 0; j < num_infiles; j++) {
fclose(infile[j]);
}
fclose(outfile); fclose(outfile);
free(infile);
free(fpga_config);
return 0; return 0;
} }
@ -106,29 +169,29 @@ int zlib_compress(FILE *infile, FILE *outfile)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *infilename; FILE **infiles;
char *outfilename; FILE *outfile;
if (argc != 3) { if (argc == 1 || argc == 2) {
usage(argv[0]); usage(argv[0]);
return -1; return -1;
} else { }
infilename = argv[1];
outfilename = argv[2]; infiles = calloc(argc-2, sizeof(FILE*));
}
for (uint16_t i = 0; i < argc-2; i++) {
infiles[i] = fopen(argv[i+1], "rb");
if (infiles[i] == NULL) {
fprintf(stderr, "Error. Cannot open input file %s", argv[i+1]);
return -1;
}
}
FILE *infile = fopen(infilename, "rb"); outfile = fopen(argv[argc-1], "wb");
if (infile == NULL) {
fprintf(stderr, "Error. Cannot open input file %s", infilename);
return -1;
}
FILE *outfile = fopen(outfilename, "wb");
if (outfile == NULL) { if (outfile == NULL) {
fprintf(stderr, "Error. Cannot open output file %s", outfilename); fprintf(stderr, "Error. Cannot open output file %s", argv[argc-1]);
fclose(infile);
return -1; return -1;
} }
return zlib_compress(infile, outfile); return zlib_compress(infiles, argc-2, outfile);
} }

View file

@ -61,10 +61,6 @@
#define SPI_FPGA_MODE 0 #define SPI_FPGA_MODE 0
#define SPI_LCD_MODE 1 #define SPI_LCD_MODE 1
#define FPGA_BITSTREAM_ERR 0
#define FPGA_BITSTREAM_LF 1
#define FPGA_BITSTREAM_HF 2
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0

View file

@ -1,17 +1,17 @@
include ../common/Makefile.common include ../common/Makefile.common
BINS = bootrom.bin osimage.bin proxmark3_recovery.bin BINS = bootrom.bin fullimage.bin proxmark3_recovery.bin
all: $(BINS) all: $(BINS)
bootrom.bin: ../bootrom/obj/bootrom.elf bootrom.bin: ../bootrom/obj/bootrom.elf
$(OBJCOPY) --gap-fill=0xff --pad-to 0x00102000 -O binary $^ $@ $(OBJCOPY) --gap-fill=0xff --pad-to 0x00102000 -O binary $^ $@
osimage.bin: ../armsrc/obj/osimage.elf fullimage.bin: ../armsrc/obj/fullimage.elf
$(OBJCOPY) --gap-fill=0xff -O binary $^ $@ $(OBJCOPY) --gap-fill=0xff -O binary $^ $@
proxmark3_recovery.bin: bootrom.bin osimage.bin proxmark3_recovery.bin: bootrom.bin fullimage.bin
cat bootrom.bin osimage.bin > $@ cat bootrom.bin fullimage.bin > $@
clean: clean:
rm -f $(BINS) rm -f $(BINS)