diff --git a/src/efinix.cpp b/src/efinix.cpp index e52448e..0433f55 100644 --- a/src/efinix.cpp +++ b/src/efinix.cpp @@ -100,30 +100,8 @@ void Efinix::program(unsigned int offset) flash.erase_and_prog(offset, data, length); /* verify write if required */ - if (_verify) { - printInfo("Verifying write"); - std::string verify_data; - verify_data.resize(length); - printInfo("Read flash ", false); - if (0 != flash.read(offset, (uint8_t*)&verify_data[0], length)) { - printError("FAIL"); - return; - } else { - printSuccess("DONE"); - } - - ProgressBar progress("Check", length, 50, _quiet); - for (int i = 0; i < length; i++) { - if ((uint8_t)verify_data[i] != data[i]) { - progress.fail(); - printError("Verification failed at " + - std::to_string(offset + i)); - return; - } - progress.display(i); - } - progress.done(); - } + if (_verify) + flash.verify(offset, data, length); _spi->gpio_set(_rst_pin); usleep(12000); @@ -145,37 +123,20 @@ bool Efinix::dumpFlash(const std::string &filename, uint32_t timeout = 1000; _spi->gpio_clear(_rst_pin); - std::string data; - data.resize(len); - /* prepare SPI access */ printInfo("Read Flash ", false); try { SPIFlash flash(reinterpret_cast(_spi), _verbose); flash.reset(); flash.power_up(); - flash.read_id(); - flash.read_status_reg(); - flash.read(base_addr, (uint8_t*)&data[0], len); + flash.dump(filename, base_addr, len); } catch (std::exception &e) { printError("Fail"); printError(std::string(e.what())); return false; } - FILE *fd = fopen(filename.c_str(), "wb"); - if (!fd) { - printError("Fail"); - return false; - } - - fwrite(data.c_str(), sizeof(uint8_t), len, fd); - fclose(fd); - - printSuccess("Done"); - - /* prepare SPI access */ - + /* release SPI access */ _spi->gpio_set(_rst_pin); usleep(12000); diff --git a/src/ice40.cpp b/src/ice40.cpp index 3fbccdb..f797cc9 100644 --- a/src/ice40.cpp +++ b/src/ice40.cpp @@ -80,30 +80,8 @@ void Ice40::program(unsigned int offset) flash.read_id(); flash.erase_and_prog(offset, data, length); - if (_verify) { - printInfo("Verifying write"); - std::string verify_data; - verify_data.resize(length); - printInfo("Read flash ", false); - if (0 != flash.read(offset, (uint8_t*)&verify_data[0], length)) { - printError("FAIL"); - return; - } else { - printSuccess("DONE"); - } - - ProgressBar progress("Check", length, 50, _quiet); - for (int i = 0; i < length; i++) { - if ((uint8_t)verify_data[i] != data[i]) { - progress.fail(); - printError("Verification failed at " + - std::to_string(offset + i)); - return; - } - progress.display(i); - } - progress.done(); - } + if (_verify) + flash.verify(offset, data, length); _spi->gpio_set(_rst_pin); usleep(12000); @@ -125,36 +103,20 @@ bool Ice40::dumpFlash(const std::string &filename, uint32_t timeout = 1000; _spi->gpio_clear(_rst_pin); - std::string data; - data.resize(len); - /* prepare SPI access */ printInfo("Read Flash ", false); try { SPIFlash flash(reinterpret_cast(_spi), _verbose); flash.reset(); flash.power_up(); - flash.read_id(); - flash.read_status_reg(); - flash.read(base_addr, (uint8_t*)&data[0], len); + flash.dump(filename, base_addr, len); } catch (std::exception &e) { printError("Fail"); printError(std::string(e.what())); return false; } - FILE *fd = fopen(filename.c_str(), "wb"); - if (!fd) { - printError("Fail"); - return false; - } - - fwrite(data.c_str(), sizeof(uint8_t), len, fd); - fclose(fd); - - printSuccess("Done"); - - /* prepare SPI access */ + /* release SPI access */ _spi->gpio_set(_rst_pin); usleep(12000); diff --git a/src/lattice.cpp b/src/lattice.cpp index 8fe4f12..ef9bf46 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -447,33 +447,12 @@ bool Lattice::program_extFlash(unsigned int offset) flash.read_status_reg(); flash.erase_and_prog(offset, data, length); - if (_verify) { - printInfo("Verifying write"); - string verify_data; - verify_data.resize(length); - printInfo("Read flash ", false); - if (0 != flash.read(offset, (uint8_t*)&verify_data[0], length)) { - printError("FAIL"); - return false; - } else { - printSuccess("DONE"); - } - - ProgressBar progress("Check", length, 50, _quiet); - for (int i = 0; i < length; i++) { - if ((uint8_t)verify_data[i] != data[i]) { - progress.fail(); - printError("Verification failed at " + - std::to_string(offset + i)); - return false; - } - progress.display(i); - } - progress.done(); - } + int ret = true; + if (_verify) + ret = flash.verify(offset, data, length); delete _bit; - return true; + return ret; } bool Lattice::program_flash(unsigned int offset) @@ -580,8 +559,6 @@ bool Lattice::dumpFlash(const string &filename, DisableISC(); - string data; - data.resize(len); /* switch to SPI mode */ _jtag->shiftIR(0x3A, 8, Jtag::EXIT1_IR); uint8_t tmp[2] = {0xFE, 0x68}; @@ -590,17 +567,7 @@ bool Lattice::dumpFlash(const string &filename, /* prepare SPI access */ SPIFlash flash(this, _verbose); flash.reset(); - flash.read_id(); - flash.read_status_reg(); - flash.read(base_addr, (uint8_t*)&data[0], len); - - FILE *fd = fopen(filename.c_str(), "wb"); - if (!fd) { - return false; - } - - fwrite(data.c_str(), sizeof(uint8_t), len, fd); - fclose(fd); + flash.dump(filename, base_addr, len); /* ISC REFRESH 0x79 */ printInfo("Refresh: ", false); diff --git a/src/main.cpp b/src/main.cpp index 0b2d0fa..3399db5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -203,7 +203,8 @@ int main(int argc, char **argv) flash.reset(); flash.read_id(); - if (!args.bit_file.empty() || !args.file_type.empty()) { + if (args.prg_type != Device::RD_FLASH && + (!args.bit_file.empty() || !args.file_type.empty())) { printInfo("Open file " + args.bit_file + " ", false); try { bit = new RawParser(args.bit_file, false); @@ -226,10 +227,13 @@ int main(int argc, char **argv) flash.erase_and_prog(args.offset, bit->getData(), bit->getLength()/8); if (args.verify) - printWarn("writing verification not supported"); + flash.verify(args.offset, bit->getData(), bit->getLength() / 8); delete bit; + } else if (args.prg_type == Device::RD_FLASH) { + flash.dump(args.bit_file, args.offset, args.file_size); } + if (board->reset_pin) spi->gpio_set(board->reset_pin, true); } diff --git a/src/xilinx.cpp b/src/xilinx.cpp index 5580ded..4378525 100644 --- a/src/xilinx.cpp +++ b/src/xilinx.cpp @@ -172,34 +172,8 @@ void Xilinx::program_spi(ConfigBitstreamParser * bit, unsigned int offset) spiFlash.erase_and_prog(offset, data, length); /* verify write if required */ - if (_verify) { - std::string verify_data; - verify_data.resize(256); - - ProgressBar progress("Verifying write", length, 50, _quiet); - int rd_length = 256; - for (int i = 0; i < length; i+=rd_length) { - if (rd_length + i > length) - rd_length = length - i; - if (0 != spiFlash.read(offset + i, (uint8_t*)&verify_data[0], - rd_length)) { - progress.fail(); - printError("Failed to read flash"); - return; - } - - for (int ii = 0; ii < rd_length; ii++) { - if ((uint8_t)verify_data[ii] != data[i + ii]) { - progress.fail(); - printError("Verification failed at " + - std::to_string(offset + i + ii)); - return; - } - } - progress.display(i); - } - progress.done(); - } + if (_verify) + spiFlash.verify(offset, data, length, 256); } void Xilinx::program_mem(ConfigBitstreamParser *bitfile) @@ -314,49 +288,26 @@ void Xilinx::program_mem(ConfigBitstreamParser *bitfile) bool Xilinx::dumpFlash(const std::string &filename, uint32_t base_addr, uint32_t len) { + int ret = true; /* first need to have bridge in RAM */ if (load_bridge() == false) return false; /* prepare SPI access */ SPIFlash flash(this, _verbose); - flash.reset(); - flash.read_id(); - flash.read_status_reg(); - FILE *fd = fopen(filename.c_str(), "wb"); - if (!fd) { - printError("Open dump file failed\n"); - return false; + try { + flash.reset(); + ret = flash.dump(filename, base_addr, len, 256); + } catch (std::exception &e) { + printError(e.what()); + ret = false; } - uint32_t rd_length = 256; - std::string data; - data.resize(rd_length); - - ProgressBar progress("Dump flash", len, 50, _quiet); - - for (uint32_t i = 0; i < len; i+=rd_length) { - if (rd_length + i > len) - rd_length = len - i; - if (0 != flash.read(base_addr + i, (uint8_t*)&data[0], - rd_length)) { - progress.fail(); - printError("Failed to read flash"); - return false; - } - - fwrite(data.c_str(), sizeof(uint8_t), rd_length, fd); - - progress.display(i); - } - progress.done(); - fclose(fd); - /* reset device */ reset(); - return false; + return ret; } /*