ao-elftohex: Allow multiple elf files to be merged together
authorKeith Packard <keithp@keithp.com>
Mon, 19 Sep 2022 16:09:04 +0000 (09:09 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 19 Sep 2022 16:19:13 +0000 (09:19 -0700)
This allows combining the flash loader and core firmware.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/ao-elftohex/ao-elftohex.1
ao-tools/ao-elftohex/ao-elftohex.c

index e52e6f5a5625ac1fc7b39e0129d484f2b06ebbc7..0c1666c8cf2849fc04dabbd603554ce34fc14982 100644 (file)
 .\"
 .TH AO-LOAD 1 "ao-elftohex" ""
 .SH NAME
-ao-elftohex \- convert a program to IHX format
+ao-elftohex \- convert programs to IHX format
 .SH SYNOPSIS
 .B "ao-elftohex"
 [\--output-\fIoutput.ihx\fP]
 [\--verbose]
-\fIinput.elf\fP
+\fIinput.elf ...\fP
 .SH DESCRIPTION
 .I ao-elftohex
-reads the specified .elf file and writes out a .ihx version.
+reads the specified .elf files and writes out a .ihx version.
 .SH OPTIONS
 .TP
 \--output=\fIoutput.ihx\fP
index f3ab0c38ca4a1fdeff140e14a553979b1aeecff6..7afd29c321ff5bc3b0e7d29d265c0c155cc605ee 100644 (file)
@@ -52,11 +52,12 @@ main (int argc, char **argv)
 {
        char                    *input = NULL;
        char                    *output = NULL;
-       struct ao_hex_image     *image;
+       struct ao_hex_image     *full_image = NULL;
        struct ao_sym           *file_symbols;
        int                     num_file_symbols;
        FILE                    *file;
        int                     c;
+       int                     i;
 
        while ((c = getopt_long(argc, argv, "v:o:", options, NULL)) != -1) {
                switch (c) {
@@ -72,17 +73,33 @@ main (int argc, char **argv)
                }
        }
 
-       input = argv[optind];
-       if (input == NULL)
+       if (optind >= argc)
                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);
+       for (i = optind; i < argc; i++) {
+               struct ao_hex_image *image;
 
-       if (!image)
-               usage(argv[0]);
+               input = argv[i];
+
+               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) {
+                       fprintf(stderr, "Failed to load %s\n", input);
+                       usage(argv[0]);
+               }
+
+               if (full_image) {
+                       full_image = ao_hex_image_cat(full_image, image);
+                       if (!full_image) {
+                               fprintf(stderr, "Can't merge image %s\n", input);
+                               usage(argv[0]);
+                       }
+               } else
+                       full_image = image;
+       }
 
        if (!output)
                file = stdout;
@@ -94,7 +111,7 @@ main (int argc, char **argv)
                }
        }
 
-       if (!ao_hex_save(file, image, file_symbols, num_file_symbols)) {
+       if (!ao_hex_save(file, full_image, file_symbols, num_file_symbols)) {
                fprintf(stderr, "%s: failed to write hex file\n", output ? output : "<stdout>");
                if (output)
                        unlink(output);