summaryrefslogtreecommitdiffstats
path: root/contrib/award_plugin_roms/award_plugin_roms.pl
blob: 2b95eed10cbe5b070ca35af0765edcbf0b4f81da (plain) (blame)
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
#!/usr/bin/perl -w
use strict;
use FileHandle;
use integer;

sub unsigned_little_endian_to_value
{
	# Assumes the data is initially little endian
	my ($buffer) = @_;
	my $bytes = length($buffer);
	my $value = 0;
	my $i;
	for($i = $bytes -1; $i >= 0; $i--) {
		my $byte = unpack('C', substr($buffer, $i, 1));
		$value = ($value * 256) + $byte;
	}
	return $value;
}

sub decode_fixed_string
{
	my ($data, $bytes) = @_;
	return $data;
}

sub decode_pstring
{
	my ($buf_ref, $offset_ref) = @_;
	# Decode a pascal string
	my $offset = ${$offset_ref};
	my $len = unpack('C',substr(${$buf_ref}, $offset, 1));
	my $data = substr(${$buf_ref}, $offset +1,  $len);
	${$offset_ref} = $offset + $len +1;
	return $data;
}

sub decode_cstring
{
	# Decode a c string
	my ($buf_ref, $offset_ref) = @_;
	my ($data, $byte);
	my $index = ${$offset_ref};
	while(1) {
		$byte = substr(${$buf_ref}, $index, 1);
		if (!defined($byte) || ($byte eq "\0")) {
			last;
		}
		$data .= $byte;
		$index++;
	}
	${$offset_ref} = $index;
	return $data;
}

sub type_size
{
	my ($entry) = @_;
	my %type_length = (
		byte => 1,
		half => 2,
		word => 4,
		xword => 8,
		'fixed-string' => $entry->[2],
		pstring => 0,
		cstring => 0,
	);
	my $type = $entry->[0];
	if (!exists($type_length{$type})) {
		 die "unknown type $type";
	 }
	my $length = $type_length{$type};
	return $length;
}

sub decode_fixed_type
{
	my ($type, $data, $bytes) = @_;
	my %decoders = (
		'byte' => \&unsigned_little_endian_to_value,
		'half' => \&unsigned_little_endian_to_value,
		'word' => \&unsigned_little_endian_to_value,
		'xword' => \&unsigned_little_endian_to_value,
		'fixed-string' => \&decode_fixed_string,
	);
	my $decoder = $decoders{$type} or die "unknow fixed type $type";
	return $decoder->($data, $bytes);
}

sub decode_variable_type
{
	my ($type, $buf_ref, $offset_ref) = @_;
	my %decoders = (
		'pstring' => \&decode_pstring,
		'cstring' => \&decode_cstring,
	);
	my $decoder = $decoders{$type} or die "unknow variable type $type";
	return $decoder->($buf_ref, $offset_ref);
}

sub decode_struct
{
	my ($buf_ref, $offset, $layout) = @_;
	my $initial_offset = $offset;
	my ($entry, %results);
	foreach $entry (@$layout) {
		my ($type, $name) = @$entry;
		my $bytes = type_size($entry);
		if ($bytes > 0) {
			my $data = substr(${$buf_ref}, $offset, $bytes);
			$results{$name} = decode_fixed_type($type, $data, $bytes);
			$offset += $bytes;
		} else {
			$results{$name} = decode_variable_type($type, $buf_ref, \$offset);
		}
	}
	return (\%results, $offset - $initial_offset);
}

sub print_big_hex
{
	my ($min_digits, $value) = @_;
	my @digits;
	while($min_digits > 0 || ($value > 0)) {
		my $digit = $value%16;
		$value /= 16;
		unshift(@digits, $digit);
		$min_digits--;
	}
	my $digit;
	foreach $digit (@digits) {
		printf("%01x", $digit);
	}
}



my %lha_signatures = (
	'-com-' => 1,	
	'-lhd-'	=> 1,
	'-lh0-'	=> 1,
	'-lh1-'	=> 1,
	'-lh2-'	=> 1,
	'-lh3-'	=> 1,
	'-lh4-'	=> 1,
	'-lh5-'	=> 1,
	'-lzs-' => 1,
	'-lz4-' => 1,
	'-lz5-' => 1,
	'-afx-'	=> 1,
	'-lzf-'	=> 1,
);

my %lha_os = (
	'M' => 'MS-DOS',
	'2' => 'OS/2',
	'9' => 'OS9',
	'K' => 'OS/68K',
	'3' => 'OS/386',
	'H' => 'HUMAN',
	'U' => 'UNIX',
	'C' => 'CP/M',
	'F' => 'FLEX',
	'm' => 'Mac',
	'R' => 'Runser',
	'T' => 'TownOS',
	'X' => 'XOSK',
	'A' => 'Amiga',
	'a' => 'atari',
	' ' => 'Award ROM',
);


