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_check=7: the kernel's own write, on a block device
vcvkkff24a22ce40f
diff --git a/xnu/files/bsd/dev/redwood_apfs_check.c b/xnu/files/bsd/dev/redwood_apfs_check.cindex 3d99317..62a2e99 100644--- a/xnu/files/bsd/dev/redwood_apfs_check.c+++ b/xnu/files/bsd/dev/redwood_apfs_check.c@@ -37,6 +37,10 @@* big to hold is cut into parts that between them cover it* exactly once.* apfs_subtree=/usr walk only that part of the volume+ * apfs_check=7 the one mode that writes: replace bytes inside a file of the+ * container on apfs_dev where they lie, with the kernel's own+ * bsd/miscfs/apfs/apfs_write.c, and read them back out of a+ * container opened again from nothing (apfs_write_check below)** A line is the same one tools/apfs/apfs-tool's census and `verify.py clines` print:*@@ -83,6 +87,11 @@#include <pexpert/pexpert.h>#include <libkern/OSAtomic.h>#include <sys/redwood_apfs_check.h>+#include <miscfs/apfs/apfs_lib.h>+#include <miscfs/apfs/apfs_write.h>+#include <sys/disk.h>+#include <sys/buf.h>+#include <sys/fcntl.h>extern void loader_return(void); /* osfmk/arm64/loader_return.c */extern void apfs_stats_print(void); /* bsd/miscfs/apfs/apfs_vfsops.c */@@ -986,6 +995,327 @@ apfs_check_concurrent(void)apfs_stats_print();}+/* ------------------------------------------- apfs_check=7: the kernel's own write, on a device */++/*+ * The one place bsd/miscfs/apfs/apfs_write.c is run inside the kernel. It is not VFS: there is+ * no vnop_write and this milestone does not call for one. It is apfs_lib and apfs_write on a+ * real block device, which is the step between the tests on the build machine+ * (tools/apfs/write-tests.sh) and anything on the phone's flash.+ *+ * apfs_check=7+ * apfs_dev=md0 the container to write in (default rvblk0)+ * apfs_write_path=/redwood/x the file in it, by path from the volume's root+ * apfs_write_at=N where in the file (default 0)+ * apfs_write_len=N how many bytes (default 4096, at most APFS_WRITE_MAX)+ * apfs_write_byte=N what to fill them with (default 0xa5)+ *+ * The container is opened, written and closed; then opened again from nothing and the file read+ * back, so what is reported came off the device and not out of apfs_lib's block cache. The+ * device is opened for writing here and nowhere else: every mount of this driver leaves+ * dev.write NULL, and without it apfs_write_in_place refuses everything.+ *+ * Where this can reach, which is the point: a block device. The phone's flash is not one under+ * this kernel -- it is read through the loader's mailbox and, on the branch that writes to it at+ * all, through a character device of its own with its own bounds (bsd/dev/redwood_ufs.c) -- so+ * nothing here can touch it. In QEMU and on the phone alike the only block devices are RAM+ * disks. And it happens at all only when asked for twice over: apfs_check=7 and a path.+ */+#define APFS_WRITE_MAX (1u << 20)++struct wdev {+ vnode_t vp;+ uint32_t devblock;+};++static int+wdev_io(struct wdev *w, uint64_t off, void *buf, size_t len, int flag)+{+ char *p = buf;++ if ((off % w->devblock) != 0 || (len % w->devblock) != 0) {+ return EINVAL;+ }+ while (len > 0) {+ size_t n = len < (128u << 10) ? len : (128u << 10);+ buf_t bp = buf_alloc(w->vp);+ int error;++ if (bp == NULL) {+ return ENOMEM;+ }+ buf_reset(bp, flag);+ buf_setblkno(bp, (daddr64_t)(off / w->devblock));+ buf_setlblkno(bp, (daddr64_t)(off / w->devblock));+ buf_setcount(bp, (uint32_t)n);+ buf_setdataptr(bp, (uintptr_t)p);+ error = VNOP_STRATEGY(bp);+ if (error == 0) {+ error = (int)buf_biowait(bp);+ }+ if (error == 0 && (buf_resid(bp) != 0 || buf_count(bp) != n)) {+ error = EIO;+ }+ buf_free(bp);+ if (error) {+ return error;+ }+ p += n;+ off += n;+ len -= n;+ }+ return 0;+}++static int+wdev_read(void *ctx, uint64_t off, void *buf, size_t len)+{+ return wdev_io(ctx, off, buf, len, B_READ);+}++static int+wdev_write(void *ctx, uint64_t off, const void *buf, size_t len)+{+ return wdev_io(ctx, off, (void *)(uintptr_t)buf, len, B_WRITE);+}++/* The volume a path names, and the inode in it; the first volume with a root directory. */+static int+wdev_open_volume(struct apfs_nx *nx, struct apfs_vol *vol)+{+ uint32_t i;++ for (i = 0; i < APFS_NX_MAX_FILE_SYSTEMS; i++) {+ struct apfs_inode ino;++ if (nx->fs_oid[i] == 0 || apfs_vol_open(nx, i, vol) != 0) {+ continue;+ }+ if (apfs_inode_get(vol, APFS_ROOT_DIR_INO_NUM, &ino) == 0) {+ return 0;+ }+ }+ return ENOENT;+}++/* The FNV-1a of a file's contents as the census lines use it, read through apfs_lib. */+static int+wdev_hash(struct apfs_vol *vol, const struct apfs_inode *ino, uint8_t *buf, size_t buflen,+ uint64_t *out)+{+ struct apfs_cfile cf;+ uint64_t h = FNV_INIT, off = 0;++ bzero(&cf, sizeof(cf));+ while (off < ino->size) {+ size_t want = (size_t)(ino->size - off), got = 0, i;++ if (want > buflen) {+ want = buflen;+ }+ if (apfs_read_file(vol, ino, &cf, off, buf, want, &got) != 0 || got == 0) {+ apfs_cfile_fini(&cf);+ return EIO;+ }+ for (i = 0; i < got; i++) {+ h ^= buf[i];+ h *= FNV_PRIME;+ }+ off += got;+ }+ apfs_cfile_fini(&cf);+ *out = h;+ return 0;+}++static void+apfs_write_check(const char *devname)+{+ vfs_context_t ctx = vfs_context_kernel();+ struct apfs_nx nx;+ struct apfs_vol vol;+ struct apfs_inode ino;+ struct apfs_write_report rep;+ struct apfs_dev dev;+ struct wdev w = { NULLVP, 0 };+ char path[256] = "";+ uint8_t *data = NULL, *scratch = NULL, *was = NULL;+ uint64_t at = 0, before = 0, after = 0, want = 0, file_id = 0, was_len = 0;+ uint32_t len = 4096, byte = 0xa5;+ const char *why = NULL;+ bool opened = false, nx_open = false;+ int error, tries;++ if (!PE_parse_boot_arg_str("apfs_write_path", path, sizeof(path)) || path[0] != '/') {+ printf("apfs-write: apfs_write_path=/some/file says which file to write\n");+ return;+ }+ PE_parse_boot_argn("apfs_write_at", &at, sizeof(at));+ PE_parse_boot_argn("apfs_write_len", &len, sizeof(len));+ PE_parse_boot_argn("apfs_write_byte", &byte, sizeof(byte));+ if (len == 0 || len > APFS_WRITE_MAX) {+ printf("apfs-write: apfs_write_len=%u is not between 1 and %u\n", len, APFS_WRITE_MAX);+ return;+ }++ for (tries = 0; tries < 100; tries++) {+ if (vnode_lookup(devname, 0, &w.vp, ctx) == 0) {+ break;+ }+ delay_for_interval(20, NSEC_PER_MSEC);+ }+ if (w.vp == NULLVP) {+ printf("apfs-write: %s never showed up in /dev\n", devname);+ return;+ }+ /* for writing, which is what no mount of this driver ever asks for */+ error = VNOP_OPEN(w.vp, FREAD | FWRITE, ctx);+ if (error) {+ printf("apfs-write: %s will not open for writing: %d\n", devname, error);+ goto out;+ }+ opened = true;+ if (VNOP_IOCTL(w.vp, DKIOCGETBLOCKSIZE, (caddr_t)&w.devblock, 0, ctx) != 0 ||+ w.devblock == 0 || (4096 % w.devblock) != 0) {+ printf("apfs-write: %s has no usable block size\n", devname);+ goto out;+ }+ data = kalloc_data(len, Z_WAITOK);+ scratch = kalloc_data(READ_CHUNK, Z_WAITOK);+ if (data == NULL || scratch == NULL) {+ printf("apfs-write: out of memory\n");+ goto out;+ }+ memset(data, (int)(byte & 0xff), len);++ memset(&dev, 0, sizeof(dev));+ dev.ctx = &w;+ dev.read = wdev_read;+ dev.read_data = wdev_read;+ dev.write = wdev_write;+ error = apfs_nx_open(&nx, &dev);+ if (error) {+ printf("apfs-write: %s is not a container we can read: %d\n", devname, error);+ goto out;+ }+ nx_open = true;+ error = wdev_open_volume(&nx, &vol);+ if (error) {+ printf("apfs-write: %s has no volume with a root directory\n", devname);+ goto out;+ }+ if (apfs_write_volume_ok(&vol, &why) != 0) {+ printf("apfs-write: volume %s will not be written: %s\n", vol.name, why);+ goto out;+ }+ error = apfs_path_lookup(&vol, path, &file_id, NULL);+ if (error) {+ printf("apfs-write: %s: no such path in %s: %d\n", path, vol.name, error);+ goto out;+ }+ error = apfs_inode_get(&vol, file_id, &ino);+ if (error == 0) {+ error = wdev_hash(&vol, &ino, scratch, READ_CHUNK, &before);+ }+ if (error) {+ printf("apfs-write: %s cannot be read before the write: %d\n", path, error);+ goto out;+ }+ printf("apfs-write: %s on %s, inode %llu, %llu bytes, hash %016llx\n", path, vol.name,+ file_id, ino.size, before);+ /*+ * The file as it is now, kept so that what it should hold afterwards can be worked out+ * from it rather than from the result -- which would prove nothing.+ */+ if (ino.size <= APFS_WRITE_MAX) {+ struct apfs_cfile cf;+ size_t got = 0;++ bzero(&cf, sizeof(cf));+ was_len = ino.size;+ was = kalloc_data((vm_size_t)was_len, Z_WAITOK);+ if (was != NULL &&+ (apfs_read_file(&vol, &ino, &cf, 0, was, (size_t)was_len, &got) != 0 ||+ got != was_len)) {+ kfree_data(was, (vm_size_t)was_len);+ was = NULL;+ }+ apfs_cfile_fini(&cf);+ }+ if (was == NULL) {+ printf("apfs-write: %s is too big to keep a copy of: only the hashes are reported\n",+ path);+ }++ error = apfs_write_in_place(&vol, file_id, at, data, len, &rep);+ printf("apfs-write: %u bytes of 0x%02x at %llu: %s; %llu blocks from %llu, %llu of them "+ "patched whole, %llu writes\n", len, byte & 0xff, at,+ error == 0 ? "written" : (rep.why != NULL ? rep.why : "failed"), rep.blocks,+ rep.first_block, rep.partial, rep.calls);+ apfs_nx_close(&nx);+ nx_open = false;+ if (error) {+ goto out;+ }++ /*+ * Again from nothing: a container opened a second time shares no cache with the first,+ * so what comes back now is what is on the device.+ */+ error = apfs_nx_open(&nx, &dev);+ if (error == 0) {+ nx_open = true;+ error = wdev_open_volume(&nx, &vol);+ }+ if (error == 0) {+ error = apfs_path_lookup(&vol, path, &file_id, NULL);+ }+ if (error == 0) {+ error = apfs_inode_get(&vol, file_id, &ino);+ }+ if (error == 0) {+ error = wdev_hash(&vol, &ino, scratch, READ_CHUNK, &after);+ }+ if (error) {+ printf("apfs-write: the volume will not read back after the write: %d\n", error);+ goto out;+ }+ if (was != NULL) {+ uint64_t off;++ want = FNV_INIT;+ for (off = 0; off < was_len; off++) {+ uint8_t b = (off >= at && off < at + len) ? (uint8_t)(byte & 0xff) : was[off];++ want ^= b;+ want *= FNV_PRIME;+ }+ }+ printf("apfs-write: read back from a container opened again: %llu bytes, hash %016llx "+ "(was %016llx); the bytes written are %s\n", ino.size, after, before,+ was == NULL ? "not checked here" :+ (want == after ? "all there and nothing else changed" : "NOT what was asked for"));+out:+ if (nx_open) {+ apfs_nx_close(&nx);+ }+ if (data != NULL) {+ kfree_data(data, len);+ }+ if (scratch != NULL) {+ kfree_data(scratch, READ_CHUNK);+ }+ if (was != NULL) {+ kfree_data(was, (vm_size_t)was_len);+ }+ if (opened) {+ (void)VNOP_CLOSE(w.vp, FREAD | FWRITE, ctx);+ }+ if (w.vp != NULLVP) {+ vnode_put(w.vp);+ }+}+static voidapfs_check_thread(void *arg, __unused wait_result_t wr){@@ -1023,6 +1353,12 @@ apfs_check_thread(void *arg, __unused wait_result_t wr)}subtree[0] = '\0';+ /* the one mode that writes: no walk, no mount, no census -- see apfs_write_check() */+ if (mode == 7) {+ apfs_write_check(devname);+ return;+ }+cs.path = kalloc_data(PATH_CAP, Z_WAITOK | Z_ZERO);cs.buf = kalloc_data(READ_CHUNK, Z_WAITOK);if (cs.path == NULL || cs.buf == NULL) {