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

xnuloader: volume payload takes the whole payload region and wipes a half-read header

shimoku98ebf999c94a
diff --git a/docs/uefi-boot.md b/docs/uefi-boot.md
index 0fcddd2..75e4b14 100644
--- a/docs/uefi-boot.md
+++ b/docs/uefi-boot.md
@@ -47,8 +47,10 @@ A file that is not a payload gives `is not a payload (no XNUP at its start)`, an
- **63 MiB.** The loader's payload region is 64 MiB and its last MiB holds the panic log. The kernel is about 15 MiB;
the **big iOS system image (2.5 GB) does not come this way**. It comes over USB (`xl ramdisk`, once per loader
session) or, in the flash recipe, from the partition `darwin` (`docs/install.md`).
-- **The region has to be free.** The loader takes it with `AllocatePages(AllocateAddress)`. A firmware that keeps it
- reserved refuses, and the loader says so and goes on without a payload, as it always did.
+- **The region has to be free.** The loader takes all 64 MiB of it with `AllocatePages(AllocateAddress)` (not only the
+ pages the file fills: a payload sent over USB later, and the panic log, are written into the rest). A firmware that
+ keeps it reserved refuses, and the loader says so and goes on without a payload, as it always did. A file that
+ cannot be read to its end is dropped, and its header is wiped, so a half-read payload is never started.
- **Nothing is signed.** The loader is not signed and no Secure Boot is involved.
## On a phone: what is known and what is not
diff --git a/xnuloader/loader.c b/xnuloader/loader.c
index 238ad1d..fe527e2 100644
--- a/xnuloader/loader.c
+++ b/xnuloader/loader.c
@@ -3994,7 +3994,12 @@ load_payload_from_volume(void)
volume_payload_note = "\\EFI\\hakios\\payload.bin is empty or does not fit in the payload region (63 MiB)";
goto close;
}
- pages = (size + 0xfff) / 0x1000;
+ /*
+ * Take the whole payload region, not only the pages the file fills: the rest of it is written later (a payload
+ * sent over USB with `download:` goes up to the 63 MiB limit, and the panic log lives in the last MiB), and
+ * memory the firmware has not given to us must not be written.
+ */
+ pages = PAYLOAD_MAX_SIZE / 0x1000;
if (EFI_ERROR(BS->allocate_pages(2 /* AllocateAddress */, 2 /* EfiLoaderData */, pages, &address))) {
volume_payload_note = "the payload region is not free (the firmware keeps it reserved)";
goto close;
@@ -4004,6 +4009,9 @@ load_payload_from_volume(void)
if (EFI_ERROR(file->read(file, &chunk, (void *)(uintptr_t)(PAYLOAD_BASE + done))) || chunk == 0) {
volume_payload_note = "\\EFI\\hakios\\payload.bin could not be read to its end";
+ /* what was read stays in memory after the pages are given back: without this, the first MiB's XNUP
+ * header would still be there and a half-read payload would be taken for a whole one */
+ ((volatile struct payload_header *)PAYLOAD_BASE)->magic = 0;
BS->free_pages(PAYLOAD_BASE, pages);
goto close;
}