1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // Flasher frontend tool
7 //-----------------------------------------------------------------------------
16 #define MAX(a,b) ((a)>(b)?(a):(b))
19 #define COMPRESS_LEVEL 9 // use best possible compression
20 #define COMPRESS_WINDOW_BITS 15 // default = 15 for a window of 2^15 = 32KBytes
21 #define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8. Must be < 9
22 /* COMPRESS_STRATEGY can be
23 Z_DEFAULT_STRATEGY (the default),
24 Z_FILTERED (more huffmann, less string matching),
25 Z_HUFFMAN_ONLY (huffman only, no string matching)
26 Z_RLE (distances limited to one)
27 Z_FIXED (prevents the use of dynamic Huffman codes)
29 #define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
30 // zlib tuning parameters:
31 #define COMPRESS_GOOD_LENGTH 258
32 #define COMPRESS_MAX_LAZY 258
33 #define COMPRESS_MAX_NICE_LENGTH 258
34 #define COMPRESS_MAX_CHAIN 8192
36 #define FPGA_INTERLEAVE_SIZE 288 // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
37 #define FPGA_CONFIG_SIZE 42336 // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
39 static void usage(char *argv0
)
41 fprintf(stderr
, "Usage: %s <infile1> <infile2> ... <infile_n> <outfile>\n\n", argv0
);
42 fprintf(stderr
, "Combines n FPGA bitstream files and compresses them into one.\n\n");
46 static voidpf
fpga_deflate_malloc(voidpf opaque
, uInt items
, uInt size
)
48 return malloc(items
*size
);
52 static void fpga_deflate_free(voidpf opaque
, voidpf address
)
58 static bool all_feof(FILE *infile
[], uint8_t num_infiles
)
60 for (uint16_t i
= 0; i
< num_infiles
; i
++) {
61 if (!feof(infile
[i
])) {
70 int zlib_compress(FILE *infile
[], uint8_t num_infiles
, FILE *outfile
)
76 z_stream compressed_fpga_stream
;
78 fpga_config
= malloc(num_infiles
* FPGA_CONFIG_SIZE
);
80 // read the input files interleaving into fpga_config[]
83 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
84 for(uint16_t k
= 0; k
< FPGA_INTERLEAVE_SIZE
; k
++) {
86 if (!feof(infile
[j
])) {
88 } else if (num_infiles
> 1) {
89 fpga_config
[i
++] = '\0';
94 if (i
> num_infiles
* FPGA_CONFIG_SIZE
) {
95 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
);
96 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
101 } while (!all_feof(infile
, num_infiles
));
103 fprintf(stderr
, "Read a total of %ld bytes from %d files\n", i
, num_infiles
);
105 // initialize zlib structures
106 compressed_fpga_stream
.next_in
= fpga_config
;
107 compressed_fpga_stream
.avail_in
= i
;
108 compressed_fpga_stream
.zalloc
= fpga_deflate_malloc
;
109 compressed_fpga_stream
.zfree
= fpga_deflate_free
;
111 ret
= deflateInit2(&compressed_fpga_stream
,
114 COMPRESS_WINDOW_BITS
,
118 // estimate the size of the compressed output
119 unsigned int outsize_max
= deflateBound(&compressed_fpga_stream
, compressed_fpga_stream
.avail_in
);
120 uint8_t *outbuf
= malloc(outsize_max
);
121 compressed_fpga_stream
.next_out
= outbuf
;
122 compressed_fpga_stream
.avail_out
= outsize_max
;
126 ret
= deflateTune(&compressed_fpga_stream
,
127 COMPRESS_GOOD_LENGTH
,
129 COMPRESS_MAX_NICE_LENGTH
,
134 ret
= deflate(&compressed_fpga_stream
, Z_FINISH
);
137 fprintf(stderr
, "\ncompressed %d input bytes to %d output bytes\n", i
, compressed_fpga_stream
.total_out
);
139 if (ret
!= Z_STREAM_END
) {
140 fprintf(stderr
, "Error in deflate(): %d %s\n", ret
, compressed_fpga_stream
.msg
);
142 deflateEnd(&compressed_fpga_stream
);
143 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
152 for (i
= 0; i
< compressed_fpga_stream
.total_out
; i
++) {
153 fputc(outbuf
[i
], outfile
);
157 deflateEnd(&compressed_fpga_stream
);
158 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
170 int zlib_decompress(FILE *infile
, FILE *outfile
)
172 #define DECOMPRESS_BUF_SIZE 1024
173 uint8_t outbuf
[DECOMPRESS_BUF_SIZE
];
174 uint8_t inbuf
[DECOMPRESS_BUF_SIZE
];
177 z_stream compressed_fpga_stream
;
178 // initialize zlib structures
179 compressed_fpga_stream
.next_in
= inbuf
;
180 compressed_fpga_stream
.avail_in
= 0;
181 compressed_fpga_stream
.next_out
= outbuf
;
182 compressed_fpga_stream
.avail_out
= DECOMPRESS_BUF_SIZE
;
183 compressed_fpga_stream
.zalloc
= fpga_deflate_malloc
;
184 compressed_fpga_stream
.zfree
= fpga_deflate_free
;
186 ret
= inflateInit2(&compressed_fpga_stream
, 0);
189 if (compressed_fpga_stream
.avail_in
== 0) {
190 compressed_fpga_stream
.next_in
= inbuf
;
193 uint8_t c
= fgetc(infile
);
196 compressed_fpga_stream
.avail_in
++;
200 } while (i
< DECOMPRESS_BUF_SIZE
);
203 ret
= inflate(&compressed_fpga_stream
, Z_SYNC_FLUSH
);
205 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
209 if (compressed_fpga_stream
.avail_out
== 0) {
210 for (uint16_t i
= 0; i
< DECOMPRESS_BUF_SIZE
; i
++) {
211 fputc(outbuf
[i
], outfile
);
213 compressed_fpga_stream
.avail_out
= DECOMPRESS_BUF_SIZE
;
214 compressed_fpga_stream
.next_out
= outbuf
;
216 } while (ret
== Z_OK
);
218 if (ret
== Z_STREAM_END
) { // reached end of input
220 while (compressed_fpga_stream
.avail_out
< DECOMPRESS_BUF_SIZE
) {
221 fputc(outbuf
[i
++], outfile
);
222 compressed_fpga_stream
.avail_out
++;
228 fprintf(stderr
, "Error. Inflate() returned error %d, %s", ret
, compressed_fpga_stream
.msg
);
237 int main(int argc
, char **argv
)
242 if (argc
== 1 || argc
== 2) {
247 if (!strcmp(argv
[1], "-d")) {
248 infiles
= calloc(1, sizeof(FILE*));
253 infiles
[0] = fopen(argv
[2], "rb");
254 if (infiles
[0] == NULL
) {
255 fprintf(stderr
, "Error. Cannot open input file %s", argv
[2]);
258 outfile
= fopen(argv
[3], "wb");
259 if (outfile
== NULL
) {
260 fprintf(stderr
, "Error. Cannot open output file %s", argv
[3]);
263 return zlib_decompress(infiles
[0], outfile
);
267 infiles
= calloc(argc
-2, sizeof(FILE*));
269 for (uint16_t i
= 0; i
< argc
-2; i
++) {
270 infiles
[i
] = fopen(argv
[i
+1], "rb");
271 if (infiles
[i
] == NULL
) {
272 fprintf(stderr
, "Error. Cannot open input file %s", argv
[i
+1]);
277 outfile
= fopen(argv
[argc
-1], "wb");
278 if (outfile
== NULL
) {
279 fprintf(stderr
, "Error. Cannot open output file %s", argv
[argc
-1]);
283 return zlib_compress(infiles
, argc
-2, outfile
);