Skip to content

Commit f02cbea

Browse files
authored
Merge pull request #747 from dhalbert/reformat
add storage.erase_filesystem() to erase and reformat CIRCUITPY
2 parents a0cd94c + eaa9923 commit f02cbea

File tree

8 files changed

+44
-6
lines changed

8 files changed

+44
-6
lines changed

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ int __attribute__((used)) main(void) {
245245
// Create a new filesystem only if we're not in a safe mode.
246246
// A power brownout here could make it appear as if there's
247247
// no SPI flash filesystem, and we might erase the existing one.
248-
filesystem_init(safe_mode == NO_SAFE_MODE);
248+
filesystem_init(safe_mode == NO_SAFE_MODE, false);
249249

250250
// Reset everything and prep MicroPython to run boot.py.
251251
reset_port();

ports/atmel-samd/common-hal/storage/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#include "flash_api.h"
3030
#include "py/mperrno.h"
3131
#include "py/runtime.h"
32+
#include "shared-bindings/microcontroller/__init__.h"
3233
#include "shared-bindings/storage/__init__.h"
34+
#include "supervisor/filesystem.h"
3335
#include "usb.h"
3436

3537
extern volatile bool mp_msc_enabled;
@@ -47,3 +49,9 @@ void common_hal_storage_remount(const char* mount_path, bool readonly) {
4749

4850
flash_set_usb_writable(readonly);
4951
}
52+
53+
void common_hal_storage_erase_filesystem(void) {
54+
filesystem_init(false, true); // Force a re-format.
55+
common_hal_mcu_reset();
56+
// We won't actually get here, since we're resetting.
57+
}

ports/atmel-samd/supervisor/filesystem.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void make_empty_file(FATFS *fatfs, const char *path) {
5050

5151
// we don't make this function static because it needs a lot of stack and we
5252
// want it to be executed without using stack within main() function
53-
void filesystem_init(bool create_allowed) {
53+
void filesystem_init(bool create_allowed, bool force_create) {
5454
// init the vfs object
5555
fs_user_mount_t *vfs_fat = &fs_user_mount_flash;
5656
vfs_fat->flags = 0;
@@ -59,11 +59,11 @@ void filesystem_init(bool create_allowed) {
5959
// try to mount the flash
6060
FRESULT res = f_mount(&vfs_fat->fatfs);
6161

62-
if (res == FR_NO_FILESYSTEM && create_allowed) {
63-
// no filesystem so create a fresh one
62+
if ((res == FR_NO_FILESYSTEM && create_allowed) || force_create) {
63+
// No filesystem so create a fresh one, or reformat has been requested.
6464
uint8_t working_buf[_MAX_SS];
6565
res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf));
66-
// Flush the new file system to make sure its repaired immediately.
66+
// Flush the new file system to make sure it's repaired immediately.
6767
flash_flush();
6868
if (res != FR_OK) {
6969
return;

ports/esp8266/common-hal/storage/__init__.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
void common_hal_storage_remount(const char* mount_path, bool readonly) {
3333
mp_raise_NotImplementedError("");
3434
}
35+
36+
void common_hal_storage_erase_filesystem() {
37+
mp_raise_NotImplementedError("Use esptool to erase flash and re-upload Python instead");
38+
}

ports/nrf/common-hal/storage/__init__.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ void common_hal_storage_remount(const char* mount_path, bool readonly) {
3737
mp_raise_OSError(MP_EINVAL);
3838
}
3939
}
40+
41+
void common_hal_storage_erase_filesystem() {
42+
mp_raise_NotImplementedError("");
43+
}

shared-bindings/storage/__init__.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,34 @@ mp_obj_t storage_getmount(const mp_obj_t mnt_in) {
131131
}
132132
MP_DEFINE_CONST_FUN_OBJ_1(storage_getmount_obj, storage_getmount);
133133

134+
//| .. function:: erase_filesystem()
135+
//|
136+
//| Erase and re-create the ``CIRCUITPY`` filesystem.
137+
//|
138+
//| On boards that present USB-visible ``CIRCUITPY`` drive (e.g., SAMD21 and SAMD51),
139+
//| then call `microcontroller.reset()` to restart CircuitPython and have the
140+
//| host computer remount CIRCUITPY.
141+
//|
142+
//| This function can be called from the REPL when ``CIRCUITPY``
143+
//| has become corrupted.
144+
//|
145+
//| .. warning:: All the data on ``CIRCUITPY`` will be lost, and
146+
//| CircuitPython will restart on certain boards.
147+
148+
mp_obj_t storage_erase_filesystem(void) {
149+
common_hal_storage_erase_filesystem();
150+
return mp_const_none;
151+
}
152+
MP_DEFINE_CONST_FUN_OBJ_0(storage_erase_filesystem_obj, storage_erase_filesystem);
153+
134154
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
135155
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) },
136156

137157
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
138158
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
139159
{ MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
140160
{ MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) },
161+
{ MP_ROM_QSTR(MP_QSTR_erase_filesystem), MP_ROM_PTR(&storage_erase_filesystem_obj) },
141162

142163
//| .. class:: VfsFat(block_device)
143164
//|

shared-bindings/storage/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ void common_hal_storage_umount_path(const char* path);
3535
void common_hal_storage_umount_object(mp_obj_t vfs_obj);
3636
void common_hal_storage_remount(const char* path, bool readonly);
3737
mp_obj_t common_hal_storage_getmount(const char* path);
38+
void common_hal_storage_erase_filesystem(void);
3839

3940
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H

supervisor/filesystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#include <stdbool.h>
3131

32-
void filesystem_init(bool create_allowed);
32+
void filesystem_init(bool create_allowed, bool force_create);
3333
void filesystem_flush(void);
3434
void filesystem_writable_by_python(bool writable);
3535
bool filesystem_present(void);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy