From aa8c262d141d78c148a89de63b54623ad2592e0e Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 9 Apr 2018 12:52:42 -0400 Subject: [PATCH 1/9] add storage.erase_filesystem() to erase and reformat CIRCUITPY --- main.c | 2 +- ports/atmel-samd/supervisor/filesystem.c | 8 ++++---- shared-bindings/storage/__init__.c | 19 +++++++++++++++++++ shared-bindings/storage/__init__.h | 1 + shared-module/storage/__init__.c | 8 ++++++++ supervisor/filesystem.h | 2 +- 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 0ad05060825d8..b88fd735bac4c 100644 --- a/main.c +++ b/main.c @@ -245,7 +245,7 @@ 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); + filesystem_init(safe_mode == NO_SAFE_MODE, false); // Reset everything and prep MicroPython to run boot.py. reset_port(); diff --git a/ports/atmel-samd/supervisor/filesystem.c b/ports/atmel-samd/supervisor/filesystem.c index bff78a41c29f1..a165d8887db1a 100644 --- a/ports/atmel-samd/supervisor/filesystem.c +++ b/ports/atmel-samd/supervisor/filesystem.c @@ -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 // want it to be executed without using stack within main() function -void filesystem_init(bool create_allowed) { +void filesystem_init(bool create_allowed, bool create_force) { // init the vfs object fs_user_mount_t *vfs_fat = &fs_user_mount_flash; vfs_fat->flags = 0; @@ -59,11 +59,11 @@ void filesystem_init(bool create_allowed) { // try to mount the flash FRESULT res = f_mount(&vfs_fat->fatfs); - if (res == FR_NO_FILESYSTEM && create_allowed) { - // no filesystem so create a fresh one + if ((res == FR_NO_FILESYSTEM && create_allowed) || create_force) { + // No filesystem so create a fresh one, or reformat has been requested. uint8_t working_buf[_MAX_SS]; 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(); if (res != FR_OK) { return; diff --git a/shared-bindings/storage/__init__.c b/shared-bindings/storage/__init__.c index 734cabcc1c95d..2195d22423e5d 100644 --- a/shared-bindings/storage/__init__.c +++ b/shared-bindings/storage/__init__.c @@ -131,6 +131,24 @@ mp_obj_t storage_getmount(const mp_obj_t mnt_in) { } MP_DEFINE_CONST_FUN_OBJ_1(storage_getmount_obj, storage_getmount); +//| .. function:: erase_filesystem() +//| +//| Erase and re-create the ``CIRCUITPY`` filesystem. 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. + +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[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) }, @@ -138,6 +156,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_remount), MP_ROM_PTR(&storage_remount_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) //| diff --git a/shared-bindings/storage/__init__.h b/shared-bindings/storage/__init__.h index 76b9ae854f73a..6a0cb9fc71ef2 100644 --- a/shared-bindings/storage/__init__.h +++ b/shared-bindings/storage/__init__.h @@ -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_remount(const char* path, bool readonly); 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 diff --git a/shared-module/storage/__init__.c b/shared-module/storage/__init__.c index 5b0bbe01ecb65..f1fa74990a5de 100644 --- a/shared-module/storage/__init__.c +++ b/shared-module/storage/__init__.c @@ -32,7 +32,9 @@ #include "py/mperrno.h" #include "py/obj.h" #include "py/runtime.h" +#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/storage/__init__.h" +#include "supervisor/filesystem.h" STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { if (vfs == MP_VFS_NONE) { @@ -125,3 +127,9 @@ void common_hal_storage_umount_path(const char* mount_path) { mp_obj_t common_hal_storage_getmount(const char *mount_path) { return storage_object_from_path(mount_path); } + +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. +} diff --git a/supervisor/filesystem.h b/supervisor/filesystem.h index cfd1ed0273c5b..63da0bcab5341 100644 --- a/supervisor/filesystem.h +++ b/supervisor/filesystem.h @@ -29,7 +29,7 @@ #include -void filesystem_init(bool create_allowed); +void filesystem_init(bool create_allowed, bool create_force); void filesystem_flush(void); void filesystem_writable_by_python(bool writable); bool filesystem_present(void); From eb7d0e317a50690c388c67a2519df08ba059cab7 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 9 Apr 2018 13:31:44 -0500 Subject: [PATCH 2/9] Create genhdr/ directory in time --- ports/atmel-samd/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 99c6231c88f30..d675e6f2d6173 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -388,7 +388,7 @@ $(BUILD)/autogen_usb_descriptor.c $(BUILD)/genhdr/autogen_usb_descriptor.h: auto .INTERMEDIATE: autogen_usb_descriptor.intermediate autogen_usb_descriptor.intermediate: tools/gen_usb_descriptor.py Makefile - install -d $(BUILD) + install -d $(BUILD)/genhdr python3 tools/gen_usb_descriptor.py \ --manufacturer $(USB_MANUFACTURER)\ --product $(USB_PRODUCT)\ From 3b511e503aebba56d14c796e87380b3cb92ad9d5 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 9 Apr 2018 15:57:16 -0700 Subject: [PATCH 3/9] Prevent a heap pointer from living on the stack longer than we need. --- lib/utils/pyexec.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c index f96deb9e1ce11..718c6828cca12 100644 --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -90,6 +90,8 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input qstr source_name = lex->source_name; mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL); + // Clear the parse tree because it has a heap pointer we don't need anymore. + *((uint32_t volatile*) &parse_tree.chunk) = 0; #else mp_raise_msg(&mp_type_RuntimeError, "script compilation not supported"); #endif From fd70383aa0c47765c6ae02584886d000be04f325 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 9 Apr 2018 15:58:33 -0700 Subject: [PATCH 4/9] Fix rom qstr pool length. --- tools/analyze_heap_dump.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tools/analyze_heap_dump.py b/tools/analyze_heap_dump.py index d5da884e482ae..55342ac693517 100644 --- a/tools/analyze_heap_dump.py +++ b/tools/analyze_heap_dump.py @@ -56,8 +56,6 @@ help="Draw the heap layout") @click.option("--draw-heap-ownership/--no-draw-heap-ownership", default=False, help="Draw the ownership graph of blocks on the heap") -@click.option("--draw-heap-ownership/--no-draw-heap-ownership", default=False, - help="Draw the ownership graph of blocks on the heap") @click.option("--analyze-snapshots", default="last", type=click.Choice(['all', 'last'])) def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_contents, print_unknown_types, print_block_state, print_conflicting_symbols, @@ -352,7 +350,7 @@ def find_qstr(qstr_index): else: rom_offset = pool_ptr - rom_start prev, total_prev_len, alloc, length = struct.unpack_from("= total_prev_len: offset = (qstr_index - total_prev_len) * 4 + 16 From 1e9a27177c5d43ffcc3d02c94b0bc12eb4e7ae34 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 9 Apr 2018 21:17:26 -0500 Subject: [PATCH 5/9] esp8266: Disable "strict aliasing" in compiler like in atmel-samd This caused a fatal compiler diagnostic after #750. This compiler flag is already specified in the atmel-samd builds, so it makes sense to do it here for the same reasons. --- ports/esp8266/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 4a598b629107b..06439a19eaa7d 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -48,7 +48,7 @@ CFLAGS_XTENSA = -fsingle-precision-constant -Wdouble-promotion \ -Wl,-EL -mlongcalls -mtext-section-literals -mforce-l32 \ -DLWIP_OPEN_SRC -CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ +CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -Wno-strict-aliasing -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ $(CFLAGS_XTENSA) $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) LDSCRIPT = esp8266.ld From 5f98953ed89f5e89b92f500788ae4ff0806886c8 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 10 Apr 2018 12:08:41 -0400 Subject: [PATCH 6/9] esp8266 and nrf: raise NotImplementedError --- ports/atmel-samd/common-hal/storage/__init__.c | 8 ++++++++ ports/esp8266/common-hal/storage/__init__.c | 7 +++++++ ports/nrf/common-hal/storage/__init__.c | 4 ++++ shared-bindings/storage/__init__.c | 8 +++++--- shared-module/storage/__init__.c | 6 ------ supervisor/filesystem.h | 2 +- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/ports/atmel-samd/common-hal/storage/__init__.c b/ports/atmel-samd/common-hal/storage/__init__.c index c4cb83142731f..3838655588de2 100644 --- a/ports/atmel-samd/common-hal/storage/__init__.c +++ b/ports/atmel-samd/common-hal/storage/__init__.c @@ -29,7 +29,9 @@ #include "flash_api.h" #include "py/mperrno.h" #include "py/runtime.h" +#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/storage/__init__.h" +#include "supervisor/filesystem.h" #include "usb.h" 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); } + +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. +} diff --git a/ports/esp8266/common-hal/storage/__init__.c b/ports/esp8266/common-hal/storage/__init__.c index a4c3a387aca60..dd732e3b7cab3 100644 --- a/ports/esp8266/common-hal/storage/__init__.c +++ b/ports/esp8266/common-hal/storage/__init__.c @@ -26,9 +26,16 @@ #include +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#include "py/mperrno.h" #include "py/runtime.h" #include "shared-bindings/storage/__init__.h" void common_hal_storage_remount(const char* mount_path, bool readonly) { mp_raise_NotImplementedError(""); } + +void common_hal_storage_erase_filesystem() { + mp_raise_NotImplementedError("Use esptool to erase flash and re-upload Python instead"); +} diff --git a/ports/nrf/common-hal/storage/__init__.c b/ports/nrf/common-hal/storage/__init__.c index 9d94d39690ba5..76ff81466839b 100644 --- a/ports/nrf/common-hal/storage/__init__.c +++ b/ports/nrf/common-hal/storage/__init__.c @@ -37,3 +37,7 @@ void common_hal_storage_remount(const char* mount_path, bool readonly) { mp_raise_OSError(MP_EINVAL); } } + +void common_hal_storage_erase_filesystem() { + mp_raise_NotImplementedError(""); +} diff --git a/shared-bindings/storage/__init__.c b/shared-bindings/storage/__init__.c index 2195d22423e5d..ca9281a1852b7 100644 --- a/shared-bindings/storage/__init__.c +++ b/shared-bindings/storage/__init__.c @@ -133,15 +133,17 @@ MP_DEFINE_CONST_FUN_OBJ_1(storage_getmount_obj, storage_getmount); //| .. function:: erase_filesystem() //| -//| Erase and re-create the ``CIRCUITPY`` filesystem. Then call -//| `microcontroller.reset()` to restart CircuitPython and have the +//| 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. +//| CircuitPython will restart on certain boards. mp_obj_t storage_erase_filesystem(void) { common_hal_storage_erase_filesystem(); diff --git a/shared-module/storage/__init__.c b/shared-module/storage/__init__.c index f1fa74990a5de..dc576ccdad250 100644 --- a/shared-module/storage/__init__.c +++ b/shared-module/storage/__init__.c @@ -127,9 +127,3 @@ void common_hal_storage_umount_path(const char* mount_path) { mp_obj_t common_hal_storage_getmount(const char *mount_path) { return storage_object_from_path(mount_path); } - -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. -} diff --git a/supervisor/filesystem.h b/supervisor/filesystem.h index 63da0bcab5341..eb9e65c091d14 100644 --- a/supervisor/filesystem.h +++ b/supervisor/filesystem.h @@ -29,7 +29,7 @@ #include -void filesystem_init(bool create_allowed, bool create_force); +void filesystem_init(bool create_allowed, bool force_create); void filesystem_flush(void); void filesystem_writable_by_python(bool writable); bool filesystem_present(void); From 04b2c8be5a416fa1687ca767c26ea3ccc25eb330 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 10 Apr 2018 12:13:21 -0400 Subject: [PATCH 7/9] remove unnecessary includes in esp8266 common-hal storage code --- ports/esp8266/common-hal/storage/__init__.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/ports/esp8266/common-hal/storage/__init__.c b/ports/esp8266/common-hal/storage/__init__.c index dd732e3b7cab3..b800ad2bd94be 100644 --- a/ports/esp8266/common-hal/storage/__init__.c +++ b/ports/esp8266/common-hal/storage/__init__.c @@ -26,9 +26,6 @@ #include -#include "lib/oofatfs/ff.h" -#include "lib/oofatfs/diskio.h" -#include "py/mperrno.h" #include "py/runtime.h" #include "shared-bindings/storage/__init__.h" From 80fb61ea44b9efa4c6e42676cd6ac85d41ac76bb Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 10 Apr 2018 12:16:46 -0400 Subject: [PATCH 8/9] remove more unneeded includes --- shared-module/storage/__init__.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/shared-module/storage/__init__.c b/shared-module/storage/__init__.c index dc576ccdad250..5b0bbe01ecb65 100644 --- a/shared-module/storage/__init__.c +++ b/shared-module/storage/__init__.c @@ -32,9 +32,7 @@ #include "py/mperrno.h" #include "py/obj.h" #include "py/runtime.h" -#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/storage/__init__.h" -#include "supervisor/filesystem.h" STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { if (vfs == MP_VFS_NONE) { From eaa9923a8fc2eee6648bf6f839a88ae1e0e72a7f Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 10 Apr 2018 12:24:27 -0400 Subject: [PATCH 9/9] force_create rename got lost due to editing error --- ports/atmel-samd/supervisor/filesystem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/supervisor/filesystem.c b/ports/atmel-samd/supervisor/filesystem.c index a165d8887db1a..2ba29f279f2db 100644 --- a/ports/atmel-samd/supervisor/filesystem.c +++ b/ports/atmel-samd/supervisor/filesystem.c @@ -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 // want it to be executed without using stack within main() function -void filesystem_init(bool create_allowed, bool create_force) { +void filesystem_init(bool create_allowed, bool force_create) { // init the vfs object fs_user_mount_t *vfs_fat = &fs_user_mount_flash; vfs_fat->flags = 0; @@ -59,7 +59,7 @@ void filesystem_init(bool create_allowed, bool create_force) { // try to mount the flash FRESULT res = f_mount(&vfs_fat->fatfs); - if ((res == FR_NO_FILESYSTEM && create_allowed) || create_force) { + if ((res == FR_NO_FILESYSTEM && create_allowed) || force_create) { // No filesystem so create a fresh one, or reformat has been requested. uint8_t working_buf[_MAX_SS]; res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)); 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