#!/usr/bin/env python3 """Host tests for the source-built nokia-coconut lk2nd port. Runs the image validator, compiles the shared C panel-init walker against the byte stream extracted from the built QCDT DTB, and verifies repository wiring. """ import os import shutil import subprocess import sys import tempfile BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PINNED = "cf45aefd22d744b2bf9b39455afae697ce430063" def run(cmd, **kwargs): return subprocess.run(cmd, cwd=BASE, capture_output=True, text=True, **kwargs) def check(condition, message): if not condition: print("FAIL: %s" % message) return False print("[ok] %s" % message) return True def main(): ok = True validator = os.path.join(BASE, "scripts", "check-lk2nd-coconut.py") build = os.path.join(BASE, "scripts", "build-lk2nd-coconut.sh") base_patch = os.path.join(BASE, "bootloader", "lk2nd", "nokia-beatles.patch") patch = os.path.join(BASE, "bootloader", "lk2nd", "nokia-coconut.patch") header = os.path.join(BASE, "bootloader", "lk2nd", "spi-panel-init.h") test_c = os.path.join(BASE, "bootloader", "lk2nd", "test_spi_panel_init.c") variants = ( ("images/boot-lk2nd-nokia-coconut.img", 0, 0), ("images/boot-lk2nd-nokia-coconut-diagnostic.img", 1, 0), ("images/boot-lk2nd-nokia-coconut-display.img", 0, 1), ) for image, fastboot, display in variants: ok &= check(os.path.isfile(os.path.join(BASE, image)), "%s exists" % image) if not os.path.isfile(os.path.join(BASE, image)): continue cmd = [sys.executable, validator, "--image", image, "--expect-fastboot", str(fastboot), "--expect-display", str(display)] r = subprocess.run(cmd, cwd=BASE, capture_output=True, text=True) for line in r.stdout.strip().split("\n"): if line: print(line) ok &= check(r.returncode == 0, "%s passes structural validation" % os.path.basename(image)) if r.returncode != 0: print(r.stderr.strip()) for path, label in ((build, "build-lk2nd-coconut.sh"), (base_patch, "nokia-beatles.patch"), (patch, "nokia-coconut.patch"), (header, "spi-panel-init.h"), (test_c, "test_spi_panel_init.c"), (os.path.join(BASE, "bootloader", "lk2nd", "Dockerfile"), "toolchain Dockerfile")): ok &= check(os.path.isfile(path), "%s exists" % label) script = open(build).read() ok &= check(PINNED in script, "build script pins upstream commit %s" % PINNED) ok &= check("LK2ND_FORCE_FASTBOOT=1" in script and "lk2nd.no-display" in script, "build script can produce the forced-fastboot no-display diagnostic image") ok &= check("lk2nd.display=on" in script and "--expect-display" in script, "build script can produce and validate the SPI display image") ok &= check("nokia-beatles.patch" in script and "nokia-coconut.patch" in script, "build script layers nokia-coconut.patch on top of nokia-beatles.patch") ok &= check("lk2nd.compatible=nokia,coconut" in script, "build script sets coconut compatible cmdline") patch_text = open(patch).read() ok &= check("nokia-coconut.dtsi" in patch_text and "msm8905-qrd-skub.dts" in patch_text and "lk2nd,panel-init" in patch_text and "nokia,coconut" in patch_text, "coconut patch adds device node, panel streams and SPI matching") ok &= check("find_panel_init_in_dev" in patch_text, "coconut patch prefers active compatible panel stream first") flasher = open(os.path.join(BASE, "flash-edl.py")).read() ok &= check("images/boot-lk2nd-nokia-coconut-display.img" in flasher, "flasher references the display-enabled coconut lk2nd image") ok &= check('"nokia-coconut": COCONUT_LK2ND_IMAGE' in flasher, "flasher maps nokia-coconut to its source-built lk2nd image") preflight = open(os.path.join(BASE, "scripts", "check-rebuild.sh")).read() ok &= check("check-lk2nd-coconut.py" in preflight, "check-rebuild.sh validates the coconut lk2nd images") coconut_dts = open(os.path.join( BASE, "devices", "nokia-coconut", "qcom-msm8905-nokia-coconut.dts" )).read() ok &= check( '"gpio0", "gpio3", "gpio7", "gpio99", "gpio6", "gpio1"' in coconut_dts and '"gpio52", "gpio56"' not in coconut_dts, "coconut DTS wires stock 6-column keypad GPIOs (gpio0, gpio3, gpio7, gpio99, gpio6, gpio1)" ) ok &= check( "0x3030067" in coconut_dts and "0x304006c" in coconut_dts and "0x105000e" in coconut_dts and "0x500d4" in coconut_dts and "linux,code = <0x74>;" in coconut_dts, "coconut DTS includes 22-key 4x6 keymap and end/power key" ) resin_block = coconut_dts.split("resin {", 1)[1].split("};", 1)[0] ok &= check( "linux,code = <0x6c>;" in resin_block and "linux,code = <0x72>;" not in resin_block, "coconut DTS maps pm8941_resin to KEY_DOWN (0x6c), not KEY_VOLUMEDOWN" ) gcc = shutil.which("gcc") or shutil.which("cc") norm_img = os.path.join(BASE, "images", "boot-lk2nd-nokia-coconut.img") if gcc and os.path.isfile(norm_img): with tempfile.TemporaryDirectory() as tmp: streams = os.path.join(tmp, "panel_streams.h") r = subprocess.run([sys.executable, validator, "--image", "images/boot-lk2nd-nokia-coconut.img", "--emit-streams-header", streams], cwd=BASE, capture_output=True, text=True) ok &= check(r.returncode == 0, "panel init stream extracted from coconut QCDT DTB") if r.returncode == 0: binary = os.path.join(tmp, "test_spi_panel_init") r = run([gcc, "-std=c11", "-Wall", "-Wextra", "-Werror", "-I", tmp, "-I", os.path.join(BASE, "bootloader", "lk2nd"), "-o", binary, test_c]) ok &= check(r.returncode == 0, "host C unit test compiles against the shared parser header") if r.returncode == 0: r = run([binary]) ok &= check(r.returncode == 0, "host C unit test accepts the coconut GC9305 v2 init stream") elif not gcc: print("[skip] no host C compiler; C unit test skipped") print("[%s] nokia-coconut lk2nd port tests" % ("ok" if ok else "FAIL")) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())