-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathihexparser.c
358 lines (300 loc) · 11.8 KB
/
ihexparser.c
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*****************************************************************************
*
* Description:
* Ihex parser module for the avrdis project, parses the record structure
* found in the ihex file and produces the data structure with the parsed
* data for further processing.
*
* Author:
* Imre Horvath <imi [dot] horvath [at] gmail [dot] com> (c) 2023
*
* License:
* GNU GPLv3
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include "avrdis.h"
enum recordtype {
RECORDTYPE_IHEX_DATA_RECORD = 0x00,
RECORDTYPE_IHEX_EOF_RECORD = 0x01,
RECORDTYPE_IHEX_EXT_SEG_ADDR_RECORD = 0x02
};
static int parsehexbyte(FILE *fp, uint8_t *b)
{
int i, c;
char buf[] = {'\0', '\0', '\0'};
for (i = 0; i < 2; i++) {
if (!isxdigit(c = fgetc(fp))) {
ungetc(c, fp);
return 0;
}
buf[i] = c;
}
if (b)
*b = strtoul(buf, NULL, 16);
return 1;
}
int ihexfile(const char *filename)
{
FILE *fp = fopen(filename, "r");
int c, res = 0;
if (!fp) {
fprintf(stderr, "Error opening file: %s\n", filename);
return -1;
}
while ((c = fgetc(fp)) != EOF && c != ':')
;
if (c == ':') {
while ((c = fgetc(fp)) != EOF && isxdigit(c))
;
if (c == '\n' || c == '\r')
res = 1;
}
fclose(fp);
return res;
}
struct wordlist *parseihexfile(const char *filename)
{
int c, lineno = 1, eofr = 0, recparsed = 0;
uint8_t bytecount, rectype, chksum, wordcount, wdh, wdl, wordsum, addrh, addrl;
uint8_t extsah, extsal;
uint16_t extsegaddr = 0;
uint32_t wordaddress;
struct wordlist *newword, *wl, *firstdatarecword, *lastdatarecword;
struct wordlist *firstword = NULL, *lastword = NULL;
FILE *fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Error opening file: %s\n", filename);
return NULL;
}
/* Main parser loop */
for (;;) {
/* Position after the record start ':' */
while ((c = fgetc(fp)) != EOF && c != ':')
;
if (c == EOF)
break;
/* When yet another record after the "End Of File" record was found, signal an error */
if (eofr) {
fprintf(stderr,
"Record after \"End Of File\" record found at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Parse record fields */
/* Byte count */
if (!parsehexbyte(fp, &bytecount)) {
fprintf(stderr,
"Error parsing \"byte count\" in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Address high byte */
if (!parsehexbyte(fp, &addrh)) {
fprintf(stderr,
"Error parsing \"address\" high byte in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Address low byte */
if (!parsehexbyte(fp, &addrl)) {
fprintf(stderr,
"Error parsing \"address\" low byte in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Record type */
if (!parsehexbyte(fp, &rectype)) {
fprintf(stderr,
"Error parsing \"record type\" in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Process records based on their type */
switch (rectype) {
case RECORDTYPE_IHEX_DATA_RECORD:
wordaddress = ((extsegaddr << 4) + ((addrh << 8) | addrl)) >> 1;
firstdatarecword = lastdatarecword = NULL;
/* Data word parser loop */
for (wordcount = bytecount >> 1; wordcount; wordcount--, wordaddress++) {
/* Parse data word low byte */
if (!parsehexbyte(fp, &wdl)) {
fprintf(stderr,
"Error parsing \"word\" low byte in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstdatarecword);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Parse data word high byte */
if (!parsehexbyte(fp, &wdh)) {
fprintf(stderr,
"Error parsing \"word\" high byte in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstdatarecword);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Allocate word structure */
newword = (struct wordlist *) malloc(sizeof(struct wordlist));
if (!newword) {
fprintf(stderr, "Error allocating memory.\n");
freewordlist(firstdatarecword);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Zero out allocated memory */
memset(newword, 0, sizeof(struct wordlist));
/* Populate word structure with data */
newword->wordaddress = wordaddress;
newword->word = (wdh << 8) | wdl;
/* Link the data record word into a list */
if (!firstdatarecword)
firstdatarecword = newword;
else
lastdatarecword->next = newword;
lastdatarecword = newword;
} /* Data word parser loop */
/* Parse checksum */
if (!parsehexbyte(fp, &chksum)) {
fprintf(stderr,
"Error parsing \"checksum\" in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstdatarecword);
freewordlist(firstword);
return NULL;
}
/* Calculate data word sum for checksum check */
for (wordsum = 0, wl = firstdatarecword; wl; wl = wl->next)
wordsum += (wl->word >> 8) + (wl->word & 0xff);
/* Check checksum */
if ((uint8_t) (bytecount + addrh + addrl + rectype + wordsum + chksum) != 0) {
fprintf(stderr, "Checksum error at line %d in file %s.\n", lineno, filename);
freewordlist(firstdatarecword);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Add accumulated list of data words to the word list */
if (!firstword)
firstword = firstdatarecword;
else
lastword->next = firstdatarecword;
lastword = lastdatarecword;
recparsed = 1; /* Flag record has been parsed */
break;
case RECORDTYPE_IHEX_EOF_RECORD:
if (!parsehexbyte(fp, &chksum)) {
fprintf(stderr,
"Error parsing \"checksum\" in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Check checksum */
if ((uint8_t) (bytecount + addrh + addrl + rectype + chksum) != 0) {
fprintf(stderr, "Checksum error at line %d in file %s.\n", lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
eofr = 1; /* Flag "End Of File" record */
recparsed = 1; /* Flag record has been parsed */
break;
case RECORDTYPE_IHEX_EXT_SEG_ADDR_RECORD:
/* Check if the "Extended Segment Address" record is in the first position */
if (recparsed) {
fprintf(stderr,
"\"Extended Segment Address\" record at line %d in file %s\n",
lineno, filename);
freewordlist(firstword);
return NULL;
}
/* Parse the "Extended Segment Address" address */
if (!parsehexbyte(fp, &extsah)) {
fprintf(stderr,
"Error parsing \"segment base address\" high byte in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
if (!parsehexbyte(fp, &extsal)) {
fprintf(stderr,
"Error parsing \"segment base address\" low byte in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Parse checksum */
if (!parsehexbyte(fp, &chksum)) {
fprintf(stderr, "Error parsing \"checksum\" in record at line %d in file %s.\n",
lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
/* Check checksum */
if ((uint8_t) (bytecount + addrh + addrl + rectype + extsah + extsal + chksum) != 0) {
fprintf(stderr, "Checksum error at line %d in file %s.\n", lineno, filename);
freewordlist(firstword);
fclose(fp);
return NULL;
}
extsegaddr = (extsah << 8) | extsal;
recparsed = 1; /* Flag record has been parsed */
break;
default:
/* Unsupported record types gets ignored */
while (isxdigit(c = fgetc(fp)))
;
ungetc(c, fp);
recparsed = 1; /* Flag record has been parsed */
break;
} /* Switch case on record type */
/* Position after the end of line */
while ((c = fgetc(fp)) != EOF && c != '\n' && c != '\r')
;
if (c == EOF)
break;
/* Increment line number when end of line found */
if (c == '\n') {
if ((c = fgetc(fp)) != '\r')
ungetc(c, fp);
lineno++;
} else if (c == '\r') {
lineno++;
}
} /* Main parser loop */
/* Close file after parsing */
fclose(fp);
/* No "End Of File" record was found */
if (!eofr) {
fprintf(stderr,
"No \"End Of File\" record was found when reaching the end of file %s\n",
filename);
freewordlist(firstword);
return NULL;
}
/* File was successfully parsed */
return firstword;
}