diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 405ef3d6d..ddb92bdbf 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -45,9 +45,9 @@ jobs: steps: - name: Packages the tests use run: | - pacman -Syu --noconfirm --needed git base-devel e2fsprogs zstd openssl \ + pacman -Syu --noconfirm --needed git base-devel e2fsprogs zstd lz4 openssl \ python python-dbus python-gobject dbus nodejs dtc cpio file jq busybox \ - xz gzip qt6-declarative shellcheck rsync + xz gzip qt6-declarative shellcheck rsync android-tools # Arch's nodejs can lag a simdjson soname bump: on 2026-10-29 # simdjson 5.0.8 dropped the libsimdjson.so.34 that nodejs still # linked, and node no longer started. Only while node can't start, diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 60e5d35b7..6193f8f80 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -20,10 +20,10 @@ Today these steps are manual. Where the install is going, one guided step from a - **A Pixel 7 Pro** whose bootloader can be unlocked. Phones sold locked to a carrier often can't be. In Android, go to Settings > About phone and tap Build number seven times to turn on Developer options, then check that Settings > System > Developer options > **OEM unlocking** can be switched on. If it's greyed out, the phone can't be unlocked. - **A computer running Linux** (Omarchy, Arch Linux or another distribution), and a USB-C cable that carries data. The installer is a shell script for Linux; installing from a web page or a desktop app is planned. -- **These programs on the computer:** `fastboot` and `adb` (Android platform tools), `e2fsprogs`, `zstd`, `openssl`, `gnupg`, `python3`, `unzip`, and `binutils`. On Omarchy or Arch Linux: +- **These programs on the computer:** `fastboot` and `adb` (Android platform tools), `e2fsprogs`, `zstd`, `lz4`, `cpio`, `openssl`, `gnupg`, `python3`, `unzip`, and `binutils`. On Omarchy or Arch Linux: ```sh - sudo pacman -S --needed android-tools e2fsprogs zstd openssl gnupg python unzip binutils erofs-utils + sudo pacman -S --needed android-tools e2fsprogs zstd lz4 cpio openssl gnupg python unzip binutils erofs-utils ``` On other distributions, install the packages with the same names (Android's tools are often split into `fastboot`, `adb` and a "libsparse" package for `img2simg` and `simg2img`). diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 743080008..eb1850e0c 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -183,7 +183,7 @@ As built (§5.5), the tool and the hook are their own package, `omamobile-boot-u The same Alpine-based initramfs, with release differences: -- **No developer keys.** It ships without `authorized_keys`: `bin/omamobile initramfs --release` builds it that way, and `tools/make-bundle` refuses to sign a bundle whose initramfs still holds SSH keys. The hook appends a second, tiny cpio to the ramdisk that holds the phone's own `/root/.ssh/authorized_keys` (the kernel unpacks concatenated cpio archives in order), so SSH into the initramfs keeps working for the phone's owner and nobody else. +- **No developer keys.** It ships without `authorized_keys`: `bin/omamobile initramfs --release` builds it that way, and `tools/make-bundle` refuses to sign a bundle whose initramfs still holds SSH keys. The hook appends a second, tiny cpio to the ramdisk that holds the phone's own `/root/.ssh/authorized_keys` (the kernel unpacks concatenated cpio archives in order), so SSH into the initramfs keeps working for the phone's owner and nobody else. `tools/install-phone` appends the same cpio, with the same commands, to a copy of the bundle's ramdisk image (`vendor_kernel_boot`, or `vendor_boot` on a phone without one) before flashing it, so the initramfs takes the owner's key from the first boot and the hook's first run finds the image it would build already on slot B (`tests/install-phone-initramfs`). A device with `INITRAMFS_MODULES` (raven) is the exception: the hook's cpio also carries those modules, so its first run rewrites the image once. - **No bring-up probes.** `probe()` and `omamobile.debug` stay in development builds only. - **Quirks from the descriptor.** The trigger write is already per device: `bin/omamobile initramfs` packs `SIMPLEFB_TRIG_CON` into `/etc/omamobile-simplefb-trig` and `COMPATIBLE` beside it. The init writes the pair only when the board's first compatible string matches. A device without the trigger key gets no register write. The storage guard's allowlist is packed the same way, from `UFS_RW` into `/etc/omamobile-ufs-rw`, and the init leaves every partition read-only without a valid one. So is the USB product string (`USB_PRODUCT`, else `NAME`). Step 2 still turns the remaining quirk (`MAIN_LUN`) into a generated `/etc/omamobile/device.conf` the shared init reads at boot. That remaining step changes what boots, so it gets its own runbook and a test boot from `bin/omamobile push`; it is not a refactor. - **Reproducible:** fixed mtimes (`SOURCE_DATE_EPOCH`), sorted file list, `cpio --reproducible`. @@ -323,9 +323,9 @@ The bundle's signed `SHA256SUMS` covers its `MANIFEST`, which names the exact ro 1. Checks the bundle's signature and checksums, and that `fastboot getvar product` is this bundle's codename, the bootloader is unlocked, and both slots carry the same bootloader version. Any doubt stops it. 2. Asks for the user's factory zip, checks its `board=`, and runs the device's firmware and chroot extraction on the laptop. -3. Makes a **local copy** of the root image and writes into it, with `debugfs -w` (no root needed): the extracted firmware, the user's SSH public key, `/etc/omamobile/boot-update.conf` with `AUTO_WRITE=yes`, and a first-boot marker naming the device package to install. Nothing extracted leaves the laptop except into the user's own phone. +3. Makes a **local copy** of the root image and writes into it, with `debugfs -w` (no root needed): the extracted firmware, the user's SSH public key, `/etc/omamobile/boot-update.conf` with `AUTO_WRITE=yes`, and a first-boot marker naming the device package to install. The same key goes into a local copy of the ramdisk image's initramfs, as the boot hook appends it (§5.2), and that copy is what step 5 flashes. Nothing extracted leaves the laptop except into the user's own phone. 4. Shows exactly what it will write (`BOOT_IMAGES` with `_b`, and `userdata`), says that `userdata` is erased (stock Android needs a factory reset if it is ever booted again), and waits for the user to type the codename. -5. Runs only `fastboot flash _b images/.img` for each `BOOT_IMAGES` entry, `fastboot flash userdata `, `fastboot --set-active=b` and `fastboot reboot`. The partition names come from the descriptor and are checked against its `UFS_RW` again; the script has no code path for any other partition, no `-w`, no `--slot`, no `flashing` command. +5. Runs only `fastboot flash _b images/.img` for each `BOOT_IMAGES` entry (the ramdisk image from its copy, step 3), `fastboot flash userdata `, `fastboot --set-active=b` and `fastboot reboot`. The partition names come from the descriptor and are checked against its `UFS_RW` again; the script has no code path for any other partition, no `-w`, no `--slot`, no `flashing` command. 6. On first boot, `omamobile-setup-system` installs the device package named by the marker (from the package cache, offline) and reminds the user to take the read-only backup of `efs`, `efs_backup`, `modem_userdata`, `persist` and `modem_a/b` ([MODEM-PLAN](../device/cheetah/MODEM-PLAN.md) M0.1) before any radio work. `tools/nv-backup DIR` (in the bundle's `tools/`) takes it over USB SSH: the partitions from the descriptor's `NV_PARTS` and `NV_BACKUP_EXTRA`, found by `PARTNAME`, refused unless every one is found once, read-only and unmounted, each copied with `dd`, read again on the phone and compared, into a directory of mode 700 with files of mode 600; `--check DIR` compares the phone with it later. `bin/omamobile flash` stays the developer's tool; `install` is for everyone else and is deliberately narrower. diff --git a/tests/all b/tests/all index 21dd14c4f..ba1946ed3 100755 --- a/tests/all +++ b/tests/all @@ -1,7 +1,8 @@ #!/bin/bash # SPDX-License-Identifier: MIT # The installer's tests (no phone, nothing flashed): tools/install-phone -# against a fake fastboot, tools/phone-provision against a synthetic backup +# against a fake fastboot (and the ramdisk image it flashes against the one +# omamobile-boot-update builds), tools/phone-provision against a synthetic backup # (and a container standing in for the phone, with Docker), tools/nv-backup # against a stand-in phone tree, # tools/phone-install-package against a stand-in phone, the initramfs's @@ -44,7 +45,7 @@ sh -n device/flame/initramfs/build bash -n tools/build-flame-probe echo "ok - scripts parse" python3 tests/bt-uart-gpio.py -for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help; do +for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs; do echo "# $t" "$t" done diff --git a/tests/install-phone b/tests/install-phone index 77a7908be..222b6f1ff 100755 --- a/tests/install-phone +++ b/tests/install-phone @@ -11,7 +11,7 @@ cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." SRC=$PWD fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } pass() { printf 'ok - %s\n' "$1"; } -for tool in debugfs mkfs.ext4 zstd openssl; do command -v $tool >/dev/null || { echo "skip: no $tool"; exit 0; }; done +for tool in debugfs mkfs.ext4 zstd openssl lz4 cpio; do command -v $tool >/dev/null || { echo "skip: no $tool"; exit 0; }; done t=$(mktemp -d) cleanup() { [[ ! -d $t/gnupg ]] || gpgconf --homedir "$t/gnupg" --kill gpg-agent 2>/dev/null || true @@ -34,7 +34,21 @@ UFS_RW="boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata" EOF } descriptor > "$R/device/testdev/device.conf" -for p in boot init_boot vendor_boot vendor_kernel_boot dtbo vbmeta; do echo "$p image" > "$t/images/$p.img"; done +for p in boot init_boot dtbo vbmeta; do echo "$p image" > "$t/images/$p.img"; done +# The ramdisk image is a real v4 vendor boot image (install-phone appends the +# owner's key to its initramfs; tests/install-phone-initramfs checks the bytes) +mkdir -p "$t/rd/etc" && echo init > "$t/rd/init" +(cd "$t/rd" && find . | LC_ALL=C sort | cpio -o -H newc --quiet) | lz4 -l -9 -q > "$t/rd.lz4" +for p in vendor_boot vendor_kernel_boot; do + python3 -I -c ' +import struct, sys +rd, page = open(sys.argv[1], "rb").read(), 4096 +pad = lambda b: b + bytes(-len(b) % page) +hdr = struct.pack("<8s5I2048sI16s2IQ4I", b"VNDRBOOT", 4, page, 0, 0, len(rd), b"", 0, b"", 2128, 3, 0, 108, 1, 108, 0) +table = struct.pack("<3I32s", len(rd), 0, 1, b"") + bytes(64) +open(sys.argv[2], "wb").write(pad(hdr) + pad(rd) + pad(b"dtb") + pad(table)) +' "$t/rd.lz4" "$t/images/$p.img" +done echo testdev > "$t/images/DEVICE" echo 'root:x:0:0::/root:/bin/bash' > "$t/rootdir/etc/passwd" mkfs.ext4 -q -L omamobile-root -d "$t/rootdir" "$t/root.img" 16M @@ -91,7 +105,7 @@ install --dry-run || { cat "$t/out"; fail "a dry run succeeds"; } expected=" would run: fastboot -s FAKE123 flash boot_b $t/images/boot.img would run: fastboot -s FAKE123 flash init_boot_b $t/images/init_boot.img would run: fastboot -s FAKE123 flash vendor_boot_b $t/images/vendor_boot.img - would run: fastboot -s FAKE123 flash vendor_kernel_boot_b $t/images/vendor_kernel_boot.img + would run: fastboot -s FAKE123 flash vendor_kernel_boot_b $t/work/ramdisk/vendor_kernel_boot.img would run: fastboot -s FAKE123 flash dtbo_b $t/images/dtbo.img would run: fastboot -s FAKE123 flash userdata $t/work/root.simg would run: fastboot -s FAKE123 --set-active=b @@ -115,7 +129,7 @@ install || { cat "$t/out"; fail "a full run succeeds"; } want="-s FAKE123 flash boot_b $t/images/boot.img -s FAKE123 flash init_boot_b $t/images/init_boot.img -s FAKE123 flash vendor_boot_b $t/images/vendor_boot.img --s FAKE123 flash vendor_kernel_boot_b $t/images/vendor_kernel_boot.img +-s FAKE123 flash vendor_kernel_boot_b $t/work/ramdisk/vendor_kernel_boot.img -s FAKE123 flash dtbo_b $t/images/dtbo.img -s FAKE123 flash userdata $t/work/root.simg -s FAKE123 --set-active=b diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs new file mode 100755 index 000000000..0f36f6a22 --- /dev/null +++ b/tests/install-phone-initramfs @@ -0,0 +1,279 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# The release bundle's install writes the ramdisk image omamobile-boot-update +# would write (#564; docs/RELEASES.md §5.2, §11 step 4). No phone, nothing +# flashed: for a gs201 layout (the ramdisk in vendor_kernel_boot, 2 KiB pages +# as on the Pixel 7 Pro and every descriptor in device/) and a gs101 layout +# (the ramdisk in vendor_boot, with 4 KiB pages for the header arithmetic), +# +# 1. the hook's own code (omamobile-boot-update build, on a stand-in phone +# root) builds the slot B images twice from the same device package and +# kernel: once without root's authorized_keys (the bundle's images, which +# tests/boot-update-parity shows bin/omamobile images gives too) and once +# with the key install-phone writes into the root image; +# 2. the bundle's install (tools/install-phone alone, without the +# repository) installs from the first set against a fake fastboot; +# 3. every image it flashes is the hook's byte for byte, so the rescue SSH +# in the initramfs has the owner's key from the first boot; and the +# hook's dry run on a slot B holding them writes nothing (on one holding +# the bundle's own images it rewrites vendor_kernel_boot, as in #564). +# +# Then the refusals, each before anything is flashed: an image the key makes +# larger than its partition, a failing step of the key's append, a ramdisk +# image under an AVB footer, an image that isn't a v4 vendor boot image; and +# a ramdisk that already holds SSH keys (a development initramfs) is flashed +# as built. The hook runs under pacman's umask, 022, and install under 077: +# its images must not depend on the owner's umask (uutils' install(1) gives +# parent directories the umask's mode). +# +# Skips without mkbootimg (MKBOOTIMG, mkbootimg on PATH, or AOSP's +# mkbootimg.py from tools/fetch-toolchain or under SRC_DIR), lz4, cpio, dtc, +# debugfs, mkfs.ext4, zstd or openssl. +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +SRC=$PWD +PKG=$SRC/packages/omamobile-boot-update +fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } +pass() { printf 'ok - %s\n' "$1"; } +skip() { printf 'ok - # skip: %s\n' "$1"; exit 0; } +for tool in lz4 cpio dtc debugfs mkfs.ext4 zstd openssl python3 flock; do command -v $tool >/dev/null || skip "no $tool"; done +t=$(mktemp -d) +trap '[[ -n ${KEEP:-} ]] || rm -rf "$t"' EXIT + +mk=${MKBOOTIMG:-} +if [[ -z $mk ]] && ! mk=$(command -v mkbootimg); then + [[ -n ${SRC_DIR:-} ]] || SRC_DIR=$(source ./omamobile.conf; [[ -f omamobile.local.conf ]] && source ./omamobile.local.conf; echo "$SRC_DIR") + for py in "${OMAMOBILE_TOOLCHAIN:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile/toolchain}/mkbootimg/mkbootimg.py" \ + "$SRC_DIR/system/tools/mkbootimg/mkbootimg.py"; do + [[ -f $py ]] || continue + mkdir -p "$t/bin" + printf '#!/bin/sh\nexec python3 %s "$@"\n' "$py" > "$t/bin/mkbootimg" + chmod +x "$t/bin/mkbootimg" + mk=$t/bin/mkbootimg + break + done +fi +[[ -n $mk ]] || skip "no mkbootimg (android-tools, or tools/fetch-toolchain)" +echo "# mkbootimg: $mk" + +ssh-keygen -q -t ed25519 -N '' -C owner -f "$t/owner" 2>/dev/null || + echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl owner' > "$t/owner.pub" +REL=7.3.0-rc6-omamobile-gaaaaaaaaaaaa + +# A stand-in phone root with an installed device package and kernel package, +# as omamobile-boot-update reads them (OMAMOBILE_BOOT_ROOT) +phone() { # root codename soc soc_id pagesize boot_images ufs_rw [initramfs extra path] + local R=$1 cn=$2 soc=$3 id=$4 s=$t/stage-$2 + mkdir -p "$R/usr/share/omamobile/linux" "$R/usr/lib/omamobile/$cn" "$R/usr/lib/modules/$REL/dtbs/exynos/google" \ + "$R/proc/device-tree" "$R/root/.ssh" "$R/etc/omamobile" + printf 'CODENAME=%s\nNAME="Test %s"\nSOC=%s\nCOMPATIBLE="google,%s-%s"\nPACKAGE=omamobile-device-%s\nDTB=exynos/google/%s-%s.dtb\nSOC_IDS="a0:%s"\nDTBO_SOURCE=%s-overlay.dts\nDTBO_BOARD_ID=0x30306\nDTBO_BOARD_REV=0x10000\nVENDOR_CMDLINE="console=tty0 clk_ignore_unused"\nVENDOR_PAGESIZE=%s\nOS_VERSION=17.0.0\nOS_PATCH_LEVEL=2026-07\nBOOT_IMAGES="%s"\nUFS_RW="%s"\nINITRAMFS_INIT=/initramfs/init\nPANEL_SIZE=1080x2400\n' \ + "$cn" "$cn" "$soc" "$soc" "$cn" "$cn" "$soc" "$cn" "$id" "$cn" "$5" "$6" "$7" > "$R/usr/share/omamobile/device.conf" + printf 'google,%s-%s\0google,%s\0' "$soc" "$cn" "$soc" > "$R/proc/device-tree/compatible" + echo "$REL" > "$R/usr/share/omamobile/linux/kernel.release" + head -c 300000 /dev/urandom > "$R/usr/lib/modules/$REL/vmlinuz" + echo linux-omamobile > "$R/usr/lib/modules/$REL/pkgbase" + echo dep > "$R/usr/lib/modules/$REL/modules.dep"; echo bin > "$R/usr/lib/modules/$REL/modules.dep.bin" + printf '/dts-v1/;\n/ {\n\tsoc_id = <%s 0x00>;\n\tcompatible = "google,%s-%s";\n\tect { };\n\tchosen { bootargs = ""; };\n};\n' "$id" "$soc" "$cn" \ + | dtc -q -@ -I dts -O dtb -o "$R/usr/lib/modules/$REL/dtbs/exynos/google/$soc-$cn.dtb" - + head -c 6000 /dev/urandom > "$R/usr/lib/omamobile/$cn/dtbo.img" + # The release initramfs: the allowlist, an init, and more than one 8 MiB + # lz4 block of data; no SSH keys unless asked + rm -rf "$s" && mkdir -p "$s/etc" "$s/usr/lib" + echo '#!/bin/sh' > "$s/init" + echo "$7" > "$s/etc/omamobile-ufs-rw" + head -c 7000000 /dev/urandom | base64 > "$s/usr/lib/payload" + if [[ -n ${8:-} ]]; then mkdir -p "$s/${8%/*}"; echo 'ssh-ed25519 AAAA developer' > "$s/$8"; fi + (cd "$s" && find . | LC_ALL=C sort | cpio -o -H newc --owner=0:0 --quiet) | lz4 -l -9 -q > "$R/usr/lib/omamobile/$cn/initramfs.cpio.lz4" +} +# The hook as pacman runs it: umask 022 (the builder's directory modes follow it) +hook() { (umask 022; OMAMOBILE_BOOT_ROOT=$1 OMAMOBILE_BOOT_UPDATE_LIB=$PKG/lib MKBOOTIMG=$mk "$PKG/bin/omamobile-boot-update" "${@:2}"); } +hook_build() { # root out + hook "$1" build > "$t/hook.log" 2>&1 || { cat "$t/hook.log"; fail "omamobile-boot-update build"; } + rm -rf "$2" && cp -r "$1/var/lib/omamobile/boot/next" "$2" +} + +# The fake fastboot: logs every call, keeps a copy of every flashed file +mkdir -p "$t/fbin" +cat > "$t/fbin/fastboot" <<'EOF' +#!/bin/bash +echo "$*" >> "$T/fastboot.log" +if [[ $1 == devices ]]; then printf 'FAKE123\tfastboot\n'; exit 0; fi +[[ $1 == -s ]] && shift 2 +case $1 in + getvar) sed 's/^/(bootloader) /' "$T/vars" >&2 ;; + flash) cp "$3" "$T/flashed/$2" ;; +esac +exit 0 +EOF +chmod +x "$t/fbin/fastboot" +export T=$t +mkdir -p "$t/rootdir/etc" "$t/rootdir/root" +echo 'root:x:0:0::/root:/bin/bash' > "$t/rootdir/etc/passwd" +mkfs.ext4 -q -L omamobile-root -d "$t/rootdir" "$t/root.img" 16M + +# bundle codename images: the bundle's install, the root image beside it +bundle() { + local cn=$1 B=$t/bundle-$1/omamobile-$1-2026.11.09 p + rm -rf "$t/bundle-$cn" && mkdir -p "$B/images" + cp tools/install-phone "$B/install" + cp "$t/r-$cn/usr/share/omamobile/device.conf" "$B/device.conf" + for p in $(sed -n 's/^BOOT_IMAGES="\(.*\)"$/\1/p' "$B/device.conf"); do cp "$2/$p.img" "$B/images/"; done + root=$t/bundle-$cn/omamobile-rootfs-$cn-2026.11.09-aarch64.img.zst + zstd -q "$t/root.img" -o "$root" + (cd "$t/bundle-$cn" && sha256sum "${root##*/}" > "${root##*/}.sha256") + printf 'device: %s\nkernel modules: %s\n' "$cn" "$REL" > "${root%.img.zst}.MANIFEST" + printf 'kernel release: %s\nroot image: %s (%s)\n' "$REL" "${root##*/}" "$(cut -d' ' -f1 "$root.sha256")" > "$B/MANIFEST" + BUNDLE=$B + seal +} +seal() { # manifest.json and SHA256SUMS for the bundle as it is now + rm -f "$BUNDLE/manifest.json" + python3 -I tools/bundle-manifest --bundle "$BUNDLE" --rootfs "$root" --rootfs-sha256 "$(cut -d' ' -f1 "$root.sha256")" \ + --version 2026.11.09 --kernel-release "$REL" --channel stable + (cd "$BUNDLE" && find . -type f ! -name SHA256SUMS -printf '%P\n' | LC_ALL=C sort | xargs sha256sum > SHA256SUMS) +} +install() { # codename [args]; RAMDISK_PART_SIZE: vendor_kernel_boot_b's size + local cn=$1 p s; shift + rm -rf "$t/flashed" && mkdir -p "$t/flashed" + : > "$t/fastboot.log" + { + printf 'product:%s\nunlocked:yes\ncurrent-slot:a\nversion-bootloader:x\nversion-bootloader-a:x\nversion-bootloader-b:x\n' "$cn" + for p in boot init_boot vendor_boot vendor_kernel_boot dtbo; do + s=0x4000000; [[ $p != vendor_kernel_boot ]] || s=${RAMDISK_PART_SIZE:-$s} + printf 'partition-size:%s_b: %s\n' "$p" "$s" + done + } > "$t/vars" + # user, password twice, host, time zone, name, email, Wi-Fi, key, confirmation + printf '%s\n' alice 1234 1234 "" UTC "" "" "" "$t/owner.pub" "$cn" | + (umask 077; HOME=$t/home PATH="$t/fbin:$PATH" "$BUNDLE/install" --unsigned --work "$t/work" "$@") > "$t/out" 2>&1 +} +refused() { # why message: install refused, said why, flashed nothing + if install twelve; then fail "refuses $1"; fi + grep -cF -- "$2" "$t/out" >/dev/null || { cat "$t/out"; fail "says why it refuses $1"; } + [[ -z $(ls -A "$t/flashed") ]] || fail "nothing is flashed when it refuses $1" +} +# slot_b root images: the stand-in phone's slot B partitions hold those +# images, padded to 16 MiB, as fastboot leaves them +slot_b() { + local R=$1 i=0 p + rm -rf "$R/sys/block" "$R/dev" && mkdir -p "$R/dev" + echo 'console=null androidboot.slot_suffix=_b' > "$R/proc/cmdline" + for p in boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata; do + i=$((i + 1)) + mkdir -p "$R/sys/block/sda/sda$i" + printf 'MAJOR=8\nMINOR=%d\nDEVNAME=sda%d\nDEVTYPE=partition\nPARTN=%d\nPARTNAME=%s\n' "$i" "$i" "$i" "$p" > "$R/sys/block/sda/sda$i/uevent" + echo 0 > "$R/sys/block/sda/sda$i/ro" + echo $((16 * 1024 * 2)) > "$R/sys/block/sda/sda$i/size" + if [[ -f $2/${p%_b}.img ]]; then cp "$2/${p%_b}.img" "$R/dev/sda$i"; else : > "$R/dev/sda$i"; fi + truncate -s 16M "$R/dev/sda$i" + done +} +hook_plan() { # root: the hook's dry run on that slot B + hook "$1" write --dry-run > "$t/plan.log" 2>&1 || { cat "$t/plan.log"; fail "omamobile-boot-update write --dry-run"; } +} + +for dev in "twelve gs201 0x9855001 0x800 vendor_kernel_boot" "six gs101 0x9845001 0x1000 vendor_boot"; do + read -r cn soc id page rimg <<<"$dev" + if [[ $cn == twelve ]]; then imgs="boot init_boot vendor_boot vendor_kernel_boot dtbo"; else imgs="boot vendor_boot dtbo"; fi + rw="$(for p in $imgs; do printf '%s_b ' "$p"; done)userdata" + R=$t/r-$cn + phone "$R" "$cn" "$soc" "$id" "$page" "$imgs" "$rw" + hook_build "$R" "$t/bundle-images-$cn" + # What install-phone writes into the root image, the file the hook reads + cp "$t/owner.pub" "$R/root/.ssh/authorized_keys" + hook_build "$R" "$t/hook-images-$cn" + grep -c "Initramfs: the owner's authorized_keys appended" "$t/hook.log" >/dev/null || fail "$cn: the hook appends the owner's key" + cmp -s "$t/bundle-images-$cn/$rimg.img" "$t/hook-images-$cn/$rimg.img" && fail "$cn: the owner's key changes the hook's $rimg.img" + + bundle "$cn" "$t/bundle-images-$cn" + install "$cn" || { cat "$t/out"; fail "$cn: the bundle installs"; } + for p in $imgs; do + [[ -f $t/flashed/${p}_b ]] || fail "$cn: ${p}_b is flashed" + cmp -s "$t/flashed/${p}_b" "$t/hook-images-$cn/$p.img" || + fail "$cn: ${p}_b is not the image omamobile-boot-update builds with the owner's key" + done + grep -c "flash ${rimg}_b $t/work/ramdisk/$rimg.img" "$t/out" >/dev/null || fail "$cn: the plan names the image copy" + cmp -s "$BUNDLE/images/$rimg.img" "$t/bundle-images-$cn/$rimg.img" || fail "$cn: the bundle's own image is left alone" + [[ -z $(ls -A "$t/work") ]] || fail "$cn: the work directory is emptied" + cp -r "$t/flashed" "$t/installed-$cn" + pass "$cn: the bundle's install flashes the hook's own $rimg.img (the owner's key appended) and the hook's other images" +done + +# The hook's first run after the install: slot B already holds its images. +# Slot B as the bundle's images left it (before this change) still needed +# the rewrite #564 found. +R=$t/r-twelve rw="boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata" +mkdir -p "$t/installed" +for p in boot init_boot vendor_boot vendor_kernel_boot dtbo; do cp "$t/installed-twelve/${p}_b" "$t/installed/$p.img"; done +slot_b "$R" "$t/installed" +hook_plan "$R" +grep -c 'Slot B already holds these images: nothing to write' "$t/plan.log" >/dev/null || { cat "$t/plan.log"; fail "the hook finds slot B already holding its images"; } +slot_b "$R" "$t/bundle-images-twelve" +hook_plan "$R" +grep -cE 'vendor_kernel_boot\.img +-> .* write$' "$t/plan.log" >/dev/null || { cat "$t/plan.log"; fail "the bundle's own image is the one the hook rewrites"; } +pass "the hook's first run after the install writes nothing; after the bundle's own images it rewrote vendor_kernel_boot" + +# A ramdisk within 80 bytes of the end of its last page, so the owner's key +# (about 170 bytes of lz4; 171 on the 2026.11.08 install) makes the image a +# page longer: on a partition that holds only the bundle's image, install +# stops before anything is flashed (twelve's pages are 2 KiB) +head -c 60000 /dev/urandom > "$t/random" +n=20000 slack=2048 +for _ in $(seq 20); do + s=$t/stage-fit && rm -rf "$s" && mkdir -p "$s/etc" + echo '#!/bin/sh' > "$s/init"; echo "$rw" > "$s/etc/omamobile-ufs-rw"; head -c "$n" "$t/random" > "$s/payload" + (cd "$s" && find . | LC_ALL=C sort | cpio -o -H newc --owner=0:0 --quiet) | lz4 -l -9 -q > "$t/fit.lz4" + slack=$(( (2048 - $(stat -c %s "$t/fit.lz4") % 2048) % 2048 )) + ((slack <= 80)) && break + # aim for 40 bytes short of the boundary; lz4 grows about a byte per byte of random data + d=$((slack - 40)); ((d <= 1024)) || d=$((d - 2048)) + n=$((n + d)) +done +((slack <= 80)) || fail "a fixture ramdisk within 80 bytes of a page boundary (got $slack)" +rm "$R/root/.ssh/authorized_keys" +cp "$t/fit.lz4" "$R/usr/lib/omamobile/twelve/initramfs.cpio.lz4" +hook_build "$R" "$t/fit-images" +bundle twelve "$t/fit-images" +size=$(stat -c %s "$t/fit-images/vendor_kernel_boot.img") +RAMDISK_PART_SIZE=$(printf '0x%x' "$size") refused "an image that no longer fits its partition" \ + "with the owner's key is $((size + 2048)) bytes, larger than vendor_kernel_boot_b ($size bytes)" +pass "an image the owner's key makes too large for its partition is refused before anything is flashed" + +# Each step of the append that fails (a stand-in that exits 1 on that call) +# stops the install with a message, rather than flashing the ramdisk without +# the key: copying the key, cpio packing it, lz4 recompressing +for step in "install 600 can't stage the owner's key" "cpio -o cpio couldn't pack the owner's key" \ + "lz4 -l lz4 couldn't recompress vendor_kernel_boot.img's ramdisk"; do + read -r tool arg why <<<"$step" + mkdir -p "$t/bad-$tool" + printf '#!/bin/bash\nfor a; do [[ $a == %q ]] && exit 1; done\nexec %q "$@"\n' "$arg" "$(type -P "$tool")" > "$t/bad-$tool/$tool" + chmod +x "$t/bad-$tool/$tool" + PATH=$t/bad-$tool:$PATH refused "a failing $tool $arg in the key's append" "$why" +done +pass "a failing step of the key's append (install, cpio or lz4) stops the install before anything is flashed" + +# The ramdisk image under an AVB footer can't take the key: refused +printf 'AVB_FOOTERS="vendor_kernel_boot:0x4000000"\nAVB_ROLLBACK_INDEX=1\nAVB_SECURITY_PATCH=2026-07-05\n' >> "$BUNDLE/device.conf" +seal +refused "a ramdisk image with an AVB footer" "AVB_FOOTERS names vendor_kernel_boot, which carries the ramdisk" +pass "a ramdisk image the descriptor signs is refused before anything is flashed" + +# A development initramfs that already holds SSH keys (bin/omamobile +# initramfs without --release): the hook refuses it, so its builder packs +# the images directly, and install flashes them as built +phone "$R" twelve gs201 0x9855001 0x800 "boot init_boot vendor_boot vendor_kernel_boot dtbo" "$rw" root/.ssh/authorized_keys +(umask 022; MKBOOTIMG=$mk "$PKG/lib/omamobile-boot-images" --descriptor "$R/usr/share/omamobile/device.conf" \ + --kernel "$R/usr/lib/modules/$REL/vmlinuz" --dtbs "$R/usr/lib/modules/$REL/dtbs" --dtbo "$R/usr/lib/omamobile/twelve/dtbo.img" \ + --initramfs "$R/usr/lib/omamobile/twelve/initramfs.cpio.lz4" --out "$t/dev-images") > "$t/dev.log" 2>&1 || + { cat "$t/dev.log"; fail "packing a development set"; } +bundle twelve "$t/dev-images" +install twelve || { cat "$t/out"; fail "a development initramfs installs"; } +cmp -s "$t/flashed/vendor_kernel_boot_b" "$t/dev-images/vendor_kernel_boot.img" || fail "a ramdisk with SSH keys is flashed as built" +grep -c "already holds SSH keys" "$t/out" >/dev/null || fail "says the ramdisk is left as built" +pass "a ramdisk that already holds SSH keys is flashed as built" + +# An image that isn't a v4 vendor boot image stops the install before any write +echo 'vendor_kernel_boot image' > "$BUNDLE/images/vendor_kernel_boot.img" +seal +refused "a ramdisk image that isn't a vendor boot image" 'not an Android vendor boot image' +pass "an image that isn't a v4 vendor boot image is refused before anything is flashed" diff --git a/tools/install-phone b/tools/install-phone index 28f808952..6c727a255 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -30,6 +30,8 @@ # development phone, set up by hand, has no such file), shows exactly what it # will run, and waits for the codename to be typed. The phone's first boot # (omamobile-firstboot) creates the user from that file and deletes it. +# The key also goes into a copy of slot B's ramdisk image, as +# omamobile-boot-update appends it, so the initramfs's rescue SSH takes it. # # install-phone [--dry-run] [options] # --dry-run read the phone's variables (read-only) and prepare @@ -147,8 +149,8 @@ if [[ $mode == repository ]]; then [[ $built_for == "$CODENAME" ]] \ || die "$images holds images for '${built_for:-an unknown device}', not $CODENAME (run: DEVICE=$CODENAME bin/omamobile images)" fi -for tool in "$FASTBOOT" debugfs dumpe2fs e2fsck zstd openssl sha256sum; do - command -v "$tool" >/dev/null || die "$tool not found (fastboot: android-tools; debugfs: e2fsprogs)" +for tool in "$FASTBOOT" debugfs dumpe2fs e2fsck zstd openssl sha256sum lz4 cpio python3; do + command -v "$tool" >/dev/null || die "$tool not found (fastboot: android-tools; debugfs: e2fsprogs; lz4: lz4; cpio: cpio)" done BOOT_IMAGES=${D[BOOT_IMAGES]:-} UFS_RW=${D[UFS_RW]:-} [[ -n $BOOT_IMAGES && -n $UFS_RW ]] || die "$conf: BOOT_IMAGES and UFS_RW are required" @@ -416,10 +418,12 @@ if [[ $current_slot == b && $reinstall != 1 ]]; then fi [[ $current_slot == b ]] && note "slot b holds Omarchy Mobile (--reinstall): it will be replaced" # Each image must fit its slot B partition, an AVB_FOOTERS image exactly. +declare -A part_size=() for p in $BOOT_IMAGES; do psize=$(sed -nE "s/^partition-size:${p}_b:[ ]*(0x[0-9a-fA-F]{1,12})\$/\1/p" <<<"$vars" | head -1) [[ -n $psize ]] || die "the phone reports no partition-size for ${p}_b" psize=$((psize)) isize=$(stat -c %s "$images/$p.img") + part_size[$p]=$psize ((isize <= psize)) || die "$p.img is $isize bytes, larger than ${p}_b ($psize bytes)" [[ -z ${avb_size[$p]:-} ]] || ((avb_size[$p] == psize)) \ || die "$conf: AVB_FOOTERS pads $p to ${avb_size[$p]} bytes, but ${p}_b is $psize: its footer would not be at the end of the partition" @@ -490,9 +494,9 @@ work=${work:-${XDG_CACHE_HOME:-$HOME/.cache}/omamobile-install} mkdir -p "$work" chmod 700 "$work" copy=$work/root.img sparse=$work/root.simg conf_file=$work/firstboot.conf keys_file=$work/authorized_keys -boot_conf=$work/boot-update.conf +boot_conf=$work/boot-update.conf ramdisk_dir=$work/ramdisk # The copy holds the password hash (and any Wi-Fi password): never left behind -cleanup() { rm -f "$conf_file" "$keys_file" "$boot_conf" "$copy" "$sparse"; } +cleanup() { rm -f "$conf_file" "$keys_file" "$boot_conf" "$copy" "$sparse"; rm -rf "$ramdisk_dir"; } trap cleanup EXIT ( umask 077 @@ -558,6 +562,100 @@ if command -v img2simg >/dev/null; then note "converted to an Android sparse image ($(du -h "$sparse" | cut -f1))" fi +# --- The owner's key in slot B's initramfs ----------------------------------- +# omamobile-boot-update builds slot B's ramdisk with root's authorized_keys +# appended (docs/RELEASES.md §5.2). Without the same here, the initramfs's +# rescue SSH has no owner key until the hook first runs, and that run rewrites +# an image the bundle should already hold (§11 step 4, #564). So a copy of the +# ramdisk image gets what lib/omamobile-boot-images appends, by its commands +# with the modes pacman's umask gives them: a cpio holding the key, after the +# shipped archive in one lz4 stream; vendor_ramdisk then sets the new size in +# the copy's v4 header and ramdisk table, where mkbootimg puts it. With the +# phone's lz4 and cpio that is the hook's image byte for byte, under any umask +# (tests/install-phone-initramfs). +# Inline, because a bundle ships this script alone. A ramdisk that already +# holds authorized_keys (a development initramfs) is left as built. +declare -A image_file=() +for p in $BOOT_IMAGES; do image_file[$p]=$images/$p.img; done +# vendor_ramdisk get IMG OUT, or vendor_ramdisk set IMG RAMDISK OUT +vendor_ramdisk() { + python3 -I - "$@" <<'PY' +import struct, sys + +def bad(why): + print("%s: %s" % (sys.argv[2], why), file=sys.stderr) + sys.exit(1) + +img = open(sys.argv[2], "rb").read() +# vendor_boot_img_hdr_v4: magic, header_version, page_size, kernel_addr, +# ramdisk_addr, vendor_ramdisk_size (offset 24), cmdline[2048], tags_addr, +# name[16], header_size (2096), dtb_size, dtb_addr (u64), +# vendor_ramdisk_table_size (2112), _entry_num, _entry_size, bootconfig_size +if len(img) < 2128 or img[:8] != b"VNDRBOOT": + bad("not an Android vendor boot image") +version, page, _, _, rd_size = struct.unpack_from("<5I", img, 8) +hdr_size, dtb_size = struct.unpack_from("<2I", img, 2096) +table_size, entries, entry_size, bootconfig_size = struct.unpack_from("<4I", img, 2112) +if version != 4 or hdr_size != 2128: + bad("not a v4 vendor boot image") +if page < 2048 or page & (page - 1): + bad("odd page size %d" % page) +if (entries, entry_size, table_size) != (1, 108, 108): + bad("expected one vendor ramdisk, as bin/omamobile images builds") +pad = lambda n: -(-n // page) * page +o_rd = pad(2128) +o_dtb = o_rd + pad(rd_size) +o_table = o_dtb + pad(dtb_size) +if len(img) != o_table + pad(table_size) + pad(bootconfig_size): + bad("its sections don't add up to its size (a footer?)") +if struct.unpack_from("<2I", img, o_table) != (rd_size, 0): + bad("its ramdisk table doesn't match its header") +if sys.argv[1] == "get": + open(sys.argv[3], "wb").write(img[o_rd:o_rd + rd_size]) + sys.exit(0) +ramdisk = open(sys.argv[3], "rb").read() +head, tail = bytearray(img[:o_rd]), bytearray(img[o_dtb:]) +struct.pack_into("/dev/null | cpio -it --quiet 2>/dev/null \ + | grep -cE '^(\./)?root/\.ssh/authorized_keys$' || true) + if [[ $keyfiles != 0 ]]; then + note "$ramdisk_image.img's initramfs already holds SSH keys (a development build): left as built" + else + # omamobile-boot-images' commands. The hook runs under pacman's umask + # (022), so root/ is 0755; its mode is set here because not every + # install(1) ignores the umask for parents (uutils' follows it). + extra=$ramdisk_dir/extra + install -d -m 755 "$extra/root" && install -d -m 700 "$extra/root/.ssh" && + install -m 600 "$keys_file" "$extra/root/.ssh/authorized_keys" && + find "$extra" -exec touch -h -d @0 {} + || die "can't stage the owner's key for the initramfs" + reproducible=() + cpio --help 2>/dev/null | grep -c -- --reproducible >/dev/null && reproducible=(--reproducible) + (cd "$extra" && find . -mindepth 1 | LC_ALL=C sort | cpio -o -H newc --owner=0:0 --quiet "${reproducible[@]}") > "$ramdisk_dir/extra.cpio" || + die "cpio couldn't pack the owner's key for the initramfs" + { lz4 -dc "$ramdisk_dir/shipped.cpio.lz4" && cat "$ramdisk_dir/extra.cpio"; } | lz4 -l -9 -q > "$ramdisk_dir/initramfs.cpio.lz4" || + die "lz4 couldn't recompress $ramdisk_image.img's ramdisk with the owner's key" + vendor_ramdisk set "$images/$ramdisk_image.img" "$ramdisk_dir/initramfs.cpio.lz4" "$ramdisk_dir/$ramdisk_image.img" || + die "rewriting $ramdisk_image.img with the new ramdisk failed" + isize=$(stat -c %s "$ramdisk_dir/$ramdisk_image.img") + ((isize <= part_size[$ramdisk_image])) || + die "$ramdisk_image.img with the owner's key is $isize bytes, larger than ${ramdisk_image}_b (${part_size[$ramdisk_image]} bytes)" + image_file[$ramdisk_image]=$ramdisk_dir/$ramdisk_image.img + note "the owner's key appended to $ramdisk_image.img's initramfs (as omamobile-boot-update appends it)" + fi +fi + # --- What will be written ---------------------------------------------------- # The only way this script writes to the phone. PART must be one of the # descriptor's boot images with _b on the allowlist, or userdata. @@ -585,7 +683,7 @@ note "SSH key: ${ssh_key:0:40}... (root and $username)" note "root image: $(basename "$rootfs")" echo echo "These commands, and only these, will run:" -for p in $BOOT_IMAGES; do echo " fastboot flash ${p}_b $images/$p.img"; done +for p in $BOOT_IMAGES; do echo " fastboot flash ${p}_b ${image_file[$p]}"; done echo " fastboot flash userdata $userdata_image" echo " fastboot --set-active=b" echo " fastboot reboot" @@ -598,7 +696,7 @@ answer=$(ask "Type the phone's codename ($CODENAME) to go ahead") [[ $answer == "$CODENAME" ]] || die "not confirmed; nothing was written" ((dry)) && say "Dry run: printing the commands instead of running them" || say "Flashing" -for p in $BOOT_IMAGES; do flash "${p}_b" "$images/$p.img"; done +for p in $BOOT_IMAGES; do flash "${p}_b" "${image_file[$p]}"; done flash userdata "$userdata_image" run --set-active=b run reboot diff --git a/tools/make-bundle b/tools/make-bundle index 375cdfe45..701d23edc 100755 --- a/tools/make-bundle +++ b/tools/make-bundle @@ -197,8 +197,8 @@ download lost the file's execute bit). $rootname must be next to this directory: unpacking $name.tar (the one-file download, checked with "sha256sum -c $name.tar.sha256") puts both side by side. If you downloaded this directory file by file, run "chmod +x tools/*" in it -once. It needs fastboot (android-tools), e2fsprogs, zstd, openssl, -gpg and python3 on this computer. The installer checks the phone first, asks for your user +once. It needs fastboot (android-tools), e2fsprogs, zstd, lz4, cpio, +openssl, gpg and python3 on this computer. The installer checks the phone first, asks for your user name, password, Wi-Fi and SSH key, shows exactly what it will write, and waits for you to type "$DEVICE".