Skip to content

mtd: apple-sftl: Boot from LwVM partitions iOS scattered over the flash - #89

Merged
henrikklee merged 3 commits into
armv7from
lwvm-scattered-partitions
Dec 2, 2026
Merged

henrikklee merged 3 commits into
armv7from
lwvm-scattered-partitions

Conversation

@henrikklee

Copy link
Copy Markdown

What users notice

A 32-bit iPhone or iPad whose Linux partition iOS scattered over its flash now boots it like any other board: the kernel registers the partition, with its PARTUUID, and orchard's initrd finds the root by root=PARTUUID= as usual. No hand-typed dm-mod.create table, no special startup path. The 16 GB iPad mini 1 needs this: its Linux partition lies in three separate runs of flash chunks.

This works on every 32-bit board and layout (P105 16 GB and 64 GB, N42 once its NAND support lands, any other). A board whose partitions are all stored in order sees no change and no new message.

How

  • The kernel's LwVM reader (block/partitions/lwvm.c) can only add a partition that is one contiguous range of the disk, so it skips scattered ones.
  • When applesftlN has scattered LwVM partitions, the SFTL driver now adds a second disk of the same size, applesftlNlwvm. Its first page is a copy of the LwVM header that lists only the scattered partitions, each in order, and every other page is read through the chunk map from the main disk. The normal LwVM reader then adds them as applesftlNlwvmP, with the same partition number, PARTUUID and name. Partitions already on the main disk are left out of the copy, so nothing shows up twice.
  • The view is read-only on every medium (no write path at all), which matches the read-only root + tmpfs overlay that the 16 GB iPad booted with. Making it writable is a separate step.
  • Partitions with a broken chunk map (hole, doubled or out-of-range chunk) are reported and left out.

Commits

  1. mtd: apple-sftl: Decode LwVM chunk maps: Berkay's (@brkosebay) decoder and KUnit tests, split out of his mtd: apple: Read the iPhone 5's NAND #60 unchanged, with his authorship and sign-off.
  2. mtd: apple-sftl: Add scattered LwVM partitions as partitions of a view disk: the view disk, on every profile.
  3. mtd: apple-sftl: Test the 16 GB iPad mini 1's scattered LwVM layout: KUnit case built from the header captured on the device (initrd: boot a read-only root on a scattered LwVM partition (16 GB iPad mini 1) orchard#438, tests/fixtures/p105-scattered-lwvm.bin; the chunk map in the test matches it entry for entry). It checks that the Linux partition maps to exactly the sectors of the dm-linear table that booted it (0 19529728 @ 8978432, 19529728 163840 @ 30048256, 19693568 532840 @ 30474240), and that the view lists it in order with its GUID.

Relation to other work

Testing

  • The decoder, the view and all KUnit cases (9) were compiled and run in userspace against stand-ins for the kernel helpers: all pass, and a deliberately broken view fails them. The armv7 configs do not enable KUnit, so CI compiles the driver but does not run these cases.
  • checkpatch --strict: clean apart from the Assisted-by tag and one blank-line note in Berkay's unchanged code.
  • Not yet booted on a device. Next: one RAM boot of the 16 GB iPad mini 1 with root=PARTUUID=1746a46a-ffc1-4a28-844f-0b168b79185f ro, which should show applesftl0lwvm: 2 scattered LwVM partitions, read-only and mount applesftl0lwvm3.

Written with LLM assistance (Assisted-by: LLM).

brkosebay and others added 3 commits December 2, 2026 21:46
block/partitions/lwvm.c adds each LwVM partition whose chunks are stored
in order and back to back as an ordinary partition, and skips the
others. iOS stores partitions in 16 MiB chunks out of order, so on many
32-bit devices Data, and often the partition Linux is installed in, are
skipped.

Add a decoder that reads the same header with the same checks
(signature, CRC, media size, 1..8 partitions, no entry in the header
chunk or past the disk) and keeps, for every partition, the map from
partition chunk to disk chunk, and a lookup from a partition byte
offset to a disk byte offset through it. A partition with a hole, a
doubled or an out-of-range chunk is reported instead of mapped;
"pieced" marks a valid partition the partition parser skips.

KUnit (apple-sftl-lwvm) covers the chunk size, contiguous, interleaved
and partial-last-chunk layouts, broken partitions and rejected tables
with synthetic headers.

Assisted-by: LLM
Signed-off-by: Berkay Kösebay <brkosebay@users.noreply.github.com>
[Henrik: split the decoder and its tests out of "mtd: apple-sftl: Serve
 scattered LwVM partitions as read-only disks" (#60),
 without the iPhone 5 profile]
Signed-off-by: Henrik Klee <henrik@goat-code.com>
…w disk

block/partitions/lwvm.c adds an LwVM partition to applesftlN only when
its chunks are stored in order and back to back. iOS scatters
partitions over the disk, so on many 32-bit devices the partition Linux
is installed in gets no block device and no PARTUUID, and the system
cannot boot from it with root=PARTUUID=. The 16 GB iPad mini 1 is one:
its Linux partition lies in three runs of chunks.

After the main disk is added, read its LwVM header (and its copy in the
last page of the first chunk if the CRC fails, as the parser does) and,
when it has scattered partitions, add a second disk of the same size,
applesftlNlwvm. Its first page is a copy of the header that lists only
the scattered partitions, each stored in order from chunk 1, with a new
CRC; its other chunks are read through the chunk map from the main disk,
or as zeroes. The partition parser then adds them as applesftlNlwvmP,
with their LwVM number, PARTUUID and name, so the initrd and the kernel
find them by root=PARTUUID= as they find any other partition. Entries
the main disk has already are cleared in the view, so no partition is
added twice. A partition whose map has a hole, a doubled or an
out-of-range chunk is reported and left out.

The view is read-only on every profile and medium and has no write
path: writes end with -EROFS and other operations with -EOPNOTSUPP
before the FTL is reached. Its reads take the parent's lock and buffer.
On a read-write medium, a write through applesftlN into one of its
chunks leaves the view's page cache stale, as a write to a whole disk
does for its partitions. A disk without scattered partitions gets no
view and no new message.

This follows Berkay Kösebay's read-only disk per scattered partition
for the iPhone 5 (#60): the same decoder and
per-page lookup, but as partitions of one disk, so that they carry
their PARTUUID, and on every profile.

KUnit (apple-sftl-lwvm) builds a view, decodes its header with the
parser's checks and looks up its pages.

Assisted-by: LLM
Signed-off-by: Henrik Klee <henrik@goat-code.com>
On a 16 GB iPad mini 1 (P105, iOS 9.3.5) System is stored in six runs
of 16 MiB chunks, Data in order, and the Linux partition in three runs,
the last chunk partial. Until now it booted only through a dm-linear
table typed by hand: 0 19529728 @ 8978432, 19529728 163840 @ 30048256,
19693568 532840 @ 30474240 (sectors).

Add that layout as a KUnit case, with the chunk map of the header
captured from the device (OrchardLinux/orchard#438). The decoder must
map the Linux partition to exactly those sectors, and the view must
list System and Linux in order under their numbers, keep Linux's GUID
and read the same sectors.

Assisted-by: LLM
Signed-off-by: Henrik Klee <henrik@goat-code.com>
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