844da7c4b7aad021b969453a01616b2ed86fb0f1
[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         }
63
64         public AltosRomconfig(AltosHexfile hexfile) {
65                 this(hexfile.data, 0xa0 - hexfile.address);
66         }
67
68         public void write(byte[] bytes, int offset) throws IOException {
69                 if (!valid)
70                         throw new IOException("rom configuration invalid");
71
72                 if (offset < 0 || bytes.length < offset + 10)
73                         throw new IOException("image cannot contain rom config");
74
75                 AltosRomconfig existing = new AltosRomconfig(bytes, offset);
76                 if (!existing.valid)
77                         throw new IOException("image does not contain existing rom config");
78
79                 switch (existing.version) {
80                 case 1:
81                         put_int(serial_number, bytes, offset + 4, 2);
82                         put_int(radio_calibration, bytes, offset + 6, 4);
83                         break;
84                 }
85         }
86
87         public void write (AltosHexfile hexfile) throws IOException {
88                 write(hexfile.data, 0xa0 - hexfile.address);
89                 new AltosRomconfig(hexfile);
90         }
91
92         public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
93                 valid = true;
94                 version = 1;
95                 check = (~version & 0xffff);
96                 serial_number = in_serial_number;
97                 radio_calibration = in_radio_calibration;
98         }
99
100         public boolean valid() {
101                 return valid && serial_number != 0;
102         }
103
104         public AltosRomconfig() {
105                 valid = false;
106         }
107 }