-
Notifications
You must be signed in to change notification settings - Fork 532
/
hexfile.cpp
142 lines (122 loc) · 4.43 KB
/
hexfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright 2021 Radinn AB.
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "hexfile.h"
#include <QFile>
#include <QDebug>
enum {
RecordType_Data = 0,
RecordType_EndOfFile,
RecordType_ExtendedSegmentAddress,
RecordType_StartSegmentAddress,
RecordType_ExtenededLinearAddress,
RecordType_StartLinearAddress,
};
bool HexFile::parseFile(QString fileName, QMap<quint32, QByteArray> &output)
{
bool endOfFileSeen = false;
quint32 currentOffset = 0;
quint32 currentAddress = 0;
QByteArray currentBytes;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Failed to open" << fileName;
return false;
}
while (!file.atEnd()) {
QByteArray line = file.readLine();
if (line.size() == 0) {
continue;
}
if (endOfFileSeen) {
qDebug() << "End-of-file record found in the middle of the file";
return false;
}
if (line[0] != ':') {
qDebug() << "Invalid line start:" << line;
return false;
}
// The starting colon will be ignored by fromHex()
QByteArray binaryLine = QByteArray::fromHex(line);
if (binaryLine.size() < 5) {
qDebug() << "Line too short:" << line;
return false;
}
quint8 dataLength = quint8(binaryLine[0]);
quint32 address = (quint32(binaryLine[1] & 0xFF) << 8) | quint32(binaryLine[2] & 0xFF);
quint8 recordType = quint8(binaryLine[3]);
if (binaryLine.size() < 5 + dataLength) {
qDebug() << "Missing data in line:" << line;
return false;
}
quint8 checksum = 0;
for (int i = 0; i < binaryLine.size() - 1; i++) {
checksum += binaryLine[i];
}
quint8 embeddedChecksum = quint8(binaryLine[4 + dataLength]);
if (quint8(checksum + embeddedChecksum) != 0) {
qDebug() << "Invalid checksum in line:" << line;
return false;
};
QByteArray data = binaryLine.mid(4, dataLength);
switch (recordType) {
case RecordType_Data:
address |= currentOffset;
if (address == currentAddress + quint32(currentBytes.size())) {
currentBytes.append(data);
} else {
if (currentBytes.size() > 0) {
output.insert(currentAddress, currentBytes);
}
currentBytes = data;
currentAddress = address;
}
break;
case RecordType_EndOfFile:
endOfFileSeen = true;
break;
case RecordType_ExtendedSegmentAddress:
if (dataLength != 2) {
qDebug() << "Invalid data length for extended segment address:" << dataLength;
return false;
}
currentOffset = ((quint32(data[0] & 0xFF) << 8) | quint32(data[1] & 0xFF)) << 4;
break;
case RecordType_StartSegmentAddress:
// Start address is not used
break;
case RecordType_ExtenededLinearAddress:
if (dataLength != 2) {
qDebug() << "Invalid data length for extended linear address:" << dataLength;
return false;
}
currentOffset = ((quint32(data[0] & 0xFF) << 8) | quint32(data[1] & 0xFF)) << 16;
break;
case RecordType_StartLinearAddress:
// Start address is not used
break;
default:
qDebug() << "Unknown record type:" << line;
return false;
}
}
if (!endOfFileSeen) {
qDebug() << "File truncated";
return false;
}
if (currentBytes.size() > 0) {
output.insert(currentAddress, currentBytes);
}
return true;
}