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
APFS in RAM in place of packfs: containers back to back, the cryptex where iOS grafts it
vcvkk44963a443387
diff --git a/userland/build.sh b/userland/build.shindex 6924605..8e304e9 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -275,6 +275,9 @@ for f in extensions.plist extensions.plist.sig; dodone[ -d root/lib/RedwoodAudio.driver ] && ln -s /redwood/lib/RedwoodAudio.driver root/hal/RedwoodAudio.driver../mkpackfs.py -o root.packfs root+# ... and the same tree as an APFS container (tools/apfs/mkapfs.py), what takes the place of packfs+# (stage0 mounts whichever of the two travels with the kernel: XL_RAMDISK=userland/root.apfs)+python3 ../tools/apfs/mkapfs.py -z -o root.apfs redwood=root | tail -1# --- the RAM part of a boot off the flash: FLASHBOOT=1 -> flashboot.packfs ------------------------------# With the iOS volume on the phone's flash as the root, all that has to come from RAM is what is not@@ -308,13 +311,32 @@ if [ -n "$FLASHBOOT" ] && [ -f $CACHE/dyld_shared_cache_arm64 ]; thendoneif [ -n "$PLAIN" ]; then pack="-b 4096 -a 16384"; elif [ -n "$ZSTD" ]; then pack="--zstd"; else pack="-z"; fi../mkpackfs.py $pack -o flashboot.packfs flashboot- rm -rf flashboot+ # ... and as APFS, flashboot.apfs: two containers back to back in the one resident image, the boot+ # volume -- all of the above but the cryptex, its Cryptexes/OS empty -- and the OS cryptex with the+ # shared cache, which init mounts on that empty directory once the volume on the flash is the root+ # (src/init.c, flash_root; the kernel's apfs takes "/dev/md0,c=1"). 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).+ rm -rf flashboot.boot+ cp -al flashboot flashboot.boot+ rm -rf flashboot.boot/Cryptexes/OS+ mkdir -p flashboot.boot/Cryptexes/OS+ python3 ../tools/apfs/mkapfs.py -z -o flashboot.boot.apfs Boot=flashboot.boot | tail -1+ 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/Cryptexes/OS | tail -1+ cat flashboot.boot.apfs flashboot.os.apfs > flashboot.apfs+ rm -f flashboot.os.apfs+ fi+ rm -rf flashboot.boot flashboot.boot.apfs flashboot+ echo "flashboot.apfs: $(( $(stat -c %s flashboot.apfs) / 1048576 )) MB"fi# --- the system image -----------------------------------------------------------------------------if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&{ [ ! -f system.packfs ] || [ obj/stage0 -nt system.packfs ] || [ $IOS/system.manifest -nt system.packfs ] ||- [ $IOS/cryptex -nt system.packfs ] || [ -n "$SYSTEM" ]; }; then+ [ $IOS/cryptex -nt system.packfs ] || [ -n "$SYSTEM" ] || { [ -n "$APFS" ] && [ ! -f system.apfs ]; }; }; 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)@@ -370,5 +392,9 @@ if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&# PLAIN=1 builds the image uncompressedif [ -n "$PLAIN" ]; then pack="-b 4096 -a 16384"; elif [ -n "$ZSTD" ]; then pack="--zstd"; else pack="-z"; fi../mkpackfs.py $pack -m $IOS/system.manifest -o system.packfs sysroot+ # APFS=1: the same root as an APFS container too, system.apfs (zlib where it saves room: 1.5 GB)+ if [ -n "$APFS" ]; then+ python3 ../tools/apfs/mkapfs.py -z -o system.apfs System=sysroot,manifest=$IOS/system.manifest | tail -1+ firm -rf sysrootfidiff --git a/userland/src/init.c b/userland/src/init.cindex d90f756..21a6139 100644--- a/userland/src/init.c+++ b/userland/src/init.c@@ -206,6 +206,22 @@ flash_root(const char *dev)return;}chdir("/");+ /*+ * When the RAM part of the boot is APFS (flashboot.apfs), the OS cryptex is not inside it+ * but its own container right after it in md0: mounted where iOS grafts it, the empty+ * Cryptexes/OS of the boot volume -- now /private/preboot/Cryptexes/OS -- where the+ * kernel takes a shared cache from (bsd/vm/vm_unix.c). No second container (the packfs+ * image, with the cryptex inside): EINVAL, nothing to do.+ */+#ifndef EINVAL+#define EINVAL 22+#endif+ if (mount("apfs", "/private/preboot/Cryptexes/OS", MNT_RDONLY, (void *)"/dev/md0,c=1") == 0) {+ check(1, "the OS cryptex (md0, its second container) on /private/preboot/Cryptexes/OS");+ } else if (errno != EINVAL) {+ printf(" the OS cryptex: %s\n", strerror(errno));+ check(0, "the OS cryptex (md0, its second container) on /private/preboot/Cryptexes/OS");+ }check(bind_dir("/private/preboot/redwood", "/redwood"), "this image back on /redwood");flash_root_mode = 1;}diff --git a/userland/src/stage0.c b/userland/src/stage0.cindex c4bf4f8..f93c073 100644--- a/userland/src/stage0.c+++ b/userland/src/stage0.c@@ -5,7 +5,9 @@* and a half to upload to the phone, so nothing that changes often lives in* it. All of that - the real init, its tests, the list of launchd jobs - is* in the small image that travels with the kernel (the second RAM disk,- * /dev/md1). This program mounts it on /redwood and hands over.+ * /dev/md1). This program mounts it on /redwood and hands over. The image+ * is an APFS container (tools/apfs/mkapfs.py) or, until packfs is gone, a+ * packfs one: APFS is tried first.*/#include "mini.h"@@ -28,7 +30,8 @@ main(int argc, char **argv, char **envp)close(fd);}- if (mount("packfs", "/redwood", MNT_RDONLY, &args) != 0) {+ if (mount("apfs", "/redwood", MNT_RDONLY, (void *)"/dev/md1") != 0 &&+ mount("packfs", "/redwood", MNT_RDONLY, &args) != 0) {printf("stage0: cannot mount /dev/md1 on /redwood: %s\n", strerror(errno));} else {execve(init_argv[0], init_argv, envp);diff --git a/xnu/files/bsd/miscfs/apfs/apfs.h b/xnu/files/bsd/miscfs/apfs/apfs.hindex c43ca64..af662d1 100644--- a/xnu/files/bsd/miscfs/apfs/apfs.h+++ b/xnu/files/bsd/miscfs/apfs/apfs.h@@ -66,6 +66,9 @@ struct apfs_mount {struct apfs_vol am_vol;uint32_t am_devblock;bool am_owns_devvp; /* we opened it, so we close it */+ bool am_shares_devvp; /* another container of a device one of ours has open: not opened again */+ dev_t am_rdev; /* the device, for those other containers to find (apfs_mounts) */+ struct apfs_mount *am_next; /* in apfs_mounts */struct apfs_node *am_hash[APFS_HASH_BUCKETS];uint64_t am_root_id;/*diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.cindex 1fef917..e37e08f 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c@@ -40,6 +40,7 @@ static LCK_GRP_DECLARE(apfs_lck_grp, "apfs");struct apfs_devctx {vnode_t dc_devvp;uint32_t dc_devblock;+ uint64_t dc_base; /* where on the device the container starts (bytes) */};static int@@ -48,6 +49,7 @@ apfs_dev_read(void *ctx, uint64_t off, void *buf, size_t len)struct apfs_devctx *dc = ctx;char *out = buf;+ off += dc->dc_base;if ((off % dc->dc_devblock) != 0) {return EINVAL;}@@ -94,6 +96,7 @@ apfs_dev_read_data(void *ctx, uint64_t off, void *buf, size_t len)struct apfs_devctx *dc = ctx;char *out = buf;+ off += dc->dc_base;if ((off % dc->dc_devblock) != 0 || (len % dc->dc_devblock) != 0) {return EINVAL;}@@ -137,6 +140,45 @@ apfs_dev_read_data(void *ctx, uint64_t off, void *buf, size_t len)* volume is mounted and cleared when it goes away.*/static struct apfs_mount *apfs_the_mount;+static LCK_MTX_DECLARE(apfs_pool_lock, &apfs_lck_grp); /* the pool's threads, and apfs_mounts */++/*+ * Every APFS mount, for a mount of another container of the same device to find that the+ * device is open already (apfs_mount): the root's RAM disk holds the boot volume and the OS+ * cryptex, each its own container. Under apfs_pool_lock.+ */+static struct apfs_mount *apfs_mounts;++static bool+apfs_device_held(dev_t rdev)+{+ struct apfs_mount *m;+ bool held = false;++ lck_mtx_lock(&apfs_pool_lock);+ for (m = apfs_mounts; m != NULL && !held; m = m->am_next) {+ held = (m->am_rdev == rdev);+ }+ lck_mtx_unlock(&apfs_pool_lock);+ return held;+}++/*+ * Which volume kern.apfs_stats is about: the root's, when the root is APFS (the iOS volume on+ * the flash, once init has switched to it); else the one mounted last.+ */+extern vnode_t rootvnode;++static struct apfs_mount *+apfs_stats_mount(void)+{+ mount_t mp = rootvnode != NULLVP ? vnode_mount(rootvnode) : NULL;++ if (mp != NULL && strcmp(vfs_statfs(mp)->f_fstypename, "apfs") == 0 && vfs_fsprivate(mp) != NULL) {+ return vfs_fsprivate(mp);+ }+ return apfs_the_mount;+}static uint64_tapfs_ticks_ms(uint64_t ticks)@@ -234,7 +276,7 @@ static intsysctl_apfs_stats SYSCTL_HANDLER_ARGS{#pragma unused(oidp, arg1, arg2)- struct apfs_mount *am = apfs_the_mount;+ struct apfs_mount *am = apfs_stats_mount();size_t cap = 16384;char *text = kalloc_data(cap, Z_WAITOK | Z_ZERO);int error;@@ -342,7 +384,7 @@ sysctl_apfs_stats SYSCTL_HANDLER_ARGSvoidapfs_stats_print(void){- struct apfs_mount *am = apfs_the_mount;+ struct apfs_mount *am = apfs_stats_mount();struct apfs_nx *nx;if (am == NULL) {@@ -790,7 +832,6 @@ struct apfs_pjob {uint32_t pj_active; /* helpers inside the job now; under apfs_pool_lock */};-static LCK_MTX_DECLARE(apfs_pool_lock, &apfs_lck_grp);static struct apfs_pjob *apfs_pool_jobs; /* oldest first */static uint32_t apfs_pool_threads;static uint32_t apfs_par_max; /* threads on one request, the caller's included */@@ -940,15 +981,22 @@ apfs_free_mount(struct apfs_mount *am)kfree_type(struct apfs_mount, am);}+/*+ * Which container of the device and which volume of it: containers lie back to back, each+ * saying in its superblock how big it is (nx_block_count blocks of nx_block_size), so the+ * N-th starts where the one before it ends -- the RAM disks of this port hold several (the+ * boot volume, a cryptex) and so may the phone's darwin partition. volume < 0: the first+ * that has a root directory.+ */static int-apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx)+apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx, uint32_t container, int volume){struct apfs_mount *am;struct apfs_devctx *dc;struct apfs_dev dev;struct vfsstatfs *sp;uint32_t devblock = 0;- uint64_t devblocks = 0;+ uint64_t devblocks = 0, nx_size = 0;int error;if (VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&devblock, 0, ctx) != 0 ||@@ -982,10 +1030,54 @@ apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx)}dc->dc_devvp = devvp;dc->dc_devblock = devblock;+ {+ uint8_t *sb = kalloc_data(APFS_META_CHUNK, Z_WAITOK);+ uint64_t devsize = devblocks * (uint64_t)devblock;+ uint32_t i;++ error = (sb == NULL) ? ENOMEM : 0;+ for (i = 0; error == 0; i++) {+ uint32_t bs;+ uint64_t bc;++ error = apfs_dev_read(dc, 0, sb, APFS_META_CHUNK);+ if (error == 0 && memcmp(sb + 32, "NXSB", 4) != 0) {+ error = EINVAL; /* (no container here: as the root, the next filesystem's turn) */+ }+ if (error) {+ break;+ }+ bs = (uint32_t)sb[36] | (uint32_t)sb[37] << 8 | (uint32_t)sb[38] << 16 | (uint32_t)sb[39] << 24;+ bc = 0;+ for (int b = 7; b >= 0; b--) {+ bc = (bc << 8) | sb[40 + b];+ }+ nx_size = bc * bs;+ if (bs < 4096 || bc == 0 || (nx_size % devblock) != 0 ||+ (devsize != 0 && dc->dc_base + nx_size > devsize)) {+ printf("apfs: container %u: %llu blocks of %u do not fit the device\n", i,+ (unsigned long long)bc, bs);+ error = EINVAL;+ break;+ }+ if (i == container) {+ break;+ }+ dc->dc_base += nx_size;+ }+ if (sb != NULL) {+ kfree_data(sb, APFS_META_CHUNK);+ }+ if (error) {+ kfree_type(struct apfs_devctx, dc);+ apfs_free_mount(am);+ return error;+ }+ }bzero(&dev, sizeof(dev));dev.ctx = dc;- dev.size = devblocks * (uint64_t)devblock;+ dev.size = nx_size;dev.read = apfs_dev_read;dev.read_data = apfs_dev_read_data;@@ -1052,7 +1144,7 @@ apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx)for (i = 0; i < APFS_NX_MAX_FILE_SYSTEMS && !got; i++) {struct apfs_inode ino;- if (am->am_nx.fs_oid[i] == 0) {+ if (am->am_nx.fs_oid[i] == 0 || (volume >= 0 && i != (uint32_t)volume)) {continue;}if (apfs_vol_open(&am->am_nx, i, &am->am_vol) != 0) {@@ -1085,23 +1177,38 @@ apfs_mount_common(mount_t mp, vnode_t devvp, vfs_context_t ctx)sp->f_bused = am->am_vol.alloc_count;sp->f_files = am->am_vol.num_files + am->am_vol.num_dirs;sp->f_ffree = 0;+ /* (several volumes of one device are told apart by which container and volume) */sp->f_fsid.val[0] = (int32_t)vnode_specrdev(devvp);- sp->f_fsid.val[1] = vfs_typenum(mp);+ sp->f_fsid.val[1] = (int32_t)((vfs_typenum(mp) & 0xff) | (container << 8) | (am->am_vol.index << 20));strlcpy(sp->f_fstypename, "apfs", sizeof(sp->f_fstypename));apfs_the_mount = am;- printf("apfs: volume \"%s\": %llu files, %llu directories, %llu blocks of %u used\n",- am->am_vol.name, (unsigned long long)am->am_vol.num_files,+ am->am_rdev = vnode_specrdev(devvp);+ lck_mtx_lock(&apfs_pool_lock);+ am->am_next = apfs_mounts;+ apfs_mounts = am;+ lck_mtx_unlock(&apfs_pool_lock);+ printf("apfs: volume %u \"%s\" of container %u (at %llu MB): %llu files, %llu directories, %llu blocks of %u used\n",+ am->am_vol.index, am->am_vol.name, container, (unsigned long long)(dc->dc_base >> 20), (unsigned long long)am->am_vol.num_files,(unsigned long long)am->am_vol.num_dirs,(unsigned long long)am->am_vol.alloc_count, am->am_nx.block_size);return 0;}-/* As the root filesystem: EINVAL lets vfs_mountroot() try the next quietly. */+/*+ * As the root filesystem: EINVAL lets vfs_mountroot() try the next quietly. Boot-args+ * apfs_root_c=N and apfs_root_v=M say which container of the device and which volume of it+ * (default: the first container, its first volume with a root directory).+ */intapfs_mountroot(mount_t mp, vnode_t devvp, vfs_context_t ctx){- return apfs_mount_common(mp, devvp, ctx);+ uint32_t c = 0;+ int v = -1;++ (void)PE_parse_boot_argn("apfs_root_c", &c, sizeof(c));+ (void)PE_parse_boot_argn("apfs_root_v", &v, sizeof(v));+ return apfs_mount_common(mp, devvp, ctx, c, v);}/*@@ -1116,6 +1223,8 @@ apfs_mount(mount_t mp, vnode_t devvp, user_addr_t data, vfs_context_t ctx)char *path;vnode_t vp = NULLVP;size_t len = 0;+ uint32_t container = 0;+ int volume = -1;int error;if (vfs_isupdate(mp)) {@@ -1124,7 +1233,7 @@ apfs_mount(mount_t mp, vnode_t devvp, user_addr_t data, vfs_context_t ctx)}if (devvp != NULLVP) {/* mounted as the root: VFS found the device and owns the vnode */- return apfs_mount_common(mp, devvp, ctx);+ return apfs_mountroot(mp, devvp, ctx);}if (data == USER_ADDR_NULL) {return EINVAL;@@ -1140,6 +1249,24 @@ apfs_mount(mount_t mp, vnode_t devvp, user_addr_t data, vfs_context_t ctx)} else {error = copyinstr(data, path, MAXPATHLEN, &len);}+ /* "DEVICE[,c=N][,v=M]": the container (from 0) and the volume (by index) */+ if (error == 0) {+ char *opt = strchr(path, ',');++ while (opt != NULL) {+ char *next = strchr(opt + 1, ',');++ *opt = '\0';+ if (strncmp(opt + 1, "c=", 2) == 0) {+ container = (uint32_t)strtoul(opt + 3, NULL, 10);+ } else if (strncmp(opt + 1, "v=", 2) == 0) {+ volume = (int)strtoul(opt + 3, NULL, 10);+ } else {+ error = EINVAL;+ }+ opt = next;+ }+ }if (error == 0) {error = vnode_lookup(path, 0, &vp, ctx);}@@ -1147,20 +1274,40 @@ apfs_mount(mount_t mp, vnode_t devvp, user_addr_t data, vfs_context_t ctx)error = ENOTBLK;}if (error == 0) {+ bool shared = false;+error = VNOP_OPEN(vp, FREAD, ctx);+ if (error == EBUSY && apfs_device_held(vnode_specrdev(vp))) {+ /*+ * Mounted already -- by us, another container of it (the root's RAM disk and the+ * cryptex after the boot volume): read through it as it is open, not opened+ * again. Anything else mounted on a device stays EBUSY.+ */+ shared = true;+ error = 0;+ }if (error == 0) {/** The iocount from the lookup is kept for as long as the volume is* mounted; apfs_unmount() closes and drops it.*/- error = apfs_mount_common(mp, vp, ctx);+ error = apfs_mount_common(mp, vp, ctx, container, volume);if (error == 0) {APFS_MP(mp)->am_owns_devvp = true;+ APFS_MP(mp)->am_shares_devvp = shared;/* what mount(8) and df show as where it came from */- if (vfs_statfs(mp)->f_mntfromname[0] == '\0') {- strlcpy(vfs_statfs(mp)->f_mntfromname, path, MAXPATHLEN);+ if (vfs_statfs(mp)->f_mntfromname[0] == '\0' || container != 0 || volume >= 0) {+ char opts[40] = "";++ if (container != 0) {+ snprintf(opts, sizeof(opts), ",c=%u", container);+ }+ if (volume >= 0) {+ snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts), ",v=%d", volume);+ }+ snprintf(vfs_statfs(mp)->f_mntfromname, MAXPATHLEN, "%s%s", path, opts);}- } else {+ } else if (!shared) {(void)VNOP_CLOSE(vp, FREAD, ctx);}}@@ -1186,9 +1333,19 @@ apfs_unmount(mount_t mp, int mntflags, vfs_context_t ctx)if (apfs_the_mount == am) {apfs_the_mount = NULL;}+ lck_mtx_lock(&apfs_pool_lock);+ for (struct apfs_mount **pp = &apfs_mounts; *pp != NULL; pp = &(*pp)->am_next) {+ if (*pp == am) {+ *pp = am->am_next;+ break;+ }+ }+ lck_mtx_unlock(&apfs_pool_lock);vfs_setfsprivate(mp, NULL);if (am->am_owns_devvp && am->am_devvp != NULLVP) {- (void)VNOP_CLOSE(am->am_devvp, FREAD, ctx);+ if (!am->am_shares_devvp) {+ (void)VNOP_CLOSE(am->am_devvp, FREAD, ctx);+ }vnode_put(am->am_devvp);}apfs_free_mount(am);diff --git a/xnu/redwood.patch b/xnu/redwood.patchindex c5a993a..356d4c4 100644--- a/xnu/redwood.patch+++ b/xnu/redwood.patch@@ -1294,6 +1294,31 @@ index 5b51e9c46a8d..6872b94631c9 100644context.vc_thread = current_thread();context.vc_ucred = cred;+diff --git a/bsd/vm/vm_unix.c b/bsd/vm/vm_unix.c+index 60c8b9eedd50..b0029de5ef21 100644+--- a/bsd/vm/vm_unix.c++++ b/bsd/vm/vm_unix.c+@@ -2538,7 +2538,19 @@ after_root_check:+ #define PREBOOT_CRYPTEX_PATH "/private/preboot/Cryptexes"+ #endif+ error = vnode_lookup(PREBOOT_CRYPTEX_PATH, 0, &preboot_vp, vfs_context_current());+- if (error || srfmp->vp->v_mount != preboot_vp->v_mount) {++ /*++ * (redwood) Or a volume of its own mounted on a directory right inside it: iOS++ * grafts each cryptex image into the Preboot volume (graftdmg), so its files are++ * that volume's; this port mounts the image there as it is (bsd/miscfs/apfs), a++ * mount of its own whose covered directory is Preboot's Cryptexes/<name>.++ */++ if (error == 0 && srfmp->vp->v_mount != preboot_vp->v_mount &&++ srfmp->vp->v_mount != NULL && srfmp->vp->v_mount->mnt_vnodecovered != NULLVP &&++ srfmp->vp->v_mount->mnt_vnodecovered->v_mount == preboot_vp->v_mount &&++ srfmp->vp->v_mount->mnt_vnodecovered->v_parent == preboot_vp) {++ (void)vnode_put(preboot_vp);++ preboot_vp = NULL;++ } else if (error || srfmp->vp->v_mount != preboot_vp->v_mount) {+ SHARED_REGION_TRACE_ERROR(+ ("shared_region: %p [%d(%s)] map(%p:'%s'): "+ "not on process' root volume nor preboot volume\n",diff --git a/config/MASTER b/config/MASTERindex 07147cb3188b..671fb11422b1 100644--- a/config/MASTER