Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

tools: new-device.py starts a port from a phone's own device tree; pull-soc.sh works on a root-only tree

shimoku8bdf5caa8759
diff --git a/ci/github-check.yml b/ci/github-check.yml
index a976fe7..598e8a3 100644
--- a/ci/github-check.yml
+++ b/ci/github-check.yml
@@ -40,3 +40,5 @@ jobs:
sh tools/soc-args-test.sh
# ... and finds the AOSS mailbox however a phone's own device tree describes it
python3 tools/soc-args-test-aoss.py
+ # ... and tools/new-device.py makes a port's directory from a phone's tree, and refuses what it should
+ python3 tools/new-device-test.py
diff --git a/devices/poco-x5-pro/pull-soc.sh b/devices/poco-x5-pro/pull-soc.sh
index bd3ef95..6bbf763 100755
--- a/devices/poco-x5-pro/pull-soc.sh
+++ b/devices/poco-x5-pro/pull-soc.sh
@@ -2,12 +2,24 @@
# Pull the phone's own device tree over adb and turn it into the SoC boot-args HakiOS needs on this
# device (tools/soc-args.py). The POCO X5 Pro matches the kernel's built-in values, so its soc.args
# comes out empty of surprises; another phone's will carry its addresses. Needs the phone in Android
-# with adb (root not required: /sys/firmware/fdt is world-readable).
-set -e
-cd "$(dirname "$0")/../.."
+# with adb. /sys/firmware/fdt is readable by everybody on most ROMs; where only root can read it (seen
+# on an SM7325 unit running a KernelSU build) the tree is copied under su to a staging file the shell
+# can read, pulled from there, and the staging file is removed again.
+set -e -o pipefail
+here=$(cd "$(dirname "$0")" && pwd) # before the cd: $0 may be a relative path
+cd "$here/../.."
PATH=$PWD/tools/macos-bin/platform-tools:$PATH
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
-adb pull /sys/firmware/fdt "$tmp/fdt" >/dev/null || { echo "pull-soc.sh: cannot read /sys/firmware/fdt over adb"; exit 1; }
-tools/soc-args.py "$tmp/fdt" | tee "$(dirname "$0")/soc.args"
-echo "pull-soc.sh: wrote $(dirname "$0")/soc.args"
+if ! adb pull /sys/firmware/fdt "$tmp/fdt" >/dev/null 2>&1; then
+ stage=/data/local/tmp/hakios-fdt
+ adb shell "su -c 'cat /sys/firmware/fdt > $stage && chmod 644 $stage'" >/dev/null 2>&1 || true
+ got=0; adb pull "$stage" "$tmp/fdt" >/dev/null 2>&1 || got=1
+ adb shell "su -c 'rm -f $stage'" >/dev/null 2>&1 || true
+ [ "$got" -eq 0 ] || { echo "pull-soc.sh: cannot read /sys/firmware/fdt over adb, with or without su"; exit 1; }
+fi
+# made aside and copied over, so that a failure leaves the soc.args that was there
+python3 tools/soc-args.py "$tmp/fdt" > "$tmp/soc.args"
+cp "$tmp/soc.args" "$here/soc.args"
+cat "$here/soc.args"
+echo "pull-soc.sh: wrote $here/soc.args"
diff --git a/tools/fdtbuild.py b/tools/fdtbuild.py
new file mode 100644
index 0000000..66d10f4
--- /dev/null
+++ b/tools/fdtbuild.py
@@ -0,0 +1,53 @@
+"""A tiny writer of flattened device trees, for tests: small trees of their own, so that no part of a
+real phone's tree has to be committed.
+
+ fdt({"": ({}, {"soc": ({}, {"qcom,ipcc@408000": ({"compatible": compat("qcom,ipcc"),
+ "reg": cells(0x408000, 0x1000)}, {})})})})
+
+A tree is {name: (properties, children)}, properties are {name: bytes}.
+"""
+import struct
+
+
+def fdt(tree):
+ """A flattened device tree for {name: (props, children)}; props is {prop: bytes}."""
+ strings, offsets, body = bytearray(), {}, bytearray()
+
+ def string_off(name):
+ if name not in offsets:
+ offsets[name] = len(strings)
+ strings.extend(name.encode() + b"\0")
+ return offsets[name]
+
+ def pad(b):
+ b.extend(b"\0" * (-len(b) % 4))
+
+ def node(name, props, children):
+ body.extend(struct.pack(">I", 1))
+ body.extend(name.encode() + b"\0")
+ pad(body)
+ for pname, value in props.items():
+ body.extend(struct.pack(">III", 3, len(value), string_off(pname)))
+ body.extend(value)
+ pad(body)
+ for cname, (cprops, cchildren) in children.items():
+ node(cname, cprops, cchildren)
+ body.extend(struct.pack(">I", 2))
+
+ node("", *tree[""])
+ body.extend(struct.pack(">I", 9))
+ header_size, rsvmap = 40, 16
+ off_struct = header_size + rsvmap
+ off_strings = off_struct + len(body)
+ total = off_strings + len(strings)
+ header = struct.pack(">IIIIIIIIII", 0xD00DFEED, total, off_struct, off_strings, header_size, 17, 16, 0,
+ len(strings), len(body))
+ return header + b"\0" * rsvmap + bytes(body) + bytes(strings)
+
+
+def cells(*values):
+ return b"".join(struct.pack(">I", v) for v in values)
+
+
+def compat(*names):
+ return b"".join(n.encode() + b"\0" for n in names)
diff --git a/tools/new-device-test.py b/tools/new-device-test.py
new file mode 100644
index 0000000..efe49b7
--- /dev/null
+++ b/tools/new-device-test.py
@@ -0,0 +1,129 @@
+#!/usr/bin/env python3
+"""tools/new-device.py: what it makes and what it refuses.
+
+ the reference phone's own tree -> "the SAME values", soc.args identical to the reference's
+ another SoC's tree -> says which values differ and which are missing; the README says so too
+ not a device tree -> a clear error, nothing made
+ a bad name, an existing directory -> refused (an existing one is written into only with --force)
+ --adb, the tree readable by root only (a stand-in adb; not on Windows, where a script is not a program)
+ -> pulled through su, and the staged copy is removed again
+"""
+import os
+import shutil
+import stat
+import subprocess
+import sys
+import tempfile
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+ROOT = os.path.dirname(HERE)
+sys.path.insert(0, HERE)
+from fdtbuild import cells, compat, fdt # noqa: E402
+
+TOOL = os.path.join(HERE, "new-device.py")
+REFERENCE_DTB = os.path.join(ROOT, "devices", "poco-x5-pro", "prebuilt", "device-tree.dtb")
+REFERENCE_ARGS = os.path.join(ROOT, "devices", "poco-x5-pro", "soc.args")
+failures = []
+
+
+def run(*args, env=None):
+ return subprocess.run([sys.executable, TOOL, *args], capture_output=True, text=True, env=env)
+
+
+def check(label, condition, detail=""):
+ print("%s %s" % ("ok " if condition else "FAIL", label))
+ if not condition:
+ failures.append(label)
+ if detail:
+ print(" " + detail.strip().replace("\n", "\n ")[:600])
+
+
+work = tempfile.mkdtemp(prefix="new-device-test-")
+try:
+ # 1. the reference phone's own tree
+ out = os.path.join(work, "ref")
+ r = run("same-soc", "--fdt", REFERENCE_DTB, "--out", out)
+ check("the reference tree: exit 0", r.returncode == 0, r.stdout + r.stderr)
+ check("the reference tree: says the SAME values", "the SAME values as the reference" in r.stdout, r.stdout)
+ check("the reference tree: soc.args is byte for byte the reference's",
+ os.path.exists(os.path.join(out, "soc.args")) and
+ open(os.path.join(out, "soc.args"), "rb").read() == open(REFERENCE_ARGS, "rb").read().replace(b"\r\n", b"\n"))
+ check("the reference tree: no carriage return in soc.args", b"\r" not in open(os.path.join(out, "soc.args"), "rb").read())
+ check("the reference tree: README, pull-soc.sh and soc.args made",
+ sorted(os.listdir(out)) == ["README.md", "pull-soc.sh", "soc.args"], str(sorted(os.listdir(out))))
+ readme = open(os.path.join(out, "README.md"), encoding="utf-8").read()
+ check("the reference tree: README names the board and carries the checklist",
+ "Yupik" in readme and "- [ ] **Cellular**" in readme and "the same as the reference" in readme)
+
+ # 2. another SoC: an IPCC at the usual place, the AOSS mailbox elsewhere, nothing else
+ other = fdt({"": ({"model": b"Some Phone\0", "compatible": compat("vendor,some-phone", "qcom,sm9999")},
+ {"soc": ({}, {"qcom,ipcc@408000": ({"compatible": compat("qcom,ipcc"), "reg": cells(0x408000, 0x1000)}, {}),
+ "power-controller@1000000": ({"compatible": compat("qcom,sc7280-aoss-qmp"),
+ "reg": cells(0x1000000, 0x400)}, {})})})})
+ other_path = os.path.join(work, "other.dtb")
+ open(other_path, "wb").write(other)
+ out = os.path.join(work, "other")
+ r = run("some-phone", "--fdt", other_path, "--out", out)
+ check("another SoC: exit 0", r.returncode == 0, r.stdout + r.stderr)
+ check("another SoC: does not claim to be the reference", "SAME" not in r.stdout and "differs" in r.stdout, r.stdout)
+ readme = open(os.path.join(out, "README.md"), encoding="utf-8").read()
+ check("another SoC: README says which differs and which are missing",
+ "**differs from the reference**" in readme and "**missing**" in readme and "Some Phone" in readme)
+ check("another SoC: soc.args holds what was found",
+ open(os.path.join(out, "soc.args")).read().split() == ["soc_ipcc=0x408000", "soc_aoss=0x1000000"],
+ open(os.path.join(out, "soc.args")).read())
+
+ # 3. refusals
+ junk = os.path.join(work, "junk.dtb")
+ open(junk, "wb").write(b"not a device tree at all")
+ r = run("x", "--fdt", junk, "--out", os.path.join(work, "junk"))
+ check("not a device tree: refused with a reason", r.returncode != 0 and "not a flattened device tree" in (r.stdout + r.stderr), r.stdout + r.stderr)
+ check("not a device tree: nothing made", not os.path.exists(os.path.join(work, "junk")))
+ r = run("Bad Name!", "--fdt", REFERENCE_DTB, "--out", os.path.join(work, "bad"))
+ check("a bad name: refused", r.returncode != 0 and "lower case" in (r.stdout + r.stderr), r.stdout + r.stderr)
+ r = run("again", "--fdt", REFERENCE_DTB, "--out", os.path.join(work, "ref"))
+ check("an existing directory: refused", r.returncode != 0 and "exists" in (r.stdout + r.stderr), r.stdout + r.stderr)
+ r = run("again", "--fdt", REFERENCE_DTB, "--out", os.path.join(work, "ref"), "--force")
+ check("an existing directory with --force: written", r.returncode == 0, r.stdout + r.stderr)
+ r = run("x", "--fdt", os.path.join(work, "missing.dtb"), "--out", os.path.join(work, "m"))
+ check("no such file: refused", r.returncode != 0 and "no such file" in (r.stdout + r.stderr), r.stdout + r.stderr)
+
+ # 4. --adb with the tree readable by root only (a stand-in adb: a shell script, so not on Windows)
+ if os.name != "nt":
+ fake = os.path.join(work, "bin")
+ os.makedirs(fake)
+ state = os.path.join(work, "state")
+ script = r"""#!/bin/sh
+# a phone whose /sys/firmware/fdt only root can read: a plain pull fails; su copies it to a staging file
+case "$1" in
+pull)
+ case "$2" in
+ /sys/firmware/fdt) echo "adb: error: failed to stat remote object '/sys/firmware/fdt': Permission denied" >&2; exit 1 ;;
+ /data/local/tmp/hakios-fdt) [ -f "$STATE" ] || exit 1; cp "$REF" "$3"; exit 0 ;;
+ esac ;;
+shell)
+ case "$2" in
+ *"cat /sys/firmware/fdt >"*) : > "$STATE"; exit 0 ;;
+ *"rm -f /data/local/tmp/hakios-fdt"*) rm -f "$STATE"; echo removed >> "$STATE.log"; exit 0 ;;
+ esac ;;
+esac
+exit 1
+"""
+ adb = os.path.join(fake, "adb")
+ open(adb, "w", newline="\n").write(script)
+ os.chmod(adb, os.stat(adb).st_mode | stat.S_IEXEC)
+ env = dict(os.environ, PATH=fake + os.pathsep + os.environ["PATH"], STATE=state, REF=REFERENCE_DTB)
+ r = run("from-adb", "--adb", "--out", os.path.join(work, "adb"), env=env)
+ check("--adb, root-only tree: pulled through su and recognised", r.returncode == 0 and "the SAME values" in r.stdout, r.stdout + r.stderr)
+ check("--adb, root-only tree: README says how it was got",
+ "through su" in open(os.path.join(work, "adb", "README.md"), encoding="utf-8").read())
+ check("--adb, root-only tree: the staged copy on the phone was removed again",
+ os.path.exists(state + ".log") and not os.path.exists(state))
+ else:
+ print("skip --adb with a stand-in adb (Windows)")
+finally:
+ shutil.rmtree(work, ignore_errors=True)
+
+if failures:
+ sys.exit("new-device-test: %d FAILED" % len(failures))
+print("new-device-test: all passed")
diff --git a/tools/new-device.py b/tools/new-device.py
new file mode 100644
index 0000000..1595232
--- /dev/null
+++ b/tools/new-device.py
@@ -0,0 +1,221 @@
+#!/usr/bin/env python3
+"""Start a port: make devices/NAME/ from a phone's own device tree, and say how far the SoC code carries.
+
+usage: tools/new-device.py NAME [--fdt FILE | --adb] [--out DIR] [--force]
+
+ NAME the new directory under devices/ (lower case letters, digits and dashes: poco-x5-pro)
+ --fdt the phone's device tree, a file (adb pull /sys/firmware/fdt; the tree of the phone's own Android)
+ --adb pull it from the phone on adb now (as the shell user, then through su: on some ROMs the file is
+ readable by root only)
+ --out where to make the directory (default: devices/NAME beside this tool); tests use it
+ --force write into a directory that exists
+
+What it makes, in devices/NAME/:
+ soc.args the soc_* boot-arguments tools/soc-args.py reads out of the tree: the addresses of the modem's,
+ the IPA's and the radio's blocks, which the run scripts add to the kernel command line
+ README.md what the tree says the phone is, which arguments were found and which not, which of them
+ differ from the reference phone's, and a checklist of what is the phone's own to port, with
+ the files whose head says where their numbers came from
+ pull-soc.sh pull the tree again and regenerate soc.args
+
+It also compares the result with the reference phone's (devices/poco-x5-pro): a tree that gives the same
+values means the kernel's built-in defaults already fit, and a port is then about the board (display,
+touch, UEFI) and not the SoC. Nothing here changes the phone; the only things read from it are its tree.
+"""
+import argparse
+import datetime
+import importlib.util
+import os
+import re
+import shutil
+import subprocess
+import sys
+import tempfile
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+ROOT = os.path.dirname(HERE)
+SOC_ARGS = os.path.join(HERE, "soc-args.py")
+REFERENCE = "poco-x5-pro"
+REFERENCE_DTB = os.path.join(ROOT, "devices", REFERENCE, "prebuilt", "device-tree.dtb")
+
+# what is the phone's own, and the files whose head says where their numbers came from
+CHECKLIST = [
+ ("UEFI", "the firmware that starts the loader: devices/%s/uefi/ is a port of Mu-Silicium; the vendor's drivers are "
+ "extracted from your own phone (uefi/README.md)" % REFERENCE),
+ ("Display, buttons, touch", "xnu/files/bsd/dev/hakios.input.c, hakios.buttons.c, hakios.goodix.c (the reference phone's "
+ "controller; another phone has another)"),
+ ("Storage", "hakios.ufs.c and hakios.ufshc.c (UFS); a phone with eMMC needs another driver"),
+ ("CPU clocks", "hakios.cpufreq.c"),
+ ("GPU", "hakios.adreno.c (Adreno 642L here: another generation needs its own firmware and registers)"),
+ ("Wi-Fi", "hakios.wifi.c, hakios.lx.c and wifi/lx/ (the WCN6750 radio of the reference phone)"),
+ ("Audio and battery", "hakios.adsp.c, hakios.audio.c, hakios.sound.c, hakios.speaker.c, hakios.mic.c, hakios.battery.c"),
+ ("Cellular", "hakios.mpss.c, hakios.qmi.c, hakios.nas.c, hakios.ipa.c, hakios.cell.c; soc.args carries the addresses, "
+ "the IPA layout is for IPA 4.11 (docs/cellular.md)"),
+ ("Haptics and cameras", "hakios.haptic.c, hakios.camera.c"),
+]
+
+
+def load_soc_args():
+ spec = importlib.util.spec_from_file_location("soc_args", SOC_ARGS)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+def generate(fdt_path):
+ """The lines tools/soc-args.py prints for a tree, or an error message."""
+ run = subprocess.run([sys.executable, SOC_ARGS, fdt_path], capture_output=True, text=True)
+ if run.returncode != 0:
+ return None, (run.stderr or run.stdout).strip()
+ return [l for l in run.stdout.splitlines() if l.strip()], None
+
+
+def text_prop(props, name):
+ value = props.get(name, b"")
+ return [s.decode(errors="replace") for s in value.split(b"\0") if s]
+
+
+def pull_fdt(dest):
+ adb = shutil.which("adb") or shutil.which("adb.exe")
+ for candidate in ("C:/platform-tools/adb.exe", "/mnt/c/platform-tools/adb.exe"):
+ adb = adb or (candidate if os.path.exists(candidate) else None)
+ if not adb:
+ sys.exit("new-device: adb not found; put platform-tools on PATH, or pass the tree with --fdt")
+
+ def adb_run(*args):
+ return subprocess.run([adb, *args], capture_output=True, text=True)
+
+ if adb_run("pull", "/sys/firmware/fdt", dest).returncode == 0 and os.path.getsize(dest) > 0:
+ return "adb pull /sys/firmware/fdt"
+ # some ROMs make it readable by root only: copy it under su to a place the shell can read, pull it, remove it
+ stage = "/data/local/tmp/hakios-fdt"
+ adb_run("shell", "su -c 'cat /sys/firmware/fdt > %s && chmod 644 %s'" % (stage, stage))
+ ok = adb_run("pull", stage, dest).returncode == 0 and os.path.exists(dest) and os.path.getsize(dest) > 0
+ adb_run("shell", "su -c 'rm -f %s'" % stage)
+ if not ok:
+ sys.exit("new-device: could not read /sys/firmware/fdt over adb, with or without su")
+ return "adb pull through su (the file is readable by root only on this ROM)"
+
+
+def main():
+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ ap.add_argument("name")
+ source = ap.add_mutually_exclusive_group(required=True)
+ source.add_argument("--fdt")
+ source.add_argument("--adb", action="store_true")
+ ap.add_argument("--out")
+ ap.add_argument("--force", action="store_true")
+ args = ap.parse_args()
+
+ if not re.fullmatch(r"[a-z0-9][a-z0-9-]*", args.name):
+ sys.exit("new-device: NAME is lower case letters, digits and dashes (like poco-x5-pro), got %r" % args.name)
+ out = args.out or os.path.join(ROOT, "devices", args.name)
+ if os.path.exists(out) and not args.force:
+ sys.exit("new-device: %s exists (--force to write into it)" % out)
+
+ tmp = tempfile.mkdtemp(prefix="new-device-")
+ try:
+ if args.adb:
+ fdt = os.path.join(tmp, "fdt")
+ how = pull_fdt(fdt)
+ else:
+ fdt = args.fdt
+ how = "the file %s" % os.path.basename(fdt)
+ if not os.path.isfile(fdt):
+ sys.exit("new-device: no such file: %s" % fdt)
+ lines, error = generate(fdt)
+ if lines is None:
+ sys.exit("new-device: %s\n (is this the phone's own device tree? adb pull /sys/firmware/fdt)" % error)
+
+ nodes = load_soc_args().nodes_of(open(fdt, "rb").read())
+ root = nodes.get("", {})
+ model = (text_prop(root, "model") or ["(the tree has no model)"])[0]
+ compatible = text_prop(root, "compatible")
+ reference, _ = generate(REFERENCE_DTB) if os.path.exists(REFERENCE_DTB) else (None, None)
+
+ found = dict(l.split("=", 1) for l in lines)
+ names = ["soc_ipa_base", "soc_gsi_base", "soc_ipcc", "soc_aoss", "soc_rsc", "soc_apps_smmu", "soc_cmd_db",
+ "soc_mpss_mem", "modem_ipa_fw_at", "soc_smem", "soc_imem", "soc_ipa_stream", "soc_ipa_stream_uc",
+ "soc_pas_mpss", "soc_pas_ipa", "soc_ipa_fw"]
+ missing = [n for n in names if n not in found]
+ ref = dict(l.split("=", 1) for l in reference) if reference else {}
+ differs = [n for n in found if n in ref and ref[n] != found[n]]
+ same_as_reference = bool(ref) and not missing and not differs and set(found) == set(ref)
+
+ os.makedirs(out, exist_ok=True)
+ open(os.path.join(out, "soc.args"), "w", newline="\n").write("\n".join(lines) + "\n")
+ pull = os.path.join(ROOT, "devices", REFERENCE, "pull-soc.sh")
+ if os.path.exists(pull):
+ shutil.copyfile(pull, os.path.join(out, "pull-soc.sh"))
+ os.chmod(os.path.join(out, "pull-soc.sh"), 0o755)
+
+ rows = "\n".join("| `%s` | `%s` | %s |" % (n, found.get(n, "(not found)"),
+ "the same as the reference" if n in found and ref.get(n) == found[n] else
+ ("**differs from the reference** (`%s`)" % ref.get(n) if n in found and n in ref else
+ "**missing**: the tree has no node this was read from; see the table in devices/%s/README.md" % REFERENCE
+ if n not in found else "")) for n in names)
+ verdict = (
+ "**The tree gives the same %d values as the reference phone (`devices/%s`).** The kernel's built-in defaults "
+ "already fit this SoC, so `soc.args` changes nothing here; what is left to port is the board (UEFI, display, "
+ "touch, and the rest of the checklist below), not the SoC." % (len(found), REFERENCE)
+ if same_as_reference else
+ "**This tree differs from the reference phone's** in %d value(s) and lacks %d: %s. Those are this phone's own "
+ "addresses; `soc.args` carries them to the kernel and the run scripts add the file to the command line." %
+ (len(differs), len(missing), ", ".join("`%s`" % n for n in (differs + missing)) or "none") if ref else
+ "No reference tree to compare with (devices/%s/prebuilt/device-tree.dtb is missing)." % REFERENCE)
+ checklist = "\n".join("- [ ] **%s**: %s" % (a, b) for a, b in CHECKLIST)
+ readme = """# Device: {model} ({name})
+
+Made by `tools/new-device.py` on {date}, from {how}. Edit it as the port goes.
+
+| What | Value |
+|---|---|
+| Board (the tree's `model`) | {model} |
+| Compatible | `{compatible}` |
+
+## The SoC addresses
+
+{verdict}
+
+| Argument | Value | Against the reference |
+|---|---|---|
+{rows}
+
+`soc.args` is what `tools/soc-args.py` read out of the tree; `pull-soc.sh` makes it again from the phone on
+adb. The run scripts add it to the kernel command line. Where each argument comes from in the tree:
+`devices/{reference}/README.md`.
+
+## What is this phone's own
+
+The kernel, loader and user space know nothing about a phone: addresses, interrupts, rails and firmware
+come from its tree and its Android. What is written for one phone is the list below. The head of each file
+says where its numbers came from; that is the map of what to rewrite.
+
+{checklist}
+
+## Firmware
+
+Pulled from your own phone, never committed: `devices/{reference}/pull-firmware.sh`, or
+`pull-firmware-root.sh` where the firmware is readable by root only. Check with `make doctor`.
+
+## Before you open a pull request
+
+`tools/provenance-check.py` must pass: no vendor firmware, no UEFI drivers, no Apple files in the tree.
+""".format(model=model, name=args.name, date=datetime.date.today().isoformat(), how=how,
+ compatible=" ".join(compatible), verdict=verdict, rows=rows, checklist=checklist, reference=REFERENCE)
+ open(os.path.join(out, "README.md"), "w", newline="\n").write(readme)
+
+ print("new-device: %s" % model)
+ print("new-device: %d of %d soc_* values read from the tree%s" % (len(found), len(names),
+ "" if not missing else "; missing: " + ", ".join(missing)))
+ print("new-device: %s" % ("the SAME values as the reference phone (%s): the SoC code already fits" % REFERENCE
+ if same_as_reference else "differs from the reference in: %s" % (", ".join(differs + missing) or "nothing")))
+ print("new-device: wrote %s" % out)
+ for f in sorted(os.listdir(out)):
+ print(" " + f)
+ finally:
+ shutil.rmtree(tmp, ignore_errors=True)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tools/soc-args-test-aoss.py b/tools/soc-args-test-aoss.py
index 9b018ad..610c2b6 100644
--- a/tools/soc-args-test-aoss.py
+++ b/tools/soc-args-test-aoss.py
@@ -13,57 +13,16 @@ tools/soc-args.py on each:
neither -> no soc_aoss, and no failure
"""
import os
-import struct
import subprocess
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
+sys.path.insert(0, HERE)
SOC_ARGS = os.path.join(HERE, "soc-args.py")
-def fdt(tree):
- """A flattened device tree for {name: (props, children)}; props is {prop: bytes}."""
- strings, offsets, body = bytearray(), {}, bytearray()
-
- def string_off(name):
- if name not in offsets:
- offsets[name] = len(strings)
- strings.extend(name.encode() + b"\0")
- return offsets[name]
-
- def pad(b):
- b.extend(b"\0" * (-len(b) % 4))
-
- def node(name, props, children):
- body.extend(struct.pack(">I", 1))
- body.extend(name.encode() + b"\0")
- pad(body)
- for pname, value in props.items():
- body.extend(struct.pack(">III", 3, len(value), string_off(pname)))
- body.extend(value)
- pad(body)
- for cname, (cprops, cchildren) in children.items():
- node(cname, cprops, cchildren)
- body.extend(struct.pack(">I", 2))
-
- node("", *tree[""])
- body.extend(struct.pack(">I", 9))
- header_size, rsvmap = 40, 16
- off_struct = header_size + rsvmap
- off_strings = off_struct + len(body)
- total = off_strings + len(strings)
- header = struct.pack(">IIIIIIIIII", 0xD00DFEED, total, off_struct, off_strings, header_size, 17, 16, 0,
- len(strings), len(body))
- return header + b"\0" * rsvmap + bytes(body) + bytes(strings)
-
-
-def cells(*values):
- return b"".join(struct.pack(">I", v) for v in values)
-
-
-def compat(*names):
- return b"".join(n.encode() + b"\0" for n in names)
+from fdtbuild import cells, compat, fdt # noqa: E402 (tools/fdtbuild.py)
def soc_with(**nodes):