]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/fpga_compress.c
Merge pull request #884 from pwpiwi/fix_iclass_snoop
[proxmark3-svn] / client / fpga_compress.c
CommitLineData
e6153040 1//-----------------------------------------------------------------------------
472345da 2// piwi, 2017, 2018
3//
e6153040 4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8e074056 8// Compression tool for FPGA config files. Compress several *.bit files at
9// compile time. Decompression is done at run time (see fpgaloader.c).
10// This uses the zlib library tuned to this specific case. The small file sizes
11// allow to use "insane" parameters for optimum compression ratio.
e6153040 12//-----------------------------------------------------------------------------
13
14#include <stdio.h>
15#include <stdlib.h>
472345da 16#include <libgen.h>
e6153040 17#include <string.h>
f3919878 18#include <stdint.h>
fb228974 19#include <stdbool.h>
472345da 20#include "fpga.h"
f3919878 21#include "zlib.h"
e6153040 22
23#define MAX(a,b) ((a)>(b)?(a):(b))
24
f3919878 25// zlib configuration
015520dc
JB
26#define COMPRESS_LEVEL 9 // use best possible compression
27#define COMPRESS_WINDOW_BITS 15 // default = max = 15 for a window of 2^15 = 32KBytes
28#define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8.
fb228974 29/* COMPRESS_STRATEGY can be
30 Z_DEFAULT_STRATEGY (the default),
31 Z_FILTERED (more huffmann, less string matching),
32 Z_HUFFMAN_ONLY (huffman only, no string matching)
33 Z_RLE (distances limited to one)
34 Z_FIXED (prevents the use of dynamic Huffman codes)
ad8a18e6
JB
35*/
36
015520dc 37#define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
fb228974 38// zlib tuning parameters:
015520dc
JB
39#define COMPRESS_GOOD_LENGTH 258
40#define COMPRESS_MAX_LAZY 258
41#define COMPRESS_MAX_NICE_LENGTH 258
42#define COMPRESS_MAX_CHAIN 8192
fb228974 43
7f9e4c25 44#define HARDNESTED_TABLE_SIZE (sizeof(uint32_t) * ((1L<<19)+1))
e6153040 45
8e074056 46static void usage(void)
e6153040 47{
36b1cdd1
OM
48 fprintf(stdout, "Usage: fpga_compress <infile1> <infile2> ... <infile_n> <outfile>\n");
49 fprintf(stdout, " Combine n FPGA bitstream files and compress them into one.\n\n");
472345da 50 fprintf(stdout, " fpga_compress -v <infile1> <infile2> ... <infile_n> <outfile>\n");
51 fprintf(stdout, " Extract Version Information from FPGA bitstream files and write it to <outfile>\n\n");
52 fprintf(stdout, " fpga_compress -d <infile> <outfile>\n");
53 fprintf(stdout, " Decompress <infile>. Write result to <outfile>\n\n");
54 fprintf(stdout, " fpga_compress -t <infile> <outfile>\n");
55 fprintf(stdout, " Compress hardnested table <infile>. Write result to <outfile>\n\n");
e6153040 56}
57
58
f3919878 59static voidpf fpga_deflate_malloc(voidpf opaque, uInt items, uInt size)
e6153040 60{
f3919878 61 return malloc(items*size);
e6153040 62}
e6153040 63
f3919878 64
65static void fpga_deflate_free(voidpf opaque, voidpf address)
e6153040 66{
472345da 67 free(address);
f3919878 68}
e6153040 69
f3919878 70
fb228974 71static bool all_feof(FILE *infile[], uint8_t num_infiles)
72{
73 for (uint16_t i = 0; i < num_infiles; i++) {
74 if (!feof(infile[i])) {
75 return false;
76 }
77 }
78
79 return true;
80}
81
82
7f9e4c25 83int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile, bool hardnested_mode)
e6153040 84{
fb228974 85 uint8_t *fpga_config;
86 uint32_t i;
ad8a18e6
JB
87 int32_t ret;
88 uint8_t c;
f3919878 89 z_stream compressed_fpga_stream;
fb228974 90
7f9e4c25 91 if (hardnested_mode) {
92 fpga_config = malloc(num_infiles * HARDNESTED_TABLE_SIZE);
93 } else {
94 fpga_config = malloc(num_infiles * FPGA_CONFIG_SIZE);
95 }
8e074056 96 // read the input files. Interleave them into fpga_config[]
e6153040 97 i = 0;
fb228974 98 do {
7b242c1c 99
7f9e4c25 100 if (i >= num_infiles * (hardnested_mode?HARDNESTED_TABLE_SIZE:FPGA_CONFIG_SIZE)) {
101 if (hardnested_mode) {
102 fprintf(stderr, "Input file too big (> %lu bytes). This is probably not a hardnested bitflip state table.\n", HARDNESTED_TABLE_SIZE);
103 } else {
104 fprintf(stderr, "Input files too big (total > %lu bytes). These are probably not PM3 FPGA config files.\n", num_infiles*FPGA_CONFIG_SIZE);
105 }
7b242c1c 106 for(uint16_t j = 0; j < num_infiles; j++) {
107 fclose(infile[j]);
108 }
38d618ba 109 free(fpga_config);
7b242c1c 110 return(EXIT_FAILURE);
111 }
112
fb228974 113 for(uint16_t j = 0; j < num_infiles; j++) {
114 for(uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) {
44964fd1 115 c = (uint8_t)fgetc(infile[j]);
0fa01ec7 116 if (!feof(infile[j])) {
117 fpga_config[i++] = c;
118 } else if (num_infiles > 1) {
119 fpga_config[i++] = '\0';
120 }
fb228974 121 }
122 }
123
fb228974 124 } while (!all_feof(infile, num_infiles));
e6153040 125
f3919878 126 // initialize zlib structures
127 compressed_fpga_stream.next_in = fpga_config;
128 compressed_fpga_stream.avail_in = i;
129 compressed_fpga_stream.zalloc = fpga_deflate_malloc;
130 compressed_fpga_stream.zfree = fpga_deflate_free;
38d618ba 131 compressed_fpga_stream.opaque = Z_NULL;
fb228974 132 ret = deflateInit2(&compressed_fpga_stream,
133 COMPRESS_LEVEL,
134 Z_DEFLATED,
135 COMPRESS_WINDOW_BITS,
136 COMPRESS_MEM_LEVEL,
137 COMPRESS_STRATEGY);
138
f3919878 139 // estimate the size of the compressed output
ad8a18e6 140 uint32_t outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in);
f3919878 141 uint8_t *outbuf = malloc(outsize_max);
142 compressed_fpga_stream.next_out = outbuf;
143 compressed_fpga_stream.avail_out = outsize_max;
ad8a18e6 144
fb228974 145 if (ret == Z_OK) {
146 ret = deflateTune(&compressed_fpga_stream,
147 COMPRESS_GOOD_LENGTH,
148 COMPRESS_MAX_LAZY,
149 COMPRESS_MAX_NICE_LENGTH,
150 COMPRESS_MAX_CHAIN);
151 }
152
f3919878 153 if (ret == Z_OK) {
154 ret = deflate(&compressed_fpga_stream, Z_FINISH);
e6153040 155 }
e6153040 156
36b1cdd1 157 fprintf(stdout, "compressed %u input bytes to %lu output bytes\n", i, compressed_fpga_stream.total_out);
f3919878 158
159 if (ret != Z_STREAM_END) {
ad8a18e6 160 fprintf(stderr, "Error in deflate(): %i %s\n", ret, compressed_fpga_stream.msg);
f3919878 161 free(outbuf);
162 deflateEnd(&compressed_fpga_stream);
fb228974 163 for(uint16_t j = 0; j < num_infiles; j++) {
164 fclose(infile[j]);
165 }
f3919878 166 fclose(outfile);
fb228974 167 free(infile);
168 free(fpga_config);
7b242c1c 169 return(EXIT_FAILURE);
f3919878 170 }
e6153040 171
f3919878 172 for (i = 0; i < compressed_fpga_stream.total_out; i++) {
173 fputc(outbuf[i], outfile);
174 }
175
176 free(outbuf);
177 deflateEnd(&compressed_fpga_stream);
fb228974 178 for(uint16_t j = 0; j < num_infiles; j++) {
179 fclose(infile[j]);
180 }
e6153040 181 fclose(outfile);
fb228974 182 free(infile);
183 free(fpga_config);
184
7b242c1c 185 return(EXIT_SUCCESS);
f3919878 186
e6153040 187}
188
189
4b3f6d79 190int zlib_decompress(FILE *infile, FILE *outfile)
191{
192 #define DECOMPRESS_BUF_SIZE 1024
193 uint8_t outbuf[DECOMPRESS_BUF_SIZE];
194 uint8_t inbuf[DECOMPRESS_BUF_SIZE];
ad8a18e6 195 int32_t ret;
4b3f6d79 196
197 z_stream compressed_fpga_stream;
8e074056 198
4b3f6d79 199 // initialize zlib structures
200 compressed_fpga_stream.next_in = inbuf;
201 compressed_fpga_stream.avail_in = 0;
202 compressed_fpga_stream.next_out = outbuf;
203 compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
204 compressed_fpga_stream.zalloc = fpga_deflate_malloc;
205 compressed_fpga_stream.zfree = fpga_deflate_free;
38d618ba 206 compressed_fpga_stream.opaque = Z_NULL;
4b3f6d79 207
208 ret = inflateInit2(&compressed_fpga_stream, 0);
209
210 do {
211 if (compressed_fpga_stream.avail_in == 0) {
212 compressed_fpga_stream.next_in = inbuf;
213 uint16_t i = 0;
214 do {
ad8a18e6 215 int32_t c = fgetc(infile);
4b3f6d79 216 if (!feof(infile)) {
38d618ba 217 inbuf[i++] = c & 0xFF;
4b3f6d79 218 compressed_fpga_stream.avail_in++;
219 } else {
220 break;
221 }
222 } while (i < DECOMPRESS_BUF_SIZE);
223 }
224
225 ret = inflate(&compressed_fpga_stream, Z_SYNC_FLUSH);
226
227 if (ret != Z_OK && ret != Z_STREAM_END) {
228 break;
229 }
230
231 if (compressed_fpga_stream.avail_out == 0) {
232 for (uint16_t i = 0; i < DECOMPRESS_BUF_SIZE; i++) {
233 fputc(outbuf[i], outfile);
234 }
235 compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
236 compressed_fpga_stream.next_out = outbuf;
237 }
238 } while (ret == Z_OK);
239
240 if (ret == Z_STREAM_END) { // reached end of input
241 uint16_t i = 0;
242 while (compressed_fpga_stream.avail_out < DECOMPRESS_BUF_SIZE) {
243 fputc(outbuf[i++], outfile);
244 compressed_fpga_stream.avail_out++;
245 }
246 fclose(outfile);
247 fclose(infile);
7b242c1c 248 return(EXIT_SUCCESS);
4b3f6d79 249 } else {
ad8a18e6 250 fprintf(stderr, "Error. Inflate() returned error %i, %s", ret, compressed_fpga_stream.msg);
4b3f6d79 251 fclose(outfile);
252 fclose(infile);
7b242c1c 253 return(EXIT_FAILURE);
4b3f6d79 254 }
255
256}
257
f3919878 258
472345da 259/* Simple Xilinx .bit parser. The file starts with the fixed opaque byte sequence
260 * 00 09 0f f0 0f f0 0f f0 0f f0 00 00 01
261 * After that the format is 1 byte section type (ASCII character), 2 byte length
262 * (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
263 * length.
264 */
265static int bitparse_find_section(FILE *infile, char section_name, unsigned int *section_length)
266{
267 int result = 0;
268 #define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section
269 uint16_t numbytes = 0;
270 while(numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) {
271 char current_name = (char)fgetc(infile);
272 numbytes++;
273 if(current_name < 'a' || current_name > 'e') {
274 /* Strange section name, abort */
275 break;
276 }
277 unsigned int current_length = 0;
278 switch(current_name) {
279 case 'e':
280 /* Four byte length field */
281 current_length += fgetc(infile) << 24;
282 current_length += fgetc(infile) << 16;
283 numbytes += 2;
284 default: /* Fall through, two byte length field */
285 current_length += fgetc(infile) << 8;
286 current_length += fgetc(infile) << 0;
287 numbytes += 2;
288 }
289
290 if(current_name != 'e' && current_length > 255) {
291 /* Maybe a parse error */
292 break;
293 }
294
295 if(current_name == section_name) {
296 /* Found it */
297 *section_length = current_length;
298 result = 1;
299 break;
300 }
301
302 for (uint16_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) {
303 (void)fgetc(infile);
304 numbytes++;
305 }
306 }
307
308 return result;
309}
310
311
312static int FpgaGatherVersion(FILE *infile, char* infile_name, char *dst, int len)
313{
314 unsigned int fpga_info_len;
315 char tempstr[40] = {0x00};
316
317 dst[0] = '\0';
318
319 for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++) {
320 if (fgetc(infile) != bitparse_fixed_header[i]) {
321 fprintf(stderr, "Invalid FPGA file. Aborting...\n\n");
322 return(EXIT_FAILURE);
323 }
324 }
325
326 strncat(dst, basename(infile_name), len-1);
327 // if (bitparse_find_section(infile, 'a', &fpga_info_len)) {
328 // for (uint16_t i = 0; i < fpga_info_len; i++) {
329 // char c = (char)fgetc(infile);
330 // if (i < sizeof(tempstr)) {
331 // tempstr[i] = c;
332 // }
333 // }
334 // strncat(dst, tempstr, len-1);
335 // }
336 strncat(dst, " built", len-1);
337 if (bitparse_find_section(infile, 'b', &fpga_info_len)) {
338 strncat(dst, " for ", len-1);
339 for (uint16_t i = 0; i < fpga_info_len; i++) {
340 char c = (char)fgetc(infile);
341 if (i < sizeof(tempstr)) {
342 tempstr[i] = c;
343 }
344 }
345 strncat(dst, tempstr, len-1);
346 }
347 if (bitparse_find_section(infile, 'c', &fpga_info_len)) {
348 strncat(dst, " on ", len-1);
349 for (uint16_t i = 0; i < fpga_info_len; i++) {
350 char c = (char)fgetc(infile);
351 if (i < sizeof(tempstr)) {
352 tempstr[i] = c;
353 }
354 }
355 strncat(dst, tempstr, len-1);
356 }
357 if (bitparse_find_section(infile, 'd', &fpga_info_len)) {
358 strncat(dst, " at ", len-1);
359 for (uint16_t i = 0; i < fpga_info_len; i++) {
360 char c = (char)fgetc(infile);
361 if (i < sizeof(tempstr)) {
362 tempstr[i] = c;
363 }
364 }
365 strncat(dst, tempstr, len-1);
366 }
367 return 0;
368}
369
370
371static void print_version_info_preamble(FILE *outfile, int num_infiles) {
372 fprintf(outfile, "//-----------------------------------------------------------------------------\n");
373 fprintf(outfile, "// piwi, 2018\n");
374 fprintf(outfile, "//\n");
375 fprintf(outfile, "// This code is licensed to you under the terms of the GNU GPL, version 2 or,\n");
376 fprintf(outfile, "// at your option, any later version. See the LICENSE.txt file for the text of\n");
377 fprintf(outfile, "// the license.\n");
378 fprintf(outfile, "//-----------------------------------------------------------------------------\n");
379 fprintf(outfile, "// Version information on fpga images\n");
380 fprintf(outfile, "//\n");
381 fprintf(outfile, "// This file is generated by fpga_compress. Don't edit!\n");
382 fprintf(outfile, "//-----------------------------------------------------------------------------\n");
383 fprintf(outfile, "\n");
384 fprintf(outfile, "\n");
385 fprintf(outfile, "const int fpga_bitstream_num = %d;\n", num_infiles);
386 fprintf(outfile, "const char* const fpga_version_information[%d] = {\n", num_infiles);
387}
388
389
390static int generate_fpga_version_info(FILE *infile[], char *infile_names[], int num_infiles, FILE *outfile) {
391
392 char version_string[80] = "";
393
394 print_version_info_preamble(outfile, num_infiles);
395
396 for (int i = 0; i < num_infiles; i++) {
397 FpgaGatherVersion(infile[i], infile_names[i], version_string, sizeof(version_string));
398 fprintf(outfile, "\t\"%s\"", version_string);
399 if (i != num_infiles-1) {
400 fprintf(outfile, ",");
401 }
402 fprintf(outfile,"\n");
403 }
404
405 fprintf(outfile, "};\n");
406
407 return 0;
408}
409
410
e6153040 411int main(int argc, char **argv)
412{
fb228974 413 FILE **infiles;
472345da 414 char **infile_names;
fb228974 415 FILE *outfile;
e6153040 416
fb228974 417 if (argc == 1 || argc == 2) {
8e074056 418 usage();
7b242c1c 419 return(EXIT_FAILURE);
fb228974 420 }
421
ad8a18e6 422 if (!strcmp(argv[1], "-d")) { // Decompress
7f9e4c25 423
4b3f6d79 424 infiles = calloc(1, sizeof(FILE*));
425 if (argc != 4) {
8e074056 426 usage();
7b242c1c 427 return(EXIT_FAILURE);
4b3f6d79 428 }
429 infiles[0] = fopen(argv[2], "rb");
430 if (infiles[0] == NULL) {
472345da 431 fprintf(stderr, "Error. Cannot open input file %s\n\n", argv[2]);
7b242c1c 432 return(EXIT_FAILURE);
4b3f6d79 433 }
434 outfile = fopen(argv[3], "wb");
435 if (outfile == NULL) {
472345da 436 fprintf(stderr, "Error. Cannot open output file %s\n\n", argv[3]);
7b242c1c 437 return(EXIT_FAILURE);
4b3f6d79 438 }
439 return zlib_decompress(infiles[0], outfile);
4b3f6d79 440
472345da 441 } else { // Compress or gemerate version info
8e074056 442
7f9e4c25 443 bool hardnested_mode = false;
472345da 444 bool generate_version_file = false;
7f9e4c25 445 int num_input_files = 0;
472345da 446 if (!strcmp(argv[1], "-t")) { // compress one hardnested table
7f9e4c25 447 if (argc != 4) {
448 usage();
449 return(EXIT_FAILURE);
450 }
451 hardnested_mode = true;
452 num_input_files = 1;
472345da 453 } else if (!strcmp(argv[1], "-v")) { // generate version info
454 generate_version_file = true;
455 num_input_files = argc-3;
456 } else { // compress 1..n fpga files
7f9e4c25 457 num_input_files = argc-2;
458 }
472345da 459
7f9e4c25 460 infiles = calloc(num_input_files, sizeof(FILE*));
472345da 461 infile_names = calloc(num_input_files, sizeof(char*));
462 for (uint16_t i = 0; i < num_input_files; i++) {
463 infile_names[i] = argv[i+((hardnested_mode || generate_version_file)?2:1)];
464 infiles[i] = fopen(infile_names[i], "rb");
8e074056 465 if (infiles[i] == NULL) {
472345da 466 fprintf(stderr, "Error. Cannot open input file %s\n\n", infile_names[i]);
7b242c1c 467 return(EXIT_FAILURE);
8e074056 468 }
469 }
470 outfile = fopen(argv[argc-1], "wb");
471 if (outfile == NULL) {
472345da 472 fprintf(stderr, "Error. Cannot open output file %s\n\n", argv[argc-1]);
7b242c1c 473 return(EXIT_FAILURE);
e6153040 474 }
472345da 475 if (generate_version_file) {
476 if (generate_fpga_version_info(infiles, infile_names, num_input_files, outfile)) {
477 return(EXIT_FAILURE);
478 }
479 } else {
480 return zlib_compress(infiles, num_input_files, outfile, hardnested_mode);
481 }
fb228974 482 }
e6153040 483}
Impressum, Datenschutz