Skip to content

Commit

Permalink
Fix crash when switching between FAT and exFAT formatted cards.
Browse files Browse the repository at this point in the history
All files must be closed before mounting new SD card.
If filesystem stays the same, access to stale file handles fails cleanly.
If filesystem type changes, access to old handle can crash.
  • Loading branch information
PetteriAimonen authored and erichelgeson committed May 26, 2023
1 parent dbf7ae4 commit 65a4566
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/BlueSCSI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,12 @@ void readSCSIDeviceConfig()

static bool mountSDCard()
{
// Prepare for mounting new SD card by closing all old files.
// When switching between FAT and exFAT cards the pointers
// are invalidated and accessing old files results in crash.
invalidate_ini_cache();
g_logfile.close();
scsiDiskCloseSDCardImages();

// Check for the common case, FAT filesystem as first partition
if (SD.begin(SD_CONFIG))
Expand Down
11 changes: 11 additions & 0 deletions src/BlueSCSI_disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ void scsiDiskResetImages()
g_DiskImages[i] = image_config_t();
}

void scsiDiskCloseSDCardImages()
{
for (int i = 0; i < S2S_MAX_TARGETS; i++)
{
if (!g_DiskImages[i].file.isRom())
{
g_DiskImages[i].file.close();
}
}
}

// Verify format conformance to SCSI spec:
// - Empty bytes filled with 0x20 (space)
// - Only values 0x20 to 0x7E
Expand Down
5 changes: 5 additions & 0 deletions src/BlueSCSI_disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ struct image_config_t: public S2S_TargetCfg
bool geometrywarningprinted;
};

// Reset all image configuration to empty reset state, close all images.
void scsiDiskResetImages();

// Close any files opened from SD card (prepare for remounting SD)
void scsiDiskCloseSDCardImages();

bool scsiDiskOpenHDDImage(int target_idx, const char *filename, int scsi_id, int scsi_lun, int blocksize, S2S_CFG_TYPE type = S2S_CFG_FIXED);
void scsiDiskLoadConfig(int target_idx);

Expand Down

0 comments on commit 65a4566

Please sign in to comment.