Skip to content

Instantly share code, notes, and snippets.

@adithya2306
Last active November 1, 2023 01:13
Show Gist options
  • Select an option

  • Save adithya2306/88e8fc468695bcdaa8b880ba4bd01420 to your computer and use it in GitHub Desktop.

Select an option

Save adithya2306/88e8fc468695bcdaa8b880ba4bd01420 to your computer and use it in GitHub Desktop.

Revisions

  1. adithya2306 revised this gist May 20, 2023. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions unpack-moto-img.py
    Original file line number Diff line number Diff line change
    @@ -7,29 +7,24 @@
    import struct
    import sys


    def abort(msg):
    sys.stderr.write(msg + "\n")
    sys.exit()


    def get_string(f, n):
    dat = f.read(n)
    return dat.replace(b"\x00", b"")


    def get_long(f):
    long_bytes = f.read(8)
    long_value = struct.unpack("Q", long_bytes)[0]
    return long_value


    def extract(name, offset, length):
    with open(name, "wb") as of:
    f.seek(offset)
    of.write(f.read(length))


    if len(sys.argv) < 2:
    abort("Usage: unpack.py radio.img")

    @@ -46,10 +41,10 @@ def extract(name, offset, length):
    size = get_long(f)
    if name == b"LONELY_N_SINGLE":
    break # no more files
    print("Name: {}, Offset: {}, Size: {}".format(name.decode(), f.tell(), size))

    extract(name.decode(), f.tell(), size)
    pad = 0
    if size % 4096 != 0:
    pad = 4096 - (size % 4096)
    f.read(pad)
    print(", Padding: {}".format(pad))
    print("Name: {}, Offset: {}, Size: {}, Padding: {}".format(name.decode(), f.tell(), size, pad))
  2. adithya2306 created this gist May 20, 2023.
    55 changes: 55 additions & 0 deletions unpack-moto-img.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #!/usr/bin/env python
    #
    # based on https://pastebin.com/raw/9w5TBnSv
    # credits to original author HemanthJabalpuri @ XDA
    # converted to python using chatgpt

    import struct
    import sys


    def abort(msg):
    sys.stderr.write(msg + "\n")
    sys.exit()


    def get_string(f, n):
    dat = f.read(n)
    return dat.replace(b"\x00", b"")


    def get_long(f):
    long_bytes = f.read(8)
    long_value = struct.unpack("Q", long_bytes)[0]
    return long_value


    def extract(name, offset, length):
    with open(name, "wb") as of:
    f.seek(offset)
    of.write(f.read(length))


    if len(sys.argv) < 2:
    abort("Usage: unpack.py radio.img")

    with open(sys.argv[1], "rb") as f:
    magic = get_string(f, 256)
    if magic != b"SINGLE_N_LONELY":
    abort("Unsupported")

    fsize = f.seek(0, 2)
    f.seek(256)

    for i in range(1, 65):
    name = get_string(f, 248)
    size = get_long(f)
    if name == b"LONELY_N_SINGLE":
    break # no more files
    print("Name: {}, Offset: {}, Size: {}".format(name.decode(), f.tell(), size))
    extract(name.decode(), f.tell(), size)
    pad = 0
    if size % 4096 != 0:
    pad = 4096 - (size % 4096)
    f.read(pad)
    print(", Padding: {}".format(pad))