Refactor bootimg_analyze (MR 2359)

This commit is contained in:
Anri Dellal 2024-07-10 15:00:51 +03:00 committed by Oliver Smith
parent aeb65f2991
commit 30d8ea997f
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB

View file

@ -11,7 +11,7 @@ import pmb.chroot.apk
from pmb.core import Chroot from pmb.core import Chroot
def is_dtb(path): def is_dtb(path) -> bool:
if not os.path.isfile(path): if not os.path.isfile(path):
return False return False
with open(path, "rb") as f: with open(path, "rb") as f:
@ -19,7 +19,7 @@ def is_dtb(path):
return f.read(4) == b"\xd0\x0d\xfe\xed" return f.read(4) == b"\xd0\x0d\xfe\xed"
def get_mtk_label(path): def get_mtk_label(path) -> str | None:
"""Read the label from the MediaTek header of the kernel or ramdisk inside """Read the label from the MediaTek header of the kernel or ramdisk inside
an extracted boot.img. an extracted boot.img.
:param path: to either the kernel or ramdisk extracted from boot.img :param path: to either the kernel or ramdisk extracted from boot.img
@ -51,7 +51,7 @@ def get_mtk_label(path):
return label return label
def get_qcdt_type(path): def get_qcdt_type(path) -> str | None:
"""Get the dt.img type by reading the first four bytes of the file. """Get the dt.img type by reading the first four bytes of the file.
:param path: to the qcdt image extracted from boot.img :param path: to the qcdt image extracted from boot.img
:returns: * None: dt.img is of unknown type :returns: * None: dt.img is of unknown type
@ -73,7 +73,7 @@ def get_qcdt_type(path):
return None return None
def bootimg(path: Path): def bootimg(path: Path) -> dict[str, str]:
if not path.exists(): if not path.exists():
raise RuntimeError(f"Could not find file '{path}'") raise RuntimeError(f"Could not find file '{path}'")
@ -136,43 +136,48 @@ def bootimg(path: Path):
if header_version >= 3: if header_version >= 3:
output["pagesize"] = "4096" output["pagesize"] = "4096"
else: else:
with open(f"{bootimg_path}-base") as f: addresses = {
output["base"] = "0x{:08x}".format(int(f.read().replace("\n", ""), 16)) "base": f"{bootimg_path}-base",
with open(f"{bootimg_path}-kernel_offset") as f: "kernel_offset": f"{bootimg_path}-kernel_offset",
output["kernel_offset"] = "0x{:08x}".format(int(f.read().replace("\n", ""), 16)) "ramdisk_offset": f"{bootimg_path}-ramdisk_offset",
with open(f"{bootimg_path}-ramdisk_offset") as f: "second_offset": f"{bootimg_path}-second_offset",
output["ramdisk_offset"] = "0x{:08x}".format(int(f.read().replace("\n", ""), 16)) "tags_offset": f"{bootimg_path}-tags_offset",
with open(f"{bootimg_path}-second_offset") as f: }
output["second_offset"] = "0x{:08x}".format(int(f.read().replace("\n", ""), 16))
with open(f"{bootimg_path}-tags_offset") as f:
output["tags_offset"] = "0x{:08x}".format(int(f.read().replace("\n", ""), 16))
with open(f"{bootimg_path}-pagesize") as f:
output["pagesize"] = f.read().replace("\n", "")
if header_version == 2: if header_version == 2:
with open(f"{bootimg_path}-dtb_offset") as f: addresses["dtb_offset"] = f"{bootimg_path}-dtb_offset"
output["dtb_offset"] = "0x{:08x}".format(int(f.read().replace("\n", ""), 16)) for key, file in addresses.items():
with open(file) as f:
output[key] = f"0x{int(trim_input(f), 16):08x}"
if get_mtk_label(f"{bootimg_path}-kernel") is not None: with open(f"{bootimg_path}-pagesize") as f:
output["mtk_label_kernel"] = get_mtk_label(f"{bootimg_path}-kernel") output["pagesize"] = trim_input(f)
if get_mtk_label(f"{bootimg_path}-ramdisk") is not None:
output["mtk_label_ramdisk"] = get_mtk_label(f"{bootimg_path}-ramdisk")
output["qcdt"] = ( output["qcdt"] = (
"true" "true"
if os.path.isfile(f"{bootimg_path}-dt") and os.path.getsize(f"{bootimg_path}-dt") > 0 if os.path.isfile(f"{bootimg_path}-dt") and os.path.getsize(f"{bootimg_path}-dt") > 0
else "false" else "false"
) )
output.update(
if get_qcdt_type(f"{bootimg_path}-dt") is not None: {
output["qcdt_type"] = get_qcdt_type(f"{bootimg_path}-dt") key: value
for key, value in {
"mtk_label_kernel": get_mtk_label(f"{bootimg_path}-kernel"),
"mtk_label_ramdisk": get_mtk_label(f"{bootimg_path}-ramdisk"),
"qcdt_type": get_qcdt_type(f"{bootimg_path}-dt"),
}.items()
if value is not None
}
)
output["dtb_second"] = "true" if is_dtb(f"{bootimg_path}-second") else "false" output["dtb_second"] = "true" if is_dtb(f"{bootimg_path}-second") else "false"
with open(f"{bootimg_path}-cmdline") as f: with open(f"{bootimg_path}-cmdline") as f:
output["cmdline"] = f.read().replace("\n", "") output["cmdline"] = trim_input(f)
# Cleanup # Cleanup
pmb.chroot.user(["rm", "-r", temp_path]) pmb.chroot.user(["rm", "-r", temp_path])
return output return output
def trim_input(f) -> str:
return f.read().replace("\n", "")