From b69d6e0cae7b07e9ae2d6681146caf27ca368513 Mon Sep 17 00:00:00 2001 From: Alexey Starikovskiy Date: Fri, 1 Sep 2023 16:35:44 +0300 Subject: [PATCH] Fixes for PVS errors --- src/altera.cpp | 2 +- src/anlogicCable.cpp | 1 - src/board.hpp | 4 ++-- src/ch347jtag.cpp | 2 +- src/ch347jtag.hpp | 1 - src/cmsisDAP.cpp | 2 +- src/colognechip.cpp | 34 +++++++++++++--------------- src/configBitstreamParser.cpp | 2 +- src/dfu.cpp | 4 ++-- src/gowin.cpp | 42 +++++++++++++++++------------------ src/ice40.cpp | 2 +- src/jedParser.cpp | 4 ++-- src/jtag.cpp | 4 +++- src/lattice.cpp | 27 ++++++++++------------ src/part.hpp | 6 ++--- src/svf_jtag.cpp | 4 ++++ src/usbBlaster.cpp | 2 +- src/xilinx.cpp | 20 ++++++++--------- src/xvc_server.hpp | 4 ++-- 19 files changed, 82 insertions(+), 85 deletions(-) diff --git a/src/altera.cpp b/src/altera.cpp index a94bc4c2ed..3458779e43 100644 --- a/src/altera.cpp +++ b/src/altera.cpp @@ -54,7 +54,7 @@ Altera::Altera(Jtag *jtag, const std::string &filename, printError("\tplease use rbf or svf file"); printError("\tor use --write-flash with: ", false); printError("-b board_name or --fpga_part xxxx"); - std::runtime_error("Error: wrong file"); + throw std::runtime_error("Error: wrong file"); } else { _mode = Device::SPI_MODE; } diff --git a/src/anlogicCable.cpp b/src/anlogicCable.cpp index f54fd92997..e37840fd38 100644 --- a/src/anlogicCable.cpp +++ b/src/anlogicCable.cpp @@ -93,7 +93,6 @@ int AnlogicCable::setClkFreq(uint32_t clkHZ) if (clkHZ > 6000000) { printWarn("Anlogic JTAG probe limited to 6MHz"); - clkHZ = 6000000; } if (clkHZ >= 6000000) { diff --git a/src/board.hpp b/src/board.hpp index 77f4af2dba..7e4d640714 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -160,9 +160,9 @@ static std::map board_list = { DFU_BOARD("icebreaker-bitsy", "", "dfu", 0x1d50, 0x6146, 0), JTAG_BOARD("kc705", "", "digilent", 0, 0, CABLE_DEFAULT), JTAG_BOARD("LD-SCHOKO", "LFE5U-45F-6CABGA256", "", 0, 0, CABLE_MHZ(6)), - DFU_BOARD("LD-SCHOKO", "", "dfu", 0x16d0, 0x116d, 0), + DFU_BOARD("LD-SCHOKO-DFU", "", "dfu", 0x16d0, 0x116d, 0), JTAG_BOARD("LD-KONFEKT", "LFE5U-12F-6BG256C", "", 0, 0, CABLE_MHZ(6)), - DFU_BOARD("LD-KONFEKT", "", "dfu", 0x16d0, 0x116d, 0), + DFU_BOARD("LD-KONFEKT-DFU", "", "dfu", 0x16d0, 0x116d, 0), JTAG_BOARD("licheeTang", "", "anlogicCable", 0, 0, CABLE_DEFAULT), /* left for backward compatibility, use tec0117 instead */ JTAG_BOARD("littleBee", "", "ft2232", 0, 0, CABLE_DEFAULT), diff --git a/src/ch347jtag.cpp b/src/ch347jtag.cpp index 7df3875513..bf0510f325 100644 --- a/src/ch347jtag.cpp +++ b/src/ch347jtag.cpp @@ -73,7 +73,7 @@ int CH347Jtag::usb_xfer(unsigned wlen, unsigned rlen, unsigned *ract, bool defer return 0; } - wcomplete = 0; + int wcomplete = 0, rcomplete = 0; libusb_fill_bulk_transfer(wtrans, dev_handle, CH347JTAG_WRITE_EP, obuf, wlen, sync_cb, &wcomplete, CH347JTAG_TIMEOUT); int r = libusb_submit_transfer(wtrans); diff --git a/src/ch347jtag.hpp b/src/ch347jtag.hpp index 044894e413..7c2c492ea4 100644 --- a/src/ch347jtag.hpp +++ b/src/ch347jtag.hpp @@ -35,7 +35,6 @@ class CH347Jtag : public JtagInterface { libusb_device_handle *dev_handle; libusb_context *usb_ctx; struct libusb_transfer *wtrans, *rtrans; - int rcomplete, wcomplete; uint8_t ibuf[512]; uint8_t _obuf[512]; uint8_t *obuf; diff --git a/src/cmsisDAP.cpp b/src/cmsisDAP.cpp index 9aecdd983c..490da947fa 100644 --- a/src/cmsisDAP.cpp +++ b/src/cmsisDAP.cpp @@ -95,7 +95,7 @@ CmsisDAP::CmsisDAP(const cable_t &cable, int index, int8_t verbose):_verbose(ver std::vector dev_found; _ll_buffer = (unsigned char *)malloc(sizeof(unsigned char) * 65); if (!_ll_buffer) - std::runtime_error("internal buffer allocation failed"); + throw std::runtime_error("internal buffer allocation failed"); _buffer = _ll_buffer+2; /* only hid support */ diff --git a/src/colognechip.cpp b/src/colognechip.cpp index b066b3c70a..b99fe8e3ea 100644 --- a/src/colognechip.cpp +++ b/src/colognechip.cpp @@ -6,6 +6,8 @@ #include "colognechip.hpp" +#include + #define JTAG_CONFIGURE 0x06 #define JTAG_SPI_BYPASS 0x05 #define SLEEP_US 500 @@ -125,7 +127,7 @@ bool CologneChip::dumpFlash(uint32_t base_addr, uint32_t len) if (_spi) { /* enable output and hold reset */ _spi->gpio_clear(_rstn_pin | _oen_pin); - } else { + } else if (_ftdi_jtag) { /* enable output and disable reset */ _ftdi_jtag->gpio_clear(_oen_pin); _ftdi_jtag->gpio_set(_rstn_pin); @@ -134,13 +136,9 @@ bool CologneChip::dumpFlash(uint32_t base_addr, uint32_t len) /* prepare SPI access */ printInfo("Read Flash ", false); try { - SPIFlash *flash; - if (_spi) { - flash = new SPIFlash(reinterpret_cast(_spi), false, - _verbose); - } else { - flash = new SPIFlash(this, false, _verbose); - } + std::unique_ptr flash(_spi ? + new SPIFlash(reinterpret_cast(_spi), false, _verbose): + new SPIFlash(this, false, _verbose)); flash->reset(); flash->power_up(); flash->dump(_filename, base_addr, len); @@ -172,14 +170,14 @@ void CologneChip::program(unsigned int offset, bool unprotect_flash) if (_mode == Device::NONE_MODE || _mode == Device::READ_MODE) return; - ConfigBitstreamParser *cfg; + std::unique_ptr cfg; if (_file_extension == "cfg") { - cfg = new CologneChipCfgParser(_filename); + cfg.reset(new CologneChipCfgParser(_filename)); } else if (_file_extension == "bit") { - cfg = new RawParser(_filename, false); + cfg.reset(new RawParser(_filename, false)); } else { /* unknown type: */ if (_mode == Device::FLASH_MODE) { - cfg = new RawParser(_filename, false); + cfg.reset(new RawParser(_filename, false)); } else { throw std::runtime_error("incompatible file format"); } @@ -192,18 +190,16 @@ void CologneChip::program(unsigned int offset, bool unprotect_flash) switch (_mode) { case Device::FLASH_MODE: - if (_jtag != NULL) { + if (_jtag != NULL) programJTAG_flash(offset, data, length, unprotect_flash); - } else if (_jtag == NULL) { + else programSPI_flash(offset, data, length, unprotect_flash); - } break; case Device::MEM_MODE: - if (_jtag != NULL) { + if (_jtag != NULL) programJTAG_sram(data, length); - } else if (_jtag == NULL) { + else programSPI_sram(data, length); - } break; default: /* avoid warning */ break; @@ -389,7 +385,7 @@ int CologneChip::spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond, uint32_t timeout, bool verbose) { uint8_t rx[2]; - uint8_t dummy[2]; + uint8_t dummy[2] = {0xff}; uint8_t tmp; uint8_t tx = ConfigBitstreamParser::reverseByte(cmd); uint32_t count = 0; diff --git a/src/configBitstreamParser.cpp b/src/configBitstreamParser.cpp index 6717eb711e..f5f854db43 100644 --- a/src/configBitstreamParser.cpp +++ b/src/configBitstreamParser.cpp @@ -216,6 +216,6 @@ bool ConfigBitstreamParser::decompress_bitstream(string source, string *dest) /* clean up and return */ (void)inflateEnd(&strm); - return ret == Z_STREAM_END; + return true; #endif } diff --git a/src/dfu.cpp b/src/dfu.cpp index 68ca792d35..d94e410be7 100644 --- a/src/dfu.cpp +++ b/src/dfu.cpp @@ -561,7 +561,7 @@ int DFU::dfu_detach() int DFU::get_status(struct dfu_status *status) { - uint8_t buffer[6]; + uint8_t buffer[6] = {0}; int res; res = send(false, DFU_GETSTATUS, 0, buffer, 6); @@ -586,7 +586,7 @@ int DFU::get_status(struct dfu_status *status) */ char DFU::get_state() { - char c; + char c = 0; int res = send(false, DFU_GETSTATE, 0, (unsigned char *)&c, 1); diff --git a/src/gowin.cpp b/src/gowin.cpp index a3935286a9..865c9b340d 100644 --- a/src/gowin.cpp +++ b/src/gowin.cpp @@ -679,23 +679,23 @@ int Gowin::spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len) _jtag->flush(); /* send bit/bit full tx content (or set di to 0 when NULL) */ - for (uint32_t i = 0; i < len * 8; i++) { - uint8_t r; - t = _spi_msk | _spi_do; - if (tx != NULL && tx[i>>3] & (1 << (7-(i&0x07)))) - t |= _spi_di; - _jtag->shiftDR(&t, NULL, 8); - _jtag->toggleClk(6); - t |= _spi_sck; - _jtag->shiftDR(&t, (rx) ? &r : NULL, 8); - _jtag->toggleClk(6); - _jtag->flush(); - /* if read reconstruct bytes */ - if (rx) { - if (r & _spi_do) - rx[i >> 3] |= 1 << (7-(i & 0x07)); - else - rx[i >> 3] &= ~(1 << (7-(i & 0x07))); + for (unsigned l = 0; l < len; ++l) { + if (rx) + rx[l] = 0; + for (uint8_t b = 0, bm = 0x80; b < 8; ++b, bm >>= 1) { + uint8_t r; + t = _spi_msk | _spi_do; + if (tx != NULL && tx[l] & bm) + t |= _spi_di; + _jtag->shiftDR(&t, NULL, 8); + _jtag->toggleClk(6); + t |= _spi_sck; + _jtag->shiftDR(&t, (rx) ? &r : NULL, 8); + _jtag->toggleClk(6); + _jtag->flush(); + /* if read reconstruct bytes */ + if (rx && (r & _spi_do)) + rx[l] |= bm; } } /* set CS and unset SCK (next xfer) */ @@ -745,9 +745,9 @@ int Gowin::spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond, _jtag->toggleClk(6); /* send command bit/bit */ - for (int i = 0; i < 8; i++) { + for (uint8_t i = 0, bm = 0x80; i < 8; ++i, bm >>= 1) { t = _spi_msk | _spi_do; - if ((cmd & (1 << (7-i))) != 0) + if ((cmd & bm) != 0) t |= _spi_di; _jtag->shiftDR(&t, NULL, 8); _jtag->toggleClk(6); @@ -761,7 +761,7 @@ int Gowin::spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond, do { tmp = 0; /* read status register bit/bit with di == 0 */ - for (int i = 0; i < 8; i++) { + for (uint8_t i = 0, bm = 0x80; i < 8; ++i, bm >>= 1) { uint8_t r; t &= ~_spi_sck; _jtag->shiftDR(&t, NULL, 8); @@ -771,7 +771,7 @@ int Gowin::spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond, _jtag->toggleClk(6); _jtag->flush(); if ((r & _spi_do) != 0) - tmp |= 1 << (7-i); + tmp |= bm; } count++; diff --git a/src/ice40.cpp b/src/ice40.cpp index f482f0f298..dbd8ccbb49 100644 --- a/src/ice40.cpp +++ b/src/ice40.cpp @@ -89,7 +89,7 @@ bool Ice40::program_cram(const uint8_t *data, uint32_t length) progress.done(); /* send 48 to 100 dummy bits */ - uint8_t dummy[12]; + uint8_t dummy[12] = {0xff}; _spi->spi_put(dummy, NULL, 12); /* wait CDONE */ diff --git a/src/jedParser.cpp b/src/jedParser.cpp index 01ace0e0b9..76605de958 100644 --- a/src/jedParser.cpp +++ b/src/jedParser.cpp @@ -187,8 +187,8 @@ void JedParser::parseEField(const vector &content) { _featuresRow = 0; string featuresRow = content[0].substr(1); - for (size_t i = 0; i < featuresRow.size(); i++) - _featuresRow |= ((featuresRow[i] - '0') << i); + for (size_t i = 0; i < featuresRow.size(); ++i) + _featuresRow |= (uint64_t(featuresRow[i] - '0') << i); string feabits = content[1]; _feabits = 0; for (size_t i = 0; i < feabits.size(); i++) { diff --git a/src/jtag.cpp b/src/jtag.cpp index 4432a8c029..14baaf9249 100644 --- a/src/jtag.cpp +++ b/src/jtag.cpp @@ -147,6 +147,8 @@ Jtag::Jtag(const cable_t &cable, const jtag_pins_conf_t *pin_conf, } _tms_buffer = (unsigned char *)malloc(sizeof(unsigned char) * _tms_buffer_size); + if (_tms_buffer == nullptr) + throw std::runtime_error("Error: memory allocation failed"); memset(_tms_buffer, 0, _tms_buffer_size); detectChain(5); @@ -618,7 +620,7 @@ void Jtag::set_state(tapState_t newState) _state = RUN_TEST_IDLE; } else { tms = 1; - _state = SELECT_DR_SCAN; + _state = SELECT_IR_SCAN; } break; case UNKNOWN:; diff --git a/src/lattice.cpp b/src/lattice.cpp index 739e785cfc..6095961d11 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -765,11 +765,10 @@ bool Lattice::program_flash(unsigned int offset, bool unprotect_flash) } else if (_file_extension == "pub") { /* clear current SRAM content */ clearSRAM(); - retval = program_pubkey_MachXO3D(); + program_pubkey_MachXO3D(); } else { // machox2 + bit if (_file_extension == "bit" && _fpga_family == MACHXO2_FAMILY) { - retval = true; try { LatticeBitParser _bit(_filename, true, _verbose); _bit.parse(); @@ -1357,23 +1356,24 @@ int Lattice::spi_put(uint8_t cmd, const uint8_t *tx, uint8_t *rx, uint32_t len) int Lattice::spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len) { - int xfer_len = len; - uint8_t jtx[xfer_len]; - uint8_t jrx[xfer_len]; + if (len == 0) + return 0; + uint8_t jtx[len]; + uint8_t jrx[len]; - if (tx) { - for (uint32_t i=0; i < len; i++) - jtx[i] = LatticeBitParser::reverseByte(tx[i]); + for (uint32_t i = 0; i < len; ++i) { + jtx[i] = (tx) ? LatticeBitParser::reverseByte(tx[i]) : 0; + jrx[i] = 0; } /* send first already stored cmd, * in the same time store each byte * to next */ - _jtag->shiftDR(jtx, (rx == NULL)? NULL: jrx, 8*xfer_len); + _jtag->shiftDR(jtx, (rx) ? jrx : nullptr, 8 * len); - if (rx != NULL) { - for (uint32_t i=0; i < len; i++) + if (rx) { + for (uint32_t i = 0; i < len; ++i) rx[i] = LatticeBitParser::reverseByte(jrx[i]); } return 0; @@ -1383,7 +1383,7 @@ int Lattice::spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond, uint32_t timeout, bool verbose) { uint8_t rx; - uint8_t dummy[2]; + uint8_t dummy[2] = {0xff}; uint8_t tmp; uint8_t tx = LatticeBitParser::reverseByte(cmd); uint32_t count = 0; @@ -2045,9 +2045,6 @@ bool Lattice::program_pubkey_MachXO3D() printf("%02x", pubkey[j]); } printf("]\n"); - } - - if (_verbose) { printf("Trailing bytes: ["); for (; i < len; i++) { printf("%02x ", data[i]); diff --git a/src/part.hpp b/src/part.hpp index 059dc67299..c4a264811a 100644 --- a/src/part.hpp +++ b/src/part.hpp @@ -179,7 +179,7 @@ static std::map fpga_list = { /* Lattice MachXO3 */ {0x012BB043, {"lattice", "MachXO3LF", "LCMX03LF-1300C", 8}}, {0x012B2043, {"lattice", "MachXO3LF", "LCMX03LF-1300E", 8}}, - {0x012BB043, {"lattice", "MachXO3LF", "LCMX03LF-2100C", 8}}, + {0x612BB043, {"lattice", "MachXO3LF", "LCMX03LF-2100C", 8}}, {0x012B3043, {"lattice", "MachXO3LF", "LCMX03LF-2100E", 8}}, {0x012BC043, {"lattice", "MachXO3LF", "LCMX03LF-4300C", 8}}, {0x012B4043, {"lattice", "MachXO3LF", "LCMX03LF-4300E", 8}}, @@ -207,8 +207,8 @@ static std::map fpga_list = { {0x010F1043, {"lattice", "CrosslinkNX", "LIFCL-40", 8}}, /* Lattice Certus-NX */ - {0x010F0043, {"lattice", "CertusNX", "LFD2NX-17", 8}}, - {0x010F1043, {"lattice", "CertusNX", "LFD2NX-40", 8}}, + {0x310F0043, {"lattice", "CertusNX", "LFD2NX-17", 8}}, + {0x310F1043, {"lattice", "CertusNX", "LFD2NX-40", 8}}, /**************************************************************************/ /* Gowin */ diff --git a/src/svf_jtag.cpp b/src/svf_jtag.cpp index 6b7ac228fa..3a830b05e6 100644 --- a/src/svf_jtag.cpp +++ b/src/svf_jtag.cpp @@ -142,6 +142,8 @@ void SVF_jtag::parse_XYR(vector const &vstr, svf_XYR &t) } if (write_data != -1) { size_t byte_len = (t.len + 7) / 8; + if (byte_len == 0) + return; unsigned char *write_buffer = parse_hex(t.tdi, byte_len, 0); if (!t.smask.empty()) { unsigned char *smaskbuff = parse_hex(t.smask, byte_len, 0); @@ -171,6 +173,8 @@ void SVF_jtag::parse_XYR(vector const &vstr, svf_XYR &t) cerr << uppercase << hex << int(read_buffer[j]); } cerr << " isn't the one expected: " << uppercase << t.tdo << endl; + delete[] tdobuf; + delete[] maskbuf; throw exception(); } } diff --git a/src/usbBlaster.cpp b/src/usbBlaster.cpp index 1fd0263449..9e2c294d7d 100644 --- a/src/usbBlaster.cpp +++ b/src/usbBlaster.cpp @@ -434,7 +434,7 @@ UsbBlasterII::UsbBlasterII(const string &firmware_path) { std::string fpath; uint8_t buf[5]; - if (firmware_path.empty() && BLASTERII_DIR == "") { + if (firmware_path.empty() && strlen(BLASTERII_DIR) == 0) { printError("missing FX2 firmware"); printError("use --probe-firmware with something"); printError("like /opt/intelFPGA/VERSION/quartus/linux64/blaster_6810.hex"); diff --git a/src/xilinx.cpp b/src/xilinx.cpp index bcf9428d1a..b2724e27ce 100644 --- a/src/xilinx.cpp +++ b/src/xilinx.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "jtag.hpp" #include "bitparser.hpp" @@ -371,10 +372,11 @@ void Xilinx::program(unsigned int offset, bool unprotect_flash) return; if (_mode == Device::FLASH_MODE && _file_extension == "jed") { - JedParser *jed; + if (_fpga_family != XC95_FAMILY && _fpga_family != XC2C_FAMILY) + throw std::runtime_error("Error: jed only supported for xc95 and xc2c"); printInfo("Open file ", false); - - jed = new JedParser(_filename, _verbose); + + std::unique_ptr jed(new JedParser(_filename, _verbose)); if (jed->parse() == EXIT_FAILURE) { printError("FAIL"); return; @@ -382,11 +384,9 @@ void Xilinx::program(unsigned int offset, bool unprotect_flash) printSuccess("DONE"); if (_fpga_family == XC95_FAMILY) - flow_program(jed); + flow_program(jed.get()); else if (_fpga_family == XC2C_FAMILY) - xc2c_flow_program(jed); - else - throw std::runtime_error("Error: jed only supported for xc95 and xc2c"); + xc2c_flow_program(jed.get()); return; } @@ -974,7 +974,7 @@ std::string Xilinx::flow_read() std::string buffer; uint8_t wr_buf[16+2]; // largest section length uint8_t rd_buf[16+2]; - memset(wr_buf, 0xff, 16); + memset(wr_buf, 0xff, sizeof(wr_buf)); /* limit JTAG clock frequency to 1MHz */ if (_jtag->getClkFreq() > 1e6) @@ -1433,7 +1433,6 @@ std::string Xilinx::xc2c_flow_read() bool Xilinx::xc2c_flow_program(JedParser *jed) { - uint8_t wr_buf[249]; // largest section length uint32_t delay_loop = (_jtag->getClkFreq() * 20) / 1000; uint8_t shift_addr = 8 - _cpld_addr_size; @@ -1472,6 +1471,7 @@ bool Xilinx::xc2c_flow_program(JedParser *jed) for (auto row : listfuse) { uint16_t pos = 0; uint8_t addr = _gray_code[iter] >> shift_addr; + uint8_t wr_buf[249] = {0}; // largest section length for (auto col : row) { if (col) wr_buf[pos >> 3] |= (1 << (pos & 0x07)); @@ -1577,7 +1577,7 @@ int Xilinx::spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond, uint32_t timeout, bool verbose) { uint8_t rx[2]; - uint8_t dummy[2]; + uint8_t dummy[2] = {0xff}; uint8_t tmp; uint8_t tx = McsParser::reverseByte(cmd); uint32_t count = 0; diff --git a/src/xvc_server.hpp b/src/xvc_server.hpp index cfad74354e..1df05d5425 100644 --- a/src/xvc_server.hpp +++ b/src/xvc_server.hpp @@ -82,8 +82,8 @@ class XVC_server { int _sock; /*!< server socket descriptor */ struct sockaddr_in _sock_addr; std::thread *_thread; /*!< connection thread */ - bool _is_stopped; /*!< true when thread is stopped */ - bool _must_stop; /*!< true to stop thread */ + volatile bool _is_stopped; /*!< true when thread is stopped */ + volatile bool _must_stop; /*!< true to stop thread */ uint32_t _buffer_size; /*!< buffer max capacity TDI+TMS */ uint8_t *_tmstdi; /*!< TDI/TMS from client */ uint8_t *_result; /*!< buffer for server -> client */