ao-tools: Split out altos symbol editing from ao-stmload
authorKeith Packard <keithp@keithp.com>
Thu, 28 Nov 2013 18:31:32 +0000 (10:31 -0800)
committerKeith Packard <keithp@keithp.com>
Thu, 28 Nov 2013 18:31:32 +0000 (10:31 -0800)
to be shared with ao-usbload

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/ao-stmload/ao-stmload.c
ao-tools/lib/Makefile.am
ao-tools/lib/ao-editaltos.c [new file with mode: 0644]
ao-tools/lib/ao-editaltos.h [new file with mode: 0644]

index 7f521bbc58f02c83e52cd0213d9efe727fb797ce..f78626ac91ecdfe0eb0e9411f9e1091c461ebed4 100644 (file)
 #include "ao-stmload.h"
 #include "ao-selfload.h"
 #include "ao-verbose.h"
+#include "ao-editaltos.h"
 
-#define AO_USB_DESC_STRING             3
-
-struct ao_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
-
-int ao_num_symbols = NUM_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 target device with arbitrary
@@ -185,30 +142,6 @@ check_flashed(stlink_t *sl, struct cc_usb *cc)
        return 1;
 }
 
-/*
- * Find the symbols needed to correctly load the program
- */
-
-static bool
-find_symbols(struct ao_sym *file_symbols, int num_file_symbols,
-            struct ao_sym *symbols, int num_symbols)
-{
-       int     f, s;
-
-       for (f = 0; f < num_file_symbols; f++) {
-               for (s = 0; s < num_symbols; s++) {
-                       if (strcmp(symbols[s].name, file_symbols[f].name) == 0) {
-                               symbols[s].addr = file_symbols[f].addr;
-                               symbols[s].found = true;
-                       }
-               }
-       }
-       for (s = 0; s < num_symbols; s++)
-               if (!symbols[s].found && symbols[s].required)
-                       return false;
-       return true;
-}
-
 static const struct option options[] = {
        { .name = "stlink", .has_arg = 0, .val = 'S' },
        { .name = "tty", .has_arg = 1, .val = 'T' },
@@ -328,7 +261,7 @@ main (int argc, char **argv)
        } else
                usage(argv[0]);
 
-       if (!find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols))
+       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) {
@@ -465,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
         */
index 5f47c08984d975e01d32033722749694a2dd3f03..a03a976cd59b3f397790965ee338524975aef925 100644 (file)
@@ -42,6 +42,8 @@ libao_tools_a_SOURCES = \
        cephes.h \
        ao-hex.c \
        ao-hex.h \
+       ao-editaltos.c \
+       ao-editaltos.h \
        ao-elf.c \
        ao-elf.h \
        ao-selfload.c \
diff --git a/ao-tools/lib/ao-editaltos.c b/ao-tools/lib/ao-editaltos.c
new file mode 100644 (file)
index 0000000..a8b6409
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * 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 <string.h>
+#include <stdlib.h>
+#include "ao-editaltos.h"
+
+struct ao_sym ao_symbols[] = {
+
+       { 0, 0, "ao_romconfig_version", 1 },
+       { 0, 0, "ao_romconfig_check",   1 },
+       { 0, 0, "ao_serial_number", 1 },
+       { 0, 0, "ao_radio_cal", 0 },
+       { 0, 0, "ao_usb_descriptors", 0 },
+};
+
+#define NUM_SYMBOLS            5
+
+int ao_num_symbols = NUM_SYMBOLS;
+
+/*
+ * Edit the to-be-written memory block
+ */
+static bool
+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 false;
+
+       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);
+       return true;
+}
+
+/*
+ * Find the symbols needed to correctly load the program
+ */
+
+bool
+ao_editaltos_find_symbols(struct ao_sym *file_symbols, int num_file_symbols,
+                         struct ao_sym *symbols, int num_symbols)
+{
+       int     f, s;
+
+       for (f = 0; f < num_file_symbols; f++) {
+               for (s = 0; s < num_symbols; s++) {
+                       if (strcmp(symbols[s].name, file_symbols[f].name) == 0) {
+                               symbols[s].addr = file_symbols[f].addr;
+                               symbols[s].found = true;
+                       }
+               }
+       }
+       for (s = 0; s < num_symbols; s++)
+               if (!symbols[s].found && symbols[s].required)
+                       return false;
+       return true;
+}
+
+bool
+ao_editaltos(struct ao_hex_image *image,
+            uint16_t serial,
+            uint32_t cal)
+{
+       uint8_t                 *serial_ucs2;
+       int                     serial_ucs2_len;
+       uint8_t                 serial_int[2];
+       unsigned int            s;
+       int                     i;
+       int                     string_num;
+       uint8_t                 cal_int[4];
+
+       /* Write the config values into the flash image
+        */
+
+       serial_int[0] = serial & 0xff;
+       serial_int[1] = (serial >> 8) & 0xff;
+
+       if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
+               fprintf(stderr, "Cannot rewrite serial integer at %08x\n",
+                       AO_SERIAL_NUMBER);
+               return false;
+       }
+
+       if (AO_USB_DESCRIPTORS) {
+               uint32_t        usb_descriptors = AO_USB_DESCRIPTORS - image->address;
+               string_num = 0;
+
+               while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
+                       if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
+                               ++string_num;
+                               if (string_num == 4)
+                                       break;
+                       }
+                       usb_descriptors += image->data[usb_descriptors];
+               }
+               if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
+                       fprintf(stderr, "Cannot rewrite serial string at %08x\n", AO_USB_DESCRIPTORS);
+                       return false;
+               }
+
+               serial_ucs2_len = image->data[usb_descriptors] - 2;
+               serial_ucs2 = malloc(serial_ucs2_len);
+               if (!serial_ucs2) {
+                       fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
+                       return false;
+               }
+               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(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len)) {
+                       fprintf (stderr, "Cannot rewrite USB descriptor at %08x\n", AO_USB_DESCRIPTORS);
+                       return false;
+               }
+       }
+
+       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(image, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
+                       fprintf(stderr, "Cannot rewrite radio calibration at %08x\n", AO_RADIO_CAL);
+                       return false;
+               }
+       }
+       return true;
+}
diff --git a/ao-tools/lib/ao-editaltos.h b/ao-tools/lib/ao-editaltos.h
new file mode 100644 (file)
index 0000000..7453043
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+#ifndef _AO_EDITALTOS_H_
+#define _AO_EDITALTOS_H_
+
+#include <stdint.h>
+#include <stdbool.h>
+#include "ao-hex.h"
+
+extern struct ao_sym ao_symbols[];
+extern int ao_num_symbols;
+
+#define AO_USB_DESC_STRING             3
+
+#define AO_ROMCONFIG_VERSION   (ao_symbols[0].addr)
+#define AO_ROMCONFIG_CHECK     (ao_symbols[1].addr)
+#define AO_SERIAL_NUMBER       (ao_symbols[2].addr)
+#define AO_RADIO_CAL           (ao_symbols[3].addr)
+#define AO_USB_DESCRIPTORS     (ao_symbols[4].addr)
+
+struct ao_editaltos_funcs {
+       uint16_t        (*get_uint16)(void *closure, uint32_t addr);
+       uint32_t        (*get_uint32)(void *closure, uint32_t addr);
+};
+
+bool
+ao_editaltos_find_symbols(struct ao_sym *file_symbols, int num_file_symbols,
+                         struct ao_sym *symbols, int num_symbols);
+
+bool
+ao_editaltos(struct ao_hex_image *image,
+            uint16_t serial,
+            uint32_t radio_cal);
+
+#endif /* _AO_EDITALTOS_H_ */