ao-tools: Split out altos symbol editing from ao-stmload
[fw/altos] / ao-tools / ao-stmload / ao-stmload.c
index 6e3906fd691a1f40f10e29ffdddd4a02254c58db..f78626ac91ecdfe0eb0e9411f9e1091c461ebed4 100644 (file)
 #include <unistd.h>
 #include <getopt.h>
 #include <string.h>
+#include <stdbool.h>
 #include "stlink-common.h"
 #include "ao-elf.h"
 #include "ccdbg.h"
 #include "cc-usb.h"
 #include "cc.h"
 #include "ao-stmload.h"
+#include "ao-selfload.h"
+#include "ao-verbose.h"
+#include "ao-editaltos.h"
 
-#define AO_USB_DESC_STRING             3
-
-struct ao_elf_sym ao_symbols[] = {
-
-       { 0, AO_BOOT_APPLICATION_BASE + 0x100,  "ao_romconfig_version", 1 },
-#define AO_ROMCONFIG_VERSION   (ao_symbols[0].addr)
-
-       { 0, AO_BOOT_APPLICATION_BASE + 0x102,  "ao_romconfig_check",   1 },
-#define AO_ROMCONFIG_CHECK     (ao_symbols[1].addr)
-
-       { 0, AO_BOOT_APPLICATION_BASE + 0x104,  "ao_serial_number", 1 },
-#define AO_SERIAL_NUMBER       (ao_symbols[2].addr)
-
-       { 0, AO_BOOT_APPLICATION_BASE + 0x108,  "ao_radio_cal", 0 },
-#define AO_RADIO_CAL           (ao_symbols[3].addr)
-
-       { 0, AO_BOOT_APPLICATION_BASE + 0x10c,  "ao_usb_descriptors", 0 },
-#define AO_USB_DESCRIPTORS     (ao_symbols[4].addr)
-};
-
-#define NUM_SYMBOLS            5
-#define NUM_REQUIRED_SYMBOLS   3
-
-int ao_num_symbols = NUM_SYMBOLS;
-int ao_num_required_symbols = NUM_REQUIRED_SYMBOLS;
-
-/*
- * Edit the to-be-written memory block
- */
-static int
-rewrite(struct ao_hex_image *load, unsigned address, uint8_t *data, int length)
-{
-       int             i;
-
-       if (address < load->address || load->address + load->length < address + length)
-               return 0;
-
-       printf("rewrite %04x:", address);
-       for (i = 0; i < length; i++)
-               printf (" %02x", load->data[address - load->address + i]);
-       printf(" ->");
-       for (i = 0; i < length; i++)
-               printf (" %02x", data[i]);
-       printf("\n");
-       memcpy(load->data + address - load->address, data, length);
-}
-
-/*
- * Read a 16-bit value from the USB target
- */
-
-static uint16_t
-get_uint16_cc(struct cc_usb *cc, uint32_t addr)
-{
-       struct ao_hex_image     *hex = ao_self_read(cc, addr, 2);
-       uint16_t                v;
-       uint8_t                 *data;
-
-       if (!hex)
-               return 0;
-       data = hex->data + addr - hex->address;
-       v = data[0] | (data[1] << 8);
-       free(hex);
-       return v;
-}
-
-static uint32_t
-get_uint32_cc(struct cc_usb *cc, uint32_t addr)
-{
-       struct ao_hex_image     *hex = ao_self_read(cc, addr, 4);
-       uint32_t                v;
-       uint8_t                 *data;
-
-       if (!hex)
-               return 0;
-       data = hex->data + addr - hex->address;
-       v = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
-       free(hex);
-       return v;
-}
 
 /*
  * Read a 16-bit value from the target device with arbitrary
@@ -145,7 +69,7 @@ get_uint16(stlink_t *sl, struct cc_usb *cc, uint32_t addr)
 {
        uint16_t        result;
        if (cc)
-               result = get_uint16_cc(cc, addr);
+               result = ao_self_get_uint16(cc, addr);
        else
                result = get_uint16_sl(sl, addr);
        printf ("read 0x%08x = 0x%04x\n", addr, result);
@@ -190,7 +114,7 @@ get_uint32(stlink_t *sl, struct cc_usb *cc, uint32_t addr)
        uint32_t        result;
 
        if (cc)
-               result = get_uint32_cc(cc, addr);
+               result = ao_self_get_uint32(cc, addr);
        else
                result = get_uint32_sl(sl, addr);
        printf ("read 0x%08x = 0x%08x\n", addr, result);
@@ -224,13 +148,13 @@ static const struct option options[] = {
        { .name = "device", .has_arg = 1, .val = 'D' },
        { .name = "cal", .has_arg = 1, .val = 'c' },
        { .name = "serial", .has_arg = 1, .val = 's' },
-       { .name = "verbose", .has_arg = 0, .val = 'v' },
+       { .name = "verbose", .has_arg = 1, .val = 'v' },
        { 0, 0, 0, 0},
 };
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--stlink] [--verbose] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
+       fprintf(stderr, "usage: %s [--stlink] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
        exit(1);
 }
 
@@ -288,8 +212,10 @@ main (int argc, char **argv)
        char                    *tty = NULL;
        int                     success;
        int                     verbose = 0;
+       struct ao_sym           *file_symbols;
+       int                     num_file_symbols;
 
-       while ((c = getopt_long(argc, argv, "T:D:c:s:Sv", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "T:D:c:s:Sv:", options, NULL)) != -1) {
                switch (c) {
                case 'T':
                        tty = optarg;
@@ -319,7 +245,7 @@ main (int argc, char **argv)
                }
        }
 
-       ao_self_verbose = verbose;
+       ao_verbose = verbose;
 
        if (verbose > 1)
                ccdbg_add_debug(CC_DEBUG_BITBANG);
@@ -329,15 +255,15 @@ main (int argc, char **argv)
                usage(argv[0]);
 
        if (ends_with (filename, ".elf")) {
-               load = ao_load_elf(filename, ao_symbols, ao_num_symbols);
+               load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
        } else if (ends_with (filename, ".ihx")) {
-               int     i;
-               load = ao_hex_load(filename);
-               for (i = 0; i < ao_num_symbols; i++)
-                       ao_symbols[i].addr = ao_symbols[i].default_addr;
+               load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
        } else
                usage(argv[0]);
 
+       if (ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols))
+               fprintf(stderr, "Cannot find required symbols\n");
+
        if (use_stlink) {
                /* Connect to the programming dongle
                 */
