[go: up one dir, main page]

File: AltosHexfile.java

package info (click to toggle)
altos 1.9-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 67,272 kB
  • sloc: ansic: 90,607; java: 39,048; makefile: 6,591; sh: 3,008; xml: 1,972; pascal: 1,597
file content (480 lines) | stat: -rw-r--r-- 11,422 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altoslib_13;

import java.io.*;
import java.util.LinkedList;
import java.util.Arrays;

class HexFileInputStream extends PushbackInputStream {
	public int line;

	public HexFileInputStream(FileInputStream o) {
		super(new BufferedInputStream(o));
		line = 1;
	}

	public int read() throws IOException {
		int	c = super.read();
		if (c == '\n')
			line++;
		return c;
	}

	public void unread(int c) throws IOException {
		if (c == '\n')
			line--;
		if (c != -1)
			super.unread(c);
	}
}

class HexRecord implements Comparable<Object> {
	public long	address;
	public int	type;
	public byte	checksum;
	public byte[]	data;

	static final int NORMAL = 0;
	static final int EOF = 1;
	static final int EXTENDED_ADDRESS = 2;

	enum read_state {
		marker,
		length,
		address,
		type,
		data,
		checksum,
		newline,
		white,
		done,
	}

	boolean ishex(int c) {
		if ('0' <= c && c <= '9')
			return true;
		if ('a' <= c && c <= 'f')
			return true;
		if ('A' <= c && c <= 'F')
			return true;
		return false;
	}

	boolean isspace(int c) {
		switch (c) {
		case ' ':
		case '\t':
			return true;
		}
		return false;
	}

	int fromhex(int c) {
		if ('0' <= c && c <= '9')
			return c - '0';
		if ('a' <= c && c <= 'f')
			return c - 'a' + 10;
		if ('A' <= c && c <= 'F')
			return c - 'A' + 10;
		return -1;
	}

	public byte checksum() {
		byte	got = 0;

		got += data.length;
		got += (address >> 8) & 0xff;
		got += (address     ) & 0xff;
		got += type;
		for (int i = 0; i < data.length; i++)
			got += data[i];
		return (byte) (-got);
	}

	public int compareTo(Object other) {
		HexRecord	o = (HexRecord) other;

		long diff = address - o.address;

		if (diff > 0)
			return 1;
		if (diff < 0)
			return -1;
		return 0;
	}

	public String toString() {
		return String.format("%04x: %02x (%d)", address, type, data.length);
	}

	public HexRecord(HexFileInputStream input) throws IOException, EOFException {
		read_state	state = read_state.marker;
		long		nhexbytes = 0;
		long		hex = 0;
		int		ndata = 0;
		byte		got_checksum;

		while (state != read_state.done) {
			int c = input.read();
			if (c < 0 && state != read_state.white && state != read_state.marker)
				throw new IOException(String.format("%d: Unexpected EOF", input.line));
			if (c == ' ')
				continue;
			switch (state) {
			case marker:
				if (c == EOF || c == -1)
					throw new EOFException();
				if (c != ':')
					throw new IOException(String.format ("Missing ':' (got %x)", c));
				state = read_state.length;
				nhexbytes = 2;
				hex = 0;
				break;
			case length:
			case address:
			case type:
			case data:
			case checksum:
				if(!ishex(c))
					throw new IOException(String.format("Non-hex char '%c'", c));
				hex = hex << 4 | fromhex(c);
				--nhexbytes;
				if (nhexbytes != 0)
					break;

				switch (state) {
				case length:
					data = new byte[(int) hex];
					state = read_state.address;
					nhexbytes = 4;
					break;
				case address:
					address = hex;
					state = read_state.type;
					nhexbytes = 2;
					break;
				case type:
					type = (int) hex;
					if (data.length > 0)
						state = read_state.data;
					else
						state = read_state.checksum;
					nhexbytes = 2;
					ndata = 0;
					break;
				case data:
					data[ndata] = (byte) hex;
					ndata++;
					nhexbytes = 2;
					if (ndata == data.length)
						state = read_state.checksum;
					break;
				case checksum:
					checksum = (byte) hex;
					state = read_state.newline;
					break;
				default:
					break;
				}
				hex = 0;
				break;
			case newline:
				if (c != '\n' && c != '\r')
					throw new IOException("Missing newline");
				state = read_state.white;
				break;
			case white:
				if (!isspace(c)) {
					input.unread(c);
					state = read_state.done;
				}
				break;
			case done:
				break;
			}
		}
		got_checksum = checksum();
		if (got_checksum != checksum)
			throw new IOException(String.format("Invalid checksum (read 0x%02x computed 0x%02x)\n",
							    checksum, got_checksum));
	}
}

