2022-11-02 22:41:45 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-11-09 07:40:26 +00:00
|
|
|
// SCSI Target Emulator RaSCSI Reloaded
|
|
|
|
// 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
|
|
|
|
// Copyright (C) 2022 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
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/log.h"
|
|
|
|
#include "shared/rasutil.h"
|
|
|
|
#include "shared/rascsi_exceptions.h"
|
|
|
|
#include "shared/rascsi_version.h"
|
2022-11-02 22:41:45 +00:00
|
|
|
#include "hal/gpiobus_factory.h"
|
2022-11-09 07:40:26 +00:00
|
|
|
#include "hal/gpiobus.h"
|
2022-11-02 22:41:45 +00:00
|
|
|
#include "hal/systimer.h"
|
2022-11-09 07:40:26 +00:00
|
|
|
#include "rasdump/rasdump_core.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <csignal>
|
2022-11-10 06:44:06 +00:00
|
|
|
#include <cstddef>
|
2022-11-09 07:40:26 +00:00
|
|
|
#include <unistd.h>
|
2022-11-02 22:41:45 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2022-11-09 07:40:26 +00:00
|
|
|
#include <fstream>
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-11-09 07:40:26 +00:00
|
|
|
using namespace spdlog;
|
|
|
|
using namespace scsi_defs;
|
2022-11-10 06:44:06 +00:00
|
|
|
using namespace ras_util;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::CleanUp()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
if (bus != nullptr) {
|
|
|
|
bus->Cleanup();
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::KillHandler(int)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
CleanUp();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
bool RasDump::Banner(const vector<char *>& args) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-10 06:44:06 +00:00
|
|
|
cout << ras_util::Banner("RaSCSI hard disk dump/restore utility");
|
2022-11-09 07:40:26 +00:00
|
|
|
|
|
|
|
if (args.size() < 2 || string(args[1]) == "-h") {
|
|
|
|
cout << "Usage: " << args[0] << " -t ID[:LUN] [-i BID] -f FILE [-v] [-r] [-s BUFFER_SIZE]\n"
|
|
|
|
<< " ID is the target device ID (0-7).\n"
|
|
|
|
<< " LUN is the optional target device LUN (0-7). Default is 0.\n"
|
|
|
|
<< " BID is the RaSCSI board ID (0-7). Default is 7.\n"
|
|
|
|
<< " FILE is the dump file path.\n"
|
|
|
|
<< " BUFFER_SIZE is the transfer buffer size, at least "
|
|
|
|
<< to_string(MINIMUM_BUFFER_SIZE / 1024) << " KiB. Default is 1 MiB.\n"
|
|
|
|
<< " -v Enable verbose logging.\n"
|
|
|
|
<< " -r Restore instead of dump.\n" << flush;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
bool RasDump::Init() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
// Interrupt handler setting
|
|
|
|
if (signal(SIGINT, KillHandler) == SIG_ERR || signal(SIGHUP, KillHandler) == SIG_ERR ||
|
|
|
|
signal(SIGTERM, KillHandler) == SIG_ERR) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
bus = GPIOBUS_Factory::Create(BUS::mode_e::INITIATOR);
|
|
|
|
|
|
|
|
return bus != nullptr;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::ParseArguments(const vector<char *>& args)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
int buffer_size = DEFAULT_BUFFER_SIZE;
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
opterr = 0;
|
2022-11-10 06:44:06 +00:00
|
|
|
while ((opt = getopt(static_cast<int>(args.size()), args.data(), "i:f:s:t:rv")) != -1) {
|
2022-11-02 22:41:45 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'i':
|
2022-11-10 06:44:06 +00:00
|
|
|
if (!GetAsUnsignedInt(optarg, initiator_id) || initiator_id > 7) {
|
|
|
|
throw parser_exception("Invalid RaSCSI board ID " + to_string(initiator_id) + " (0-7)");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
case 'f':
|
|
|
|
filename = optarg;
|
2022-11-02 22:41:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
case 's':
|
2022-11-10 06:44:06 +00:00
|
|
|
if (!GetAsUnsignedInt(optarg, buffer_size) || buffer_size < MINIMUM_BUFFER_SIZE) {
|
|
|
|
throw parser_exception("Buffer size must be at least " + to_string(MINIMUM_BUFFER_SIZE / 1024) + "KiB");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-11-11 20:08:48 +00:00
|
|
|
case 't': {
|
|
|
|
const string error = ProcessId(optarg, 8, target_id, target_lun);
|
|
|
|
if (!error.empty()) {
|
|
|
|
throw parser_exception(error);
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 07:40:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
set_level(level::debug);
|
2022-11-02 22:41:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
restore = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (target_id == initiator_id) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("Target ID and RaSCSI board ID must not be identical");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (filename.empty()) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("Missing filename");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
buffer = vector<uint8_t>(buffer_size);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::WaitPhase(BUS::phase_t phase) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGDEBUG("Waiting for %s phase", BUS::GetPhaseStrRaw(phase))
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
// Timeout (3000ms)
|
|
|
|
const uint32_t now = SysTimer::GetTimerLow();
|
|
|
|
while ((SysTimer::GetTimerLow() - now) < 3 * 1000 * 1000) {
|
|
|
|
bus->Acquire();
|
|
|
|
if (bus->GetREQ() && bus->GetPhase() == phase) {
|
2022-11-09 07:40:26 +00:00
|
|
|
return;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("Expected " + string(BUS::GetPhaseStrRaw(phase)) + " phase, actual phase is "
|
2022-11-09 07:40:26 +00:00
|
|
|
+ string(BUS::GetPhaseStrRaw(bus->GetPhase())));
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::Selection() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
// Set initiator and target ID
|
2022-11-10 06:44:06 +00:00
|
|
|
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-11-02 22:41:45 +00:00
|
|
|
bus->SetSEL(true);
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
WaitForBusy();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
bus->SetSEL(false);
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::Command(scsi_command cmd, vector<uint8_t>& cdb) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGDEBUG("Executing %s", command_mapping.find(cmd)->second.second)
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Selection();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
WaitPhase(BUS::phase_t::command);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
cdb[0] = static_cast<uint8_t>(cmd);
|
2022-11-10 06:44:06 +00:00
|
|
|
cdb[1] = static_cast<uint8_t>(static_cast<byte>(cdb[1]) | static_cast<byte>(target_lun << 5));
|
2022-11-09 07:40:26 +00:00
|
|
|
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
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception(command_mapping.find(cmd)->second.second + string(" failed"));
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::DataIn(int length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
WaitPhase(BUS::phase_t::datain);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (!bus->ReceiveHandShake(buffer.data(), length)) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("DATA IN failed");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::DataOut(int length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
WaitPhase(BUS::phase_t::dataout);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (!bus->SendHandShake(buffer.data(), length, BUS::SEND_NO_DELAY)) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("DATA OUT failed");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::Status() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
WaitPhase(BUS::phase_t::status);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (array<uint8_t, 256> buf; bus->ReceiveHandShake(buf.data(), 1) != 1) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("STATUS failed");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::MessageIn() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
WaitPhase(BUS::phase_t::msgin);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (array<uint8_t, 256> buf; bus->ReceiveHandShake(buf.data(), 1) != 1) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("MESSAGE IN failed");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::BusFree() const
|
|
|
|
{
|
|
|
|
bus->Reset();
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::TestUnitReady() const
|
|
|
|
{
|
|
|
|
vector<uint8_t> cdb(6);
|
|
|
|
Command(scsi_command::eCmdTestUnitReady, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
BusFree();
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::RequestSense()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
vector<uint8_t> cdb(6);
|
|
|
|
cdb[4] = 0xff;
|
|
|
|
Command(scsi_command::eCmdRequestSense, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
DataIn(256);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
BusFree();
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::Inquiry()
|
|
|
|
{
|
|
|
|
vector<uint8_t> cdb(6);
|
|
|
|
cdb[4] = 0xff;
|
|
|
|
Command(scsi_command::eCmdInquiry, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
DataIn(256);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
pair<uint64_t, uint32_t> RasDump::ReadCapacity()
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +00:00
|
|
|
vector<uint8_t> cdb(10);
|
|
|
|
Command(scsi_command::eCmdReadCapacity10, cdb);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
DataIn(8);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
BusFree();
|
|
|
|
|
2022-11-09 07:40:26 +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-11-09 07:40:26 +00:00
|
|
|
int sector_size_offset = 4;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +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-11-09 07:40:26 +00:00
|
|
|
DataIn(14);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
BusFree();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +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-11-09 07:40:26 +00:00
|
|
|
sector_size_offset = 8;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +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
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
return make_pair(capacity, sector_size);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::Read10(uint32_t bstart, uint32_t blength, uint32_t length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +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-11-09 07:40:26 +00:00
|
|
|
DataIn(length);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
BusFree();
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::Write10(uint32_t bstart, uint32_t blength, uint32_t length)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-11-09 07:40:26 +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-11-09 07:40:26 +00:00
|
|
|
DataOut(length);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
Status();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
MessageIn();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
BusFree();
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void RasDump::WaitForBusy() const
|
|
|
|
{
|
|
|
|
// 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--);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
// Success if the target is busy
|
|
|
|
if(!bus->GetBSY()) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("SELECTION failed");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int RasDump::run(const vector<char *>& args)
|
|
|
|
{
|
|
|
|
if (!Banner(args)) {
|
2022-11-09 07:40:26 +00:00
|
|
|
return EXIT_SUCCESS;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Init()) {
|
2022-11-09 07:40:26 +00:00
|
|
|
cerr << "Error: Initializing. Are you root?" << endl;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
// Probably not root
|
2022-11-09 07:40:26 +00:00
|
|
|
return EPERM;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
try {
|
|
|
|
ParseArguments(args);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
#ifndef USE_SEL_EVENT_ENABLE
|
2022-11-09 07:40:26 +00:00
|
|
|
cerr << "Error: No RaSCSI hardware support" << endl;
|
|
|
|
return EXIT_FAILURE;
|
2022-11-02 22:41:45 +00:00
|
|
|
#endif
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
return DumpRestore();
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2022-11-10 06:44:06 +00:00
|
|
|
catch(const parser_exception& e) {
|
2022-11-09 07:40:26 +00:00
|
|
|
cerr << "Error: " << e.what() << endl;
|
|
|
|
|
|
|
|
CleanUp();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
return EXIT_FAILURE;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
CleanUp();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
int RasDump::DumpRestore()
|
|
|
|
{
|
2022-11-10 06:44:06 +00:00
|
|
|
const auto [capacity, sector_size] = GetDeviceInfo();
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
fstream fs;
|
|
|
|
fs.open(filename, (restore ? ios::in : ios::out) | ios::binary);
|
|
|
|
|
|
|
|
if (fs.fail()) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("Can't open image file '" + filename + "'");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
if (restore) {
|
2022-11-09 07:40:26 +00:00
|
|
|
cout << "Starting restore\n" << flush;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
// filesystem::file_size cannot be used here because gcc < 10.3.0 cannot handle more than 2 GiB
|
|
|
|
off_t size;
|
|
|
|
if (struct stat st; !stat(filename.c_str(), &st)) {
|
|
|
|
size = st.st_size;
|
|
|
|
}
|
|
|
|
else {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("Can't determine file size");
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
cout << "Restore file size: " << size << " bytes\n";
|
|
|
|
if (size > (off_t)(sector_size * capacity)) {
|
|
|
|
cout << "WARNING: File size is larger than disk size\n" << flush;
|
|
|
|
} else if (size < (off_t)(sector_size * capacity)) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("File size is smaller than disk size");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cout << "Starting dump\n" << flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump by buffer size
|
|
|
|
auto dsiz = static_cast<int>(buffer.size());
|
|
|
|
const int duni = dsiz / sector_size;
|
2022-11-10 06:44:06 +00:00
|
|
|
auto dnum = static_cast<int>((capacity * sector_size) / dsiz);
|
2022-11-09 07:40:26 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < dnum; i++) {
|
2022-11-02 22:41:45 +00:00
|
|
|
if (restore) {
|
2022-11-09 07:40:26 +00:00
|
|
|
fs.read((char *)buffer.data(), dsiz);
|
|
|
|
Write10(i * duni, duni, dsiz);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Read10(i * duni, duni, dsiz);
|
|
|
|
fs.write((const char *)buffer.data(), dsiz);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (fs.fail()) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("File I/O failed");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
cout << ((i + 1) * 100 / dnum) << "%" << " (" << ( i + 1) * duni << "/" << capacity << ")\n" << flush;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rounding on capacity
|
2022-11-09 07:40:26 +00:00
|
|
|
dnum = capacity % duni;
|
|
|
|
dsiz = dnum * sector_size;
|
2022-11-02 22:41:45 +00:00
|
|
|
if (dnum > 0) {
|
|
|
|
if (restore) {
|
2022-11-09 07:40:26 +00:00
|
|
|
fs.read((char *)buffer.data(), dsiz);
|
|
|
|
if (!fs.fail()) {
|
|
|
|
Write10(i * duni, dnum, dsiz);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-09 07:40:26 +00:00
|
|
|
else {
|
|
|
|
Read10(i * duni, dnum, dsiz);
|
|
|
|
fs.write((const char *)buffer.data(), dsiz);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.fail()) {
|
2022-11-10 06:44:06 +00:00
|
|
|
throw parser_exception("File I/O failed");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << "100% (" << capacity << "/" << capacity << ")\n" << flush;
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
pair<uint64_t, uint32_t> RasDump::GetDeviceInfo()
|
2022-11-09 07:40:26 +00:00
|
|
|
{
|
2022-11-10 06:44:06 +00:00
|
|
|
// Assert RST for 1 ms
|
|
|
|
bus->SetRST(true);
|
|
|
|
const timespec ts = { .tv_sec = 0, .tv_nsec = 1000 * 1000};
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
bus->SetRST(false);
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
cout << "Target device ID: " << target_id << ", LUN: " << target_lun << "\n";
|
|
|
|
cout << "RaSCSI board ID: " << initiator_id << "\n" << flush;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
Inquiry();
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
// Display INQUIRY information
|
|
|
|
array<char, 17> str = {};
|
|
|
|
memcpy(str.data(), &buffer[8], 8);
|
|
|
|
cout << "Vendor: " << str.data() << "\n";
|
|
|
|
str.fill(0);
|
|
|
|
memcpy(str.data(), &buffer[16], 16);
|
|
|
|
cout << "Product: " << str.data() << "\n";
|
|
|
|
str.fill(0);
|
|
|
|
memcpy(str.data(), &buffer[32], 4);
|
|
|
|
cout << "Revision: " << str.data() << "\n" << flush;
|
|
|
|
|
|
|
|
if (auto type = static_cast<device_type>(buffer[0]);
|
|
|
|
type != device_type::DIRECT_ACCESS && type != device_type::CD_ROM && type != device_type::OPTICAL_MEMORY) {
|
|
|
|
throw parser_exception("Invalid device type, supported types are DIRECT ACCESS, CD-ROM and OPTICAL MEMORY");
|
2022-11-09 07:40:26 +00:00
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-10 06:44:06 +00:00
|
|
|
TestUnitReady();
|
|
|
|
|
|
|
|
RequestSense();
|
|
|
|
|
|
|
|
const auto [capacity, sector_size] = ReadCapacity();
|
|
|
|
|
|
|
|
cout << "Number of sectors: " << capacity << "\n"
|
|
|
|
<< "Sector size: " << sector_size << " bytes\n"
|
|
|
|
<< "Capacity: " << sector_size * capacity / 1024 / 1024 << " MiB ("
|
|
|
|
<< sector_size * capacity << " bytes)\n\n" << flush;
|
|
|
|
|
|
|
|
return make_pair(capacity, sector_size);
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|