]> git.zerfleddert.de Git - proxmark3-svn/blame - client/fpga_compress.c
Merge pull request #119 from marshmellow42/pm3+reveng
[proxmark3-svn] / client / fpga_compress.c
CommitLineData
e6153040 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
4// the license.
5//-----------------------------------------------------------------------------
8e074056 6// Compression tool for FPGA config files. Compress several *.bit files at
7// compile time. Decompression is done at run time (see fpgaloader.c).
8// This uses the zlib library tuned to this specific case. The small file sizes
9// allow to use "insane" parameters for optimum compression ratio.
e6153040 10//-----------------------------------------------------------------------------
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
f3919878 15#include <stdint.h>
fb228974 16#include <stdbool.h>
f3919878 17#include "zlib.h"
e6153040 18
19#define MAX(a,b) ((a)>(b)?(a):(b))
20
f3919878 21// zlib configuration
015520dc
JB
22#define COMPRESS_LEVEL 9 // use best possible compression
23#define COMPRESS_WINDOW_BITS 15 // default = max = 15 for a window of 2^15 = 32KBytes
24#define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8.
fb228974 25/* COMPRESS_STRATEGY can be
26 Z_DEFAULT_STRATEGY (the default),
27 Z_FILTERED (more huffmann, less string matching),
28 Z_HUFFMAN_ONLY (huffman only, no string matching)
29 Z_RLE (distances limited to one)
30 Z_FIXED (prevents the use of dynamic Huffman codes)
ad8a18e6
JB
31*/
32
015520dc 33#define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
fb228974 34// zlib tuning parameters:
015520dc
JB
35#define COMPRESS_GOOD_LENGTH 258
36#define COMPRESS_MAX_LAZY 258
37#define COMPRESS_MAX_NICE_LENGTH 258
38#define COMPRESS_MAX_CHAIN 8192
fb228974 39
015520dc
JB
40#define FPGA_INTERLEAVE_SIZE 288 // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
41#define FPGA_CONFIG_SIZE 42336 // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
e6153040 42
8e074056 43static void usage(void)
e6153040 44{
8e074056 45 fprintf(stderr, "Usage: fpga_compress <infile1> <infile2> ... <infile_n> <outfile>\n");
46 fprintf(stderr, " Combine n FPGA bitstream files and compress them into one.\n\n");
47 fprintf(stderr, " fpga_compress -d <infile> <outfile>");
48 fprintf(stderr, " Decompress <infile>. Write result to <outfile>");
e6153040 49}
50
51
f3919878 52static voidpf fpga_deflate_malloc(voidpf opaque, uInt items, uInt size)
e6153040 53{
f3919878 54 return malloc(items*size);
e6153040 55}
e6153040 56
f3919878 57
58static void fpga_deflate_free(voidpf opaque, voidpf address)
e6153040 59{
f3919878 60 return free(address);
61}
e6153040 62
f3919878 63
fb228974 64static bool all_feof(FILE *infile[], uint8_t num_infiles)
65{
66 for (uint16_t i = 0; i < num_infiles; i++) {
67 if (!feof(infile[i])) {
68 return false;
69 }
70 }
71
72 return true;
73}
74
75
76int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile)
e6153040 77{
fb228974 78 uint8_t *fpga_config;
79 uint32_t i;
ad8a18e6
JB
80 int32_t ret;
81 uint8_t c;
f3919878 82 z_stream compressed_fpga_stream;
fb228974 83
84 fpga_config = malloc(num_infiles * FPGA_CONFIG_SIZE);
e6153040 85
8e074056 86 // read the input files. Interleave them into fpga_config[]
e6153040 87 i = 0;
fb228974 88 do {
7b242c1c 89
90 if (i >= num_infiles * FPGA_CONFIG_SIZE) {
ad8a18e6 91 fprintf(stderr, "Input files too big (total > %u bytes). These are probably not PM3 FPGA config files.\n", num_infiles*FPGA_CONFIG_SIZE);
7b242c1c 92 for(uint16_t j = 0; j < num_infiles; j++) {
93 fclose(infile[j]);
94 }
38d618ba 95 free(fpga_config);
7b242c1c 96 return(EXIT_FAILURE);
97 }
98
fb228974 99 for(uint16_t j = 0; j < num_infiles; j++) {
100 for(uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) {
101 c = fgetc(infile[j]);
0fa01ec7 102 if (!feof(infile[j])) {
103 fpga_config[i++] = c;
104 } else if (num_infiles > 1) {
105 fpga_config[i++] = '\0';
106 }
fb228974 107 }
108 }
109
fb228974 110 } while (!all_feof(infile, num_infiles));
e6153040 111
f3919878 112 // initialize zlib structures
113 compressed_fpga_stream.next_in = fpga_config;
114 compressed_fpga_stream.avail_in = i;
115 compressed_fpga_stream.zalloc = fpga_deflate_malloc;
116 compressed_fpga_stream.zfree = fpga_deflate_free;
38d618ba 117 compressed_fpga_stream.opaque = Z_NULL;
fb228974 118 ret = deflateInit2(&compressed_fpga_stream,
119 COMPRESS_LEVEL,
120 Z_DEFLATED,
121 COMPRESS_WINDOW_BITS,
122 COMPRESS_MEM_LEVEL,
123 COMPRESS_STRATEGY);
124
f3919878 125 // estimate the size of the compressed output
ad8a18e6 126 uint32_t outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in);
f3919878 127 uint8_t *outbuf = malloc(outsize_max);
128 compressed_fpga_stream.next_out = outbuf;
129 compressed_fpga_stream.avail_out = outsize_max;
ad8a18e6 130
fb228974 131 if (ret == Z_OK) {
132 ret = deflateTune(&compressed_fpga_stream,
133 COMPRESS_GOOD_LENGTH,
134 COMPRESS_MAX_LAZY,
135 COMPRESS_MAX_NICE_LENGTH,
136 COMPRESS_MAX_CHAIN);
137 }
138
f3919878 139 if (ret == Z_OK) {
140 ret = deflate(&compressed_fpga_stream, Z_FINISH);
e6153040 141 }
e6153040 142
ad8a18e6 143 fprintf(stderr, "compressed %u input bytes to %lu output bytes\n", i, compressed_fpga_stream.total_out);
f3919878 144
145 if (ret != Z_STREAM_END) {
ad8a18e6 146 fprintf(stderr, "Error in deflate(): %i %s\n", ret, compressed_fpga_stream.msg);
f3919878 147 free(outbuf);
148 deflateEnd(&compressed_fpga_stream);
fb228974 149 for(uint16_t j = 0; j < num_infiles; j++) {
150 fclose(infile[j]);
151 }
f3919878 152 fclose(outfile);
fb228974 153 free(infile);
154 free(fpga_config);
7b242c1c 155 return(EXIT_FAILURE);
f3919878 156 }
e6153040 157
f3919878 158 for (i = 0; i < compressed_fpga_stream.total_out; i++) {
159 fputc(outbuf[i], outfile);
160 }
161
162 free(outbuf);
163 deflateEnd(&compressed_fpga_stream);
fb228974 164 for(uint16_t j = 0; j < num_infiles; j++) {
165 fclose(infile[j]);
166 }
e6153040 167 fclose(outfile);
fb228974 168 free(infile);
169 free(fpga_config);
170
7b242c1c 171 return(EXIT_SUCCESS);
f3919878 172
e6153040 173}
174
175
4b3f6d79 176int zlib_decompress(FILE *infile, FILE *outfile)
177{
178 #define DECOMPRESS_BUF_SIZE 1024
179 uint8_t outbuf[DECOMPRESS_BUF_SIZE];
180 uint8_t inbuf[DECOMPRESS_BUF_SIZE];
ad8a18e6 181 int32_t ret;
4b3f6d79 182
183 z_stream compressed_fpga_stream;
8e074056 184
4b3f6d79 185 // initialize zlib structures
186 compressed_fpga_stream.next_in = inbuf;
187 compressed_fpga_stream.avail_in = 0;
188 compressed_fpga_stream.next_out = outbuf;
189 compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
190 compressed_fpga_stream.zalloc = fpga_deflate_malloc;
191 compressed_fpga_stream.zfree = fpga_deflate_free;
38d618ba 192 compressed_fpga_stream.opaque = Z_NULL;
4b3f6d79 193
194 ret = inflateInit2(&compressed_fpga_stream, 0);
195
196 do {
197 if (compressed_fpga_stream.avail_in == 0) {
198 compressed_fpga_stream.next_in = inbuf;
199 uint16_t i = 0;
200 do {
ad8a18e6 201 int32_t c = fgetc(infile);
4b3f6d79 202 if (!feof(infile)) {
38d618ba 203 inbuf[i++] = c & 0xFF;
4b3f6d79 204 compressed_fpga_stream.avail_in++;
205 } else {
206 break;
207 }
208 } while (i < DECOMPRESS_BUF_SIZE);
209 }
210
211 ret = inflate(&compressed_fpga_stream, Z_SYNC_FLUSH);
212
213 if (ret != Z_OK && ret != Z_STREAM_END) {
214 break;
215 }
216
217 if (compressed_fpga_stream.avail_out == 0) {
218 for (uint16_t i = 0; i < DECOMPRESS_BUF_SIZE; i++) {
219 fputc(outbuf[i], outfile);
220 }
221 compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
222 compressed_fpga_stream.next_out = outbuf;
223 }
224 } while (ret == Z_OK);
225
226 if (ret == Z_STREAM_END) { // reached end of input
227 uint16_t i = 0;
228 while (compressed_fpga_stream.avail_out < DECOMPRESS_BUF_SIZE) {
229 fputc(outbuf[i++], outfile);
230 compressed_fpga_stream.avail_out++;
231 }
232 fclose(outfile);
233 fclose(infile);
7b242c1c 234 return(EXIT_SUCCESS);
4b3f6d79 235 } else {
ad8a18e6 236 fprintf(stderr, "Error. Inflate() returned error %i, %s", ret, compressed_fpga_stream.msg);
4b3f6d79 237 fclose(outfile);
238 fclose(infile);
7b242c1c 239 return(EXIT_FAILURE);
4b3f6d79 240 }
241
242}
243
f3919878 244
e6153040 245int main(int argc, char **argv)
246{
fb228974 247 FILE **infiles;
248 FILE *outfile;
e6153040 249
fb228974 250 if (argc == 1 || argc == 2) {
8e074056 251 usage();
7b242c1c 252 return(EXIT_FAILURE);
fb228974 253 }
254
ad8a18e6 255 if (!strcmp(argv[1], "-d")) { // Decompress
4b3f6d79 256 infiles = calloc(1, sizeof(FILE*));
257 if (argc != 4) {
8e074056 258 usage();
7b242c1c 259 return(EXIT_FAILURE);
4b3f6d79 260 }
261 infiles[0] = fopen(argv[2], "rb");
262 if (infiles[0] == NULL) {
263 fprintf(stderr, "Error. Cannot open input file %s", argv[2]);
7b242c1c 264 return(EXIT_FAILURE);
4b3f6d79 265 }
266 outfile = fopen(argv[3], "wb");
267 if (outfile == NULL) {
268 fprintf(stderr, "Error. Cannot open output file %s", argv[3]);
7b242c1c 269 return(EXIT_FAILURE);
4b3f6d79 270 }
271 return zlib_decompress(infiles[0], outfile);
4b3f6d79 272
ad8a18e6 273 } else { // Compress
8e074056 274
275 infiles = calloc(argc-2, sizeof(FILE*));
276 for (uint16_t i = 0; i < argc-2; i++) {
277 infiles[i] = fopen(argv[i+1], "rb");
278 if (infiles[i] == NULL) {
279 fprintf(stderr, "Error. Cannot open input file %s", argv[i+1]);
7b242c1c 280 return(EXIT_FAILURE);
8e074056 281 }
282 }
283 outfile = fopen(argv[argc-1], "wb");
284 if (outfile == NULL) {
285 fprintf(stderr, "Error. Cannot open output file %s", argv[argc-1]);
7b242c1c 286 return(EXIT_FAILURE);
e6153040 287 }
8e074056 288 return zlib_compress(infiles, argc-2, outfile);
fb228974 289 }
e6153040 290}
Impressum, Datenschutz