Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -281,7 +281,7 @@ OBJECTS := \
smc.o \
ctxr_exec.o \
smp.o \
spmi.o pmu_counter.o ace3.o \
spmi.o pmu_counter.o proxy_fallback.o ace3.o \
start.o \
startup.o \
string.o \
Expand Down
55 changes: 31 additions & 24 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "pcie.h"
#include "pmgr.h"
#include "pmu_counter.h"
#include "proxy_fallback.h"
#include "secboot.h"
#include "sep.h"
#include "smp.h"
Expand Down Expand Up @@ -269,19 +270,6 @@ void run_actions(void)
fb_set_active(true);

printf("No valid payload found\n");
/*
* An aura-try trial that cannot start its payload must not stop here: nobody may be at the
* Mac, and the proxy below is not guaranteed to keep the trial watchdog running. The trial
* marker was spent before this image was read, so a reboot lands on the configured Stage 2.
* Leave the error on the panel for a few seconds first.
*/
if (aura_trial_enabled()) {
printf("\naura-try: the trial could not start its payload; rebooting to the configured "
"Stage 2 in 10 s\n");
mdelay(10000);
pmu_chain_leave();
flush_and_reboot();
}
if (stage1_config_target())
/* Shown on the panel: this is where a user without a USB host ends up. */
printf("\nStage 1: could not start the configured Stage 2 from the EFI partition.\n"
Expand Down Expand Up @@ -310,17 +298,28 @@ void run_actions(void)
flush_and_reboot();
}

/*
* An aura-try trial, or a stage whose failed-boot count allows it, reboots after a bounded
* wait for a host instead of stopping here (proxy_fallback.c). It listens on the carriers
* already up and decides before USB is brought up below: that bring-up may reset a port
* controller and arm a watchdog of its own, which a stage on its way to a reboot must not do.
*/
bool host_attached = proxy_fallback("no payload could be started", payload_start_failed());

#ifndef BRINGUP
/* A proxy window brought USB up already; a second bring-up would register its ports twice. */
if (!usb_up && !aura_debug_enabled() && !(stage1_window_done && chip_id != T8140)) {
/*
* A proxy window brought USB up already; a second bring-up would register its ports twice. A
* host that attached in the fallback window has its carrier: bringing USB up could drop it.
*/
if (!host_attached && !usb_up && !aura_debug_enabled() &&
!(stage1_window_done && chip_id != T8140)) {
usb_init();
usb_iodev_init();
}
#endif

printf("Running proxy...\n");

pmu_chain_leave();
uartproxy_run(NULL);
}

Expand Down Expand Up @@ -490,17 +489,15 @@ void aura_main(void)
panic("Refusing next stage while secondary callbacks are pending\n");
}

/* A chainloaded aura stage carries the chain on; anything else ends it here. */
if (!next_stage.chained)
pmu_chain_leave();

printf("Preparing to run next stage at %p...\n", next_stage.entry);

if (!nvme_shutdown()) {
printf("NVMe handoff failed; returning to proxy\n");
pmu_chain_leave();
if (!secboot_host_proxy_allowed())
if (!secboot_host_proxy_allowed()) {
pmu_chain_leave();
flush_and_reboot();
}
proxy_fallback("NVMe handoff failed", true);
uartproxy_run(NULL);
panic("NVMe handoff failed\n");
}
Expand All @@ -518,13 +515,23 @@ void aura_main(void)
/* Its firmware may still use memory the next stage will reuse. */
if (display_shutdown(DCP_SLEEP_IF_EXTERNAL) < 0) {
printf("Display handoff failed; returning to proxy\n");
pmu_chain_leave();
if (!secboot_host_proxy_allowed())
if (!secboot_host_proxy_allowed()) {
pmu_chain_leave();
flush_and_reboot();
}
proxy_fallback("Display handoff failed", true);
uartproxy_run(NULL);
panic("Display handoff failed\n");
}
}
#endif
/*
* Both handoffs are done. A chainloaded aura stage carries the chain on; anything else ends it
* here. Not earlier: a handoff that fails must still find the count it reboots on.
*/
if (!next_stage.chained)
pmu_chain_leave();
#ifndef BRINGUP
#ifdef USE_FB
fb_shutdown(next_stage.restore_logo);
#endif
Expand Down
44 changes: 17 additions & 27 deletions src/payload.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,44 +454,31 @@ bool payload_logo(void **custom_128, void **custom_256)
}

/*
* Reboot after a failed start while fewer than this many boots in a row have failed: an ANS that
* a Stage 2 couldn't start from off came up again after one or two reboots each time on the J713
* (about 1 start in 7 failed, 2026-11-21). iBoot counts each such reset as a failed boot, and no
* aura stage clears the count until the chain reaches an OS or the proxy (pmu_chain_leave()), so
* a failure that persists ends in the proxy after this many reboots.
* A post-M4 ANS that didn't start from off stays down until a reboot. The stage ends up where
* every stage with no payload does: proxy_fallback() reboots while the chain's failed-boot count
* allows it (an ANS that failed to start came up again after one or two reboots each time on the
* J713, about 1 start in 7 failing, 2026-11-21) and stops in the proxy after that.
*/
#define PAYLOAD_NVME_START_REBOOTS 2

