Skip to content

Commit a54ddb3

Browse files
authored
Merge pull request #10648 from dhalbert/usb-fs-write-prot
Mounted filesystems USB write protection
2 parents 8e2d612 + 179a52e commit a54ddb3

File tree

9 files changed

+33
-17
lines changed

9 files changed

+33
-17
lines changed

main.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,22 @@ static void start_mp(safe_mode_t safe_mode) {
211211

212212
static void stop_mp(void) {
213213
#if MICROPY_VFS
214-
mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table);
215214

216215
// Unmount all heap allocated vfs mounts.
217-
while (gc_ptr_on_heap(vfs)) {
216+
mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table);
217+
do {
218+
if (gc_ptr_on_heap(vfs)) {
219+
// mp_vfs_umount will splice out an unmounted vfs from the vfs_mount_table linked list.
220+
mp_vfs_umount(vfs->obj);
221+
// Start over at the beginning of the list since the first entry may have been removed.
222+
vfs = MP_STATE_VM(vfs_mount_table);
223+
continue;
224+
}
218225
vfs = vfs->next;
219-
}
220-
MP_STATE_VM(vfs_mount_table) = vfs;
226+
} while (vfs != NULL);
227+
221228
// The last vfs is CIRCUITPY and the root directory.
229+
vfs = MP_STATE_VM(vfs_mount_table);
222230
while (vfs->next != NULL) {
223231
vfs = vfs->next;
224232
}

ports/atmel-samd/boards/circuitbrains_basic_m0/mpconfigboard.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ EXTERNAL_FLASH_DEVICES = "W25Q32JVxQ"
1111
LONGINT_IMPL = MPZ
1212

1313
CIRCUITPY_CODEOP = 0
14-
CIRCUITPY_JPEGIO = 0
14+
CIRCUITPY_ERRNO = 0
15+
CIRCUITPY_RAINBOWIO = 0

ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ EXTERNAL_FLASH_DEVICES = "S25FL216K, GD25Q16C, W25Q16JVxQ"
1111
LONGINT_IMPL = MPZ
1212

1313
CIRCUITPY_CODEOP = 0
14+
CIRCUITPY_ERRNO = 0
1415
CIRCUITPY_RAINBOWIO = 0

ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ EXTERNAL_FLASH_DEVICES = "S25FL064L"
1111
LONGINT_IMPL = MPZ
1212

1313
CIRCUITPY_CODEOP = 0
14+
CIRCUITPY_ERRNO = 0
1415
CIRCUITPY_RAINBOWIO = 0

ports/atmel-samd/boards/feather_m4_can/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CIRCUITPY_FLOPPYIO = 0
1717
CIRCUITPY_GIFIO = 0
1818
CIRCUITPY_I2CTARGET = 0
1919
CIRCUITPY_JPEGIO = 0
20+
CIRCUITPY_PS2IO = 0
2021
CIRCUITPY_SYNTHIO = 0
2122

2223
CIRCUITPY_LTO_PARTITION = one

py/circuitpy_mpconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ typedef long mp_off_t;
345345
#endif
346346

347347

348+
// For easy debugging printf's.
349+
#define PLAT_PRINTF(...) mp_printf(&mp_plat_print, __VA_ARGS__)
350+
348351
#if MICROPY_PY_ASYNC_AWAIT && !CIRCUITPY_TRACEBACK
349352
#error CIRCUITPY_ASYNCIO requires CIRCUITPY_TRACEBACK
350353
#endif

shared-bindings/storage/__init__.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,11 @@ static mp_obj_t storage_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t
5454
// get the mount point
5555
const char *mnt_str = mp_obj_str_get_str(args[ARG_mount_path].u_obj);
5656

57-
// Make sure we're given an object we can mount.
58-
// TODO(tannewt): Make sure we have all the methods we need to operating it
59-
// as a file system.
57+
6058
mp_obj_t vfs_obj = args[ARG_filesystem].u_obj;
61-
mp_obj_t dest[2];
62-
mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest);
63-
if (dest[0] == MP_OBJ_NULL) {
64-
mp_raise_ValueError(MP_ERROR_TEXT("filesystem must provide mount method"));
65-
}
59+
60+
// Currently, the only supported filesystem is VfsFat.
61+
mp_arg_validate_type(vfs_obj, &mp_fat_vfs_type, MP_QSTR_filesystem);
6662

6763
common_hal_storage_mount(vfs_obj, mnt_str, args[ARG_readonly].u_bool);
6864

shared-module/storage/__init__.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ void common_hal_storage_mount(mp_obj_t vfs_obj, const char *mount_path, bool rea
126126
// call the underlying object to do any mounting operation
127127
mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t *)&args);
128128

129+
fs_user_mount_t *vfs_fat = MP_OBJ_TO_PTR(vfs_obj);
130+
// Filesystem is read-only to USB if writable by CircuitPython, and vice versa.
131+
filesystem_set_writable_by_usb(vfs_fat, readonly);
132+
filesystem_set_concurrent_write_protection(vfs_fat, true);
133+
129134
// Insert the vfs into the mount table by pushing it onto the front of the
130135
// mount table.
131136
mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table);

supervisor/shared/filesystem.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ void filesystem_set_writable_by_usb(fs_user_mount_t *vfs, bool usb_writable) {
247247
}
248248

249249
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs) {
250-
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
251-
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) == 0;
250+
return ((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0) ||
251+
((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) == 0);
252252
}
253253

254254
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs) {
255-
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
256-
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) != 0;
255+
return ((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0) ||
256+
((vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) != 0);
257257
}
258258

259259
void filesystem_set_internal_concurrent_write_protection(bool concurrent_write_protection) {

0 commit comments

Comments
 (0)