-
Notifications
You must be signed in to change notification settings - Fork 19
/
ihex_parse.c
335 lines (277 loc) · 7.64 KB
/
ihex_parse.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
/*
* (C) 2013 Martin Helmich <martin.helmich@hs-osnabrueck.de>
* Oliver Erxleben <oliver.erxleben@hs-osnabrueck.de>
*
* University of Applied Sciences Osnabrück
*
* This file is part of the CIntelHex library (libcintelhex).
*
* libcintelhex is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* libcintelhex 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libcintelhex. If not, see
* <http://www.gnu.org/licenses/>.
*/
#define IHEX_PARSE_C
#include "cintelhex.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#define IHEX_CHR_RECORDMARK 0x3A
#define IHEX_SET_ERROR(errnum, error, ...) \
{ char *e = malloc(512); \
snprintf(e, 512, error, ##__VA_ARGS__); \
ihex_set_error(errnum, e); }
#define IHEX_SET_ERROR_RETURN(errnum, error, ...) \
{ IHEX_SET_ERROR(errnum, error, ##__VA_ARGS__); \
return errnum; }
// Flags for open() syscall
#ifndef O_BINARY
#define O_BINARY 0
#endif
void ihex_set_error(ihex_error_t errnum, char* error);
static int ihex_parse_single_record(ihex_rdata_t data, unsigned int length, ihex_record_t* record);
ihex_recordset_t* ihex_rs_from_file(const char* filename)
{
struct stat s;
int fd;
ulong_t l;
char* c;
ihex_recordset_t* r;
fd = open(filename, O_RDONLY | O_BINARY);
if (fd < 0)
{
IHEX_SET_ERROR(IHEX_ERR_NO_INPUT, "Input file %s does not exist", filename);
goto open_failed;
}
if (fstat (fd, &s) != 0)
{
IHEX_SET_ERROR(IHEX_ERR_NO_INPUT, "Could not stat input file %s", filename);
goto stat_failed;
}
l = s.st_size;
#ifdef HAVE_MMAP
if ((c = (char*) mmap(NULL, l, PROT_READ, MAP_PRIVATE, fd, 0)) == (void*) -1)
{
IHEX_SET_ERROR(IHEX_ERR_MMAP_FAILED, "Could not map file %s", filename);
goto mmap_failed;
}
#else
if ((c = (char*) malloc(l)) == NULL)
{
IHEX_SET_ERROR(IHEX_ERR_READ_FAILED, "Could not allocate memory for reading file %s", filename);
goto malloc_failed;
}
ulong_t rest = l;
while (rest > 0)
{
ssize_t bytes = read(fd, c + (l - rest), rest);
if (bytes < 0)
{
IHEX_SET_ERROR(IHEX_ERR_READ_FAILED, "Could not read file %s", filename);
goto read_failed;
}
else if (bytes == 0)
{
break; //end of file
}
rest -= bytes;
}
#endif
r = ihex_rs_from_mem(c, l);
// No special error treatment necessary, we need to unmap and close
// the file anyway.
#ifdef HAVE_MMAP
munmap((void*) c, l);
#else
free(c);
#endif
close(fd);
return r;
// Clean up on error.
#ifdef HAVE_MMAP
mmap_failed:
#else
read_failed:
free(c);
malloc_failed:
#endif
stat_failed:
close(fd);
open_failed:
return NULL;
}
ihex_recordset_t* ihex_rs_from_mem(const char* data, size_t size)
{
uint_t i = 0;
int r = 0, c = 0;
const char *end = data + size;
ihex_last_errno = 0;
ihex_last_error = NULL;
ihex_record_t *rec;
ihex_recordset_t *recls;
// Count number of record marks in input string.
for (const char *p = data; p < end && *p != 0x00; p ++)
{
if (*p == IHEX_CHR_RECORDMARK)
{
c ++;
}
}
// Allocate memory for the record container structure and for each
// individual record.
if ((rec = (ihex_record_t*) calloc(c, sizeof(ihex_record_t))) == NULL)
{
IHEX_SET_ERROR(IHEX_ERR_MALLOC_FAILED, "Could not allocate memory");
goto malloc_rec_failed;
}
if ((recls = (ihex_recordset_t*) malloc(sizeof(ihex_recordset_t))) == NULL)
{
IHEX_SET_ERROR(IHEX_ERR_MALLOC_FAILED, "Could not allocate memory");
goto malloc_recls_failed;
}
recls->ihrs_count = c;
recls->ihrs_records = rec;
while (data < end && *(data) != 0x00)
{
i ++;
if (data + 3 >= end)
{
break;
}
ihex_rlen_t l = ihex_fromhex8(((ihex_rdata_t) data) + 1);
if (data + 9 + l * 2 >= end ||
(r = ihex_parse_single_record((ihex_rdata_t) data, l, rec + i - 1)) != 0)
{
IHEX_SET_ERROR(r, "Line %i: %s", i, ihex_last_error);
goto parse_single_failed;
}
data += (rec[i - 1].ihr_length * 2) + 10;
while (data < end && *(data) != IHEX_CHR_RECORDMARK && *(data) != 0x00)
{
data ++;
}
}
if (recls->ihrs_records[recls->ihrs_count - 1].ihr_type != IHEX_EOF)
{
IHEX_SET_ERROR(IHEX_ERR_NO_EOF, "Missing EOF record");
goto missing_eof_record;
}
return recls;
missing_eof_record:
for (i = 0; i < recls->ihrs_count; i ++)
{
free(recls->ihrs_records[i].ihr_data);
}
parse_single_failed:
free(recls);
malloc_recls_failed:
free(rec);
malloc_rec_failed:
return NULL;
}
ihex_recordset_t* ihex_rs_from_string(const char* data)
{
return ihex_rs_from_mem(data, strlen(data));
}
static int ihex_parse_single_record(ihex_rdata_t data, unsigned int length, ihex_record_t* record)
{
uint_t i;
// Records needs to begin with record mark (usually ":")
if (*(data) != IHEX_CHR_RECORDMARK)
{
IHEX_SET_ERROR_RETURN(IHEX_ERR_PARSE_ERROR, "Missing record mark");
}
// Record layout:
// 1 2 3 4
// 0 12 3456 78 90123456789012345678901234567890 12
// : 10 0100 00 214601360121470136007EFE09D21901 40
record->ihr_length = (ihex_rlen_t) length;
record->ihr_address = (ihex_addr_t) ihex_fromhex16(data + 3);
record->ihr_type = (ihex_rtype_t) ihex_fromhex8 (data + 7);
record->ihr_checksum = (ihex_rchks_t) ihex_fromhex8 (data + 9 + record->ihr_length * 2);
if ((record->ihr_data = (ihex_rdata_t) malloc(record->ihr_length)) == NULL)
{
IHEX_SET_ERROR_RETURN(IHEX_ERR_MALLOC_FAILED, "Could not allocate memory");
}
// Records needs to end with CRLF or LF.
if ( ( data[11 + record->ihr_length * 2] != 0x0D
|| data[12 + record->ihr_length * 2] != 0x0A)
&& (data[11 + record->ihr_length * 2] != 0x0A))
{
free(record->ihr_data);
IHEX_SET_ERROR_RETURN(IHEX_ERR_WRONG_RECORD_LENGTH, "Incorrect record length");
}
for (i = 0; i < record->ihr_length; i ++)
{
if (data[9 + i*2] == 0x0A || data[9 + i*2] == 0x0D)
{
free(record->ihr_data);
IHEX_SET_ERROR_RETURN(IHEX_ERR_WRONG_RECORD_LENGTH, "Unexpected end of line");
}
record->ihr_data[i] = ihex_fromhex8(data + 9 + i*2);
}
if (ihex_check_record(record) != 0)
{
free(record->ihr_data);
IHEX_SET_ERROR_RETURN(IHEX_ERR_INCORRECT_CHECKSUM, "Checksum validation failed");
}
return 0;
}
int ihex_check_record(ihex_record_t *r)
{
uint_t i;
uint8_t t = 0;
t += r->ihr_length + ((r->ihr_address >> 8) & 0xFF)
+ (r->ihr_address & 0xFF) + r->ihr_type + r->ihr_checksum;
for (i = 0; i < r->ihr_length; i ++)
{
t += r->ihr_data[i];
}
return ((t & 0xFF) == 0) ? 0 : 1;
}
ihex_error_t ihex_errno()
{
return ihex_last_errno;
}
char* ihex_error()
{
return ihex_last_error;
}
void ihex_set_error(ihex_error_t errnum, char* error)
{
ihex_last_errno = errnum;
ihex_last_error = error;
#ifdef IHEX_DEBUG
printf(error);
#endif
}
static inline uint8_t ihex_fromhex4(uint8_t i)
{
if (i >= 0x61 && i <= 0x66) return i - 0x61 + 10;
else if (i >= 0x41 && i <= 0x46) return i - 0x41 + 10;
else if (i >= 0x30 && i <= 0x39) return i - 0x30;
else return 0;
}
uint8_t ihex_fromhex8(uint8_t *i)
{
return (ihex_fromhex4(i[0]) << 4) + ihex_fromhex4(i[1]);
}
uint16_t ihex_fromhex16(uint8_t *i)
{
return (ihex_fromhex4(i[0]) << 12) + (ihex_fromhex4(i[1]) << 8) +
(ihex_fromhex4(i[2]) << 4) + ihex_fromhex4(i[3]);
}