2 * Copyright © 2016 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; version 2 of the License.
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.
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.
27 static const struct option options[] = {
28 { .name = "verbose", .has_arg = 0, .val = 'v' },
29 { .name = "output", .has_arg = 1, .val = 'o' },
30 { .name = "base", .has_arg = 1, .val = 'b' },
31 { .name = "align", .has_arg = 1, .val = 'a' },
32 { .name = "dfu", .has_arg = 0, .val = 'd' },
36 static void usage(char *program)
38 fprintf(stderr, "usage: %s [--verbose=<level>] [--output=<output.bin>] [--base=<base-address>] [--align=<align>] [--dfu] <input.elf> ...\n", program);
43 ends_with(char *whole, char *suffix)
45 int whole_len = strlen(whole);
46 int suffix_len = strlen(suffix);
48 if (suffix_len > whole_len)
50 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
53 static struct ao_dfu_info dfu_info = {
60 main (int argc, char **argv)
63 struct ao_hex_image *image = NULL;
64 struct ao_sym *file_symbols;
68 uint32_t base = 0xffffffff;
74 while ((c = getopt_long(argc, argv, "dvo:b:a:", options, NULL)) != -1) {
83 base = strtoul(optarg, NULL, 0);
86 align = strtoul(optarg, NULL, 0);
97 while (argv[optind]) {
98 char *input = argv[optind];
99 struct ao_hex_image *tmp;
101 if (ends_with (input, ".ihx"))
102 tmp = ao_hex_load(input, &file_symbols, &num_file_symbols);
104 tmp = ao_load_elf(input, &file_symbols, &num_file_symbols);
110 fprintf(stderr, "%s: 0x%x %d\n", input, tmp->address, tmp->length);
113 image = ao_hex_image_cat(image, tmp);
121 if (base != 0xffffffff && base > image->address) {
122 fprintf(stderr, "requested base 0x%x is after image address 0x%x\n",
123 base, image->address);
128 fprintf(stderr, "%s: base 0x%x length %d\n", output ? output : "<stdout>", image->address, image->length);
133 file = fopen(output, "w");
141 if (!ao_dfu_write(file, &dfu_info, 1, image)) {
142 fprintf(stderr, "%s: dfu_write failed: %s\n", output, strerror(errno));
148 while (base < image->address) {
153 if (fwrite(image->data, 1, image->length, file) != image->length) {
154 fprintf(stderr, "%s: failed to write bin file\n", output ? output : "<stdout>");
161 length = image->length;
163 while (length % align) {