Skip to content
Closed
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
13 changes: 13 additions & 0 deletions docs/boot-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ The boot-argument tokens are matched exactly:
NVRAM: `touch <esp>/aura-proxy` to turn it on, `rm <esp>/aura-proxy` to turn
it off. Without either, Stage 1 boots straight through.

- `m1n1.wdt=keep` leaves the SoC watchdog as this stage found it, if it is
armed, instead of disabling it at start. It is for a first stage that is not
aura, armed the watchdog before starting this one and has a fallback of its
own (an A/B m1n1 stage 1 does: a reset before the OS counts as a failed boot,
and it then boots its other slot). A hang in this stage or in a kernel that
never takes the watchdog over then resets the Mac. A stage with the token that
would stop in the proxy waits 15 s for a host and then reboots, as an
`aura-try` trial does. Nothing feeds the watchdog: a host that needs longer
than its period disarms it (`P_WDT_DISABLE`). Linux's `apple_wdt` keeps an
armed watchdog fed once it probes. The token is read from the boot arguments
up to their terminator only, and it keeps the spelling of the stage that
writes it. It does not belong in the NVRAM boot-args.

- `aura-console` keeps the debug UART as the OS console (see below) without
the rest of `aura-debug`. `aura-debug` keeps it too.

Expand Down
12 changes: 12 additions & 0 deletions src/boot_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ static bool debug_enabled;
static bool console_requested;
static bool proxy_requested;
static bool trial_running;
static bool wdt_keep;
static bool is_stage1;

static bool boot_space(char c)
Expand Down Expand Up @@ -45,6 +46,7 @@ void boot_options_init(const char *line, size_t capacity)
console_requested = boot_option_present(line, capacity, "aura-console");
proxy_requested = boot_option_present(line, capacity, "aura-proxy");
trial_running = boot_option_present(line, capacity, "aura-trial");
wdt_keep = boot_option_present(line, capacity, "m1n1.wdt=keep");
is_stage1 = true;
}

Expand Down Expand Up @@ -88,6 +90,16 @@ void boot_options_set_trial(bool trial)
trial_running = trial;
}

/*
* m1n1.wdt=keep, in the boot arguments as iBoot passes them: a first stage that is not aura armed
* the watchdog for this one and has a fallback behind it (wdt_disable_at_start()). Only the line
* itself counts, up to its terminator; nothing past it is read, as iBoot defines no bytes there.
*/
bool boot_wdt_keep_enabled(void)
{
return wdt_keep;
}

bool boot_options_append(char *line, size_t capacity, const char *option)
{
if (!line || !option || !option[0] || boot_option_present(line, capacity, option))
Expand Down
2 changes: 2 additions & 0 deletions src/boot_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ void boot_options_set_stage1(bool stage1);
bool aura_proxy_enabled(void);
bool aura_trial_enabled(void);
void boot_options_set_trial(bool trial);
/* m1n1.wdt=keep is in the boot arguments: leave the watchdog an earlier stage armed */
bool boot_wdt_keep_enabled(void);
/* Add a whole-word option to a NUL-terminated line of capacity bytes; false if it does not fit */
bool boot_options_append(char *line, size_t capacity, const char *option);
#endif
11 changes: 8 additions & 3 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,11 +1027,12 @@ static int dt_reserve_secondary_ro(void)
* On an aura-try trial each aura stage arms the trial watchdog (wdt.c) so a hang anywhere before
* Linux feeds it resets into the configured Stage 2. U-Boot with WATCHDOG_AUTOSTART takes the
* watchdog over and services it from its own loops, so a U-Boot that waits forever (on a disk it
* cannot reach, say) would never be reset. Tell it to leave the watchdog alone on a trial boot.
* cannot reach, say) would never be reset. Tell it to leave the watchdog alone on a trial boot,
* and on one whose first stage armed it for a fallback of its own (m1n1.wdt=keep).
*/
static void dt_set_trial_watchdog(void)
{
if (!aura_trial_enabled())
if (!aura_trial_enabled() && !wdt_left_armed())
return;
/* The compatibles U-Boot's apple_wdt driver binds to */
static const char *const compat[] = {"apple,t8103-wdt", "apple,wdt"};
Expand All @@ -1043,7 +1044,8 @@ static void dt_set_trial_watchdog(void)
if (fdt_setprop(dt, node, "u-boot,noautostart", NULL, 0))
printf("FDT: couldn't mark the watchdog for the trial\n");
else
printf("FDT: trial boot: U-Boot leaves the watchdog to the trial budget\n");
printf("FDT: %s: U-Boot leaves the watchdog to the boot's budget\n",
aura_trial_enabled() ? "trial boot" : "m1n1.wdt=keep");
}
}
}
Expand Down Expand Up @@ -14103,6 +14105,9 @@ int kboot_prepare_dt(void *fdt)

