Files
bananarchy/scripts/build-beatles-no-cover-dtb.py
T

116 lines
4.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Build a 2720 Flip DTB with the external panel disabled (hardware rollback).
The cover panel DT is shipped enabled, but gpio13 mux polarity / external
reset polarity still need a live sign-off. If the main panel or keypad
regresses, this script produces a DTB that keeps the verified internal panel
and disables only display@1, without touching the spi-mux or the DTS source.
./scripts/build-beatles-no-cover-dtb.py # writes images/...no-cover.dtb
./scripts/build-beatles-no-cover-dtb.py --check # validation only
./scripts/build-beatles-no-cover-dtb.py --install # also patches local images
--install builds a separate no-cover boot.img and replaces boot.img/DTB in
the local raw image's pmOS_boot; it never changes the tracked DTS/DTB. Run
check-rebuild.sh before rolling back: in the rollback state its pmOS_boot
check expects the shipping DTB and will report the deliberate mismatch.
"""
import argparse
import pathlib
import subprocess
import sys
import tempfile
BASE = pathlib.Path(__file__).resolve().parent.parent
DTS = BASE / "devices/nokia-beatles/qcom-msm8905-nokia-beatles.dts"
OUTPUT = BASE / "images/qcom-msm8905-nokia-beatles-no-cover.dtb"
OPENING = '\t\t\t\tdisplay@1 {\n'
MARKER = 'nokia,beatles-gc9305-ext-panel'
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--check", action="store_true",
help="validate the transformation without writing images/")
parser.add_argument("--install", action="store_true",
help="also build a no-cover boot.img and patch the local raw image")
args = parser.parse_args()
source = DTS.read_text()
start = source.find(OPENING)
if start < 0:
sys.stderr.write("FAIL: display@1 is missing from the shipping DTS\n")
return 1
end = source.find("\n\t\t\t\t};\n", start)
if end < 0:
sys.stderr.write("FAIL: display@1 node is not terminated\n")
return 1
block = source[start:end]
if MARKER not in block or 'status = "disabled";' in block:
sys.stderr.write("FAIL: unexpected display@1 node contents\n")
return 1
patched = (source[:start] + OPENING +
'\t\t\t\t\tstatus = "disabled";\n' +
source[start + len(OPENING):])
with tempfile.TemporaryDirectory(prefix="bananarchy-no-cover-") as tmp:
root = pathlib.Path(tmp)
dts = root / "no-cover.dts"
dtb = root / "no-cover.dtb"
dec = root / "no-cover-decomp.dts"
dts.write_text(patched)
for cmd in (["dtc", "-I", "dts", "-O", "dtb", "-o", str(dtb), str(dts)],
["dtc", "-I", "dtb", "-O", "dts", "-o", str(dec), str(dtb)]):
proc = subprocess.run(cmd, capture_output=True, text=True)
if proc.returncode:
sys.stderr.write("FAIL: %s\n%s" % (" ".join(cmd), proc.stderr))
return 1
text = dec.read_text(encoding="utf-8", errors="replace")
ext_start = text.find("display@1 {")
ext = text[ext_start:text.find("\n\t\t\t\t};", ext_start) + 10] \
if ext_start >= 0 else ""
if 'status = "disabled";' not in ext or MARKER not in ext:
sys.stderr.write("FAIL: rollback DTB did not disable the external panel\n")
return 1
for token in ('qcom,msm-id = <0x14b 0x00>;',
'qcom,board-id = <0x1000b 0x100>;',
'qcom,pmic-id = <0x1000d 0x00 0x00 0x00>;',
'reg = <0x80000000 0x20000000>;',
'dc-gpios = <0x27 0x6e 0x00>;',
'spi-max-frequency = <0x17d7840>;'):
if token not in text:
sys.stderr.write("FAIL: rollback DTB lost %s\n" % token)
return 1
if args.check:
print("[ok] no-cover DTB transformation validated (%d bytes)" %
dtb.stat().st_size)
return 0
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
OUTPUT.write_bytes(dtb.read_bytes())
print("wrote %s" % OUTPUT.relative_to(BASE))
no_cover_boot = BASE / "images/boot-bananarchy-nokia-beatles-no-cover.img"
shipping_boot = BASE / "images/boot-bananarchy-nokia-beatles.img"
raw = BASE / "images/bananarchy-nokia-beatles.img"
repack = ["python3", str(BASE / "scripts/repack-beatles-bootimg.py"),
"--boot-image", str(shipping_boot), "--dtb", str(OUTPUT),
"--output", str(no_cover_boot)]
sync = ["python3", str(BASE / "scripts/sync-beatles-bootfs.py"),
"--raw-image", str(raw), "--boot-image", str(no_cover_boot),
"--dtb", str(OUTPUT)]
if not args.install:
print("install with: %s" % " ".join(repack))
print("then: %s" % " ".join(sync))
return 0
for cmd in (repack, sync):
proc = subprocess.run(cmd, capture_output=True, text=True)
if proc.returncode:
sys.stderr.write("FAIL: %s\n%s%s" %
(" ".join(cmd), proc.stdout, proc.stderr))
return 1
print(proc.stdout.strip())
print("rollback installed into %s; restore the shipping DTB/boot.img with "
"sync-beatles-bootfs.py (no overrides) to undo." % raw.name)
return 0
if __name__ == "__main__":
sys.exit(main())