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 App cryptex, Apple's image as it is: Safari's daemons from /System/Cryptexes/App

vcvkkbfd7fa936aa0
diff --git a/ios165/fetch.sh b/ios165/fetch.sh
index 73a2717..ef6f43d 100755
--- a/ios165/fetch.sh
+++ b/ios165/fetch.sh
@@ -6,6 +6,10 @@
# system.files/.manifest what was chosen, with owners, modes and symbolic links
# var.skel the skeleton of /private/var (see mkvarskel.py)
# cryptex/ the OS cryptex without the dyld shared cache
+# cryptex-app.dmg the App cryptex as Apple ships it (10 MB, an APFS container: Safari's
+# daemons, its settings and the bundles around it, which the root volume's
+# launchd jobs and links reach through /System/Cryptexes/App)
+# cryptex-app/ ... and its files
# 20F66__iPhone10,3_6/ the dyld shared cache (arm64)
#
# Nothing of this is in the repository: the two disk images are fetched out of the IPSW on
@@ -19,6 +23,7 @@ set -e
cd "$(dirname "$0")"
ROOTFS_DMG=096-04088-077.dmg # the root filesystem, APFS
CRYPTEX_DMG=096-04666-078.dmg # the OS cryptex: dyld shared cache and a few libraries
+APP_CRYPTEX_DMG=096-03536-083.dmg # the App cryptex (BuildManifest: Cryptex1,AppOS)
CACHE=20F66__iPhone10,3_6
UNZBM=../xnu-deps/host/bin/unzbm
@@ -81,4 +86,21 @@ if [ ! -f .done-cryptex ]; then
touch .done-cryptex
[ -n "$KEEP" ] || rm -f cryptex-os.dmg
fi
-echo "fetch.sh: done: $(du -sh rootfs | cut -f1) rootfs, $(du -sh cryptex | cut -f1) cryptex, $(du -sh $CACHE | cut -f1) dyld shared cache"
+
+# --- the App cryptex ------------------------------------------------------------------------------
+# kept as it is (the boot off the flash puts it in memory unchanged, ../userland/build.sh FLASHBOOT=1)
+# and unpacked (the system image has its files at /System/Cryptexes/App)
+if [ ! -f .done-cryptex-app ]; then
+ if [ ! -f cryptex-app.dmg ]; then
+ echo "fetch.sh: downloading the App cryptex image (10 MB)"
+ python3 fetch-from-ipsw.py $APP_CRYPTEX_DMG cryptex-app.dmg.part
+ mv cryptex-app.dmg.part cryptex-app.dmg
+ fi
+ echo "fetch.sh: extracting the App cryptex"
+ rm -rf cryptex-app
+ 7z x -y -ocryptex-app cryptex-app.dmg > extract-cryptex-app.log 2>&1 || true
+ unzbm_all cryptex-app
+ [ -f cryptex-app/usr/libexec/webbookmarksd ] || { echo "fetch.sh: the App cryptex is incomplete"; exit 1; }
+ touch .done-cryptex-app
+fi
+echo "fetch.sh: done: $(du -sh rootfs | cut -f1) rootfs, $(du -sh cryptex | cut -f1) cryptex, $(du -sh cryptex-app | cut -f1) App cryptex, $(du -sh $CACHE | cut -f1) dyld shared cache"
diff --git a/userland/build.sh b/userland/build.sh
index 60e1248..192482b 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -287,19 +287,20 @@ python3 ../tools/apfs/mkapfs.py -z --plain fw -o root.apfs redwood=root | tail -
# --- the RAM part of a boot off the flash: FLASHBOOT=1 -> flashboot.apfs --------------------------------
# With the iOS volume on the phone's flash as the root, all that has to come from RAM is what is not
-# on it -- the OS cryptex with the dyld shared cache -- and what starts the boot: two APFS containers
-# back to back in the one resident image. The first, the boot volume, is the root the kernel starts
-# from (rd=md0): stage0 as /sbin/launchd mounts the small image on /redwood and runs its init, which
-# with boot-arg root_apfs=DEV mounts the volume on /flash, makes it the root and moves the boot volume
-# to the volume's /private/preboot, then mounts the second container -- the OS cryptex -- on the boot
-# volume's empty Cryptexes/OS: /private/preboot/Cryptexes/OS, where the volume's own
-# /System/Cryptexes/OS points and where iOS grafts the cryptex (src/init.c, flash_root; the kernel's
-# apfs takes "/dev/md0,c=1", and bsd/vm/vm_unix.c takes a shared cache from there). CRYPTEX_IMAGE=FILE:
+# on it -- the two cryptexes, OS with the dyld shared cache and App -- and what starts the boot: three
+# APFS containers back to back in the one resident image. The first, the boot volume, is the root the
+# kernel starts from (rd=md0): stage0 as /sbin/launchd mounts the small image on /redwood and runs its
+# init, which with boot-arg root_apfs=DEV mounts the volume on /flash, makes it the root and moves the
+# boot volume to the volume's /private/preboot, then mounts the second container -- the OS cryptex -- on
+# the boot volume's empty Cryptexes/OS and the third -- Apple's App cryptex as it is -- on Cryptexes/App:
+# /private/preboot/Cryptexes/OS and App, where the volume's own /System/Cryptexes/OS and App point and
+# where iOS grafts the cryptexes (src/init.c, flash_root; the kernel's apfs takes "/dev/md0,c=1" and
+# "c=2", and bsd/vm/vm_unix.c takes a shared cache from Cryptexes/OS). CRYPTEX_IMAGE=FILE:
# Apple's own image of the cryptex as it is instead of this one remade (2.6 GB against 1 GB: too much
# for the phone's memory beside iOS, not for QEMU's). Upload it with `xl ramdisk userland/flashboot.apfs`.
if [ -n "$FLASHBOOT" ] && [ -f $CACHE/dyld_shared_cache_arm64 ]; then
rm -rf flashboot flashboot.os
- mkdir -p flashboot/sbin flashboot/redwood flashboot/dev flashboot/flash flashboot/Cryptexes/OS
+ mkdir -p flashboot/sbin flashboot/redwood flashboot/dev flashboot/flash flashboot/Cryptexes/OS flashboot/Cryptexes/App
cp obj/stage0 flashboot/sbin/launchd
if [ -d $IOS/cryptex ]; then cp -al $IOS/cryptex flashboot.os; else mkdir -p flashboot.os; fi
# the audio DSP's firmware and the speaker amplifiers' configuration, as in the system image's /fw: the kernel
@@ -321,12 +322,15 @@ if [ -n "$FLASHBOOT" ] && [ -f $CACHE/dyld_shared_cache_arm64 ]; then
done
python3 ../tools/apfs/mkapfs.py -z --plain fw -o flashboot.boot.apfs Boot=flashboot | tail -1
[ -z "$CHECK" ] || ../tools/apfs/check-image.sh flashboot.boot.apfs Boot=flashboot
+ # the third container: Apple's App cryptex as it is (10 MB; fetch.sh), for the boot volume's Cryptexes/App
+ APP=""
+ [ -f $IOS/cryptex-app.dmg ] && APP=$IOS/cryptex-app.dmg
if [ -n "$CRYPTEX_IMAGE" ]; then
- cat flashboot.boot.apfs "$CRYPTEX_IMAGE" > flashboot.apfs
+ cat flashboot.boot.apfs "$CRYPTEX_IMAGE" $APP > flashboot.apfs
else
python3 ../tools/apfs/mkapfs.py -z -o flashboot.os.apfs OS=flashboot.os | tail -1
[ -z "$CHECK" ] || ../tools/apfs/check-image.sh flashboot.os.apfs OS=flashboot.os
- cat flashboot.boot.apfs flashboot.os.apfs > flashboot.apfs
+ cat flashboot.boot.apfs flashboot.os.apfs $APP > flashboot.apfs
rm -f flashboot.os.apfs
fi
rm -rf flashboot flashboot.os flashboot.boot.apfs
@@ -336,7 +340,7 @@ fi
# --- the system image -----------------------------------------------------------------------------
if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&
{ [ ! -f system.apfs ] || [ obj/stage0 -nt system.apfs ] || [ $IOS/system.manifest -nt system.apfs ] ||
- [ $IOS/cryptex -nt system.apfs ] || [ -n "$SYSTEM" ]; }; then
+ [ $IOS/cryptex -nt system.apfs ] || [ $IOS/cryptex-app -nt system.apfs ] || [ -n "$SYSTEM" ]; }; then
# what ios165/select-system.py has chosen since ios165/rootfs was extracted is not in it, and would not be
# in the image either (so it was once with the audio plug-ins and the system's sounds: iOS found no
# speaker and played nothing, though our device was there and the kernel's side worked)
@@ -380,6 +384,9 @@ if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&
# shared cache (dyld looks there first) and a few libraries and resources outside the cache.
mkdir -p sysroot/System/Cryptexes
if [ -d $IOS/cryptex ]; then cp -al $IOS/cryptex sysroot/System/Cryptexes/OS; fi
+ # the App cryptex, the same way: Safari's daemons (eight launchd jobs run them from there), its settings and
+ # the bundles around it, which the root's links reach (System/Library/PreferenceBundles/MobileSafariSettings.bundle...)
+ if [ -d $IOS/cryptex-app ]; then cp -al $IOS/cryptex-app sysroot/System/Cryptexes/App; fi
D=sysroot/System/Cryptexes/OS/System/Library/Caches/com.apple.dyld
mkdir -p $D
for f in $CACHE/dyld_shared_cache_arm64*; do
diff --git a/userland/mkjobs.py b/userland/mkjobs.py
index 325b24c..fb6f265 100755
--- a/userland/mkjobs.py
+++ b/userland/mkjobs.py
@@ -49,6 +49,20 @@ if rest:
src_root, dest, labels = args[0], args[1], args[2:]
oslog = os.environ.get('OSLOG', '').split()
src = f'{src_root}/System/Library/LaunchDaemons'
+# A program in a cryptex -- on a device a volume of its own, grafted at /System/Cryptexes/NAME (eight of
+# Safari's daemons are in the App cryptex) -- is looked for where fetch.sh unpacked that cryptex, beside
+# the root filesystem; the images have it at the same place (build.sh).
+CRYPTEXES = {'/System/Cryptexes/OS/': 'cryptex', '/System/Cryptexes/App/': 'cryptex-app'}
+
+
+def present(program):
+ for prefix, unpacked in CRYPTEXES.items():
+ if program.startswith(prefix):
+ return os.path.isfile(os.path.join(os.path.dirname(os.path.abspath(src_root)), unpacked,
+ program[len(prefix):]))
+ return os.path.isfile(f'{src_root}{program}')
+
+
angels = f'{src_root}/System/Library/LaunchAngels'
os.makedirs(dest, exist_ok=True)
for label in labels:
@@ -63,7 +77,7 @@ for label in labels:
with open(f'{dest}/{label}.plist', 'wb') as f:
plistlib.dump(job, f, fmt=plistlib.FMT_BINARY)
program = job.get('Program') or job['ProgramArguments'][0]
- missing = '' if os.path.isfile(f'{src_root}{program}') else ' (NOT in the system image!)'
+ missing = '' if present(program) else ' (NOT in the system image!)'
print(f' job {label}: {program}{missing}')
if rest:
@@ -78,7 +92,7 @@ if rest:
with open(f'{directory}/{name}', 'rb') as f:
job = plistlib.load(f)
program = job.get('Program') or (job.get('ProgramArguments') or [''])[0]
- if not job.get('MachServices') or not os.path.isfile(f'{src_root}{program}'):
+ if not job.get('MachServices') or not present(program):
continue
for key in ('RunAtLoad', 'KeepAlive', 'StartInterval', 'StartCalendarInterval', 'WatchPaths',
'QueueDirectories', 'StartOnMount', 'LaunchEvents'):
diff --git a/userland/rc.jobs.flashcheck b/userland/rc.jobs.flashcheck
index 5821693..b080b80 100644
--- a/userland/rc.jobs.flashcheck
+++ b/userland/rc.jobs.flashcheck
@@ -4,14 +4,19 @@
# rec_sound=12 wifi=1, and no battery=1, which keeps the battery from the audio DSP off): what is mounted
# from where, the firmware on /fw, the battery as iOS has it, something to hear (a tone of three seconds,
# the lock sound, a click, a fanfare: ./xl sound), the network over the phone's Wi-Fi (an address by
-# DHCP, a name, a page over HTTPS) -- all of it with APFS alone in RAM. The results also go to a
-# file shown at the end, where the kernel's log keeps them.
+# DHCP, a name, a page over HTTPS), and Safari opening a page with its daemons from the App cryptex
+# (Apple's image as it is, the third container of flashboot.apfs, through the volume's own
+# /System/Cryptexes/App) -- all of it with APFS alone in RAM. The results also go to a file shown
+# at the end, where the kernel's log keeps them.
/sbin/mount
-/redwood/bin/vol_ls fs / /redwood /private/preboot /private/preboot/Cryptexes/OS
+/redwood/bin/vol_ls fs / /redwood /private/preboot /private/preboot/Cryptexes/OS /private/preboot/Cryptexes/App
+/redwood/bin/vol_ls ls /System/Cryptexes/App/usr/libexec
/redwood/bin/vol_ls ls /fw
/redwood/bin/net_test show dhcp en0 waitaddr en0 60 >> /private/var/results
/redwood/bin/net_test resolve example.com fetch https://example.com/ fetch https://www.apple.com/library/test/success.html >> /private/var/results
/redwood/bin/sound_test springboard wait 5 tone 3 wait 3 system 1100 wait 3 system 1104 wait 3 file /System/Library/Audio/UISounds/New/Fanfare.caf
+/redwood/bin/battery_test open https://example.com
+/redwood/bin/rwtouch wait 20
/redwood/bin/battery_test 1 >> /private/var/results
/redwood/bin/vol_ls sysctl kern.redwood_memory >> /private/var/results
/bin/ps -A
diff --git a/userland/src/init.c b/userland/src/init.c
index ea80ba4..f4940b2 100644
--- a/userland/src/init.c
+++ b/userland/src/init.c
@@ -129,9 +129,9 @@ synthetic_dir(const char *path)
* applications passed over), made before /private/var is covered by a memory
* filesystem.
* Then the volume becomes the root (sysctl kern.redwood_switch_root) and the boot volume
- * goes to its /private/preboot, the cryptex onto that volume's Cryptexes/OS: the iOS volume's
- * /System/Cryptexes/OS points to /private/preboot/Cryptexes/OS, and the kernel takes a shared
- * cache from there. This
+ * goes to its /private/preboot, the cryptexes onto that volume's Cryptexes/OS and App: the iOS
+ * volume's /System/Cryptexes/OS and App point to /private/preboot/Cryptexes/OS and App, and the
+ * kernel takes a shared cache from the first. This
* image goes along with it, to /private/preboot/redwood, and is bound back on /redwood, a
* synthetic directory of the volume. The boot-args stay those of a RAM disk (rd=md0),
* which is what launchd reads to leave out the boot tasks of a real device.
@@ -223,6 +223,16 @@ flash_root(const char *dev)
printf(" the OS cryptex: %s\n", strerror(errno));
check(0, "the OS cryptex (md0, its second container) on /private/preboot/Cryptexes/OS");
}
+ /*
+ * The third container, when there is one: Apple's App cryptex as it is, on Cryptexes/App, where the
+ * iOS volume's /System/Cryptexes/App points -- Safari's daemons, its settings, the bundles around it.
+ */
+ if (mount("apfs", "/private/preboot/Cryptexes/App", MNT_RDONLY, (void *)"/dev/md0,c=2") == 0) {
+ check(1, "the App cryptex (md0, its third container) on /private/preboot/Cryptexes/App");
+ } else if (errno != EINVAL) {
+ printf(" the App cryptex: %s\n", strerror(errno));
+ check(0, "the App cryptex (md0, its third container) on /private/preboot/Cryptexes/App");
+ }
check(bind_dir("/private/preboot/redwood", "/redwood"), "this image back on /redwood");
flash_root_mode = 1;
}