@@ -81,8 +81,7 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp
8181 }
8282
8383 // create new object
84- fs_user_mount_t * vfs ;
85- MP_STATE_PORT (fs_user_mount )[i ] = vfs = m_new_obj (fs_user_mount_t );
84+ fs_user_mount_t * vfs = m_new_obj (fs_user_mount_t );
8685 vfs -> str = mnt_str ;
8786 vfs -> len = mnt_len ;
8887 vfs -> flags = FSUSER_FREE_OBJ ;
@@ -108,6 +107,11 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp
108107 vfs -> writeblocks [0 ] = MP_OBJ_NULL ;
109108 }
110109
110+ // Register the vfs object so that it can be found by the FatFS driver using
111+ // ff_get_ldnumber. We don't register it any earlier than this point in case there
112+ // is an exception, in which case there would remain a partially mounted device.
113+ MP_STATE_PORT (fs_user_mount )[i ] = vfs ;
114+
111115 // mount the block device (if mkfs, only pre-mount)
112116 FRESULT res = f_mount (& vfs -> fatfs , vfs -> str , !mkfs );
113117 // check the result
@@ -120,6 +124,7 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp
120124 res = f_mkfs (vfs -> str , 1 , 0 );
121125 if (res != FR_OK ) {
122126mkfs_error :
127+ MP_STATE_PORT (fs_user_mount )[i ] = NULL ;
123128 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "can't mkfs" ));
124129 }
125130 if (mkfs ) {
@@ -132,6 +137,7 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp
132137 return NULL ;
133138 }
134139 } else {
140+ MP_STATE_PORT (fs_user_mount )[i ] = NULL ;
135141 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "can't mount" ));
136142 }
137143
0 commit comments