Update java library version numbers
[fw/altos] / altoslib / AltosRomconfig.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_10;
19
20 import java.io.*;
21
22 public class AltosRomconfig {
23         public boolean  valid;
24         public int      version;
25         public int      check;
26         public int      serial_number;
27         public int      radio_calibration;
28
29         static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
30                 AltosHexsym symbol = hexfile.lookup_symbol(name);
31                 if (symbol == null)
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);
36                 return offset;
37         }
38
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);
42
43                 int     v = 0;
44                 int     o = 0;
45                 while (len > 0) {
46                         v = v | ((((int) bytes[start]) & 0xff) << o);
47                         start++;
48                         len--;
49                         o += 8;
50                 }
51                 return v;
52         }
53
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);
57
58                 while (len > 0) {
59                         bytes[start] = (byte) (value & 0xff);
60                         start++;
61                         len--;
62                         value >>= 8;
63                 }
64         }
65
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());
69
70                 for (int i = 0; i < value.length(); i++)
71                         bytes[start + i] = (byte) value.charAt(i);
72         }
73
74         static final int AO_USB_DESC_STRING     = 3;
75
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);
79
80                 int string_num = 0;
81
82                 while (start < bytes.length && bytes[start] != 0) {
83                         if (bytes[start + 1] == AO_USB_DESC_STRING) {
84                                 ++string_num;
85                                 if (string_num == 4)
86                                         break;
87                         }
88                         start += ((int) bytes[start]) & 0xff;
89                 }
90                 if (start >= bytes.length || bytes[start] == 0)
91                         throw new AltosNoSymbol(name);
92
93                 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
94                 String fmt = String.format("%%0%dd", len);
95
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));
99
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;
103                 }
104         }
105
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";
111
112         public AltosRomconfig(AltosHexfile hexfile) {
113                 try {
114                         version = get_int(hexfile, ao_romconfig_version, 2);
115                         check = get_int(hexfile, ao_romconfig_check, 2);
116                         if (check == (~version & 0xffff)) {
117                                 switch (version) {
118                                 case 2:
119                                 case 1:
120                                         serial_number = get_int(hexfile, ao_serial_number, 2);
121                                         try {
122                                                 radio_calibration = get_int(hexfile, ao_radio_cal, 4);
123                                         } catch (AltosNoSymbol missing) {
124                                                 radio_calibration = 0;
125                                         }
126                                         valid = true;
127                                         break;
128                                 }
129                         }
130                 } catch (AltosNoSymbol missing) {
131                         valid = false;
132                 }
133         }
134
135         private final static String[] fetch_names = {
136                 ao_romconfig_version,
137                 ao_romconfig_check,
138                 ao_serial_number,
139                 ao_radio_cal
140         };
141
142         private final static String[] required_names = {
143                 ao_romconfig_version,
144                 ao_romconfig_check,
145                 ao_serial_number
146         };
147
148         private static boolean name_required(String name) {
149                 for (String required : required_names)
150                         if (name.equals(required))
151                                 return true;
152                 return false;
153         }
154
155         public static int fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
156                 int     base = 0x7fffffff;
157                 for (String name : fetch_names) {
158                         try {
159                                 int     addr = find_offset(hexfile, name, 2) + hexfile.address;
160                                 if (addr < base)
161                                         base = addr;
162                         } catch (AltosNoSymbol ns) {
163                                 if (name_required(name))
164                                         throw (ns);
165                         }
166                 }
167                 return base;
168         }
169
170         public static int fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
171                 int     bounds = 0;
172                 for (String name : fetch_names) {
173                         try {
174                                 int     addr = find_offset(hexfile, name, 2) + hexfile.address;
175                                 if (addr > bounds)
176                                         bounds = addr;
177                         } catch (AltosNoSymbol ns) {
178                                 if (name_required(name))
179                                         throw (ns);
180                         }
181                 }
182                 return bounds + 2;
183         }
184
185         public void write (AltosHexfile hexfile) throws IOException {
186                 if (!valid)
187                         throw new IOException("rom configuration invalid");
188
189                 AltosRomconfig existing = new AltosRomconfig(hexfile);
190                 if (!existing.valid)
191                         throw new IOException("image does not contain existing rom config");
192
193                 try {
194                         switch (existing.version) {
195                         case 2:
196                                 try {
197                                         put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
198                                 } catch (AltosNoSymbol missing) {
199                                 }
200                                 /* fall through ... */
201                         case 1:
202                                 put_int(serial_number, hexfile, ao_serial_number, 2);
203                                 try {
204                                         put_int(radio_calibration, hexfile, ao_radio_cal, 4);
205                                 } catch (AltosNoSymbol missing) {
206                                 }
207                                 break;
208                         }
209                 } catch (AltosNoSymbol missing) {
210                         throw new IOException(missing.getMessage());
211                 }
212
213                 AltosRomconfig check = new AltosRomconfig(hexfile);
214                 if (!check.valid)
215                         throw new IOException("writing new rom config failed\n");
216         }
217
218         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
219                 valid = true;
220                 version = 1;
221                 check = (~version & 0xffff);
222                 serial_number = in_serial_number;
223                 radio_calibration = in_radio_calibration;
224         }
225
226         public boolean valid() {
227                 return valid && serial_number != 0;
228         }
229
230         public AltosRomconfig() {
231                 valid = false;
232         }
233 }