diff --git a/docs/ios27/SCION-LOADER.md b/docs/ios27/SCION-LOADER.md index 3d8fbf62c..944812dd7 100644 --- a/docs/ios27/SCION-LOADER.md +++ b/docs/ios27/SCION-LOADER.md @@ -37,6 +37,23 @@ tools/ios27/s9/make-boot-image.sh --kernel ... --donor-adt ... --template ... \ Without `--loader-kind scion` nothing changes: the same steps and the same image as before. +To put the loader around a payload that already boots (the first phone test wants exactly that), take the payload +out of that image and pack it as it is, instead of making a new one (which would add `sweetepoch=` and depend on +every input being the same): + +```sh +tools/ios27/starlte-pack-boot.py payload BOOT-THAT-BOOTS.img payload.bin +tools/ios27/s9/make-boot-image.sh --payload payload.bin --template BOOT-THAT-BOOTS.img \ + --loader-kind scion --scion-boot ~/src/scion-boot-exynos9810 +``` + +Its command line is then the payload's own, unchanged (the script prints only its length and SHA-256). For a boot +identical to sboot's, build the loader with `make PLATFORM=starlte LDR_OS_TOKENS=0 loader`: by default it appends +`scion_ldr=` and `scion_guard=` (54 bytes) to the line, and the kernel appends its built-in arguments +(osfmk/arm/arm_init.c, starlte_default_bootargs) only while its 1024-byte line has room. With iOS 27.2.1's line of +18 November (321 bytes; 1018 of 1024 with the built-ins) those 54 bytes leave out cam_switch, sgu_irq, decmpfs_lz4 +and sweetatpath. + ## The first test on the phone The plan is scion-boot's diff --git a/tools/ios27/s9/make-boot-image.sh b/tools/ios27/s9/make-boot-image.sh index 5257acaa0..262f512ce 100755 --- a/tools/ios27/s9/make-boot-image.sh +++ b/tools/ios27/s9/make-boot-image.sh @@ -39,6 +39,10 @@ # build marked safe (the boot-loop guard starts it after three failed boots) # [--scion-chain-sboot] scion: also append the sboot loader above with this payload as the entry "Starbite" (its menus, # store, recovery and fastboot one menu entry away; the payload is then in BOOT twice) +# [--payload P] pack an existing SWEETXNU payload exactly as it is instead of making one: no --kernel, --donor-adt, +# --cmdline, --inject-record, --camera-node, --firmware or --canvas, no sweetepoch= added; the payload's own +# command line is used as it stands. For a new loader around the payload of an image that already boots +# (`starlte-pack-boot.py payload BOOTIMG OUT` takes it out of one). Its line is not printed. # What it does (the steps the project's test images are made with): strip -x copy of the kernel; the S9's Apple device tree # (mkadt_starlte.py --big --bank2 --bank2-mb 1536 --iphone11, then adt-extra-ram.py: 8 cores, the 1.5 GiB window of the second DRAM # bank, seven extra RAM ranges of the first; adt-firmware.py, see --firmware; camera/adt-isp-node.py, see --camera-node); mkpayload.py (kernel + ADT + command line -> SWEETXNU payload, padded to 4 KiB); @@ -52,7 +56,7 @@ PY=${PY:-python3} KERNEL= DONOR= TEMPLATE= LINE= OUT=$ROOT/out/xnu-boot-ios27.img LOADER=$ROOT/xnuloader/sboot/out/xnu_boot.Image LINEFILE=$T27/cmdline/starlte-rc.txt RECORD=default CAMERA_NODE=auto FIRMWARE=auto CANVAS= -LOADER_KIND=sboot SCION_BOOT= SCION_CHAIN=0 SCION_ENTRIES=() +LOADER_KIND=sboot SCION_BOOT= SCION_CHAIN=0 SCION_ENTRIES=() PAYLOAD= while [ $# -gt 0 ]; do case "$1" in --kernel) KERNEL=$2; shift ;; --donor-adt) DONOR=$2; shift ;; --template) TEMPLATE=$2; shift ;; @@ -65,14 +69,21 @@ while [ $# -gt 0 ]; do --scion-boot) SCION_BOOT=$2; shift ;; --scion-entry) SCION_ENTRIES+=("$2"); shift ;; --scion-chain-sboot) SCION_CHAIN=1 ;; - -h | --help) sed -n '3,48p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; + --payload) PAYLOAD=$2; shift ;; + -h | --help) sed -n '3,52p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "make-boot-image: unknown argument $1" >&2; exit 2 ;; esac shift done die() { echo "make-boot-image: $*" >&2; exit 1; } -[ -f "$KERNEL" ] || die "--kernel: no kernel at '$KERNEL' (OS_PROFILE=ios-27 make kernel)" -[ -f "$DONOR" ] || die "--donor-adt: no device tree at '$DONOR' (tools/ios27/s9/fetch-apple-inputs.sh)" +if [ -n "$PAYLOAD" ]; then + [ -f "$PAYLOAD" ] || die "--payload: no file at '$PAYLOAD'" + [ -z "$KERNEL$DONOR" ] || die "--payload replaces --kernel and --donor-adt: give one or the other" + [ "$(head -c 8 "$PAYLOAD")" = SWEETXNU ] || die "--payload: $PAYLOAD is not a SWEETXNU payload" +else + [ -f "$KERNEL" ] || die "--kernel: no kernel at '$KERNEL' (OS_PROFILE=ios-27 make kernel)" + [ -f "$DONOR" ] || die "--donor-adt: no device tree at '$DONOR' (tools/ios27/s9/fetch-apple-inputs.sh)" +fi [ -f "$TEMPLATE" ] || die "--template: no boot image at '$TEMPLATE' (tools/ios27/s9/flash.sh pull-template)" case "$LOADER_KIND" in sboot) @@ -89,6 +100,23 @@ scion) esac case "$CANVAS" in '' | [1-9]*x[1-9]*) ;; *) die "--canvas WxH (got '$CANVAS')" ;; esac case "$CANVAS" in *[!0-9x]* | *x*x*) die "--canvas WxH (got '$CANVAS')" ;; esac +mkdir -p "$(dirname "$OUT")" +W=$(mktemp -d "${TMPDIR:-/tmp}/bootimg.XXXXXX"); trap 'rm -rf "$W"' EXIT +if [ -n "$PAYLOAD" ]; then + "$PY" - "$PAYLOAD" "$W/payload.bin" <<'PY' +import hashlib, sys +from pathlib import Path +b = Path(sys.argv[1]).read_bytes() +line = b[0x48:0x48 + 608] +assert b[:8] == b'SWEETXNU' and len(b) > 0x2a8, 'not a SWEETXNU payload' +assert line[607] == 0, 'the payload\'s command line is not terminated' +n = len(line.split(b'\0')[0]) +Path(sys.argv[2]).write_bytes(b + b'\0' * (-len(b) % 4096)) +print(f'make-boot-image: --payload: {len(b)} bytes, sha256 {hashlib.sha256(b).hexdigest()}; its command line {n} of 607 bytes ' + f'(sha256 {hashlib.sha256(line.split(bytes(1))[0]).hexdigest()}), used as it is') +PY + n=; LINE= +else # the sensor hub's image for the device tree (firmware.sh: complete and checked, or no image) SHUB_FILES= case "$FIRMWARE" in @@ -126,8 +154,6 @@ if [ -n "$CANVAS" ]; then fi n=$(printf '%s' "$LINE" | wc -c | tr -d ' ') [ "$n" -le 607 ] || die "the command line is $n bytes; the payload holds 607 (it would be cut)" -mkdir -p "$(dirname "$OUT")" -W=$(mktemp -d "${TMPDIR:-/tmp}/bootimg.XXXXXX"); trap 'rm -rf "$W"' EXIT cp "$KERNEL" "$W/kernel.x"; xcrun strip -x "$W/kernel.x" "$PY" "$T27/mkadt_starlte.py" --big --bank2 --bank2-mb 1536 --iphone11 "$DONOR" ${CANVAS:+--canvas "$CANVAS"} "$W/base.adt" >/dev/null "$PY" "$T27/adt-extra-ram.py" "$W/base.adt" "$W/ram.adt" @@ -151,6 +177,7 @@ if packed != sys.argv[2]: p.write_bytes(b + b'\0' * (-len(b) % 4096)) print(f'command line: {len(packed)} of 607 bytes, read back from the payload') PY +fi head -c 4096 /dev/zero > "$W/ramdisk.bin" if [ "$LOADER_KIND" = sboot ]; then "$PY" "$T27/starlte-pack-boot.py" pack "$TEMPLATE" "$LOADER" "$OUT" --append "$W/payload.bin" --ramdisk "$W/ramdisk.bin" | tail -1 @@ -170,4 +197,4 @@ else echo "make-boot-image: scion-boot loader: NOT YET RUN ON A PHONE (docs/ios27/SCION-LOADER.md: the first test, and the way back)" fi shasum -a 256 "$OUT" | tee "$OUT.sha256" -echo "command line ($n bytes): $LINE" +[ -n "$PAYLOAD" ] || echo "command line ($n bytes): $LINE" diff --git a/tools/ios27/starlte-pack-boot.py b/tools/ios27/starlte-pack-boot.py index 5648d01db..fb42890f1 100755 --- a/tools/ios27/starlte-pack-boot.py +++ b/tools/ios27/starlte-pack-boot.py @@ -14,6 +14,8 @@ info TEMPLATE pack TEMPLATE IMAGE OUT [--append PAYLOAD] [--ramdisk R] [--cmdline S] [--image-size-from-file] kernel BOOTIMG OUT (write the kernel slot, e.g. the woa UEFI's bootshim + FD) + payload BOOTIMG OUT (write the SWEETXNU payload sboot's image carries after the loader, as packed: for + make-boot-image.sh --payload; prints its size and SHA-256, not its command line) """ import argparse import hashlib @@ -139,6 +141,16 @@ def kernel(args): print(f"{args.out}: {len(k)} bytes") +def payload(args): + k = parse(Path(args.boot).read_bytes())["kernel"] + offs = [o for o in range(0, len(k), 4096) if k[o:o + 8] == b"SWEETXNU"] + if len(offs) != 1: + raise SystemExit(f"{args.boot}: {len(offs)} SWEETXNU payloads at 4 KiB offsets of the kernel slot (want exactly one)") + p = k[offs[0]:] + Path(args.out).write_bytes(p) + print(f"{args.out}: {len(p)} bytes from kernel slot offset {offs[0]:#x}, sha256 {hashlib.sha256(p).hexdigest()}") + + def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) sub = ap.add_subparsers(dest="cmd", required=True) @@ -156,8 +168,12 @@ def main(): k = sub.add_parser("kernel") k.add_argument("boot") k.add_argument("out") + q = sub.add_parser("payload") + q.add_argument("boot") + q.add_argument("out") args = ap.parse_args() - {"info": lambda: info(args.template), "pack": lambda: pack(args), "kernel": lambda: kernel(args)}[args.cmd]() + {"info": lambda: info(args.template), "pack": lambda: pack(args), "kernel": lambda: kernel(args), + "payload": lambda: payload(args)}[args.cmd]() if __name__ == "__main__":