2022-02-10 18:54:48 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-08-26 01:01:39 +00:00
|
|
|
// SCSI Target Emulator RaSCSI Reloaded
|
2022-02-10 18:54:48 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
2022-02-17 02:04:42 +00:00
|
|
|
// Host Services with realtime clock and shutdown support
|
|
|
|
//
|
2022-02-10 18:54:48 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//
|
|
|
|
// Features of the host services device:
|
|
|
|
//
|
2022-09-21 06:27:51 +00:00
|
|
|
// 1. Vendor-specific mode page 0x20 returns the current date and time, see mode_page_datetime
|
2022-02-10 18:54:48 +00:00
|
|
|
//
|
2022-02-23 03:08:22 +00:00
|
|
|
// 2. START/STOP UNIT shuts down RaSCSI or shuts down/reboots the Raspberry Pi
|
2022-02-10 18:54:48 +00:00
|
|
|
// a) !start && !load (STOP): Shut down RaSCSI
|
|
|
|
// b) !start && load (EJECT): Shut down the Raspberry Pi
|
2022-02-23 03:08:22 +00:00
|
|
|
// c) start && load (LOAD): Reboot the Raspberry Pi
|
2022-02-10 18:54:48 +00:00
|
|
|
//
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "controllers/scsi_controller.h"
|
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
|
|
|
#include "rascsi_exceptions.h"
|
2022-09-25 21:49:24 +00:00
|
|
|
#include "scsi_command_util.h"
|
2022-02-10 18:54:48 +00:00
|
|
|
#include "host_services.h"
|
2022-10-01 15:56:06 +00:00
|
|
|
#include <algorithm>
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-02-13 19:30:02 +00:00
|
|
|
using namespace scsi_defs;
|
2022-09-25 21:49:24 +00:00
|
|
|
using namespace scsi_command_util;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
bool HostServices::Init(const unordered_map<string, string>& params)
|
2022-02-13 19:30:02 +00:00
|
|
|
{
|
2022-11-02 14:36:19 +00:00
|
|
|
ModePageDevice::Init(params);
|
|
|
|
|
|
|
|
AddCommand(scsi_command::eCmdTestUnitReady, [this] { TestUnitReady(); });
|
|
|
|
AddCommand(scsi_command::eCmdStartStop, [this] { StartStopUnit(); });
|
2022-10-11 15:04:17 +00:00
|
|
|
|
|
|
|
SetReady(true);
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
return true;
|
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
|
|
|
void HostServices::TestUnitReady()
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
|
|
|
// Always successful
|
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
|
|
|
EnterStatusPhase();
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 14:36:19 +00:00
|
|
|
vector<uint8_t> HostServices::InquiryInternal() const
|
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
|
|
|
return HandleInquiry(device_type::PROCESSOR, scsi_level::SPC_3, false);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
void HostServices::StartStopUnit() const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
const bool start = GetController()->GetCmd(4) & 0x01;
|
|
|
|
const bool load = GetController()->GetCmd(4) & 0x02;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
|
|
|
if (!start) {
|
|
|
|
if (load) {
|
2022-11-04 07:22:32 +00:00
|
|
|
GetController()->ScheduleShutdown(AbstractController::rascsi_shutdown_mode::STOP_PI);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-11-04 07:22:32 +00:00
|
|
|
GetController()->ScheduleShutdown(AbstractController::rascsi_shutdown_mode::STOP_RASCSI);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
else if (load) {
|
2022-11-04 07:22:32 +00:00
|
|
|
GetController()->ScheduleShutdown(AbstractController::rascsi_shutdown_mode::RESTART_PI);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
2022-02-23 03:08:22 +00:00
|
|
|
else {
|
2022-10-08 17:26:04 +00:00
|
|
|
throw scsi_exception(sense_key::ILLEGAL_REQUEST, asc::INVALID_FIELD_IN_CDB);
|
2022-02-23 03:08:22 +00:00
|
|
|
}
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
EnterStatusPhase();
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
int HostServices::ModeSense6(const vector<int>& cdb, vector<uint8_t>& buf) const
|
2022-02-13 19:30:02 +00:00
|
|
|
{
|
2022-02-27 21:58:01 +00:00
|
|
|
// Block descriptors cannot be returned
|
|
|
|
if (!(cdb[1] & 0x08)) {
|
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-11-02 06:36:25 +00:00
|
|
|
const auto length = static_cast<int>(min(buf.size(), static_cast<size_t>(cdb[4])));
|
2022-10-01 15:56:06 +00:00
|
|
|
fill_n(buf.begin(), length, 0);
|
2022-02-13 19:30:02 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// 4 bytes basic information
|
2022-11-04 07:22:32 +00:00
|
|
|
const int size = AddModePages(cdb, buf, 4, length, 255);
|
2022-02-13 19:30:02 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
buf[0] = (uint8_t)size;
|
2022-02-13 19:30:02 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
int HostServices::ModeSense10(const vector<int>& cdb, vector<uint8_t>& buf) const
|
2022-02-13 19:30:02 +00:00
|
|
|
{
|
2022-02-27 21:58:01 +00:00
|
|
|
// Block descriptors cannot be returned
|
|
|
|
if (!(cdb[1] & 0x08)) {
|
2022-10-08 17:26:04 +00:00
|
|
|
throw scsi_exception(sense_key::ILLEGAL_REQUEST, asc::INVALID_FIELD_IN_CDB);
|
2022-02-13 19:30:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
const auto length = static_cast<int>(min(buf.size(), static_cast<size_t>(GetInt16(cdb, 7))));
|
2022-10-01 15:56:06 +00:00
|
|
|
fill_n(buf.begin(), length, 0);
|
2022-02-13 19:30:02 +00:00
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
// 8 bytes basic information
|
2022-11-04 07:22:32 +00:00
|
|
|
const int size = AddModePages(cdb, buf, 8, length, 65535);
|
2022-02-13 19:30:02 +00:00
|
|
|
|
2022-10-01 15:56:06 +00:00
|
|
|
SetInt16(buf, 0, size);
|
2022-02-13 19:30:02 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-09-25 21:49:24 +00:00
|
|
|
void HostServices::SetUpModePages(map<int, vector<byte>>& pages, int page, bool changeable) const
|
2022-02-27 21:58:01 +00:00
|
|
|
{
|
|
|
|
if (page == 0x20 || page == 0x3f) {
|
|
|
|
AddRealtimeClockPage(pages, changeable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 06:27:51 +00:00
|
|
|
void HostServices::AddRealtimeClockPage(map<int, vector<byte>>& pages, bool changeable) const
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-11-04 07:22:32 +00:00
|
|
|
pages[32] = vector<byte>(10);
|
2022-10-23 19:51:39 +00:00
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
if (!changeable) {
|
2022-10-01 15:56:06 +00:00
|
|
|
time_t t = time(nullptr);
|
2022-09-15 21:01:10 +00:00
|
|
|
tm localtime;
|
|
|
|
localtime_r(&t, &localtime);
|
2022-09-21 06:27:51 +00:00
|
|
|
|
|
|
|
mode_page_datetime datetime;
|
2022-09-25 21:49:24 +00:00
|
|
|
datetime.major_version = 0x01;
|
|
|
|
datetime.minor_version = 0x00;
|
2022-09-21 06:27:51 +00:00
|
|
|
datetime.year = (uint8_t)localtime.tm_year;
|
|
|
|
datetime.month = (uint8_t)localtime.tm_mon;
|
|
|
|
datetime.day = (uint8_t)localtime.tm_mday;
|
|
|
|
datetime.hour = (uint8_t)localtime.tm_hour;
|
|
|
|
datetime.minute = (uint8_t)localtime.tm_min;
|
2022-02-10 18:54:48 +00:00
|
|
|
// Ignore leap second for simplicity
|
2022-09-21 06:27:51 +00:00
|
|
|
datetime.second = (uint8_t)(localtime.tm_sec < 60 ? localtime.tm_sec : 59);
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
memcpy(&pages[32][2], &datetime, sizeof(datetime));
|
2022-03-14 00:45:52 +00:00
|
|
|
}
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|