8000 shared-bindings: Introduce storage.remount() so you can set root as · ladyada/circuitpython@725d715 · GitHub
[go: up one dir, main page]

Skip to content

Commit 725d715

Browse files
committed
shared-bindings: Introduce storage.remount() so you can set root as
writeable and prevent USB from editing it.
1 parent 1e16e81 commit 725d715

File tree

7 files changed

+112
-7
lines changed

7 files changed

+112
-7
lines changed

atmel-samd/Makefile

Lines changed: 1 addition & 8000 amp; 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ SRC_COMMON_HAL = \
247247
pulseio/PulseIn.c \
248248
pulseio/PulseOut.c \
249249
pulseio/PWMOut.c \
250+
storage/__init__.c \
250251
time/__init__.c \
251252
touchio/__init__.c \
252253
touchio/TouchIn.c \
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
29+
#include "flash_api.h"
30+
#include "py/mperrno.h"
31+
#include "py/runtime.h"
32+
#include "shared-bindings/storage/__init__.h"
33+
34+
extern volatile bool mp_msc_enabled;
35+
36+
void common_hal_storage_remount(const char* mount_path, bool readonly) {
37+
if (strcmp(mount_path, "/") != 0) {
38+
mp_raise_OSError(MP_EINVAL);
39+
}
40+
41+
if (mp_msc_enabled) {
42+
mp_raise_RuntimeError("Cannot remount '/' when USB is active.");
43+
}
44+
45+
flash_set_usb_writeable(readonly);
46+
}

atmel-samd/main.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -591,15 +591,23 @@ int main(void) {
591591
// Turn on autoreload by default but before boot.py in case it wants to change it.
592592
autoreload_enable();
593593

594+
// By default our internal flash is readonly to local python code and
595+
// writeable over USB. Set it here so that boot.py can change it.
596+
flash_set_usb_writeable(true);
597+
594598
// If not in safe mode, run boot before initing USB and capture output in a
595599
// file.
596600
if (safe_mode == NO_SAFE_MODE && MP_STATE_VM(vfs_mount_table) != NULL) {
597601
new_status_color(BOOT_RUNNING);
598602
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
603+
// Since USB isn't up yet we can cheat and let ourselves write the boot
604+
// output file.
605+
flash_set_usb_writeable(false);
599606
FIL file_pointer;
600607
boot_output_file = &file_pointer;
601608
f_open(&((fs_user_mount_t *) MP_STATE_VM(vfs_mount_table)->obj)->fatfs,
602609
boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_WRITE | FA_CREATE_ALWAYS);
610+
flash_set_usb_writeable(true);
603611
#endif
604612

605613
// TODO(tannewt): Re-add support for flashing boot error output.
@@ -611,6 +619,7 @@ int main(void) {
611619

612620
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
613621
f_close(boot_output_file);
622+
flash_flush();
614623
boot_output_file = NULL;
615624
#endif
616625

@@ -620,9 +629,6 @@ int main(void) {
620629
reset_mp();
621630
}
622631

623-
// Turn off local writing in favor of USB writing prior to initializing USB.
624-
flash_set_usb_writeable(true);
625-
626632
usb_hid_init();
627633

628634
// Start USB after getting everything going.
@@ -674,10 +680,9 @@ void gc_collect(void) {
674680
// pointers from CPU registers, and thus may function incorrectly.
675681
void *dummy;
676682
gc_collect_start();
677-
// This collects root pointers from the first VFS entry which is statically
678-
// allocated. This way we can do VFS operations prior to setting up the heap.
679-
// This also means it can be done once on boot and not repeatedly.
680-
gc_collect_root((void**)&mp_vfs_mount_flash, sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
683+
// This collects root pointers from the VFS mount table. Some of them may
684+
// have lost their references in the VM even though they are mounted.
685+
gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
681686
// This naively collects all object references from an approximate stack
682687
// range.
683688
gc_collect_root(&dummy, ((mp_uint_t)&_estack - (mp_uint_t)&dummy) / sizeof(mp_uint_t));

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ SRC_COMMON_HAL = \
118118
busio/UART.c \
119119
neopixel_write/__init__.c \
120120
os/__init__.c \
121+
storage/__init__.c \
121122
time/__init__.c \
122123
board/__init__.c
123124

esp8266/common-hal/storage/__init__.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
29+
#include "py/runtime.h"
30+
#include "shared-bindings/storage/__init__.h"
31+
32+
void common_hal_storage_remount(const char* mount_path, bool readonly) {
33+
mp_raise_NotImplementedError("");
34+
}

shared-bindings/storage/__init__.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,28 @@ mp_obj_t storage_umount(mp_obj_t mnt_in) {
101101
}
102102
MP_DEFINE_CONST_FUN_OBJ_1(storage_umount_obj, storage_umount);
103103

104+
//| .. function:: remount(mount_path, readonly)
105+
//|
106+
//| Remounts the given path with new parameters.
107+
//|
108+
mp_obj_t storage_remount(mp_obj_t mount_path, mp_obj_t readonly) {
109+
if (!MP_OBJ_IS_STR(mount_path)) {
110+
mp_raise_ValueError("mount_path must be string");
111+
}
112+
113+
common_hal_storage_remount(mp_obj_str_get_str(mount_path),
114+
mp_obj_is_true(readonly));
115+
116+
return mp_const_none;
117+
}
118+
MP_DEFINE_CONST_FUN_OBJ_2(storage_remount_obj, storage_remount);
119+
104120
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
105121
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_storage) },
106122

107123
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
108124
{ MP_OBJ_NEW_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
125+
{ MP_OBJ_NEW_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
109126
{ MP_OBJ_NEW_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
110127
};
111128

shared-bindings/storage/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
void common_hal_storage_mount(mp_obj_t vfs_obj, const char* path, bool readonly);
3434
void common_hal_storage_umount_path(const char* path);
3535
void common_hal_storage_umount_object(mp_obj_t vfs_obj);
36+
void common_hal_storage_remount(const char* path, bool readonly);
3637

3738
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H__

0 commit comments

Comments
 (0)
0