altoslib/altosuilib: Validate rom image is for target device
[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_12;
20
21 import java.io.*;
22
23 public class AltosRomconfig {
24         public boolean  valid;
25         public int      version;
26         public int      check;
27         public int      serial_number;
28         public int      radio_calibration;
29         public AltosUsbId       usb_id;
30         public String           usb_product;
31
32         static private long find_address(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
33                 AltosHexsym symbol = hexfile.lookup_symbol(name);
34                 if (symbol == null) {
35                         System.out.printf("no symbol %s\n", name);
36                         throw new AltosNoSymbol(name);
37                 }
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;
41                 }
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);
45         }
46
47         static private int find_offset(AltosHexfile hexfile, String name, int len) throws AltosNoSymbol {
48                 return (int) (find_address(hexfile, name, len) - hexfile.address);
49         }
50
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);
54
55                 int     v = 0;
56                 int     o = 0;
57                 while (len > 0) {
58                         v = v | ((((int) bytes[start]) & 0xff) << o);
59                         start++;
60                         len--;
61                         o += 8;
62                 }
63                 return v;
64         }
65
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);
69
70                 while (len > 0) {
71                         bytes[start] = (byte) (value & 0xff);
72                         start++;
73                         len--;
74                         value >>= 8;
75                 }
76         }
77
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());
81
82                 for (int i = 0; i < value.length(); i++)
83                         bytes[start + i] = (byte) value.charAt(i);
84         }
85
86         static final int AO_USB_DESC_STRING     = 3;
87
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);
91
92                 int string_num = 0;
93
94                 while (start < bytes.length && bytes[start] != 0) {
95                         if (bytes[start + 1] == AO_USB_DESC_STRING) {
96                                 ++string_num;
97                                 if (string_num == 4)
98                                         break;
99                         }
100                         start += ((int) bytes[start]) & 0xff;
101                 }
102                 if (start >= bytes.length || bytes[start] == 0)
103                         throw new AltosNoSymbol(name);
104
105                 int len = ((((int) bytes[start]) & 0xff) - 2) / 2;
106                 String fmt = String.format("%%0%dd", len);
107
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));
111
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;
115                 }
116         }
117
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";
123
124         public AltosRomconfig(AltosHexfile hexfile) {
125                 try {
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)) {
132                                 switch (version) {
133                                 case 2:
134                                 case 1:
135                                         serial_number = get_int(hexfile, ao_serial_number, 2);
136                                         System.out.printf("serial %d\n", serial_number);
137                                         try {
138                                                 radio_calibration = get_int(hexfile, ao_radio_cal, 4);
139                                         } catch (AltosNoSymbol missing) {
140                                                 radio_calibration = 0;
141                                         }
142                                         valid = true;
143                                         break;
144                                 }
145                         }
146                         System.out.printf("attempting usbid\n");
147                         usb_id = hexfile.find_usb_id();
148                         if (usb_id == null)
149                                 System.out.printf("No usb id\n");
150                         else
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");
156                         else
157                                 System.out.printf("usb product: %s\n", usb_product);
158
159                 } catch (AltosNoSymbol missing) {
160                         valid = false;
161                 }
162         }
163
164         private final static String[] fetch_names = {
165                 ao_romconfig_version,
166                 ao_romconfig_check,
167                 ao_serial_number,
168                 ao_radio_cal,
169                 ao_usb_descriptors,
170         };
171
172         private static int fetch_len(String name) {
173                 if (name.equals(ao_usb_descriptors))
174                         return 256;
175                 return 2;
176         }
177
178         private final static String[] required_names = {
179                 ao_romconfig_version,
180                 ao_romconfig_check,
181                 ao_serial_number
182         };
183
184         private static boolean name_required(String name) {
185                 for (String required : required_names)
186                         if (name.equals(required))
187                                 return true;
188                 return false;
189         }
190
191         public static long fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
192                 long    base = 0xffffffffL;
193                 for (String name : fetch_names) {
194                         try {
195                                 int     len = fetch_len(name);
196                                 long    addr = find_address(hexfile, name, len);
197
198                                 if (addr < base)
199                                         base = addr;
200                                 System.out.printf("symbol %s at %x base %x\n", name, addr, base);
201                         } catch (AltosNoSymbol ns) {
202                                 if (name_required(name))
203                                         throw (ns);
204                         }
205                 }
206                 return base;
207         }
208
209         public static long fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
210                 long    bounds = 0;
211                 for (String name : fetch_names) {
212                         try {
213                                 int     len = fetch_len(name);
214                                 long    addr = find_address(hexfile, name, len) + len;
215                                 if (addr > bounds)
216                                         bounds = addr;
217                                 System.out.printf("symbol %s at %x bounds %x\n", name, addr, bounds);
218                         } catch (AltosNoSymbol ns) {
219                                 if (name_required(name))
220                                         throw (ns);
221                         }
222                 }
223
224                 return bounds;
225         }
226
227         public void write (AltosHexfile hexfile) throws IOException {
228                 if (!valid)
229                         throw new IOException("rom configuration invalid");
230
231                 AltosRomconfig existing = new AltosRomconfig(hexfile);
232                 if (!existing.valid)
233                         throw new IOException("image does not contain existing rom config");
234
235                 try {
236                         switch (existing.version) {
237                         case 2:
238                                 try {
239                                         put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
240                                 } catch (AltosNoSymbol missing) {
241                                 }
242                                 /* fall through ... */
243                         case 1:
244                                 put_int(serial_number, hexfile, ao_serial_number, 2);
245                                 try {
246                                         put_int(radio_calibration, hexfile, ao_radio_cal, 4);
247                                 } catch (AltosNoSymbol missing) {
248                                 }
249                                 break;
250                         }
251                 } catch (AltosNoSymbol missing) {
252                         throw new IOException(missing.getMessage());
253                 }
254
255                 AltosRomconfig check = new AltosRomconfig(hexfile);
256                 if (!check.valid)
257                         throw new IOException("writing new rom config failed\n");
258         }
259
260         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
261                 valid = true;
262                 version = 1;
263                 check = (~version & 0xffff);
264                 serial_number = in_serial_number;
265                 radio_calibration = in_radio_calibration;
266         }
267
268         public boolean valid() {
269                 return valid && serial_number != 0;
270         }
271
272         public AltosRomconfig() {
273                 valid = false;
274         }
275 }