Disk: Allow mismatched access for Read and Write drivers
This will cause the driver to fall back to the least common denominator.v0.3.0-dev
parent
1118428250
commit
0a15adbe91
|
|
@ -488,6 +488,19 @@ optional<uint64_t> Device::readLogicalDisk(uint64_t pos, uint8_t* into, uint64_t
|
|||
|
||||
std::lock_guard<std::mutex> lk(diskLock);
|
||||
|
||||
if(diskReadDriver->getAccess() == Disk::Access::EntireCard && diskWriteDriver->getAccess() == Disk::Access::VSA) {
|
||||
// We have mismatched drivers, we need to add an offset to the diskReadDriver
|
||||
const auto offset = Disk::FindVSAInFAT([this, &timeout](uint64_t pos, uint8_t *into, uint64_t amount) {
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
auto ret = diskReadDriver->readLogicalDisk(*com, report, pos, into, amount, timeout);
|
||||
timeout -= std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
|
||||
return ret;
|
||||
});
|
||||
if(!offset.has_value())
|
||||
return nullopt;
|
||||
diskReadDriver->setVSAOffset(*offset);
|
||||
}
|
||||
|
||||
// This is needed for certain read drivers which take over the communication stream
|
||||
const auto lifetime = suppressDisconnects();
|
||||
|
||||
|
|
@ -550,9 +563,18 @@ optional<uint64_t> Device::getVSAOffsetInLogicalDisk() {
|
|||
if (diskReadDriver->getAccess() == Disk::Access::VSA || diskReadDriver->getAccess() == Disk::Access::None)
|
||||
return 0ull;
|
||||
|
||||
return Disk::FindVSAInFAT([this](uint64_t pos, uint8_t *into, uint64_t amount) {
|
||||
auto offset = Disk::FindVSAInFAT([this](uint64_t pos, uint8_t *into, uint64_t amount) {
|
||||
return diskReadDriver->readLogicalDisk(*com, report, pos, into, amount);
|
||||
});
|
||||
if(!offset.has_value())
|
||||
return nullopt;
|
||||
|
||||
if(diskReadDriver->getAccess() == Disk::Access::EntireCard && diskWriteDriver->getAccess() == Disk::Access::VSA) {
|
||||
// We have mismatched drivers, we need to add an offset to the diskReadDriver
|
||||
diskReadDriver->setVSAOffset(*offset);
|
||||
return 0ull;
|
||||
}
|
||||
return *offset;
|
||||
}
|
||||
|
||||
optional<bool> Device::getDigitalIO(IO type, size_t number /* = 1 */) {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ optional<uint64_t> ReadDriver::readLogicalDisk(Communication& com, device_eventh
|
|||
// beginning or end of the block.
|
||||
std::vector<uint8_t> alignedReadBuffer;
|
||||
|
||||
pos += vsaOffset;
|
||||
const uint32_t idealBlockSize = getBlockSizeBounds().second;
|
||||
const uint64_t startBlock = pos / idealBlockSize;
|
||||
const uint32_t posWithinFirstBlock = static_cast<uint32_t>(pos % idealBlockSize);
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ optional<uint64_t> WriteDriver::writeLogicalDisk(Communication& com, device_even
|
|||
// ensure an operation is atomic
|
||||
std::vector<uint8_t> atomicBuffer(idealBlockSize);
|
||||
|
||||
pos += vsaOffset;
|
||||
const uint64_t startBlock = pos / idealBlockSize;
|
||||
const uint32_t posWithinFirstBlock = static_cast<uint32_t>(pos % idealBlockSize);
|
||||
uint64_t blocks = amount / idealBlockSize + (amount % idealBlockSize ? 1 : 0);
|
||||
|
|
|
|||
|
|
@ -25,8 +25,27 @@ enum class Access {
|
|||
class Driver {
|
||||
public:
|
||||
virtual ~Driver() = default;
|
||||
virtual Access getAccess() const = 0;
|
||||
Access getAccess() const {
|
||||
if(vsaOffset)
|
||||
return Access::VSA;
|
||||
return getPossibleAccess();
|
||||
}
|
||||
virtual std::pair<uint32_t, uint32_t> getBlockSizeBounds() const = 0;
|
||||
|
||||
void setVSAOffset(uint64_t offset) { vsaOffset = offset; }
|
||||
|
||||
protected:
|
||||
uint64_t vsaOffset = 0;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Report the possible access that this driver has
|
||||
*
|
||||
* In some cases, such as a mismatched possible access between
|
||||
* read and write drivers, this will be overridden in the driver
|
||||
* layer.
|
||||
*/
|
||||
virtual Access getPossibleAccess() const = 0;
|
||||
};
|
||||
|
||||
} // namespace Disk
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ namespace Disk {
|
|||
*/
|
||||
class ExtExtractorDiskReadDriver : public ReadDriver {
|
||||
public:
|
||||
Access getAccess() const override { return Access::EntireCard; }
|
||||
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override {
|
||||
static_assert(SectorSize <= std::numeric_limits<uint32_t>::max(), "Incorrect sector size");
|
||||
static_assert(SectorSize >= std::numeric_limits<uint32_t>::min(), "Incorrect sector size");
|
||||
|
|
@ -36,6 +35,8 @@ private:
|
|||
|
||||
uint8_t headerLength = 7; // Correct for Ethernet
|
||||
|
||||
Access getPossibleAccess() const override { return Access::EntireCard; }
|
||||
|
||||
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ namespace Disk {
|
|||
*/
|
||||
class NeoMemoryDiskReadDriver : public ReadDriver {
|
||||
public:
|
||||
Access getAccess() const override { return Access::VSA; }
|
||||
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override {
|
||||
static_assert(SectorSize <= std::numeric_limits<uint32_t>::max(), "Incorrect sector size");
|
||||
static_assert(SectorSize >= std::numeric_limits<uint32_t>::min(), "Incorrect sector size");
|
||||
|
|
@ -33,6 +32,8 @@ private:
|
|||
uint64_t cachePos = 0;
|
||||
std::chrono::time_point<std::chrono::steady_clock> cachedAt;
|
||||
|
||||
Access getPossibleAccess() const override { return Access::VSA; }
|
||||
|
||||
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ public:
|
|||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout = DefaultTimeout) override;
|
||||
optional<uint64_t> writeLogicalDisk(Communication& com, device_eventhandler_t report, ReadDriver& readDriver,
|
||||
uint64_t pos, const uint8_t* from, uint64_t amount, std::chrono::milliseconds timeout = DefaultTimeout) override;
|
||||
Access getAccess() const override { return Access::None; }
|
||||
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override {
|
||||
static_assert(SectorSize <= std::numeric_limits<uint32_t>::max(), "Incorrect sector size");
|
||||
static_assert(SectorSize >= std::numeric_limits<uint32_t>::min(), "Incorrect sector size");
|
||||
|
|
@ -30,6 +29,8 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
Access getPossibleAccess() const override { return Access::None; }
|
||||
|
||||
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||
optional<uint64_t> writeLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ namespace Disk {
|
|||
*/
|
||||
class PlasionDiskReadDriver : public ReadDriver {
|
||||
public:
|
||||
Access getAccess() const override { return Access::EntireCard; }
|
||||
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override {
|
||||
static_assert(SectorSize <= std::numeric_limits<uint32_t>::max(), "Incorrect sector size");
|
||||
static_assert(SectorSize >= std::numeric_limits<uint32_t>::min(), "Incorrect sector size");
|
||||
|
|
@ -31,6 +30,8 @@ private:
|
|||
uint64_t cachePos = 0;
|
||||
std::chrono::time_point<std::chrono::steady_clock> cachedAt;
|
||||
|
||||
Access getPossibleAccess() const override { return Access::EntireCard; }
|
||||
|
||||
optional<uint64_t> readLogicalDiskAligned(Communication& com, device_eventhandler_t report,
|
||||
uint64_t pos, uint8_t* into, uint64_t amount, std::chrono::milliseconds timeout) override;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ using namespace icsneo;
|
|||
|
||||
class MockDiskDriver : public Disk::ReadDriver, public Disk::WriteDriver {
|
||||
public:
|
||||
Disk::Access getAccess() const override { return Disk::Access::EntireCard; }
|
||||
std::pair<uint32_t, uint32_t> getBlockSizeBounds() const override { return { 8, 256 }; }
|
||||
|
||||
optional<uint64_t> readLogicalDiskAligned(Communication&, device_eventhandler_t,
|
||||
|
|
@ -74,6 +73,9 @@ public:
|
|||
size_t atomicityChecks = 0;
|
||||
bool supportsAtomic = true; // Ability to simulate a driver that doesn't support atomic writes
|
||||
std::function<void(void)> afterReadHook;
|
||||
|
||||
private:
|
||||
Disk::Access getPossibleAccess() const override { return Disk::Access::EntireCard; }
|
||||
};
|
||||
|
||||
class DiskDriverTest : public ::testing::Test {
|
||||
|
|
|
|||
Loading…
Reference in New Issue