8000 xflash: handle aligned UFS HBA literal pools by Jz8Root · Pull Request #426 · bkerler/mtkclient · GitHub
[go: up one dir, main page]

Skip to content

xflash: handle aligned UFS HBA literal pools - #426

Open
Jz8Root wants to merge 1 commit into
bkerler:mainfrom
Jz8Root:jz8/ufs-literal-alignment
Open

Jz8Root wants to merge 1 commit into
bkerler:mainfrom
Jz8Root:jz8/ufs-literal-alignment

Conversation

@Jz8Root
@Jz8Root Jz8Root commented Oct 28, 2026
Copy link
Copy Markdown
Contributor

Summary

Handle the optional 16-bit Thumb alignment NOP between an MT6833
ufshcd_probe_hba return sequence and the following UFS HBA literal.

Root cause

XFlashExt.patch() currently locates this MT6833 return sequence:

20 46 0D B0 BD E8 F0 83

and reads the following pointer at pattern_offset + 8.

The DA used by the POCO M4 5G thunder report places a NOP before the literal:

DA2 file offset 0xD59A, runtime 0x4000D59A

0x4000D59A: mov   r0, r4
0x4000D59C: add   sp, #0x34
0x4000D59E: pop.w {r4, r5, r6, r7, r8, r9, pc}
0x4000D5A2: nop
0x4000D5A4: .word 0x40066CD0

The fixed +8 read starts on the NOP and produces 0x6CD0BF00. The correct,
word-aligned literal is 0x40066CD0.

Change

  • Resolve the 32-bit literal following the Thumb return.
  • Consume 00 BF only when it is required to reach word alignment.
  • Preserve the existing no-padding layout.
  • Return no pointer for truncated or still-unaligned input instead of injecting
    a partial address into da_x.bin.

No device pointer or DA-specific absolute address is hardcoded.

Validation

Added focused unittest fixtures for:

  • the exact NOP-aligned layout from the hardware report
  • an already aligned layout without a NOP
  • a truncated aligned literal
  • an unaligned layout without the required NOP

Commands run on current upstream main (118a000a):

python3 -m unittest discover -s tests -v
python3 -m compileall -q mtkclient
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
git diff --check

All checks pass. The reporter's standalone equivalent was validated on hardware;
this PR implements the same correction generically. The generic upstream diff
has not been re-run on that phone.

Evidence and attribution

Reported by @ogmkp in:
Jz8Root/xiaomi-hyperos-bootloader-unlock#18

The report includes the DA2 region coordinates, disassembly, incorrect/correct
pointer values and the successful RPMB operation after the RAM-only correction.
The reporter requested attribution by GitHub handle rather than an email trailer.

Some MT6833 download agents insert a 16-bit Thumb NOP between the
ufshcd_probe_hba return and the following 32-bit HBA literal. Reading the
literal at a fixed +8 offset then combines the NOP with half of the address.

Resolve the word-aligned literal after the return, consume the alignment NOP
when required, and fail closed on truncated or unaligned data. Add focused
fixtures for aligned, unpadded and invalid layouts.

Reported-by: ogmkp
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.

1 participant

0