2018-12-06 17:24:20 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "supervisor/shared/safe_mode.h"
|
|
|
|
|
|
|
|
#include "mphalport.h"
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
#if defined(CIRCUITPY_BOOT_BUTTON)
|
2018-12-06 17:24:20 -05:00
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2021-01-20 19:47:18 -05:00
|
|
|
#endif
|
2020-11-21 23:29:52 -05:00
|
|
|
#include "shared-bindings/microcontroller/Processor.h"
|
|
|
|
#include "shared-bindings/microcontroller/ResetReason.h"
|
2018-12-06 17:24:20 -05:00
|
|
|
|
2023-02-28 18:07:35 -05:00
|
|
|
#include "supervisor/linker.h"
|
2018-12-06 17:24:20 -05:00
|
|
|
#include "supervisor/serial.h"
|
|
|
|
#include "supervisor/shared/rgb_led_colors.h"
|
2021-04-16 19:18:01 -04:00
|
|
|
#include "supervisor/shared/status_leds.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2019-11-18 09:22:41 -05:00
|
|
|
#include "supervisor/shared/tick.h"
|
2018-12-06 17:24:20 -05:00
|
|
|
|
|
|
|
#define SAFE_MODE_DATA_GUARD 0xad0000af
|
|
|
|
#define SAFE_MODE_DATA_GUARD_MASK 0xff0000ff
|
|
|
|
|
2023-02-11 23:50:20 -05:00
|
|
|
static safe_mode_t _safe_mode;
|
|
|
|
|
|
|
|
safe_mode_t get_safe_mode(void) {
|
|
|
|
return _safe_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_safe_mode(safe_mode_t safe_mode) {
|
|
|
|
_safe_mode = safe_mode;
|
|
|
|
}
|
2018-12-06 17:24:20 -05:00
|
|
|
|
|
|
|
safe_mode_t wait_for_safe_mode_reset(void) {
|
|
|
|
uint32_t reset_state = port_get_saved_word();
|
2023-02-11 23:50:20 -05:00
|
|
|
safe_mode_t safe_mode = SAFE_MODE_NONE;
|
2018-12-06 17:24:20 -05:00
|
|
|
if ((reset_state & SAFE_MODE_DATA_GUARD_MASK) == SAFE_MODE_DATA_GUARD) {
|
|
|
|
safe_mode = (reset_state & ~SAFE_MODE_DATA_GUARD_MASK) >> 8;
|
|
|
|
}
|
2023-02-11 23:50:20 -05:00
|
|
|
if (safe_mode != SAFE_MODE_NONE) {
|
2018-12-06 17:24:20 -05:00
|
|
|
port_set_saved_word(SAFE_MODE_DATA_GUARD);
|
2023-02-11 23:50:20 -05:00
|
|
|
_safe_mode = safe_mode;
|
2018-12-06 17:24:20 -05:00
|
|
|
return safe_mode;
|
2021-07-14 19:45:47 -04:00
|
|
|
} else {
|
2023-02-11 23:50:20 -05:00
|
|
|
_safe_mode = SAFE_MODE_NONE;
|
2018-12-06 17:24:20 -05:00
|
|
|
}
|
2020-11-21 23:29:52 -05:00
|
|
|
|
|
|
|
const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason();
|
|
|
|
if (reset_reason != RESET_REASON_POWER_ON &&
|
2021-03-01 00:01:02 -05:00
|
|
|
reset_reason != RESET_REASON_RESET_PIN &&
|
2021-07-14 19:45:47 -04:00
|
|
|
reset_reason != RESET_REASON_UNKNOWN &&
|
|
|
|
reset_reason != RESET_REASON_SOFTWARE) {
|
2023-02-11 23:50:20 -05:00
|
|
|
return SAFE_MODE_NONE;
|
2020-10-27 20:55:03 -04:00
|
|
|
}
|
2023-01-28 14:31:59 -05:00
|
|
|
#if CIRCUITPY_SKIP_SAFE_MODE_WAIT
|
2023-02-11 23:50:20 -05:00
|
|
|
return SAFE_MODE_NONE;
|
2021-08-30 17:05:00 -04:00
|
|
|
#endif
|
2023-02-11 23:50:20 -05:00
|
|
|
port_set_saved_word(SAFE_MODE_DATA_GUARD | (SAFE_MODE_USER << 8));
|
2018-12-06 17:24:20 -05:00
|
|
|
// Wait for a while to allow for reset.
|
2021-04-16 19:18:01 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_STATUS_LED
|
|
|
|
status_led_init();
|
2018-12-06 17:24:20 -05:00
|
|
|
#endif
|
2020-09-11 11:36:54 -04:00
|
|
|
#ifdef CIRCUITPY_BOOT_BUTTON
|
|
|
|
digitalio_digitalinout_obj_t boot_button;
|
|
|
|
common_hal_digitalio_digitalinout_construct(&boot_button, CIRCUITPY_BOOT_BUTTON);
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_input(&boot_button, PULL_UP);
|
|
|
|
#endif
|
2019-11-18 09:22:41 -05:00
|
|
|
uint64_t start_ticks = supervisor_ticks_ms64();
|
2018-12-06 17:24:20 -05:00
|
|
|
uint64_t diff = 0;
|
2021-04-16 19:18:01 -04:00
|
|
|
bool boot_in_safe_mode = false;
|
2021-05-18 20:37:21 -04:00
|
|
|
while (diff < 1000) {
|
2021-04-16 19:18:01 -04:00
|
|
|
#ifdef CIRCUITPY_STATUS_LED
|
2021-05-19 19:22:07 -04:00
|
|
|
// Blink on for 100, off for 100
|
|
|
|
bool led_on = (diff % 250) < 125;
|
2021-04-16 19:18:01 -04:00
|
|
|
if (led_on) {
|
|
|
|
new_status_color(SAFE_MODE);
|
|
|
|
} else {
|
|
|
|
new_status_color(BLACK);
|
|
|
|
}
|
2018-12-06 17:24:20 -05:00
|
|
|
#endif
|
2020-09-13 13:47:14 -04:00
|
|
|
#ifdef CIRCUITPY_BOOT_BUTTON
|
2020-09-11 11:36:54 -04:00
|
|
|
if (!common_hal_digitalio_digitalinout_get_value(&boot_button)) {
|
2021-04-16 19:18:01 -04:00
|
|
|
boot_in_safe_mode = true;
|
|
|
|
break;
|
2020-09-11 11:36:54 -04:00
|
|
|
}
|
|
|
|
#endif
|
2019-11-18 09:22:41 -05:00
|
|
|
diff = supervisor_ticks_ms64() - start_ticks;
|
2018-12-06 17:24:20 -05:00
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
#if CIRCUITPY_STATUS_LED
|
|
|
|
new_status_color(BLACK);
|
|
|
|
status_led_deinit();
|
2018-12-06 17:24:20 -05:00
|
|
|
#endif
|
2021-04-16 19:18:01 -04:00
|
|
|
if (boot_in_safe_mode) {
|
2023-02-11 23:50:20 -05:00
|
|
|
return SAFE_MODE_USER;
|
2021-04-16 19:18:01 -04:00
|
|
|
}
|
2023-03-18 11:17:02 -04:00
|
|
|
// Restore the original state of the saved word if no reset occurred during our wait period.
|
2021-05-19 19:22:07 -04:00
|
|
|
port_set_saved_word(reset_state);
|
2023-02-11 23:50:20 -05:00
|
|
|
return SAFE_MODE_NONE;
|
2018-12-06 17:24:20 -05:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:07:35 -05:00
|
|
|
void PLACE_IN_ITCM(safe_mode_on_next_reset)(safe_mode_t reason) {
|
2019-05-09 13:15:28 -04:00
|
|
|
port_set_saved_word(SAFE_MODE_DATA_GUARD | (reason << 8));
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:58:50 -04:00
|
|
|
// Don't inline this so it's easy to break on it from GDB.
|
2023-08-14 00:47:22 -04:00
|
|
|
void __attribute__((noinline, )) PLACE_IN_ITCM(reset_into_safe_mode)(safe_mode_t reason) {
|
2023-02-11 23:50:20 -05:00
|
|
|
if (_safe_mode > SAFE_MODE_BROWNOUT && reason > SAFE_MODE_BROWNOUT) {
|
2018-12-06 17:24:20 -05:00
|
|
|
while (true) {
|
|
|
|
// This very bad because it means running in safe mode didn't save us. Only ignore brownout
|
|
|
|
// because it may be due to a switch bouncing.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 13:15:28 -04:00
|
|
|
safe_mode_on_next_reset(reason);
|
2018-12-06 17:24:20 -05:00
|
|
|
reset_cpu();
|
|
|
|
}
|
|
|
|
|
2019-12-12 14:35:24 -05:00
|
|
|
|
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
void print_safe_mode_message(safe_mode_t reason) {
|
2023-02-11 23:50:20 -05:00
|
|
|
if (reason == SAFE_MODE_NONE) {
|
2019-05-08 18:23:40 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-05-12 15:53:52 -04:00
|
|
|
|
2023-02-13 21:29:57 -05:00
|
|
|
serial_write_compressed(translate("\nYou are in safe mode because:\n"));
|
2021-05-12 15:53:52 -04:00
|
|
|
|
2023-10-25 15:40:11 -04:00
|
|
|
mp_rom_error_text_t message = NULL;
|
2021-05-12 15:53:52 -04:00
|
|
|
|
|
|
|
// First check for safe mode reasons that do not necessarily reflect bugs.
|
2019-12-12 14:35:24 -05:00
|
|
|
|
2020-09-11 11:36:54 -04:00
|
|
|
switch (reason) {
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_BROWNOUT:
|
2023-08-19 12:14:58 -04:00
|
|
|
message = translate("Power dipped. Make sure you are providing enough power.");
|
2023-02-11 23:50:20 -05:00
|
|
|
break;
|
|
|
|
case SAFE_MODE_USER:
|
2022-10-29 22:55:59 -04:00
|
|
|
#if defined(BOARD_USER_SAFE_MODE_ACTION)
|
2021-05-20 12:57:33 -04:00
|
|
|
message = BOARD_USER_SAFE_MODE_ACTION;
|
2022-03-21 20:58:43 -04:00
|
|
|
#elif defined(CIRCUITPY_BOOT_BUTTON)
|
2023-02-13 21:29:57 -05:00
|
|
|
message = translate("You pressed the BOOT button at start up");
|
2023-02-11 23:50:20 -05:00
|
|
|
#else
|
|
|
|
message = translate("You pressed the reset button during boot.");
|
2020-09-11 11:36:54 -04:00
|
|
|
#endif
|
2021-05-12 15:53:52 -04:00
|
|
|
break;
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_NO_CIRCUITPY:
|
|
|
|
message = translate("CIRCUITPY drive could not be found or created.");
|
2021-05-12 15:53:52 -04:00
|
|
|
break;
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_PROGRAMMATIC:
|
2023-02-13 21:29:57 -05:00
|
|
|
message = translate("The `microcontroller` module was used to boot into safe mode.");
|
2020-09-11 11:36:54 -04:00
|
|
|
break;
|
2023-02-13 21:29:57 -05:00
|
|
|
#if CIRCUITPY_SAFEMODE_PY
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_SAFEMODE_PY_ERROR:
|
|
|
|
message = translate("Error in safemode.py.");
|
2021-05-12 15:53:52 -04:00
|
|
|
break;
|
2023-02-13 21:29:57 -05:00
|
|
|
#endif
|
|
|
|
case SAFE_MODE_STACK_OVERFLOW:
|
|
|
|
message = translate("Heap was corrupted because the stack was too small. Increase stack size.");
|
|
|
|
break;
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_USB_TOO_MANY_ENDPOINTS:
|
2021-05-12 15:53:52 -04:00
|
|
|
message = translate("USB devices need more endpoints than are available.");
|
|
|
|
break;
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_USB_TOO_MANY_INTERFACE_NAMES:
|
2021-05-12 15:53:52 -04:00
|
|
|
message = translate("USB devices specify too many interface names.");
|
|
|
|
break;
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_USB_BOOT_DEVICE_NOT_INTERFACE_ZERO:
|
2023-02-13 21:29:57 -05:00
|
|
|
message = translate("Boot device must be first (interface #0).");
|
2021-10-13 12:30:01 -04:00
|
|
|
break;
|
2023-02-11 23:50:20 -05:00
|
|
|
case SAFE_MODE_WATCHDOG:
|
2022-06-29 15:31:18 -04:00
|
|
|
message = translate("Internal watchdog timer expired.");
|
2021-05-12 15:53:52 -04:00
|
|
|
break;
|
2020-09-11 11:36:54 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:53:52 -04:00
|
|
|
if (message) {
|
2023-02-13 21:29:57 -05:00
|
|
|
// Non-crash safe mode.
|
2021-05-12 15:53:52 -04:00
|
|
|
serial_write_compressed(message);
|
2023-02-13 21:29:57 -05:00
|
|
|
} else {
|
|
|
|
// Something worse happened.
|
|
|
|
serial_write_compressed(translate("CircuitPython core code crashed hard. Whoops!\n"));
|
|
|
|
switch (reason) {
|
|
|
|
case SAFE_MODE_GC_ALLOC_OUTSIDE_VM:
|
|
|
|
message = translate("Heap allocation when VM not running.");
|
|
|
|
break;
|
|
|
|
case SAFE_MODE_FLASH_WRITE_FAIL:
|
|
|
|
message = translate("Failed to write internal flash.");
|
|
|
|
break;
|
|
|
|
case SAFE_MODE_HARD_FAULT:
|
2023-08-19 10:07:46 -04:00
|
|
|
message = translate("Hard fault: memory access or instruction error.");
|
2023-02-13 21:29:57 -05:00
|
|
|
break;
|
|
|
|
case SAFE_MODE_INTERRUPT_ERROR:
|
|
|
|
message = translate("Interrupt error.");
|
|
|
|
break;
|
|
|
|
case SAFE_MODE_NLR_JUMP_FAIL:
|
|
|
|
message = translate("NLR jump failed. Likely memory corruption.");
|
|
|
|
break;
|
|
|
|
case SAFE_MODE_NO_HEAP:
|
|
|
|
message = translate("Unable to allocate the heap.");
|
|
|
|
break;
|
|
|
|
case SAFE_MODE_SDK_FATAL_ERROR:
|
|
|
|
message = translate("Third-party firmware fatal error.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = translate("Unknown reason.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
serial_write_compressed(message);
|
2023-08-19 12:14:58 -04:00
|
|
|
serial_write_compressed(translate("\nPlease file an issue with your program at github.com/adafruit/circuitpython/issues."));
|
2021-05-12 15:53:52 -04:00
|
|
|
}
|
|
|
|
|
2023-02-13 21:29:57 -05:00
|
|
|
// Always tell user how to get out of safe mode.
|
|
|
|
serial_write_compressed(translate("\nPress reset to exit safe mode.\n"));
|
2020-09-13 13:47:14 -04:00
|
|
|
}
|