2017-06-27 18:17:30 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2017-06-27 18:17:30 -04:00
|
|
|
* Copyright (c) 2015 Josef Gajdusek
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "extmod/vfs.h"
|
|
|
|
#include "py/mperrno.h"
|
2020-07-28 17:52:56 -04:00
|
|
|
#include "py/mphal.h"
|
2017-06-27 18:17:30 -04:00
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/runtime.h"
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2018-06-15 13:53:30 -04:00
|
|
|
#include "shared-bindings/os/__init__.h"
|
2017-06-27 18:17:30 -04:00
|
|
|
#include "shared-bindings/storage/__init__.h"
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "supervisor/filesystem.h"
|
|
|
|
#include "supervisor/flash.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "supervisor/usb.h"
|
2021-04-27 23:53:23 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_USB_MSC
|
2021-04-13 23:33:44 -04:00
|
|
|
#include "tusb.h"
|
|
|
|
|
2021-04-27 14:37:36 -04:00
|
|
|
static const uint8_t usb_msc_descriptor_template[] = {
|
2021-04-20 22:20:34 -04:00
|
|
|
// MSC Interface Descriptor
|
2021-04-19 23:24:18 -04:00
|
|
|
0x09, // 0 bLength
|
|
|
|
0x04, // 1 bDescriptorType (Interface)
|
|
|
|
0xFF, // 2 bInterfaceNumber [SET AT RUNTIME]
|
2021-04-23 00:18:05 -04:00
|
|
|
#define MSC_INTERFACE_INDEX (2)
|
2021-04-19 23:24:18 -04:00
|
|
|
0x00, // 3 bAlternateSetting
|
|
|
|
0x02, // 4 bNumEndpoints 2
|
|
|
|
0x08, // 5 bInterfaceClass: MSC
|
|
|
|
0x06, // 6 bInterfaceSubClass: TRANSPARENT
|
|
|
|
0x50, // 7 bInterfaceProtocol: BULK
|
2021-04-20 22:20:34 -04:00
|
|
|
0xFF, // 8 iInterface (String Index) [SET AT RUNTIME]
|
2021-04-23 00:18:05 -04:00
|
|
|
#define MSC_INTERFACE_STRING_INDEX (8)
|
2021-04-19 23:24:18 -04:00
|
|
|
|
2021-04-20 22:20:34 -04:00
|
|
|
// MSC Endpoint IN Descriptor
|
2021-04-19 23:24:18 -04:00
|
|
|
0x07, // 9 bLength
|
|
|
|
0x05, // 10 bDescriptorType (Endpoint)
|
2021-04-21 23:25:36 -04:00
|
|
|
0xFF, // 11 bEndpointAddress (IN/D2H) [SET AT RUNTIME: 0x80 | number]
|
2021-04-23 00:18:05 -04:00
|
|
|
#define MSC_IN_ENDPOINT_INDEX (11)
|
2021-04-19 23:24:18 -04:00
|
|
|
0x02, // 12 bmAttributes (Bulk)
|
2021-05-19 07:35:17 -04:00
|
|
|
#if USB_HIGHSPEED
|
2021-05-18 11:02:16 -04:00
|
|
|
0x00, 0x02, // 13,14 wMaxPacketSize 512
|
2021-05-19 07:35:17 -04:00
|
|
|
#else
|
2021-04-19 23:24:18 -04:00
|
|
|
0x40, 0x00, // 13,14 wMaxPacketSize 64
|
2021-05-19 07:35:17 -04:00
|
|
|
#endif
|
2021-04-19 23:24:18 -04:00
|
|
|
0x00, // 15 bInterval 0 (unit depends on device speed)
|
|
|
|
|
2021-04-20 22:20:34 -04:00
|
|
|
// MSC Endpoint OUT Descriptor
|
2021-04-19 23:24:18 -04:00
|
|
|
0x07, // 16 bLength
|
|
|
|
0x05, // 17 bDescriptorType (Endpoint)
|
|
|
|
0xFF, // 18 bEndpointAddress (OUT/H2D) [SET AT RUNTIME]
|
2021-04-23 00:18:05 -04:00
|
|
|
#define MSC_OUT_ENDPOINT_INDEX (18)
|
2021-04-19 23:24:18 -04:00
|
|
|
0x02, // 19 bmAttributes (Bulk)
|
2021-05-19 07:35:17 -04:00
|
|
|
#if USB_HIGHSPEED
|
2021-05-18 11:02:16 -04:00
|
|
|
0x00, 0x02, // 20,21 wMaxPacketSize 512
|
2021-05-19 07:35:17 -04:00
|
|
|
#else
|
2021-04-20 22:20:34 -04:00
|
|
|
0x40, 0x00, // 20,21 wMaxPacketSize 64
|
2021-05-19 07:35:17 -04:00
|
|
|
#endif
|
2021-04-20 22:20:34 -04:00
|
|
|
0x00, // 22 bInterval 0 (unit depends on device speed)
|
2021-04-19 23:24:18 -04:00
|
|
|
};
|
|
|
|
|
2021-04-13 23:33:44 -04:00
|
|
|
// Is the MSC device enabled?
|
2021-04-27 14:37:36 -04:00
|
|
|
bool storage_usb_is_enabled;
|
|
|
|
|
2021-04-28 23:48:26 -04:00
|
|
|
void storage_usb_set_defaults(void) {
|
2021-04-28 13:00:44 -04:00
|
|
|
storage_usb_is_enabled = CIRCUITPY_USB_MSC_ENABLED_DEFAULT;
|
2021-04-27 23:53:23 -04:00
|
|
|
}
|
|
|
|
|
2021-04-27 14:37:36 -04:00
|
|
|
bool storage_usb_enabled(void) {
|
|
|
|
return storage_usb_is_enabled;
|
|
|
|
}
|
2021-04-19 23:24:18 -04:00
|
|
|
|
2021-04-20 22:20:34 -04:00
|
|
|
size_t storage_usb_descriptor_length(void) {
|
2021-04-27 14:37:36 -04:00
|
|
|
return sizeof(usb_msc_descriptor_template);
|
2021-04-19 23:24:18 -04:00
|
|
|
}
|
|
|
|
|
2021-04-30 10:40:12 -04:00
|
|
|
static const char storage_interface_name[] = USB_INTERFACE_NAME " Mass Storage";
|
2021-04-21 23:25:36 -04:00
|
|
|
|
2021-05-13 21:49:04 -04:00
|
|
|
size_t storage_usb_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *descriptor_counts, uint8_t *current_interface_string) {
|
2021-04-27 14:37:36 -04:00
|
|
|
memcpy(descriptor_buf, usb_msc_descriptor_template, sizeof(usb_msc_descriptor_template));
|
2021-05-13 21:49:04 -04:00
|
|
|
descriptor_buf[MSC_INTERFACE_INDEX] = descriptor_counts->current_interface;
|
|
|
|
descriptor_counts->current_interface++;
|
|
|
|
|
|
|
|
descriptor_buf[MSC_IN_ENDPOINT_INDEX] =
|
|
|
|
0x80 | (USB_MSC_EP_NUM_IN ? USB_MSC_EP_NUM_IN : descriptor_counts->current_endpoint);
|
|
|
|
descriptor_counts->num_in_endpoints++;
|
|
|
|
descriptor_buf[MSC_OUT_ENDPOINT_INDEX] =
|
|
|
|
USB_MSC_EP_NUM_OUT ? USB_MSC_EP_NUM_OUT : descriptor_counts->current_endpoint;
|
|
|
|
descriptor_counts->num_out_endpoints++;
|
|
|
|
descriptor_counts->current_endpoint++;
|
2021-04-21 23:25:36 -04:00
|
|
|
|
2021-04-27 14:37:36 -04:00
|
|
|
usb_add_interface_string(*current_interface_string, storage_interface_name);
|
2021-04-21 23:25:36 -04:00
|
|
|
descriptor_buf[MSC_INTERFACE_STRING_INDEX] = *current_interface_string;
|
|
|
|
(*current_interface_string)++;
|
|
|
|
|
2021-04-27 14:37:36 -04:00
|
|
|
return sizeof(usb_msc_descriptor_template);
|
2021-04-19 23:24:18 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 22:22:29 -04:00
|
|
|
static bool usb_drive_set_enabled(bool enabled) {
|
2021-04-27 23:53:23 -04:00
|
|
|
// We can't change the descriptors once we're connected.
|
|
|
|
if (tud_connected()) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-28 18:07:35 -05:00
|
|
|
filesystem_set_internal_writable_by_usb(enabled);
|
2021-04-27 23:53:23 -04:00
|
|
|
storage_usb_is_enabled = enabled;
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-03 22:22:29 -04:00
|
|
|
|
|
|
|
bool common_hal_storage_disable_usb_drive(void) {
|
|
|
|
return usb_drive_set_enabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_storage_enable_usb_drive(void) {
|
|
|
|
return usb_drive_set_enabled(true);
|
|
|
|
}
|
2021-07-08 22:11:05 -04:00
|
|
|
#else
|
|
|
|
bool common_hal_storage_disable_usb_drive(void) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_storage_enable_usb_drive(void) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-27 23:53:23 -04:00
|
|
|
#endif // CIRCUITPY_USB_MSC
|
2017-06-27 18:17:30 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) {
|
|
|
|
if (vfs == MP_VFS_NONE) {
|
|
|
|
// mount point not found
|
|
|
|
mp_raise_OSError(MP_ENODEV);
|
|
|
|
}
|
|
|
|
if (vfs == MP_VFS_ROOT) {
|
|
|
|
// can't do operation on root dir
|
|
|
|
mp_raise_OSError(MP_EPERM);
|
|
|
|
}
|
|
|
|
mp_obj_t meth[n_args + 2];
|
|
|
|
mp_load_method(vfs->obj, meth_name, meth);
|
|
|
|
if (args != NULL) {
|
|
|
|
memcpy(meth + 2, args, n_args * sizeof(*args));
|
|
|
|
}
|
|
|
|
return mp_call_method_n_kw(n_args, 0, meth);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_storage_mount(mp_obj_t vfs_obj, const char *mount_path, bool readonly) {
|
2017-06-27 18:17:30 -04:00
|
|
|
// create new object
|
|
|
|
mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t);
|
|
|
|
vfs->str = mount_path;
|
|
|
|
vfs->len = strlen(mount_path);
|
|
|
|
vfs->obj = vfs_obj;
|
|
|
|
vfs->next = NULL;
|
|
|
|
|
|
|
|
mp_obj_t args[2];
|
|
|
|
args[0] = readonly ? mp_const_true : mp_const_false;
|
|
|
|
args[1] = mp_const_false; // Don't make the file system automatically when mounting.
|
|
|
|
|
2018-06-15 13:53:30 -04:00
|
|
|
// Check that there's no file or directory with the same name as the mount point.
|
2018-06-21 09:24:40 -04:00
|
|
|
// But it's ok to mount '/' in any case.
|
|
|
|
if (strcmp(vfs->str, "/") != 0) {
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
common_hal_os_stat(mount_path);
|
|
|
|
nlr_pop();
|
|
|
|
// Something with the same name exists.
|
|
|
|
mp_raise_OSError(MP_EEXIST);
|
|
|
|
}
|
2018-06-15 13:53:30 -04:00
|
|
|
}
|
2017-06-27 18:17:30 -04:00
|
|
|
|
|
|
|
// check that the destination mount point is unused
|
|
|
|
const char *path_out;
|
|
|
|
mp_vfs_mount_t *existing_mount = mp_vfs_lookup_path(mount_path, &path_out);
|
|
|
|
if (existing_mount != MP_VFS_NONE && existing_mount != MP_VFS_ROOT) {
|
|
|
|
if (vfs->len != 1 && existing_mount->len == 1) {
|
|
|
|
// if root dir is mounted, still allow to mount something within a subdir of root
|
|
|
|
} else {
|
|
|
|
// mount point in use
|
|
|
|
mp_raise_OSError(MP_EPERM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 13:53:30 -04:00
|
|
|
// call the underlying object to do any mounting operation
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t *)&args);
|
2018-06-15 13:53:30 -04:00
|
|
|
|
2017-06-28 17:46:49 -04:00
|
|
|
// Insert the vfs into the mount table by pushing it onto the front of the
|
|
|
|
// mount table.
|
2017-06-27 18:17:30 -04:00
|
|
|
mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table);
|
2017-06-28 17:46:49 -04:00
|
|
|
vfs->next = *vfsp;
|
2017-06-27 18:17:30 -04:00
|
|
|
*vfsp = vfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_storage_umount_object(mp_obj_t vfs_obj) {
|
|
|
|
// remove vfs from the mount table
|
|
|
|
mp_vfs_mount_t *vfs = NULL;
|
|
|
|
for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) {
|
|
|
|
if ((*vfsp)->obj == vfs_obj) {
|
|
|
|
vfs = *vfsp;
|
|
|
|
*vfsp = (*vfsp)->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vfs == NULL) {
|
|
|
|
mp_raise_OSError(MP_EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we unmounted the current device then set current to root
|
|
|
|
if (MP_STATE_VM(vfs_cur) == vfs) {
|
|
|
|
MP_STATE_VM(vfs_cur) = MP_VFS_ROOT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call the underlying object to do any unmounting operation
|
|
|
|
mp_vfs_proxy_call(vfs, MP_QSTR_umount, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC mp_obj_t storage_object_from_path(const char *mount_path) {
|
2017-06-27 18:17:30 -04:00
|
|
|
for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) {
|
|
|
|
if (strcmp(mount_path, (*vfsp)->str) == 0) {
|
2018-03-27 22:28:18 -04:00
|
|
|
return (*vfsp)->obj;
|
2017-06-27 18:17:30 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-27 22:28:18 -04:00
|
|
|
mp_raise_OSError(MP_EINVAL);
|
|
|
|
}
|
2017-06-27 18:17:30 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_storage_umount_path(const char *mount_path) {
|
2018-03-27 22:28:18 -04:00
|
|
|
common_hal_storage_umount_object(storage_object_from_path(mount_path));
|
2017-06-27 18:17:30 -04:00
|
|
|
}
|
2018-03-27 22:28:18 -04:00
|
|
|
|
|
|
|
mp_obj_t common_hal_storage_getmount(const char *mount_path) {
|
|
|
|
return storage_object_from_path(mount_path);
|
|
|
|
}
|
2018-10-19 21:46:22 -04:00
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
void common_hal_storage_remount(const char *mount_path, bool readonly, bool disable_concurrent_write_protection) {
|
2018-10-19 21:46:22 -04:00
|
|
|
if (strcmp(mount_path, "/") != 0) {
|
|
|
|
mp_raise_OSError(MP_EINVAL);
|
|
|
|
}
|
|
|
|
|
2021-03-22 12:45:29 -04:00
|
|
|
#if CIRCUITPY_USB_MSC
|
2021-04-29 22:26:38 -04:00
|
|
|
if (!usb_msc_ejected() && storage_usb_is_enabled) {
|
|
|
|
mp_raise_RuntimeError(translate("Cannot remount '/' when visible via USB."));
|
2018-10-19 21:46:22 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
filesystem_set_internal_writable_by_usb(readonly);
|
|
|
|
filesystem_set_internal_concurrent_write_protection(!disable_concurrent_write_protection);
|
2018-10-19 21:46:22 -04:00
|
|
|
}
|
|
|
|
|
2022-10-05 23:50:54 -04:00
|
|
|
void common_hal_storage_erase_filesystem(bool extended) {
|
2021-04-16 10:59:11 -04:00
|
|
|
#if CIRCUITPY_USB
|
2020-07-28 17:52:56 -04:00
|
|
|
usb_disconnect();
|
2021-04-16 10:59:11 -04:00
|
|
|
#endif
|
2020-07-28 17:52:56 -04:00
|
|
|
mp_hal_delay_ms(1000);
|
2022-10-05 23:50:54 -04:00
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
supervisor_flash_set_extended(extended);
|
|
|
|
#endif
|
2021-12-27 18:58:24 -05:00
|
|
|
(void)filesystem_init(false, true); // Force a re-format. Ignore failure.
|
2018-10-19 21:46:22 -04:00
|
|
|
common_hal_mcu_reset();
|
|
|
|
// We won't actually get here, since we're resetting.
|
|
|
|
}
|