Files
bananarchy/scripts/check-beatles-bootimg.py
T
Luxferre d7992904bf Bring up nokia-sparkler SMP/cpufreq/lk2nd/cam/haptics, fix timer and EDL USB timeout
- Add 0013 early-SCM 4-core SMP + cpufreq kernel patch and clock-frequency DT nodes
- Complete nokia-sparkler lk2nd port, boot/X11 session startup, 180-deg cached camera, and 157 Hz haptic audio
- Replace root-only timer backend with permissionless sxmo_timer.sh and keypad/hook cancellation
- Fix EDL Firehose rawmode ACK, USB active-config preservation, *+# hint, and 30 s default USB timeout
- Sync a1120 (cocojamboo) prebuilts, docs, tests, and verified checksums in check-rebuild.sh
2026-11-16 11:45:38 +02:00

211 lines
7.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Validate the 2720 Flip boot image before it is flashed over EDL.
Checks the Android header, the kernel+0x2c appended-DTB pointer, the exact
stock Qualcomm match IDs and the 512 MiB memory fallback inside the DTB. Skips
cleanly when the image has not been built yet, so check-rebuild.sh stays useful
in a fresh clone.
"""
import argparse
import os
import struct
import sys
BOOT_MAGIC = b"ANDROID!"
FDT_MAGIC = 0xd00dfeed
DTB_OFFSET_FIELD = 0x2c
BOOT_PARTITION_BYTES = 26165248
FDT_BEGIN_NODE = 1
FDT_END_NODE = 2
FDT_PROP = 3
FDT_NOP = 4
FDT_END = 9
STOCK_MSM_ID = struct.pack(">2I", 0x14b, 0)
STOCK_BOARD_ID = struct.pack(">2I", 0x1000b, 0x100)
STOCK_PMIC_ID = struct.pack(">4I", 0x1000d, 0, 0, 0)
MEMORY_512M = struct.pack(">2I", 0x80000000, 0x20000000)
DEVICE_SPECS = {
"nokia-beatles": {
"model": "Nokia 2720 Flip",
"short": "2720",
"compatible": b"nokia,beatles",
"msm_id": STOCK_MSM_ID,
"board_id": STOCK_BOARD_ID,
"pmic_id": STOCK_PMIC_ID,
"addrs": (0x80008000, 0x81000000, 0x80000100),
},
"nokia-coconut": {
"model": "Nokia 800 Tough",
"short": "800 Tough",
"compatible": b"nokia,coconut",
"msm_id": STOCK_MSM_ID,
"board_id": STOCK_BOARD_ID,
"pmic_id": STOCK_PMIC_ID,
"addrs": (0x80008000, 0x81000000, 0x80000100),
},
"nokia-sparkler": {
"model": "Nokia 8000 4G",
"short": "8000 4G",
"compatible": b"nokia,sparkler",
"msm_id": struct.pack(">2I", 0xf5, 0),
"board_id": struct.pack(">2I", 0x8, 0x100),
"pmic_id": STOCK_PMIC_ID,
"addrs": (0x80008000, 0x82700000, 0x82500000),
},
}
DEVICE_MODELS = {dev: spec["model"] for dev, spec in DEVICE_SPECS.items()}
DEVICE_SHORT = {dev: spec["short"] for dev, spec in DEVICE_SPECS.items()}
def align(value, page):
return (value + page - 1) // page * page
def parse_fdt(data):
vals = struct.unpack_from(">10I", data, 0)
magic, totalsize, off_struct, off_strings, off_rsv, version = vals[:6]
size_strings, size_struct = vals[8], vals[9]
if magic != FDT_MAGIC:
raise ValueError("bad FDT magic")
if version not in (16, 17):
raise ValueError("unsupported FDT version %d" % version)
strings = data[off_strings:off_strings + size_strings]
nodes = {}
stack = []
pos = off_struct
while True:
token = struct.unpack_from(">I", data, pos)[0]
pos += 4
if token == FDT_BEGIN_NODE:
end = data.index(b"\0", pos)
name = data[pos:end].decode("utf-8", "replace")
pos = (end + 4) & ~3
if not stack:
path = "/"
elif stack[-1] == "/":
path = "/" + name
else:
path = stack[-1] + "/" + name
stack.append(path)
nodes[stack[-1]] = {}
elif token == FDT_END_NODE:
stack.pop()
if not stack:
break
elif token == FDT_PROP:
length, nameoff = struct.unpack_from(">II", data, pos)
pos += 8
value = data[pos:pos + length]
pos = (pos + length + 3) & ~3
end = strings.index(b"\0", nameoff)
prop = strings[nameoff:end].decode("utf-8", "replace")
nodes[stack[-1]][prop] = value
elif token == FDT_NOP:
continue
elif token == FDT_END:
break
else:
raise ValueError("bad FDT token 0x%x" % token)
return nodes, totalsize
def parse_args():
parser = argparse.ArgumentParser(
description="Validate MSM8905 lk2nd-chained mainline boot.img"
)
parser.add_argument(
"--device",
choices=tuple(DEVICE_MODELS.keys()),
default="nokia-beatles",
help="Target device to validate (default: nokia-beatles)",
)
parser.add_argument(
"--image",
default="",
help="Explicit path to boot-bananarchy-<device>.img",
)
return parser.parse_args()
def main():
args = parse_args()
device = args.device
spec = DEVICE_SPECS[device]
short_name = spec["short"]
expected_model = spec["model"]
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
boot = args.image or os.path.join(base, "images", "boot-bananarchy-%s.img" % device)
if not os.path.isfile(boot):
print("[skip] %s boot image not built yet" % short_name)
return 0
data = open(boot, "rb").read()
problems = []
if data[:8] != BOOT_MAGIC:
problems.append("not an Android boot image")
fields = struct.unpack_from("<8s8I", data, 0)
kernel_size, kernel_addr, ramdisk_size, ramdisk_addr = fields[1:5]
second_size, second_addr, tags_addr, page = fields[5:9]
if page != 2048:
problems.append("page size is %d, expected 2048" % page)
if len(data) > BOOT_PARTITION_BYTES:
problems.append("boot image is larger than the 25 MiB boot partition")
cmdline = data[64:576].split(b"\0")[0].decode("utf-8", "replace")
cmdline_parts = cmdline.split()
if "earlycon" in cmdline_parts:
problems.append("cmdline still contains earlycon")
if "quiet" not in cmdline_parts or "splash" not in cmdline_parts:
problems.append("cmdline is missing 'quiet splash' (Plymouth never starts)")
for flag in ("loglevel=8", "ignore_loglevel", "initcall_debug"):
if flag in cmdline_parts:
problems.append("cmdline still has kernel debug flag %s" % flag)
if (kernel_addr, ramdisk_addr, tags_addr) != spec["addrs"]:
problems.append("boot header load addresses do not match the stock %s" % short_name)
kernel_off = page
dtb_rel = struct.unpack_from("<I", data, kernel_off + DTB_OFFSET_FIELD)[0]
dtb_abs = kernel_off + dtb_rel
if not 0 < dtb_rel < kernel_size:
problems.append("kernel+0x2c points outside the kernel payload")
elif data[dtb_abs:dtb_abs + 4] != b"\xd0\x0d\xfe\xed":
problems.append("kernel+0x2c does not point at an FDT header")
else:
dtb = data[dtb_abs:kernel_off + kernel_size]
nodes, totalsize = parse_fdt(dtb)
if totalsize > len(dtb):
problems.append("appended DTB is truncated")
root = nodes.get("/", {})
model = root.get("model", b"").rstrip(b"\0").decode("utf-8", "replace")
if model != expected_model:
problems.append("appended DTB model is %r (expected %r)" % (model, expected_model))
compat_tokens = [c for c in root.get("compatible", b"").split(b"\0") if c]
if spec["compatible"] not in compat_tokens:
problems.append("appended DTB compatible %r lacks %r" % (compat_tokens, spec["compatible"]))
for prop, expected in (("qcom,msm-id", spec["msm_id"]),
("qcom,board-id", spec["board_id"]),
("qcom,pmic-id", spec["pmic_id"])):
if root.get(prop) != expected:
problems.append("%s does not match the expected %s value" % (prop, short_name))
mem = None
for path, props in nodes.items():
if props.get("device_type") == b"memory\0":
mem = props
break
if mem is None:
problems.append("appended DTB has no /memory node")
elif mem.get("reg") != MEMORY_512M:
problems.append("appended DTB /memory:reg is not the 512 MiB fallback")
if ramdisk_size <= 0 or second_size != 0:
problems.append("unexpected ramdisk/second sizes")
if problems:
for problem in problems:
print("FAIL: %s" % problem)
return 1
print("[ok] %s boot image: exact stock IDs, 512 MiB memory fallback, appended DTB pointer" % short_name)
return 0
if __name__ == "__main__":
sys.exit(main())