From 4bd1e43632ad305d09fd8125c592a04d0bfb507c Mon Sep 17 00:00:00 2001 From: Matt Barr Date: Tue, 10 Nov 2026 22:12:48 -0500 Subject: [PATCH 1/5] push: find slot B's images on the descriptor's UFS_RW_LUNS LUNs too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit push looked for _b only under /sys/block/sda. It now looks on sda and on each LUN in UFS_RW_LUNS, the same LUNs the init guards, and refuses a name found on more than one partition or a path it doesn't expect. No descriptor sets UFS_RW_LUNS yet, so every lookup today is the sda one it was; enchilada's sde comes with its slot-B install, after its CONTRIBUTING §3.1 entry. tests/images-layout: the fake phone places partitions on one LUN; push finds them on a UFS_RW_LUNS LUN, not on a LUN the descriptor doesn't name, and refuses a duplicate name before writing anything. Co-Authored-By: Claude Opus 5.5 --- bin/omamobile | 16 +++++++++++----- docs/RELEASES.md | 2 +- tests/images-layout | 32 +++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/bin/omamobile b/bin/omamobile index fb234d0e9..fdb9a848a 100755 --- a/bin/omamobile +++ b/bin/omamobile @@ -1363,16 +1363,22 @@ cmd_push() { # AVB_FOOTERS image is padded to the descriptor's partition size, with its # footer at the end, so that size must be the partition's exactly. local -A devs=() - local sectors bytes avb e + local sectors bytes avb e sys globs="" l + # The LUNs the init guards: sda, plus the descriptor's UFS_RW_LUNS + # (load_device checked those are sdb..sdz) + for l in sda ${DEV_UFS_RW_LUNS:-}; do globs+=" /sys/block/$l/$l*"; done for p in $DEV_BOOT_IMAGES; do slot_b_allowed "$p" || die "${p}_b is not on device/$DEVICE's storage-guard allowlist; refusing" [[ -f "$img/$p.img" ]] || die "missing $img/$p.img (run: omamobile images)" # Find the partition by GPT name; the init guard leaves only these writable - dev=$("$ssh" "for s in /sys/block/sda/sda*; do grep -qx 'PARTNAME=${p}_b' \$s/uevent && echo /dev/\${s##*/}; done; true") - [[ -n "$dev" ]] || die "${p}_b not found on the phone" - [[ "$("$ssh" "cat /sys/block/sda/${dev#/dev/}/ro")" == 0 ]] || die "$dev (${p}_b) is read-only; refusing" + sys=$("$ssh" "for s in$globs; do grep -qx 'PARTNAME=${p}_b' \$s/uevent && echo \$s; done; true") + [[ -n "$sys" ]] || die "${p}_b not found on the phone (looked on:$globs)" + [[ $sys != *$'\n'* ]] || die "${p}_b is on more than one partition (${sys//$'\n'/, }); refusing" + [[ $sys =~ ^/sys/block/(sd[a-z])/(sd[a-z])[0-9]+$ && ${BASH_REMATCH[1]} == "${BASH_REMATCH[2]}" ]] || die "unexpected partition path for ${p}_b: $sys" + dev=/dev/${sys##*/} + [[ "$("$ssh" "cat $sys/ro")" == 0 ]] || die "$dev (${p}_b) is read-only; refusing" # sysfs counts 512-byte sectors, whatever the device's block size - sectors=$("$ssh" "cat /sys/block/sda/${dev#/dev/}/size") + sectors=$("$ssh" "cat $sys/size") [[ $sectors =~ ^[0-9]{1,15}$ ]] || die "cannot read the size of $dev (${p}_b)" bytes=$((sectors * 512)) (($(stat -c %s "$img/$p.img") <= bytes)) || die "$p.img is larger than ${p}_b ($dev, $bytes bytes); refusing" diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 9ace1d840..4b8e56cd3 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -82,7 +82,7 @@ Bring-up tools (`*-stage`, `display-hz`, `usbc-deadman`) stay per plan: they pok | `BOOT_IMAGES` | `boot init_boot vendor_boot vendor_kernel_boot dtbo` | The images written to slot B (the `_b` suffix is added by the tools and is not configurable). `boot`, `vendor_boot` and `dtbo` are always there. Without `init_boot`, `boot` carries the empty generic ramdisk and the OS version; without `vendor_kernel_boot`, `vendor_boot` carries the DTB and the initramfs (the Pixel 6: `boot vendor_boot dtbo`) | | `BOOT_UPDATE_AUTO_WRITE` | (unset; defaults to `yes`) | Optional: `yes` or `no`. The installer records it as `AUTO_WRITE` in `/etc/omamobile/boot-update.conf`; `no` keeps an installed development root from writing slot B through package updates. It does not change the installer's own allowed writes | | `UFS_RW` | `boot_b … dtbo_b userdata` | The storage guard's allowlist, packed into the initramfs as `/etc/omamobile-ufs-rw`. Only slot B's boot images (`boot_b`, `init_boot_b`, `vendor_boot_b`, `vendor_kernel_boot_b`, `dtbo_b`) and `userdata` are accepted, `userdata` and every `BOOT_IMAGES` entry must be on it, and each name only once. The init fails closed: a missing, empty or malformed file, or any other name in it, leaves every partition read-only | -| `UFS_RW_LUNS` | (unset) | Optional. The LUNs besides `sda` that hold `UFS_RW` partitions, as disk names `sdb` to `sdz`, packed as `/etc/omamobile-ufs-rw-luns`. The init guards each as it guards `sda`: the whole-disk node removed, the disk left writable, and only its `UFS_RW` partitions left writable. Absent (every descriptor so far), malformed, or with a closed allowlist: every LUN but `sda` stays read-only at the disk level. Boot-image writers on the phone (`omamobile-boot-update`) still look only on `sda` | +| `UFS_RW_LUNS` | (unset) | Optional. The LUNs besides `sda` that hold `UFS_RW` partitions, as disk names `sdb` to `sdz`, packed as `/etc/omamobile-ufs-rw-luns`. The init guards each as it guards `sda`: the whole-disk node removed, the disk left writable, and only its `UFS_RW` partitions left writable. Absent (every descriptor so far), malformed, or with a closed allowlist: every LUN but `sda` stays read-only at the disk level. `omamobile push` looks for slot B's images on these LUNs too; boot-image writers on the phone (`omamobile-boot-update`) still look only on `sda` | | `INITRAMFS_INIT` | `/initramfs/init` | The init script. A leading `/` is relative to the repo root, so devices share one script. Any other value is relative to the descriptor | | `INITRAMFS_ROOT_LABEL` | (unset) | Optional: `omamobile-root` enables read-only label/header checks before ext4 repair or mounting; only `e2fsck -p` statuses 0 and 1 proceed. Omitted: existing root handling, including unlabelled hand-installed roots, is unchanged | | `INITRAMFS_ROOT_MODE` | (unset) | Optional: `persistent-development` or `ram-rescue`, requiring `INITRAMFS_ROOT_LABEL=omamobile-root`. The former skips growth and writes `/var/lib/omamobile/root-boot-pending` before handover; an installed, enabled SSH-ready helper must clear it after USB SSH is ready, or subsequent boots stay in RAM until the marker is cleared. The latter always stays in RAM. Omitted: existing handover behavior | diff --git a/tests/images-layout b/tests/images-layout index 2c1099426..43a0355aa 100755 --- a/tests/images-layout +++ b/tests/images-layout @@ -226,9 +226,14 @@ case $cmd in state=$(sed -n "${n}p" "$P/script"); [[ -n $state ]] || state=$(tail -n 1 "$P/script") case $state in old) echo old-boot ;; new) echo new-boot ;; *) exit 255 ;; esac ;; *.omamobile-tarball*) cat "$P/modules-sum" ;; - *PARTNAME=*) [[ $cmd =~ PARTNAME=([a-z_]+) ]] && echo "/dev/sda_${BASH_REMATCH[1]}" ;; - cat\ /sys/block/sda/*/ro) echo 0 ;; - cat\ /sys/block/sda/*/size) echo "${SECTORS:-131072}" ;; + # Each partition sits on $FAKE_LUN (default sda) and is found only when + # push looks there; FAKE_DUP adds a second partition of the same name. + *PARTNAME=*) L=${FAKE_LUN:-sda} + [[ $cmd == *" /sys/block/$L/$L*"* && $cmd =~ PARTNAME=([a-z_]+) ]] || exit 0 + echo "/sys/block/$L/$L$(( $(printf %s "${BASH_REMATCH[1]}" | cksum | cut -d' ' -f1) % 90 + 1 ))" + [[ -z ${FAKE_DUP:-} ]] || echo "/sys/block/$L/${L}99" ;; + cat\ /sys/block/sd?/sd*/ro) echo 0 ;; + cat\ /sys/block/sd?/sd*/size) echo "${SECTORS:-131072}" ;; dd\ of=*) dev=${cmd#dd of=/dev/}; cat > "$P/${dev%% *}" ;; head\ -c*) read -r _ _ n dev _ <<<"$cmd"; head -c "$n" "$P/${dev#/dev/}" | sha256sum ;; sync\;*) echo rebooted >> "$P/log" ;; @@ -238,12 +243,12 @@ esac PHONE chmod +x "$R/tools/phone-ssh" push() { # the boot_id answers, one per argument - rm -f "$P/reads" "$P/log" "$P"/sda_* + rm -f "$P/reads" "$P/log" "$P"/sd[a-z][0-9]* printf '%s\n' "$@" > "$P/script" om six push } 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" +[[ $(find "$P" -name 'sd[a-z][0-9]*' | 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 @@ -269,7 +274,7 @@ pass "push reports no SSH when the phone never answers after the reboot" # 16 KiB partitions: every image is too big, and nothing is written if SECTORS=32 push old new; then fail "push refuses an image larger than its partition"; fi grep -q 'larger than boot_b' "$t/log" || { cat "$t/log"; fail "push names the image too big for its partition"; } -[[ $(find "$P" -name 'sda_*' | wc -l) == 0 && ! -s $P/log ]] || fail "push writes nothing, and does not reboot, when an image does not fit" +[[ $(find "$P" -name 'sd[a-z][0-9]*' | wc -l) == 0 && ! -s $P/log ]] || fail "push writes nothing, and does not reboot, when an image does not fit" pass "push checks every image against its partition's size before writing any" # An AVB_FOOTERS image's partition must be exactly the descriptor's size cp "$R/device/six/device.conf" "$t/six.conf" @@ -277,9 +282,22 @@ printf 'AVB_FOOTERS="boot:0x4000000"\nAVB_ROLLBACK_INDEX=1\nAVB_SECURITY_PATCH=2 push old new || { cat "$t/log"; fail "push writes an AVB_FOOTERS image to a partition of its size"; } if SECTORS=262144 push old new; then fail "push refuses an AVB_FOOTERS size that is not the partition's"; fi grep -q 'AVB_FOOTERS pads boot to 67108864 bytes, but boot_b' "$t/log" || { cat "$t/log"; fail "push names the AVB size and the partition's"; } -[[ $(find "$P" -name 'sda_*' | wc -l) == 0 ]] || fail "push writes nothing when an AVB size is wrong" +[[ $(find "$P" -name 'sd[a-z][0-9]*' | wc -l) == 0 ]] || fail "push writes nothing when an AVB size is wrong" cp "$t/six.conf" "$R/device/six/device.conf" pass "push refuses an AVB_FOOTERS image whose partition is not the descriptor's size" +# A second LUN: push looks for slot B's images only on sda and the +# descriptor's UFS_RW_LUNS, the LUNs the init guards +if FAKE_LUN=sde push old new; then fail "push finds no image on a LUN the descriptor does not name"; fi +grep -q 'boot_b not found on the phone (looked on: /sys/block/sda/sda\*)' "$t/log" || { cat "$t/log"; fail "push names the LUNs it looked on"; } +echo 'UFS_RW_LUNS=sde' >> "$R/device/six/device.conf" +FAKE_LUN=sde push old new || { cat "$t/log"; fail "push writes images on a UFS_RW_LUNS LUN"; } +[[ $(find "$P" -name 'sde[0-9]*' | wc -l) == 3 ]] || fail "push writes the three images on sde" +grep -q 'boot_b (/dev/sde24) written and verified' "$t/log" || { cat "$t/log"; fail "push names the sde partition it wrote"; } +if FAKE_LUN=sde FAKE_DUP=1 push old new; then fail "push refuses a name on two partitions"; fi +grep -q 'boot_b is on more than one partition' "$t/log" || { cat "$t/log"; fail "push names the duplicate"; } +[[ $(find "$P" -name 'sd[a-z][0-9]*' | wc -l) == 0 ]] || fail "push writes nothing when a name is on two partitions" +cp "$t/six.conf" "$R/device/six/device.conf" +pass "push finds slot B's images on the descriptor's UFS_RW_LUNS, only there and sda, and refuses a duplicate name" # Building the images needs AOSP's mkbootimg and mkdtboimg, lz4 and a dtc; # without them only this part skips. From 58141ae93560624c9c5b34818c8e12721e6515ac Mon Sep 17 00:00:00 2001 From: Matt Barr Date: Tue, 10 Nov 2026 22:12:48 -0500 Subject: [PATCH 2/5] =?UTF-8?q?tools/mark-slot-b:=20the=20write=20a=20CONT?= =?UTF-8?q?RIBUTING=20=C2=A73.1=20entry=20would=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On a Qualcomm A/B phone slot B is marked successful in its GPT, which the storage guard can't police, so the write gets its own tool, run by hand once per install of slot B's boot image and never from a service. It is inert until a device has its §3.1 entry: no descriptor sets SLOT_B_MARK, and without it the tool refuses. The descriptor would name the write (SLOT_B_MARK: the LUN, the partition and the four LBAs). Over phone-ssh the tool first checks that the phone's first /proc/device-tree/compatible string is the descriptor's COMPATIBLE, before anything makes a node. It then reads both GPT copies through a private whole-disk node under /run, checks both headers, both entry arrays and their CRCs, finds the partition by name once, and refuses unless its sectors are the named four, the phone booted slot B, and B is active and not unbootable. It sets bit 54 and nothing else, recomputes the entry-array CRC and both header CRCs, and checks that only those four sectors change. The default is a dry run; --write asks for 'mark boot_b', re-reads the four sectors, writes the primary pair, reads it back, then the backup pair. No sector is piped over SSH into the disk: each lands in a file in the private directory, its SHA-256 is checked on the phone, and dd writes it from that file. tests/mark-slot-b runs it against a synthetic 4096-byte-sector GPT behind a fake phone-ssh, including another phone, a transfer cut short and each refusal. Co-Authored-By: Claude Opus 5.5 --- docs/RELEASES.md | 1 + tests/all | 2 +- tests/mark-slot-b | 120 ++++++++++++++++++++++ tools/mark-slot-b | 247 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 369 insertions(+), 1 deletion(-) create mode 100755 tests/mark-slot-b create mode 100755 tools/mark-slot-b diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 4b8e56cd3..9e7c88587 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -83,6 +83,7 @@ Bring-up tools (`*-stage`, `display-hz`, `usbc-deadman`) stay per plan: they pok | `BOOT_UPDATE_AUTO_WRITE` | (unset; defaults to `yes`) | Optional: `yes` or `no`. The installer records it as `AUTO_WRITE` in `/etc/omamobile/boot-update.conf`; `no` keeps an installed development root from writing slot B through package updates. It does not change the installer's own allowed writes | | `UFS_RW` | `boot_b … dtbo_b userdata` | The storage guard's allowlist, packed into the initramfs as `/etc/omamobile-ufs-rw`. Only slot B's boot images (`boot_b`, `init_boot_b`, `vendor_boot_b`, `vendor_kernel_boot_b`, `dtbo_b`) and `userdata` are accepted, `userdata` and every `BOOT_IMAGES` entry must be on it, and each name only once. The init fails closed: a missing, empty or malformed file, or any other name in it, leaves every partition read-only | | `UFS_RW_LUNS` | (unset) | Optional. The LUNs besides `sda` that hold `UFS_RW` partitions, as disk names `sdb` to `sdz`, packed as `/etc/omamobile-ufs-rw-luns`. The init guards each as it guards `sda`: the whole-disk node removed, the disk left writable, and only its `UFS_RW` partitions left writable. Absent (every descriptor so far), malformed, or with a closed allowlist: every LUN but `sda` stays read-only at the disk level. `omamobile push` looks for slot B's images on these LUNs too; boot-image writers on the phone (`omamobile-boot-update`) still look only on `sda` | +| `SLOT_B_MARK` | (unset) | Optional, Qualcomm A/B only, and set only once the device has its [CONTRIBUTING](../CONTRIBUTING.md) §3.1 entry (none does yet): that entry as `LUN PART P_HDR P_ENT B_ENT B_HDR`, the LUN, slot B's boot partition and the four LBAs (primary header, primary entry sector holding it, backup entry sector, backup header). Only `tools/mark-slot-b` reads it: by hand, once per install of slot B's boot image, it sets attribute bit 54 (successful) on that partition and rewrites the entry-array CRC and both header CRCs, and refuses a GPT whose sectors differ from these. Nothing else ever writes a partition table | | `INITRAMFS_INIT` | `/initramfs/init` | The init script. A leading `/` is relative to the repo root, so devices share one script. Any other value is relative to the descriptor | | `INITRAMFS_ROOT_LABEL` | (unset) | Optional: `omamobile-root` enables read-only label/header checks before ext4 repair or mounting; only `e2fsck -p` statuses 0 and 1 proceed. Omitted: existing root handling, including unlabelled hand-installed roots, is unchanged | | `INITRAMFS_ROOT_MODE` | (unset) | Optional: `persistent-development` or `ram-rescue`, requiring `INITRAMFS_ROOT_LABEL=omamobile-root`. The former skips growth and writes `/var/lib/omamobile/root-boot-pending` before handover; an installed, enabled SSH-ready helper must clear it after USB SSH is ready, or subsequent boots stay in RAM until the marker is cleared. The latter always stays in RAM. Omitted: existing handover behavior | diff --git a/tests/all b/tests/all index be2267308..2b511966f 100755 --- a/tests/all +++ b/tests/all @@ -53,7 +53,7 @@ python3 tests/boot-arch-label.py python3 tests/persistent-root-boot.py python3 tests/initramfs-root-policy.py python3 tests/bt-uart-gpio.py -for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-placeholders tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/build-packages-host-tools tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe tests/dipper-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/build-host-sync tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs tests/install-slot-descriptor; do +for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-placeholders tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/build-packages-host-tools tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/mark-slot-b tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe tests/dipper-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/build-host-sync tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs tests/install-slot-descriptor; do echo "# $t" "$t" done diff --git a/tests/mark-slot-b b/tests/mark-slot-b new file mode 100755 index 000000000..99f38fee0 --- /dev/null +++ b/tests/mark-slot-b @@ -0,0 +1,120 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# tools/mark-slot-b against a synthetic GPT with 4096-byte sectors behind a +# fake phone-ssh: the dry run writes nothing, --write sets bit 54 on boot_b +# alone and changes exactly the four named sectors, and every refusal holds. +set -euo pipefail +cd "$(dirname "$0")/.." +command -v sfdisk >/dev/null || { echo "ok - # skip: no sfdisk for tests/mark-slot-b"; exit 0; } +fail() { echo "not ok - $*"; exit 1; } +t=$(mktemp -d); trap 'rm -rf "$t"' EXIT +mkdir -p "$t/repo/tools" "$t/repo/device/probe" "$t/bin" "$t/run" "$t/sys/block/sde/queue" +cp tools/mark-slot-b "$t/repo/tools/" + +# The phone: a 16384-sector LUN, 40 partitions, boot_b the 39th (entry 38, in +# the second entry sector, as on the real sde), active with priority 2 and 7 tries. +truncate -s $((16384 * 4096)) "$t/disk.img" +{ echo "label: gpt"; echo "table-length: 96" + for i in $(seq 1 40); do + case $i in + 11) echo 'size=8, name=boot_a, attrs="GUID:48,49,50,52"' ;; + 39) echo 'size=8, name=boot_b, attrs="GUID:49,50,51,52,53"' ;; + *) echo "size=8, name=p$i" ;; + esac + done; } | sfdisk -q --sector-size 4096 "$t/disk.img" +echo 8:64 > "$t/sys/block/sde/dev" +echo $((16384 * 8)) > "$t/sys/block/sde/size" +echo 4096 > "$t/sys/block/sde/queue/logical_block_size" +echo "console=tty0 androidboot.slot_suffix=_b" > "$t/cmdline" +printf 'acme,probe\0qcom,sdm845\0' > "$t/compatible" +printf '#!/bin/sh\nln -s "%s/disk.img" "$1"\n' "$t" > "$t/bin/mknod" +cat > "$t/ssh" <> "$t/log" +# CUT=1 cuts every write's transfer short, as a dropped SSH link would +[[ -n \${CUT:-} && \$c == *'cat > '* ]] && { head -c 100 | PATH="$t/bin:\$PATH" sh -c "\$c"; exit; } +PATH="$t/bin:\$PATH" exec sh -c "\$c" +EOF +chmod +x "$t/ssh" "$t/bin/mknod" +export OMAMOBILE_MARK_SSH=$t/ssh + +# The four LBAs, read from the image as the §3.1 entry would record them +lbas=$(/usr/bin/python3 - "$t/disk.img" <<'EOF' +import struct, sys +f = open(sys.argv[1], "rb") +def hdr(lba): + f.seek(lba * 4096); h = f.read(92) + return struct.unpack_from(" "$t/repo/device/probe/device.conf"; } +conf "$lbas" +mark() { "$t/repo/tools/mark-slot-b" --device probe "$@"; } +sum() { sha256sum "$t/disk.img" | cut -d' ' -f1; } +before=$(sum); cp "$t/disk.img" "$t/disk.orig" + +mark > "$t/out" 2>&1 || { cat "$t/out"; fail "dry run fails"; } +grep -q "would set bit 54: 0x007e000000000000" "$t/out" || { cat "$t/out"; fail "dry run does not plan bit 54"; } +grep -q "dry run: nothing written" "$t/out" || fail "dry run does not say so" +[[ $(sum) == "$before" ]] || fail "dry run changed the disk" +grep -c 'dd of=' "$t/log" >/dev/null && fail "dry run ran a write" +[[ -z $(ls -A "$t/run") ]] || fail "the private node was left behind" + +echo "yes" | mark --write > "$t/out" 2>&1 && fail "--write takes a wrong confirmation" +grep -q "not confirmed; nothing written" "$t/out" || fail "wrong confirmation fails for the wrong reason" +[[ $(sum) == "$before" ]] || fail "a refused --write changed the disk" + +echo "mark boot_b" | CUT=1 mark --write > "$t/out" 2>&1 && fail "--write takes a sector cut short" +grep -q "arrived damaged on the phone; nothing written" "$t/out" || { cat "$t/out"; fail "a cut transfer fails for the wrong reason"; } +[[ $(sum) == "$before" ]] || fail "a cut transfer reached the disk" +[[ -z $(ls -A "$t/run") ]] || fail "the private directory was left behind after a cut transfer" +: > "$t/log" +echo "mark boot_b" | mark --write > "$t/out" 2>&1 || { cat "$t/out"; fail "--write fails"; } +grep 'dd of=' "$t/log" | grep -vc 'dd if="$d/sector" of="$n"' >/dev/null && fail "--write pipes into the disk instead of writing from the checked file" +grep -q "boot_b marked successful" "$t/out" || fail "--write does not report the mark" +[[ -z $(ls -A "$t/run") ]] || fail "the private node was left behind after --write" +changed=$(cmp -l "$t/disk.orig" "$t/disk.img" | awk '{print int(($1 - 1) / 4096)}' | sort -nu | xargs) +[[ $changed == "$lbas" ]] || fail "--write changed sectors '$changed', not '$lbas'" +sfdisk -q --sector-size 4096 -V "$t/disk.img" > "$t/verify" 2>&1 || { cat "$t/verify"; fail "sfdisk rejects the marked GPT"; } +sfdisk --sector-size 4096 -d "$t/disk.img" > "$t/dump" 2>&1 +grep 'name="boot_b"' "$t/dump" | grep -c 'GUID:49,50,51,52,53,54' >/dev/null || { grep boot "$t/dump"; fail "boot_b is not marked in the dump"; } +grep 'name="boot_a"' "$t/dump" | grep -c 'attrs="GUID:48,49,50,52"$' >/dev/null || fail "boot_a changed" +mark > "$t/out" 2>&1 || fail "the marked GPT fails its own check" +grep -q "already marked successful" "$t/out" || fail "a second run does not see the mark" + +cp "$t/disk.orig" "$t/disk.img" +: > "$t/log" +printf 'acme,other\0qcom,sdm845\0' > "$t/compatible" +mark > "$t/out" 2>&1 && fail "marks another phone" +grep -q "not device/probe" "$t/out" || { cat "$t/out"; fail "another phone refused for the wrong reason"; } +grep -c 'mknod' "$t/log" >/dev/null && fail "made a node before checking which phone answered" +printf 'acme,probe\0qcom,sdm845\0' > "$t/compatible" +echo "console=tty0 androidboot.slot_suffix=_a" > "$t/cmdline" +mark > "$t/out" 2>&1 && fail "marks from a boot of slot A" +grep -q "did not boot slot B" "$t/out" || fail "slot A boot refused for the wrong reason" +echo "console=tty0 androidboot.slot_suffix=_b" > "$t/cmdline" + +read -r a b c d <<<"$lbas"; conf "$a $((b + 1)) $c $d" +mark > "$t/out" 2>&1 && fail "takes a GPT the entry does not name" +grep -q "not the descriptor's" "$t/out" || fail "wrong LBAs refused for the wrong reason" +conf "$lbas" + +printf '\xff' | dd of="$t/disk.img" bs=1 seek=$((c * 4096 + 100)) count=1 conv=notrunc status=none +mark > "$t/out" 2>&1 && fail "takes differing entry arrays" +grep -q "entry arrays differ" "$t/out" || { cat "$t/out"; fail "differing arrays refused for the wrong reason"; } + +cp "$t/disk.orig" "$t/disk.img" +sfdisk -q --sector-size 4096 --part-attrs "$t/disk.img" 39 "GUID:49,51,52,53" +mark > "$t/out" 2>&1 && fail "marks an inactive boot_b" +grep -q "not active" "$t/out" || fail "inactive boot_b refused for the wrong reason" + +printf 'CODENAME=probe\n' > "$t/repo/device/probe/device.conf" +mark > "$t/out" 2>&1 && fail "runs without a §3.1 entry" +grep -q "no CONTRIBUTING §3.1 entry" "$t/out" || fail "missing entry refused for the wrong reason" + +echo "ok - mark-slot-b: phone check, dry run, confirmation, checked sector files, bit 54 on boot_b in exactly LBA ${lbas// /,}, and its refusals" diff --git a/tools/mark-slot-b b/tools/mark-slot-b new file mode 100755 index 000000000..e7809f077 --- /dev/null +++ b/tools/mark-slot-b @@ -0,0 +1,247 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""Mark slot B successful in a Qualcomm GPT: the CONTRIBUTING §3.1 write. + + tools/mark-slot-b --device CODENAME [--write] + +For a device whose descriptor has SLOT_B_MARK="LUN PART P_HDR P_ENT B_ENT B_HDR" +(its §3.1 entry: the LUN, the slot-B boot partition, and the four LBAs: +primary header, primary entry sector holding PART, backup entry sector, +backup header). Over tools/phone-ssh it reads both GPT copies of the LUN +through a private block node (the init removes the whole-disk node), checks +both headers and both entry arrays (signature, sizes, CRCs, copies equal), +finds PART by name exactly once, and refuses unless the phone's first +/proc/device-tree/compatible string is the descriptor's COMPATIBLE (checked +before any node is made), its sectors are the four +LBAs above, the phone booted slot B (androidboot.slot_suffix=_b) and B is +active and not unbootable. Then it sets attribute bit 54 (successful) on PART +and nothing else, recomputes the entry-array CRC and both header CRCs, and +checks that exactly the four sectors change. + +Without --write it only prints the plan (read-only). With --write, after the +human types the confirmation on stdin, it re-reads the four sectors (they +must not have changed since the check), then writes the primary entry sector and header, +reads them back by SHA-256, then the backup pair, and removes the node. Each +sector is copied to a file in the private directory and its SHA-256 checked on +the phone before dd writes it from that file. + +By hand, once per install of slot B's boot image, never from a service or at +boot (AGENTS.md §1). The rollback for a corrupted GPT is the device's NOTES. +""" +import argparse +import hashlib +import os +import re +import struct +import subprocess +import sys +import zlib + +SUCCESSFUL, ACTIVE, UNBOOTABLE = 54, 50, 55 +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +# The phone side: a private node for the LUN, removed on exit whatever happens. +# $1 is the LUN (sd[b-z]); every command runs as `phone-ssh sh -c ... `. +NODE = ('set -e; l=$1; shift; d=/run/omamobile-mark-slot-b; ' + 'rm -rf "$d"; mkdir -m 700 "$d"; trap \'rm -rf "$d"\' EXIT; ' + 'IFS=: read -r ma mi < /sys/block/$l/dev; mknod "$d/$l" b "$ma" "$mi"; n="$d/$l"; ') + + +def die(msg): + print("mark-slot-b: " + msg, file=sys.stderr) + sys.exit(1) + + +def descriptor(path): + """KEY="value" or KEY=value lines, never sourced (as tools/bundle-manifest).""" + d = {} + with open(path) as f: + for n, line in enumerate(f, 1): + line = line.rstrip("\n") + if re.match(r"^\s*(#.*)?$", line): + continue + m = re.match(r'^([A-Z][A-Z0-9_]*)="([^"$`\\]*)"$', line) or \ + re.match(r"^([A-Z][A-Z0-9_]*)=([^\"$`\\'\s]*)$", line) + if not m: + die("%s:%d: not a KEY=value line" % (path, n)) + d[m.group(1)] = m.group(2) + return d + + +class Phone: + def __init__(self, ssh, lun): + self.ssh, self.lun = ssh, lun + + def run(self, script, data=None): + r = subprocess.run([self.ssh, "sh -c '%s' sh %s" % ((NODE + script).replace("'", "'\\''"), self.lun)], + input=data, stdout=subprocess.PIPE) + if r.returncode: + die("phone command failed (exit %d)" % r.returncode) + return r.stdout + + def text(self, cmd): + r = subprocess.run([self.ssh, cmd], stdout=subprocess.PIPE, text=True) + if r.returncode: + die("cannot run on the phone: " + cmd) + return r.stdout.strip() + + def read(self, lba, count, ss): + out = self.run('dd if="$n" bs=%d skip=%d count=%d 2>/dev/null' % (ss, lba, count)) + if len(out) != count * ss: + die("short read at LBA %d (%d bytes)" % (lba, len(out))) + return out + + def write(self, lba, data, ss): + # Never pipe SSH into the disk: a transfer cut short would write part of a sector. + # The sector lands in a file in the private directory first, and only a file + # with the expected SHA-256 is written. + self.run('cat > "$d/sector"; ' + '[ "$(sha256sum < "$d/sector" | cut -d" " -f1)" = %s ] || ' + '{ echo "mark-slot-b: LBA %d arrived damaged on the phone; nothing written" >&2; exit 3; }; ' + 'dd if="$d/sector" of="$n" bs=%d seek=%d count=1 conv=notrunc,fsync 2>/dev/null' + % (hashlib.sha256(data).hexdigest(), lba, ss, lba), data) + + +def header(raw, lba): + """Check a GPT header sector; return its fields.""" + if raw[:8] != b"EFI PART": + die("no GPT header at LBA %d" % lba) + size, crc = struct.unpack_from("> 48 & 3, "yes" if a >> ACTIVE & 1 else "no", a >> 51 & 7, + "yes" if a >> SUCCESSFUL & 1 else "no", "yes" if a >> UNBOOTABLE & 1 else "no") + + +def plan(prim_hdr, prim_ent, back_hdr, back_ent, part, ss, want): + """Check both copies and build the four new sectors: {lba: (old, new)}.""" + p, b = header(prim_hdr, want[0]), header(back_hdr, want[3]) + if p["alt"] != want[3] or b["alt"] != want[0] or (p["num"], p["ecrc"]) != (b["num"], b["ecrc"]): + die("the primary and backup headers do not describe each other") + arr = p["num"] * 128 + if prim_ent[:arr] != back_ent[:arr]: + die("the primary and backup entry arrays differ") + if zlib.crc32(prim_ent[:arr]) != p["ecrc"]: + die("the entry-array CRC does not match") + hits = [i for i in range(p["num"]) + if prim_ent[i * 128 + 56:i * 128 + 128].decode("utf-16-le", "replace").rstrip("\0") == part] + if len(hits) != 1: + die("%s found %d times in the GPT" % (part, len(hits))) + i = hits[0] + off = i * 128 + sec = off // ss + if (p["ent"] + sec, b["ent"] + sec) != (want[1], want[2]): + die("%s (entry %d) is at LBA %d and %d, not the descriptor's %d and %d: the GPT is not the one " + "the §3.1 entry names; refusing" % (part, i, p["ent"] + sec, b["ent"] + sec, want[1], want[2])) + attrs, = struct.unpack_from("> SUCCESSFUL & 1: + print("%s is already marked successful; nothing to write" % part) + return + if not attrs >> ACTIVE & 1 or attrs >> UNBOOTABLE & 1: + die("%s is not active, or is unbootable: run fastboot --set-active=b first" % part) + new = attrs | 1 << SUCCESSFUL + print("would set bit %d: 0x%016x (%s)" % (SUCCESSFUL, new, bits(new))) + print("entry-array CRC 0x%08x -> 0x%08x; header CRCs recomputed" % (old_ecrc, ecrc)) + print("sectors to write (%d bytes each): LBA %s" % (ss, ", ".join(str(x) for x in want))) + if not a.write: + print("dry run: nothing written (add --write)") + return + sys.stderr.write("Type 'mark %s' to write these four sectors: " % part) + sys.stderr.flush() + if sys.stdin.readline().strip() != "mark " + part: + die("not confirmed; nothing written") + if any(ph.read(lba, 1, ss) != out[lba][0] for lba in want): + die("the GPT changed since it was checked; nothing written") + for pair in ((want[1], want[0]), (want[2], want[3])): + for lba in pair: + ph.write(lba, out[lba][1], ss) + for lba in pair: + if hashlib.sha256(ph.read(lba, 1, ss)).digest() != hashlib.sha256(out[lba][1]).digest(): + die("read-back of LBA %d does not match: restore from the NOTES rollback copies" % lba) + print("LBA %d and %d written and read back" % pair) + print("%s marked successful" % part) + + +if __name__ == "__main__": + main() From a3d248aa928257984e7a8c590844cd4377599fc2 Mon Sep 17 00:00:00 2001 From: Matt Barr Date: Tue, 10 Nov 2026 22:12:48 -0500 Subject: [PATCH 3/5] device/enchilada: NOTES on sde's GPT, what it holds, and the EDL case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit What a §3.1 entry for boot_b needs from this phone, read-only: sde's geometry and boot_b's place in both GPT copies, the 72 partitions sde holds by PARTNAME (abl_a and abl_b among them, xbl not), and so what a whole-disk sde node reaches. With abl on sde, a phone left with no valid table there has no rescue and no fastboot, only Qualcomm EDL; the notes say so. The attribute bits are cited from Qualcomm's ABL, and only a getvar after a write would prove bit 54 on OnePlus's build of it. Co-Authored-By: Claude Opus 5.5 --- device/enchilada/NOTES.md | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/device/enchilada/NOTES.md b/device/enchilada/NOTES.md index a3b456784..35a932a09 100644 --- a/device/enchilada/NOTES.md +++ b/device/enchilada/NOTES.md @@ -101,6 +101,63 @@ tree and goes away if stock is restored. 20 s and panics, and `panic=10` reboots the phone. ABL then starts the active slot, A, which is the rescue. +## 2026-11-11: `sde`'s GPT, and what marking `boot_b` would reach + +Groundwork for this phone's [CONTRIBUTING](../../CONTRIBUTING.md) §3.1 +entry, which the maintainer writes. `tools/mark-slot-b` exists, but the +descriptor sets neither `UFS_RW_LUNS` nor `SLOT_B_MARK` yet, so it refuses +this phone. Nothing here has written to the phone. + +**`sde`'s GPT**, read on 2026-11-09 from the slot-A rescue (`sfdisk --dump` +and the raw sectors of both copies, read-only): + +- 4096-byte sectors, 786432 of them (last LBA 786431); 96 entries of 128 + bytes. The primary header is at LBA 1 with entries in LBA 2-4; the backup + entries are in LBA 786427-786429, with its header at 786431. +- `boot_b` is `sde39`, entry 38, at byte 768 of LBA 3 and of LBA 786428. So + setting its bit 54 changes LBA 1, 3, 786428 and 786431, not the + first entry sector. +- Before any install its attributes read `0x003a000000000000`: priority 2, + not active, 7 tries, not successful, not unbootable. The CRCs then were + entries `0x0a9fe94a`, primary header `0xb62ccea8`, backup header + `0x619c2dc8`. `set-active b` rewrites all three, so the tool checks the + live values, not these. + +**What `sde` holds** (`PARTNAME`, 72 partitions), so what a whole-disk node +for it reaches: + +- for each slot, `_a` and `_b`: `aop`, `tz`, `hyp`, `modem`, `bluetooth`, + `mdtpsecapp`, `mdtp`, `abl`, `dsp`, `keymaster`, `boot`, `cmnlib`, + `cmnlib64`, `devcfg`, `qupfw`, `vendor`, `vbmeta`, `dtbo`, `storsec`, + `LOGO`, `fw_4j1ed`, `fw_4u1ea`, `fw_ufs3` to `fw_ufs8`; +- unslotted: `minidump`, `aging`, `op1`, `sec`, `devinfo`, `dip`, `apdp`, + `msadp`, `spunvm`, `splash`, `limits`, `toolsfv`, `logfs`, `sti`, + `logdump`, `ImageFv`. + +`xbl` and `xbl_config` are not on `sde`. Which LUN holds them has not been +read yet. + +**If both GPT copies on `sde` became invalid**, the phone could not load +`abl` from either slot, because `abl_a` and `abl_b` are on `sde`. Then there +is no fastboot, no `fastboot boot` and no slot-A rescue. The buttons would +not help either. The only way in would be Qualcomm's emergency download mode +(EDL, USB `05c6:9008`), with a programmer that matches this phone, to write +back the two read-only copies. A copy of `sde`'s LBA 0-5 and 786426-786431 +(both GPT copies whole) and its `sfdisk --dump` were taken read-only on +2026-11-09, and their SHA-256 matched the phone's. They are kept off the +repository because they hold the disk's GUIDs. `tools/mark-slot-b` writes +the primary pair and reads it back before it touches the backup pair, so a +valid copy exists at every moment. The owner's acceptance on #432 has to +cover the EDL case in so many words before anything writes this table. + +**The bit layout** is Qualcomm's ABL (`QcomModulePkg/Include/Library/PartitionTableUpdate.h` +in the `abl/tianocore/edk2` tree): `PART_ATT_PRIORITY_BIT` 48 (two bits), +`PART_ATT_ACTIVE_BIT` 50, `PART_ATT_MAX_RETRY_CNT_BIT` 51 (three bits), +`PART_ATT_SUCCESS_BIT` 54, `PART_ATT_UNBOOTABLE_BIT` 55. This phone's `abl` +is OnePlus's build of it, and bits 54 and 55 have never been seen set +here. Only `fastboot getvar slot-successful:b` reading `yes` after a write +would prove that bit 54 means "successful" to this bootloader. + ## 2026-11-08: the LUN-aware guard, booted RAM-only `fastboot boot` of an initramfs built from commit 2331a85 (the From 86fba70f4f13187b784b8b0411577a412500ab06 Mon Sep 17 00:00:00 2001 From: Matt Barr Date: Wed, 11 Nov 2026 19:51:46 -0500 Subject: [PATCH 4/5] tools/mark-slot-b: recover an interrupted --write, test every check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A --write cut off after its first, second or third sector left a table that every later run refused, and the only way on was a dd by hand. The tool now saves the four old and new sectors on the laptop before it writes (off the repository, as they hold the disk's GUID, with a SHA256SUMS as CONTRIBUTING §3.3 asks of backups). The next run recognises a half-written table and offers to finish the write or put the old sectors back, under the same typed confirmation. It also refuses early when the LUN is not in UFS_RW_LUNS or is read-only, and reads with iflag=direct so the read-back sees the disk, not the page cache. The test passed with most checks removed. It now has a case for each: a table changed between check and write, a write that does not land, each bad CRC, headers that do not describe each other, the last LBA, two boot_b, bit 55, the internal byte check, the dd order, O_DIRECT reads and a damaged saved sector, plus finish and restore after a cut at each sector. Each check was removed in turn and the test failed every time. Co-Authored-By: Claude Opus 5.5 --- device/enchilada/NOTES.md | 5 +- tests/mark-slot-b | 136 +++++++++++++++++++++++++++++++++++--- tools/mark-slot-b | 135 ++++++++++++++++++++++++++++++------- 3 files changed, 241 insertions(+), 35 deletions(-) diff --git a/device/enchilada/NOTES.md b/device/enchilada/NOTES.md index 35a932a09..8cfb75eb3 100644 --- a/device/enchilada/NOTES.md +++ b/device/enchilada/NOTES.md @@ -147,7 +147,10 @@ back the two read-only copies. A copy of `sde`'s LBA 0-5 and 786426-786431 2026-11-09, and their SHA-256 matched the phone's. They are kept off the repository because they hold the disk's GUIDs. `tools/mark-slot-b` writes the primary pair and reads it back before it touches the backup pair, so a -valid copy exists at every moment. The owner's acceptance on #432 has to +valid copy exists at every moment. It saves the four old sectors on the +laptop first (`~/android/reference/enchilada/mark-slot-b-sde/`), so after a +cut cable the next run sees the half-written table and offers to finish the +write or put the old sectors back. The owner's acceptance on #432 has to cover the EDL case in so many words before anything writes this table. **The bit layout** is Qualcomm's ABL (`QcomModulePkg/Include/Library/PartitionTableUpdate.h` diff --git a/tests/mark-slot-b b/tests/mark-slot-b index 99f38fee0..0058f7695 100755 --- a/tests/mark-slot-b +++ b/tests/mark-slot-b @@ -2,7 +2,8 @@ # SPDX-License-Identifier: MIT # tools/mark-slot-b against a synthetic GPT with 4096-byte sectors behind a # fake phone-ssh: the dry run writes nothing, --write sets bit 54 on boot_b -# alone and changes exactly the four named sectors, and every refusal holds. +# alone and changes exactly the four named sectors in order, a --write cut off +# at any sector can be finished or undone, and every refusal holds. set -euo pipefail cd "$(dirname "$0")/.." command -v sfdisk >/dev/null || { echo "ok - # skip: no sfdisk for tests/mark-slot-b"; exit 0; } @@ -25,19 +26,34 @@ truncate -s $((16384 * 4096)) "$t/disk.img" echo 8:64 > "$t/sys/block/sde/dev" echo $((16384 * 8)) > "$t/sys/block/sde/size" echo 4096 > "$t/sys/block/sde/queue/logical_block_size" +echo 0 > "$t/sys/block/sde/ro" echo "console=tty0 androidboot.slot_suffix=_b" > "$t/cmdline" printf 'acme,probe\0qcom,sdm845\0' > "$t/compatible" printf '#!/bin/sh\nln -s "%s/disk.img" "$1"\n' "$t" > "$t/bin/mknod" -cat > "$t/ssh" < "$t/ssh" <<'EOF' #!/bin/bash -c=\$1; c=\${c//\/proc\/cmdline/$t/cmdline}; c=\${c//\/proc\/device-tree\/compatible/$t/compatible}; c=\${c//\/sys\/block\//$t/sys/block/}; c=\${c//\/run\//$t/run/} -printf '%s\n' "\$c" >> "$t/log" +T=$MARK_TEST +c=$1; c=${c//\/proc\/cmdline/$T/cmdline}; c=${c//\/proc\/device-tree\/compatible/$T/compatible}; c=${c//\/sys\/block\//$T/sys/block/}; c=${c//\/run\//$T/run/} +printf '%s\n' "$c" >> "$T/log" +count() { local n; n=$(( $(cat "$T/$1" 2>/dev/null || echo 0) + 1 )); echo $n > "$T/$1"; echo $n; } +if [[ $c == *'of="$n"'* ]]; then + w=$(count writes) + # FAIL_AT=N: the link drops before the Nth sector is written + [[ $w == "${FAIL_AT:-}" ]] && exit 255 + # DROP_AT=N: the Nth write reports success but never reaches the disk + [[ $w == "${DROP_AT:-}" ]] && { cat > /dev/null; exit 0; } +elif [[ $c == *'if="$n"'* ]]; then + # CHANGE_AT=N: something else writes byte CHANGE_OFF just before the Nth read + [[ $(count reads) == "${CHANGE_AT:-}" ]] && printf Z | dd of="$T/disk.img" bs=1 seek="$CHANGE_OFF" count=1 conv=notrunc status=none +fi # CUT=1 cuts every write's transfer short, as a dropped SSH link would -[[ -n \${CUT:-} && \$c == *'cat > '* ]] && { head -c 100 | PATH="$t/bin:\$PATH" sh -c "\$c"; exit; } -PATH="$t/bin:\$PATH" exec sh -c "\$c" +[[ -n ${CUT:-} && $c == *'cat > '* ]] && { head -c 100 | PATH="$T/bin:$PATH" sh -c "$c"; exit; } +PATH="$T/bin:$PATH" exec sh -c "$c" EOF chmod +x "$t/ssh" "$t/bin/mknod" -export OMAMOBILE_MARK_SSH=$t/ssh +export OMAMOBILE_MARK_SSH=$t/ssh MARK_TEST=$t HOME=$t/home +saved=$t/home/android/reference/probe/mark-slot-b-sde +fresh() { rm -f "$t/reads" "$t/writes"; : > "$t/log"; } # The four LBAs, read from the image as the §3.1 entry would record them lbas=$(/usr/bin/python3 - "$t/disk.img" <<'EOF' @@ -52,7 +68,8 @@ sec = 38 * 128 // 4096 print(1, pent + sec, bent + sec, alt) EOF ) -conf() { printf 'CODENAME=probe\nCOMPATIBLE="acme,probe"\nSLOT_B_MARK="sde boot_b %s"\n' "$1" > "$t/repo/device/probe/device.conf"; } +read -r a b c d <<<"$lbas" +conf() { printf 'CODENAME=probe\nCOMPATIBLE="acme,probe"\nUFS_RW_LUNS=%s\nSLOT_B_MARK="sde boot_b %s"\n' "${2-sde}" "$1" > "$t/repo/device/probe/device.conf"; } conf "$lbas" mark() { "$t/repo/tools/mark-slot-b" --device probe "$@"; } sum() { sha256sum "$t/disk.img" | cut -d' ' -f1; } @@ -73,10 +90,15 @@ echo "mark boot_b" | CUT=1 mark --write > "$t/out" 2>&1 && fail "--write takes a grep -q "arrived damaged on the phone; nothing written" "$t/out" || { cat "$t/out"; fail "a cut transfer fails for the wrong reason"; } [[ $(sum) == "$before" ]] || fail "a cut transfer reached the disk" [[ -z $(ls -A "$t/run") ]] || fail "the private directory was left behind after a cut transfer" -: > "$t/log" +fresh echo "mark boot_b" | mark --write > "$t/out" 2>&1 || { cat "$t/out"; fail "--write fails"; } grep 'dd of=' "$t/log" | grep -vc 'dd if="$d/sector" of="$n"' >/dev/null && fail "--write pipes into the disk instead of writing from the checked file" grep -q "boot_b marked successful" "$t/out" || fail "--write does not report the mark" +order=$(grep -o 'of="$n" bs=4096 seek=[0-9]*' "$t/log" | sed 's/.*seek=//' | xargs) +grep 'if="$n"' "$t/log" | grep -vc 'iflag=direct' >/dev/null && fail "a disk read goes through the page cache, not O_DIRECT" +[[ $order == "$b $a $c $d" ]] || fail "--write wrote LBA '$order', not primary entries, primary header, backup entries, backup header ($b $a $c $d)" +[[ $(stat -c %a "$saved") == 700 && $(ls "$saved" | wc -l) == 9 ]] && (cd "$saved" && sha256sum --quiet -c SHA256SUMS) || fail "the old and new sectors were not saved privately" +cmp -s "$saved/$b.old" <(dd if="$t/disk.orig" bs=4096 skip="$b" count=1 status=none) || fail "the saved old sector is not the disk's" [[ -z $(ls -A "$t/run") ]] || fail "the private node was left behind after --write" changed=$(cmp -l "$t/disk.orig" "$t/disk.img" | awk '{print int(($1 - 1) / 4096)}' | sort -nu | xargs) [[ $changed == "$lbas" ]] || fail "--write changed sectors '$changed', not '$lbas'" @@ -86,6 +108,63 @@ grep 'name="boot_b"' "$t/dump" | grep -c 'GUID:49,50,51,52,53,54' >/dev/null || grep 'name="boot_a"' "$t/dump" | grep -c 'attrs="GUID:48,49,50,52"$' >/dev/null || fail "boot_a changed" mark > "$t/out" 2>&1 || fail "the marked GPT fails its own check" grep -q "already marked successful" "$t/out" || fail "a second run does not see the mark" +cp "$t/disk.img" "$t/disk.marked" + +# A --write cut off before the 2nd, 3rd or 4th sector: the next run sees it, +# and finishes the write or puts the old sectors back. +for at in 2 3 4; do + for how in mark restore; do + cp "$t/disk.orig" "$t/disk.img"; fresh + echo "mark boot_b" | FAIL_AT=$at mark --write > "$t/out" 2>&1 && fail "--write cut off at sector $at says it worked" + fresh + mark > "$t/out" 2>&1 || { cat "$t/out"; fail "cut off at sector $at: the next run refuses the table"; } + grep -q "an interrupted --write left" "$t/out" || { cat "$t/out"; fail "cut off at sector $at: not recognised"; } + grep -c 'of="$n"' "$t/log" >/dev/null && fail "cut off at sector $at: the dry run wrote" + echo "nothing" | mark --write > "$t/out" 2>&1 && fail "cut off at sector $at: recovery takes a wrong confirmation" + echo "$how boot_b" | mark --write > "$t/out" 2>&1 || { cat "$t/out"; fail "cut off at sector $at: $how fails"; } + want=$t/disk.marked; [[ $how == restore ]] && want=$t/disk.orig + cmp -s "$t/disk.img" "$want" || fail "cut off at sector $at: $how did not give the $how table" + done +done + +# A saved sector that no longer matches its checksum is never written back +cp "$t/disk.orig" "$t/disk.img"; fresh +echo "mark boot_b" | FAIL_AT=2 mark --write > /dev/null 2>&1 && fail "--write cut off at sector 2 says it worked" +cp "$saved/$a.old" "$t/keep"; printf X | dd of="$saved/$a.old" bs=1 seek=500 count=1 conv=notrunc status=none +echo "restore boot_b" | mark --write > "$t/out" 2>&1 && fail "restores from a damaged saved sector" +grep -q "do not match their SHA256SUMS" "$t/out" || { cat "$t/out"; fail "a damaged saved sector refused for the wrong reason"; } +cp "$t/keep" "$saved/$a.old" + +# A write that reports success but never lands is caught by the read-back +cp "$t/disk.orig" "$t/disk.img"; fresh +echo "mark boot_b" | DROP_AT=1 mark --write > "$t/out" 2>&1 && fail "takes a write that did not land" +grep -q "read-back of LBA $b does not match" "$t/out" || { cat "$t/out"; fail "a lost write fails for the wrong reason"; } +echo "restore boot_b" | mark --write > "$t/out" 2>&1 || { cat "$t/out"; fail "cannot restore after a lost write"; } +cmp -s "$t/disk.img" "$t/disk.orig" || fail "restore after a lost write did not give the old table" + +# A table changed between the check and the write +fresh; mark > /dev/null 2>&1; n=$(grep -c 'if="$n"' "$t/log"); fresh +echo "mark boot_b" | CHANGE_AT=$((n + 1)) CHANGE_OFF=$((b * 4096 + 3000)) mark --write > "$t/out" 2>&1 && fail "writes a table changed after its check" +grep -q "the GPT changed since it was checked; nothing written" "$t/out" || { cat "$t/out"; fail "a changed table refused for the wrong reason"; } +grep -c 'of="$n"' "$t/log" >/dev/null && fail "wrote to a table changed after its check" + +# The internal check that only CRC and attribute bytes change +/usr/bin/python3 - "$t/repo/tools/mark-slot-b" "$t/disk.orig" $lbas > "$t/out" 2>&1 <<'EOF' && fail "the internal check lets other bytes change" +import importlib.machinery, importlib.util, sys +ld = importlib.machinery.SourceFileLoader("msb", sys.argv[1]) +m = importlib.util.module_from_spec(importlib.util.spec_from_loader("msb", ld)); ld.exec_module(m) +want = [int(x) for x in sys.argv[3:]] +f = open(sys.argv[2], "rb") +def rd(lba, n=1): + f.seek(lba * 4096); return f.read(n * 4096) +ph, bh = rd(want[0]), rd(want[3]) +p, b = m.header(ph, want[0]), m.header(bh, want[3]) +n = -(-p["num"] * 128 // 4096) +real = m.with_crcs +m.with_crcs = lambda raw, ecrc: real(raw, ecrc)[:200] + b"X" + real(raw, ecrc)[201:] +m.plan(ph, rd(p["ent"], n), bh, rd(b["ent"], n), "boot_b", 4096, want) +EOF +grep -q "internal: LBA 1 would change outside" "$t/out" || { cat "$t/out"; fail "the internal check fails for the wrong reason"; } cp "$t/disk.orig" "$t/disk.img" : > "$t/log" @@ -99,7 +178,20 @@ mark > "$t/out" 2>&1 && fail "marks from a boot of slot A" grep -q "did not boot slot B" "$t/out" || fail "slot A boot refused for the wrong reason" echo "console=tty0 androidboot.slot_suffix=_b" > "$t/cmdline" -read -r a b c d <<<"$lbas"; conf "$a $((b + 1)) $c $d" +conf "$lbas" sdf +mark > "$t/out" 2>&1 && fail "takes a LUN outside UFS_RW_LUNS" +grep -q "not in device/probe's UFS_RW_LUNS" "$t/out" || fail "LUN outside UFS_RW_LUNS refused for the wrong reason" +conf "$lbas" +echo 1 > "$t/sys/block/sde/ro" +mark > "$t/out" 2>&1 && fail "takes a read-only LUN" +grep -q "sde is read-only" "$t/out" || fail "read-only LUN refused for the wrong reason" +echo 0 > "$t/sys/block/sde/ro" +echo $((16385 * 8)) > "$t/sys/block/sde/size" +mark > "$t/out" 2>&1 && fail "takes a LUN whose last LBA is not the entry's" +grep -q "the §3.1 entry says last LBA $d" "$t/out" || fail "wrong last LBA refused for the wrong reason" +echo $((16384 * 8)) > "$t/sys/block/sde/size" + +conf "$a $((b + 1)) $c $d" mark > "$t/out" 2>&1 && fail "takes a GPT the entry does not name" grep -q "not the descriptor's" "$t/out" || fail "wrong LBAs refused for the wrong reason" conf "$lbas" @@ -108,7 +200,29 @@ printf '\xff' | dd of="$t/disk.img" bs=1 seek=$((c * 4096 + 100)) count=1 conv=n mark > "$t/out" 2>&1 && fail "takes differing entry arrays" grep -q "entry arrays differ" "$t/out" || { cat "$t/out"; fail "differing arrays refused for the wrong reason"; } +# Headers that do not point at each other, and a bad CRC in each place +hdr_set() { /usr/bin/python3 - "$t/disk.img" "$@" <<'EOF' +import struct, sys, zlib +lba, off, val = map(int, sys.argv[2:5]) +with open(sys.argv[1], "r+b") as f: + f.seek(lba * 4096); h = bytearray(f.read(92)) + struct.pack_into(" "$t/out" 2>&1 && fail "takes $2"; grep -q "$1" "$t/out" || { cat "$t/out"; fail "$2 refused for the wrong reason"; }; cp "$t/disk.orig" "$t/disk.img"; } cp "$t/disk.orig" "$t/disk.img" +hdr_set "$a" 32 $((d - 1)); refuses "headers do not describe each other" "a primary header pointing elsewhere" +flip $((a * 4096 + 60)); refuses "LBA $a: header CRC does not match" "a bad primary header CRC" +flip $((d * 4096 + 60)); refuses "LBA $d: header CRC does not match" "a bad backup header CRC" +flip $((b * 4096 + 100)); flip $((c * 4096 + 100)); refuses "entry-array CRC does not match" "a bad entry-array CRC" +sfdisk -q --sector-size 4096 --part-label "$t/disk.img" 40 boot_b +refuses "boot_b found 2 times" "two partitions named boot_b" +sfdisk -q --sector-size 4096 --part-attrs "$t/disk.img" 39 "GUID:49,50,51,52,53,55" +refuses "is not active, or is unbootable" "an unbootable boot_b" + sfdisk -q --sector-size 4096 --part-attrs "$t/disk.img" 39 "GUID:49,51,52,53" mark > "$t/out" 2>&1 && fail "marks an inactive boot_b" grep -q "not active" "$t/out" || fail "inactive boot_b refused for the wrong reason" @@ -117,4 +231,4 @@ printf 'CODENAME=probe\n' > "$t/repo/device/probe/device.conf" mark > "$t/out" 2>&1 && fail "runs without a §3.1 entry" grep -q "no CONTRIBUTING §3.1 entry" "$t/out" || fail "missing entry refused for the wrong reason" -echo "ok - mark-slot-b: phone check, dry run, confirmation, checked sector files, bit 54 on boot_b in exactly LBA ${lbas// /,}, and its refusals" +echo "ok - mark-slot-b: phone check, dry run, confirmation, checked sector files, bit 54 on boot_b in exactly LBA ${lbas// /,} in order, saved sectors, finish or restore after a cut at each sector, and its refusals" diff --git a/tools/mark-slot-b b/tools/mark-slot-b index e7809f077..92fa46f7f 100755 --- a/tools/mark-slot-b +++ b/tools/mark-slot-b @@ -12,18 +12,26 @@ through a private block node (the init removes the whole-disk node), checks both headers and both entry arrays (signature, sizes, CRCs, copies equal), finds PART by name exactly once, and refuses unless the phone's first /proc/device-tree/compatible string is the descriptor's COMPATIBLE (checked -before any node is made), its sectors are the four -LBAs above, the phone booted slot B (androidboot.slot_suffix=_b) and B is -active and not unbootable. Then it sets attribute bit 54 (successful) on PART +before any node is made), the LUN is in the descriptor's UFS_RW_LUNS and its +/sys/block//ro reads 0, its last LBA is the fourth above, the phone +booted slot B (androidboot.slot_suffix=_b) and B is active and not +unbootable. Then it sets attribute bit 54 (successful) on PART and nothing else, recomputes the entry-array CRC and both header CRCs, and checks that exactly the four sectors change. Without --write it only prints the plan (read-only). With --write, after the -human types the confirmation on stdin, it re-reads the four sectors (they -must not have changed since the check), then writes the primary entry sector and header, -reads them back by SHA-256, then the backup pair, and removes the node. Each -sector is copied to a file in the private directory and its SHA-256 checked on -the phone before dd writes it from that file. +human types the confirmation on stdin, it saves the four old and the four new +sectors on the laptop with a SHA256SUMS (~/android/reference// +mark-slot-b-/, off the repository: they hold the disk's GUID), re-reads the four sectors (they must +not have changed since the check), then writes the primary entry sector and +header, reads them back from the disk (O_DIRECT), then the backup pair, and +removes the node. Each sector is copied to a file in the private directory and +its SHA-256 checked on the phone before dd writes it from that file. + +A --write cut off part way leaves each of the four sectors old or new. The next +run recognises that against the saved sectors and offers, under the typed +confirmation, to finish the write ('mark PART') or put the old sectors back +('restore PART'), in the same order and with the same checks. By hand, once per install of slot B's boot image, never from a service or at boot (AGENTS.md §1). The rollback for a corrupted GPT is the device's NOTES. @@ -32,6 +40,7 @@ import argparse import hashlib import os import re +import shutil import struct import subprocess import sys @@ -85,7 +94,8 @@ class Phone: return r.stdout.strip() def read(self, lba, count, ss): - out = self.run('dd if="$n" bs=%d skip=%d count=%d 2>/dev/null' % (ss, lba, count)) + # iflag=direct: the disk, not the page cache, so a read-back sees what landed + out = self.run('dd if="$n" bs=%d skip=%d count=%d iflag=direct 2>/dev/null' % (ss, lba, count)) if len(out) != count * ss: die("short read at LBA %d (%d bytes)" % (lba, len(out))) return out @@ -173,6 +183,73 @@ def plan(prim_hdr, prim_ent, back_hdr, back_ent, part, ss, want): return i, attrs, ecrc, p["ecrc"], out +def saved_dir(device, lun): + return os.path.join(os.path.expanduser("~/android/reference"), device, "mark-slot-b-" + lun) + + +def load(path, want, ss): + """The saved {lba: (old, new)}, or None when there is no whole set.""" + try: + sums = dict(l.split()[::-1] for l in open(os.path.join(path, "SHA256SUMS"))) + except FileNotFoundError: + return None + out = {} + for lba in want: + try: + pair = tuple(open(os.path.join(path, "%d.%s" % (lba, k)), "rb").read() for k in ("old", "new")) + except FileNotFoundError: + return None + if any(len(x) != ss for x in pair): + return None + if [sums.get("%d.%s" % (lba, k)) for k in ("old", "new")] != [hashlib.sha256(x).hexdigest() for x in pair]: + die("the sectors saved in %s do not match their SHA256SUMS; refusing" % path) + out[lba] = pair + return out + + +def save(path, out, ss): + """Keep the old and new sectors on the laptop before anything is written.""" + tmp = path + ".tmp" + shutil.rmtree(tmp, ignore_errors=True) + os.makedirs(tmp, mode=0o700) + files = {"%d.%s" % (lba, kind): data for lba, pair in out.items() for kind, data in zip(("old", "new"), pair)} + # SHA256SUMS last, as CONTRIBUTING §3.3 asks of every backup; load() checks it + files["SHA256SUMS"] = "".join("%s %s\n" % (hashlib.sha256(d).hexdigest(), n) for n, d in files.items()).encode() + for name, data in files.items(): + with open(os.open(os.path.join(tmp, name), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "wb") as f: + f.write(data) + os.fsync(f.fileno()) + shutil.rmtree(path, ignore_errors=True) + os.rename(tmp, path) + fd = os.open(os.path.dirname(path), os.O_RDONLY) + os.fsync(fd) + os.close(fd) + if load(path, list(out), ss) != out: + die("the sectors saved in %s do not read back; nothing written" % path) + print("old and new sectors saved in " + path) + + +def apply(ph, cur, target, want, ss, path): + """Write target over cur: the primary pair (entries, then header), then the backup + pair, each pair read back from the disk. Sectors already right are not rewritten.""" + if any(ph.read(lba, 1, ss) != cur[lba] for lba in want): + die("the GPT changed since it was checked; nothing written") + for pair in ((want[1], want[0]), (want[2], want[3])): + for lba in pair: + if cur[lba] != target[lba]: + ph.write(lba, target[lba], ss) + for lba in pair: + if ph.read(lba, 1, ss) != target[lba]: + die("read-back of LBA %d does not match; run again to finish or restore (saved in %s)" % (lba, path)) + print("LBA %d and %d written and read back" % pair) + + +def confirm(prompt): + sys.stderr.write(prompt) + sys.stderr.flush() + return sys.stdin.readline().strip() + + def main(): ap = argparse.ArgumentParser(description="Mark slot B successful in a Qualcomm GPT (CONTRIBUTING §3.1).") ap.add_argument("--device", required=True) @@ -189,6 +266,8 @@ def main(): not all(x.isdigit() for x in f[2:]): die("device/%s has no CONTRIBUTING §3.1 entry (SLOT_B_MARK=\"LUN PART P_HDR P_ENT B_ENT B_HDR\")" % a.device) lun, part, want = f[0], f[1], [int(x) for x in f[2:]] + if lun not in conf_vals.get("UFS_RW_LUNS", "").split(): + die("%s is not in device/%s's UFS_RW_LUNS; refusing" % (lun, a.device)) compatible = conf_vals.get("COMPATIBLE", "") if not compatible: die("device/%s has no COMPATIBLE" % a.device) @@ -202,15 +281,34 @@ def main(): cmdline = ph.text("cat /proc/cmdline").split() if "androidboot.slot_suffix=_b" not in cmdline: die("the phone did not boot slot B (no androidboot.slot_suffix=_b): mark B only from a boot of B") + if ph.text("cat /sys/block/%s/ro" % lun) != "0": + die("%s is read-only (the storage guard has not left it writable); refusing" % lun) ss = int(ph.text("cat /sys/block/%s/queue/logical_block_size" % lun)) sectors = int(ph.text("cat /sys/block/%s/size" % lun)) * 512 // ss if ss not in (512, 4096) or sectors - 1 != want[3]: die("%s: %d-byte sectors, last LBA %d; the §3.1 entry says last LBA %d" % (lun, ss, sectors - 1, want[3])) - prim_hdr = ph.read(want[0], 1, ss) + cur = {lba: ph.read(lba, 1, ss) for lba in want} + path = saved_dir(a.device, lun) + saved = load(path, want, ss) + if saved and all(cur[x] in saved[x] for x in want) and \ + len({cur[x] == saved[x][1] for x in want}) == 2: + print("an interrupted --write left LBA %s; saved sectors in %s" % ( + ", ".join("%d %s" % (x, "new" if cur[x] == saved[x][1] else "old") for x in want), path)) + if not a.write: + print("dry run: nothing written (add --write to finish the mark or restore the old sectors)") + return + got = confirm("Type 'mark %s' to finish the write, or 'restore %s' to put the old sectors back: " + % (part, part)) + if got not in ("mark " + part, "restore " + part): + die("not confirmed; nothing written") + k = 1 if got.startswith("mark") else 0 + apply(ph, cur, {x: saved[x][k] for x in want}, want, ss, path) + print("%s %s" % (part, "marked successful" if k else "restored to its old sectors")) + return + prim_hdr, back_hdr = cur[want[0]], cur[want[3]] p = header(prim_hdr, want[0]) n = -(-p["num"] * 128 // ss) prim_ent = ph.read(p["ent"], n, ss) - back_hdr = ph.read(want[3], 1, ss) back_ent = ph.read(header(back_hdr, want[3])["ent"], n, ss) i, attrs, ecrc, old_ecrc, out = plan(prim_hdr, prim_ent, back_hdr, back_ent, part, ss, want) @@ -227,19 +325,10 @@ def main(): if not a.write: print("dry run: nothing written (add --write)") return - sys.stderr.write("Type 'mark %s' to write these four sectors: " % part) - sys.stderr.flush() - if sys.stdin.readline().strip() != "mark " + part: + if confirm("Type 'mark %s' to write these four sectors: " % part) != "mark " + part: die("not confirmed; nothing written") - if any(ph.read(lba, 1, ss) != out[lba][0] for lba in want): - die("the GPT changed since it was checked; nothing written") - for pair in ((want[1], want[0]), (want[2], want[3])): - for lba in pair: - ph.write(lba, out[lba][1], ss) - for lba in pair: - if hashlib.sha256(ph.read(lba, 1, ss)).digest() != hashlib.sha256(out[lba][1]).digest(): - die("read-back of LBA %d does not match: restore from the NOTES rollback copies" % lba) - print("LBA %d and %d written and read back" % pair) + save(path, out, ss) + apply(ph, {x: out[x][0] for x in want}, {x: out[x][1] for x in want}, want, ss, path) print("%s marked successful" % part) From a6b59c4a8ac9e1ce96efe632c87c3b8efe3ca62b Mon Sep 17 00:00:00 2001 From: Matt Barr Date: Thu, 12 Nov 2026 19:24:08 -0500 Subject: [PATCH 5/5] tools/mark-slot-b: complete a torn copy first, keep earlier saved sets A copy with one sector old and one new is already invalid, so a finish or a restore that started on the other copy could leave no valid copy at all. apply() now sorts the pairs so a torn one is written first, and the test checks, after every single write of a finish and a restore from each of the 14 mixes of old and new sectors, that one copy still has a valid header CRC and entry-array CRC. The NOTES say what that guarantees. Also from the review: an earlier saved set is kept under a dated name rather than deleted; a write that fails on a dropped link says not to restart the phone but to run the tool again; calls that send no data get stdin from /dev/null, so phone-ssh cannot swallow a piped confirmation; the tests assert fsync on the write and the header's own-LBA check; and tests/mark-slot-b has its own lines at the end of tests/all. Co-Authored-By: Claude Opus 5.5 --- device/enchilada/NOTES.md | 5 ++-- tests/all | 4 ++- tests/mark-slot-b | 53 +++++++++++++++++++++++++++++++++++++-- tools/mark-slot-b | 41 ++++++++++++++++++++++-------- 4 files changed, 87 insertions(+), 16 deletions(-) diff --git a/device/enchilada/NOTES.md b/device/enchilada/NOTES.md index 8cfb75eb3..4c4fb21c6 100644 --- a/device/enchilada/NOTES.md +++ b/device/enchilada/NOTES.md @@ -146,8 +146,9 @@ back the two read-only copies. A copy of `sde`'s LBA 0-5 and 786426-786431 (both GPT copies whole) and its `sfdisk --dump` were taken read-only on 2026-11-09, and their SHA-256 matched the phone's. They are kept off the repository because they hold the disk's GUIDs. `tools/mark-slot-b` writes -the primary pair and reads it back before it touches the backup pair, so a -valid copy exists at every moment. It saves the four old sectors on the +one pair and reads it back before it touches the other, and a pair with one +sector old and one new goes first, so from a table with a valid copy at least +one copy stays valid after every single write. It saves the four old sectors on the laptop first (`~/android/reference/enchilada/mark-slot-b-sde/`), so after a cut cable the next run sees the half-written table and offers to finish the write or put the old sectors back. The owner's acceptance on #432 has to diff --git a/tests/all b/tests/all index 2b511966f..efae535c6 100755 --- a/tests/all +++ b/tests/all @@ -53,7 +53,7 @@ python3 tests/boot-arch-label.py python3 tests/persistent-root-boot.py python3 tests/initramfs-root-policy.py python3 tests/bt-uart-gpio.py -for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-placeholders tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/build-packages-host-tools tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/mark-slot-b tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe tests/dipper-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/build-host-sync tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs tests/install-slot-descriptor; do +for t in tests/pr-check tests/kernel-pr-check tests/phone-smoke tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/rootfs-placeholders tests/rootfs-resolve tests/phone-provision tests/nv-backup tests/initramfs-grow tests/images-layout tests/boot-update-parity tests/stage-boot-files tests/build-packages-host-tools tests/initramfs-reproducible tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/grep-q-pipes tests/boot-method tests/modules-push tests/make-bundle-keys tests/make-bundle tests/release-signing tests/pinned-packages tests/flame-probe tests/dipper-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds tests/phone-install-package tests/publish-kernel tests/fetch-toolchain tests/release-upload tests/extract-camera-data tests/release tests/build-host-sync tests/lineage-ref tests/repo-publish tests/repo-publish-stage tests/extract-vendor-firmware tests/alpine-fetch-callers tests/alpine-fetch tests/tools-help tests/install-phone-initramfs tests/install-slot-descriptor; do echo "# $t" "$t" done @@ -100,3 +100,5 @@ echo "# tests/build-packages-deps" # a package's dependencies into the build c tests/build-packages-deps echo "# tests/build-packages-cache" # a forced same-version rebuild drops the stale cached package tests/build-packages-cache +echo "# tests/mark-slot-b" +tests/mark-slot-b diff --git a/tests/mark-slot-b b/tests/mark-slot-b index 0058f7695..9f218bbb0 100755 --- a/tests/mark-slot-b +++ b/tests/mark-slot-b @@ -3,7 +3,8 @@ # tools/mark-slot-b against a synthetic GPT with 4096-byte sectors behind a # fake phone-ssh: the dry run writes nothing, --write sets bit 54 on boot_b # alone and changes exactly the four named sectors in order, a --write cut off -# at any sector can be finished or undone, and every refusal holds. +# at any sector can be finished or undone, a valid GPT copy survives every +# single write, and every refusal holds. set -euo pipefail cd "$(dirname "$0")/.." command -v sfdisk >/dev/null || { echo "ok - # skip: no sfdisk for tests/mark-slot-b"; exit 0; } @@ -48,6 +49,14 @@ elif [[ $c == *'if="$n"'* ]]; then fi # CUT=1 cuts every write's transfer short, as a dropped SSH link would [[ -n ${CUT:-} && $c == *'cat > '* ]] && { head -c 100 | PATH="$T/bin:$PATH" sh -c "$c"; exit; } +# A call that sends no data reads its stdin all the same, as the real +# phone-ssh would, and would swallow a piped confirmation +[[ $c == *'cat > '* || -t 0 ]] || cat > /dev/null +# SNAP=DIR keeps a copy of the disk after every write +if [[ -n ${SNAP:-} && $c == *'of="$n"'* ]]; then + rc=0; PATH="$T/bin:$PATH" sh -c "$c" || rc=$? + cp "$T/disk.img" "$SNAP/$w"; exit $rc +fi PATH="$T/bin:$PATH" exec sh -c "$c" EOF chmod +x "$t/ssh" "$t/bin/mknod" @@ -96,9 +105,13 @@ grep 'dd of=' "$t/log" | grep -vc 'dd if="$d/sector" of="$n"' >/dev/null && fail grep -q "boot_b marked successful" "$t/out" || fail "--write does not report the mark" order=$(grep -o 'of="$n" bs=4096 seek=[0-9]*' "$t/log" | sed 's/.*seek=//' | xargs) grep 'if="$n"' "$t/log" | grep -vc 'iflag=direct' >/dev/null && fail "a disk read goes through the page cache, not O_DIRECT" +grep 'of="$n"' "$t/log" | grep -vc 'conv=notrunc,fsync' >/dev/null && fail "a disk write is not synced (conv=notrunc,fsync)" [[ $order == "$b $a $c $d" ]] || fail "--write wrote LBA '$order', not primary entries, primary header, backup entries, backup header ($b $a $c $d)" [[ $(stat -c %a "$saved") == 700 && $(ls "$saved" | wc -l) == 9 ]] && (cd "$saved" && sha256sum --quiet -c SHA256SUMS) || fail "the old and new sectors were not saved privately" cmp -s "$saved/$b.old" <(dd if="$t/disk.orig" bs=4096 skip="$b" count=1 status=none) || fail "the saved old sector is not the disk's" +# the cut transfer above saved a set first: it is kept under a dated name +earlier=("$saved".*) +[[ -d ${earlier[0]} ]] && (cd "${earlier[0]}" && sha256sum --quiet -c SHA256SUMS) || fail "an earlier saved set was not kept" [[ -z $(ls -A "$t/run") ]] || fail "the private node was left behind after --write" changed=$(cmp -l "$t/disk.orig" "$t/disk.img" | awk '{print int(($1 - 1) / 4096)}' | sort -nu | xargs) [[ $changed == "$lbas" ]] || fail "--write changed sectors '$changed', not '$lbas'" @@ -116,6 +129,7 @@ for at in 2 3 4; do for how in mark restore; do cp "$t/disk.orig" "$t/disk.img"; fresh echo "mark boot_b" | FAIL_AT=$at mark --write > "$t/out" 2>&1 && fail "--write cut off at sector $at says it worked" + grep -q "don't restart the phone; run this tool again" "$t/out" || { cat "$t/out"; fail "cut off at sector $at: no word not to restart the phone"; } fresh mark > "$t/out" 2>&1 || { cat "$t/out"; fail "cut off at sector $at: the next run refuses the table"; } grep -q "an interrupted --write left" "$t/out" || { cat "$t/out"; fail "cut off at sector $at: not recognised"; } @@ -127,6 +141,39 @@ for at in 2 3 4; do done done +# From each of the 14 mixes of old and new sectors, a finish and a restore +# leave at least one copy with a valid header CRC and entry-array CRC after +# every single write +mkdir "$t/snap" +for mix in $(seq 1 14); do + for how in mark restore; do + cp "$t/disk.orig" "$t/disk.img"; i=0 + for lba in $lbas; do + (( mix >> i & 1 )) && dd if="$t/disk.marked" of="$t/disk.img" bs=4096 skip="$lba" seek="$lba" count=1 conv=notrunc status=none + i=$((i + 1)) + done + fresh; rm -f "$t/snap"/* + echo "$how boot_b" | SNAP=$t/snap mark --write > "$t/out" 2>&1 || { cat "$t/out"; fail "mix $mix: $how fails"; } + want=$t/disk.marked; [[ $how == restore ]] && want=$t/disk.orig + cmp -s "$t/disk.img" "$want" || fail "mix $mix: $how did not give the $how table" + /usr/bin/python3 - "$a" "$d" "$t/snap"/* <<'EOF' || fail "mix $mix: $how left no valid copy after a write" +import struct, sys, zlib +def valid(img, lba): + img.seek(lba * 4096); h = img.read(4096) + size, crc = struct.unpack_from(" /dev/null 2>&1 && fail "--write cut off at sector 2 says it worked" @@ -216,6 +263,8 @@ refuses() { mark > "$t/out" 2>&1 && fail "takes $2"; grep -q "$1" "$t/out" || { cp "$t/disk.orig" "$t/disk.img" hdr_set "$a" 32 $((d - 1)); refuses "headers do not describe each other" "a primary header pointing elsewhere" flip $((a * 4096 + 60)); refuses "LBA $a: header CRC does not match" "a bad primary header CRC" +hdr_set "$a" 24 $((a + 1)); refuses "LBA $a: unexpected header (my LBA $((a + 1))" "a primary header that names another LBA as its own" +hdr_set "$d" 24 $((d - 1)); refuses "LBA $d: unexpected header (my LBA $((d - 1))" "a backup header that names another LBA as its own" flip $((d * 4096 + 60)); refuses "LBA $d: header CRC does not match" "a bad backup header CRC" flip $((b * 4096 + 100)); flip $((c * 4096 + 100)); refuses "entry-array CRC does not match" "a bad entry-array CRC" sfdisk -q --sector-size 4096 --part-label "$t/disk.img" 40 boot_b @@ -231,4 +280,4 @@ printf 'CODENAME=probe\n' > "$t/repo/device/probe/device.conf" mark > "$t/out" 2>&1 && fail "runs without a §3.1 entry" grep -q "no CONTRIBUTING §3.1 entry" "$t/out" || fail "missing entry refused for the wrong reason" -echo "ok - mark-slot-b: phone check, dry run, confirmation, checked sector files, bit 54 on boot_b in exactly LBA ${lbas// /,} in order, saved sectors, finish or restore after a cut at each sector, and its refusals" +echo "ok - mark-slot-b: phone check, dry run, confirmation, checked sector files, bit 54 on boot_b in exactly LBA ${lbas// /,} in order, saved sectors, finish or restore after a cut at each sector, a valid copy after every write from each of the 14 mixed states, and its refusals" diff --git a/tools/mark-slot-b b/tools/mark-slot-b index 92fa46f7f..04f0704a2 100755 --- a/tools/mark-slot-b +++ b/tools/mark-slot-b @@ -22,16 +22,18 @@ checks that exactly the four sectors change. Without --write it only prints the plan (read-only). With --write, after the human types the confirmation on stdin, it saves the four old and the four new sectors on the laptop with a SHA256SUMS (~/android/reference// -mark-slot-b-/, off the repository: they hold the disk's GUID), re-reads the four sectors (they must -not have changed since the check), then writes the primary entry sector and -header, reads them back from the disk (O_DIRECT), then the backup pair, and -removes the node. Each sector is copied to a file in the private directory and +mark-slot-b-/, off the repository: they hold the disk's GUID; an earlier +set is kept under a dated name), re-reads the four sectors (they must not have +changed since the check), then writes the primary entry sector and header, +reads them back from the disk (O_DIRECT), then the backup pair, and removes +the node. Each sector is copied to a file in the private directory and its SHA-256 checked on the phone before dd writes it from that file. A --write cut off part way leaves each of the four sectors old or new. The next run recognises that against the saved sectors and offers, under the typed confirmation, to finish the write ('mark PART') or put the old sectors back -('restore PART'), in the same order and with the same checks. +('restore PART'), with the same checks. A copy with one sector old and one new +is written first, so a valid copy is never touched while it is the only one. By hand, once per install of slot B's boot image, never from a service or at boot (AGENTS.md §1). The rollback for a corrupted GPT is the device's NOTES. @@ -44,6 +46,7 @@ import shutil import struct import subprocess import sys +import time import zlib SUCCESSFUL, ACTIVE, UNBOOTABLE = 54, 50, 55 @@ -79,16 +82,21 @@ def descriptor(path): class Phone: def __init__(self, ssh, lun): self.ssh, self.lun = ssh, lun + self.writing = False def run(self, script, data=None): + # stdin only for the sector itself: ssh would swallow a piped confirmation + io = {"input": data} if data is not None else {"stdin": subprocess.DEVNULL} r = subprocess.run([self.ssh, "sh -c '%s' sh %s" % ((NODE + script).replace("'", "'\\''"), self.lun)], - input=data, stdout=subprocess.PIPE) + stdout=subprocess.PIPE, **io) if r.returncode: - die("phone command failed (exit %d)" % r.returncode) + die("phone command failed (exit %d)" % r.returncode + ( + "\nmark-slot-b: the write may have stopped part way: don't restart the phone;" + " run this tool again to finish the write or put the old sectors back" if self.writing else "")) return r.stdout def text(self, cmd): - r = subprocess.run([self.ssh, cmd], stdout=subprocess.PIPE, text=True) + r = subprocess.run([self.ssh, cmd], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True) if r.returncode: die("cannot run on the phone: " + cmd) return r.stdout.strip() @@ -219,7 +227,13 @@ def save(path, out, ss): with open(os.open(os.path.join(tmp, name), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "wb") as f: f.write(data) os.fsync(f.fileno()) - shutil.rmtree(path, ignore_errors=True) + if os.path.exists(path): + # the only copies of those old sectors: keep them under a dated name + old = path + "." + time.strftime("%Y-%m-%dT%H%M%SZ", time.gmtime()) + n = 1 + while os.path.exists(old + ("-%d" % n) * (n > 1)): + n += 1 + os.rename(path, old + ("-%d" % n) * (n > 1)) os.rename(tmp, path) fd = os.open(os.path.dirname(path), os.O_RDONLY) os.fsync(fd) @@ -231,10 +245,15 @@ def save(path, out, ss): def apply(ph, cur, target, want, ss, path): """Write target over cur: the primary pair (entries, then header), then the backup - pair, each pair read back from the disk. Sectors already right are not rewritten.""" + pair (a pair with one sector old and one new goes first), each pair read back from + the disk. Sectors already right are not rewritten.""" if any(ph.read(lba, 1, ss) != cur[lba] for lba in want): die("the GPT changed since it was checked; nothing written") - for pair in ((want[1], want[0]), (want[2], want[3])): + ph.writing = True + pairs = [(want[1], want[0]), (want[2], want[3])] + # a copy with one sector old and one new is already invalid: make it whole first + pairs.sort(key=lambda pr: len({cur[l] == target[l] for l in pr}) == 1) + for pair in pairs: for lba in pair: if cur[lba] != target[lba]: ph.write(lba, target[lba], ss)