5a2df313b048b940108ddf70192640a0c41252c3
[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         public AltosRomconfig(byte[] bytes, int offset) {
51                 version = get_int(bytes, offset + 0, 2);
52                 check = get_int(bytes, offset + 2, 2);
53                 if (check == (~version & 0xffff)) {
54                         switch (version) {
55                         case 1:
56                                 serial_number = get_int(bytes, offset + 4, 2);
57                                 radio_calibration = get_int(bytes, offset + 6, 4);
58                                 valid = true;
59                                 break;
60                         }
61                 }
62                 System.out.printf("version 0x%x check 0x%x valid %s serial %d cal %d\n",
63                                   version, check, valid ? "true" : "false",
64                                   serial_number, radio_calibration);
65         }
66
67         public AltosRomconfig(AltosHexfile hexfile) {
68                 this(hexfile.data, 0xa0 - hexfile.address);
69         }
70
71         public void write(byte[] bytes, int offset) throws IOException {
72                 if (!valid)
73                         throw new IOException("rom configuration invalid");
74
75                 if (offset < 0 || bytes.length < offset + 10)
76                         throw new IOException("image cannot contain rom config");
77
78                 AltosRomconfig existing = new AltosRomconfig(bytes, offset);
79                 if (!existing.valid)
80                         throw new IOException("image does not contain existing rom config");
81
82                 switch (existing.version) {
83                 case 1:
84                         put_int(serial_number, bytes, offset + 4, 2);
85                         put_int(radio_calibration, bytes, offset + 6, 4);
86                         break;
87                 }
88         }
89
90         public void write (AltosHexfile hexfile) throws IOException {
91                 write(hexfile.data, 0xa0 - hexfile.address);
92                 new AltosRomconfig(hexfile);
93         }
94
95         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
96                 valid = true;
97                 version = 1;
98                 check = (~version & 0xffff);
99                 serial_number = in_serial_number;
100                 radio_calibration = in_radio_calibration;
101         }
102
103         public boolean valid() {
104                 return valid && serial_number != 0;
105         }
106
107         public AltosRomconfig() {
108                 valid = false;
109         }
110 }