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

check-image.sh: every image the build makes checked by apfsck and read back by the kernel's own code

vcvkka55db8db139a
diff --git a/tools/apfs/check-image.sh b/tools/apfs/check-image.sh
new file mode 100755
index 0000000..d01520f
--- /dev/null
+++ b/tools/apfs/check-image.sh
@@ -0,0 +1,43 @@
+#!/bin/bash
+# Check an image made by mkapfs.py with two readers that are not it (tools/apfs/check-image.py):
+# apfsck -cw has to find the container clean, and the kernel's own apfs_lib (apfs-tool) has to
+# read every volume back as the directory it was made of. Builds what that takes, once:
+#
+# apfsck linux-apfs/apfsprogs (GPL-2.0, not in this repository) at the commit below, cloned
+# into xnu-deps/build/apfsprogs and built there
+# apfs-tool tools/apfs/build-tool.sh -> apfs-work/apfs-tool, when apfs_lib is newer than it
+# fnvsum tools/apfs/fnvsum.c -> apfs-work/fnvsum (check-image.py builds it)
+#
+# usage: tools/apfs/check-image.sh IMAGE [VOLUME=]DIR[,manifest=FILE] ... (the volumes as mkapfs.py had them)
+#
+# One container: the first one in IMAGE. Containers back to back (flashboot.apfs) are checked one by
+# one before they are put together (userland/build.sh, CHECK=1).
+set -eu
+HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+ROOT=$(cd "$HERE/../.." && pwd)
+APFSPROGS=$ROOT/xnu-deps/build/apfsprogs
+APFSPROGS_URL=https://github.com/linux-apfs/apfsprogs.git
+APFSPROGS_COMMIT=1bd1c1c2140d13d1b6d2e03379316572bee70aa3 # "mkapfs: set INODE_NO_RSRC_FORK on the directories"
+
+[ $# -ge 2 ] || { sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'; exit 2; }
+
+if [ ! -x "$APFSPROGS/apfsck/apfsck" ]; then
+ if [ ! -d "$APFSPROGS/.git" ]; then
+ mkdir -p "$(dirname "$APFSPROGS")"
+ git clone -q "$APFSPROGS_URL" "$APFSPROGS"
+ fi
+ git -C "$APFSPROGS" checkout -q "$APFSPROGS_COMMIT"
+ make -C "$APFSPROGS/apfsck" --silent --no-print-directory > /dev/null
+fi
+
+TOOL=$ROOT/apfs-work/apfs-tool
+stale=""
+[ -x "$TOOL" ] || stale=1
+for f in "$HERE/apfs-tool.c" "$HERE/build-tool.sh" \
+ "$ROOT"/xnu/files/bsd/miscfs/apfs/*.[ch] "$ROOT"/xnu/files/bsd/miscfs/apfs/libzbitmap/*.[ch]; do
+ [ -n "$stale" ] && break
+ [ "$f" -nt "$TOOL" ] && stale=1
+done
+[ -z "$stale" ] || "$HERE/build-tool.sh" "$TOOL" > /dev/null
+
+exec python3 "$HERE/check-image.py" "$@"
diff --git a/userland/build.sh b/userland/build.sh
index bc33d12..60e1248 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -48,6 +48,9 @@
# PREBUILT=1 do not compile anything: take our programs from ../prebuilt/userland (this is also
# what happens when the cross toolchain is not set up, see ../xnu-deps/setup.sh);
# the images are then made of them and of the iOS files alone (python3)
+# CHECK=1 check every image (every container) as it is made: apfsck has to find it clean and
+# the kernel's apfs_lib has to read every volume back as the directory it was made of
+# (../tools/apfs/check-image.sh; the system image takes a few minutes more)
set -e
cd "$(dirname "$(readlink -f "$0")")"
source ../xnu-env.sh
@@ -280,6 +283,7 @@ done
# the image: zlib where it saves room (a small file whole in its attribute, as Apple keeps them); fw/
# as it is, which the kernel's drivers use where it lies in memory (bsd/dev/redwood_image.c)
python3 ../tools/apfs/mkapfs.py -z --plain fw -o root.apfs redwood=root | tail -1
+[ -z "$CHECK" ] || ../tools/apfs/check-image.sh root.apfs redwood=root
# --- 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
@@ -316,10 +320,12 @@ if [ -n "$FLASHBOOT" ] && [ -f $CACHE/dyld_shared_cache_arm64 ]; then
ln "$f" $D/
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
if [ -n "$CRYPTEX_IMAGE" ]; then
cat flashboot.boot.apfs "$CRYPTEX_IMAGE" > 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
rm -f flashboot.os.apfs
fi
@@ -383,5 +389,6 @@ if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&
# zlib where it saves room, as Apple keeps the root (a small file whole in its attribute): 5.4 GB of
# files in 2.5 GB, what fits into the phone's RAM beside iOS
python3 ../tools/apfs/mkapfs.py -z -o system.apfs System=sysroot,manifest=$IOS/system.manifest | tail -1
+ [ -z "$CHECK" ] || ../tools/apfs/check-image.sh system.apfs System=sysroot,manifest=$IOS/system.manifest
rm -rf sysroot
fi