Skip to content

Instantly share code, notes, and snippets.

@tewilove
Created April 30, 2024 07:51
Show Gist options
  • Save tewilove/4f8a347a3f88fbaef5843fdfa5f78282 to your computer and use it in GitHub Desktop.
Save tewilove/4f8a347a3f88fbaef5843fdfa5f78282 to your computer and use it in GitHub Desktop.
MTK md1image unpacking.
#!/usr/bin/env python
import os
import struct
import sys
MD1IMG_MAGIC1 = 0x58881688
MD1IMG_MAGIC2 = 0x58891689
def main():
path = sys.argv[1]
stat = os.stat(path)
offset = 0
size = stat.st_size
with open(path, "rb") as fp:
while offset < size:
fp.seek(offset)
header = fp.read(80)
unpacked = struct.unpack('<II32sIIIIIIIIII', header)
magic1 = unpacked[0]
magic2 = unpacked[5]
if magic1 != MD1IMG_MAGIC1 or magic2 != MD1IMG_MAGIC2:
break
data_size = unpacked[1]
name = unpacked[2]
name = name[:name.find(b"\0")].decode()
base = unpacked[3]
mode = unpacked[4]
data_offset = unpacked[6]
print("Found {:s}@0x{:x},0x{:x}".format(name, base, data_size))
offset += data_offset
fp.seek(offset)
data = fp.read(data_size)
with open("{:s}.{:s}.bin".format(path, name), "wb") as out:
out.write(data)
offset += data_size
if offset % 16 != 0:
offset += 16 - (offset % 16)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment