Skip to content

Commit

Permalink
Remove interface header files, split executor
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Dec 10, 2024
1 parent 3382cb9 commit 93d917b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 75 deletions.
80 changes: 80 additions & 0 deletions cpp/s2pdump/disk_executor.cpp
Original file line number Diff line number Diff line change
@@ -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 <spdlog/spdlog.h>
#include "shared/memory_util.h"

using namespace spdlog;
using namespace memory_util;

pair<uint64_t, uint32_t> DiskExecutor::ReadCapacity()
{
vector<uint8_t> buffer(14);
vector<uint8_t> 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<int32_t>(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<int>(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<uint8_t> buffer, uint32_t bstart, uint32_t blength, int length, bool is_write)
{
if (bstart < 16777216 && blength <= 256) {
vector<uint8_t> cdb(6);
cdb[1] = static_cast<uint8_t>(bstart >> 16);
cdb[2] = static_cast<uint8_t>(bstart >> 8);
cdb[3] = static_cast<uint8_t>(bstart);
cdb[4] = static_cast<uint8_t>(blength);

return !initiator_executor->Execute(is_write ? scsi_command::write_6 : scsi_command::read_6, cdb, buffer,
length);
}
else {
vector<uint8_t> cdb(10);
cdb[2] = static_cast<uint8_t>(bstart >> 24);
cdb[3] = static_cast<uint8_t>(bstart >> 16);
cdb[4] = static_cast<uint8_t>(bstart >> 8);
cdb[5] = static_cast<uint8_t>(bstart);
cdb[7] = static_cast<uint8_t>(blength >> 8);
cdb[8] = static_cast<uint8_t>(blength);

return !initiator_executor->Execute(is_write ? scsi_command::write_10 : scsi_command::read_10, cdb, buffer,
length);
}
}

void DiskExecutor::SynchronizeCache()
{
vector<uint8_t> cdb(10);

initiator_executor->Execute(scsi_command::synchronize_cache_10, cdb, { }, 0);
}
31 changes: 31 additions & 0 deletions cpp/s2pdump/disk_executor.h
Original file line number Diff line number Diff line change
@@ -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<uint64_t, uint32_t> ReadCapacity();
bool ReadWriteDisk(span<uint8_t>, uint32_t, uint32_t, int, bool);
void SynchronizeCache();

private:

unique_ptr<S2pDumpExecutor> s2pdump_executor;
};
5 changes: 3 additions & 2 deletions cpp/s2pdump/s2pdump_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
#include <regex>
#include <getopt.h>
#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
{
Expand Down Expand Up @@ -81,7 +82,7 @@ bool S2pDump::Init(bool in_process)
return false;
}

executor = make_unique<S2pDumpExecutor>(*bus, initiator_id, *default_logger());
executor = make_unique<DiskExecutor>(*bus, initiator_id, *default_logger());

instance = this;
// Signal handler for cleaning up
Expand Down
4 changes: 2 additions & 2 deletions cpp/s2pdump/s2pdump_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <fstream>
#include <unordered_map>
#include <vector>
#include "s2pdump_executor.h"
#include "disk_executor.h"

using namespace std;

Expand Down Expand Up @@ -60,7 +60,7 @@ class S2pDump

unique_ptr<Bus> bus;

unique_ptr<S2pDumpExecutor> executor;
unique_ptr<DiskExecutor> executor;

scsi_device_info_t scsi_device_info = { };

Expand Down
66 changes: 0 additions & 66 deletions cpp/s2pdump/s2pdump_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,65 +37,6 @@ bool S2pDumpExecutor::Inquiry(span<uint8_t> buffer)
return !initiator_executor->Execute(scsi_command::inquiry, cdb, buffer, static_cast<int>(buffer.size()));
}

pair<uint64_t, uint32_t> S2pDumpExecutor::ReadCapacity()
{
vector<uint8_t> buffer(14);
vector<uint8_t> 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<int32_t>(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<int>(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<uint8_t> buffer, uint32_t bstart, uint32_t blength, int length, bool is_write)
{
if (bstart < 16777216 && blength <= 256) {
vector<uint8_t> cdb(6);
cdb[1] = static_cast<uint8_t>(bstart >> 16);
cdb[2] = static_cast<uint8_t>(bstart >> 8);
cdb[3] = static_cast<uint8_t>(bstart);
cdb[4] = static_cast<uint8_t>(blength);

return !initiator_executor->Execute(is_write ? scsi_command::write_6 : scsi_command::read_6, cdb, buffer,
length);
}
else {
vector<uint8_t> cdb(10);
cdb[2] = static_cast<uint8_t>(bstart >> 24);
cdb[3] = static_cast<uint8_t>(bstart >> 16);
cdb[4] = static_cast<uint8_t>(bstart >> 8);
cdb[5] = static_cast<uint8_t>(bstart);
cdb[7] = static_cast<uint8_t>(blength >> 8);
cdb[8] = static_cast<uint8_t>(blength);

return !initiator_executor->Execute(is_write ? scsi_command::write_10 : scsi_command::read_10, cdb, buffer,
length);
}
}

bool S2pDumpExecutor::ModeSense6(span<uint8_t> buffer)
{
vector<uint8_t> cdb(6);
Expand All @@ -106,13 +47,6 @@ bool S2pDumpExecutor::ModeSense6(span<uint8_t> buffer)
return !initiator_executor->Execute(scsi_command::mode_sense_6, cdb, buffer, static_cast<int>(buffer.size()));
}

void S2pDumpExecutor::SynchronizeCache()
{
vector<uint8_t> cdb(10);

initiator_executor->Execute(scsi_command::synchronize_cache_10, cdb, { }, 0);
}

set<int> S2pDumpExecutor::ReportLuns()
{
vector<uint8_t> buffer(512);
Expand Down
8 changes: 3 additions & 5 deletions cpp/s2pdump/s2pdump_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ class S2pDumpExecutor
S2pDumpExecutor(Bus &bus, int id, logger &l) : initiator_executor(make_unique<InitiatorExecutor>(bus, id, l))
{
}
~S2pDumpExecutor() = default;

void TestUnitReady() const;
void RequestSense() const;
bool Inquiry(span<uint8_t>);
pair<uint64_t, uint32_t> ReadCapacity();
bool ReadWriteDisk(span<uint8_t>, uint32_t, uint32_t, int, bool);
bool ModeSense6(span<uint8_t>);
void SynchronizeCache();
set<int> ReportLuns();

void Rewind();
Expand All @@ -40,9 +36,11 @@ class S2pDumpExecutor
initiator_executor->SetTarget(id, lun, sasi);
}

private:
protected:

unique_ptr<InitiatorExecutor> initiator_executor;

private:

int default_length = 0xffffff;
};

0 comments on commit 93d917b

Please sign in to comment.