2022-11-02 22:41:45 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-11-09 07:40:26 +00:00
|
|
|
// for Raspberry Pi
|
2022-11-02 22:41:45 +00:00
|
|
|
//
|
2022-11-09 07:40:26 +00:00
|
|
|
// Powered by XM6 TypeG Technology.
|
|
|
|
// Copyright (C) 2016-2020 GIMONS
|
2022-12-09 15:50:45 +00:00
|
|
|
// Copyright (C) 2022 akuker
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-11-02 22:41:45 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
// TODO Evaluate CHECK CONDITION after sending a command
|
|
|
|
// TODO Send IDENTIFY message in order to support LUNS > 7
|
2023-10-30 10:34:07 +00:00
|
|
|
// TODO Get rid of some fields in favor of method arguments
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
#include "scsidump/scsidump_core.h"
|
|
|
|
#include "hal/gpiobus_factory.h"
|
|
|
|
#include "hal/systimer.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "controllers/controller_manager.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-12-09 15:50:45 +00:00
|
|
|
#include "shared/piscsi_util.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <filesystem>
|
2022-12-09 15:50:45 +00:00
|
|
|
#include <chrono>
|
2022-11-09 07:40:26 +00:00
|
|
|
#include <csignal>
|
2022-11-10 06:44:06 +00:00
|
|
|
#include <cstddef>
|
2022-11-02 22:41:45 +00:00
|
|
|
#include <cstring>
|
2022-11-09 07:40:26 +00:00
|
|
|
#include <fstream>
|
2022-12-09 15:50:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <unistd.h>
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2023-10-15 06:38:15 +00:00
|
|
|
using namespace filesystem;
|
2022-11-09 07:40:26 +00:00
|
|
|
using namespace spdlog;
|
|
|
|
using namespace scsi_defs;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_util;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::CleanUp()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
if (bus != nullptr) {
|
|
|
|
bus->Cleanup();
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
void ScsiDump::TerminationHandler(int)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
CleanUp();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
// Process will terminate automatically
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool ScsiDump::Banner(span<char *> args) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
cout << piscsi_util::Banner("(Hard Disk Dump/Restore Utility)");
|
|
|
|
|
|
|
|
if (args.size() < 2 || string(args[1]) == "-h" || string(args[1]) == "--help") {
|
2023-10-30 10:34:07 +00:00
|
|
|
cout << "Usage: " << args[0] << " -t ID[:LUN] [-i BID] -f FILE [-v] [-r] [-s BUFFER_SIZE] [-p] [-I] [-S]\n"
|
2023-10-15 06:38:15 +00:00
|
|
|
<< " ID is the target device ID (0-" << (ControllerManager::GetScsiIdMax() - 1) << ").\n"
|
|
|
|
<< " LUN is the optional target device LUN (0-" << (ControllerManager::GetScsiLunMax() -1 ) << ")."
|
|
|
|
<< " Default is 0.\n"
|
2022-12-09 15:50:45 +00:00
|
|
|
<< " BID is the PiSCSI board ID (0-7). Default is 7.\n"
|
|
|
|
<< " FILE is the dump file path.\n"
|
2023-10-15 06:38:15 +00:00
|
|
|
<< " BUFFER_SIZE is the transfer buffer size in bytes, at least " << MINIMUM_BUFFER_SIZE
|
2022-12-09 15:50:45 +00:00
|
|
|
<< " bytes. Default is 1 MiB.\n"
|
|
|
|
<< " -v Enable verbose logging.\n"
|
|
|
|
<< " -r Restore instead of dump.\n"
|
|
|
|
<< " -p Generate .properties file to be used with the PiSCSI web interface. Only valid for dump mode.\n"
|
2023-10-30 10:34:07 +00:00
|
|
|
<< " -I Display INQUIRY data of ID[:LUN].\n"
|
|
|
|
<< " -S Scan SCSI bus for devices.\n"
|
2022-12-09 15:50:45 +00:00
|
|
|
<< flush;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
bool ScsiDump::Init() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
// Signal handler for cleaning up
|
|
|
|
struct sigaction termination_handler;
|
|
|
|
termination_handler.sa_handler = TerminationHandler;
|
|
|
|
sigemptyset(&termination_handler.sa_mask);
|
|
|
|
termination_handler.sa_flags = 0;
|
|
|
|
sigaction(SIGTERM, &termination_handler, nullptr);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
bus = GPIOBUS_Factory::Create(BUS::mode_e::INITIATOR);
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
return bus != nullptr;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
void ScsiDump::ParseArguments(span<char *> args)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
int opt;
|
|
|
|
|
|
|
|
int buffer_size = DEFAULT_BUFFER_SIZE;
|
|
|
|
|
|
|
|
opterr = 0;
|
2023-10-30 10:34:07 +00:00
|
|
|
while ((opt = getopt(static_cast<int>(args.size()), args.data(), "i:f:s:t:rvpIS")) != -1) {
|
2022-12-09 15:50:45 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'i':
|
|
|
|
if (!GetAsUnsignedInt(optarg, initiator_id) || initiator_id > 7) {
|
|
|
|
throw parser_exception("Invalid PiSCSI board ID " + to_string(initiator_id) + " (0-7)");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
filename = optarg;
|
|
|
|
break;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
case 'I':
|
|
|
|
inquiry = true;
|
|
|
|
break;
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
case 's':
|
|
|
|
if (!GetAsUnsignedInt(optarg, buffer_size) || buffer_size < MINIMUM_BUFFER_SIZE) {
|
2023-10-15 06:38:15 +00:00
|
|
|
throw parser_exception("Buffer size must be at least " + to_string(MINIMUM_BUFFER_SIZE / 1024) + " KiB");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
case 'S':
|
|
|
|
scan_bus = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (const string error = ProcessId(optarg, target_id, target_lun); !error.empty()) {
|
2022-12-09 15:50:45 +00:00
|
|
|
throw parser_exception(error);
|
|
|
|
}
|
2023-10-30 10:34:07 +00:00
|
|
|
break;
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
case 'v':
|
|
|
|
set_level(level::debug);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
restore = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
properties_file = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
if (!scan_bus && !inquiry && filename.empty()) {
|
|
|
|
throw parser_exception("Missing filename");
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:53:05 +00:00
|
|
|
if (!scan_bus && target_id == -1) {
|
2023-10-30 10:34:07 +00:00
|
|
|
throw parser_exception("Missing target ID");
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
if (target_id == initiator_id) {
|
|
|
|
throw parser_exception("Target ID and PiSCSI board ID must not be identical");
|
|
|
|
}
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
if (target_lun == -1) {
|
|
|
|
target_lun = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scan_bus) {
|
|
|
|
inquiry = false;
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = vector<uint8_t>(buffer_size);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
void ScsiDump::WaitForPhase(phase_t phase) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
spdlog::debug(string("Waiting for ") + BUS::GetPhaseStrRaw(phase) + " phase");
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
// Timeout (3000ms)
|
|
|
|
const uint32_t now = SysTimer::GetTimerLow();
|
2023-10-30 10:34:07 +00:00
|
|
|
while ((SysTimer::GetTimerLow() - now) < 3'000'000) {
|
2022-12-09 15:50:45 +00:00
|
|
|
bus->Acquire();
|
|
|
|
if (bus->GetREQ() && bus->GetPhase() == phase) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
throw phase_exception("Expected " + string(BUS::GetPhaseStrRaw(phase)) + " phase, actual phase is " +
|
|
|
|
string(BUS::GetPhaseStrRaw(bus->GetPhase())));
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::Selection() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
// Set initiator and target ID
|
|
|
|
auto data = static_cast<byte>(1 << initiator_id);
|
|
|
|
data |= static_cast<byte>(1 << target_id);
|
|
|
|
bus->SetDAT(static_cast<uint8_t>(data));
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
bus->SetSEL(true);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
WaitForBusy();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
bus->SetSEL(false);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::Command(scsi_command cmd, vector<uint8_t>& cdb) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
spdlog::debug("Executing " + command_mapping.find(cmd)->second.second);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Selection();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
WaitForPhase(phase_t::command);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
cdb[0] = static_cast<uint8_t>(cmd);
|
|
|
|
cdb[1] = static_cast<uint8_t>(static_cast<byte>(cdb[1]) | static_cast<byte>(target_lun << 5));
|
|
|
|
if (static_cast<int>(cdb.size()) !=
|
|
|
|
bus->SendHandShake(cdb.data(), static_cast<int>(cdb.size()), BUS::SEND_NO_DELAY)) {
|
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
throw phase_exception(command_mapping.find(cmd)->second.second + string(" failed"));
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::DataIn(int length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
WaitForPhase(phase_t::datain);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
if (!bus->ReceiveHandShake(buffer.data(), length)) {
|
2023-10-30 10:34:07 +00:00
|
|
|
throw phase_exception("DATA IN failed");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::DataOut(int length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
WaitForPhase(phase_t::dataout);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
if (!bus->SendHandShake(buffer.data(), length, BUS::SEND_NO_DELAY)) {
|
2023-10-30 10:34:07 +00:00
|
|
|
throw phase_exception("DATA OUT failed");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::Status() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
WaitForPhase(phase_t::status);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
if (array<uint8_t, 256> buf; bus->ReceiveHandShake(buf.data(), 1) != 1) {
|
2023-10-30 10:34:07 +00:00
|
|
|
throw phase_exception("STATUS failed");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::MessageIn() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
WaitForPhase(phase_t::msgin);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
if (array<uint8_t, 256> buf; bus->ReceiveHandShake(buf.data(), 1) != 1) {
|
2023-10-30 10:34:07 +00:00
|
|
|
throw phase_exception("MESSAGE IN failed");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::BusFree() const
|
2022-11-09 07:40:26 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
bus->Reset();
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::TestUnitReady() const
|
2022-11-09 07:40:26 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
vector<uint8_t> cdb(6);
|
|
|
|
Command(scsi_command::eCmdTestUnitReady, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::RequestSense()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
vector<uint8_t> cdb(6);
|
|
|
|
cdb[4] = 0xff;
|
|
|
|
Command(scsi_command::eCmdRequestSense, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
DataIn(256);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
BusFree();
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::Inquiry()
|
2022-11-09 07:40:26 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
vector<uint8_t> cdb(6);
|
|
|
|
cdb[4] = 0xff;
|
|
|
|
Command(scsi_command::eCmdInquiry, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
DataIn(256);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
pair<uint64_t, uint32_t> ScsiDump::ReadCapacity()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
vector<uint8_t> cdb(10);
|
|
|
|
Command(scsi_command::eCmdReadCapacity10, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
DataIn(8);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
uint64_t capacity = (static_cast<uint32_t>(buffer[0]) << 24) | (static_cast<uint32_t>(buffer[1]) << 16) |
|
|
|
|
(static_cast<uint32_t>(buffer[2]) << 8) | static_cast<uint32_t>(buffer[3]);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
int sector_size_offset = 4;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
if (static_cast<int32_t>(capacity) == -1) {
|
|
|
|
cdb.resize(16);
|
|
|
|
// READ CAPACITY(16), not READ LONG(16)
|
|
|
|
cdb[1] = 0x10;
|
|
|
|
Command(scsi_command::eCmdReadCapacity16_ReadLong16, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
DataIn(14);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
capacity = (static_cast<uint64_t>(buffer[0]) << 56) | (static_cast<uint64_t>(buffer[1]) << 48) |
|
|
|
|
(static_cast<uint64_t>(buffer[2]) << 40) | (static_cast<uint64_t>(buffer[3]) << 32) |
|
|
|
|
(static_cast<uint64_t>(buffer[4]) << 24) | (static_cast<uint64_t>(buffer[5]) << 16) |
|
|
|
|
(static_cast<uint64_t>(buffer[6]) << 8) | static_cast<uint64_t>(buffer[7]);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
sector_size_offset = 8;
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
const uint32_t sector_size = (static_cast<uint32_t>(buffer[sector_size_offset]) << 24) |
|
|
|
|
(static_cast<uint32_t>(buffer[sector_size_offset + 1]) << 16) |
|
|
|
|
(static_cast<uint32_t>(buffer[sector_size_offset + 2]) << 8) |
|
|
|
|
static_cast<uint32_t>(buffer[sector_size_offset + 3]);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
return { capacity, sector_size };
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::Read10(uint32_t bstart, uint32_t blength, uint32_t length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
vector<uint8_t> cdb(10);
|
|
|
|
cdb[2] = (uint8_t)(bstart >> 24);
|
|
|
|
cdb[3] = (uint8_t)(bstart >> 16);
|
|
|
|
cdb[4] = (uint8_t)(bstart >> 8);
|
|
|
|
cdb[5] = (uint8_t)bstart;
|
|
|
|
cdb[7] = (uint8_t)(blength >> 8);
|
|
|
|
cdb[8] = (uint8_t)blength;
|
|
|
|
Command(scsi_command::eCmdRead10, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
DataIn(length);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::Write10(uint32_t bstart, uint32_t blength, uint32_t length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
vector<uint8_t> cdb(10);
|
|
|
|
cdb[2] = (uint8_t)(bstart >> 24);
|
|
|
|
cdb[3] = (uint8_t)(bstart >> 16);
|
|
|
|
cdb[4] = (uint8_t)(bstart >> 8);
|
|
|
|
cdb[5] = (uint8_t)bstart;
|
|
|
|
cdb[7] = (uint8_t)(blength >> 8);
|
|
|
|
cdb[8] = (uint8_t)blength;
|
|
|
|
Command(scsi_command::eCmdWrite10, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
DataOut(length);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
BusFree();
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
void ScsiDump::WaitForBusy() const
|
2022-11-09 07:40:26 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
// Wait for busy for up to 2 s
|
|
|
|
int count = 10000;
|
|
|
|
do {
|
|
|
|
// Wait 20 ms
|
|
|
|
const timespec ts = {.tv_sec = 0, .tv_nsec = 20 * 1000};
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
bus->Acquire();
|
|
|
|
if (bus->GetBSY()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (count--);
|
|
|
|
|
|
|
|
// Success if the target is busy
|
|
|
|
if (!bus->GetBSY()) {
|
2023-10-30 10:34:07 +00:00
|
|
|
throw phase_exception("SELECTION failed");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
int ScsiDump::run(span<char *> args)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
if (!Banner(args)) {
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
try {
|
|
|
|
ParseArguments(args);
|
2023-10-30 10:34:07 +00:00
|
|
|
}
|
|
|
|
catch (const parser_exception& e) {
|
|
|
|
cerr << "Error: " << e.what() << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
if (getuid()) {
|
|
|
|
cerr << "Error: GPIO bus access requires root permissions. Are you running as root?" << endl;
|
2022-12-09 15:50:45 +00:00
|
|
|
return EXIT_FAILURE;
|
2023-10-30 10:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef USE_SEL_EVENT_ENABLE
|
|
|
|
cerr << "Error: No PiSCSI hardware support" << endl;
|
|
|
|
return EXIT_FAILURE;
|
2022-11-02 22:41:45 +00:00
|
|
|
#endif
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
if (!Init()) {
|
|
|
|
cerr << "Error: Can't initialize bus" << endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (scan_bus) {
|
|
|
|
ScanBus();
|
|
|
|
}
|
|
|
|
else if (inquiry) {
|
|
|
|
DisplayBoardId();
|
|
|
|
|
|
|
|
inquiry_info_t inq_info;
|
|
|
|
DisplayInquiry(inq_info, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DumpRestore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const phase_exception& e) {
|
|
|
|
cerr << "Error: " << e.what() << endl;
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
CleanUp();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
return EXIT_FAILURE;
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
CleanUp();
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
return EXIT_SUCCESS;
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
void ScsiDump::DisplayBoardId() const
|
|
|
|
{
|
|
|
|
cout << DIVIDER << "\nPiSCSI board ID is " << initiator_id << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiDump::ScanBus()
|
|
|
|
{
|
|
|
|
DisplayBoardId();
|
|
|
|
|
|
|
|
for (target_id = 0; target_id < ControllerManager::GetScsiIdMax(); target_id++) {
|
|
|
|
if (initiator_id == target_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (target_lun = 0; target_lun < 8; target_lun++) {
|
|
|
|
inquiry_info_t inq_info;
|
|
|
|
try {
|
|
|
|
DisplayInquiry(inq_info, false);
|
|
|
|
}
|
|
|
|
catch(const phase_exception&) {
|
|
|
|
// Continue with next ID if there is no LUN 0
|
|
|
|
if (!target_lun) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScsiDump::DisplayInquiry(ScsiDump::inquiry_info_t& inq_info, bool check_type)
|
|
|
|
{
|
|
|
|
// Assert RST for 1 ms
|
|
|
|
bus->SetRST(true);
|
|
|
|
const timespec ts = {.tv_sec = 0, .tv_nsec = 1000 * 1000};
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
bus->SetRST(false);
|
|
|
|
|
|
|
|
cout << DIVIDER << "\nTarget device is " << target_id << ":" << target_lun << "\n" << flush;
|
|
|
|
|
|
|
|
Inquiry();
|
|
|
|
|
|
|
|
const auto type = static_cast<byte>(buffer[0]);
|
|
|
|
if ((type & byte{0x1f}) == byte{0x1f}) {
|
|
|
|
// Requested LUN is not available
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
array<char, 17> str = {};
|
|
|
|
memcpy(str.data(), &buffer[8], 8);
|
|
|
|
inq_info.vendor = string(str.data());
|
|
|
|
cout << "Vendor: " << inq_info.vendor << "\n";
|
|
|
|
|
|
|
|
str.fill(0);
|
|
|
|
memcpy(str.data(), &buffer[16], 16);
|
|
|
|
inq_info.product = string(str.data());
|
|
|
|
cout << "Product: " << inq_info.product << "\n";
|
|
|
|
|
|
|
|
str.fill(0);
|
|
|
|
memcpy(str.data(), &buffer[32], 4);
|
|
|
|
inq_info.revision = string(str.data());
|
|
|
|
cout << "Revision: " << inq_info.revision << "\n" << flush;
|
|
|
|
|
|
|
|
if (const auto& t = DEVICE_TYPES.find(type & byte{0x1f}); t != DEVICE_TYPES.end()) {
|
|
|
|
cout << "Device Type: " << (*t).second << "\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cout << "Device Type: Unknown\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Removable: " << (((static_cast<byte>(buffer[1]) & byte{0x80}) == byte{0x80}) ? "Yes" : "No") << "\n";
|
|
|
|
|
|
|
|
if (check_type && type != static_cast<byte>(device_type::direct_access) &&
|
|
|
|
type != static_cast<byte>(device_type::cd_rom) && type != static_cast<byte>(device_type::optical_memory)) {
|
|
|
|
cerr << "Invalid device type, supported types for dump/restore are DIRECT ACCESS, CD-ROM/DVD/BD and OPTICAL MEMORY" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
int ScsiDump::DumpRestore()
|
2022-11-09 07:40:26 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
inquiry_info_t inq_info;
|
|
|
|
if (!GetDeviceInfo(inq_info)) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
fstream fs;
|
|
|
|
fs.open(filename, (restore ? ios::in : ios::out) | ios::binary);
|
|
|
|
|
|
|
|
if (fs.fail()) {
|
|
|
|
throw parser_exception("Can't open image file '" + filename + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restore) {
|
|
|
|
cout << "Starting restore\n" << flush;
|
|
|
|
|
|
|
|
off_t size;
|
2023-10-15 06:38:15 +00:00
|
|
|
try {
|
|
|
|
size = file_size(path(filename));
|
|
|
|
}
|
|
|
|
catch (const filesystem_error& e) {
|
|
|
|
throw parser_exception(string("Can't determine file size: ") + e.what());
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << "Restore file size: " << size << " bytes\n";
|
|
|
|
if (size > (off_t)(inq_info.sector_size * inq_info.capacity)) {
|
|
|
|
cout << "WARNING: File size is larger than disk size\n" << flush;
|
|
|
|
} else if (size < (off_t)(inq_info.sector_size * inq_info.capacity)) {
|
|
|
|
throw parser_exception("File size is smaller than disk size");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cout << "Starting dump\n" << flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump by buffer size
|
2023-10-15 06:38:15 +00:00
|
|
|
auto dsiz = static_cast<int>(buffer.size());
|
2022-12-09 15:50:45 +00:00
|
|
|
const int duni = dsiz / inq_info.sector_size;
|
2023-10-15 06:38:15 +00:00
|
|
|
auto dnum = static_cast<int>((inq_info.capacity * inq_info.sector_size) / dsiz);
|
2022-12-09 15:50:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
const auto start_time = chrono::high_resolution_clock::now();
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < dnum; i++) {
|
|
|
|
if (restore) {
|
|
|
|
fs.read((char*)buffer.data(), dsiz);
|
|
|
|
Write10(i * duni, duni, dsiz);
|
|
|
|
} else {
|
|
|
|
Read10(i * duni, duni, dsiz);
|
|
|
|
fs.write((const char*)buffer.data(), dsiz);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.fail()) {
|
|
|
|
throw parser_exception("File I/O failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << ((i + 1) * 100 / dnum) << "%"
|
|
|
|
<< " (" << (i + 1) * duni << "/" << inq_info.capacity << ")\n"
|
|
|
|
<< flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rounding on capacity
|
|
|
|
dnum = inq_info.capacity % duni;
|
|
|
|
dsiz = dnum * inq_info.sector_size;
|
|
|
|
if (dnum > 0) {
|
|
|
|
if (restore) {
|
|
|
|
fs.read((char*)buffer.data(), dsiz);
|
|
|
|
if (!fs.fail()) {
|
|
|
|
Write10(i * duni, dnum, dsiz);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Read10(i * duni, dnum, dsiz);
|
|
|
|
fs.write((const char*)buffer.data(), dsiz);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.fail()) {
|
|
|
|
throw parser_exception("File I/O failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "100% (" << inq_info.capacity << "/" << inq_info.capacity << ")\n" << flush;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
const auto stop_time = chrono::high_resolution_clock::now();
|
2022-12-09 15:50:45 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
const auto duration = chrono::duration_cast<chrono::seconds>(stop_time - start_time).count();
|
2022-12-09 15:50:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
cout << DIVIDER << "\n";
|
2023-10-15 06:38:15 +00:00
|
|
|
cout << "Transfered : " << inq_info.capacity * inq_info.sector_size << " bytes ["
|
|
|
|
<< inq_info.capacity * inq_info.sector_size / 1024 / 1024 << "MiB]\n";
|
|
|
|
cout << "Total time: " << duration << " seconds (" << duration / 60 << " minutes\n";
|
2023-11-01 11:53:05 +00:00
|
|
|
cout << "Average transfer rate: " << (inq_info.capacity * inq_info.sector_size / 8) / duration
|
2023-10-15 06:38:15 +00:00
|
|
|
<< " bytes per second (" << (inq_info.capacity * inq_info.sector_size / 8) / duration / 1024
|
2022-12-09 15:50:45 +00:00
|
|
|
<< " KiB per second)\n";
|
2023-10-30 10:34:07 +00:00
|
|
|
cout << DIVIDER << "\n";
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
if (properties_file && !restore) {
|
2023-10-30 10:34:07 +00:00
|
|
|
inq_info.GeneratePropertiesFile(filename + ".properties");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
bool ScsiDump::GetDeviceInfo(inquiry_info_t& inq_info)
|
2022-11-09 07:40:26 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
DisplayBoardId();
|
2022-12-09 15:50:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
if (!DisplayInquiry(inq_info, true)) {
|
|
|
|
return false;
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TestUnitReady();
|
|
|
|
|
|
|
|
RequestSense();
|
|
|
|
|
|
|
|
const auto [capacity, sector_size] = ReadCapacity();
|
2023-10-15 06:38:15 +00:00
|
|
|
inq_info.capacity = capacity;
|
|
|
|
inq_info.sector_size = sector_size;
|
2022-12-09 15:50:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
cout << "Sectors: " << capacity << "\n"
|
|
|
|
<< "Sector size: " << sector_size << " bytes\n"
|
|
|
|
<< "Capacity: " << sector_size * capacity / 1024 / 1024 << " MiB (" << sector_size * capacity
|
2022-12-09 15:50:45 +00:00
|
|
|
<< " bytes)\n"
|
2023-10-30 10:34:07 +00:00
|
|
|
<< DIVIDER << "\n\n"
|
2022-12-09 15:50:45 +00:00
|
|
|
<< flush;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
return true;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2022-12-09 15:50:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
void ScsiDump::inquiry_info::GeneratePropertiesFile(const string& property_file) const
|
2022-12-09 15:50:45 +00:00
|
|
|
{
|
2023-10-30 10:34:07 +00:00
|
|
|
ofstream prop_stream(property_file);
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
prop_stream << "{" << endl;
|
2023-10-30 10:34:07 +00:00
|
|
|
prop_stream << " \"vendor\": \"" << vendor << "\"," << endl;
|
|
|
|
prop_stream << " \"product\": \"" << product << "\"," << endl;
|
|
|
|
prop_stream << " \"revision\": \"" << revision << "\"," << endl;
|
|
|
|
prop_stream << " \"block_size\": \"" << sector_size << "\"" << endl;
|
2022-12-09 15:50:45 +00:00
|
|
|
prop_stream << "}" << endl;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
if (prop_stream.fail()) {
|
|
|
|
spdlog::warn("Unable to create properties file '" + property_file + "'");
|
2022-12-09 15:50:45 +00:00
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
}
|