/* setup console log buffer early to capture as much log as possible */
dt_setup_mtd_phram();
/* Repeated for the OS log: wdt_disable_at_start() ran before the log buffer existed */
if (wdt_left_armed())
printf("kboot: watchdog left armed for the OS (m1n1.wdt=keep)\n");

if (dt_set_m3_defaults())
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ void aura_main(void)
aic_init();
smp_init();
#endif
wdt_disable();
wdt_disable_at_start();
if (aura_trial_enabled() && wdt_arm_trial())
printf("WDT: could not arm the trial watchdog\n");
#if defined(CHAINLOADING) && !defined(ESP_STAGE2)
Expand Down
34 changes: 25 additions & 9 deletions src/proxy_fallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
* Nobody may be at the Mac when an aura-try trial would stop in the proxy, and only a host can
* drive it or reboot it there; the trial watchdog is the only other way out. The trial spent its
* marker on the ESP before its image was read, so a reboot lands on the configured image. This
* needs nothing from Stage 1 beyond the trial itself. Any other stage stops in the proxy as before.
* needs nothing from Stage 1 beyond the trial itself.
*
* A stage booted with m1n1.wdt=keep has the same fallback behind it in a first stage that is not
* aura: a reset before the OS counts as a failed boot, and that stage then boots its other slot.
* It reboots after the same host window, whether or not the watchdog is still armed (a power
* cycle does not count as a failed boot). Any other stage stops in the proxy as before.
*/
bool proxy_fallback(const char *why)
{
if (!aura_trial_enabled())
bool trial = aura_trial_enabled();

if (!trial && !boot_wdt_keep_enabled())
return false;

/*
Expand All @@ -23,20 +30,29 @@ bool proxy_fallback(const char *why)
* host can attach, so a host that goes away cannot strand the trial. Without a watchdog, a
* host window could, so reboot now instead.
*/
if (wdt_boot_watchdog_armed() && wdt_arm_trial()) {
if (trial && wdt_boot_watchdog_armed() && wdt_arm_trial()) {
printf("\naura-try: %s; no trial watchdog, rebooting to the configured image\n", why);
flush_and_reboot();
}

/* Shown on the panel */
printf("\naura-try: %s; waiting %u s for a host, then rebooting to the configured image\n", why,
PROXY_FALLBACK_HOST_MS / 1000);
if (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("\nm1n1.wdt=keep: %s; waiting %u s for a host, then rebooting into the first "
"stage's fallback\n",
why, PROXY_FALLBACK_HOST_MS / 1000);

if (uartproxy_wait_host(PROXY_FALLBACK_HOST_MS)) {
/* 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);
/* Not disarmed for the host: a host that goes away must not strand the boot. */
if (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; a watchdog the first stage armed still runs, "
"a host that needs longer disarms it\n");
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions src/proxy_fallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

#include "types.h"

/* How long an aura-try trial that would stop in the proxy waits for a host before it reboots */
/* How long a trial (or m1n1.wdt=keep) stage that would stop in the proxy waits for a host */
#define PROXY_FALLBACK_HOST_MS 15000

/*
* A stage that would stop in the proxy with nobody to drive it calls this first, with the reason.
* On an aura-try trial it waits PROXY_FALLBACK_HOST_MS for a host and reboots if none came. It
* returns when the proxy should run: true if a host attached in the window (it has a carrier
* already), false if this is not a trial (the proxy runs as before).
* On an aura-try trial, or with m1n1.wdt=keep, it waits PROXY_FALLBACK_HOST_MS for a host and
* reboots if none came. It returns when the proxy should run: true if a host attached in the
* window (it has a carrier already), false if this is neither (the proxy runs as before).
*/
bool proxy_fallback(const char *why);

Expand Down
36 changes: 36 additions & 0 deletions src/wdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "wdt.h"
#include "adt.h"
#include "boot_options.h"
#include "memory.h"
#include "types.h"
#include "utils.h"
Expand All @@ -23,6 +24,7 @@
#define WDT_RECONFIG_WD2 (1u << 4)

static u64 wdt_base = 0;
static bool wdt_kept;
static u64 wdt_reconfig = 0;

static void wdt_set_addrs(u64 primary, u64 reconfig)
Expand Down Expand Up @@ -87,6 +89,7 @@ static u32 wdt_arm_wd2(u64 primary, u64 reconfig, u32 ticks)

void wdt_disable(void)
{
wdt_kept = false;
int path[8];
int node = adt_path_offset_trace(adt, "/arm-io/wdt", path);

Expand Down Expand Up @@ -130,6 +133,38 @@ void wdt_disable(void)
}
}

/*
* m1n1.wdt=keep: a first stage that is not aura (the J716s' A/B m1n1 stage 1 arms 150 s) armed the
* watchdog for this stage, so that a hang before the OS takes it over resets the Mac, iBoot counts
* a failed boot and that stage boots its fallback. Leave the primary and the secondary as they
* are if the primary is armed; otherwise disable both as usual. Nothing in aura feeds it: as on an
* aura-try trial, a host that needs longer than the period disarms it (P_WDT_DISABLE), and every
* other disarm or re-arm in this stage ends the keep.
*/
void wdt_disable_at_start(void)
{
int path[8];
u64 base = 0, size = 0, reconfig = 0;
int node = adt_path_offset_trace(adt, "/arm-io/wdt", path);

if (boot_wdt_keep_enabled() && node >= 0 && !adt_get_reg(adt, path, "reg", 0, &base, &size) &&
base && (read32(base + WDT_CTL) & WDT_CTL_RESET_EN)) {
wdt_set_addrs(base, wdt_has_simple_reconfig(node, path, size, &reconfig) ? reconfig : 0);
wdt_kept = true;
printf("Primary WDT register @ 0x%lx\n", wdt_base);
printf("Primary WDT left armed (m1n1.wdt=keep)\n");
return;
}
if (boot_wdt_keep_enabled())
printf("WDT: m1n1.wdt=keep, but the watchdog is not armed\n");
wdt_disable();
}

bool wdt_left_armed(void)
{
return wdt_kept;
}

void wdt_reboot(void)
{
/* A fault before wdt_disable() (early init) must still reset the Mac, not spin */
Expand Down Expand Up @@ -249,6 +284,7 @@ static int wdt_arm(unsigned seconds)
return -1;

u32 alarm = seconds * 24000000u;
wdt_kept = false;

/*
* On wdt-version >= 3 machines reporting simple-reconfig-wdog-support with a 4-byte reg[3],
Expand Down
4 changes: 4 additions & 0 deletions src/wdt.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "types.h"

void wdt_disable(void);
/* At start: wdt_disable(), unless m1n1.wdt=keep asks to leave an armed watchdog as found */
void wdt_disable_at_start(void);
/* wdt_disable_at_start() left the watchdog armed, and nothing has disarmed or re-armed it since */
bool wdt_left_armed(void);
void wdt_reboot(void);
int wdt_arm_seconds(unsigned seconds);
bool wdt_simple_reconfig_reg(u64 *primary_out, u64 *reconfig_out);
Expand Down
41 changes: 41 additions & 0 deletions tests/python/test_boot_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,44 @@ def test_early_debug_option(tmp_path):
binary = tmp_path/'test'
subprocess.run(shlex.split(os.environ.get('CC', 'cc')) + ['-std=c11','-Wall','-Wextra','-Werror','-I',str(ROOT/'src'),str(test),str(ROOT/'src/boot_options.c'),'-o',str(binary)],check=True)
subprocess.run([str(binary)],check=True)


def test_wdt_keep_option(tmp_path):
test = tmp_path / 'test.c'
test.write_text(r'''
#include "boot_options.h"
#include <assert.h>
#include <string.h>
int main(void) {
const char *yes[] = {"m1n1.wdt=keep", "-v m1n1.wdt=keep", "chainload.x=1\tm1n1.wdt=keep\n"};
const char *no[] = {"", "m1n1.wdt", "m1n1.wdt=", "m1n1.wdt=keeps", "m1n1.wdt=off",
"xm1n1.wdt=keep", "aura-debug aura-proxy"};
for (unsigned i=0; i<sizeof(yes)/sizeof(*yes); i++) {
boot_options_init(yes[i], strlen(yes[i])+1);
assert(boot_wdt_keep_enabled());
}
for (unsigned i=0; i<sizeof(no)/sizeof(*no); i++) {
boot_options_init(no[i], strlen(no[i])+1);
assert(!boot_wdt_keep_enabled());
}
/* Independent of the stage: a chainloaded stage 2 is where it matters */
boot_options_init("m1n1.wdt=keep aura-proxy", 25);
boot_options_set_stage1(false);
assert(boot_wdt_keep_enabled() && !aura_proxy_enabled());
/* Nothing past the line's terminator is read: not a token left 4 bytes in, where a stage
* with m1n1's unpacked boot_args union writes its command line */
char skewed[256] = {0};
memcpy(skewed + 4, " m1n1.wdt=keep", 15);
boot_options_init(skewed, sizeof(skewed));
assert(!boot_wdt_keep_enabled());
char unterminated[] = {'m','1','n','1','.','w','d','t','=','k','e','e','p'};
boot_options_init(unterminated, sizeof(unterminated));
assert(!boot_wdt_keep_enabled());
boot_options_init(0, 0);
assert(!boot_wdt_keep_enabled());
return 0;
}
''')
binary = tmp_path/'test'
subprocess.run(shlex.split(os.environ.get('CC', 'cc')) + ['-std=c11','-Wall','-Wextra','-Werror','-I',str(ROOT/'src'),str(test),str(ROOT/'src/boot_options.c'),'-o',str(binary)],check=True)
subprocess.run([str(binary)],check=True)
32 changes: 27 additions & 5 deletions tests/python/test_proxy_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
4. The window takes the DockChannel from a CDC carrier, or a byte stream without the sync word
counts as a host.
5. The window outlives the trial watchdog, which would then reset the Mac with no log line.
6. A stage that is not a trial waits or reboots (it must stop in the proxy as before), or a trial
with a host in the window reboots under it.
6. A stage that is neither a trial nor booted with m1n1.wdt=keep waits or reboots (it must stop
in the proxy as before), or one with a host in the window reboots under it.
7. A handoff failure leaves the panel dark (FB_SILENT_MODE), or a CDC transition still scheduled
runs from the window's usb_cdc_poll() after USB was shut down for the handoff.
"""
Expand Down Expand Up @@ -201,9 +201,10 @@ def test_host_after_the_deadline_is_too_late(tmp_path):
#include <string.h>
typedef unsigned int u32;
#define WDT_TRIAL_SECONDS 178
static bool trial, host, boot_wdt, arm_fails;
static bool trial, host, boot_wdt, arm_fails, keep;
static unsigned waited, reboots, trial_arms;
static bool aura_trial_enabled(void) { return trial; }
static bool boot_wdt_keep_enabled(void) { return keep; }
static bool wdt_boot_watchdog_armed(void) { return boot_wdt; }
static int wdt_arm_trial(void)
{
Expand All @@ -230,14 +231,15 @@ def test_host_after_the_deadline_is_too_late(tmp_path):
host = atoi(argv[2]);
boot_wdt = atoi(argv[3]);
arm_fails = atoi(argv[4]);
keep = atoi(argv[5]);
bool r = proxy_fallback("NVMe handoff failed");
printf("ret=%d waited=%u reboots=%u arms=%u\n", r, waited, reboots, trial_arms);
return 0;
}
'''


def _fallback(tmp_path, trial, host, boot_wdt=False, arm_fails=False):
def _fallback(tmp_path, trial, host, boot_wdt=False, arm_fails=False, keep=False):
header = (ROOT / "src/proxy_fallback.h").read_text()
header = header[header.index("#define PROXY_FALLBACK_HOST_MS"):]
header = header[:header.index("\n")]
Expand All @@ -249,7 +251,7 @@ def _fallback(tmp_path, trial, host, boot_wdt=False, arm_fails=False):
subprocess.run([*host_cc(), "-std=gnu11", "-Wall", "-Werror", "-Wno-unused-function",
"-Wno-unused-parameter", str(src), "-o", str(exe)], check=True)
out = subprocess.run([str(exe), str(int(trial)), str(int(host)), str(int(boot_wdt)),
str(int(arm_fails))], check=True,
str(int(arm_fails)), str(int(keep))], check=True,
capture_output=True, text=True).stdout
return dict(item.split("=") for item in out.strip().splitlines()[-1].split())

Expand Down Expand Up @@ -285,6 +287,26 @@ def test_stage1_trial_without_a_trial_watchdog_reboots_without_a_window(tmp_path
"ret": "reboot", "waited": "0", "reboots": "1", "arms": "1"}


def test_wdt_keep_without_a_host_reboots_after_the_window(tmp_path):
"""m1n1.wdt=keep: the first stage's fallback is behind a reset, whether or not the watchdog it
armed still runs. aura arms no watchdog of its own for it."""
for boot_wdt in (False, True):
assert _fallback(tmp_path, False, False, boot_wdt, keep=True) == {
"ret": "reboot", "waited": "15000", "reboots": "1", "arms": "0"}


def test_wdt_keep_with_a_host_keeps_the_proxy(tmp_path):
assert _fallback(tmp_path, False, True, keep=True) == {"ret": "1", "waited": "15000",
"reboots": "0", "arms": "0"}


def test_a_trial_under_wdt_keep_is_a_trial(tmp_path):
assert _fallback(tmp_path, True, True, boot_wdt=True, keep=True) == {
"ret": "1", "waited": "15000", "reboots": "0", "arms": "1"}
assert _fallback(tmp_path, True, True, boot_wdt=True, arm_fails=True, keep=True) == {
"ret": "reboot", "waited": "0", "reboots": "1", "arms": "1"}


def test_the_trial_watchdog_is_armed_before_the_host_window():
fn = _body((ROOT / "src/proxy_fallback.c").read_text(), "bool proxy_fallback(const char *why)")
assert fn.index("wdt_arm_trial()") < fn.index("uartproxy_wait_host(")
Expand Down
2 changes: 2 additions & 0 deletions tests/python/test_wdt_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def test_wdt_arm_seconds(tmp_path):
static void *adt = &adt_dummy;
static void dc_cvac_range(const void *p, size_t n)
{ (void)p; (void)n; cache_cleans++; }
/* m1n1.wdt=keep is not given: wdt_disable_at_start() is wdt_disable() */
static bool boot_wdt_keep_enabled(void) { return false; }
static int adt_path_offset_trace(void *a, const char *p, int *trace)
{ (void)a; (void)p; (void)trace; return 1; }
static const void *adt_getprop(void *a, int node, const char *key, u32 *len)
Expand Down
Loading