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