/* The decision alone, from the count the earlier stages handed on. */
static bool payload_nvme_start_reboot(bool passed, u8 failed_boots)
static void payload_nvme_start_failed(void)
{
return passed && failed_boots < PAYLOAD_NVME_START_REBOOTS;
printf("nvme: ANS didn't start from off\n");
}

/*
* A post-M4 ANS that didn't start from off stays down until a reboot. Rather than stop in the
* proxy, where only a tether can reboot it, reboot while the chain's failed-boot count allows it.
* Without a count from an earlier stage nothing bounds the reboots, and the proxy stays.
* Whether this stage had something to start and starting it failed (a load that returned an error,
* or a handoff that was refused), as opposed to an image with nothing to start. proxy_fallback()
* reboots only for the first.
*/
static void payload_nvme_start_failed(void)
static bool payload_failed;

bool payload_start_failed(void)
{
u8 failed = 0;
bool passed = pmu_failed_boots_passed(&failed);

if (!payload_nvme_start_reboot(passed, failed)) {
if (!passed)
printf("nvme: no failed-boot count from an earlier stage: no reboot, proxy\n");
else
printf("nvme: %u failed boots in a row already: no reboot, proxy\n", failed);
return;
}
printf("nvme: ANS start failed: rebooting (%u failed boots in a row before, limit %u)\n",
failed, PAYLOAD_NVME_START_REBOOTS);
flush_and_reboot();
return payload_failed;
}

int payload_run(void)
{
payload_failed = false;
const char *target = adt_getprop(adt, 0, "target-type", NULL);
if (target) {
strcpy(expect_compatible, "apple,");
Expand Down Expand Up @@ -573,6 +560,7 @@ int payload_run(void)
}
int ret = chainload_load(chainload_spec, chosen, &chosen_cnt, ARRAY_SIZE(chosen));
if (ret) {
payload_failed = true;
next_stage.entry = NULL;
nvme_shutdown();
}
Expand All @@ -584,6 +572,7 @@ int payload_run(void)
int ret = boot_storage_load(boot_spec, &kernel, &fdt);
cpufreq_payload_boost(false);
if (ret) {
payload_failed = true;
next_stage.entry = NULL;
return -1;
}
Expand Down Expand Up @@ -683,6 +672,7 @@ int payload_run(void)
#endif

boot_failed:
payload_failed = true;
next_stage.entry = NULL;
if (boot_spec)
kboot_set_initrd(NULL, 0);
Expand Down
1 change: 1 addition & 0 deletions src/payload.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
bool payload_logo(void **custom_128, void **custom_256);

int payload_run(void);
bool payload_start_failed(void);
int payload_boot_storage(const char *spec);

#endif
81 changes: 81 additions & 0 deletions src/proxy_fallback.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* SPDX-License-Identifier: MIT */

#include "proxy_fallback.h"
#include "boot_options.h"
#include "pmu_counter.h"
#include "uartproxy.h"
#include "utils.h"
#include "wdt.h"

/*
* Nobody may be at the Mac when a stage would stop in the proxy, and only a USB host can drive it
* or reboot it there. Reboot instead where a reboot is known to end:
* - An aura-try trial spent its marker on the ESP before its image was read, so the reboot lands
* on the configured image. This needs nothing from Stage 1 beyond the trial itself.
* - Otherwise, when something failed to start (an image with nothing to start stops in the proxy
* as before), while the failed-boot count the earlier stages handed on is under the limit. iBoot
* counts each such reset, and no stage clears the count before the chain leaves aura, so a
* failure that persists ends in the proxy after PROXY_FALLBACK_REBOOTS reboots. A stage handed
* no count (an older Stage 1 clears it at start) can't tell a first failure from a cleared
* count, and keeps the proxy.
*/
enum proxy_fallback_action proxy_fallback_decide(bool trial, bool start_failed, bool count_passed,
u8 failed_boots)
{
if (trial)
return PROXY_FALLBACK_REBOOT_TRIAL;
if (start_failed && count_passed && failed_boots < PROXY_FALLBACK_REBOOTS)
return PROXY_FALLBACK_REBOOT_COUNTED;
return PROXY_FALLBACK_PROXY;
}

bool proxy_fallback(const char *why, bool start_failed)
{
u8 failed = 0;
bool passed = pmu_failed_boots_passed(&failed);
enum proxy_fallback_action action =
proxy_fallback_decide(aura_trial_enabled(), start_failed, passed, failed);

if (action == PROXY_FALLBACK_PROXY) {
if (!start_failed)
printf("%s: nothing failed to start: no reboot, proxy\n", why);
else if (!passed)
printf("%s: no failed-boot count from an earlier stage: no reboot, proxy\n", why);
else
printf("%s: %u failed boots in a row already: no reboot, proxy\n", why, failed);
pmu_chain_leave();
return false;
}

/* Shown on the panel */
if (action == PROXY_FALLBACK_REBOOT_TRIAL)
printf("\naura-try: %s; waiting %u s for a host, then rebooting to the configured "
"image\n",
why, PROXY_FALLBACK_HOST_MS / 1000);
else
printf("\n%s; waiting %u s for a host, then rebooting (%u failed boots in a row before, "
"limit %u)\n",
why, PROXY_FALLBACK_HOST_MS / 1000, failed, PROXY_FALLBACK_REBOOTS);

if (uartproxy_wait_host(PROXY_FALLBACK_HOST_MS)) {
if (action == PROXY_FALLBACK_REBOOT_TRIAL)
/* Not disarmed for the host: a host that goes away must not strand the trial. */
printf("Proxy: host attached, no reboot; the trial watchdog still runs (%u s), a host "
"that needs longer disarms it\n",
WDT_TRIAL_SECONDS);
else
printf("Proxy: host attached, no reboot\n");
pmu_chain_leave();
return true;
}

/*
* A trial's reboot is a deliberate one, so clear the count first, as run_actions() does before
* its other deliberate reboots: the configured image then starts with only this reset counted.
*/
if (action == PROXY_FALLBACK_REBOOT_TRIAL)
pmu_chain_leave();
printf("No host: rebooting\n");
flush_and_reboot();
return false;
}
38 changes: 38 additions & 0 deletions src/proxy_fallback.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: MIT */

#ifndef PROXY_FALLBACK_H
#define PROXY_FALLBACK_H

#include "types.h"

/*
* At most this many self-reboots in a row before a stage stops in the proxy: the chain's
* failed-boot count (aura.failed-boots=) bounds them.
*/
#define PROXY_FALLBACK_REBOOTS 2

/* How long a stage that falls back waits for a host before it reboots */
#define PROXY_FALLBACK_HOST_MS 15000

enum proxy_fallback_action {
/* Stop in the proxy, as before */
PROXY_FALLBACK_PROXY,
/* An aura-try trial: reboot to the configured image, the trial marker is spent */
PROXY_FALLBACK_REBOOT_TRIAL,
/* Reboot, and let iBoot count it: the chain's failed-boot count is under the limit */
PROXY_FALLBACK_REBOOT_COUNTED,
};

enum proxy_fallback_action proxy_fallback_decide(bool trial, bool start_failed, bool count_passed,
u8 failed_boots);

/*
* A stage that would stop in the proxy calls this first, with the reason and whether something
* failed to start (as opposed to an image with nothing to start). It waits a bounded time for a
* host and reboots if none came and the decision above allows it. It returns, with the chain left
* (pmu_chain_leave()), when the proxy should run: true if a host attached in the window (it has a
* carrier already), false if no reboot is allowed.
*/
bool proxy_fallback(const char *why, bool start_failed);

#endif
25 changes: 25 additions & 0 deletions src/uartproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,31 @@ enum stage1_proxy_result uartproxy_wait_stage1(unsigned int timeout_ms)
}
}

