Skip to content

Commit 9eb86e8

Browse files
committed
Add support for USB writeable, MicroPython read-only volumes.
This prevents file system corruption due to two systems mutating it at once.
1 parent eb62d03 commit 9eb86e8

4 files changed

Lines changed: 11 additions & 6 deletions

File tree

atmel-samd/internal_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ const mp_obj_type_t internal_flash_type = {
278278
};
279279

280280
void flash_init_vfs(fs_user_mount_t *vfs) {
281-
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
281+
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL | FSUSER_USB_WRITEABLE;
282282
vfs->readblocks[0] = (mp_obj_t)&internal_flash_obj_readblocks_obj;
283283
vfs->readblocks[1] = (mp_obj_t)&internal_flash_obj;
284284
vfs->readblocks[2] = (mp_obj_t)internal_flash_read_blocks; // native version

atmel-samd/spi_flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ const mp_obj_type_t spi_flash_type = {
609609
};
610610

611611
void flash_init_vfs(fs_user_mount_t *vfs) {
612-
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
612+
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL | FSUSER_USB_WRITEABLE;
613613
vfs->readblocks[0] = (mp_obj_t)&spi_flash_obj_readblocks_obj;
614614
vfs->readblocks[1] = (mp_obj_t)&spi_flash_obj;
615615
vfs->readblocks[2] = (mp_obj_t)spi_flash_read_blocks; // native version

extmod/fsusermount.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
#include "py/obj.h"
2929

3030
// these are the values for fs_user_mount_t.flags
31-
#define FSUSER_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func
32-
#define FSUSER_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount
33-
#define FSUSER_HAVE_IOCTL (0x0004) // new protocol with ioctl
31+
#define FSUSER_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func
32+
#define FSUSER_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount
33+
#define FSUSER_HAVE_IOCTL (0x0004) // new protocol with ioctl
34+
// Device is write-able over USB and read-only to MicroPython.
35+
#define FSUSER_USB_WRITEABLE (0x0008)
3436

3537
// constants for block protocol ioctl
3638
#define BP_IOCTL_INIT (1)

extmod/vfs_fat_diskio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ DSTATUS disk_status (
9898
return STA_NOINIT;
9999
}
100100

101-
if (vfs->writeblocks[0] == MP_OBJ_NULL) {
101+
// This is used to determine the writeability of the disk from MicroPython.
102+
// So, if its USB writeable we make it read-only from MicroPython.
103+
if (vfs->writeblocks[0] == MP_OBJ_NULL ||
104+
(vfs->flags & FSUSER_USB_WRITEABLE) != 0) {
102105
return STA_PROTECT;
103106
} else {
104107
return 0;

0 commit comments

Comments
 (0)