forked from queueRAM/pilotwings_64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pw64_filesys_dump.py
executable file
·165 lines (151 loc) · 7.72 KB
/
pw64_filesys_dump.py
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
#!/usr/bin/env python
import argparse
import struct
import sys
def auto_int(x):
return int(x, 0)
def decompress_mio0(raw_bytes):
magic = raw_bytes[:4]
assert magic == b'MIO0'
uncompressed_size, lengths_offs, data_offs = struct.unpack('>LLL', raw_bytes[4:16])
flags_offs = 0x10
output = b""
while True:
command_byte = raw_bytes[flags_offs]
flags_offs += 1
for i in reversed(range(8)):
if command_byte & (1 << i):
# Literal
uncompressed_size -= 1
output += bytes([raw_bytes[data_offs]])
data_offs += 1
else:
# LZSS
tmp, = struct.unpack('>H', raw_bytes[lengths_offs:lengths_offs+2])
lengths_offs += 2
window_offset = (tmp & 0x0FFF) + 1
window_length = (tmp >> 12) + 3
uncompressed_size -= window_length
for j in range(window_length):
output += bytes([output[-window_offset]])
if uncompressed_size <= 0:
return output
def print_hex_dump(raw_bytes):
count = 0
for b in raw_bytes:
if count % 16 == 0:
sys.stdout.write(' ' * 4)
sys.stdout.write('{:02x} '.format(b))
count += 1
if count % 16 == 0:
sys.stdout.write('\n')
if count % 16:
sys.stdout.write('\n')
def pw64_dump_filesys(fname, startOffset, hexSize):
def hexdump(raw_bytes):
if hexSize > 0:
if len(raw_bytes) > hexSize:
raw_bytes = raw_bytes[:hexSize]
print_hex_dump(raw_bytes)
with open(fname, 'rb') as fin:
fin.seek(startOffset)
dumping = True
while (dumping):
fileOffset = fin.tell()
sys.stdout.write('0x%06X|%06X: ' % (fileOffset, fileOffset - startOffset))
magic = fin.read(4)
magicInt = int.from_bytes(magic, byteorder='big')
if magicInt == 0:
print('00000000 [EOF]')
break
magicStr = magic.decode('utf-8')
if magicStr == 'FORM':
formLength = int.from_bytes(fin.read(4), byteorder='big')
formEnd = fin.tell() + formLength
print('%s: 0x%06X (end: 0x%06X)' % (magicStr, formLength, formEnd))
while (fin.tell() < formEnd):
fileOffset = fin.tell()
sys.stdout.write('0x%06X|%06X: ' % (fileOffset, fileOffset - startOffset))
magic = fin.read(4)
magicStr = magic.decode('utf-8')
# no length on these
if magicStr in ['UVSY', 'UVEN', 'UVLT', 'UVTR', 'UVLV',
'UVSQ', 'UVTP', 'UVMD', 'UVCT', 'UVTX',
'UVAN', 'UVFT', 'UPWL', 'SPTH', 'UPWT',
'ADAT', '3VUE', 'PDAT', 'UVBT', 'UVSX',
'UVRM']:
print(' %s' % magicStr)
blockType = magicStr
elif magicStr == 'NAME': # ASCII name identifier
length = int.from_bytes(fin.read(4), byteorder='big')
name = fin.read(length)
nameStr = name.decode('utf-8').rstrip('\0')
print(' %s: 0x%06X: %s' % (magicStr, length, nameStr))
elif magicStr == 'INFO': # usually mission objective
length = int.from_bytes(fin.read(4), byteorder='big')
info = fin.read(length)
infoStr = info.decode('utf-8').rstrip('\0')
print(' %s: 0x%06X: %s' % (magicStr, length, infoStr))
elif magicStr == 'JPTX': # some ASCII identifier
length = int.from_bytes(fin.read(4), byteorder='big')
info = fin.read(length)
infoStr = info.decode('utf-8').rstrip('\0')
print(' %s: 0x%06X: %s' % (magicStr, length, infoStr))
elif magicStr == 'GZIP': # not actually gzip, but MIO0 container
gzipLength = int.from_bytes(fin.read(4), byteorder='big')
absOffset = fin.tell() + gzipLength
decompType = fin.read(4)
decompTypeStr = decompType.decode('utf-8')
decompLength = int.from_bytes(fin.read(4), byteorder='big')
compBytes = fin.read(gzipLength - 8)
decompBytes = decompress_mio0(compBytes)
print(' %s: 0x%06X: %s' % (magicStr, gzipLength, decompTypeStr))
hexdump(decompBytes)
# generic handler for lengths that are not yet parsed
elif magicStr == 'COMM':
length = int.from_bytes(fin.read(4), byteorder='big')
sectionData = fin.read(length)
print(' %s: 0x%06X:' % (magicStr, length))
if blockType == 'UVSQ':
count = int(sectionData[0])
uvsq = '>Hf'
# +1 becuase last u16/float might be special
for i in range(count + 1):
(idx, val) = struct.unpack(uvsq, sectionData[1+6*i:7+6*i])
print(' 0x%04X: %f' % (idx, val))
else:
hexdump(sectionData)
# generic handler for lengths that are not yet parsed
elif magicStr in ['PART', 'STRG', 'FRMT', 'ESND',
'TPAD', 'CNTG', 'HOPD', 'LWIN', 'LSTP',
'TARG', 'FALC', 'BALS', 'HPAD', 'BTGT',
'THER', 'PHTS', 'SIZE', 'DATA', 'QUAT',
'XLAT', 'PHDR', 'RHDR', 'PPOS', 'RPKT',
'.CTL', '.TBL',
'SCPP', 'SCPH', 'SCPX', 'SCPY', 'SCPR', 'SCPZ', 'SCP#',
'LEVL', 'RNGS', 'BNUS', 'WOBJ', 'LPAD', 'TOYS', 'TPTS', 'APTS']:
length = int.from_bytes(fin.read(4), byteorder='big')
sectionData = fin.read(length)
print(' %s: 0x%06X:' % (magicStr, length))
hexdump(sectionData)
# PAD always seems to be 4 bytes of 0 - ignore it
elif magicStr in ['PAD ']:
length = int.from_bytes(fin.read(4), byteorder='big')
print(' %s: 0x%06X:' % (magicStr, length))
fin.seek(length, 1)
else:
nextInt = int.from_bytes(fin.read(4), byteorder='big')
print('unknown magic: %s [%08X]' % (magicStr, nextInt))
dumping = False
return
else:
print('unknown top magic: ' + ''.join('{:02x}'.format(x) for x in magic) + ' "' + magicStr + '"')
dumping = False
return
if __name__ == '__main__':
ap = argparse.ArgumentParser(description='Pilotwings 64 File System Dumper')
ap.add_argument('file', help='File path of input')
ap.add_argument('-s', '--start', dest='startOffset', type=auto_int, default=0x0DF5B0, help='Start offset of file system')
ap.add_argument('-x', '--hex', dest='hexSize', type=auto_int, default=0x60, help='Size of hexdump for unparsed sections')
args = ap.parse_args()
pw64_dump_filesys(args.file, args.startOffset, args.hexSize)