-
Notifications
You must be signed in to change notification settings - Fork 88
/
mdloader_parser.c
397 lines (343 loc) · 11.1 KB
/
mdloader_parser.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (C) 2018 Massdrop Inc.
*
* This file is part of Massdrop Loader.
*
* Massdrop Loader 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 3 of the License, or
* (at your option) any later version.
*
* Massdrop Loader 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 Massdrop Loader. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mdloader_common.h"
#include "mdloader_parser.h"
void free_data(data_t *data)
{
if (!data)
{
printf("Error: Parser: Attempt to free NULL data!\n");
return;
}
if (data->data)
{
free(data->data);
data->data = NULL;
}
free(data);
data = NULL;
}
data_t *create_data(uint32_t data_length)
{
data_t *data = (data_t *)malloc(sizeof(data_t));
if (!data)
{
printf("ERROR: Parser: Could not allocate parser memory!\n");
return NULL;
}
data->data = (char *)malloc(data_length);
if (!data->data)
{
printf("ERROR: Parser: Could not allocate parser data memory!\n");
free_data(data);
return NULL;
}
return data;
}
//Assuming validity checks on incoming variables have been completed prior to call
data_t *parse_bin(char *rawdata, uint32_t rawlength)
{
if (!rawdata)
{
printf("ERROR: Parser: Bin: Raw data null!\n");
return NULL;
}
if (!rawlength)
{
printf("ERROR: Parser: Bin: Raw data length zero!\n");
return NULL;
}
data_t *data = create_data(rawlength);
if (data)
{
memcpy(data->data, rawdata, rawlength);
data->size = rawlength;
data->addr = mcu->flash_addr + bootloader_length;
}
else
printf("Error: Parser: Bin: Error creating parser storage!\n");
return data;
}
char hex_conv_error; //Cleared on conversion attempt and set to 1 if read fails
//Must check hex_conv_error != 0 for conversion error after call
char ascii_to_hex(char bh, char bl)
{
hex_conv_error = 0;
if (bh >= '0' && bh <= '9') bh -= '0';
else if (bh >= 'A' && bh <= 'F') bh -= 'A' - 0xA;
else if (bh >= 'a' && bh <= 'a') bh -= 'a' - 0xA;
else
{
hex_conv_error = 1;
return 0;
}
if (bl >= '0' && bl <= '9') bl -= '0';
else if (bl >= 'A' && bl <= 'F') bl -= 'A' - 0xA;
else if (bl >= 'a' && bl <= 'a') bl -= 'a' - 0xA;
else
{
hex_conv_error = 1;
return 0;
}
return (bh << 4) | bl;
}
//Assuming validity checks on incoming variables have been completed prior to call
data_t *parse_hex(char *rawdata, uint32_t rawlength)
{
if (!rawdata)
{
printf("ERROR: Parser: Hex: Raw data null!\n");
return NULL;
}
if (!rawlength)
{
printf("ERROR: Parser: Hex: Raw data length zero!\n");
return NULL;
}
uint8_t first_address_set = 0;
uint32_t base_address = 0;
uint32_t start_offset;
uint8_t *rds = (uint8_t *)rawdata;
uint8_t *rde = (uint8_t *)rawdata + rawlength;
uint8_t *bindata = (uint8_t *)rawdata;
hex_record_t *hex;
uint8_t *hex_data;
uint8_t checksum;
uint8_t checksum_given;
uint16_t line = 0;
uint32_t binlength = 0;
uint8_t byte_count;
uint32_t next_address = 0;
uint32_t start_segment_address = 0;
uint8_t start_segment_address_set = 0;
while (rds < rde)
{
line++;
if (rde - rds < sizeof(hex_record_t))
{
printf("Error: Parser: Hex: Unexpected end of header! (Line %i)\n",line);
return NULL;
}
hex = (hex_record_t *)rds;
if (hex->start_code != ':')
{
printf("Error: Parser: Hex: Invalid start code! (Line %i)\n",line);
return NULL;
}
checksum = 0;
//Convert byte count to safe storage
byte_count = ascii_to_hex(*hex->byte_count, *(hex->byte_count+1));
if (hex_conv_error)
{
printf("Error: Parser: Hex: Unexpected ASCII in byte count! (Line %i)\n",line);
return NULL;
}
checksum += byte_count;
//Convert record type in place
*hex->record_type = ascii_to_hex(*hex->record_type, *(hex->record_type+1));
if (hex_conv_error)
{
printf("Error: Parser: Hex: Unexpected ASCII in record type! (Line %i)\n",line);
return NULL;
}
checksum += *hex->record_type;
//Convert address in place
*hex->address.c = ascii_to_hex(*(hex->address.c+0), *(hex->address.c+1));
if (hex_conv_error)
{
printf("Error: Parser: Hex: Unexpected ASCII in address byte 1! (Line %i)\n",line);
return NULL;
}
checksum += *hex->address.c;
*(hex->address.c+1) = ascii_to_hex(*(hex->address.c+2), *(hex->address.c+3));
if (hex_conv_error)
{
printf("Error: Parser: Hex: Unexpected ASCII in address byte 2! (Line %i)\n",line);
return NULL;
}
checksum += *(hex->address.c+1);
hex->address.i = (*hex->address.c << 8) + *(hex->address.c+1);
//Check enough data remains to parse
if (rde - rds < sizeof(hex_record_t) + (byte_count * 2) + 2)
{
printf("Error: Parser: Hex: Malformed data! (Line %i)\n",line);
return NULL;
}
hex_data = rds + sizeof(hex_record_t);
//Convert data in place
for (start_offset = 0; start_offset < byte_count * 2; start_offset += 2)
{
*(hex_data + start_offset / 2) = ascii_to_hex(*(hex_data + start_offset), *(hex_data + start_offset + 1));
if (hex_conv_error)
{
printf("Error: Parser: Hex: Unexpected ASCII in data byte! (Line %i)\n",line);
return NULL;
}
checksum += *(hex_data + start_offset / 2);
}
checksum *= -1;
//Convert checksum
checksum_given = ascii_to_hex(*(hex_data + start_offset), *(hex_data + start_offset + 1));
if (hex_conv_error)
{
printf("Error: Parser: Hex: Unexpected ASCII in checksum byte! (Line %i)\n",line);
return NULL;
}
if (checksum != checksum_given)
{
printf("Error: Parser: Hex: Checksum mismatch! (Line %i)\n",line);
return NULL;
}
//Print hex line
//printf("%c ",hex->start_code);
//printf("%02X ",byte_count);
//printf("%04X ",hex->address.i);
//for (start_offset = 0; start_offset < byte_count; start_offset++)
// printf("%02X",*(hex_data + start_offset));
//if (byte_count) printf(" ");
//printf("%02X\n",checksum);
if (*hex->record_type == 0) //Data
{
if (!first_address_set)
{
first_address_set = 1;
next_address = base_address + hex->address.i;
}
if (hex->address.i + base_address != next_address)
{
printf("Error: Parser: Hex: Address not contiguous! (Line %i)\n",line);
return NULL;
}
//Write binary data into rawdata buffer (write position will never reach read position)
binlength += byte_count;
for (start_offset = 0; start_offset < byte_count; start_offset++)
{
*bindata = *(hex_data + start_offset);
bindata++;
}
next_address = base_address + ((next_address + byte_count) & 0xFFFF);
}
else if (*hex->record_type == 1) //EOF
{
break; //No need to go further
}
else if (*hex->record_type == 2) //Extended Segment Address
{
base_address = ((*hex_data << 8) + *(hex_data+1)) << 16;
next_address += base_address;
}
else if (*hex->record_type == 3) //Start Segment Address
{
start_segment_address = ((*hex_data << 24) + (*(hex_data+1) << 16) + (*(hex_data+2) << 8) + (*(hex_data+3)));
start_segment_address_set = 1;
}
else if (*hex->record_type == 4) //Extended Linear Address
{
printf("Error: Parser: Hex: 32-bit addressing is not supported! (Line %i)\n",line);
return NULL;
}
else if (*hex->record_type == 5) //Start Linear Address
{
printf("Error: Parser: Hex: Start linear address is not supported! (Line %i)\n",line);
return NULL;
}
else
{
printf("Error: Parser: Hex: Unknown record type! (Line %i)\n",line);
return NULL;
}
rds += sizeof(hex_record_t) + (byte_count * 2) + 2; //Bypass header, data, checksum
while (rds < rde && (*rds == '\r' || *rds == '\n')) rds++; //Bypass EOL characters
}
if (!start_segment_address_set)
{
printf("Error: Parser: Hex: Missing start segment address!\n");
return NULL;
}
data_t *data = create_data(binlength);
if (data)
{
memcpy(data->data, rawdata, binlength);
data->size = binlength;
data->addr = start_segment_address;
}
else
printf("Error: Parser: Hex: Error creating parser storage!\n");
return data;
}
char get_type_by_ext(char *fname)
{
char *pext = fname + (strlen(fname) - 4);
if (!strcmp(pext,EXT_HEX)) return FTYPE_HEX;
else if (!strcmp(pext,EXT_BIN)) return FTYPE_BIN;
else return FTYPE_NONE;
}
data_t *load_file(char *fname)
{
if (!fname)
{
printf("ERROR: Parser: No file given!\n");
return NULL;
}
if (strlen(fname) < 5)
{
printf("ERROR: Parser: File name must end in %s or %s!\n", EXT_HEX, EXT_BIN);
return NULL;
}
char ftype = get_type_by_ext(fname);
if (ftype == FTYPE_NONE)
{
printf("ERROR: Parser: File name must end in %s or %s!\n", EXT_HEX, EXT_BIN);
return NULL;
}
uint32_t fsize = filesize(fname);
if (fsize == 0)
{
printf("ERROR: Parser: File is empty!\n");
return NULL;
}
FILE *fIn = fopen(fname, "rb");
if (!fIn)
{
printf("ERROR: Parser: Could not open file for read!\n");
return NULL;
}
char *rawdata = (char *)malloc(fsize);
if (!rawdata)
{
printf("ERROR: Parser: Could no allocated file memory buffer!\n");
fclose(fIn);
return NULL;
}
uint32_t readbytes = (uint32_t)fread(rawdata, 1, fsize, fIn);
fclose(fIn);
if (readbytes != fsize)
{
printf("ERROR: Parser: File read size mismatch!\n");
free(rawdata);
return NULL;
}
if (ftype == FTYPE_HEX)
return parse_hex(rawdata, readbytes);
else if (ftype == FTYPE_BIN)
return parse_bin(rawdata, readbytes);
else
printf("ERROR: Parser: Unknown file type!\n");
return NULL;
}