my @lha_level_1_header = (
	[ 'byte',         'header_size' ],    # 1
	[ 'byte',         'header_sum', ],    # 2
	[ 'fixed-string', 'method_id', 5 ],   # 7
	[ 'word',         'skip_size', ],     # 11
	[ 'word',         'original_size' ],  # 15
	[ 'half',         'dos_time' ],       # 17
	[ 'half',         'dos_date' ],       # 19
    	[ 'byte',         'fixed'   ],        # 20
	[ 'byte',         'level'   ],        # 21
	[ 'pstring',      'filename' ],       # 22
	[ 'half',         'crc' ],
	[ 'fixed-string', 'os_id', 1 ],
	[ 'half',         'ext_size' ],		  
);

# General lha_header
my @lha_header = (
	[ 'byte',         'header_size' ],
	[ 'byte',         'header_sum', ],
	[ 'fixed-string', 'method_id', 5 ],
	[ 'word',         'skip_size', ],
	[ 'word',         'original_size' ],
	[ 'half',         'dos_time' ],
	[ 'half',         'dos_date' ],
	[ 'half',         'rom_addr' ],
	[ 'half',         'rom_flags' ],
    	[ 'byte',         'fixed'   ],
	[ 'byte',         'level'   ],
	[ 'pstring',      'filename' ],
	[ 'half',         'crc' ],
	[ 'lha_os',	  'os_id', 1 ],
	[ 'half',         'ext_size' ],
	[ 'byte',         'zero' ],
	[ 'byte',         'total_checksum' ],
	[ 'half',         'total_size' ],
);

sub print_struct
{
	my ($layout, $self) = @_;
	my $entry;
	my $width = 0;
	foreach $entry(@$layout) {
		my ($type, $name) = @$entry;
		if (length($name) > $width) {
			$width = length($name);
		}
	}
	foreach $entry (@$layout) {
		my ($type, $name) = @$entry;
		printf("%*s = ", $width, $name);
		my $value = $self->{$name};
		if (!defined($value)) {
			print "undefined";
		}
		elsif ($type eq "lha_os") {
			print "$lha_os{$value}";
		}
		elsif ($type =~ m/string/) {
			print "$value";
		} 
		else {
			my $len = type_size($entry);
			print "0x";
			print_big_hex($len *2, $value);
		}
		print "\n";
	}
}

sub checksum
{
	my ($buf_ref, $offset, $length) = @_;
	my ($i, $sum);
	$sum = 0;
	for($i = 0; $i < $length; $i++) {
		my $byte = unpack('C', substr($$buf_ref, $offset + $i, 1));
		$sum = ($sum + $byte) %256;
	}
	return $sum;
}

sub decode_lha_header
{
	my ($buf_ref, $offset) = @_;
	my $level = unpack('C',substr(${$buf_ref}, $offset + 20, 1));

	my %self;
	my ($struct, $bytes);
	if ($level == 1) {
		($struct, $bytes) 
			= decode_struct($buf_ref, $offset, \@lha_level_1_header);
		%self = %$struct;
		if ($self{fixed} != 0x20) {
			 die "bad fixed value";
		}
		$self{total_size} = $self{header_size} + 2 + $self{skip_size};
		if ($bytes != $self{header_size} +2) {
			die "$bytes != $self{header_size} +2";
		}
		my $checksum = checksum($buf_ref, $offset +2, $self{header_size});
		if ($checksum != $self{header_sum}) {
			printf("WARN: Header bytes checksum to %02lx\n", 
				     $checksum);
		}
		# If we are an award rom...
		if ($self{os_id} eq ' ') {
			@self{qw(zero total_checksum)} = 
			    unpack('CC', substr($$buf_ref, 
				$offset + $self{total_size}, 2));
			if ($self{zero} != 0) {
				warn "Award ROM without trailing zero";
			}
			else {
				$self{total_size}++;
			}
			my $checksum = 
				checksum($buf_ref, $offset, $self{total_size});
			if ($self{total_checksum} != $checksum) {
				printf("WARN: Image bytes checksum to %02lx\n", 
					$checksum);
			}
			else {
				$self{total_size}++;
			}
			$self{rom_addr} = $self{dos_time};
			$self{rom_flags} = $self{dos_date};
			delete @self{qw(dos_time dos_date)};
		}
	}
	else {
		die "Unknown header type";
	}
	return \%self;
}

sub main
{
	my ($filename, $rom_length) = @_;
	my $fd = new FileHandle;
	if (!defined($rom_length)) {
		my ($dev, $ino, $mode, $nlink, $uid, $gid,$rdev,$size,
			$atime, $mtime, $ctime, $blksize, $blocks)
			= stat($filename);
		$rom_length = $size;
	}
	$fd->open("<$filename") or die "Cannot ope $filename";
	my $data;
	$fd->read($data, $rom_length);
	$fd->close();
	
	my $i;
	for($i = 0; $i < $rom_length; $i++) {
		my $sig = substr($data, $i, 5);
		if (exists($lha_signatures{$sig})) {
			my $start = $i -2;
			my $header = decode_lha_header(\$data, $start);
			
			my $length = $header->{total_size};
			print "AT:  $start - @{[$start + $length -1]},  $length bytes\n";
			print_struct(\@lha_header, $header);
			print "\n";

		}
	}
}

main(@ARGV);