shared-bindings: Introduce storage.remount() so you can set root as

writeable and prevent USB from editing it.
This commit is contained in:
Scott Shawcroft 2017-06-28 16:18:13 -07:00
parent 1e16e813e8
commit 725d715a1b
7 changed files with 112 additions and 7 deletions

View File

@ -247,6 +247,7 @@ SRC_COMMON_HAL = \
pulseio/PulseIn.c \
pulseio/PulseOut.c \
pulseio/PWMOut.c \
storage/__init__.c \
time/__init__.c \
touchio/__init__.c \
touchio/TouchIn.c \

View File

@ -0,0 +1,46 @@
/*
* This file is part of the Micro Python 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.
*/
#include <string.h>
#include "flash_api.h"
#include "py/mperrno.h"
#include "py/runtime.h"
#include "shared-bindings/storage/__init__.h"
extern volatile bool mp_msc_enabled;
void common_hal_storage_remount(const char* mount_path, bool readonly) {
if (strcmp(mount_path, "/") != 0) {
mp_raise_OSError(MP_EINVAL);
}
if (mp_msc_enabled) {
mp_raise_RuntimeError("Cannot remount '/' when USB is active.");
}
flash_set_usb_writeable(readonly);
}

View File

@ -591,15 +591,23 @@ int main(void) {
// Turn on autoreload by default but before boot.py in case it wants to change it.
autoreload_enable();
// By default our internal flash is readonly to local python code and
// writeable over USB. Set it here so that boot.py can change it.
flash_set_usb_writeable(true);
// If not in safe mode, run boot before initing USB and capture output in a
// file.
if (safe_mode == NO_SAFE_MODE && MP_STATE_VM(vfs_mount_table) != NULL) {
new_status_color(BOOT_RUNNING);
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
// Since USB isn't up yet we can cheat and let ourselves write the boot
// output file.
flash_set_usb_writeable(false);
FIL file_pointer;
boot_output_file = &file_pointer;
f_open(&((fs_user_mount_t *) MP_STATE_VM(vfs_mount_table)->obj)->fatfs,
boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_WRITE | FA_CREATE_ALWAYS);
flash_set_usb_writeable(true);
#endif
// TODO(tannewt): Re-add support for flashing boot error output.
@ -611,6 +619,7 @@ int main(void) {
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
f_close(boot_output_file);
flash_flush();
boot_output_file = NULL;
#endif
@ -620,9 +629,6 @@ int main(void) {
reset_mp();
}
// Turn off local writing in favor of USB writing prior to initializing USB.
flash_set_usb_writeable(true);
usb_hid_init();
// Start USB after getting everything going.
@ -674,10 +680,9 @@ void gc_collect(void) {
// pointers from CPU registers, and thus may function incorrectly.
void *dummy;
gc_collect_start();
// This collects root pointers from the first VFS entry which is statically
// allocated. This way we can do VFS operations prior to setting up the heap.
// This also means it can be done once on boot and not repeatedly.
gc_collect_root((void**)&mp_vfs_mount_flash, sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
// This collects root pointers from the VFS mount table. Some of them may
// have lost their references in the VM even though they are mounted.
gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
// This naively collects all object references from an approximate stack
// range.
gc_collect_root(&dummy, ((mp_uint_t)&_estack - (mp_uint_t)&dummy) / sizeof(mp_uint_t));

View File

@ -118,6 +118,7 @@ SRC_COMMON_HAL = \
busio/UART.c \
neopixel_write/__init__.c \
os/__init__.c \
storage/__init__.c \
time/__init__.c \
board/__init__.c

View File

@ -0,0 +1,34 @@
/*
* This file is part of the Micro Python 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.
*/
#include <string.h>
#include "py/runtime.h"
#include "shared-bindings/storage/__init__.h"
void common_hal_storage_remount(const char* mount_path, bool readonly) {
mp_raise_NotImplementedError("");
}

View File

@ -101,11 +101,28 @@ mp_obj_t storage_umount(mp_obj_t mnt_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(storage_umount_obj, storage_umount);
//| .. function:: remount(mount_path, readonly)
//|
//| Remounts the given path with new parameters.
//|
mp_obj_t storage_remount(mp_obj_t mount_path, mp_obj_t readonly) {
if (!MP_OBJ_IS_STR(mount_path)) {
mp_raise_ValueError("mount_path must be string");
}
common_hal_storage_remount(mp_obj_str_get_str(mount_path),
mp_obj_is_true(readonly));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(storage_remount_obj, storage_remount);
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_storage) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
};

View File

@ -33,5 +33,6 @@
void common_hal_storage_mount(mp_obj_t vfs_obj, const char* path, bool readonly);
void common_hal_storage_umount_path(const char* path);
void common_hal_storage_umount_object(mp_obj_t vfs_obj);
void common_hal_storage_remount(const char* path, bool readonly);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H__