Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ OBJECTS := \
isp.o \
j873.o \
kboot.o kboot_atc.o kboot_audio.o kboot_mte.o kboot_gpu_t8122.o kboot_gpu_t8122_power.o kboot_pmp_t8122.o \
kboot_t6020_compat.o \
kboot_t6020_compat.o kboot_t8132.o \
kboot_display_handoff.o kboot_display_facts.o kboot_gpu26.o kboot_gpu_t6031.o kboot_t6031.o \
main.o boot_options.o \
mitigations.o \
Expand Down
19 changes: 16 additions & 3 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -9019,16 +9019,23 @@ static int dt_reserve_t8132_aop_windows(const char *path)
* DAPF, then reserve both segments at the IOVAs the firmware uses (their remap
* addresses: EC0p for __ETEXT, the physical address for __OS_LOG) and the
* DAPF windows. Without all of it the firmware cannot run, so on any failure
* the AOP is disabled.
* the AOP is disabled. No other M4 board's AOP handoff is qualified: there the
* AOP is disabled too, without touching it or its DART.
*/
static int dt_set_t8132_aop(const char *path)
{
const char *why;
int node;

/*
* The J773g (Mac mini) tree states the same handoff contract and its 26.6.2 ADT has the same
* AOP, DART and segment layout; its headphone jack's audio runs on the AOP.
*/
if (fdt_node_check_compatible(dt, 0, "apple,j713") &&
fdt_node_check_compatible(dt, 0, "apple,j604"))
return 0; /* No other M4 board's AOP handoff is qualified. */
fdt_node_check_compatible(dt, 0, "apple,j604") && !kboot_is_j773g(dt)) {
why = "no handoff is qualified for this board";
goto disable;
}

