altoslib: Reduce debug printf spamming during flashing operation
[fw/altos] / altoslib / AltosRomconfig.java
index 7d9cc09611ebc9412918abc573980627d658d4c5..ccd01274427b9520bb4622a532cf2db4b5523deb 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_2;
+package org.altusmetrum.altoslib_13;
 
 import java.io.*;
 
@@ -25,20 +26,27 @@ public class AltosRomconfig {
        public int      check;
        public int      serial_number;
        public int      radio_calibration;
+       public AltosUsbId       usb_id;
+       public String           usb_product;
 
-       static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
+       static private long find_address(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
                AltosHexsym symbol = hexfile.lookup_symbol(name);
-               if (symbol == null)
-                       throw new AltosNoSymbol(name);
-               int offset = (int) symbol.address - hexfile.address;
-               if (offset < 0 || hexfile.data.length < offset + len)
+               if (symbol == null) {
                        throw new AltosNoSymbol(name);
-               return offset;
+               }
+               if (hexfile.address <= symbol.address && symbol.address + len <= hexfile.max_address) {
+                       return symbol.address;
+               }
+               throw new AltosNoSymbol(name);
+       }
+
+       static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
+               return (int) (find_address(hexfile, name, len) - hexfile.address);
        }
 
        static int get_int(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
                byte[] bytes = hexfile.data;
-               int start = find_offset(hexfile, name, len);
+               int start = (int) find_offset(hexfile, name, len);
 
                int     v = 0;
                int     o = 0;
@@ -118,41 +126,82 @@ public class AltosRomconfig {
                                case 2:
                                case 1:
                                        serial_number = get_int(hexfile, ao_serial_number, 2);
-                                       radio_calibration = get_int(hexfile, ao_radio_cal, 4);
+                                       try {
+                                               radio_calibration = get_int(hexfile, ao_radio_cal, 4);
+                                       } catch (AltosNoSymbol missing) {
+                                               radio_calibration = 0;
+                                       }
                                        valid = true;
                                        break;
                                }
                        }
+                       usb_id = hexfile.find_usb_id();
+                       usb_product = hexfile.find_usb_product();
+
                } catch (AltosNoSymbol missing) {
                        valid = false;
                }
        }
 
-       final static String[] fetch_names = {
+       private final static String[] fetch_names = {
                ao_romconfig_version,
                ao_romconfig_check,
                ao_serial_number,
-               ao_radio_cal
+               ao_radio_cal,
+               ao_usb_descriptors,
+       };
+
+       private static int fetch_len(String name) {
+               if (name.equals(ao_usb_descriptors))
+                       return 256;
+               return 2;
+       }
+
+       private final static String[] required_names = {
+               ao_romconfig_version,
+               ao_romconfig_check,
+               ao_serial_number
        };
 
-       public static int fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
-               int     base = 0x7fffffff;
+       private static boolean name_required(String name) {
+               for (String required : required_names)
+                       if (name.equals(required))
+                               return true;
+               return false;
+       }
+
+       public static long fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
+               long    base = 0xffffffffL;
                for (String name : fetch_names) {
-                       int     addr = find_offset(hexfile, name, 2) + hexfile.address;
-                       if (addr < base)
-                               base = addr;
+                       try {
+                               int     len = fetch_len(name);
+                               long    addr = find_address(hexfile, name, len);
+
+                               if (addr < base)
+                                       base = addr;
+                       } catch (AltosNoSymbol ns) {
+                               if (name_required(name))
+                                       throw (ns);
+                       }
                }
                return base;
        }
 
-       public static int fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
-               int     bounds = 0;
+       public static long fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
+               long    bounds = 0;
                for (String name : fetch_names) {
-                       int     addr = find_offset(hexfile, name, 2) + hexfile.address;
-                       if (addr > bounds)
-                               bounds = addr;
+                       try {
+                               int     len = fetch_len(name);
+                               long    addr = find_address(hexfile, name, len) + len;
+                               if (addr > bounds)
+                                       bounds = addr;
+                       } catch (AltosNoSymbol ns) {
+                               if (name_required(name))
+                                       throw (ns);
+                       }
                }
-               return bounds + 2;
+
+               return bounds;
        }
 
        public void write (AltosHexfile hexfile) throws IOException {
@@ -166,10 +215,17 @@ public class AltosRomconfig {
                try {
                        switch (existing.version) {
                        case 2:
-                               put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
+                               try {
+                                       put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
+                               } catch (AltosNoSymbol missing) {
+                               }
+                               /* fall through ... */
                        case 1:
                                put_int(serial_number, hexfile, ao_serial_number, 2);
-                               put_int(radio_calibration, hexfile, ao_radio_cal, 4);
+                               try {
+                                       put_int(radio_calibration, hexfile, ao_radio_cal, 4);
+                               } catch (AltosNoSymbol missing) {
+                               }
                                break;
                        }
                } catch (AltosNoSymbol missing) {