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"
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
2018-04-10 12:24:27 -04:00
|
|
|
void 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;
|
2017-10-10 14:36:00 -04:00
|
|
|
vfs_fat->flags = 0;
|
2018-10-19 21:46:22 -04:00
|
|
|
supervisor_flash_init_vfs(vfs_fat);
|
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);
|
2017-10-10 14:36:00 -04:00
|
|
|
|
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.
|
2017-10-10 14:36:00 -04:00
|
|
|
uint8_t working_buf[_MAX_SS];
|
|
|
|
res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf));
|
2018-04-09 12:52:42 -04:00
|
|
|
// Flush the new file system to make sure it's repaired immediately.
|
2018-10-19 21:46:22 -04:00
|
|
|
supervisor_flash_flush();
|
2017-10-10 14:36:00 -04:00
|
|
|
if (res != FR_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set label
|
|
|
|
f_setlabel(&vfs_fat->fatfs, "CIRCUITPY");
|
2018-03-22 09:06:36 -04:00
|
|
|
|
|
|
|
// inhibit file indexing on MacOS
|
|
|
|
f_mkdir(&vfs_fat->fatfs, "/.fseventsd");
|
|
|
|
make_empty_file(&vfs_fat->fatfs, "/.metadata_never_index");
|
|
|
|
make_empty_file(&vfs_fat->fatfs, "/.Trashes");
|
2018-03-24 11:44:24 -04:00
|
|
|
make_empty_file(&vfs_fat->fatfs, "/.fseventsd/no_log");
|
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) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-19 21:46:22 -04:00
|
|
|
mp_vfs_mount_t *vfs = &_mp_vfs;
|
2017-10-10 14:36:00 -04:00
|
|
|
vfs->str = "/";
|
|
|
|
vfs->len = 1;
|
|
|
|
vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
|
|
|
|
vfs->next = NULL;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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) {
|
2018-10-19 21:46:22 -04:00
|
|
|
vfs->flags |= FSUSER_USB_WRITABLE;
|
|
|
|
} else {
|
|
|
|
vfs->flags &= ~FSUSER_USB_WRITABLE;
|
|
|
|
}
|
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) {
|
|
|
|
return (vfs->flags & FSUSER_CONCURRENT_WRITE_PROTECTED) == 0 ||
|
|
|
|
(vfs->flags & FSUSER_USB_WRITABLE) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs) {
|
|
|
|
return (vfs->flags & FSUSER_CONCURRENT_WRITE_PROTECTED) == 0 ||
|
|
|
|
(vfs->flags & FSUSER_USB_WRITABLE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
vfs->flags |= FSUSER_CONCURRENT_WRITE_PROTECTED;
|
|
|
|
} else {
|
|
|
|
vfs->flags &= ~FSUSER_CONCURRENT_WRITE_PROTECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 14:36:00 -04:00
|
|
|
bool filesystem_present(void) {
|
|
|
|
return true;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|