summaryrefslogtreecommitdiff
path: root/ao-tools/ao-elftohex
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2013-11-28 09:31:02 -0800
committerKeith Packard <keithp@keithp.com>2013-11-28 09:31:02 -0800
commit14204e3d147ad99cc249ad8de254809180fe5c38 (patch)
treeda6dfb4b4d5ea02af596a83748af27080f0dfec7 /ao-tools/ao-elftohex
parentee07f1a0f8e431bebb3b948f6249f5f33413e966 (diff)
ao-tools: Add ao-elftohex and .ihx symbol support
ao-elftohex converts an elf file into a hex file so that we can load it with java. Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'ao-tools/ao-elftohex')
-rw-r--r--ao-tools/ao-elftohex/Makefile.am18
-rw-r--r--ao-tools/ao-elftohex/ao-elftohex.138
-rw-r--r--ao-tools/ao-elftohex/ao-elftohex.c102
3 files changed, 158 insertions, 0 deletions
diff --git a/ao-tools/ao-elftohex/Makefile.am b/ao-tools/ao-elftohex/Makefile.am
new file mode 100644
index 00000000..33c9923f
--- /dev/null
+++ b/ao-tools/ao-elftohex/Makefile.am
@@ -0,0 +1,18 @@
+if LIBSTLINK
+
+bin_PROGRAMS=ao-elftohex
+
+LIBSTLINKDIR=/local/src/stlink
+
+AM_CFLAGS=-I$(top_srcdir)/ao-tools/lib $(LIBSTLINK_CFLAGS) $(LIBUSB_CFLAGS)
+AO_STMLOAD_LIBS=$(top_builddir)/ao-tools/lib/libao-tools.a
+
+ao_elftohex_DEPENDENCIES = $(AO_STMLOAD_LIBS)
+
+ao_elftohex_LDADD=$(AO_STMLOAD_LIBS) $(LIBSTLINK_LIBS) $(LIBUSB_LIBS) -lelf
+
+ao_elftohex_SOURCES=ao-elftohex.c
+
+man_MANS = ao-elftohex.1
+
+endif
diff --git a/ao-tools/ao-elftohex/ao-elftohex.1 b/ao-tools/ao-elftohex/ao-elftohex.1
new file mode 100644
index 00000000..e52e6f5a
--- /dev/null
+++ b/ao-tools/ao-elftohex/ao-elftohex.1
@@ -0,0 +1,38 @@
+.\"
+.\" Copyright © 2013 Keith Packard <keithp@keithp.com>
+.\"
+.\" This program is free software; you can redistribute it and/or modify
+.\" it under the terms of the GNU General Public License as published by
+.\" the Free Software Foundation; either version 2 of the License, or
+.\" (at your option) any later version.
+.\"
+.\" This program is distributed in the hope that it will be useful, but
+.\" WITHOUT ANY WARRANTY; without even the implied warranty of
+.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+.\" General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public License along
+.\" with this program; if not, write to the Free Software Foundation, Inc.,
+.\" 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+.\"
+.\"
+.TH AO-LOAD 1 "ao-elftohex" ""
+.SH NAME
+ao-elftohex \- convert a program to IHX format
+.SH SYNOPSIS
+.B "ao-elftohex"
+[\--output-\fIoutput.ihx\fP]
+[\--verbose]
+\fIinput.elf\fP
+.SH DESCRIPTION
+.I ao-elftohex
+reads the specified .elf file and writes out a .ihx version.
+.SH OPTIONS
+.TP
+\--output=\fIoutput.ihx\fP
+This specifies the output file (default is stdout)
+.TP
+\--verbose
+Dumps some debug information.
+.SH AUTHOR
+Keith Packard
diff --git a/ao-tools/ao-elftohex/ao-elftohex.c b/ao-tools/ao-elftohex/ao-elftohex.c
new file mode 100644
index 00000000..db8f86f1
--- /dev/null
+++ b/ao-tools/ao-elftohex/ao-elftohex.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright © 2013 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <getopt.h>
+#include <stdlib.h>
+#include <string.h>
+#include "ao-hex.h"
+#include "ao-elf.h"
+#include "ao-verbose.h"
+
+static const struct option options[] = {
+ { .name = "verbose", .has_arg = 1, .val = 'v' },
+ { .name = "output", .has_arg = 1, .val = 'o' },
+ { 0, 0, 0, 0},
+};
+
+static void usage(char *program)
+{
+ fprintf(stderr, "usage: %s [--verbose=<level>] [--output=<output.ihx>] <input.elf>\n", program);
+ exit(1);
+}
+
+static int
+ends_with(char *whole, char *suffix)
+{
+ int whole_len = strlen(whole);
+ int suffix_len = strlen(suffix);
+
+ if (suffix_len > whole_len)
+ return 0;
+ return strcmp(whole + whole_len - suffix_len, suffix) == 0;
+}
+
+int
+main (int argc, char **argv)
+{
+ char *input = NULL;
+ char *output = NULL;
+ struct ao_hex_image *image;
+ struct ao_sym *file_symbols;
+ int num_file_symbols;
+ FILE *file;
+ int c;
+
+ while ((c = getopt_long(argc, argv, "v:o:", options, NULL)) != -1) {
+ switch (c) {
+ case 'o':
+ output = optarg;
+ break;
+ case 'v':
+ ao_verbose = (int) strtol(optarg, NULL, 0);
+ break;
+ default:
+ usage(argv[0]);
+ break;
+ }
+ }
+
+ input = argv[optind];
+ if (input == NULL)
+ usage(argv[0]);
+
+ if (ends_with (input, ".ihx"))
+ image = ao_hex_load(input, &file_symbols, &num_file_symbols);
+ else
+ image = ao_load_elf(input, &file_symbols, &num_file_symbols);
+
+ if (!image)
+ usage(argv[0]);
+
+ if (!output)
+ file = stdout;
+ else {
+ file = fopen(output, "w");
+ if (!file) {
+ perror(output);
+ exit(1);
+ }
+ }
+
+ if (!ao_hex_save(file, image, file_symbols, num_file_symbols)) {
+ fprintf(stderr, "%s: failed to write hex file\n", output ? output : "<stdout>");
+ if (output)
+ unlink(output);
+ exit(1);
+ }
+ exit(0);
+}