aboutsummaryrefslogtreecommitdiffstats
path: root/host/utils/ihexcvt.cpp
blob: 0605ee61c4cf0cc6a7c7d7c1de108ee2be2e984f (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*  IHexCvt - Intel HEX File <=> Binary Converter (C++)
    Copyright (C) 2014  Ali Nakisaee

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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/

//Include needed stuff from C++
#include <iostream>
#include <fstream>
#include <string>

//... and also from C
#include <stdio.h>

#include <boost/filesystem.hpp>

#include <uhd/exception.hpp>
#include "ihexcvt.hpp"

//Avoid repeating 'std::':
using namespace std;

//The following function reads a hexadecimal number from a text file.
template <class T>
static bool ReadValueFromHex(ifstream& InputFile, T& outCh, unsigned char* ApplyChecksum)
{
	char V, L;
	T X = 0;
	outCh = 0;

	//Get the characters one by one.
	//Remember: These values are big-endian.
	//Remember: Every two hex characters (0-9/A-F) indicate ONE byte.
	for (size_t i = 0; i < 2 * sizeof(T); i++)
	{
		InputFile.get( V );
		if (InputFile.fail())
			return false;

		X <<= 4;
		if (V >= '0' && V <= '9')
			L = (V - '0');
		else if (V >= 'a' && V <= 'f')
			L = (V - 'a' + 10);
		else if (V >= 'A' && V <= 'F')
			L = (V - 'A' + 10);
		else
			return false;
		X |= L;

		//Apply this character to the checksum
		if (ApplyChecksum && i % 2 == 1) *ApplyChecksum += X & 0xFF;
	}

	//Return...
	outCh = X;
	return true;
}

//The following function writes a hexadecimal number from a text file.
template <class T>
static bool WriteHexValue(ofstream& OutFile, T Value, unsigned char* CalcChecksum)
{
	unsigned char V0 = 0;
	char C;

	//Remember: These values are big-endian.
	for (size_t i = 0; i < sizeof(T); i++)
	{
		//Get byte #i from the value.
		V0 = (Value >> ((sizeof(T) - i - 1) * 8)) & 0xFF;

		//Extract the high nibble (4-bits)
		if ((V0 & 0xF0) <= 0x90)
			C = (V0 >> 4) + '0';
		else
			C = (V0 >> 4) + ('A' - 10);
		OutFile.put( C );

		//Extract the low nibble (4-bits)
		if ((V0 & 0xF) <= 0x9)
			C = (V0 & 0xF) + '0';
		else
			C = (V0 & 0xF) + ('A' - 10);
		OutFile.put( C );

		//Calculate the checksum
		if (CalcChecksum) *CalcChecksum += V0;
	}
	return true;
}

//Skip any incoming whitespaces
static void SkipWhitespace(ifstream& InputFile)
{
	for (;;)
	{
		char C;
		InputFile.get(C);
		if (InputFile.eof() || InputFile.fail()) break;
		if (!(C == '\n' || C == '\r' || C == ' ' || C == '\t' || C == '\v'))
		{
			InputFile.putback(C);
			break;
		}
	}
}

//The function responsible for conversion from HEX files to BINary.
void Hex2Bin(const char* SrcName, const char* DstName, bool IgnoreChecksum)
{
	ifstream Src(SrcName);
	if (Src.bad())
	{
        throw uhd::runtime_error("Could not convert Intel .hex file to binary.");
	}

	ofstream Dst(DstName, ios_base::binary);
	if (Dst.bad())
	{
        throw uhd::runtime_error("Could not convert Intel .hex file to binary.");
	}

	char Ch;
	int LineIdx = 1;

	unsigned char ByteCount;
	unsigned short AddressLow;
	unsigned short Extra;
	unsigned long ExtraL;
	unsigned long AddressOffset = 0;
	unsigned char RecordType;
	unsigned char Data[255];
	unsigned char CurChecksum;
	unsigned char FileChecksum;
	bool EOFMarker = false;
	bool EOFWarn = false;
	
	for ( ;; )
	{
		Src.get(Ch);
		if (Src.eof())
			break;
		if (EOFMarker && !EOFWarn)
		{
            throw uhd::runtime_error("Could not convert Intel .hex file to binary.");
		}
		if (Ch != ':') goto genericErr;

		CurChecksum = 0;
		if (!ReadValueFromHex( Src, ByteCount, &CurChecksum )) goto genericErr;
		if (!ReadValueFromHex( Src, AddressLow, &CurChecksum )) goto genericErr;
		if (!ReadValueFromHex( Src, RecordType, &CurChecksum )) goto genericErr;

		switch (RecordType)
		{
		case 0x00: //Data record
			for (int i = 0; i < ByteCount; i++)
				if (!ReadValueFromHex( Src, Data[i], &CurChecksum )) goto genericErr;
			break;
		case 0x01: //End Marker
			if ( ByteCount != 0 )
			{
				goto onErrExit;
			}
			EOFMarker = true;
			break;
		case 0x02: //Extended Segment Address
			if ( ByteCount != 2 || AddressLow != 0 )
			{
				goto onErrExit;
			}
			if (!ReadValueFromHex( Src, Extra, &CurChecksum )) goto genericErr;
			AddressOffset = (unsigned long)Extra << 4;
			break;
		case 0x03: //Start Segment Address
			if ( ByteCount != 4 || AddressLow != 0 )
			{
				goto onErrExit;
			}
			if (!ReadValueFromHex( Src, ExtraL, &CurChecksum )) goto genericErr;
			break;
		case 0x04: //Extended Linear Address
			if ( ByteCount != 2 || AddressLow != 0 )
			{
				goto onErrExit;
			}
			if (!ReadValueFromHex( Src, Extra, &CurChecksum )) goto genericErr;
			AddressOffset = (unsigned long)Extra << 16;
			break;
		case 0x05: //Start Linear Address
			if ( ByteCount != 4 || AddressLow != 0 )
			{
				goto onErrExit;
			}
			if (!ReadValueFromHex( Src, ExtraL, &CurChecksum )) goto genericErr;
			break;
		}
		
		//Verify checksum
		CurChecksum = (~(CurChecksum & 0xFF) + 1) & 0xFF;
		if (!ReadValueFromHex( Src, FileChecksum, NULL )) goto genericErr;
		if (CurChecksum != FileChecksum)
		{
			if (!IgnoreChecksum) goto onErrExit;
		}

		//Put Data
		if (RecordType == 0x00)
		{
			Dst.seekp( AddressLow + AddressOffset );
			for (int i = 0; i < ByteCount; i++)
			{
				Dst.put( Data[i] );
			}
		}

		//Skip any white space
		SkipWhitespace( Src );

		LineIdx++;
	}

	Dst << flush;
	Dst.close();

	return;

genericErr:
    throw uhd::runtime_error("Invalid Intel .hex file detected.");

onErrExit:
	Dst.close();
	Src.close();
    boost::filesystem::remove(DstName);
    throw uhd::runtime_error("Could not convert Intel .hex file to binary.");
}