Skip to content

Commit 41d494d

Browse files
committed
go into safe mode if not CIRCUITPY available
1 parent ea638c0 commit 41d494d

File tree

8 files changed

+26
-12
lines changed

8 files changed

+26
-12
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,10 @@ msgstr ""
659659
msgid "CBC blocks must be multiples of 16 bytes"
660660
msgstr ""
661661

662+
#: supervisor/shared/safe_mode.c
663+
msgid "CIRCUITPY drive could not be found or created."
664+
msgstr ""
665+
662666
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
663667
msgid "CRC or checksum was invalid"
664668
msgstr ""

main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,11 @@ int __attribute__((used)) main(void) {
811811
// Create a new filesystem only if we're not in a safe mode.
812812
// A power brownout here could make it appear as if there's
813813
// no SPI flash filesystem, and we might erase the existing one.
814-
filesystem_init(safe_mode == NO_SAFE_MODE, false);
814+
815+
// Check whether CIRCUITPY is available. Don't check if it already hasn't been found.
816+
if (safe_mode != NO_CIRCUITPY && !filesystem_init(safe_mode == NO_SAFE_MODE, false)) {
817+
reset_into_safe_mode(NO_CIRCUITPY);
818+
}
815819

816820
// displays init after filesystem, since they could share the flash SPI
817821
board_init();

shared-module/storage/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void common_hal_storage_erase_filesystem(void) {
271271
usb_disconnect();
272272
#endif
273273
mp_hal_delay_ms(1000);
274-
filesystem_init(false, true); // Force a re-format.
274+
(void)filesystem_init(false, true); // Force a re-format. Ignore failure.
275275
common_hal_mcu_reset();
276276
// We won't actually get here, since we're resetting.
277277
}

supervisor/filesystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern volatile bool filesystem_flush_requested;
3535

3636
void filesystem_background(void);
3737
void filesystem_tick(void);
38-
void filesystem_init(bool create_allowed, bool force_create);
38+
bool filesystem_init(bool create_allowed, bool force_create);
3939
void filesystem_flush(void);
4040
bool filesystem_present(void);
4141
void filesystem_set_internal_writable_by_usb(bool usb_writable);

supervisor/shared/filesystem.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static void make_sample_code_file(FATFS *fatfs) {
8686

8787
// we don't make this function static because it needs a lot of stack and we
8888
// want it to be executed without using stack within main() function
89-
void filesystem_init(bool create_allowed, bool force_create) {
89+
bool filesystem_init(bool create_allowed, bool force_create) {
9090
// init the vfs object
9191
fs_user_mount_t *vfs_fat = &_internal_vfs;
9292
vfs_fat->blockdev.flags = 0;
@@ -102,11 +102,11 @@ void filesystem_init(bool create_allowed, bool force_create) {
102102
formats |= FM_EXFAT | FM_FAT32;
103103
#endif
104104
res = f_mkfs(&vfs_fat->fatfs, formats, 0, working_buf, sizeof(working_buf));
105-
// Flush the new file system to make sure it's repaired immediately.
106-
supervisor_flash_flush();
107105
if (res != FR_OK) {
108-
return;
106+
return false;
109107
}
108+
// Flush the new file system to make sure it's repaired immediately.
109+
supervisor_flash_flush();
110110

111111
// set label
112112
#ifdef CIRCUITPY_DRIVE_LABEL
@@ -115,13 +115,13 @@ void filesystem_init(bool create_allowed, bool force_create) {
115115
res = f_setlabel(&vfs_fat->fatfs, "CIRCUITPY");
116116
#endif
117117
if (res != FR_OK) {
118-
return;
118+
return false;
119119
}
120120

121121
// inhibit file indexing on MacOS
122122
res = f_mkdir(&vfs_fat->fatfs, "/.fseventsd");
123123
if (res != FR_OK) {
124-
return;
124+
return false;
125125
}
126126
make_empty_file(&vfs_fat->fatfs, "/.metadata_never_index");
127127
make_empty_file(&vfs_fat->fatfs, "/.Trashes");
@@ -132,13 +132,13 @@ void filesystem_init(bool create_allowed, bool force_create) {
132132
// create empty lib directory
133133
res = f_mkdir(&vfs_fat->fatfs, "/lib");
134134
if (res != FR_OK) {
135-
return;
135+
return false;
136136
}
137137

138138
// and ensure everything is flushed
139139
supervisor_flash_flush();
140140
} else if (res != FR_OK) {
141-
return;
141+
return false;
142142
}
143143
mp_vfs_mount_t *vfs = &_mp_vfs;
144144
vfs->str = "/";
@@ -150,6 +150,8 @@ void filesystem_init(bool create_allowed, bool force_create) {
150150
// The current directory is used as the boot up directory.
151151
// It is set to the internal flash filesystem by default.
152152
MP_STATE_PORT(vfs_cur) = vfs;
153+
154+
return true;
153155
}
154156

155157
void filesystem_flush(void) {

supervisor/shared/safe_mode.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ void print_safe_mode_message(safe_mode_t reason) {
175175
case WATCHDOG_RESET:
176176
message = translate("Watchdog timer expired.");
177177
break;
178+
case NO_CIRCUITPY:
179+
message = translate("CIRCUITPY drive could not be found or created.");
178180
default:
179181
break;
180182
}

supervisor/shared/safe_mode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef enum {
4848
USB_TOO_MANY_INTERFACE_NAMES,
4949
USB_BOOT_DEVICE_NOT_INTERFACE_ZERO,
5050
NO_HEAP,
51+
NO_CIRCUITPY,
5152
} safe_mode_t;
5253

5354
safe_mode_t wait_for_safe_mode_reset(void);

supervisor/stub/filesystem.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626

2727
#include "supervisor/filesystem.h"
2828

29-
void filesystem_init(bool create_allowed, bool force_create) {
29+
bool filesystem_init(bool create_allowed, bool force_create) {
3030
(void)create_allowed;
3131
(void)force_create;
32+
return true;
3233
}
3334

3435
void filesystem_flush(void) {

0 commit comments

Comments
 (0)