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 f4ba4a3c3..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 @@ -102,6 +103,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 +590,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 @@ -1219,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. @@ -1232,8 +1245,31 @@ 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 + # 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 ]]; 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. for p in $DEV_BOOT_IMAGES; do @@ -1271,6 +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, 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; 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. @@ -1371,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/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/docs/ACTIVE-SLOT-PLAN.md b/docs/ACTIVE-SLOT-PLAN.md index a4cb4649d..4da766703 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); 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 (`flash`, or `flash --from-slot-a` after a fallback to slot A) | low | + ## 7. The changes, in order **First, changing no existing phone:** @@ -126,4 +137,5 @@ When every step passes, the maintainer adds the format-2 and accepted-slot lines 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/initramfs/init b/initramfs/init index f5782c331..810799965 100755 --- a/initramfs/init +++ b/initramfs/init @@ -46,25 +46,58 @@ 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 +} +# 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 ] && [ -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 + [ -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 + 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 +127,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/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..5fa9ac741 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,32 @@ 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 + [[ -z $slot ]] || refuse "$CONF:$n: a second SLOT= line (the slot must be named once)" + 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 +171,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 +223,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 +268,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 +292,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 +330,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 +413,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 +434,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 +442,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 +456,23 @@ 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: moved + # aside (previous.slot--