-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexmanager.cpp
246 lines (206 loc) · 6.33 KB
/
hexmanager.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
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
#include "hexmanager.h"
#include <string.h>
#include <iostream>
#include "bootloader.h"
// Virtual Flash.
#define KB (1024)
#define MB (KB*KB)
// 5 MB flash
static unsigned char VirtualFlash[5*MB];
#define BOOT_SECTOR_BEGIN 0x9FC00000
#define APPLICATION_START 0x9D000000
#define PA_TO_VFA(x) (x-APPLICATION_START)
#define PA_TO_KVA0(x) (x|0x80000000)
#define DATA_RECORD 0
#define END_OF_FILE_RECORD 1
#define EXT_SEG_ADRS_RECORD 2
#define EXT_LIN_ADRS_RECORD 4
/****************************************************************************
* Loads hex file
*
* \param
* \param
* \param
* \return true if hex file loads successfully
*****************************************************************************/
bool CHexManager::LoadHexFile(void)
{
// Open file
char hexFilePathCString[HexFilePath.length()];
int i;
for (i = 0; i < sizeof(hexFilePathCString); i++)
{
hexFilePathCString[i] = HexFilePath[i];
}
std::cout << "Hex file being opened: " << hexFilePathCString << std::endl;
HexFilePtr = fopen(hexFilePathCString, "r");
if(HexFilePtr == NULL)
{
// Failed to open hex file.
return false;
}
else
{
char HexRec[255];
HexTotalLines = 0;
while(!feof(HexFilePtr))
{
fgets(HexRec, sizeof(HexRec), HexFilePtr);
HexTotalLines++;
}
}
return true;
}
/****************************************************************************
* Gets next hex record from the hex file
*
* \param HexRec: Pointer to HexRec.
* \param BuffLen: Buffer Length
* \param
* \return Length of the hex record in bytes.
*****************************************************************************/
static char Ascii[1000];
unsigned short CHexManager::GetNextHexRecord(char *HexRec, unsigned int BuffLen)
{
unsigned short len = 0;
if(!feof(HexFilePtr))
{
fgets(Ascii, BuffLen, HexFilePtr);
if(Ascii[0] != ':')
{
// Not a valid hex record.
return 0;
}
// Convert rest to hex.
len = ConvertAsciiToHex((void *)&Ascii[1], (void *)HexRec);
HexCurrLineNo++;
}
return len;
}
/****************************************************************************
* Resets hex file pointer.
*
* \param
* \param
* \param
* \return True if resets file pointer.
*****************************************************************************/
bool CHexManager::ResetHexFilePointer(void)
{
// Reset file pointer.
if(HexFilePtr == NULL)
{
return false;
}
else
{
fseek(HexFilePtr, 0, 0);
HexCurrLineNo = 0;
return true;
}
}
/****************************************************************************
* Converts ASCII to hex.
*
* \param VdAscii: Hex Record in ASCII format.
* \param VdHexRec: Hex record in Hex format.
* \param
* \return Number of bytes in Hex record(Hex format)
*****************************************************************************/
unsigned short CHexManager::ConvertAsciiToHex(void *VdAscii, void *VdHexRec)
{
char temp[5] = {'0','x',NULL, NULL, NULL};
unsigned int i = 0;
char *Ascii;
char *HexRec;
Ascii = (char *)VdAscii;
HexRec = (char *)VdHexRec;
while(1)
{
temp[2] = Ascii[i++];
temp[3] = Ascii[i++];
if((temp[2] == NULL) || (temp[3] == NULL))
{
// Not a valid ASCII. Stop conversion and break.
i -= 2;
break;
}
else
{
// Convert ASCII to hex.
sscanf(temp, "%x", HexRec);
HexRec++;
}
}
return (i/2); // i/2: Because, an representing Hex in ASCII takes 2 bytes.
}
/****************************************************************************
* Verifies flash
*
* \param StartAddress: Pointer to program start address
* \param ProgLen: Pointer to Program length in bytes
* \param crc : Pointer to CRC
* \return
*****************************************************************************/
void CHexManager::VerifyFlash(unsigned int* StartAdress, unsigned int* ProgLen, unsigned short* crc)
{
unsigned short HexRecLen;
char HexRec[255];
hexrecord_t HexRecordSt;
unsigned int VirtualFlashAdrs;
unsigned int ProgAddress;
// Virtual Flash Erase (Set all bytes to 0xFF)
memset((void*)VirtualFlash, 0xFF, sizeof(VirtualFlash));
// Start decoding the hex file and write into virtual flash
// Reset file pointer.
fseek(HexFilePtr, 0, 0);
// Reset max address and min address.
HexRecordSt.MaxAddress = 0;
HexRecordSt.MinAddress = 0xFFFFFFFF;
while((HexRecLen = GetNextHexRecord(&HexRec[0], 255)) != 0)
{
HexRecordSt.RecDataLen = HexRec[0];
HexRecordSt.RecType = HexRec[3];
HexRecordSt.Data = (unsigned char*)&HexRec[4];
switch(HexRecordSt.RecType)
{
case DATA_RECORD: //Record Type 00, data record.
HexRecordSt.Address = (((HexRec[1] << 8) & 0x0000FF00) | (HexRec[2] & 0x000000FF)) & (0x0000FFFF);
HexRecordSt.Address = HexRecordSt.Address + HexRecordSt.ExtLinAddress + HexRecordSt.ExtSegAddress;
ProgAddress = PA_TO_KVA0(HexRecordSt.Address);
if(ProgAddress < BOOT_SECTOR_BEGIN) // Make sure we are not writing boot sector.
{
if(HexRecordSt.MaxAddress < (ProgAddress + HexRecordSt.RecDataLen))
{
HexRecordSt.MaxAddress = ProgAddress + HexRecordSt.RecDataLen;
}
if(HexRecordSt.MinAddress > ProgAddress)
{
HexRecordSt.MinAddress = ProgAddress;
}
VirtualFlashAdrs = PA_TO_VFA(ProgAddress); // Program address to local virtual flash address
memcpy((void *)&VirtualFlash[VirtualFlashAdrs], HexRecordSt.Data, HexRecordSt.RecDataLen);
}
break;
case EXT_SEG_ADRS_RECORD: // Record Type 02, defines 4 to 19 of the data address.
HexRecordSt.ExtSegAddress = ((HexRecordSt.Data[0] << 16) & 0x00FF0000) | ((HexRecordSt.Data[1] << 8) & 0x0000FF00);
HexRecordSt.ExtLinAddress = 0;
break;
case EXT_LIN_ADRS_RECORD:
HexRecordSt.ExtLinAddress = ((HexRecordSt.Data[0] << 24) & 0xFF000000) | ((HexRecordSt.Data[1] << 16) & 0x00FF0000);
HexRecordSt.ExtSegAddress = 0;
break;
case END_OF_FILE_RECORD: //Record Type 01
default:
HexRecordSt.ExtSegAddress = 0;
HexRecordSt.ExtLinAddress = 0;
break;
}
}
HexRecordSt.MinAddress -= HexRecordSt.MinAddress % 4;
HexRecordSt.MaxAddress += HexRecordSt.MaxAddress % 4;
*ProgLen = HexRecordSt.MaxAddress - HexRecordSt.MinAddress;
*StartAdress = HexRecordSt.MinAddress;
VirtualFlashAdrs = PA_TO_VFA(HexRecordSt.MinAddress);
*crc = Bootloader::CalculateCrc((char*)&VirtualFlash[VirtualFlashAdrs], *ProgLen);
}