add minimal support for anlogic bit file

This commit is contained in:
Gwenhael Goavec-Merou 2020-08-21 14:01:07 +02:00
parent 5a214b4c79
commit 4a9fcaf1b9
3 changed files with 189 additions and 0 deletions

View File

@ -41,6 +41,7 @@ endif()
set(OPENFPGALOADER_SOURCE
src/anlogic.cpp
src/anlogicBitParser.cpp
src/anlogicCable.cpp
src/dirtyJtag.cpp
src/spiFlash.cpp
@ -71,6 +72,7 @@ set(OPENFPGALOADER_SOURCE
set(OPENFPGALOADER_HEADERS
src/altera.hpp
src/anlogic.hpp
src/anlogicBitParser.hpp
src/anlogicCable.hpp
src/cxxopts.hpp
src/dirtyJtag.hpp

145
src/anlogicBitParser.cpp Normal file
View File

@ -0,0 +1,145 @@
/*
* Copyright (C) 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
* Based on Miodrag Milanovic https://github.com/mmicko/prjtang/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <cctype>
#include <iostream>
#include <locale>
#include <vector>
#include "anlogicBitParser.hpp"
#include "display.hpp"
using namespace std;
AnlogicBitParser::AnlogicBitParser(const string &filename, bool verbose):
ConfigBitstreamParser(filename, ConfigBitstreamParser::BIN_MODE, verbose)
{}
AnlogicBitParser::~AnlogicBitParser()
{
}
void AnlogicBitParser::displayHeader()
{
cout << "Anlogic bitstream header infos" << endl;
for (auto it = _hdr.begin(); it != _hdr.end(); it++) {
cout << (*it).first << ": " << (*it).second << endl;
}
}
/* read all ascii lines starting with '#'
* stop when an empty line is found
*/
int AnlogicBitParser::parseHeader()
{
int ret = EXIT_SUCCESS;
printInfo("parseHeader");
while(1) {
string buffer;
std::getline(_fd, buffer, '\n');
if (_fd.eof()) {
printError("End of file before start of data");
return EXIT_FAILURE;
}
if (buffer.empty()) {
printInfo("header end");
break;
}
if (buffer[0] != '#') {
printError("header must start with #\n");
return EXIT_FAILURE;
}
string content = buffer.substr(2);
size_t pos = content.find(':');
if (pos == string::npos) {
_hdr["tool"] = content;
} else {
string entry = content.substr(0, pos);
string val = content.substr(pos+2);
_hdr[entry] = val;
}
}
return ret;
}
int AnlogicBitParser::parse()
{
if (parseHeader() == EXIT_FAILURE)
return EXIT_FAILURE;
uint8_t dummy = _fd.get();
if (dummy != 0x00) {
printError("Header must end with 0x00 (binary) bit");
return EXIT_FAILURE;
}
size_t start_data = _fd.tellg();
start_data--;
_fd.seekg(0, _fd.end);
size_t size_data = (size_t)_fd.tellg() - start_data;
_fd.seekg(start_data, _fd.beg);
vector<uint8_t> data;
data.resize(size_data);
_fd.read(reinterpret_cast<char *>(&(data[0])), size_data);
int pos = 0;
std::vector<std::vector<uint8_t>> blocks;
do {
uint16_t len = (data[pos++] << 8);
len += data[pos++];
if ((len & 7) != 0) {
printf("error\n");
return EXIT_FAILURE;
}
len >>= 3;
if ((pos + len) > data.size()) {
printf("error\n");
return EXIT_FAILURE;
}
std::vector<uint8_t> block = std::vector<uint8_t>(data.begin() + pos,
data.begin() + pos + len);
blocks.push_back(block);
pos += len;
} while (pos < data.size());
_bit_data.clear();
for (auto it = blocks.begin(); it != blocks.end(); it++) {
for (size_t pos = 0; pos < it->size(); pos++) {
_bit_data += reverseByte(((*it)[pos]));
}
}
_bit_length = _bit_data.size() * 8;
return 0;
}

42
src/anlogicBitParser.hpp Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2020 Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SRC_ANLOGICBITPARSER_HPP_
#define SRC_ANLOGICBITPARSER_HPP_
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include "configBitstreamParser.hpp"
/*
* Minimal implementation for anlogic bitstream
* only parse header part and store data in a buffer
*/
class AnlogicBitParser: public ConfigBitstreamParser {
public:
AnlogicBitParser(const std::string &filename, bool verbose = false);
~AnlogicBitParser();
int parse() override;
void displayHeader();
private:
int parseHeader();
};
#endif // SRC_ANLOGICBITPARSER_HPP_