diff --git a/cpp/s2pdump/disk_executor.cpp b/cpp/s2pdump/disk_executor.cpp new file mode 100644 index 00000000..b5d18e31 --- /dev/null +++ b/cpp/s2pdump/disk_executor.cpp @@ -0,0 +1,80 @@ +//--------------------------------------------------------------------------- +// +// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi +// +// Copyright (C) 2024 Uwe Seimet +// +//--------------------------------------------------------------------------- + +#include "disk_executor.h" +#include +#include "shared/memory_util.h" + +using namespace spdlog; +using namespace memory_util; + +pair DiskExecutor::ReadCapacity() +{ + vector buffer(14); + vector cdb(10); + + if (initiator_executor->Execute(scsi_command::read_capacity_10, cdb, buffer, 8)) { + return {0, 0}; + } + + uint64_t capacity = GetInt32(buffer, 0); + + int sector_size_offset = 4; + + if (static_cast(capacity) == -1) { + cdb.resize(16); + // READ CAPACITY(16), not READ LONG(16) + cdb[1] = 0x10; + + if (initiator_executor->Execute(scsi_command::read_capacity_16_read_long_16, cdb, buffer, + static_cast(buffer.size()))) { + return {0, 0}; + } + + capacity = GetInt64(buffer, 0); + + sector_size_offset = 8; + } + + const uint32_t sector_size = GetInt32(buffer, sector_size_offset); + + return {capacity + 1, sector_size}; +} + +bool DiskExecutor::ReadWriteDisk(span buffer, uint32_t bstart, uint32_t blength, int length, bool is_write) +{ + if (bstart < 16777216 && blength <= 256) { + vector cdb(6); + cdb[1] = static_cast(bstart >> 16); + cdb[2] = static_cast(bstart >> 8); + cdb[3] = static_cast(bstart); + cdb[4] = static_cast(blength); + + return !initiator_executor->Execute(is_write ? scsi_command::write_6 : scsi_command::read_6, cdb, buffer, + length); + } + else { + vector cdb(10); + cdb[2] = static_cast(bstart >> 24); + cdb[3] = static_cast(bstart >> 16); + cdb[4] = static_cast(bstart >> 8); + cdb[5] = static_cast(bstart); + cdb[7] = static_cast(blength >> 8); + cdb[8] = static_cast(blength); + + return !initiator_executor->Execute(is_write ? scsi_command::write_10 : scsi_command::read_10, cdb, buffer, + length); + } +} + +void DiskExecutor::SynchronizeCache() +{ + vector cdb(10); + + initiator_executor->Execute(scsi_command::synchronize_cache_10, cdb, { }, 0); +} diff --git a/cpp/s2pdump/disk_executor.h b/cpp/s2pdump/disk_executor.h new file mode 100644 index 00000000..9cc802a8 --- /dev/null +++ b/cpp/s2pdump/disk_executor.h @@ -0,0 +1,31 @@ +//--------------------------------------------------------------------------- +// +// SCSI2Pi, SCSI device emulator and SCSI tools for the Raspberry Pi +// +// Copyright (C) 2024 Uwe Seimet +// +//--------------------------------------------------------------------------- + +#pragma once + +#include "s2pdump_executor.h" + +using namespace std; + +class DiskExecutor : public S2pDumpExecutor +{ + +public: + + DiskExecutor(Bus &b, int i, logger &l) : S2pDumpExecutor(b, i, l) + { + } + + pair ReadCapacity(); + bool ReadWriteDisk(span, uint32_t, uint32_t, int, bool); + void SynchronizeCache(); + +private: + + unique_ptr s2pdump_executor; +}; diff --git a/cpp/s2pdump/s2pdump_core.cpp b/cpp/s2pdump/s2pdump_core.cpp index 5ad9dc1c..5bafab28 100644 --- a/cpp/s2pdump/s2pdump_core.cpp +++ b/cpp/s2pdump/s2pdump_core.cpp @@ -15,14 +15,15 @@ #include #include #include "buses/bus_factory.h" +#include "disk_executor.h" #include "initiator/initiator_util.h" #include "shared/s2p_exceptions.h" #include "shared/simh_util.h" using namespace filesystem; using namespace s2p_util; -using namespace simh_util; using namespace initiator_util; +using namespace simh_util; void S2pDump::CleanUp() const { @@ -81,7 +82,7 @@ bool S2pDump::Init(bool in_process) return false; } - executor = make_unique(*bus, initiator_id, *default_logger()); + executor = make_unique(*bus, initiator_id, *default_logger()); instance = this; // Signal handler for cleaning up diff --git a/cpp/s2pdump/s2pdump_core.h b/cpp/s2pdump/s2pdump_core.h index 6a83266a..9d540211 100644 --- a/cpp/s2pdump/s2pdump_core.h +++ b/cpp/s2pdump/s2pdump_core.h @@ -11,7 +11,7 @@ #include #include #include -#include "s2pdump_executor.h" +#include "disk_executor.h" using namespace std; @@ -60,7 +60,7 @@ class S2pDump unique_ptr bus; - unique_ptr executor; + unique_ptr executor; scsi_device_info_t scsi_device_info = { }; diff --git a/cpp/s2pdump/s2pdump_executor.cpp b/cpp/s2pdump/s2pdump_executor.cpp index 00505d41..f5bee280 100644 --- a/cpp/s2pdump/s2pdump_executor.cpp +++ b/cpp/s2pdump/s2pdump_executor.cpp @@ -37,65 +37,6 @@ bool S2pDumpExecutor::Inquiry(span buffer) return !initiator_executor->Execute(scsi_command::inquiry, cdb, buffer, static_cast(buffer.size())); } -pair S2pDumpExecutor::ReadCapacity() -{ - vector buffer(14); - vector cdb(10); - - if (initiator_executor->Execute(scsi_command::read_capacity_10, cdb, buffer, 8)) { - return {0, 0}; - } - - uint64_t capacity = GetInt32(buffer, 0); - - int sector_size_offset = 4; - - if (static_cast(capacity) == -1) { - cdb.resize(16); - // READ CAPACITY(16), not READ LONG(16) - cdb[1] = 0x10; - - if (initiator_executor->Execute(scsi_command::read_capacity_16_read_long_16, cdb, buffer, - static_cast(buffer.size()))) { - return {0, 0}; - } - - capacity = GetInt64(buffer, 0); - - sector_size_offset = 8; - } - - const uint32_t sector_size = GetInt32(buffer, sector_size_offset); - - return {capacity + 1, sector_size}; -} - -bool S2pDumpExecutor::ReadWriteDisk(span buffer, uint32_t bstart, uint32_t blength, int length, bool is_write) -{ - if (bstart < 16777216 && blength <= 256) { - vector cdb(6); - cdb[1] = static_cast(bstart >> 16); - cdb[2] = static_cast(bstart >> 8); - cdb[3] = static_cast(bstart); - cdb[4] = static_cast(blength); - - return !initiator_executor->Execute(is_write ? scsi_command::write_6 : scsi_command::read_6, cdb, buffer, - length); - } - else { - vector cdb(10); - cdb[2] = static_cast(bstart >> 24); - cdb[3] = static_cast(bstart >> 16); - cdb[4] = static_cast(bstart >> 8); - cdb[5] = static_cast(bstart); - cdb[7] = static_cast(blength >> 8); - cdb[8] = static_cast(blength); - - return !initiator_executor->Execute(is_write ? scsi_command::write_10 : scsi_command::read_10, cdb, buffer, - length); - } -} - bool S2pDumpExecutor::ModeSense6(span buffer) { vector cdb(6); @@ -106,13 +47,6 @@ bool S2pDumpExecutor::ModeSense6(span buffer) return !initiator_executor->Execute(scsi_command::mode_sense_6, cdb, buffer, static_cast(buffer.size())); } -void S2pDumpExecutor::SynchronizeCache() -{ - vector cdb(10); - - initiator_executor->Execute(scsi_command::synchronize_cache_10, cdb, { }, 0); -} - set S2pDumpExecutor::ReportLuns() { vector buffer(512); diff --git a/cpp/s2pdump/s2pdump_executor.h b/cpp/s2pdump/s2pdump_executor.h index 0db00923..d2bb481b 100644 --- a/cpp/s2pdump/s2pdump_executor.h +++ b/cpp/s2pdump/s2pdump_executor.h @@ -21,15 +21,11 @@ class S2pDumpExecutor S2pDumpExecutor(Bus &bus, int id, logger &l) : initiator_executor(make_unique(bus, id, l)) { } - ~S2pDumpExecutor() = default; void TestUnitReady() const; void RequestSense() const; bool Inquiry(span); - pair ReadCapacity(); - bool ReadWriteDisk(span, uint32_t, uint32_t, int, bool); bool ModeSense6(span); - void SynchronizeCache(); set ReportLuns(); void Rewind(); @@ -40,9 +36,11 @@ class S2pDumpExecutor initiator_executor->SetTarget(id, lun, sasi); } -private: +protected: unique_ptr initiator_executor; +private: + int default_length = 0xffffff; };