/*
* A bounded wait for a host on any proxy iodev, for a stage that would otherwise stop in the proxy
* with nobody to drive it (proxy_fallback.c). A host's sync word ends it early, and the proxy then
* runs presynced on that iodev.
*/
bool uartproxy_wait_host(unsigned int timeout_ms)
{
u64 start = ticks_to_msecs(get_ticks());

while (ticks_to_msecs(get_ticks()) - start < timeout_ms) {
usb_cdc_poll();
for (iodev_id_t i = 0; i < IODEV_MAX; i++) {
if (i == IODEV_DOCKCHANNEL_UART && usb_cdc_owns_carrier())
continue;
if (uartproxy_sync_iodev(i)) {
presynced_iodev = i;
wdt_host_attached();
return true;
}
}
mdelay(1);
}
return false;
}

int uartproxy_run_presynced(void)
{
if (presynced_iodev == IODEV_MAX)
Expand Down
1 change: 1 addition & 0 deletions src/uartproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct uartproxy_msg_start {
int uartproxy_run(struct uartproxy_msg_start *start);
bool uartproxy_wait_dockchannel(unsigned int timeout_ms);
enum stage1_proxy_result uartproxy_wait_stage1(unsigned int timeout_ms);
bool uartproxy_wait_host(unsigned int timeout_ms);
int uartproxy_run_presynced(void);
void uartproxy_send_event(u16 event_type, void *data, u16 length);

Expand Down
1 change: 1 addition & 0 deletions tests/python/test_m3_defaults_optout.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def harness():
("/* No failed-boot count here: an ANS start failure stays in the proxy */\n"
"static void payload_nvme_start_failed(void) {}\n"
if "payload_nvme_start_failed" in payload else "") + \
("static bool payload_failed;\n" if "static bool payload_failed;" in payload else "") + \
function(payload, "int payload_run(void)\n{") + r'''
static u8 image[1 << 20] __attribute__((aligned(16)));

Expand Down
Loading