ao-tools: Add ao-elftohex and .ihx symbol support
[fw/altos] / ao-tools / ao-elftohex / ao-elftohex.c
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 #include <getopt.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "ao-hex.h"
22 #include "ao-elf.h"
23 #include "ao-verbose.h"
24
25 static const struct option options[] = {
26         { .name = "verbose", .has_arg = 1, .val = 'v' },
27         { .name = "output", .has_arg = 1, .val = 'o' },
28         { 0, 0, 0, 0},
29 };
30
31 static void usage(char *program)
32 {
33         fprintf(stderr, "usage: %s [--verbose=<level>] [--output=<output.ihx>] <input.elf>\n", program);
34         exit(1);
35 }
36
37 static int
38 ends_with(char *whole, char *suffix)
39 {
40         int whole_len = strlen(whole);
41         int suffix_len = strlen(suffix);
42
43         if (suffix_len > whole_len)
44                 return 0;
45         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
46 }
47
48 int
49 main (int argc, char **argv)
50 {
51         char                    *input = NULL;
52         char                    *output = NULL;
53         struct ao_hex_image     *image;
54         struct ao_sym           *file_symbols;
55         int                     num_file_symbols;
56         FILE                    *file;
57         int                     c;
58
59         while ((c = getopt_long(argc, argv, "v:o:", options, NULL)) != -1) {
60                 switch (c) {
61                 case 'o':
62                         output = optarg;
63                         break;
64                 case 'v':
65                         ao_verbose = (int) strtol(optarg, NULL, 0);
66                         break;
67                 default:
68                         usage(argv[0]);
69                         break;
70                 }
71         }
72
73         input = argv[optind];
74         if (input == NULL)
75                 usage(argv[0]);
76
77         if (ends_with (input, ".ihx"))
78                 image = ao_hex_load(input, &file_symbols, &num_file_symbols);
79         else
80                 image = ao_load_elf(input, &file_symbols, &num_file_symbols);
81
82         if (!image)
83                 usage(argv[0]);
84
85         if (!output)
86                 file = stdout;
87         else {
88                 file = fopen(output, "w");
89                 if (!file) {
90                         perror(output);
91                         exit(1);
92                 }
93         }
94
95         if (!ao_hex_save(file, image, file_symbols, num_file_symbols)) {
96                 fprintf(stderr, "%s: failed to write hex file\n", output ? output : "<stdout>");
97                 if (output)
98                         unlink(output);
99                 exit(1);
100         }
101         exit(0);
102 }