@@ -211,14 +211,22 @@ static void start_mp(safe_mode_t safe_mode) {
211211
212212static void stop_mp (void ) {
213213 #if MICROPY_VFS
214- mp_vfs_mount_t * vfs = MP_STATE_VM (vfs_mount_table );
215214
216215 // Unmount all heap allocated vfs mounts.
217- while (gc_ptr_on_heap (vfs )) {
216+ mp_vfs_mount_t * vfs = MP_STATE_VM (vfs_mount_table );
217+ do {
218+ if (gc_ptr_on_heap (vfs )) {
219+ // mp_vfs_umount will splice out an unmounted vfs from the vfs_mount_table linked list.
220+ mp_vfs_umount (vfs -> obj );
221+ // Start over at the beginning of the list since the first entry may have been removed.
222+ vfs = MP_STATE_VM (vfs_mount_table );
223+ continue ;
224+ }
218225 vfs = vfs -> next ;
219- }
220- MP_STATE_VM ( vfs_mount_table ) = vfs ;
226+ } while ( vfs != NULL );
227+
221228 // The last vfs is CIRCUITPY and the root directory.
229+ vfs = MP_STATE_VM (vfs_mount_table );
222230 while (vfs -> next != NULL ) {
223231 vfs = vfs -> next ;
224232 }
@@ -855,6 +863,14 @@ static void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
855863 boot_output = & boot_text ;
856864 #endif
857865
866+ // Get the base filesystem.
867+ fs_user_mount_t * vfs = filesystem_circuitpy ();
868+ FATFS * fs = & vfs -> fatfs ;
869+
870+ // Allow boot.py access to CIRCUITPY, and allow writes to boot_out.txt.
871+ // We can't use the regular flags for this, because they might get modified inside boot.py.
872+ filesystem_set_ignore_write_protection (vfs , true);
873+
858874 // Write version info
859875 mp_printf (& mp_plat_print , "%s\nBoard ID:%s\n" , MICROPY_FULL_VERSION_INFO , CIRCUITPY_BOARD_ID );
860876 #if CIRCUITPY_MICROCONTROLLER && COMMON_HAL_MCU_PROCESSOR_UID_LENGTH > 0
@@ -873,10 +889,6 @@ static void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
873889
874890
875891 #ifdef CIRCUITPY_BOOT_OUTPUT_FILE
876- // Get the base filesystem.
877- fs_user_mount_t * vfs = filesystem_circuitpy ();
878- FATFS * fs = & vfs -> fatfs ;
879-
880892 boot_output = NULL ;
881893 #if CIRCUITPY_STATUS_BAR
882894 supervisor_status_bar_resume ();
@@ -898,16 +910,16 @@ static void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
898910 // in case power is momentary or will fail shortly due to, say a low, battery.
899911 mp_hal_delay_ms (1000 );
900912
901- // USB isn't up, so we can write the file.
902- // operating at the oofatfs (f_open) layer means the usb concurrent write permission
903- // is not even checked!
904913 f_open (fs , & boot_output_file , CIRCUITPY_BOOT_OUTPUT_FILE , FA_WRITE | FA_CREATE_ALWAYS );
905914 UINT chars_written ;
906915 f_write (& boot_output_file , boot_text .buf , boot_text .len , & chars_written );
907916 f_close (& boot_output_file );
908917 filesystem_flush ();
909918 }
910919 #endif
920+
921+ // Back to regular filesystem protections.
922+ filesystem_set_ignore_write_protection (vfs , false);
911923 }
912924
913925 cleanup_after_vm (_exec_result .exception );
0 commit comments