ao-tools/ao-test-gps: Improve output formatting
[fw/altos] / ao-tools / ao-usbload / ao-usbload.c
index 9e32b2b9ee10fa779dd85e8a34d7f29c251a2dce..fd34fbdcfad8f21f7c0443a89fe312f4c500527e 100644 (file)
@@ -82,6 +82,7 @@ check_flashed(struct cc_usb *cc)
 static const struct option options[] = {
        { .name = "tty", .has_arg = 1, .val = 'T' },
        { .name = "device", .has_arg = 1, .val = 'D' },
+       { .name = "raw", .has_arg = 0, .val = 'r' },
        { .name = "cal", .has_arg = 1, .val = 'c' },
        { .name = "serial", .has_arg = 1, .val = 's' },
        { .name = "verbose", .has_arg = 1, .val = 'v' },
@@ -90,7 +91,7 @@ static const struct option options[] = {
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
+       fprintf(stderr, "usage: %s [--raw] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
        exit(1);
 }
 
@@ -119,6 +120,7 @@ main (int argc, char **argv)
        char                    *device = NULL;
        char                    *filename;
        Elf                     *e;
+       int                     raw = 0;
        char                    *serial_end;
        unsigned int            serial = 0;
        char                    *serial_ucs2;
@@ -140,8 +142,10 @@ main (int argc, char **argv)
        int                     verbose = 0;
        struct ao_sym           *file_symbols;
        int                     num_file_symbols;
+       uint32_t                flash_base, flash_bound;
+       int                     has_flash_size = 0;
 
-       while ((c = getopt_long(argc, argv, "T:D:c:s:v:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "rT:D:c:s:v:", options, NULL)) != -1) {
                switch (c) {
                case 'T':
                        tty = optarg;
@@ -149,6 +153,9 @@ main (int argc, char **argv)
                case 'D':
                        device = optarg;
                        break;
+               case 'r':
+                       raw = 1;
+                       break;
                case 'c':
                        cal = strtoul(optarg, &cal_end, 10);
                        if (cal_end == optarg || *cal_end != '\0')
@@ -184,8 +191,12 @@ main (int argc, char **argv)
        } 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 (!raw) {
+               if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
+                       fprintf(stderr, "Cannot find required symbols\n");
+                       usage(argv[0]);
+               }
+       }
 
        {
                int     is_loader;
@@ -196,7 +207,7 @@ main (int argc, char **argv)
                        if (!this_tty)
                                this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
                        if (!this_tty)
-                               this_tty = cc_usbdevs_find_by_arg(device, "MegaMetrum");
+                               this_tty = cc_usbdevs_find_by_arg(device, "TeleMega");
                        if (!this_tty)
                                this_tty = getenv("ALTOS_TTY");
                        if (!this_tty)
@@ -213,6 +224,14 @@ main (int argc, char **argv)
                                cc_usb_getline(cc, line, sizeof(line));
                                if (!strncmp(line, "altos-loader", 12))
                                        is_loader = 1;
+                               if (!strncmp(line, "flash-range", 11)) {
+                                       int i;
+                                       for (i = 11; i < strlen(line); i++)
+                                               if (line[i] != ' ')
+                                                       break;
+                                       if (sscanf(line + i, "%x %x", &flash_base, &flash_bound) == 2)
+                                               has_flash_size = 1;
+                               }
                                if (!strncmp(line, "software-version", 16))
                                        break;
                        }
@@ -253,38 +272,56 @@ main (int argc, char **argv)
 #endif
        }
 
-       /* Go fetch existing config values
-        * if available
+       /* If the device can tell us the size of flash, make sure
+        * the image fits in that
         */
-       was_flashed = check_flashed(cc);
-
-       if (!serial) {
-               if (!was_flashed) {
-                       fprintf (stderr, "Must provide serial number\n");
-                       done(cc, 1);
-               }
-               serial = get_uint16(cc, AO_SERIAL_NUMBER);
-               if (!serial || serial == 0xffff) {
-                       fprintf (stderr, "Invalid existing serial %d\n", serial);
+       if (has_flash_size) {
+               if (load->address < flash_base ||
+                   load->address + load->length > flash_bound)
+               {
+                       fprintf(stderr, "Image does not fit on device.\n");
+                       fprintf(stderr, "  Image base:  %08x bounds %08x\n",
+                               load->address, load->address + load->length);
+                       fprintf(stderr, "  Device base: %08x bounds %08x\n",
+                               flash_base, flash_bound);
                        done(cc, 1);
                }
        }
 
-       if (!cal && AO_RADIO_CAL && was_flashed) {
-               cal = get_uint32(cc, AO_RADIO_CAL);
-               if (!cal || cal == 0xffffffff) {
-                       fprintf (stderr, "Invalid existing rf cal %d\n", cal);
-                       done(cc, 1);
+       if (!raw) {
+               /* Go fetch existing config values
+                * if available
+                */
+               was_flashed = check_flashed(cc);
+
+               if (!serial) {
+                       if (!was_flashed) {
+                               fprintf (stderr, "Must provide serial number\n");
+                               done(cc, 1);
+                       }
+                       serial = get_uint16(cc, AO_SERIAL_NUMBER);
+                       if (!serial || serial == 0xffff) {
+                               fprintf (stderr, "Invalid existing serial %d\n", serial);
+                               done(cc, 1);
+                       }
                }
-       }
 
-       if (!ao_editaltos(load, serial, cal))
-               done(cc, 1);
+               if (!cal && AO_RADIO_CAL && was_flashed) {
+                       cal = get_uint32(cc, AO_RADIO_CAL);
+                       if (!cal || cal == 0xffffffff) {
+                               fprintf (stderr, "Invalid existing rf cal %d\n", cal);
+                               done(cc, 1);
+                       }
+               }
+
+               if (!ao_editaltos(load, serial, cal))
+                       done(cc, 1);
+       }
 
        /* And flash the resulting image to the device
         */
        success = ao_self_write(cc, load);
-               
+
        if (!success) {
                fprintf (stderr, "\"%s\": Write failed\n", filename);
                done(cc, 1);