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 iOS root volume on the flash, mounted read only at /System/Volumes/iOS in an ordinary session

vcvkk6c6ce369d093
diff --git a/userland/build.sh b/userland/build.sh
index 0949467..efbd2ce 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -124,6 +124,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then
$DYN -framework CoreFoundation -framework CoreGraphics -framework IOSurface -o obj/cg_test src/cg_test.c
$DYN -o obj/shm_test src/shm_test.c
$DYN -o obj/apfs_mmap_test src/apfs_mmap_test.c
+ $DYN -o obj/vol_ls src/vol_ls.c
$DYN -framework CoreFoundation -framework CoreGraphics -framework ImageIO -o obj/snap_test src/snap_test.c
$DYN -framework CoreFoundation -framework CoreGraphics -framework CoreText -o obj/font_test src/font_test.c
$DYN -fobjc-arc -framework Foundation -framework SystemConfiguration -o obj/net_test src/net_test.m
@@ -169,6 +170,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then
[ -f obj/cg_test ] && cp obj/cg_test root/bin/
[ -f obj/shm_test ] && cp obj/shm_test root/bin/
[ -f obj/apfs_mmap_test ] && cp obj/apfs_mmap_test root/bin/
+ [ -f obj/vol_ls ] && cp obj/vol_ls root/bin/
[ -f obj/snap_test ] && cp obj/snap_test root/bin/
[ -f obj/font_test ] && cp obj/font_test root/bin/
[ -f obj/net_test ] && cp obj/net_test root/bin/
@@ -282,6 +284,9 @@ if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&
sysroot/System/Library/LaunchAngels sysroot/System/Library/xpc/launchd.plist
ln -s /redwood/LaunchDaemons sysroot/System/Library/LaunchDaemons
mkdir -p sysroot/redwood sysroot/sbin
+ # where the kernel mounts the iOS root container off the phone's flash (boot-arg apfs_volume=):
+ # iOS has no /System/Volumes, and nothing can make a directory in this image once it is built
+ mkdir -p sysroot/System/Volumes/iOS
# the applications iOS installs on its first boot are system applications here (see select-system.py)
if [ -d sysroot/private/var/staged_system_apps ]; then
mv sysroot/private/var/staged_system_apps/* sysroot/Applications/
diff --git a/userland/rc.jobs.ios b/userland/rc.jobs.ios
new file mode 100644
index 0000000..54967c9
--- /dev/null
+++ b/userland/rc.jobs.ios
@@ -0,0 +1,16 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# The iOS root container on the phone's flash, which the kernel mounts read only on
+# /System/Volumes/iOS at boot (ufs=2 apfs_volume=ufs_darwin, with bootcpu=7): what user space
+# sees of it, in an ordinary session. iOS's own mount and df first, then ours (vol_ls: ls, cat,
+# hashes through read(2) and through a mapping), then every file of /usr/sbin both ways.
+# The host's hashes: SystemVersion.plist 47687daabd864929, usr/lib/dyld 602fba9751ad6aa7,
+# usr/sbin/mediaserverd 895037cd97f96801 (tools/apfs/apfs-tool census, apfs.py).
+/sbin/mount
+/bin/df
+/redwood/bin/vol_ls fs /System/Volumes/iOS
+/redwood/bin/vol_ls ls /System/Volumes/iOS
+/redwood/bin/vol_ls ls /System/Volumes/iOS/System/Library/CoreServices
+/redwood/bin/vol_ls cat /System/Volumes/iOS/System/Library/CoreServices/SystemVersion.plist
+/redwood/bin/vol_ls hash /System/Volumes/iOS/System/Library/CoreServices/SystemVersion.plist /System/Volumes/iOS/usr/lib/dyld /System/Volumes/iOS/usr/sbin/mediaserverd
+/redwood/bin/apfs_mmap_test mmap /System/Volumes/iOS/usr/sbin
+/redwood/bin/apfs_mmap_test read /System/Volumes/iOS/usr/sbin 65536
diff --git a/userland/src/apfs_mmap_test.c b/userland/src/apfs_mmap_test.c
index 1ab7d77..ff30dea 100644
--- a/userland/src/apfs_mmap_test.c
+++ b/userland/src/apfs_mmap_test.c
@@ -16,7 +16,9 @@
*
* usage: apfs_mmap_test mmap [PATH]
* apfs_mmap_test read [PATH] [CHUNK]
- * PATH defaults to /private/var/apfs-check/usr, CHUNK to 4096.
+ * PATH defaults to /private/var/apfs-check/usr, CHUNK to 4096. PATH may be anywhere on a
+ * mounted volume: the lines name files from that volume's mount point (statfs), which is
+ * what makes them the host's lines.
*/
#include <dirent.h>
@@ -28,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
+#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <time.h>
@@ -37,6 +40,7 @@
#define FNV_PRIME 0x100000001b3ULL
#define MOUNT "/private/var/apfs-check"
+static size_t mount_len = sizeof(MOUNT) - 1; /* the mount point's length in a path */
static const char *tag; /* what to print each line under */
static int chunk_size = 4096;
@@ -169,12 +173,12 @@ do_file(const char *path, off_t size)
int rc = use_mmap ? hash_mapped(path, size, &h) : hash_read(path, size, &h);
if (rc != 0) {
- printf("%s %s: cannot read it: %s\n", tag, path + sizeof(MOUNT) - 1,
+ printf("%s %s: cannot read it: %s\n", tag, path + mount_len,
strerror(errno));
total_errors++;
return;
}
- snprintf(line, sizeof(line), "%s\t%lld\t%016llx\n", path + sizeof(MOUNT) - 1,
+ snprintf(line, sizeof(line), "%s\t%lld\t%016llx\n", path + mount_len,
(long long)size, h);
printf("%s %s", tag, line);
total_sum += fnv1a((const unsigned char *)line, strlen(line), FNV_INIT);
@@ -190,7 +194,7 @@ walk(char *path, size_t len)
if (d == NULL) {
printf("%s %s: cannot open the directory: %s\n", tag,
- path + sizeof(MOUNT) - 1, strerror(errno));
+ path + mount_len, strerror(errno));
total_errors++;
return;
}
@@ -209,7 +213,7 @@ walk(char *path, size_t len)
memcpy(path + len + 1, de->d_name, n + 1);
/* lstat, not stat: a symbolic link is not followed and not read */
if (lstat(path, &st) != 0) {
- printf("%s %s: lstat failed: %s\n", tag, path + sizeof(MOUNT) - 1,
+ printf("%s %s: lstat failed: %s\n", tag, path + mount_len,
strerror(errno));
total_errors++;
} else if (S_ISDIR(st.st_mode)) {
@@ -242,11 +246,6 @@ main(int argc, char **argv)
chunk_size = 4096;
}
}
- if (strncmp(where, MOUNT, sizeof(MOUNT) - 1) != 0) {
- printf("%s a path under %s, please\n", tag, MOUNT);
- return 2;
- }
-
/* the kernel mounts the volume as soon as /private/var is there; wait for it */
for (tries = 0; tries < 120; tries++) {
struct stat st;
@@ -256,6 +255,16 @@ main(int argc, char **argv)
}
sleep(1);
}
+ {
+ struct statfs sf;
+
+ if (statfs(where, &sf) == 0 && strncmp(where, sf.f_mntonname,
+ strlen(sf.f_mntonname)) == 0) {
+ mount_len = strcmp(sf.f_mntonname, "/") == 0 ? 0 : strlen(sf.f_mntonname);
+ printf("%s %s is %s on %s\n", tag, sf.f_mntonname, sf.f_mntfromname,
+ sf.f_fstypename);
+ }
+ }
printf("%s %s, %s%s\n", tag, where, use_mmap ? "through a mapping" : "with read(2)",
use_mmap ? "" : " in pieces");
diff --git a/userland/src/vol_ls.c b/userland/src/vol_ls.c
new file mode 100644
index 0000000..e05ac18
--- /dev/null
+++ b/userland/src/vol_ls.c
@@ -0,0 +1,228 @@
+/*
+ * A look at a mounted volume from user space, on a system that has no ls of its own (the
+ * iOS root has df and ps in /bin and nothing else): what ls -l and cat would show, how
+ * the volume is mounted, and a hash of a file read both ways.
+ *
+ * usage: vol_ls fs PATH the file system PATH is on: type, device, mount point, flags
+ * vol_ls ls DIR... one line an entry, sorted: kind and mode, links, owner,
+ * group, size, name (and where a symbolic link points)
+ * vol_ls cat FILE [BYTES] the file's first BYTES (default 4096), as they are
+ * vol_ls hash FILE... FNV-1a of the whole file through read(2) and through a
+ * mapping, and its size: the hash tools/apfs prints on the
+ * build machine for the same file
+ */
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define FNV_INIT 0xcbf29ce484222325ULL
+#define FNV_PRIME 0x100000001b3ULL
+
+static int
+by_name(const void *a, const void *b)
+{
+ return strcmp(*(char *const *)a, *(char *const *)b);
+}
+
+static void
+mode_string(mode_t m, char *out)
+{
+ const char *rwx = "rwxrwxrwx";
+ int i;
+
+ out[0] = S_ISDIR(m) ? 'd' : S_ISLNK(m) ? 'l' : S_ISREG(m) ? '-' : S_ISCHR(m) ? 'c' :
+ S_ISBLK(m) ? 'b' : S_ISFIFO(m) ? 'p' : S_ISSOCK(m) ? 's' : '?';
+ for (i = 0; i < 9; i++) {
+ out[1 + i] = (m & (0400 >> i)) ? rwx[i] : '-';
+ }
+ if (m & S_ISUID) {
+ out[3] = (m & 0100) ? 's' : 'S';
+ }
+ if (m & S_ISGID) {
+ out[6] = (m & 0010) ? 's' : 'S';
+ }
+ if (m & S_ISVTX) {
+ out[9] = (m & 0001) ? 't' : 'T';
+ }
+ out[10] = '\0';
+}
+
+static int
+do_fs(const char *path)
+{
+ struct statfs sf;
+
+ if (statfs(path, &sf) != 0) {
+ printf("vol_ls: %s: statfs: %s\n", path, strerror(errno));
+ return 1;
+ }
+ printf("vol_ls: %s is on %s, a %s file system mounted on %s%s%s%s; %llu blocks of %u, "
+ "%llu files\n", path, sf.f_mntfromname, sf.f_fstypename, sf.f_mntonname,
+ (sf.f_flags & MNT_RDONLY) ? ", read only" : ", writable",
+ (sf.f_flags & MNT_NOSUID) ? ", nosuid" : "", (sf.f_flags & MNT_NODEV) ? ", nodev" : "",
+ (unsigned long long)sf.f_blocks, (unsigned)sf.f_bsize, (unsigned long long)sf.f_files);
+ return 0;
+}
+
+static int
+do_ls(const char *dir)
+{
+ DIR *d = opendir(dir);
+ struct dirent *de;
+ char **names = NULL;
+ size_t n = 0, cap = 0, i;
+ int bad = 0;
+
+ if (d == NULL) {
+ printf("vol_ls: %s: %s\n", dir, strerror(errno));
+ return 1;
+ }
+ while ((de = readdir(d)) != NULL) {
+ if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) {
+ continue;
+ }
+ if (n == cap) {
+ cap = cap ? cap * 2 : 64;
+ names = realloc(names, cap * sizeof(*names));
+ if (names == NULL) {
+ closedir(d);
+ return 1;
+ }
+ }
+ names[n++] = strdup(de->d_name);
+ }
+ closedir(d);
+ qsort(names, n, sizeof(*names), by_name);
+ printf("vol_ls: %s: %zu entries\n", dir, n);
+ for (i = 0; i < n; i++) {
+ char path[PATH_MAX], mode[11], target[PATH_MAX];
+ struct stat st;
+
+ snprintf(path, sizeof(path), "%s/%s", dir, names[i]);
+ if (lstat(path, &st) != 0) {
+ printf(" %s: %s\n", names[i], strerror(errno));
+ bad++;
+ free(names[i]);
+ continue;
+ }
+ mode_string(st.st_mode, mode);
+ if (S_ISLNK(st.st_mode)) {
+ ssize_t len = readlink(path, target, sizeof(target) - 1);
+
+ target[len > 0 ? len : 0] = '\0';
+ printf(" %s %3u %4u %4u %12lld %s -> %s\n", mode, (unsigned)st.st_nlink,
+ (unsigned)st.st_uid, (unsigned)st.st_gid, (long long)st.st_size, names[i],
+ target);
+ } else {
+ printf(" %s %3u %4u %4u %12lld %s\n", mode, (unsigned)st.st_nlink,
+ (unsigned)st.st_uid, (unsigned)st.st_gid, (long long)st.st_size, names[i]);
+ }
+ free(names[i]);
+ }
+ free(names);
+ return bad;
+}
+
+static int
+do_cat(const char *file, long want)
+{
+ char *buf;
+ ssize_t got;
+ int fd = open(file, O_RDONLY);
+
+ if (fd < 0) {
+ printf("vol_ls: %s: %s\n", file, strerror(errno));
+ return 1;
+ }
+ buf = malloc((size_t)want);
+ got = buf != NULL ? read(fd, buf, (size_t)want) : -1;
+ close(fd);
+ if (got < 0) {
+ printf("vol_ls: %s: read: %s\n", file, strerror(errno));
+ free(buf);
+ return 1;
+ }
+ printf("vol_ls: %s, the first %zd bytes:\n", file, got);
+ fwrite(buf, 1, (size_t)got, stdout);
+ if (got > 0 && buf[got - 1] != '\n') {
+ putchar('\n');
+ }
+ free(buf);
+ return 0;
+}
+
+static int
+do_hash(const char *file)
+{
+ static unsigned char buf[65536];
+ unsigned long long h_read = FNV_INIT, h_map = FNV_INIT;
+ struct stat st;
+ ssize_t got;
+ off_t i;
+ int fd = open(file, O_RDONLY);
+
+ if (fd < 0 || fstat(fd, &st) != 0) {
+ printf("vol_ls: %s: %s\n", file, strerror(errno));
+ if (fd >= 0) {
+ close(fd);
+ }
+ return 1;
+ }
+ while ((got = read(fd, buf, sizeof(buf))) > 0) {
+ for (i = 0; i < got; i++) {
+ h_read = (h_read ^ buf[i]) * FNV_PRIME;
+ }
+ }
+ if (st.st_size > 0) {
+ const unsigned char *m = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+
+ if (m == MAP_FAILED) {
+ printf("vol_ls: %s: mmap: %s\n", file, strerror(errno));
+ close(fd);
+ return 1;
+ }
+ for (i = 0; i < st.st_size; i++) {
+ h_map = (h_map ^ m[i]) * FNV_PRIME;
+ }
+ munmap((void *)m, (size_t)st.st_size);
+ }
+ close(fd);
+ printf("vol_ls: %s: %lld bytes, read %016llx, mapped %016llx%s\n", file,
+ (long long)st.st_size, h_read, h_map, h_read == h_map ? "" : " -- they differ");
+ return h_read == h_map ? 0 : 1;
+}
+
+int
+main(int argc, char **argv)
+{
+ int i, bad = 0;
+
+ if (argc < 3) {
+ printf("usage: vol_ls fs PATH | ls DIR... | cat FILE [BYTES] | hash FILE...\n");
+ return 2;
+ }
+ if (strcmp(argv[1], "fs") == 0) {
+ return do_fs(argv[2]);
+ }
+ if (strcmp(argv[1], "cat") == 0) {
+ return do_cat(argv[2], argc > 3 ? atol(argv[3]) : 4096);
+ }
+ for (i = 2; i < argc; i++) {
+ if (strcmp(argv[1], "ls") == 0) {
+ bad += do_ls(argv[i]);
+ } else if (strcmp(argv[1], "hash") == 0) {
+ bad += do_hash(argv[i]);
+ } else {
+ printf("vol_ls: no such thing as %s\n", argv[1]);
+ return 2;
+ }
+ }
+ return bad ? 1 : 0;
+}
diff --git a/xnu/files/bsd/dev/redwood_apfs_check.c b/xnu/files/bsd/dev/redwood_apfs_check.c
index 630d4eb..25f8d21 100644
--- a/xnu/files/bsd/dev/redwood_apfs_check.c
+++ b/xnu/files/bsd/dev/redwood_apfs_check.c
@@ -945,12 +945,54 @@ apfs_check_runner(void *arg, wait_result_t wr)
apfs_check_finished = 1;
}
+/*
+ * apfs_volume=DEV: a volume for user space in an ordinary boot. Mounted read only (and
+ * nosuid, nodev) on apfs_volume_at -- by default /System/Volumes/iOS, which the system image
+ * has for it (userland/build.sh) -- right here, after the root and devfs and before launchd,
+ * so that all of user space sees it from the start. apfs_volume=ufs_darwin is the iOS root
+ * container on the phone's flash (ufs=2 for the loader, bootcpu=7).
+ */
+static void
+apfs_volume_mount(void)
+{
+ char dev[48], at[256] = "/System/Volumes/iOS", path[64];
+ vfs_context_t ctx = vfs_context_kernel();
+ uint64_t t0, ns = 0;
+ vnode_t vp = NULLVP;
+ int error, tries;
+
+ if (!PE_parse_boot_arg_str("apfs_volume", dev, sizeof(dev)) || dev[0] == '\0') {
+ return;
+ }
+ (void)PE_parse_boot_arg_str("apfs_volume_at", at, sizeof(at));
+ snprintf(path, sizeof(path), "/dev/%s", dev);
+ /* a node devfs_make_node() has just made is not in /dev at once (see apfs_check_thread) */
+ for (tries = 0; tries < 100; tries++) {
+ if (vnode_lookup(path, 0, &vp, ctx) == 0) {
+ vnode_put(vp);
+ break;
+ }
+ delay_for_interval(50, NSEC_PER_MSEC);
+ }
+ t0 = mach_absolute_time();
+ error = kernel_mount("apfs", NULLVP, NULLVP, at, path, 0,
+ MNT_RDONLY | MNT_NOSUID | MNT_NODEV, KERNEL_MOUNT_NOAUTH, ctx);
+ absolutetime_to_nanoseconds(mach_absolute_time() - t0, &ns);
+ if (error) {
+ printf("apfs: mounting %s on %s failed: %d\n", path, at, error);
+ } else {
+ printf("apfs: %s mounted on %s, read only, in %llu ms\n", path, at, ns / 1000000);
+ }
+}
+
void
redwood_apfs_check_init(void)
{
uint32_t mode = 0;
thread_t th;
+ apfs_volume_mount();
+
if (!PE_parse_boot_argn("apfs_check", &mode, sizeof(mode)) || mode == 0) {
return;
}
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c
index e87f411..e47d95d 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c
+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c
@@ -781,6 +781,10 @@ apfs_mount(mount_t mp, vnode_t devvp, user_addr_t data, vfs_context_t ctx)
error = apfs_mount_common(mp, vp, ctx);
if (error == 0) {
APFS_MP(mp)->am_owns_devvp = true;
+ /* 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);
+ }
} else {
(void)VNOP_CLOSE(vp, FREAD, ctx);
}