ao-tools: Add ao-elftohex and .ihx symbol support
[fw/altos] / ao-tools / lib / ao-hex.h
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #ifndef _AO_HEX_H_
19 #define _AO_HEX_H_
20
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24
25 #define AO_HEX_RECORD_NORMAL                    0x00
26 #define AO_HEX_RECORD_EOF                       0x01
27 #define AO_HEX_RECORD_EXTENDED_ADDRESS_4        0x02
28 #define AO_HEX_RECORD_EXTENDED_ADDRESS_8        0x04
29 #define AO_HEX_RECORD_SYMBOL                    0xfe
30
31 /* Intel hex file format data
32  */
33 struct ao_hex_record {
34         uint8_t length;
35         uint16_t address;
36         uint8_t type;
37         uint8_t checksum;
38         uint8_t data[0];
39 };
40
41 struct ao_hex_file {
42         int                     nrecord;
43         struct ao_hex_record    *records[0];
44 };
45
46 struct ao_hex_image {
47         uint32_t        address;
48         uint32_t        length;
49         uint8_t         data[0];
50 };
51
52 struct ao_sym {
53         unsigned        addr;
54         unsigned        default_addr;
55         char            *name;
56         bool            required;
57         bool            found;
58 };
59
60 struct ao_hex_file *
61 ao_hex_file_read(FILE *file, char *name);
62
63 void
64 ao_hex_file_free(struct ao_hex_file *hex);
65
66 struct ao_hex_image *
67 ao_hex_image_create(struct ao_hex_file *hex);
68
69 void
70 ao_hex_image_free(struct ao_hex_image *image);
71
72 struct ao_hex_image *
73 ao_hex_load(char *filename, struct ao_sym **symbols, int *num_symbols);
74
75 int
76 ao_hex_image_equal(struct ao_hex_image *a, struct ao_hex_image *b);
77
78 bool
79 ao_hex_save(FILE *file, struct ao_hex_image *image,
80             struct ao_sym *symbols, int num_symbols);
81
82 #endif /* _AO_HEX_H_ */