From 4a4cf12bca1018d617641bdeca1496bd935658c4 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Wed, 25 Aug 2021 12:45:56 +0200 Subject: [PATCH] Added sector size validation --- src/raspberrypi/devices/disk.cpp | 10 ++++++++-- src/raspberrypi/devices/disk.h | 2 +- src/raspberrypi/devices/sasihd.cpp | 9 +++++++-- src/raspberrypi/devices/scsicd.cpp | 2 +- src/raspberrypi/devices/scsihd.cpp | 7 ++++++- src/raspberrypi/devices/scsimo.cpp | 8 ++++---- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/raspberrypi/devices/disk.cpp b/src/raspberrypi/devices/disk.cpp index 951f2bca..7eb502f9 100644 --- a/src/raspberrypi/devices/disk.cpp +++ b/src/raspberrypi/devices/disk.cpp @@ -1747,11 +1747,15 @@ bool Disk::GetStartAndCount(SASIDEV *controller, uint64_t& start, uint32_t& coun int Disk::GetSectorSizeInBytes() const { - return 1 << disk.size; + return disk.size ? 1 << disk.size : 0; } -void Disk::SetSectorSizeInBytes(int size) +bool Disk::SetSectorSizeInBytes(int size, bool sasi) { + if (sasi && size != 256 && size != 1024) { + return false; + } + switch (size) { case 256: disk.size = 8; @@ -1778,6 +1782,8 @@ void Disk::SetSectorSizeInBytes(int size) disk.size = 9; break; } + + return true; } int Disk::GetSectorSize() const diff --git a/src/raspberrypi/devices/disk.h b/src/raspberrypi/devices/disk.h index 196b45ef..dbf5933f 100644 --- a/src/raspberrypi/devices/disk.h +++ b/src/raspberrypi/devices/disk.h @@ -124,7 +124,7 @@ public: int SelectCheck10(const DWORD *cdb); // SELECT(10) check int GetSectorSizeInBytes() const; - void SetSectorSizeInBytes(int); + bool SetSectorSizeInBytes(int, bool); int GetSectorSize() const; bool IsSectorSizeConfigurable() const; vector GetSectorSizes() const; diff --git a/src/raspberrypi/devices/sasihd.cpp b/src/raspberrypi/devices/sasihd.cpp index 196ca0d4..f3200294 100644 --- a/src/raspberrypi/devices/sasihd.cpp +++ b/src/raspberrypi/devices/sasihd.cpp @@ -70,7 +70,12 @@ void SASIHD::Open(const Filepath& path) fio.Close(); // Sector size (default 256 bytes) and number of blocks - SetSectorSizeInBytes(GetConfiguredSectorSize() ? GetConfiguredSectorSize() : 256); + if (!SetSectorSizeInBytes(GetConfiguredSectorSize() ? GetConfiguredSectorSize() : 256, true)) { + stringstream error; + error << "Invalid sector size " << GetConfiguredSectorSize(); + throw io_exception(error.str()); + + } SetBlockCount((DWORD)(size >> GetSectorSize())); #if defined(REMOVE_FIXED_SASIHD_SIZE) @@ -110,8 +115,8 @@ void SASIHD::Open(const Filepath& path) } #endif // REMOVE_FIXED_SASIHD_SIZE - // Call the base class Disk::Open(path); + FileSupport::SetPath(path); } //--------------------------------------------------------------------------- diff --git a/src/raspberrypi/devices/scsicd.cpp b/src/raspberrypi/devices/scsicd.cpp index 382a446b..b3d75c7c 100644 --- a/src/raspberrypi/devices/scsicd.cpp +++ b/src/raspberrypi/devices/scsicd.cpp @@ -342,7 +342,7 @@ void SCSICD::Open(const Filepath& path) ASSERT(GetBlockCount() > 0); // Sector size 2048 bytes - SetSectorSizeInBytes(2048); + SetSectorSizeInBytes(2048, false); Disk::Open(path); FileSupport::SetPath(path); diff --git a/src/raspberrypi/devices/scsihd.cpp b/src/raspberrypi/devices/scsihd.cpp index 13a75c3a..176e7bc9 100644 --- a/src/raspberrypi/devices/scsihd.cpp +++ b/src/raspberrypi/devices/scsihd.cpp @@ -71,7 +71,12 @@ void SCSIHD::Open(const Filepath& path) fio.Close(); // Sector size (default 512 bytes) and number of blocks - SetSectorSizeInBytes(GetConfiguredSectorSize() ? GetConfiguredSectorSize() : 512); + if (!SetSectorSizeInBytes(GetConfiguredSectorSize() ? GetConfiguredSectorSize() : 512, false)) { + stringstream error; + error << "Invalid sector size " << GetConfiguredSectorSize(); + throw io_exception(error.str()); + + } SetBlockCount((DWORD)(size >> GetSectorSize())); // File size must be a multiple of the sector size diff --git a/src/raspberrypi/devices/scsimo.cpp b/src/raspberrypi/devices/scsimo.cpp index 2e05280a..cbf3a6e2 100644 --- a/src/raspberrypi/devices/scsimo.cpp +++ b/src/raspberrypi/devices/scsimo.cpp @@ -57,28 +57,28 @@ void SCSIMO::Open(const Filepath& path) // 128MB case 0x797f400: // 512 bytes per sector - SetSectorSizeInBytes(512); + SetSectorSizeInBytes(512, false); SetBlockCount(248826); break; // 230MB case 0xd9eea00: // 512 bytes per sector - SetSectorSizeInBytes(512); + SetSectorSizeInBytes(512, false); SetBlockCount(446325); break; // 540MB case 0x1fc8b800: // 512 bytes per sector - SetSectorSizeInBytes(512); + SetSectorSizeInBytes(512, false); SetBlockCount(1041500); break; // 640MB case 0x25e28000: // 2048 bytes per sector - SetSectorSizeInBytes(2048); + SetSectorSizeInBytes(2048, false); SetBlockCount(310352); break;