2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_4;
22 public class AltosRomconfig {
26 public int serial_number;
27 public int radio_calibration;
29 static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
30 AltosHexsym symbol = hexfile.lookup_symbol(name);
32 throw new AltosNoSymbol(name);
33 int offset = (int) symbol.address - hexfile.address;
34 if (offset < 0 || hexfile.data.length < offset + len)
35 throw new AltosNoSymbol(name);
39 static int get_int(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
40 byte[] bytes = hexfile.data;
41 int start = find_offset(hexfile, name, len);
46 v = v | ((((int) bytes[start]) & 0xff) << o);
54 static void put_int(int value, AltosHexfile hexfile, String name, int len) throws AltosNoSymbol, IOException {
55 byte[] bytes = hexfile.data;
56 int start = find_offset(hexfile, name, len);
59 bytes[start] = (byte) (value & 0xff);
66 static void put_string(String value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
67 byte[] bytes = hexfile.data;
68 int start = find_offset(hexfile, name, value.length());
70 for (int i = 0; i < value.length(); i++)
71 bytes[start + i] = (byte) value.charAt(i);
74 static final int AO_USB_DESC_STRING = 3;
76 static void put_usb_serial(int value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
77 byte[] bytes = hexfile.data;
78 int start = find_offset(hexfile, name, 2);
82 while (start < bytes.length && bytes[start] != 0) {
83 if (bytes[start + 1] == AO_USB_DESC_STRING) {
88 start += ((int) bytes[start]) & 0xff;
90 if (start >= bytes.length || bytes[start] == 0)
91 throw new AltosNoSymbol(name);
93 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
94 String fmt = String.format("%%0%dd", len);
96 String s = String.format(fmt, value);
97 if (s.length() != len)
98 throw new AltosNoSymbol(String.format("weird usb length issue %s isn't %d\n", s, len));
100 for (int i = 0; i < len; i++) {
101 bytes[start + 2 + i*2] = (byte) s.charAt(i);
102 bytes[start + 2 + i*2+1] = 0;
106 final static String ao_romconfig_version = "ao_romconfig_version";
107 final static String ao_romconfig_check = "ao_romconfig_check";
108 final static String ao_serial_number = "ao_serial_number";
109 final static String ao_radio_cal = "ao_radio_cal";
110 final static String ao_usb_descriptors = "ao_usb_descriptors";
112 public AltosRomconfig(AltosHexfile hexfile) {
114 version = get_int(hexfile, ao_romconfig_version, 2);
115 check = get_int(hexfile, ao_romconfig_check, 2);
116 if (check == (~version & 0xffff)) {
120 serial_number = get_int(hexfile, ao_serial_number, 2);
122 radio_calibration = get_int(hexfile, ao_radio_cal, 4);
123 } catch (AltosNoSymbol missing) {
124 radio_calibration = 0;
130 } catch (AltosNoSymbol missing) {
135 private final static String[] fetch_names = {
136 ao_romconfig_version,
142 private final static String[] required_names = {
143 ao_romconfig_version,
148 private static boolean name_required(String name) {
149 for (String required : required_names)
150 if (name.equals(required))
155 public static int fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
156 int base = 0x7fffffff;
157 for (String name : fetch_names) {
159 int addr = find_offset(hexfile, name, 2) + hexfile.address;
162 } catch (AltosNoSymbol ns) {
163 if (name_required(name))
170 public static int fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
172 for (String name : fetch_names) {
174 int addr = find_offset(hexfile, name, 2) + hexfile.address;
177 } catch (AltosNoSymbol ns) {
178 if (name_required(name))
185 public void write (AltosHexfile hexfile) throws IOException {
187 throw new IOException("rom configuration invalid");
189 AltosRomconfig existing = new AltosRomconfig(hexfile);
191 throw new IOException("image does not contain existing rom config");
194 switch (existing.version) {
197 put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
198 } catch (AltosNoSymbol missing) {
200 /* fall through ... */
202 put_int(serial_number, hexfile, ao_serial_number, 2);
204 put_int(radio_calibration, hexfile, ao_radio_cal, 4);
205 } catch (AltosNoSymbol missing) {
209 } catch (AltosNoSymbol missing) {
210 throw new IOException(missing.getMessage());
213 AltosRomconfig check = new AltosRomconfig(hexfile);
215 throw new IOException("writing new rom config failed\n");
218 public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
221 check = (~version & 0xffff);
222 serial_number = in_serial_number;
223 radio_calibration = in_radio_calibration;
226 public boolean valid() {
227 return valid && serial_number != 0;
230 public AltosRomconfig() {