2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
32 enum ao_hex_read_state {
46 ao_hex_error(struct ao_hex_input *input, char *format, ...)
51 fprintf(stderr, "Hex error %s:%d: ", input->name, input->line);
52 vfprintf(stderr, format, ap);
53 fprintf(stderr, "\n");
58 ao_hex_free(struct ao_hex_record *record)
64 static struct ao_hex_record *
65 ao_hex_alloc(uint8_t length)
67 struct ao_hex_record *record;
69 record = calloc(1, sizeof(struct ao_hex_record) + length);
70 record->length = length;
77 return isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
85 if ('a' <= c && c <= 'f')
87 if ('A' <= c && c <= 'F')
94 ao_hex_checksum(struct ao_hex_record *record)
99 checksum += record->length;
100 checksum += record->address >> 8;
101 checksum += record->address & 0xff;
102 checksum += record->type;
103 for (i = 0; i < record->length; i++)
104 checksum += record->data[i];
108 static struct ao_hex_record *
109 ao_hex_read_record(struct ao_hex_input *input)
111 struct ao_hex_record *record = NULL;
112 enum ao_hex_read_state state = read_marker;
119 while (state != read_done) {
120 c = getc(input->file);
121 if (c == EOF && state != read_white) {
122 ao_hex_error(input, "Unexpected EOF");
132 ao_hex_error(input, "Missing ':'");
145 ao_hex_error(input, "Non-hex char '%c'",
149 hex = hex << 4 | fromhex(c);
156 record = ao_hex_alloc(hex);
158 ao_hex_error(input, "Out of memory");
161 state = read_address;
165 record->address = hex;
176 record->data[ndata] = hex;
181 record->checksum = hex;
182 state = read_newline;
187 if (state == read_data)
188 if (ndata == record->length) {
190 state = read_checksum;
195 if (c != '\n' && c != '\r') {
196 ao_hex_error(input, "Missing newline");
206 ungetc(c, input->file);
214 checksum = ao_hex_checksum(record);
215 if (checksum != record->checksum) {
216 ao_hex_error(input, "Invalid checksum (read 0x%02x computed 0x%02x)\n",
217 record->checksum, checksum);
228 ao_hex_file_free(struct ao_hex_file *hex)
234 for (i = 0; i < hex->nrecord; i++)
235 ao_hex_free(hex->records[i]);
240 ao_hex_file_read(FILE *file, char *name)
242 struct ao_hex_input input;
243 struct ao_hex_file *hex = NULL, *newhex;
244 struct ao_hex_record *record;
248 hex = calloc(sizeof (struct ao_hex_file) + sizeof (struct ao_hex_record *), 1);
253 record = ao_hex_read_record(&input);
256 if (hex->nrecord == srecord) {
258 newhex = realloc(hex,
259 sizeof (struct ao_hex_file) +
260 srecord * sizeof (struct ao_hex_record *));
265 hex->records[hex->nrecord++] = record;
266 if (record->type == AO_HEX_RECORD_EOF)
272 ao_hex_file_free(hex);
276 struct ao_hex_image *
277 ao_hex_image_create(struct ao_hex_file *hex)
279 struct ao_hex_image *image;
280 struct ao_hex_record *record;
283 uint32_t base, bound;
285 uint32_t extended_addr;
292 for (i = 0; i < hex->nrecord; i++) {
294 record = hex->records[i];
295 switch (record->type) {
297 addr = extended_addr + record->address;
298 r_bound = addr + record->length;
307 if (record->length != 2)
309 extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
312 if (record->length != 2)
314 extended_addr = ((record->data[0] << 8) | record->data[1]) << 16;
319 length = bound - base;
320 image = calloc(sizeof(struct ao_hex_image) + length, 1);
323 image->address = base;
324 image->length = length;
325 memset(image->data, 0xff, length);
327 for (i = 0; i < hex->nrecord; i++) {
328 record = hex->records[i];
329 switch (record->type) {
331 addr = extended_addr + record->address;
332 offset = addr - base;
333 memcpy(image->data + offset, record->data, record->length);
338 extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
341 extended_addr = ((record->data[0] << 8) | record->data[1]) << 16;
349 ao_hex_image_free(struct ao_hex_image *image)
355 ao_hex_image_equal(struct ao_hex_image *a, struct ao_hex_image *b)
357 if (a->length != b->length)
359 if (memcmp(a->data, b->data, a->length) != 0)
364 struct ao_hex_image *
365 ao_hex_load(char *filename)
368 struct ao_hex_file *hex_file;
369 struct ao_hex_image *hex_image;
371 file = fopen (filename, "r");
375 hex_file = ao_hex_file_read(file, filename);
379 hex_image = ao_hex_image_create(hex_file);
382 ao_hex_file_free(hex_file);