2017-09-22 21:05:51 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
#include "supervisor/filesystem.h"
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "extmod/vfs_fat.h"
|
|
|
|
#include "lib/oofatfs/ff.h"
|
|
|
|
#include "lib/oofatfs/diskio.h"
|
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
#include "py/mpstate.h"
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "supervisor/flash.h"
|
2023-02-28 18:07:35 -05:00
|
|
|
#include "supervisor/linker.h"
|
2017-10-10 14:36:00 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
static mp_vfs_mount_t _mp_vfs;
|
|
|
|
static fs_user_mount_t _internal_vfs;
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2019-03-20 12:21:36 -04:00
|
|
|
static volatile uint32_t filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
|
|
|
|
volatile bool filesystem_flush_requested = false;
|
|
|
|
|
|
|
|
void filesystem_background(void) {
|
|
|
|
if (filesystem_flush_requested) {
|
2019-04-03 21:28:27 -04:00
|
|
|
filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
|
|
|
|
// Flush but keep caches
|
|
|
|
supervisor_flash_flush();
|
2019-03-20 12:21:36 -04:00
|
|
|
filesystem_flush_requested = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void filesystem_tick(void) {
|
|
|
|
if (filesystem_flush_interval_ms == 0) {
|
|
|
|
// 0 means not turned on.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (filesystem_flush_interval_ms == 1) {
|
|
|
|
filesystem_flush_requested = true;
|
|
|
|
filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
|
|
|
|
} else {
|
|
|
|
filesystem_flush_interval_ms--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 09:06:36 -04:00
|
|
|
static void make_empty_file(FATFS *fatfs, const char *path) {
|
|
|
|
FIL fp;
|
|
|
|
f_open(fatfs, &fp, path, FA_WRITE | FA_CREATE_ALWAYS);
|
|
|
|
f_close(&fp);
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:29:29 -05:00
|
|
|
|
|
|
|
static void make_sample_code_file(FATFS *fatfs) {
|
2019-11-25 14:43:46 -05:00
|
|
|
#if CIRCUITPY_FULL_BUILD
|
2019-11-25 12:01:01 -05:00
|
|
|
FIL fs;
|
|
|
|
UINT char_written = 0;
|
2021-01-11 16:26:27 -05:00
|
|
|
const byte buffer[] = "print(\"Hello World!\")\n";
|
2021-03-15 09:57:36 -04:00
|
|
|
// Create or modify existing code.py file
|
2019-11-18 15:29:29 -05:00
|
|
|
f_open(fatfs, &fs, "/code.py", FA_WRITE | FA_CREATE_ALWAYS);
|
2019-11-23 10:17:30 -05:00
|
|
|
f_write(&fs, buffer, sizeof(buffer) - 1, &char_written);
|
2019-11-18 15:29:29 -05:00
|
|
|
f_close(&fs);
|
2021-01-20 19:47:18 -05:00
|
|
|
#else
|
|
|
|
make_empty_file(fatfs, "/code.py");
|
2019-11-23 10:28:33 -05:00
|
|
|
#endif
|
2019-11-18 15:29:29 -05:00
|
|
|
}
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
// we don't make this function static because it needs a lot of stack and we
|
|
|
|
// want it to be executed without using stack within main() function
|
2021-12-27 18:58:24 -05:00
|
|
|
bool filesystem_init(bool create_allowed, bool force_create) {
|
2017-10-10 14:36:00 -04:00
|
|
|
// init the vfs object
|
2018-10-19 21:46:22 -04:00
|
|
|
fs_user_mount_t *vfs_fat = &_internal_vfs;
|
2021-04-23 15:26:42 -04:00
|
|
|
vfs_fat->blockdev.flags = 0;
|
2018-10-19 21:46:22 -04:00
|
|
|
supervisor_flash_init_vfs(vfs_fat);
|
2017-10-10 14:36:00 -04:00
|
|
|
|
2022-08-04 19:06:27 -04:00
|
|
|
mp_vfs_mount_t *vfs = &_mp_vfs;
|
|
|
|
vfs->len = 0;
|
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
// try to mount the flash
|
2018-11-14 21:30:47 -05:00
|
|
|
FRESULT res = f_mount(&vfs_fat->fatfs);
|
2018-04-10 12:24:27 -04:00
|
|
|
if ((res == FR_NO_FILESYSTEM && create_allowed) || force_create) {
|
2018-04-09 12:52:42 -04:00
|
|
|
// No filesystem so create a fresh one, or reformat has been requested.
|
2021-04-22 20:55:39 -04:00
|
|
|
uint8_t working_buf[FF_MAX_SS];
|
2021-08-04 19:27:54 -04:00
|
|
|
BYTE formats = FM_FAT;
|
|
|
|
#if FF_FS_EXFAT
|
|
|
|
formats |= FM_EXFAT | FM_FAT32;
|
|
|
|
#endif
|
|
|
|
res = f_mkfs(&vfs_fat->fatfs, formats, 0, working_buf, sizeof(working_buf));
|
2017-10-10 14:36:00 -04:00
|
|
|
if (res != FR_OK) {
|
2021-12-27 18:58:24 -05:00
|
|
|
return false;
|
2017-10-10 14:36:00 -04:00
|
|
|
}
|
2021-12-27 18:58:24 -05:00
|
|
|
// Flush the new file system to make sure it's repaired immediately.
|
|
|
|
supervisor_flash_flush();
|
2017-10-10 14:36:00 -04:00
|
|
|
|
|
|
|
// set label
|
2021-03-15 09:57:36 -04:00
|
|
|
#ifdef CIRCUITPY_DRIVE_LABEL
|
2020-09-28 22:21:17 -04:00
|
|
|
res = f_setlabel(&vfs_fat->fatfs, CIRCUITPY_DRIVE_LABEL);
|
2021-03-15 09:57:36 -04:00
|
|
|
#else
|
2020-09-28 22:21:17 -04:00
|
|
|
res = f_setlabel(&vfs_fat->fatfs, "CIRCUITPY");
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2020-09-28 22:21:17 -04:00
|
|
|
if (res != FR_OK) {
|
2021-12-27 18:58:24 -05:00
|
|
|
return false;
|
2020-09-28 22:21:17 -04:00
|
|
|
}
|
2018-03-22 09:06:36 -04:00
|
|
|
|
2023-08-29 10:23:17 -04:00
|
|
|
#if CIRCUITPY_USB
|
2018-03-22 09:06:36 -04:00
|
|
|
// inhibit file indexing on MacOS
|
2020-09-28 22:21:17 -04:00
|
|
|
res = f_mkdir(&vfs_fat->fatfs, "/.fseventsd");
|
|
|
|
if (res != FR_OK) {
|
2021-12-27 18:58:24 -05:00
|
|
|
return false;
|
2020-09-28 22:21:17 -04:00
|
|
|
}
|
2018-03-24 11:44:24 -04:00
|
|
|
make_empty_file(&vfs_fat->fatfs, "/.fseventsd/no_log");
|
2023-08-28 10:03:32 -04:00
|
|
|
make_empty_file(&vfs_fat->fatfs, "/.metadata_never_index");
|
|
|
|
|
|
|
|
// Prevent storing trash on all OSes.
|
|
|
|
make_empty_file(&vfs_fat->fatfs, "/.Trashes"); // MacOS
|
|
|
|
make_empty_file(&vfs_fat->fatfs, "/.Trash-1000"); // Linux, XDG trash spec:
|
|
|
|
// https://specifications.freedesktop.org/trash-spec/trashspec-latest.html
|
2023-08-29 10:23:17 -04:00
|
|
|
#endif
|
2023-08-28 10:03:32 -04:00
|
|
|
|
2022-12-08 16:21:20 -05:00
|
|
|
#if CIRCUITPY_OS_GETENV
|
2022-12-08 13:56:24 -05:00
|
|
|
make_empty_file(&vfs_fat->fatfs, "/settings.toml");
|
|
|
|
#endif
|
2019-11-18 15:29:29 -05:00
|
|
|
// make a sample code.py file
|
|
|
|
make_sample_code_file(&vfs_fat->fatfs);
|
2018-03-22 09:06:36 -04:00
|
|
|
|
2019-06-14 16:41:32 -04:00
|
|
|
// create empty lib directory
|
2020-09-28 22:21:17 -04:00
|
|
|
res = f_mkdir(&vfs_fat->fatfs, "/lib");
|
|
|
|
if (res != FR_OK) {
|
2021-12-27 18:58:24 -05:00
|
|
|
return false;
|
2020-09-28 22:21:17 -04:00
|
|
|
}
|
2019-06-14 16:41:32 -04:00
|
|
|
|
2018-03-22 09:06:36 -04:00
|
|
|
// and ensure everything is flushed
|
2018-10-19 21:46:22 -04:00
|
|
|
supervisor_flash_flush();
|
2017-10-10 14:36:00 -04:00
|
|
|
} else if (res != FR_OK) {
|
2021-12-27 18:58:24 -05:00
|
|
|
return false;
|
2017-10-10 14:36:00 -04:00
|
|
|
}
|
2022-10-05 10:35:47 -04:00
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
vfs->str = "/";
|
|
|
|
vfs->len = 1;
|
|
|
|
vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
|
|
|
|
vfs->next = NULL;
|
2022-10-05 10:35:47 -04:00
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
MP_STATE_VM(vfs_mount_table) = vfs;
|
|
|
|
|
|
|
|
// The current directory is used as the boot up directory.
|
|
|
|
// It is set to the internal flash filesystem by default.
|
|
|
|
MP_STATE_PORT(vfs_cur) = vfs;
|
2021-12-27 18:58:24 -05:00
|
|
|
|
2022-10-05 10:35:47 -04:00
|
|
|
#if CIRCUITPY_STORAGE_EXTEND
|
|
|
|
supervisor_flash_update_extended();
|
|
|
|
#endif
|
|
|
|
|
2021-12-27 18:58:24 -05:00
|
|
|
return true;
|
2017-10-10 14:36:00 -04:00
|
|
|
}
|
|
|
|
|
2023-02-28 18:07:35 -05:00
|
|
|
void PLACE_IN_ITCM(filesystem_flush)(void) {
|
2019-03-25 20:42:08 -04:00
|
|
|
// Reset interval before next flush.
|
|
|
|
filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
|
2018-10-19 21:46:22 -04:00
|
|
|
supervisor_flash_flush();
|
2019-04-03 21:28:27 -04:00
|
|
|
// Don't keep caches because this is called when starting or stopping the VM.
|
|
|
|
supervisor_flash_release_cache();
|
2017-10-10 14:36:00 -04:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
void filesystem_set_internal_writable_by_usb(bool writable) {
|
2018-10-19 21:46:22 -04:00
|
|
|
fs_user_mount_t *vfs = &_internal_vfs;
|
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
filesystem_set_writable_by_usb(vfs, writable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void filesystem_set_writable_by_usb(fs_user_mount_t *vfs, bool usb_writable) {
|
|
|
|
if (usb_writable) {
|
2021-04-23 15:26:42 -04:00
|
|
|
vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_USB_WRITABLE;
|
2018-10-19 21:46:22 -04:00
|
|
|
} else {
|
2021-04-23 15:26:42 -04:00
|
|
|
vfs->blockdev.flags &= ~MP_BLOCKDEV_FLAG_USB_WRITABLE;
|
2018-10-19 21:46:22 -04:00
|
|
|
}
|
2017-10-10 14:36:00 -04:00
|
|
|
}
|
|
|
|
|
2019-02-19 17:45:45 -05:00
|
|
|
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs) {
|
2021-04-23 15:26:42 -04:00
|
|
|
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
|
|
|
|
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) == 0;
|
2019-02-19 17:45:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs) {
|
2021-04-23 15:26:42 -04:00
|
|
|
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
|
|
|
|
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) != 0;
|
2019-02-19 17:45:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void filesystem_set_internal_concurrent_write_protection(bool concurrent_write_protection) {
|
|
|
|
filesystem_set_concurrent_write_protection(&_internal_vfs, concurrent_write_protection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concurrent_write_protection) {
|
|
|
|
if (concurrent_write_protection) {
|
2021-04-23 15:26:42 -04:00
|
|
|
vfs->blockdev.flags |= MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED;
|
2019-02-19 17:45:45 -05:00
|
|
|
} else {
|
2021-04-23 15:26:42 -04:00
|
|
|
vfs->blockdev.flags &= ~MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED;
|
2019-02-19 17:45:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
bool filesystem_present(void) {
|
2022-08-04 19:06:27 -04:00
|
|
|
return _mp_vfs.len > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FATFS *filesystem_circuitpy(void) {
|
|
|
|
if (!filesystem_present()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &_internal_vfs.fatfs;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|