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_file_read(FILE *file, char *name)
239 struct hex_input input;
240 struct hex_file *hex = NULL, *newhex;
241 struct hex_record *record;
245 hex = calloc(sizeof (struct hex_file) + sizeof (struct hex_record *), 1);
250 record = ccdbg_hex_read_record(&input);
253 if (hex->nrecord == srecord) {
255 newhex = realloc(hex,
256 sizeof (struct hex_file) +
257 srecord * sizeof (struct hex_record *));
262 hex->records[hex->nrecord++] = record;
263 if (record->type == HEX_RECORD_EOF)
269 ccdbg_hex_file_free(hex);
274 ccdbg_hex_image_create(struct hex_file *hex)
276 struct hex_image *image;
277 struct hex_record *record;
280 uint32_t base, bound;
282 uint32_t extended_addr;
289 for (i = 0; i < hex->nrecord; i++) {
291 record = hex->records[i];
292 switch (record->type) {
294 addr = extended_addr + record->address;
295 r_bound = addr + record->length;
304 if (record->length != 2)
306 extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
309 if (record->length != 2)
311 extended_addr = ((record->data[0] << 8) | record->data[1]) << 16;
316 length = bound - base;
317 image = calloc(sizeof(struct hex_image) + length, 1);
320 image->address = base;
321 image->length = length;
322 memset(image->data, 0xff, length);
324 for (i = 0; i < hex->nrecord; i++) {
325 record = hex->records[i];
326 switch (record->type) {
328 addr = extended_addr + record->address;
329 offset = addr - base;
330 memcpy(image->data + offset, record->data, record->length);
335 extended_addr = ((record->data[0] << 8) | record->data[1]) << 4;
338 extended_addr = ((record->data[0] << 8) | record->data[1]) << 16;
346 ccdbg_hex_image_free(struct hex_image *image)
352 ccdbg_hex_image_equal(struct hex_image *a, struct hex_image *b)
354 if (a->length != b->length)
356 if (memcmp(a->data, b->data, a->length) != 0)
362 ccdbg_hex_load(char *filename)
365 struct hex_file *hex_file;
366 struct hex_image *hex_image;
368 file = fopen (filename, "r");
372 hex_file = ccdbg_hex_file_read(file, filename);
376 hex_image = ccdbg_hex_image_create(hex_file);
379 ccdbg_hex_file_free(hex_file);