From 125b276af02d373153de6f4277d22e9d215980ec Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 4 Aug 2022 16:06:27 -0700 Subject: [PATCH] Get CIRCUITPY FATFS directly. Otherwise, you may actually get a non-root filesystem. Fixes #6575 --- ports/espressif/boards/mixgo_ce_serial/board.c | 3 ++- shared-module/dotenv/__init__.c | 3 ++- supervisor/filesystem.h | 2 ++ supervisor/shared/bluetooth/file_transfer.c | 13 +++++++------ supervisor/shared/filesystem.c | 13 +++++++++++-- supervisor/shared/web_workflow/web_workflow.c | 3 ++- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/ports/espressif/boards/mixgo_ce_serial/board.c b/ports/espressif/boards/mixgo_ce_serial/board.c index e07fe5cfd7..32cb1749ca 100644 --- a/ports/espressif/boards/mixgo_ce_serial/board.c +++ b/ports/espressif/boards/mixgo_ce_serial/board.c @@ -31,6 +31,7 @@ #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" #include "py/mpstate.h" +#include "supervisor/filesystem.h" void board_init(void) { // Debug UART @@ -41,7 +42,7 @@ void board_init(void) { mp_import_stat_t stat_b = mp_import_stat("boot.py"); if (stat_b != MP_IMPORT_STAT_FILE) { - FATFS *fatfs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fatfs = filesystem_circuitpy(); FIL fs; UINT char_written = 0; const byte buffer[] = "#Serial port upload mode\nimport storage\nstorage.remount(\"/\", False)\nstorage.disable_usb_drive()\n"; diff --git a/shared-module/dotenv/__init__.c b/shared-module/dotenv/__init__.c index bd1973366d..f17d684e12 100644 --- a/shared-module/dotenv/__init__.c +++ b/shared-module/dotenv/__init__.c @@ -32,6 +32,7 @@ #include "extmod/vfs_fat.h" #include "py/mpstate.h" #include "py/objstr.h" +#include "supervisor/filesystem.h" STATIC uint8_t consume_spaces(FIL *active_file) { uint8_t character = ' '; @@ -188,7 +189,7 @@ STATIC mp_int_t read_value(FIL *active_file, char *value, size_t value_len) { mp_int_t dotenv_get_key(const char *path, const char *key, char *value, mp_int_t value_len) { FIL active_file; - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); FRESULT result = f_open(fs, &active_file, path, FA_READ); if (result != FR_OK) { return -1; diff --git a/supervisor/filesystem.h b/supervisor/filesystem.h index 6f4faf0b82..3954291513 100644 --- a/supervisor/filesystem.h +++ b/supervisor/filesystem.h @@ -45,4 +45,6 @@ void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concu bool filesystem_is_writable_by_python(fs_user_mount_t *vfs); bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs); +FATFS *filesystem_circuitpy(void); + #endif // MICROPY_INCLUDED_SUPERVISOR_FILESYSTEM_H diff --git a/supervisor/shared/bluetooth/file_transfer.c b/supervisor/shared/bluetooth/file_transfer.c index a6a2f8062a..216cf3d567 100644 --- a/supervisor/shared/bluetooth/file_transfer.c +++ b/supervisor/shared/bluetooth/file_transfer.c @@ -43,6 +43,7 @@ #include "common-hal/_bleio/__init__.h" #include "supervisor/fatfs_port.h" +#include "supervisor/filesystem.h" #include "supervisor/shared/reload.h" #include "supervisor/shared/bluetooth/file_transfer.h" #include "supervisor/shared/bluetooth/file_transfer_protocol.h" @@ -172,7 +173,7 @@ STATIC uint8_t _process_read(const uint8_t *raw_buf, size_t command_len) { char *path = (char *)((uint8_t *)command) + header_size; path[command->path_length] = '\0'; - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); FRESULT result = f_open(fs, &active_file, path, FA_READ); if (result != FR_OK) { response.status = STATUS_ERROR; @@ -289,7 +290,7 @@ STATIC uint8_t _process_write(const uint8_t *raw_buf, size_t command_len) { return ANY_COMMAND; } - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); DWORD fattime; _truncated_time = truncate_time(command->modification_time, &fattime); override_fattime(fattime); @@ -438,7 +439,7 @@ STATIC uint8_t _process_delete(const uint8_t *raw_buf, size_t command_len) { if (command_len < header_size + command->path_length) { return THIS_COMMAND; } - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); char *path = (char *)((uint8_t *)command) + header_size; path[command->path_length] = '\0'; FILINFO file; @@ -495,7 +496,7 @@ STATIC uint8_t _process_mkdir(const uint8_t *raw_buf, size_t command_len) { if (command_len < header_size + command->path_length) { return THIS_COMMAND; } - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); char *path = (char *)command->path; _terminate_path(path, command->path_length); @@ -552,7 +553,7 @@ STATIC uint8_t _process_listdir(uint8_t *raw_buf, size_t command_len) { return THIS_COMMAND; } - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); char *path = (char *)&command->path; _terminate_path(path, command->path_length); // mp_printf(&mp_plat_print, "list %s\n", path); @@ -640,7 +641,7 @@ STATIC uint8_t _process_move(const uint8_t *raw_buf, size_t command_len) { if (command_len < header_size + total_path_length) { return THIS_COMMAND; } - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); char *old_path = (char *)command->paths; old_path[command->old_path_length] = '\0'; diff --git a/supervisor/shared/filesystem.c b/supervisor/shared/filesystem.c index 820dbe9783..283849adc3 100644 --- a/supervisor/shared/filesystem.c +++ b/supervisor/shared/filesystem.c @@ -92,6 +92,9 @@ bool filesystem_init(bool create_allowed, bool force_create) { vfs_fat->blockdev.flags = 0; supervisor_flash_init_vfs(vfs_fat); + mp_vfs_mount_t *vfs = &_mp_vfs; + vfs->len = 0; + // try to mount the flash FRESULT res = f_mount(&vfs_fat->fatfs); if ((res == FR_NO_FILESYSTEM && create_allowed) || force_create) { @@ -140,7 +143,6 @@ bool filesystem_init(bool create_allowed, bool force_create) { } else if (res != FR_OK) { return false; } - mp_vfs_mount_t *vfs = &_mp_vfs; vfs->str = "/"; vfs->len = 1; vfs->obj = MP_OBJ_FROM_PTR(vfs_fat); @@ -199,5 +201,12 @@ void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concu } bool filesystem_present(void) { - return true; + return _mp_vfs.len > 0; +} + +FATFS *filesystem_circuitpy(void) { + if (!filesystem_present()) { + return NULL; + } + return &_internal_vfs.fatfs; } diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index 9e8c32d963..dc93aeb0bc 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -36,6 +36,7 @@ #include "shared-module/storage/__init__.h" #include "shared/timeutils/timeutils.h" #include "supervisor/fatfs_port.h" +#include "supervisor/filesystem.h" #include "supervisor/shared/reload.h" #include "supervisor/shared/translate/translate.h" #include "supervisor/shared/web_workflow/web_workflow.h" @@ -979,7 +980,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) { } else { char *path = request->path + 3; size_t pathlen = strlen(path); - FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs; + FATFS *fs = filesystem_circuitpy(); // Trailing / is a directory. bool directory = false; if (path[pathlen - 1] == '/') {