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.
43 ccdbg_hex_error(struct hex_input *input, char *format, ...)
48 fprintf(stderr, "Hex error %s:%d: ", input->name, input->line);
49 vfprintf(stderr, format, ap);
50 fprintf(stderr, "\n");
55 ccdbg_hex_free(struct hex_record *record)
61 static struct hex_record *
62 ccdbg_hex_alloc(uint8_t length)
64 struct hex_record *record;
66 record = calloc(1, sizeof(struct hex_record) + length);
67 record->length = length;
74 return isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
82 if ('a' <= c && c <= 'f')
84 if ('A' <= c && c <= 'F')
91 ccdbg_hex_checksum(struct hex_record *record)
96 checksum += record->length;
97 checksum += record->address >> 8;
98 checksum += record->address & 0xff;
99 checksum += record->type;
100 for (i = 0; i < record->length; i++)
101 checksum += record->data[i];
105 static struct hex_record *
106 ccdbg_hex_read_record(struct hex_input *input)
108 struct hex_record *record = NULL;
109 enum hex_read_state state = read_marker;
116 while (state != read_done) {
117 c = getc(input->file);
118 if (c == EOF && state != read_white) {
119 ccdbg_hex_error(input, "Unexpected EOF");
127 ccdbg_hex_error(input, "Missing ':'");
140 ccdbg_hex_error(input, "Non-hex char '%c'",
144 hex = hex << 4 | fromhex(c);
151 record = ccdbg_hex_alloc(hex);
153 ccdbg_hex_error(input, "Out of memory");
156 state = read_address;
160 record->address = hex;
171 record->data[ndata] = hex;
176 record->checksum = hex;
177 state = read_newline;
182 if (state == read_data)
183 if (ndata == record->length) {
185 state = read_checksum;
190 if (c != '\n' && c != '\r') {
191 ccdbg_hex_error(input, "Missing newline");
201 ungetc(c, input->file);
209 checksum = ccdbg_hex_checksum(record);
210 if (checksum != record->checksum) {
211 ccdbg_hex_error(input, "Invalid checksum (read 0x%02x computed 0x%02x)\n",
212 record->checksum, checksum);
218 ccdbg_hex_free(record);
223 ccdbg_hex_file_free(struct hex_file *hex)
229 for (i = 0; i < hex->nrecord; i++)
230 ccdbg_hex_free(hex->records[i]);
235 ccdbg_hex_record_compar(const void *av, const void *bv)
237 const struct hex_record *a = *(struct hex_record **) av;
238 const struct hex_record *b = *(struct hex_record **) bv;
240 return (int) a->address - (int) b->address;
244 ccdbg_hex_file_read(FILE *file, char *name)
246 struct hex_input input;
247 struct hex_file *hex = NULL, *newhex;
248 struct hex_record *record;
252 hex = calloc(sizeof (struct hex_file) + sizeof (struct hex_record *), 1);
257 record = ccdbg_hex_read_record(&input);
260 if (hex->nrecord == srecord) {
262 newhex = realloc(hex,
263 sizeof (struct hex_file) +
264 srecord * sizeof (struct hex_record *));
269 hex->records[hex->nrecord++] = record;
270 if (record->type == HEX_RECORD_EOF)
274 * Sort them into increasing addresses, except for EOF
276 qsort(hex->records, hex->nrecord - 1, sizeof (struct hex_record *),
277 ccdbg_hex_record_compar);
281 ccdbg_hex_file_free(hex);
286 ccdbg_hex_image_create(struct hex_file *hex)
288 struct hex_image *image;
289 struct hex_record *first, *last, *record;
291 uint32_t base, bound;
294 first = hex->records[0];
295 last = hex->records[hex->nrecord - 2]; /* skip EOF */
296 base = (uint32_t) first->address;
297 bound = (uint32_t) last->address + (uint32_t) last->length - 1;
298 image = calloc(sizeof(struct hex_image) + bound - base, 1);
301 image->address = base;
302 image->length = bound - base;
303 memset(image->data, 0xff, image->length);
304 for (i = 0; i < hex->nrecord - 1; i++) {
305 record = hex->records[i];
306 offset = record->address - base;
307 memcpy(image->data + offset, record->data, record->length);
313 ccdbg_hex_image_free(struct hex_image *image)
319 ccdbg_hex_image_equal(struct hex_image *a, struct hex_image *b)
321 if (a->length != b->length)
323 if (memcmp(a->data, b->data, a->length) != 0)