pull up maps for arbitrary locations
[fw/altos] / ao-tools / altosui / 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 altosui;
19 import java.io.*;
20 import altosui.AltosHexfile;
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 int get_int(byte[] bytes, int start, int len) {
30                 int     v = 0;
31                 int     o = 0;
32                 while (len > 0) {
33                         v = v | ((((int) bytes[start]) & 0xff) << o);
34                         start++;
35                         len--;
36                         o += 8;
37                 }
38                 return v;
39         }
40
41         static void put_int(int value, byte[] bytes, int start, int len) {
42                 while (len > 0) {
43                         bytes[start] = (byte) (value & 0xff);
44                         start++;
45                         len--;
46                         value >>= 8;
47                 }
48         }
49
50         static void put_string(String value, byte[] bytes, int start) {
51                 for (int i = 0; i < value.length(); i++)
52                         bytes[start + i] = (byte) value.charAt(i);
53         }
54
55         static final int AO_USB_DESC_STRING     = 3;
56
57         static void put_usb_serial(int value, byte[] bytes, int start) {
58                 int offset = start + 0xa;
59                 int string_num = 0;
60
61                 while (offset < bytes.length && bytes[offset] != 0) {
62                         if (bytes[offset + 1] == AO_USB_DESC_STRING) {
63                                 ++string_num;
64                                 if (string_num == 4)
65                                         break;
66                         }
67                         offset += ((int) bytes[offset]) & 0xff;
68                 }
69                 if (offset >= bytes.length || bytes[offset] == 0)
70                         return;
71                 int len = ((((int) bytes[offset]) & 0xff) - 2) / 2;
72                 String fmt = String.format("%%0%dd", len);
73
74                 String s = String.format(fmt, value);
75                 if (s.length() != len) {
76                         System.out.printf("weird usb length issue %s isn't %d\n",
77                                           s, len);
78                         return;
79                 }
80                 for (int i = 0; i < len; i++) {
81                         bytes[offset + 2 + i*2] = (byte) s.charAt(i);
82                         bytes[offset + 2 + i*2+1] = 0;
83                 }
84         }
85
86         public AltosRomconfig(byte[] bytes, int offset) {
87                 version = get_int(bytes, offset + 0, 2);
88                 check = get_int(bytes, offset + 2, 2);
89                 if (check == (~version & 0xffff)) {
90                         switch (version) {
91                         case 2:
92                         case 1:
93                                 serial_number = get_int(bytes, offset + 4, 2);
94                                 radio_calibration = get_int(bytes, offset + 6, 4);
95                                 valid = true;
96                                 break;
97                         }
98                 }
99         }
100
101         public AltosRomconfig(AltosHexfile hexfile) {
102                 this(hexfile.data, 0xa0 - hexfile.address);
103         }
104
105         public void write(byte[] bytes, int offset) throws IOException {
106                 if (!valid)
107                         throw new IOException("rom configuration invalid");
108
109                 if (offset < 0 || bytes.length < offset + 10)
110                         throw new IOException("image cannot contain rom config");
111
112                 AltosRomconfig existing = new AltosRomconfig(bytes, offset);
113                 if (!existing.valid)
114                         throw new IOException("image does not contain existing rom config");
115
116                 switch (existing.version) {
117                 case 2:
118                         put_usb_serial(serial_number, bytes, offset);
119                 case 1:
120                         put_int(serial_number, bytes, offset + 4, 2);
121                         put_int(radio_calibration, bytes, offset + 6, 4);
122                         break;
123                 }
124         }
125
126         public void write (AltosHexfile hexfile) throws IOException {
127                 write(hexfile.data, 0xa0 - hexfile.address);
128                 AltosRomconfig check = new AltosRomconfig(hexfile);
129                 if (!check.valid())
130                         throw new IOException("writing new rom config failed\n");
131         }
132
133         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
134                 valid = true;
135                 version = 1;
136                 check = (~version & 0xffff);
137                 serial_number = in_serial_number;
138                 radio_calibration = in_radio_calibration;
139         }
140
141         public boolean valid() {
142                 return valid && serial_number != 0;
143         }
144
145         public AltosRomconfig() {
146                 valid = false;
147         }
148 }