2022-10-08 17:26:04 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-08 17:26:04 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2021-2023 Uwe Seimet
|
2022-10-08 17:26:04 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "piscsi_util.h"
|
2022-10-08 17:26:04 +00:00
|
|
|
#include "protobuf_util.h"
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <unistd.h>
|
2022-10-08 17:26:04 +00:00
|
|
|
#include <sstream>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <array>
|
|
|
|
#include <vector>
|
2023-10-22 14:19:40 +00:00
|
|
|
#include <iomanip>
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_util;
|
|
|
|
using namespace piscsi_interface;
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
#define FPRT(fp, ...) fprintf(fp, __VA_ARGS__ )
|
|
|
|
|
|
|
|
void protobuf_util::ParseParameters(PbDeviceDefinition& device, const string& params)
|
|
|
|
{
|
|
|
|
if (params.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
// Old style parameter (filename), for backwards compatibility only
|
2022-10-08 17:26:04 +00:00
|
|
|
if (params.find(KEY_VALUE_SEPARATOR) == string::npos) {
|
2022-10-23 19:51:39 +00:00
|
|
|
SetParam(device, "file", params);
|
2022-10-08 17:26:04 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
for (const auto& p : Split(params, COMPONENT_SEPARATOR)) {
|
|
|
|
if (const auto& param = Split(p, KEY_VALUE_SEPARATOR, 2); param.size() == 2) {
|
|
|
|
SetParam(device, param[0], param[1]);
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
void protobuf_util::SetCommandParams(PbCommand& command, const string& params)
|
2022-10-08 17:26:04 +00:00
|
|
|
{
|
|
|
|
string folder_pattern;
|
|
|
|
string file_pattern;
|
2023-10-30 12:32:45 +00:00
|
|
|
string operations;
|
2023-10-15 06:38:15 +00:00
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
switch (const auto& components = Split(params, COMPONENT_SEPARATOR, 3); components.size()) {
|
|
|
|
case 3:
|
|
|
|
operations = components[2];
|
|
|
|
[[fallthrough]];
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
folder_pattern = components[0];
|
|
|
|
file_pattern = components[1];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
file_pattern = components[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
SetParam(command, "folder_pattern", folder_pattern);
|
|
|
|
SetParam(command, "file_pattern", file_pattern);
|
2023-10-30 12:32:45 +00:00
|
|
|
SetParam(command, "operations", operations);
|
2022-10-08 17:26:04 +00:00
|
|
|
}
|
2022-11-10 06:44:06 +00:00
|
|
|
|
2022-11-11 20:08:48 +00:00
|
|
|
void protobuf_util::SetProductData(PbDeviceDefinition& device, const string& data)
|
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
const auto& components = Split(data, COMPONENT_SEPARATOR, 3);
|
|
|
|
switch (components.size()) {
|
|
|
|
case 3:
|
|
|
|
device.set_revision(components[2]);
|
|
|
|
[[fallthrough]];
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
device.set_product(components[1]);
|
|
|
|
[[fallthrough]];
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
device.set_vendor(components[0]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2022-11-11 20:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string protobuf_util::SetIdAndLun(PbDeviceDefinition& device, const string& value)
|
2022-11-11 20:08:48 +00:00
|
|
|
{
|
|
|
|
int id;
|
|
|
|
int lun;
|
2023-10-15 06:38:15 +00:00
|
|
|
if (const string error = ProcessId(value, id, lun); !error.empty()) {
|
2022-11-11 20:08:48 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
device.set_id(id);
|
2023-10-15 06:38:15 +00:00
|
|
|
device.set_unit(lun != -1 ? lun : 0);
|
2022-11-11 20:08:48 +00:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
string protobuf_util::ListDevices(const vector<PbDevice>& pb_devices)
|
2022-11-10 06:44:06 +00:00
|
|
|
{
|
|
|
|
if (pb_devices.empty()) {
|
|
|
|
return "No devices currently attached.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
ostringstream s;
|
|
|
|
s << "+----+-----+------+-------------------------------------\n"
|
|
|
|
<< "| ID | LUN | TYPE | IMAGE FILE\n"
|
|
|
|
<< "+----+-----+------+-------------------------------------\n";
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
vector<PbDevice> devices = pb_devices;
|
|
|
|
ranges::sort(devices, [](const auto& a, const auto& b) { return a.id() < b.id() || a.unit() < b.unit(); });
|
2022-11-10 06:44:06 +00:00
|
|
|
|
|
|
|
for (const auto& device : devices) {
|
|
|
|
string filename;
|
|
|
|
switch (device.type()) {
|
|
|
|
case SCBR:
|
|
|
|
filename = "X68000 HOST BRIDGE";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCDP:
|
|
|
|
filename = "DaynaPort SCSI/Link";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCHS:
|
|
|
|
filename = "Host Services";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCLP:
|
|
|
|
filename = "SCSI Printer";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
filename = device.file().name();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-22 14:19:40 +00:00
|
|
|
s << "| " << device.id() << " | " << setw(3) << device.unit() << " | " << PbDeviceType_Name(device.type()) << " | "
|
2022-11-10 06:44:06 +00:00
|
|
|
<< (filename.empty() ? "NO MEDIUM" : filename)
|
|
|
|
<< (!device.status().removed() && (device.properties().read_only() || device.status().protected_()) ? " (READ-ONLY)" : "")
|
|
|
|
<< '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
s << "+----+-----+------+-------------------------------------\n";
|
|
|
|
|
|
|
|
return s.str();
|
|
|
|
}
|
2023-10-15 06:38:15 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Serialize/Deserialize protobuf message: Length followed by the actual data.
|
|
|
|
// A little endian platform is assumed.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void protobuf_util::SerializeMessage(int fd, const google::protobuf::Message& message)
|
|
|
|
{
|
|
|
|
const string data = message.SerializeAsString();
|
|
|
|
|
|
|
|
// Write the size of the protobuf data as a header
|
|
|
|
const auto size = static_cast<int32_t>(data.length());
|
|
|
|
if (write(fd, &size, sizeof(size)) != sizeof(size)) {
|
|
|
|
throw io_exception("Can't write protobuf message size");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the actual protobuf data
|
|
|
|
if (write(fd, data.data(), size) != size) {
|
|
|
|
throw io_exception("Can't write protobuf message data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void protobuf_util::DeserializeMessage(int fd, google::protobuf::Message& message)
|
|
|
|
{
|
|
|
|
// Read the header with the size of the protobuf data
|
|
|
|
array<byte, sizeof(int32_t)> header_buf;
|
|
|
|
if (ReadBytes(fd, header_buf) < header_buf.size()) {
|
|
|
|
throw io_exception("Can't read protobuf message size");
|
|
|
|
}
|
|
|
|
|
|
|
|
const int size = (static_cast<int>(header_buf[3]) << 24) + (static_cast<int>(header_buf[2]) << 16)
|
|
|
|
+ (static_cast<int>(header_buf[1]) << 8) + static_cast<int>(header_buf[0]);
|
|
|
|
if (size < 0) {
|
|
|
|
throw io_exception("Invalid protobuf message size");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the binary protobuf data
|
|
|
|
vector<byte> data_buf(size);
|
|
|
|
if (ReadBytes(fd, data_buf) != data_buf.size()) {
|
|
|
|
throw io_exception("Invalid protobuf message data");
|
|
|
|
}
|
|
|
|
|
|
|
|
message.ParseFromArray(data_buf.data(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t protobuf_util::ReadBytes(int fd, span<byte> buf)
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
while (offset < buf.size()) {
|
|
|
|
const auto len = read(fd, &buf.data()[offset], buf.size() - offset);
|
|
|
|
if (len == -1) {
|
|
|
|
throw io_exception("Read error: " + string(strerror(errno)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|