From 502b19bfb218febcc8953f009302d5d08ad4495d Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Sun, 29 Aug 2021 16:45:12 +0200 Subject: [PATCH] Improved block size check --- src/raspberrypi/devices/device_factory.cpp | 14 +++++--------- src/raspberrypi/devices/device_factory.h | 6 +++--- src/raspberrypi/devices/disk.cpp | 14 +++++++------- src/raspberrypi/devices/disk.h | 4 ++-- src/raspberrypi/rascsi.cpp | 2 +- src/raspberrypi/rasutil.cpp | 2 +- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/raspberrypi/devices/device_factory.cpp b/src/raspberrypi/devices/device_factory.cpp index c6f4feaf..cbc9f616 100644 --- a/src/raspberrypi/devices/device_factory.cpp +++ b/src/raspberrypi/devices/device_factory.cpp @@ -32,20 +32,16 @@ DeviceFactory::DeviceFactory() sector_sizes_scsi.insert(2048); sector_sizes_scsi.insert(4096); - // 128 MB, 512 bytes per block, 248826 blocks + // 128 MB, 512 bytes per sector, 248826 sectors geometries_mo[0x797f400] = make_pair(512, 248826); - // 230 MB, 512 bytes per sector, 446325 blocks + // 230 MB, 512 bytes per block, 446325 sectors geometries_mo[0xd9eea00] = make_pair(512, 446325); - // 540 MB, 512 bytes per sector, 1041500 blocks + // 540 MB, 512 bytes per sector, 1041500 sectors geometries_mo[0x1fc8b800] = make_pair(512, 1041500); - // 640 MB, 20248 bytes per sector, 310352 blocks + // 640 MB, 20248 bytes per sector, 310352 sectors geometries_mo[0x25e28000] = make_pair(2048, 310352); } -DeviceFactory::~DeviceFactory() -{ -} - DeviceFactory& DeviceFactory::instance() { static DeviceFactory instance; @@ -143,7 +139,7 @@ Device *DeviceFactory::CreateDevice(PbDeviceType& type, const string& filename, } } catch(const illegal_argument_exception& e) { - // There was an internal problem with setting the INQUIRY DATA + // There was an internal problem with setting up the device data return NULL; } diff --git a/src/raspberrypi/devices/device_factory.h b/src/raspberrypi/devices/device_factory.h index b4fd77ac..6bdf543c 100644 --- a/src/raspberrypi/devices/device_factory.h +++ b/src/raspberrypi/devices/device_factory.h @@ -18,16 +18,16 @@ using namespace std; +typedef pair Geometry; + class Device; class DeviceFactory { public: - typedef pair Geometry; - DeviceFactory(); - ~DeviceFactory(); + ~DeviceFactory() {}; static DeviceFactory& instance(); diff --git a/src/raspberrypi/devices/disk.cpp b/src/raspberrypi/devices/disk.cpp index e003f9d8..f129b6c3 100644 --- a/src/raspberrypi/devices/disk.cpp +++ b/src/raspberrypi/devices/disk.cpp @@ -1756,7 +1756,7 @@ void Disk::SetSectorSizeInBytes(uint32_t size, bool sasi) set sector_sizes = sasi ? DeviceFactory::instance().GetSasiSectorSizes() : DeviceFactory::instance().GetScsiSectorSizes(); if (sector_sizes.find(size) == sector_sizes.end()) { stringstream error; - error << "Invalid sector size of " << size << " bytes"; + error << "Invalid block size of " << size << " bytes"; throw io_exception(error.str()); } @@ -1809,8 +1809,10 @@ uint32_t Disk::GetConfiguredSectorSize() const bool Disk::SetConfiguredSectorSize(uint32_t configured_sector_size) { - if (configured_sector_size != 512 && configured_sector_size != 1024 && - configured_sector_size != 2048 && configured_sector_size != 4096) { + const DeviceFactory& device_factory = DeviceFactory::instance(); + + set sector_sizes = IsSCSI() ? device_factory.GetScsiSectorSizes() : device_factory.GetSasiSectorSizes(); + if (sector_sizes.find(configured_sector_size) == sector_sizes.end()) { return false; } @@ -1819,15 +1821,13 @@ bool Disk::SetConfiguredSectorSize(uint32_t configured_sector_size) return true; } -bool Disk::SetGeometries(const map& geometries) +void Disk::SetGeometries(const map& geometries) { if (!IsMo()) { - return false; + throw illegal_argument_exception("Can't set geometry for"); } this->geometries = geometries; - - return true; } void Disk::SetGeometryForCapacity(uint64_t capacity) { diff --git a/src/raspberrypi/devices/disk.h b/src/raspberrypi/devices/disk.h index 3fe7eacb..2e845c59 100644 --- a/src/raspberrypi/devices/disk.h +++ b/src/raspberrypi/devices/disk.h @@ -43,7 +43,7 @@ private: uint32_t configured_sector_size; // The mapping of supported capacities to block sizes and block counts, empty if there is no capacity restriction - map geometries; + map geometries; SASIDEV::ctrl_t *ctrl; @@ -137,7 +137,7 @@ public: void SetSectorSizes(const set&); uint32_t GetConfiguredSectorSize() const; bool SetConfiguredSectorSize(uint32_t); - bool SetGeometries(const map&); + void SetGeometries(const map&); void SetGeometryForCapacity(uint64_t); uint32_t GetBlockCount() const; void SetBlockCount(uint32_t); diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index 327a3a0d..09e3196b 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -727,7 +727,7 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pbDevice, const PbOperation op Disk *disk = dynamic_cast(device); if (disk && disk->IsSectorSizeConfigurable()) { if (!disk->SetConfiguredSectorSize(pbDevice.block_size())) { - error << "Invalid block size " << pbDevice.block_size(); + error << "Invalid block size " << pbDevice.block_size() << " bytes"; return ReturnStatus(fd, false, error); } } diff --git a/src/raspberrypi/rasutil.cpp b/src/raspberrypi/rasutil.cpp index c96ecbe2..e99c9086 100644 --- a/src/raspberrypi/rasutil.cpp +++ b/src/raspberrypi/rasutil.cpp @@ -34,7 +34,7 @@ string ListDevices(const PbDevices& devices) } list sorted_devices = { devices.devices().begin(), devices.devices().end() }; - sorted_devices.sort([](const PbDevice& a, const PbDevice& b) { return a.id() < b.id() && a.unit() < b.unit(); }); + sorted_devices.sort([](const auto& a, const auto& b) { return a.id() < b.id() && a.unit() < b.unit(); }); for (const auto& device : sorted_devices) { string filename;