File: mcuHexBin.cpp

package info (click to toggle)
limesuite 23.11.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,228 kB
  • sloc: cpp: 157,511; ansic: 6,852; python: 197; sh: 56; xml: 21; makefile: 19
file content (251 lines) | stat: -rw-r--r-- 7,842 bytes parent folder | download | duplicates (6)
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
251
#include "mcuHexBin.h"

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

typedef struct
{
	unsigned long m_startAddress;
	std::vector<unsigned char> m_bytes;
} MemBlock;

int MCU_HEX2BIN(const char* filename, const uint16_t limit, uint8_t *binImage, uint16_t *imgSize)
{
    vector<MemBlock>	m_chunks;
	unsigned long		m_top;

    FILE* m_file = fopen(filename, "r");
    if (m_file == NULL)
    {
        printf("File not found: %s\n", filename);
        return -1;
    }
    memset(binImage, 0, limit);

    const uint16_t cMaxLineLen = 1024;
    char szLine[cMaxLineLen];
    bool endSeen = false;
    bool linear = true;				// Only used for intel hex
    unsigned long addressBase = 0;	// Only used for intel hex
    while(!feof(m_file))
    {
        //memset(szLine, 0, cMaxLineLen);
        if(fgets(szLine, cMaxLineLen, m_file) == 0)
        {
            if (ferror(m_file))
                printf("Error reading input!\n");
            continue;
        }

        //clear \r\n
        for(int i=0; i<2; ++i)
        {
            int lastCharPos = strlen(szLine)-1;
            if (szLine[lastCharPos] == '\n' || szLine[lastCharPos] == '\r')
                szLine[lastCharPos] = 0;
        }

        if (strlen(szLine) == cMaxLineLen-1)
        {
            printf("Hex file lines to long!\n");
            return -2;
        }
        // Ignore blank lines
        if (szLine[0] == '\n' || szLine[0] == '\r')
            continue;

        // Detect format and warn if garbage lines are found
        if (szLine[0] != ':')
        {
            printf("Ignoring garbage line!\n");
            continue;
        }

        if(endSeen)
        {
            printf("Hex line after end of file record!\n");
            return -3;
        }

        unsigned long dataBytes;
        unsigned long startAddress;
        unsigned long type;
        if(sscanf(&szLine[1], "%2lx%4lx%2lx", &dataBytes, &startAddress, &type) != 3)
        {
            printf("Hex line beginning corrupt!\n");
            return -4;
        }
        // Check line length
        if (szLine[11 + dataBytes * 2] != '\n' && szLine[11 + dataBytes * 2] != 0)
        {
            printf("Hex line length incorrect!\n");
            return -6;
        }
        // Check line checksum
        uint8_t checkSum = 0;
        unsigned long tmp;
        for (unsigned int i = 0; i <= dataBytes + 4; ++i)
        {
            if (sscanf(&szLine[1 + i * 2], "%2lx", &tmp) != 1)
            {
                printf("Hex line data corrupt!\n");
                return -7;
            }
            checkSum += tmp;
        }
        if (checkSum != 0)
        {
            printf("Hex line checksum error!\n");
            return -8;
        }

        switch (type)
        {
        case 0: // Data record
            if (!linear)
            {
                // Segmented
                unsigned long test = startAddress;
                test += dataBytes;
                if (test > 0xffff)
                {
                    printf("Can't handle wrapped segments!\n");
                    return -9;
                }
            }
            if (!m_chunks.size() ||
                m_chunks.back().m_startAddress + m_chunks.back().m_bytes.size() !=
                addressBase + startAddress)
            {
                m_chunks.push_back(MemBlock());
                m_chunks.back().m_startAddress = addressBase + startAddress;
            }
            {
                unsigned char i = 0;
                for (i = 0; i < dataBytes; ++i)
                {
                    sscanf(&szLine[9 + i * 2], "%2lx", &tmp);
                    if (addressBase + startAddress + i > limit)
                    {
                        cout << "Ignoring data above address space!\n";
                        cout << "Data address: " << addressBase + startAddress + i;
                        cout << " Limit: " << limit << "\n";
                        if (!m_chunks.back().m_bytes.size())
                        {
                            m_chunks.pop_back();
                        }
                        continue;
                    }
                    m_chunks.back().m_bytes.push_back(tmp);
                }
            }
            break;

        case 1: // End-of-file record
            if (dataBytes != 0)
                printf("Warning: End of file record not zero length!\n");
            if (startAddress != 0)
                printf("Warning: End of file record address not zero!\n");
            endSeen = true;
            break;

        case 2: // Extended segment address record
            if(dataBytes != 2)
            {
                printf("Length field must be 2 in extended segment address record!\n");
                return -10;
            }
            if (startAddress != 0)
            {
                printf("Address field must be zero in extended segment address record!\n");
                return -11;
            }
            sscanf(&szLine[9], "%4lx", &startAddress);
            addressBase = startAddress << 4;
            linear = false;
            break;

        case 3: // Start segment address record
            if (dataBytes != 4)
                printf("Warning: Length field must be 4 in start segment address record!\n");
            if (startAddress != 0)
                printf("Warning: Address field must be zero in start segment address record!\n");
            if (dataBytes == 4)
            {
                unsigned long ssa;
                char	ssaStr[16];
                sscanf(&szLine[9], "%8lx", &ssa);
                sprintf(ssaStr, "%08lX\n", ssa);
                cout << "Segment start address (CS/IP): ";
                cout << ssaStr;
            }
            break;

        case 4: // Extended linear address record
            if (dataBytes != 2)
            {
                printf("Length field must be 2 in extended linear address record!\n");
                return -12;
            }
            if (startAddress != 0)
            {
                printf("Address field must be zero in extended linear address record!\n");
                return -13;
            }
            sscanf(&szLine[9], "%4lx", &startAddress);
            addressBase = ((unsigned long)startAddress) << 16;
            linear = true;
            break;

        case 5: // Start linear address record
            if (dataBytes != 4)
                printf("Warning: Length field must be 4 in start linear address record!\n");
            if (startAddress != 0)
                printf("Warning: Address field must be zero in start linear address record!\n");
            if (dataBytes == 4)
            {
                unsigned long lsa;
                char	lsaStr[16];
                sscanf(&szLine[9], "%8lx", &lsa);
                sprintf(lsaStr, "%08lX\n", lsa);
                cout << "Linear start address: ";
                cout << lsaStr;
            }
            break;

        default:
            printf("Waring: Unknown record found!\n");
        }
    }
    if (!endSeen)
        printf("No end of file record!\n");
    if (!m_chunks.size())
    {
        printf("No data in file!\n");
        return -14;
    }
    vector<MemBlock>::iterator	vi;
    m_top = 0;
    for (vi = m_chunks.begin(); vi < m_chunks.end(); vi++)
    {
        m_top = std::max<size_t>(m_top, vi->m_startAddress + vi->m_bytes.size() - 1);
    }
    if(binImage)
    {
        memset(binImage, 0, limit);
        for(auto i : m_chunks)
        {
            for(size_t j=0; j<i.m_bytes.size(); ++j)
                if(i.m_startAddress+j < limit)
                    binImage[i.m_startAddress+j] = i.m_bytes[j];
        }
    }
    if(imgSize)
        *imgSize = m_top;
    return 0;
}