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_12;
23 public class AltosRomconfig {
27 public int serial_number;
28 public int radio_calibration;
29 public AltosUsbId usb_id;
30 public String usb_product;
32 static private long find_address(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
33 AltosHexsym symbol = hexfile.lookup_symbol(name);
35 System.out.printf("no symbol %s\n", name);
36 throw new AltosNoSymbol(name);
38 if (hexfile.address <= symbol.address && symbol.address + len < hexfile.max_address) {
39 System.out.printf("%s: %x\n", name, symbol.address);
40 return symbol.address;
42 System.out.printf("invalid symbol addr %x range is %x - %x\n",
43 symbol.address, hexfile.address, hexfile.max_address);
44 throw new AltosNoSymbol(name);
47 static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
48 return (int) (find_address(hexfile, name, len) - hexfile.address);
51 static int get_int(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
52 byte[] bytes = hexfile.data;
53 int start = (int) find_offset(hexfile, name, len);
58 v = v | ((((int) bytes[start]) & 0xff) << o);
66 static void put_int(int value, AltosHexfile hexfile, String name, int len) throws AltosNoSymbol, IOException {
67 byte[] bytes = hexfile.data;
68 int start = find_offset(hexfile, name, len);
71 bytes[start] = (byte) (value & 0xff);
78 static void put_string(String value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
79 byte[] bytes = hexfile.data;
80 int start = find_offset(hexfile, name, value.length());
82 for (int i = 0; i < value.length(); i++)
83 bytes[start + i] = (byte) value.charAt(i);
86 static final int AO_USB_DESC_STRING = 3;
88 static void put_usb_serial(int value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
89 byte[] bytes = hexfile.data;
90 int start = find_offset(hexfile, name, 2);
94 while (start < bytes.length && bytes[start] != 0) {
95 if (bytes[start + 1] == AO_USB_DESC_STRING) {
100 start += ((int) bytes[start]) & 0xff;
102 if (start >= bytes.length || bytes[start] == 0)
103 throw new AltosNoSymbol(name);
105 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
106 String fmt = String.format("%%0%dd", len);
108 String s = String.format(fmt, value);
109 if (s.length() != len)
110 throw new AltosNoSymbol(String.format("weird usb length issue %s isn't %d\n", s, len));
112 for (int i = 0; i < len; i++) {
113 bytes[start + 2 + i*2] = (byte) s.charAt(i);
114 bytes[start + 2 + i*2+1] = 0;
118 final static String ao_romconfig_version = "ao_romconfig_version";
119 final static String ao_romconfig_check = "ao_romconfig_check";
120 final static String ao_serial_number = "ao_serial_number";
121 final static String ao_radio_cal = "ao_radio_cal";
122 final static String ao_usb_descriptors = "ao_usb_descriptors";
124 public AltosRomconfig(AltosHexfile hexfile) {
126 System.out.printf("Attempting symbols\n");
127 version = get_int(hexfile, ao_romconfig_version, 2);
128 System.out.printf("version %d\n", version);
129 check = get_int(hexfile, ao_romconfig_check, 2);
130 System.out.printf("check %d\n", check);
131 if (check == (~version & 0xffff)) {
135 serial_number = get_int(hexfile, ao_serial_number, 2);
136 System.out.printf("serial %d\n", serial_number);
138 radio_calibration = get_int(hexfile, ao_radio_cal, 4);
139 } catch (AltosNoSymbol missing) {
140 radio_calibration = 0;
146 System.out.printf("attempting usbid\n");
147 usb_id = hexfile.find_usb_id();
149 System.out.printf("No usb id\n");
151 System.out.printf("usb id: %04x:%04x\n",
152 usb_id.vid, usb_id.pid);
153 usb_product = hexfile.find_usb_product();
154 if (usb_product == null)
155 System.out.printf("No usb product\n");
157 System.out.printf("usb product: %s\n", usb_product);
159 } catch (AltosNoSymbol missing) {
164 private final static String[] fetch_names = {
165 ao_romconfig_version,
172 private static int fetch_len(String name) {
173 if (name.equals(ao_usb_descriptors))
178 private final static String[] required_names = {
179 ao_romconfig_version,
184 private static boolean name_required(String name) {
185 for (String required : required_names)
186 if (name.equals(required))
191 public static long fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
192 long base = 0xffffffffL;
193 for (String name : fetch_names) {
195 int len = fetch_len(name);
196 long addr = find_address(hexfile, name, len);
200 System.out.printf("symbol %s at %x base %x\n", name, addr, base);
201 } catch (AltosNoSymbol ns) {
202 if (name_required(name))
209 public static long fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
211 for (String name : fetch_names) {
213 int len = fetch_len(name);
214 long addr = find_address(hexfile, name, len) + len;
217 System.out.printf("symbol %s at %x bounds %x\n", name, addr, bounds);
218 } catch (AltosNoSymbol ns) {
219 if (name_required(name))
227 public void write (AltosHexfile hexfile) throws IOException {
229 throw new IOException("rom configuration invalid");
231 AltosRomconfig existing = new AltosRomconfig(hexfile);
233 throw new IOException("image does not contain existing rom config");
236 switch (existing.version) {
239 put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
240 } catch (AltosNoSymbol missing) {
242 /* fall through ... */
244 put_int(serial_number, hexfile, ao_serial_number, 2);
246 put_int(radio_calibration, hexfile, ao_radio_cal, 4);
247 } catch (AltosNoSymbol missing) {
251 } catch (AltosNoSymbol missing) {
252 throw new IOException(missing.getMessage());
255 AltosRomconfig check = new AltosRomconfig(hexfile);
257 throw new IOException("writing new rom config failed\n");
260 public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
263 check = (~version & 0xffff);
264 serial_number = in_serial_number;
265 radio_calibration = in_radio_calibration;
268 public boolean valid() {
269 return valid && serial_number != 0;
272 public AltosRomconfig() {