Browse Source

Limit drive capacity to actual file size (#80)

In versions 1.0.8 and 1.0.9, the full allocated size of the file
was used instead of its actual size. Because FAT allocates space
typically in 32 kB blocks, this could be larger than the actual
size of the image.

This fix can affect drives formatted using firmware versions 1.0.8
and 1.0.9 if the image file size was different than the allocated
size. A notice is printed in that case and the old behavior can
be restored with config setting [SCSI] UseFATAllocSize = 1
Petteri Aimonen 3 years ago
parent
commit
811309de53
1 changed files with 18 additions and 2 deletions
  1. 18 2
      src/ZuluSCSI_disk.cpp

+ 18 - 2
src/ZuluSCSI_disk.cpp

@@ -96,15 +96,31 @@ public:
         {
             m_fsfile = SD.open(filename, O_RDWR);
 
+            uint32_t sectorcount = m_fsfile.size() / SD_SECTOR_SIZE;
             uint32_t begin = 0, end = 0;
-            if (m_fsfile.contiguousRange(&begin, &end))
+            if (m_fsfile.contiguousRange(&begin, &end) && end >= begin + sectorcount)
             {
                 // Convert to raw mapping, this avoids some unnecessary
                 // access overhead in SdFat library.
                 m_israw = true;
                 m_blockdev = SD.card();
                 m_bgnsector = begin;
-                m_endsector = end;
+
+                if (end != begin + sectorcount)
+                {
+                    uint32_t allocsize = end - begin + 1;
+                    azlog("---- NOTE: File ", filename, " has FAT allocated size of ", (int)allocsize, " sectors and file size of ", (int)sectorcount, " sectors");
+                    azlog("---- Due to issue #80 in ZuluSCSI version 1.0.8 and 1.0.9 the allocated size was mistakenly reported to SCSI controller.");
+                    azlog("---- If the drive was formatted using those versions, you may have problems accessing it with newer firmware.");
+                    azlog("---- The old behavior can be restored with setting  [SCSI] UseFATAllocSize = 1 in " CONFIGFILE);
+
+                    if (ini_getbool("SCSI", "UseFATAllocSize", 0, CONFIGFILE))
+                    {
+                        sectorcount = allocsize;
+                    }
+                }
+
+                m_endsector = begin + sectorcount - 1;
                 m_fsfile.close();
             }
         }