Files
bananarchy/scripts/test-lk2nd-sparkler-port.py
T

233 lines
8.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Host tests for the source-built nokia-sparkler lk2nd port.
Runs the image validator across all three build variants, compiles a host C
panel-init walker test against the GC9307 HLT and ST7789VX2 CTC byte streams
extracted from the QCDT DTB, checks repository wiring, and exercises negative
validation checks on tampered binaries.
"""
import os
import shutil
import subprocess
import sys
import tempfile
BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PINNED = "cf45aefd22d744b2bf9b39455afae697ce430063"
HOST_TEST_C = r"""
#include <stdio.h>
#include <string.h>
#include "spi-panel-init.h"
#include "panel_streams_sparkler.h"
static unsigned char cmds[64];
static unsigned char data[64][16];
static unsigned int data_len[64];
static unsigned int cmd_count, delay_total, delay_count;
static int rec_cmd(const unsigned char *cmd)
{
if(cmd_count < 64)
cmds[cmd_count] = cmd[0];
cmd_count++;
return 0;
}
static int rec_data(const unsigned char *buf, unsigned int len)
{
if(cmd_count > 0 && cmd_count <= 64 && len <= 16) {
memcpy(data[cmd_count - 1], buf, len);
data_len[cmd_count - 1] = len;
}
return 0;
}
static void rec_delay(unsigned int ms)
{
delay_total += ms;
delay_count++;
}
static const struct lk2nd_spi_panel_init_ops ops = {
.write_cmd = rec_cmd,
.write_data = rec_data,
.delay = rec_delay,
};
static void reset(void)
{
cmd_count = delay_total = delay_count = 0;
memset(cmds, 0, sizeof(cmds));
memset(data_len, 0, sizeof(data_len));
}
#define CHECK(cond, msg) do { \
if(!(cond)) { \
printf("[fail] %s (line %d)\n", (msg), __LINE__); \
return 1; \
} \
} while(0)
static int has_cmd(const unsigned char *cmd, unsigned int len)
{
unsigned int i;
for(i = 0; i < cmd_count; i++) {
if(cmds[i] != cmd[0])
continue;
if(len == 1)
return 1;
if(data_len[i] == len - 1 && memcmp(data[i], cmd + 1, len - 1) == 0)
return 1;
}
return 0;
}
static int verify_stream(const char *label, const unsigned char *buf,
unsigned int len, unsigned int expected_cmds)
{
static const unsigned char rgb565[] = {0x3a, 0x05};
static const unsigned char window_x[] = {0x2a, 0x00, 0x00, 0x00, 0xef};
static const unsigned char window_y[] = {0x2b, 0x00, 0x00, 0x01, 0x3f};
static const unsigned char sleep_out[] = {0x11};
static const unsigned char disp_on[] = {0x29};
reset();
CHECK(lk2nd_spi_panel_init_run(buf, len, &ops) == 0, label);
CHECK(cmd_count == expected_cmds, "unexpected command count");
CHECK(delay_total == 120 && delay_count == 1, "expected 120 ms delay once");
CHECK(has_cmd(rgb565, sizeof(rgb565)), "missing RGB565 0x3a 0x05");
CHECK(has_cmd(window_x, sizeof(window_x)), "missing column window");
CHECK(has_cmd(window_y, sizeof(window_y)), "missing row window");
CHECK(has_cmd(sleep_out, sizeof(sleep_out)), "missing sleep-out 0x11");
CHECK(has_cmd(disp_on, sizeof(disp_on)), "missing display-on 0x29");
CHECK(cmds[cmd_count - 1] == 0x2c, "stream must end with RAMWR 0x2c");
return 0;
}
int main(void)
{
if(verify_stream("gc9307", stream_gc9307, sizeof(stream_gc9307), 15))
return 1;
if(verify_stream("st7789vx2", stream_st7789vx2, sizeof(stream_st7789vx2), 11))
return 1;
printf("[ok] verified sparkler GC9307 (15 cmds) and ST7789VX2 (11 cmds)\n");
return 0;
}
"""
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-sparkler.py")
build = os.path.join(BASE, "scripts", "build-lk2nd-sparkler.sh")
beatles_patch = os.path.join(BASE, "bootloader", "lk2nd", "nokia-beatles.patch")
coconut_patch = os.path.join(BASE, "bootloader", "lk2nd", "nokia-coconut.patch")
sparkler_patch = os.path.join(BASE, "bootloader", "lk2nd", "nokia-sparkler.patch")
header = os.path.join(BASE, "bootloader", "lk2nd", "spi-panel-init.h")
variants = (
("images/boot-lk2nd-nokia-sparkler.img", 0, 0),
("images/boot-lk2nd-nokia-sparkler-diagnostic.img", 1, 0),
("images/boot-lk2nd-nokia-sparkler-display.img", 0, 1),
)
for image, fastboot, display in variants:
full = os.path.join(BASE, image)
ok &= check(os.path.isfile(full), "%s exists" % image)
if not os.path.isfile(full):
continue
r = run([sys.executable, validator, "--image", image,
"--expect-fastboot", str(fastboot),
"--expect-display", str(display)])
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-sparkler.sh"),
(beatles_patch, "nokia-beatles.patch"),
(coconut_patch, "nokia-coconut.patch"),
(sparkler_patch, "nokia-sparkler.patch"),
(header, "spi-panel-init.h")):
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 supports forced-fastboot no-display diagnostic image")
ok &= check("lk2nd.compatible=nokia,sparkler" in script and
"lk2nd.display=on" in script,
"build script uses nokia,sparkler cmdline and display mode")
patch_text = open(sparkler_patch).read()
for needed in ("lk2nd/device/2nd/spi-display.c",
"lk2nd/device/dts/msm8909/msm8909-mtp.dts",
"lk2nd/device/dts/msm8909/nokia-sparkler.dtsi",
'"nokia,sparkler"',
"qcom,mdss_spi_gc9307_hlt_28_qvga_cmd",
"qcom,mdss_spi_st7789vx2_ctc_28_qvga_cmd",
"KEY_VOLUMEUP"):
ok &= check(needed in patch_text, "sparkler patch contains %s" % needed)
gcc = shutil.which("gcc") or shutil.which("cc")
img_path = os.path.join(BASE, "images", "boot-lk2nd-nokia-sparkler-display.img")
if gcc and os.path.isfile(img_path):
with tempfile.TemporaryDirectory() as tmp:
streams = os.path.join(tmp, "panel_streams_sparkler.h")
test_src = os.path.join(tmp, "test_sparkler_streams.c")
r = run([sys.executable, validator,
"--image", img_path,
"--expect-display", "1",
"--emit-streams-header", streams])
ok &= check(r.returncode == 0, "panel init streams extracted from QCDT DTB")
if r.returncode == 0:
with open(test_src, "w") as fh:
fh.write(HOST_TEST_C)
binary = os.path.join(tmp, "test_sparkler_streams")
r = run([gcc, "-std=c11", "-Wall", "-Wextra", "-Werror",
"-I", tmp, "-I", os.path.join(BASE, "bootloader", "lk2nd"),
"-o", binary, test_src])
ok &= check(r.returncode == 0,
"host C unit test compiles against spi-panel-init.h")
if r.returncode == 0:
r = run([binary])
if r.stdout.strip():
print(r.stdout.strip())
ok &= check(r.returncode == 0,
"host C unit test accepts GC9307 and ST7789VX2 streams")
tampered = os.path.join(tmp, "tampered.img")
raw = bytearray(open(img_path, "rb").read())
raw[-16:] = b"X" * 16
with open(tampered, "wb") as fh:
fh.write(raw)
r = run([sys.executable, validator, "--image", tampered,
"--expect-display", "1"])
ok &= check(r.returncode != 0 and "SEANDROIDENFORCE" in r.stdout,
"validator rejects tampered SEANDROIDENFORCE trailer")
elif not gcc:
print("[skip] no host C compiler; C unit test skipped")
print("[%s] nokia-sparkler lk2nd port tests" % ("ok" if ok else "FAIL"))
return 0 if ok else 1
if __name__ == "__main__":
sys.exit(main())