-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallwinner_minfs.py
239 lines (196 loc) · 8.41 KB
/
allwinner_minfs.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
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
import argparse
import struct
# TBC: These are guesses
FLAG_DIR = 1
FLAG_COMPRESSION = 2
FLAG_COMPRESSION_TYPE_LZMA = 4
class MINFSHeader:
def __init__(self, bytes):
# Check magic string
if bytes[0:5] != b'MINFS':
raise RuntimeError("Bad header")
# Decode MinFS header
self.version = struct.unpack('<H', bytes[6:8])[0]
self.offset = struct.unpack('<L', bytes[8:12])[0]
self.root_size = struct.unpack('<L', bytes[12:16])[0]
self.file_count = struct.unpack('<L', bytes[16:20])[0]
self.tree_size = struct.unpack('<L', bytes[20:24])[0] # Size of directory block
self.fdata_length = struct.unpack('<L', bytes[24:28])[0] # Size of data block
self.image_size = struct.unpack('<L', bytes[28:32])[0]
def to_bytes(self):
header = b'MINFS\x00'
header += struct.pack('<H', self.version)
header += struct.pack('<L', self.offset)
header += struct.pack('<L', self.root_size)
header += struct.pack('<L', self.file_count)
header += struct.pack('<L', self.tree_size)
header += struct.pack('<L', self.fdata_length)
header += struct.pack('<L', self.image_size)
return header
def print_details(self):
print ("Version", self.version)
print ("Offset", self.offset)
print ("Root size", self.root_size)
print ("File count", self.file_count)
print ("Tree size", self.tree_size)
print ("FData length", self.fdata_length)
print ("Image size", self.image_size)
print ("")
class MINFSFile:
def __init__(self, bytes, offset):
self.binary_buffer = bytes
self.header_offset = offset
self.flash_offset = struct.unpack('<L', bytes[offset:offset+4])[0]
offset = offset + 4
self.raw_size = struct.unpack('<L', bytes[offset:offset+4])[0]
offset = offset + 4
self.uncompressed_size = struct.unpack('<L', bytes[offset:offset+4])[0]
offset = offset + 4
self.entry_length = struct.unpack('<H', bytes[offset:offset+2])[0]
offset = offset + 2
self.flags = struct.unpack('<H', bytes[offset:offset+2])[0]
offset = offset + 2
self.name_length = struct.unpack('<H', bytes[offset:offset+2])[0]
offset = offset + 2
self.extra_length = struct.unpack('<H', bytes[offset:offset+2])[0]
offset = offset + 2
self.name = bytes[offset:offset+self.name_length].decode("utf-8")
offset = offset + self.name_length
self.extra_data = bytes[offset:offset+self.extra_length]
offset = offset + self.extra_length
self.content = bytes[self.flash_offset : self.flash_offset + self.raw_size]
def header(self):
header = b''
header += struct.pack('<L', self.flash_offset)
header += struct.pack('<L', self.raw_size)
header += struct.pack('<L', self.uncompressed_size)
header += struct.pack('<H', self.entry_length)
header += struct.pack('<H', self.flags)
header += struct.pack('<H', self.name_length)
header += struct.pack('<H', self.extra_length)
header += self.name.encode('utf-8')
header += self.extra_data
header += b'\x00' * (self.entry_length - len(header))
return header
def print_details(self):
print ("Name", self.name)
print ("Flash offset", self.flash_offset)
print ("Raw size", self.raw_size)
print ("Uncompressed size", self.uncompressed_size)
print ("Entry length", self.entry_length)
print ("Flags", self.flags)
print ("Name length", self.name_length)
print ("Extra length", self.extra_length)
print ("")
def extract(self):
"""Extracts file to disk"""
file_object.print_details()
if self.flags & FLAG_COMPRESSION:
raise RuntimeError("Handling compressed files not yet implemented")
content = self.binary_buffer[self.flash_offset:self.flash_offset + self.raw_size]
with open(self.name, 'wb') as file:
file.write(content)
def replace_content(self, content):
if self.flags & FLAG_COMPRESSION:
raise RuntimeError("Handling compressed files not yet implemented")
self.raw_size = len(content)
self.uncompressed_size = len(content)
self.content = content
def decode(filename):
"""Decodes Allwinner Minfs FEX"""
file_objects = []
image_content = ""
with open(filename, 'rb') as file:
image_content = file.read()
# Decode MinFS header
header = MINFSHeader(image_content)
offset = header.offset
for file_index in range(header.file_count):
file_object = MINFSFile(image_content, offset)
file_objects.append(file_object)
offset += file_object.entry_length
offset = file_objects[0].flash_offset
return header, file_objects
def replace(header, file_objects, file_object_to_replace, new_file):
"""Replaces file with new contents"""
new_file_content = ""
with open(new_file, 'rb') as file:
new_file_content = file.read()
file_object_to_replace.replace_content(new_file_content)
# Recompute file offsets
fdata_start = header.offset + header.tree_size + 24
offset = fdata_start
for file_object in file_objects:
if file_object.flags & FLAG_DIR:
continue
file_object.flash_offset = offset
offset += file_object.raw_size + file_object.raw_size % 2
# Byte align offset
offset += offset % 4
header.fdata_length = offset - fdata_start
# Fail if offset exceeds image_size
if offset >= header.image_size:
raise RuntimeError("Data exceeds image capacity!")
def write(filename, header, file_objects):
offset = 0
with open(filename, 'wb') as file:
# Write MinFS header
header_bytes = header.to_bytes()
file.write(header_bytes)
file.write(b'\x00' * (header.offset - len(header_bytes)))
offset = header.offset
# Write directory listing
for file_object in file_objects:
file_header = file_object.header()
file.write(file_header)
offset += len(file_header)
# Padding after directory listing
file.write(b'\x00' * 24)
offset += 24
# Write each file content
for file_object in file_objects:
if file_object.flags & FLAG_DIR:
continue
if offset != file_object.flash_offset:
raise RuntimeError("Offset mismatch in " + file_object.name + " " + str(offset) + " != " + str(file_object.flash_offset))
content = file_object.content
file.write(content)
offset += file_object.raw_size
file_content_end_offset = offset
offset += file_object.raw_size % 2
offset += offset % 4
padding = offset - file_content_end_offset
file.write(b'\x00' * padding)
# Write remainder of block
file.write(b'\x00' * (header.image_size - offset))
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog='allwinner_minfs')
parser.add_argument('file')
parser.add_argument('--verbose', action='store_true', help='list')
parser.add_argument('--list', action='store_true', help='list')
parser.add_argument('--extract', help='file to extract')
parser.add_argument('--replace', nargs=2, help='replace contents of named file')
parser.add_argument('--output', help='output filename')
args = parser.parse_args()
header, file_objects = decode(args.file)
if args.verbose:
header.print_details()
if args.list:
for file_object in file_objects:
if args.verbose:
file_object.print_details()
else:
print(file_object.name)
if args.extract:
file_to_extract = args.extract
for file_object in file_objects:
if file_object.name == file_to_extract:
file_object.extract()
if args.replace:
file_to_extract, new_file = args.replace
for file_object in file_objects:
if file_object.name == file_to_extract:
replace(header, file_objects, file_object, new_file)
output_filename = args.file + ".modified" if args.output is None else args.output
write(output_filename, header, file_objects)
break