Merge pull request #747 from dhalbert/reformat

add storage.erase_filesystem() to erase and reformat CIRCUITPY
This commit is contained in:
Scott Shawcroft 2018-04-10 10:26:07 -07:00 committed by GitHub
commit f02cbeabab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 6 deletions

2
main.c
View File

@ -245,7 +245,7 @@ int __attribute__((used)) main(void) {
// Create a new filesystem only if we're not in a safe mode. // 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 // A power brownout here could make it appear as if there's
// no SPI flash filesystem, and we might erase the existing one. // no SPI flash filesystem, and we might erase the existing one.
filesystem_init(safe_mode == NO_SAFE_MODE); filesystem_init(safe_mode == NO_SAFE_MODE, false);
// Reset everything and prep MicroPython to run boot.py. // Reset everything and prep MicroPython to run boot.py.
reset_port(); reset_port();

View File

@ -29,7 +29,9 @@
#include "flash_api.h" #include "flash_api.h"
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/storage/__init__.h" #include "shared-bindings/storage/__init__.h"
#include "supervisor/filesystem.h"
#include "usb.h" #include "usb.h"
extern volatile bool mp_msc_enabled; extern volatile bool mp_msc_enabled;
@ -47,3 +49,9 @@ void common_hal_storage_remount(const char* mount_path, bool readonly) {
flash_set_usb_writable(readonly); flash_set_usb_writable(readonly);
} }
void common_hal_storage_erase_filesystem(void) {
filesystem_init(false, true); // Force a re-format.
common_hal_mcu_reset();
// We won't actually get here, since we're resetting.
}

View File

@ -50,7 +50,7 @@ static void make_empty_file(FATFS *fatfs, const char *path) {
// we don't make this function static because it needs a lot of stack and we // 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 // want it to be executed without using stack within main() function
void filesystem_init(bool create_allowed) { void filesystem_init(bool create_allowed, bool force_create) {
// init the vfs object // init the vfs object
fs_user_mount_t *vfs_fat = &fs_user_mount_flash; fs_user_mount_t *vfs_fat = &fs_user_mount_flash;
vfs_fat->flags = 0; vfs_fat->flags = 0;
@ -59,11 +59,11 @@ void filesystem_init(bool create_allowed) {
// try to mount the flash // try to mount the flash
FRESULT res = f_mount(&vfs_fat->fatfs); FRESULT res = f_mount(&vfs_fat->fatfs);
if (res == FR_NO_FILESYSTEM && create_allowed) { if ((res == FR_NO_FILESYSTEM && create_allowed) || force_create) {
// no filesystem so create a fresh one // No filesystem so create a fresh one, or reformat has been requested.
uint8_t working_buf[_MAX_SS]; uint8_t working_buf[_MAX_SS];
res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)); res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf));
// Flush the new file system to make sure its repaired immediately. // Flush the new file system to make sure it's repaired immediately.
flash_flush(); flash_flush();
if (res != FR_OK) { if (res != FR_OK) {
return; return;

View File

@ -32,3 +32,7 @@
void common_hal_storage_remount(const char* mount_path, bool readonly) { void common_hal_storage_remount(const char* mount_path, bool readonly) {
mp_raise_NotImplementedError(""); mp_raise_NotImplementedError("");
} }
void common_hal_storage_erase_filesystem() {
mp_raise_NotImplementedError("Use esptool to erase flash and re-upload Python instead");
}

View File

@ -37,3 +37,7 @@ void common_hal_storage_remount(const char* mount_path, bool readonly) {
mp_raise_OSError(MP_EINVAL); mp_raise_OSError(MP_EINVAL);
} }
} }
void common_hal_storage_erase_filesystem() {
mp_raise_NotImplementedError("");
}

View File

@ -131,6 +131,26 @@ mp_obj_t storage_getmount(const mp_obj_t mnt_in) {
} }
MP_DEFINE_CONST_FUN_OBJ_1(storage_getmount_obj, storage_getmount); MP_DEFINE_CONST_FUN_OBJ_1(storage_getmount_obj, storage_getmount);
//| .. function:: erase_filesystem()
//|
//| Erase and re-create the ``CIRCUITPY`` filesystem.
//|
//| On boards that present USB-visible ``CIRCUITPY`` drive (e.g., SAMD21 and SAMD51),
//| then call `microcontroller.reset()` to restart CircuitPython and have the
//| host computer remount CIRCUITPY.
//|
//| This function can be called from the REPL when ``CIRCUITPY``
//| has become corrupted.
//|
//| .. warning:: All the data on ``CIRCUITPY`` will be lost, and
//| CircuitPython will restart on certain boards.
mp_obj_t storage_erase_filesystem(void) {
common_hal_storage_erase_filesystem();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(storage_erase_filesystem_obj, storage_erase_filesystem);
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = { STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) },
@ -138,6 +158,7 @@ STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) }, { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
{ MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) }, { MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
{ MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) }, { MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) },
{ MP_ROM_QSTR(MP_QSTR_erase_filesystem), MP_ROM_PTR(&storage_erase_filesystem_obj) },
//| .. class:: VfsFat(block_device) //| .. class:: VfsFat(block_device)
//| //|

View File

@ -35,5 +35,6 @@ void common_hal_storage_umount_path(const char* path);
void common_hal_storage_umount_object(mp_obj_t vfs_obj); void common_hal_storage_umount_object(mp_obj_t vfs_obj);
void common_hal_storage_remount(const char* path, bool readonly); void common_hal_storage_remount(const char* path, bool readonly);
mp_obj_t common_hal_storage_getmount(const char* path); mp_obj_t common_hal_storage_getmount(const char* path);
void common_hal_storage_erase_filesystem(void);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H

View File

@ -29,7 +29,7 @@
#include <stdbool.h> #include <stdbool.h>
void filesystem_init(bool create_allowed); void filesystem_init(bool create_allowed, bool force_create);
void filesystem_flush(void); void filesystem_flush(void);
void filesystem_writable_by_python(bool writable); void filesystem_writable_by_python(bool writable);
bool filesystem_present(void); bool filesystem_present(void);