shared/runtime/pyexec: Don't allow Ctrl+C to interrupt frozen boot code.
Helps prevent the filesystem from getting formatted by mistake, among other things. For example, on a Pico board, entering Ctrl+D and Ctrl+C fast many times will eventually wipe the filesystem (without warning or notice). Further rationale: Ctrl+C is used a lot by automation scripts (eg mpremote) and UI's (eg Mu, Thonny) to get the board into a known state. If the board is not responding for a short time then it's not possible to know if it's just a slow start up (eg in _boot.py), or an infinite loop in the main application. The former should not be interrupted, but the latter should. The only way to distinguish these two cases would be to wait "long enough", and if there's nothing on the serial after "long enough" then assume it's running the application and Ctrl+C should break out of it. But defining "long enough" is impossible for all the different boards and their possible behaviour. The solution in this commit is to make it so that frozen start-up code cannot be interrupted by Ctrl+C. That code then effectively acts like normal C start-up code, which also cannot be interrupted. Note: on the stm32 port this was never seen as an issue because all start-up code is in C. But now other ports start to put more things in _boot.py and so this problem crops up. Signed-off-by: David Grayson <davidegrayson@gmail.com>
This commit is contained in:
parent
db4b416ea8
commit
c046b23ea2
|
@ -161,7 +161,7 @@ soft_reset:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// run boot-up scripts
|
// run boot-up scripts
|
||||||
pyexec_frozen_module("_boot.py");
|
pyexec_frozen_module("_boot.py", false);
|
||||||
pyexec_file_if_exists("boot.py");
|
pyexec_file_if_exists("boot.py");
|
||||||
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
|
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
|
||||||
int ret = pyexec_file_if_exists("main.py");
|
int ret = pyexec_file_if_exists("main.py");
|
||||||
|
|
|
@ -74,7 +74,7 @@ STATIC void mp_reset(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MICROPY_MODULE_FROZEN
|
#if MICROPY_MODULE_FROZEN
|
||||||
pyexec_frozen_module("_boot.py");
|
pyexec_frozen_module("_boot.py", false);
|
||||||
pyexec_file_if_exists("boot.py");
|
pyexec_file_if_exists("boot.py");
|
||||||
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
|
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
|
||||||
pyexec_file_if_exists("main.py");
|
pyexec_file_if_exists("main.py");
|
||||||
|
|
|
@ -85,7 +85,7 @@ int main(void) {
|
||||||
readline_init0();
|
readline_init0();
|
||||||
|
|
||||||
// Execute _boot.py to set up the filesystem.
|
// Execute _boot.py to set up the filesystem.
|
||||||
pyexec_frozen_module("_boot.py");
|
pyexec_frozen_module("_boot.py", false);
|
||||||
|
|
||||||
// Execute user scripts.
|
// Execute user scripts.
|
||||||
int ret = pyexec_file_if_exists("boot.py");
|
int ret = pyexec_file_if_exists("boot.py");
|
||||||
|
|
|
@ -55,7 +55,7 @@ int main(int argc, char **argv) {
|
||||||
// do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
|
// do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
|
||||||
// do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
|
// do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
|
||||||
#else
|
#else
|
||||||
pyexec_frozen_module("frozentest.py");
|
pyexec_frozen_module("frozentest.py", false);
|
||||||
#endif
|
#endif
|
||||||
mp_deinit();
|
mp_deinit();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -184,7 +184,7 @@ soft_reset:
|
||||||
int ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
|
int ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
|
||||||
|
|
||||||
if ((ret == -MP_ENODEV) || (ret == -MP_EIO)) {
|
if ((ret == -MP_ENODEV) || (ret == -MP_EIO)) {
|
||||||
pyexec_frozen_module("_mkfs.py"); // Frozen script for formatting flash filesystem.
|
pyexec_frozen_module("_mkfs.py", false); // Frozen script for formatting flash filesystem.
|
||||||
ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
|
ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,7 +95,7 @@ int main(int argc, char **argv) {
|
||||||
pyexec_friendly_repl();
|
pyexec_friendly_repl();
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
pyexec_frozen_module("frozentest.py");
|
pyexec_frozen_module("frozentest.py", false);
|
||||||
#endif
|
#endif
|
||||||
mp_deinit();
|
mp_deinit();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -325,7 +325,7 @@ soft_reset:
|
||||||
|
|
||||||
// Run optional frozen boot code.
|
// Run optional frozen boot code.
|
||||||
#ifdef MICROPY_BOARD_FROZEN_BOOT_FILE
|
#ifdef MICROPY_BOARD_FROZEN_BOOT_FILE
|
||||||
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE);
|
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE, false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Run boot.py (or whatever else a board configures at this stage).
|
// Run boot.py (or whatever else a board configures at this stage).
|
||||||
|
|
|
@ -168,9 +168,9 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
// Execute _boot.py to set up the filesystem.
|
// Execute _boot.py to set up the filesystem.
|
||||||
#if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC
|
#if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC
|
||||||
pyexec_frozen_module("_boot_fat.py");
|
pyexec_frozen_module("_boot_fat.py", false);
|
||||||
#else
|
#else
|
||||||
pyexec_frozen_module("_boot.py");
|
pyexec_frozen_module("_boot.py", false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Execute user scripts.
|
// Execute user scripts.
|
||||||
|
|
|
@ -52,7 +52,7 @@ void samd_main(void) {
|
||||||
readline_init0();
|
readline_init0();
|
||||||
|
|
||||||
// Execute _boot.py to set up the filesystem.
|
// Execute _boot.py to set up the filesystem.
|
||||||
pyexec_frozen_module("_boot.py");
|
pyexec_frozen_module("_boot.py", false);
|
||||||
|
|
||||||
// Execute user scripts.
|
// Execute user scripts.
|
||||||
int ret = pyexec_file_if_exists("boot.py");
|
int ret = pyexec_file_if_exists("boot.py");
|
||||||
|
|
|
@ -559,7 +559,7 @@ soft_reset:
|
||||||
|
|
||||||
// Run optional frozen boot code.
|
// Run optional frozen boot code.
|
||||||
#ifdef MICROPY_BOARD_FROZEN_BOOT_FILE
|
#ifdef MICROPY_BOARD_FROZEN_BOOT_FILE
|
||||||
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE);
|
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE, false);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Run boot.py (or whatever else a board configures at this stage).
|
// Run boot.py (or whatever else a board configures at this stage).
|
||||||
|
|
|
@ -298,7 +298,7 @@ soft_reset:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MICROPY_MODULE_FROZEN
|
#if MICROPY_MODULE_FROZEN
|
||||||
pyexec_frozen_module("boot.py");
|
pyexec_frozen_module("boot.py", false);
|
||||||
#else
|
#else
|
||||||
if (!pyexec_file_if_exists("/boot.py")) {
|
if (!pyexec_file_if_exists("/boot.py")) {
|
||||||
flash_error(4);
|
flash_error(4);
|
||||||
|
@ -310,7 +310,7 @@ soft_reset:
|
||||||
|
|
||||||
// run main script
|
// run main script
|
||||||
#if MICROPY_MODULE_FROZEN
|
#if MICROPY_MODULE_FROZEN
|
||||||
pyexec_frozen_module("main.py");
|
pyexec_frozen_module("main.py", true);
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
vstr_t *vstr = vstr_new(16);
|
vstr_t *vstr = vstr_new(16);
|
||||||
|
|
|
@ -57,6 +57,7 @@ STATIC bool repl_display_debugging_info = 0;
|
||||||
#define EXEC_FLAG_SOURCE_IS_VSTR (1 << 4)
|
#define EXEC_FLAG_SOURCE_IS_VSTR (1 << 4)
|
||||||
#define EXEC_FLAG_SOURCE_IS_FILENAME (1 << 5)
|
#define EXEC_FLAG_SOURCE_IS_FILENAME (1 << 5)
|
||||||
#define EXEC_FLAG_SOURCE_IS_READER (1 << 6)
|
#define EXEC_FLAG_SOURCE_IS_READER (1 << 6)
|
||||||
|
#define EXEC_FLAG_NO_INTERRUPT (1 << 7)
|
||||||
|
|
||||||
// parses, compiles and executes the code in the lexer
|
// parses, compiles and executes the code in the lexer
|
||||||
// frees the lexer before returning
|
// frees the lexer before returning
|
||||||
|
@ -113,7 +114,9 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute code
|
// execute code
|
||||||
mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
|
if (!(exec_flags & EXEC_FLAG_NO_INTERRUPT)) {
|
||||||
|
mp_hal_set_interrupt_char(CHAR_CTRL_C);
|
||||||
|
}
|
||||||
#if MICROPY_REPL_INFO
|
#if MICROPY_REPL_INFO
|
||||||
start = mp_hal_ticks_ms();
|
start = mp_hal_ticks_ms();
|
||||||
#endif
|
#endif
|
||||||
|
@ -686,7 +689,7 @@ int pyexec_file(const char *filename) {
|
||||||
int pyexec_file_if_exists(const char *filename) {
|
int pyexec_file_if_exists(const char *filename) {
|
||||||
#if MICROPY_MODULE_FROZEN
|
#if MICROPY_MODULE_FROZEN
|
||||||
if (mp_find_frozen_module(filename, NULL, NULL) == MP_IMPORT_STAT_FILE) {
|
if (mp_find_frozen_module(filename, NULL, NULL) == MP_IMPORT_STAT_FILE) {
|
||||||
return pyexec_frozen_module(filename);
|
return pyexec_frozen_module(filename, true);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (mp_import_stat(filename) != MP_IMPORT_STAT_FILE) {
|
if (mp_import_stat(filename) != MP_IMPORT_STAT_FILE) {
|
||||||
|
@ -696,20 +699,22 @@ int pyexec_file_if_exists(const char *filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MICROPY_MODULE_FROZEN
|
#if MICROPY_MODULE_FROZEN
|
||||||
int pyexec_frozen_module(const char *name) {
|
int pyexec_frozen_module(const char *name, bool allow_keyboard_interrupt) {
|
||||||
void *frozen_data;
|
void *frozen_data;
|
||||||
int frozen_type;
|
int frozen_type;
|
||||||
mp_find_frozen_module(name, &frozen_type, &frozen_data);
|
mp_find_frozen_module(name, &frozen_type, &frozen_data);
|
||||||
|
mp_uint_t exec_flags = allow_keyboard_interrupt ? 0 : EXEC_FLAG_NO_INTERRUPT;
|
||||||
|
|
||||||
switch (frozen_type) {
|
switch (frozen_type) {
|
||||||
#if MICROPY_MODULE_FROZEN_STR
|
#if MICROPY_MODULE_FROZEN_STR
|
||||||
case MP_FROZEN_STR:
|
case MP_FROZEN_STR:
|
||||||
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, 0);
|
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, exec_flags);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MICROPY_MODULE_FROZEN_MPY
|
#if MICROPY_MODULE_FROZEN_MPY
|
||||||
case MP_FROZEN_MPY:
|
case MP_FROZEN_MPY:
|
||||||
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_RAW_CODE);
|
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, exec_flags |
|
||||||
|
EXEC_FLAG_SOURCE_IS_RAW_CODE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -46,7 +46,7 @@ int pyexec_raw_repl(void);
|
||||||
int pyexec_friendly_repl(void);
|
int pyexec_friendly_repl(void);
|
||||||
int pyexec_file(const char *filename);
|
int pyexec_file(const char *filename);
|
||||||
int pyexec_file_if_exists(const char *filename);
|
int pyexec_file_if_exists(const char *filename);
|
||||||
int pyexec_frozen_module(const char *name);
|
int pyexec_frozen_module(const char *name, bool allow_keyboard_interrupt);
|
||||||
void pyexec_event_repl_init(void);
|
void pyexec_event_repl_init(void);
|
||||||
int pyexec_event_repl_process_char(int c);
|
int pyexec_event_repl_process_char(int c);
|
||||||
extern uint8_t pyexec_repl_active;
|
extern uint8_t pyexec_repl_active;
|
||||||
|
|
Loading…
Reference in New Issue