@@ -472,64 +398,8 @@ main (int argc, char **argv)
                }
        }
 
-       /* Write the config values into the flash image
-        */
-
-       serial_int[0] = serial & 0xff;
-       serial_int[1] = (serial >> 8) & 0xff;
-
-       if (!rewrite(load, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
-               fprintf(stderr, "Cannot rewrite serial integer at %08x\n",
-                       AO_SERIAL_NUMBER);
+       if (!ao_editaltos(load, serial, cal))
                done(sl, cc, 1);
-       }
-
-       if (AO_USB_DESCRIPTORS) {
-               uint32_t        usb_descriptors = AO_USB_DESCRIPTORS - load->address;
-               string_num = 0;
-
-               while (load->data[usb_descriptors] != 0 && usb_descriptors < load->length) {
-                       if (load->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
-                               ++string_num;
-                               if (string_num == 4)
-                                       break;
-                       }
-                       usb_descriptors += load->data[usb_descriptors];
-               }
-               if (usb_descriptors >= load->length || load->data[usb_descriptors] == 0 ) {
-                       fprintf(stderr, "Cannot rewrite serial string at %08x\n", AO_USB_DESCRIPTORS);
-                       done(sl, cc, 1);
-               }
-
-               serial_ucs2_len = load->data[usb_descriptors] - 2;
-               serial_ucs2 = malloc(serial_ucs2_len);
-               if (!serial_ucs2) {
-                       fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
-                       done(sl, cc, 1);
-               }
-               s = serial;
-               for (i = serial_ucs2_len / 2; i; i--) {
-                       serial_ucs2[i * 2 - 1] = 0;
-                       serial_ucs2[i * 2 - 2] = (s % 10) + '0';
-                       s /= 10;
-               }
-               if (!rewrite(load, usb_descriptors + 2 + load->address, serial_ucs2, serial_ucs2_len)) {
-                       fprintf (stderr, "Cannot rewrite USB descriptor at %08x\n", AO_USB_DESCRIPTORS);
-                       done(sl, cc, 1);
-               }
-       }
-
-       if (cal && AO_RADIO_CAL) {
-               cal_int[0] = cal & 0xff;
-               cal_int[1] = (cal >> 8) & 0xff;
-               cal_int[2] = (cal >> 16) & 0xff;
-               cal_int[3] = (cal >> 24) & 0xff;
-
-               if (!rewrite(load, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
-                       fprintf(stderr, "Cannot rewrite radio calibration at %08x\n", AO_RADIO_CAL);
-                       exit(1);
-               }
-       }
 
        /* And flash the resulting image to the device
         */