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 data volume of iOS on the phone's flash: HFS Plus in darwin, mounted on /private/var

shimoku74f0c70fd8f8e4
diff --git a/drivers/storage/hakios.ufs.c b/drivers/storage/hakios.ufs.c
index 7ebb021..44bb61b 100644
--- a/drivers/storage/hakios.ufs.c
+++ b/drivers/storage/hakios.ufs.c
@@ -28,6 +28,13 @@
* ever overwrites the other's. The data buffer, which only core 0's reads fill, is mapped cacheable
* and the part of it an answer covers is invalidated here before it is copied out.
*
+ * The data volume of iOS (HFS Plus, built into this kernel: bsd/hfs) is /dev/ufs_darwin_data: darwin from
+ * UFS_WRITE_FIRST to its end, a block device of its own that reads and writes, there with ufs_write=1 only. Its
+ * writes go the same way as /dev/rufs_darwin's -- through the loader, which flushes and reads every one back before it
+ * answers -- so a write that has come back is on the flash, which is what the file system's journal counts on. The
+ * one and the other are not used in the same run: once the data volume has been opened for writing, the packed store
+ * (var_store) is refused, and while a saver holds the store the data volume does not open for writing.
+ *
* ufs_check=1 reads, once at start, the partitions there are images of in the ROM's
* firmware-update/ (bluetooth, xbl, tz, ...) through the same path and prints an FNV-1a hash of
* as many bytes as each image has: tools/ufs-survey.py compares those with the images and with
@@ -208,6 +215,12 @@ static volatile bool ufs_write_busy; /* a write of ours has this kern
static bool ufs_write_out, ufs_write_was_native; /* the list is out for writing, and was in before */
static uint64_t ufs_writes, ufs_write_bytes, ufs_write_errors, ufs_write_refused;
+/* /dev/ufs_darwin_data: the minor number past every partition's, and the block size it is counted in */
+#define UFS_DATA_MINOR BLOCK_MAX_PARTS
+static uint32_t ufs_data_bsize = 4096;
+static bool ufs_data_used; /* opened for writing this run: the packed store is not */
+static LCK_MTX_DECLARE(ufs_data_mtx, &ufs_lck_grp); /* one write of the data volume at a time */
+
SYSCTL_DECL(_kern_hakios);
SYSCTL_QUAD(_kern_hakios, OID_AUTO, ufs_requests, CTLFLAG_RD | CTLFLAG_LOCKED, &ufs_requests,
"requests the phone's storage has answered through the loader");
@@ -264,7 +277,9 @@ ufs_sysctl_var_busy(__unused struct sysctl_oid *oidp, __unused void *arg1, __unu
return error;
}
lck_mtx_lock(&ufs_write_mtx);
- if (value == 1) {
+ if (value == 1 && ufs_data_used) {
+ error = EBUSY; /* the data volume is mounted from darwin this run: no packed store into it */
+ } else if (value == 1) {
if (ufs_var_busy_pid != 0 && ufs_var_busy_pid != me) {
proc_t held = proc_find(ufs_var_busy_pid);
@@ -656,11 +671,150 @@ ufs_write_restore(void)
lck_mtx_unlock(&ufs_write_mtx);
}
+/* ---- the data volume: darwin from UFS_WRITE_FIRST on ---- */
+
+static bool
+ufs_is_data(dev_t dev)
+{
+ return ufs_ready && ufs_write_part >= 0 && (uint32_t)minor(dev) == UFS_DATA_MINOR;
+}
+
+static uint64_t
+ufs_data_bytes(void)
+{
+ return ufs_part_bytes((uint32_t)ufs_write_part) - UFS_WRITE_FIRST * 4096;
+}
+
+/*
+ * Bytes [offset, offset + len) of the data volume, read into p or written from it. A write that is not whole blocks
+ * of 4096 (the device may be counted in 512-byte ones) is made whole here: the blocks around it read, patched,
+ * written.
+ */
+static int
+ufs_data_io(bool write, uint64_t offset, uint8_t *p, uint64_t len)
+{
+ uint64_t base = UFS_WRITE_FIRST * 4096, size = ufs_data_bytes();
+ uint8_t *bounce;
+ int error = 0;
+
+ if (offset > size || len > size - offset) {
+ return EINVAL;
+ }
+ if (!write) {
+ return ufs_read((uint32_t)ufs_write_part, base + offset, p, len);
+ }
+ lck_mtx_lock(&ufs_data_mtx);
+ if (offset % 4096 == 0 && len % 4096 == 0) {
+ error = ufs_write((uint32_t)ufs_write_part, base + offset, p, len);
+ lck_mtx_unlock(&ufs_data_mtx);
+ return error;
+ }
+ bounce = kalloc_data(ufs_buffer_size, Z_WAITOK);
+ if (bounce == NULL) {
+ lck_mtx_unlock(&ufs_data_mtx);
+ return ENOMEM;
+ }
+ while (len > 0 && error == 0) {
+ uint64_t first = offset & ~4095ULL, skip = offset - first;
+ uint64_t span = (skip + len + 4095) & ~4095ULL, take;
+
+ if (span > ufs_buffer_size) {
+ span = ufs_buffer_size;
+ }
+ take = MIN(span - skip, len);
+ error = ufs_read((uint32_t)ufs_write_part, base + first, bounce, span);
+ if (error == 0) {
+ memcpy(bounce + skip, p, (size_t)take);
+ error = ufs_write((uint32_t)ufs_write_part, base + first, bounce, span);
+ }
+ offset += take;
+ p += take;
+ len -= take;
+ }
+ kfree_data(bounce, ufs_buffer_size);
+ lck_mtx_unlock(&ufs_data_mtx);
+ return error;
+}
+
+static void
+ufs_data_strategy(buf_t bp)
+{
+ uint64_t offset = (uint64_t)buf_blkno(bp) * ufs_data_bsize, len = buf_count(bp), size = ufs_data_bytes();
+ caddr_t mapped;
+ int error;
+
+ if (offset >= size) {
+ buf_seterror(bp, offset == size ? 0 : EINVAL);
+ buf_setresid(bp, buf_count(bp));
+ buf_biodone(bp);
+ return;
+ }
+ len = MIN(len, size - offset);
+ /* HFS Plus's cluster I/O hands over pages with no kernel address yet: buf_map() gives them one */
+ if ((error = buf_map(bp, &mapped)) == 0) {
+ error = ufs_data_io(!(buf_flags(bp) & B_READ), offset, (uint8_t *)mapped, len);
+ buf_unmap(bp);
+ }
+ buf_setresid(bp, (uint32_t)(buf_count(bp) - (error ? 0 : len)));
+ if (error) {
+ buf_seterror(bp, error);
+ }
+ buf_biodone(bp);
+}
+
+static int
+ufs_data_ioctl(u_long cmd, caddr_t data)
+{
+ switch (cmd) {
+ case DKIOCGETBLOCKSIZE:
+ *(uint32_t *)data = ufs_data_bsize;
+ return 0;
+ case DKIOCSETBLOCKSIZE:
+ if (*(uint32_t *)data < 512 || *(uint32_t *)data > 4096 ||
+ (*(uint32_t *)data & (*(uint32_t *)data - 1)) != 0) {
+ return EINVAL;
+ }
+ ufs_data_bsize = *(uint32_t *)data;
+ return 0;
+ case DKIOCGETPHYSICALBLOCKSIZE:
+ *(uint32_t *)data = 4096;
+ return 0;
+ case DKIOCGETBLOCKCOUNT:
+ *(uint64_t *)data = ufs_data_bytes() / ufs_data_bsize;
+ return 0;
+ case DKIOCISWRITABLE:
+ *(uint32_t *)data = 1;
+ return 0;
+ case DKIOCGETMAXBLOCKCOUNTREAD:
+ case DKIOCGETMAXBLOCKCOUNTWRITE:
+ *(uint64_t *)data = ufs_buffer_size / ufs_data_bsize;
+ return 0;
+ case DKIOCSYNCHRONIZE:
+ case DKIOCSYNCHRONIZECACHE:
+ /* the loader flushes after every write and reads it back before it answers: nothing is left to flush */
+ return 0;
+ default:
+ return ENOTTY;
+ }
+}
+
/* ---- the devices ---- */
static int
ufs_open(dev_t dev, int flags, __unused int devtype, __unused proc_t p)
{
+ if (ufs_is_data(dev)) {
+ if (flags & FWRITE) {
+ lck_mtx_lock(&ufs_write_mtx);
+ if (ufs_var_busy_pid != 0) {
+ lck_mtx_unlock(&ufs_write_mtx);
+ return EBUSY; /* a saver is writing the packed store into darwin */
+ }
+ ufs_data_used = true;
+ lck_mtx_unlock(&ufs_write_mtx);
+ }
+ return 0;
+ }
if (!ufs_ready || (uint32_t)minor(dev) >= ufs_parts || (ufs_page->part[minor(dev)].flags & BLOCK_PART_UNIT)) {
return ENXIO;
}
@@ -675,6 +829,9 @@ static int
ufs_copen(dev_t dev, int flags, int devtype, proc_t p)
{
if ((flags & FWRITE) && ufs_write_part >= 0 && (uint32_t)minor(dev) == (uint32_t)ufs_write_part) {
+ if (ufs_data_used) {
+ return EBUSY; /* the data volume is in darwin this run */
+ }
flags &= ~FWRITE;
}
return ufs_open(dev, flags, devtype, p);
@@ -705,6 +862,10 @@ ufs_strategy(buf_t bp)
uint64_t offset, len, size;
int error;
+ if (ufs_is_data(buf_device(bp))) {
+ ufs_data_strategy(bp);
+ return;
+ }
if (!ufs_ready || part >= ufs_parts) {
buf_seterror(bp, ENXIO);
buf_biodone(bp);
@@ -740,6 +901,9 @@ ufs_ioctl(dev_t dev, u_long cmd, caddr_t data, __unused int fflag, __unused proc
{
uint32_t part = (uint32_t)minor(dev), native;
+ if (ufs_is_data(dev)) {
+ return ufs_data_ioctl(cmd, data);
+ }
if (!ufs_ready || part >= ufs_parts) {
return ENXIO;
}
@@ -782,6 +946,9 @@ ufs_psize(dev_t dev)
{
uint32_t part = (uint32_t)minor(dev);
+ if (ufs_is_data(dev)) {
+ return (int)ufs_data_bsize;
+ }
return ufs_ready && part < ufs_parts ? (int)ufs_bsize[part] : -1;
}
@@ -1604,6 +1771,11 @@ hakios_ufs_init(void)
printf("ufs: /dev/rufs_darwin takes writes, whole blocks from %llu on (%llu MB of it); "
"everything else of the storage is read only\n", UFS_WRITE_FIRST,
(UFS_DARWIN_BLOCKS - UFS_WRITE_FIRST) * 4096 >> 20);
+ if (devfs_make_node(makedev(ufs_bmajor, UFS_DATA_MINOR), DEVFS_BLOCK, UID_ROOT, GID_OPERATOR, 0640,
+ "ufs_darwin_data") != NULL) {
+ printf("ufs: /dev/ufs_darwin_data: the same %llu MB as a block device, for the data volume\n",
+ ufs_data_bytes() >> 20);
+ }
} else {
printf("ufs: nothing takes writes: darwin is not among the partitions\n");
}
diff --git a/tools/mkdatavol.py b/tools/mkdatavol.py
new file mode 100755
index 0000000..a34f293
--- /dev/null
+++ b/tools/mkdatavol.py
@@ -0,0 +1,111 @@
+#!/usr/bin/env python3
+"""The data volume of iOS for the phone: an empty HFS Plus volume in darwin past the iOS volume and the cryptexes,
+as an Android sparse image of the whole of darwin that writes that volume and nothing else.
+
+usage: mkdatavol.py OUT.simg [MKFS] (MKFS: mkfs.hfsplus, default the one in $PATH)
+
+ fastboot flash darwin OUT.simg
+
+The kernel gives that stretch of darwin out as /dev/ufs_darwin_data (drivers/storage/hakios.ufs.c, ufs_write=1) and
+init mounts it on /private/var (userland/src/init.c, var_hfs=ufs_darwin_data). Flashing this again empties it.
+
+The image says DONT_CARE for darwin's blocks before the volume -- the iOS volume and both cryptexes, which the
+bootloader therefore leaves as they are -- and, inside the volume, for every block the new volume does not use: what
+is in free space does not matter to HFS Plus, and twelve gigabytes of zeros need not go over USB. The blocks it uses
+(the volume headers, the allocation bitmap, the catalog and extents trees, the journal) are written whole, zeros
+included: which those are is read from the volume's own allocation bitmap.
+
+Case-sensitive (HFSX), as iOS's own data volume is, and journaled.
+"""
+import os
+import struct
+import subprocess
+import sys
+import tempfile
+
+BS = 4096
+DARWIN_BLOCKS = 5242875 # darwin, in blocks of 4096 (README, "The port's partition")
+DATA_FIRST = 2042368 # where the volume begins: drivers/storage/hakios.ufs.c, UFS_WRITE_FIRST
+MAGIC, RAW, FILL, DONT_CARE = 0xED26FF3A, 0xCAC1, 0xCAC2, 0xCAC3
+
+
+def chunk(kind, blocks, data_size):
+ return struct.pack('<HHII', kind, 0, blocks, 12 + data_size)
+
+
+def used_blocks(img):
+ """The volume's allocation blocks marked in use, from its header and bitmap; the block size must be BS."""
+ img.seek(1024)
+ vh = img.read(512)
+ sig, = struct.unpack('>H', vh[0:2])
+ if sig not in (0x482B, 0x4858): # H+, HX
+ sys.exit('mkdatavol.py: no HFS Plus volume header in what mkfs made')
+ block_size, total = struct.unpack('>II', vh[40:48])
+ if block_size != BS:
+ sys.exit(f'mkdatavol.py: the volume has blocks of {block_size}, not {BS}')
+ fork = vh[112:192] # allocationFile: HFSPlusForkData
+ extents = [struct.unpack('>II', fork[16 + 8 * i:24 + 8 * i]) for i in range(8)]
+ bitmap = bytearray()
+ for start, count in extents:
+ if count:
+ img.seek(start * BS)
+ bitmap += img.read(count * BS)
+ if len(bitmap) * 8 < total:
+ sys.exit('mkdatavol.py: the allocation file is in more pieces than its fork says')
+ return total, lambda b: bitmap[b >> 3] & (0x80 >> (b & 7))
+
+
+def main():
+ if len(sys.argv) not in (2, 3):
+ sys.exit(__doc__)
+ out = sys.argv[1]
+ mkfs = sys.argv[2] if len(sys.argv) > 2 else 'mkfs.hfsplus'
+ blocks = DARWIN_BLOCKS - DATA_FIRST
+ with tempfile.TemporaryDirectory() as tmp:
+ path = os.path.join(tmp, 'data.hfs')
+ with open(path, 'wb') as f:
+ f.truncate(blocks * BS)
+ # its root is /private/var: root's, 0755, as on an iPhone
+ subprocess.run([mkfs, '-s', '-J', '-b', str(BS), '-U', '0', '-G', '0', '-M', '755', '-v', 'Data', path],
+ check=True)
+ with open(path, 'rb') as img:
+ total, used = used_blocks(img)
+ if total != blocks:
+ sys.exit(f'mkdatavol.py: the volume has {total} blocks, darwin has {blocks} for it')
+ runs = [] # [kind, first, count], in order
+ for b in range(total):
+ kind = RAW if used(b) else DONT_CARE
+ if runs and runs[-1][0] == kind and runs[-1][1] + runs[-1][2] == b:
+ runs[-1][2] += 1
+ else:
+ runs.append([kind, b, 1])
+ chunks, written = [chunk(DONT_CARE, DATA_FIRST, 0)], 0
+ for kind, first, count in runs:
+ if kind == DONT_CARE:
+ chunks.append(chunk(DONT_CARE, count, 0))
+ continue
+ img.seek(first * BS)
+ data = img.read(count * BS)
+ # zeros in a used stretch go as FILL, which costs nothing to send; everything else as RAW
+ at = 0
+ while at < count:
+ zero = data[at * BS:(at + 1) * BS] == bytes(BS)
+ end = at
+ while end < count and (data[end * BS:(end + 1) * BS] == bytes(BS)) == zero:
+ end += 1
+ if zero:
+ chunks.append(chunk(FILL, end - at, 4) + bytes(4))
+ else:
+ chunks.append(chunk(RAW, end - at, (end - at) * BS) + data[at * BS:end * BS])
+ written += end - at
+ at = end
+ with open(out, 'wb') as f:
+ f.write(struct.pack('<IHHHHIIII', MAGIC, 1, 0, 28, 12, BS, DARWIN_BLOCKS, len(chunks), 0))
+ for c in chunks:
+ f.write(c)
+ print(f'{out}: {os.path.getsize(out)} bytes; the volume: {blocks} blocks ({blocks * BS >> 20} MB) from darwin '
+ f'block {DATA_FIRST}, {written} of them written, the rest of darwin left as it is')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/userland/src/init.c b/userland/src/init.c
index 6eadcec..755cace 100644
--- a/userland/src/init.c
+++ b/userland/src/init.c
@@ -2,7 +2,8 @@
* Process 1 of the Darwin bring-up, until it hands over to the real launchd.
*
* It lives in the small image mounted on /redwood (see stage0.c) and
- * - mounts a memory filesystem on /private/var and creates the directories
+ * - mounts the data volume on /private/var -- HFS Plus on the phone's flash
+ * (var_hfs=DEV), or else a memory filesystem -- and creates the directories
* a system expects there;
* - checks what the kernel can do for user space, one line per check;
* - runs the commands listed in etc/rc of its image: usually just
@@ -820,6 +821,86 @@ read_whole(const char *path, char *buf, size_t size)
return (ssize_t)got;
}
+/*
+ * The data volume on the phone's flash (boot-arg var_hfs=DEV, DEV a node of /dev: ufs_darwin_data, darwin past the
+ * iOS volume and the cryptexes): HFS Plus, which this kernel has built in, so that what iOS keeps in /private/var is
+ * there at the next boot and its size is the size of the space -- not a memory filesystem's quarter of the RAM.
+ * Journaled: a phone reset in the middle of a write finds the volume whole at the next mount. If it does not mount
+ * (no volume made there yet, or this kernel cannot write it), the memory filesystem as before.
+ */
+static int var_kept; /* /private/var is the volume on the flash: what is in it stays */
+
+static int
+mount_data_volume(void)
+{
+ const char *name = boot_arg("var_hfs");
+ static char dev[64];
+ /* struct hfs_mount_args (bsd/hfs/hfs_mount.h) */
+ struct {
+ const char *fspec;
+ uint32_t uid, gid;
+ uint16_t mask, pad;
+ uint32_t encoding;
+ int tz[2];
+ int flags, journal_tbuffer_size, journal_flags, journal_disable;
+ } args;
+
+ if (name == NULL || strlen(name) + 6 > sizeof(dev)) {
+ return 0;
+ }
+ strcpy(dev, "/dev/");
+ strcpy(dev + 5, name);
+ memset(&args, 0, sizeof(args));
+ args.fspec = dev;
+ if (mount("hfs", "/private/var", 0, &args) != 0) {
+ printf(" HFS Plus from %s: %s; a memory filesystem instead, empty at every boot\n", dev, strerror(errno));
+ return 0;
+ }
+ printf(" /private/var: HFS Plus from %s, kept between boots\n", dev);
+ return 1;
+}
+
+/* What is under PATH, gone; PATH itself stays (a kept volume's tmp, run and vm are each boot's own) */
+static void
+empty_tree(const char *path, int depth)
+{
+ char buf[4096], child[1100];
+ off_t position = 0;
+ ssize_t n;
+ struct stat st;
+ int fd = open(path, O_RDONLY | O_DIRECTORY, 0), passes = 0;
+
+ if (fd < 0 || depth > 32) {
+ if (fd >= 0) {
+ close(fd);
+ }
+ return;
+ }
+ /* what is removed moves the rest, so the directory is read again from its start until it is empty (or stuck) */
+ while (passes++ < 64 && (n = getdirentries(fd, buf, sizeof(buf), &position)) > 0) {
+ for (ssize_t at = 0; at < n; at += ((struct dirent *)(buf + at))->d_reclen) {
+ const char *name = ((struct dirent *)(buf + at))->d_name;
+ size_t lp = strlen(path), ln = strlen(name);
+
+ if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 || lp + ln + 2 > sizeof(child)) {
+ continue;
+ }
+ memcpy(child, path, lp);
+ child[lp] = '/';
+ memcpy(child + lp + 1, name, ln + 1);
+ if (lstat(child, &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR) {
+ empty_tree(child, depth + 1);
+ rmdir(child);
+ } else {
+ unlink(child);
+ }
+ }
+ position = 0;
+ lseek(fd, 0, 0);
+ }
+ close(fd);
+}
+
static void
mount_writable(void)
{
@@ -831,11 +912,22 @@ mount_writable(void)
struct stat st;
printf("\n-- writable filesystem --\n");
- ok = mount("ramfs", "/private/var", 0, NULL) == 0;
- if (!ok) {
- printf(" mount: %s\n", strerror(errno));
+ var_kept = mount_data_volume();
+ if (var_kept) {
+ static const char *const own[] = { "/private/var/tmp", "/private/var/run", "/private/var/vm" };
+
+ for (unsigned i = 0; i < sizeof(own) / sizeof(own[0]); i++) {
+ empty_tree(own[i], 0);
+ }
+ ok = 1;
+ check(ok, "mount the data volume on /private/var");
+ } else {
+ ok = mount("ramfs", "/private/var", 0, NULL) == 0;
+ if (!ok) {
+ printf(" mount: %s\n", strerror(errno));
+ }
+ check(ok, "mount a memory filesystem on /private/var");
}
- check(ok, "mount a memory filesystem on /private/var");
/* the directories of the data volume, as listed in the template that ships with iOS (sysimage/mkvarskel.py) */
read_whole(in_image("/etc/var.skel"), skeleton, sizeof(skeleton));
@@ -850,7 +942,8 @@ mount_writable(void)
if (count == 5 && f[0][0] == 'd') {
int mode = (int)parse_number(f[1], 8);
- ok = mkdir(f[4], mode) == 0 && chmod(f[4], mode) == 0 &&
+ /* on the kept volume most of them are there from the boots before */
+ ok = (mkdir(f[4], mode) == 0 || (var_kept && errno == 17 /* EEXIST */)) && chmod(f[4], mode) == 0 &&
chown(f[4], (int)parse_number(f[2], 10), (int)parse_number(f[3], 10)) == 0;
if (!ok) {
printf(" %s: %s\n", f[4], strerror(errno));
@@ -859,8 +952,11 @@ mount_writable(void)
} else if (count == 3 && f[0][0] == 'l') {
symlink(f[1], f[2]);
} else if (count == 6 && f[0][0] == 'f') {
- /* a file of ours to start with (build.sh: the var/ directory): mode, owner, group, path, source in the image */
- if (!copy_file(in_image(f[5]), f[4], (int)parse_number(f[1], 8), (int)parse_number(f[2], 10),
+ /* a file of ours to start with (build.sh: the var/ directory): mode, owner, group, path, source in the image;
+ * on the kept volume only if it is not there yet -- what iOS made of it since is what counts */
+ if (var_kept && lstat(f[4], &st) == 0) {
+ files++;
+ } else if (!copy_file(in_image(f[5]), f[4], (int)parse_number(f[1], 8), (int)parse_number(f[2], 10),
(int)parse_number(f[3], 10))) {
printf(" %s: %s\n", f[4], strerror(errno));
}
@@ -902,6 +998,15 @@ mount_writable(void)
* application from there, and from /private/var not at all -- everything a package put in
* /var/jb/Applications was "Unable to register", with nothing on the files even looked at.
*/
+ /* the prefix is grafted anew at every boot: on the kept volume, last boot's goes first */
+ if (var_kept && lstat(to, &st) == 0) {
+ if ((st.st_mode & S_IFMT) == S_IFDIR) {
+ empty_tree(to, 0);
+ rmdir(to);
+ } else {
+ unlink(to);
+ }
+ }
if (strcmp(jb, "/private/preboot/jb") == 0 && stat("/private/preboot/jb-rw", &st) == 0) {
if (mount("ramfs", "/private/preboot/jb-rw", 0, NULL) == 0 &&
symlink("/private/preboot/jb-rw/procursus", "/private/var/jb") == 0) {