diff --git a/drivers/accel/ane/ane_fw_cold.c b/drivers/accel/ane/ane_fw_cold.c index 2dc8d2239b89c..3be92b4690617 100644 --- a/drivers/accel/ane/ane_fw_cold.c +++ b/drivers/accel/ane/ane_fw_cold.c @@ -73,7 +73,7 @@ struct ane_fw_backend { */ struct ane_fw_surface preload[2], heap_tail; u64 preload_phys[2]; - u8 data_digest[SHA256_DIGEST_SIZE]; + u8 text_digest[SHA256_DIGEST_SIZE], data_digest[SHA256_DIGEST_SIZE]; /* Powered down for system sleep; a (cold or warm) boot is due. */ bool slept; /* @@ -719,7 +719,7 @@ static int ane_fw_reload_segment(struct ane_fw_backend *b, unsigned int i) memunmap(source); dma_wmb(); sha256(s->cpu, s->size, digest); - return memcmp(digest, i ? b->data_digest : b->soc->text_pin, sizeof(digest)) ? + return memcmp(digest, i ? b->data_digest : b->text_digest, sizeof(digest)) ? -EKEYREJECTED : 0; } @@ -1522,6 +1522,54 @@ static int ane_fw_dapf(struct ane_fw_backend *b) return 0; } +/* + * Whether TEXT is the build's firmware: as loaded (text_pin, measured) or as + * code, with the loader-filled environment sections zeroed (text_code_pin, + * from the image). Both digests are returned for the log; code is zero + * without a code pin or with an environment table that does not fit TEXT. + */ +static bool ane_fw_text_pinned(const struct ane_fw_platform *soc, const u8 *text, + size_t size, u8 raw[SHA256_DIGEST_SIZE], + u8 code[SHA256_DIGEST_SIZE]) +{ + static const u8 zero[256]; + struct sha256_ctx hash; + size_t pos = 0; + unsigned int i; + bool ok; + + sha256(text, size, raw); + ok = soc->text_pin && !memcmp(raw, soc->text_pin, SHA256_DIGEST_SIZE); + memset(code, 0, SHA256_DIGEST_SIZE); + if (!soc->text_code_pin || !soc->text_env[0].size) + return ok; + for (i = 0; i < ARRAY_SIZE(soc->text_env) && soc->text_env[i].size; i++) { + const struct ane_fw_text_env *env = &soc->text_env[i]; + + if (env->offset < pos || env->offset > size || env->size > size - env->offset) + return ok; + pos = env->offset + env->size; + } + sha256_init(&hash); + pos = 0; + for (i = 0; i < ARRAY_SIZE(soc->text_env) && soc->text_env[i].size; i++) { + const struct ane_fw_text_env *env = &soc->text_env[i]; + size_t left = env->size; + + sha256_update(&hash, text + pos, env->offset - pos); + while (left) { + size_t n = min(left, sizeof(zero)); + + sha256_update(&hash, zero, n); + left -= n; + } + pos = env->offset + env->size; + } + sha256_update(&hash, text + pos, size - pos); + sha256_final(&hash, code); + return ok || !memcmp(code, soc->text_code_pin, SHA256_DIGEST_SIZE); +} + /* * The loader's preload hand-off. apple,preload-data-sha256 is optional: with * it the private DATA copy must match the loader's digest of what it handed @@ -1561,7 +1609,8 @@ static int ane_fw_preload(struct ane_fw_backend *b) { u64 physical[2], size[2], dva[2], tail, tail_size; struct ane_fw_surface snapshots[2], heap_tail; - u8 digest[SHA256_DIGEST_SIZE], data_pin[SHA256_DIGEST_SIZE]; + u8 digest[SHA256_DIGEST_SIZE], code[SHA256_DIGEST_SIZE]; + u8 data_pin[SHA256_DIGEST_SIZE]; bool have_data_pin; unsigned int i; int ret; @@ -1588,10 +1637,17 @@ static int ane_fw_preload(struct ane_fw_backend *b) memunmap(source); if (ret) return ret; + if (!i) { + /* The copy is what a reload after sleep must give back. */ + if (!ane_fw_text_pinned(b->soc, snapshots[0].cpu, size[0], + b->text_digest, code)) + return -EKEYREJECTED; + continue; + } sha256_init(&hash); sha256_update(&hash, snapshots[i].cpu, size[i]); sha256_final(&hash, digest); - if (memcmp(digest, i ? data_pin : b->soc->text_pin, sizeof(digest))) + if (memcmp(digest, data_pin, sizeof(digest))) return -EKEYREJECTED; } /* Preserve every personalized DATA byte, including the iBoot TLV table. */ @@ -2125,7 +2181,8 @@ static bool ane_fw_handoff_present(struct platform_device *pdev, struct device_node *np = pdev->dev.of_node; struct device_node *chosen __free(device_node) = of_find_node_by_path("/chosen"); struct ane_fw_handoff h = {}; - u8 digest[SHA256_DIGEST_SIZE] = {}; + u8 digest[SHA256_DIGEST_SIZE] = {}, code[SHA256_DIGEST_SIZE] = {}; + bool mapped = false; unsigned int i; if (chosen) @@ -2150,12 +2207,34 @@ static bool ane_fw_handoff_present(struct platform_device *pdev, void *text = memremap(h.phys[0], h.size[0], MEMREMAP_WB); if (text) { - sha256(text, h.size[0], digest); + bool pinned = ane_fw_text_pinned(soc, text, h.size[0], digest, code); + size_t pre = soc->text_env[0].offset; + + /* + * On a code pin miss, the code before the first loader-filled + * section tells a loader write elsewhere from a different build. + */ + if (!pinned && soc->text_code_pin && soc->text_env[0].size && + pre <= h.size[0]) { + u8 head[SHA256_DIGEST_SIZE]; + + sha256(text, pre, head); + dev_info(&pdev->dev, + "H17 ANE: TEXT before 0x%zx sha256 %*phN\n", + pre, (int)sizeof(head), head); + } memunmap(text); + mapped = true; h.text_ok = ane_fw_preload_ranges_valid(soc, h.phys, h.size, h.dva) && - !memcmp(digest, soc->text_pin, sizeof(digest)); + pinned; } } + /* Each boot shows whether the code pin holds, whatever matched. */ + if (mapped && soc->text_code_pin) + dev_info(&pdev->dev, "H17 ANE: firmware %s, TEXT code sha256 %*phN (%s)\n", + soc->build_name ?: "?", (int)sizeof(code), code, + memcmp(code, soc->text_code_pin, sizeof(code)) ? + "not the code pin" : "code pin"); *hp = h; if (ane_fw_cold_handoff_loader_ok(&h, soc)) return true; @@ -2272,6 +2351,8 @@ int ane_fw_staged_init(struct platform_device *pdev, const struct ane_fw_platfor if (ane_backend || !ane_fw_platform_staged(soc) || soc->dapf_count > ANE_FW_DAPF_MAX) return -EOPNOTSUPP; + /* The firmware build the loader preloaded, if the SoC has several. */ + soc = ane_fw_platform_build(np, soc); /* No hand-off (an older loader, a warm boot): leave the engine alone. */ if (!ane_fw_handoff_present(pdev, soc, &handoff)) return -ENODEV; diff --git a/drivers/accel/ane/ane_fw_platform.c b/drivers/accel/ane/ane_fw_platform.c index 219e4560ca0af..39265e26e5299 100644 --- a/drivers/accel/ane/ane_fw_platform.c +++ b/drivers/accel/ane/ane_fw_platform.c @@ -246,10 +246,7 @@ const struct ane_fw_platform ane_t6050_platform = { * The M5 (T8142, J813/J815) ANE: "ane,t8132exclave", ane-type 0x210, * exclave-assigned and preloaded by iBoot. From the T8142 ADT: ANE_SYS at * PMGR +0x2e8 (pmgr device 73, parent AFISOCNI3), ANE_CPU +0xc000 and - * ANE_TDBASE +0xc008 as on T8140; four DAPF records. The macOS 27.0 (26A428) - * stub's iBoot preloads TEXT 0xc0000 and DATA 0x2ac000 at T8140's IOVAs, - * measured on a J815 under aura with TEXT sha256 1212c260...b6d4e3 (the 27.2 - * host's preload is 0xbc000/0x2b0000 at 0x100_000b_c000). + * ANE_TDBASE +0xc008 as on T8140; four DAPF records. * * Measured under aura's hypervisor on a J815 running macOS 27.2.1 (cold * model load, power-up and power-down traced): the PWGATE window at PMGR @@ -259,6 +256,68 @@ const struct ane_fw_platform ane_t6050_platform = { * T8140's order: sub-block, PWGATE request 3, acknowledge 0, CPU, then * ANE_SYS. So sleep and the parked start follow T8140. */ +#define ANE_T8142_PLATFORM \ + .soc = 0x8142, \ + .chip_family = 17, \ + .ane_type = 0x210, \ + .hwx_subtype = 9, \ + .task_generation = 19, \ + .interrupt_count = 2, \ + .owner = ANE_FW_OWNER_EXCLAVE, \ + .reg_names = ane_h17_reg_names, \ + .reg_sizes = ane_h17_reg_sizes, \ + .reg_count = ARRAY_SIZE(ane_h17_reg_names), \ + .wrapper = 0x1600000, \ + .mailbox = 0x1608000, \ + .scratch = 0x1880028, \ + .doorbell = 0x1884000, \ + .irq_status = 0x188c000, \ + .irq_ack = 0x1890000, \ + .stream_mask = BIT(0), \ + .firmware_preloaded = true, \ + .safe_reset = false, \ + /* Exclave mode at boot, cold sleep cycle and parked start. */ \ + .exclave_mode_stop = true, \ + .macos_sleep = true, \ + .sleep_warm = false, \ + .pmgr_sys = 0x2e8, \ + .pmgr_cpu = 0xc000, \ + .pmgr_subblock = 0xc008, \ + .dart_compatible = "apple,t8142-ane-dart", \ + .dart_revision = 0x204, \ + .dapf_count = 4 + +/* + * The macOS 27.2 (26B123) firmware, which 27.2.1 (26B131) ships unchanged: + * the 27.2 host ADT has iBoot preload TEXT 0xbc000 and DATA 0x2b0000, DATA + * at 0x100_000b_c000; both end where 27.0's do. Its TEXT has two sections + * the loader fills: ce_env (0x4000 bytes at 0xb4f80, new in this build, all + * zero in the image but its first byte) and text_env (0x20 at 0xb8f80). The + * code pin is from the image (h17_ane_fw_theia_j73y, RTKit 3514.41.1) with + * both zeroed; with text_env alone the first J815 boot missed it. + */ +static const u8 ane_t8142_code_26b123[] = { + 0x9b, 0x57, 0x48, 0x67, 0x90, 0xff, 0x41, 0x45, + 0x8e, 0x1d, 0x33, 0x46, 0x34, 0xed, 0x36, 0x53, + 0xa4, 0xb4, 0xd3, 0xcd, 0xf4, 0x43, 0xe2, 0x17, + 0x7d, 0x95, 0x6d, 0x11, 0x1d, 0xa7, 0xf4, 0xc2, +}; + +static const struct ane_fw_platform ane_t8142_platform_26b123 = { + ANE_T8142_PLATFORM, + .build_name = "27.2 26B123", + .preload_dva = { 0x10000000000, 0x100000bc000 }, + .preload_size = { 0xbc000, 0x2b0000 }, + .text_code_pin = ane_t8142_code_26b123, + .text_env = { { 0xb4f80, 0x4000 }, { 0xb8f80, 0x20 } }, +}; + +/* + * The macOS 27.0 (26A428) stub's iBoot preloads TEXT 0xc0000 and DATA + * 0x2ac000 at T8140's IOVAs. TEXT as loaded was measured on a J815 under + * aura; it differs from the image's (RTKit 3514.0.15) only by what iBoot + * writes into text_env (0xbc280), which the code pin leaves out. + */ static const u8 ane_t8142_text_26a428[] = { 0x12, 0x12, 0xc2, 0x60, 0x6e, 0xf8, 0xca, 0xce, 0x51, 0xfd, 0x95, 0x1e, 0x60, 0xbc, 0xce, 0x75, @@ -266,39 +325,22 @@ static const u8 ane_t8142_text_26a428[] = { 0xac, 0x3b, 0x58, 0x07, 0x49, 0xd6, 0xb4, 0xe3, }; +static const u8 ane_t8142_code_26a428[] = { + 0xaa, 0x5c, 0x7b, 0xd4, 0xdb, 0x5b, 0x17, 0x25, + 0x5d, 0x4a, 0x16, 0xa5, 0x80, 0x06, 0xca, 0x37, + 0x96, 0x26, 0xf3, 0x91, 0x58, 0x1b, 0x9c, 0x49, + 0x0a, 0xcd, 0x7c, 0x56, 0x6f, 0xda, 0x24, 0x9c, +}; + const struct ane_fw_platform ane_t8142_platform = { - .soc = 0x8142, - .chip_family = 17, - .ane_type = 0x210, - .hwx_subtype = 9, - .task_generation = 19, - .interrupt_count = 2, - .owner = ANE_FW_OWNER_EXCLAVE, - .reg_names = ane_h17_reg_names, - .reg_sizes = ane_h17_reg_sizes, - .reg_count = ARRAY_SIZE(ane_h17_reg_names), - .wrapper = 0x1600000, - .mailbox = 0x1608000, - .scratch = 0x1880028, - .doorbell = 0x1884000, - .irq_status = 0x188c000, - .irq_ack = 0x1890000, + ANE_T8142_PLATFORM, + .build_name = "27.0 26A428", .preload_dva = { 0x10000000000, 0x100000c0000 }, .preload_size = { 0xc0000, 0x2ac000 }, - .stream_mask = BIT(0), - .firmware_preloaded = true, - .safe_reset = false, - /* Exclave mode at boot, cold sleep cycle and parked start, as on T8140. */ - .exclave_mode_stop = true, - .macos_sleep = true, - .sleep_warm = false, - .pmgr_sys = 0x2e8, - .pmgr_cpu = 0xc000, - .pmgr_subblock = 0xc008, - .dart_compatible = "apple,t8142-ane-dart", - .dart_revision = 0x204, - .dapf_count = 4, .text_pin = ane_t8142_text_26a428, + .text_code_pin = ane_t8142_code_26a428, + .text_env = { { 0xbc280, 0x20 } }, + .next_build = &ane_t8142_platform_26b123, }; /* @@ -331,6 +373,30 @@ int ane_fw_preload_range(struct device_node *np, unsigned int index, u64 *phys, } EXPORT_SYMBOL_GPL(ane_fw_preload_range); +/* + * The build whose preload layout the loader handed over: the SoC's own data + * or one of its next_build entries. Anything else (no usable hand-off, an + * unknown layout) keeps the SoC's own data, which then refuses as before. + */ +const struct ane_fw_platform *ane_fw_platform_build(struct device_node *np, + const struct ane_fw_platform *soc) +{ + const struct ane_fw_platform *build; + u64 phys[2], size[2], dva[2]; + + if (!soc->next_build || + of_property_count_u64_elems(np, "apple,firmware-iovas") != 2 || + of_property_read_u64_array(np, "apple,firmware-iovas", dva, 2) || + ane_fw_preload_range(np, 0, &phys[0], &size[0]) || + ane_fw_preload_range(np, 1, &phys[1], &size[1])) + return soc; + for (build = soc; build; build = build->next_build) + if (ane_fw_preload_ranges_valid(build, phys, size, dva)) + return build; + return soc; +} +EXPORT_SYMBOL_GPL(ane_fw_platform_build); + static int ane_fw_preload_check(struct device *dev, const struct ane_fw_platform *soc) { diff --git a/drivers/accel/ane/ane_fw_platform.h b/drivers/accel/ane/ane_fw_platform.h index 6f4372e8ad3c7..f203f426a9184 100644 --- a/drivers/accel/ane/ane_fw_platform.h +++ b/drivers/accel/ane/ane_fw_platform.h @@ -106,6 +106,23 @@ struct ane_fw_platform { const u8 *text_pin; /* H16: an aux-3 window, which the build's XL tunables go to. */ bool has_xl; + /* + * Or the build's code: the TEXT digest with the loader-filled + * environment sections zeroed (__TEXT,text_env and, from 27.2, + * __TEXT,ce_env; ascending, not overlapping, unused ones size 0). The + * loader fills them in (the image has a fixed pattern or zeroes there), + * so unlike text_pin this is computed from the image. + */ + const u8 *text_code_pin; + struct ane_fw_text_env { + u32 offset, size; + } text_env[2]; + /* + * Another firmware build the loader may preload on this SoC, with its + * own preload layout and pins; the handed-over layout selects it. + */ + const struct ane_fw_platform *next_build; + const char *build_name; }; /* Pure range check: the boot loader supplies the physical addresses. */ @@ -164,6 +181,8 @@ void ane_fw_platform_remove(struct platform_device *pdev); int ane_fw_platform_suspend(struct device *dev); int ane_fw_platform_resume(struct device *dev); int ane_fw_preload_range(struct device_node *np, unsigned int index, u64 *phys, u64 *size); +const struct ane_fw_platform *ane_fw_platform_build(struct device_node *np, + const struct ane_fw_platform *soc); int ane_h16_backend_probe(struct platform_device *pdev, const struct ane_fw_platform *soc); void ane_h16_backend_remove(struct platform_device *pdev); int ane_h16_backend_suspend(struct device *dev);