Add new tools to .gitignore
[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_2;
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                                         radio_calibration = get_int(hexfile, ao_radio_cal, 4);
122                                         valid = true;
123                                         break;
124                                 }
125                         }
126                 } catch (AltosNoSymbol missing) {
127                         valid = false;
128                 }
129         }
130
131         final static String[] fetch_names = {
132                 ao_romconfig_version,
133                 ao_romconfig_check,
134                 ao_serial_number,
135                 ao_radio_cal
136         };
137
138         public static int fetch_base(AltosHexfile hexfile) throws AltosNoSymbol {
139                 int     base = 0x7fffffff;
140                 for (String name : fetch_names) {
141                         int     addr = find_offset(hexfile, name, 2) + hexfile.address;
142                         if (addr < base)
143                                 base = addr;
144                 }
145                 return base;
146         }
147
148         public static int fetch_bounds(AltosHexfile hexfile) throws AltosNoSymbol {
149                 int     bounds = 0;
150                 for (String name : fetch_names) {
151                         int     addr = find_offset(hexfile, name, 2) + hexfile.address;
152                         if (addr > bounds)
153                                 bounds = addr;
154                 }
155                 return bounds + 2;
156         }
157
158         public void write (AltosHexfile hexfile) throws IOException {
159                 if (!valid)
160                         throw new IOException("rom configuration invalid");
161
162                 AltosRomconfig existing = new AltosRomconfig(hexfile);
163                 if (!existing.valid)
164                         throw new IOException("image does not contain existing rom config");
165
166                 try {
167                         switch (existing.version) {
168                         case 2:
169                                 put_usb_serial(serial_number, hexfile, ao_usb_descriptors);
170                         case 1:
171                                 put_int(serial_number, hexfile, ao_serial_number, 2);
172                                 put_int(radio_calibration, hexfile, ao_radio_cal, 4);
173                                 break;
174                         }
175                 } catch (AltosNoSymbol missing) {
176                         throw new IOException(missing.getMessage());
177                 }
178
179                 AltosRomconfig check = new AltosRomconfig(hexfile);
180                 if (!check.valid)
181                         throw new IOException("writing new rom config failed\n");
182         }
183
184         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
185                 valid = true;
186                 version = 1;
187                 check = (~version & 0xffff);
188                 serial_number = in_serial_number;
189                 radio_calibration = in_radio_calibration;
190         }
191
192         public boolean valid() {
193                 return valid && serial_number != 0;
194         }
195
196         public AltosRomconfig() {
197                 valid = false;
198         }
199 }