From 0710a37429bb6b2c231ba03634b2558c3d8ef190 Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Fri, 30 Aug 2024 16:04:07 +0800 Subject: [PATCH] fix: bitstream_index_map overflow --- armsrc/appmain.c | 2 +- armsrc/fpgaloader.c | 6 +++--- client/src/cmdhw.c | 2 +- common_fpga/fpga.h | 2 ++ 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index a2b1e9f10..e9b37be78 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -797,7 +797,7 @@ static void PacketReceived(PacketCommandNG *packet) { } case CMD_SET_FPGAMODE: { uint8_t mode = packet->data.asBytes[0]; - if (mode >= FPGA_BITSTREAM_LF && mode <= FPGA_BITSTREAM_HF_15) { + if (mode >= FPGA_BITSTREAM_MIN && mode <= FPGA_BITSTREAM_MAX) { FpgaDownloadAndGo(mode); reply_ng(CMD_SET_FPGAMODE, PM3_SUCCESS, NULL, 0); } diff --git a/armsrc/fpgaloader.c b/armsrc/fpgaloader.c index 75bff89ad..bf7ad765b 100644 --- a/armsrc/fpgaloader.c +++ b/armsrc/fpgaloader.c @@ -222,11 +222,11 @@ static int get_from_fpga_combined_stream(lz4_streamp_t compressed_fpga_stream, u } static int bitstream_target_to_index(FPGA_config bitstream_target) { - static int8_t bitstream_index_map[FPGA_BITSTREAM_MAX] = {-1}; + static int8_t bitstream_index_map[FPGA_CONFIG_COUNT] = {-1}; // Initialize - if (bitstream_index_map[0] == -1) { - bitstream_index_map[0] = 0; + if (bitstream_index_map[FPGA_BITSTREAM_UNKNOWN] == -1) { + bitstream_index_map[FPGA_BITSTREAM_UNKNOWN] = 0; for (size_t i = 0; i < g_fpga_bitstream_num; i++) { FPGA_VERSION_INFORMATION info = g_fpga_version_information[i]; diff --git a/client/src/cmdhw.c b/client/src/cmdhw.c index 6b4118d02..c1007a86a 100644 --- a/client/src/cmdhw.c +++ b/client/src/cmdhw.c @@ -1432,7 +1432,7 @@ static int CmdBootloader(const char *Cmd) { } int set_fpga_mode(uint8_t mode) { - if (mode < FPGA_BITSTREAM_LF || mode > FPGA_BITSTREAM_HF_15) { + if (mode < FPGA_BITSTREAM_MIN || mode > FPGA_BITSTREAM_MAX) { return PM3_EINVARG; } uint8_t d[] = {mode}; diff --git a/common_fpga/fpga.h b/common_fpga/fpga.h index 07746893c..c383d2d44 100644 --- a/common_fpga/fpga.h +++ b/common_fpga/fpga.h @@ -36,10 +36,12 @@ typedef enum { FPGA_BITSTREAM_UNKNOWN = 0, FPGA_BITSTREAM_LF = 1, + FPGA_BITSTREAM_MIN = FPGA_BITSTREAM_LF, FPGA_BITSTREAM_HF, FPGA_BITSTREAM_HF_FELICA, FPGA_BITSTREAM_HF_15, FPGA_BITSTREAM_MAX = FPGA_BITSTREAM_HF_15, + FPGA_CONFIG_COUNT } FPGA_config; typedef struct {