public class AltosHexfile {
	public long		address;
	public long		max_address;
	public byte[]		data;
	LinkedList<AltosHexsym>	symlist = new LinkedList<AltosHexsym>();

	public byte get_byte(long a) {
		return data[(int) (a - address)];
	}

	public int get_u8(long a) {
		return ((int) get_byte(a)) & 0xff;
	}

	public int get_u16(long a) {
		return get_u8(a) | (get_u8(a+1) << 8);
	}

	/* CC1111-based products have the romconfig stuff located
	 * at a fixed address; when the file we load has no symbols,
	 * assume it is one of those and set the symbols appropriately
	 */
	final static int ao_romconfig_version_addr = 0xa0;
	final static int ao_romconfig_check_addr = 0xa2;
	final static int ao_serial_number_addr = 0xa4;
	final static int ao_radio_cal_addr = 0xa6;
	final static int ao_usb_descriptors_addr = 0xaa;

	static AltosHexsym[] cc_symbols = {
		new AltosHexsym("ao_romconfig_version", ao_romconfig_version_addr),
		new AltosHexsym("ao_romconfig_check", ao_romconfig_check_addr),
		new AltosHexsym("ao_serial_number", ao_serial_number_addr),
		new AltosHexsym("ao_radio_cal", ao_radio_cal_addr),
		new AltosHexsym("ao_usb_descriptors", ao_usb_descriptors_addr)
	};

	static final int AO_USB_DESC_DEVICE		= 1;
	static final int AO_USB_DESC_STRING		= 3;

	static final int AO_ROMCONFIG_VERSION_INDEX	= 0;
	static final int AO_ROMCONFIG_CHECK_INDEX	= 1;
	static final int AO_SERIAL_NUMBER_INDEX		= 2;
	static final int AO_RADIO_CAL_INDEX		= 3;
	static final int AO_USB_DESCRIPTORS_INDEX	= 4;

	private void add_cc_symbols() {
		for (int i = 0; i < cc_symbols.length; i++)
			symlist.add(cc_symbols[i]);
	}

	public void add_symbol(AltosHexsym symbol) {
		symlist.add(symbol);
	}

	/* Take symbols from another hexfile and duplicate them here */
	public void add_symbols(AltosHexfile other) {
		for (AltosHexsym symbol : other.symlist)
			symlist.add(symbol);
	}

	public AltosHexsym lookup_symbol(String name) {
		if (symlist.isEmpty())
			add_cc_symbols();

		for (AltosHexsym symbol : symlist)
			if (name.equals(symbol.name))
				return symbol;
		return null;
	}

	private long find_usb_descriptors() {
		AltosHexsym	usb_descriptors = lookup_symbol("ao_usb_descriptors");
		long		a;

		if (usb_descriptors == null)
			return -1;

		try {
			/* Walk the descriptors looking for the device */
			a = usb_descriptors.address;
			while (get_u8(a+1) != AO_USB_DESC_DEVICE) {
				int delta = get_u8(a);
				a += delta;
				if (delta == 0 || a >= max_address)
					return -1;
			}
			return a;
		} catch (ArrayIndexOutOfBoundsException ae) {
			return -1;
		}
	}

	public AltosUsbId find_usb_id() {
		long a = find_usb_descriptors();

		if (a == -1)
			return null;

		/* Walk the descriptors looking for the device */
		while (get_u8(a+1) != AO_USB_DESC_DEVICE) {
			int delta = get_u8(a);
			a += delta;
			if (delta == 0 || a >= max_address)
				return null;
		}

		return new AltosUsbId(get_u16(a + 8),
				      get_u16(a + 10));
	}

