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; 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.
28 static const struct option options[] = {
29 { .name = "verbose", .has_arg = 0, .val = 'v' },
30 { .name = "output", .has_arg = 1, .val = 'o' },
31 { .name = "base", .has_arg = 1, .val = 'b' },
32 { .name = "align", .has_arg = 1, .val = 'a' },
33 { .name = "dfu", .has_arg = 0, .val = 'd' },
37 static void usage(char *program)
39 fprintf(stderr, "usage: %s [--verbose=<level>] [--output=<output.bin>] [--base=<base-address>] [--align=<align>] [--dfu] <input.elf> ...\n", program);
44 ends_with(char *whole, char *suffix)
46 int whole_len = strlen(whole);
47 int suffix_len = strlen(suffix);
49 if (suffix_len > whole_len)
51 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
54 static struct ao_dfu_info dfu_info = {
61 main (int argc, char **argv)
64 struct ao_hex_image *image = NULL;
65 struct ao_sym *file_symbols;
69 uint32_t base = 0xffffffff;
75 while ((c = getopt_long(argc, argv, "dvo:b:a:", options, NULL)) != -1) {
84 base = strtoul(optarg, NULL, 0);
87 align = strtoul(optarg, NULL, 0);
98 while (argv[optind]) {
99 char *input = argv[optind];
100 struct ao_hex_image *tmp;
102 if (ends_with (input, ".ihx"))
103 tmp = ao_hex_load(input, &file_symbols, &num_file_symbols);
105 tmp = ao_load_elf(input, &file_symbols, &num_file_symbols);
111 fprintf(stderr, "%s: 0x%x %d\n", input, tmp->address, tmp->length);
114 image = ao_hex_image_cat(image, tmp);
122 if (base != 0xffffffff && base > image->address) {
123 fprintf(stderr, "requested base 0x%x is after image address 0x%x\n",
124 base, image->address);
129 fprintf(stderr, "%s: base 0x%x length %d\n", output ? output : "<stdout>", image->address, image->length);
134 file = fopen(output, "w");
142 if (!ao_dfu_write(file, &dfu_info, 1, image)) {
143 fprintf(stderr, "%s: dfu_write failed: %s\n", output, strerror(errno));
149 while (base < image->address) {
154 if (fwrite(image->data, 1, image->length, file) != image->length) {
155 fprintf(stderr, "%s: failed to write bin file\n", output ? output : "<stdout>");
162 length = image->length;
164 while (length % align) {