Skip to content

Commit 7ab8a66

Browse files
authored
Merge pull request #6695 from tannewt/always_circuitpy
Get CIRCUITPY FATFS directly.
2 parents b92c3bb + 125b276 commit 7ab8a66

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

ports/espressif/boards/mixgo_ce_serial/board.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "lib/oofatfs/ff.h"
3232
#include "extmod/vfs_fat.h"
3333
#include "py/mpstate.h"
34+
#include "supervisor/filesystem.h"
3435

3536
void board_init(void) {
3637
// Debug UART
@@ -41,7 +42,7 @@ void board_init(void) {
4142

4243
mp_import_stat_t stat_b = mp_import_stat("boot.py");
4344
if (stat_b != MP_IMPORT_STAT_FILE) {
44-
FATFS *fatfs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
45+
FATFS *fatfs = filesystem_circuitpy();
4546
FIL fs;
4647
UINT char_written = 0;
4748
const byte buffer[] = "#Serial port upload mode\nimport storage\nstorage.remount(\"/\", False)\nstorage.disable_usb_drive()\n";

shared-module/dotenv/__init__.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "extmod/vfs_fat.h"
3333
#include "py/mpstate.h"
3434
#include "py/objstr.h"
35+
#include "supervisor/filesystem.h"
3536

3637
STATIC uint8_t consume_spaces(FIL *active_file) {
3738
uint8_t character = ' ';
@@ -188,7 +189,7 @@ STATIC mp_int_t read_value(FIL *active_file, char *value, size_t value_len) {
188189

189190
mp_int_t dotenv_get_key(const char *path, const char *key, char *value, mp_int_t value_len) {
190191
FIL active_file;
191-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
192+
FATFS *fs = filesystem_circuitpy();
192193
FRESULT result = f_open(fs, &active_file, path, FA_READ);
193194
if (result != FR_OK) {
194195
return -1;

supervisor/filesystem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concu
4545
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs);
4646
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs);
4747

48+
FATFS *filesystem_circuitpy(void);
49+
4850
#endif // MICROPY_INCLUDED_SUPERVISOR_FILESYSTEM_H

supervisor/shared/bluetooth/file_transfer.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "common-hal/_bleio/__init__.h"
4444

4545
#include "supervisor/fatfs_port.h"
46+
#include "supervisor/filesystem.h"
4647
#include "supervisor/shared/reload.h"
4748
#include "supervisor/shared/bluetooth/file_transfer.h"
4849
#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) {
172173
char *path = (char *)((uint8_t *)command) + header_size;
173174
path[command->path_length] = '\0';
174175

175-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
176+
FATFS *fs = filesystem_circuitpy();
176177
FRESULT result = f_open(fs, &active_file, path, FA_READ);
177178
if (result != FR_OK) {
178179
response.status = STATUS_ERROR;
@@ -289,7 +290,7 @@ STATIC uint8_t _process_write(const uint8_t *raw_buf, size_t command_len) {
289290
return ANY_COMMAND;
290291
}
291292

292-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
293+
FATFS *fs = filesystem_circuitpy();
293294
DWORD fattime;
294295
_truncated_time = truncate_time(command->modification_time, &fattime);
295296
override_fattime(fattime);
@@ -438,7 +439,7 @@ STATIC uint8_t _process_delete(const uint8_t *raw_buf, size_t command_len) {
438439
if (command_len < header_size + command->path_length) {
439440
return THIS_COMMAND;
440441
}
441-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
442+
FATFS *fs = filesystem_circuitpy();
442443
char *path = (char *)((uint8_t *)command) + header_size;
443444
path[command->path_length] = '\0';
444445
FILINFO file;
@@ -495,7 +496,7 @@ STATIC uint8_t _process_mkdir(const uint8_t *raw_buf, size_t command_len) {
495496
if (command_len < header_size + command->path_length) {
496497
return THIS_COMMAND;
497498
}
498-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
499+
FATFS *fs = filesystem_circuitpy();
499500
char *path = (char *)command->path;
500501
_terminate_path(path, command->path_length);
501502

@@ -552,7 +553,7 @@ STATIC uint8_t _process_listdir(uint8_t *raw_buf, size_t command_len) {
552553
return THIS_COMMAND;
553554
}
554555

555-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
556+
FATFS *fs = filesystem_circuitpy();
556557
char *path = (char *)&command->path;
557558
_terminate_path(path, command->path_length);
558559
// 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) {
640641
if (command_len < header_size + total_path_length) {
641642
return THIS_COMMAND;
642643
}
643-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
644+
FATFS *fs = filesystem_circuitpy();
644645
char *old_path = (char *)command->paths;
645646
old_path[command->old_path_length] = '\0';
646647

supervisor/shared/filesystem.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ bool filesystem_init(bool create_allowed, bool force_create) {
9292
vfs_fat->blockdev.flags = 0;
9393
supervisor_flash_init_vfs(vfs_fat);
9494

95+
mp_vfs_mount_t *vfs = &_mp_vfs;
96+
vfs->len = 0;
97+
9598
// try to mount the flash
9699
FRESULT res = f_mount(&vfs_fat->fatfs);
97100
if ((res == FR_NO_FILESYSTEM && create_allowed) || force_create) {
@@ -140,7 +143,6 @@ bool filesystem_init(bool create_allowed, bool force_create) {
140143
} else if (res != FR_OK) {
141144
return false;
142145
}
143-
mp_vfs_mount_t *vfs = &_mp_vfs;
144146
vfs->str = "/";
145147
vfs->len = 1;
146148
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
199201
}
200202

201203
bool filesystem_present(void) {
202-
return true;
204+
return _mp_vfs.len > 0;
205+
}
206+
207+
FATFS *filesystem_circuitpy(void) {
208+
if (!filesystem_present()) {
209+
return NULL;
210+
}
211+
return &_internal_vfs.fatfs;
203212
}

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "shared-module/storage/__init__.h"
3737
#include "shared/timeutils/timeutils.h"
3838
#include "supervisor/fatfs_port.h"
39+
#include "supervisor/filesystem.h"
3940
#include "supervisor/shared/reload.h"
4041
#include "supervisor/shared/translate/translate.h"
4142
#include "supervisor/shared/web_workflow/web_workflow.h"
@@ -979,7 +980,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
979980
} else {
980981
char *path = request->path + 3;
981982
size_t pathlen = strlen(path);
982-
FATFS *fs = &((fs_user_mount_t *)MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
983+
FATFS *fs = filesystem_circuitpy();
983984
// Trailing / is a directory.
984985
bool directory = false;
985986
if (path[pathlen - 1] == '/') {

0 commit comments

Comments
 (0)