Skip to content

tools/mark-slot-b and push over UFS_RW_LUNS, inert until the §3.1 entry (part 1 of #616) - #651

Open
matthewjaybarr-png wants to merge 5 commits into
derekross:mainfrom
matthewjaybarr-png:enchilada-mark-slot-b
Open

matthewjaybarr-png wants to merge 5 commits into
derekross:mainfrom
matthewjaybarr-png:enchilada-mark-slot-b

Conversation

@matthewjaybarr-png

Copy link
Copy Markdown
Contributor

What and why

Part 1 of the split asked for on #616: the push change and tools/mark-slot-b with its test, inert until enchilada has its CONTRIBUTING §3.1 entry. No descriptor sets UFS_RW_LUNS or SLOT_B_MARK, and AGENTS.md and CONTRIBUTING.md are untouched; the rule and the entry are the maintainer's, after #639.

push ── sda ─────────────── (today, every device)
     └─ UFS_RW_LUNS LUNs ── (none set yet)

mark-slot-b ── no SLOT_B_MARK ──> refuses   (every device today)
            └─ COMPATIBLE ≠ phone ──> refuses before any node is made

Changes from #616, per its review:

  • Which phone answered: mark-slot-b compares the first /proc/device-tree/compatible string with the descriptor's COMPATIBLE before it makes the node, as the init does. Tested: an acme,other phone is refused and the log shows no mknod.
  • No SSH pipe into the disk: each sector goes to a file in the private directory, its SHA-256 is checked on the phone, and dd writes from that file. Tested: a transfer cut to 100 bytes fails with "arrived damaged on the phone; nothing written", the disk is unchanged, and the private directory is gone. Every dd of= in a real --write is dd if="$d/sector".
  • NOTES: sde's 72 partitions by PARTNAME, and so what the private whole-disk node reaches. abl_a and abl_b are on sde; xbl is not, and its LUN is still to be read on the phone. The "both copies invalid" case now says what it really means: no rescue, no fastboot, only Qualcomm EDL. The bit layout is cited from Qualcomm ABL's PartitionTableUpdate.h.
  • docs/RELEASES.md: SLOT_B_MARK is documented as set only once a device has its entry, and none does.

Before/after, on this branch:

I checked by mutation that the test catches both regressions. With the old pipe write, it fails "--write takes a sector cut short". Without the compatible check, it fails "marks another phone".

Merge danger: low. Nothing on a phone changes until a descriptor sets these keys, which waits for the §3.1 entry. The push lookup over sda is as it was, and tests/images-layout still covers it.

Device and build

  • Phone: none: tools, a test and notes. Nothing here was run on a phone.
  • Kernel release (uname -r): n/a
  • Packages changed, with versions: none

Checks (no phone needed)

  • tools/pr-check origin/main passes
  • tests/all passes
  • tests/packages passes (or the packages I touched: none)
  • Each changed package has its version bumped, and the new version is in the commit subject (no packages changed)
  • Commits have area prefixes and bodies that say why; any AI help is a Co-Authored-By: or Assisted-by: trailer, never a Signed-off-by:

On the phone

  • I ran this on my own phone, booted into this build (not applicable: inert until the entry)
  • Runbook steps passed: none; the runbook comes with part 3
  • What I did not test: no run against the real phone; xbl's LUN is not yet read
  • Nothing in this change writes outside slot B and userdata, weakens the storage guard, or reads registers from userspace (AGENTS.md §1). mark-slot-b is the write the §3.1 entry will name, and without that entry it refuses.

🤖 Generated with Claude Code

@derekross

Copy link
Copy Markdown
Owner

Thank you, @matthewjaybarr-png. This is the split I asked for, and done cleanly: nothing sets UFS_RW_LUNS or SLOT_B_MARK, the two policy files are untouched, the tool checks which phone answered before it makes the node, and no sector is piped into the disk any more. The NOTES on what sde holds and on the EDL case are what the entry needs, and thank you for restating your acceptance on #432. The gate and the suites pass here on today's main, and the push change leaves the Pixels' lookup as it was.

Two things I'd like in the tool before I merge it, while it is still inert:

1. A --write that is interrupted leaves a table the tool then refuses for good. I failed the second, third and fourth sector write in turn against your test's disk:

Write that failed Disk afterwards Every later run
2nd (primary header) primary copy invalid, backup good "the primary and backup entry arrays differ"
3rd (backup entries) both copies valid, but different "the primary and backup headers do not describe each other"
4th (backup header) backup copy invalid, primary good "the primary and backup headers do not describe each other"

After a dropped cable mid-write the only way on would be a dd by hand through a hand-made node, from the 11-09 copies, which predate --set-active=b. Please have the tool save the four old sectors on the laptop before it writes (off the repository, they hold the disk's GUID), recognise each of these three states on the next run, and offer to finish the write or put the old sectors back, under the same typed confirmation. A test per interruption point, please.

2. The test passes with most of the checks removed. I took these out one at a time and tests/mark-slot-b still said ok:

  • the re-read of the four sectors before writing;
  • the read-back after each pair;
  • the last-LBA check;
  • "the headers describe each other";
  • the entry-array CRC and the header CRC checks;
  • the unbootable refusal;
  • the write order (backup pair first passes, and so does header before entries);
  • "found exactly once" (relaxed to "at least once");
  • the internal "only CRC and attribute bytes change" check.

It does catch the slot check, the phone check, the confirmation, the cut transfer, wrong LBAs, differing arrays and an inactive boot_b. Please add a case for each of the others: a table changed between the check and the write, a write that doesn't land, a bad CRC in each place, two partitions named boot_b, bit 55 set, and an assertion on the order of the dd lines.

Smaller things, for this PR or part 3, your choice:

  • "The phone booted slot B" is read from androidboot.slot_suffix=_b. I'd expect a fastboot boot into RAM with B active to carry that too, and then the tool would mark boot_b without its image ever having run. Do you know what your bootloader passes there? If it does, please compare boot_b with the local image by SHA-256 (as push does), or make it a gate in the runbook.
  • Please refuse early when the LUN isn't in UFS_RW_LUNS or /sys/block/<lun>/ro isn't 0, rather than after the confirmation.
  • iflag=direct on the read-back would make sure it reads the disk and not the cache.
  • On the first dry run on the phone (part 3), please record findmnt -no OPTIONS /run: a nodev /run would refuse the private node.

xbl's LUN can wait for the cable; post it on #432 when you have it. The rule and the entry are mine, after #639, and I'll use the numbers in your NOTES.

@matthewjaybarr-png

Copy link
Copy Markdown
Contributor Author

Thanks. Pushed, rebased on today's main (tests/all had a one-line conflict).

  1. Before writing, --write now saves the four old and four new sectors, with a SHA256SUMS, in ~/android/reference/<device>/mark-slot-b-<lun>/. If a run finds a table that is part old and part new against that set, it says which LBAs are which and offers mark boot_b or restore boot_b under the typed confirmation. The test cuts the write at the 2nd, 3rd and 4th sector, then finishes and restores from each one. A damaged saved sector is refused.

  2. The test now has a case for each of the checks you listed. It also asserts the dd order and that every read uses iflag=direct. I took each check out in turn, and the test failed every time.

Smaller ones: the UFS_RW_LUNS and ro refusals now come before the GPT is read or the confirmation is asked, and reads use iflag=direct. On androidboot.slot_suffix: I don't know yet what this bootloader passes on a fastboot boot. I'd like to put that in part 3, as a runbook gate plus a SHA-256 compare of boot_b with the local image once the android-v0 paths are there. findmnt -no OPTIONS /run goes in the first phone dry run there too.

@derekross

Copy link
Copy Markdown
Owner

Thank you, @matthewjaybarr-png. This is a careful answer to both points.

The tests. I took 22 things out of the tool one at a time, the nine from last time among them, and tests/mark-slot-b failed for 20, each on the case you wrote for it. The two it didn't notice are small: fsync dropped from the write's conv= (an assertion on the dd line, as you did for iflag=direct, would do), and the header's "my LBA" compare.

The recovery. Saving the old and new sectors first, recognising a half-written table against them, and offering to finish or restore under the typed confirmation is what I asked for. I cut the first write before the 2nd, 3rd and 4th sector and ran both choices from each: all six end on the right image, byte for byte.

One thing in it has to change before I merge, and it's the kind of thing only the recovery could have introduced. apply() always writes the primary pair first. After a write cut before the 4th sector, the primary copy is valid and the backup is the broken one (new entries, old header). restore then starts by rewriting the primary entry sector, and until the primary header lands the LUN has no valid copy at all:

first --write cut before sector 4     primary valid, marked  | backup INVALID
'restore', cut before its 2nd write   primary INVALID        | backup INVALID

The mirror image exists too: a restore from that state cut before its 3rd write leaves the primary valid and the backup broken, and mark from there breaks the primary first. I walked all 14 mixes of old and new sectors, both choices, cut before each write: four starting states pass through "no valid copy" from a table that had one. Your NOTES say what that state means on this phone (EDL only), and the tool still recognises it on the next run, but a second dropped cable at that moment is exactly the case we're writing this for.

The fix is small: bring a torn copy to the target first, so the good copy is never touched while it's the only one.

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:

I ran that: the same 140 walks find no such state, a fresh mark keeps its primary-then-backup order, and your test passes unchanged. Please add a case that checks, after every single write of a finish and of a restore from each mixed state, that at least one copy has a valid header CRC and entry-array CRC; and change the NOTES sentence "so a valid copy exists at every moment" to what the tool then guarantees.

Smaller things, for this push if you can:

  • tests/all: please put tests/mark-slot-b on its own two lines at the end of the file, under "One suite per line from here on", rather than in the long loop line. You met the reason on this rebase.
  • save() deletes the previous saved set before it writes the new one. Could it keep earlier sets under a dated name? They're the only copies of those old sectors.
  • When a write fails on a dropped link the tool says "phone command failed (exit 255)". One more line there would help the person at the phone: don't restart it, run the tool again.
  • The reads inherit the tool's stdin. With the real tools/phone-ssh I'd expect a piped confirmation to be swallowed by the first read, so the tool refuses, which is the safe side; stdin=subprocess.DEVNULL on the calls that send no data would make that deliberate. Your call.

The slot_suffix question on a fastboot boot, the SHA-256 compare of boot_b and findmnt -no OPTIONS /run are fine in part 3, as you propose. The push change is as it was: on the Pixels it sends the same three reads as today.

The rule and the entry are still mine. Your numbers will go into it as they are (LBA 1, 3, 786428 and 786431 on sde), which also means my rule's "LBAs 1, 2, last-1 and last" needs rewording, since it describes another phone's table.

mptfire pushed a commit to mptfire/omarchy-mobile that referenced this pull request Nov 12, 2026
- 6-vs-7 partition discrepancy resolved from source (device.conf:90-91:
  6 NV_PARTS + devinfo extra = 7 dumped, 6 consumed).
- NEW-2 tested empirically: duplicate UFS_RW keys are last-wins in all
  three parsers (no desync); install-phone and omamobile refuse the
  hostile value fail-closed; the true differential is bundle-manifest's
  missing every-entry rule — covered by the installer's flash gate and
  plan equality. proof/duplicate-key-check.sh records it.
- Check 17 added to the battery: --unsigned --reinstall + boot_a/persist
  manifest writes refused at allowlist equality. Battery now 16/16.
- Severity re-rulings recorded (S-1 Medium, S-7 Medium with Layer-3
  boundary correction), NEW-1 implemented on the fix branch, derekross#651 verdict
  amended (stable-LUN-identity ask), S-3 pre-seed design filed.
@matthewjaybarr-png

Copy link
Copy Markdown
Contributor Author

Thanks, all done in bb532d3 (rebased onto main, so it needs a force push):

  • apply() has your sort, word for word, so a torn copy is made whole first.
  • New test case: for each of the 14 mixes of old and new sectors, a finish and a restore are run, and after every single write at least one copy has a valid header CRC and entry-array CRC. With the sort taken out it fails (mix 4, the finish).
  • NOTES: the sentence now says what the tool guarantees: from a table with a valid copy, at least one copy stays valid after every single write.
  • Tests: conv=notrunc,fsync is asserted on every write, and a primary or backup header naming another LBA as its own is refused.
  • tests/all: tests/mark-slot-b is on its own two lines at the end, out of the long loop.
  • save() keeps an earlier set under a dated name (mark-slot-b-sde.<UTC time>) instead of deleting it.
  • A write that fails on a dropped link adds: don't restart the phone; run this tool again to finish the write or put the old sectors back.
  • stdin=subprocess.DEVNULL on the calls that send no data. The fake ssh in the test now reads its stdin like the real one, so the piped confirmations prove it.

I left the rule text alone.

@derekross

Copy link
Copy Markdown
Owner

Thank you, @matthewjaybarr-png. Everything from the last round is in, and I checked each one by taking it back out.

The one that mattered: with your sort removed from apply(), the new case fails at "mix 4: mark left no valid copy after a write", so the test now holds the guarantee your NOTES state, that from a table with a valid copy at least one copy stays valid after every single write. The others the same way: without fsync the suite fails "a disk write is not synced", without the header's own-LBA compare it fails on both headers, with the old delete in save() it fails "an earlier saved set was not kept", without the new line it fails "no word not to restart the phone", and without stdin=subprocess.DEVNULL (in either call) the first piped confirmation is swallowed and the suite fails there. Making the fake ssh read its stdin like the real one was the right way to prove that.

tools/pr-check passes, tests/mark-slot-b and tests/images-layout pass here on today's main, and nothing sets SLOT_B_MARK or UFS_RW_LUNS, so it is still inert.

The red check on this push is not yours. tests/all passed, your suite included; what failed is one line in omamobile-shell's permission-sheet test ("a closed request's sheet is taken back"), a 3-second wait that missed on the runner, in a package this pull request doesn't touch. Main passed the same test before and after. I'll re-run the job and merge when it's green.

Part 3 then carries what we agreed: what your bootloader passes as slot_suffix on a fastboot boot, the SHA-256 compare of boot_b with the local image, and findmnt -no OPTIONS /run on the first dry run. The rule and the §3.1 entry are still mine, with your numbers as they are.

matthewjaybarr-png and others added 5 commits November 18, 2026 20:25
push looked for <image>_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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@matthewjaybarr-png

Copy link
Copy Markdown
Contributor Author

Rebased onto current main. The only conflict was the test list in tests/all, which main had added to since. I kept main's list and added tests/mark-slot-b; nothing else in the PR changed. tests/all and tests/packages both pass locally, so the new CI run should come up green. Thanks for looking at it again when you have a moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants