2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
22 import java.util.LinkedList;
23 import java.util.Arrays;
25 class HexFileInputStream extends PushbackInputStream {
28 public HexFileInputStream(FileInputStream o) {
29 super(new BufferedInputStream(o));
33 public int read() throws IOException {
40 public void unread(int c) throws IOException {
48 class HexRecord implements Comparable<Object> {
54 static final int NORMAL = 0;
55 static final int EOF = 1;
56 static final int EXTENDED_ADDRESS = 2;
70 boolean ishex(int c) {
71 if ('0' <= c && c <= '9')
73 if ('a' <= c && c <= 'f')
75 if ('A' <= c && c <= 'F')
80 boolean isspace(int c) {
90 if ('0' <= c && c <= '9')
92 if ('a' <= c && c <= 'f')
94 if ('A' <= c && c <= 'F')
99 public byte checksum() {
103 got += (address >> 8) & 0xff;
104 got += (address ) & 0xff;
106 for (int i = 0; i < data.length; i++)
108 return (byte) (-got);
111 public int compareTo(Object other) {
112 HexRecord o = (HexRecord) other;
113 return address - o.address;
116 public String toString() {
117 return String.format("%04x: %02x (%d)", address, type, data.length);
120 public HexRecord(HexFileInputStream input) throws IOException, EOFException {
121 read_state state = read_state.marker;
127 while (state != read_state.done) {
128 int c = input.read();
129 if (c < 0 && state != read_state.white && state != read_state.marker)
130 throw new IOException(String.format("%d: Unexpected EOF", input.line));
135 if (c == EOF || c == -1)
136 throw new EOFException();
138 throw new IOException(String.format ("Missing ':' (got %x)", c));
139 state = read_state.length;
149 throw new IOException(String.format("Non-hex char '%c'", c));
150 hex = hex << 4 | fromhex(c);
157 data = new byte[hex];
158 state = read_state.address;
163 state = read_state.type;
169 state = read_state.data;
171 state = read_state.checksum;
176 data[ndata] = (byte) hex;
179 if (ndata == data.length)
180 state = read_state.checksum;
183 checksum = (byte) hex;
184 state = read_state.newline;
192 if (c != '\n' && c != '\r')
193 throw new IOException("Missing newline");
194 state = read_state.white;
199 state = read_state.done;
206 got_checksum = checksum();
207 if (got_checksum != checksum)
208 throw new IOException(String.format("Invalid checksum (read 0x%02x computed 0x%02x)\n",
209 checksum, got_checksum));
213 public class AltosHexfile {
216 LinkedList<AltosHexsym> symlist = new LinkedList<AltosHexsym>();
218 public byte get_byte(int a) {
219 return data[a - address];
222 /* CC1111-based products have the romconfig stuff located
223 * at a fixed address; when the file we load has no symbols,
224 * assume it is one of those and set the symbols appropriately
226 final static int ao_romconfig_version_addr = 0xa0;
227 final static int ao_romconfig_check_addr = 0xa2;
228 final static int ao_serial_number_addr = 0xa4;
229 final static int ao_radio_cal_addr = 0xa6;
230 final static int ao_usb_descriptors_addr = 0xaa;
232 static AltosHexsym[] cc_symbols = {
233 new AltosHexsym("ao_romconfig_version", ao_romconfig_version_addr),
234 new AltosHexsym("ao_romconfig_check", ao_romconfig_check_addr),
235 new AltosHexsym("ao_serial_number", ao_serial_number_addr),
236 new AltosHexsym("ao_radio_cal", ao_radio_cal_addr),
237 new AltosHexsym("ao_usb_descriptors", ao_usb_descriptors_addr)
240 private void add_cc_symbols() {
241 for (int i = 0; i < cc_symbols.length; i++)
242 symlist.add(cc_symbols[i]);
245 public void add_symbol(AltosHexsym symbol) {
249 /* Take symbols from another hexfile and duplicate them here */
250 public void add_symbols(AltosHexfile other) {
251 for (AltosHexsym symbol : other.symlist)
255 public AltosHexsym lookup_symbol(String name) {
256 if (symlist.isEmpty())
259 for (AltosHexsym symbol : symlist)
260 if (name.equals(symbol.name))
265 private String make_string(byte[] data, int start, int length) {
267 for (int i = 0; i < length; i++)
268 s += (char) data[start + i];
272 public AltosHexfile(byte[] bytes, int offset) {
277 public AltosHexfile(FileInputStream file) throws IOException {
278 HexFileInputStream input = new HexFileInputStream(file);
279 LinkedList<HexRecord> record_list = new LinkedList<HexRecord>();
280 boolean done = false;
284 HexRecord record = new HexRecord(input);
286 record_list.add(record);
287 } catch (EOFException eof) {
292 long extended_addr = 0;
296 for (HexRecord record : record_list) {
298 switch (record.type) {
300 addr = extended_addr + record.address;
301 long r_bound = addr + record.data.length;
302 if (!set || addr < base)
304 if (!set || r_bound > bound)
311 if (record.data.length != 2)
312 throw new IOException("invalid extended segment address record");
313 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 4;
316 if (record.data.length != 2)
317 throw new IOException("invalid extended segment address record");
318 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 16;
321 String name = make_string(record.data, 0, record.data.length);
322 addr = extended_addr + record.address;
323 AltosHexsym s = new AltosHexsym(name, addr);
327 throw new IOException ("invalid hex record type");
331 if (!set || base >= bound)
332 throw new IOException("invalid hex file");
334 if (bound - base > 4 * 1024 * 1024)
335 throw new IOException("hex file too large");
337 data = new byte[(int) (bound - base)];
338 address = (int) base;
339 Arrays.fill(data, (byte) 0xff);
341 /* Paint the records into the new array */
342 for (HexRecord record : record_list) {
343 switch (record.type) {
345 long addr = extended_addr + record.address;
346 long r_bound = addr + record.data.length;
347 for (int j = 0; j < record.data.length; j++)
348 data[(int) (addr - base) + j] = record.data[j];
353 if (record.data.length != 2)
354 throw new IOException("invalid extended segment address record");
355 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 4;
358 if (record.data.length != 2)
359 throw new IOException("invalid extended segment address record");
360 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 16;
365 throw new IOException ("invalid hex record type");