altoslib: Deal with reflashing EasyMega boards with ancient firmware
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.io.*;
22 import java.util.concurrent.*;
23
24 public class AltosRomconfig implements AltosUnitInfoListener {
25         public boolean  valid;
26         public boolean  radio_calibration_broken;
27         public int      version;
28         public int      check;
29         public int      serial_number;
30         public int      radio_calibration;
31         public int      address_offset;
32         public AltosUsbId       usb_id;
33         public String           usb_product;
34
35         static private long find_address(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
36                 AltosHexsym symbol = hexfile.lookup_symbol(name);
37                 if (symbol == null) {
38                         throw new AltosNoSymbol(name);
39                 }
40                 if (hexfile.address <= symbol.address && symbol.address + len <= hexfile.max_address) {
41                         return symbol.address;
42                 }
43                 throw new AltosNoSymbol(name);
44         }
45
46         static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
47                 return (int) (find_address(hexfile, name, len) - hexfile.address);
48         }
49
50         static private int get_int(AltosHexfile hexfile, String name, int len, int adjust) throws AltosNoSymbol {
51                 byte[] bytes = hexfile.data;
52                 int start = (int) find_offset(hexfile, name, len) + adjust;
53
54                 int     v = 0;
55                 int     o = 0;
56                 while (len > 0) {
57                         v = v | ((((int) bytes[start]) & 0xff) << o);
58                         start++;
59                         len--;
60                         o += 8;
61                 }
62                 return v;
63         }
64
65         static void put_int(int value, AltosHexfile hexfile, String name, int len) throws AltosNoSymbol, IOException {
66                 byte[] bytes = hexfile.data;
67                 int start = find_offset(hexfile, name, len);
68
69                 while (len > 0) {
70                         bytes[start] = (byte) (value & 0xff);
71                         start++;
72                         len--;
73                         value >>= 8;
74                 }
75         }
76
77         static void put_string(String value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
78                 byte[] bytes = hexfile.data;
79                 int start = find_offset(hexfile, name, value.length());
80
81                 for (int i = 0; i < value.length(); i++)
82                         bytes[start + i] = (byte) value.charAt(i);
83         }
84
85         static final int AO_USB_DESC_STRING     = 3;
86
87         static void put_usb_serial(int value, AltosHexfile hexfile, String name) throws AltosNoSymbol {
88                 byte[] bytes = hexfile.data;
89                 int start = find_offset(hexfile, name, 2);
90
91                 int string_num = 0;
92
93                 while (start < bytes.length && bytes[start] != 0) {
94                         if (bytes[start + 1] == AO_USB_DESC_STRING) {
95                                 ++string_num;
96                                 if (string_num == 4)
97                                         break;
98                         }
99                         start += ((int) bytes[start]) & 0xff;
100                 }
101                 if (start >= bytes.length || bytes[start] == 0)
102                         throw new AltosNoSymbol(name);
103
104                 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
105                 String fmt = String.format("%%0%dd", len);
106
107                 String s = String.format(fmt, value);
108                 if (s.length() != len)
109                         throw new AltosNoSymbol(String.format("weird usb length issue %s isn't %d\n", s, len));
110
111                 for (int i = 0; i < len; i++) {
112                         bytes[start + 2 + i*2] = (byte) s.charAt(i);
113                         bytes[start + 2 + i*2+1] = 0;
114                 }
115         }
116
117         final static String ao_romconfig_version = "ao_romconfig_version";
118         final static String ao_romconfig_check = "ao_romconfig_check";
119         final static String ao_serial_number = "ao_serial_number";
120         final static String ao_radio_cal = "ao_radio_cal";
121         final static String ao_usb_descriptors = "ao_usb_descriptors";
122
123         Semaphore       unit_info_done;
124
125         public void notify_unit_info(AltosUnitInfo unit_info) {
126                 unit_info_done.release();
127         }
128
129         private void fetch_radio_cal() {
130                 unit_info_done = new Semaphore(0);
131                 AltosUnitInfo   info = new AltosUnitInfo(serial_number, this);
132
133                 /* Block waiting for the rf calibration data */
134                 radio_calibration_broken = true;
135                 try {
136                         unit_info_done.acquire();
137                         int new_cal = info.rfcal();
138                         if (new_cal != AltosLib.MISSING) {
139                                 radio_calibration = new_cal;
140                                 radio_calibration_broken = false;
141                         }
142                 } catch (InterruptedException ie) {
143                 }
144         }
145
146         static final int adjust_rom[] = { 0, -4, 4 };
147
148         public AltosRomconfig(AltosHexfile hexfile) {
149                 try {
150                         for (int adjust : adjust_rom) {
151                                 try {
152                                         version = get_int(hexfile, ao_romconfig_version, 2, adjust);
153                                         check = get_int(hexfile, ao_romconfig_check, 2, adjust);
154                                         System.out.printf("adjust %d version %x check %x\n", adjust, version, check);
155                                         if (check == (~version & 0xffff)) {
156                                                 switch (version) {
157                                                 case 2:
158                                                 case 1:
159                                                         serial_number = get_int(hexfile, ao_serial_number, 2, adjust);
160                                                         try {
161                                                                 radio_calibration = get_int(hexfile, ao_radio_cal, 4, adjust);
162                                                         } catch (AltosNoSymbol missing) {
163                                                                 radio_calibration = 0;
164                                                         }
165
166                                                         valid = true;
167
168                                                         /* XXX TeleBT v4.0 units originally shipped without RF calibration programmed. Go fetch
169                                                          * the correct value from the web site
170                                                          */
171                                                         if (serial_number == 2584 ||
172                                                             (3686 <= serial_number && serial_number <= 3938 && radio_calibration == 5695485))
173                                                         {
174                                                                 fetch_radio_cal();
175                                                         }
176
177                                                         break;
178                                                 }
179                                                 break;
180                                         }
181                                 } catch (ArrayIndexOutOfBoundsException aie) {
182                                         System.out.printf("adjust %d failed\n", adjust);
183                                         continue;
184                                 }
185                         }
186                         usb_id = hexfile.find_usb_id();
187                         usb_product = hexfile.find_usb_product();
188
189                 } catch (AltosNoSymbol missing) {
190                         valid = false;
191                 }
192         }
193
194         private final static String[] fetch_names = {
195                 ao_romconfig_version,
196                 ao_romconfig_check,
197                 ao_serial_number,
198                 ao_radio_cal,
199                 ao_usb_descriptors,
200         };
201
202         private static int fetch_len(String name) {
203                 if (name.equals(ao_usb_descriptors))
204                         return 256;
205                 return 2;
206         }
207
208         private final static String[] required_names = {
209                 ao_romconfig_version,
210                 ao_romconfig_check,
211                 ao_serial_number
212         };
213
214         private static boolean name_required(String name) {
215                 for (String required : required_names)
216                         if (name.equals(required))
217                                 return true;
218                 return false;
219         }
220
221         public static long fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
222                 long    base = 0xffffffffL;
223                 for (String name : fetch_names) {
224                         try {
225                                 int     len = fetch_len(name);
226                                 long    addr = find_address(hexfile, name, len);
227
228                                 if (addr < base)
229                                         base = addr;
230                         } catch (AltosNoSymbol ns) {
231                                 if (name_required(name))
232                                         throw (ns);
233                         }
234                 }
235                 return base;
236         }
237
238         public static long fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
239                 long    bounds = 0;
240                 for (String name : fetch_names) {
241                         try {
242                                 int     len = fetch_len(name);
243                                 long    addr = find_address(hexfile, name, len) + len;
244                                 if (addr > bounds)
245                                         bounds = addr;
246                         } catch (AltosNoSymbol ns) {
247                                 if (name_required(name))
248                                         throw (ns);
249                         }
250                 }
251
252                 return bounds;
253         }
254
255         public void write (AltosHexfile hexfile) throws IOException {
256                 if (!valid)
257                         throw new IOException("rom configuration invalid");
258
259                 AltosRomconfig existing = new AltosRomconfig(hexfile);
260                 if (!existing.valid)
261                         throw new IOException("image does not contain existing rom config");
262
263                 try {
264                         switch (existing.version) {
265                         case 2:
266                                 try {
267                                         put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
268                                 } catch (AltosNoSymbol missing) {
269                                 }
270                                 /* fall through ... */
271                         case 1:
272                                 put_int(serial_number, hexfile, ao_serial_number, 2);
273                                 try {
274                                         put_int(radio_calibration, hexfile, ao_radio_cal, 4);
275                                 } catch (AltosNoSymbol missing) {
276                                 }
277                                 break;
278                         }
279                 } catch (AltosNoSymbol missing) {
280                         throw new IOException(missing.getMessage());
281                 }
282
283                 AltosRomconfig check = new AltosRomconfig(hexfile);
284                 if (!check.valid)
285                         throw new IOException("writing new rom config failed\n");
286         }
287
288         public String toString() {
289                 return String.format("valid %b version %d serial %d radio %d usb %04x:%04x %s",
290                                      valid, version, serial_number, radio_calibration,
291                                      usb_id == null ? 0 : usb_id.vid,
292                                      usb_id == null ? 0 : usb_id.pid,
293                                      usb_product == null ? "unknown" : usb_product);
294         }
295
296         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
297                 valid = true;
298                 version = 1;
299                 check = (~version & 0xffff);
300                 serial_number = in_serial_number;
301                 radio_calibration = in_radio_calibration;
302         }
303
304         public boolean valid() {
305                 return valid;
306         }
307
308         public AltosRomconfig() {
309                 valid = false;
310         }
311 }