-
Notifications
You must be signed in to change notification settings - Fork 2
/
rcx_unpack
executable file
·52 lines (41 loc) · 1.32 KB
/
rcx_unpack
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
#!/usr/bin/env python
import pathlib
import sys
import rcx
import struct
infile = open(sys.argv[1], 'rb')
outdir = pathlib.Path(sys.argv[2])
outdir.mkdir(exist_ok=True)
rcx_header = bytearray(infile.read(1))
while rcx_header[-1:] != b'\f':
rcx_header.extend(infile.read(1))
with open(outdir / 'rcx_header', 'w') as fp:
fp.write(rcx_header.decode('ascii'))
def unpack_ipl(infile, index):
header = infile.read(0x1000)
segment_record_bytes, = struct.unpack('<H', header[0xe:0x10])
assert segment_record_bytes == 3*0x24
segments_found = []
for i in range(3):
base = 0x10 + 0x24*i
segment_header = header[base:base+0x24]
length, = struct.unpack('<L', segment_header[0xc:0x10])
if length:
segments_found.append(length)
with open(outdir / ('ipl_%d_hdr' % index), 'wb') as fp:
fp.write(header)
for seg, length in enumerate(segments_found):
with open(outdir / ('ipl_%d_seg%d' % (index, seg)), 'wb') as fp:
fp.write(infile.read(length))
index = 0
while True:
pos = infile.tell()
signature = infile.read(9)
if signature == b'': # EOW
break
infile.seek(pos, 0)
if signature != b'EPSON IPL':
print("ERROR: unexpected data at offset 0x%x" % pos)
sys.exit(1)
unpack_ipl(infile, index)
index += 1