From 41d494df0b35bb845a981c07a27ccdbed720d7f6 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 27 Dec 2021 18:58:24 -0500 Subject: [PATCH 1/2] go into safe mode if not CIRCUITPY available --- locale/circuitpython.pot | 4 ++++ main.c | 6 +++++- shared-module/storage/__init__.c | 2 +- supervisor/filesystem.h | 2 +- supervisor/shared/filesystem.c | 18 ++++++++++-------- supervisor/shared/safe_mode.c | 2 ++ supervisor/shared/safe_mode.h | 1 + supervisor/stub/filesystem.c | 3 ++- 8 files changed, 26 insertions(+), 12 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 59a8dcd1b1..7ca30dc063 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -659,6 +659,10 @@ msgstr "" msgid "CBC blocks must be multiples of 16 bytes" msgstr "" +#: supervisor/shared/safe_mode.c +msgid "CIRCUITPY drive could not be found or created." +msgstr "" + #: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c msgid "CRC or checksum was invalid" msgstr "" diff --git a/main.c b/main.c index a962405498..3f9d0f2cae 100644 --- a/main.c +++ b/main.c @@ -811,7 +811,11 @@ int __attribute__((used)) main(void) { // Create a new filesystem only if we're not in a safe mode. // A power brownout here could make it appear as if there's // no SPI flash filesystem, and we might erase the existing one. - filesystem_init(safe_mode == NO_SAFE_MODE, false); + + // Check whether CIRCUITPY is available. Don't check if it already hasn't been found. + if (safe_mode != NO_CIRCUITPY && !filesystem_init(safe_mode == NO_SAFE_MODE, false)) { + reset_into_safe_mode(NO_CIRCUITPY); + } // displays init after filesystem, since they could share the flash SPI board_init(); diff --git a/shared-module/storage/__init__.c b/shared-module/storage/__init__.c index 620e536ba4..e36d3ec21e 100644 --- a/shared-module/storage/__init__.c +++ b/shared-module/storage/__init__.c @@ -271,7 +271,7 @@ void common_hal_storage_erase_filesystem(void) { usb_disconnect(); #endif mp_hal_delay_ms(1000); - filesystem_init(false, true); // Force a re-format. + (void)filesystem_init(false, true); // Force a re-format. Ignore failure. common_hal_mcu_reset(); // We won't actually get here, since we're resetting. } diff --git a/supervisor/filesystem.h b/supervisor/filesystem.h index c7c951a5e6..6f4faf0b82 100644 --- a/supervisor/filesystem.h +++ b/supervisor/filesystem.h @@ -35,7 +35,7 @@ extern volatile bool filesystem_flush_requested; void filesystem_background(void); void filesystem_tick(void); -void filesystem_init(bool create_allowed, bool force_create); +bool filesystem_init(bool create_allowed, bool force_create); void filesystem_flush(void); bool filesystem_present(void); void filesystem_set_internal_writable_by_usb(bool usb_writable); diff --git a/supervisor/shared/filesystem.c b/supervisor/shared/filesystem.c index 2ab64e5dbd..820dbe9783 100644 --- a/supervisor/shared/filesystem.c +++ b/supervisor/shared/filesystem.c @@ -86,7 +86,7 @@ static void make_sample_code_file(FATFS *fatfs) { // we don't make this function static because it needs a lot of stack and we // want it to be executed without using stack within main() function -void filesystem_init(bool create_allowed, bool force_create) { +bool filesystem_init(bool create_allowed, bool force_create) { // init the vfs object fs_user_mount_t *vfs_fat = &_internal_vfs; vfs_fat->blockdev.flags = 0; @@ -102,11 +102,11 @@ void filesystem_init(bool create_allowed, bool force_create) { formats |= FM_EXFAT | FM_FAT32; #endif res = f_mkfs(&vfs_fat->fatfs, formats, 0, working_buf, sizeof(working_buf)); + if (res != FR_OK) { + return false; + } // Flush the new file system to make sure it's repaired immediately. supervisor_flash_flush(); - if (res != FR_OK) { - return; - } // set label #ifdef CIRCUITPY_DRIVE_LABEL @@ -115,13 +115,13 @@ void filesystem_init(bool create_allowed, bool force_create) { res = f_setlabel(&vfs_fat->fatfs, "CIRCUITPY"); #endif if (res != FR_OK) { - return; + return false; } // inhibit file indexing on MacOS res = f_mkdir(&vfs_fat->fatfs, "/.fseventsd"); if (res != FR_OK) { - return; + return false; } make_empty_file(&vfs_fat->fatfs, "/.metadata_never_index"); make_empty_file(&vfs_fat->fatfs, "/.Trashes"); @@ -132,13 +132,13 @@ void filesystem_init(bool create_allowed, bool force_create) { // create empty lib directory res = f_mkdir(&vfs_fat->fatfs, "/lib"); if (res != FR_OK) { - return; + return false; } // and ensure everything is flushed supervisor_flash_flush(); } else if (res != FR_OK) { - return; + return false; } mp_vfs_mount_t *vfs = &_mp_vfs; vfs->str = "/"; @@ -150,6 +150,8 @@ void filesystem_init(bool create_allowed, bool force_create) { // The current directory is used as the boot up directory. // It is set to the internal flash filesystem by default. MP_STATE_PORT(vfs_cur) = vfs; + + return true; } void filesystem_flush(void) { diff --git a/supervisor/shared/safe_mode.c b/supervisor/shared/safe_mode.c index 59b5ab838d..62006baf98 100644 --- a/supervisor/shared/safe_mode.c +++ b/supervisor/shared/safe_mode.c @@ -175,6 +175,8 @@ void print_safe_mode_message(safe_mode_t reason) { case WATCHDOG_RESET: message = translate("Watchdog timer expired."); break; + case NO_CIRCUITPY: + message = translate("CIRCUITPY drive could not be found or created."); default: break; } diff --git a/supervisor/shared/safe_mode.h b/supervisor/shared/safe_mode.h index 600abd7672..0c8d018bfe 100644 --- a/supervisor/shared/safe_mode.h +++ b/supervisor/shared/safe_mode.h @@ -48,6 +48,7 @@ typedef enum { USB_TOO_MANY_INTERFACE_NAMES, USB_BOOT_DEVICE_NOT_INTERFACE_ZERO, NO_HEAP, + NO_CIRCUITPY, } safe_mode_t; safe_mode_t wait_for_safe_mode_reset(void); diff --git a/supervisor/stub/filesystem.c b/supervisor/stub/filesystem.c index 2b518f4142..3d2bf5e3be 100644 --- a/supervisor/stub/filesystem.c +++ b/supervisor/stub/filesystem.c @@ -26,9 +26,10 @@ #include "supervisor/filesystem.h" -void filesystem_init(bool create_allowed, bool force_create) { +bool filesystem_init(bool create_allowed, bool force_create) { (void)create_allowed; (void)force_create; + return true; } void filesystem_flush(void) { From 3c9f7f6ae91379edc46df57160d1b2df0a229e24 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 27 Dec 2021 19:36:11 -0500 Subject: [PATCH 2/2] address review comments --- main.c | 2 +- supervisor/shared/safe_mode.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 3f9d0f2cae..e0d99e3143 100644 --- a/main.c +++ b/main.c @@ -813,7 +813,7 @@ int __attribute__((used)) main(void) { // no SPI flash filesystem, and we might erase the existing one. // Check whether CIRCUITPY is available. Don't check if it already hasn't been found. - if (safe_mode != NO_CIRCUITPY && !filesystem_init(safe_mode == NO_SAFE_MODE, false)) { + if ((safe_mode != NO_CIRCUITPY) && !filesystem_init(safe_mode == NO_SAFE_MODE, false)) { reset_into_safe_mode(NO_CIRCUITPY); } diff --git a/supervisor/shared/safe_mode.c b/supervisor/shared/safe_mode.c index 62006baf98..4cb9d7ad61 100644 --- a/supervisor/shared/safe_mode.c +++ b/supervisor/shared/safe_mode.c @@ -177,6 +177,7 @@ void print_safe_mode_message(safe_mode_t reason) { break; case NO_CIRCUITPY: message = translate("CIRCUITPY drive could not be found or created."); + break; default: break; }