47 lines
1.4 KiB
Python
Executable File
47 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Rewrite the cmdline field of an Android v0 boot.img header.
|
|
|
|
Only the 512-byte cmdline field changes; kernel, ramdisk, DTB and load
|
|
addresses stay byte-identical, so the image still fits its partition.
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import struct
|
|
import sys
|
|
|
|
BOOT_MAGIC = b"ANDROID!"
|
|
CMDLINE_OFFSET = 64
|
|
CMDLINE_SIZE = 512
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("src")
|
|
parser.add_argument("dst")
|
|
parser.add_argument("--cmdline", required=True)
|
|
args = parser.parse_args()
|
|
data = bytearray(open(args.src, "rb").read())
|
|
if len(data) < CMDLINE_OFFSET + CMDLINE_SIZE or data[:8] != BOOT_MAGIC:
|
|
print("FAIL: not an Android boot image")
|
|
return 1
|
|
raw = args.cmdline.encode("utf-8")
|
|
if len(raw) >= CMDLINE_SIZE:
|
|
print("FAIL: cmdline is too long (%d >= %d)" % (len(raw), CMDLINE_SIZE))
|
|
return 1
|
|
old = bytes(data[CMDLINE_OFFSET:CMDLINE_OFFSET + CMDLINE_SIZE]).rstrip(b"\0")
|
|
data[CMDLINE_OFFSET:CMDLINE_OFFSET + CMDLINE_SIZE] = (
|
|
raw + b"\0" * (CMDLINE_SIZE - len(raw)))
|
|
with open(args.dst, "wb") as handle:
|
|
handle.write(data)
|
|
fields = struct.unpack_from("<8s8I", data)
|
|
print("old cmdline: %r" % old.decode("utf-8", "replace"))
|
|
print("new cmdline: %r" % args.cmdline)
|
|
print("kernel=%d ramdisk=%d page=%d -> %s (%d bytes)" %
|
|
(fields[1], fields[3], fields[8], args.dst, len(data)))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|