Skip to content

Get CIRCUITPY FATFS directly. #6695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ports/espressif/boards/mixgo_ce_serial/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion shared-module/dotenv/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' ';
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions supervisor/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions supervisor/shared/bluetooth/file_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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';

Expand Down
13 changes: 11 additions & 2 deletions supervisor/shared/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -199,5 +201,12 @@ void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concu
}

bool filesystem_present(void) {
return true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a very unlikely scenario but a device could have damaged flash but someone mounts an sd filesystem via the repl. This would make this function return true but the internal filesystem object would be invalid.

The rest of the PR is great though and this is an unlikely story so don't let this alone stop it being merged

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannewt is it worth checking for this? Right now you go into safe mode if filesystem_init() fails for CIRCUITPY.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will merge anyway to get this in and an issue could be opened if it's worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_mp_vfs.len isn't the length of vfs mounts. It's the length of the mount point string for the "native" filesystems vfs entry. So I think it'll be ok even when only an SD card exists.

return _mp_vfs.len > 0;
}

FATFS *filesystem_circuitpy(void) {
if (!filesystem_present()) {
return NULL;
}
return &_internal_vfs.fatfs;
}
3 changes: 2 additions & 1 deletion supervisor/shared/web_workflow/web_workflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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] == '/') {
Expand Down