diff options
| author | dangofactory <root@dangofactory.net> | 2026-11-22 04:09:02 +0000 |
|---|---|---|
| committer | dangofactory <root@dangofactory.net> | 2026-11-22 04:09:02 +0000 |
| commit | 1f61487e5c4931e00f5702b6016222ffaa4276d4 (patch) | |
| tree | acf451f7b47758e8d4c722b655d1772113fc0dcc /tools/mkboard.py | |
| download | ckb01-master.tar.gz ckb01-master.zip | |
Diffstat (limited to 'tools/mkboard.py')
| -rw-r--r-- | tools/mkboard.py | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/tools/mkboard.py b/tools/mkboard.py new file mode 100644 index 0000000..ef45220 --- /dev/null +++ b/tools/mkboard.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python3 +"""Build ath11k artefacts for CKB01 from the vendor material. + +Inputs (read-only): + /ROM/ckb01/ckb01-main/GD25LQ32C(1.8v)air5ok.BIN raw SPI flash (+0x600 trailing FF) + ~/ckb01/firmware/wifi-ckb01-ipq8074/files/bdwlan.* vendor per-board BDFs + +Outputs (~/ckb01/wifi/): + caldata.bin ART+0x1000, 128 KiB (this device's own calibration) + board-oppo_ckb01.ipq8074 board-2.bin container (all vendor BDFs + bus=ahb fallback) + +board-2.bin format (drivers/net/wireless/ath/ath11k/{core.c,hw.h}, linux master): + "QCA-ATH11K-BOARD\0" padded to 4 bytes (20 bytes total) + repeat: + le32 id = 0 (ATH11K_BD_IE_BOARD) + le32 len = inner length (name IE + data IE, padded) + inner: + le32 id = 0 (BOARD_NAME), le32 len, name bytes, pad to 4 + le32 id = 1 (BOARD_DATA), le32 len, bdf bytes, pad to 4 +""" +import glob +import os +import struct +import sys + +SPI = "/ROM/Projects/ckb01/sources/ckb01-main/GD25LQ32C(1.8v)air5ok.BIN" +BDFDIR = "/ROM/Projects/ckb01/firmware/wifi-ckb01-ipq8074/files" +OUT = "/ROM/Projects/ckb01/wifi" + +ART_OFF = 0x2A0000 +CAL_OFF = ART_OFF + 0x1000 +CAL_LEN = 0x20000 + +MAGIC = b"QCA-ATH11K-BOARD\0" +IE_BOARD = 0 +IE_BOARD_NAME = 0 +IE_BOARD_DATA = 1 + + +def pad4(b): + return b + b"\x00" * ((4 - len(b) % 4) % 4) + + +def ie(ie_id, payload): + return struct.pack("<II", ie_id, len(payload)) + pad4(payload) + + +def board_entry(name, data): + inner = ie(IE_BOARD_NAME, name.encode()) + ie(IE_BOARD_DATA, data) + return ie(IE_BOARD, inner) + + +def build_board2(entries): + out = MAGIC + b"\x00" * ((4 - len(MAGIC) % 4) % 4) + for name, data in entries: + out += board_entry(name, data) + return out + + +def parse_board2(buf, wanted): + """Re-implementation of the kernel's parser, to prove the file is readable.""" + magic_len = len(MAGIC) + magic_len = (magic_len + 3) & ~3 + if buf[:len(MAGIC)] != MAGIC: + raise SystemExit("bad magic") + pos = magic_len + found = {} + while len(buf) - pos > 8: + ie_id, ie_len = struct.unpack_from("<II", buf, pos) + pos += 8 + if len(buf) - pos < ((ie_len + 3) & ~3): + raise SystemExit("invalid outer ie length") + if ie_id == IE_BOARD: + inner = buf[pos:pos + ie_len] + p = 0 + name = None + while len(inner) - p > 8: + bid, blen = struct.unpack_from("<II", inner, p) + p += 8 + if len(inner) - p < ((blen + 3) & ~3): + raise SystemExit("invalid inner ie length") + val = inner[p:p + blen] + p += (blen + 3) & ~3 + if bid == IE_BOARD_NAME: + name = val.decode() + elif bid == IE_BOARD_DATA: + if name in wanted: + found[name] = len(val) + pos += (ie_len + 3) & ~3 + return found + + +def main(): + os.makedirs(OUT, exist_ok=True) + raw = open(SPI, "rb").read() + if len(raw) < ART_OFF + 0x40000: + raise SystemExit("SPI dump too short") + + cal = raw[CAL_OFF:CAL_OFF + CAL_LEN] + if cal[:4] != b"\x01\x00\x04\x04": + print("warning: caldata does not start with the expected 01 00 04 04", file=sys.stderr) + open(os.path.join(OUT, "caldata.bin"), "wb").write(cal) + print("caldata.bin %d bytes sha256=%s" % (len(cal), __import__("hashlib").sha256(cal).hexdigest()[:16])) + + bdfs = sorted(glob.glob(os.path.join(BDFDIR, "bdwlan.b*"))) + entries = [] + for p in bdfs: + suffix = os.path.basename(p).split(".b")[-1] + if not suffix.isdigit(): + continue + data = open(p, "rb").read() + if len(data) != CAL_LEN: + print("skip %s (size %d)" % (p, len(data)), file=sys.stderr) + continue + entries.append(("bus=ahb,qmi-chip-id=0,qmi-board-id=%s" % suffix, data)) + + # The stock DTS says qcom,board-id = <0x08 0x00> and the firmware has no + # bdwlan.b08, so the vendor default bdwlan.bin is the BDF this device uses. + # mainline ath11k asks for qmi-board-id=255 (the FW reports no board info), + # so bind that name (and the bus fallback) to bdwlan.bin as well. + default = os.path.join(BDFDIR, "bdwlan.bin") + if os.path.exists(default): + data = open(default, "rb").read() + else: + data = entries[0][1] + entries.insert(0, ("bus=ahb,qmi-chip-id=0,qmi-board-id=255", data)) + entries.append(("bus=ahb", data)) + + # board.bin (API1) is what the stock firmware loads; keep it as a fallback + open(os.path.join(OUT, "board.bin"), "wb").write(data) + print("board.bin %d bytes sha256=%s" % (len(data), __import__("hashlib").sha256(data).hexdigest()[:16])) + + blob = build_board2(entries) + path = os.path.join(OUT, "board-oppo_ckb01.ipq8074") + open(path, "wb").write(blob) + print("board-2.bin %d bytes entries=%d" % (len(blob), len(entries))) + + wanted = {n for n, _ in entries} + got = parse_board2(blob, wanted) + print("parser check: %d/%d entries readable" % (len(got), len(entries))) + print("fallback bus=ahb present:", "bus=ahb" in got) + for n in list(got)[:3]: + print(" %-50s %d" % (n, got[n])) + + +if __name__ == "__main__": + main() |
