summaryrefslogtreecommitdiff
path: root/atmel-samd
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2017-06-28 16:18:13 -0700
committerScott Shawcroft <scott.shawcroft@gmail.com>2017-06-28 16:18:13 -0700
commit725d715a1bcaa5f7c05fd64c1b9da87768b1cbc0 (patch)
tree256bc14063c3d49a502efd0200dcac7816a7d026 /atmel-samd
parent1e16e813e8f1f4534db75a6cc251ffaef76e2162 (diff)
shared-bindings: Introduce storage.remount() so you can set root as
writeable and prevent USB from editing it.
Diffstat (limited to 'atmel-samd')
-rw-r--r--atmel-samd/Makefile1
-rw-r--r--atmel-samd/common-hal/storage/__init__.c46
-rw-r--r--atmel-samd/main.c19
3 files changed, 59 insertions, 7 deletions
diff --git a/atmel-samd/Makefile b/atmel-samd/Makefile
index ec8cd9b26..d3da8241d 100644
--- a/atmel-samd/Makefile
+++ b/atmel-samd/Makefile
@@ -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 \
diff --git a/atmel-samd/common-hal/storage/__init__.c b/atmel-samd/common-hal/storage/__init__.c
new file mode 100644
index 000000000..1cdf5b050
--- /dev/null
+++ b/atmel-samd/common-hal/storage/__init__.c
@@ -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);
+}
diff --git a/atmel-samd/main.c b/atmel-samd/main.c
index 18a3750ce..64b7d8c7f 100644
--- a/atmel-samd/main.c
+++ b/atmel-samd/main.c
@@ -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));