From 9eb86e801547169af6914995c9cd7d10bc0c3124 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 21 Oct 2016 15:18:05 -0700 Subject: [PATCH] Add support for USB writeable, MicroPython read-only volumes. This prevents file system corruption due to two systems mutating it at once. --- atmel-samd/internal_flash.c | 2 +- atmel-samd/spi_flash.c | 2 +- extmod/fsusermount.h | 8 +++++--- extmod/vfs_fat_diskio.c | 5 ++++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/atmel-samd/internal_flash.c b/atmel-samd/internal_flash.c index f7d0aea6c6..444c174da6 100644 --- a/atmel-samd/internal_flash.c +++ b/atmel-samd/internal_flash.c @@ -278,7 +278,7 @@ const mp_obj_type_t internal_flash_type = { }; void flash_init_vfs(fs_user_mount_t *vfs) { - vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL | FSUSER_USB_WRITEABLE; vfs->readblocks[0] = (mp_obj_t)&internal_flash_obj_readblocks_obj; vfs->readblocks[1] = (mp_obj_t)&internal_flash_obj; vfs->readblocks[2] = (mp_obj_t)internal_flash_read_blocks; // native version diff --git a/atmel-samd/spi_flash.c b/atmel-samd/spi_flash.c index fef630eff1..812ec84e32 100644 --- a/atmel-samd/spi_flash.c +++ b/atmel-samd/spi_flash.c @@ -609,7 +609,7 @@ const mp_obj_type_t spi_flash_type = { }; void flash_init_vfs(fs_user_mount_t *vfs) { - vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL | FSUSER_USB_WRITEABLE; vfs->readblocks[0] = (mp_obj_t)&spi_flash_obj_readblocks_obj; vfs->readblocks[1] = (mp_obj_t)&spi_flash_obj; vfs->readblocks[2] = (mp_obj_t)spi_flash_read_blocks; // native version diff --git a/extmod/fsusermount.h b/extmod/fsusermount.h index 3eb54f8bd4..7f2912b7f0 100644 --- a/extmod/fsusermount.h +++ b/extmod/fsusermount.h @@ -28,9 +28,11 @@ #include "py/obj.h" // these are the values for fs_user_mount_t.flags -#define FSUSER_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func -#define FSUSER_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount -#define FSUSER_HAVE_IOCTL (0x0004) // new protocol with ioctl +#define FSUSER_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func +#define FSUSER_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount +#define FSUSER_HAVE_IOCTL (0x0004) // new protocol with ioctl +// Device is write-able over USB and read-only to MicroPython. +#define FSUSER_USB_WRITEABLE (0x0008) // constants for block protocol ioctl #define BP_IOCTL_INIT (1) diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index 5608e06453..4d776f6b4b 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -98,7 +98,10 @@ DSTATUS disk_status ( return STA_NOINIT; } - if (vfs->writeblocks[0] == MP_OBJ_NULL) { + // This is used to determine the writeability of the disk from MicroPython. + // So, if its USB writeable we make it read-only from MicroPython. + if (vfs->writeblocks[0] == MP_OBJ_NULL || + (vfs->flags & FSUSER_USB_WRITEABLE) != 0) { return STA_PROTECT; } else { return 0;