From 44a1bae6dd09a6496924072626581d98d723f16a Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sun, 22 Nov 2026 15:57:54 +0100 Subject: [PATCH 1/2] spi: apple-qspi: admit NVRAM writes by the DT partition and the part A controller became a write candidate only through a row of apple_qspi_boards[]: controller and machine compatible, controller base, the nvram window the device tree had to describe exactly, the JEDEC IDs and the block-protect bits the firmware leaves set. Each new Mac needed a row, and five of the ten rows were assumed rather than checked against an ADT and a probe. Take those facts from the machine instead, and check the device tree's partition against the flash before anything is written to it: - The window is the node's one "nvram" partition, if it is not marked read-only, is a whole number of 64 KiB NVRAM banks from a 4 KiB boundary and lies within three-byte addressing (apple_qspi_window_valid()). - The part is read back before every program or erase and must be in a short part table (W25Q64, Macronix 2B27, W25Q16, GD25UF16E), which gives its size and the block-protect bits its firmware leaves set. The window must end within the part: a three-byte address past it wraps around into the start of the flash. - Before the first program or erase, and latched for the controller: the part's SFDP density must be the size the table bounds the window with, and every bank of the window must start with a v3 NVRAM store header ("3VVN", version 1, a store size within the bank) or be blank throughout, with at least one store. A partition over firmware, a stale one, or one running into padding or an image partway through a bank, admits nothing. - Also at that point, the block-protect bits are decoded, and a bank they reach even in part is dropped from the writable banks. A program or erase there is refused before WREN, so a bank erase cannot stop halfway with the bank's header already gone. On the J700, BP0 covers the top 64 KiB, so the window's last bank [0x1e4000, 0x1f4000) is never written. The bits are decoded against each request as well: one in the protected range is refused before WREN instead of failing its readback and latching read-only. The read-only default and the root opt-in, the opcode, range, page and erase checks, the readback verify and the fault latch are unchanged. Refusals for the part, its size, SFDP, the window and protection are logged, rate limited, with the reason. The boards the table admitted keep their window and part. Any other board whose device tree describes a usable nvram partition is admitted by these checks alone. The rules live in spi-apple-qspi-policy.h, where KUnit covers the window rule, the bank header, the part table, the protected ranges, the banks they leave writable and SFDP parsing. tools/asahi/qspi-nvram-tests.py runs the driver's write path against a flash model with SFDP and NVRAM banks, with changed-guard controls for each new check. Closes: https://github.com/aurora-silicon/linux/issues/2815 Signed-off-by: Thomas Glanzmann --- drivers/spi/spi-apple-qspi-policy-test.c | 182 ++++++++++++++ drivers/spi/spi-apple-qspi-policy.h | 175 +++++++++++++- drivers/spi/spi-apple-qspi.c | 290 ++++++++++++++--------- tools/asahi/qspi-nvram-tests.py | 241 +++++++++++++------ 4 files changed, 699 insertions(+), 189 deletions(-) diff --git a/drivers/spi/spi-apple-qspi-policy-test.c b/drivers/spi/spi-apple-qspi-policy-test.c index 5418c3a4d63ab..5cd22c258ff91 100644 --- a/drivers/spi/spi-apple-qspi-policy-test.c +++ b/drivers/spi/spi-apple-qspi-policy-test.c @@ -5,6 +5,7 @@ */ #include +#include #include "spi-apple-qspi-policy.h" @@ -208,6 +209,181 @@ static void qspi_policy_part_status(struct kunit *test) KUNIT_EXPECT_FALSE(test, apple_qspi_part_status_allows_write(0xc2, 0x20, 0, 0, 0x1c)); } +static void qspi_policy_window(struct kunit *test) +{ + /* The J813 and J700 partitions. */ + KUNIT_EXPECT_TRUE(test, apple_qspi_window_valid(1, 2, 0, 0x700000, 0x100000)); + KUNIT_EXPECT_TRUE(test, apple_qspi_window_valid(1, 2, 0, 0x24000, 0x1d0000)); + /* Two nvram labels, or none (a disabled one is not counted). */ + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(2, 2, 0, 0x700000, 0x100000)); + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(0, 2, 0, 0x700000, 0x100000)); + /* Marked read-only. */ + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 1, 0x700000, 0x100000)); + /* Three reg cells, or none. */ + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 3, 0, 0x700000, 0x100000)); + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 0, 0, 0x700000, 0x100000)); + /* A misaligned start, a part of a bank, an empty partition. */ + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0x700800, 0x100000)); + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0x700000, 0xff000)); + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0x700000, 0)); + /* Past three-byte addressing. */ + KUNIT_EXPECT_TRUE(test, apple_qspi_window_valid(1, 2, 0, 0xf00000, 0x100000)); + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0xf10000, 0x100000)); + KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0xfffff000, 0x10000)); +} + +static void qspi_policy_bank_state(struct kunit *test) +{ + unsigned char h[24]; + + memset(h, 0xff, sizeof(h)); + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 0); + memset(h, 0, sizeof(h)); + memcpy(h, "3VVN", 4); + h[5] = 0x40; /* store size 0x4000 */ + h[14] = 1; + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 1); + h[14] = 2; /* another version */ + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1); + h[14] = 1; + h[5] = 0; + h[6] = 1; /* 0x10000: a full bank */ + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 1); + h[4] = 1; /* past the bank */ + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1); + h[4] = 23; + h[6] = 0; /* smaller than the header */ + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1); + h[4] = 24; + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 1); + h[0] = '2'; + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1); + /* Firmware, not NVRAM, and a half-erased bank. */ + memset(h, 0, sizeof(h)); + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1); + memset(h, 0xff, sizeof(h)); + h[23] = 0xfe; + KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1); +} + +static void qspi_policy_parts(struct kunit *test) +{ + static const unsigned char known[][3] = { + { 0xef, 0x65, 0x17 }, { 0xc2, 0x2b, 0x27 }, + { 0xef, 0x65, 0x15 }, { 0xc8, 0x83, 0x15 }, + }; + static const unsigned int sizes[] = { 0x800000, 0x800000, 0x200000, 0x200000 }; + static const unsigned char bp_ok[] = { 0, 0, 0x1c, 0x1c }; + static const unsigned char unknown[][3] = { + { 0xef, 0x65, 0x16 }, { 0xef, 0x40, 0x17 }, { 0xc2, 0x2b, 0x28 }, + { 0xc8, 0x60, 0x15 }, { 0, 0, 0 }, { 0xff, 0xff, 0xff }, + }; + const struct apple_qspi_part *part; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(known); i++) { + part = apple_qspi_find_part(known[i]); + KUNIT_ASSERT_NOT_NULL(test, part); + KUNIT_EXPECT_EQ(test, part->size, sizes[i]); + KUNIT_EXPECT_EQ(test, part->bp_ok, bp_ok[i]); + } + for (i = 0; i < ARRAY_SIZE(unknown); i++) + KUNIT_EXPECT_NULL(test, apple_qspi_find_part(unknown[i])); + /* The size bounds the window: the J700's fits 2 MiB, the J813's does not. */ + part = apple_qspi_find_part(known[2]); + KUNIT_EXPECT_LE(test, 0x1f4000ULL, (unsigned long long)part->size); + KUNIT_EXPECT_GT(test, 0x800000ULL, (unsigned long long)part->size); +} + +static void qspi_policy_protected(struct kunit *test) +{ + unsigned long long s, e; + + KUNIT_EXPECT_EQ(test, apple_qspi_protected_range(0xef, 0, 0x200000, &s, &e), 0); + /* BP0 on the J700's parts: the top 64 KiB, inside the window's last bank. */ + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x04, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0x1f0000ULL); + KUNIT_EXPECT_EQ(test, e, 0x200000ULL); + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xc8, 0x04, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0x1f0000ULL); + /* BP0+BP2: the top half; BP1+BP2: all of it. */ + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x14, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0x100000ULL); + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x18, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0ULL); + KUNIT_EXPECT_EQ(test, e, 0x200000ULL); + /* Winbond TB: the bottom 64 KiB, below the J700's window. */ + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x24, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0ULL); + KUNIT_EXPECT_EQ(test, e, 0x10000ULL); + /* Winbond SEC: 4 KiB to 32 KiB. */ + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x44, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0x1ff000ULL); + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x54, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0x1f8000ULL); + /* GigaDevice SR1 bit 5 is BP3, not TB (the status policy refuses it). */ + KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xc8, 0x24, 0x200000, &s, &e), 1); + KUNIT_EXPECT_EQ(test, s, 0x1f0000ULL); +} + +static void qspi_policy_unprotected_banks(struct kunit *test) +{ + unsigned long long s, e; + int ret; + + /* Nothing protected inside the window: all of it. */ + ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x10000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 1); + KUNIT_EXPECT_EQ(test, s, 0x24000ULL); + KUNIT_EXPECT_EQ(test, e, 0x1f4000ULL); + ret = apple_qspi_unprotected_banks(0x700000, 0x800000, 0, 0x700000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 1); + KUNIT_EXPECT_EQ(test, s, 0x700000ULL); + /* BP0 on the J700: its last bank [0x1e4000, 0x1f4000) is a quarter covered, and dropped. */ + ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0x1f0000, 0x200000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 1); + KUNIT_EXPECT_EQ(test, s, 0x24000ULL); + KUNIT_EXPECT_EQ(test, e, 0x1e4000ULL); + /* TB with BP0+BP1: the bottom 256 KiB takes the first two banks. */ + ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x40000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 1); + KUNIT_EXPECT_EQ(test, s, 0x44000ULL); + KUNIT_EXPECT_EQ(test, e, 0x1f4000ULL); + /* A range ending on a bank boundary takes exactly those banks. */ + ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x44000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 1); + KUNIT_EXPECT_EQ(test, s, 0x44000ULL); + ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0x1e4000, 0x200000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 1); + KUNIT_EXPECT_EQ(test, e, 0x1e4000ULL); + /* The top half, or all of the part: nothing left. */ + ret = apple_qspi_unprotected_banks(0x700000, 0x800000, 0x400000, 0x800000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 0); + ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x200000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 0); + /* A range inside the window. */ + ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0x100000, 0x110000, &s, &e); + KUNIT_EXPECT_EQ(test, ret, 0); +} + +static void qspi_policy_sfdp(struct kunit *test) +{ + unsigned char h[16] = { 'S', 'F', 'D', 'P', 6, 1, 1, 0xff, + 0x00, 6, 1, 16, 0x30, 0, 0, 0xff }; + + KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_basic_table(h), 0x30L); + h[3] = 'Q'; + KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_basic_table(h), -1L); + h[3] = 'P'; + h[8] = 0x84; /* not the basic table */ + KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_basic_table(h), -1L); + /* 64 Mbit and 16 Mbit as bit counts minus one, and as a power of two. */ + KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x03ffffff), 0x800000ULL); + KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x00ffffff), 0x200000ULL); + KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x80000000 | 26), 0x800000ULL); + KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x80000000 | 1), 0ULL); +} + static struct kunit_case apple_qspi_policy_cases[] = { KUNIT_CASE(qspi_policy_write_enable), KUNIT_CASE(qspi_policy_program), @@ -220,6 +396,12 @@ static struct kunit_case apple_qspi_policy_cases[] = { KUNIT_CASE(qspi_policy_mx_status), KUNIT_CASE(qspi_policy_gd_status), KUNIT_CASE(qspi_policy_part_status), + KUNIT_CASE(qspi_policy_window), + KUNIT_CASE(qspi_policy_bank_state), + KUNIT_CASE(qspi_policy_parts), + KUNIT_CASE(qspi_policy_protected), + KUNIT_CASE(qspi_policy_unprotected_banks), + KUNIT_CASE(qspi_policy_sfdp), {} }; diff --git a/drivers/spi/spi-apple-qspi-policy.h b/drivers/spi/spi-apple-qspi-policy.h index 361ecc689fca7..f0ac13bf978b3 100644 --- a/drivers/spi/spi-apple-qspi-policy.h +++ b/drivers/spi/spi-apple-qspi-policy.h @@ -6,8 +6,8 @@ #define APPLE_QSPI_ERASE_SIZE 4096U /* - * [start, end) is the board's nvram partition. Subtraction avoids overflow; - * addr is not truncated to wire width first. + * [start, end) is the write window, the device tree's nvram partition. + * Subtraction avoids overflow; addr is not truncated to wire width first. */ static inline int apple_qspi_range_allowed(unsigned long long addr, unsigned int len, unsigned long long start, @@ -113,4 +113,175 @@ static inline int apple_qspi_part_status_allows_write(unsigned int mfr, return apple_qspi_status_allows_write(sr1, sr2, sr3, bp_ok); } } + +/* The size of one NVRAM v3 bank ("3VVN" store). */ +#define APPLE_QSPI_NVRAM_BANK 0x10000U + +/* + * Whether an nvram partition can be the write window: the only one with that + * label, one address and one size cell, not marked read-only, a whole number + * of NVRAM banks from a 4 KiB boundary, and within three-byte addressing. + */ +static inline int apple_qspi_window_valid(unsigned int found, + unsigned int reg_cells, int read_only, + unsigned long long start, + unsigned long long size) +{ + return found == 1 && reg_cells == 2 && !read_only && size && + !(start % APPLE_QSPI_ERASE_SIZE) && + !(size % APPLE_QSPI_NVRAM_BANK) && start + size <= 1ULL << 24; +} + +/* + * The first 24 bytes of an NVRAM bank: 1 for a v3 store header ("3VVN", + * version 1, a store size from the header itself up to the bank), 0 for a + * bank that may be erased (the caller checks the rest of it), -1 for + * anything else. The v3 header has no checksum of its own; each variable in + * the store carries a CRC32. + */ +static inline int apple_qspi_nvram_bank_state(const unsigned char h[24]) +{ + unsigned int size = h[4] | h[5] << 8 | h[6] << 16 | (unsigned int)h[7] << 24; + int i; + + for (i = 0; i < 24 && h[i] == 0xff; i++) + ; + if (i == 24) + return 0; + if (h[0] == '3' && h[1] == 'V' && h[2] == 'V' && h[3] == 'N' && + h[14] == 1 && size >= 24 && size <= APPLE_QSPI_NVRAM_BANK) + return 1; + return -1; +} + +/* + * The boot NOR parts whose nvram partition may be written: the JEDEC ID, the + * size, which bounds the window (a three-byte address past it wraps around) + * and must match the part's SFDP density, and the SR1 block-protect bits the + * part's firmware leaves set. Those stay set; apple_qspi_protected_range() + * says what they protect. + */ +struct apple_qspi_part { + unsigned char jedec[3]; + unsigned int size; + unsigned char bp_ok; +}; + +#define APPLE_QSPI_JEDEC_W25Q64 { 0xef, 0x65, 0x17 } +#define APPLE_QSPI_JEDEC_W25Q16 { 0xef, 0x65, 0x15 } +#define APPLE_QSPI_JEDEC_MX_2B27 { 0xc2, 0x2b, 0x27 } +#define APPLE_QSPI_JEDEC_GD25UF16E { 0xc8, 0x83, 0x15 } + +static const struct apple_qspi_part apple_qspi_parts[] = { + /* J613, J713, J813; also on the bus of some J616s */ + { APPLE_QSPI_JEDEC_W25Q64, 0x800000, 0 }, + /* J616s, the ID macOS reports */ + { APPLE_QSPI_JEDEC_MX_2B27, 0x800000, 0 }, + /* J700: iBoot leaves BP0 set, over the top 64 KiB */ + { APPLE_QSPI_JEDEC_W25Q16, 0x200000, 0x1c }, + /* Some J700s; BP0 covers the same range */ + { APPLE_QSPI_JEDEC_GD25UF16E, 0x200000, 0x1c }, +}; + +static inline const struct apple_qspi_part * +apple_qspi_find_part(const unsigned char id[3]) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(apple_qspi_parts); i++) + if (id[0] == apple_qspi_parts[i].jedec[0] && + id[1] == apple_qspi_parts[i].jedec[1] && + id[2] == apple_qspi_parts[i].jedec[2]) + return &apple_qspi_parts[i]; + return NULL; +} + +/* + * What SR1 protects on a part that tolerates BP bits (the 2 MiB parts, whose + * status policy leaves BP0-BP2 and, on Winbond, TB and SEC as found; CMP, and + * BP3/BP4 on GigaDevice, are refused before this). BP0-BP2 of 1 to 5 protect + * 1/32 to 1/2 of the part, or with SEC 4 to 32 KiB; 6 and 7 protect all of it. + * TB puts the range at the bottom instead of the top. Sets [*start, *end) and + * returns 1, or returns 0 when nothing is protected. + */ +static inline int apple_qspi_protected_range(unsigned int mfr, unsigned int sr1, + unsigned long long size, + unsigned long long *start, + unsigned long long *end) +{ + unsigned int bp = (sr1 >> 2) & 7, shift; + int bottom = 0, sec = 0; + unsigned long long len; + + if (!bp) + return 0; + if (mfr != APPLE_QSPI_MFR_GIGADEVICE) { + bottom = !!(sr1 & 0x20); + sec = !!(sr1 & 0x40); + } + if (bp >= 6) { + len = size; + } else if (sec) { + shift = bp - 1 < 3 ? bp - 1 : 3; + len = 0x1000ULL << shift; + } else { + len = (size >> 5) << (bp - 1); + } + *start = bottom ? 0 : size - len; + *end = *start + len; + return 1; +} + +/* + * The banks of the window [start, end) that a protected range [ps, pe) does + * not reach, as [*wstart, *wend): a bank the range only partly covers is + * dropped whole, so an erase of it never stops halfway. The decoded range + * always reaches one end of the part, so it takes a run of banks from one + * end of the window. Returns 0 when no bank is left, or for a range inside + * the window with banks on both sides of it, which no decode produces. + */ +static inline int apple_qspi_unprotected_banks(unsigned long long start, + unsigned long long end, + unsigned long long ps, + unsigned long long pe, + unsigned long long *wstart, + unsigned long long *wend) +{ + unsigned long long bank = APPLE_QSPI_NVRAM_BANK; + + *wstart = start; + *wend = end; + if (pe <= start || ps >= end) + return 1; + if (ps <= start) + *wstart = start + (pe - start + bank - 1) / bank * bank; + else if (pe >= end) + *wend = start + (ps - start) / bank * bank; + else + return 0; + return *wstart < *wend; +} + +/* + * The JEDEC basic flash parameter table from the first 16 bytes of the SFDP + * space: "SFDP", then the first parameter header, which must be the basic + * table (ID 0x00, MSB 0xff). Returns the table's address, or -1. + */ +static inline long apple_qspi_sfdp_basic_table(const unsigned char h[16]) +{ + if (h[0] != 'S' || h[1] != 'F' || h[2] != 'D' || h[3] != 'P' || + h[8] != 0x00 || h[15] != 0xff || h[11] < 2) + return -1; + return h[12] | h[13] << 8 | h[14] << 16; +} + +/* The part size in bytes from the basic table's second DWORD, or 0. */ +static inline unsigned long long apple_qspi_sfdp_size(unsigned int dword2) +{ + unsigned int n = dword2 & 0x7fffffff; + + if (dword2 & 0x80000000) + return n >= 3 && n < 64 ? 1ULL << (n - 3) : 0; + return ((unsigned long long)dword2 + 1) / 8; +} #endif diff --git a/drivers/spi/spi-apple-qspi.c b/drivers/spi/spi-apple-qspi.c index 06e2d356dcbec..10d618dccea9c 100644 --- a/drivers/spi/spi-apple-qspi.c +++ b/drivers/spi/spi-apple-qspi.c @@ -7,10 +7,10 @@ * command FIFO. This driver intentionally implements the PIO spi-mem path * only; that is sufficient for SPI NOR discovery and access. * - * The boot NOR starts read-only. On the qualified boards (J613, J713, - * J616s and the J700) root may opt in at runtime to bounded, verified - * programs and 4 KiB erases inside the nvram partition; everything else - * stays read-only. On T8140 data reads reach the whole NOR. + * The boot NOR starts read-only. Where the device tree describes its nvram + * partition and the flash is a part listed here, root may opt in at runtime + * to bounded, verified programs and 4 KiB erases inside that partition; + * everything else stays read-only. On T8140 data reads reach the whole NOR. */ #include @@ -25,9 +25,11 @@ #include #include #include +#include #include #include #include +#include #include "spi-apple-qspi-policy.h" @@ -97,61 +99,6 @@ struct apple_qspi_soc { bool runtime_pm; }; -/* - * A board whose NVRAM may be written: the controller compatible, the - * machine, the controller aperture, the nvram partition the device tree - * must describe, the boot NOR JEDEC ID, the SR1 block-protect bits its - * firmware leaves set at boot and the JEDEC ID of a second part, if any. - */ -struct apple_qspi_board { - const char *controller; - const char *machine; - resource_size_t base; - u32 nvram_start; - u32 nvram_size; - u8 jedec[3]; - u8 bp_ok; - u8 jedec_alt[3]; -}; - -#define APPLE_QSPI_JEDEC_W25Q64 { 0xef, 0x65, 0x17 } -#define APPLE_QSPI_JEDEC_W25Q16 { 0xef, 0x65, 0x15 } -#define APPLE_QSPI_JEDEC_MX_2B27 { 0xc2, 0x2b, 0x27 } -#define APPLE_QSPI_JEDEC_GD25UF16E { 0xc8, 0x83, 0x15 } - -static const struct apple_qspi_board apple_qspi_boards[] = { - { "apple,t8122-qspi", "apple,j613", 0x2a1118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_W25Q64, 0 }, - { "apple,t8132-qspi", "apple,j713", 0x3ad118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_W25Q64, 0 }, - /* The J715 ADT has the J713's nvram; its NOR part is assumed the same. */ - { "apple,t8132-qspi", "apple,j715", 0x3ad118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_W25Q64, 0 }, - /* - * The J773g ADT has the same nvram; the J604's dump omits it and the - * iMacs have no ADT yet. All are assumed to carry the J713's part. - */ - { "apple,t8132-qspi", "apple,j604", 0x3ad118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_W25Q64, 0 }, - { "apple,t8132-qspi", "apple,j623", 0x3ad118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_W25Q64, 0 }, - { "apple,t8132-qspi", "apple,j624", 0x3ad118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_W25Q64, 0 }, - { "apple,t8132-qspi", "apple,j773g", 0x3ad118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_W25Q64, 0 }, - /* Macronix is the ID macOS reports; a J616s also reads W25Q64 on the bus. */ - { "apple,t6041-qspi", "apple,j616s", 0x429118000ULL, - 0x700000, 0x100000, APPLE_QSPI_JEDEC_MX_2B27, 0, - APPLE_QSPI_JEDEC_W25Q64 }, - /* - * iBoot leaves BP0 set on the J700, over the top 64 KiB. Some J700s - * carry a GigaDevice GD25UF16E, where BP0 covers the same range. - */ - { "apple,t8140-qspi", "apple,j700", 0x385110000ULL, - 0x24000, 0x1d0000, APPLE_QSPI_JEDEC_W25Q16, 0x1c, - APPLE_QSPI_JEDEC_GD25UF16E }, -}; - static DEFINE_MUTEX(apple_qspi_policy_lock); static bool read_only = true; static bool runtime_ready; @@ -189,7 +136,7 @@ static const struct kernel_param_ops apple_qspi_read_only_ops = { .get = apple_qspi_get_read_only, }; module_param_cb(read_only, &apple_qspi_read_only_ops, &read_only, 0644); -MODULE_PARM_DESC(read_only, "Default true; root runtime opt-in for bounded NVRAM writes on qualified boards"); +MODULE_PARM_DESC(read_only, "Default true; root runtime opt-in for bounded NVRAM writes in the nvram partition of a known part"); static bool apple_qspi_opcode_is_read(u8 opcode) { @@ -220,11 +167,17 @@ struct apple_qspi { /* The nvram partition [window_start, window_end). */ u64 window_start; u64 window_end; - /* Expected JEDEC IDs and the SR1 block-protect bits left by firmware. */ - u8 jedec[3]; - u8 jedec_alt[3]; - u8 bp_ok; + /* The device tree describes a usable nvram partition. */ bool nvram_write_candidate; + /* + * The window checked against the flash before its first program or + * erase, then latched: 0 not yet, 1 passed, a negative errno refused. + */ + int qualified; + const char *qualify_denial; + /* Once qualified: the banks of the window no block protection reaches. */ + u64 write_start; + u64 write_end; bool write_fault; void __iomem *base; void __iomem *p; @@ -718,9 +671,125 @@ static int apple_qspi_status(struct spi_mem *mem, u8 mfr, u8 sr[3]) return 0; } -static int apple_qspi_nvram_modify(struct spi_mem *mem, const struct spi_mem_op *op) +static int apple_qspi_read_sfdp(struct spi_mem *mem, u32 addr, u8 *buf, u32 len) +{ + struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x5a, 1), + SPI_MEM_OP_ADDR(3, addr, 1), SPI_MEM_OP_DUMMY(1, 1), + SPI_MEM_OP_DATA_IN(len, buf, 1)); + + return apple_qspi_transfer_op(mem, &op); +} + +/* + * Before the first program or erase, check the window against the flash, + * and latch the result: the part's SFDP density must be the size the part + * table bounds the window with, and every bank of the window must start with + * a v3 NVRAM store header or be erased throughout, with at least one store. + * A device tree partition that is not the NVRAM, or a part of another size, + * admits nothing. Writes are then admitted only in the banks that the block + * protection found in SR1 does not reach, even in part. + */ +static int apple_qspi_qualify(struct spi_mem *mem, struct apple_qspi *qspi, + const struct apple_qspi_part *part, + const char **denial) +{ + unsigned long long ps, pe, ws, we; + u8 head[24], dword[8], sr[3]; + u8 *buf = NULL; + u64 bank, at; + long table; + int state, stores = 0, ret; + + if (qspi->qualified) { + *denial = qspi->qualify_denial; + return qspi->qualified < 0 ? qspi->qualified : 0; + } + ret = apple_qspi_read_sfdp(mem, 0, head, 16); + if (ret) + return ret; + table = apple_qspi_sfdp_basic_table(head); + if (table < 0) { + *denial = "sfdp"; + ret = -ENODEV; + goto latch; + } + ret = apple_qspi_read_sfdp(mem, table, dword, sizeof(dword)); + if (ret) + return ret; + if (apple_qspi_sfdp_size(get_unaligned_le32(&dword[4])) != part->size) { + *denial = "part-size"; + ret = -ENODEV; + goto latch; + } + for (bank = qspi->window_start; bank < qspi->window_end; + bank += APPLE_QSPI_NVRAM_BANK) { + ret = apple_qspi_read_array(mem, bank, head, sizeof(head)); + if (ret) + goto out; + state = apple_qspi_nvram_bank_state(head); + /* Blank where a header would be is not enough: all of it. */ + if (!state && !buf) { + buf = kmalloc(APPLE_QSPI_ERASE_SIZE, GFP_KERNEL); + if (!buf) { + ret = -ENOMEM; + goto out; + } + } + for (at = bank; !state && at < bank + APPLE_QSPI_NVRAM_BANK; + at += APPLE_QSPI_ERASE_SIZE) { + ret = apple_qspi_read_array(mem, at, buf, APPLE_QSPI_ERASE_SIZE); + if (ret) + goto out; + if (memchr_inv(buf, 0xff, APPLE_QSPI_ERASE_SIZE)) + state = -1; + } + if (state < 0) { + *denial = "window"; + ret = -EACCES; + goto latch; + } + stores += state; + } + if (!stores) { + *denial = "window"; + ret = -EACCES; + goto latch; + } + ret = apple_qspi_status(mem, part->jedec[0], sr); + if (ret) + goto out; + ws = qspi->window_start; + we = qspi->window_end; + if (part->bp_ok && + apple_qspi_protected_range(part->jedec[0], sr[0], part->size, &ps, &pe) && + !apple_qspi_unprotected_banks(qspi->window_start, qspi->window_end, + ps, pe, &ws, &we)) { + *denial = "protected"; + ret = -EACCES; + goto latch; + } + qspi->write_start = ws; + qspi->write_end = we; + qspi->qualified = 1; + dev_info(&mem->spi->dev, + "nvram window [%#llx, %#llx): %d stores, writes in [%#llx, %#llx), part %02x%02x%02x\n", + qspi->window_start, qspi->window_end, stores, ws, we, + part->jedec[0], part->jedec[1], part->jedec[2]); + goto out; +latch: + qspi->qualified = ret; + qspi->qualify_denial = *denial; +out: + kfree(buf); + return ret; +} + +static int apple_qspi_nvram_modify(struct spi_mem *mem, const struct spi_mem_op *op, + const char **denial) { struct apple_qspi *qspi = spi_controller_get_devdata(mem->spi->controller); + const struct apple_qspi_part *part; + unsigned long long ps, pe; unsigned long deadline; u8 id[3], before[3], after[3], sr1; u8 *verify; @@ -734,17 +803,37 @@ static int apple_qspi_nvram_modify(struct spi_mem *mem, const struct spi_mem_op ret = apple_qspi_read_register(mem, 0x9f, id, sizeof(id)); if (ret) goto out; - if (memcmp(id, qspi->jedec, sizeof(id)) && - (!qspi->jedec_alt[0] || memcmp(id, qspi->jedec_alt, sizeof(id)))) { + /* A known part, the partition within it, and checked against it. */ + part = apple_qspi_find_part(id); + if (!part || qspi->window_end > part->size) { + *denial = part ? "part-size" : "part"; ret = -ENODEV; goto out; } + ret = apple_qspi_qualify(mem, qspi, part, denial); + if (ret) + goto out; + /* Never into a bank the protection reaches, so none is left half erased. */ + if (op->addr.val < qspi->write_start || op->addr.val + len > qspi->write_end) { + *denial = "protected"; + ret = -EACCES; + goto out; + } ret = apple_qspi_status(mem, id[0], before); if (ret) goto out; /* No unlocking. Conservatively refuse BP/CMP/WPS, BUSY/WEL or suspend. */ if (!apple_qspi_part_status_allows_write(id[0], before[0], before[1], - before[2], qspi->bp_ok)) { + before[2], part->bp_ok)) { + *denial = "status"; + ret = -EACCES; + goto out; + } + /* The flash would ignore this one: refuse it rather than latch a fault. */ + if (part->bp_ok && + apple_qspi_protected_range(id[0], before[0], part->size, &ps, &pe) && + op->addr.val < pe && op->addr.val + len > ps) { + *denial = "protected"; ret = -EACCES; goto out; } @@ -884,9 +973,9 @@ static int apple_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) ret = 0; goto out; } - ret = apple_qspi_nvram_modify(mem, op); + ret = apple_qspi_nvram_modify(mem, op, &denial); out: - if (ret == -EACCES) + if (ret == -EACCES || ret == -ENODEV) dev_warn_ratelimited(&mem->spi->dev, "NVRAM denied: reason=%s opcode=%02x addr=%#llx addr_bytes=%u len=%u width=%u addr_width=%u dummy=%u dir=%u ro=%u candidate=%u fault=%u\n", denial, op->cmd.opcode, (unsigned long long)op->addr.val, @@ -922,35 +1011,19 @@ static const struct spi_controller_mem_ops apple_qspi_mem_ops = { .exec_op = apple_qspi_mem_exec_op, }; -/* Keep write qualification tied to the board and its controller aperture. */ -static const struct apple_qspi_board * -apple_qspi_nvram_write_board(struct device_node *node, resource_size_t base) -{ - const struct apple_qspi_board *board; - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(apple_qspi_boards); i++) { - board = &apple_qspi_boards[i]; - if (of_device_is_compatible(node, board->controller) && - of_machine_is_compatible(board->machine) && - base == board->base) - return board; - } - return NULL; -} - /* - * The one "nvram" partition of the flash on CS0, 4 KiB aligned and within - * three-byte addressing, since the policy only admits three-byte programs - * and erases. Without it, no program or erase is admitted. + * The one "nvram" partition of the flash on CS0 that apple_qspi_window_valid() + * accepts: not read-only, whole NVRAM banks from a 4 KiB boundary, within + * three-byte addressing, since the policy only admits three-byte programs and + * erases. Without it, no program or erase is admitted. */ static void apple_qspi_nvram_window(struct device *dev, struct apple_qspi *qspi) { struct device_node *flash, *parts, *part; const char *label; - u32 reg[2], cells[2], cs; + u32 reg[2] = { 0 }, cells[2], cs; u64 start = 0, end = 0; - int found = 0; + int found = 0, reg_cells = 0, ro = 0; flash = of_get_child_by_name(dev->of_node, "flash"); if (!flash) @@ -973,18 +1046,18 @@ static void apple_qspi_nvram_window(struct device *dev, struct apple_qspi *qspi) strcmp(label, "nvram")) continue; found++; - if (of_property_count_u32_elems(part, "reg") == 2 && - !of_property_read_u32_array(part, "reg", reg, 2)) { - start = reg[0]; - end = start + reg[1]; - } + ro = of_property_read_bool(part, "read-only"); + reg_cells = of_property_count_u32_elems(part, "reg"); + if (reg_cells != 2 || + of_property_read_u32_array(part, "reg", reg, 2)) + memset(reg, 0, sizeof(reg)); } } of_node_put(parts); - if (found != 1 || start >= end || end > 1ULL << 24 || - !IS_ALIGNED(start, APPLE_QSPI_ERASE_SIZE) || - !IS_ALIGNED(end, APPLE_QSPI_ERASE_SIZE)) - start = end = 0; + if (apple_qspi_window_valid(found, reg_cells, ro, reg[0], reg[1])) { + start = reg[0]; + end = start + reg[1]; + } out: qspi->window_start = start; qspi->window_end = end; @@ -997,7 +1070,6 @@ static void apple_qspi_nvram_window(struct device *dev, struct apple_qspi *qspi) static int apple_qspi_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; - const struct apple_qspi_board *board; const struct apple_qspi_soc *soc; struct spi_controller *ctlr; struct apple_qspi *qspi; @@ -1028,15 +1100,11 @@ static int apple_qspi_probe(struct platform_device *pdev) return dev_err_probe(dev, -EINVAL, "QSPIMC aperture is too small\n"); - /* The device tree must describe exactly the board's nvram partition. */ - board = apple_qspi_nvram_write_board(dev->of_node, res->start); - if (board && qspi->window_start == board->nvram_start && - qspi->window_end == (u64)board->nvram_start + board->nvram_size) { - memcpy(qspi->jedec, board->jedec, sizeof(qspi->jedec)); - memcpy(qspi->jedec_alt, board->jedec_alt, sizeof(qspi->jedec_alt)); - qspi->bp_ok = board->bp_ok; - qspi->nvram_write_candidate = true; - } + /* + * The device tree's nvram partition is the write window. Each program + * or erase then checks the part and that the window lies within it. + */ + qspi->nvram_write_candidate = qspi->window_start < qspi->window_end; qspi->base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(qspi->base)) return PTR_ERR(qspi->base); diff --git a/tools/asahi/qspi-nvram-tests.py b/tools/asahi/qspi-nvram-tests.py index 3e377466e9444..a67a44af65025 100644 --- a/tools/asahi/qspi-nvram-tests.py +++ b/tools/asahi/qspi-nvram-tests.py @@ -32,11 +32,8 @@ def function(source, signature): #include #include typedef uint8_t u8; typedef uint32_t u32; typedef uint64_t u64; -typedef uint64_t resource_size_t; -struct device_node { const char *compatible; }; -static const char *machine; -static bool of_device_is_compatible(struct device_node *node,const char *compat){return !strcmp(node->compatible,compat);} -static bool of_machine_is_compatible(const char *compat){return !strcmp(machine,compat);} +#define SZ_2M 0x200000 +#define SZ_8M 0x800000 #define EOPNOTSUPP 95 #define GFP_KERNEL 0 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) @@ -49,6 +46,10 @@ def function(source, signature): #define SPI_MEM_OP_ADDR(n,a,w) { .val=a,.nbytes=n,.buswidth=w } #define SPI_MEM_OP_NO_ADDR {0} #define SPI_MEM_OP_NO_DUMMY {0} +#define SPI_MEM_OP_DUMMY(n,w) { .nbytes=n,.buswidth=w } +#define get_unaligned_le32(p) ((u32)((const u8*)(p))[0]|(u32)((const u8*)(p))[1]<<8|(u32)((const u8*)(p))[2]<<16|(u32)((const u8*)(p))[3]<<24) +static int info_count; +#define dev_info(device,...) (info_count++) #define SPI_MEM_OP_DATA_IN(n,b,w) { .nbytes=n,.buf.in=b,.buswidth=w,.dir=SPI_MEM_DATA_IN } #define SPI_MEM_OP_NO_DATA {0} #define SPI_MEM_OP(c,a,d,v) { .cmd=c,.addr=a,.dummy=d,.data=v } @@ -68,10 +69,12 @@ def function(source, signature): static int param_get_bool(char*b,const struct kernel_param*p){b[0]=*(bool*)p->arg?'Y':'N';return 1;} struct apple_qspi { bool nvram_policy, nvram_write_candidate, write_fault, nvram_window; - u64 window_start, window_end; - u8 jedec[3]; u8 jedec_alt[3]; u8 bp_ok; + int qualified; const char *qualify_denial; + u64 window_start, window_end, write_start, write_end; int irq; }; +/* The flash model admits only the window, which the controls set. */ +static struct apple_qspi q; struct spi_controller; struct device { struct device *parent; struct spi_controller *controller; }; struct spi_controller { struct apple_qspi *q; bool auto_runtime_pm; struct device dev; }; @@ -92,7 +95,18 @@ def function(source, signature): static bool deny_alloc; static void *kmalloc(size_t n,int flags){return deny_alloc?NULL:malloc(n);} #define kfree free +static void *memchr_inv(const void*p,int c,size_t n){const u8*b=p;for(size_t i=0;i>8;sfdp[0x36]=dword2>>16;sfdp[0x37]=dword2>>24; +} +/* A v3 NVRAM store header at the start of a bank. */ +static void store_at(u64 at){memset(flash+at,0,24);memcpy(flash+at,"3VVN",4);flash[at+5]=0x40;flash[at+14]=1;} static unsigned int wire[4096],wire_count,fail_opcode; static bool corrupt_readback,stuck_busy,programmed; static unsigned int pm_refs,busy_calls,busy_wire; @@ -105,6 +119,8 @@ def function(source, signature): if(o->cmd.opcode==fail_opcode)return -EIO; switch(o->cmd.opcode){ case 0x9f:memcpy(o->data.buf.in,jedec,o->data.nbytes);break; + case 0x5a:assert(o->addr.nbytes==3&&o->dummy.nbytes==1&&o->addr.val+o->data.nbytes<=sizeof(sfdp)); + memcpy(o->data.buf.in,sfdp+o->addr.val,o->data.nbytes);sfdp_reads++;break; case 0x05:*(u8*)o->data.buf.in=sr[0]|(stuck_busy&&programmed?1:0);break; case 0x35:assert(jedec[0]!=0xc2);*(u8*)o->data.buf.in=sr[1];break; case 0x15:*(u8*)o->data.buf.in=sr[2];break; @@ -116,10 +132,10 @@ def function(source, signature): case 0x06:assert(!(sr[0]&3));sr[0]|=2;break; case 0x04:sr[0]&=~2;break; case 0x02: - assert(sr[0]&2);assert(o->addr.val>=0x700000&&o->addr.val+o->data.nbytes<=0x800000); + assert(sr[0]&2);assert(o->addr.val>=q.window_start&&o->addr.val+o->data.nbytes<=q.window_end); for(unsigned int i=0;idata.nbytes;i++)flash[o->addr.val+i]&=((const u8*)o->data.buf.out)[i]; sr[0]&=~2;programmed=true;break; - case 0x20:assert(sr[0]&2);assert(o->addr.val>=0x700000&&o->addr.val+4096<=0x800000); + case 0x20:assert(sr[0]&2);assert(o->addr.val>=q.window_start&&o->addr.val+4096<=q.window_end); memset(flash+o->addr.val,0xff,4096);sr[0]&=~2;programmed=true;break; default:assert(!"forbidden opcode reached the wire"); }return 0; @@ -134,13 +150,15 @@ def function(source, signature): static struct spi_mem mem={&spi}; static struct kernel_param parameter={&read_only}; static void reset(void){ - memset(flash,0xff,sizeof(flash));memset(sr,0,sizeof(sr));q=(struct apple_qspi){.nvram_policy=true,.nvram_write_candidate=true,.window_start=0x700000,.window_end=0x800000,.jedec={0xef,0x65,0x17}}; + memset(flash,0xff,sizeof(flash));memset(sr,0,sizeof(sr));q=(struct apple_qspi){.nvram_policy=true,.nvram_write_candidate=true,.window_start=0x700000,.window_end=0x800000,.write_start=0x700000,.write_end=0x800000,.qualified=1};sfdp_set(0x03ffffff);sfdp_reads=0;info_count=0; read_only=true;runtime_ready=false;write_fault_latched=false;wire_count=0;fail_opcode=0;spi.cs=0; corrupt_readback=false;stuck_busy=false;programmed=false;deny_alloc=false;jiffies=0; jedec[0]=0xef;jedec[1]=0x65;jedec[2]=0x17; ctl.auto_runtime_pm=false;ctl.dev.parent=&parent; pm_refs=busy_calls=busy_wire=0; } +/* A window already qualified with nothing protected, unless a test clears q.qualified. */ +static void window(u64 start,u64 end){q.window_start=q.write_start=start;q.window_end=q.write_end=end;} static struct spi_mem_op program(u64 addr,unsigned int n,const u8*b){ struct spi_mem_op o=SPI_MEM_OP(SPI_MEM_OP_CMD(0x02,1),SPI_MEM_OP_ADDR(3,addr,1),SPI_MEM_OP_NO_DUMMY,SPI_MEM_OP_NO_DATA); o.data.nbytes=n;o.data.dir=SPI_MEM_DATA_OUT;o.data.buswidth=1;o.data.buf.out=b;return o; @@ -149,49 +167,48 @@ def function(source, signature): static bool sent(unsigned int o){for(unsigned int i=0;isize==sizes[i]&&part->bp_ok==tolerated[i]); } - const struct apple_qspi_board *qb; - struct device_node t6041={"apple,t6041-qspi"},t8140={"apple,t8140-qspi"}; - machine="apple,j616s";qb=apple_qspi_nvram_write_board(&t6041,0x429118000ULL); - assert(qb&&qb->nvram_start==0x700000&&qb->nvram_size==0x100000&&qb->jedec[0]==0xc2&&qb->jedec[1]==0x2b&&qb->jedec[2]==0x27&&!qb->bp_ok); - assert(qb->jedec_alt[0]==0xef&&qb->jedec_alt[1]==0x65&&qb->jedec_alt[2]==0x17); - machine="apple,j700";qb=apple_qspi_nvram_write_board(&t8140,0x385110000ULL); - assert(qb&&qb->nvram_start==0x24000&&qb->nvram_size==0x1d0000&&qb->jedec[2]==0x15&&qb->bp_ok==0x1c); - assert(qb->jedec_alt[0]==0xc8&&qb->jedec_alt[1]==0x83&&qb->jedec_alt[2]==0x15); - puts("PASS J613/J713/J700/J616s board, controller and full-width aperture qualification (140 combinations)"); + const u8 unknown[][3]={{0xef,0x65,0x16},{0xef,0x65,0x18},{0xc2,0x2b,0x28},{0xc8,0x60,0x15},{0xef,0x40,0x17},{0,0,0},{0xff,0xff,0xff}}; + for(unsigned int i=0;icontroller) &&', ''), - ('board', 'of_machine_is_compatible(board->machine) &&', ''), - ('aperture', 'base == board->base', 'base != 0'), - ('j616s-aperture', '"apple,j616s", 0x429118000ULL', '"apple,j616s", 0x429118800ULL'), - ('j616s-partition', '"apple,j616s", 0x429118000ULL,\n\t 0x700000', '"apple,j616s", 0x429118000ULL,\n\t 0x680000'), + ('part-id', 'return &apple_qspi_parts[i];\n\treturn NULL;', 'return &apple_qspi_parts[i];\n\treturn &apple_qspi_parts[0];'), + ('part-size', '!part || qspi->window_end > part->size', '!part'), + ('part-bp', 'before[2], part->bp_ok)', 'before[2], 0x1c)'), + ('w25q16-size', '{ APPLE_QSPI_JEDEC_W25Q16, 0x200000, 0x1c }', '{ APPLE_QSPI_JEDEC_W25Q16, 0x800000, 0x1c }'), + ('protected', 'op->addr.val < pe && op->addr.val + len > ps', '0'), + ('bank-invalid', 'if (state < 0) {', 'if (0) {'), + ('bank-none', 'if (!stores) {', 'if (stores < 0) {'), + ('bank-latch', 'qspi->qualified = ret;', '(void)0;'), + ('bank-blank', 'if (memchr_inv(buf, 0xff, APPLE_QSPI_ERASE_SIZE))', 'if (0)'), + ('protected-bank', 'op->addr.val < qspi->write_start || op->addr.val + len > qspi->write_end', '0'), + ('protected-qualify', '!apple_qspi_unprotected_banks(', '0 && !apple_qspi_unprotected_banks('), + ('sfdp-size', '!= part->size) {', '!= part->size && 0) {'), + ('window-read-only', '!read_only && size &&', 'size &&'), + ('window-bank', '!(size % APPLE_QSPI_NVRAM_BANK) &&', ''), ('range', 'addr >= start &&', 'addr >= 1 &&'), ('bp-ok', '!(sr1 & 0x1c & ~bp_ok)', '1'), ('mx-bp3', '!(sr & 0x3c & ~bp_ok)', '!(sr & 0x1c & ~bp_ok)'), ('mx-eqio', 'opcode = macronix;', 'opcode = winbond;'), ('mx-eqio-read', '!ret && id[0] == APPLE_QSPI_MFR_MACRONIX', '0'), - ('jedec-alt', 'memcmp(id, qspi->jedec_alt, sizeof(id))', '1'), - ('jedec-alt-empty', '!qspi->jedec_alt[0] || ', ''), ('gd-dispatch', 'return apple_qspi_gd_status_allows_write(sr1, sr2, bp_ok);', 'return apple_qspi_status_allows_write(sr1, sr2, sr3, bp_ok);'), ('gd-bp34', '!(sr1 & 0x7c & ~bp_ok)', '!(sr1 & 0x1c & ~bp_ok)'), From 87650bc2e3106bc2739080df8ae3c9a9ea45f65c Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Sun, 22 Nov 2026 19:16:21 +0100 Subject: [PATCH 2/2] spi: apple-qspi: cover the J813's NOR status in KUnit The J813 (MacBook Air 13-inch, M5) reads a Winbond W25Q64 (ef6517) with QE set and the SR3 drive strength at its default, and no block protection: flash_status: jedec=ef6517 sr1=00 sr2=02 sr3=60 Since #2817 the J813 is a write candidate through its device tree's nvram partition (0x700000, 1 MiB, the ADT anvram) and its probed part, with no board entry. Check in KUnit that these status registers pass the write policy for a part that tolerates no block-protect bits. Signed-off-by: Thomas Glanzmann --- drivers/spi/spi-apple-qspi-policy-test.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/spi/spi-apple-qspi-policy-test.c b/drivers/spi/spi-apple-qspi-policy-test.c index 5cd22c258ff91..68b7de086be2f 100644 --- a/drivers/spi/spi-apple-qspi-policy-test.c +++ b/drivers/spi/spi-apple-qspi-policy-test.c @@ -152,6 +152,8 @@ static void qspi_policy_status(struct kunit *test) /* Block protect only where the board allows it. */ KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0x04, 0, 0, 0)); KUNIT_EXPECT_TRUE(test, apple_qspi_status_allows_write(0x04, 0x02, 0, 0x1c)); + /* The J813 W25Q64: QE and the SR3 drive strength, no block protection. */ + KUNIT_EXPECT_TRUE(test, apple_qspi_status_allows_write(0x00, 0x02, 0x60, 0)); /* CMP, SUS and WPS are refused everywhere. */ KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0, 0x40, 0, 0x1c)); KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0, 0x80, 0, 0x1c));