latticeBitParser: when .bit is encrypted use Part: from header to retrieve idcode

This commit is contained in:
Gwenhael Goavec-Merou 2022-03-27 07:51:18 +02:00
parent da3dfff609
commit 386f89ea3a
1 changed files with 29 additions and 13 deletions

View File

@ -15,6 +15,7 @@
#include <utility> #include <utility>
#include "display.hpp" #include "display.hpp"
#include "part.hpp"
#include "latticeBitParser.hpp" #include "latticeBitParser.hpp"
@ -82,7 +83,8 @@ int LatticeBitParser::parse()
return EXIT_FAILURE; return EXIT_FAILURE;
/* check preamble */ /* check preamble */
if ((*(uint32_t *)&_raw_data[_endHeader+1]) != 0xb3bdffff) { uint32_t preamble = (*(uint32_t *)&_raw_data[_endHeader+1]);
if ((preamble != 0xb3bdffff) && (preamble != 0xb3bfffff)) {
printError("Error: missing preamble\n"); printError("Error: missing preamble\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -92,6 +94,7 @@ int LatticeBitParser::parse()
std::move(_raw_data.begin()+_endHeader, _raw_data.end(), _bit_data.begin()); std::move(_raw_data.begin()+_endHeader, _raw_data.end(), _bit_data.begin());
_bit_length = _bit_data.size() * 8; _bit_length = _bit_data.size() * 8;
if (preamble == 0xb3bdffff) {
/* extract idcode from configuration data (area starting with 0xE2) */ /* extract idcode from configuration data (area starting with 0xE2) */
for (size_t i = 0; i < _bit_data.size();i++) { for (size_t i = 0; i < _bit_data.size();i++) {
if ((uint8_t)_bit_data[i] != 0xe2) if ((uint8_t)_bit_data[i] != 0xe2)
@ -105,6 +108,19 @@ int LatticeBitParser::parse()
snprintf(&_hdr["idcode"][0], 9, "%08x", idcode); snprintf(&_hdr["idcode"][0], 9, "%08x", idcode);
break; break;
} }
} else { // encrypted bitstream
string part = getHeaderVal("Part");
string subpart = part.substr(0, part.find_last_of("-"));
for (auto && fpga : fpga_list) {
if (fpga.second.manufacturer != "lattice")
continue;
string model = fpga.second.model;
if (subpart.compare(0, model.size(), model) == 0) {
_hdr["idcode"] = string(8, ' ');
snprintf(&_hdr["idcode"][0], 9, "%08x", fpga.first);
}
}
}
return 0; return 0;
} }