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