From 8d5571e50e12a239dd9ac27d270ef914e2775e7f Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Mon, 9 Nov 2026 17:58:18 +0000 Subject: [PATCH 1/2] install-phone: append the owner's key to slot B's initramfs, as the boot hook does MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On a fresh release install the first omamobile-boot-update run rewrote vendor_kernel_boot_b with root's authorized_keys appended to the initramfs. Until then the rescue SSH in the initramfs had no owner key (exactly when it is needed: a root that doesn't mount), and the bundle did not boot the bytes the hook writes later (RELEASES §11 step 4). install-phone now does to a copy of the ramdisk image (vendor_kernel_boot, or vendor_boot without one) what lib/omamobile-boot-images does: a cpio holding the key, made with the builder's commands under pacman's umask (022), after the shipped archive in one lz4 stream. A small inline Python then sets the new ramdisk size in the copy's v4 vendor boot header and ramdisk table, where mkbootimg puts it. The code is inline because a bundle ships install-phone alone (L-497). The copy is what gets flashed; the bundle's own image and the write path (slot B and userdata only, the allowlist, the typed codename) are unchanged. A ramdisk that already holds SSH keys (a development initramfs) is flashed as built. An image the key makes too large for its partition, a ramdisk image under an AVB footer, or one that isn't a single-ramdisk v4 vendor boot image stops the install before any write. install-phone now needs lz4, cpio and python3 on the laptop. tests/install-phone-initramfs runs the hook's own build (omamobile-boot- update build on a stand-in phone root) with and without the owner's key, installs a bundle of the keyless set with the bundle's install against a fake fastboot, and checks that every flashed image is the hook's byte for byte, for a gs201 and a gs101 layout; the hook's dry run on a slot B holding them then writes nothing (on the bundle's own images it rewrites vendor_kernel_boot, as in #564). It also covers each refusal (each one mutation-checked). It fails on main and skips without mkbootimg. tests/install-phone's ramdisk images are now real v4 vendor boot images. CI installs lz4 explicitly. Fixes #564 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/checks.yml | 2 +- docs/INSTALL.md | 4 +- docs/RELEASES.md | 6 +- tests/all | 5 +- tests/install-phone | 22 ++- tests/install-phone-initramfs | 263 ++++++++++++++++++++++++++++++++++ tools/install-phone | 109 +++++++++++++- tools/lib/alarm.sh | 117 ++++++++++++++- 8 files changed, 503 insertions(+), 25 deletions(-) create mode 100755 tests/install-phone-initramfs diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 405ef3d6d..29631b556 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -45,7 +45,7 @@ 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 # Arch's nodejs can lag a simdjson soname bump: on 2026-10-29 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 ac96ac586..2e78fe5e1 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -182,7 +182,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`. @@ -322,9 +322,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 fa3efaac9..ade544089 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 @@ -42,7 +43,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/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; do +for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/install-phone-initramfs 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/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; do echo "# $t" "$t" done diff --git a/tests/install-phone b/tests/install-phone index 78a3c1e26..1e843eef8 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..125117f56 --- /dev/null +++ b/tests/install-phone-initramfs @@ -0,0 +1,263 @@ +#!/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, 4 KiB pages) +# and a gs101 layout (the ramdisk in vendor_boot, 2 KiB pages), +# +# 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 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, whatever the caller's. +# +# 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" | + 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 0x1000 vendor_kernel_boot" "six gs101 0x9845001 0x800 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 +head -c 60000 /dev/urandom > "$t/random" +n=20000 slack=4096 +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=$(( (4096 - $(stat -c %s "$t/fit.lz4") % 4096) % 4096 )) + ((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 <= 2048)) || d=$((d - 4096)) + 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 + 4096)) 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" + +# 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 0x1000 "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 0339dca95..5656122b0 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 @@ -82,7 +84,7 @@ while (($#)); do --bootloader-slots-match) slots_match=${2:?}; shift ;; --reinstall) reinstall=1 ;; --unsigned) unsigned=1 ;; - -h|--help) sed -n '3,50p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;; + -h|--help) awk 'NR > 2 && !/^#/ { exit } NR > 2 { sub(/^# ?/, ""); print }' "${BASH_SOURCE[0]}"; exit 0 ;; *) die "unknown argument $1 (see --help)" ;; esac shift @@ -146,7 +148,7 @@ 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 +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)" done BOOT_IMAGES=${D[BOOT_IMAGES]:-} UFS_RW=${D[UFS_RW]:-} @@ -415,10 +417,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" @@ -489,9 +493,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 @@ -557,6 +561,99 @@ 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 +# under pacman's umask: 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 (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; pacman runs the hook with umask 022 + ( + umask 022 + extra=$ramdisk_dir/extra + mkdir -p "$extra" + install -d -m 700 "$extra/root/.ssh" + install -m 600 "$keys_file" "$extra/root/.ssh/authorized_keys" + find "$extra" -exec touch -h -d @0 {} + + 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" + { lz4 -dc "$ramdisk_dir/shipped.cpio.lz4" || die "$ramdisk_image.img's ramdisk is not lz4"; cat "$ramdisk_dir/extra.cpio"; } | lz4 -l -9 -q > "$ramdisk_dir/initramfs.cpio.lz4" + ) || die "appending the owner's key to the initramfs failed" + 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. @@ -584,7 +681,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" @@ -597,7 +694,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/lib/alarm.sh b/tools/lib/alarm.sh index 151db306a..296aefff0 100644 --- a/tools/lib/alarm.sh +++ b/tools/lib/alarm.sh @@ -28,9 +28,22 @@ ALARM_TARBALL=$ALARM_CACHE/ArchLinuxARM-aarch64-latest.tar.gz # hyprland). Stable, the release the phone's Omarchy patches follow; edge # carries release candidates (4.5.0rc1 on 2026-10-20) they may not apply to. OMARCHY_CHANNEL=${OMARCHY_CHANNEL:-stable} -# The keyring that signs [omarchy], taken from this (Omarchy) laptop, which -# already trusts it: the container can't bootstrap that trust on its own. +# The keyring that signs [omarchy]. On an Omarchy machine it is the host's +# (/usr/share/pacman/keyrings, kept current by pacman), which already trusts +# it: the container can't bootstrap that trust on its own. A host without it +# (a build server on another distribution) fetches omarchy-keyring from +# Omarchy's repository instead, checked like the tarball: its detached +# signature must be by Omarchy's packaging key, pinned below, and its +# SHA-256 the one the repository's index lists. The three files are cached +# in $ALARM_CACHE/omarchy-keyring with the package and its signature, which +# are verified again at every use; the cache is refreshed when the index +# names a newer version and kept when it names an older one (a rollback is +# refused). Setting OMARCHY_KEYRING_DIR uses that folder and nothing else. +OMARCHY_KEYRING_DIR_SET=${OMARCHY_KEYRING_DIR:+1} OMARCHY_KEYRING_DIR=${OMARCHY_KEYRING_DIR:-/usr/share/pacman/keyrings} +# Omarchy : the key omarchy-keyring's omarchy-trusted names +OMARCHY_KEY=40DFB630FF42BCFFB047046CF0134EE680CAC571 +OMARCHY_PKGS_URL=${OMARCHY_PKGS_URL:-https://pkgs.omarchy.org} # A contributor's local signing key (tools/local-key-init; --local in # build-packages and build-rootfs): the path of its exported public key. # When set, the build image also trusts that key, locally signed, and only @@ -82,6 +95,98 @@ alarm_fetch() { alarm_die "bad or missing signature on $ALARM_TARBALL (expected key $ALARM_KEY)" } +# Verify an omarchy-keyring package against its detached signature by the +# pinned packaging key $OMARCHY_KEY. $1: the package, $2: its .sig. +_omarchy_keyring_verify() { + local gnupg status + gnupg=$(mktemp -d) + if [[ ! -s $ALARM_CACHE/omarchy-pkgs.asc ]]; then + GNUPGHOME=$gnupg gpg -q --batch --keyserver "$ALARM_KEYSERVER" --recv-keys "$OMARCHY_KEY" || + { rm -rf "$gnupg"; alarm_die "could not fetch Omarchy's packaging key $OMARCHY_KEY"; } + GNUPGHOME=$gnupg gpg -q --batch --armor --export "$OMARCHY_KEY" >"$ALARM_CACHE/omarchy-pkgs.asc" + else + GNUPGHOME=$gnupg gpg -q --batch --import "$ALARM_CACHE/omarchy-pkgs.asc" + fi + status=$(GNUPGHOME=$gnupg gpg --batch --status-fd 1 --verify "$2" "$1" 2>/dev/null || true) + rm -rf "$gnupg" + grep -Eq "^\[GNUPG:\] VALIDSIG .* $OMARCHY_KEY\$" <<<"$status" +} + +# The cached keyring is used only while its package still verifies and the +# three files are the package's own, byte for byte. $1: the cache folder. +_omarchy_keyring_cached_ok() { + local dir=$1 pkg f + pkg=$(cat "$dir/PACKAGE" 2>/dev/null) || return 1 + [[ -n $pkg && -s $dir/$pkg && -s $dir/$pkg.sig ]] || return 1 + _omarchy_keyring_verify "$dir/$pkg" "$dir/$pkg.sig" || return 1 + for f in omarchy.gpg omarchy-trusted omarchy-revoked; do + cmp -s "$dir/$f" <(bsdtar -xOf "$dir/$pkg" "usr/share/pacman/keyrings/$f") || return 1 + done +} + +# The folder holding omarchy.gpg, omarchy-trusted and omarchy-revoked; prints +# it. The host's when it has all three or OMARCHY_KEYRING_DIR was set, +# otherwise the verified copy described at OMARCHY_KEYRING_DIR above: the +# package and its signature are kept beside it and checked again at every +# use, and an index naming an older keyring than the cached one is refused. +alarm_omarchy_keyring() { + local f have=1 dir=$ALARM_CACHE/omarchy-keyring base want file sum tmp cached + for f in omarchy.gpg omarchy-trusted omarchy-revoked; do [[ -f $OMARCHY_KEYRING_DIR/$f ]] || have=0; done + if ((have)) || [[ -n $OMARCHY_KEYRING_DIR_SET ]]; then + ((have)) || alarm_die "no Omarchy keyring in OMARCHY_KEYRING_DIR=$OMARCHY_KEYRING_DIR (omarchy.gpg, omarchy-trusted, omarchy-revoked)" + echo "$OMARCHY_KEYRING_DIR"; return + fi + command -v bsdtar >/dev/null || alarm_die "fetching Omarchy's keyring needs bsdtar (libarchive-tools on Debian and Ubuntu)" + cached=$(cat "$dir/VERSION" 2>/dev/null || true) + if [[ -n $cached ]] && ! _omarchy_keyring_cached_ok "$dir"; then + alarm_say "The cached Omarchy keyring no longer verifies: fetching it again" >&2 + rm -rf "$dir"; cached= + fi + base=$OMARCHY_PKGS_URL/$OMARCHY_CHANNEL/aarch64 + tmp=$(mktemp -d) + if ! curl -fsSL -o "$tmp/omarchy.db" "$base/omarchy.db"; then + rm -rf "$tmp" + [[ -n $cached ]] || alarm_die "no Omarchy keyring on this host and $base/omarchy.db can't be fetched" + alarm_say "Omarchy's index can't be fetched: using the cached keyring ($cached, verified)" >&2 + echo "$dir"; return + fi + # The index isn't signed: it only names the file; the package's own + # signature and its listed SHA-256 are what's checked + read -r want file sum < <(bsdtar -xOf "$tmp/omarchy.db" --include='omarchy-keyring-*/desc' | + awk '/^%VERSION%$/ { getline; v = $0 } /^%FILENAME%$/ { getline; f = $0 } /^%SHA256SUM%$/ { getline; s = $0 } END { print v, f, s }') + [[ -n $file && -n $sum ]] || { rm -rf "$tmp"; alarm_die "omarchy-keyring isn't in $base/omarchy.db"; } + if [[ -n $cached && $want == "$cached" ]]; then + rm -rf "$tmp"; echo "$dir"; return + fi + # sort -V orders pkgver-pkgrel; Omarchy's keyring has no epoch (an epoch + # would read as older here, which stops the build rather than letting one in) + if [[ -n $cached && $(printf '%s\n%s\n' "$want" "$cached" | sort -V | tail -1) == "$cached" ]]; then + rm -rf "$tmp" + alarm_die "Omarchy's index names omarchy-keyring $want, older than the cached $cached: refusing a rollback (remove $dir to accept it)" + fi + alarm_say "Fetching Omarchy's keyring (omarchy-keyring $want)" >&2 + curl -fsSL -o "$tmp/$file" "$base/$file" && curl -fsSL -o "$tmp/$file.sig" "$base/$file.sig" || + { rm -rf "$tmp"; alarm_die "could not download $base/$file"; } + [[ $(sha256sum <"$tmp/$file" | cut -d' ' -f1) == "$sum" ]] || + { rm -rf "$tmp"; alarm_die "$file doesn't match the SHA-256 Omarchy's index lists"; } + _omarchy_keyring_verify "$tmp/$file" "$tmp/$file.sig" || + { rm -rf "$tmp"; alarm_die "bad or missing signature on $file (expected key $OMARCHY_KEY)"; } + # The version the index gave must be the signed package's own, or an old + # package Omarchy once signed could be cached under a newer number + [[ $(bsdtar -xOf "$tmp/$file" .PKGINFO | sed -n 's/^pkgver = //p') == "$want" ]] || + { rm -rf "$tmp"; alarm_die "$file isn't omarchy-keyring $want, the version Omarchy's index names"; } + mkdir -p "$tmp/x" && + bsdtar -xf "$tmp/$file" -C "$tmp/x" usr/share/pacman/keyrings/omarchy.gpg \ + usr/share/pacman/keyrings/omarchy-trusted usr/share/pacman/keyrings/omarchy-revoked || + { rm -rf "$tmp"; alarm_die "$file holds no Omarchy keyring"; } + f=$tmp/x/usr/share/pacman/keyrings + echo "$want" >"$f/VERSION"; echo "$file" >"$f/PACKAGE" + mv "$tmp/$file" "$tmp/$file.sig" "$f/" + rm -rf "$dir.new" && mv "$f" "$dir.new" && rm -rf "$dir" && mv "$dir.new" "$dir" + rm -rf "$tmp" + echo "$dir" +} + # The plain tarball as a Docker image; prints its tag. alarm_base_image() { local sum tag @@ -100,13 +205,11 @@ alarm_base_image() { # pacman.conf, base-devel, arch-install-scripts and a "builder" user with # this laptop's uid. Prints its tag. $1: the repository root. alarm_build_image() { - local root=$1 base tag hash ctx name + local root=$1 base tag hash ctx name keyring base=$(alarm_base_image) - for f in omarchy.gpg omarchy-trusted omarchy-revoked; do - [[ -f $OMARCHY_KEYRING_DIR/$f ]] || alarm_die "no $OMARCHY_KEYRING_DIR/$f: this needs Omarchy's keyring (omarchy-keyring) on the build machine" - done + keyring=$(alarm_omarchy_keyring) || exit 1 ctx=$(mktemp -d) - cp "$OMARCHY_KEYRING_DIR"/omarchy{.gpg,-trusted,-revoked} "$ctx/" + cp "$keyring"/omarchy{.gpg,-trusted,-revoked} "$ctx/" cp "$root"/packages/omamobile-keyring/omamobile{.gpg,-trusted,-revoked} "$ctx/" if [[ -n $ALARM_LOCAL_KEY ]]; then [[ -f $ALARM_LOCAL_KEY ]] || alarm_die "no local key $ALARM_LOCAL_KEY" From a3b6793c03eef3b8ba0f0c986b53368463d8095e Mon Sep 17 00:00:00 2001 From: Victor Vogel Date: Mon, 9 Nov 2026 19:55:42 +0000 Subject: [PATCH 2/2] install-phone: stop on any failing step of the key's append, and list lz4 and cpio This addresses the review on #576. The append ran in ( ... ) || die only for umask 022, and bash turns errexit off in a subshell whose status is tested, so a failing install or cpio carried on and only the last lz4 pipeline decided: a cpio that failed gave the shipped ramdisk recompressed without the key, flashed with "the owner's key appended". The steps now run at the top level, each with its own die: staging the key, cpio packing it, lz4 recompressing. Dropping the umask alone would change the bytes on some laptops: GNU install -d makes the parent root/ 0755 under any umask, but uutils' install (coreutils 0.8.0 here, Ubuntu's default) gives it the umask's mode, so under 077 or 002 vendor_kernel_boot no longer matched the hook's. root/ is now made 0755 explicitly, the mode pacman's umask gives the hook's. tests/install-phone-initramfs runs every install under umask 077 and checks that a stand-in install, cpio or lz4 that exits 1 stops it with the step's message before anything is flashed (it fails on 8d5571e5's installer at the first injected install failure; each die removed in turn fails it). The gs201 fixture now uses 2 KiB pages as the Pixel 7 Pro does (VENDOR_PAGESIZE=0x800, as every descriptor in device/), and the gs101 one 4 KiB, so both page sizes stay covered. The test passes with the caller's umask at 022, 077 and 002, and fails under 077 and 002, and from a 022 caller, without the explicit root/ mode. The bundle's README.txt lists lz4 and cpio with the other tools the install needs, and install-phone's "not found" hint names their packages. Co-Authored-By: Claude Opus 5.5 --- tests/install-phone-initramfs | 44 ++++++++++++++++++++++++----------- tools/install-phone | 37 +++++++++++++++-------------- tools/make-bundle | 4 ++-- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/tests/install-phone-initramfs b/tests/install-phone-initramfs index 125117f56..0f36f6a22 100755 --- a/tests/install-phone-initramfs +++ b/tests/install-phone-initramfs @@ -2,8 +2,9 @@ # 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, 4 KiB pages) -# and a gs101 layout (the ramdisk in vendor_boot, 2 KiB pages), +# 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 @@ -18,10 +19,12 @@ # 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 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, whatever the caller's. +# 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, @@ -142,7 +145,7 @@ install() { # codename [args]; RAMDISK_PART_SIZE: vendor_kernel_boot_b's size } > "$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" | - HOME=$t/home PATH="$t/fbin:$PATH" "$BUNDLE/install" --unsigned --work "$t/work" "$@" > "$t/out" 2>&1 + (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 @@ -169,7 +172,7 @@ 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 0x1000 vendor_kernel_boot" "six gs101 0x9845001 0x800 vendor_boot"; do +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" @@ -213,17 +216,17 @@ pass "the hook's first run after the install writes nothing; after the bundle's # 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 +# stops before anything is flashed (twelve's pages are 2 KiB) head -c 60000 /dev/urandom > "$t/random" -n=20000 slack=4096 +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=$(( (4096 - $(stat -c %s "$t/fit.lz4") % 4096) % 4096 )) + 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 <= 2048)) || d=$((d - 4096)) + 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)" @@ -233,9 +236,22 @@ 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 + 4096)) bytes, larger than vendor_kernel_boot_b ($size bytes)" + "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 @@ -245,7 +261,7 @@ 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 0x1000 "boot init_boot vendor_boot vendor_kernel_boot dtbo" "$rw" root/.ssh/authorized_keys +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 || diff --git a/tools/install-phone b/tools/install-phone index 5656122b0..bec47aca3 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -149,7 +149,7 @@ if [[ $mode == repository ]]; then || 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 lz4 cpio python3; do - command -v "$tool" >/dev/null || die "$tool not found (fastboot: android-tools; debugfs: e2fsprogs)" + 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" @@ -567,10 +567,11 @@ fi # 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 -# under pacman's umask: 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 (tests/install-phone-initramfs). +# 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=() @@ -631,19 +632,19 @@ if [[ -n $ramdisk_image ]]; then 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; pacman runs the hook with umask 022 - ( - umask 022 - extra=$ramdisk_dir/extra - mkdir -p "$extra" - install -d -m 700 "$extra/root/.ssh" - install -m 600 "$keys_file" "$extra/root/.ssh/authorized_keys" - find "$extra" -exec touch -h -d @0 {} + - 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" - { lz4 -dc "$ramdisk_dir/shipped.cpio.lz4" || die "$ramdisk_image.img's ramdisk is not lz4"; cat "$ramdisk_dir/extra.cpio"; } | lz4 -l -9 -q > "$ramdisk_dir/initramfs.cpio.lz4" - ) || die "appending the owner's key to the initramfs failed" + # 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") diff --git a/tools/make-bundle b/tools/make-bundle index fd08dec68..b0f50e181 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".