Browse Source

Fix crash when switching between FAT and exFAT formatted cards.

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.
Petteri Aimonen 2 năm trước cách đây
mục cha
commit
65a4566e0d
3 tập tin đã thay đổi với 21 bổ sung0 xóa
  1. 5 0
      src/BlueSCSI.cpp
  2. 11 0
      src/BlueSCSI_disk.cpp
  3. 5 0
      src/BlueSCSI_disk.h

+ 5 - 0
src/BlueSCSI.cpp

@@ -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))

+ 11 - 0
src/BlueSCSI_disk.cpp

@@ -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

+ 5 - 0
src/BlueSCSI_disk.h

@@ -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);