2022-11-02 22:41:45 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-11-02 22:41:45 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-11-02 22:41:45 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "hal/bus.h"
|
2022-12-09 15:50:45 +00:00
|
|
|
#include <memory>
|
2022-11-09 07:40:26 +00:00
|
|
|
#include <string>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <span>
|
2022-11-02 22:41:45 +00:00
|
|
|
#include <vector>
|
2023-10-30 10:34:07 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <stdexcept>
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
class phase_exception : public runtime_error
|
|
|
|
{
|
|
|
|
using runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
class ScsiDump
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
public:
|
2023-10-30 10:34:07 +00:00
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
ScsiDump() = default;
|
|
|
|
~ScsiDump() = default;
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
int run(const span<char *>);
|
2022-12-09 15:50:45 +00:00
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
struct inquiry_info {
|
2022-12-09 15:50:45 +00:00
|
|
|
string vendor;
|
|
|
|
string product;
|
|
|
|
string revision;
|
|
|
|
uint32_t sector_size;
|
|
|
|
uint64_t capacity;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
void GeneratePropertiesFile(const string&) const;
|
|
|
|
};
|
|
|
|
using inquiry_info_t = struct inquiry_info;
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
private:
|
2023-10-30 10:34:07 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool Banner(span<char *>) const;
|
2022-12-09 15:50:45 +00:00
|
|
|
bool Init() const;
|
2023-10-15 06:38:15 +00:00
|
|
|
void ParseArguments(span<char *>);
|
2023-10-30 10:34:07 +00:00
|
|
|
void DisplayBoardId() const;
|
|
|
|
void ScanBus();
|
|
|
|
bool DisplayInquiry(inquiry_info_t&, bool);
|
2022-12-09 15:50:45 +00:00
|
|
|
int DumpRestore();
|
2023-10-30 10:34:07 +00:00
|
|
|
bool GetDeviceInfo(inquiry_info_t&);
|
|
|
|
void WaitForPhase(phase_t) const;
|
2022-12-09 15:50:45 +00:00
|
|
|
void Selection() const;
|
|
|
|
void Command(scsi_defs::scsi_command, vector<uint8_t>&) const;
|
|
|
|
void DataIn(int);
|
|
|
|
void DataOut(int);
|
|
|
|
void Status() const;
|
|
|
|
void MessageIn() const;
|
|
|
|
void BusFree() const;
|
|
|
|
void TestUnitReady() const;
|
|
|
|
void RequestSense();
|
|
|
|
void Inquiry();
|
|
|
|
pair<uint64_t, uint32_t> ReadCapacity();
|
|
|
|
void Read10(uint32_t, uint32_t, uint32_t);
|
|
|
|
void Write10(uint32_t, uint32_t, uint32_t);
|
|
|
|
void WaitForBusy() const;
|
|
|
|
|
|
|
|
static void CleanUp();
|
2023-10-30 10:34:07 +00:00
|
|
|
static void TerminationHandler(int);
|
2022-12-09 15:50:45 +00:00
|
|
|
|
|
|
|
// A static instance is needed because of the signal handler
|
|
|
|
static inline unique_ptr<BUS> bus;
|
|
|
|
|
|
|
|
vector<uint8_t> buffer;
|
|
|
|
|
|
|
|
int target_id = -1;
|
|
|
|
|
|
|
|
int target_lun = 0;
|
|
|
|
|
|
|
|
int initiator_id = 7;
|
|
|
|
|
|
|
|
string filename;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
bool inquiry = false;
|
|
|
|
|
|
|
|
bool scan_bus = false;
|
|
|
|
|
2022-12-09 15:50:45 +00:00
|
|
|
bool restore = false;
|
|
|
|
|
|
|
|
bool properties_file = false;
|
|
|
|
|
2023-10-30 10:34:07 +00:00
|
|
|
static const int MINIMUM_BUFFER_SIZE = 1024 * 64;
|
|
|
|
static const int DEFAULT_BUFFER_SIZE = 1024 * 1024;
|
|
|
|
|
|
|
|
static inline const string DIVIDER = "----------------------------------------";
|
|
|
|
|
|
|
|
static inline const unordered_map<byte, string> DEVICE_TYPES = {
|
|
|
|
{ byte{0}, "Direct Access" },
|
|
|
|
{ byte{1}, "Sequential Access" },
|
|
|
|
{ byte{2}, "Printer" },
|
|
|
|
{ byte{3}, "Processor" },
|
|
|
|
{ byte{4}, "Write-Once" },
|
|
|
|
{ byte{5}, "CD-ROM/DVD/BD/DVD-RAM" },
|
|
|
|
{ byte{6}, "Scanner" },
|
|
|
|
{ byte{7}, "Optical Memory" },
|
|
|
|
{ byte{8}, "Media Changer" },
|
|
|
|
{ byte{9}, "Communications" },
|
|
|
|
{ byte{10}, "Graphic Arts Pre-Press" },
|
|
|
|
{ byte{11}, "Graphic Arts Pre-Press" },
|
|
|
|
{ byte{12}, "Storage Array Controller" },
|
|
|
|
{ byte{13}, "Enclosure Services" },
|
|
|
|
{ byte{14}, "Simplified Direct Access" },
|
|
|
|
{ byte{15}, "Optical Card Reader/Writer" },
|
|
|
|
{ byte{16}, "Bridge Controller" },
|
|
|
|
{ byte{17}, "Object-based Storage" },
|
|
|
|
{ byte{18}, "Automation/Drive Interface" },
|
|
|
|
{ byte{19}, "Security Manager" },
|
|
|
|
{ byte{20}, "Host Managed Zoned Block" },
|
|
|
|
{ byte{30}, "Well Known Logical Unit" }
|
|
|
|
};
|
2022-11-02 22:41:45 +00:00
|
|
|
};
|