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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
23 public class AltosRomconfig {
27 public int serial_number;
28 public int radio_calibration;
30 static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
31 AltosHexsym symbol = hexfile.lookup_symbol(name);
33 throw new AltosNoSymbol(name);
34 int offset = (int) symbol.address - hexfile.address;
35 if (offset < 0 || hexfile.data.length < offset + len)
36 throw new AltosNoSymbol(name);
40 static int get_int(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
41 byte[] bytes = hexfile.data;
42 int start = find_offset(hexfile, name, len);
47 v = v | ((((int) bytes[start]) & 0xff) << o);
55 static void put_int(int value, AltosHexfile hexfile, String name, int len) throws AltosNoSymbol, IOException {
56 byte[] bytes = hexfile.data;
57 int start = find_offset(hexfile, name, len);
60 bytes[start] = (byte) (value & 0xff);
67 static void put_string(String value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
68 byte[] bytes = hexfile.data;
69 int start = find_offset(hexfile, name, value.length());
71 for (int i = 0; i < value.length(); i++)
72 bytes[start + i] = (byte) value.charAt(i);
75 static final int AO_USB_DESC_STRING = 3;
77 static void put_usb_serial(int value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
78 byte[] bytes = hexfile.data;
79 int start = find_offset(hexfile, name, 2);
83 while (start < bytes.length && bytes[start] != 0) {
84 if (bytes[start + 1] == AO_USB_DESC_STRING) {
89 start += ((int) bytes[start]) & 0xff;
91 if (start >= bytes.length || bytes[start] == 0)
92 throw new AltosNoSymbol(name);
94 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
95 String fmt = String.format("%%0%dd", len);
97 String s = String.format(fmt, value);
98 if (s.length() != len)
99 throw new AltosNoSymbol(String.format("weird usb length issue %s isn't %d\n", s, len));
101 for (int i = 0; i < len; i++) {
102 bytes[start + 2 + i*2] = (byte) s.charAt(i);
103 bytes[start + 2 + i*2+1] = 0;
107 final static String ao_romconfig_version = "ao_romconfig_version";
108 final static String ao_romconfig_check = "ao_romconfig_check";
109 final static String ao_serial_number = "ao_serial_number";
110 final static String ao_radio_cal = "ao_radio_cal";
111 final static String ao_usb_descriptors = "ao_usb_descriptors";
113 public AltosRomconfig(AltosHexfile hexfile) {
115 version = get_int(hexfile, ao_romconfig_version, 2);
116 check = get_int(hexfile, ao_romconfig_check, 2);
117 if (check == (~version & 0xffff)) {
121 serial_number = get_int(hexfile, ao_serial_number, 2);
123 radio_calibration = get_int(hexfile, ao_radio_cal, 4);
124 } catch (AltosNoSymbol missing) {
125 radio_calibration = 0;
131 } catch (AltosNoSymbol missing) {
136 private final static String[] fetch_names = {
137 ao_romconfig_version,
143 private final static String[] required_names = {
144 ao_romconfig_version,
149 private static boolean name_required(String name) {
150 for (String required : required_names)
151 if (name.equals(required))
156 public static int fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
157 int base = 0x7fffffff;
158 for (String name : fetch_names) {
160 int addr = find_offset(hexfile, name, 2) + hexfile.address;
163 } catch (AltosNoSymbol ns) {
164 if (name_required(name))
171 public static int fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
173 for (String name : fetch_names) {
175 int addr = find_offset(hexfile, name, 2) + hexfile.address;
178 } catch (AltosNoSymbol ns) {
179 if (name_required(name))
186 public void write (AltosHexfile hexfile) throws IOException {
188 throw new IOException("rom configuration invalid");
190 AltosRomconfig existing = new AltosRomconfig(hexfile);
192 throw new IOException("image does not contain existing rom config");
195 switch (existing.version) {
198 put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
199 } catch (AltosNoSymbol missing) {
201 /* fall through ... */
203 put_int(serial_number, hexfile, ao_serial_number, 2);
205 put_int(radio_calibration, hexfile, ao_radio_cal, 4);
206 } catch (AltosNoSymbol missing) {
210 } catch (AltosNoSymbol missing) {
211 throw new IOException(missing.getMessage());
214 AltosRomconfig check = new AltosRomconfig(hexfile);
216 throw new IOException("writing new rom config failed\n");
219 public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
222 check = (~version & 0xffff);
223 serial_number = in_serial_number;
224 radio_calibration = in_radio_calibration;
227 public boolean valid() {
228 return valid && serial_number != 0;
231 public AltosRomconfig() {