From 26091a9e52b01467311efe9446b66da3a9cbb1e0 Mon Sep 17 00:00:00 2001 From: Cody Ho Date: Wed, 2 Dec 2026 19:44:28 -0800 Subject: [PATCH 1/3] adt: add exact-length property read and phandle path lookup helpers Add adt_getprop_exact() for bounds-safe fixed-size ADT property reads and adt_find_phandle_path() to resolve an AAPL,phandle under a subtree while populating the trace array for adt_get_reg() and its path string. Signed-off-by: Cody Ho Assisted-By: LLM --- src/adt.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/adt.h | 4 +++ 2 files changed, 93 insertions(+) diff --git a/src/adt.c b/src/adt.c index a20a7a0c..164019c2 100644 --- a/src/adt.c +++ b/src/adt.c @@ -1,6 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ +#include + #include "adt.h" +#include "utils.h" #include "xnuboot.h" /* This API is designed to match libfdt's read-only API */ @@ -9,3 +12,89 @@ u32 adt_get_size(void) { return cur_boot_args.devtree_size; } + +bool adt_getprop_exact(const void *dt, int nodeoffset, const char *name, void *out, + size_t expected_len) +{ + u32 len = 0; + const void *p = adt_getprop(dt, nodeoffset, name, &len); + if (!p || len != expected_len) + return false; + memcpy(out, p, expected_len); + return true; +} + +static int adt_find_phandle_rec(const void *dt, int parent, u32 target, int *adt_path, size_t depth, + size_t max_depth, char *path_buf, size_t path_buf_size, + size_t prefix_len) +{ + if (depth + 1 >= max_depth) + return -1; + + int child = parent; + ADT_FOREACH_CHILD(dt, child) + { + const char *name = adt_get_name(dt, child); + size_t next_len = prefix_len; + if (path_buf && path_buf_size > 0) { + if (!name) + continue; + int n = snprintf(path_buf + prefix_len, path_buf_size - prefix_len, "/%s", name); + if (n < 0 || prefix_len + (size_t)n >= path_buf_size) + continue; + next_len = prefix_len + (size_t)n; + } + + adt_path[depth] = child; + adt_path[depth + 1] = 0; + + u32 phandle = 0; + if (adt_getprop_exact(dt, child, "AAPL,phandle", &phandle, sizeof(phandle)) && + phandle == target) + return child; + + int found = adt_find_phandle_rec(dt, child, target, adt_path, depth + 1, max_depth, + path_buf, path_buf_size, next_len); + if (found >= 0) + return found; + } + + adt_path[depth] = 0; + if (path_buf && prefix_len < path_buf_size) + path_buf[prefix_len] = '\0'; + return -1; +} + +int adt_find_phandle_path(const void *dt, const char *root_path, u32 phandle, int *adt_path, + size_t max_depth, char *path_buf, size_t path_buf_size) +{ + if (!root_path || !adt_path || max_depth < 3) + return -1; + + int trace[8] = {0}; + int root = adt_path_offset_trace(dt, root_path, trace); + if (root < 0) + return -1; + + size_t depth = 0; + while (depth < ARRAY_SIZE(trace) && trace[depth] != 0) { + if (depth + 1 >= max_depth) + return -1; + adt_path[depth] = trace[depth]; + depth++; + } + adt_path[depth] = 0; + + size_t prefix_len = 0; + if (path_buf && path_buf_size > 0) { + int n = snprintf(path_buf, path_buf_size, "%s", root_path); + if (n < 0 || (size_t)n >= path_buf_size) + return -1; + prefix_len = (size_t)n; + while (prefix_len > 0 && path_buf[prefix_len - 1] == '/') + prefix_len--; + } + + return adt_find_phandle_rec(dt, root, phandle, adt_path, depth, max_depth, path_buf, + path_buf_size, prefix_len); +} diff --git a/src/adt.h b/src/adt.h index fed06883..fb1e6a41 100644 --- a/src/adt.h +++ b/src/adt.h @@ -51,6 +51,10 @@ const void *adt_getprop_by_offset(const void *adt, int offset, const char **name const void *adt_getprop(const void *adt, int nodeoffset, const char *name, u32 *lenp); int adt_setprop(void *adt, int nodeoffset, const char *name, void *value, size_t len); int adt_getprop_copy(const void *adt, int nodeoffset, const char *name, void *out, size_t len); +bool adt_getprop_exact(const void *adt, int nodeoffset, const char *name, void *out, + size_t expected_len); +int adt_find_phandle_path(const void *adt, const char *root_path, u32 phandle, int *adt_path, + size_t max_depth, char *path_buf, size_t path_buf_size); #define ADT_GETPROP(adt, nodeoffset, name, val) \ adt_getprop_copy(adt, nodeoffset, name, (val), sizeof(*(val))) From 625aa0b860832acf9102e16015264e7a363c4909 Mon Sep 17 00:00:00 2001 From: Cody Ho Date: Wed, 2 Dec 2026 19:58:08 -0800 Subject: [PATCH 2/3] gpio: add Apple GPIO pin sampling helper Add dedicated Apple GPIO register definitions and helpers (gpio_sample_pin() and gpio_adt_sample_fn()) to temporarily sample an input pin described by an ADT GPIO function property and restore its original pin configuration. Only invoke pmgr_adt_power_enable() when the GPIO controller actually declares clock-gates (always-on gpio0 nodes omit clock-gates). Signed-off-by: Cody Ho Assisted-By: LLM --- Makefile | 1 + src/gpio.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gpio.h | 18 +++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/gpio.c create mode 100644 src/gpio.h diff --git a/Makefile b/Makefile index 92a17d66..9eb442c9 100644 --- a/Makefile +++ b/Makefile @@ -148,6 +148,7 @@ OBJECTS := \ fb.o font.o font_retina.o \ firmware.o \ gapf.o \ + gpio.o \ gxf.o gxf_asm.o \ heapblock.o \ hv.o hv_vm.o hv_exc.o hv_sptm.o \ diff --git a/src/gpio.c b/src/gpio.c new file mode 100644 index 00000000..79f0f72a --- /dev/null +++ b/src/gpio.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "gpio.h" +#include "adt.h" +#include "pmgr.h" +#include "utils.h" + +#define APPLE_GPIO_DATA BIT(0) +#define APPLE_GPIO_MODE_MASK (7U << 1) +#define APPLE_GPIO_MODE_IN_OFF (7U << 1) +#define APPLE_GPIO_PERIPH_MASK (3U << 5) +#define APPLE_GPIO_INPUT_ENABLE BIT(9) +#define APPLE_GPIO_LOCK BIT(21) + +int gpio_sample_pin(u64 base, u64 size, u32 pin, u32 flags) +{ + if ((u64)pin >= size / 4) + return -1; + + u64 reg = base + (u64)pin * 4; + u32 orig = read32(reg); + u32 cfg = (orig & ~(APPLE_GPIO_MODE_MASK | APPLE_GPIO_PERIPH_MASK)) | APPLE_GPIO_MODE_IN_OFF | + APPLE_GPIO_INPUT_ENABLE; + bool modified = false; + + if (cfg != orig && !(orig & APPLE_GPIO_LOCK)) { + write32(reg, cfg); + (void)read32(reg); /* MMIO write barrier before settle delay */ + udelay(10); + modified = true; + } + + int val = !!(read32(reg) & APPLE_GPIO_DATA); + if (flags & 1) + val ^= 1; + + if (modified) { + write32(reg, orig); + (void)read32(reg); + } + + return val; +} + +int gpio_adt_sample_fn(const struct adt_gpio_fn *fn) +{ + int adt_path[8] = {0}; + char pmgr_path[128]; + + int node = adt_find_phandle_path(adt, "/arm-io", fn->phandle, adt_path, ARRAY_SIZE(adt_path), + pmgr_path, sizeof(pmgr_path)); + if (node < 0) + return -1; + + u32 npins = 0; + if (adt_getprop_exact(adt, node, "#gpio-pins", &npins, sizeof(npins)) && fn->pin >= npins) + return -1; + + u64 base = 0, size = 0; + if (adt_get_reg(adt, adt_path, "reg", 0, &base, &size) < 0) + return -1; + + if (adt_getprop(adt, node, "clock-gates", NULL) && pmgr_adt_power_enable(pmgr_path) < 0) + return -1; + + return gpio_sample_pin(base, size, fn->pin, fn->flags); +} diff --git a/src/gpio.h b/src/gpio.h new file mode 100644 index 00000000..43bfa96d --- /dev/null +++ b/src/gpio.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef GPIO_H +#define GPIO_H + +#include "types.h" + +struct adt_gpio_fn { + u32 phandle; + u32 fourcc; + u32 pin; + u32 flags; +} PACKED; + +int gpio_sample_pin(u64 base, u64 size, u32 pin, u32 flags); +int gpio_adt_sample_fn(const struct adt_gpio_fn *fn); + +#endif From 8d00244f7c63ff689828293e4dbbfe547674fe6f Mon Sep 17 00:00:00 2001 From: Cody Ho Date: Wed, 2 Dec 2026 19:58:08 -0800 Subject: [PATCH 3/3] kboot_audio: forward Apple audio calibration and speaker identity Move audio /chosen DT forwarding into dedicated kboot_audio.{c,h} (matching kboot_atc.{c,h}) and keep kboot.c limited to calling kboot_setup_audio(dt). Forwards apple,acoustic-id, apple,speaker-thiele-small (spts), apple,speaker-cpms-budget-*, apple,speaker-config, and GPIO-strapped apple,speaker-id across all Apple Silicon Macs (including M1 audio-tas5770L-* speaker nodes). Signed-off-by: Cody Ho Assisted-By: LLM --- Makefile | 2 +- src/kboot.c | 3 + src/kboot_audio.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++ src/kboot_audio.h | 8 +++ 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 src/kboot_audio.c create mode 100644 src/kboot_audio.h diff --git a/Makefile b/Makefile index 9eb442c9..ad7befcb 100644 --- a/Makefile +++ b/Makefile @@ -160,7 +160,7 @@ OBJECTS := \ iodev.o \ iova.o \ isp.o \ - kboot.o kboot_atc.o kboot_utils.o \ + kboot.o kboot_atc.o kboot_audio.o kboot_utils.o \ kboot_t6020_compat.o \ main.o \ mitigations.o \ diff --git a/src/kboot.c b/src/kboot.c index 838bd2b5..1248456c 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -15,6 +15,7 @@ #include "iodev.h" #include "isp.h" #include "kboot_atc.h" +#include "kboot_audio.h" #include "kboot_utils.h" #include "malloc.h" #include "mcc.h" @@ -3165,6 +3166,8 @@ int kboot_prepare_dt(void *fdt) return -1; if (dt_set_ipd()) return -1; + if (kboot_setup_audio(dt)) + return -1; if (dt_disable_missing_devs("usb-drd", "usb@", 8)) return -1; if (dt_disable_missing_devs("i2c", "i2c@", 8)) diff --git a/src/kboot_audio.c b/src/kboot_audio.c new file mode 100644 index 00000000..bfc21e28 --- /dev/null +++ b/src/kboot_audio.c @@ -0,0 +1,173 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "kboot_audio.h" +#include "adt.h" +#include "gpio.h" +#include "utils.h" + +#include "libfdt/libfdt.h" + +#define bail(...) \ + do { \ + printf(__VA_ARGS__); \ + return -1; \ + } while (0) + +static bool spts_blob_valid(const u8 *spts, u32 len) +{ + if (!spts || len < 6 + 22 || len > 512 || ((len - 6) % 22 != 0)) + return false; + + bool nonzero = false; + u16 sum = 0; + for (u32 i = 0; i < len - 2; i++) { + if (spts[i] != 0) + nonzero = true; + sum = (u16)(sum + spts[i]); + } + if (!nonzero) + return false; + + u16 unk1 = (u16)spts[2] | ((u16)spts[3] << 8); + u32 n_recs = (len - 6) / 22; + if ((unk1 & 0xff) != 22 || (u32)(unk1 >> 8) != n_recs) + return false; + + u16 ck = (u16)spts[len - 2] | ((u16)spts[len - 1] << 8); + return (u16)(sum + ck) == 0; +} + +static int dt_set_product_audio(void *dt, int chosen) +{ + int pa = adt_path_offset(adt, "/product/audio"); + if (pa < 0) + return 0; + + u32 acoustic_id = 0; + if (adt_getprop_exact(adt, pa, "acoustic-id", &acoustic_id, sizeof(acoustic_id)) && + acoustic_id != 0) { + if (fdt_setprop_u32(dt, chosen, "apple,acoustic-id", acoustic_id)) + bail("FDT: couldn't set apple,acoustic-id\n"); + printf("FDT: apple,acoustic-id = %u\n", acoustic_id); + } + + u32 spts_len = 0; + const u8 *spts = adt_getprop(adt, pa, "speaker-thiele-small", &spts_len); + if (spts && spts_len > 0) { + if (spts_blob_valid(spts, spts_len)) { + if (fdt_setprop(dt, chosen, "apple,speaker-thiele-small", spts, spts_len)) + bail("FDT: couldn't set apple,speaker-thiele-small\n"); + printf("FDT: forwarded apple,speaker-thiele-small (%u bytes)\n", spts_len); + } else { + printf("ADT: speaker-thiele-small invalid/uncalibrated (%u bytes); skipping\n", + spts_len); + } + } + + static const struct { + const char *adt_name; + const char *fdt_name; + } cpms_props[] = { + {"speaker-cpms-bgd_inst", "apple,speaker-cpms-budget-inst"}, + {"speaker-cpms-bgd_100ms", "apple,speaker-cpms-budget-100ms"}, + {"speaker-cpms-bgd_1s", "apple,speaker-cpms-budget-1s"}, + }; + for (size_t i = 0; i < ARRAY_SIZE(cpms_props); i++) { + u32 pair[2]; + if (adt_getprop_exact(adt, pa, cpms_props[i].adt_name, pair, sizeof(pair)) && + (pair[0] != ~0U || pair[1] != ~0U)) { + fdt32_t fdt_pair[2] = {cpu_to_fdt32(pair[0]), cpu_to_fdt32(pair[1])}; + if (fdt_setprop(dt, chosen, cpms_props[i].fdt_name, fdt_pair, sizeof(fdt_pair))) + bail("FDT: couldn't set %s\n", cpms_props[i].fdt_name); + } + } + + return 0; +} + +static int dt_set_speaker_id(void *dt, int chosen, int spk) +{ + u32 find_sid = 0; + if (!adt_getprop_exact(adt, spk, "find-speaker-id", &find_sid, sizeof(find_sid)) || + find_sid != 1) + return 0; + + struct adt_gpio_fn fn0, fn1; + if (!adt_getprop_exact(adt, spk, "function-speaker-id0", &fn0, sizeof(fn0)) || + !adt_getprop_exact(adt, spk, "function-speaker-id1", &fn1, sizeof(fn1))) + return 0; + + int b0 = gpio_adt_sample_fn(&fn0); + int b1 = gpio_adt_sample_fn(&fn1); + if (b0 < 0 || b1 < 0) + return 0; + + u32 sid = (u32)(b0 | (b1 << 1)); + u32 vlen = 0; + const u32 *valid = adt_getprop(adt, spk, "valid-speaker-ids", &vlen); + bool ok = !valid || vlen < 4; + if (valid && vlen >= 4) { + for (u32 j = 0; j < vlen / 4; j++) { + if (valid[j] == sid) { + ok = true; + break; + } + } + } + + if (ok) { + if (fdt_setprop_u32(dt, chosen, "apple,speaker-id", sid)) + bail("FDT: couldn't set apple,speaker-id\n"); + printf("FDT: apple,speaker-id = %u\n", sid); + } + + return 0; +} + +static int dt_set_speaker_info(void *dt, int chosen) +{ + for (int bus = 0; bus < 8; bus++) { + char path[32]; + snprintf(path, sizeof(path), "/arm-io/i2c%d", bus); + int i2c = adt_path_offset(adt, path); + if (i2c < 0) + continue; + + int spk = i2c; + ADT_FOREACH_CHILD(adt, spk) + { + u32 cfg_len = 0; + const u32 *cfg = adt_getprop(adt, spk, "speaker-config", &cfg_len); + bool has_sid = adt_getprop(adt, spk, "find-speaker-id", NULL) != NULL; + if (!cfg && !has_sid) + continue; + + if (cfg && cfg_len >= 4 && cfg_len <= 64 && (cfg_len % 4 == 0)) { + fdt32_t fdt_cfg[16]; + u32 n_cells = cfg_len / 4; + for (u32 j = 0; j < n_cells; j++) + fdt_cfg[j] = cpu_to_fdt32(cfg[j]); + if (fdt_setprop(dt, chosen, "apple,speaker-config", fdt_cfg, cfg_len)) + bail("FDT: couldn't set apple,speaker-config\n"); + } + + return dt_set_speaker_id(dt, chosen, spk); + } + } + + return 0; +} + +int kboot_setup_audio(void *dt) +{ + int chosen = fdt_path_offset(dt, "/chosen"); + if (chosen < 0) + bail("FDT: /chosen node not found\n"); + + if (dt_set_product_audio(dt, chosen)) + return -1; + if (dt_set_speaker_info(dt, chosen)) + return -1; + + return 0; +} diff --git a/src/kboot_audio.h b/src/kboot_audio.h new file mode 100644 index 00000000..fd37c5b9 --- /dev/null +++ b/src/kboot_audio.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef KBOOT_AUDIO_H +#define KBOOT_AUDIO_H + +int kboot_setup_audio(void *dt); + +#endif