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

An installed HakiOS: the loader takes its resident image from the payload, mkubootimg makes the boot partition's image

shimoku74940cb6ccaad8
diff --git a/devices/qemu/u-boot/files/configs/hakios_qemu_defconfig b/devices/qemu/u-boot/files/configs/hakios_qemu_defconfig
index 123adf4..0b9b72d 100644
--- a/devices/qemu/u-boot/files/configs/hakios_qemu_defconfig
+++ b/devices/qemu/u-boot/files/configs/hakios_qemu_defconfig
@@ -5,6 +5,8 @@
CONFIG_CMD_HAKIOS=y
CONFIG_BOOTDELAY=0
-CONFIG_BOOTCOMMAND="hakios load 0x50000000 && hakios boot"
+CONFIG_BOOTCOMMAND="hakios load 0x50000000 && run hakios_start"
# (a host tool that needs GnuTLS, and capsules are not used here)
# CONFIG_TOOLS_MKEFICAPSULE is not set
+# the menu of an installed HakiOS (tools/mkubootimg.py --install), tried here before a phone
+CONFIG_CMD_BOOTMENU=y
diff --git a/tools/mkubootimg.py b/tools/mkubootimg.py
index a7ee06a..14bf7b2 100755
--- a/tools/mkubootimg.py
+++ b/tools/mkubootimg.py
@@ -14,7 +14,7 @@ The same container on its own (-c) is what the host sends to a U-Boot that is al
HKUB, little-endian: u32 magic "HKUB", u32 version 1, u32 count, u32 header size,
count x { char name[16]; u32 offset; u32 size; u32 crc32; u32 reserved }, entries 4 KiB aligned
-usage: mkubootimg.py [-d DEVICE] [-u u-boot.bin] [-l loader.efi] [-p payload.bin] [-o out.img] [-c out.hkub]
+usage: mkubootimg.py [-d DEVICE] [-u u-boot.bin] [-l loader.efi] [-p payload.bin] [-o out.img] [-c out.hkub] [--env F | --install]
mkubootimg.py -p payload.bin -c x.hkub the container alone
"""
import argparse, gzip, os, struct, subprocess, sys, zlib
@@ -47,6 +47,9 @@ def main():
ap.add_argument('-o', '--output', help='the boot image (default: hakios-u-boot.img)')
ap.add_argument('-c', '--container', help='write the HKUB container on its own (for fastboot stage); without -o, only it')
ap.add_argument('--no-ramdisk', action='store_true', help='U-Boot only: it waits for the loader over fastboot')
+ ap.add_argument('--env', help='an environment for U-Boot to take with the loader (name=value lines): what the image does')
+ ap.add_argument('--install', action='store_true',
+ help='the image for a boot partition: with devices/DEVICE/u-boot/install.env (the menu: Android or HakiOS)')
args = ap.parse_args()
dev = os.path.join(ROOT, 'devices', args.device)
@@ -88,6 +91,12 @@ def main():
entries.append(('payload', data))
# after the payload: a loader.efi the payload carries (xl enter's) gives way to this one
entries.append(('loader', efi))
+ env = args.env or (os.path.join(dev, 'u-boot', 'install.env') if args.install else None)
+ if env:
+ if not os.path.isfile(env):
+ sys.exit(f'mkubootimg.py: {env} not found')
+ # last: U-Boot takes it once the loader and the payload are in place
+ entries.append(('env', open(env, 'rb').read()))
hkub = container(entries) if entries else b''
if args.container:
open(args.container, 'wb').write(hkub)
diff --git a/xnuloader/loader.c b/xnuloader/loader.c
index 2558e02..2be65df 100644
--- a/xnuloader/loader.c
+++ b/xnuloader/loader.c
@@ -2535,6 +2535,8 @@ struct payload_info {
uint64_t checkpoint;
uint64_t forced_base;
uint64_t resident_rd; /* size of a root image the host put at the resident address */
+ const uint8_t *resident; /* ... or one the payload carries, which the loader puts there itself */
+ uint32_t resident_size;
const char *kernel_version; /* kernel.ver: the version of the kernel in this payload (tools/kernel-version.py) */
int usb_mode;
};
@@ -2605,6 +2607,9 @@ parse_payload(struct payload_info *pi, int verbose)
pi->cmdline = text;
} else if (str_eq(e->name, "physbase")) {
pi->forced_base = parse_number(text, 16); /* testing aid; must be free RAM */
+ } else if (str_eq(e->name, "resident")) {
+ pi->resident = p + sizeof(*e); /* the image xl ramdisk would send: a boot with no host */
+ pi->resident_size = e->size;
} else if (str_eq(e->name, "rdres")) {
pi->resident_rd = parse_number(text, 16); /* e.g. loaded there by QEMU; see RESIDENT_RD_OFFSET */
} else if (str_eq(e->name, "kernel.ver")) {
@@ -2894,6 +2899,20 @@ run_kernel(int have_checkpoint, uint64_t checkpoint)
}
resident_rd_size = pi.resident_rd;
}
+ /*
+ * The payload's own resident image (a boot from the phone's boot partition, where no host sends one): put where
+ * "oem ramdisk" puts it, once per run, and taken as if it had been sent.
+ */
+ if (pi.resident != NULL) {
+ if (win_size <= RESIDENT_RD_OFFSET + RESIDENT_RD_RESERVE ||
+ pi.resident_size > win_size - RESIDENT_RD_OFFSET - RESIDENT_RD_RESERVE) {
+ print("resident %x does not fit in the memory window\n", (uint64_t)pi.resident_size);
+ return 0;
+ }
+ memcpy((void *)(phys_base + RESIDENT_RD_OFFSET), pi.resident, pi.resident_size);
+ resident_rd_size = pi.resident_size;
+ print("resident image from the payload: %x bytes\n", (uint64_t)pi.resident_size);
+ }
if (resident_rd_size != 0) {
uint64_t res_pa = phys_base + RESIDENT_RD_OFFSET;
uint64_t res_size = (resident_rd_size + PAGE_SIZE_MAX - 1) & ~(PAGE_SIZE_MAX - 1);
diff --git a/xnuloader/mkpayload.py b/xnuloader/mkpayload.py
index 4eb8bfa..3709a4d 100755
--- a/xnuloader/mkpayload.py
+++ b/xnuloader/mkpayload.py
@@ -16,6 +16,7 @@ ap.add_argument('--cmdline', default='')
ap.add_argument('--physbase', default='', help='hex: force XNU physical base (testing)')
ap.add_argument('--usb', action='store_true', help='make the loader stay resident and take commands over USB')
ap.add_argument('--rdres', default='', help='hex size of a root image already placed at the resident address (QEMU)')
+# (a resident image the loader places itself: give it as an item, resident=FILE -- a boot with no host, from the phone's boot partition)
ap.add_argument('--checkpoint', default='', help='kernel checkpoint to stop at; 999 = loader self test')
ap.add_argument('--kernel-version', default='',
help='the kernel.ver the loader prints under its banner (default: tools/kernel-version.py)')