altoslib: When flashing hardware, pull USB data from device if needed
[fw/altos] / altoslib / AltosSelfFlash.java
index 051aa76698f420df39e9e943b1076db79c132f31..70a5ced5e8db0ffc551946e0740813fc4824cc2d 100644 (file)
@@ -3,7 +3,8 @@
  *
  * 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.
+ * 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
@@ -15,7 +16,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altoslib_3;
+package org.altusmetrum.altoslib_13;
 
 import java.io.*;
 
@@ -50,12 +51,26 @@ public class AltosSelfFlash extends AltosProgrammer {
 
                        if (reply == null)
                                throw new IOException("Read device memory timeout");
-                       for (b = 0; b < len; b++)
+                       for (b = 0; b < 0x100 && b + offset < len; b++)
                                data[b+offset] = reply[b];
                }
                return data;
        }
 
+       AltosHexfile read_hexfile(long addr, int len) throws InterruptedException {
+               try {
+                       byte[] mem = read_memory(addr, len);
+
+                       AltosHexfile    hexfile = new AltosHexfile(mem, addr);
+
+                       if (image != null)
+                               hexfile.add_symbols(image);
+                       return hexfile;
+               } catch (IOException ie) {
+                       return null;
+               }
+       }
+
        void write_memory(long addr, byte[] data, int start, int len) {
                int b;
                link.printf("W %x\n", addr);
@@ -85,7 +100,7 @@ public class AltosSelfFlash extends AltosProgrammer {
                        long flash_addr = image.address;
                        int image_start = 0;
 
-                       action("start", 0);
+                       action(AltosFlashListener.flash_start, 0);
                        action(0, image.data.length);
                        while (remain > 0 && !aborted) {
                                int this_time = remain;
@@ -113,7 +128,7 @@ public class AltosSelfFlash extends AltosProgrammer {
                                action(image.data.length - remain, image.data.length);
                        }
                        if (!aborted) {
-                               action("done", 100);
+                               action(AltosFlashListener.flash_done, 100);
                        }
                        close();
                } catch (IOException ie) {
@@ -142,18 +157,15 @@ public class AltosSelfFlash extends AltosProgrammer {
 
        private AltosHexfile get_rom() throws InterruptedException {
                try {
-                       int base = AltosRomconfig.fetch_base(image);
-                       int bounds = AltosRomconfig.fetch_bounds(image);
-                       byte[] data = read_memory(base, bounds - base);
-                       AltosHexfile hexfile = new AltosHexfile(data, base);
-                       hexfile.add_symbols(image);
-                       return hexfile;
-               } catch (AltosNoSymbol none) {
-                       return null;
-               } catch (IOException ie) {
+                       long base = AltosRomconfig.fetch_base(image);
+                       long bounds = AltosRomconfig.fetch_bounds(image);
+
+                       if (link.debug)
+                               System.out.printf("rom base %x bounds %x\n", base, bounds);
+                       return read_hexfile(base, (int) (bounds - base));
+               } catch (AltosNoSymbol ns) {
                        return null;
                }
-
        }
 
        public boolean check_rom_config() throws InterruptedException {
@@ -172,12 +184,20 @@ public class AltosSelfFlash extends AltosProgrammer {
                rom_config = romconfig;
        }
 
-       public AltosRomconfig romconfig() throws InterruptedException {
+       public AltosRomconfig target_romconfig(AltosUsbId usb_id, String usb_product) throws InterruptedException {
                if (!check_rom_config())
                        return null;
+               if (rom_config.usb_id == null)
+                       rom_config.usb_id = usb_id;
+               if (rom_config.usb_product == null)
+                       rom_config.usb_product = usb_product;
                return rom_config;
        }
 
+       public AltosRomconfig image_romconfig() {
+               return new AltosRomconfig(image);
+       }
+
        public AltosSelfFlash(File file, AltosLink link, AltosFlashListener listener)
                throws IOException, FileNotFoundException, InterruptedException {
                this.file = file;
@@ -186,4 +206,4 @@ public class AltosSelfFlash extends AltosProgrammer {
                input = new FileInputStream(file);
                image = new AltosHexfile(input);
        }
-}
\ No newline at end of file
+}