diff --git a/cpp/devices/disk.cpp b/cpp/devices/disk.cpp index bc8b53d02c..788e7ee69b 100644 --- a/cpp/devices/disk.cpp +++ b/cpp/devices/disk.cpp @@ -696,11 +696,17 @@ uint32_t Disk::GetSectorSizeInBytes() const void Disk::SetSectorSizeInBytes(uint32_t size_in_bytes) { if (DeviceFactory device_factory; !device_factory.GetSectorSizes(GetType()).contains(size_in_bytes)) { - throw io_exception("Invalid sector size of " + to_string(size_in_bytes) + " byte(s)"); + throw io_exception("Invalid sector size of " + to_string(size_in_bytes) + " byte(s)"); } + uint64_t current_blocks = GetBlockCount(); + uint32_t current_size_shift_count = size_shift_count; + uint64_t current_size = current_blocks << current_size_shift_count; size_shift_count = CalculateShiftCount(size_in_bytes); assert(size_shift_count); + if ((current_blocks > 0) && (current_size_shift_count > 0)) { + SetBlockCount(current_size >> size_shift_count); + } } uint32_t Disk::GetConfiguredSectorSize() const @@ -714,7 +720,7 @@ bool Disk::SetConfiguredSectorSize(const DeviceFactory& device_factory, uint32_t return false; } - configured_sector_size = configured_size; + configured_sector_size = configured_size; return true; }