	public String find_usb_product() {
		long		a = find_usb_descriptors();
		int		num_strings;
		int		product_string;

		if (a == -1)
			return null;

		product_string = get_u8(a+15);

		/* Walk the descriptors looking for the device */
		num_strings = 0;
		for (;;) {
			if (get_u8(a+1) == AO_USB_DESC_STRING) {
				++num_strings;
				if (num_strings == product_string + 1)
					break;
			}

			int delta = get_u8(a);
			a += delta;
			if (delta == 0 || a >= max_address)
				return null;
		}

		int product_len = get_u8(a);

		if (product_len <= 0)
			return null;

		String product = "";

		for (int i = 0; i < product_len - 2; i += 2) {
			int	c = get_u16(a + 2 + i);

			product += Character.toString((char) c);
		}

		if (AltosLink.debug)
			System.out.printf("product %s\n", product);

		return product;
	}

	private String make_string(byte[] data, int start, int length) {
		String s = "";
		for (int i = 0; i < length; i++)
			s += (char) data[start + i];
		return s;
	}

	public AltosHexfile(byte[] bytes, long offset) {
		data = bytes;
		address = offset;
		max_address = address + bytes.length;
	}

	public AltosHexfile(FileInputStream file) throws IOException {
		HexFileInputStream	input = new HexFileInputStream(file);
		LinkedList<HexRecord>	record_list = new LinkedList<HexRecord>();
		boolean			done = false;

		while (!done) {
			try {
				HexRecord	record = new HexRecord(input);

				record_list.add(record);
			} catch (EOFException eof) {
				done = true;
			}
		}

		long	extended_addr = 0;
		long	base = 0;
		long	bound = 0;
		boolean	set = false;
		for (HexRecord record : record_list) {
			long addr;
			switch (record.type) {
			case 0:
				addr = extended_addr + record.address;
				long r_bound = addr + record.data.length;
				if (!set || addr < base)
					base = addr;
				if (!set || r_bound > bound)
					bound = r_bound;
				set = true;
				break;
			case 1:
				break;
			case 2:
				if (record.data.length != 2)
					throw new IOException("invalid extended segment address record");
				extended_addr = ((record.data[0] << 8) + (record.data[1])) << 4;
				break;
			case 4:
				if (record.data.length != 2)
					throw new IOException("invalid extended segment address record");
				extended_addr = ((record.data[0] << 8) + (record.data[1])) << 16;
				break;
			case 0xfe:
				String name = make_string(record.data, 0, record.data.length);
				addr = extended_addr + record.address;
				AltosHexsym s = new AltosHexsym(name, addr);
				symlist.add(s);
				break;
			default:
				throw new IOException ("invalid hex record type");
			}
		}

		if (!set || base >= bound)
			throw new IOException("invalid hex file");

		if (bound - base > 4 * 1024 * 1024)
			throw new IOException("hex file too large");

		data = new byte[(int) (bound - base)];
		address = base;
		max_address = bound;
		Arrays.fill(data, (byte) 0xff);

		/* Paint the records into the new array */
		for (HexRecord record : record_list) {
			switch (record.type) {
			case 0:
				long addr = extended_addr + record.address;
				long r_bound = addr + record.data.length;
				for (int j = 0; j < record.data.length; j++)
					data[(int) (addr - base) + j] = record.data[j];
				break;
			case 1:
				break;
			case 2:
				if (record.data.length != 2)
					throw new IOException("invalid extended segment address record");
				extended_addr = ((record.data[0] << 8) + (record.data[1])) << 4;
				break;
			case 4:
				if (record.data.length != 2)
					throw new IOException("invalid extended segment address record");
				extended_addr = ((record.data[0] << 8) + (record.data[1])) << 16;
				break;
			case 0xfe:
				break;
			default:
				throw new IOException ("invalid hex record type");
			}
		}
	}
}