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.
26 #include "ao-verbose.h"
34 enum ao_hex_read_state {
48 ao_hex_error(struct ao_hex_input *input, char *format, ...)
53 fprintf(stderr, "Hex error %s:%d: ", input->name, input->line);
54 vfprintf(stderr, format, ap);
55 fprintf(stderr, "\n");
60 ao_hex_free(struct ao_hex_record *record)
66 static struct ao_hex_record *
67 ao_hex_alloc(uint8_t length)
69 struct ao_hex_record *record;
71 record = calloc(1, sizeof(struct ao_hex_record) + length);
72 record->length = length;
79 return isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
87 if ('a' <= c && c <= 'f')
89 if ('A' <= c && c <= 'F')
96 ao_hex_checksum(struct ao_hex_record *record)
101 checksum += record->length;
102 checksum += record->address >> 8;
103 checksum += record->address & 0xff;
104 checksum += record->type;
105 for (i = 0; i < record->length; i++)
106 checksum += record->data[i];
110 static struct ao_hex_record *
111 ao_hex_read_record(struct ao_hex_input *input)
113 struct ao_hex_record *record = NULL;
114 enum ao_hex_read_state state = read_marker;
121 while (state != read_done) {
122 c = getc(input->file);
123 if (c == EOF && state != read_white && state != read_marker) {
124 ao_hex_error(input, "Unexpected EOF");
136 ao_hex_error(input, "Missing ':'");
149 ao_hex_error(input, "Non-hex char '%c'",
153 hex = hex << 4 | fromhex(c);
160 record = ao_hex_alloc(hex);
162 ao_hex_error(input, "Out of memory");
165 state = read_address;
169 record->address = hex;
180 record->data[ndata] = hex;
185 record->checksum = hex;
186 state = read_newline;
191 if (state == read_data)
192 if (ndata == record->length) {
194 state = read_checksum;
199 if (c != '\n' && c != '\r') {
200 ao_hex_error(input, "Missing newline");
210 ungetc(c, input->file);
218 checksum = ao_hex_checksum(record);
219 if (checksum != record->checksum) {
220 ao_hex_error(input, "Invalid checksum (read 0x%02x computed 0x%02x)\n",
221 record->checksum, checksum);
232 ao_hex_file_free(struct ao_hex_file *hex)
238 for (i = 0; i < hex->nrecord; i++)
239 ao_hex_free(hex->records[i]);
244 ao_hex_file_read(FILE *file, char *name)
246 struct ao_hex_input input;
247 struct ao_hex_file *hex = NULL, *newhex;
248 struct ao_hex_record *record;
252 hex = calloc(sizeof (struct ao_hex_file) + sizeof (struct ao_hex_record *), 1);
259 record = ao_hex_read_record(&input);
261 if (feof(input.file)) {
267 if (hex->nrecord == srecord) {
269 newhex = realloc(hex,
270 sizeof (struct ao_hex_file) +
271 srecord * sizeof (struct ao_hex_record *));
276 hex->records[hex->nrecord++] = record;
281 ao_hex_file_free(hex);
285 static struct ao_sym *
286 load_symbols(struct ao_hex_file *hex,
289 uint32_t extended_addr;
292 struct ao_hex_record *record;
293 struct ao_sym *symbols = NULL;
294 struct ao_sym *symbol;
296 int size_symbols = 0;
299 for (i = 0; i < hex->nrecord; i++) {
300 record = hex->records[i];
301 switch (record->type) {
302 case AO_HEX_RECORD_NORMAL:
303 addr = extended_addr + record->address;
305 case AO_HEX_RECORD_EOF:
307 case AO_HEX_RECORD_EXTENDED_ADDRESS_4:
308 if (record->length != 2)
310 extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
312 case AO_HEX_RECORD_EXTENDED_ADDRESS_8:
313 if (record->length != 2)
315 extended_addr = (record->data[0] << 24) | (record->data[1] << 16);
317 case AO_HEX_RECORD_SYMBOL:
318 addr = extended_addr + record->address;
319 if (num_symbols == size_symbols) {
320 struct ao_sym *new_symbols;
326 new_size = size_symbols * 2;
327 new_symbols = realloc(symbols, new_size * sizeof (struct ao_sym));
331 symbols = new_symbols;
332 size_symbols = new_size;
334 symbol = &symbols[num_symbols];
335 memset(symbol, 0, sizeof (struct ao_sym));
336 symbol->name = calloc(record->length + 1, 1);
339 memcpy(symbol->name, record->data, record->length);
341 ao_printf(AO_VERBOSE_EXE, "Add symbol %s: %08x\n", symbol->name, symbol->addr);
346 *num_symbolsp = num_symbols;
349 for (i = 0; i < num_symbols; i++)
350 free(symbols[i].name);
356 ao_hex_record_set_checksum(struct ao_hex_record *record)
361 cksum += record->length;
362 cksum += record->address >> 8;
363 cksum += record->address;
364 cksum += record->type;
365 for (i = 0; i < record->length; i++)
366 cksum += record->data[i];
368 record->checksum = -cksum;
371 struct ao_hex_image *
372 ao_hex_image_create(struct ao_hex_file *hex)
374 struct ao_hex_image *image;
375 struct ao_hex_record *record;
378 uint32_t base, bound;
380 uint32_t extended_addr;
384 /* Find the address bounds of the file
389 for (i = 0; i < hex->nrecord; i++) {
391 record = hex->records[i];
392 switch (record->type) {
393 case AO_HEX_RECORD_NORMAL:
394 addr = extended_addr + record->address;
395 r_bound = addr + record->length;
401 case AO_HEX_RECORD_EOF:
403 case AO_HEX_RECORD_EXTENDED_ADDRESS_4:
404 if (record->length != 2)
406 extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
408 case AO_HEX_RECORD_EXTENDED_ADDRESS_8:
409 if (record->length != 2)
411 extended_addr = (record->data[0] << 24) | (record->data[1] << 16);
413 case AO_HEX_RECORD_SYMBOL:
417 length = bound - base;
418 image = calloc(sizeof(struct ao_hex_image) + length, 1);
421 image->address = base;
422 image->length = length;
423 memset(image->data, 0xff, length);
425 for (i = 0; i < hex->nrecord; i++) {
426 record = hex->records[i];
427 switch (record->type) {
428 case AO_HEX_RECORD_NORMAL:
429 addr = extended_addr + record->address;
430 offset = addr - base;
431 memcpy(image->data + offset, record->data, record->length);
433 case AO_HEX_RECORD_EOF:
435 case AO_HEX_RECORD_EXTENDED_ADDRESS_4:
436 extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
438 case AO_HEX_RECORD_EXTENDED_ADDRESS_8:
439 extended_addr = (record->data[0] << 24) | (record->data[1] << 16);
441 case AO_HEX_RECORD_SYMBOL:
449 ao_hex_image_free(struct ao_hex_image *image)
454 uint32_t min(uint32_t a, uint32_t b) { return a < b ? a : b; }
455 uint32_t max(uint32_t a, uint32_t b) { return a > b ? a : b; }
457 struct ao_hex_image *
458 ao_hex_image_cat(struct ao_hex_image *a, struct ao_hex_image *b)
460 struct ao_hex_image *n;
461 uint32_t base, bound;
464 base = min(a->address, b->address);
465 bound = max(a->address + a->length, b->address + b->length);
466 length = bound - base;
468 n = calloc (sizeof (struct ao_hex_image) + length, 1);
473 memset(n->data, 0xff, length);
474 memcpy(n->data + a->address - n->address, a->data, a->length);
475 memcpy(n->data + b->address - n->address, b->data, b->length);
480 ao_hex_image_equal(struct ao_hex_image *a, struct ao_hex_image *b)
482 if (a->length != b->length)
484 if (memcmp(a->data, b->data, a->length) != 0)
489 struct ao_hex_image *
490 ao_hex_load(char *filename, struct ao_sym **symbols, int *num_symbolsp)
493 struct ao_hex_file *hex_file;
494 struct ao_hex_image *hex_image;
496 file = fopen (filename, "r");
500 hex_file = ao_hex_file_read(file, filename);
504 hex_image = ao_hex_image_create(hex_file);
509 *symbols = load_symbols(hex_file, num_symbolsp);
511 ao_hex_file_free(hex_file);
515 #define BYTES_PER_RECORD 32
517 static struct ao_hex_file *
518 ao_hex_file_create(struct ao_hex_image *image, struct ao_sym *symbols, int num_symbols)
520 /* split data into n-byte-sized chunks */
521 uint32_t data_records = (image->length + BYTES_PER_RECORD-1) / BYTES_PER_RECORD;
522 /* extended address and data for each block, EOF, address and data for each symbol */
523 uint32_t total_records = data_records * 2 + 1 + num_symbols * 2;
528 struct ao_hex_file *hex_file;
531 struct ao_hex_record *record;
533 hex_file = calloc(sizeof (struct ao_hex_file) + sizeof (struct ao_hex_record *) * total_records, 1);
539 for (offset = 0; offset < image->length; offset += BYTES_PER_RECORD) {
540 uint32_t address = image->address + offset;
541 uint32_t length = image->length - offset;
543 if (length > BYTES_PER_RECORD)
544 length = BYTES_PER_RECORD;
546 record = calloc(sizeof (struct ao_hex_record) + 2, 1);
547 record->type = AO_HEX_RECORD_EXTENDED_ADDRESS_8;
550 record->data[0] = address >> 24;
551 record->data[1] = address >> 16;
552 ao_hex_record_set_checksum(record);
554 hex_file->records[nrecord++] = record;
556 record = calloc(sizeof (struct ao_hex_record) + length, 1);
557 record->type = AO_HEX_RECORD_NORMAL;
558 record->address = address;
559 record->length = length;
560 memcpy(record->data, image->data + offset, length);
561 ao_hex_record_set_checksum(record);
563 hex_file->records[nrecord++] = record;
566 /* Stick an EOF after the data
568 record = calloc(sizeof (struct ao_hex_record), 1);
569 record->type = AO_HEX_RECORD_EOF;
574 ao_hex_record_set_checksum(record);
576 hex_file->records[nrecord++] = record;
581 for (s = 0; s < num_symbols; s++) {
583 name = symbols[s].name;
584 address = symbols[s].addr;
585 length = strlen (name);
587 record = calloc(sizeof (struct ao_hex_record) + 2, 1);
588 record->type = AO_HEX_RECORD_EXTENDED_ADDRESS_8;
591 record->data[0] = address >> 24;
592 record->data[1] = address >> 16;
593 ao_hex_record_set_checksum(record);
595 hex_file->records[nrecord++] = record;
597 record = calloc(sizeof (struct ao_hex_record) + length, 1);
598 record->type = AO_HEX_RECORD_SYMBOL;
599 record->address = address;
600 record->length = length;
601 memcpy(record->data, name, length);
602 ao_hex_record_set_checksum(record);
604 hex_file->records[nrecord++] = record;
607 hex_file->nrecord = nrecord;
612 ao_hex_write_record(FILE *file, struct ao_hex_record *record)
617 fprintf(file, "%02x", record->length);
618 fprintf(file, "%04x", record->address);
619 fprintf(file, "%02x", record->type);
620 for (i = 0; i < record->length; i++)
621 fprintf(file, "%02x", record->data[i]);
622 fprintf(file, "%02x", record->checksum);
628 ao_hex_save(FILE *file, struct ao_hex_image *image,
629 struct ao_sym *symbols, int num_symbols)
631 struct ao_hex_file *hex_file;
635 hex_file = ao_hex_file_create(image, symbols, num_symbols);
639 for (i = 0; i < hex_file->nrecord; i++) {
640 if (!ao_hex_write_record(file, hex_file->records[i]))
645 if (fflush(file) != 0)
648 ao_hex_file_free(hex_file);