151 lines
5.3 KiB
Python
151 lines
5.3 KiB
Python
#!/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 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)
|
||
|
|
|
||
|
|
|
||
|
|
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 main():
|
||
|
|
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
boot = os.path.join(base, "images", "boot-bananarchy-nokia-beatles.img")
|
||
|
|
if not os.path.isfile(boot):
|
||
|
|
print("[skip] 2720 boot image not built yet")
|
||
|
|
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 != 0x80008000 or ramdisk_addr != 0x81000000 or tags_addr != 0x80000100:
|
||
|
|
problems.append("boot header load addresses do not match the stock 2720")
|
||
|
|
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 != "Nokia 2720 Flip":
|
||
|
|
problems.append("appended DTB model is %r" % model)
|
||
|
|
for prop, expected in (("qcom,msm-id", STOCK_MSM_ID),
|
||
|
|
("qcom,board-id", STOCK_BOARD_ID),
|
||
|
|
("qcom,pmic-id", STOCK_PMIC_ID)):
|
||
|
|
if root.get(prop) != expected:
|
||
|
|
problems.append("%s does not match the stock 2720 QRD SKUB value" % prop)
|
||
|
|
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] 2720 boot image: exact stock IDs, 512 MiB memory fallback, appended DTB pointer")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|