From 328585f1607b94ed365858432116e4b3e880c341 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 14 Feb 2023 11:20:29 -0500 Subject: [PATCH] don't enter safemode.py on USER safe mode --- README.rst | 6 ++++-- main.c | 4 +++- shared-bindings/supervisor/SafeModeReason.c | 6 +++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 35a66669ed..1a6dc2c33e 100644 --- a/README.rst +++ b/README.rst @@ -139,8 +139,10 @@ Behavior possible to fix code that causes nasty crashes by making it available through mass storage after the crash. A reset (the button) is needed after it's fixed to get back into normal mode. - Safe mode may be handled programmatically by providing a ``safemode.py``. - ``safemode.py`` is run if the board has reset due to entering safe mode. USB is not - available so nothing can be printed. ``safemode.py`` can determine why the safe mode occurred + ``safemode.py`` is run if the board has reset due to entering safe mode, unless the safe mode + initiated by the user by pressing button(s). + USB is not available so nothing can be printed. + ``safemode.py`` can determine why the safe mode occurred using ``supervisor.runtime.safe_mode_reason``, and take appropriate action. For instance, if a hard crash occurred, ``safemode.py`` may do a ``microcontroller.reset()`` to automatically restart despite the crash. diff --git a/main.c b/main.c index 4d37e17992..c0b4cb7be2 100644 --- a/main.c +++ b/main.c @@ -733,7 +733,9 @@ vstr_t *boot_output; #if CIRCUITPY_SAFEMODE_PY STATIC void __attribute__ ((noinline)) run_safemode_py(safe_mode_t safe_mode) { // Don't run if we aren't in safe mode or we won't be able to find safemode.py. - if (safe_mode == SAFE_MODE_NONE || !filesystem_present()) { + // Also don't run if it's a user-initiated safemode (pressing button(s) during boot), + // since that's deliberate. + if (safe_mode == SAFE_MODE_NONE || safe_mode == SAFE_MODE_USER || !filesystem_present()) { return; } diff --git a/shared-bindings/supervisor/SafeModeReason.c b/shared-bindings/supervisor/SafeModeReason.c index 180b7a1530..2da03bdced 100644 --- a/shared-bindings/supervisor/SafeModeReason.c +++ b/shared-bindings/supervisor/SafeModeReason.c @@ -137,7 +137,11 @@ MAKE_ENUM_MAP(supervisor_safe_mode_reason) { MAKE_ENUM_MAP_ENTRY(safe_mode_reason, USB_TOO_MANY_INTERFACE_NAMES), //| USER: object -//| """The user pressed one or more buttons to enter safe mode.""" +//| """The user pressed one or more buttons to enter safe mode. +//| This safe mode does **not** cause ``safemode.py`` to be run, since its purpose +//| is to prevent all user code from running. +//| This allows errors in ``safemode.py`` to be corrected easily. +//| """ //| MAKE_ENUM_MAP_ENTRY(safe_mode_reason, USER),