Skip to content

Commit fe115e1

Browse files
authored
Merge pull request #598 from zh522130/efinix-header-verify
efinix: Add header parsing and flash programming validation
2 parents c16e6d0 + ebf2f6f commit fe115e1

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/efinix.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string.h>
99
#include <unistd.h>
1010

11+
#include <algorithm>
1112
#include <iostream>
1213
#include <stdexcept>
1314
#include <string>
@@ -205,6 +206,29 @@ void Efinix::program(unsigned int offset, bool unprotect_flash)
205206
if (_verbose)
206207
bit->displayHeader();
207208

209+
if (_mode == FLASH_MODE) {
210+
try {
211+
if (!_device_package.empty()) {
212+
std::string device = bit->getHeaderVal("device");
213+
std::transform(device.begin(), device.end(), device.begin(), ::tolower);
214+
std::string target = _device_package;
215+
std::transform(target.begin(), target.end(), target.begin(), ::tolower);
216+
if (!device.empty() && device != target) {
217+
delete bit;
218+
throw std::runtime_error("device mismatch: " + device + " != " + target);
219+
}
220+
}
221+
std::string mode = bit->getHeaderVal("mode");
222+
if (mode.find("passive") != std::string::npos) {
223+
delete bit;
224+
throw std::runtime_error("passive mode not supported for flash");
225+
}
226+
} catch (std::runtime_error& e) {
227+
throw;
228+
} catch (...) {
229+
}
230+
}
231+
208232
switch (_mode) {
209233
case MEM_MODE:
210234
if (!programJTAG(data, length)) {

src/efinixHexParser.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,55 @@ EfinixHexParser::EfinixHexParser(const string &filename):
1717
false)
1818
{}
1919

20+
int EfinixHexParser::parseHeader()
21+
{
22+
string buffer;
23+
istringstream lineStream(_raw_data);
24+
int bytesRead = 0;
25+
string headerText;
26+
bool foundPaddedBits = false;
27+
28+
while (std::getline(lineStream, buffer, '\n')) {
29+
bytesRead += buffer.size() + 1;
30+
31+
if (buffer != "0A") {
32+
try {
33+
uint8_t byte = std::stol(buffer, nullptr, 16);
34+
headerText += (char)byte;
35+
} catch (...) {
36+
}
37+
} else {
38+
headerText += '\n';
39+
if (foundPaddedBits)
40+
break;
41+
}
42+
43+
if (headerText.find("PADDED_BITS") != string::npos)
44+
foundPaddedBits = true;
45+
}
46+
47+
size_t pos;
48+
if ((pos = headerText.find("Mode: ")) != string::npos) {
49+
size_t end = headerText.find('\n', pos);
50+
_hdr["mode"] = headerText.substr(pos + 6, end - pos - 6);
51+
}
52+
if ((pos = headerText.find("Width: ")) != string::npos) {
53+
size_t end = headerText.find('\n', pos);
54+
_hdr["width"] = headerText.substr(pos + 7, end - pos - 7);
55+
}
56+
if ((pos = headerText.find("Device: ")) != string::npos) {
57+
size_t end = headerText.find('\n', pos);
58+
_hdr["device"] = headerText.substr(pos + 8, end - pos - 8);
59+
}
60+
61+
return bytesRead;
62+
}
63+
2064
int EfinixHexParser::parse()
2165
{
2266
string buffer;
67+
parseHeader();
68+
2369
istringstream lineStream(_raw_data);
2470

2571
while (std::getline(lineStream, buffer, '\n')) {

src/efinixHexParser.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class EfinixHexParser: public ConfigBitstreamParser {
2828
* \return EXIT_SUCCESS is file is fully read, EXIT_FAILURE otherwise
2929
*/
3030
int parse() override;
31+
32+
private:
33+
int parseHeader();
3134
};
3235

3336
#endif // SRC_EFINIXHEXPARSER_HPP_

0 commit comments

Comments
 (0)