-
Notifications
You must be signed in to change notification settings - Fork 5
/
db-extract.py
executable file
·134 lines (101 loc) · 3.63 KB
/
db-extract.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
#!/usr/bin/env python3
import os
import struct
import sys
import savefilesystem
def main():
if len(sys.argv) < 2:
print("Usage: %s input [output]" % sys.argv[0])
exit(1)
inputPath = None
outputPath = None
i = 1
while i < len(sys.argv):
if inputPath is None:
inputPath = sys.argv[i]
else:
outputPath = sys.argv[i]
i += 1
if outputPath is None:
print("No output directory given. Will only do data checking.")
file = open(inputPath, 'rb')
magic, magic2, b, c = struct.unpack('<IIII', file.read(0x10))
if magic == 0x4B434954:
print("Info: magic = TICK")
elif magic == 0x444E414E:
print("Info: magic = NAND")
file.read(0x70)
elif magic == 0x504D4554:
print("Info: magic = TEMP")
file.read(0x70)
else:
print("Error: unknown magic")
exit(1)
print("Info: Pre Header 0x%08X 0x%08X 0x%08X" % (magic2, b, c))
dbri = file.read()
file.close()
BDRI, ver, filesystemHeaderOff, imageSize, imageBlockSize, x00 \
= struct.unpack('<IIQQII', dbri[0:0x20])
if BDRI != 0x49524442:
print("Error: Wrong BDRI magic")
exit(1)
if ver != 0x00030000:
print("Error: Wrong BDRI version")
exit(1)
if x00 != 0:
print("Warning: unknown 0 = 0x%X in BDRI header" % x00)
fsHeader = savefilesystem.Header(
dbri[filesystemHeaderOff: filesystemHeaderOff+0x68], False)
dataRegion = dbri[
fsHeader.dataRegionOff: fsHeader.dataRegionOff +
fsHeader.dataRegionSize * fsHeader.blockSize]
# Parses hash tables
dirHashTable = savefilesystem.getHashTable(fsHeader.dirHashTableOff,
fsHeader.dirHashTableSize,
dbri)
fileHashTable = savefilesystem.getHashTable(fsHeader.fileHashTableOff,
fsHeader.fileHashTableSize,
dbri)
# Parses FAT
fat = savefilesystem.FAT(fsHeader, dbri)
# Parses directory & file entry table
dirList = savefilesystem.getTdbDirList(
fsHeader, dataRegion, fat)
print("Directory list:")
for i in range(len(dirList)):
dirList[i].printEntry(i)
fileList = savefilesystem.getTdbFileList(
fsHeader, dataRegion, fat)
print("File list:")
for i in range(len(fileList)):
fileList[i].printEntry(i)
# Verifies directory & file hash table
print("Verifying directory hash table")
savefilesystem.verifyHashTable(dirHashTable, dirList)
print("Verifying file hash table")
savefilesystem.verifyHashTable(fileHashTable, fileList)
# Walks through free blocks
print("Walking through free blocks")
fat.visitFreeBlock()
def saveFileDumper(fileEntry, file, _):
fileSize = fileEntry.size
def blockDumper(index):
nonlocal fileSize
if fileSize == 0:
print("Warning: excessive block")
return
tranSize = min(fileSize, fsHeader.blockSize)
pos = index * fsHeader.blockSize
if file is not None:
file.write(dataRegion[pos: pos + tranSize])
fileSize -= tranSize
if fileSize != 0:
fat.walk(fileEntry.blockIndex, blockDumper)
if fileSize != 0:
print("Warning: not enough block")
print("Walking through files and dumping")
savefilesystem.extractAll(dirList, fileList, outputPath, saveFileDumper)
fat.allVisited()
print("Finished!")
if __name__ == "__main__":
main()