ao-elftohex: Allow multiple elf files to be merged together
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #include <getopt.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include "ao-hex.h"
24 #include "ao-elf.h"
25 #include "ao-verbose.h"
26
27 static const struct option options[] = {
28         { .name = "verbose", .has_arg = 1, .val = 'v' },
29         { .name = "output", .has_arg = 1, .val = 'o' },
30         { 0, 0, 0, 0},
31 };
32
33 static void usage(char *program)
34 {
35         fprintf(stderr, "usage: %s [--verbose=<level>] [--output=<output.ihx>] <input.elf>\n", program);
36         exit(1);
37 }
38
39 static int
40 ends_with(char *whole, char *suffix)
41 {
42         int whole_len = strlen(whole);
43         int suffix_len = strlen(suffix);
44
45         if (suffix_len > whole_len)
46                 return 0;
47         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
48 }
49
50 int
51 main (int argc, char **argv)
52 {
53         char                    *input = NULL;
54         char                    *output = NULL;
55         struct ao_hex_image     *full_image = NULL;
56         struct ao_sym           *file_symbols;
57         int                     num_file_symbols;
58         FILE                    *file;
59         int                     c;
60         int                     i;
61
62         while ((c = getopt_long(argc, argv, "v:o:", options, NULL)) != -1) {
63                 switch (c) {
64                 case 'o':
65                         output = optarg;
66                         break;
67                 case 'v':
68                         ao_verbose = (int) strtol(optarg, NULL, 0);
69                         break;
70                 default:
71                         usage(argv[0]);
72                         break;
73                 }
74         }
75
76         if (optind >= argc)
77                 usage(argv[0]);
78
79         for (i = optind; i < argc; i++) {
80                 struct ao_hex_image *image;
81
82                 input = argv[i];
83
84                 if (ends_with (input, ".ihx"))
85                         image = ao_hex_load(input, &file_symbols, &num_file_symbols);
86                 else
87                         image = ao_load_elf(input, &file_symbols, &num_file_symbols);
88
89                 if (!image) {
90                         fprintf(stderr, "Failed to load %s\n", input);
91                         usage(argv[0]);
92                 }
93
94                 if (full_image) {
95                         full_image = ao_hex_image_cat(full_image, image);
96                         if (!full_image) {
97                                 fprintf(stderr, "Can't merge image %s\n", input);
98                                 usage(argv[0]);
99                         }
100                 } else
101                         full_image = image;
102         }
103
104         if (!output)
105                 file = stdout;
106         else {
107                 file = fopen(output, "w");
108                 if (!file) {
109                         perror(output);
110                         exit(1);
111                 }
112         }
113
114         if (!ao_hex_save(file, full_image, file_symbols, num_file_symbols)) {
115                 fprintf(stderr, "%s: failed to write hex file\n", output ? output : "<stdout>");
116                 if (output)
117                         unlink(output);
118                 exit(1);
119         }
120         exit(0);
121 }