Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
84 lines
5.3 KiB
C++
84 lines
5.3 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
#include "firmware_image.h"
|
|
#include "hexfile.h"
|
|
#include <QTemporaryFile>
|
|
#include <QMap>
|
|
#include <algorithm>
|
|
|
|
struct FirmwareSource { QByteArray image; QJsonObject info; };
|
|
|
|
// Validate the container strictly before passing it to the original HexFile
|
|
// reader. In particular, upstream's permissive fromHex must not silently omit
|
|
// junk, duplicate addresses, out-of-order overlapping records or sparse bombs.
|
|
static FirmwareSource inspectFirmwareSource(const QByteArray &source, const QString &kind, const QString &format, quint32 swdBase=0, int swdLimit=0) {
|
|
require(format == "bin" || format == "hex", "Use BIN or Intel HEX");
|
|
require(source.size() >= (format == "hex" ? 32 : 256) && source.size() <= (format == "hex" ? 8 : 2) * 1024 * 1024,
|
|
"Source file exceeds its bound");
|
|
QByteArray image = source;
|
|
quint32 base = swdLimit ? swdBase : kind == "bootloader" ? 0x080E0000 : 0x08000000;
|
|
const int limit=swdLimit ? swdLimit : kind == "bootloader" ? 65536 : 2*1024*1024;
|
|
require(limit>0 && limit<=2*1024*1024,"Invalid flash span");
|
|
int segments = 1;
|
|
if (format == "hex") {
|
|
QMap<quint32,quint32> ranges;
|
|
quint32 offset = 0; bool eof = false; int count = 0;
|
|
for (auto line : source.split('\n')) {
|
|
if (line.endsWith('\r')) line.chop(1);
|
|
if (line.isEmpty()) continue;
|
|
require(!eof && ++count <= 131074 && line.size() <= 521,
|
|
"HEX has trailing records or too many records");
|
|
require(QRegularExpression("^:[0-9A-Fa-f]+$").match(QString::fromLatin1(line)).hasMatch()
|
|
&& line.size() % 2 == 1, "Invalid HEX characters");
|
|
const auto record = QByteArray::fromHex(line.mid(1));
|
|
require(record.size() >= 5 && record.size() == 5 + quint8(record[0]), "Invalid HEX record length");
|
|
unsigned sum = 0; for (unsigned char c : record) sum += c;
|
|
require((sum & 255) == 0, "Invalid HEX checksum");
|
|
const int length = quint8(record[0]), type = quint8(record[3]);
|
|
const quint32 address = (quint8(record[1]) << 8) | quint8(record[2]);
|
|
if (type == 0) {
|
|
require(length > 0 && address + length <= 65536, "HEX record crosses address window");
|
|
// HexFile uses OR for these parts. Reject ambiguous segment
|
|
// combinations rather than reinterpret the upstream output.
|
|
require((offset & address) == 0, "Ambiguous HEX segment address");
|
|
const quint64 start = quint64(offset) + address, end = start + length;
|
|
require(start >= base && end <= quint64(base) + limit,
|
|
"HEX address does not belong to the selected image kind");
|
|
auto next = ranges.lowerBound(quint32(start));
|
|
require(next == ranges.end() || next.key() >= end, "Overlapping HEX records");
|
|
if (next != ranges.begin()) { auto prev = next; --prev; require(prev.value() <= start, "Overlapping HEX records"); }
|
|
ranges.insert(quint32(start), quint32(end));
|
|
} else if (type == 1) {
|
|
require(length == 0 && address == 0, "Invalid HEX EOF"); eof = true;
|
|
} else if (type == 2 || type == 4) {
|
|
require(length == 2 && address == 0, "Invalid HEX address record");
|
|
offset = quint32((quint8(record[4]) << 8) | quint8(record[5])) << (type == 2 ? 4 : 16);
|
|
} else if (type == 3 || type == 5) {
|
|
require(length == 4 && address == 0, "Invalid HEX entry point");
|
|
} else require(false, "Unsupported HEX record type");
|
|
}
|
|
require(eof && !ranges.isEmpty() && ranges.firstKey() == base, "HEX is truncated or has the wrong base address");
|
|
QTemporaryFile file;
|
|
require(file.open() && file.write(source) == source.size() && file.flush(), "Cannot inspect HEX source");
|
|
QMap<quint32,QByteArray> blocks;
|
|
require(HexFile::parseFile(file.fileName(), blocks) && !blocks.isEmpty(), "Original VESC Tool rejected HEX");
|
|
const auto size = quint64(ranges.last()) - base;
|
|
require(size >= 256 && size <= limit, "HEX span exceeds bound");
|
|
image = QByteArray(int(size), char(0xFF)); segments = blocks.size();
|
|
quint64 sourceBytes = 0, parsedBytes = 0;
|
|
for (auto it=ranges.begin();it!=ranges.end();++it) sourceBytes += it.value()-it.key();
|
|
for (auto it=blocks.begin();it!=blocks.end();++it) {
|
|
require(it.key() >= base && quint64(it.key())+it.value().size() <= quint64(base)+size,
|
|
"Original HEX reader returned an invalid address");
|
|
std::copy(it.value().begin(), it.value().end(), image.begin()+int(it.key()-base)); parsedBytes += it.value().size();
|
|
}
|
|
require(parsedBytes == sourceBytes, "Original HEX reader changed the data coverage");
|
|
}
|
|
require(image.size()<=limit,"Image exceeds selected flash span");
|
|
auto info = swdLimit ? QJsonObject{{"sha256",sha256(image)},{"bytes",image.size()}} : inspectFirmwareImage(image, kind);
|
|
info.insert("source_format", format); info.insert("source_bytes", source.size());
|
|
info.insert("source_sha256", sha256(source)); info.insert("base_address", double(base));
|
|
info.insert("segments", segments);
|
|
return {image, info};
|
|
}
|