From 937dbe5a6b3481b1eba42d21e9118ff75ee2bdc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 29 Nov 2026 14:44:03 +0100 Subject: [PATCH 1/3] mtd: apple-sftl: Decode LwVM chunk maps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 [Henrik: split the decoder and its tests out of "mtd: apple-sftl: Serve scattered LwVM partitions as read-only disks" (OrchardLinux/linux#60), without the iPhone 5 profile] Signed-off-by: Henrik Klee --- drivers/mtd/Kconfig | 1 + drivers/mtd/Makefile | 2 + drivers/mtd/apple-sftl-lwvm-test.c | 302 +++++++++++++++++++++++++++++ drivers/mtd/apple-sftl-lwvm.c | 171 ++++++++++++++++ drivers/mtd/apple-sftl-lwvm.h | 45 +++++ 5 files changed, 521 insertions(+) create mode 100644 drivers/mtd/apple-sftl-lwvm-test.c create mode 100644 drivers/mtd/apple-sftl-lwvm.c create mode 100644 drivers/mtd/apple-sftl-lwvm.h diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig index 42e979b1784a1b..6243e3aeac0e22 100644 --- a/drivers/mtd/Kconfig +++ b/drivers/mtd/Kconfig @@ -209,6 +209,7 @@ config MTD_PARTITIONED_MASTER config MTD_APPLE_SFTL tristate "Apple PPN VFL/SwissPPNFTL translation" depends on BLOCK + select CRC32 help Recover the logical block medium stored in Apple VFL/SwissPPNFTL metadata on the measured 64 GB and 16 GB Hynix PPN layouts. The diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile index 227eac233d55eb..d11d92d982389b 100644 --- a/drivers/mtd/Makefile +++ b/drivers/mtd/Makefile @@ -34,6 +34,8 @@ obj-$(CONFIG_MTD_HYPERBUS) += hyperbus/ obj-$(CONFIG_MTD_APPLE_SFTL) += apple-sftl.o apple-sftl-y := apple-sftl-core.o apple-sftl-block.o apple-sftl-reclaim.o apple-sftl-account.o apple-sftl-provider.o apple-sftl-vfl.o +apple-sftl-y += apple-sftl-lwvm.o obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-test.o obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-reclaim-test.o obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-account-test.o apple-sftl-provider-test.o apple-sftl-vfl-test.o +obj-$(CONFIG_MTD_APPLE_SFTL_KUNIT_TEST) += apple-sftl-lwvm-test.o diff --git a/drivers/mtd/apple-sftl-lwvm-test.c b/drivers/mtd/apple-sftl-lwvm-test.c new file mode 100644 index 00000000000000..cd174d51a18b29 --- /dev/null +++ b/drivers/mtd/apple-sftl-lwvm-test.c @@ -0,0 +1,302 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Synthetic LwVM headers: contiguous, interleaved and partial partitions. */ +#include +#include +#include +#include +#include + +#include "apple-sftl-lwvm.h" + +/* 8 MiB is the smallest LwVM disk: 1024 chunks of 8 KiB. */ +#define TEST_MEDIA (8ULL << 20) +#define TEST_CHUNK 8192ULL + +static const u8 test_signature[16] = { + 0x6a, 0x90, 0x88, 0xcf, 0x8a, 0xfd, 0x63, 0x0a, + 0xe3, 0x51, 0xe2, 0x48, 0x87, 0xe0, 0xb9, 0x8b, +}; + +struct lwvm_fixture { + u8 *header; + struct apple_sftl_lwvm *lwvm; +}; + +static void header_init(u8 *h, u64 media, u32 count) +{ + unsigned int k; + + memset(h, 0, APPLE_SFTL_LWVM_HEADER_BYTES); + memcpy(h, test_signature, sizeof(test_signature)); + put_unaligned_le64(media, h + 0x20); + put_unaligned_le32(count, h + 0x28); + /* iOS marks the header chunk f000 and free chunks f111. */ + put_unaligned_le16(0xf000, h + 0x800); + for (k = 1; k < APPLE_SFTL_LWVM_CHUNKS; k++) + put_unaligned_le16(0xf111, h + 0x800 + 2 * k); +} + +static void header_part(u8 *h, unsigned int i, u64 begin, u64 end, const char *name) +{ + u8 *entry = h + 0x200 + 0x80 * i; + unsigned int c; + + memset(entry + 16, 0x10 + i, 16); + put_unaligned_le64(begin, entry + 32); + put_unaligned_le64(end, entry + 40); + for (c = 0; name[c]; c++) + put_unaligned_le16(name[c], entry + 56 + 2 * c); +} + +static void header_map(u8 *h, unsigned int disk_chunk, unsigned int owner, unsigned int index) +{ + put_unaligned_le16(owner << 12 | index, h + 0x800 + 2 * disk_chunk); +} + +static void header_seal(u8 *h) +{ + put_unaligned_le32(0, h + 0x2c); + put_unaligned_le32(~crc32_le(~0, h, APPLE_SFTL_LWVM_HEADER_BYTES), h + 0x2c); +} + +static int lwvm_init(struct kunit *test) +{ + struct lwvm_fixture *f = kunit_kzalloc(test, sizeof(*f), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, f); + f->header = kunit_kzalloc(test, APPLE_SFTL_LWVM_HEADER_BYTES, GFP_KERNEL); + f->lwvm = kunit_kzalloc(test, sizeof(*f->lwvm), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, f->header); + KUNIT_ASSERT_NOT_NULL(test, f->lwvm); + test->priv = f; + return 0; +} + +static u64 map(struct kunit *test, const struct apple_sftl_lwvm_part *part, u64 offset) +{ + u64 disk = U64_MAX; + + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_map(part, offset, &disk), 0); + return disk; +} + +static void lwvm_chunk_size(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_chunk_shift(TEST_MEDIA), 13); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_chunk_shift(TEST_MEDIA - 512), 0); + /* iPhone 5 16 GB: 3906250 logical pages, 953 whole 16 MiB chunks. */ + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_chunk_shift(3906250ULL * 4096), 24); + + header_init(f->header, 3906250ULL * 4096, 1); + header_part(f->header, 0, 0, 3ULL << 24, "System"); + header_map(f->header, 1, 0, 0); + header_map(f->header, 2, 0, 1); + header_map(f->header, 3, 0, 2); + header_seal(f->header); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->header, 3906250ULL * 4096, f->lwvm), 0); + KUNIT_EXPECT_EQ(test, f->lwvm->chunks, 953); + KUNIT_EXPECT_EQ(test, f->lwvm->parts[0].nr_chunks, 3); + KUNIT_EXPECT_FALSE(test, f->lwvm->parts[0].pieced); +} + +/* In order and back to back: the partition parser adds these, never pieced. */ +static void lwvm_contiguous(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + struct apple_sftl_lwvm_part *data = &f->lwvm->parts[1]; + + header_init(f->header, TEST_MEDIA, 2); + header_part(f->header, 0, 0, 2 * TEST_CHUNK, "System"); + header_part(f->header, 1, 2 * TEST_CHUNK, 5 * TEST_CHUNK, "Data"); + header_map(f->header, 1, 0, 0); + header_map(f->header, 2, 0, 1); + header_map(f->header, 3, 1, 0); + header_map(f->header, 4, 1, 1); + header_map(f->header, 5, 1, 2); + header_seal(f->header); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->header, TEST_MEDIA, f->lwvm), 0); + KUNIT_EXPECT_EQ(test, f->lwvm->nr_parts, 2); + KUNIT_EXPECT_STREQ(test, f->lwvm->parts[0].name, "System"); + KUNIT_EXPECT_STREQ(test, data->name, "Data"); + KUNIT_EXPECT_EQ(test, f->lwvm->parts[0].error, 0); + KUNIT_EXPECT_EQ(test, data->error, 0); + KUNIT_EXPECT_FALSE(test, f->lwvm->parts[0].pieced); + KUNIT_EXPECT_FALSE(test, data->pieced); + KUNIT_EXPECT_EQ(test, data->nr_chunks, 3); + KUNIT_EXPECT_EQ(test, map(test, data, 0), 3 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, data, 3 * TEST_CHUNK - 1), 6 * TEST_CHUNK - 1); +} + +/* + * System in order but with gaps, Data out of order between them. Pages on + * both sides of a chunk border land in unrelated disk chunks. + */ +static void lwvm_interleaved(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + struct apple_sftl_lwvm_part *system = &f->lwvm->parts[0]; + struct apple_sftl_lwvm_part *data = &f->lwvm->parts[1]; + + header_init(f->header, TEST_MEDIA, 2); + header_part(f->header, 0, 0, 3 * TEST_CHUNK, "System"); + header_part(f->header, 1, 3 * TEST_CHUNK, 6 * TEST_CHUNK, "Data"); + header_map(f->header, 1, 0, 0); + header_map(f->header, 3, 0, 1); + header_map(f->header, 5, 0, 2); + header_map(f->header, 6, 1, 0); + header_map(f->header, 2, 1, 1); + header_map(f->header, 4, 1, 2); + header_seal(f->header); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->header, TEST_MEDIA, f->lwvm), 0); + KUNIT_EXPECT_TRUE(test, system->pieced); + KUNIT_EXPECT_TRUE(test, data->pieced); + KUNIT_EXPECT_EQ(test, data->mapped, 3); + + KUNIT_EXPECT_EQ(test, map(test, system, 0), 1 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, system, TEST_CHUNK - 4096), 2 * TEST_CHUNK - 4096); + KUNIT_EXPECT_EQ(test, map(test, system, TEST_CHUNK), 3 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, system, 2 * TEST_CHUNK + 512), 5 * TEST_CHUNK + 512); + + KUNIT_EXPECT_EQ(test, map(test, data, 0), 6 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, data, TEST_CHUNK - 1), 7 * TEST_CHUNK - 1); + KUNIT_EXPECT_EQ(test, map(test, data, TEST_CHUNK), 2 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, data, 2 * TEST_CHUNK - 4096), 3 * TEST_CHUNK - 4096); + KUNIT_EXPECT_EQ(test, map(test, data, 2 * TEST_CHUNK), 4 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, data, 3 * TEST_CHUNK - 1), 5 * TEST_CHUNK - 1); +} + +/* The last chunk is only half used: the size, not the chunk, ends the map. */ +static void lwvm_partial_last_chunk(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + struct apple_sftl_lwvm_part *data = &f->lwvm->parts[0]; + u64 disk = 0; + + header_init(f->header, TEST_MEDIA, 1); + header_part(f->header, 0, 0x100000, 0x100000 + 2 * TEST_CHUNK + TEST_CHUNK / 2, "Data"); + header_map(f->header, 7, 0, 0); + header_map(f->header, 1, 0, 1); + header_map(f->header, 1023, 0, 2); + header_seal(f->header); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->header, TEST_MEDIA, f->lwvm), 0); + KUNIT_EXPECT_TRUE(test, data->pieced); + KUNIT_EXPECT_EQ(test, data->nr_chunks, 3); + KUNIT_EXPECT_EQ(test, map(test, data, TEST_CHUNK + 4096), TEST_CHUNK + 4096); + KUNIT_EXPECT_EQ(test, map(test, data, 2 * TEST_CHUNK), 1023 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, data, 2 * TEST_CHUNK + TEST_CHUNK / 2 - 1), + 1023 * TEST_CHUNK + TEST_CHUNK / 2 - 1); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_map(data, 2 * TEST_CHUNK + TEST_CHUNK / 2, &disk), + -ERANGE); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_map(data, U64_MAX, &disk), -ERANGE); +} + +/* A partition the map cannot serve is refused alone; the others stay. */ +static void lwvm_broken_partitions(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + struct apple_sftl_lwvm *lwvm = f->lwvm; + u64 disk = 0; + + header_init(f->header, TEST_MEDIA, 5); + header_part(f->header, 0, 0, 2 * TEST_CHUNK, "Hole"); + header_part(f->header, 1, 0, 2 * TEST_CHUNK, "Twice"); + header_part(f->header, 2, 0, 1 * TEST_CHUNK, "Beyond"); + header_part(f->header, 3, 4096, 4096, "Empty"); + header_part(f->header, 4, 0, 2 * TEST_CHUNK, "Good"); + header_map(f->header, 10, 0, 1); + header_map(f->header, 11, 1, 0); + header_map(f->header, 12, 1, 0); + header_map(f->header, 13, 1, 1); + header_map(f->header, 14, 2, 0); + header_map(f->header, 15, 2, 1); + header_map(f->header, 21, 4, 0); + header_map(f->header, 20, 4, 1); + header_seal(f->header); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->header, TEST_MEDIA, lwvm), 0); + KUNIT_EXPECT_EQ(test, lwvm->parts[0].error, -ENODATA); + KUNIT_EXPECT_EQ(test, lwvm->parts[1].error, -EEXIST); + KUNIT_EXPECT_EQ(test, lwvm->parts[2].error, -ERANGE); + KUNIT_EXPECT_EQ(test, lwvm->parts[3].error, -EINVAL); + KUNIT_EXPECT_EQ(test, lwvm->parts[4].error, 0); + KUNIT_EXPECT_FALSE(test, lwvm->parts[0].pieced); + KUNIT_EXPECT_FALSE(test, lwvm->parts[1].pieced); + KUNIT_EXPECT_FALSE(test, lwvm->parts[2].pieced); + KUNIT_EXPECT_FALSE(test, lwvm->parts[3].pieced); + KUNIT_EXPECT_TRUE(test, lwvm->parts[4].pieced); + KUNIT_EXPECT_EQ(test, lwvm->parts[1].mapped, 3); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_map(&lwvm->parts[0], TEST_CHUNK, &disk), -ERANGE); + KUNIT_EXPECT_EQ(test, map(test, &lwvm->parts[4], 0), 21 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, &lwvm->parts[4], TEST_CHUNK), 20 * TEST_CHUNK); +} + +/* Headers and maps that block/partitions/lwvm.c rejects as a whole. */ +static void lwvm_bad_tables(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + u8 *h = f->header; + + header_init(h, TEST_MEDIA, 1); + header_part(h, 0, 0, TEST_CHUNK, "System"); + header_map(h, 1, 0, 0); + header_seal(h); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA, f->lwvm), 0); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA + TEST_CHUNK, f->lwvm), + -EBADMSG); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA / 2, f->lwvm), -ENOENT); + h[100] ^= 1; + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA, f->lwvm), -EBADMSG); + h[0] ^= 1; + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA, f->lwvm), -ENOENT); + + header_init(h, TEST_MEDIA, 0); + header_seal(h); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA, f->lwvm), -EBADMSG); + + /* An owner past the partition count. */ + header_init(h, TEST_MEDIA, 1); + header_part(h, 0, 0, TEST_CHUNK, "System"); + header_map(h, 1, 0, 0); + header_map(h, 2, 1, 0); + header_seal(h); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA, f->lwvm), -EINVAL); + + /* A partition in the header chunk. */ + header_init(h, TEST_MEDIA, 1); + header_part(h, 0, 0, TEST_CHUNK, "System"); + header_map(h, 0, 0, 0); + header_seal(h); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA, f->lwvm), -EINVAL); + + /* 16 KiB chunks: only 512 lie on the disk. */ + header_init(h, TEST_MEDIA + 4096, 1); + header_part(h, 0, 0, 16384, "System"); + header_map(h, 512, 0, 0); + header_seal(h); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA + 4096, f->lwvm), -EINVAL); + header_map(h, 512, 0xf, 0x111); + header_map(h, 511, 0, 0); + header_seal(h); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(h, TEST_MEDIA + 4096, f->lwvm), 0); + KUNIT_EXPECT_EQ(test, f->lwvm->chunks, 512); + KUNIT_EXPECT_EQ(test, map(test, &f->lwvm->parts[0], 16383), 511 * 16384ULL + 16383); +} + +static struct kunit_case lwvm_cases[] = { + KUNIT_CASE(lwvm_chunk_size), + KUNIT_CASE(lwvm_contiguous), + KUNIT_CASE(lwvm_interleaved), + KUNIT_CASE(lwvm_partial_last_chunk), + KUNIT_CASE(lwvm_broken_partitions), + KUNIT_CASE(lwvm_bad_tables), + {} +}; + +static struct kunit_suite lwvm_suite = { + .name = "apple-sftl-lwvm", + .init = lwvm_init, + .test_cases = lwvm_cases, +}; +kunit_test_suite(lwvm_suite); +MODULE_LICENSE("GPL"); diff --git a/drivers/mtd/apple-sftl-lwvm.c b/drivers/mtd/apple-sftl-lwvm.c new file mode 100644 index 00000000000000..2f2c4967ffe665 --- /dev/null +++ b/drivers/mtd/apple-sftl-lwvm.c @@ -0,0 +1,171 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * LwVM partitions stored in pieces. + * + * 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. + * This decoder reads the same header with the same checks and keeps, for + * every partition, the map from partition chunk to disk chunk, so the SFTL + * block frontend can serve a scattered partition read-only through it. + * + * A map entry is the partition index in the top four bits and the partition + * chunk in the low twelve; owner 0xf is the header chunk or a free chunk. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "apple-sftl-lwvm.h" + +#define LWVM_MEDIA_OFFSET 0x20 +#define LWVM_COUNT_OFFSET 0x28 +#define LWVM_CRC_OFFSET 0x2c +#define LWVM_PARTS_OFFSET 0x200 +#define LWVM_PART_BYTES 0x80 +#define LWVM_MAP_OFFSET 0x800 +#define LWVM_CHUNK_FREE 0xf +#define LWVM_NAME_CHARS 36 + +/* 6a9088cf-8afd-630a-e351-e24887e0b98b, the CRC-protected variant. */ +static const u8 lwvm_signature[16] = { + 0x6a, 0x90, 0x88, 0xcf, 0x8a, 0xfd, 0x63, 0x0a, + 0xe3, 0x51, 0xe2, 0x48, 0x87, 0xe0, 0xb9, 0x8b, +}; + +/* The chunk is the smallest power of two that splits the disk in 1024. */ +unsigned int apple_sftl_lwvm_chunk_shift(u64 media_bytes) +{ + if (media_bytes < 2ULL * APPLE_SFTL_LWVM_HEADER_BYTES * APPLE_SFTL_LWVM_CHUNKS) + return 0; + return fls64(DIV_ROUND_UP_ULL(media_bytes, APPLE_SFTL_LWVM_CHUNKS) - 1); +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_chunk_shift); + +static bool lwvm_crc_ok(const u8 *header) +{ + static const u8 zero[4]; + u32 crc; + + crc = crc32_le(~0, header, LWVM_CRC_OFFSET); + crc = crc32_le(crc, zero, sizeof(zero)); + crc = crc32_le(crc, header + LWVM_CRC_OFFSET + 4, + APPLE_SFTL_LWVM_HEADER_BYTES - LWVM_CRC_OFFSET - 4); + return ~crc == get_unaligned_le32(header + LWVM_CRC_OFFSET); +} + +static void lwvm_part(const u8 *entry, unsigned int shift, struct apple_sftl_lwvm_part *part) +{ + unsigned int i; + + memcpy(part->guid, entry + 16, sizeof(part->guid)); + part->begin = get_unaligned_le64(entry + 32); + part->end = get_unaligned_le64(entry + 40); + part->attributes = get_unaligned_le64(entry + 48); + for (i = 0; i < LWVM_NAME_CHARS; i++) { + u16 c = get_unaligned_le16(entry + 56 + 2 * i); + + if (!c) + break; + part->name[i] = c < 0x80 && isprint(c) ? c : '!'; + } + part->name[i] = 0; + part->chunk_shift = shift; + memset(part->chunk, 0xff, sizeof(part->chunk)); + /* block/partitions/lwvm.c ignores these entries without a word. */ + if (part->end <= part->begin || (part->begin | part->end) & 511) { + part->error = -EINVAL; + return; + } + if (part->end - part->begin > (u64)APPLE_SFTL_LWVM_CHUNKS << shift) { + part->error = -EFBIG; + return; + } + part->nr_chunks = DIV_ROUND_UP_ULL(part->end - part->begin, 1ULL << shift); +} + +/* + * Returns -ENOENT without an LwVM signature, -EBADMSG for a header that fails + * its CRC, size or count check (try the copy), and -EINVAL for a map that + * block/partitions/lwvm.c rejects as a whole. + */ +int apple_sftl_lwvm_decode(const u8 *header, u64 media_bytes, struct apple_sftl_lwvm *lwvm) +{ + unsigned int shift = apple_sftl_lwvm_chunk_shift(media_bytes); + unsigned int i, k; + u32 count; + + if (!shift || memcmp(header, lwvm_signature, sizeof(lwvm_signature))) + return -ENOENT; + count = get_unaligned_le32(header + LWVM_COUNT_OFFSET); + if (!lwvm_crc_ok(header) || + get_unaligned_le64(header + LWVM_MEDIA_OFFSET) != media_bytes || + count < 1 || count > APPLE_SFTL_LWVM_MAX_PARTS) + return -EBADMSG; + + memset(lwvm, 0, sizeof(*lwvm)); + lwvm->media_bytes = media_bytes; + lwvm->chunk_shift = shift; + lwvm->chunks = media_bytes >> shift; + lwvm->nr_parts = count; + for (i = 0; i < count; i++) + lwvm_part(header + LWVM_PARTS_OFFSET + i * LWVM_PART_BYTES, shift, + &lwvm->parts[i]); + + for (k = 0; k < APPLE_SFTL_LWVM_CHUNKS; k++) { + u16 entry = get_unaligned_le16(header + LWVM_MAP_OFFSET + 2 * k); + unsigned int owner = entry >> 12, index = entry & 0xfff; + struct apple_sftl_lwvm_part *part; + + if (owner == LWVM_CHUNK_FREE) + continue; + if (!k || k >= lwvm->chunks || owner >= count) + return -EINVAL; + part = &lwvm->parts[owner]; + part->mapped++; + if (part->error) + continue; + if (index >= part->nr_chunks) + part->error = -ERANGE; + else if (part->chunk[index] != APPLE_SFTL_LWVM_NO_CHUNK) + part->error = -EEXIST; + else + part->chunk[index] = k; + } + + for (i = 0; i < count; i++) { + struct apple_sftl_lwvm_part *part = &lwvm->parts[i]; + bool in_order = true; + + if (part->error) + continue; + for (k = 0; k < part->nr_chunks; k++) { + if (part->chunk[k] == APPLE_SFTL_LWVM_NO_CHUNK) { + part->error = -ENODATA; + break; + } + if (part->chunk[k] != part->chunk[0] + k) + in_order = false; + } + part->pieced = !part->error && !in_order; + } + return 0; +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_decode); + +/* Partition byte offset to disk byte offset, through the chunk map. */ +int apple_sftl_lwvm_map(const struct apple_sftl_lwvm_part *part, u64 offset, u64 *disk_offset) +{ + u64 mask = (1ULL << part->chunk_shift) - 1; + + if (part->error || offset >= part->end - part->begin) + return -ERANGE; + *disk_offset = ((u64)part->chunk[offset >> part->chunk_shift] << part->chunk_shift) | + (offset & mask); + return 0; +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_map); diff --git a/drivers/mtd/apple-sftl-lwvm.h b/drivers/mtd/apple-sftl-lwvm.h new file mode 100644 index 00000000000000..a6df74d76ef35e --- /dev/null +++ b/drivers/mtd/apple-sftl-lwvm.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _APPLE_SFTL_LWVM_H +#define _APPLE_SFTL_LWVM_H + +#include + +#define APPLE_SFTL_LWVM_HEADER_BYTES 4096U +#define APPLE_SFTL_LWVM_CHUNKS 1024U +#define APPLE_SFTL_LWVM_MAX_PARTS 8U +#define APPLE_SFTL_LWVM_NO_CHUNK 0xffffU + +/* + * One LwVM partition. chunk[] maps each partition chunk to the disk chunk + * that stores it. error is 0 when every chunk is mapped exactly once, else + * why the map cannot serve the partition. pieced marks a valid partition + * that block/partitions/lwvm.c skips because it is not stored in order and + * back to back. + */ +struct apple_sftl_lwvm_part { + u64 begin; + u64 end; + u64 attributes; + u8 guid[16]; + char name[37]; + u8 chunk_shift; + bool pieced; + int error; + u32 nr_chunks; /* chunks the size needs, the last one can be partial */ + u32 mapped; /* map entries that name this partition */ + u16 chunk[APPLE_SFTL_LWVM_CHUNKS]; +}; + +struct apple_sftl_lwvm { + u64 media_bytes; + u32 chunks; /* whole chunks on the disk */ + u32 nr_parts; + u8 chunk_shift; + struct apple_sftl_lwvm_part parts[APPLE_SFTL_LWVM_MAX_PARTS]; +}; + +unsigned int apple_sftl_lwvm_chunk_shift(u64 media_bytes); +int apple_sftl_lwvm_decode(const u8 *header, u64 media_bytes, struct apple_sftl_lwvm *lwvm); +int apple_sftl_lwvm_map(const struct apple_sftl_lwvm_part *part, u64 offset, u64 *disk_offset); + +#endif From d5c7a0c210d2d7e87cc99d462499f8a3002d1973 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Wed, 2 Dec 2026 21:50:42 +0800 Subject: [PATCH 2/3] mtd: apple-sftl: Add scattered LwVM partitions as partitions of a view disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (OrchardLinux/linux#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 --- Documentation/driver-api/mtd/apple-sftl.rst | 12 + drivers/mtd/Kconfig | 6 + drivers/mtd/apple-sftl-block.c | 229 +++++++++++++++++++- drivers/mtd/apple-sftl-lwvm-test.c | 98 ++++++++- drivers/mtd/apple-sftl-lwvm.c | 75 ++++++- drivers/mtd/apple-sftl-lwvm.h | 26 +++ 6 files changed, 430 insertions(+), 16 deletions(-) diff --git a/Documentation/driver-api/mtd/apple-sftl.rst b/Documentation/driver-api/mtd/apple-sftl.rst index b043c3d9f3a2d2..9a21f897fc31fc 100644 --- a/Documentation/driver-api/mtd/apple-sftl.rst +++ b/Documentation/driver-api/mtd/apple-sftl.rst @@ -45,6 +45,18 @@ profile 3,906,266 and 3,906,250 entries, or 16,000,000,000 bytes. The disk has is ``applesftlN``. LwVM and filesystem interpretation happen above this logical provider. Encrypted partition contents remain ciphertext. +The LwVM partition parser adds a partition to ``applesftlN`` only when its +chunks are stored in order and back to back. iOS often scatters a partition +over the disk. For those, the frontend adds a second, read-only disk, +``applesftlNlwvm``, of the same size: its first page is a copy of the LwVM +header that lists only the scattered partitions, each stored in order from +chunk 1, and every other chunk of it is 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 +``root=PARTUUID=`` finds them as it finds any other partition. Partitions +whose map has a hole, a doubled or an out-of-range chunk are reported and +left out. The view has no write path. + Failure and lifetime rules -------------------------- diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig index 6243e3aeac0e22..4db4ec217d9ef7 100644 --- a/drivers/mtd/Kconfig +++ b/drivers/mtd/Kconfig @@ -225,6 +225,11 @@ config MTD_APPLE_SFTL validates. Providers without a qualified erase operation return ENOSPC when their supply of blank blocks runs out. + The LwVM partition parser adds only partitions stored in order on + applesftlN. Those that iOS stored in scattered chunks are added + read-only as partitions of a second disk, applesftlNlwvm, with their + LwVM number, PARTUUID and name. + config MTD_APPLE_SFTL_KUNIT_TEST tristate "KUnit tests for Apple VFL/SwissPPNFTL" if !KUNIT_ALL_TESTS depends on KUNIT && MTD_APPLE_SFTL @@ -236,6 +241,7 @@ config MTD_APPLE_SFTL_KUNIT_TEST readback and recovery using synthetic flash. Exercise native accounting, checkpoint page cuts, interrupted lane retirement, both replicated VFL banks and fresh-loader replay. + Decode LwVM chunk maps and check the view of scattered partitions. All test media and provider operations are synthetic. source "drivers/mtd/chips/Kconfig" diff --git a/drivers/mtd/apple-sftl-block.c b/drivers/mtd/apple-sftl-block.c index 148a245358663e..11b028b286af16 100644 --- a/drivers/mtd/apple-sftl-block.c +++ b/drivers/mtd/apple-sftl-block.c @@ -13,11 +13,14 @@ #include #include "apple-sftl.h" +#include "apple-sftl-lwvm.h" #define SFTL_BLOCK_NAME "apple-sftl" #define SFTL_DISK_MINORS 16 #define SFTL_LOGICAL_BYTES 4096U +struct sftl_view; + struct sftl_disk { struct apple_sftl *ftl; struct blk_mq_tag_set tags; @@ -27,13 +30,32 @@ struct sftl_disk { unsigned int id; bool dead; bool writable; + struct sftl_view *view; +}; + +/* + * The LwVM partitions that iOS stored in scattered chunks, which the + * partition parser cannot add to the main disk. The view disk reads the + * parent's pages through the chunk map, under the parent's lock and with + * its buffer. There is no write path. + */ +struct sftl_view { + struct sftl_disk *parent; + struct blk_mq_tag_set tags; + struct gendisk *disk; + struct apple_sftl_lwvm_view map; }; static DEFINE_IDA(sftl_disk_ids); static int sftl_major; -/* The blocking queue and this lock keep the preallocated buffer exclusive. */ -static int sftl_read_request(struct sftl_disk *sftl, struct request *request) +/* + * The blocking queue and this lock keep the preallocated buffer exclusive. + * The view passes its map: each 4 KiB page is looked up in it, so a request + * can cross chunks in any order. + */ +static int sftl_read_request(struct sftl_disk *sftl, struct request *request, + const struct apple_sftl_lwvm_view *view) { struct req_iterator iterator; struct bio_vec bvec; @@ -44,22 +66,37 @@ static int sftl_read_request(struct sftl_disk *sftl, struct request *request) unsigned int done = 0; while (done < bvec.bv_len) { - u32 logical = position >> 12; + enum apple_sftl_lwvm_page page = APPLE_SFTL_LWVM_PAGE_DISK; unsigned int offset = position & (SFTL_LOGICAL_BYTES - 1); unsigned int length = min(bvec.bv_len - done, SFTL_LOGICAL_BYTES - offset); + u64 source = position; + const u8 *from; void *mapped; + u32 logical; int ret; - if (logical != cached) { - ret = apple_sftl_read(sftl->ftl, logical, sftl->buffer); - if (ret) - return ret; - cached = logical; + if (view) + page = apple_sftl_lwvm_view_map(view, position, &source); + if (page == APPLE_SFTL_LWVM_PAGE_HEADER) { + from = view->header + offset; + } else if (page == APPLE_SFTL_LWVM_PAGE_ZERO) { + from = NULL; + } else { + logical = source >> 12; + if (logical != cached) { + ret = apple_sftl_read(sftl->ftl, logical, sftl->buffer); + if (ret) + return ret; + cached = logical; + } + from = sftl->buffer + offset; } mapped = kmap_local_page(bvec.bv_page); - memcpy(mapped + bvec.bv_offset + done, - sftl->buffer + offset, length); + if (from) + memcpy(mapped + bvec.bv_offset + done, from, length); + else + memset(mapped + bvec.bv_offset + done, 0, length); kunmap_local(mapped); done += length; position += length; @@ -157,7 +194,7 @@ static blk_status_t sftl_queue_rq(struct blk_mq_hw_ctx *hctx, } else if (req_op(request) == REQ_OP_WRITE) { ret = sftl->writable ? sftl_write_request(sftl, request) : -EROFS; } else { - ret = sftl_read_request(sftl, request); + ret = sftl_read_request(sftl, request, NULL); } memalloc_noio_restore(noio_flags); mutex_unlock(&sftl->io_lock); @@ -166,14 +203,182 @@ static blk_status_t sftl_queue_rq(struct blk_mq_hw_ctx *hctx, return BLK_STS_OK; } +/* Reads only: anything else ends here and never reaches the FTL. */ +static blk_status_t sftl_view_queue_rq(struct blk_mq_hw_ctx *hctx, + const struct blk_mq_queue_data *data) +{ + struct request *request = data->rq; + struct sftl_view *view = request->q->queuedata; + struct sftl_disk *sftl = view->parent; + sector_t capacity = get_capacity(view->disk); + unsigned int noio_flags; + int ret; + + if (req_op(request) != REQ_OP_READ) { + blk_mq_start_request(request); + ret = req_op(request) == REQ_OP_WRITE ? -EROFS : -EOPNOTSUPP; + blk_mq_end_request(request, errno_to_blk_status(ret)); + return BLK_STS_OK; + } + mutex_lock(&sftl->io_lock); + noio_flags = memalloc_noio_save(); + blk_mq_start_request(request); + if (sftl->dead) + ret = -ENODEV; + else if (blk_rq_pos(request) > capacity || + blk_rq_sectors(request) > capacity - blk_rq_pos(request)) + ret = -EIO; + else + ret = sftl_read_request(sftl, request, &view->map); + memalloc_noio_restore(noio_flags); + mutex_unlock(&sftl->io_lock); + blk_mq_end_request(request, errno_to_blk_status(ret)); + return BLK_STS_OK; +} + static const struct blk_mq_ops sftl_mq_ops = { .queue_rq = sftl_queue_rq, }; +static const struct blk_mq_ops sftl_view_mq_ops = { + .queue_rq = sftl_view_queue_rq, +}; + static const struct block_device_operations sftl_block_ops = { .owner = THIS_MODULE, }; +/* + * Read the LwVM header (or its copy in the last page of the first chunk if + * it fails its CRC, as the partition parser does) and build the view of + * the partitions the parser skips because they are scattered. NULL when + * there are none or the header is not usable; the parser reports that. + */ +static struct sftl_view *sftl_view_alloc(struct platform_device *pdev, + struct sftl_disk *sftl, u64 media) +{ + struct apple_sftl_lwvm *lwvm; + struct sftl_view *view; + unsigned int noio_flags, i; + u8 *header; + int ret = -ENOMEM; + + view = kzalloc(sizeof(*view), GFP_KERNEL); + lwvm = kzalloc(sizeof(*lwvm), GFP_KERNEL); + header = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL); + if (!view || !lwvm || !header) + goto out; + mutex_lock(&sftl->io_lock); + noio_flags = memalloc_noio_save(); + ret = apple_sftl_read(sftl->ftl, 0, header); + if (!ret) + ret = apple_sftl_lwvm_decode(header, media, lwvm); + if (ret == -EBADMSG) { + u32 copy = (1U << (apple_sftl_lwvm_chunk_shift(media) - 12)) - 1; + + ret = apple_sftl_read(sftl->ftl, copy, header); + if (!ret) + ret = apple_sftl_lwvm_decode(header, media, lwvm); + } + memalloc_noio_restore(noio_flags); + mutex_unlock(&sftl->io_lock); + if (ret) + goto out; + /* Empty or oversized entries are skipped silently, as by the parser. */ + for (i = 0; i < lwvm->nr_parts; i++) + if (lwvm->parts[i].error && lwvm->parts[i].error != -EINVAL && + lwvm->parts[i].error != -EFBIG) + dev_warn(&pdev->dev, "LwVM partition %u \"%s\" has a broken chunk map (%d), not added\n", + i + 1, lwvm->parts[i].name, lwvm->parts[i].error); + if (apple_sftl_lwvm_view(header, lwvm, &view->map)) + goto out; + ret = -ENOENT; +out: + if (ret && ret != -ENOENT) + dev_warn(&pdev->dev, "LwVM header not read (%d), no scattered partitions\n", ret); + kfree(header); + kfree(lwvm); + if (ret) { + kfree(view); + return NULL; + } + return view; +} + +/* + * Give the scattered LwVM partitions a disk of their own, applesftlNlwvm, + * whose partitions keep their LwVM number, PARTUUID and name. It is + * read-only on every medium. On a read-write medium a write through the + * main disk into one of its chunks leaves its page cache stale, as a write + * to a whole disk does for a partition. Every failure here leaves the main + * disk as it is. + */ +static void sftl_add_view(struct platform_device *pdev, struct sftl_disk *sftl, + const struct apple_sftl_info *info) +{ + struct queue_limits limits = { + .logical_block_size = SECTOR_SIZE, + .physical_block_size = SFTL_LOGICAL_BYTES, + .max_hw_sectors = 128, + }; + u64 media = (u64)info->disk_pages * SFTL_LOGICAL_BYTES; + struct sftl_view *view; + struct gendisk *disk; + int ret; + + view = sftl_view_alloc(pdev, sftl, media); + if (!view) + return; + view->parent = sftl; + ret = blk_mq_alloc_sq_tag_set(&view->tags, &sftl_view_mq_ops, 8, + BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING); + if (ret) + goto free_view; + /* Its reads can wait behind a write of the main disk. */ + view->tags.timeout = sftl->tags.timeout; + disk = blk_mq_alloc_disk(&view->tags, &limits, view); + if (IS_ERR(disk)) { + ret = PTR_ERR(disk); + goto free_tags; + } + view->disk = disk; + /* No major and no minors: dynamic device numbers for it and its partitions. */ + disk->fops = &sftl_block_ops; + disk->private_data = view; + snprintf(disk->disk_name, DISK_NAME_LEN, "applesftl%ulwvm", sftl->id); + set_capacity(disk, media >> SECTOR_SHIFT); + set_disk_ro(disk, true); + ret = device_add_disk(&pdev->dev, disk, NULL); + if (ret) + goto put_disk; + sftl->view = view; + dev_info(&pdev->dev, "%s: %u scattered LwVM partitions, read-only\n", + disk->disk_name, view->map.nr_parts); + return; + +put_disk: + put_disk(disk); +free_tags: + blk_mq_free_tag_set(&view->tags); +free_view: + kfree(view); + dev_warn(&pdev->dev, "scattered LwVM partitions not added: %d\n", ret); +} + +/* Before the parent: its reads take the parent's lock, buffer and FTL. */ +static void sftl_remove_view(struct sftl_disk *sftl) +{ + struct sftl_view *view = sftl->view; + + if (!view) + return; + del_gendisk(view->disk); + put_disk(view->disk); + blk_mq_free_tag_set(&view->tags); + kfree(view); + sftl->view = NULL; +} + static int sftl_disk_probe(struct platform_device *pdev) { struct queue_limits limits = { @@ -262,6 +467,7 @@ static int sftl_disk_probe(struct platform_device *pdev) dev_info(&pdev->dev, "%s: %s, %u logical pages, sequence %llu\n", disk->disk_name, sftl->writable ? "read-write" : "read-only", info.disk_pages, info.sequence); + sftl_add_view(pdev, sftl, &info); return 0; put_disk: @@ -287,6 +493,7 @@ static void sftl_disk_remove(struct platform_device *pdev) mutex_lock(&sftl->io_lock); sftl->dead = true; mutex_unlock(&sftl->io_lock); + sftl_remove_view(sftl); /* del_gendisk freezes and drains the queue before the provider is freed. */ del_gendisk(sftl->disk); put_disk(sftl->disk); diff --git a/drivers/mtd/apple-sftl-lwvm-test.c b/drivers/mtd/apple-sftl-lwvm-test.c index cd174d51a18b29..e2daf54459c90e 100644 --- a/drivers/mtd/apple-sftl-lwvm-test.c +++ b/drivers/mtd/apple-sftl-lwvm-test.c @@ -1,5 +1,8 @@ // SPDX-License-Identifier: GPL-2.0-only -/* Synthetic LwVM headers: contiguous, interleaved and partial partitions. */ +/* + * Synthetic LwVM headers: contiguous, interleaved and partial partitions, + * and the view that lists the scattered ones in order. + */ #include #include #include @@ -20,6 +23,8 @@ static const u8 test_signature[16] = { struct lwvm_fixture { u8 *header; struct apple_sftl_lwvm *lwvm; + struct apple_sftl_lwvm *again; /* the view's header, decoded */ + struct apple_sftl_lwvm_view *view; }; static void header_init(u8 *h, u64 media, u32 count) @@ -66,8 +71,12 @@ static int lwvm_init(struct kunit *test) KUNIT_ASSERT_NOT_NULL(test, f); f->header = kunit_kzalloc(test, APPLE_SFTL_LWVM_HEADER_BYTES, GFP_KERNEL); f->lwvm = kunit_kzalloc(test, sizeof(*f->lwvm), GFP_KERNEL); + f->again = kunit_kzalloc(test, sizeof(*f->again), GFP_KERNEL); + f->view = kunit_kzalloc(test, sizeof(*f->view), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, f->header); KUNIT_ASSERT_NOT_NULL(test, f->lwvm); + KUNIT_ASSERT_NOT_NULL(test, f->again); + KUNIT_ASSERT_NOT_NULL(test, f->view); test->priv = f; return 0; } @@ -80,6 +89,15 @@ static u64 map(struct kunit *test, const struct apple_sftl_lwvm_part *part, u64 return disk; } +static u64 view_map(struct kunit *test, const struct apple_sftl_lwvm_view *view, u64 offset) +{ + u64 disk = U64_MAX; + + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view_map(view, offset, &disk), + APPLE_SFTL_LWVM_PAGE_DISK); + return disk; +} + static void lwvm_chunk_size(struct kunit *test) { struct lwvm_fixture *f = test->priv; @@ -283,6 +301,82 @@ static void lwvm_bad_tables(struct kunit *test) KUNIT_EXPECT_EQ(test, map(test, &f->lwvm->parts[0], 16383), 511 * 16384ULL + 16383); } +/* + * Only the scattered partition is listed, from chunk 1 and in order, so the + * partition parser adds it under its own number, GUID and name; the one it + * adds to the disk already is cleared. Every view page reads the right + * disk page. + */ +static void lwvm_view(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + struct apple_sftl_lwvm_view *view = f->view; + struct apple_sftl_lwvm_part *data = &f->again->parts[1]; + static const u8 empty[0x80]; + u64 disk = 0; + + header_init(f->header, TEST_MEDIA, 2); + header_part(f->header, 0, 0, 2 * TEST_CHUNK, "System"); + header_part(f->header, 1, 2 * TEST_CHUNK, 5 * TEST_CHUNK + 512, "Data"); + header_map(f->header, 1, 0, 0); + header_map(f->header, 2, 0, 1); + header_map(f->header, 9, 1, 0); + header_map(f->header, 4, 1, 1); + header_map(f->header, 3, 1, 2); + header_map(f->header, 700, 1, 3); + header_seal(f->header); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->header, TEST_MEDIA, f->lwvm), 0); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_view(f->header, f->lwvm, view), 1); + KUNIT_EXPECT_EQ(test, view->media_bytes, TEST_MEDIA); + + /* The partition parser's checks pass, and Data is now in order. */ + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(view->header, TEST_MEDIA, f->again), 0); + KUNIT_EXPECT_EQ(test, f->again->nr_parts, 2); + KUNIT_EXPECT_EQ(test, f->again->parts[0].error, -EINVAL); + KUNIT_EXPECT_MEMEQ(test, view->header + 0x200, empty, sizeof(empty)); + KUNIT_EXPECT_EQ(test, data->error, 0); + KUNIT_EXPECT_FALSE(test, data->pieced); + KUNIT_EXPECT_STREQ(test, data->name, "Data"); + KUNIT_EXPECT_MEMEQ(test, view->header + 0x280, f->header + 0x280, 0x80); + KUNIT_EXPECT_EQ(test, data->nr_chunks, 4); + KUNIT_EXPECT_EQ(test, map(test, data, 0), 1 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, map(test, data, 3 * TEST_CHUNK + 511), 4 * TEST_CHUNK + 511); + KUNIT_EXPECT_MEMEQ(test, view->header, f->header, 0x2c); + + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view_map(view, 0, &disk), + APPLE_SFTL_LWVM_PAGE_HEADER); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view_map(view, 4095, &disk), + APPLE_SFTL_LWVM_PAGE_HEADER); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view_map(view, 4096, &disk), + APPLE_SFTL_LWVM_PAGE_ZERO); + KUNIT_EXPECT_EQ(test, view_map(test, view, TEST_CHUNK), 9 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, view_map(test, view, 2 * TEST_CHUNK - 1), 10 * TEST_CHUNK - 1); + KUNIT_EXPECT_EQ(test, view_map(test, view, 2 * TEST_CHUNK), 4 * TEST_CHUNK); + KUNIT_EXPECT_EQ(test, view_map(test, view, 3 * TEST_CHUNK + 4096), 3 * TEST_CHUNK + 4096); + KUNIT_EXPECT_EQ(test, view_map(test, view, 4 * TEST_CHUNK + 511), 700 * TEST_CHUNK + 511); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view_map(view, 5 * TEST_CHUNK, &disk), + APPLE_SFTL_LWVM_PAGE_ZERO); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view_map(view, TEST_MEDIA, &disk), + APPLE_SFTL_LWVM_PAGE_ZERO); +} + +/* Nothing scattered, nothing to list; broken partitions are left out too. */ +static void lwvm_view_empty(struct kunit *test) +{ + struct lwvm_fixture *f = test->priv; + + header_init(f->header, TEST_MEDIA, 2); + header_part(f->header, 0, 0, 2 * TEST_CHUNK, "System"); + header_part(f->header, 1, 2 * TEST_CHUNK, 4 * TEST_CHUNK, "Hole"); + header_map(f->header, 1, 0, 0); + header_map(f->header, 2, 0, 1); + header_map(f->header, 9, 1, 1); + header_seal(f->header); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->header, TEST_MEDIA, f->lwvm), 0); + KUNIT_EXPECT_EQ(test, f->lwvm->parts[1].error, -ENODATA); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view(f->header, f->lwvm, f->view), 0); +} + static struct kunit_case lwvm_cases[] = { KUNIT_CASE(lwvm_chunk_size), KUNIT_CASE(lwvm_contiguous), @@ -290,6 +384,8 @@ static struct kunit_case lwvm_cases[] = { KUNIT_CASE(lwvm_partial_last_chunk), KUNIT_CASE(lwvm_broken_partitions), KUNIT_CASE(lwvm_bad_tables), + KUNIT_CASE(lwvm_view), + KUNIT_CASE(lwvm_view_empty), {} }; diff --git a/drivers/mtd/apple-sftl-lwvm.c b/drivers/mtd/apple-sftl-lwvm.c index 2f2c4967ffe665..712c741c309d80 100644 --- a/drivers/mtd/apple-sftl-lwvm.c +++ b/drivers/mtd/apple-sftl-lwvm.c @@ -5,8 +5,10 @@ * 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. * This decoder reads the same header with the same checks and keeps, for - * every partition, the map from partition chunk to disk chunk, so the SFTL - * block frontend can serve a scattered partition read-only through it. + * every partition, the map from partition chunk to disk chunk. From it the + * SFTL block frontend serves a read-only view disk whose header lists only + * the scattered partitions, each in order and back to back, so that the + * partition parser adds them as it adds the others. * * A map entry is the partition index in the top four bits and the partition * chunk in the low twelve; owner 0xf is the header chunk or a free chunk. @@ -46,7 +48,8 @@ unsigned int apple_sftl_lwvm_chunk_shift(u64 media_bytes) } EXPORT_SYMBOL_GPL(apple_sftl_lwvm_chunk_shift); -static bool lwvm_crc_ok(const u8 *header) +/* The CRC of the header with its CRC field read as zero. */ +static u32 lwvm_crc(const u8 *header) { static const u8 zero[4]; u32 crc; @@ -55,7 +58,12 @@ static bool lwvm_crc_ok(const u8 *header) crc = crc32_le(crc, zero, sizeof(zero)); crc = crc32_le(crc, header + LWVM_CRC_OFFSET + 4, APPLE_SFTL_LWVM_HEADER_BYTES - LWVM_CRC_OFFSET - 4); - return ~crc == get_unaligned_le32(header + LWVM_CRC_OFFSET); + return ~crc; +} + +static bool lwvm_crc_ok(const u8 *header) +{ + return lwvm_crc(header) == get_unaligned_le32(header + LWVM_CRC_OFFSET); } static void lwvm_part(const u8 *entry, unsigned int shift, struct apple_sftl_lwvm_part *part) @@ -169,3 +177,62 @@ int apple_sftl_lwvm_map(const struct apple_sftl_lwvm_part *part, u64 offset, u64 return 0; } EXPORT_SYMBOL_GPL(apple_sftl_lwvm_map); + +/* + * Build the view of the pieced partitions from a header that + * apple_sftl_lwvm_decode() accepted into lwvm. Every other entry is + * cleared, so the partition parser skips it without a word and no + * partition is added twice. Returns the number of partitions listed; the + * view is only needed when that is not zero. + */ +u32 apple_sftl_lwvm_view(const u8 *header, const struct apple_sftl_lwvm *lwvm, + struct apple_sftl_lwvm_view *view) +{ + u8 *h = view->header; + unsigned int i, k, next = 1; + + memcpy(h, header, APPLE_SFTL_LWVM_HEADER_BYTES); + memset(view->chunk, 0xff, sizeof(view->chunk)); + view->media_bytes = lwvm->media_bytes; + view->chunk_shift = lwvm->chunk_shift; + view->nr_parts = 0; + for (k = 1; k < APPLE_SFTL_LWVM_CHUNKS; k++) + put_unaligned_le16(LWVM_CHUNK_FREE << 12, h + LWVM_MAP_OFFSET + 2 * k); + + /* Each disk chunk has one owner, so the partitions fit after chunk 0. */ + for (i = 0; i < lwvm->nr_parts; i++) { + const struct apple_sftl_lwvm_part *part = &lwvm->parts[i]; + + if (!part->pieced) { + memset(h + LWVM_PARTS_OFFSET + i * LWVM_PART_BYTES, 0, LWVM_PART_BYTES); + continue; + } + for (k = 0; k < part->nr_chunks; k++, next++) { + put_unaligned_le16(i << 12 | k, h + LWVM_MAP_OFFSET + 2 * next); + view->chunk[next] = part->chunk[k]; + } + view->nr_parts++; + } + put_unaligned_le32(lwvm_crc(h), h + LWVM_CRC_OFFSET); + return view->nr_parts; +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_view); + +/* What a view byte offset holds, and where on the disk if it is stored. */ +enum apple_sftl_lwvm_page apple_sftl_lwvm_view_map(const struct apple_sftl_lwvm_view *view, + u64 offset, u64 *disk_offset) +{ + u64 mask = (1ULL << view->chunk_shift) - 1; + u16 chunk; + + if (offset < APPLE_SFTL_LWVM_HEADER_BYTES) + return APPLE_SFTL_LWVM_PAGE_HEADER; + if (offset >= view->media_bytes) + return APPLE_SFTL_LWVM_PAGE_ZERO; + chunk = view->chunk[offset >> view->chunk_shift]; + if (chunk == APPLE_SFTL_LWVM_NO_CHUNK) + return APPLE_SFTL_LWVM_PAGE_ZERO; + *disk_offset = ((u64)chunk << view->chunk_shift) | (offset & mask); + return APPLE_SFTL_LWVM_PAGE_DISK; +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_view_map); diff --git a/drivers/mtd/apple-sftl-lwvm.h b/drivers/mtd/apple-sftl-lwvm.h index a6df74d76ef35e..11604305ee20dd 100644 --- a/drivers/mtd/apple-sftl-lwvm.h +++ b/drivers/mtd/apple-sftl-lwvm.h @@ -42,4 +42,30 @@ unsigned int apple_sftl_lwvm_chunk_shift(u64 media_bytes); int apple_sftl_lwvm_decode(const u8 *header, u64 media_bytes, struct apple_sftl_lwvm *lwvm); int apple_sftl_lwvm_map(const struct apple_sftl_lwvm_part *part, u64 offset, u64 *disk_offset); +/* + * A disk of the medium's size that lists only the pieced partitions, each + * stored in order and back to back from chunk 1, so that the partition + * parser adds them with their LwVM number, PARTUUID and name. header is + * its first page; chunk[] maps each of its chunks to the disk chunk that + * stores it, and the rest of it reads as zeroes. + */ +struct apple_sftl_lwvm_view { + u64 media_bytes; + u8 chunk_shift; + u32 nr_parts; /* pieced partitions it lists */ + u16 chunk[APPLE_SFTL_LWVM_CHUNKS]; + u8 header[APPLE_SFTL_LWVM_HEADER_BYTES]; +}; + +enum apple_sftl_lwvm_page { + APPLE_SFTL_LWVM_PAGE_DISK, /* *disk_offset on the disk */ + APPLE_SFTL_LWVM_PAGE_HEADER, /* the view's own header */ + APPLE_SFTL_LWVM_PAGE_ZERO, /* no data */ +}; + +u32 apple_sftl_lwvm_view(const u8 *header, const struct apple_sftl_lwvm *lwvm, + struct apple_sftl_lwvm_view *view); +enum apple_sftl_lwvm_page apple_sftl_lwvm_view_map(const struct apple_sftl_lwvm_view *view, + u64 offset, u64 *disk_offset); + #endif From 99c94216f92f0a9461787442cc812be0c293dae6 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Wed, 2 Dec 2026 21:51:29 +0800 Subject: [PATCH 3/3] mtd: apple-sftl: Test the 16 GB iPad mini 1's scattered LwVM layout 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 --- drivers/mtd/apple-sftl-lwvm-test.c | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/drivers/mtd/apple-sftl-lwvm-test.c b/drivers/mtd/apple-sftl-lwvm-test.c index e2daf54459c90e..c66d1564f74479 100644 --- a/drivers/mtd/apple-sftl-lwvm-test.c +++ b/drivers/mtd/apple-sftl-lwvm-test.c @@ -377,6 +377,88 @@ static void lwvm_view_empty(struct kunit *test) KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_view(f->header, f->lwvm, f->view), 0); } +static void header_run(u8 *h, unsigned int owner, unsigned int index, + unsigned int disk_chunk, unsigned int count) +{ + while (count--) + header_map(h, disk_chunk++, owner, index++); +} + +/* + * The 16 GB iPad mini 1 (P105) as iOS 9.3.5 left it after the Linux + * partition was added: System scattered over six runs, Data in order, and + * Linux (3) in three runs, partial last chunk included. The view must + * read the sectors the dm-linear table that booted it named: + * 0 19529728 @ 8978432, 19529728 163840 @ 30048256, 19693568 532840 @ 30474240. + */ +static void lwvm_p105_16gb(struct kunit *test) +{ + static const u8 linux_guid[16] = { + 0x17, 0x46, 0xa4, 0x6a, 0xff, 0xc1, 0x4a, 0x28, + 0x84, 0x4f, 0x0b, 0x16, 0x8b, 0x79, 0x18, 0x5f, + }; + const u64 media = 3906250ULL * 4096, chunk = 1ULL << 24; + struct lwvm_fixture *f = test->priv; + struct apple_sftl_lwvm_part *linux_part = &f->lwvm->parts[2]; + struct apple_sftl_lwvm_part *in_view = &f->again->parts[2]; + u8 *h = f->header; + + header_init(h, media, 3); + header_part(h, 0, 0x6000, 0x87bd9000, "System"); + header_part(h, 1, 0x87bd9000, 0x147bd9000, "Data"); + header_part(h, 2, 0x147bd9000, 0x3b1006000, "Linux"); + memcpy(h + 0x200 + 2 * 0x80 + 16, linux_guid, sizeof(linux_guid)); + header_run(h, 0, 0, 1, 81); + header_run(h, 0, 81, 924, 3); + header_run(h, 0, 84, 913, 4); + header_run(h, 0, 88, 870, 43); + header_run(h, 0, 131, 927, 3); + header_run(h, 0, 134, 922, 2); + header_run(h, 1, 0, 82, 192); + header_run(h, 2, 0, 274, 596); + header_run(h, 2, 596, 917, 5); + header_run(h, 2, 601, 930, 17); + header_seal(h); + + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(h, media, f->lwvm), 0); + KUNIT_EXPECT_EQ(test, f->lwvm->chunks, 953); + KUNIT_EXPECT_TRUE(test, f->lwvm->parts[0].pieced); + KUNIT_EXPECT_EQ(test, f->lwvm->parts[0].nr_chunks, 136); + KUNIT_EXPECT_FALSE(test, f->lwvm->parts[1].pieced); + KUNIT_EXPECT_EQ(test, f->lwvm->parts[1].error, 0); + KUNIT_EXPECT_TRUE(test, linux_part->pieced); + KUNIT_EXPECT_EQ(test, linux_part->nr_chunks, 618); + KUNIT_EXPECT_EQ(test, linux_part->end - linux_part->begin, 20226408ULL * 512); + KUNIT_EXPECT_EQ(test, map(test, linux_part, 0), 8978432ULL * 512); + KUNIT_EXPECT_EQ(test, map(test, linux_part, 19529728ULL * 512 - 1), + (8978432ULL + 19529728) * 512 - 1); + KUNIT_EXPECT_EQ(test, map(test, linux_part, 19529728ULL * 512), 30048256ULL * 512); + KUNIT_EXPECT_EQ(test, map(test, linux_part, 19693568ULL * 512), 30474240ULL * 512); + KUNIT_EXPECT_EQ(test, map(test, linux_part, 20226408ULL * 512 - 1), + (30474240ULL + 532840) * 512 - 1); + + /* System, then Linux from chunk 137: in order, same number and GUID. */ + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_view(h, f->lwvm, f->view), 2); + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_decode(f->view->header, media, f->again), 0); + KUNIT_EXPECT_EQ(test, f->again->parts[0].error, 0); + KUNIT_EXPECT_FALSE(test, f->again->parts[0].pieced); + KUNIT_EXPECT_EQ(test, f->again->parts[0].chunk[0], 1); + KUNIT_EXPECT_EQ(test, f->again->parts[1].error, -EINVAL); + KUNIT_EXPECT_EQ(test, in_view->error, 0); + KUNIT_EXPECT_FALSE(test, in_view->pieced); + KUNIT_EXPECT_STREQ(test, in_view->name, "Linux"); + KUNIT_EXPECT_MEMEQ(test, in_view->guid, linux_guid, sizeof(linux_guid)); + KUNIT_EXPECT_EQ(test, in_view->chunk[0], 137); + KUNIT_EXPECT_EQ(test, view_map(test, f->view, 137 * chunk), 8978432ULL * 512); + KUNIT_EXPECT_EQ(test, view_map(test, f->view, 137 * chunk + 19529728ULL * 512), + 30048256ULL * 512); + KUNIT_EXPECT_EQ(test, view_map(test, f->view, 137 * chunk + 19693568ULL * 512 + 4096), + 30474240ULL * 512 + 4096); + KUNIT_EXPECT_EQ(test, view_map(test, f->view, 137 * chunk + 20226408ULL * 512 - 1), + (30474240ULL + 532840) * 512 - 1); + KUNIT_EXPECT_EQ(test, view_map(test, f->view, 89 * chunk), 870 * chunk); +} + static struct kunit_case lwvm_cases[] = { KUNIT_CASE(lwvm_chunk_size), KUNIT_CASE(lwvm_contiguous), @@ -386,6 +468,7 @@ static struct kunit_case lwvm_cases[] = { KUNIT_CASE(lwvm_bad_tables), KUNIT_CASE(lwvm_view), KUNIT_CASE(lwvm_view_empty), + KUNIT_CASE(lwvm_p105_16gb), {} };