Merge pull request #598 from zh522130/efinix-header-verify

efinix: Add header parsing and flash programming validation
This commit is contained in:
Gwenhael Goavec-Merou 2025-12-13 08:53:37 +01:00 committed by GitHub
commit fe115e10dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include <string.h>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <string>
@ -205,6 +206,29 @@ void Efinix::program(unsigned int offset, bool unprotect_flash)
if (_verbose)
bit->displayHeader();
if (_mode == FLASH_MODE) {
try {
if (!_device_package.empty()) {
std::string device = bit->getHeaderVal("device");
std::transform(device.begin(), device.end(), device.begin(), ::tolower);
std::string target = _device_package;
std::transform(target.begin(), target.end(), target.begin(), ::tolower);
if (!device.empty() && device != target) {
delete bit;
throw std::runtime_error("device mismatch: " + device + " != " + target);
}
}
std::string mode = bit->getHeaderVal("mode");
if (mode.find("passive") != std::string::npos) {
delete bit;
throw std::runtime_error("passive mode not supported for flash");
}
} catch (std::runtime_error& e) {
throw;
} catch (...) {
}
}
switch (_mode) {
case MEM_MODE:
if (!programJTAG(data, length)) {

View File

@ -17,9 +17,55 @@ EfinixHexParser::EfinixHexParser(const string &filename):
false)
{}
int EfinixHexParser::parseHeader()
{
string buffer;
istringstream lineStream(_raw_data);
int bytesRead = 0;
string headerText;
bool foundPaddedBits = false;
while (std::getline(lineStream, buffer, '\n')) {
bytesRead += buffer.size() + 1;
if (buffer != "0A") {
try {
uint8_t byte = std::stol(buffer, nullptr, 16);
headerText += (char)byte;
} catch (...) {
}
} else {
headerText += '\n';
if (foundPaddedBits)
break;
}
if (headerText.find("PADDED_BITS") != string::npos)
foundPaddedBits = true;
}
size_t pos;
if ((pos = headerText.find("Mode: ")) != string::npos) {
size_t end = headerText.find('\n', pos);
_hdr["mode"] = headerText.substr(pos + 6, end - pos - 6);
}
if ((pos = headerText.find("Width: ")) != string::npos) {
size_t end = headerText.find('\n', pos);
_hdr["width"] = headerText.substr(pos + 7, end - pos - 7);
}
if ((pos = headerText.find("Device: ")) != string::npos) {
size_t end = headerText.find('\n', pos);
_hdr["device"] = headerText.substr(pos + 8, end - pos - 8);
}
return bytesRead;
}
int EfinixHexParser::parse()
{
string buffer;
parseHeader();
istringstream lineStream(_raw_data);
while (std::getline(lineStream, buffer, '\n')) {

View File

@ -28,6 +28,9 @@ class EfinixHexParser: public ConfigBitstreamParser {
* \return EXIT_SUCCESS is file is fully read, EXIT_FAILURE otherwise
*/
int parse() override;
private:
int parseHeader();
};
#endif // SRC_EFINIXHEXPARSER_HPP_