-
Notifications
You must be signed in to change notification settings - Fork 183
/
ihex.c
190 lines (172 loc) · 6.35 KB
/
ihex.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
/***************************************************************************
* Intel hex read and write utility functions *
* *
* Copyright (c) Valentin Dudouyt, 2004 2012 - 2014 *
* Copyright (c) Philipp Klaus Krause, 2021 *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ihex.h"
#define HI(x) (((x) & 0xff00) >> 8)
#define LO(x) ((x) & 0xff)
static unsigned char checksum(unsigned char *buf, unsigned int length, int chunk_len, int chunk_addr, int chunk_type) {
int sum = chunk_len + (LO(chunk_addr)) + (HI(chunk_addr)) + chunk_type;
int i;
for(i = 0; i < length; i++) {
sum += buf[i];
}
int complement = (~sum + 1);
return(complement & 0xff);
}
int ihex_read(FILE *pFile, unsigned char *buf, unsigned int start, unsigned int end) {
// a record in intel hex looks like
// :LLXXXXTTDDDD....DDCC
// Where LL is the length of the data field and can be as large as 255. So the maximum size of a record is 9 characters for the
// header, 255 * 2 characters for the data, 2 characters for the Checksum, and an additional 2 characters (possibly a Carriage
// Return and linefeed. This is then a total an 9 + 510 + 2 + 2 = 523.
static char line[523];
fseek(pFile, 0, SEEK_SET);
unsigned int chunk_len, chunk_addr, chunk_type, i, byte, line_no = 0, greatest_addr = 0, offset = 0;
while(fgets(line, sizeof(line), pFile)) {
line_no++;
// Strip off Carriage Return at end of line if it exists.
if (line[strlen(line)] == '\r')
{
line[strlen(line)] = 0;
}
// Reading chunk header
if(sscanf(line, ":%02x%04x%02x", &chunk_len, &chunk_addr, &chunk_type) != 3) {
free(buf);
fprintf(stderr, "Error while parsing IHEX at line %d\n", line_no);
return(-1);
}
// Reading chunk data
if(chunk_type == 2) // Extended Segment Address
{
unsigned int esa;
if(sscanf(&line[9],"%04x",&esa) != 1) {
free(buf);
fprintf(stderr, "Error while parsing IHEX at line %d\n", line_no);
return(-1);
}
offset = esa * 16;
}
if(chunk_type == 4) // Extended Linear Address
{
unsigned int ela;
if(sscanf(&line[9],"%04x",&ela) != 1) {
free(buf);
fprintf(stderr, "Error while parsing IHEX at line %d\n", line_no);
return(-1);
}
offset = ela * 65536;
}
for(i = 9; i < strlen(line) - 1; i +=2) {
if(sscanf(&(line[i]), "%02x", &byte) != 1) {
free(buf);
fprintf(stderr, "Error while parsing IHEX at line %d byte %d\n", line_no, i);
return(-1);
}
if(chunk_type != 0x00) {
// The only data records have to be processed
break;
}
if((i - 9) / 2 >= chunk_len) {
// Respect chunk_len and do not capture checksum as data
break;
}
if((chunk_addr + offset) < start) {
free(buf);
fprintf(stderr, "Address %04x is out of range at line %d\n", chunk_addr, line_no);
return(-1);
}
if(chunk_addr + offset + chunk_len > end) {
free(buf);
fprintf(stderr, "Address %04x + %d is out of range at line %d\n", chunk_addr, chunk_len, line_no);
return(-1);
}
if(chunk_addr + offset + chunk_len > greatest_addr) {
greatest_addr = chunk_addr + offset + chunk_len;
}
buf[chunk_addr + offset - start + (i - 9) / 2] = byte;
}
}
return(greatest_addr - start);
}
int ihex_write(FILE *pFile, unsigned char *buf, unsigned int start, unsigned int end) {
unsigned int chunk_len, chunk_start, i;
int cur_ela = 0;
// If any address is above the first 64k, cause an initial ELA record to be written
if (end > 65535)
{
cur_ela = -1;
}
chunk_start = start;
while(chunk_start < end)
{
// Assume the rest of the bytes will be written in this chunk
chunk_len = end - chunk_start;
// Limit the length to 32 bytes per chunk
if (chunk_len > 32)
{
chunk_len = 32;
}
// Check if length would go past the end of the current 64k block
if (((chunk_start & 0xffff) + chunk_len) > 0xffff)
{
// If so, reduce the length to go only to the end of the block
chunk_len = 0x10000 - (chunk_start & 0xffff);
}
// See if the last ELA record that was written matches the current block
if (cur_ela != (chunk_start >> 16))
{
// If not, write an ELA record
unsigned char ela_bytes[2];
cur_ela = chunk_start >> 16;
ela_bytes[0] = HI(cur_ela);
ela_bytes[1] = LO(cur_ela);
if(fprintf(pFile, ":02000004%04X%02X\n",cur_ela,checksum(ela_bytes,2,2,0,4)) < 0)
{
fprintf(stderr, "I/O error during IHEX write\n");
return(-1);
}
}
// Write the data record
fprintf(pFile, ":%02X%04X00",chunk_len,chunk_start & 0xffff);
for(i = chunk_start - start; i < (chunk_start + chunk_len - start); i++)
if(fprintf(pFile, "%02X",buf[i]) < 0)
{
fprintf(stderr, "I/O error during IHEX write\n");
return(-1);
}
if(fprintf(pFile, "%02X\n", checksum( &buf[chunk_start - start], chunk_len, chunk_len, chunk_start, 0)) < 0)
{
fprintf(stderr, "I/O error during IHEX write\n");
return(-1);
}
chunk_start += chunk_len;
}
// Add the END record
if(fprintf(pFile,":00000001FF\n") < 0)
{
fprintf(stderr, "I/O error during IHEX write\n");
return(-1);
}
return(0);
}