Skip to content

mtd: apple-sftl: Add fenced 64 GB PPN 1.3 (j2a) write, erase, and reclaim support - #167

Open
alistairreay wants to merge 5 commits into
OrchardLinux:armv7from
alistairreay:ipad3-nand-write-kunit
Open

alistairreay wants to merge 5 commits into
OrchardLinux:armv7from
alistairreay:ipad3-nand-write-kunit

Conversation

@alistairreay

Copy link
Copy Markdown

Builds on #155 (which remains read-only; this PR adds opt-in, LwVM-fenced write, erase, and blank DIRTY_FREE superblock reclaim for 64 GB PPN 1.3 8 KiB / lpp=2 iPad3,3 (j2a) hardware).

Summary

  • Opt-in Kconfig gate (CONFIG_MTD_APPLE_SFTL_J2A_WRITE, default n): Keeps sftl_64gb_8k strictly read-only unless explicitly enabled. Raw MTD devices (j2a-lane*) always remain MTD_NO_ERASE without MTD_WRITEABLE (-EROFS).
  • lpp=2 (8 KiB) SFTL writer + LwVM chunk fence: Generalizes apple-sftl-{core,block,account,reclaim} for 2 logical 4 KiB pages per physical 8 KiB page, and adds an immutable LwVM chunk allowlist (apple_sftl_lwvm_build_fence / apple_sftl_lwvm_lba_writable) so only contiguous Linux (0fc63daf-...) or XBOOTLDR (bc13c2ff-...) partitions are writable; chunk 0 (LwVM headers), iOS System, iOS Data, and free chunks always return -EROFS. Also fixes a pre-existing apple_sftl_rotate_vfl() bug where cold->{lpp,psize,osize} were left zeroed.
  • Gated 64 GB PPN 1.3 program_page and erase_block (drivers/misc/apple-h2fmi-ro.c): Implements 3-byte row 0x8a/0x17/0x79 program and 0x6a 0xd7/0x79/0x77 0x7d/0x77 erase (accepting PPN 1.3's 0x64 CMD 79 ready status with BIT(0)|BIT(4) clear, followed by 0x40 completion status). Protects block 0 and bootstrap/VFL/factory blocks 2032..2063 with -EPERM, verifies erased pre-readback and byte-for-byte post-readback on every program, verifies all 256 pages read back erased after every erase, and latches a sticky failed flag on any error.
  • Blank DIRTY_FREE superblock reclaim (sftl_reuse_blank_dirty): Wear-levels, end-checks, verifies unmapped, and erases blank DIRTY_FREE (role 9) superblocks on 12H321 native media while leaving iOS's native CLEAN_FREE pool untouched.
  • Gated LwVM install debugfs helper (apple_sftl_j2a/lwvm_install): Validates p1 untouched, p2 shrunk (>= 8 GiB) with retained chunks unchanged, and contiguous p3 before writing backup (LBA 16383) then primary (LBA 0) with readback verification.

Verification

  • ARM KUnit (all 7 suites, QEMU ARM):
    • CONFIG_MTD_APPLE_SFTL_J2A_WRITE=y: 120 / 120 passed (0 failed, 0 skipped), including new sftl_64gb_8k write, BTOC close/reload, program-failure evacuation, blank DIRTY_FREE reclaim, LwVM fence, accounting, reclaim, and PPN 1.3 H2FMI tests.
    • CONFIG_MTD_APPLE_SFTL_J2A_WRITE=n: 117 passed, 3 skipped, 0 failed.
  • Build check: drivers/misc/apple-h2fmi-ro.o, drivers/mtd/apple-sftl-*.o, and full zImage build with zero warnings for both =y and =n.
  • Live 64 GB iPad3,3 (j2a, iOS 8.4.1 / 12H321) hardware tests:
    • Verified -EROFS / permission denial on /dev/applesftl0 (LBA 0), /dev/applesftl0p1 (System), /dev/applesftl0p2 (Data), and /dev/mtd0.
    • Phase 1: Wrote validated LwVM p3 (4 GiB Linux) and a 32 MiB ext4 filesystem + 5 MiB test files (4,788 verified 8 KiB page programs + BTOC superblock close); rebooted cleanly into iOS 8.4.1 (System & Data mounted, SFTL checkpointed); rebooted back to Linux with bit-for-bit SHA-256 match.
    • Phase 2: Wrote a 192 MiB ext4 image + 16 MiB random file (208 MiB total in 132.9 s, 1.6 MB/s sustained), reclaiming 7 blank DIRTY_FREE superblocks (112 verified PPN 1.3 block erases, 26,894 verified page programs, 6 closed 0x401c BTOC footers, zero errors); rebooted cleanly into iOS 8.4.1 (SFTL checkpointed all 7 superblocks cleanly, / and /private/var intact); rebooted back to Linux with bit-for-bit SHA-256 match.

Generalize the strictly read-only S5L8945X H2FMI/PPN driver to match
both measured iPad3,3 geometries from the PPN Device Info parameter page:
- 32 GB PPN 1.6: 2 CAUs/CE (cau_bits 3), 2048 blocks (11 bits), 8 lanes,
  4-byte row / 8-byte full read address (PPN >= 1.5)
- 64 GB PPN 1.3: 4 CAUs/CE (cau_bits 2), 2064 blocks (12 bits), 16 lanes,
  3-byte row / 7-byte full read address (PPN <= 1.4, verified in iBEC)

Both capacities share 8 KiB data + 32 B OOB pages (8224 raw bytes, 256
MLC / 128 SLC pages per block) and remain strictly read-only with no
write or erase opcodes.

Signed-off-by: Alistair Reay <alistair.reay@gmail.com>
Add the measured read-only SFTL geometry for 64 GB PPN 1.3 iPad 3 units
(4 packages x 4 CAUs = 16 lanes, 2064 blocks/CAU, 256 MLC / 128 SLC pages,
8 KiB pages / lpp=2, 2057 superblocks, VFL in block 2061 of lanes 0-7,
15625000 exported + 16 hidden logical pages). Strictly read-only
(writer = false).

Signed-off-by: Alistair Reay <alistair.reay@gmail.com>
…LwVM fence

Generalize the Apple SFTL append, close, evacuation, accounting, and
reclamation paths from hardcoded 16 KiB (lpp=4) pages to support 8 KiB
(lpp=2) pages on the measured 64 GB PPN 1.3 iPad 3 (j2a) layout
(2057 superblocks, 24 opaque context records, map_batch=16, map_min=32).

Add default-off CONFIG_MTD_APPLE_SFTL_J2A_WRITE and an immutable LwVM
write allowlist (apple_sftl_lwvm_build_fence / apple_sftl_lwvm_lba_writable)
that restricts block writes strictly to validated contiguous Linux or
XBOOTLDR LwVM partition chunks, keeping chunk 0, HFS+ iOS partitions,
free chunks, and raw MTDs strictly read-only.

Also fix a pre-existing bug in apple_sftl_rotate_vfl() where cold->lpp,
cold->psize, and cold->osize were left zeroed, breaking
sftl_test_rotate_mounted.

Add PPN 1.3 64 GB geometry/split-join helpers to mtd-apple-h2fmi.h and
extend all KUnit suites (apple-h2fmi, apple-sftl, apple-sftl-reclaim,
apple-sftl-accounting, apple-sftl-lwvm). All 119 ARM KUnit tests pass in
QEMU with CONFIG_MTD_APPLE_SFTL_J2A_WRITE=y, and 117 pass / 2 skip with
it disabled (strictly read-only). Zero hardware writes are enabled in
apple-h2fmi-ro.c.

Signed-off-by: Alistair Reay <alistair.reay@gmail.com>
Wire PPN 1.3 page program (0x8a + 3-byte row + 8224 B PIO + 0x17 + 0x79)
and block erase (0x6a 0xd7 + 3-byte row + 0x79 + 0x77 0x7d + 0x77) into
the SFTL PPN provider when CONFIG_MTD_APPLE_SFTL_J2A_WRITE is enabled and
the probed packages match the 64 GB PPN 1.3 (16-lane, 2064-block) geometry.

Raw MTD devices remain strictly read-only (MTD_NO_ERASE, no MTD_WRITEABLE).
Block 0 and the top 32 bootstrap/VFL/factory blocks (2032..2063) are
rejected with -EPERM. Every program verifies the target page reads back
erased (0x49 + all 0xff) beforehand and compares all 8224 bytes on
readback afterward; every erase checks CMD 79 mutation status (0x64 on
PPN 1.3: BIT(6) ready, BIT(0)|BIT(4) clear), CMD 77 7D completion status
(0x40), and verifies all 256 pages read back erased. Any failure latches a
sticky failed flag refusing further mutations.

Assisted-by: LLM
Signed-off-by: Alistair Reay <alistair.reay@gmail.com>
… for 64 GB j2a

On 12H321 (iOS 8.4.1) 64 GB PPN 1.3 media, blank DIRTY_FREE (role 9)
superblocks hold no live pages and can be wear-leveled, erased, and reused
without touching iOS's native CLEAN_FREE pool. Add sftl_reuse_blank_dirty()
gated on fenced_write + native_context + erase_block, with end-page blank
checks, live-map verification, and full erased readback.

Also add a CONFIG_MTD_APPLE_SFTL_J2A_WRITE-gated debugfs helper
(apple_sftl_j2a/lwvm_install and status) that validates a shrunk-Data +
contiguous-Linux-p3 LwVM table before writing backup (LBA 16383) then
primary (LBA 0) with readback verification, fix a misleading journal-floor
warning when the LwVM fence keeps a stock 2-partition disk read-only, and
add KUnit coverage for blank dirty-free reclaim on sftl_64gb_8k.

Assisted-by: LLM
Signed-off-by: Alistair Reay <alistair.reay@gmail.com>
@henrikklee

Copy link
Copy Markdown

Thanks for all the work here, @alistairreay. The KUnit coverage and the 8 KiB-page (lpp=2) support are impressive. We're not merging this yet, though, and we'll leave it open. Here's why.

It has the same open points we raised on #69, and it builds a second copy of #69's design in the same files (apple-sftl-*, the H2FMI driver). Two writers for one FTL would be hard to keep safe, so we'd like one shared design first. The points:

  1. The kernel trusts userspace about the iOS Data size. The LwVM install path takes the new layout from userspace and checks it, but the kernel should decide on its own whether iOS Data can shrink (how much is in use, and where).
  2. Install interface in debugfs. debugfs isn't an ABI and is often not mounted. An install step that writes the partition map needs a proper interface (or belongs entirely in userspace on top of a writable block device).
  3. Board-named Kconfig and identity checks. CONFIG_MTD_APPLE_SFTL_J2A_WRITE and table entries keyed to j2a / 12H321 tie the writer to one board and one firmware. Upstream practice selects behaviour from per-layout match data (geometry, lpp, protected ranges), so another 8 KiB PPN 1.3 part gets it automatically.
  4. Fixed protected blocks. Block 0 and 2032..2063 ("top 32") are hard-coded. They should come from the layout's own values (VFL/bootstrap/factory ranges read from the media or held in the layout data).
  5. Older-epoch acceptance on a writer. iPad 3: Add 64 GB PPN 1.3 read-only NAND and SFTL support #155 accepts older-epoch pages for reading (older_epoch_subtype1). On a writer that has to stay off until it's confirmed that it can't make the writer overwrite newer data.
  6. No automatic checkpoint. Nothing closes the BTOC / writes a checkpoint on shutdown, reboot or flush. A writer needs that, or a power loss after a write leaves the FTL inconsistent.

What we'd like: you and @brkosebay agree on one shared writer design, along the four-part split from the #59 review (#59):

  1. read-only fast transfers (parallel/DMA reads) on their own;
  2. one writer, enabled by per-layout data. Your 8 KiB-page support is the valuable part here and should be part of it from the start;
  3. one install policy, with a kernel-side gate on iOS Data;
  4. automatic checkpoints on shutdown/flush.

Alongside that: one flash-controller driver (H2FMI) with per-chip match data for A5, A5X and A6, not a driver per board.

Once that shared base exists, the j2a-specific part should shrink to a layout table entry. Happy to review a design sketch before you two write more code.

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