Skip to content

Commit c046b23

Browse files
DavidEGraysondpgeorge
authored andcommitted
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 <[email protected]>
1 parent db4b416 commit c046b23

File tree

13 files changed

+24
-19
lines changed

13 files changed

+24
-19
lines changed

ports/esp32/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void mp_task(void *pvParameter) {
161161
#endif
162162

163163
// run boot-up scripts
164-
pyexec_frozen_module("_boot.py");
164+
pyexec_frozen_module("_boot.py", false);
165165
pyexec_file_if_exists("boot.py");
166166
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
167167
int ret = pyexec_file_if_exists("main.py");

ports/esp8266/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ STATIC void mp_reset(void) {
7474
}
7575

7676
#if MICROPY_MODULE_FROZEN
77-
pyexec_frozen_module("_boot.py");
77+
pyexec_frozen_module("_boot.py", false);
7878
pyexec_file_if_exists("boot.py");
7979
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
8080
pyexec_file_if_exists("main.py");

ports/mimxrt/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int main(void) {
8585
readline_init0();
8686

8787
// Execute _boot.py to set up the filesystem.
88-
pyexec_frozen_module("_boot.py");
88+
pyexec_frozen_module("_boot.py", false);
8989

9090
// Execute user scripts.
9191
int ret = pyexec_file_if_exists("boot.py");

ports/minimal/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int main(int argc, char **argv) {
5555
// do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
5656
// do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
5757
#else
58-
pyexec_frozen_module("frozentest.py");
58+
pyexec_frozen_module("frozentest.py", false);
5959
#endif
6060
mp_deinit();
6161
return 0;

ports/nrf/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ int main(int argc, char **argv) {
184184
int ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
185185

186186
if ((ret == -MP_ENODEV) || (ret == -MP_EIO)) {
187-
pyexec_frozen_module("_mkfs.py"); // Frozen script for formatting flash filesystem.
187+
pyexec_frozen_module("_mkfs.py", false); // Frozen script for formatting flash filesystem.
188188
ret = mp_vfs_mount_and_chdir_protected((mp_obj_t)&nrf_flash_obj, mount_point);
189189
}
190190

ports/powerpc/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ int main(int argc, char **argv) {
9595
pyexec_friendly_repl();
9696
#endif
9797
#else
98-
pyexec_frozen_module("frozentest.py");
98+
pyexec_frozen_module("frozentest.py", false);
9999
#endif
100100
mp_deinit();
101101
return 0;

ports/renesas-ra/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void ra_main(uint32_t reset_mode) {
325325

326326
// Run optional frozen boot code.
327327
#ifdef MICROPY_BOARD_FROZEN_BOOT_FILE
328-
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE);
328+
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE, false);
329329
#endif
330330

331331
// Run boot.py (or whatever else a board configures at this stage).

ports/rp2/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ int main(int argc, char **argv) {
168168

169169
// Execute _boot.py to set up the filesystem.
170170
#if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC
171-
pyexec_frozen_module("_boot_fat.py");
171+
pyexec_frozen_module("_boot_fat.py", false);
172172
#else
173-
pyexec_frozen_module("_boot.py");
173+
pyexec_frozen_module("_boot.py", false);
174174
#endif
175175

176176
// Execute user scripts.

ports/samd/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void samd_main(void) {
5252
readline_init0();
5353

5454
// Execute _boot.py to set up the filesystem.
55-
pyexec_frozen_module("_boot.py");
55+
pyexec_frozen_module("_boot.py", false);
5656

5757
// Execute user scripts.
5858
int ret = pyexec_file_if_exists("boot.py");

ports/stm32/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ void stm32_main(uint32_t reset_mode) {
559559

560560
// Run optional frozen boot code.
561561
#ifdef MICROPY_BOARD_FROZEN_BOOT_FILE
562-
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE);
562+
pyexec_frozen_module(MICROPY_BOARD_FROZEN_BOOT_FILE, false);
563563
#endif
564564

565565
// Run boot.py (or whatever else a board configures at this stage).

ports/teensy/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ int main(void) {
298298
#endif
299299

300300
#if MICROPY_MODULE_FROZEN
301-
pyexec_frozen_module("boot.py");
301+
pyexec_frozen_module("boot.py", false);
302302
#else
303303
if (!pyexec_file_if_exists("/boot.py")) {
304304
flash_error(4);
@@ -310,7 +310,7 @@ int main(void) {
310310

311311
// run main script
312312
#if MICROPY_MODULE_FROZEN
313-
pyexec_frozen_module("main.py");
313+
pyexec_frozen_module("main.py", true);
314314
#else
315315
{
316316
vstr_t *vstr = vstr_new(16);

shared/runtime/pyexec.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ STATIC bool repl_display_debugging_info = 0;
5757
#define EXEC_FLAG_SOURCE_IS_VSTR (1 << 4)
5858
#define EXEC_FLAG_SOURCE_IS_FILENAME (1 << 5)
5959
#define EXEC_FLAG_SOURCE_IS_READER (1 << 6)
60+
#define EXEC_FLAG_NO_INTERRUPT (1 << 7)
6061

6162
// parses, compiles and executes the code in the lexer
6263
// frees the lexer before returning
@@ -113,7 +114,9 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
113114
}
114115

115116
// execute code
116-
mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
117+
if (!(exec_flags & EXEC_FLAG_NO_INTERRUPT)) {
118+
mp_hal_set_interrupt_char(CHAR_CTRL_C);
119+
}
117120
#if MICROPY_REPL_INFO
118121
start = mp_hal_ticks_ms();
119122
#endif
@@ -686,7 +689,7 @@ int pyexec_file(const char *filename) {
686689
int pyexec_file_if_exists(const char *filename) {
687690
#if MICROPY_MODULE_FROZEN
688691
if (mp_find_frozen_module(filename, NULL, NULL) == MP_IMPORT_STAT_FILE) {
689-
return pyexec_frozen_module(filename);
692+
return pyexec_frozen_module(filename, true);
690693
}
691694
#endif
692695
if (mp_import_stat(filename) != MP_IMPORT_STAT_FILE) {
@@ -696,20 +699,22 @@ int pyexec_file_if_exists(const char *filename) {
696699
}
697700

698701
#if MICROPY_MODULE_FROZEN
699-
int pyexec_frozen_module(const char *name) {
702+
int pyexec_frozen_module(const char *name, bool allow_keyboard_interrupt) {
700703
void *frozen_data;
701704
int frozen_type;
702705
mp_find_frozen_module(name, &frozen_type, &frozen_data);
706+
mp_uint_t exec_flags = allow_keyboard_interrupt ? 0 : EXEC_FLAG_NO_INTERRUPT;
703707

704708
switch (frozen_type) {
705709
#if MICROPY_MODULE_FROZEN_STR
706710
case MP_FROZEN_STR:
707-
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, 0);
711+
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, exec_flags);
708712
#endif
709713

710714
#if MICROPY_MODULE_FROZEN_MPY
711715
case MP_FROZEN_MPY:
712-
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_RAW_CODE);
716+
return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, exec_flags |
717+
EXEC_FLAG_SOURCE_IS_RAW_CODE);
713718
#endif
714719

715720
default:

shared/runtime/pyexec.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int pyexec_raw_repl(void);
4646
int pyexec_friendly_repl(void);
4747
int pyexec_file(const char *filename);
4848
int pyexec_file_if_exists(const char *filename);
49-
int pyexec_frozen_module(const char *name);
49+
int pyexec_frozen_module(const char *name, bool allow_keyboard_interrupt);
5050
void pyexec_event_repl_init(void);
5151
int pyexec_event_repl_process_char(int c);
5252
extern uint8_t pyexec_repl_active;

0 commit comments

Comments
 (0)