2021-06-26 15:24:07 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-12-20 08:47:38 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2019 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
|
|
|
|
|
*/
|
2019-12-22 08:16:37 +01:00
|
|
|
#include <string.h>
|
2019-12-20 08:47:38 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <cctype>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <locale>
|
2021-02-24 06:36:48 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include "display.hpp"
|
2022-03-27 07:51:18 +02:00
|
|
|
#include "part.hpp"
|
2019-12-20 08:47:38 +01:00
|
|
|
|
|
|
|
|
#include "latticeBitParser.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
LatticeBitParser::LatticeBitParser(const string &filename, bool verbose):
|
|
|
|
|
ConfigBitstreamParser(filename, ConfigBitstreamParser::BIN_MODE, verbose),
|
2021-02-24 06:36:48 +01:00
|
|
|
_endHeader(0)
|
2019-12-20 08:47:38 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
LatticeBitParser::~LatticeBitParser()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LatticeBitParser::parseHeader()
|
|
|
|
|
{
|
2021-02-24 06:36:48 +01:00
|
|
|
int currPos = 0;
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* check header signature */
|
2020-09-26 07:58:03 +02:00
|
|
|
|
|
|
|
|
/* radiant .bit start with LSCC */
|
2021-02-24 06:36:48 +01:00
|
|
|
if (_raw_data[0] == 'L') {
|
|
|
|
|
if (_raw_data.substr(0, 4) != "LSCC") {
|
|
|
|
|
printf("Wrong File %s\n", _raw_data.substr(0, 4).c_str());
|
2020-09-26 07:58:03 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2021-02-24 06:36:48 +01:00
|
|
|
currPos += 4;
|
2020-09-26 07:58:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* bit file comment area start with 0xff00 */
|
2021-02-24 06:36:48 +01:00
|
|
|
if ((uint8_t)_raw_data[currPos] != 0xff || (uint8_t)_raw_data[currPos + 1] != 0x00) {
|
|
|
|
|
printf("Wrong File %02x%02x\n", (uint8_t) _raw_data[currPos],
|
|
|
|
|
(uint8_t)_raw_data[currPos]);
|
2019-12-20 08:47:38 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2021-02-24 06:36:48 +01:00
|
|
|
currPos+=2;
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
|
|
|
|
|
_endHeader = _raw_data.find(0xff, currPos);
|
|
|
|
|
if (_endHeader == string::npos) {
|
|
|
|
|
printError("Error: preamble not found\n");
|
2019-12-22 08:16:37 +01:00
|
|
|
return EXIT_FAILURE;
|
2021-02-24 06:36:48 +01:00
|
|
|
}
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2022-05-14 19:00:21 +02:00
|
|
|
/* .bit for MACHXO3D seems to have more 0xff before preamble key */
|
|
|
|
|
size_t pos = _raw_data.find(0xb3, _endHeader);
|
|
|
|
|
if (pos == string::npos) {
|
|
|
|
|
printError("Preamble key not found");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
if ((uint8_t)_raw_data[pos-1] != 0xbd && (uint8_t)_raw_data[pos-1] != 0xbf) {
|
|
|
|
|
printError("Wrong preamble key");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
_endHeader = pos - 4;
|
|
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* parse header */
|
|
|
|
|
istringstream lineStream(_raw_data.substr(currPos, _endHeader-currPos));
|
|
|
|
|
string buff;
|
|
|
|
|
while (std::getline(lineStream, buff, '\0')) {
|
|
|
|
|
size_t pos = buff.find_first_of(':', 0);
|
|
|
|
|
if (pos != string::npos) {
|
|
|
|
|
string key(buff.substr(0, pos));
|
|
|
|
|
string val(buff.substr(pos+1, buff.size()));
|
|
|
|
|
int startPos = val.find_first_not_of(" ");
|
|
|
|
|
int endPos = val.find_last_not_of(" ")+1;
|
|
|
|
|
_hdr[key] = val.substr(startPos, endPos).c_str();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int LatticeBitParser::parse()
|
|
|
|
|
{
|
|
|
|
|
/* until 0xFFFFBDB3 0xFFFF */
|
|
|
|
|
if (parseHeader() < 0)
|
|
|
|
|
return EXIT_FAILURE;
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* check preamble */
|
2022-03-27 07:51:18 +02:00
|
|
|
uint32_t preamble = (*(uint32_t *)&_raw_data[_endHeader+1]);
|
|
|
|
|
if ((preamble != 0xb3bdffff) && (preamble != 0xb3bfffff)) {
|
2021-02-24 06:36:48 +01:00
|
|
|
printError("Error: missing preamble\n");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2019-12-20 08:47:38 +01:00
|
|
|
|
2021-02-24 06:36:48 +01:00
|
|
|
/* read All data */
|
|
|
|
|
_bit_data.resize(_raw_data.size() - _endHeader);
|
|
|
|
|
std::move(_raw_data.begin()+_endHeader, _raw_data.end(), _bit_data.begin());
|
2019-12-20 08:47:38 +01:00
|
|
|
_bit_length = _bit_data.size() * 8;
|
2021-02-24 06:36:48 +01:00
|
|
|
|
2022-03-27 07:51:18 +02:00
|
|
|
if (preamble == 0xb3bdffff) {
|
|
|
|
|
/* extract idcode from configuration data (area starting with 0xE2) */
|
|
|
|
|
for (size_t i = 0; i < _bit_data.size();i++) {
|
|
|
|
|
if ((uint8_t)_bit_data[i] != 0xe2)
|
|
|
|
|
continue;
|
|
|
|
|
/* E2: verif id */
|
|
|
|
|
uint32_t idcode = (((uint32_t)_bit_data[i+4]) << 24) |
|
|
|
|
|
(((uint32_t)_bit_data[i+5]) << 16) |
|
|
|
|
|
(((uint32_t)_bit_data[i+6]) << 8) |
|
|
|
|
|
(((uint32_t)_bit_data[i+7]) << 0);
|
|
|
|
|
_hdr["idcode"] = string(8, ' ');
|
|
|
|
|
snprintf(&_hdr["idcode"][0], 9, "%08x", idcode);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-20 17:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-20 08:47:38 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|