mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 10:37:23 -07:00
add lz4 to arm side
This commit is contained in:
parent
cb03286420
commit
1a8eff5d42
5 changed files with 78 additions and 79 deletions
|
@ -17,10 +17,19 @@
|
|||
#include "ticks.h"
|
||||
#include "dbprint.h"
|
||||
#include "util.h"
|
||||
#include "zlib.h"
|
||||
#include "fpga.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "lz4.h" // uncompress
|
||||
|
||||
typedef struct lz4_stream_s {
|
||||
LZ4_streamDecode_t* lz4StreamDecode;
|
||||
char * next_in;
|
||||
int avail_in;
|
||||
} lz4_stream;
|
||||
|
||||
typedef lz4_stream* lz4_streamp;
|
||||
|
||||
// remember which version of the bitstream we have already downloaded to the FPGA
|
||||
static int downloaded_bitstream = 0;
|
||||
|
||||
|
@ -30,8 +39,6 @@ extern uint8_t _binary_obj_fpga_all_bit_z_start, _binary_obj_fpga_all_bit_z_end;
|
|||
static uint8_t *fpga_image_ptr = NULL;
|
||||
static uint32_t uncompressed_bytes_cnt;
|
||||
|
||||
#define OUTPUT_BUFFER_LEN 80
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Set up the Serial Peripheral Interface as master
|
||||
// Used to write the FPGA config word
|
||||
|
@ -180,18 +187,25 @@ bool FpgaSetupSscDma(uint8_t *buf, int len) {
|
|||
// Uncompress (inflate) the FPGA data. Returns one decompressed byte with
|
||||
// each call.
|
||||
//----------------------------------------------------------------------------
|
||||
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
|
||||
compressed_fpga_stream->next_out = output_buffer;
|
||||
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
|
||||
static int get_from_fpga_combined_stream(lz4_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
if (fpga_image_ptr == output_buffer + FPGA_RING_BUFFER_BYTES) { // need more data
|
||||
fpga_image_ptr = output_buffer;
|
||||
int res = inflate(compressed_fpga_stream, Z_SYNC_FLUSH);
|
||||
|
||||
if (res != Z_OK)
|
||||
Dbprintf("inflate returned: %d, %s", res, compressed_fpga_stream->msg);
|
||||
|
||||
if (res < 0)
|
||||
int cmp_bytes = *(compressed_fpga_stream->next_in + 3);
|
||||
cmp_bytes = (cmp_bytes << 8) + *(compressed_fpga_stream->next_in+2);
|
||||
cmp_bytes = (cmp_bytes << 8) + *(compressed_fpga_stream->next_in+1);
|
||||
cmp_bytes = (cmp_bytes << 8) + *(compressed_fpga_stream->next_in+0);
|
||||
compressed_fpga_stream->next_in += 4;
|
||||
compressed_fpga_stream->avail_in -= cmp_bytes + 4;
|
||||
int res = LZ4_decompress_safe_continue(compressed_fpga_stream->lz4StreamDecode,
|
||||
compressed_fpga_stream->next_in,
|
||||
(char*)output_buffer,
|
||||
cmp_bytes,
|
||||
FPGA_RING_BUFFER_BYTES);
|
||||
if (res <= 0) {
|
||||
Dbprintf("inflate returned: %d", res);
|
||||
return res;
|
||||
}
|
||||
compressed_fpga_stream->next_in += cmp_bytes;
|
||||
}
|
||||
uncompressed_bytes_cnt++;
|
||||
return *fpga_image_ptr++;
|
||||
|
@ -202,7 +216,7 @@ static int get_from_fpga_combined_stream(z_streamp compressed_fpga_stream, uint8
|
|||
// are combined into one big file:
|
||||
// 288 bytes from FPGA file 1, followed by 288 bytes from FGPA file 2, etc.
|
||||
//----------------------------------------------------------------------------
|
||||
static int get_from_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
static int get_from_fpga_stream(int bitstream_version, lz4_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
while ((uncompressed_bytes_cnt / FPGA_INTERLEAVE_SIZE) % g_fpga_bitstream_num != (bitstream_version - 1)) {
|
||||
// skip undesired data belonging to other bitstream_versions
|
||||
get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
|
||||
|
@ -211,37 +225,23 @@ static int get_from_fpga_stream(int bitstream_version, z_streamp compressed_fpga
|
|||
return get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
|
||||
}
|
||||
|
||||
static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size) {
|
||||
return BigBuf_malloc(items * size);
|
||||
}
|
||||
|
||||
// free eventually allocated BigBuf memory
|
||||
static void fpga_inflate_free(voidpf opaque, voidpf address) {
|
||||
BigBuf_free();
|
||||
BigBuf_Clear_ext(false);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Initialize decompression of the respective (HF or LF) FPGA stream
|
||||
//----------------------------------------------------------------------------
|
||||
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, lz4_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
uint8_t header[FPGA_BITSTREAM_FIXED_HEADER_SIZE];
|
||||
|
||||
uncompressed_bytes_cnt = 0;
|
||||
|
||||
// initialize z_stream structure for inflate:
|
||||
compressed_fpga_stream->next_in = &_binary_obj_fpga_all_bit_z_start;
|
||||
compressed_fpga_stream->next_in = (char*)&_binary_obj_fpga_all_bit_z_start;
|
||||
compressed_fpga_stream->avail_in = &_binary_obj_fpga_all_bit_z_end - &_binary_obj_fpga_all_bit_z_start;
|
||||
compressed_fpga_stream->next_out = output_buffer;
|
||||
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
|
||||
compressed_fpga_stream->zalloc = &fpga_inflate_malloc;
|
||||
compressed_fpga_stream->zfree = &fpga_inflate_free;
|
||||
|
||||
int res = inflateInit2(compressed_fpga_stream, 0);
|
||||
if (res < 0)
|
||||
|
||||
int res = LZ4_setStreamDecode(compressed_fpga_stream->lz4StreamDecode, NULL, 0);
|
||||
if (res == 0)
|
||||
return false;
|
||||
|
||||
fpga_image_ptr = output_buffer;
|
||||
fpga_image_ptr = output_buffer + FPGA_RING_BUFFER_BYTES;
|
||||
|
||||
for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++)
|
||||
header[i] = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
|
||||
|
@ -266,7 +266,7 @@ static void DownloadFPGA_byte(uint8_t w) {
|
|||
}
|
||||
|
||||
// Download the fpga image starting at current stream position with length FpgaImageLen bytes
|
||||
static void DownloadFPGA(int bitstream_version, int FpgaImageLen, z_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
static void DownloadFPGA(int bitstream_version, int FpgaImageLen, lz4_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
int i = 0;
|
||||
|
||||
AT91C_BASE_PIOA->PIO_OER = GPIO_FPGA_ON;
|
||||
|
@ -348,7 +348,7 @@ static void DownloadFPGA(int bitstream_version, int FpgaImageLen, z_streamp comp
|
|||
* (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
|
||||
* length.
|
||||
*/
|
||||
static int bitparse_find_section(int bitstream_version, char section_name, uint32_t *section_length, z_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
static int bitparse_find_section(int bitstream_version, char section_name, uint32_t *section_length, lz4_streamp compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
int result = 0;
|
||||
#define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section
|
||||
uint16_t numbytes = 0;
|
||||
|
@ -407,8 +407,6 @@ void FpgaDownloadAndGo(int bitstream_version) {
|
|||
|
||||
// Send waiting time extension request as this will take a while
|
||||
send_wtx(1500);
|
||||
z_stream compressed_fpga_stream;
|
||||
uint8_t output_buffer[OUTPUT_BUFFER_LEN] = {0x00};
|
||||
|
||||
bool verbose = (DBGLEVEL > 3);
|
||||
|
||||
|
@ -416,6 +414,11 @@ void FpgaDownloadAndGo(int bitstream_version) {
|
|||
BigBuf_free();
|
||||
BigBuf_Clear_ext(verbose);
|
||||
|
||||
lz4_stream compressed_fpga_stream;
|
||||
LZ4_streamDecode_t lz4StreamDecode_body = {{ 0 }};
|
||||
compressed_fpga_stream.lz4StreamDecode = &lz4StreamDecode_body;
|
||||
uint8_t * output_buffer = BigBuf_malloc(FPGA_RING_BUFFER_BYTES);
|
||||
|
||||
if (!reset_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer))
|
||||
return;
|
||||
|
||||
|
@ -425,8 +428,6 @@ void FpgaDownloadAndGo(int bitstream_version) {
|
|||
downloaded_bitstream = bitstream_version;
|
||||
}
|
||||
|
||||
inflateEnd(&compressed_fpga_stream);
|
||||
|
||||
// turn off antenna
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue