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 survives a reboot: /private/var kept in the flash
vcvkkf087ab734936
diff --git a/userland/build.sh b/userland/build.shindex 48b6213..bf18382 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -95,7 +95,7 @@ else$CC $LD -o hello src/hello.c$CC -Ilibmini -c libmini/mini.c -o obj/mini.o$CC -c libmini/start.S -o obj/start.o- for prog in init child stage0; do+ for prog in init child stage0 var_store; do$CC -Ilibmini $LD -o obj/$prog.new src/$prog.c obj/mini.o obj/start.o# keep the old file (and its time stamp) when nothing changed: system.apfs depends on stage0cmp -s obj/$prog.new obj/$prog && rm obj/$prog.new || mv obj/$prog.new obj/$prog@@ -107,6 +107,7 @@ rm -rf rootmkdir -p root/sbin root/bin root/etc root/share root/LaunchDaemonscp obj/init root/sbin/initcp obj/child root/bin/child+[ -f obj/var_store ] && cp obj/var_store root/bin/ # the data volume into the flash and back (src/var_store.c)echo "Darwin on redwood: image built $(date '+%Y-%m-%d %H:%M')" > root/etc/motd# 300000 bytes, byte i = (i*31 + (i>>8)) & 0xff; init verifies it through read() and mmap()python3 -c "import sys; sys.stdout.buffer.write(bytes((i*31 + (i>>8)) & 0xff for i in range(300000)))" > root/share/pattern.bindiff --git a/userland/libmini/mini.c b/userland/libmini/mini.cindex d277543..56c316b 100644--- a/userland/libmini/mini.c+++ b/userland/libmini/mini.c@@ -215,6 +215,42 @@ chown(const char *path, int uid, int gid)return (int)SC(SYS_chown, path, uid, gid, 0, 0, 0);}+int+lchown(const char *path, int uid, int gid)+{+ return (int)SC(SYS_lchown, path, uid, gid, 0, 0, 0);+}++int+chflags(const char *path, int flags)+{+ return (int)SC(SYS_chflags, path, flags, 0, 0, 0, 0);+}++int+utimes(const char *path, const struct timeval times[2])+{+ return (int)SC(SYS_utimes, path, times, 0, 0, 0, 0);+}++ssize_t+listxattr(const char *path, char *names, size_t size, int options)+{+ return SC(SYS_listxattr, path, names, size, options, 0, 0);+}++ssize_t+getxattr(const char *path, const char *name, void *value, size_t size, uint32_t position, int options)+{+ return SC(SYS_getxattr, path, name, value, size, position, options);+}++int+setxattr(const char *path, const char *name, const void *value, size_t size, uint32_t position, int options)+{+ return (int)SC(SYS_setxattr, path, name, value, size, position, options);+}+intftruncate(int fd, off_t length){diff --git a/userland/libmini/mini.h b/userland/libmini/mini.hindex 39f0d54..cd2ec73 100644--- a/userland/libmini/mini.h+++ b/userland/libmini/mini.h@@ -21,12 +21,13 @@ extern char **environ;enum {SYS_exit = 1, SYS_read = 3, SYS_write = 4, SYS_open = 5, SYS_close = 6, SYS_wait4 = 7, SYS_link = 9,SYS_unlink = 10, SYS_chdir = 12, SYS_chmod = 15, SYS_chown = 16, SYS_getpid = 20, SYS_getuid = 24,- SYS_kill = 37, SYS_getppid = 39, SYS_dup = 41, SYS_ioctl = 54, SYS_reboot = 55, SYS_symlink = 57,- SYS_readlink = 58, SYS_execve = 59, SYS_msync = 65, SYS_munmap = 73, SYS_dup2 = 90, SYS_select = 93,- SYS_fsync = 95, SYS_socket = 97, SYS_bind = 104, SYS_gettimeofday = 116, SYS_rename = 128, SYS_mkdir = 136,- SYS_rmdir = 137, SYS_pread = 153, SYS_pwrite = 154, SYS_mount = 167, SYS_mmap = 197, SYS_lseek = 199,- SYS_ftruncate = 201, SYS_sysctlbyname = 274, SYS_stat64 = 338, SYS_fstat64 = 339, SYS_lstat64 = 340,- SYS_getdirentries64 = 344,+ SYS_chflags = 34, SYS_kill = 37, SYS_getppid = 39, SYS_dup = 41, SYS_ioctl = 54, SYS_reboot = 55,+ SYS_symlink = 57, SYS_readlink = 58, SYS_execve = 59, SYS_msync = 65, SYS_munmap = 73, SYS_dup2 = 90,+ SYS_select = 93, SYS_fsync = 95, SYS_socket = 97, SYS_bind = 104, SYS_gettimeofday = 116,+ SYS_rename = 128, SYS_mkdir = 136, SYS_rmdir = 137, SYS_utimes = 138, SYS_pread = 153, SYS_pwrite = 154,+ SYS_mount = 167, SYS_mmap = 197, SYS_lseek = 199, SYS_ftruncate = 201, SYS_getxattr = 234,+ SYS_setxattr = 236, SYS_listxattr = 240, SYS_sysctlbyname = 274, SYS_stat64 = 338, SYS_fstat64 = 339,+ SYS_lstat64 = 340, SYS_getdirentries64 = 344, SYS_lchown = 364,};/*@@ -75,6 +76,9 @@ struct sockaddr_un {#define S_IFREG 0100000#define S_IFLNK 0120000#define S_IFSOCK 0140000+#define S_IFIFO 0010000++#define XATTR_NOFOLLOW 0x0001#define DT_DIR 4#define DT_REG 8@@ -152,6 +156,12 @@ int link(const char *from, const char *to);int symlink(const char *target, const char *path);int chmod(const char *path, int mode);int chown(const char *path, int uid, int gid);+int lchown(const char *path, int uid, int gid);+int chflags(const char *path, int flags);+int utimes(const char *path, const struct timeval times[2]);+ssize_t listxattr(const char *path, char *names, size_t size, int options);+ssize_t getxattr(const char *path, const char *name, void *value, size_t size, uint32_t position, int options);+int setxattr(const char *path, const char *name, const void *value, size_t size, uint32_t position, int options);int ftruncate(int fd, off_t length);int fsync(int fd);ssize_t pread(int fd, void *buf, size_t count, off_t offset);diff --git a/userland/rc.jobs.persist b/userland/rc.jobs.persistnew file mode 100644index 0000000..2dbcbe1--- /dev/null+++ b/userland/rc.jobs.persist@@ -0,0 +1,19 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# The data volume of iOS kept between boots (var_store=1 ufs_write=1; src/var_store.c): what the+# store held when this boot began, the line every run before this one left in /private/var and the+# names SpringBoard had made by then with the seconds they were written, then this run's own line;+# and after two minutes of iOS left to itself -- long enough for the home screen, the snapshots and+# whatever was touched -- the same names again and the whole of /private/var into the store.+# Run it twice over, with ./xl reboot in between: the second run's output holds the first run's+# line and the first run's seconds, which is the whole point.+/redwood/bin/var_store show+/redwood/bin/vol_ls cat /private/var/redwood-stamp 4096+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/var_store stamp /private/var/redwood-stamp+sleep 120+/redwood/bin/vol_ls ls /private/var/mobile/Library/SpringBoard+/redwood/bin/vol_ls ls /private/var/mobile/Library+/redwood/bin/vol_ls cat /private/var/redwood-stamp 4096+/redwood/bin/var_store save+/redwood/bin/var_store show+/redwood/bin/vol_ls sysctl kern.redwood.ufs_writes kern.redwood.ufs_write_bytes kern.redwood.ufs_write_errors kern.redwood.ufs_write_refuseddiff --git a/userland/src/init.c b/userland/src/init.cindex 6482ce3..45ee283 100644--- a/userland/src/init.c+++ b/userland/src/init.c@@ -645,6 +645,42 @@ mount_writable(void)check(ok && made > 50, "create the directories of the data volume");}+/*+ * What the last boot left in /private/var, back out of the phone's flash (var_store=1): the+ * memory filesystem is empty at every boot, so everything iOS writes -- the home screen's layout,+ * the applications' own data, the snapshots and the baked wallpaper -- would otherwise be new+ * again every time. userland/src/var_store.c keeps a copy in the partition darwin, past the iOS+ * volume and the cryptexes, and puts it back here, over the skeleton: a name the skeleton has and+ * the store has too comes out as the store has it. A store that is not there or does not check+ * out leaves what the skeleton made, and the boot goes on either way -- so a bad store cannot+ * keep the phone from starting.+ */+static void+restore_var(void)+{+ const char *wanted = boot_arg("var_store");+ char *argv[3];+ int status = 0;+ pid_t pid;++ if (wanted == NULL || strcmp(wanted, "1") != 0) {+ return;+ }+ printf("\n-- the data volume of the last boot --\n");+ argv[0] = (char *)in_image("/bin/var_store");+ argv[1] = (char *)"load";+ argv[2] = NULL;+ pid = fork();+ if (pid == 0) {+ execve(argv[0], argv, environ);+ printf(" %s: %s\n", argv[0], strerror(errno));+ _exit(127);+ }+ if (pid < 0 || wait4(pid, &status, 0, NULL) != pid || !WIFEXITED(status)) {+ printf(" var_store did not run\n");+ }+}+static intcount_entries(const char *path){@@ -1063,6 +1099,7 @@ main(int argc, char **argv, char **envp)show_system();mount_writable();+ restore_var();test_files();test_writable();test_memory();diff --git a/userland/src/var_store.c b/userland/src/var_store.cnew file mode 100644index 0000000..bd80612--- /dev/null+++ b/userland/src/var_store.c@@ -0,0 +1,1080 @@+/*+ * The data volume of iOS between two boots: /private/var packed into the phone's flash and+ * unpacked out of it again.+ *+ * /private/var is a memory filesystem (bsd/miscfs/ramfs), mounted empty by init at every boot and+ * given the directories of the template iOS ships with (ios165/mkvarskel.py), so nothing iOS keeps+ * there outlives the boot that wrote it: no home screen layout, no application data, no baked+ * wallpaper. The kernel's APFS driver reads and does not write, and writing APFS is not the point+ * here -- what is needed is that the bytes come back. So the tree is packed into a plain archive+ * and the archive is written into the one place on the phone's flash this port owns and nothing of+ * it uses: the partition darwin, past the iOS volume and both cryptexes. The kernel takes those+ * writes on /dev/rufs_darwin alone and refuses anything else (bsd/dev/redwood_ufs.c, ufs_write=1).+ *+ * var_store save pack /private/var into the store+ * var_store load unpack the newest good store into /private/var (init, at boot)+ * var_store show what the store holds, both slots+ * var_store erase forget both slots (the next boot starts empty again)+ * var_store stamp PATH write a line into PATH saying when: a marker for a run+ *+ * Two slots, written in turn: the one not holding the newest good archive is the one written, its+ * header first zeroed, the archive next, the header last. So a save that does not finish -- the+ * phone reset in the middle of it -- loses nothing: the other slot still holds the archive from+ * before, and `load` takes the newest slot whose header and contents both check out.+ *+ * `load` reads the archive twice: once to see that its checksum is the one in the header, and only+ * then to make files. A store that was torn or is half a different archive therefore leaves+ * /private/var as init made it rather than half filled.+ *+ * What is left out: sockets and devices, which the daemons of a new boot make for themselves, and+ * the directories in SKIP below, which hold what only this boot can mean (its runtime sockets, its+ * swap, its logs) or what is grafted from a read-only image at every boot anyway.+ */+#include "mini.h"++#define BLOCK 4096+#define BUFFER (4u << 20) /* the loader's own buffer: one request a write */+#define STORE_AT 8589934592LL /* darwin's block 2097152: past the cryptexes, with room to spare */+#define SLOT_BYTES 0x80000000LL /* 2 GiB a slot, two slots */+#define SLOTS 2+#define DEVICE "/dev/rufs_darwin"+#define ROOT "/private/var"+#define MAX_PATH 1024+#define MAX_DEPTH 32+#define MAX_XATTR (256u << 10) /* one attribute's value; iOS keeps small ones */+#define XPACK (512u << 10) /* all the attributes of one file, packed */+#define LINKS 8192 /* names remembered for hard links */+#define LINK_PATH 256+/*+ * The file flags put back: the ones a tree of files can mean. UF_COMPRESSED is not among them+ * (a ramfs file is not a compressed HFS one, and the flag would make it read as empty), nor are+ * the flags only a single-user boot can clear.+ */+#define FLAGS_KEPT (0x1u | 0x2u | 0x4u | 0x8u | 0x8000u) /* nodump, immutable, append, opaque, hidden */++/* the directories whose contents are not kept (the directory itself is) */+static const char *const SKIP[] = {+ "tmp", /* its name says it */+ "run", /* runtime sockets and pid files: this boot's, not the last one's */+ "vm", /* the compressor's swap */+ "log", "logs", /* what the last boot said */+ "db/diagnostics", "db/uuidtext", /* logd's archive: tens of MB a boot, and remade */+ "jb", /* the package prefix, grafted from the read-only image at every boot */+ NULL,+};++enum { ENT_DIR = 1, ENT_FILE = 2, ENT_SYMLINK = 3, ENT_HARDLINK = 4, ENT_DIRTIME = 5 };++#define HEAD_MAGIC "RDWVAR\0"+#define HEAD_VERSION 1++struct head {+ char magic[8];+ uint32_t version;+ uint32_t slot;+ uint64_t generation; /* the newest good slot is the one with the highest */+ uint64_t bytes; /* the archive, without the padding to a whole block */+ uint64_t entries;+ uint64_t seconds; /* when it was written */+ uint32_t crc; /* of those bytes */+ uint32_t head_crc; /* of this structure with head_crc zero */+};++struct entry {+ uint32_t kind;+ uint32_t mode; /* permission bits */+ uint32_t uid, gid;+ uint32_t flags; /* st_flags */+ uint32_t pathlen;+ uint32_t nxattr;+ uint32_t mtime_nsec;+ int64_t mtime_sec;+ uint64_t size; /* a file's contents, a link's target, a hard link's first name */+};++/* ---- checksum (the same one the loader reads a write back with) ---- */++static uint32_t crc_table[256];++static void+crc_init(void)+{+ for (uint32_t i = 0; i < 256; i++) {+ uint32_t c = i;++ for (int k = 0; k < 8; k++) {+ c = (c >> 1) ^ (0xedb88320u & (0u - (c & 1)));+ }+ crc_table[i] = c;+ }+}++static uint32_t+crc_add(uint32_t c, const void *p, uint64_t n)+{+ const uint8_t *b = p;++ for (uint64_t i = 0; i < n; i++) {+ c = crc_table[(c ^ b[i]) & 0xff] ^ (c >> 8);+ }+ return c;+}++static uint32_t+head_checksum(const struct head *h)+{+ struct head copy = *h;++ copy.head_crc = 0;+ return crc_add(0xffffffffu, ©, sizeof(copy)) ^ 0xffffffffu;+}++/* ---- the store, as a stream of bytes in one slot ---- */++static int fd = -1;+static uint8_t *buffer;+static uint64_t buffered; /* bytes in the buffer: to be written, or still to be read */+static uint64_t taken; /* of those, already read out (load) */+static off_t at; /* where in the device the buffer begins */+static uint64_t stream_bytes; /* the archive's own bytes so far */+static uint64_t stream_left; /* bytes of the archive still to come (load) */+static off_t stream_limit; /* the end of the slot: nothing is read or written past it */+static uint32_t stream_crc;+static uint64_t entries;++static off_t+slot_at(int slot)+{+ return STORE_AT + (off_t)slot * SLOT_BYTES;+}++static int+device_write(off_t where, const void *data, uint64_t n)+{+ const uint8_t *p = data;++ while (n > 0) {+ ssize_t wrote = pwrite(fd, p, (size_t)n, where);++ if (wrote <= 0) {+ printf("var_store: writing %llu bytes at %lld: %s\n", (unsigned long long)n,+ (long long)where, strerror(errno));+ return 0;+ }+ p += wrote;+ where += wrote;+ n -= (uint64_t)wrote;+ }+ return 1;+}++static int+device_read(off_t where, void *data, uint64_t n)+{+ uint8_t *p = data;++ while (n > 0) {+ ssize_t got = pread(fd, p, (size_t)n, where);++ if (got <= 0) {+ printf("var_store: reading %llu bytes at %lld: %s\n", (unsigned long long)n,+ (long long)where, strerror(errno));+ return 0;+ }+ p += got;+ where += got;+ n -= (uint64_t)got;+ }+ return 1;+}++/* ---- writing the archive out ---- */++static int+out_flush(int last)+{+ uint64_t n = buffered;++ if (n == 0) {+ return 1;+ }+ if (last) {+ /* whole blocks only: the tail is padded, and the header says where the archive ends */+ while (n % BLOCK != 0) {+ buffer[n++] = 0;+ }+ }+ if (at + (off_t)n > stream_limit) {+ printf("var_store: the archive does not fit in its slot: nothing more is written\n");+ return 0;+ }+ if (!device_write(at, buffer, n)) {+ return 0;+ }+ at += (off_t)n;+ buffered = 0;+ return 1;+}++static int+out(const void *data, uint64_t n)+{+ const uint8_t *p = data;++ stream_crc = crc_add(stream_crc, data, n);+ stream_bytes += n;+ while (n > 0) {+ uint64_t room = BUFFER - buffered;++ if (room == 0) {+ if (!out_flush(0)) {+ return 0;+ }+ continue;+ }+ if (room > n) {+ room = n;+ }+ memcpy(buffer + buffered, p, (size_t)room);+ buffered += room;+ p += room;+ n -= room;+ }+ return 1;+}++static int+out_pad(void)+{+ static const uint8_t zero[8] = { 0 };+ uint64_t over = stream_bytes % 8;++ return over == 0 ? 1 : out(zero, 8 - over);+}++/* ---- reading the archive in ---- */++static int+in(void *data, uint64_t n)+{+ uint8_t *p = data;++ if (n > stream_left) {+ printf("var_store: the archive ends in the middle of an entry\n");+ return 0;+ }+ stream_left -= n;+ stream_bytes += n;+ while (n > 0) {+ uint64_t have = buffered - taken;++ if (have == 0) {+ uint64_t want = BUFFER;++ if (at + (off_t)want > stream_limit) {+ want = (uint64_t)(stream_limit - at);+ }+ if (want == 0 || !device_read(at, buffer, want)) {+ return 0;+ }+ at += (off_t)want;+ buffered = want;+ taken = 0;+ continue;+ }+ if (have > n) {+ have = n;+ }+ if (p != NULL) {+ memcpy(p, buffer + taken, (size_t)have);+ p += have;+ }+ stream_crc = crc_add(stream_crc, buffer + taken, have);+ taken += have;+ n -= have;+ }+ return 1;+}++/* n bytes of the archive passed over, their checksum counted in */+static int+in_skip(uint64_t n)+{+ uint8_t waste[512];++ while (n > 0) {+ uint64_t piece = n > sizeof(waste) ? sizeof(waste) : n;++ if (!in(waste, piece)) {+ return 0;+ }+ n -= piece;+ }+ return 1;+}++static int+in_pad(void)+{+ uint64_t over = stream_bytes % 8;++ return over == 0 ? 1 : in_skip(8 - over);+}++static void+stream_start(int slot, uint64_t bytes)+{+ at = slot_at(slot) + BLOCK;+ stream_limit = slot_at(slot) + SLOT_BYTES;+ buffered = taken = 0;+ stream_bytes = 0;+ stream_left = bytes;+ stream_crc = 0xffffffffu;+}++/* ---- the slots ---- */++/* The slot's header, if it is one of ours and says of itself what it should. */+static int+read_head(int slot, struct head *into)+{+ static uint8_t block[BLOCK];+ struct head *h = (struct head *)block;++ if (!device_read(slot_at(slot), block, BLOCK)) {+ return 0;+ }+ if (memcmp(h->magic, HEAD_MAGIC, sizeof(h->magic)) != 0 || h->version != HEAD_VERSION ||+ h->slot != (uint32_t)slot || h->bytes > (uint64_t)SLOT_BYTES - BLOCK ||+ h->head_crc != head_checksum(h)) {+ return 0;+ }+ *into = *h;+ return 1;+}++/* The newest slot whose header checks out, or -1. */+static int+newest(struct head *out_head)+{+ int best = -1;++ for (int slot = 0; slot < SLOTS; slot++) {+ struct head h;++ if (!read_head(slot, &h)) {+ continue;+ }+ if (best < 0 || h.generation > out_head->generation) {+ best = slot;+ *out_head = h;+ }+ }+ return best;+}++/* ---- packing ---- */++static char path[MAX_PATH]; /* the whole path, under ROOT */+static struct { uint64_t ino; char path[LINK_PATH]; } *links;+static uint64_t skipped_kinds, skipped_big;++static int+is_skipped(const char *relative)+{+ for (int i = 0; SKIP[i] != NULL; i++) {+ if (strcmp(relative, SKIP[i]) == 0) {+ return 1;+ }+ }+ return 0;+}++/* The name this inode was first met under, if it has one; otherwise this one is remembered. */+static const char *+link_name(uint64_t ino, const char *relative)+{+ uint64_t slot = (ino * 2654435761u) % LINKS;++ for (uint64_t tries = 0; tries < LINKS; tries++, slot = (slot + 1) % LINKS) {+ if (links[slot].ino == ino) {+ return links[slot].path;+ }+ if (links[slot].ino == 0) {+ if (strlen(relative) < LINK_PATH) {+ links[slot].ino = ino;+ strcpy(links[slot].path, relative);+ }+ return NULL;+ }+ }+ return NULL; /* the table is full: this one is written out whole */+}++/*+ * The extended attributes of path -- iOS keeps container metadata and much else in them -- packed+ * into a buffer of their own, because the entry's head says how many there are and has to go out+ * before them. One that does not fit is left out and counted.+ */+static uint8_t xpack[XPACK];++static uint64_t+pack_xattrs(uint32_t *count)+{+ static char names[16384];+ static uint8_t value[MAX_XATTR];+ ssize_t n = listxattr(path, names, sizeof(names), XATTR_NOFOLLOW);+ uint64_t used = 0;++ *count = 0;+ if (n <= 0) {+ return 0;+ }+ for (ssize_t i = 0; i < n && i < (ssize_t)sizeof(names); i += (ssize_t)strlen(names + i) + 1) {+ uint32_t namelen = (uint32_t)strlen(names + i), vlen;+ ssize_t size = getxattr(path, names + i, value, sizeof(value), 0, XATTR_NOFOLLOW);++ if (size < 0) {+ continue;+ }+ vlen = (uint32_t)size;+ if (used + 8 + namelen + vlen > XPACK) {+ skipped_big++;+ continue;+ }+ memcpy(xpack + used, &namelen, sizeof(namelen));+ memcpy(xpack + used + 4, &vlen, sizeof(vlen));+ memcpy(xpack + used + 8, names + i, namelen);+ memcpy(xpack + used + 8 + namelen, value, vlen);+ used += 8 + namelen + vlen;+ (*count)++;+ }+ return used;+}++/* One entry: its head, its name, its extended attributes, its contents. */+static int+pack_one(const char *relative, const struct stat *st)+{+ struct entry e;+ static char target[MAX_PATH];+ const char *first;+ uint32_t kind = 0;+ uint64_t size = 0;+ const void *tail = NULL;+ int in_fd = -1;++ switch (st->st_mode & S_IFMT) {+ case S_IFDIR:+ kind = ENT_DIR;+ break;+ case S_IFLNK: {+ ssize_t n = readlink(path, target, sizeof(target));++ if (n <= 0) {+ return 1;+ }+ kind = ENT_SYMLINK;+ size = (uint64_t)n;+ tail = target;+ break;+ }+ case S_IFREG:+ kind = ENT_FILE;+ size = (uint64_t)st->st_size;+ if (st->st_nlink > 1) {+ first = link_name(st->st_ino, relative);+ if (first != NULL) {+ kind = ENT_HARDLINK;+ size = strlen(first);+ tail = first;+ break;+ }+ }+ in_fd = open(path, O_RDONLY, 0);+ if (in_fd < 0) {+ return 1; /* gone, or not ours to read: it is not in the archive */+ }+ break;+ default:+ skipped_kinds++; /* a socket, a fifo, a device: a new boot makes its own */+ return 1;+ }++ memset(&e, 0, sizeof(e));+ e.kind = kind;+ e.mode = st->st_mode & 07777;+ e.uid = st->st_uid;+ e.gid = st->st_gid;+ e.flags = st->st_flags & FLAGS_KEPT;+ e.pathlen = (uint32_t)strlen(relative);+ e.mtime_sec = st->st_mtimespec.tv_sec;+ e.mtime_nsec = (uint32_t)st->st_mtimespec.tv_nsec;+ e.size = size;+ {+ uint64_t xlen = pack_xattrs(&e.nxattr);++ if (!out(&e, sizeof(e)) || !out(relative, e.pathlen) || !out(xpack, xlen)) {+ if (in_fd >= 0) {+ close(in_fd);+ }+ return 0;+ }+ }+ if (tail != NULL && !out(tail, size)) {+ if (in_fd >= 0) {+ close(in_fd);+ }+ return 0;+ }+ if (in_fd >= 0) {+ static uint8_t piece[256 * 1024];+ uint64_t left = size;++ while (left > 0) {+ uint64_t want = left > sizeof(piece) ? sizeof(piece) : left;+ ssize_t got = read(in_fd, piece, (size_t)want);++ if (got <= 0) {+ /* the file shrank under us: the rest is zeros, so the archive stays whole */+ memset(piece, 0, (size_t)want);+ got = (ssize_t)want;+ }+ if (!out(piece, (uint64_t)got)) {+ close(in_fd);+ return 0;+ }+ left -= (uint64_t)got;+ }+ close(in_fd);+ }+ if (!out_pad()) {+ return 0;+ }+ entries++;+ return 1;+}++/* The directory's own times once more, after its contents: making them moved them. */+static int+pack_dirtime(const char *relative, const struct stat *st)+{+ struct entry e;++ memset(&e, 0, sizeof(e));+ e.kind = ENT_DIRTIME;+ e.flags = st->st_flags & FLAGS_KEPT;+ e.pathlen = (uint32_t)strlen(relative);+ e.mtime_sec = st->st_mtimespec.tv_sec;+ e.mtime_nsec = (uint32_t)st->st_mtimespec.tv_nsec;+ if (!out(&e, sizeof(e)) || !out(relative, e.pathlen) || !out_pad()) {+ return 0;+ }+ entries++;+ return 1;+}++/* relative: the path under ROOT, "" for ROOT itself. path holds the whole of it. */+static int+pack_tree(const char *relative)+{+ static char dirbuf[MAX_DEPTH][4096]; /* one read buffer a level */+ static int depth;+ struct stat st;+ off_t position = 0;+ ssize_t n;+ size_t base = strlen(path), rbase = strlen(relative);+ int dir_fd, ok = 1;++ if (lstat(path, &st) != 0) {+ return 1;+ }+ if (relative[0] != 0 && !pack_one(relative, &st)) {+ return 0;+ }+ if ((st.st_mode & S_IFMT) != S_IFDIR || (relative[0] != 0 && is_skipped(relative))) {+ return 1;+ }+ if (depth >= MAX_DEPTH) {+ printf("var_store: %s: deeper than this program walks\n", path);+ return 1;+ }+ if ((dir_fd = open(path, O_RDONLY | O_DIRECTORY, 0)) < 0) {+ return 1;+ }+ depth++;+ while (ok && (n = getdirentries(dir_fd, dirbuf[depth - 1], sizeof(dirbuf[0]), &position)) > 0) {+ for (ssize_t i = 0; ok && i < n;) {+ struct dirent *d = (struct dirent *)(dirbuf[depth - 1] + i);+ const char *name = d->d_name;+ char child[MAX_PATH]; /* the caller's while this one recurses: not static */++ i += d->d_reclen;+ if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {+ continue;+ }+ if (base + 1 + strlen(name) >= MAX_PATH || rbase + 1 + strlen(name) >= MAX_PATH) {+ printf("var_store: %s/%s: the path is too long\n", path, name);+ continue;+ }+ path[base] = '/';+ strcpy(path + base + 1, name);+ if (rbase > 0) {+ memcpy(child, relative, rbase);+ child[rbase] = '/';+ strcpy(child + rbase + 1, name);+ } else {+ strcpy(child, name);+ }+ ok = pack_tree(child);+ path[base] = 0;+ }+ }+ depth--;+ close(dir_fd);+ if (ok && relative[0] != 0 && lstat(path, &st) == 0) {+ ok = pack_dirtime(relative, &st);+ }+ return ok;+}++static int+save(void)+{+ struct head old, h;+ int slot, best;+ static uint8_t block[BLOCK];++ memset(&old, 0, sizeof(old));+ best = newest(&old);+ slot = best < 0 ? 0 : (best + 1) % SLOTS;+ printf("var_store: saving into slot %d (slot %d holds generation %llu)\n", slot, best,+ best < 0 ? 0ULL : (unsigned long long)old.generation);++ /* the slot's header first, so that a save cut short cannot leave an old header over a new archive */+ memset(block, 0, sizeof(block));+ if (!device_write(slot_at(slot), block, BLOCK)) {+ return 0;+ }+ stream_start(slot, 0);+ entries = 0;+ strcpy(path, ROOT);+ if (!pack_tree("") || !out_flush(1)) {+ return 0;+ }+ memset(&h, 0, sizeof(h));+ memcpy(h.magic, HEAD_MAGIC, sizeof(h.magic));+ h.version = HEAD_VERSION;+ h.slot = (uint32_t)slot;+ h.generation = (best < 0 ? 0 : old.generation) + 1;+ h.bytes = stream_bytes;+ h.entries = entries;+ {+ struct timeval tv = { 0, 0 };++ gettimeofday(&tv, NULL);+ h.seconds = (uint64_t)tv.tv_sec;+ }+ h.crc = stream_crc ^ 0xffffffffu;+ h.head_crc = head_checksum(&h);+ if (h.bytes > (uint64_t)SLOT_BYTES - BLOCK) {+ printf("var_store: the archive is %llu bytes, more than a slot holds (%llu): nothing saved\n",+ (unsigned long long)h.bytes, (unsigned long long)(SLOT_BYTES - BLOCK));+ return 0;+ }+ memset(block, 0, sizeof(block));+ memcpy(block, &h, sizeof(h));+ if (!device_write(slot_at(slot), block, BLOCK)) {+ return 0;+ }+ printf("var_store: slot %d: generation %llu, %llu entries, %llu bytes, crc %08x%s%s\n", slot,+ (unsigned long long)h.generation, (unsigned long long)h.entries, (unsigned long long)h.bytes,+ h.crc, skipped_kinds ? " (sockets and the like left out)" : "",+ skipped_big ? " (something too big left out)" : "");+ return 1;+}++/* ---- unpacking ---- */++static uint64_t made_dirs, made_files, made_links, made_bytes;++static int+whole_path(const char *relative, uint32_t len)+{+ if (len == 0 || len >= MAX_PATH - sizeof(ROOT) - 1 || relative[0] == '/') {+ return 0;+ }+ for (uint32_t i = 0; i + 1 < len; i++) {+ if (relative[i] == '.' && relative[i + 1] == '.' &&+ (i == 0 || relative[i - 1] == '/') && (i + 2 == len || relative[i + 2] == '/')) {+ return 0; /* nothing of this archive is unpacked outside ROOT */+ }+ }+ strcpy(path, ROOT);+ path[sizeof(ROOT) - 1] = '/';+ memcpy(path + sizeof(ROOT), relative, len);+ path[sizeof(ROOT) + len] = 0;+ return 1;+}++static void+set_times(const struct entry *e)+{+ struct timeval times[2];++ times[0].tv_sec = (long)e->mtime_sec;+ times[0].tv_usec = (int)(e->mtime_nsec / 1000);+ times[1] = times[0];+ utimes(path, times);+}++/*+ * One entry out of the archive and into /private/var. make: with 0, the entry is only read past+ * (the first pass, which proves the archive's checksum before anything is made).+ */+static int+unpack_one(int make)+{+ struct entry e;+ static char relative[MAX_PATH];+ static uint8_t value[MAX_XATTR];+ static char name[1024];+ int out_fd = -1, ok = 1;++ if (!in(&e, sizeof(e))) {+ return 0;+ }+ if (e.pathlen >= MAX_PATH || e.nxattr > 4096) {+ printf("var_store: an entry this program cannot read (kind %u, path %u bytes)\n", e.kind,+ e.pathlen);+ return 0;+ }+ if (!in(relative, e.pathlen)) {+ return 0;+ }+ relative[e.pathlen] = 0;+ if (make && !whole_path(relative, e.pathlen)) {+ make = 0; /* read past it: an archive is not to make names of its own choosing */+ }+ if (make) {+ switch (e.kind) {+ case ENT_DIR:+ if (mkdir(path, (int)e.mode) == 0) {+ made_dirs++;+ }+ break;+ case ENT_FILE:+ out_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, (int)e.mode);+ if (out_fd < 0) {+ printf("var_store: %s: %s\n", path, strerror(errno));+ } else {+ made_files++;+ }+ break;+ default:+ break;+ }+ }+ /* the attributes, before the contents, as they were packed */+ for (uint32_t i = 0; i < e.nxattr; i++) {+ uint32_t namelen, vlen;++ if (!in(&namelen, sizeof(namelen)) || !in(&vlen, sizeof(vlen))) {+ ok = 0;+ break;+ }+ if (namelen >= sizeof(name) || vlen > sizeof(value)) {+ ok = in_skip(namelen) && in_skip(vlen);+ skipped_big++;+ continue;+ }+ if (!in(name, namelen) || !in(value, vlen)) {+ ok = 0;+ break;+ }+ name[namelen] = 0;+ if (make && e.kind != ENT_HARDLINK) {+ setxattr(path, name, value, vlen, 0, XATTR_NOFOLLOW);+ }+ }+ if (!ok) {+ if (out_fd >= 0) {+ close(out_fd);+ }+ return 0;+ }+ /* the contents */+ if (e.kind == ENT_FILE) {+ static uint8_t piece[256 * 1024];+ uint64_t left = e.size;++ while (left > 0) {+ uint64_t want = left > sizeof(piece) ? sizeof(piece) : left;++ if (!in(piece, want)) {+ ok = 0;+ break;+ }+ if (out_fd >= 0 && write(out_fd, piece, (size_t)want) != (ssize_t)want) {+ printf("var_store: %s: %s\n", path, strerror(errno));+ close(out_fd);+ out_fd = -1;+ }+ left -= want;+ }+ if (make) {+ made_bytes += e.size;+ }+ } else if (e.kind == ENT_SYMLINK || e.kind == ENT_HARDLINK) {+ static char target[MAX_PATH];++ if (e.size >= MAX_PATH) {+ ok = in_skip(e.size);+ } else if (!in(target, e.size)) {+ ok = 0;+ } else {+ target[e.size] = 0;+ if (make && e.kind == ENT_SYMLINK) {+ if (symlink(target, path) == 0) {+ made_links++;+ }+ } else if (make) {+ static char from[MAX_PATH];+ char keep[MAX_PATH];++ strcpy(keep, path);+ if (whole_path(target, (uint32_t)e.size)) {+ strcpy(from, path);+ strcpy(path, keep);+ if (link(from, path) == 0) {+ made_links++;+ }+ } else {+ strcpy(path, keep);+ }+ }+ }+ }+ if (out_fd >= 0) {+ close(out_fd);+ }+ if (!ok || !in_pad()) {+ return 0;+ }+ if (!make) {+ return 1;+ }+ /*+ * Owner, then permissions, then the times (writing moved them), and the flags last of all:+ * one of them may be what keeps the file from being changed again.+ */+ switch (e.kind) {+ case ENT_DIR:+ /* its flags wait for ENT_DIRTIME: one of them may be what stops anything being made in it */+ chown(path, (int)e.uid, (int)e.gid);+ chmod(path, (int)e.mode);+ break;+ case ENT_DIRTIME:+ set_times(&e);+ if (e.flags != 0) {+ chflags(path, (int)e.flags);+ }+ break;+ case ENT_SYMLINK:+ lchown(path, (int)e.uid, (int)e.gid);+ break;+ case ENT_HARDLINK:+ break; /* the same inode: it has them already */+ default:+ chown(path, (int)e.uid, (int)e.gid);+ chmod(path, (int)e.mode);+ set_times(&e);+ if (e.flags != 0) {+ chflags(path, (int)e.flags);+ }+ break;+ }+ return 1;+}++static int+pass(const struct head *h, int make)+{+ stream_start((int)h->slot, h->bytes);+ for (uint64_t i = 0; i < h->entries; i++) {+ if (!unpack_one(make)) {+ return 0;+ }+ }+ if (stream_left != 0) {+ printf("var_store: %llu bytes left over after %llu entries\n",+ (unsigned long long)stream_left, (unsigned long long)h->entries);+ return 0;+ }+ if ((stream_crc ^ 0xffffffffu) != h->crc) {+ printf("var_store: slot %u: the contents are not the %08x the header says (%08x)\n", h->slot,+ h->crc, stream_crc ^ 0xffffffffu);+ return 0;+ }+ return 1;+}++static int+load(void)+{+ struct head h;+ int best;++ memset(&h, 0, sizeof(h));+ best = newest(&h);+ if (best < 0) {+ printf("var_store: no store yet: /private/var stays as init made it\n");+ return 1;+ }+ printf("var_store: slot %d: generation %llu of %llu seconds, %llu entries, %llu bytes\n", best,+ (unsigned long long)h.generation, (unsigned long long)h.seconds,+ (unsigned long long)h.entries, (unsigned long long)h.bytes);+ /* read once to prove the checksum, and only then make anything */+ if (!pass(&h, 0)) {+ struct head other;+ int left = -1;++ /* the newest is torn: the one before it, which a save never touches, may still be whole */+ for (int slot = 0; slot < SLOTS; slot++) {+ if (slot != best && read_head(slot, &other) && pass(&other, 0)) {+ left = slot;+ break;+ }+ }+ if (left < 0) {+ printf("var_store: nothing good in the store: /private/var stays as init made it\n");+ return 1;+ }+ h = other;+ printf("var_store: slot %d is good after all: generation %llu\n", left,+ (unsigned long long)h.generation);+ }+ if (!pass(&h, 1)) {+ printf("var_store: the archive read differently the second time: what was made stays\n");+ return 0;+ }+ printf("var_store: %llu directories, %llu files (%llu bytes), %llu links back in /private/var\n",+ (unsigned long long)made_dirs, (unsigned long long)made_files, (unsigned long long)made_bytes,+ (unsigned long long)made_links);+ return 1;+}++static void+show(void)+{+ for (int slot = 0; slot < SLOTS; slot++) {+ static uint8_t block[BLOCK];+ struct head *h = (struct head *)block;++ if (!device_read(slot_at(slot), block, BLOCK)) {+ continue;+ }+ if (memcmp(h->magic, HEAD_MAGIC, sizeof(h->magic)) != 0) {+ printf("var_store: slot %d: empty\n", slot);+ continue;+ }+ printf("var_store: slot %d: version %u, generation %llu, %llu entries, %llu bytes, crc %08x, "+ "written at %llu, header %s\n", slot, h->version, (unsigned long long)h->generation,+ (unsigned long long)h->entries, (unsigned long long)h->bytes, h->crc,+ (unsigned long long)h->seconds, h->head_crc == head_checksum(h) ? "right" : "WRONG");+ }+}++static int+erase(void)+{+ static uint8_t block[BLOCK];++ memset(block, 0, sizeof(block));+ for (int slot = 0; slot < SLOTS; slot++) {+ if (!device_write(slot_at(slot), block, BLOCK)) {+ return 0;+ }+ }+ printf("var_store: both slots forgotten\n");+ return 1;+}++/* A line saying when, for a run to leave behind and the next one to find. */+static int+stamp(const char *where)+{+ struct timeval tv = { 0, 0 };+ char line[256];+ int out_fd = open(where, O_WRONLY | O_CREAT | O_APPEND, 0644);+ int n;++ if (out_fd < 0) {+ printf("var_store: %s: %s\n", where, strerror(errno));+ return 0;+ }+ gettimeofday(&tv, NULL);+ n = 0;+ {+ static const char head[] = "written by run of ";+ unsigned long long seconds = (unsigned long long)tv.tv_sec;+ char digits[24];+ int k = 0;++ memcpy(line, head, sizeof(head) - 1);+ n = (int)sizeof(head) - 1;+ do {+ digits[k++] = (char)('0' + seconds % 10);+ seconds /= 10;+ } while (seconds > 0);+ while (k-- > 0) {+ line[n++] = digits[k];+ }+ line[n++] = '\n';+ }+ if (write(out_fd, line, (size_t)n) != n) {+ printf("var_store: %s: %s\n", where, strerror(errno));+ close(out_fd);+ return 0;+ }+ close(out_fd);+ printf("var_store: %s: %d bytes added\n", where, n);+ return 1;+}++int+main(int argc, char **argv, char **envp)+{+ const char *what = argc > 1 ? argv[1] : "show";+ int ok;++ (void)envp;+ crc_init();+ if (strcmp(what, "stamp") == 0) {+ return stamp(argc > 2 ? argv[2] : "/private/var/redwood-stamp") ? 0 : 1;+ }+ buffer = mmap(NULL, BUFFER, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);+ links = mmap(NULL, LINKS * sizeof(*links), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);+ if (buffer == MAP_FAILED || links == MAP_FAILED) {+ printf("var_store: no memory for the buffers\n");+ return 1;+ }+ fd = open(DEVICE, strcmp(what, "load") == 0 || strcmp(what, "show") == 0 ? O_RDONLY : O_RDWR, 0);+ if (fd < 0) {+ printf("var_store: %s: %s (the kernel takes writes with ufs_write=1)\n", DEVICE,+ strerror(errno));+ return 1;+ }+ if (strcmp(what, "save") == 0) {+ ok = save();+ } else if (strcmp(what, "load") == 0) {+ ok = load();+ } else if (strcmp(what, "erase") == 0) {+ ok = erase();+ } else {+ show();+ ok = 1;+ }+ close(fd);+ return ok ? 0 : 1;+}diff --git a/userland/src/vol_ls.c b/userland/src/vol_ls.cindex ced3f5a..71ff153 100644--- a/userland/src/vol_ls.c+++ b/userland/src/vol_ls.c@@ -124,12 +124,14 @@ do_ls(const char *dir)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);+ /* the time of writing as the seconds of the epoch: whether a name is this boot's */+ printf(" %s %3u %4u %4u %12lld %11lld %s -> %s\n", mode, (unsigned)st.st_nlink,+ (unsigned)st.st_uid, (unsigned)st.st_gid, (long long)st.st_size,+ (long long)st.st_mtimespec.tv_sec, 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]);+ printf(" %s %3u %4u %4u %12lld %11lld %s\n", mode, (unsigned)st.st_nlink,+ (unsigned)st.st_uid, (unsigned)st.st_gid, (long long)st.st_size,+ (long long)st.st_mtimespec.tv_sec, names[i]);}free(names[i]);}diff --git a/xnu/files/bsd/dev/redwood_ufs.c b/xnu/files/bsd/dev/redwood_ufs.cindex 3d4d8be..daf69f4 100644--- a/xnu/files/bsd/dev/redwood_ufs.c+++ b/xnu/files/bsd/dev/redwood_ufs.c@@ -1,6 +1,7 @@/*- * The phone's storage, read only: one block device and one character device for every GPT- * partition of its UFS (/dev/ufs_NAME and /dev/rufs_NAME, NAME as the partition table has it).+ * The phone's storage: one block device and one character device for every GPT partition of its+ * UFS (/dev/ufs_NAME and /dev/rufs_NAME, NAME as the partition table has it). Read only, but for+ * the one place the data of iOS lives (ufs_write=1, below).** There is no UFS driver here, and none is needed. UEFI never exits on this port: Qualcomm's own* UFS driver (UFSDxe) has brought up the controller, its PHY, its clocks and power, and stays@@ -11,9 +12,15 @@* driver's ReadBlocks into the buffer (xnuloader/loader.c, block_poll). Nothing in this kernel* touches the storage controller.*- * Read only by construction: opening for writing is refused, a write that reaches the strategy- * routine is refused, and the page has no field in which a write could be asked for -- the- * loader's side calls ReadBlocks and nothing else.+ * Read only wherever a write could do harm, by construction. The block devices never write: the+ * strategy routine refuses, so no filesystem mounted from one can write, and opening one for+ * writing is refused. With ufs_write=1 one character device alone takes writes -- /dev/rufs_darwin,+ * the port's own partition -- and in it only whole blocks from UFS_WRITE_FIRST on, past the iOS+ * volume and both cryptexes that the build writes from the computer (tools/darwin-write.py); that+ * is where the data volume of iOS is kept between runs (userland/src/var_store.c). The partition+ * has to be the one the port was set up with (its name, size, place and logical unit here; its+ * GUIDs as well on the loader's side), and the loader refuses the same things over again and reads+ * every write back before it answers. Without ufs_write=1 nothing writes at all.** One request at a time (the page has one), so requests are serialized by a mutex and split into* pieces of the buffer's size. The kernel writes only the request's cache lines and core 0 only@@ -42,6 +49,7 @@#include <sys/systm.h>#include <sys/proc.h>#include <sys/sysctl.h>+#include <sys/uio.h>#include <sys/redwood_input.h>#include <libkern/crc.h>#include <vm/memory_types.h>@@ -54,10 +62,22 @@ extern void FlushPoC_DcacheRegion(vm_offset_t va, size_t length); /* osfmk/arm/* the page the loader publishes: the same layout as struct loader_block in xnuloader/loader.c */#define BLOCK_MAGIC 0x4b434f4c42574452ULL /* "RDWBLOCK" */-#define BLOCK_VERSION 3+#define BLOCK_VERSION 4#define BLOCK_MAX_PARTS 160#define BLOCK_MAX_UNITS 16#define BLOCK_PART_UNIT 1u /* a whole logical unit, not a partition */+#define BLOCK_OP_READ 0u+#define BLOCK_OP_WRITE 1u++/*+ * Where a write may go, and nowhere else: darwin, the port's own 20 GiB at the end of logical+ * unit 0, and in it only past what the build puts there from the computer -- the iOS volume+ * (blocks 0..1374207) and the two cryptexes behind it (..2042367). The loader keeps the same+ * bounds and the GUIDs of the partition besides (xnuloader/loader.c, BLOCK_WRITE_FIRST).+ */+#define UFS_WRITE_FIRST 2042368ULL /* darwin's blocks of 4096 */+#define UFS_DARWIN_FIRST 25374720ULL /* its first LBA on the unit, as the GPT has it */+#define UFS_DARWIN_BLOCKS 5242875ULLstruct loader_block_part {char name[36];@@ -79,6 +99,8 @@ struct loader_block {volatile uint32_t request;uint32_t part;uint64_t lba, count;+ uint32_t op; /* BLOCK_OP_READ, BLOCK_OP_WRITE */+ uint32_t reserved;} ask __attribute__((aligned(128)));struct {volatile uint32_t answered;@@ -108,7 +130,7 @@ struct loader_block {#define UFS_TIMEOUT_MS 20000 /* core 0 answers in well under a second; this is for a dead loader */static volatile struct loader_block *ufs_page;-static const uint8_t *ufs_buffer;+static uint8_t *ufs_buffer; /* the loader's data buffer: filled by it for a read, here for a write */static uint32_t ufs_buffer_size, ufs_parts, ufs_seq;static uint32_t ufs_bsize[BLOCK_MAX_PARTS]; /* the block size each device is counted in (spec_open sets 512) */static int ufs_bmajor = -1, ufs_cmajor = -1;@@ -134,6 +156,13 @@ static uint64_t ufs_native_fallbacks, ufs_mailbox_refused;static LCK_GRP_DECLARE(ufs_lck_grp, "redwood-ufs");static LCK_MTX_DECLARE(ufs_lock, &ufs_lck_grp);+/* One writer at a time; never held while the mailbox is, nor while user memory is touched. */+static LCK_MTX_DECLARE(ufs_write_mtx, &ufs_lck_grp);++/* ufs_write=1 and the one partition it is for: everything else answers EROFS as it always did */+static int ufs_write_part = -1;+static volatile bool ufs_write_busy; /* a write of ours has this kernel's list out: not a failure */+static uint64_t ufs_writes, ufs_write_bytes, ufs_write_errors, ufs_write_refused;SYSCTL_DECL(_kern_redwood);SYSCTL_QUAD(_kern_redwood, OID_AUTO, ufs_requests, CTLFLAG_RD | CTLFLAG_LOCKED, &ufs_requests,@@ -146,13 +175,21 @@ SYSCTL_QUAD(_kern_redwood, OID_AUTO, ufs_wait_us, CTLFLAG_RD | CTLFLAG_LOCKED, &"microseconds spent waiting for core 0's answers");SYSCTL_QUAD(_kern_redwood, OID_AUTO, ufs_copy_us, CTLFLAG_RD | CTLFLAG_LOCKED, &ufs_copy_us,"microseconds spent copying out of the loader's buffer");+SYSCTL_QUAD(_kern_redwood, OID_AUTO, ufs_writes, CTLFLAG_RD | CTLFLAG_LOCKED, &ufs_writes,+ "requests written into darwin and read back by the loader");+SYSCTL_QUAD(_kern_redwood, OID_AUTO, ufs_write_bytes, CTLFLAG_RD | CTLFLAG_LOCKED, &ufs_write_bytes,+ "bytes written into darwin");+SYSCTL_QUAD(_kern_redwood, OID_AUTO, ufs_write_errors, CTLFLAG_RD | CTLFLAG_LOCKED, &ufs_write_errors,+ "writes the loader refused or did not finish");+SYSCTL_QUAD(_kern_redwood, OID_AUTO, ufs_write_refused, CTLFLAG_RD | CTLFLAG_LOCKED, &ufs_write_refused,+ "writes refused here: not darwin, or not past what the build writes");/*- * Native blocks [lba, lba + count) of partition part, into dst: one request of at most the- * buffer's size. Called with ufs_lock held.+ * Native blocks [lba, lba + count) of partition part, one request of at most the buffer's size:+ * read into dst, or, with src, written from there. Called with ufs_lock held.*/static int-ufs_request(uint32_t part, uint64_t lba, uint64_t count, uint8_t *dst)+ufs_request(uint32_t op, uint32_t part, uint64_t lba, uint64_t count, uint8_t *dst, const uint8_t *src){uint64_t bytes = count * ufs_page->part[part].block_size, started, waited_ms = 0;uint32_t seq = ++ufs_seq;@@ -161,14 +198,25 @@ ufs_request(uint32_t part, uint64_t lba, uint64_t count, uint8_t *dst)if (ufs_dead) {return EIO;}- if (part == ufs_last_part && lba == ufs_last_end) {- ufs_sequential++;+ if (op == BLOCK_OP_READ) {+ if (part == ufs_last_part && lba == ufs_last_end) {+ ufs_sequential++;+ }+ ufs_last_part = part;+ ufs_last_end = lba + count;+ } else {+ /*+ * The data into the buffer and out of this core's cache before the request is raised:+ * core 0 drops its own lines of the buffer and hands it to UEFI's driver.+ */+ memcpy(ufs_buffer, src, (size_t)bytes);+ FlushPoC_DcacheRegion((vm_offset_t)ufs_buffer, (size_t)bytes);+ ufs_last_part = ~0u;}- ufs_last_part = part;- ufs_last_end = lba + count;ufs_page->ask.part = part;ufs_page->ask.lba = lba;ufs_page->ask.count = count;+ ufs_page->ask.op = op;__asm__ volatile ("dmb sy" ::: "memory");ufs_page->ask.request = seq;__asm__ volatile ("dmb sy" ::: "memory");@@ -204,17 +252,25 @@ ufs_request(uint32_t part, uint64_t lba, uint64_t count, uint8_t *dst)}status = ufs_page->answer.status;if (status != 0) {- printf("ufs: %s: blocks %llu..%llu: the loader's read failed (status %llx)\n",- ufs_page->part[part].name, lba, lba + count - 1, (uint64_t)status);+ printf("ufs: %s: blocks %llu..%llu: the loader's %s failed (status %llx)\n",+ ufs_page->part[part].name, lba, lba + count - 1,+ op == BLOCK_OP_WRITE ? "write" : "read", (uint64_t)status);ufs_errors++;return EIO;}+ if (op == BLOCK_OP_WRITE) {+ /* the loader has read the blocks back and found them the same */+ ufs_writes++;+ ufs_write_bytes += bytes;+ return 0;+ }started = mach_absolute_time();/** The buffer is mapped cacheable here (copying out of an uncached mapping went at 870 MB/s,- * as slow as the storage itself). This side never writes it, and core 0 cleans and- * invalidates it after every read; whatever this core may still hold of an earlier read --- * a line brought in by a prefetch, say -- goes before the copy.+ * as slow as the storage itself). Core 0 cleans and invalidates it after every read, and this+ * side only ever fills it for a write of its own and cleans its lines then; whatever this+ * core may still hold of an earlier request -- a line brought in by a prefetch, say -- goes+ * before the copy.*/FlushPoC_DcacheRegion((vm_offset_t)ufs_buffer, (size_t)bytes);memcpy(dst, ufs_buffer, (size_t)bytes);@@ -262,6 +318,17 @@ redwood_ufs_report(const char *when)"the one before), %llu errors, %llu bytes; waiting %llu ms, of it in ReadBlocks %llu ms; ""copying %llu ms\n", when, ufs_requests, ufs_small, ufs_medium, ufs_large, ufs_sequential,ufs_errors, ufs_bytes, ufs_wait_us / 1000, loader_ns / 1000000, ufs_copy_us / 1000);+ if (ufs_write_part >= 0 || ufs_writes != 0 || ufs_write_refused != 0) {+ printf("ufs: %s: %llu writes into darwin, %llu bytes, read back by the loader; %llu did not "+ "finish, %llu refused here\n", when, ufs_writes, ufs_write_bytes, ufs_write_errors,+ ufs_write_refused);+ }+}++static uint64_t+ufs_part_bytes(uint32_t part)+{+ return ufs_page->part[part].blocks * ufs_page->part[part].block_size;}/*@@ -304,9 +371,9 @@ ufs_read_mailbox(uint32_t part, uint64_t offset, uint8_t *dst, uint64_t len)take = len;}if (skip == 0 && take == count * nbs) {- error = ufs_request(part, first, count, dst); /* whole blocks: straight in */+ error = ufs_request(BLOCK_OP_READ, part, first, count, dst, NULL); /* whole blocks: straight in */} else {- error = ufs_request(part, first, count, bounce);+ error = ufs_request(BLOCK_OP_READ, part, first, count, bounce, NULL);if (error == 0) {memcpy(dst, bounce + skip, (size_t)take);}@@ -363,6 +430,10 @@ ufs_read(uint32_t part, uint64_t offset, uint8_t *dst, uint64_t len)if (error == 0) {return 0;}+ if (ufs_write_busy) {+ /* the list is out for a write of ours, not broken: this read goes the other way */+ return ufs_read_mailbox(part, offset, dst, len);+ }if (ufs_native_on) {ufs_native_on = false;ufs_native_fallbacks++;@@ -374,10 +445,75 @@ ufs_read(uint32_t part, uint64_t offset, uint8_t *dst, uint64_t len)return ufs_read_mailbox(part, offset, dst, len);}-static uint64_t-ufs_part_bytes(uint32_t part)+/*+ * Whole blocks [offset, offset + len) of the one partition that takes writes, from src, through+ * the loader. This kernel's own driver never writes -- its whitelist of commands has no write in+ * it -- so for the length of a write its transfer list goes out and UEFI's driver, which the+ * loader calls, has the controller again; reads meanwhile go through the loader too (ufs_read),+ * which is slower and right. Nothing of iOS waits on this but the writer itself: the data volume+ * is in memory (ramfs) and only its copy for the next boot is written here.+ */+static int+ufs_write(uint32_t part, uint64_t offset, const uint8_t *src, uint64_t len){- return ufs_page->part[part].blocks * ufs_page->part[part].block_size;+ uint64_t nbs, per;+ bool native;+ int error = 0;++ if (!ufs_ready || ufs_write_part < 0 || part != (uint32_t)ufs_write_part) {+ ufs_write_refused++;+ return EROFS;+ }+ nbs = ufs_page->part[part].block_size;+ if (len == 0 || offset % nbs != 0 || len % nbs != 0) {+ return EINVAL; /* whole blocks only: nothing is read before a write */+ }+ if (offset / nbs < UFS_WRITE_FIRST || offset > ufs_part_bytes(part) ||+ len > ufs_part_bytes(part) - offset) {+ /* the iOS volume, the cryptexes, and anything past the partition's end: never */+ ufs_write_refused++;+ return EROFS;+ }+ per = ufs_buffer_size / nbs;+ lck_mtx_lock(&ufs_write_mtx);+ /*+ * ufs_native_on goes first, so that a reader about to start sees it and goes the other way+ * of its own accord; one already inside this kernel's driver gets an error and is sent the+ * other way by ufs_write_busy.+ */+ native = ufs_native_on;+ ufs_write_busy = true;+ if (native) {+ ufs_native_on = false;+ ufs_native_out("a write of the data volume");+ }+ while (len > 0 && error == 0) {+ uint64_t count = len / nbs;++ if (count > per) {+ count = per;+ }+ lck_mtx_lock(&ufs_lock);+ if (ufshc_list_in()) {+ /* it did not go out after all: UEFI's driver must not be asked to ring */+ error = EIO;+ } else {+ error = ufs_request(BLOCK_OP_WRITE, part, offset / nbs, count, NULL, src);+ }+ lck_mtx_unlock(&ufs_lock);+ offset += count * nbs;+ src += count * nbs;+ len -= count * nbs;+ }+ if (native && ufs_native_in(false) == 0) {+ ufs_native_on = true;+ }+ ufs_write_busy = false;+ lck_mtx_unlock(&ufs_write_mtx);+ if (error != 0) {+ ufs_write_errors++;+ }+ return error;}/* ---- the devices ---- */@@ -389,11 +525,21 @@ ufs_open(dev_t dev, int flags, __unused int devtype, __unused proc_t p)return ENXIO;}if (flags & FWRITE) {- return EROFS;+ return EROFS; /* a block device of this driver never writes */}return 0;}+/* The same, for the character devices: the one partition that takes writes may be opened for them. */+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) {+ flags &= ~FWRITE;+ }+ return ufs_open(dev, flags, devtype, p);+}+static intufs_close(__unused dev_t dev, __unused int flags, __unused int devtype, __unused proc_t p){@@ -469,7 +615,7 @@ ufs_ioctl(dev_t dev, u_long cmd, caddr_t data, __unused int fflag, __unused proc*(uint64_t *)data = ufs_part_bytes(part) / ufs_bsize[part];return 0;case DKIOCISWRITABLE:- *(uint32_t *)data = 0;+ *(uint32_t *)data = 0; /* the block device, which is what a filesystem would ask */return 0;case DKIOCGETMAXBLOCKCOUNTREAD:*(uint64_t *)data = ufs_buffer_size / ufs_bsize[part];@@ -494,6 +640,51 @@ ufs_cread(dev_t dev, struct uio *uio, __unused int ioflag)ufs_bsize[minor(dev) < BLOCK_MAX_PARTS ? minor(dev) : 0]);}+/*+ * A write to /dev/rufs_darwin: whole blocks, out of user memory a bufferful at a time and into the+ * partition. Nothing of this driver is held while user memory is touched -- a fault there can come+ * back as a read of the storage -- so the bytes are taken first and written after.+ */+static int+ufs_cwrite(dev_t dev, struct uio *uio, __unused int ioflag)+{+ uint32_t part = (uint32_t)minor(dev);+ uint64_t nbs;+ uint8_t *bounce;+ int error = 0;++ if (!ufs_ready || part >= ufs_parts || ufs_write_part < 0 || part != (uint32_t)ufs_write_part) {+ return EROFS;+ }+ nbs = ufs_page->part[part].block_size;+ if (uio_offset(uio) < 0 || (uint64_t)uio_offset(uio) % nbs != 0 || (uint64_t)uio_resid(uio) % nbs != 0) {+ return EINVAL;+ }+ bounce = kalloc_data(ufs_buffer_size, Z_WAITOK);+ if (bounce == NULL) {+ return ENOMEM;+ }+ while (error == 0 && uio_resid(uio) > 0) {+ user_ssize_t want = uio_resid(uio);+ off_t at = uio_offset(uio);++ if (want > (user_ssize_t)ufs_buffer_size) {+ want = (user_ssize_t)ufs_buffer_size;+ }+ error = uiomove((caddr_t)bounce, (int)want, uio);+ if (error == 0) {+ error = ufs_write(part, (uint64_t)at, bounce, (uint64_t)want);+ if (error != 0) {+ /* these bytes did not go in: the write is to say so, not to count them */+ uio_setoffset(uio, at);+ uio_setresid(uio, uio_resid(uio) + want);+ }+ }+ }+ kfree_data(bounce, ufs_buffer_size);+ return error;+}+static const struct bdevsw ufs_bdevsw = {.d_open = ufs_open,.d_close = ufs_close,@@ -505,10 +696,10 @@ static const struct bdevsw ufs_bdevsw = {};static const struct cdevsw ufs_cdevsw = {- .d_open = ufs_open,+ .d_open = ufs_copen,.d_close = ufs_close,.d_read = ufs_cread,- .d_write = eno_rdwrt,+ .d_write = ufs_cwrite,.d_ioctl = ufs_ioctl,.d_stop = eno_stop,.d_reset = eno_reset,@@ -1089,6 +1280,45 @@ redwood_ufs_init(void)printf("ufs: %u partitions of the phone's storage as /dev/ufs_NAME and /dev/rufs_NAME, read only, ""through the loader's page at %llx and its %u KB buffer\n", made, pa, ufs_buffer_size >> 10);+ /*+ * ufs_write=1: /dev/rufs_darwin takes writes, past the iOS volume and the cryptexes. darwin+ * has to be the partition the port was set up with, in everything the loader's table says of+ * it; the loader looks at its GUIDs besides, and refuses the same bounds of its own accord.+ */+ check = 0;+ if (PE_parse_boot_argn("ufs_write", &check, sizeof(check)) && check == 1) {+ for (uint32_t i = 0; i < ufs_parts; i++) {+ const char *why = NULL;++ if (strncmp((const char *)ufs_page->part[i].name, "darwin", 7) != 0) {+ continue;+ }+ if (ufs_page->part[i].flags & BLOCK_PART_UNIT) {+ why = "it is a whole logical unit, not the partition";+ } else if (ufs_page->part[i].lun != 0) {+ why = "it is not on logical unit 0";+ } else if (ufs_page->part[i].start_lba != UFS_DARWIN_FIRST) {+ why = "it does not begin where it did";+ } else if (ufs_page->part[i].blocks != UFS_DARWIN_BLOCKS) {+ why = "it is not the size it was";+ } else if (ufs_page->part[i].block_size != 4096) {+ why = "its blocks are not 4096 bytes";+ }+ if (why != NULL) {+ printf("ufs: darwin: no write will be asked for: %s\n", why);+ continue;+ }+ ufs_write_part = (int)i;+ }+ if (ufs_write_part >= 0) {+ 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);+ } else {+ printf("ufs: nothing takes writes: darwin is not among the partitions\n");+ }+ }+/** ufs_native=1: through this kernel's own driver instead, its own transfer list in the* controller for the run, if all is as expected; 2: only look; 3: a dry run -- the list in anddiff --git a/xnuloader/loader.c b/xnuloader/loader.cindex a0448dc..867f11a 100644--- a/xnuloader/loader.c+++ b/xnuloader/loader.c@@ -1447,21 +1447,41 @@ ufs_survey(void)* with that driver's ReadBlocks into a buffer both can see. The kernel's side is* bsd/dev/redwood_ufs.c; it gets the page's address as ufs_shared_pa= on its command line.*- * Read only, by construction: this service calls read_blocks and nothing else, the kernel's side- * refuses writes before they are asked for, and there is no field in the page that could ask for- * one.+ * Reads, and -- with ufs_write=1 on the command line -- writes into one partition and nowhere+ * else: the port's own darwin, proved by its GPT entry (name, both GUIDs, first and last LBA, its+ * logical unit), and in it only past everything the build writes from the computer, so that no+ * answer to the kernel can reach Android's partitions, the iOS volume or the cryptexes. The+ * refusals are here, in the kernel (bsd/dev/redwood_ufs.c) and in UEFI's own driver, which gives+ * out a partition's BlockIo and bounds it: a request has to pass all three.** Each side writes its own cache lines only: the kernel the request, core 0 the answer (and the* table, once, before the kernel starts). A line one side cleans out of its cache therefore never- * carries a stale copy of what the other side wrote into it. Core 0 never writes into the data- * buffer itself, so after the controller's DMA there is nothing of its own in its cache to write- * back over the data.+ * carries a stale copy of what the other side wrote into it. Into the data buffer core 0 writes+ * only what it has read back after a write of its own, so after the controller's DMA there is+ * nothing of its own in its cache to write back over the data; the kernel, which fills the buffer+ * for a write, cleans its own lines of it before it asks.*/#define BLOCK_MAGIC 0x4b434f4c42574452ULL /* "RDWBLOCK" */-#define BLOCK_VERSION 3+#define BLOCK_VERSION 4#define BLOCK_MAX_PARTS 160#define BLOCK_MAX_UNITS 16#define BLOCK_PART_UNIT 1u /* the entry is a whole logical unit, not a partition */+#define BLOCK_OP_READ 0u+#define BLOCK_OP_WRITE 1u++/*+ * The one partition a write may reach, and from which of its blocks. darwin is the port's own+ * 20 GiB at the end of logical unit 0 (README, "The port's partition"); what the build puts there+ * from the computer (tools/darwin-write.py) is the iOS volume at its start and the two cryptexes+ * behind it, and nothing of this port reads or writes the rest -- so the data of iOS lives there+ * and a write is refused before that block. The identity is the whole of darwin-write.py's: the+ * same partition, named the same, of the same size, in the same place, on logical unit 0.+ */+#define BLOCK_WRITE_FIRST 2042368ULL /* darwin's blocks of 4096: past both cryptexes */+#define BLOCK_DARWIN_FIRST 25374720ULL /* its first and last LBA of unit 0, as the GPT has them */+#define BLOCK_DARWIN_LAST 30617594ULL+#define BLOCK_DARWIN_TYPE { 0x7c3457ef, 0x0000, 0x11aa, { 0xaa, 0x11, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac } }+#define BLOCK_DARWIN_UNIQ { 0xfd6854ad, 0x24aa, 0x4fc3, { 0x94, 0xfe, 0x29, 0x01, 0x16, 0x85, 0x8d, 0x68 } }/** The partitions first, then the logical units they are on (BLOCK_PART_UNIT, named unit0, unit1,@@ -1491,6 +1511,8 @@ struct loader_block {volatile uint32_t request;uint32_t part;uint64_t lba, count; /* in the partition's own blocks */+ uint32_t op; /* BLOCK_OP_READ, BLOCK_OP_WRITE */+ uint32_t reserved;} ask __attribute__((aligned(128)));/* written by the loader: answered becomes request when the data is in the buffer */struct {@@ -1529,6 +1551,9 @@ static void block_io2_probe(efi_handle h, efi_block_io *bio);static int block_probe_requested; /* ufs_probe=1: block_io2_probe() at block_start() */static efi_block_io *block_bios[BLOCK_MAX_PARTS];static int block_enabled;+static int block_write_requested; /* ufs_write=1 on the command line */+static int block_write_part = -1; /* the only partition a write may reach; -1: none, writes refused */+static uint64_t block_writes, block_writes_refused, block_write_bytes;/* the bytes of a device path before its end node */static uint64_t@@ -1774,6 +1799,45 @@ block_tables(void)}}+/*+ * Is this partition the one a write may reach? Everything about it has to be what it was when the+ * port was set up; one field out of place and nothing is written this run. The name UEFI reports+ * is already in bp->name (ufs_name), in ASCII.+ */+static int+block_is_darwin(const efi_partition_info *info, const efi_block_io *bio, const struct loader_block_part *bp)+{+ static const efi_guid type = BLOCK_DARWIN_TYPE, uniq = BLOCK_DARWIN_UNIQ;+ static const char name[] = "darwin";+ const char *why = NULL;++ for (unsigned i = 0; i < sizeof(name); i++) {+ if (bp->name[i] != name[i]) {+ return 0; /* another partition: nothing to say about it */+ }+ }+ if (memcmp(&info->type_guid, &type, sizeof(type)) != 0) {+ why = "its type GUID is not the one it had";+ } else if (memcmp(&info->unique_guid, &uniq, sizeof(uniq)) != 0) {+ why = "its unique GUID is not the one it had";+ } else if (info->start_lba != BLOCK_DARWIN_FIRST || info->end_lba != BLOCK_DARWIN_LAST) {+ why = "it does not begin and end where it did";+ } else if (bp->lun != 0) {+ why = "it is not on logical unit 0";+ } else if (bp->block_size != 4096 || bp->blocks != BLOCK_DARWIN_LAST - BLOCK_DARWIN_FIRST + 1) {+ why = "its blocks are not what they were";+ } else if (bio->media->read_only) {+ why = "UEFI gives it out read only";+ }+ if (why != NULL) {+ print("ufs: darwin: no write will be served: %s\n", why);+ return 0;+ }+ print("ufs: darwin (LBA %d..%d of unit 0): its blocks from %d on may be written this run\n",+ info->start_lba, info->end_lba, BLOCK_WRITE_FIRST);+ return 1;+}+static voidblock_start(void){@@ -1844,6 +1908,9 @@ block_start(void)}}}+ if (block_write_requested && block_is_darwin(info, bio, bp)) {+ block_write_part = (int)block_shared.parts;+ }block_bios[block_shared.parts++] = bio;if (block_probe_requested && bp->name[0] == 'd' && bp->name[1] == 'a' && bp->name[2] == 'r' &&bp->name[3] == 'w' && bp->name[4] == 'i' && bp->name[5] == 'n' && bp->name[6] == 0) {@@ -1981,7 +2048,7 @@ static voidblock_poll(void){uint64_t ask = (uint64_t)(uintptr_t)&block_shared.ask;- uint32_t request, part;+ uint32_t request, part, op;uint64_t lba, count, bytes, started;efi_status s = 0x8000000000000002ULL; /* EFI_INVALID_PARAMETER: a request out of bounds */@@ -1994,6 +2061,7 @@ block_poll(void)part = block_shared.ask.part;lba = block_shared.ask.lba;count = block_shared.ask.count;+ op = block_shared.ask.op;started = READ_SYSREG(CNTVCT_EL0);bytes = 0;if (part < block_shared.parts && count != 0 && count <= BLOCK_BUFFER_SIZE / block_shared.part[part].block_size &&@@ -2008,11 +2076,51 @@ block_poll(void)}s = 0x800000000000000FULL; /* EFI_ACCESS_DENIED */bytes = 0;- } else {+ } else if (op == BLOCK_OP_WRITE) {+ if (block_write_part < 0 || part != (uint32_t)block_write_part || lba < BLOCK_WRITE_FIRST) {+ /* not darwin, or not past what the build writes from the computer: never */+ if (block_writes_refused++ < 8) {+ print("ufs: a write of %d blocks from %d of %s: REFUSED\n", count, lba,+ block_shared.part[part].name);+ }+ s = 0x800000000000000FULL; /* EFI_ACCESS_DENIED */+ bytes = 0;+ } else {+ /*+ * The kernel filled the buffer and cleaned its lines of it; drop whatever this+ * core holds, write, and read back -- the answer says the blocks hold what the+ * kernel gave, or the write is no good. The read back overwrites the buffer,+ * which the kernel does not need: it has the data.+ */+ uint32_t want;++ flush_dcache((uint64_t)(uintptr_t)block_buffer,+ (uint64_t)(uintptr_t)block_buffer + bytes);+ want = ufs_crc32(block_buffer, bytes);+ s = bio->write_blocks(bio, bio->media->media_id, lba, bytes, block_buffer);+ if (!EFI_ERROR(s)) {+ bio->flush_blocks(bio);+ s = bio->read_blocks(bio, bio->media->media_id, lba, bytes, block_buffer);+ flush_dcache((uint64_t)(uintptr_t)block_buffer,+ (uint64_t)(uintptr_t)block_buffer + bytes);+ if (!EFI_ERROR(s) && ufs_crc32(block_buffer, bytes) != want) {+ print("ufs: %d blocks from %d of darwin read back DIFFERENT\n",+ count, lba);+ s = 0x800000000000000AULL; /* EFI_VOLUME_CORRUPTED */+ }+ }+ if (!EFI_ERROR(s)) {+ block_writes++;+ block_write_bytes += bytes;+ }+ }+ } else if (op == BLOCK_OP_READ) {s = bio->read_blocks(bio, bio->media->media_id, lba, bytes, block_buffer);+ /* what the controller wrote is in memory; drop whatever this core held of the buffer */+ flush_dcache((uint64_t)(uintptr_t)block_buffer, (uint64_t)(uintptr_t)block_buffer + bytes);+ } else {+ bytes = 0; /* an operation this loader does not know: nothing is touched */}- /* what the controller wrote is in memory; drop whatever this core held of the buffer */- flush_dcache((uint64_t)(uintptr_t)block_buffer, (uint64_t)(uintptr_t)block_buffer + bytes);}block_shared.answer.status = (int64_t)s;block_shared.answer.ticks += READ_SYSREG(CNTVCT_EL0) - started;@@ -2728,6 +2836,8 @@ run_kernel(int have_checkpoint, uint64_t checkpoint)ufs_requested = c[4] - '0';}block_probe_requested |= starts_with(c, "ufs_probe=1") && (c == pi.cmdline || c[-1] == ' ');+ block_write_requested |= starts_with(c, "ufs_write=1") && (c == pi.cmdline || c[-1] == ' ') &&+ (c[11] == 0 || c[11] == ' ');hc_requested |= starts_with(c, "ufs_native=") && (c == pi.cmdline || c[-1] == ' ') && c[11] >= '1' &&c[11] <= '9';}@@ -3196,6 +3306,10 @@ run_kernel(int have_checkpoint, uint64_t checkpoint)print("ufs: the kernel put its own list in %d times and took it out %d times\n",(uint64_t)block_shared.kernel_native.switched_in, (uint64_t)block_shared.kernel_native.switched_out);}+ if (block_write_requested) {+ print("ufs: %d writes into darwin, %d bytes, read back the same; %d refused\n", block_writes,+ block_write_bytes, block_writes_refused);+ }/* ... and its other cores should all be off again: the next kernel cannot start one that is not */for (int i = 1; i < cpu_count; i++) {int64_t state = psci_call(PSCI_AFFINITY_INFO, cpu_mpidr[i], 0, 0);