From 413a17a7a6b3c010797c536a2b3ae1cd9d893d1c Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Mon, 26 Aug 2024 02:34:14 +0800 Subject: [PATCH] feature: Make the FPGA bitstreams working with SKIP_* define. Now, you can enable at least two of your favorite technologies (such as LF and HF 14443A) attached a standalone mode and still have spare ROM space for other functionalities on a Proxmark3 Easy with a 256KiB ROM. --- armsrc/appmain.c | 2 +- armsrc/fpgaloader.c | 20 +++++++++++--- common_arm/Makefile.hal | 41 ++++++++++++++++++++++++----- common_fpga/fpga.h | 8 +++++- tools/fpga_compress/fpga_compress.c | 17 ++++++++++-- 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 99b719a6f..fcd667c52 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -306,7 +306,7 @@ static void SendVersion(void) { strncat(VersionString, "\n [ "_YELLOW_("FPGA")" ] \n ", sizeof(VersionString) - strlen(VersionString) - 1); for (int i = 0; i < g_fpga_bitstream_num; i++) { - strncat(VersionString, g_fpga_version_information[i], sizeof(VersionString) - strlen(VersionString) - 1); + strncat(VersionString, g_fpga_version_information[i].versionString, sizeof(VersionString) - strlen(VersionString) - 1); if (i < g_fpga_bitstream_num - 1) { strncat(VersionString, "\n ", sizeof(VersionString) - strlen(VersionString) - 1); } diff --git a/armsrc/fpgaloader.c b/armsrc/fpgaloader.c index 7ac52a42a..8bef852d5 100644 --- a/armsrc/fpgaloader.c +++ b/armsrc/fpgaloader.c @@ -221,14 +221,28 @@ static int get_from_fpga_combined_stream(lz4_streamp_t compressed_fpga_stream, u return *fpga_image_ptr++; } +static int bitstream_version_to_index(FPGA_config bitstream_version) { + static int8_t bitstream_index_map[FPGA_BITSTREAM_MAX] = {-1}; + + // Initialize + if (bitstream_index_map[0] == -1){ + for (size_t i = 0; i < g_fpga_bitstream_num; i++) { + FPGA_VERSION_INFORMATION info = g_fpga_version_information[i]; + bitstream_index_map[info.target_config] = i; + } + } + + return bitstream_index_map[bitstream_version]; +} + //---------------------------------------------------------------------------- // Undo the interleaving of several FPGA config files. FPGA config files // 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, lz4_streamp_t 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 + int bitstream_index = bitstream_version_to_index(bitstream_version); + while ((uncompressed_bytes_cnt / FPGA_INTERLEAVE_SIZE) % g_fpga_bitstream_num != bitstream_index) { get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer); } @@ -603,7 +617,7 @@ void SetAdcMuxFor(uint32_t whichGpio) { void Fpga_print_status(void) { DbpString(_CYAN_("Current FPGA image")); - Dbprintf(" mode....................%s", g_fpga_version_information[downloaded_bitstream - 1]); + Dbprintf(" mode.................... %s", g_fpga_version_information[downloaded_bitstream - 1]); } int FpgaGetCurrent(void) { diff --git a/common_arm/Makefile.hal b/common_arm/Makefile.hal index 179dd4b15..a5ea1e274 100644 --- a/common_arm/Makefile.hal +++ b/common_arm/Makefile.hal @@ -97,31 +97,58 @@ PLTNAME = Unknown Platform PLATFORM_FPGA = fpga-undefined ifeq ($(PLATFORM),PM3RDV4) - # FPGA bitstream files, the order matters! - FPGA_BITSTREAMS = fpga_pm3_lf.bit fpga_pm3_hf.bit fpga_pm3_felica.bit fpga_pm3_hf_15.bit + # FPGA bitstream files, the order doesn't matter anymore + FPGA_BITSTREAMS = fpga_pm3_hf.bit + ifneq ($(SKIP_LF),1) + FPGA_BITSTREAMS += fpga_pm3_lf.bit + endif + ifneq ($(SKIP_FELICA),1) + FPGA_BITSTREAMS += fpga_pm3_felica.bit + endif + ifneq ($(SKIP_ISO15693),1) + FPGA_BITSTREAMS += fpga_pm3_hf_15.bit + endif PLATFORM_DEFS = -DWITH_SMARTCARD -DWITH_FLASH -DRDV4 PLTNAME = Proxmark3 RDV4 PLATFORM_FPGA = xc2s30 RDV4 = yes else ifeq ($(PLATFORM),PM3OTHER) $(warning PLATFORM=PM3OTHER is deprecated, please use PLATFORM=PM3GENERIC) - # FPGA bitstream files, the order matters! - FPGA_BITSTREAMS = fpga_pm3_lf.bit fpga_pm3_hf.bit fpga_pm3_felica.bit fpga_pm3_hf_15.bit + # FPGA bitstream files, the order doesn't matter anymore + FPGA_BITSTREAMS = fpga_pm3_hf.bit + ifneq ($(SKIP_LF),1) + FPGA_BITSTREAMS += fpga_pm3_lf.bit + endif + ifneq ($(SKIP_FELICA),1) + FPGA_BITSTREAMS += fpga_pm3_felica.bit + endif + ifneq ($(SKIP_ISO15693),1) + FPGA_BITSTREAMS += fpga_pm3_hf_15.bit + endif PLTNAME = Proxmark3 generic target PLATFORM_FPGA = xc2s30 ifeq ($(LED_ORDER),PM3EASY) PLATFORM_DEFS = -DLED_ORDER_PM3EASY endif else ifeq ($(PLATFORM),PM3GENERIC) - # FPGA bitstream files, the order matters! - FPGA_BITSTREAMS = fpga_pm3_lf.bit fpga_pm3_hf.bit fpga_pm3_felica.bit fpga_pm3_hf_15.bit + # FPGA bitstream files, the order doesn't matter anymore + FPGA_BITSTREAMS = fpga_pm3_hf.bit + ifneq ($(SKIP_LF),1) + FPGA_BITSTREAMS += fpga_pm3_lf.bit + endif + ifneq ($(SKIP_FELICA),1) + FPGA_BITSTREAMS += fpga_pm3_felica.bit + endif + ifneq ($(SKIP_ISO15693),1) + FPGA_BITSTREAMS += fpga_pm3_hf_15.bit + endif PLTNAME = Proxmark3 generic target PLATFORM_FPGA = xc2s30 ifeq ($(LED_ORDER),PM3EASY) PLATFORM_DEFS = -DLED_ORDER_PM3EASY endif else ifeq ($(PLATFORM),PM3ICOPYX) - # FPGA bitstream files, the order matters - only hf has a bitstream, the other 3 files are 0 bytes + # FPGA bitstream files, the order doesn't matter anymore - only hf has a bitstream, the other 3 files are 0 bytes FPGA_BITSTREAMS = fpga_icopyx_lf.bit fpga_icopyx_hf.bit fpga_icopyx_felica.bit fpga_icopyx_hf_15.bit PLATFORM_DEFS = -DWITH_FLASH -DICOPYX -DXC3 PLTNAME = iCopy-X with XC3S100E diff --git a/common_fpga/fpga.h b/common_fpga/fpga.h index 2818af369..8248896da 100644 --- a/common_fpga/fpga.h +++ b/common_fpga/fpga.h @@ -43,8 +43,14 @@ typedef enum FPGA_BITSTREAM_MAX = FPGA_BITSTREAM_HF_15, } FPGA_config; +typedef struct +{ + const char *const versionString; + const FPGA_config target_config; +} FPGA_VERSION_INFORMATION; + static const uint8_t bitparse_fixed_header[] = {0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01}; extern const int g_fpga_bitstream_num; -extern const char *const g_fpga_version_information[]; +extern const FPGA_VERSION_INFORMATION g_fpga_version_information[]; #endif diff --git a/tools/fpga_compress/fpga_compress.c b/tools/fpga_compress/fpga_compress.c index ab1d93243..97ba90024 100644 --- a/tools/fpga_compress/fpga_compress.c +++ b/tools/fpga_compress/fpga_compress.c @@ -420,8 +420,9 @@ static void print_version_info_preamble(FILE *outfile, int num_infiles) { fprintf(outfile, "// This file is generated by fpga_compress. Don't edit!\n"); fprintf(outfile, "//-----------------------------------------------------------------------------\n"); fprintf(outfile, "\n\n"); + fprintf(outfile, "#include \"fpga.h\"\n\n"); fprintf(outfile, "const int g_fpga_bitstream_num = %d;\n", num_infiles); - fprintf(outfile, "const char *const g_fpga_version_information[%d] = {\n", num_infiles); + fprintf(outfile, "const FPGA_VERSION_INFORMATION g_fpga_version_information[%d] = {\n", num_infiles); } static int generate_fpga_version_info(FILE *infile[], char *infile_names[], int num_infiles, FILE *outfile) { @@ -432,7 +433,19 @@ static int generate_fpga_version_info(FILE *infile[], char *infile_names[], int for (int i = 0; i < num_infiles; i++) { FpgaGatherVersion(infile[i], infile_names[i], version_string, sizeof(version_string)); - fprintf(outfile, " \" %s\"", version_string); + fprintf(outfile, " { \"%s\"", version_string); + + if (!memcmp("fpga_pm3_lf.ncd", version_string, sizeof("fpga_pm3_lf.ncd") - 1)) + fprintf(outfile, ", FPGA_BITSTREAM_LF }"); + else if (!memcmp("fpga_pm3_hf_15.ncd", version_string, sizeof("fpga_pm3_hf_15.ncd") - 1)) + fprintf(outfile, ", FPGA_BITSTREAM_HF_15 }"); + else if (!memcmp("fpga_pm3_hf.ncd", version_string, sizeof("fpga_pm3_hf.ncd") - 1)) + fprintf(outfile, ", FPGA_BITSTREAM_HF }"); + else if (!memcmp("fpga_pm3_felica.ncd", version_string, sizeof("fpga_pm3_felica.ncd") - 1)) + fprintf(outfile, ", FPGA_BITSTREAM_HF_FELICA }"); + else + fprintf(outfile, ", FPGA_BITSTREAM_UNKNOWN }"); + if (i != num_infiles - 1) { fprintf(outfile, ","); }