int chosen = fdt_path_offset(dt, "/chosen");
if (chosen < 0 ||
Expand Down Expand Up @@ -11452,6 +11459,12 @@ int kboot_prepare_dt(void *fdt)
return -1;
if (dt_set_aop_firmware())
return -1;
/*
* Before dt_set_isp(): on a Mac with no ISP in its ADT that disables the ISP but not its
* DARTs, and this rule leaves an ISP that is already disabled as it is.
*/
if (dt_disable_t8132_absent_isp(dt))
return -1;
if (dt_set_isp())
return -1;
dt_set_j713_isp();
Expand Down
6 changes: 6 additions & 0 deletions src/kboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ int kboot_boot(void *kernel);
/* Reserve the DMA a USB controller that could not be stopped keeps (main.c, after USB shutdown). */
int kboot_reserve_usb_dma(void);

/* True only when the live J773g on 26.6.2 boots an OS tree that describes it. */
bool kboot_is_j773g(const void *fdt);

/* Disable the ISP and the DARTs only it uses on a T8132 Mac whose ADT has no camera. */
int dt_disable_t8132_absent_isp(void *dt);

/* Boot-args RAM range and boot framebuffer base, for the kboot_* SoC helpers */
u64 kboot_ram_base(void);
u64 kboot_ram_size(void);
Expand Down
4 changes: 3 additions & 1 deletion src/kboot_atc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "kboot_atc.h"
#include "adt.h"
#include "devicetree.h"
#include "kboot.h"
#include "malloc.h"
#include "pmgr.h"
#include "utils.h"
Expand Down Expand Up @@ -872,9 +873,10 @@ static void dt_copy_atc_tunables(void *dt, const char *adt_path, const char *dt_
if (adt_node < 0)
return;

/* The J773g's 26.6.2 ADT has every group of this bundle on all four PHYs. */
if (chip_id == T8132 &&
(!fdt_node_check_compatible(dt, 0, "apple,j713") ||
!fdt_node_check_compatible(dt, 0, "apple,j604")) &&
!fdt_node_check_compatible(dt, 0, "apple,j604") || kboot_is_j773g(dt)) &&
!fdt_node_check_compatible(dt, fdt_node, "apple,t8132-atcphy") &&
adt_is_compatible_at(adt, adt_node, "atc-phy,t8132", 0)) {
dt_copy_t8132_usb3_tunables(dt, adt_node, fdt_node);
Expand Down
89 changes: 89 additions & 0 deletions src/kboot_t8132.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* SPDX-License-Identifier: MIT */
/*
* T8132 (M4) board rules. They live here, not in kboot.c: kboot.c's code is linked into Stage 1's
* .init slot (aura-raw.ld), which has almost no room left.
*/

#include "kboot.h"
#include "adt.h"
#include "firmware.h"
#include "soc.h"
#include "utils.h"

#include "libfdt/libfdt.h"

#define bail(...) \
do { \
printf(__VA_ARGS__); \
return -1; \
} while (0)

/*
* J773g (Mac mini, M4): the J773g admissions in this tree were checked against one ADT, that
* of 26.6.2 (target J773gAP, board-id 0x2a, iBoot mBoot-18000.161.10). Admit them only when
* the live machine, its firmware and the OS tree all say J773g.
*/
bool kboot_is_j773g(const void *fdt)
{
return fdt && chip_id == T8132 && board_id == 0x2a && adt_is_compatible(adt, 0, "J773gAP") &&
os_firmware.version == V26_6_2 && !fdt_node_check_compatible(fdt, 0, "apple,j773g") &&
!fdt_node_check_compatible(fdt, 0, "apple,t8132");
}

/* Whether a node other than skip names the IOMMU phandle in its iommus list. */
static bool dt_iommu_shared(const void *dt, u32 phandle, int skip)
{
int node;

for (node = fdt_next_node(dt, -1, NULL); node >= 0; node = fdt_next_node(dt, node, NULL)) {
int len;
const fdt32_t *iommus = node == skip ? NULL : fdt_getprop(dt, node, "iommus", &len);

for (int i = 0; iommus && i + 1 < len / (int)sizeof(*iommus); i += 2)
if (fdt32_ld(&iommus[i]) == phandle)
return true;
}
return false;
}

/*
* A T8132 Mac without a camera (the J773g) has no isp or isp0 in its ADT. A tree that enables
* the ISP there would have the OS probe a camera and power domains that do not exist: disable
* the ISP and the DARTs that only it uses. A tree that leaves it disabled is not changed.
*/
int dt_disable_t8132_absent_isp(void *dt)
{
if (chip_id != T8132 || adt_path_offset(adt, "/arm-io/isp") >= 0 ||
adt_path_offset(adt, "/arm-io/isp0") >= 0)
return 0;

int isp = -1;
while ((isp = fdt_node_offset_by_compatible(dt, isp, "apple,t8132-isp")) >= 0) {
const char *status = fdt_getprop(dt, isp, "status", NULL);
if (status && !strcmp(status, "disabled"))
continue;

/* One cell of stream ID per DART, as in t8132.dtsi. Decide before any write. */
u32 darts[8];
bool own[8];
int len, count = 0;
const fdt32_t *iommus = fdt_getprop(dt, isp, "iommus", &len);
int cells = iommus && len > 0 && !(len % sizeof(*iommus)) ? len / sizeof(*iommus) : 0;
if (!(cells % 2) && cells / 2 <= (int)ARRAY_SIZE(darts))
for (; count < cells / 2; count++) {
darts[count] = fdt32_ld(&iommus[2 * count]);
own[count] = !dt_iommu_shared(dt, darts[count], isp);
}

if (fdt_setprop_string(dt, isp, "status", "disabled"))
bail("FDT: cannot disable the T8132 ISP\n");
for (int i = 0; i < count; i++) {
int dart = own[i] ? fdt_node_offset_by_phandle(dt, darts[i]) : -1;
if (dart >= 0 && fdt_setprop_string(dt, dart, "status", "disabled"))
bail("FDT: cannot disable a T8132 ISP DART\n");
}
printf("FDT: no ISP in the ADT; T8132 ISP disabled with the DARTs only it uses\n");
isp = -1; /* Offsets moved; start over. Disabled nodes are skipped. */
}
return 0;
}
61 changes: 61 additions & 0 deletions tests/python/test_j773g_board_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# SPDX-License-Identifier: GPL-2.0-only
"""Admit J773g only when the live machine, its firmware and the OS tree all say so."""
from pathlib import Path
import subprocess

from test_j713_dapf_instance import block


def test_j773g_board_gate(tmp_path):
repo = Path(__file__).resolve().parents[2]
production = block((repo / "src/kboot_t8132.c").read_text(), "bool kboot_is_j773g(")
# Each case breaks one fact used by the J773g gate: target J773gAP, board-id 42, chip-id
# 0x8132, OS firmware 26.6.2, or the OS tree.
harness = r'''
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#define T8132 0x8132
enum fw_version { V_UNKNOWN, V26_6, V26_6_2, V26_7 };
static struct { enum fw_version version; } os_firmware;
static unsigned chip_id, board_id;
static void *adt = (void *)1;
static const char *live, *board, *soc;
static bool adt_is_compatible(const void *tree, int node, const char *compatible)
{
return tree == adt && node == 0 && !strcmp(compatible, live);
}
static int fdt_node_check_compatible(const void *fdt, int node, const char *compatible)
{
assert(fdt && node == 0);
return strcmp(compatible, board) && strcmp(compatible, soc);
}
static void j773g(void)
{
chip_id = T8132; board_id = 0x2a; os_firmware.version = V26_6_2;
live = "J773gAP"; board = "apple,j773g"; soc = "apple,t8132";
}
''' + production + r'''
int main(void)
{
static char tree;
j773g(); assert(kboot_is_j773g(&tree));
assert(!kboot_is_j773g(NULL));
j773g(); chip_id = 0x8122; assert(!kboot_is_j773g(&tree));
j773g(); board_id = 0x2c; assert(!kboot_is_j773g(&tree));
j773g(); live = "J713AP"; assert(!kboot_is_j773g(&tree));
j773g(); board = "apple,j713"; assert(!kboot_is_j773g(&tree)); /* wrong OS tree */
j773g(); soc = "apple,t8122"; assert(!kboot_is_j773g(&tree));
j773g(); os_firmware.version = V26_6; assert(!kboot_is_j773g(&tree));
j773g(); os_firmware.version = V26_7; assert(!kboot_is_j773g(&tree));
j773g(); os_firmware.version = V_UNKNOWN; assert(!kboot_is_j773g(&tree));
return 0;
}
'''
source = tmp_path / "j773g-board.c"
source.write_text(harness)
binary = tmp_path / "j773g-board"
subprocess.run(["cc", "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter",
str(source), "-o", str(binary)], check=True)
subprocess.run([str(binary)], check=True)
111 changes: 111 additions & 0 deletions tests/python/test_t8132_aop_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# SPDX-License-Identifier: GPL-2.0-only
"""Run the production T8132 AOP handoff gate on J713, J604, J773g and boards it does not qualify."""
from pathlib import Path

from test_j713_dapf_instance import block
from test_siliconforest_handoff import run_c

ROOT = Path(__file__).resolve().parents[2]

# What can go wrong: an unqualified board keeps an enabled AOP that no loader handed over
# (the OS then starts it without its DAPF or its DRAM segments); the gate touches the AOP
# or its DART on a board it does not qualify; a J713 or J604 refusal leaves it enabled.
HARNESS = r'''
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "libfdt.h"
typedef uint32_t u32;
typedef unsigned long u64;
typedef struct { int unused; } asc_dev_t;
#define bail(...) do { printf(__VA_ARGS__); return -1; } while (0)
static void *dt;
static asc_dev_t the_asc;
static int touched, fail;
static bool running, live_j773g;
/* The live machine is a J773g on its qualified firmware and the tree says so. */
static bool kboot_is_j773g(const void *fdt)
{ return live_j773g && !fdt_node_check_compatible(fdt, 0, "apple,j773g"); }
static asc_dev_t *asc_init(const char *path)
{ assert(!strcmp(path, "/arm-io/aop")); touched++; return fail == 1 ? NULL : &the_asc; }
static bool asc_cpu_running(asc_dev_t *asc) { return running; }
static void asc_free(asc_dev_t *asc) {}
static int dapf_init_t8132_halted(const char *path, int index)
{ assert(!strcmp(path, "/arm-io/dart-aop") && index == 1); touched++; return fail == 2 ? -1 : 0; }
static int dt_reserve_asc_segments(const char *adt_path, const char *alt, const char *path,
bool remap, u64 base, bool dram_only)
{ assert(!strcmp(adt_path, "/arm-io/aop/iop-aop-nub") && remap && dram_only); touched++;
return fail == 3 ? -1 : 0; }
static int dt_reserve_t8132_aop_windows(const char *path) { touched++; return fail == 4 ? -1 : 0; }
'''

MAIN = r'''
static unsigned char blob[4096];
static void tree(const char *board, bool requested)
{
char compatible[64];
int len = snprintf(compatible, sizeof(compatible), "%s%c%s", board, 0, "apple,t8132") + 1;
dt = blob;
assert(!fdt_create_empty_tree(dt, sizeof(blob)));
assert(!fdt_setprop(dt, 0, "compatible", compatible, len));
int chosen = fdt_add_subnode(dt, 0, "chosen");
if (requested)
assert(!fdt_setprop(dt, chosen, "linux-enablement-mac,dapf-handoff",
"/arm-io/aop\0/arm-io/pmp", sizeof("/arm-io/aop\0/arm-io/pmp")));
int aop = fdt_add_subnode(dt, 0, "aop@390600000");
assert(aop >= 0 && !fdt_setprop_string(dt, aop, "compatible", "apple,t8132-aop"));
assert(!fdt_setprop_string(dt, aop, "status", "okay"));
touched = fail = 0;
running = false;
live_j773g = !strcmp(board, "apple,j773g");
}
static const char *status(void)
{
return fdt_getprop(dt, fdt_path_offset(dt, "/aop@390600000"), "status", NULL);
}
int main(void)
{
/* J713, J604 and a live J773g: a requested, halted AOP is handed over; any failed step
* disables it. */
static const char *const qualified[] = {"apple,j713", "apple,j604", "apple,j773g"};
for (unsigned i = 0; i < sizeof(qualified) / sizeof(*qualified); i++) {
tree(qualified[i], true);
assert(!dt_set_t8132_aop("/aop@390600000") && !strcmp(status(), "okay") && touched == 4);
for (int f = 1; f <= 4; f++) {
tree(qualified[i], true);
fail = f;
assert(!dt_set_t8132_aop("/aop@390600000") && !strcmp(status(), "disabled"));
}
tree(qualified[i], true);
running = true;
assert(!dt_set_t8132_aop("/aop@390600000") && !strcmp(status(), "disabled") && touched == 1);
tree(qualified[i], false);
assert(!dt_set_t8132_aop("/aop@390600000") && !strcmp(status(), "disabled") && !touched);
}
/* Other T8132 boards: the AOP ends disabled and neither it nor its DART is touched,
* whether or not the tree asks for the handoff. */
static const char *const others[] = {"apple,j623", "apple,j624", "apple,j715",
"apple,j773g"};
for (unsigned i = 0; i < sizeof(others) / sizeof(*others); i++)
for (int requested = 0; requested <= 1; requested++) {
tree(others[i], requested);
/* A J773g tree on any other machine or firmware is not qualified. */
live_j773g = false;
assert(!dt_set_t8132_aop("/aop@390600000"));
assert(!strcmp(status(), "disabled") && !touched);
}
return 0;
}
'''


def production():
kboot = (ROOT / "src/kboot.c").read_text()
return block(kboot, "static int dt_set_t8132_aop(") + "\n"


def test_t8132_aop_gate(tmp_path):
run_c(tmp_path, "t8132-aop-gate",
HARNESS + production() + MAIN)
Loading
Loading