don't build all the "fat" jar deliverables by default
[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                 System.out.printf("Put usb serial %d\n", value);
61
62                 while (offset < bytes.length && bytes[offset] != 0) {
63                         if (bytes[offset + 1] == AO_USB_DESC_STRING) {
64                                 ++string_num;
65                                 if (string_num == 4)
66                                         break;
67                         }
68                         offset += ((int) bytes[offset]) & 0xff;
69                 }
70                 System.out.printf("offset %d content %d\n",
71                                   offset, bytes[offset]);
72                 if (offset >= bytes.length || bytes[offset] == 0)
73                         return;
74                 int len = ((((int) bytes[offset]) & 0xff) - 2) / 2;
75                 String fmt = String.format("%%0%dd", len);
76                 System.out.printf("existing serial length %d format %s\n", len, fmt);
77
78                 String s = String.format(fmt, value);
79                 if (s.length() != len) {
80                         System.out.printf("weird usb length issue %s isn't %d\n",
81                                           s, len);
82                         return;
83                 }
84                 for (int i = 0; i < len; i++) {
85                         bytes[offset + 2 + i*2] = (byte) s.charAt(i);
86                         bytes[offset + 2 + i*2+1] = 0;
87                 }
88         }
89
90         public AltosRomconfig(byte[] bytes, int offset) {
91                 version = get_int(bytes, offset + 0, 2);
92                 check = get_int(bytes, offset + 2, 2);
93                 System.out.printf("version %d check %d\n", version, check);
94                 if (check == (~version & 0xffff)) {
95                         switch (version) {
96                         case 2:
97                         case 1:
98                                 serial_number = get_int(bytes, offset + 4, 2);
99                                 radio_calibration = get_int(bytes, offset + 6, 4);
100                                 System.out.printf("serial %d cal %d\n", serial_number, radio_calibration);
101                                 valid = true;
102                                 break;
103                         }
104                 }
105         }
106
107         public AltosRomconfig(AltosHexfile hexfile) {
108                 this(hexfile.data, 0xa0 - hexfile.address);
109         }
110
111         public void write(byte[] bytes, int offset) throws IOException {
112                 if (!valid)
113                         throw new IOException("rom configuration invalid");
114
115                 if (offset < 0 || bytes.length < offset + 10)
116                         throw new IOException("image cannot contain rom config");
117
118                 AltosRomconfig existing = new AltosRomconfig(bytes, offset);
119                 if (!existing.valid)
120                         throw new IOException("image does not contain existing rom config");
121
122                 switch (existing.version) {
123                 case 2:
124                         put_usb_serial(serial_number, bytes, offset);
125                 case 1:
126                         put_int(serial_number, bytes, offset + 4, 2);
127                         put_int(radio_calibration, bytes, offset + 6, 4);
128                         break;
129                 }
130         }
131
132         public void write (AltosHexfile hexfile) throws IOException {
133                 write(hexfile.data, 0xa0 - hexfile.address);
134                 AltosRomconfig check = new AltosRomconfig(hexfile);
135                 if (!check.valid())
136                         throw new IOException("writing new rom config failed\n");
137         }
138
139         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
140                 valid = true;
141                 version = 1;
142                 check = (~version & 0xffff);
143                 serial_number = in_serial_number;
144                 radio_calibration = in_radio_calibration;
145         }
146
147         public boolean valid() {
148                 return valid && serial_number != 0;
149         }
150
151         public AltosRomconfig() {
152                 valid = false;
153         }
154 }