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");
129 ccdbg_hex_error(input, "Missing ':'");
142 ccdbg_hex_error(input, "Non-hex char '%c'",
146 hex = hex << 4 | fromhex(c);
153 record = ccdbg_hex_alloc(hex);
155 ccdbg_hex_error(input, "Out of memory");
158 state = read_address;
162 record->address = hex;
173 record->data[ndata] = hex;
178 record->checksum = hex;
179 state = read_newline;
184 if (state == read_data)
185 if (ndata == record->length) {
187 state = read_checksum;
192 if (c != '\n' && c != '\r') {
193 ccdbg_hex_error(input, "Missing newline");
203 ungetc(c, input->file);
211 checksum = ccdbg_hex_checksum(record);
212 if (checksum != record->checksum) {
213 ccdbg_hex_error(input, "Invalid checksum (read 0x%02x computed 0x%02x)\n",
214 record->checksum, checksum);
220 ccdbg_hex_free(record);
225 ccdbg_hex_file_free(struct hex_file *hex)
231 for (i = 0; i < hex->nrecord; i++)
232 ccdbg_hex_free(hex->records[i]);
237 ccdbg_hex_record_compar(const void *av, const void *bv)
239 const struct hex_record *a = *(struct hex_record **) av;
240 const struct hex_record *b = *(struct hex_record **) bv;
242 return (int) a->address - (int) b->address;
246 ccdbg_hex_file_read(FILE *file, char *name)
248 struct hex_input input;
249 struct hex_file *hex = NULL, *newhex;
250 struct hex_record *record;
254 hex = calloc(sizeof (struct hex_file) + sizeof (struct hex_record *), 1);
259 record = ccdbg_hex_read_record(&input);
262 if (hex->nrecord == srecord) {
264 newhex = realloc(hex,
265 sizeof (struct hex_file) +
266 srecord * sizeof (struct hex_record *));
271 hex->records[hex->nrecord++] = record;
272 if (record->type == HEX_RECORD_EOF)
276 * Sort them into increasing addresses, except for EOF
278 qsort(hex->records, hex->nrecord - 1, sizeof (struct hex_record *),
279 ccdbg_hex_record_compar);
283 ccdbg_hex_file_free(hex);
288 ccdbg_hex_image_create(struct hex_file *hex)
290 struct hex_image *image;
291 struct hex_record *first, *last, *record;
293 uint32_t base, bound;
297 first = hex->records[0];
298 last = hex->records[hex->nrecord - 2]; /* skip EOF */
299 base = (uint32_t) first->address;
300 bound = (uint32_t) last->address + (uint32_t) last->length;
301 length = bound - base;
302 image = calloc(sizeof(struct hex_image) + length, 1);
305 image->address = base;
306 image->length = length;
307 memset(image->data, 0xff, length);
308 for (i = 0; i < hex->nrecord - 1; i++) {
309 record = hex->records[i];
310 offset = record->address - base;
311 memcpy(image->data + offset, record->data, record->length);
317 ccdbg_hex_image_free(struct hex_image *image)
323 ccdbg_hex_image_equal(struct hex_image *a, struct hex_image *b)
325 if (a->length != b->length)
327 if (memcmp(a->data, b->data, a->length) != 0)