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

The loader's identity header is made at build time from the builder's own IPSW, not kept in the repository

shimoku742d43f0d2353c
diff --git a/.gitignore b/.gitignore
index 8fe3fbb..afce41c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -80,3 +80,8 @@ __pycache__/
!/xnu-deps/build/lzbitmap/libzbitmap.h
/tools/android/f2fs-resize
/tools/macos-bin/platform-tools/
+
+# made at build time from the device tree of the builder's own IPSW (xnuloader/mkidentity.sh)
+/xnuloader/identity.h
+!/xnuloader/im4p.py
+!/xnuloader/mkidentity.sh
diff --git a/README.md b/README.md
index 4131e47..5bbe54c 100644
--- a/README.md
+++ b/README.md
@@ -125,7 +125,11 @@ xnu-deps/setup.sh # зависимости, инструменты и SDK
./xnu-build.sh -j8 -k # → xnu-src/…/BUILD/obj/DEVELOPMENT_ARM64_BCM2837/mach.development.bcm2837
```
-Загрузчик: `xnuloader/build.sh` (clang + lld-link, EFI-приложение без EDK2). Userland и образы:
+Загрузчик: `xnuloader/build.sh` (clang + lld-link, EFI-приложение без EDK2). Заголовок
+`xnuloader/identity.h` — свойства, по которым iOS узнаёт, «какой она iPhone», — в репозитории не
+хранится: `xnuloader/mkidentity.sh` делает его при сборке из дерева устройств вашего IPSW (один файл
+в 34 КБ с серверов Apple, LZFSE декодирует `lzfse`, `compression_tool` macOS или модуль Python
+`lzfse`; без них собирается открытая библиотека lzfse). Userland и образы:
`REST=1 userland/build.sh` (`MODEM=1 SYSTEM=1` кладут прошивку модема в образ системы). Подробности —
`xnu/README.md` и `docs/notes.md`.
diff --git a/docs/notes.md b/docs/notes.md
index 4c9be05..e7578dc 100644
--- a/docs/notes.md
+++ b/docs/notes.md
@@ -1709,8 +1709,8 @@ xnu-deps/setup.sh # зависимости, инструменты и SDK
xnuloader/build.sh # clang + lld-link → xnuloader/loader.efi (EFI-приложение без EDK2)
```
-`xnuloader/identity.h` сгенерирован `mkidentity.py` из дерева устройств iPhone X (так система узнаёт, «какой она
-iPhone»); перегенерировать: `python3 ios165/fetch-from-ipsw.py Firmware/all_flash/DeviceTree.d22ap.im4p dt.im4p`,
+`xnuloader/identity.h` генерируется при сборке `xnuloader/mkidentity.sh` (через `mkidentity.py`) из дерева устройств
+iPhone X в вашем IPSW (так система узнаёт, «какой она iPhone»); в репозитории его нет. Вручную: `python3 ios165/fetch-from-ipsw.py Firmware/all_flash/DeviceTree.d22ap.im4p dt.im4p`,
`ipsw img4 im4p extract dt.im4p -o dt.bin`, `xnuloader/mkidentity.py dt.bin > xnuloader/identity.h`. Кроме свойств
корня и `/product` оттуда же берётся узел `/product/haptics` (`closed-loop`, `supports-3rd-party-haptics`): по нему
MobileGestalt отвечает `DeviceSupportsClosedLoopHaptics`, и iOS играет вибрацию и отклики волнами на устройстве
diff --git a/xnuloader/build.sh b/xnuloader/build.sh
index d2a3129..d8e696a 100755
--- a/xnuloader/build.sh
+++ b/xnuloader/build.sh
@@ -2,6 +2,9 @@
# Build the XNU loader as a freestanding AArch64 UEFI application.
set -e
cd "$(dirname "$0")"
+# the iPhone's identity for XNU, from the device tree of the builder's own IPSW (not in the repository)
+[ -f identity.h ] || ./mkidentity.sh
+
clang --target=aarch64-unknown-windows -ffreestanding -fno-builtin -fno-stack-protector -fshort-wchar \
-mgeneral-regs-only -O1 -Wall -Wextra -Wno-unused-parameter -c loader.c -o loader.obj
# /base: UEFI loads an image at its preferred address when that is free. The PE default (0x140000000)
diff --git a/xnuloader/identity.h b/xnuloader/identity.h
deleted file mode 100644
index 980ca05..0000000
--- a/xnuloader/identity.h
+++ /dev/null
@@ -1,172 +0,0 @@
-/* Generated by mkidentity.py from the device tree of the iOS firmware. Do not edit. */
-struct identity_prop { const char *name; unsigned offset, length; };
-
-/* 6 properties */
-static const struct identity_prop identity_root[] = {
- { "target-type", 0, 4 },
- { "manufacturer", 4, 11 },
- { "target-sub-type", 15, 6 },
- { "compatible", 21, 26 },
- { "model", 47, 11 },
- { "device_type", 58, 8 },
-};
-static const unsigned char identity_root_data[] = {
- 0x44, 0x32, 0x32, 0x00, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x00, 0x44, 0x32, 0x32, 0x41, 0x50,
- 0x00, 0x44, 0x32, 0x32, 0x41, 0x50, 0x00, 0x69, 0x50, 0x68, 0x6f, 0x6e, 0x65, 0x31, 0x30, 0x2c, 0x33, 0x00, 0x41, 0x70,
- 0x70, 0x6c, 0x65, 0x41, 0x52, 0x4d, 0x00, 0x69, 0x50, 0x68, 0x6f, 0x6e, 0x65, 0x31, 0x30, 0x2c, 0x33, 0x00, 0x62, 0x6f,
- 0x6f, 0x74, 0x72, 0x6f, 0x6d, 0x00,
-};
-
-/* 61 properties */
-static const struct identity_prop identity_product[] = {
- { "sandman-support", 0, 4 },
- { "display-mirroring", 4, 4 },
- { "car-integration", 8, 4 },
- { "assistant", 12, 4 },
- { "lockdown-certtype", 16, 4 },
- { "ui-pip", 20, 4 },
- { "wifi-chipset", 24, 5 },
- { "product-name", 29, 9 },
- { "hearingaid-audio-equalization", 38, 4 },
- { "RF-exposure-separation-distance", 42, 4 },
- { "mobiledevice-min-ver", 46, 5 },
- { "product-id", 51, 20 },
- { "gps-capable", 71, 4 },
- { "product-description", 75, 9 },
- { "allow-32bit-apps", 84, 4 },
- { "low-power-wallet-mode", 88, 4 },
- { "builtin-mics", 92, 4 },
- { "has-boot-chime", 96, 4 },
- { "supports-burnin-mitigation", 100, 4 },
- { "compatible-device-fallback", 104, 10 },
- { "device-perf-memory-class", 114, 4 },
- { "artwork-scale-factor", 118, 4 },
- { "baseband-chipset", 122, 6 },
- { "hearingaid-low-energy-audio", 128, 4 },
- { "ui-background-quality", 132, 4 },
- { "supports-avatars", 136, 0 },
- { "public-key-accelerator", 136, 4 },
- { "disable-globe-map", 140, 0 },
- { "artwork-device-idiom", 140, 6 },
- { "device-color-policy", 146, 4 },
- { "graphics-featureset-class", 150, 7 },
- { "siri-gesture", 157, 4 },
- { "supports-maps-optical-heading", 161, 0 },
- { "compatible-app-variant", 161, 2 },
- { "artwork-dynamic-displaymode", 163, 2 },
- { "bluetooth-le", 165, 4 },
- { "watch-companion", 169, 4 },
- { "has-applelpm", 173, 4 },
- { "iap2-protocol-supported", 177, 4 },
- { "ephemeral-data-mode", 181, 4 },
- { "activation-protocol-version", 185, 4 },
- { "volume-up-button-location", 189, 20 },
- { "ptp-large-files", 209, 4 },
- { "udid-version", 213, 4 },
- { "offline-dictation", 217, 4 },
- { "location-reminders", 221, 4 },
- { "ui-weather-quality", 225, 4 },
- { "graphics-featureset-fallbacks", 229, 38 },
- { "dictation", 267, 4 },
- { "itunes-min-ver", 271, 4 },
- { "artwork-display-gamut", 275, 3 },
- { "front-cam-offset-from-center", 278, 16 },
- { "ui-reachability", 294, 4 },
- { "volume-down-button-location", 298, 20 },
- { "personal-hotspot", 318, 4 },
- { "single-stage-boot", 322, 4 },
- { "mobiledevice-beta-min-ver", 326, 11 },
- { "oled-display", 337, 4 },
- { "thin-bezel", 341, 4 },
- { "artwork-device-subtype", 345, 4 },
- { "side-button-location", 349, 20 },
-};
-static const unsigned char identity_product_data[] = {
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x34, 0x33, 0x35, 0x37, 0x00, 0x69, 0x50, 0x68, 0x6f, 0x6e, 0x65, 0x20, 0x58, 0x00, 0x01, 0x00,
- 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x31, 0x34, 0x30, 0x30, 0x00, 0xfd, 0x07, 0x9b, 0x29, 0x81, 0x44, 0xa7, 0xde, 0xb4,
- 0xd7, 0xc8, 0x7f, 0xef, 0x3e, 0xb4, 0x74, 0xd8, 0xf0, 0xe7, 0xeb, 0x01, 0x00, 0x00, 0x00, 0x69, 0x50, 0x68, 0x6f, 0x6e,
- 0x65, 0x20, 0x58, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x69, 0x50, 0x68, 0x6f, 0x6e, 0x65, 0x39, 0x2c, 0x32, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00,
- 0x00, 0x00, 0x6d, 0x61, 0x76, 0x31, 0x37, 0x00, 0x01, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x50, 0x50, 0x4c, 0x45, 0x34, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x30, 0x00, 0x30, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x78, 0xb0, 0x09, 0x00, 0xff, 0x96, 0x14, 0x00, 0x30, 0xe0, 0x03,
- 0x00, 0xca, 0x9d, 0x01, 0x00, 0x10, 0x27, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x41, 0x50, 0x50, 0x4c, 0x45, 0x33, 0x3a, 0x41, 0x50, 0x50, 0x4c,
- 0x45, 0x33, 0x76, 0x31, 0x3a, 0x41, 0x50, 0x50, 0x4c, 0x45, 0x32, 0x3a, 0x41, 0x50, 0x50, 0x4c, 0x45, 0x31, 0x3a, 0x47,
- 0x4c, 0x45, 0x53, 0x32, 0x2c, 0x30, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x07, 0x0c, 0x00, 0x50, 0x33, 0x00, 0xd0, 0x01,
- 0x01, 0x00, 0x9e, 0x1d, 0x00, 0x00, 0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0xb0,
- 0x09, 0x00, 0xff, 0x96, 0x14, 0x00, 0x48, 0xd0, 0x05, 0x00, 0xca, 0x9d, 0x01, 0x00, 0x10, 0x27, 0x00, 0x00, 0x01, 0x00,
- 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x31, 0x31, 0x39, 0x30, 0x2e, 0x31, 0x30, 0x30, 0x2e, 0x32, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x84, 0x09, 0x00, 0x00, 0x75, 0xf3, 0x00, 0x00, 0x2a, 0x0f, 0x02, 0x00, 0x00, 0x71, 0x00,
- 0x00, 0xea, 0x3f, 0x00, 0x00, 0xe8, 0x03, 0x00, 0x00,
-};
-
-/* 2 properties */
-static const struct identity_prop identity_haptics[] = {
- { "closed-loop", 0, 4 },
- { "supports-3rd-party-haptics", 4, 4 },
-};
-static const unsigned char identity_haptics_data[] = {
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
-};
-
-/* 43 properties */
-static const struct identity_prop identity_camera[] = {
- { "aggregate-cam-video-zoom", 0, 4 },
- { "rear-max-video-fps-4k", 4, 4 },
- { "rear-max-video-zoom", 8, 4 },
- { "rear-max-burst-length", 12, 4 },
- { "camera-hdr-version", 16, 4 },
- { "front-max-burst-length", 20, 4 },
- { "auto-focus", 24, 4 },
- { "front-auto-hdr", 28, 4 },
- { "front-burst-image-duration", 32, 4 },
- { "rear-max-video-fps-720p", 36, 4 },
- { "rear-slowmo", 40, 4 },
- { "flash", 44, 4 },
- { "pipelined-stillimage-capability", 48, 4 },
- { "rear-max-slomo-video-fps-1080p", 52, 4 },
- { "front-hdr", 56, 4 },
- { "front-flash-capability", 60, 4 },
- { "pearl-camera", 64, 4 },
- { "video-cap", 68, 4 },
- { "front-hdr-on", 72, 4 },
- { "rear-auto-hdr", 76, 4 },
- { "front-burst", 80, 4 },
- { "p3-color-space-video-recording", 84, 4 },
- { "rear-burst-image-duration", 88, 4 },
- { "rear-max-slomo-video-fps-720p", 92, 4 },
- { "video-stills", 96, 4 },
- { "rear-max-video-frame_rate", 100, 4 },
- { "rear-hdr-on", 104, 4 },
- { "rear-max-video-fps-1080p", 108, 4 },
- { "aggregate-camera", 112, 4 },
- { "rear-stage-light-portrait", 116, 0 },
- { "aggregate-cam-photo-zoom", 116, 4 },
- { "rear-hdr", 120, 4 },
- { "rear-burst", 124, 4 },
- { "rear-cam-telephoto-capability", 128, 4 },
- { "post-effects", 132, 4 },
- { "stage-light-portrait-preview", 136, 4 },
- { "live-effects", 140, 4 },
- { "front-max-video-zoom", 144, 4 },
- { "front-max-video-fps-720p", 148, 4 },
- { "panorama", 152, 4 },
- { "front-max-video-fps-1080p", 156, 4 },
- { "front-stage-light-portrait", 160, 0 },
- { "live-photo-capture", 160, 4 },
-};
-static const unsigned char identity_camera_data[] = {
- 0x58, 0x02, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x58, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x58, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x3c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xe8, 0x03, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00,
-};
-
diff --git a/xnuloader/im4p.py b/xnuloader/im4p.py
new file mode 100755
index 0000000..bbc9331
--- /dev/null
+++ b/xnuloader/im4p.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+"""The payload of an IM4P container (an Image4 payload: what the files of Firmware/ in an IPSW are).
+usage: im4p.py IN.im4p OUT
+IM4P is DER: SEQUENCE { IA5String "IM4P", IA5String type, IA5String description, OCTET STRING payload, ... }.
+The payload comes out as it is in the container -- for a device tree that is an LZFSE stream ("bvx2"),
+to be decoded by lzfse(1), compression_tool(1) of macOS, or the lzfse Python module (see mkidentity.sh).
+"""
+import sys
+
+
+def tlv(data, pos):
+ """One DER element: (tag, value start, value end)."""
+ tag = data[pos]
+ length = data[pos + 1]
+ pos += 2
+ if length & 0x80:
+ n = length & 0x7f
+ length = int.from_bytes(data[pos:pos + n], 'big')
+ pos += n
+ return tag, pos, pos + length
+
+
+def main():
+ data = open(sys.argv[1], 'rb').read()
+ tag, pos, end = tlv(data, 0)
+ if tag != 0x30:
+ sys.exit('im4p.py: not a DER sequence')
+ strings = []
+ while pos < end:
+ tag, start, stop = tlv(data, pos)
+ if tag == 0x16:
+ strings.append(data[start:stop].decode('ascii', 'replace'))
+ elif tag == 0x04:
+ if len(strings) < 2 or strings[0] != 'IM4P':
+ sys.exit('im4p.py: not an IM4P (%r)' % strings)
+ with open(sys.argv[2], 'wb') as out:
+ out.write(data[start:stop])
+ print('im4p.py: %s (%s): payload of %d bytes%s' % (strings[1], strings[2] if len(strings) > 2 else '',
+ stop - start, ', LZFSE' if data[start:start + 3] == b'bvx' else ''))
+ return
+ pos = stop
+ sys.exit('im4p.py: no payload')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/xnuloader/mkidentity.sh b/xnuloader/mkidentity.sh
new file mode 100755
index 0000000..77fe6d9
--- /dev/null
+++ b/xnuloader/mkidentity.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+# Make xnuloader/identity.h: the identity of the iPhone whose files iOS 16.5 was built for, taken
+# from the device tree in the IPSW (Firmware/all_flash/DeviceTree.d22ap.im4p) at build time, on
+# the builder's machine, from their own copy of the firmware. The header is not in the repository.
+#
+# ios165/fetch-from-ipsw.py the one file out of the IPSW on Apple's CDN (34 KB, HTTP ranges)
+# xnuloader/im4p.py its payload: an LZFSE stream
+# lzfse(1) / compression_tool(1) / python3 -m lzfse / xnu-deps/host/bin/lzfse (built from the
+# BSD-licensed github.com/lzfse/lzfse when nothing else is there)
+# xnuloader/mkidentity.py the header from the decoded tree
+#
+# usage: xnuloader/mkidentity.sh [DeviceTree.im4p] (an im4p already at hand skips the download)
+set -e
+HERE=$(cd "$(dirname "$0")" && pwd)
+TOP=$(cd "$HERE/.." && pwd)
+WORK=$(mktemp -d "${TMPDIR:-/tmp}/mkidentity.XXXXXX")
+trap 'rm -rf "$WORK"' EXIT
+IM4P=${1:-$WORK/dt.im4p}
+if [ ! -f "$IM4P" ]; then
+ python3 "$TOP/ios165/fetch-from-ipsw.py" Firmware/all_flash/DeviceTree.d22ap.im4p "$IM4P"
+fi
+python3 "$HERE/im4p.py" "$IM4P" "$WORK/dt.lzfse"
+decode() {
+ if command -v lzfse >/dev/null; then lzfse -decode -i "$1" -o "$2"
+ elif [ -x /usr/bin/compression_tool ]; then /usr/bin/compression_tool -decode -a lzfse -i "$1" -o "$2"
+ elif python3 -c 'import lzfse' 2>/dev/null; then python3 -c 'import lzfse,sys; open(sys.argv[2],"wb").write(lzfse.decompress(open(sys.argv[1],"rb").read()))' "$1" "$2"
+ elif [ -x "$TOP/xnu-deps/host/bin/lzfse" ]; then "$TOP/xnu-deps/host/bin/lzfse" -decode -i "$1" -o "$2"
+ else
+ echo "mkidentity.sh: no LZFSE decoder: building lzfse (BSD, github.com/lzfse/lzfse) into xnu-deps/host/bin" >&2
+ git clone -q --depth 1 https://github.com/lzfse/lzfse "$WORK/lzfse" && make -s -C "$WORK/lzfse" lzfse >/dev/null &&
+ mkdir -p "$TOP/xnu-deps/host/bin" && cp "$WORK/lzfse/build/bin/lzfse" "$TOP/xnu-deps/host/bin/lzfse" &&
+ "$TOP/xnu-deps/host/bin/lzfse" -decode -i "$1" -o "$2"
+ fi
+}
+decode "$WORK/dt.lzfse" "$WORK/dt.bin"
+python3 "$HERE/mkidentity.py" "$WORK/dt.bin" > "$HERE/identity.h.new"
+mv "$HERE/identity.h.new" "$HERE/identity.h"
+echo "mkidentity.sh: xnuloader/identity.h made from the device tree ($(wc -c < "$WORK/dt.bin") bytes)"