From 9ffd1b1fabf6386cb37fc7de8e9f58453f8e89f8 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:14:33 -0500 Subject: [PATCH 01/10] initramfs: the storage guard opens the running slot's boot images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The guard's allowlist named slot B's images literally, so a release booted from slot A would have left its own boot images read-only and slot B writable: the wrong way round (docs/ACTIVE-SLOT-PLAN.md §0, §4). The packed list stays in slot form (X_b). ufs_rw_load now reads the running slot from androidboot.slot_suffix in /proc/bootconfig, else /proc/cmdline, and maps each X_b to X of that slot, keeping userdata; the other slot stays read-only. With no readable suffix (none, two that disagree, anything but _a or _b) every boot partition stays read-only and userdata alone is writable. On slot B the list, the log line and the partitions left writable are exactly what they were. bin/omamobile initramfs packs /etc/omamobile-ufs-rw-slot ("running"), the marker a format-2 bundle is checked for; any other content in it closes the guard. tests/storage-guard covers slot A and B from bootconfig and the command line, bootconfig winning, no suffix, two that disagree, a bad value, the marker absent or malformed, the hand-over check on A and a second LUN on A. Co-Authored-By: Claude Opus 5.5 --- bin/omamobile | 6 ++++ initramfs/init | 59 +++++++++++++++++++++++++++++++--- tests/storage-guard | 78 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 134 insertions(+), 9 deletions(-) diff --git a/bin/omamobile b/bin/omamobile index f4ba4a3c3..eba54e0ab 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -102,6 +102,8 @@ die() { printf '\e[31merror:\e[0m %s\n' "$*" >&2; exit 1; } # Every name a descriptor's UFS_RW may hold: slot B's boot images and # userdata. Never persist, efs*, modem*, devinfo, vbmeta*, bootloader, radio # or anything in slot A. initramfs/init's ufs_rw_load accepts the same set. +# The names are in slot form: the init maps each X_b to X of the slot the +# phone runs from, and leaves the other slot read-only (ACTIVE-SLOT-PLAN §4). UFS_RW_ALLOWED="boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata" # Load device/$DEVICE/device.conf into DEV_* variables. The descriptor is data, @@ -587,6 +589,10 @@ initramfs_config() { # The storage guard's allowlist. Without this file the init leaves every # partition read-only. printf '%s\n' "${rw[*]}" > "$etc/omamobile-ufs-rw" + # The list is in slot form (X_b); this marker says the init maps it to the + # slot the phone runs from (docs/ACTIVE-SLOT-PLAN.md §4). A format-2 + # bundle is refused without it. + printf '%s\n' running > "$etc/omamobile-ufs-rw-slot" if [[ -n ${DEV_UFS_RW_LUNS:-} ]]; then printf '%s\n' "$DEV_UFS_RW_LUNS" > "$etc/omamobile-ufs-rw-luns" fi diff --git a/initramfs/init b/initramfs/init index f5782c331..5dfe50dce 100755 --- a/initramfs/init +++ b/initramfs/init @@ -46,25 +46,56 @@ fi # Nothing in this initramfs runs mdev/udev, so no one else touches the disks. ufs_ro_log() { echo "omamobile: ufs-ro: $*" > /dev/kmsg; } # Storage guard. Everything on the UFS is read-only except an allowlist of -# partitions we own: slot B's boot images (in-place kernel updates) and -# userdata (the Arch root). The kernel only lets a partition be written if +# partitions we own: the running slot's boot images (in-place kernel +# updates) and userdata (the Arch root). The kernel only lets a partition be written if # its whole disk is writable too, so the main disk (sda) stays writable but # its whole-disk node is removed, so nothing can write across partitions by # accident; a LUN the descriptor's UFS_RW_LUNS names is guarded the same way. # The other LUNs (on Pixels sdb, sdc, sdd: bootloader images) are read-only # at the disk level. Calibration partitions (efs, persist, modem_userdata...) -# and all of slot A stay read-only. +# and all of the other slot stay read-only. # The allowlist is the descriptor's UFS_RW, which bin/omamobile initramfs # checks and packs into /etc/omamobile-ufs-rw. It fails closed: a missing, # empty or malformed file leaves every partition read-only, so Arch does not # start and the initramfs shell stays up over SSH. Whatever the file says, # only slot B's boot images and userdata can be named. +# The packed names are in slot form: X_b means "X of the slot this boot runs +# from" (docs/ACTIVE-SLOT-PLAN.md §4). The slot is the bootloader's +# androidboot.slot_suffix, from /proc/bootconfig, else /proc/cmdline. On +# slot B the list is what it says; on slot A each X_b becomes X_a and slot +# B stays read-only; with no readable suffix (none, or two that disagree) +# every boot partition stays read-only and userdata alone is writable. +# bin/omamobile initramfs packs /etc/omamobile-ufs-rw-slot ("running") to +# say this init maps the list so; any other content in it closes the guard. UFS_RW="" UFS_RW_LUNS="" # Is $1 a disk that holds allowlisted partitions? sda always. ufs_rw_disk() { case " sda $UFS_RW_LUNS " in *" $1 "*) true ;; *) false ;; esac; } +# The running slot's suffix: _a or _b, or nothing. Run it in a subshell +# ($(...)): it turns off globbing for the command line's words. +ufs_slot_add() { # one value: the first is kept, another one makes it unknown + case "$1" in _a|_b) ;; *) ufs_slot=bad; return 0 ;; esac + if [ -z "$ufs_slot" ]; then ufs_slot=$1 + elif [ "$ufs_slot" != "$1" ]; then ufs_slot=bad; fi +} +ufs_slot_suffix() { + local w + ufs_slot="" + set -f + if [ -r /proc/bootconfig ]; then + for w in $(sed -n 's/^androidboot\.slot_suffix *= *"\(.*\)" *$/\1/p' /proc/bootconfig); do + ufs_slot_add "$w" + done + fi + if [ -z "$ufs_slot" ] && [ -r /proc/cmdline ]; then + for w in $(cat /proc/cmdline); do + case "$w" in androidboot.slot_suffix=*) ufs_slot_add "${w#androidboot.slot_suffix=}" ;; esac + done + fi + case "$ufs_slot" in _a|_b) echo "$ufs_slot" ;; esac +} ufs_rw_load() { - local list p + local list p slot mapped if [ -s /etc/omamobile-nfsroot ]; then ufs_ro_log "NFS root requested: every UFS partition read-only" return 0 @@ -94,6 +125,26 @@ ufs_rw_load() { return 0 ;; esac done + if [ -e /etc/omamobile-ufs-rw-slot ] && [ "$(cat /etc/omamobile-ufs-rw-slot)" != running ]; then + ufs_ro_log "malformed slot marker in /etc/omamobile-ufs-rw-slot: every partition read-only" + return 0 + fi + # Slot form to the running slot: X_b is X of the slot this boot runs from + slot=$(ufs_slot_suffix) + mapped="" + for p in "$@"; do + case "$p" in + *_b) [ -z "$slot" ] || mapped="$mapped ${p%_b}$slot" ;; + *) mapped="$mapped $p" ;; + esac + done + if [ -n "$slot" ]; then + ufs_ro_log "running from slot ${slot#_}: its boot images are the allowlisted ones, the other slot stays read-only" + else + ufs_ro_log "no slot suffix in /proc/bootconfig or /proc/cmdline: every boot partition read-only, userdata writable" + fi + # shellcheck disable=SC2086 # split into names on purpose + set -- $mapped UFS_RW="$*" ufs_ro_log "allowlist: $UFS_RW" # LUNs besides sda that hold allowlisted partitions (the descriptor's diff --git a/tests/storage-guard b/tests/storage-guard index f51d7fafb..f0536fb42 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -10,7 +10,12 @@ # whole-disk node is removed either way. A LUN named in UFS_RW_LUNS (a # test descriptor's sde, enchilada's layout) is guarded as sda is; without # that list, or with a bad one or a closed allowlist, it stays read-only at -# the disk level. +# the disk level. The packed list is in slot form (ACTIVE-SLOT-PLAN §4): +# booted from slot B (androidboot.slot_suffix in /proc/bootconfig, else +# /proc/cmdline) it holds as packed; from slot A each X_b becomes X_a and +# slot B stays read-only; with no readable suffix (none, two that disagree, +# another value) only userdata is writable. bin/omamobile packs the marker +# /etc/omamobile-ufs-rw-slot ("running"); any other content closes the guard. set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } @@ -24,6 +29,7 @@ for d in cheetah:"$cheetah_rw" felix:"$cheetah_rw" lynx:"$cheetah_rw" akita:"$ch dev=${d%%:*} want=${d#*:} DEVICE=$dev bin/omamobile initramfs-config "$t/etc-$dev" > "$t/log" 2>&1 || { cat "$t/log"; fail "$dev: initramfs-config"; } [[ $(cat "$t/etc-$dev/omamobile-ufs-rw") == "$want" ]] || fail "$dev packs its UFS_RW (got $(cat "$t/etc-$dev/omamobile-ufs-rw"))" + [[ $(cat "$t/etc-$dev/omamobile-ufs-rw-slot") == running ]] || fail "$dev packs the running-slot marker" if [[ $dev == felix ]]; then [[ $(cat "$t/etc-$dev/omamobile-simplefb-trig") == $'0x1c241030 0x01003061\n0x1c240030 0x00003061' ]] \ || fail "felix packs both trigger pairs" @@ -37,6 +43,7 @@ grep -q '^ initramfs_config "\$stage/etc"$' bin/omamobile || fail "initramfs pa [[ ! -e $t/etc-cheetah/omamobile-ufs-rw-luns && ! -e $t/etc-oriole/omamobile-ufs-rw-luns ]] || fail "the Pixels pack no LUN list" pass "the Pixels pack no LUN list" pass "cheetah, felix and akita pack the six-name list, oriole and bluejay their three boot images and userdata" +pass "every image packs the running-slot marker /etc/omamobile-ufs-rw-slot" # --- Pack time: refused descriptors, in a repository-shaped tree --- R=$t/repo @@ -92,7 +99,9 @@ grep -q '^ufs_rw_load$' "$t/guard.sh" || fail "the init loads the allowlist befo grep -q '^ufs_ro_pass() {$' "$t/guard.sh" || fail "ufs_ro_pass found in initramfs/init" B=$t/b sed -e "s|> /dev/kmsg|>> /dev/kmsg|" -e "s|/sys/block/|$B/sys/block/|g" -e "s|/dev/|$B/dev/|g" -e "s|/run/|$B/run/|g" \ - -e "s|/etc/omamobile-|$B/etc/omamobile-|g" -e 's/\[ -b /[ -e /' "$t/guard.sh" > "$t/guard-test.sh" + -e "s|/etc/omamobile-|$B/etc/omamobile-|g" -e "s|/proc/|$B/proc/|g" -e 's/\[ -b /[ -e /' "$t/guard.sh" > "$t/guard-test.sh" +grep -c "$B/proc/bootconfig" "$t/guard-test.sh" >/dev/null && grep -c "$B/proc/cmdline" "$t/guard-test.sh" >/dev/null \ + || fail "the guard reads the slot from /proc/bootconfig and /proc/cmdline" mkdir -p "$t/bin" cat > "$t/bin/blockdev" < "$B/proc/bootconfig" + [[ ${1:-_b} == none ]] || printf 'androidboot.slot_suffix = "%s"\n' "${1:-_b}" >> "$B/proc/bootconfig" + printf 'console=ttynull panic=10\n' > "$B/proc/cmdline" echo 0 > "$B/sys/block/sda/ro"; : > "$B/dev/sda" local i=0 p for p in $parts; do @@ -137,6 +152,7 @@ writable() { # names of the partitions left writable on a disk (sda), in GPT ord done printf '%s' "${out% }" } +sorted() { tr ' ' '\n' | sort | tr '\n' ' ' | sed 's/ $//'; } disk_checks() { # $1: case name; $2: sde when sde is a listed LUN [[ ! -e $B/dev/sda ]] || fail "$1: /dev/sda's whole-disk node is removed" [[ $(cat "$B/sys/block/sda/ro") == 0 ]] || fail "$1: sda itself stays writable (the kernel needs it for its partitions)" @@ -151,12 +167,57 @@ disk_checks() { # $1: case name; $2: sde when sde is a listed LUN } fake_disks -printf '%s\n' "$cheetah_rw" > "$B/etc/omamobile-ufs-rw" +cp "$t/etc-cheetah/omamobile-ufs-rw" "$t/etc-cheetah/omamobile-ufs-rw-slot" "$B/etc/" guard [[ $(writable) == "$cheetah_rw" ]] || fail "cheetah's list leaves exactly its six partitions writable (got: $(writable))" grep -q "allowlist: $cheetah_rw" "$B/dev/kmsg" || fail "the allowlist is logged" +grep -q "running from slot b: its boot images" "$B/dev/kmsg" || fail "the running slot is logged" disk_checks cheetah -pass "cheetah's packed list: the same six partitions writable as the old hard-coded list" +pass "cheetah's packed list on slot B: the same six partitions writable as the old hard-coded list" + +# --- The running slot --- +cheetah_a="boot_a init_boot_a vendor_boot_a vendor_kernel_boot_a dtbo_a userdata" +slot_case() { # what, expected writable set (sorted), expected kmsg text; disks set up by the caller + cp "$t/etc-cheetah/omamobile-ufs-rw" "$B/etc/" + guard + [[ $(writable | sorted) == "$(sorted <<<"$2")" ]] || { cat "$B/dev/kmsg"; fail "$1: writable $2 (got: $(writable))"; } + grep -q -- "$3" "$B/dev/kmsg" || { cat "$B/dev/kmsg"; fail "$1: says so on kmsg ($3)"; } + disk_checks "$1" + pass "$1" +} +fake_disks _a; cp "$t/etc-cheetah/omamobile-ufs-rw-slot" "$B/etc/" +slot_case "slot A (bootconfig): its five boot images and userdata, slot B read-only" "$cheetah_a" "allowlist: $cheetah_a" +grep -q "running from slot a" "$B/dev/kmsg" || fail "slot A is logged" +unguarded() { PATH="$t/bin:$PATH" sh -c ". '$t/guard-test.sh'; ufs_unguarded"; } +[[ -z $(unguarded) ]] || fail "slot A: nothing outside its allowlist is writable (got: $(unguarded))" +echo 0 > "$B/sys/block/sda/sda2/ro" +[[ $(unguarded) == "sda2 (boot_b)" ]] || fail "slot A: a reopened boot_b is reported at the hand-over (got: $(unguarded))" +pass "slot A: the hand-over check reports slot B's boot image made writable" +fake_disks none; printf 'console=ttynull androidboot.slot_suffix=_a\n' > "$B/proc/cmdline" +slot_case "slot A from the command line when bootconfig names none" "$cheetah_a" "running from slot a" +fake_disks _b; printf 'androidboot.slot_suffix=_a\n' > "$B/proc/cmdline" +slot_case "bootconfig wins over the command line" "$cheetah_rw" "running from slot b" +fake_disks none +slot_case "no slot suffix: userdata alone writable" "userdata" "every boot partition read-only, userdata writable" +fake_disks none; rm -f "$B/proc/bootconfig" "$B/proc/cmdline" +slot_case "no /proc/bootconfig or /proc/cmdline: userdata alone writable" "userdata" "no slot suffix" +fake_disks _b; printf 'androidboot.slot_suffix = "_a"\n' >> "$B/proc/bootconfig" +slot_case "two suffixes that disagree: userdata alone writable" "userdata" "no slot suffix" +fake_disks _c +slot_case "a suffix that is neither _a nor _b: userdata alone writable" "userdata" "no slot suffix" +fake_disks none; printf 'androidboot.slot_suffix=_b androidboot.slot_suffix=* x\n' > "$B/proc/cmdline" +slot_case "a glob for a suffix on the command line: userdata alone writable" "userdata" "no slot suffix" +fake_disks _a +slot_case "no slot marker: the list is still mapped to the running slot" "$cheetah_a" "running from slot a" +for bad in slot-b '' 'running x'; do + fake_disks _a; printf '%s\n' "$bad" > "$B/etc/omamobile-ufs-rw-slot" + cp "$t/etc-cheetah/omamobile-ufs-rw" "$B/etc/" + guard + [[ -z $(writable) ]] || fail "slot marker '$bad': every partition read-only (writable: $(writable))" + grep -q "malformed slot marker" "$B/dev/kmsg" || { cat "$B/dev/kmsg"; fail "slot marker '$bad': says so on kmsg"; } + disk_checks "slot marker '$bad'" +done +pass "a slot marker other than 'running' leaves every partition read-only" # The check boot_arch makes before handing over: nothing outside the list. unguarded() { PATH="$t/bin:$PATH" sh -c ". '$t/guard-test.sh'; ufs_unguarded"; } @@ -239,6 +300,13 @@ echo 0 > "$B/sys/block/sde/sde4/ro" [[ $(unguarded) == "sde4 (vbmeta_b)" ]] || fail "lun: a reopened partition on sde is reported (got: $(unguarded))" pass "the lun lists: sde guarded as sda, boot_b alone writable there; the hand-over check covers sde" +fake_disks _a +cp "$t/etc-lun/omamobile-ufs-rw" "$t/etc-lun/omamobile-ufs-rw-luns" "$B/etc/" +guard +[[ $(writable sde) == boot_a && $(writable) == "boot_a userdata" ]] || fail "lun on slot A: boot_a on sde and sda (got: sde $(writable sde), sda $(writable))" +disk_checks "lun on slot A" sde +pass "the lun lists on slot A: boot_a writable on sde, boot_b there read-only" + fake_disks cp "$t/etc-lun/omamobile-ufs-rw" "$B/etc/" guard From bf39ea121ceb886c002af5aeaaa0fbf41fdff0e6 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:18:47 -0500 Subject: [PATCH 02/10] omamobile-boot-update 0.2.0: SLOT=a|b, written only while running from it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A release installed on the slot it ran from (docs/ACTIVE-SLOT-PLAN.md §2) updates its kernel in place on that slot. boot-update.conf gains SLOT=a|b; absent means b, so every phone installed before this keeps writing slot B exactly as before. The tool refuses unless every androidboot.slot_suffix it finds in /proc/bootconfig and /proc/cmdline is _$SLOT (a release on A that fell back to a development system on B never writes B; two values that disagree, or none, are refused), finds, checks and writes only _$SLOT, while the descriptor's allowlist stays in slot form (_b) as the storage guard reads it. For a SLOT other than b it also refuses a device package whose initramfs lacks the running-slot marker: an init from before it opens slot B's images whatever slot boots. previous/ and current/ record their slot (none: b); a rollback refuses a set saved from the other slot. Messages name the slot. An older tool refuses a SLOT= line ("anything else is an error"), so a format-2 phone with an old package fails closed. No device package depends on this one with a version: the kernel package's plain depends is enough, as an old tool on a SLOT=a phone refuses rather than writes. Co-Authored-By: Claude Opus 5.5 --- packages/omamobile-boot-update/PKGBUILD | 2 +- packages/omamobile-boot-update/README.md | 27 ++- .../bin/omamobile-boot-update | 228 +++++++++++------- .../etc/boot-update.conf | 6 + .../omamobile-boot-update/lib/descriptor.sh | 11 +- .../libalpm/90-omamobile-boot.hook | 2 +- packages/omamobile-boot-update/test/all | 98 +++++++- packages/omamobile-boot-update/version | 2 +- 8 files changed, 268 insertions(+), 108 deletions(-) diff --git a/packages/omamobile-boot-update/PKGBUILD b/packages/omamobile-boot-update/PKGBUILD index 6d8d297eb..47479cc38 100644 --- a/packages/omamobile-boot-update/PKGBUILD +++ b/packages/omamobile-boot-update/PKGBUILD @@ -3,7 +3,7 @@ pkgname=omamobile-boot-update pkgver=$(cat "${startdir:-.}/version") pkgrel=1 -pkgdesc="Omarchy Mobile: rebuild a phone's slot-B boot images from linux-omamobile and write them, guarded and verified (pacman hook, opt-in)" +pkgdesc="Omarchy Mobile: rebuild a phone's boot images (slot B, or the slot boot-update.conf names) from linux-omamobile and write them, guarded and verified (pacman hook, opt-in)" arch=(any) url="https://github.com/derekross/omarchy-mobile" license=(MIT Apache-2.0) diff --git a/packages/omamobile-boot-update/README.md b/packages/omamobile-boot-update/README.md index 4f652cd0e..15667a40d 100644 --- a/packages/omamobile-boot-update/README.md +++ b/packages/omamobile-boot-update/README.md @@ -16,14 +16,25 @@ overlays or the initramfs. It writes slot B only when `tools/install-phone` writes on a release install. A development phone has no such file, and `bin/omamobile push` stays slot B's only writer there. -It writes only `_b` for the descriptor's `BOOT_IMAGES`, found by GPT -name on the main UFS LUN, on the storage guard's allowlist and left writable -by the guard, and reads every image back. It never reboots. The kernel it -builds from is the release the installed `linux-omamobile` names -(`/usr/share/omamobile/linux/kernel.release`), never the newest-looking -directory in `/usr/lib/modules` (kernel-modules-hook leaves a copy of the -running release there after every upgrade). It refuses a boot that doesn't -name slot B. +It writes only `_` for the descriptor's `BOOT_IMAGES`, found by +GPT name on the main UFS LUN, on the storage guard's allowlist (in slot form, +`_b`) and left writable by the guard, and reads every image back. It +never reboots. The kernel it builds from is the release the installed +`linux-omamobile` names (`/usr/share/omamobile/linux/kernel.release`), never +the newest-looking directory in `/usr/lib/modules` (kernel-modules-hook +leaves a copy of the running release there after every upgrade). + +`SLOT` is `b` unless `boot-update.conf` says `SLOT=a`, which a release +installer writes for a phone it installed on the slot it ran from, slot A +([ACTIVE-SLOT-PLAN](../../docs/ACTIVE-SLOT-PLAN.md) §2). It refuses a boot +that isn't from `SLOT` (`androidboot.slot_suffix` in `/proc/bootconfig` and +`/proc/cmdline`), so a release on slot A that fell back to a development +system on slot B never writes B. For a `SLOT` other than `b` it also +refuses a device package whose initramfs lacks the running-slot guard +(`etc/omamobile-ufs-rw-slot`): an older init opens slot B's images whatever +slot boots. `previous/` and `current/` record the slot they describe; a +rollback refuses a set saved from the other slot. Before 0.2.0 the tool +refused any `SLOT=` line, so a phone with an older package fails closed. ## Commands diff --git a/packages/omamobile-boot-update/bin/omamobile-boot-update b/packages/omamobile-boot-update/bin/omamobile-boot-update index 282d0fe16..6a76751f4 100755 --- a/packages/omamobile-boot-update/bin/omamobile-boot-update +++ b/packages/omamobile-boot-update/bin/omamobile-boot-update @@ -1,40 +1,44 @@ #!/bin/bash # SPDX-License-Identifier: MIT -# Turn the installed kernel package into this phone's slot-B boot images and -# write them, on the phone (docs/RELEASES.md §5.3). What bin/omamobile images -# and push do from the laptop, with the same descriptor, the same commands -# and the same guarded, verified write. +# Turn the installed kernel package into this phone's boot images and write +# them to its slot (SLOT= in boot-update.conf, b when absent), on the phone +# (docs/RELEASES.md §5.3). What bin/omamobile images and push do from the +# laptop, with the same descriptor, commands and guarded, verified write. # -# omamobile-boot-update status what is installed, built and on slot B +# omamobile-boot-update status what is installed, built and on the slot # omamobile-boot-update preflight [--json] # read-only: would a write be allowed now? # (omamobile-update asks before a kernel update) # omamobile-boot-update build build the images into /var/lib/omamobile/boot/next # omamobile-boot-update write [--dry-run] [--yes] [--ignore-battery] -# build, check, save slot B, write, verify +# build, check, save the slot, write, verify # omamobile-boot-update rollback [--dry-run] [--yes] [--ignore-battery] # write the saved previous images back # omamobile-boot-update hook the pacman hook (90-omamobile-boot.hook) # # What it writes, and only that: each BOOT_IMAGES entry of the installed # descriptor (/usr/share/omamobile/device.conf) to the partition whose GPT -# name is _b on the main UFS LUN, when that name is on the -# descriptor's storage-guard allowlist (UFS_RW) and the kernel says the -# partition is writable (ro 0: the initramfs guard decides, this never -# changes it). Never slot A, userdata, bootloader, vbmeta, devinfo, or any -# other partition; never a whole disk; never blockdev --setrw. It never -# reboots: the next restart boots the new images. +# name is _ on the main UFS LUN, when _b is on the +# descriptor's storage-guard allowlist (UFS_RW, in slot form: the guard maps +# it to the running slot) and the kernel says the partition is writable +# (ro 0: the initramfs guard decides, this never changes it). SLOT is b +# unless /etc/omamobile/boot-update.conf says SLOT=a (a release installed on +# the slot it ran from, docs/ACTIVE-SLOT-PLAN.md §2), and the boot must be +# from that slot. Never the other slot, userdata, bootloader, vbmeta, +# devinfo, or any other partition; never a whole disk; never blockdev +# --setrw. It never reboots: the next restart boots the new images. # # Steps of a write: # 1. /etc/omamobile/boot-update.conf is data (KEY=value, never sourced); # the hook does nothing unless it says AUTO_WRITE=yes (development -# phones keep bin/omamobile push as slot B's only writer). +# phones keep bin/omamobile push as slot B's only writer). SLOT=a|b +# names the slot (absent: b). # 2. The descriptor must be valid (the same rules as bin/omamobile's # load_device, lib/descriptor.sh), and /proc/device-tree/compatible # must start with its COMPATIBLE: one Pixel's package never writes -# another's slot B. The boot must name slot B -# (androidboot.slot_suffix=_b in /proc/bootconfig or /proc/cmdline); -# slot A, or no slot named at all, is refused. +# another's slot. The boot must be from SLOT (androidboot.slot_suffix +# in /proc/bootconfig and /proc/cmdline, every one it finds _); +# the other slot, two that disagree or no slot named is refused. # 3. The release the installed linux-omamobile names in # /usr/share/omamobile/linux/kernel.release (never the newest-looking # directory: release names end in a commit hash, and kernel-modules-hook @@ -44,16 +48,17 @@ # 4. The device package's files (/usr/lib/omamobile//: dtbo.img, # initramfs.cpio.lz4, overlays/.dtbo) must be there; the # initramfs must carry exactly the descriptor's allowlist, and no -# developer SSH keys. +# developer SSH keys; for a SLOT other than b, also the running-slot +# marker (an older init would open slot B's images, not SLOT's). # 5. lib/omamobile-boot-images packs the images (bin/omamobile images' # commands, AVB footers included) into /var/lib/omamobile/boot/next. # 6. Each image must fit its partition, and an AVB_FOOTERS image must be # the partition's exact size. Partitions that already hold their image # are left alone; when all do, nothing is written. # 7. The battery must be at 30% or more, or charging. -# 8. Slot B's current contents (whole partitions) and that release's -# module tree go to /var/lib/omamobile/boot/previous, unless slot B -# was written since this boot (the kernel's write counters in +# 8. The slot's current contents (whole partitions) and that release's +# module tree go to /var/lib/omamobile/boot/previous (with the slot +# they came from in SLOT), unless the slot was written since this boot (the kernel's write counters in # /sys/block///stat: this tool, push, a rollback; a fastboot # flash comes before the boot and counts as booted) or a write was # left incomplete: then previous keeps the last set that did boot. @@ -96,7 +101,7 @@ CPIO=${CPIO:-cpio} say() { printf '==> %s\n' "$*"; } note() { printf ' %s\n' "$*"; } die() { printf 'error: omamobile-boot-update: %s\n' "$*" >&2; record failed "$*"; exit 1; } -refuse() { printf 'error: omamobile-boot-update: not writing slot B: %s\n' "$*" >&2; record refused "$*"; exit 1; } +refuse() { printf 'error: omamobile-boot-update: not writing slot %s: %s\n' "${SLOT^^}" "$*" >&2; record refused "$*"; exit 1; } # shellcheck source=../lib/descriptor.sh source "$LIB/descriptor.sh" @@ -108,7 +113,7 @@ record() { [[ $COMMAND == write || $COMMAND == rollback || $COMMAND == hook ]] || return 0 [[ ${DRY_RUN:-0} == 0 ]] || return 0 mkdir -p "$RUN" 2>/dev/null || return 0 - # kernel_changed: the release slot B now holds is not the running one, so + # kernel_changed: the release the slot now holds is not the running one, so # the next restart changes the kernel (not only the initramfs or dtbo) local changed=false [[ -z $RELEASE || $RELEASE == "$UNAME_R" ]] || changed=true @@ -128,26 +133,31 @@ real_root() { [[ -z $R ]]; } # ---------------------------------------------------------------- inputs -# /etc/omamobile/boot-update.conf: AUTO_WRITE=yes|no (default no) and an -# optional AVB_KEY=/absolute/path. Data, never sourced; anything else in it -# is an error, so a typo can't turn the hook on or off by accident. +# /etc/omamobile/boot-update.conf: AUTO_WRITE=yes|no (default no), SLOT=a|b +# (default b: every phone installed before the slot was named runs from B) +# and an optional AVB_KEY=/absolute/path. Data, never sourced; anything else +# in it is an error, so a typo can't turn the hook on or off by accident. AUTO_WRITE=no AVB_KEY= +SLOT=b SLOT_FROM_DEFAULT=1 load_conf() { - local line n=0 - AUTO_WRITE=no AVB_KEY= + local line n=0 slot= + AUTO_WRITE=no AVB_KEY= SLOT=b SLOT_FROM_DEFAULT=1 [[ -f $CONF ]] || return 0 while IFS= read -r line || [[ -n $line ]]; do n=$((n + 1)) [[ $line =~ ^[[:space:]]*(#.*)?$ ]] && continue if [[ $line =~ ^AUTO_WRITE=\"?(yes|no)\"?$ ]]; then AUTO_WRITE=${BASH_REMATCH[1]} + elif [[ $line =~ ^SLOT=\"?(a|b)\"?$ ]]; then + slot=${BASH_REMATCH[1]} elif [[ $line =~ ^AVB_KEY=\"?(/[A-Za-z0-9_./-]+)\"?$ && $line != *..* ]]; then AVB_KEY=${BASH_REMATCH[1]} else - refuse "$CONF:$n: expected AUTO_WRITE=yes|no or AVB_KEY=/path, got '$line'" + refuse "$CONF:$n: expected AUTO_WRITE=yes|no, SLOT=a|b or AVB_KEY=/path, got '$line'" fi done < "$CONF" + [[ -z $slot ]] || SLOT=$slot SLOT_FROM_DEFAULT= } # The board must be the descriptor's: the first string of the root @@ -160,18 +170,36 @@ check_board() { || refuse "this board is '$got', but the installed descriptor is for $DEV_CODENAME ('$DEV_COMPATIBLE')" } -# Linux runs from slot B, and the bootloader must say so: slot A, or a boot -# that names no slot, is not ours to update. +# Every androidboot.slot_suffix the bootloader passed, one per line: +# /proc/bootconfig's (key = "value") and /proc/cmdline's (key=value). +slot_suffixes() { + local w words=() + [[ ! -r $PROC/bootconfig ]] || + sed -n 's/^androidboot\.slot_suffix *= *"\(.*\)" *$/\1/p' "$PROC/bootconfig" + if [[ -r $PROC/cmdline ]]; then + read -ra words < "$PROC/cmdline" || true + for w in "${words[@]}"; do + [[ $w != androidboot.slot_suffix=* ]] || printf '%s\n' "${w#androidboot.slot_suffix=}" + done + fi +} + +# Linux runs from SLOT, and the bootloader must say so: the other slot, two +# values that disagree, or a boot that names no slot is not ours to update +# (a release on slot A that fell back to a development system on B must +# not write B). check_slot() { - local f b=0 - for f in "$PROC/cmdline" "$PROC/bootconfig"; do - [[ -r $f ]] || continue - if grep -E 'androidboot\.slot_suffix *= *"?_a' "$f" >/dev/null; then - refuse "the bootloader says this boot is from slot A ($f)" - fi - grep -E 'androidboot\.slot_suffix *= *"?_b' "$f" >/dev/null && b=1 + local v seen=() other + while IFS= read -r v; do seen+=("$v"); done < <(slot_suffixes) + ((${#seen[@]})) || refuse "neither $PROC/bootconfig nor $PROC/cmdline says which slot this boot is from (androidboot.slot_suffix=_$SLOT)" + for v in "${seen[@]}"; do + case $v in + "_$SLOT") ;; + _a|_b) other=${v#_} + refuse "the bootloader says this boot is from slot ${other^^}, but $CONF names slot ${SLOT^^}${SLOT_FROM_DEFAULT:+ (it has no SLOT= line: B)}" ;; + *) refuse "the bootloader's androidboot.slot_suffix is '$v', not _a or _b" ;; + esac done - ((b)) || refuse "neither $PROC/bootconfig nor $PROC/cmdline says this boot is from slot B (androidboot.slot_suffix=_b)" } device_dir() { echo "$R/usr/lib/omamobile/$DEV_CODENAME"; } @@ -194,6 +222,16 @@ check_device_files() { | "$CPIO" -i --quiet --to-stdout 'etc/omamobile-ufs-rw' './etc/omamobile-ufs-rw' 2>/dev/null || true) [[ $allow == "$(read -ra a <<<"$DEV_UFS_RW"; echo "${a[*]}")" ]] \ || refuse "the device package's initramfs allowlist ('$allow') is not the descriptor's UFS_RW ('$DEV_UFS_RW')" + # An init from before the running-slot guard opens slot B's images + # whatever slot boots: on another slot the next boot's guard would leave + # SLOT's images read-only and B's writable + if [[ $SLOT != b ]]; then + local mark + mark=$("$LZ4" -dc "$d/initramfs.cpio.lz4" 2>/dev/null \ + | "$CPIO" -i --quiet --to-stdout 'etc/omamobile-ufs-rw-slot' './etc/omamobile-ufs-rw-slot' 2>/dev/null || true) + [[ $mark == running ]] \ + || refuse "the device package's initramfs has no running-slot guard (etc/omamobile-ufs-rw-slot): its storage guard opens slot B's images, not slot ${SLOT^^}'s" + fi keyfiles=$("$LZ4" -dc "$d/initramfs.cpio.lz4" 2>/dev/null | "$CPIO" -it --quiet 2>/dev/null \ | grep -cE '^(\./)?root/\.ssh/authorized_keys$' || true) [[ $keyfiles == 0 ]] || refuse "the device package's initramfs carries developer SSH keys (not a release initramfs)" @@ -229,13 +267,13 @@ declare -A PART_DEV=() PART_BYTES=() sysfs_part() { echo "$SYS/block/$DEV_MAIN_LUN/$1"; } -# Find _b on the main LUN by GPT name: exactly one partition, on the -# allowlist, writable, of a readable size. +# Find _ on the main LUN by GPT name: exactly one partition, on +# the allowlist (as _b, its slot form), writable, of a readable size. find_partition() { local img=$1 want s name found=() bytes sectors - want=${img}_b + want=${img}_$SLOT [[ " $BU_IMAGES_ALLOWED " == *" $img "* ]] || refuse "$img is not a boot image" - [[ " $DEV_UFS_RW " == *" $want "* ]] || refuse "$want is not on the storage guard's allowlist (UFS_RW)" + [[ " $DEV_UFS_RW " == *" ${img}_b "* ]] || refuse "${img}_b is not on the storage guard's allowlist (UFS_RW)" for s in "$SYS/block/$DEV_MAIN_LUN/$DEV_MAIN_LUN"*; do [[ -f $s/uevent ]] || continue grep -cx "PARTNAME=$want" "$s/uevent" >/dev/null && found+=("${s##*/}") @@ -253,12 +291,13 @@ find_partition() { } # Re-checked right before every write: the node is the partition we found, -# the name is still _b on the allowlist, the guard left it writable. +# the name is still _, its slot form _b on the +# allowlist, and the guard left it writable. check_writable() { local img=$1 dev name want - dev=${PART_DEV[$img]} name=${dev##*/} want=${img}_b - [[ $want =~ ^(boot|init_boot|vendor_boot|vendor_kernel_boot|dtbo)_b$ ]] || refuse "refusing to write $want" - [[ " $DEV_UFS_RW " == *" $want "* && " $BU_UFS_RW_ALLOWED " == *" $want "* ]] || refuse "$want is not allowed" + dev=${PART_DEV[$img]} name=${dev##*/} want=${img}_$SLOT + [[ $SLOT == [ab] && $want =~ ^(boot|init_boot|vendor_boot|vendor_kernel_boot|dtbo)_[ab]$ ]] || refuse "refusing to write $want" + [[ " $DEV_UFS_RW " == *" ${img}_b "* && " $BU_UFS_RW_ALLOWED " == *" ${img}_b "* ]] || refuse "$want is not allowed" [[ $dev == "$DEV/$DEV_MAIN_LUN"[0-9]* ]] || refuse "$dev is not a partition of $DEV_MAIN_LUN" grep -cx "PARTNAME=$want" "$(sysfs_part "$name")/uevent" >/dev/null 2>&1 || refuse "$dev is no longer $want" if real_root; then @@ -290,14 +329,14 @@ check_sizes() { for img in $DEV_BOOT_IMAGES; do [[ -s $dir/$img.img ]] || refuse "missing $dir/$img.img" size=$(stat -c %s "$dir/$img.img") bytes=${PART_BYTES[$img]} - ((size <= bytes)) || refuse "$img.img ($size bytes) is larger than ${img}_b ($bytes bytes)" + ((size <= bytes)) || refuse "$img.img ($size bytes) is larger than ${img}_$SLOT ($bytes bytes)" avb=$(bu_avb_size "$img") if [[ -n $avb ]]; then - ((avb == bytes)) || refuse "AVB_FOOTERS pads $img to $avb bytes, but ${img}_b is $bytes: its footer would not be at the end" + ((avb == bytes)) || refuse "AVB_FOOTERS pads $img to $avb bytes, but ${img}_$SLOT is $bytes: its footer would not be at the end" ((size == bytes)) || refuse "$img.img is $size bytes; with its AVB footer it must be $bytes" fi if ((exact)); then - ((size == bytes)) || refuse "$img.img is $size bytes, ${img}_b is $bytes: not a dump of that partition" + ((size == bytes)) || refuse "$img.img is $size bytes, ${img}_$SLOT is $bytes: not a dump of that partition" fi done } @@ -373,16 +412,16 @@ confirm() { local what=$1 answer ((YES)) && return 0 [[ -t 0 ]] || refuse "$what needs --yes when not run in a terminal" - printf 'This writes %s to slot B of this %s. Type the codename (%s) to go on: ' "$what" "${DEV_NAME:-phone}" "$DEV_CODENAME" + printf 'This writes %s to slot %s of this %s. Type the codename (%s) to go on: ' "$what" "${SLOT^^}" "${DEV_NAME:-phone}" "$DEV_CODENAME" read -r answer [[ $answer == "$DEV_CODENAME" ]] || refuse "not confirmed" } -# Slot B's whole partitions and the release that booted from them, into +# The slot's whole partitions and the release that booted from them, into # previous/, swapped in only once complete. -# The slot B images written since this boot, from the kernel's own write +# The slot's images written since this boot, from the kernel's own write # counters (field 7 of /sys/block///stat, sectors written). A set -# that booted from slot B is still there exactly when none was written: a +# that booted from the slot is still there exactly when none was written: a # fastboot flash (the documented recovery) happens before the boot and is # not counted; this tool's own writes, bin/omamobile push and a rollback in # the same boot are. An unreadable counter counts as written, and so does @@ -394,7 +433,7 @@ confirm() { written_since_boot() { local img name f n out=() if [[ $(cat "$SYS/block/$DEV_MAIN_LUN/queue/iostats" 2>/dev/null) != 1 ]]; then - for img in $DEV_BOOT_IMAGES; do out+=("${img}_b"); done + for img in $DEV_BOOT_IMAGES; do out+=("${img}_$SLOT"); done echo "${out[*]} (I/O accounting off)" return 0 fi @@ -402,7 +441,7 @@ written_since_boot() { name=${PART_DEV[$img]##*/} f=$(sysfs_part "$name")/stat n=$(awk '{print $7}' "$f" 2>/dev/null || true) - [[ $n == 0 ]] || out+=("${img}_b") + [[ $n == 0 ]] || out+=("${img}_$SLOT") done echo "${out[*]}" } @@ -416,9 +455,18 @@ fake_count_write() { awk -v s="$(( ($2 + 511) / 512 ))" '{ $7 += s; print }' "$f" > "$f.new" && mv "$f.new" "$f" } +# The slot previous/ was saved from: its SLOT file, b when it has none (a +# set saved before the slot was recorded came from slot B). +previous_slot() { cat "$STATE/previous/SLOT" 2>/dev/null || echo b; } + save_previous() { local img dev bytes rel booted=yes need avail written new=$STATE/previous.new - # Booted means slot B unwritten since this boot, whatever any record says: + # A set saved from the other slot is no rollback for this one + if [[ -d $STATE/previous && $(previous_slot) != "$SLOT" ]]; then + note "previous/ holds slot $(previous_slot | tr ab AB)'s images, not slot ${SLOT^^}'s: replacing it" + rm -rf "$STATE/previous" + fi + # Booted means the slot unwritten since this boot, whatever any record says: # then it holds the running kernel's set rel=$UNAME_R written=$(written_since_boot) @@ -433,33 +481,34 @@ save_previous() { return 0 fi booted=no - note "an earlier write did not finish and nothing was saved before it: saving slot B as it is, for what it is worth" + note "an earlier write did not finish and nothing was saved before it: saving slot ${SLOT^^} as it is, for what it is worth" elif [[ $booted == no && -d $STATE/previous ]]; then if [[ $(cat "$STATE/previous/BOOTED" 2>/dev/null) == no ]]; then - note "slot B was written since this boot ($written; it holds $rel) and has not booted: keeping previous/ ($(cat "$STATE/previous/RELEASE" 2>/dev/null)), which never booted either" + note "slot ${SLOT^^} was written since this boot ($written; it holds $rel) and has not booted: keeping previous/ ($(cat "$STATE/previous/RELEASE" 2>/dev/null)), which never booted either" else - note "slot B was written since this boot ($written; it holds $rel) and has not booted: keeping previous/ ($(cat "$STATE/previous/RELEASE" 2>/dev/null)), the last set that booted" + note "slot ${SLOT^^} was written since this boot ($written; it holds $rel) and has not booted: keeping previous/ ($(cat "$STATE/previous/RELEASE" 2>/dev/null)), the last set that booted" fi return 0 elif [[ $booted == no ]]; then - note "slot B was written since this boot ($written) and nothing was saved before: saving it as it is, marked not booted" + note "slot ${SLOT^^} was written since this boot ($written) and nothing was saved before: saving it as it is, marked not booted" fi need=0 for img in $DEV_BOOT_IMAGES; do need=$((need + PART_BYTES[$img])); done [[ -d $MODULES/$rel ]] && need=$((need + $(du -sb "$MODULES/$rel" | cut -f1))) avail=$(free_bytes "$STATE") ((avail > need + SPACE_MARGIN)) || refuse "not enough space in $STATE to keep the previous images ($need bytes needed)" - say "Saving slot B ($rel) to $STATE/previous" + say "Saving slot ${SLOT^^} ($rel) to $STATE/previous" rm -rf "$new" && mkdir -p "$new" for img in $DEV_BOOT_IMAGES; do dev=${PART_DEV[$img]} bytes=${PART_BYTES[$img]} - head -c "$bytes" "$dev" > "$new/$img.img" || refuse "could not read ${img}_b" - [[ $(stat -c %s "$new/$img.img") == "$bytes" ]] || refuse "short read of ${img}_b" + head -c "$bytes" "$dev" > "$new/$img.img" || refuse "could not read ${img}_$SLOT" + [[ $(stat -c %s "$new/$img.img") == "$bytes" ]] || refuse "short read of ${img}_$SLOT" done (cd "$new" && for img in $DEV_BOOT_IMAGES; do sha256sum "$img.img"; done) > "$new/SHA256SUMS" printf '%s\n' "$DEV_CODENAME" > "$new/DEVICE" printf '%s\n' "$rel" > "$new/RELEASE" printf '%s\n' "$booted" > "$new/BOOTED" + printf '%s\n' "$SLOT" > "$new/SLOT" if [[ -d $MODULES/$rel ]]; then mkdir -p "$new/modules" cp -a "$MODULES/$rel" "$new/modules/$rel" || refuse "could not save the modules of $rel" @@ -480,13 +529,13 @@ write_one() { dev=${PART_DEV[$img]} size=$(stat -c %s "$file") want=$(sha_file "$file") - dd if="$file" of="$dev" bs=1M conv=fsync,notrunc status=none || die "writing ${img}_b ($dev) failed: slot B is incomplete; omamobile-boot-update rollback restores the saved set" + dd if="$file" of="$dev" bs=1M conv=fsync,notrunc status=none || die "writing ${img}_$SLOT ($dev) failed: slot ${SLOT^^} is incomplete; omamobile-boot-update rollback restores the saved set" fake_count_write "$dev" "$size" # Drop the kernel's cached copy so the read-back comes from the device if real_root; then blockdev --flushbufs "$dev" || true; fi got=$(sha_head "$dev" "$size") - [[ $want == "$got" ]] || die "verify failed for ${img}_b ($dev): slot B is incomplete; omamobile-boot-update rollback restores the saved set" - say "${img}_b ($dev) written and verified" + [[ $want == "$got" ]] || die "verify failed for ${img}_$SLOT ($dev): slot ${SLOT^^} is incomplete; omamobile-boot-update rollback restores the saved set" + say "${img}_$SLOT ($dev) written and verified" } # do_write MODE: MODE is write (the new release) or rollback (previous/). @@ -511,6 +560,7 @@ do_write() { src=$STATE/previous [[ -d $src ]] || refuse "no saved images in $src" [[ $(cat "$src/DEVICE" 2>/dev/null) == "$DEV_CODENAME" ]] || refuse "$src holds images for another device" + [[ $(previous_slot) == "$SLOT" ]] || refuse "$src holds slot $(previous_slot | tr ab AB)'s images, not slot ${SLOT^^}'s" (cd "$src" && sha256sum --quiet -c SHA256SUMS) || refuse "$src fails its checksums" for img in $DEV_BOOT_IMAGES; do grep -cE " $img\.img$" "$src/SHA256SUMS" >/dev/null || refuse "$src/SHA256SUMS does not cover $img.img" @@ -530,17 +580,17 @@ do_write() { say "Plan ($mode${RELEASE:+, $RELEASE}):" for img in $DEV_BOOT_IMAGES; do if [[ " ${changed[*]} " == *" $img "* ]]; then why=write; else why="already there"; fi - note "$(printf '%-20s -> %-22s %10s bytes %s' "$img.img" "${img}_b (${PART_DEV[$img]##*/})" "$(stat -c %s "$src/$img.img")" "$why")" + note "$(printf '%-20s -> %-22s %10s bytes %s' "$img.img" "${img}_$SLOT (${PART_DEV[$img]##*/})" "$(stat -c %s "$src/$img.img")" "$why")" done if ((${#changed[@]} == 0)); then - say "Slot B already holds these images: nothing to write" + say "Slot ${SLOT^^} already holds these images: nothing to write" if ((!DRY_RUN)); then rm -f "$STATE/incomplete" - # Also after a rollback: slot B holds the saved set now, whoever put + # Also after a rollback: the slot holds the saved set now, whoever put # it there (bin/omamobile flash from a copy of previous/, say) [[ $mode != rollback ]] || restore_modules finish_current "$src" - record identical "slot B already holds ${RELEASE:-these images}" + record identical "slot ${SLOT^^} already holds ${RELEASE:-these images}" fi return 0 fi @@ -560,7 +610,7 @@ do_write() { # Every image of the set, including those left alone, must read back for img in $DEV_BOOT_IMAGES; do [[ $(sha_head "${PART_DEV[$img]}" "$(stat -c %s "$src/$img.img")") == "$(sha_file "$src/$img.img")" ]] \ - || die "${img}_b does not hold its image after the write: slot B is incomplete" + || die "${img}_$SLOT does not hold its image after the write: slot ${SLOT^^} is incomplete" done rm -f "$STATE/incomplete" if [[ $mode == write ]]; then @@ -571,8 +621,8 @@ do_write() { finish_current "$src" fi sync - record written "slot B now holds ${RELEASE:-the previous images}; restart to boot it" - say "Slot B holds ${RELEASE:-the previous images}. Restart the phone to boot it (nothing restarts by itself)." + record written "slot ${SLOT^^} now holds ${RELEASE:-the previous images}; restart to boot it" + say "Slot ${SLOT^^} holds ${RELEASE:-the previous images}. Restart the phone to boot it (nothing restarts by itself)." [[ $mode == rollback ]] && rollback_hold_note return 0 } @@ -585,14 +635,14 @@ rollback_hold_note() { cat <-aarch64.pkg.tar.zst), - which writes it to slot B through this hook and leaves nothing to hold. + which writes it to slot ${SLOT^^} through this hook and leaves nothing to hold. EOF } @@ -603,6 +653,7 @@ finish_current() { for img in $DEV_BOOT_IMAGES; do printf '%s %s\n' "$img" "$(stat -c %s "$src/$img.img")"; done > "$STATE/current.new/SIZES" printf '%s\n' "$DEV_CODENAME" > "$STATE/current.new/DEVICE" printf '%s\n' "${RELEASE:-unknown}" > "$STATE/current.new/RELEASE" + printf '%s\n' "$SLOT" > "$STATE/current.new/SLOT" rm -rf "$STATE/current" mv "$STATE/current.new" "$STATE/current" } @@ -625,15 +676,17 @@ restore_modules() { # ---------------------------------------------------------------- commands -# Does slot B still hold what current/ records? Only this tool updates +# Does the slot still hold what current/ records? Only this tool updates # current/; bin/omamobile flash (the documented recovery) and push rewrite -# slot B without it. Prints "yes", "stale" (slot B changed outside this +# the slot without it. Prints "yes", "stale" (the slot changed outside this # tool: the record says nothing about it) or "none" (no record). Needs # find_partition for every image first. current_state() { local img size want [[ -f $STATE/current/RELEASE ]] || { echo none; return 0; } [[ -f $STATE/current/SIZES && -f $STATE/current/SHA256SUMS ]] || { echo stale; return 0; } + # A record of the other slot says nothing about this one (none: slot B) + [[ $(cat "$STATE/current/SLOT" 2>/dev/null || echo b) == "$SLOT" ]] || { echo stale; return 0; } for img in $DEV_BOOT_IMAGES; do size=$(awk -v i="$img" '$1 == i {print $2}' "$STATE/current/SIZES") want=$(awk -v f="$img.img" '$2 == f {print $1}' "$STATE/current/SHA256SUMS") @@ -649,6 +702,7 @@ cmd_status() { bu_load_descriptor "$DESCRIPTOR" echo "device: $DEV_CODENAME ($DEV_COMPATIBLE)" echo "auto write: $AUTO_WRITE ($CONF)" + echo "slot: $SLOT${SLOT_FROM_DEFAULT:+ (no SLOT= line in $CONF: b)}" echo "running: $UNAME_R" slot=$( (for img in $DEV_BOOT_IMAGES; do find_partition "$img" >/dev/null 2>&1 || exit 1; done case $(current_state) in @@ -656,35 +710,35 @@ cmd_status() { stale) echo "changed outside omamobile-boot-update (last written here: $(cat "$STATE/current/RELEASE"))" ;; *) echo "not written by this tool" ;; esac) 2>/dev/null) || slot="unknown (partitions not readable)" - echo "slot B: $slot" + echo "slot ${SLOT^^}: $slot" echo "previous: $(cat "$STATE/previous/RELEASE" 2>/dev/null || echo none)$([[ -d $STATE/previous ]] && echo " (booted: $(cat "$STATE/previous/BOOTED" 2>/dev/null || echo unknown))")" [[ ! -e $STATE/incomplete ]] || echo "INCOMPLETE: $(cat "$STATE/incomplete") (omamobile-boot-update rollback, or write again)" [[ ! -f $RUN/boot-update.json ]] || echo "last result: $(cat "$RUN/boot-update.json")" for img in $DEV_BOOT_IMAGES; do - ( find_partition "$img" 2>/dev/null && echo "partition: ${img}_b ${PART_DEV[$img]##*/} ${PART_BYTES[$img]} bytes, writable" ) \ - || echo "partition: ${img}_b NOT writable or not found" + ( find_partition "$img" 2>/dev/null && echo "partition: ${img}_$SLOT ${PART_DEV[$img]##*/} ${PART_BYTES[$img]} bytes, writable" ) \ + || echo "partition: ${img}_$SLOT NOT writable or not found" done } -# preflight only: slot B must hold the running kernel's set (or one this tool +# preflight only: the slot must hold the running kernel's set (or one this tool # never wrote). A set written but never booted is not refused by the hook # itself (previous/ keeps the last set that booted), but a kernel upgrade on # top of it would have pacman remove that release's modules, and # kernel-modules-hook keeps only the running release's: restart into it first. check_booted() { local cur written - # Slot B written since this boot with no booted set saved (none, or one + # The slot written since this boot with no booted set saved (none, or one # marked BOOTED=no): a kernel update now would leave nothing that is known # to boot. The hook itself still writes; only preflight stops here. written=$(written_since_boot) if [[ -n $written && ( ! -d $STATE/previous || $(cat "$STATE/previous/BOOTED" 2>/dev/null) == no ) ]]; then - refuse "slot B was written since this boot ($written) and no set that booted is saved: restart into it before the next kernel update" + refuse "slot ${SLOT^^} was written since this boot ($written) and no set that booted is saved: restart into it before the next kernel update" fi - # A record slot B no longer matches (flash, push) says nothing: pass + # A record the slot no longer matches (flash, push) says nothing: pass [[ $(current_state) == yes ]] || return 0 cur=$(cat "$STATE/current/RELEASE") [[ $cur == "$UNAME_R" ]] \ - || refuse "slot B holds $cur, written but not booted yet (running $UNAME_R): restart into it before the next kernel update" + || refuse "slot ${SLOT^^} holds $cur, written but not booted yet (running $UNAME_R): restart into it before the next kernel update" } # preflight only: room for the new images (next/), the saved partitions @@ -719,7 +773,7 @@ cmd_preflight() { check_booted check_space echo "auto_write=$AUTO_WRITE" - ) 2>&1) || problems+=("$(grep -m1 '^error:' <<<"$out" | sed 's/^error: omamobile-boot-update: //; s/^not writing slot B: //')") + ) 2>&1) || problems+=("$(grep -m1 '^error:' <<<"$out" | sed 's/^error: omamobile-boot-update: //; s/^not writing slot [AB]: //')") local auto auto=$(sed -n 's/^auto_write=//p' <<<"$out") [[ -n $auto ]] || auto=$( (load_conf >/dev/null 2>&1 && echo "$AUTO_WRITE") || echo unknown) @@ -751,7 +805,7 @@ cmd_build() { cmd_hook() { load_conf if [[ $AUTO_WRITE != yes ]]; then - say "Slot B left alone: AUTO_WRITE is not yes in $CONF (bin/omamobile push stays the writer)" + say "Slot ${SLOT^^} left alone: AUTO_WRITE is not yes in $CONF (bin/omamobile push stays the writer)" record skipped "AUTO_WRITE is off" return 0 fi diff --git a/packages/omamobile-boot-update/etc/boot-update.conf b/packages/omamobile-boot-update/etc/boot-update.conf index 29b3b55fb..3a3455243 100644 --- a/packages/omamobile-boot-update/etc/boot-update.conf +++ b/packages/omamobile-boot-update/etc/boot-update.conf @@ -10,6 +10,12 @@ # `omamobile-boot-update write` still writes by hand, after asking. AUTO_WRITE=no # +# SLOT=a|b: the slot whose boot images the hook writes, which must be the +# slot the phone runs from (androidboot.slot_suffix), or it refuses. Absent +# means b, as on every phone installed onto slot B. A release installer +# writes SLOT=a for a phone it installed on slot A (docs/ACTIVE-SLOT-PLAN.md +# §2); a development phone has no such file. +# # AVB_KEY=/path/to/key.pem: the key for the AVB footers a phone's vbmeta # chain needs (AVB_FOOTERS in the descriptor). Default: AOSP's public test # key, shipped with the package, as bin/omamobile images uses it, so the diff --git a/packages/omamobile-boot-update/lib/descriptor.sh b/packages/omamobile-boot-update/lib/descriptor.sh index 94a2b6b56..bd6536da9 100644 --- a/packages/omamobile-boot-update/lib/descriptor.sh +++ b/packages/omamobile-boot-update/lib/descriptor.sh @@ -6,14 +6,19 @@ # omamobile-boot-update and omamobile-boot-images; the caller defines die. # # Only BOOT_METHOD=android-v4 (the default) is accepted: the Qualcomm chain, -# the RAM-only probe and anything else have no slot-B update path here. +# the RAM-only probe and anything else have no slot-B update path here +# (nor one on slot A: the reader's verdict doesn't depend on the slot). # tests/boot-update-descriptor checks that every descriptor in device/ gets # the same verdict from this file as from bin/omamobile. # Every name a descriptor's UFS_RW may hold (bin/omamobile UFS_RW_ALLOWED, -# initramfs/init ufs_rw_load). +# initramfs/init ufs_rw_load). The names are in slot form: X_b stands for X +# of the slot the phone runs from, which the initramfs guard opens and +# omamobile-boot-update writes (SLOT= in boot-update.conf, b when absent; +# docs/ACTIVE-SLOT-PLAN.md §2, §4). BU_UFS_RW_ALLOWED="boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata" -# Every image a phone writes. The partition is always _b. +# Every image a phone writes. The partition is _: _b on +# every phone whose boot-update.conf names no slot. BU_IMAGES_ALLOWED="boot init_boot vendor_boot vendor_kernel_boot dtbo" # bu_load_descriptor FILE: set DEV_* from FILE, or die. diff --git a/packages/omamobile-boot-update/libalpm/90-omamobile-boot.hook b/packages/omamobile-boot-update/libalpm/90-omamobile-boot.hook index ec4d71ccc..46b92ed76 100644 --- a/packages/omamobile-boot-update/libalpm/90-omamobile-boot.hook +++ b/packages/omamobile-boot-update/libalpm/90-omamobile-boot.hook @@ -19,6 +19,6 @@ Target = usr/lib/omamobile/*/initramfs.cpio.lz4 Target = usr/lib/omamobile/*/overlays/* [Action] -Description = Writing the kernel into slot B (omamobile-boot-update)... +Description = Writing the kernel into its boot slot (omamobile-boot-update)... When = PostTransaction Exec = /usr/bin/omamobile-boot-update hook diff --git a/packages/omamobile-boot-update/test/all b/packages/omamobile-boot-update/test/all index a3abcc193..27dddfc9d 100755 --- a/packages/omamobile-boot-update/test/all +++ b/packages/omamobile-boot-update/test/all @@ -6,7 +6,8 @@ # power supply. Covers the descriptor rules, the image builder (with the real # mkbootimg, avbtool, lz4 and dtc when installed, else stand-ins), the hook's # opt-in, every refusal before a write, the write and its read-back, the -# saved previous set, an interrupted write and the rollback. +# saved previous set, an interrupted write and the rollback, and a phone +# whose boot-update.conf names slot A (SLOT=a) booted from slot A. set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." PKG=$PWD @@ -189,12 +190,14 @@ fi export LZ4=$LZ4_BIN MKBOOTIMG=$MKBOOTIMG_BIN AVBTOOL=$AVBTOOL_BIN OPENSSL=$OPENSSL_BIN echo "# tools: dtc=${DTC_BIN:-none} fdtoverlay=${FDTOVERLAY_BIN:-none} lz4=$LZ4 mkbootimg=$MKBOOTIMG avbtool=$AVBTOOL" -# A release initramfs: an allowlist, an init, no SSH keys +# A release initramfs: an allowlist, the running-slot marker (RD_MARK= +# leaves it out, as an init from before it), an init, no SSH keys mkinitramfs() { # out ufs_rw [extra-path] local s=$t/initramfs-stage rm -rf "$s" && mkdir -p "$s/etc" echo '#!/bin/sh' > "$s/init" echo "$2" > "$s/etc/omamobile-ufs-rw" + [[ -z ${RD_MARK-running} ]] || echo "${RD_MARK-running}" > "$s/etc/omamobile-ufs-rw-slot" if [[ -n ${3:-} ]]; then mkdir -p "$s/${3%/*}"; echo key > "$s/$3"; fi (cd "$s" && find . | LC_ALL=C sort | cpio -o -H newc --owner=0:0 --quiet) | "$LZ4" -l -9 -q > "$1" } @@ -310,20 +313,20 @@ export OMAMOBILE_BOOT_UPDATE_LIB=$stub OMAMOBILE_BOOT_AVB_TESTKEY=$PKG/avb/testk U=$PKG/bin/omamobile-boot-update PARTS="boot_a init_boot_a vendor_boot_a vendor_kernel_boot_a dtbo_a boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b vbmeta_b devinfo persist efs userdata" -mkroot() { # R - local R=$1 i=0 p +mkroot() { # R [slot the phone runs from, writable to the guard: b] + local R=$1 i=0 p ws=${2:-b} rm -rf "$R" mkdir -p "$R/usr/share/omamobile" "$R/proc/device-tree" "$R/dev" "$R/etc/omamobile" "$R/usr/lib/omamobile/twelve" \ "$R/sys/block/sda" "$R/sys/block/sdb/sdb1" "$R/root/.ssh" cat "$t/twelve.conf" > "$R/usr/share/omamobile/device.conf" printf 'AVB_FOOTERS="boot:0x40000 init_boot:0x20000"\nAVB_ROLLBACK_INDEX=1783209600\nAVB_SECURITY_PATCH=2026-07-05\n' >> "$R/usr/share/omamobile/device.conf" printf 'google,gs201-twelve\0google,gs201\0' > "$R/proc/device-tree/compatible" - echo 'console=null androidboot.slot_suffix=_b panic=10' > "$R/proc/cmdline" + echo "console=null androidboot.slot_suffix=_$ws panic=10" > "$R/proc/cmdline" for p in $PARTS; 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" - case $p in boot_b|init_boot_b|vendor_boot_b|vendor_kernel_boot_b|dtbo_b|userdata) echo 0 ;; *) echo 1 ;; esac > "$R/sys/block/sda/sda$i/ro" + case $p in "boot_$ws"|"init_boot_$ws"|"vendor_boot_$ws"|"vendor_kernel_boot_$ws"|"dtbo_$ws"|userdata) echo 0 ;; *) echo 1 ;; esac > "$R/sys/block/sda/sda$i/ro" case $p in boot_*) s=0x40000 ;; init_boot_*) s=0x20000 ;; *) s=0x10000 ;; esac echo $((s / 512)) > "$R/sys/block/sda/sda$i/size" echo '0 0 0 0 0 0 0 0 0 0 0' > "$R/sys/block/sda/sda$i/stat" @@ -419,7 +422,7 @@ printf 'google,gs201-twelve\0google,gs201\0' > "$R/proc/device-tree/compatible" echo 'androidboot.slot_suffix=_a' > "$R/proc/cmdline" refused "a boot from slot A" "slot A" hook echo 'console=null panic=10' > "$R/proc/cmdline" -refused "a boot that names no slot" "says this boot is from slot B" hook +refused "a boot that names no slot" "says which slot this boot is from" hook printf 'androidboot.slot_suffix = "_b"\n' > "$R/proc/bootconfig" run write --dry-run || { cat "$t/out"; fail "slot B named in /proc/bootconfig (as on cheetah) is enough"; } rm "$R/proc/bootconfig" @@ -695,6 +698,87 @@ reboot_counters "$R" python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); assert "no set that booted" not in d["problem"], d' "$t/pf" || fail "after a restart that refusal is gone" pass "booted means slot B unwritten since this boot (write counters): a same-release rewrite, a half push and an unbooted push never replace the set that booted" +# ---------------------------------------------------------------- SLOT=a +# A release installed on the slot it ran from: boot-update.conf says SLOT=a, +# the bootloader says _a, and the guard left slot A's images writable. +mkroot "$R" a +printf 'AUTO_WRITE=yes\nSLOT=a\n' > "$R/etc/omamobile/boot-update.conf" +declare -A olda=() +for p in $PARTS; do olda[$p]=$(sha256sum < "$(part "$R" "$p")"); done +run write --dry-run || { cat "$t/out"; fail "SLOT=a: a dry run on slot A"; } +grep -c 'boot.img .*-> boot_a (sda1).* write$' "$t/out" >/dev/null || { cat "$t/out"; fail "SLOT=a: the plan names boot_a"; } +grep -c '_b (' "$t/out" >/dev/null && { cat "$t/out"; fail "SLOT=a: the plan names no _b partition"; } +run hook || { cat "$t/out"; fail "SLOT=a: the hook writes slot A"; } +for p in boot_a init_boot_a vendor_boot_a vendor_kernel_boot_a dtbo_a; do + grep -c "^IMG ${p%_a} " "$(part "$R" $p)" >/dev/null || fail "SLOT=a: $p holds ${p%_a}.img" +done +for p in boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b vbmeta_b devinfo persist efs userdata; do + [[ $(sha256sum < "$(part "$R" "$p")") == "${olda[$p]}" ]] || fail "SLOT=a: $p is untouched" +done +grep -c 'boot_a (.*) written and verified' "$t/out" >/dev/null && grep -c 'Slot A holds' "$t/out" >/dev/null \ + || { cat "$t/out"; fail "SLOT=a: it names slot A's partitions and slot A"; } +[[ $(cat "$S/previous/SLOT") == a && $(head -c 10 "$S/previous/boot.img") == "OLD boot_a" ]] || fail "SLOT=a: previous/ holds slot A's old set and says so" +[[ $(cat "$S/current/SLOT") == a ]] || fail "SLOT=a: current/ records slot A" +"$U" status > "$t/st" 2>&1 +grep -c '^slot: *a$' "$t/st" >/dev/null && grep -c '^slot A: ' "$t/st" >/dev/null && grep -c '^partition: boot_a ' "$t/st" >/dev/null \ + || { cat "$t/st"; fail "SLOT=a: status names slot A"; } +"$U" preflight --json > "$t/pf" || true +python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); assert "not writing slot" not in d["problem"], d' "$t/pf" || fail "preflight strips the slot A prefix too" +run rollback --yes || { cat "$t/out"; fail "SLOT=a: rollback"; } +for p in boot_a init_boot_a vendor_boot_a vendor_kernel_boot_a dtbo_a boot_b; do + [[ $(sha256sum < "$(part "$R" "$p")") == "${olda[$p]}" ]] || fail "SLOT=a: rollback restores $p (or leaves it)" +done +pass "SLOT=a on slot A: writes, saves and rolls back slot A's images only, and says so" + +# The slot must be the running one, both ways, and the slot-A guard there +mkroot "$R" a +refused "slot A booted, no SLOT= line (b)" "this boot is from slot A, but" hook +grep -c '(it has no SLOT= line: B)' "$t/out" >/dev/null || fail "and says the default is B" +printf 'AUTO_WRITE=yes\nSLOT=a\n' > "$R/etc/omamobile/boot-update.conf" +printf 'androidboot.slot_suffix = "_b"\n' > "$R/proc/bootconfig" +refused "SLOT=a with bootconfig and the command line disagreeing" "this boot is from slot B, but" hook +rm "$R/proc/bootconfig" +mkroot "$R" b +printf 'AUTO_WRITE=yes\nSLOT=a\n' > "$R/etc/omamobile/boot-update.conf" +refused "SLOT=a on a phone that fell back to slot B" "not writing slot A: the bootloader says this boot is from slot B, but" hook +mkroot "$R" a +printf 'AUTO_WRITE=yes\nSLOT=a\n' > "$R/etc/omamobile/boot-update.conf" +echo 'androidboot.slot_suffix=_c' > "$R/proc/cmdline" +refused "a suffix that is neither _a nor _b" "is '_c', not _a or _b" hook +echo 'androidboot.slot_suffix=_a' > "$R/proc/cmdline" +RD_MARK= mkinitramfs "$R/usr/lib/omamobile/twelve/initramfs.cpio.lz4" "$RW5" +refused "SLOT=a with an initramfs from before the running-slot guard" "no running-slot guard" hook +RD_MARK=slot-b mkinitramfs "$R/usr/lib/omamobile/twelve/initramfs.cpio.lz4" "$RW5" +refused "SLOT=a with a malformed running-slot marker" "no running-slot guard" hook +mkinitramfs "$R/usr/lib/omamobile/twelve/initramfs.cpio.lz4" "$RW5" +for bad in 'SLOT=c' 'SLOT=' 'SLOT=A' 'SLOT=_a' 'SLOT=a b'; do + printf 'AUTO_WRITE=yes\n%s\n' "$bad" > "$R/etc/omamobile/boot-update.conf" + refused "boot-update.conf with $bad" "SLOT=a|b" hook +done +printf 'AUTO_WRITE=yes\nSLOT="a"\n' > "$R/etc/omamobile/boot-update.conf" +run write --dry-run || { cat "$t/out"; fail "SLOT=\"a\" (quoted) is read as a"; } +# A slot-B rollback set is no rollback for slot A +printf 'AUTO_WRITE=yes\nSLOT=a\n' > "$R/etc/omamobile/boot-update.conf" +run hook || { cat "$t/out"; fail "SLOT=a: a write to keep a previous set"; } +echo b > "$S/previous/SLOT" +refused "a rollback of a set saved from slot B" "holds slot B's images, not slot A's" rollback --yes +rm "$S/previous/SLOT" +refused "a rollback of a set saved before the slot was recorded (slot B)" "holds slot B's images, not slot A's" rollback --yes +echo a > "$S/previous/SLOT" +# A record of slot B's set says nothing about slot A +echo b > "$S/current/SLOT" +"$U" status > "$t/st" 2>&1 +grep -c '^slot A: *changed outside' "$t/st" >/dev/null || { cat "$t/st"; fail "a current/ record of slot B is stale on slot A"; } +pass "SLOT= must name the running slot, every suffix found must agree, slot A needs the running-slot initramfs, and previous/ and current/ are per slot" + +# A phone with no SLOT= line keeps slot B, its old previous/ (no SLOT file) +# included +mkroot "$R" +run hook || { cat "$t/out"; fail "no SLOT=: the first write"; } +rm "$S/previous/SLOT" "$S/current/SLOT" +run rollback --dry-run || { cat "$t/out"; fail "no SLOT=: a previous/ from before the slot was recorded is slot B's"; } +pass "no SLOT= line: slot B, and sets saved before the slot was recorded count as slot B's" + # End to end with the real builder (the tools above), when dtc is there: # slot B ends up holding exactly what omamobile-boot-images makes if [[ -n $DTC_BIN ]]; then diff --git a/packages/omamobile-boot-update/version b/packages/omamobile-boot-update/version index 6e8bf73aa..0ea3a944b 100644 --- a/packages/omamobile-boot-update/version +++ b/packages/omamobile-boot-update/version @@ -1 +1 @@ -0.1.0 +0.2.0 From 73f6c015c4c60ea673b8d7ff340156c380e4c4a7 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:18:47 -0500 Subject: [PATCH 03/10] omamobile-omarchy 0.1.48: omamobile-update reads boot-update.conf's SLOT= line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit omamobile-update reads boot-update.conf as omamobile-boot-update does, and took any line but AUTO_WRITE and AVB_KEY as unreadable, blocking the kernel update. omamobile-boot-update 0.2.0 adds SLOT=a|b (the slot a release installed on slot A updates in place, ACTIVE-SLOT-PLAN §2); this accepts it, and the test covers a SLOT=a config read with AUTO_WRITE=yes. Co-Authored-By: Claude Opus 5.5 --- packages/omamobile-omarchy/bin/omamobile-update | 2 ++ packages/omamobile-omarchy/test/update.sh | 3 +++ packages/omamobile-omarchy/version | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/omamobile-omarchy/bin/omamobile-update b/packages/omamobile-omarchy/bin/omamobile-update index 7df208655..169fe08e4 100755 --- a/packages/omamobile-omarchy/bin/omamobile-update +++ b/packages/omamobile-omarchy/bin/omamobile-update @@ -404,6 +404,8 @@ def auto_write_conf(path=None): m = re.match(r'^AUTO_WRITE="?(yes|no)"?$', line) if m: value = m.group(1) + elif re.match(r'^SLOT="?(a|b)"?$', line): + continue elif not re.match(r'^AVB_KEY="?/[A-Za-z0-9_./-]+"?$', line) or ".." in line: return "" return value diff --git a/packages/omamobile-omarchy/test/update.sh b/packages/omamobile-omarchy/test/update.sh index 90add925e..607189bdb 100755 --- a/packages/omamobile-omarchy/test/update.sh +++ b/packages/omamobile-omarchy/test/update.sh @@ -262,6 +262,9 @@ ok "kernel: the first kernel package with no config passes (the hook will skip)" echo 'AUTO_WRITE=yes' >"$t/boot-update.conf" out=$(OMAMOBILE_BOOT_UPDATE=$t/no-such-tool python3 -I "$U" check --json) ok "kernel: AUTO_WRITE=yes but no omamobile-boot-update to ask: blocked" 'python3 -c "import json,sys; d=json.loads(sys.argv[1]); assert not d[\"ok\"] and any(\"AUTO_WRITE=yes\" in b for b in d[\"blockers\"])" "$out"' +printf 'AUTO_WRITE=yes\nSLOT=a\n' >"$t/boot-update.conf" +out=$(OMAMOBILE_BOOT_UPDATE=$t/no-such-tool python3 -I "$U" check --json) +ok "kernel: a SLOT= line (omamobile-boot-update 0.2.0) is read with AUTO_WRITE=yes, not taken as unreadable" 'python3 -c "import json,sys; d=json.loads(sys.argv[1]); assert not any(\"be read\" in b for b in d[\"blockers\"]) and any(\"AUTO_WRITE=yes\" in b for b in d[\"blockers\"]), d" "$out"' echo 'AUTO_WRITE=perhaps' >"$t/boot-update.conf" out=$(OMAMOBILE_BOOT_UPDATE=$t/no-such-tool python3 -I "$U" check --json) ok "kernel: an unreadable boot-update.conf blocks" 'python3 -c "import json,sys; d=json.loads(sys.argv[1]); assert not d[\"ok\"] and any(\"be read\" in b for b in d[\"blockers\"])" "$out"' diff --git a/packages/omamobile-omarchy/version b/packages/omamobile-omarchy/version index 9506d143f..5946febf1 100644 --- a/packages/omamobile-omarchy/version +++ b/packages/omamobile-omarchy/version @@ -1 +1 @@ -0.1.47 +0.1.48 From 41cdaccb57ab80a1edd1a151e68d76481d58340e Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:21:17 -0500 Subject: [PATCH 04/10] phone-smoke, bin/omamobile: the running slot in the boot and guard rows; push and flash refuse slot A MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A release installed on the slot it ran from runs from slot A (docs/ACTIVE-SLOT-PLAN.md §2, §4), so the phone-side checks learn the slot instead of assuming B. tools/phone-smoke's boot row wants the slot boot-update.conf names (SLOT=, b when the file or the line is absent: development phones and every install so far), and its storage guard row allows userdata and the running slot's five boot images, userdata alone when no slot is named. --slot-from DIR (tests only) runs those two rows on a fake root; tests/phone-smoke covers B as before, A with SLOT=a, A without it, a SLOT=a phone that fell back to B, the other slot's images writable either way, and no slot named. bin/omamobile push reads the slot first (read only, the matched suffix alone) and refuses a phone on slot A: "this phone runs a release from slot A; push writes slot B, development only". flash reads fastboot's current-slot and refuses slot A too, since it writes slot B and makes it active. Fastboot can't tell a release on A from stock Android on A, and a development phone that fell back to stock (or that `omamobile fastboot` crashed into it) is flashed from there, so OMAMOBILE_FLASH_FROM_SLOT_A=yes names that case; the message says so. A phone on B, or one that names no slot, is unchanged. Co-Authored-By: Claude Opus 5.5 --- bin/omamobile | 18 +++++++++++++++- tests/images-layout | 28 +++++++++++++++++++++++- tests/phone-smoke | 52 ++++++++++++++++++++++++++++++++++++++++++++- tools/phone-smoke | 38 ++++++++++++++++++++++++--------- 4 files changed, 123 insertions(+), 13 deletions(-) diff --git a/bin/omamobile b/bin/omamobile index eba54e0ab..71bd907e3 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -1238,8 +1238,17 @@ cmd_flash() { die "flash refuses BOOT_METHOD=qcom-abl (no in-tree day-2 update path yet; see device/$DEVICE/BRINGUP-M1.md)" images_for_device "$img" in_fastboot || die "phone not in fastboot (try: omamobile fastboot)" - local serial + local serial cur serial=$(fastboot_serial) || exit 1 + # Read-only: a release installed on the slot it ran from runs from slot A + # (docs/ACTIVE-SLOT-PLAN.md §2), and this writes slot B and makes it + # active, which is development only. Fastboot can't tell that release + # from stock Android on A, which a development phone falls back to (and + # `omamobile fastboot` crashes it into), so that case is named by hand. + cur=$(timeout 10 fastboot -s "$serial" getvar current-slot 2>&1 | sed -n 's/^current-slot: *_\{0,1\}\([ab]\)$/\1/p' | head -1) + if [[ $cur == a && ${OMAMOBILE_FLASH_FROM_SLOT_A:-} != yes ]]; then + die "this phone runs from slot A (a release from slot A, or stock Android); flash writes slot B and makes it active, development only. A development phone that fell back to stock on slot A: run again with OMAMOBILE_FLASH_FROM_SLOT_A=yes" + fi # Every image and allowlist entry first: never start writing slot B and # find out halfway that a later image is missing. for p in $DEV_BOOT_IMAGES; do @@ -1277,6 +1286,13 @@ cmd_push() { die "push refuses BOOT_METHOD=qcom-abl (no in-tree day-2 update path yet; see device/$DEVICE/BRINGUP-M1.md)" images_for_device "$img" "$ssh" true || die "phone not reachable at 172.16.42.1" + # Read-only: a release installed on the slot it ran from runs from slot A + # (docs/ACTIVE-SLOT-PLAN.md §2), where its storage guard leaves slot B + # read-only anyway; say so plainly before anything else + local slots + slots=$("$ssh" "cat /proc/bootconfig /proc/cmdline 2>/dev/null | grep -oE 'androidboot[.]slot_suffix( = \"|=)_[ab]'; true") \ + || die "cannot read the phone's boot slot" + [[ $slots != *_a* ]] || die "this phone runs a release from slot A; push writes slot B, development only" # Refuse before any write. A phone whose initramfs has no reboot-to would # otherwise take the images, swallow the reboot, and report success while # the old kernel is still running. diff --git a/tests/images-layout b/tests/images-layout index 9a70d97c4..fe28bf569 100755 --- a/tests/images-layout +++ b/tests/images-layout @@ -6,7 +6,9 @@ # load_device refuses bad descriptors; push, against a fake phone, refuses an # image too big for its partition before writing any, writes and verifies the # images and tells a reboot that did not happen from a phone that never came -# back. Then, with a stub kernel output (a random Image, minimal DTBs, a +# back; both refuse a phone that runs from slot A (a release there), flash +# unless OMAMOBILE_FLASH_FROM_SLOT_A=yes names a development phone fallen +# back to stock. Then, with a stub kernel output (a random Image, minimal DTBs, a # random initramfs), a gs201-style descriptor yields its five boot images and # a gs101-style one (no init_boot or vendor_kernel_boot) yields boot (ramdisk # and OS version), vendor_boot (DTB and initramfs) and dtbo; a zuma-style one @@ -100,6 +102,8 @@ fi # Every other call must name the phone. [[ $1 == -s && $2 == "${WANT_SERIAL:-FAKE123}" ]] || { echo "unpinned or wrong serial: $*" >&2; exit 64; } shift 2 +# fastboot prints getvar answers on stderr +[[ $1 == getvar && $2 == current-slot ]] && { echo "current-slot: ${CURRENT_SLOT:-b}" >&2; echo 'Finished. Total time: 0.001s' >&2; exit 0; } [[ $1 == flash && ${FAIL_FLASH:-} == "$2" ]] && { echo FAILED; exit 17; } [[ $1 == flash ]] && echo OKAY exit 0 @@ -150,6 +154,20 @@ if om six flash; then fail "flash refuses an incomplete image set"; fi mv "$t/dtbo.img" "$K/images/dtbo.img" pass "a missing later image is refused before the first flash" +# A phone that runs from slot A: refused before any write, unless the +# owner names it a development phone that fell back to stock there +for cur in a _a; do + : > "$t/fastboot.log" + if CURRENT_SLOT=$cur om six flash; then fail "flash refuses a phone on slot A ($cur)"; fi + grep -c 'this phone runs from slot A' "$t/log" >/dev/null || { cat "$t/log"; fail "flash says the phone runs from slot A"; } + [[ -z $(flashed) ]] && ! grep -Eq -- '--set-active|reboot' "$t/fastboot.log" || fail "flash writes nothing on slot A" +done +grep -c 'getvar current-slot' "$t/fastboot.log" >/dev/null || fail "the slot is read with getvar" +: > "$t/fastboot.log" +CURRENT_SLOT=a OMAMOBILE_FLASH_FROM_SLOT_A=yes om six flash || { cat "$t/log"; fail "OMAMOBILE_FLASH_FROM_SLOT_A=yes lets a development phone back onto slot B"; } +[[ $(flashed) == "boot_b vendor_boot_b dtbo_b " ]] || fail "and flash writes slot B as before" +pass "flash refuses a phone running from slot A unless OMAMOBILE_FLASH_FROM_SLOT_A=yes" + # Bad descriptors are refused before anything is built or written bad() { # what, sed expression on six's descriptor, expected message cp "$R/device/six/device.conf" "$t/good.conf" @@ -198,6 +216,7 @@ cat > "$R/tools/phone-ssh" <<'PHONE' P=$T/phone cmd="$*" case $cmd in true|*'command -v reboot-to'*) exit 0 ;; + *androidboot*slot_suffix*) printf '%s\n' "${PHONE_SLOT:-androidboot.slot_suffix = \"_b}" ;; 'cat /proc/sys/kernel/random/boot_id') n=$(( $(cat "$P/reads" 2>/dev/null || echo 0) + 1 )); echo $n > "$P/reads" state=$(sed -n "${n}p" "$P/script"); [[ -n $state ]] || state=$(tail -n 1 "$P/script") @@ -222,6 +241,13 @@ push() { # the boot_id answers, one per argument push old down new || { cat "$t/log"; fail "push succeeds when the phone comes back with a new boot id"; } [[ $(find "$P" -name 'sda_*' | wc -l) == 3 && $(cat "$P/log") == rebooted ]] || fail "push writes the three images and reboots once" pass "push writes and verifies the images, then sees the new boot id" +for slot in 'androidboot.slot_suffix = "_a' 'androidboot.slot_suffix=_a'; do + if PHONE_SLOT=$slot push old new; then fail "push refuses a phone running from slot A ($slot)"; fi + grep -c 'this phone runs a release from slot A; push writes slot B, development only' "$t/log" >/dev/null || { cat "$t/log"; fail "push says why"; } + [[ $(find "$P" -name 'sda_*' | wc -l) == 0 && ! -s $P/log ]] || fail "push writes nothing and does not reboot on slot A" +done +PHONE_SLOT= push old down new || { cat "$t/log"; fail "push on a phone that names no slot goes on as before"; } +pass "push refuses a phone running from slot A before any write" if push old old; then fail "push fails when the boot id never changes"; fi grep -c 'same boot id: the reboot did not happen' "$t/log" >/dev/null || { cat "$t/log"; fail "an unchanged boot id is reported as such"; } pass "push reports a reboot that did not happen" diff --git a/tests/phone-smoke b/tests/phone-smoke index b51f81d26..7bfb3799e 100755 --- a/tests/phone-smoke +++ b/tests/phone-smoke @@ -5,7 +5,9 @@ # ceiling, and a UFS link that doesn't come back failing kernel health; and # from the laptop side, against a stand-in for the phone (PHONE_SSH): the # device named from the repository's descriptors, or in a release bundle -# from its one device.conf. +# from its one device.conf. The boot and guard rows against a fake root +# (--slot-from): the slot boot-update.conf names, b when absent, and the +# running slot's boot images. set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." t=$(mktemp -d); trap 'rm -rf "$t"' EXIT @@ -124,6 +126,54 @@ has 'of them ASoC no backend DAIs (PipeWire probe) x1, UFS dme-set 0x4021 (s2idl && has 'new dmesg errors|some driver: something new broke x1' || fail "counted and new lines together" pass "counted and new lines together" +# The boot and storage guard rows (--slot-from: a fake root). The boot row +# wants the slot boot-update.conf names (b when absent); the guard row +# allows userdata and the running slot's five boot images. +fr=$t/fr +fakeroot() { # running suffix (or none), then the writable partitions + rm -rf "$fr"; mkdir -p "$fr/proc" "$fr/etc/omamobile" + echo '123.45 100.00' > "$fr/proc/uptime" + [[ $1 == none ]] || printf 'androidboot.slot_suffix = "%s"\n' "$1" > "$fr/proc/bootconfig" + echo 'console=ttynull' > "$fr/proc/cmdline" + shift + local i=0 p + for p in boot_a init_boot_a vendor_boot_a vendor_kernel_boot_a dtbo_a boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata efs persist; do + i=$((i + 1)); mkdir -p "$fr/sys/class/block/sda$i"; echo "$i" > "$fr/sys/class/block/sda$i/partition" + printf 'PARTNAME=%s\n' "$p" > "$fr/sys/class/block/sda$i/uevent" + if [[ " $* " == *" $p "* ]]; then echo 0; else echo 1; fi > "$fr/sys/class/block/sda$i/ro" + done +} +rows() { out=$(tools/phone-smoke --slot-from "$fr"); } +B5="boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata" +A5="boot_a init_boot_a vendor_boot_a vendor_kernel_boot_a dtbo_a userdata" +fakeroot _b $B5; rows +has 'ok|1|boot|kernel ' && has ', slot b, up 123s' && has 'ok|1|storage guard|13 partitions; only userdata and slot b' \ + || fail "a development phone on slot B, no boot-update.conf: both rows pass as before" +pass "a development phone on slot B, no boot-update.conf: both rows pass as before" +echo 'AUTO_WRITE=yes' > "$fr/etc/omamobile/boot-update.conf"; rows +has 'ok|1|boot|' && has 'ok|1|storage guard|' || fail "a release on slot B whose boot-update.conf names no slot: b" +pass "a release on slot B whose boot-update.conf names no slot: b" +fakeroot _a $A5; printf 'AUTO_WRITE=yes\nSLOT=a\n' > "$fr/etc/omamobile/boot-update.conf"; rows +has 'ok|1|boot|' && has ', slot a, up' && has 'ok|1|storage guard|13 partitions; only userdata and slot a' \ + || fail "a release on slot A with SLOT=a: both rows pass" +pass "a release on slot A with SLOT=a: both rows pass" +fakeroot _a $A5; rows +has 'not ok|1|boot|' && has 'on slot a: our builds boot from slot b' || fail "slot A without SLOT=a fails the boot row" +pass "slot A without SLOT=a fails the boot row" +fakeroot _b $B5; printf 'SLOT="a"\n' > "$fr/etc/omamobile/boot-update.conf"; rows +has 'not ok|1|boot|' && has 'on slot b: boot-update.conf says SLOT=a' || fail "SLOT=a booted from slot B (a fallback) fails the boot row" +pass "SLOT=a booted from slot B (a fallback) fails the boot row" +fakeroot _a $A5 boot_b; printf 'SLOT=a\n' > "$fr/etc/omamobile/boot-update.conf"; rows +has 'not ok|1|storage guard|writable outside the allowlist: boot_b' || fail "on slot A, slot B's boot image writable fails the guard row" +fakeroot _b $B5 dtbo_a; rows +has 'not ok|1|storage guard|writable outside the allowlist: dtbo_a' || fail "on slot B, slot A's dtbo writable fails the guard row" +pass "the other slot's boot images writable fail the guard row, either way" +fakeroot none userdata; rows +has 'skip|1|boot|' && has 'ok|1|storage guard|13 partitions; only userdata writable' || fail "no slot named: the boot row skips, the guard allows userdata alone" +fakeroot none $B5; rows +has 'not ok|1|storage guard|writable outside the allowlist: ' && has ' boot_b ' || fail "no slot named: a writable boot image fails the guard row" +pass "no slot named: the boot row skips and only userdata may be writable" + # From the laptop: PHONE_SSH stands in for the phone. It answers the # compatible question and records what the smoke run was started with. printf '%s\n' '#!/bin/bash' 'case $1 in' ' tr*) echo "$COMPAT" ;;' \ diff --git a/tools/phone-smoke b/tools/phone-smoke index 9d6e56afd..a4a84e200 100755 --- a/tools/phone-smoke +++ b/tools/phone-smoke @@ -29,7 +29,7 @@ set -uo pipefail refused_only() { # FAILS REFUSALS LAST_ERRNO: yes when every failure is a refusal [[ $1 =~ ^[0-9]+$ && $2 =~ ^[0-9]+$ ]] && (($2 > 0 && $1 <= $2)) && [[ $3 == -16 ]] } -here=0 json=0 errors_from="" +here=0 json=0 errors_from="" slot_from="" while (($#)); do a=$1; shift case $a in @@ -37,6 +37,9 @@ while (($#)); do --json) json=1 ;; # tests only: the kernel health rows for a saved error log, then exit --errors-from) errors_from=${1:?--errors-from FILE}; shift; here=1 ;; + # tests only: the boot and storage guard rows for a fake root (its proc, + # sys/class/block and etc/omamobile), then exit + --slot-from) slot_from=${1:?--slot-from DIR}; shift; here=1 ;; # tests only: the refused-suspend rule for FAILS REFUSALS LAST_ERRNO, then exit --refused-only) refused_only "${1:-}" "${2:-}" "${3:-}"; exit $? ;; -h|--help) source "$(dirname -- "${BASH_SOURCE[0]}")/lib/usage.sh"; usage_from_header --raw "$0"; exit 0 ;; @@ -79,22 +82,32 @@ uid=$(id -u "$user" 2>/dev/null || true) as_user() { runuser -u "$user" -- env XDG_RUNTIME_DIR="/run/user/$uid" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$uid/bus" "$@" /dev/null) -[[ -n $slot ]] || slot=$(grep -oE 'androidboot\.slot_suffix=_[ab]' /proc/cmdline 2>/dev/null | cut -d= -f2) -up=$(cut -d. -f1 /proc/uptime) +slot=$(sed -n 's/^androidboot\.slot_suffix = "\(_[ab]\)"$/\1/p' "$sr/proc/bootconfig" 2>/dev/null | head -1) +[[ -n $slot ]] || slot=$(grep -oE 'androidboot\.slot_suffix=_[ab]' "$sr/proc/cmdline" 2>/dev/null | head -1 | cut -d= -f2) +up=$(cut -d. -f1 "$sr/proc/uptime" 2>/dev/null) +# The slot this phone's system boots from: SLOT= in boot-update.conf (a +# release installed on the slot it ran from, docs/ACTIVE-SLOT-PLAN.md §2), +# b when the file or the line is absent (development phones, older installs) +want=b want_from="our builds boot from slot b" +v=$(sed -n 's/^SLOT="\{0,1\}\([ab]\)"\{0,1\}$/\1/p' "$sr/etc/omamobile/boot-update.conf" 2>/dev/null | tail -1) +[[ -z $v ]] || want=$v want_from="boot-update.conf says SLOT=$v" case $slot in - _b) check ok 1 "boot" "kernel $release, slot b, up ${up}s" ;; + "_$want") check ok 1 "boot" "kernel $release, slot ${slot#_}, up ${up}s" ;; "") check skip 1 "boot" "kernel $release, no slot in bootconfig or on the command line" ;; - *) check "not ok" 1 "boot" "kernel $release on slot ${slot#_}: our builds boot from slot b" ;; + *) check "not ok" 1 "boot" "kernel $release on slot ${slot#_}: $want_from" ;; esac # ---------------------------------------------------------------- storage -# Every UFS partition is read-only except userdata and slot B's boot images. -allowed=" userdata boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b " +# Every UFS partition is read-only except userdata and the running slot's +# boot images (the guard maps its list to the slot the phone runs from; with +# no slot named, only userdata). +allowed=" userdata " +[[ -z $slot ]] || allowed+="boot$slot init_boot$slot vendor_boot$slot vendor_kernel_boot$slot dtbo$slot " rw=() total=0 -for s in /sys/class/block/sd*; do +for s in "$sr"/sys/class/block/sd*; do [[ -r $s/partition && -r $s/uevent ]] || continue name=$(sed -n 's/^PARTNAME=//p' "$s/uevent") [[ -n $name ]] || continue @@ -106,7 +119,12 @@ if ((total == 0)); then elif ((${#rw[@]})); then check "not ok" 1 "storage guard" "writable outside the allowlist: ${rw[*]}" else - check ok 1 "storage guard" "$total partitions; only userdata and slot B's boot images writable" + imgs=""; [[ -z $slot ]] || imgs=" and slot ${slot#_}'s boot images" + check ok 1 "storage guard" "$total partitions; only userdata$imgs writable" +fi +if [[ -n $slot_from ]]; then + printf '%s\n' "${results[@]}" + exit 0 fi rootdev=$(findmnt -no SOURCE / 2>/dev/null) From 14ae1bab92b87d3e3d5da3323571ed598db4eec8 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:24:03 -0500 Subject: [PATCH 05/10] =?UTF-8?q?ACTIVE-SLOT-PLAN:=20=C2=A76.1,=20the=20ru?= =?UTF-8?q?nning-slot=20guard's=20first=20boot=20on=20a=20development=20ph?= =?UTF-8?q?one?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit §7 step 2 asks for its own short runbook, since the init is in every image and in device-cheetah's boot files: a slot-B boot of the new guard with the partitions' ro values compared before and after, read-only steps first, before the device-cheetah version moves. Co-Authored-By: Claude Opus 5.5 --- docs/ACTIVE-SLOT-PLAN.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/ACTIVE-SLOT-PLAN.md b/docs/ACTIVE-SLOT-PLAN.md index a4cb4649d..1c2b29496 100644 --- a/docs/ACTIVE-SLOT-PLAN.md +++ b/docs/ACTIVE-SLOT-PLAN.md @@ -94,6 +94,17 @@ Before: the radio-backup check passes; the home folder and settings are copied o When every step passes, the maintainer adds the format-2 and accepted-slot lines to cheetah's descriptor and notes, and the next release is format 2. +### 6.1 The running-slot guard on a development phone (slot B), before any of the above + +Step 2 of §7 changes what every phone boots (the init is in device-cheetah's boot files and in every `bin/omamobile initramfs`), so one development boot of it comes first. The 7 Pro on slot B, as today; every step is the maintainer's go; the device-cheetah version is set by the maintainer only after this boots. + +| Step | What | Pass | Stop | Rollback | Risk | +| --- | --- | --- | --- | --- | --- | +| G0 | read only: `grep slot_suffix /proc/bootconfig`; for every `sd*` partition its `PARTNAME` and `ro` | `_b`; the five `_b` boot images and `userdata` `ro` 0, everything else 1 | another slot | none | none | +| G1 | `bin/omamobile initramfs`, `images`, then `systemctl stop omamobile-modem` and `bin/omamobile push` | SSH back on the new boot id | no SSH after about a minute (the safety net crashes back to fastboot or slot A) | `bin/omamobile flash` of the last known-good set (built by the current tool, footers included) | low: the guard is the only change | +| G2 | read only: `dmesg \| grep ufs-ro:`, G0's list again, `tools/phone-smoke` | "running from slot b", "allowlist: boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata"; the same `ro` values as G0; the boot and storage guard rows ok | any `_a` partition `ro` 0, or a `_b` boot image `ro` 1: write nothing, flash back | as G1 | none | +| G3 | a second `bin/omamobile push` of the same images | it writes and verifies through the guard, as before | a read-only refusal | as G1 | low | + ## 7. The changes, in order **First, changing no existing phone:** From a90ddfe15b871cef61600f29fc5fd24f7341814a Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:52:56 -0500 Subject: [PATCH 06/10] initramfs: a slot suffix key in bootconfig decides alone; tests/storage-guard under busybox ash Review of #847: when /proc/bootconfig carries androidboot.slot_suffix at all, the guard now reads only it, so an empty or unquoted value there is unknown (boot partitions read-only) rather than a reason to fall back to the command line. tests/storage-guard gains those two cases and a "_?" suffix with a file _a in the current directory, in bootconfig and on the command line, which fails if set -f is removed; GUARD_SH runs the guard under another shell (busybox ash, as on the phone). Co-Authored-By: Claude Opus 5.5 --- initramfs/init | 8 +++++--- tests/storage-guard | 22 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/initramfs/init b/initramfs/init index 5dfe50dce..810799965 100755 --- a/initramfs/init +++ b/initramfs/init @@ -78,16 +78,18 @@ ufs_slot_add() { # one value: the first is kept, another one makes it unknown if [ -z "$ufs_slot" ]; then ufs_slot=$1 elif [ "$ufs_slot" != "$1" ]; then ufs_slot=bad; fi } +# When bootconfig carries the key at all, it alone decides: an empty or +# unquoted value there is unknown, never a reason to read the command line. ufs_slot_suffix() { local w ufs_slot="" set -f - if [ -r /proc/bootconfig ]; then + if [ -r /proc/bootconfig ] && [ -n "$(sed -n '/^androidboot\.slot_suffix *=/p' /proc/bootconfig)" ]; then for w in $(sed -n 's/^androidboot\.slot_suffix *= *"\(.*\)" *$/\1/p' /proc/bootconfig); do ufs_slot_add "$w" done - fi - if [ -z "$ufs_slot" ] && [ -r /proc/cmdline ]; then + [ -n "$ufs_slot" ] || ufs_slot=bad + elif [ -r /proc/cmdline ]; then for w in $(cat /proc/cmdline); do case "$w" in androidboot.slot_suffix=*) ufs_slot_add "${w#androidboot.slot_suffix=}" ;; esac done diff --git a/tests/storage-guard b/tests/storage-guard index f0536fb42..7a07a206a 100755 --- a/tests/storage-guard +++ b/tests/storage-guard @@ -142,8 +142,11 @@ fake_disks() { echo 0 > "$B/sys/block/sde/sde$i/ro"; : > "$B/dev/sde$i" done } +# GUARD_SH: the shell the guard runs under (default sh; the phone's is +# busybox ash, e.g. GUARD_SH="busybox sh") +GSH=${GUARD_SH:-sh} guard() { # run the guard's load and one pass - PATH="$t/bin:$PATH" sh -c ". '$t/guard-test.sh'; ufs_ro_pass" + PATH="$t/bin:$PATH" $GSH -c ". '$t/guard-test.sh'; ufs_ro_pass" } writable() { # names of the partitions left writable on a disk (sda), in GPT order local s out="" @@ -188,7 +191,7 @@ slot_case() { # what, expected writable set (sorted), expected kmsg text; disks fake_disks _a; cp "$t/etc-cheetah/omamobile-ufs-rw-slot" "$B/etc/" slot_case "slot A (bootconfig): its five boot images and userdata, slot B read-only" "$cheetah_a" "allowlist: $cheetah_a" grep -q "running from slot a" "$B/dev/kmsg" || fail "slot A is logged" -unguarded() { PATH="$t/bin:$PATH" sh -c ". '$t/guard-test.sh'; ufs_unguarded"; } +unguarded() { PATH="$t/bin:$PATH" $GSH -c ". '$t/guard-test.sh'; ufs_unguarded"; } [[ -z $(unguarded) ]] || fail "slot A: nothing outside its allowlist is writable (got: $(unguarded))" echo 0 > "$B/sys/block/sda/sda2/ro" [[ $(unguarded) == "sda2 (boot_b)" ]] || fail "slot A: a reopened boot_b is reported at the hand-over (got: $(unguarded))" @@ -207,6 +210,17 @@ fake_disks _c slot_case "a suffix that is neither _a nor _b: userdata alone writable" "userdata" "no slot suffix" fake_disks none; printf 'androidboot.slot_suffix=_b androidboot.slot_suffix=* x\n' > "$B/proc/cmdline" slot_case "a glob for a suffix on the command line: userdata alone writable" "userdata" "no slot suffix" +fake_disks none; printf 'androidboot.slot_suffix = ""\n' >> "$B/proc/bootconfig"; printf 'androidboot.slot_suffix=_a\n' > "$B/proc/cmdline" +slot_case "an empty suffix in bootconfig: unknown, the command line not read" "userdata" "every boot partition read-only, userdata writable" +fake_disks none; printf 'androidboot.slot_suffix = _a\n' >> "$B/proc/bootconfig"; printf 'androidboot.slot_suffix=_a\n' > "$B/proc/cmdline" +slot_case "an unquoted suffix in bootconfig: unknown, the command line not read" "userdata" "every boot partition read-only, userdata writable" +# Globbing is off while the suffix is read: with a file _a in the current +# directory, "_?" must not become _a +mkdir -p "$t/cwd"; : > "$t/cwd/_a" +fake_disks _? +(cd "$t/cwd" && slot_case "a glob for a suffix in bootconfig, a file _a beside it: userdata alone writable" "userdata" "every boot partition read-only") +fake_disks none; printf 'androidboot.slot_suffix=_? x\n' > "$B/proc/cmdline" +(cd "$t/cwd" && slot_case "a glob on the command line, a file _a beside it: userdata alone writable" "userdata" "every boot partition read-only") fake_disks _a slot_case "no slot marker: the list is still mapped to the running slot" "$cheetah_a" "running from slot a" for bad in slot-b '' 'running x'; do @@ -220,7 +234,7 @@ done pass "a slot marker other than 'running' leaves every partition read-only" # The check boot_arch makes before handing over: nothing outside the list. -unguarded() { PATH="$t/bin:$PATH" sh -c ". '$t/guard-test.sh'; ufs_unguarded"; } +unguarded() { PATH="$t/bin:$PATH" $GSH -c ". '$t/guard-test.sh'; ufs_unguarded"; } [[ -z $(unguarded) ]] || fail "after a pass nothing outside the allowlist is writable (got: $(unguarded))" mkdir -p "$B/sys/block/sda/sda99" printf 'PARTNAME=late\n' > "$B/sys/block/sda/sda99/uevent"; echo 0 > "$B/sys/block/sda/sda99/ro" @@ -287,7 +301,7 @@ fake_disks; printf 'boot_a userdata\n' > "$B/etc/omamobile-ufs-rw" closed "a slot A name in the allowlist" "allowlist names boot_a" # --- A second LUN: boot_b on sde (enchilada's layout) --- -unguarded() { PATH="$t/bin:$PATH" sh -c ". '$t/guard-test.sh'; ufs_unguarded"; } +unguarded() { PATH="$t/bin:$PATH" $GSH -c ". '$t/guard-test.sh'; ufs_unguarded"; } fake_disks cp "$t/etc-lun/omamobile-ufs-rw" "$t/etc-lun/omamobile-ufs-rw-luns" "$B/etc/" guard From fc96ab15f6ec01af985c6e4368e0c3e79227804b Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:54:04 -0500 Subject: [PATCH 07/10] device/dodge: the chain forwards slot suffix _b only Review of #847: stage 2's storage guard now opens the boot images of the slot androidboot.slot_suffix names, and the chain forwards that suffix from bootconfig, chosen/bootargs or the packed OMAMOBILE_SLOT fallback. This chain boots slot B only, so any other value (slot A, a stray packed string) is logged and not forwarded, and stage 2 keeps every boot partition read-only. tests/boot-method runs the filter on _b, _a, a stray value and none. Co-Authored-By: Claude Opus 5.5 --- device/dodge/chainload/README.md | 5 ++++- device/dodge/chainload/init | 11 +++++++++++ tests/boot-method | 10 ++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/device/dodge/chainload/README.md b/device/dodge/chainload/README.md index 3076d492a..684a63054 100644 --- a/device/dodge/chainload/README.md +++ b/device/dodge/chainload/README.md @@ -18,7 +18,10 @@ our DTB without flashing `vendor_boot` is this chain: `/omamobile/slot_suffix` (from `OMAMOBILE_SLOT` — this ABL puts the property in neither bootconfig nor bootargs under `fastboot boot`; stage 1 dumps both to kmsg and logs `source=packed`) (stage 1's own `/proc/cmdline` is - `CMDLINE_FORCE`d and has no ABL keys), and `kexec -c` + `CMDLINE_FORCE`d and has no ABL keys); it forwards `_b` only, since + stage 2's storage guard opens the slot it is told and this chain boots + slot B (any other value is logged and dropped, so stage 2's boot + partitions stay read-only); and `kexec -c` into stage 2. Stage 1's kernel config has UFS disabled (`SCSI_UFS_QCOM` / `SCSI_UFSHCD`), so it does not probe storage on stock's DTB. diff --git a/device/dodge/chainload/init b/device/dodge/chainload/init index 48898d566..0dd73ca90 100755 --- a/device/dodge/chainload/init +++ b/device/dodge/chainload/init @@ -72,6 +72,17 @@ if [ -z "$SLOT_SUFFIX" ] && [ -f /omamobile/slot_suffix ]; then SLOT_SUFFIX=$($B cat /omamobile/slot_suffix) SLOT_SRC=packed fi +# Stage 2's storage guard opens the boot images of the slot it is told +# (docs/ACTIVE-SLOT-PLAN.md §4), and this chain boots slot B only: forward +# _b alone. Anything else (slot A, a packed value, a stray string) is not +# forwarded, so stage 2 sees no suffix and keeps every boot partition +# read-only. +case $SLOT_SUFFIX in + ""|_b) ;; + *) + echo "omamobile-chain: slot_suffix=$SLOT_SUFFIX source=$SLOT_SRC not forwarded (only _b is): stage 2 keeps every boot partition read-only" > /dev/kmsg 2>/dev/null || true + SLOT_SUFFIX= SLOT_SRC= ;; +esac if [ -n "$SLOT_SUFFIX" ]; then echo "omamobile-chain: slot_suffix=$SLOT_SUFFIX source=$SLOT_SRC" > /dev/kmsg 2>/dev/null || true tok="androidboot.slot_suffix=$SLOT_SUFFIX" diff --git a/tests/boot-method b/tests/boot-method index 1b09223ca..9fe41f20d 100755 --- a/tests/boot-method +++ b/tests/boot-method @@ -230,6 +230,16 @@ printf '_b' > "$pk" got=$("$rs" /nonexistent /nonexistent "$pk") [[ $got == _b ]] || fail "packed fallback failed, got '$got'" echo 'ok - slot_suffix falls back to packed /omamobile/slot_suffix' +# Only _b is forwarded to stage 2 (whose guard opens the slot it is told) +fwd=$(sed -n '/^case \$SLOT_SUFFIX in$/,/^esac$/p' device/dodge/chainload/init) +[[ -n $fwd ]] || fail "the chain's forwarding filter is in its init" +for v in _b _a packed-junk _c ''; do + got=$(SLOT_SUFFIX=$v SLOT_SRC=packed sh -c "$(printf '%s\n' "$fwd" | sed 's|/dev/kmsg|/dev/null|')"' +printf "%s" "$SLOT_SUFFIX"') + if [[ $v == _b ]]; then [[ $got == _b ]] || fail "_b is forwarded (got '$got')" + else [[ -z $got ]] || fail "'$v' is not forwarded (got '$got')"; fi +done +echo 'ok - the chain forwards slot suffix _b only; _a, a packed stray value or none forward nothing' got=$("$rs" "$bc" /nonexistent "$pk") [[ $got == _b ]] || fail "bootconfig must beat packed, got '$got'" # Force bootconfig to _a to prove priority over packed _b From a0fb2c41f9a8c6d777db183f50ab115c6f6f9a9a Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:55:48 -0500 Subject: [PATCH 08/10] bin/omamobile: push needs slot B up front; flash --from-slot-a replaces the environment override MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review of #847. push now requires the phone to name slot B before anything else, modules-push included: slot A, no slot named, or a failed read is refused. flash's override for a development phone that fell back to stock on slot A is an explicit flag, --from-slot-a, which prints a loud warning and waits 10 s before writing slot B and making it active; the OMAMOBILE_FLASH_FROM_SLOT_A variable is gone. CONTRIBUTING §3.4 step 5, KERNEL-UPDATE-PLAN's fastboot recovery and ACTIVE-SLOT-PLAN §6.1's rollbacks name the flag. The plan's risks gain one: a RAM boot with stock active on slot A now opens stock's _a boot images. Co-Authored-By: Claude Opus 5.5 --- CONTRIBUTING.md | 2 +- bin/omamobile | 41 ++++++++++++++++++++++------ device/cheetah/KERNEL-UPDATE-PLAN.md | 2 +- docs/ACTIVE-SLOT-PLAN.md | 7 +++-- tests/images-layout | 23 +++++++++++----- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9a48ae28c..cf08a8271 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -116,7 +116,7 @@ Bring-up steps hang phones. Plan for it before you start: 2. Unplug and replug the cable once. 3. Force a restart: hold Power for about 30 s until the phone is off, then press Power. On the Pixel 8 Pro, hold Power + Volume Up for 15-30 s; Power alone does nothing under our kernel ([BRINGUP-M1](device/husky/BRINGUP-M1.md) test 23). 4. Reach fastboot by hand: with the phone off, hold Volume Down, add Power, and keep both held about 20 s until "Fastboot Mode" appears. Letting go early lands on the unlocked-bootloader warning, which carries on booting slot B. - 5. From fastboot, flash known-good images to slot B, or `fastboot --set-active=a` to boot stock Android. + 5. From fastboot, flash known-good images to slot B, or `fastboot --set-active=a` to boot stock Android. `bin/omamobile flash` refuses a phone whose `current-slot` is `a`, since a release installed on slot A looks the same from fastboot; for a development phone that fell back to stock on slot A, run `bin/omamobile flash --from-slot-a` (it warns, then writes slot B and makes it active). - **Slot fallback:** a slot B that hasn't been marked successful falls back to stock slot A after the bootloader's retries run out. Once `omamobile-boot-success` has marked slot B, don't rely on that fallback: use fastboot. Keep a known-good image set (we keep one in `~/omamobile-kernel/known-good`). ### 3.5 Never touch PMU registers from userspace; mind `devmem` diff --git a/bin/omamobile b/bin/omamobile index 71bd907e3..0ef4b5294 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -90,6 +90,7 @@ Usage: [DEVICE=] omamobile fastboot Get the phone into fastboot without buttons (adb, reboot-to, or crash until ABL falls back to stock Android on slot A) flash Flash the experiment images to slot B and boot them + (--from-slot-a: a development phone that fell back to stock on slot A) push Write the images into slot B from the running phone over SSH, verify them, and reboot into them (no fastboot) kernel-shell Open a shell in the build environment, in the kernel tree @@ -1225,7 +1226,13 @@ cmd_fastboot() { } cmd_flash() { - local img="$KERNEL_OUT/images" p + local img="$KERNEL_OUT/images" p from_a=0 + case ${1:-} in + "") ;; + --from-slot-a) from_a=1 ;; + *) die "usage: omamobile flash [--from-slot-a]" ;; + esac + (($# <= 1)) || die "usage: omamobile flash [--from-slot-a]" load_device # Qualcomm ABL keeps slot flags in the GPT. A fastboot set_active can clear # successful and refill retries; the bit-54 mark has no §3.1 entry yet. @@ -1244,10 +1251,24 @@ cmd_flash() { # (docs/ACTIVE-SLOT-PLAN.md §2), and this writes slot B and makes it # active, which is development only. Fastboot can't tell that release # from stock Android on A, which a development phone falls back to (and - # `omamobile fastboot` crashes it into), so that case is named by hand. + # `omamobile fastboot` crashes it into), so that case is named by hand + # with --from-slot-a. cur=$(timeout 10 fastboot -s "$serial" getvar current-slot 2>&1 | sed -n 's/^current-slot: *_\{0,1\}\([ab]\)$/\1/p' | head -1) - if [[ $cur == a && ${OMAMOBILE_FLASH_FROM_SLOT_A:-} != yes ]]; then - die "this phone runs from slot A (a release from slot A, or stock Android); flash writes slot B and makes it active, development only. A development phone that fell back to stock on slot A: run again with OMAMOBILE_FLASH_FROM_SLOT_A=yes" + if [[ $cur == a ]]; then + ((from_a)) || die "this phone runs from slot A (a release from slot A, or stock Android); flash writes slot B and makes it active, development only. A development phone that fell back to stock on slot A: omamobile flash --from-slot-a" + printf '\e[1;31m' >&2 + cat >&2 <<'WARN' +WARNING: --from-slot-a. This phone runs from slot A, and flash writes slot B +and makes it active. That is right only for a development phone that fell +back to stock Android on slot A. On a phone that runs a release from slot A +it switches the phone to slot B: the release's slot is left behind and both +systems share one userdata. Stop now (Ctrl-C) unless this is a development +phone. Going on in 10 seconds. +WARN + printf '\e[0m' >&2 + sleep "${OMAMOBILE_FLASH_WARN_SECONDS:-10}" + elif ((from_a)); then + say "--from-slot-a given, but the phone reads current-slot ${cur:-unknown}: flashing as usual" fi # Every image and allowlist entry first: never start writing slot B and # find out halfway that a later image is missing. @@ -1286,13 +1307,15 @@ cmd_push() { die "push refuses BOOT_METHOD=qcom-abl (no in-tree day-2 update path yet; see device/$DEVICE/BRINGUP-M1.md)" images_for_device "$img" "$ssh" true || die "phone not reachable at 172.16.42.1" - # Read-only: a release installed on the slot it ran from runs from slot A - # (docs/ACTIVE-SLOT-PLAN.md §2), where its storage guard leaves slot B - # read-only anyway; say so plainly before anything else + # Read-only, before anything else (modules-push included): push writes + # slot B, so the phone must say it runs from B. A release installed on the + # slot it ran from runs from A (docs/ACTIVE-SLOT-PLAN.md §2); a boot that + # names no slot, or a failed read, is refused too. local slots slots=$("$ssh" "cat /proc/bootconfig /proc/cmdline 2>/dev/null | grep -oE 'androidboot[.]slot_suffix( = \"|=)_[ab]'; true") \ - || die "cannot read the phone's boot slot" + || die "cannot read the phone's boot slot; refusing to write" [[ $slots != *_a* ]] || die "this phone runs a release from slot A; push writes slot B, development only" + [[ $slots == *_b* ]] || die "the phone names no boot slot (androidboot.slot_suffix in /proc/bootconfig or /proc/cmdline); push writes slot B only to a phone running from it" # Refuse before any write. A phone whose initramfs has no reboot-to would # otherwise take the images, swallow the reboot, and report success while # the old kernel is still running. @@ -1393,7 +1416,7 @@ case "${1:-}" in images) cmd_images ;; kexec) cmd_kexec ;; fastboot) cmd_fastboot ;; - flash) cmd_flash ;; + flash) shift; cmd_flash "$@" ;; push) cmd_push ;; kernel-shell) cmd_kernel_shell ;; ''|-h|--help) usage ;; diff --git a/device/cheetah/KERNEL-UPDATE-PLAN.md b/device/cheetah/KERNEL-UPDATE-PLAN.md index f5b63f8ef..2871b5329 100644 --- a/device/cheetah/KERNEL-UPDATE-PLAN.md +++ b/device/cheetah/KERNEL-UPDATE-PLAN.md @@ -12,7 +12,7 @@ No kernel branch: the kernel is the published release tag `kernel-2026.11.01`, b - Before each write it saves slot B's five partitions, whole, and the running release's modules in `/var/lib/omamobile/boot/previous/` (about 220 MB on cheetah plus one module tree); `omamobile-boot-update rollback` writes them back. - It never reboots, never touches slot A, `vbmeta`, `devinfo` or the bootloader, never changes the storage guard. -**Recovery when slot B doesn't boot.** `omamobile-boot-success` marks slot B successful at every good boot, so ABL does **not** fall back to slot A after a bad write: the phone stops in fastboot or keeps resetting, and `previous/` on `userdata` is out of reach. So the fallback lives on the laptop: step R2 copies the set slot B holds today into `~/omamobile-kernel/known-good/2026-11-02/images/` (with its `DEVICE` file) and checks the copy against the phone, and P3 also copies `previous/` off the phone before any reboot. Never flash from `~/omamobile-kernel/out-release`: other tools build into it (`tools/release build` writes the release kernel there). Then: Power held about 30 s, then Volume Down + Power about 20 s for fastboot, and on the laptop `KERNEL_OUT=~/omamobile-kernel/known-good/2026-11-02 bin/omamobile flash`. On the recovery screen of a stock boot choose neither option (AGENTS §4); a stock boot means a fresh read-only backup of `efs`, `persist` and `modem_userdata` afterwards. +**Recovery when slot B doesn't boot.** `omamobile-boot-success` marks slot B successful at every good boot, so ABL does **not** fall back to slot A after a bad write: the phone stops in fastboot or keeps resetting, and `previous/` on `userdata` is out of reach. So the fallback lives on the laptop: step R2 copies the set slot B holds today into `~/omamobile-kernel/known-good/2026-11-02/images/` (with its `DEVICE` file) and checks the copy against the phone, and P3 also copies `previous/` off the phone before any reboot. Never flash from `~/omamobile-kernel/out-release`: other tools build into it (`tools/release build` writes the release kernel there). Then: Power held about 30 s, then Volume Down + Power about 20 s for fastboot, and on the laptop `KERNEL_OUT=~/omamobile-kernel/known-good/2026-11-02 bin/omamobile flash`; if the phone fell back to stock on slot A (`fastboot getvar current-slot` reads `a`), `flash` refuses it and the same command takes `--from-slot-a`, which warns before it writes slot B and makes it active. On the recovery screen of a stock boot choose neither option (AGENTS §4); a stock boot means a fresh read-only backup of `efs`, `persist` and `modem_userdata` afterwards. ## 2. Why the running kernel can't be packaged as it is diff --git a/docs/ACTIVE-SLOT-PLAN.md b/docs/ACTIVE-SLOT-PLAN.md index 1c2b29496..4da766703 100644 --- a/docs/ACTIVE-SLOT-PLAN.md +++ b/docs/ACTIVE-SLOT-PLAN.md @@ -101,9 +101,9 @@ Step 2 of §7 changes what every phone boots (the init is in device-cheetah's bo | Step | What | Pass | Stop | Rollback | Risk | | --- | --- | --- | --- | --- | --- | | G0 | read only: `grep slot_suffix /proc/bootconfig`; for every `sd*` partition its `PARTNAME` and `ro` | `_b`; the five `_b` boot images and `userdata` `ro` 0, everything else 1 | another slot | none | none | -| G1 | `bin/omamobile initramfs`, `images`, then `systemctl stop omamobile-modem` and `bin/omamobile push` | SSH back on the new boot id | no SSH after about a minute (the safety net crashes back to fastboot or slot A) | `bin/omamobile flash` of the last known-good set (built by the current tool, footers included) | low: the guard is the only change | +| G1 | `bin/omamobile initramfs`, `images`, then `systemctl stop omamobile-modem` and `bin/omamobile push` | SSH back on the new boot id | no SSH after about a minute (the safety net crashes back to fastboot or slot A) | `bin/omamobile flash` of the last known-good set (built by the current tool, footers included); after a fallback to stock on slot A (`current-slot` a), `bin/omamobile flash --from-slot-a`, which warns before writing | low: the guard is the only change | | G2 | read only: `dmesg \| grep ufs-ro:`, G0's list again, `tools/phone-smoke` | "running from slot b", "allowlist: boot_b init_boot_b vendor_boot_b vendor_kernel_boot_b dtbo_b userdata"; the same `ro` values as G0; the boot and storage guard rows ok | any `_a` partition `ro` 0, or a `_b` boot image `ro` 1: write nothing, flash back | as G1 | none | -| G3 | a second `bin/omamobile push` of the same images | it writes and verifies through the guard, as before | a read-only refusal | as G1 | low | +| G3 | a second `bin/omamobile push` of the same images | it writes and verifies through the guard, as before | a read-only refusal | as G1 (`flash`, or `flash --from-slot-a` after a fallback to slot A) | low | ## 7. The changes, in order @@ -137,4 +137,5 @@ Step 2 of §7 changes what every phone boots (the init is in device-cheetah's bo 6. **Version skew** fails closed both ways: old installers refuse format 2, old hooks refuse `SLOT=`; existing slot-B phones keep `SLOT=b` by default. 7. **The modem's slot** while `b` is accepted (§4). 8. **The reference phone ends with the release on A and the development system on B sharing one `userdata`.** How development continues is the maintainer's choice (likely `bin/omamobile flash` to B over the release root, or a separate development phone). -9. **Secure mode later** writes `vbmeta_` and the custom key with no stock fallback on a locked phone: SECURITY-PLAN's wording changes, not its design. +9. **A RAM boot (`fastboot boot`) with stock active on slot A now opens stock's `_a` boot images**: the bootloader names `_a`, and the running-slot guard maps the list to it. Before, such a boot opened slot B's. Nothing of ours writes there from a RAM boot (`push` requires `_b`, `omamobile-boot-update` requires its `SLOT`), but the guard no longer keeps stock's boot images read-only on it. +10. **Secure mode later** writes `vbmeta_` and the custom key with no stock fallback on a locked phone: SECURITY-PLAN's wording changes, not its design. diff --git a/tests/images-layout b/tests/images-layout index fe28bf569..2c1099426 100755 --- a/tests/images-layout +++ b/tests/images-layout @@ -7,8 +7,7 @@ # image too big for its partition before writing any, writes and verifies the # images and tells a reboot that did not happen from a phone that never came # back; both refuse a phone that runs from slot A (a release there), flash -# unless OMAMOBILE_FLASH_FROM_SLOT_A=yes names a development phone fallen -# back to stock. Then, with a stub kernel output (a random Image, minimal DTBs, a +# unless --from-slot-a names a development phone fallen back to stock. Then, with a stub kernel output (a random Image, minimal DTBs, a # random initramfs), a gs201-style descriptor yields its five boot images and # a gs101-style one (no init_boot or vendor_kernel_boot) yields boot (ramdisk # and OS version), vendor_boot (DTB and initramfs) and dtbo; a zuma-style one @@ -164,9 +163,14 @@ for cur in a _a; do done grep -c 'getvar current-slot' "$t/fastboot.log" >/dev/null || fail "the slot is read with getvar" : > "$t/fastboot.log" -CURRENT_SLOT=a OMAMOBILE_FLASH_FROM_SLOT_A=yes om six flash || { cat "$t/log"; fail "OMAMOBILE_FLASH_FROM_SLOT_A=yes lets a development phone back onto slot B"; } +grep -c 'omamobile flash --from-slot-a' "$t/log" >/dev/null || fail "the refusal names --from-slot-a" +OMAMOBILE_FLASH_FROM_SLOT_A=yes CURRENT_SLOT=a om six flash && fail "the old environment override is gone" +: > "$t/fastboot.log" +OMAMOBILE_FLASH_WARN_SECONDS=0 CURRENT_SLOT=a om six flash --from-slot-a || { cat "$t/log"; fail "--from-slot-a lets a development phone back onto slot B"; } [[ $(flashed) == "boot_b vendor_boot_b dtbo_b " ]] || fail "and flash writes slot B as before" -pass "flash refuses a phone running from slot A unless OMAMOBILE_FLASH_FROM_SLOT_A=yes" +grep -c 'WARNING: --from-slot-a' "$t/log" >/dev/null || fail "--from-slot-a prints a loud warning" +om six flash --bogus && fail "flash refuses an unknown option" +pass "flash refuses a phone running from slot A unless --from-slot-a, which warns" # Bad descriptors are refused before anything is built or written bad() { # what, sed expression on six's descriptor, expected message @@ -216,7 +220,7 @@ cat > "$R/tools/phone-ssh" <<'PHONE' P=$T/phone cmd="$*" case $cmd in true|*'command -v reboot-to'*) exit 0 ;; - *androidboot*slot_suffix*) printf '%s\n' "${PHONE_SLOT:-androidboot.slot_suffix = \"_b}" ;; + *androidboot*slot_suffix*) [[ ${PHONE_SLOT-} != FAIL ]] || exit 255; printf '%s\n' "${PHONE_SLOT-androidboot.slot_suffix = \"_b}" ;; 'cat /proc/sys/kernel/random/boot_id') n=$(( $(cat "$P/reads" 2>/dev/null || echo 0) + 1 )); echo $n > "$P/reads" state=$(sed -n "${n}p" "$P/script"); [[ -n $state ]] || state=$(tail -n 1 "$P/script") @@ -246,8 +250,13 @@ for slot in 'androidboot.slot_suffix = "_a' 'androidboot.slot_suffix=_a'; do grep -c 'this phone runs a release from slot A; push writes slot B, development only' "$t/log" >/dev/null || { cat "$t/log"; fail "push says why"; } [[ $(find "$P" -name 'sda_*' | wc -l) == 0 && ! -s $P/log ]] || fail "push writes nothing and does not reboot on slot A" done -PHONE_SLOT= push old down new || { cat "$t/log"; fail "push on a phone that names no slot goes on as before"; } -pass "push refuses a phone running from slot A before any write" +if PHONE_SLOT= push old new; then fail "push refuses a phone that names no slot"; fi +grep -c 'the phone names no boot slot' "$t/log" >/dev/null || { cat "$t/log"; fail "push says the phone names no slot"; } +[[ $(find "$P" -name 'sda_*' | wc -l) == 0 && ! -s $P/log ]] || fail "push writes nothing when no slot is named" +if PHONE_SLOT=FAIL push old new; then fail "push refuses when the slot can't be read"; fi +grep -c "cannot read the phone's boot slot" "$t/log" >/dev/null || { cat "$t/log"; fail "push says the read failed"; } +grep -c 'modules-push\|Module marker' "$t/log" >/dev/null && fail "the slot is checked before the modules" +pass "push refuses a phone not running from slot B (A, none named, unreadable) before modules-push and any write" if push old old; then fail "push fails when the boot id never changes"; fi grep -c 'same boot id: the reboot did not happen' "$t/log" >/dev/null || { cat "$t/log"; fail "an unchanged boot id is reported as such"; } pass "push reports a reboot that did not happen" From 74de60cff9bc5d54476a21234682875ee9412333 Mon Sep 17 00:00:00 2001 From: Derek Ross Date: Mon, 16 Nov 2026 10:57:44 -0500 Subject: [PATCH 09/10] omamobile-boot-update, phone-smoke, omamobile-update: SLOT= once; the other slot's previous/ moved aside Review of #847. A boot-update.conf that names SLOT twice is refused by omamobile-boot-update, fails phone-smoke's boot row and is unreadable to omamobile-update, the same verdict everywhere. A previous/ saved from the other slot is no longer deleted before the next save: it is moved to previous.slot--