2022-02-10 18:54:48 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-02-10 18:54:48 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
// A basic device with mode page support, to be used for subclassing
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-10-01 15:56:06 +00:00
|
|
|
#include "scsi_command_util.h"
|
2022-02-10 18:54:48 +00:00
|
|
|
#include "mode_page_device.h"
|
2022-09-25 21:49:24 +00:00
|
|
|
#include <cstddef>
|
2022-11-11 20:08:48 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
2022-02-10 18:54:48 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-02-13 19:30:02 +00:00
|
|
|
using namespace scsi_defs;
|
2022-10-01 15:56:06 +00:00
|
|
|
using namespace scsi_command_util;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
bool ModePageDevice::Init(const unordered_map<string, string>& params)
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-02 14:36:19 +00:00
|
|
|
PrimaryDevice::Init(params);
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
AddCommand(scsi_command::eCmdModeSense6, [this] { ModeSense6(); });
|
|
|
|
AddCommand(scsi_command::eCmdModeSense10, [this] { ModeSense10(); });
|
|
|
|
AddCommand(scsi_command::eCmdModeSelect6, [this] { ModeSelect6(); });
|
|
|
|
AddCommand(scsi_command::eCmdModeSelect10, [this] { ModeSelect10(); });
|
|
|
|
|
|
|
|
return true;
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
int ModePageDevice::AddModePages(const vector<int>& cdb, vector<uint8_t>& buf, int offset, int length, int max_size) const
|
2022-02-27 21:58:01 +00:00
|
|
|
{
|
2022-11-02 14:36:19 +00:00
|
|
|
const int max_length = length - offset;
|
2022-09-08 02:45:00 +00:00
|
|
|
if (max_length < 0) {
|
2022-10-23 19:51:39 +00:00
|
|
|
return length;
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 17:26:04 +00:00
|
|
|
const bool changeable = (cdb[2] & 0xc0) == 0x40;
|
2022-02-27 21:58:01 +00:00
|
|
|
|
|
|
|
// Get page code (0x3f means all pages)
|
2022-10-08 17:26:04 +00:00
|
|
|
const int page = cdb[2] & 0x3f;
|
2022-02-27 21:58:01 +00:00
|
|
|
|
2022-11-11 20:08:48 +00:00
|
|
|
stringstream s;
|
|
|
|
s << "Requesting mode page $" << setfill('0') << setw(2) << hex << page;
|
|
|
|
GetLogger().Trace(s.str());
|
2022-02-27 21:58:01 +00:00
|
|
|
|
|
|
|
// Mode page data mapped to the respective page numbers, C++ maps are ordered by key
|
2022-09-21 06:27:51 +00:00
|
|
|
map<int, vector<byte>> pages;
|
2022-09-25 21:49:24 +00:00
|
|
|
SetUpModePages(pages, page, changeable);
|
2022-02-27 21:58:01 +00:00
|
|
|
|
|
|
|
if (pages.empty()) {
|
2022-11-11 20:08:48 +00:00
|
|
|
s << "Unsupported mode page $" << page;
|
|
|
|
GetLogger().Trace(s.str());
|
2022-10-08 17:26:04 +00:00
|
|
|
throw scsi_exception(sense_key::ILLEGAL_REQUEST, asc::INVALID_FIELD_IN_CDB);
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 00:34:31 +00:00
|
|
|
// Holds all mode page data
|
2022-09-21 06:27:51 +00:00
|
|
|
vector<byte> result;
|
2022-02-27 21:58:01 +00:00
|
|
|
|
2022-09-21 06:27:51 +00:00
|
|
|
vector<byte> page0;
|
2022-11-02 14:36:19 +00:00
|
|
|
for (const auto& [index, data] : pages) {
|
2022-08-20 00:34:31 +00:00
|
|
|
// The specification mandates that page 0 must be returned after all others
|
2022-09-10 05:59:41 +00:00
|
|
|
if (index) {
|
2022-10-08 17:26:04 +00:00
|
|
|
const size_t off = result.size();
|
2022-08-20 00:34:31 +00:00
|
|
|
|
|
|
|
// Page data
|
2022-09-10 05:59:41 +00:00
|
|
|
result.insert(result.end(), data.begin(), data.end());
|
2022-08-20 00:34:31 +00:00
|
|
|
// Page code, PS bit may already have been set
|
2022-10-04 15:23:42 +00:00
|
|
|
result[off] |= (byte)index;
|
2022-08-20 00:34:31 +00:00
|
|
|
// Page payload size
|
2022-10-04 15:23:42 +00:00
|
|
|
result[off + 1] = (byte)(data.size() - 2);
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-09-10 05:59:41 +00:00
|
|
|
page0 = data;
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page 0 must be last
|
|
|
|
if (!page0.empty()) {
|
2022-11-02 14:36:19 +00:00
|
|
|
const size_t off = result.size();
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-08-20 00:34:31 +00:00
|
|
|
// Page data
|
|
|
|
result.insert(result.end(), page0.begin(), page0.end());
|
2022-02-27 21:58:01 +00:00
|
|
|
// Page payload size
|
2022-10-04 15:23:42 +00:00
|
|
|
result[off + 1] = (byte)(page0.size() - 2);
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
if (static_cast<int>(result.size()) > max_size) {
|
2022-10-23 19:51:39 +00:00
|
|
|
throw scsi_exception(sense_key::ILLEGAL_REQUEST, asc::INVALID_FIELD_IN_CDB);
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
const auto size = static_cast<int>(min(static_cast<size_t>(max_length), result.size()));
|
2022-10-01 15:56:06 +00:00
|
|
|
memcpy(&buf.data()[offset], result.data(), size);
|
2022-08-20 00:34:31 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// Do not return more than the requested number of bytes
|
|
|
|
return size + offset < length ? size + offset : length;
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
void ModePageDevice::ModeSense6() const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
GetController()->SetLength(ModeSense6(GetController()->GetCmd(), GetController()->GetBuffer()));
|
2022-02-10 18:54:48 +00:00
|
|
|
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
EnterDataInPhase();
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
void ModePageDevice::ModeSense10() const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
GetController()->SetLength(ModeSense10(GetController()->GetCmd(), GetController()->GetBuffer()));
|
2022-02-10 18:54:48 +00:00
|
|
|
|
SASI code removal, error handling update, bug fixes, code cleanup (#806)
Summary ov most important changes triggered by the SASI code removal:
- Removed the SASI controller code
- New controller management. There is a new controller base class AbstractController and a class ControllerManager managing the controller lifecycle. The lifecycle management was removed from rasci.cpp and is covered by unit tests.
- New device management. The DeviceFactory manages the device lifecycle instead of rascsi.cpp. The new code is covered by unit tests.
- The lifecycle managment uses C++ collections with variable size instead of arrays with hard-coded sizes.
- The ScsiController method contains most of what was previously contained in scsidev_ctrl.cpp plus the code from sasidev_ctrl.cpp that was relevant for SCSI.
- scsi_command_util contains helper methods used for identical SCSI command implementations of more than one device
- Devices know their controllers, so that the controller instance does not need to be passed to each SCSI command. This change helps to decouple the devices from the controller. The phase_handler interface is also part of this decoupling.
- Use scsi_command_exception for propagating SCSI command execution errors, This resolves issues with the previous error handling, which was based on return values and often on magic numbers.
- Removed legacy SCSI error codes, all errors are now encoded by sense_key::, asc:: and status::.
- Fixed various warnings reported with -Wextra, -Weffc++ and -Wpedantic.
- Use constructor member initialization lists (recommended for ISO C++)
- Consistently use new/delete instead of malloc/free (recommended for ISO C++), resulting in better type safety and error handling
- Replaced variable sized arrays on the stack (violates ISO C++ and can cause a stack overflow)
- Replaced NULL by nullptr (recommended for C++), resulting in better type safety
- Use more const member functions in order to avoid side effects
- The format device page can now also be changed for hard disk drives (Fujitsu M2624S supports this, for instance), not just for MOs.
- Better encapsulation, updated access specifiers in many places
- Removed unused methods and method arguments
- Fixed a number of TODOs
- Added/updated unit tests for a lot of non-legacy classes
- Makefile support for creating HTML coverage reports with lcov/genhtml
2022-09-03 14:53:53 +00:00
|
|
|
EnterDataInPhase();
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
void ModePageDevice::ModeSelect(scsi_command, const vector<int>&, const vector<uint8_t>&, int) const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-02 14:36:19 +00:00
|
|
|
// There is no default implementation of MDOE SELECT
|
2022-10-08 17:26:04 +00:00
|
|
|
throw scsi_exception(sense_key::ILLEGAL_REQUEST, asc::INVALID_COMMAND_OPERATION_CODE);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
void ModePageDevice::ModeSelect6() const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
SaveParametersCheck(GetController()->GetCmd(4));
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
void ModePageDevice::ModeSelect10() const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
const auto length = min(GetController()->GetBuffer().size(), static_cast<size_t>(GetInt16(GetController()->GetCmd(), 7)));
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
SaveParametersCheck(static_cast<uint32_t>(length));
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
void ModePageDevice::SaveParametersCheck(int length) const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
if (!SupportsSaveParameters() && (GetController()->GetCmd(1) & 0x01)) {
|
2022-10-08 17:26:04 +00:00
|
|
|
throw scsi_exception(sense_key::ILLEGAL_REQUEST, asc::INVALID_FIELD_IN_CDB);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
GetController()->SetLength(length);
|
2022-11-02 14:36:19 +00:00
|
|
|
|
|
|
|
EnterDataOutPhase();
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|