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/soc-args.py: generate the soc_* boot-args from a phone's own device tree -- reads the qcom,ipa / ipcc / aoss / rpmh-rsc / apps-smmu nodes and prints the register bases; devices/<name>/pull-soc.sh pulls /sys/firmware/fdt over adb and writes devices/<name>/soc.args, which modem-cycle appends to the kernel command line; every value it generates for the POCO X5 Pro equals the kernel's built-in default (verified), so the first phone is unaffected and soc.args stands as the worked example for porting

shimoku743a4f6a9ea5a6
diff --git a/devices/poco-x5-pro/README.md b/devices/poco-x5-pro/README.md
index 84ca947..6f5ffcb 100644
--- a/devices/poco-x5-pro/README.md
+++ b/devices/poco-x5-pro/README.md
@@ -31,3 +31,21 @@ TFTP) and the cameras (`fw-camera/`). All of it is the vendor's and stays out of
From the top of the repository, see the main README: `./xl enter`, `./xl ramdisk`, `./xl run`,
or everything in one command: `tools/modem-cycle.sh "ARGS" SECS`, or `make phone`.
+
+## Porting to another Qualcomm phone: the SoC addresses
+
+The kernel's modem and IPA drivers carry this phone's register and memory addresses as defaults and
+take any that differ from boot-args `soc_*` (`hakios.mpss.c`, `soc_override()`, and `hakios.ipa.c`).
+To bring up another phone, pull its own device tree and turn it into those arguments:
+
+```sh
+devices/<name>/pull-soc.sh # adb pull /sys/firmware/fdt, then tools/soc-args.py -> devices/<name>/soc.args
+```
+
+`tools/soc-args.py DEVICE-TREE.dtb` reads the `qcom,ipa`, `qcom,ipcc`, the AOSS mailbox, the RPMh
+controller and `apps-smmu` nodes and prints `soc_ipa_base`, `soc_gsi_base`, `soc_ipcc`, `soc_aoss`,
+`soc_rsc`, `soc_apps_smmu`. The run scripts append `devices/<name>/soc.args` to the kernel command
+line automatically (`SOC_ARGS=` to point elsewhere). The memory addresses (`soc_smem`, `soc_cmd_db`,
+`soc_mpss_mem`, `soc_imem`) come from the phone's reserved-memory nodes; add them by hand for now.
+For this phone every generated value equals the built-in default, so `soc.args` changes nothing here
+and is kept only as the worked example.
diff --git a/devices/poco-x5-pro/pull-soc.sh b/devices/poco-x5-pro/pull-soc.sh
new file mode 100755
index 0000000..bd3ef95
--- /dev/null
+++ b/devices/poco-x5-pro/pull-soc.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+# 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")/../.."
+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"
diff --git a/devices/poco-x5-pro/soc.args b/devices/poco-x5-pro/soc.args
new file mode 100644
index 0000000..acda697
--- /dev/null
+++ b/devices/poco-x5-pro/soc.args
@@ -0,0 +1,6 @@
+soc_ipa_base=0x1e40000
+soc_gsi_base=0x1e04000
+soc_ipcc=0x408000
+soc_aoss=0xc300000
+soc_rsc=0x18220000
+soc_apps_smmu=0x15000000
diff --git a/tools/modem-cycle.sh b/tools/modem-cycle.sh
index a69d7ec..4907abe 100755
--- a/tools/modem-cycle.sh
+++ b/tools/modem-cycle.sh
@@ -6,11 +6,16 @@
cd "$(dirname "$0")/.." || exit 1
PATH=$PWD/tools/macos-bin/platform-tools:$PWD/tools/macos-bin:$PATH
ARGS=$1; SECS=${2:-300}
+# A port's SoC addresses (devices/<name>/soc.args, made by devices/<name>/pull-soc.sh): added to the
+# kernel command line so hakios.mpss.c / hakios.ipa.c map that phone's blocks. SOC_ARGS names the file;
+# default is the one device there is. The POCO's values equal the kernel defaults, so this is a no-op there.
+SOC_ARGS=${SOC_ARGS-$(ls devices/*/soc.args 2>/dev/null | head -1)}
+SOC=""; [ -n "$SOC_ARGS" ] && [ -f "$SOC_ARGS" ] && SOC=$(tr '\n' ' ' < "$SOC_ARGS")
for i in $(seq 1 2160); do s=$(./xl state); [ "$s" != absent ] && break; sleep 10; done
echo "$(date +%T) phone: $s"; [ "$s" = absent ] && { echo "NO PHONE"; exit 1; }
./xl enter 2>&1 | tail -1
./xl ramdisk userland/system.apfs 2>&1 | tail -1
-XL_SB_DELAY=${XL_SB_DELAY:-600} XL_CMDLINE="debug=0x8 rd=md0 hv_wdt_pet=1 bootcpu=7 cpus=8 cpufreq=1 ufs=2 $ARGS" XL_SECS=$SECS XL_FOLLOW=1 XL_FOLLOW_KEEP=1 ./xl run 2>&1 | tail -2
+XL_SB_DELAY=${XL_SB_DELAY:-600} XL_CMDLINE="debug=0x8 rd=md0 hv_wdt_pet=1 bootcpu=7 cpus=8 cpufreq=1 ufs=2 $ARGS $SOC" XL_SECS=$SECS XL_FOLLOW=1 XL_FOLLOW_KEEP=1 ./xl run 2>&1 | tail -2
echo "$(date +%T) state after run: $(./xl state)"
L=$(ls -t logs/*-run.log.live | head -1); F=${L%.live}; echo "LOG=$F"
cat $L $F 2>/dev/null | grep -a "mpss: \(7: \(THE\|the watch\)\|11\|rmtfs\|tftp\|servreg\|dms\|nas\|ipa: \(THE\|from node\|the modem\|a probe\)\|link: \(THE\|tftp\)\|LAST WORDS\|step 1[01]\)\|serving system\|panic(\|ipa: \|cell: \|net: en1\|wda: \|wds: \(THE\|the client\|about\)" | grep -v "about to ask\|about to tell" | sed 's/^\[ *[0-9.]*\]: //' | awk '!seen[$0]++' | cut -c1-230 | head -80
diff --git a/tools/soc-args.py b/tools/soc-args.py
new file mode 100755
index 0000000..1b5058c
--- /dev/null
+++ b/tools/soc-args.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python3
+"""Print the modem/IPA SoC addresses of a Qualcomm phone as HakiOS boot-args, from its device tree.
+
+usage: tools/soc-args.py DEVICE-TREE.dtb
+
+The kernel's modem and IPA drivers (bsd/dev/hakios.mpss.c, hakios.ipa.c) carry the first phone's
+addresses as defaults and take any that differ from boot-args soc_* (see soc_override there). This
+reads a phone's own flattened device tree -- /sys/firmware/fdt on a rooted phone, or the dtb a port
+already has -- and prints the soc_* it needs, which devices/<name>/soc.args holds and the run
+scripts pass along. Only numbers are read out; no part of the tree is copied.
+"""
+import struct
+import sys
+
+
+def nodes_of(blob):
+ if blob[:4] != b'\xd0\x0d\xfe\xed':
+ sys.exit('soc-args.py: not a flattened device tree')
+ off_struct, off_strings = struct.unpack('>II', blob[8:16])
+ size_strings, = struct.unpack('>I', blob[32:36])
+ strings = blob[off_strings:off_strings + size_strings]
+ out, path, at = {}, [], off_struct
+ while True:
+ token, = struct.unpack('>I', blob[at:at + 4])
+ at += 4
+ if token == 1: # FDT_BEGIN_NODE
+ end = blob.index(b'\0', at)
+ path.append(blob[at:end].decode())
+ at = (end + 4) & ~3
+ out['/'.join(path)] = {}
+ elif token == 2: # FDT_END_NODE
+ path.pop()
+ elif token == 3: # FDT_PROP
+ length, name_off = struct.unpack('>II', blob[at:at + 8])
+ at += 8
+ name = strings[name_off:strings.index(b'\0', name_off)].decode()
+ out['/'.join(path)][name] = blob[at:at + length]
+ at = (at + length + 3) & ~3
+ elif token == 9: # FDT_END
+ return out
+ elif token != 4: # 4 = FDT_NOP
+ sys.exit(f'soc-args.py: unexpected token {token}')
+
+
+def find(nodes, compatible):
+ for path, props in nodes.items():
+ if compatible.encode() in props.get('compatible', b'').split(b'\0'):
+ return path, props
+ return None, None
+
+
+def find_named(nodes, leaf):
+ """The node whose last path component begins with `leaf` (e.g. apps-smmu@..., several SMMUs share a compatible)."""
+ for path, props in nodes.items():
+ if path.rsplit('/', 1)[-1].split('@', 1)[0] == leaf:
+ return path, props
+ return None, None
+
+
+def u32(value, index=0):
+ return struct.unpack_from('>I', value, index * 4)[0]
+
+
+def main():
+ if len(sys.argv) != 2:
+ sys.exit(__doc__)
+ nodes = nodes_of(open(sys.argv[1], 'rb').read())
+ args = []
+
+ path, ipa = find(nodes, 'qcom,ipa')
+ if ipa and 'reg' in ipa and len(ipa['reg']) >= 16: # reg = ipa-base, size, gsi-base, size (1 address + 1 size cell each)
+ ipa_region = u32(ipa['reg'], 0) # the IPA region's start; the registers the driver maps are 0x40000 into it
+ gsi_base = u32(ipa['reg'], 2) # the GSI's base is given directly
+ args.append(('soc_ipa_base', ipa_region + 0x40000))
+ args.append(('soc_gsi_base', gsi_base))
+
+ def block(compatible, arg, cell=0): # these nodes have 1 address + 1 size cell: address at an even index
+ _, props = find(nodes, compatible)
+ if props and 'reg' in props and len(props['reg']) >= (cell + 1) * 4:
+ args.append((arg, u32(props['reg'], cell)))
+
+ block('qcom,ipcc', 'soc_ipcc') # cell 0: the address
+ block('qcom,sm8350-aoss-qmp', 'soc_aoss') # the AOSS mailbox (its compatible varies by SoC)
+ block('qcom,rpmh-rsc', 'soc_rsc', cell=4) # drv-2 (the application processors') is the third addr/size pair (reg-names drv-0/1/2)
+ _, smmu = find_named(nodes, 'apps-smmu') # not by compatible: the GPU's SMMU shares qcom,qsmmu-v500
+ if smmu and 'reg' in smmu:
+ args.append(('soc_apps_smmu', u32(smmu['reg'], 0)))
+
+ for name, value in args:
+ print(f'{name}={value:#x}')
+ if not args:
+ sys.exit('soc-args.py: found none of the expected nodes; is this the phone\'s own device tree?')
+
+
+if __name__ == '__main__':
+ main()