Skip to content

Commit 51120a9

Browse files
authored
Merge pull request #4730 from dhalbert/fix-usb-safe-mode
Do USB init even in safe mode
2 parents 80f9f51 + 4b45c37 commit 51120a9

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

main.c

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,17 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
447447
FIL* boot_output_file;
448448

449449
STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
450-
// If not in safe mode, run boot before initing USB and capture output in a
451-
// file.
452-
if (filesystem_present() && safe_mode == NO_SAFE_MODE && MP_STATE_VM(vfs_mount_table) != NULL) {
453-
static const char * const boot_py_filenames[] = STRING_LIST("settings.txt", "settings.py", "boot.py", "boot.txt");
450+
// If not in safe mode, run boot before initing USB and capture output in a file.
454451

452+
// There is USB setup to do even if boot.py is not actually run.
453+
const bool ok_to_run = filesystem_present()
454+
&& safe_mode == NO_SAFE_MODE
455+
&& MP_STATE_VM(vfs_mount_table) != NULL;
456+
457+
static const char * const boot_py_filenames[] = STRING_LIST("settings.txt", "settings.py", "boot.py", "boot.txt");
458+
bool skip_boot_output = false;
459+
460+
if (ok_to_run) {
455461
new_status_color(BOOT_RUNNING);
456462

457463
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
@@ -463,8 +469,6 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
463469

464470
bool have_boot_py = first_existing_file_in_list(boot_py_filenames) != NULL;
465471

466-
bool skip_boot_output = false;
467-
468472
// If there's no boot.py file that might write some changing output,
469473
// read the existing copy of CIRCUITPY_BOOT_OUTPUT_FILE and see if its contents
470474
// match the version info we would print anyway. If so, skip writing CIRCUITPY_BOOT_OUTPUT_FILE.
@@ -505,15 +509,20 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
505509
#endif
506510

507511
filesystem_flush();
508-
supervisor_allocation* heap = allocate_remaining_memory();
509-
start_mp(heap);
512+
}
510513

511-
#if CIRCUITPY_USB
512-
// Set up default USB values after boot.py VM starts but before running boot.py.
513-
usb_set_defaults();
514-
#endif
514+
// Do USB setup even if boot.py is not run.
515+
516+
supervisor_allocation* heap = allocate_remaining_memory();
517+
start_mp(heap);
518+
519+
#if CIRCUITPY_USB
520+
// Set up default USB values after boot.py VM starts but before running boot.py.
521+
usb_set_defaults();
522+
#endif
515523

516-
// TODO(tannewt): Re-add support for flashing boot error output.
524+
// TODO(tannewt): Re-add support for flashing boot error output.
525+
if (ok_to_run) {
517526
bool found_boot = maybe_run_list(boot_py_filenames, NULL);
518527
(void) found_boot;
519528

@@ -524,29 +533,28 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
524533
}
525534
boot_output_file = NULL;
526535
#endif
536+
}
527537

528538

529-
#if CIRCUITPY_USB
530-
531-
// Some data needs to be carried over from the USB settings in boot.py
532-
// to the next VM, while the heap is still available.
533-
// Its size can vary, so save it temporarily on the stack,
534-
// and then when the heap goes away, copy it in into a
535-
// storage_allocation.
539+
#if CIRCUITPY_USB
536540

537-
size_t size = usb_boot_py_data_size();
538-
uint8_t usb_boot_py_data[size];
539-
usb_get_boot_py_data(usb_boot_py_data, size);
540-
#endif
541+
// Some data needs to be carried over from the USB settings in boot.py
542+
// to the next VM, while the heap is still available.
543+
// Its size can vary, so save it temporarily on the stack,
544+
// and then when the heap goes away, copy it in into a
545+
// storage_allocation.
541546

542-
cleanup_after_vm(heap);
547+
size_t size = usb_boot_py_data_size();
548+
uint8_t usb_boot_py_data[size];
549+
usb_get_boot_py_data(usb_boot_py_data, size);
550+
#endif
543551

544-
#if CIRCUITPY_USB
545-
// Now give back the data we saved from the heap going away.
546-
usb_return_boot_py_data(usb_boot_py_data, size);
547-
#endif
552+
cleanup_after_vm(heap);
548553

549-
}
554+
#if CIRCUITPY_USB
555+
// Now give back the data we saved from the heap going away.
556+
usb_return_boot_py_data(usb_boot_py_data, size);
557+
#endif
550558
}
551559

552560
STATIC int run_repl(void) {

0 commit comments

Comments
 (0)