From f0b569b507b716c8af0bf43913e62eb371824466 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 13 Sep 2026 08:27:53 +0200 Subject: [PATCH] fpga_compress: pad a single bitstream to the interleave boundary zlib_decompress() walks its output in whole FPGA_INTERLEAVE_SIZE chunks: for (long k = 0; k < *outsize / (FPGA_INTERLEAVE_SIZE * num_outfiles); k++) so a stream that is not a whole number of chunks loses its trailing partial one. With two or more inputs the read loop zero-pads each stream past EOF and the total lands on a boundary, but the padding was guarded by 'num_infiles > 1', so the single input case was left ragged. 42172 bytes of fpga_pm3_hf.bit is 146.43 chunks, and -d handed back 39788 - a clean looking prefix, short by 2384 bytes, 22 of them real bitstream data. Gate the padding on single_block instead. It must not be 'always pad': -s is the .data section, and start.c's uncompress_data_section() sizes the decompression with __data_end__ - __data_start__. Rounding .data from 14944 up to 14976 makes LZ4_decompress_safe() return an error, and that path is the LED panic loop, so the firmware would never reach AppMain(). 1 bitstream 42172 in -> 42336 packed, -d round trip byte identical, archive 28730 -> 28731 4 bitstreams archive byte identical to 43fe6c3eb, round trip exact -s .data byte identical to 43fe6c3eb on the same input, unpadded The 164 padding bytes never reach the FPGA: DownloadFPGA() shifts out only bitstream_length bytes, taken from the .bit 'e' section header. Also simulated the ARM decoder over the new archive - LZ4_decompress_safe_continue() block by block into a FPGA_RING_BUFFER_BYTES buffer - 3 blocks of 16384/16384/9568, none over the ring buffer. Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 1 + tools/fpga_compress/fpga_compress.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 887fb759e..cb7018c4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed `fpga_compress -d` - a single bitstream archive was unpacked 2384 bytes short, the trailing partial interleave chunk was dropped (@iceman1001) - Fixed `fpga_compress` - a build with only one FPGA bitstream (`SKIP_LF`, `SKIP_FELICA`, `SKIP_ISO15693`) packed it as a single 1 MB block the ARM could not decompress, `inflate returned: -13247` (@iceman1001) - Fixed `FpgaDownloadAndGo` - a failed FPGA decompress leaked the ring buffer, leaving BigBuf 16 kB short for the rest of the session (@iceman1001) - Added `hf mf dump --sk` - save extracted keys to binary file (@iceman1001) diff --git a/tools/fpga_compress/fpga_compress.c b/tools/fpga_compress/fpga_compress.c index a484bd66c..29e5481ec 100644 --- a/tools/fpga_compress/fpga_compress.c +++ b/tools/fpga_compress/fpga_compress.c @@ -71,13 +71,23 @@ static int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile, boo return (EXIT_FAILURE); } + // Pad a bitstream that ran out mid chunk with zeroes, so total_size stays a + // whole number of FPGA_INTERLEAVE_SIZE chunks. zlib_decompress() walks the + // output in whole chunks and drops a trailing partial one, so an unpadded + // stream comes back short - 42172 bytes of fpga_pm3_hf.bit returned as 39788. + // + // Not for "-s". That is the .data section, and start.c sizes the + // decompression with __data_end__ - __data_start__. Rounding .data up to a + // chunk boundary makes LZ4_decompress_safe() overrun that and return an + // error, which drops the firmware into the LED panic loop in + // uncompress_data_section(). for (uint16_t j = 0; j < num_infiles; j++) { for (uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) { uint8_t c = (uint8_t)fgetc(infile[j]); if (!feof(infile[j])) { fpga_config[total_size++] = c; - } else if (num_infiles > 1) { + } else if (single_block == false) { fpga_config[total_size++] = '\0'; } }