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 drivers find their firmware in an APFS image too; the phone boots off the flash with APFS alone in RAM

vcvkk9d26b1b6a697
diff --git a/tools/apfs/mkapfs.py b/tools/apfs/mkapfs.py
index d729188..e9b404b 100755
--- a/tools/apfs/mkapfs.py
+++ b/tools/apfs/mkapfs.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""Build an APFS container from directories: the port's own read-only volumes, as images.
-usage: mkapfs.py -o IMAGE [-z] [--time SECONDS] [--slack BLOCKS] NAME=DIR[,manifest=FILE] [NAME=DIR ...]
+usage: mkapfs.py -o IMAGE [-z [--plain DIR]...] [--time SECONDS] [--slack BLOCKS] NAME=DIR[,manifest=FILE] [NAME=DIR ...]
Each NAME=DIR becomes one volume of the container (volume 0 first), case-sensitive and
normalization-insensitive like the iOS volumes, unencrypted, read-only content laid out once.
@@ -10,7 +10,8 @@ unless a manifest says otherwise (the same lines as mkpackfs.py -m: mode in octa
type bits, uid, gid, path, [link target]; a directory or symbolic link that is only in the
manifest is made). With -z a regular file is stored compressed when that makes it smaller:
zlib in 64 KB pieces in its resource fork (decmpfs type 4), as Apple stores most of the iOS
-root volume. Times are all --time (default: 2023-05-12, the iOS 16.5 images'), so the same
+root volume; but not what is under a --plain DIR (relative to the volume's root), which stays as
+it is and in one piece -- the firmware a driver uses where it lies in memory. Times are all --time (default: 2023-05-12, the iOS 16.5 images'), so the same
input gives the same image byte for byte.
The image is what Apple's newfs_apfs and a single transaction would leave (one checkpoint,
@@ -285,8 +286,9 @@ def xfields(fields):
class Volume:
- def __init__(self, index, name, root, compress, when, spool=None):
+ def __init__(self, index, name, root, compress, when, spool=None, plain=()):
self.spool = spool # where compressed resource forks wait to be written (a file)
+ self.plain = tuple(d.strip('/') + '/' for d in plain) # never compressed below these
self.index = index
self.name = name
self.root = root
@@ -307,14 +309,15 @@ class Volume:
self.items = [] # (node, parent oid)
self.root.oid = ROOT_DIR_INO
- def walk(node, parent):
+ def walk(node, parent, path):
for name in sorted(node.children):
child = node.children[name]
child.oid = self.new_oid()
+ child.path = path + name
self.items.append((child, node.oid))
if stat.S_ISDIR(child.mode):
- walk(child, child.oid)
- walk(self.root, ROOT_DIR_INO)
+ walk(child, child.oid, child.path + '/')
+ walk(self.root, ROOT_DIR_INO, '')
# private id -> where its bytes are: ('file', path, size) or ('spool', offset, size); never in memory
self.data = {}
self.decmpfs = {} # file oid -> (header, rsrc stream id)
@@ -325,11 +328,12 @@ class Volume:
if self.compress:
# compressed by as many processes as there are processors, taken in the files' order
pool = concurrent.futures.ProcessPoolExecutor()
- forks = pool.map(compressed_fork, [n.source for n in files if n.size > 0], chunksize=16)
+ forks = pool.map(compressed_fork, [n.source for n in files if n.size > 0 and not self.is_plain(n)],
+ chunksize=16)
for node in files:
if node.size == 0:
continue
- if self.compress:
+ if self.compress and not self.is_plain(node):
rsrc = next(forks)
if rsrc is not None:
sid = self.new_oid()
@@ -342,6 +346,9 @@ class Volume:
if self.compress:
pool.shutdown()
+ def is_plain(self, node):
+ return any(node.path.startswith(p) for p in self.plain)
+
def place(self, first_block):
"""Data in order of private id, each stream in one extent. Returns the next free block."""
self.extents = {}
@@ -879,6 +886,7 @@ def main():
ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
ap.add_argument('-o', '--output', required=True)
ap.add_argument('-z', '--compress', action='store_true', help='zlib in the resource fork where it saves space')
+ ap.add_argument('--plain', action='append', default=[], help='with -z: never compress what is under this directory')
ap.add_argument('--time', type=int, default=1683900000, help='every timestamp, in seconds')
ap.add_argument('--slack', type=int, default=256, help='free blocks to leave (at least)')
ap.add_argument('volumes', nargs='+', help='NAME=DIR[,manifest=FILE]')
@@ -893,7 +901,7 @@ def main():
k, _, val = opt.partition('=')
if k == 'manifest':
apply_manifest(root, val)
- vols.append(Volume(i, name, root, args.compress, args.time * 1000000000, spool))
+ vols.append(Volume(i, name, root, args.compress, args.time * 1000000000, spool, args.plain))
c = Container(vols, args.slack, os.path.basename(args.output))
c.layout()
free = c.write(args.output)
diff --git a/userland/build.sh b/userland/build.sh
index 8e304e9..1baae42 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -276,8 +276,9 @@ done
[ -d root/lib/RedwoodAudio.driver ] && ln -s /redwood/lib/RedwoodAudio.driver root/hal/RedwoodAudio.driver
../mkpackfs.py -o root.packfs root
# ... and the same tree as an APFS container (tools/apfs/mkapfs.py), what takes the place of packfs
-# (stage0 mounts whichever of the two travels with the kernel: XL_RAMDISK=userland/root.apfs)
-python3 ../tools/apfs/mkapfs.py -z -o root.apfs redwood=root | tail -1
+# (stage0 mounts whichever of the two travels with the kernel: XL_RAMDISK=userland/root.apfs); fw/
+# as it is, which the kernel's drivers use where it lies in memory (bsd/dev/redwood_image.c)
+python3 ../tools/apfs/mkapfs.py -z --plain fw -o root.apfs redwood=root | tail -1
# --- the RAM part of a boot off the flash: FLASHBOOT=1 -> flashboot.packfs ------------------------------
# With the iOS volume on the phone's flash as the root, all that has to come from RAM is what is not
@@ -321,7 +322,7 @@ if [ -n "$FLASHBOOT" ] && [ -f $CACHE/dyld_shared_cache_arm64 ]; then
cp -al flashboot flashboot.boot
rm -rf flashboot.boot/Cryptexes/OS
mkdir -p flashboot.boot/Cryptexes/OS
- python3 ../tools/apfs/mkapfs.py -z -o flashboot.boot.apfs Boot=flashboot.boot | tail -1
+ python3 ../tools/apfs/mkapfs.py -z --plain fw -o flashboot.boot.apfs Boot=flashboot.boot | tail -1
if [ -n "$CRYPTEX_IMAGE" ]; then
cat flashboot.boot.apfs "$CRYPTEX_IMAGE" > flashboot.apfs
else
diff --git a/userland/rc.jobs.flashcheck b/userland/rc.jobs.flashcheck
new file mode 100644
index 0000000..ab15b4a
--- /dev/null
+++ b/userland/rc.jobs.flashcheck
@@ -0,0 +1,13 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# The iOS root volume on the phone's flash as the root, everything in RAM as APFS (flashboot.apfs resident,
+# root.apfs with the kernel; root_apfs=ufs_darwin ufs=2 ufs_native=1 adsp=1 sound=4 rec_sound=12 wifi=1):
+# what is mounted from where, the firmware on /fw, something to hear (a tone of three seconds, the lock
+# sound, a click, a fanfare: ./xl sound), the network over the phone's Wi-Fi (an address by DHCP, a name,
+# a page over HTTPS) -- what packfs did, done with APFS alone.
+/sbin/mount
+/redwood/bin/vol_ls fs / /redwood /private/preboot /private/preboot/Cryptexes/OS
+/redwood/bin/vol_ls ls /fw
+/redwood/bin/net_test show dhcp en0 waitaddr en0 60 store
+/redwood/bin/net_test resolve example.com fetch https://example.com/ fetch https://www.apple.com/library/test/success.html
+/redwood/bin/sound_test springboard wait 5 tone 3 wait 3 system 1100 wait 3 system 1104 wait 3 file /System/Library/Audio/UISounds/New/Fanfare.caf
+/bin/ps -A
diff --git a/xnu/files/bsd/dev/redwood_adreno.c b/xnu/files/bsd/dev/redwood_adreno.c
index 644d319..bd7c608 100644
--- a/xnu/files/bsd/dev/redwood_adreno.c
+++ b/xnu/files/bsd/dev/redwood_adreno.c
@@ -1599,26 +1599,6 @@ step_trustzone(void)
extern dev_t mdevlookup(int devid);
extern int mdevgetkernelrange(dev_t dev, vm_offset_t *base, uint64_t *size);
-/* An entry of a directory of a packfs image in memory, by name. */
-static const struct packfs_dnode *
-image_entry(const uint8_t *image, const struct packfs_super *super, const struct packfs_dnode *directory, const char *name)
-{
- const struct packfs_dnode *nodes = (const struct packfs_dnode *)(image + super->ps_nodes_off);
- size_t length = strlen(name);
-
- if (directory == NULL || !S_ISDIR(directory->pd_mode) || directory->pd_data + directory->pd_size > super->ps_node_count) {
- return NULL;
- }
- for (uint64_t i = 0; i < directory->pd_size; i++) {
- const struct packfs_dnode *node = &nodes[directory->pd_data + i];
-
- if (node->pd_name_len == length && (uint64_t)node->pd_name + length <= super->ps_strings_size &&
- memcmp(image + super->ps_strings_off + node->pd_name, name, length) == 0) {
- return node;
- }
- }
- return NULL;
-}
/*
* A file of the small image that travels with the kernel -- userland/build.sh puts what is in
@@ -1628,38 +1608,18 @@ image_entry(const uint8_t *image, const struct packfs_super *super, const struct
*/
static bool image_asked_quietly; /* image_file() says nothing: is the file there at all */
+extern const uint8_t *redwood_image_file(const char *dir, const char *name, uint64_t *size, int *disk_out);
static const uint8_t *
image_file(const char *directory, const char *name, uint64_t *size)
{
- for (int disk = 1; disk >= 0; disk--) {
- const struct packfs_super *super;
- const struct packfs_dnode *file;
- vm_offset_t base = 0;
- uint64_t length = 0;
- dev_t dev = mdevlookup(disk);
+ int disk = -1;
+ const uint8_t *data = redwood_image_file(directory, name, size, &disk); /* (redwood_image.c: APFS or packfs) */
- if (dev == (dev_t)-1 || mdevgetkernelrange(dev, &base, &length) != 0 || length < PACKFS_SUPER_SIZE) {
- continue;
- }
- super = (const struct packfs_super *)base;
- if (memcmp(super->ps_magic, PACKFS_MAGIC, sizeof(super->ps_magic)) != 0 || super->ps_image_size > length ||
- super->ps_node_count == 0 ||
- super->ps_nodes_off + (uint64_t)super->ps_node_count * sizeof(struct packfs_dnode) > super->ps_image_size ||
- super->ps_strings_off + super->ps_strings_size > super->ps_image_size) {
- continue;
- }
- file = (const struct packfs_dnode *)(base + super->ps_nodes_off); /* the root directory */
- file = image_entry((const uint8_t *)base, super, file, directory);
- file = image_entry((const uint8_t *)base, super, file, name);
- if (file != NULL && S_ISREG(file->pd_mode) && !(file->pd_flags & PACKFS_F_CHUNKED) &&
- file->pd_data + file->pd_size <= super->ps_image_size) {
- if (!image_asked_quietly) {
- printf("adreno: %u: %s/%s: %llu bytes in the image of /dev/md%d\n", step, directory, name, file->pd_size,
- disk);
- }
- *size = file->pd_size;
- return (const uint8_t *)base + file->pd_data;
+ if (data != NULL) {
+ if (!image_asked_quietly) {
+ printf("adreno: %u: %s/%s: %llu bytes in the image of /dev/md%d\n", step, directory, name, *size, disk);
}
+ return data;
}
if (!image_asked_quietly) {
printf("adreno: %u: no %s/%s in the small image: put the phone's /vendor/firmware/%s into fw-gpu/ and run "
diff --git a/xnu/files/bsd/dev/redwood_adsp.c b/xnu/files/bsd/dev/redwood_adsp.c
index 43a4361..7ff9829 100644
--- a/xnu/files/bsd/dev/redwood_adsp.c
+++ b/xnu/files/bsd/dev/redwood_adsp.c
@@ -966,26 +966,6 @@ step_survey(void)
#define ADSP_MEMORY_SIZE 0x04000000u
#define PAS_ADSP 1 /* dt: qcom,pas-id */
-/* An entry of a directory of a packfs image in memory, by name (as in redwood_wifi.c). */
-static const struct packfs_dnode *
-image_entry(const uint8_t *image, const struct packfs_super *super, const struct packfs_dnode *directory, const char *name)
-{
- const struct packfs_dnode *nodes = (const struct packfs_dnode *)(image + super->ps_nodes_off);
- size_t length = strlen(name);
-
- if (directory == NULL || !S_ISDIR(directory->pd_mode) || directory->pd_data + directory->pd_size > super->ps_node_count) {
- return NULL;
- }
- for (uint64_t i = 0; i < directory->pd_size; i++) {
- const struct packfs_dnode *node = &nodes[directory->pd_data + i];
-
- if (node->pd_name_len == length && (uint64_t)node->pd_name + length <= super->ps_strings_size &&
- memcmp(image + super->ps_strings_off + node->pd_name, name, length) == 0) {
- return node;
- }
- }
- return NULL;
-}
/*
* A file of /fw on the root file system, read whole into memory of ours: the system image, where
@@ -1052,33 +1032,15 @@ redwood_adsp_fw_free(const uint8_t *data, uint64_t size)
}
/* A file of fw/ in the small image (ADSP=1 userland/build.sh puts fw-audio/ there), else of /fw on the root (above). */
+extern const uint8_t *redwood_image_file(const char *dir, const char *name, uint64_t *size, int *disk_out);
static const uint8_t *
image_file(const char *name, uint64_t *size)
{
- for (int disk = 1; disk >= 0; disk--) {
- const struct packfs_super *super;
- const struct packfs_dnode *file;
- vm_offset_t base = 0;
- uint64_t length = 0;
- dev_t dev = mdevlookup(disk);
+ {
+ const uint8_t *data = redwood_image_file("fw", name, size, NULL); /* (redwood_image.c: APFS or packfs) */
- if (dev == (dev_t)-1 || mdevgetkernelrange(dev, &base, &length) != 0 || length < PACKFS_SUPER_SIZE) {
- continue;
- }
- super = (const struct packfs_super *)base;
- if (memcmp(super->ps_magic, PACKFS_MAGIC, sizeof(super->ps_magic)) != 0 || super->ps_image_size > length ||
- super->ps_node_count == 0 ||
- super->ps_nodes_off + (uint64_t)super->ps_node_count * sizeof(struct packfs_dnode) > super->ps_image_size ||
- super->ps_strings_off + super->ps_strings_size > super->ps_image_size) {
- continue;
- }
- file = (const struct packfs_dnode *)(base + super->ps_nodes_off);
- file = image_entry((const uint8_t *)base, super, file, "fw");
- file = image_entry((const uint8_t *)base, super, file, name);
- if (file != NULL && S_ISREG(file->pd_mode) && !(file->pd_flags & PACKFS_F_CHUNKED) &&
- file->pd_data + file->pd_size <= super->ps_image_size) {
- *size = file->pd_size;
- return (const uint8_t *)base + file->pd_data;
+ if (data != NULL) {
+ return data;
}
}
{
diff --git a/xnu/files/bsd/dev/redwood_image.c b/xnu/files/bsd/dev/redwood_image.c
new file mode 100644
index 0000000..b67c414
--- /dev/null
+++ b/xnu/files/bsd/dev/redwood_image.c
@@ -0,0 +1,237 @@
+/*
+ * A file of one of this port's RAM disks, by path, for the drivers that want one before there is
+ * a root or without asking it: the GPU's firmware (redwood_adreno.c), the Wi-Fi DSP's
+ * (redwood_wifi.c), the audio DSP's (redwood_adsp.c) -- "fw/NAME" of the image that travels with
+ * the kernel (md1; md0 when there is nothing else) or of the resident one (md0).
+ *
+ * The image is APFS (tools/apfs/mkapfs.py: what userland/build.sh makes) or, until packfs is
+ * gone, packfs. A file stored as it is in one piece is used where it lies in the RAM disk's
+ * memory, as the drivers always did (build.sh has mkapfs.py store fw/ that way: --plain fw).
+ * Any other APFS file is read out once by apfs_lib -- the same code as the volume's when
+ * mounted, compressed or not -- into memory of its own, and kept: firmware is asked for once
+ * or twice a session and its driver keeps it while the phone runs, so nothing gives it back.
+ */
+#include <kern/kalloc.h>
+#include <kern/locks.h>
+#include <libkern/libkern.h>
+#include <sys/errno.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/systm.h>
+#include <miscfs/apfs/apfs_lib.h>
+#include <miscfs/packfs/packfs.h>
+
+extern dev_t mdevlookup(int devid);
+extern int mdevgetkernelrange(dev_t dev, vm_offset_t *base, uint64_t *size);
+
+const uint8_t *redwood_image_file(const char *dir, const char *name, uint64_t *size, int *disk_out);
+
+/* ---- packfs ---- */
+
+static const struct packfs_dnode *
+pack_entry(const uint8_t *image, const struct packfs_super *super, const struct packfs_dnode *directory,
+ const char *name)
+{
+ const struct packfs_dnode *nodes = (const struct packfs_dnode *)(image + super->ps_nodes_off);
+ size_t length = strlen(name);
+
+ if (directory == NULL || !S_ISDIR(directory->pd_mode) || directory->pd_data + directory->pd_size > super->ps_node_count) {
+ return NULL;
+ }
+ for (uint64_t i = 0; i < directory->pd_size; i++) {
+ const struct packfs_dnode *node = &nodes[directory->pd_data + i];
+
+ if (node->pd_name_len == length && (uint64_t)node->pd_name + length <= super->ps_strings_size &&
+ memcmp(image + super->ps_strings_off + node->pd_name, name, length) == 0) {
+ return node;
+ }
+ }
+ return NULL;
+}
+
+static const uint8_t *
+pack_file(const uint8_t *base, uint64_t length, const char *dir, const char *name, uint64_t *size)
+{
+ const struct packfs_super *super = (const struct packfs_super *)base;
+ const struct packfs_dnode *file;
+
+ if (length < PACKFS_SUPER_SIZE || memcmp(super->ps_magic, PACKFS_MAGIC, sizeof(super->ps_magic)) != 0 ||
+ super->ps_image_size > length || super->ps_node_count == 0 ||
+ super->ps_nodes_off + (uint64_t)super->ps_node_count * sizeof(struct packfs_dnode) > super->ps_image_size ||
+ super->ps_strings_off + super->ps_strings_size > super->ps_image_size) {
+ return NULL;
+ }
+ file = (const struct packfs_dnode *)(base + super->ps_nodes_off); /* the root directory */
+ file = pack_entry(base, super, file, dir);
+ file = pack_entry(base, super, file, name);
+ if (file != NULL && S_ISREG(file->pd_mode) && !(file->pd_flags & PACKFS_F_CHUNKED) &&
+ file->pd_data + file->pd_size <= super->ps_image_size) {
+ *size = file->pd_size;
+ return base + file->pd_data;
+ }
+ return NULL;
+}
+
+/* ---- APFS ---- */
+
+struct mem_dev {
+ const uint8_t *base;
+ uint64_t length;
+};
+
+static int
+mem_read(void *ctx, uint64_t off, void *buf, size_t len)
+{
+ struct mem_dev *m = ctx;
+
+ if (off > m->length || len > m->length - off) {
+ return EIO;
+ }
+ memcpy(buf, m->base + off, len);
+ return 0;
+}
+
+static LCK_GRP_DECLARE(image_lck_grp, "redwood_image");
+static LCK_MTX_DECLARE(image_lock, &image_lck_grp); /* kept[]: the drivers start beside each other */
+static struct {
+ int disk;
+ char path[96];
+ uint8_t *data;
+ uint64_t size;
+} kept[64];
+
+static const uint8_t *
+apfs_file(int disk, const uint8_t *base, uint64_t length, const char *dir, const char *name, uint64_t *size)
+{
+ struct mem_dev m = { .base = base, .length = length };
+ struct apfs_dev dev = { .ctx = &m, .size = length, .read = mem_read, .read_data = mem_read };
+ struct apfs_nx *nx = NULL;
+ struct apfs_vol *vol = NULL;
+ struct apfs_inode ino;
+ struct apfs_cfile cf;
+ char path[96];
+ uint64_t id = 0, fsize = 0, off = 0;
+ uint8_t dt = 0, *data = NULL;
+ uint32_t i;
+ int error;
+
+ if (length < 4096 || memcmp(base + 32, "NXSB", 4) != 0) {
+ return NULL;
+ }
+ snprintf(path, sizeof(path), "/%s/%s", dir, name);
+ lck_mtx_lock(&image_lock);
+ for (i = 0; i < sizeof(kept) / sizeof(kept[0]) && kept[i].data != NULL; i++) {
+ if (kept[i].disk == disk && strcmp(kept[i].path, path) == 0) {
+ *size = kept[i].size;
+ lck_mtx_unlock(&image_lock);
+ return kept[i].data;
+ }
+ }
+ nx = kalloc_type(struct apfs_nx, Z_WAITOK | Z_ZERO);
+ vol = kalloc_type(struct apfs_vol, Z_WAITOK | Z_ZERO);
+ if (nx == NULL || vol == NULL) {
+ goto out;
+ }
+ if (apfs_nx_open(nx, &dev) != 0) {
+ kfree_type(struct apfs_nx, nx);
+ nx = NULL;
+ goto out;
+ }
+ error = ENOENT;
+ for (uint32_t v = 0; v < APFS_NX_MAX_FILE_SYSTEMS && error != 0; v++) {
+ if (nx->fs_oid[v] != 0 && apfs_vol_open(nx, v, vol) == 0) {
+ error = apfs_path_lookup(vol, path, &id, &dt);
+ }
+ }
+ if (error == 0) {
+ error = apfs_inode_get(vol, id, &ino);
+ }
+ if (error == 0 && (ino.mode & APFS_S_IFMT) != APFS_S_IFREG) {
+ error = EISDIR;
+ }
+ if (error == 0) {
+ error = apfs_inode_size(vol, &ino, &fsize);
+ }
+ if (error == 0 && (fsize == 0 || fsize > 256 * 1024 * 1024)) {
+ error = EFBIG;
+ }
+ if (error == 0) {
+ uint64_t addr = 0;
+
+ /* in one piece as it is: where it lies, no copy */
+ if (apfs_file_in_place(vol, &ino, &addr) == 0 && addr <= length / nx->block_size &&
+ fsize <= length - addr * nx->block_size) {
+ apfs_nx_close(nx);
+ *size = fsize;
+ data = __DECONST(uint8_t *, base) + addr * nx->block_size;
+ goto out;
+ }
+ if (i == sizeof(kept) / sizeof(kept[0])) {
+ printf("redwood_image: %s of /dev/md%d: kept %lu files already, no room for a copy of another\n", path, disk,
+ (unsigned long)i);
+ error = ENOSPC;
+ }
+ }
+ if (error == 0) {
+ data = kalloc_data((vm_size_t)fsize, Z_WAITOK);
+ error = data == NULL ? ENOMEM : 0;
+ }
+ memset(&cf, 0, sizeof(cf));
+ cf.held = APFS_NO_PIECE;
+ while (error == 0 && off < fsize) {
+ size_t got = 0;
+
+ error = apfs_read_file(vol, &ino, &cf, off, data + off, (size_t)(fsize - off), &got);
+ if (error == 0 && got == 0) {
+ error = EIO;
+ }
+ off += got;
+ }
+ apfs_cfile_fini(&cf);
+ apfs_nx_close(nx);
+ if (error == 0) {
+ kept[i].disk = disk;
+ strlcpy(kept[i].path, path, sizeof(kept[i].path));
+ kept[i].size = fsize;
+ kept[i].data = data; /* (last: a reader of the table sees all of it or none) */
+ *size = fsize;
+ } else if (data != NULL) {
+ kfree_data(data, (vm_size_t)fsize);
+ data = NULL;
+ }
+out:
+ lck_mtx_unlock(&image_lock);
+ if (vol != NULL) {
+ kfree_type(struct apfs_vol, vol);
+ }
+ if (nx != NULL) {
+ kfree_type(struct apfs_nx, nx);
+ }
+ return data;
+}
+
+const uint8_t *
+redwood_image_file(const char *dir, const char *name, uint64_t *size, int *disk_out)
+{
+ for (int disk = 1; disk >= 0; disk--) {
+ vm_offset_t base = 0;
+ uint64_t length = 0;
+ const uint8_t *data;
+ dev_t dev = mdevlookup(disk);
+
+ if (dev == (dev_t)-1 || mdevgetkernelrange(dev, &base, &length) != 0) {
+ continue;
+ }
+ data = pack_file((const uint8_t *)base, length, dir, name, size);
+ if (data == NULL) {
+ data = apfs_file(disk, (const uint8_t *)base, length, dir, name, size);
+ }
+ if (data != NULL) {
+ if (disk_out != NULL) {
+ *disk_out = disk;
+ }
+ return data;
+ }
+ }
+ return NULL;
+}
diff --git a/xnu/files/bsd/dev/redwood_wifi.c b/xnu/files/bsd/dev/redwood_wifi.c
index 66c50dc..7a83d8e 100644
--- a/xnu/files/bsd/dev/redwood_wifi.c
+++ b/xnu/files/bsd/dev/redwood_wifi.c
@@ -1265,59 +1265,19 @@ step_pins(void)
extern dev_t mdevlookup(int devid);
extern int mdevgetkernelrange(dev_t dev, vm_offset_t *base, uint64_t *size);
-/* An entry of a directory of a packfs image in memory, by name (as in redwood_adreno.c). */
-static const struct packfs_dnode *
-image_entry(const uint8_t *image, const struct packfs_super *super, const struct packfs_dnode *directory, const char *name)
-{
- const struct packfs_dnode *nodes = (const struct packfs_dnode *)(image + super->ps_nodes_off);
- size_t length = strlen(name);
-
- if (directory == NULL || !S_ISDIR(directory->pd_mode) || directory->pd_data + directory->pd_size > super->ps_node_count) {
- return NULL;
- }
- for (uint64_t i = 0; i < directory->pd_size; i++) {
- const struct packfs_dnode *node = &nodes[directory->pd_data + i];
-
- if (node->pd_name_len == length && (uint64_t)node->pd_name + length <= super->ps_strings_size &&
- memcmp(image + super->ps_strings_off + node->pd_name, name, length) == 0) {
- return node;
- }
- }
- return NULL;
-}
/*
* A file of fw/ in the small image that travels with the kernel -- userland/build.sh puts what
* is in fw-wifi/ there --, found in the memory of the RAM disk itself (see redwood_adreno.c).
*/
+extern const uint8_t *redwood_image_file(const char *dir, const char *name, uint64_t *size, int *disk_out);
static const uint8_t *
image_file(const char *name, uint64_t *size)
{
- for (int disk = 1; disk >= 0; disk--) {
- const struct packfs_super *super;
- const struct packfs_dnode *file;
- vm_offset_t base = 0;
- uint64_t length = 0;
- dev_t dev = mdevlookup(disk);
-
- if (dev == (dev_t)-1 || mdevgetkernelrange(dev, &base, &length) != 0 || length < PACKFS_SUPER_SIZE) {
- continue;
- }
- super = (const struct packfs_super *)base;
- if (memcmp(super->ps_magic, PACKFS_MAGIC, sizeof(super->ps_magic)) != 0 || super->ps_image_size > length ||
- super->ps_node_count == 0 ||
- super->ps_nodes_off + (uint64_t)super->ps_node_count * sizeof(struct packfs_dnode) > super->ps_image_size ||
- super->ps_strings_off + super->ps_strings_size > super->ps_image_size) {
- continue;
- }
- file = (const struct packfs_dnode *)(base + super->ps_nodes_off); /* the root directory */
- file = image_entry((const uint8_t *)base, super, file, "fw");
- file = image_entry((const uint8_t *)base, super, file, name);
- if (file != NULL && S_ISREG(file->pd_mode) && !(file->pd_flags & PACKFS_F_CHUNKED) &&
- file->pd_data + file->pd_size <= super->ps_image_size) {
- *size = file->pd_size;
- return (const uint8_t *)base + file->pd_data;
- }
+ const uint8_t *data = redwood_image_file("fw", name, size, NULL); /* (redwood_image.c: APFS or packfs) */
+
+ if (data != NULL) {
+ return data;
}
printf("wifi: %u: no fw/%s in the small image: put the phone's /vendor/firmware_mnt/image/%s into fw-wifi/ and run "
"userland/build.sh\n", step, name, name);
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.c b/xnu/files/bsd/miscfs/apfs/apfs_lib.c
index 57df8fb..169b5f3 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.c
+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.c
@@ -2298,6 +2298,40 @@ ext_next(struct ext_iter *it, uint64_t *logical, uint64_t *length, uint64_t *phy
}
}
+int
+apfs_file_in_place(struct apfs_vol *vol, const struct apfs_inode *ino, uint64_t *addr)
+{
+ bool compressed = false, in_rsrc = false;
+ uint32_t type = 0;
+ uint64_t csize = 0, logical, length, phys;
+ struct ext_iter it;
+ int error;
+
+ if ((ino->mode & APFS_S_IFMT) != APFS_S_IFREG || !ino->has_dstream || ino->size == 0) {
+ return ENOENT;
+ }
+ error = cfile_header(vol, ino, &compressed, &type, &csize, &in_rsrc);
+ if (error) {
+ return error;
+ }
+ if (compressed) {
+ return ENOENT;
+ }
+ error = ext_begin(vol, ino->private_id, 0, &it);
+ if (error == 0) {
+ error = ext_next(&it, &logical, &length, &phys);
+ }
+ if (error) {
+ return error == ENOENT ? ENOENT : error;
+ }
+ if (logical != 0 || length < ino->size || phys == 0 || phys >= vol->nx->block_count ||
+ (length / vol->nx->block_size) > vol->nx->block_count - phys) {
+ return ENOENT;
+ }
+ *addr = phys;
+ return 0;
+}
+
int
apfs_read_stream(struct apfs_vol *vol, uint64_t stream_id, uint64_t stream_size,
uint64_t off, void *buf, size_t len, size_t *out)
diff --git a/xnu/files/bsd/miscfs/apfs/apfs_lib.h b/xnu/files/bsd/miscfs/apfs/apfs_lib.h
index 3784ec6..40dca52 100644
--- a/xnu/files/bsd/miscfs/apfs/apfs_lib.h
+++ b/xnu/files/bsd/miscfs/apfs/apfs_lib.h
@@ -536,6 +536,13 @@ void apfs_cfile_fini(struct apfs_cfile *cf);
int apfs_read_file(struct apfs_vol *vol, const struct apfs_inode *ino,
struct apfs_cfile *cf, uint64_t off, void *buf, size_t len, size_t *out);
+/*
+ * Where a file's bytes lie on the device, when they lie in one piece as they are: not
+ * compressed, and one extent from its start covering its size. *addr is that extent's first
+ * block. ENOENT otherwise (compressed, in pieces, empty).
+ */
+int apfs_file_in_place(struct apfs_vol *vol, const struct apfs_inode *ino, uint64_t *addr);
+
/* ---- odds and ends worth testing on their own ---- */
uint64_t apfs_fletcher64(const uint8_t *data, size_t len);
diff --git a/xnu/redwood.patch b/xnu/redwood.patch
index 356d4c4..5f578bc 100644
--- a/xnu/redwood.patch
+++ b/xnu/redwood.patch
@@ -62,10 +62,10 @@ index a4d25040f38f..aad72993d139 100644
kern_debug.o_INCFLAGS += -I$(SRCROOT)/osfmk
diff --git a/bsd/conf/files b/bsd/conf/files
-index 44274016a4cf..47198fd9ec89 100644
+index 44274016a4cf..dc897f572180 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
-@@ -109,6 +109,36 @@ bsd/dev/munge.c optional config_requires_u32_munging
+@@ -109,6 +109,37 @@ bsd/dev/munge.c optional config_requires_u32_munging
bsd/dev/unix_startup.c standard
bsd/dev/monotonic.c optional monotonic
@@ -91,6 +91,7 @@ index 44274016a4cf..47198fd9ec89 100644
+bsd/dev/redwood_mic.c standard
+bsd/dev/redwood_wifi.c standard
+bsd/dev/redwood_adsp.c standard
++bsd/dev/redwood_image.c standard
+bsd/dev/redwood_virtio_blk.c standard
+bsd/dev/redwood_sparse.c standard
+bsd/dev/redwood_apfs_check.c optional apfs
@@ -102,7 +103,7 @@ index 44274016a4cf..47198fd9ec89 100644
bsd/dev/dev_perfmon.c optional config_perfmon
bsd/libkern/crc32.c standard
-@@ -155,6 +185,15 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +186,15 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
bsd/miscfs/mockfs/mockfs_fsnode.c optional mockfs
bsd/miscfs/mockfs/mockfs_vfsops.c optional mockfs
bsd/miscfs/mockfs/mockfs_vnops.c optional mockfs
@@ -118,7 +119,7 @@ index 44274016a4cf..47198fd9ec89 100644
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +400,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +401,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -126,7 +127,7 @@ index 44274016a4cf..47198fd9ec89 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +423,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +424,7 @@ bsd/kern/kern_debug.c standard
bsd/kern/kern_symfile.c standard
bsd/kern/kern_descrip.c standard
bsd/kern/kern_guarded.c standard