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
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2022-2023 Uwe Seimet
|
2022-02-10 18:54:48 +00:00
|
|
|
//
|
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-12-05 17:58:23 +00:00
|
|
|
// 2. START/STOP UNIT shuts down PiSCSI or shuts down/reboots the Raspberry Pi
|
|
|
|
// a) !start && !load (STOP): Shut down PiSCSI
|
2022-02-10 18:54:48 +00:00
|
|
|
// 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-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_exceptions.h"
|
2022-10-04 15:23:42 +00:00
|
|
|
#include "controllers/scsi_controller.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>
|
2023-10-15 06:38:15 +00:00
|
|
|
#include <chrono>
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
using namespace std::chrono;
|
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
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool HostServices::Init(const param_map& 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
|
|
|
{
|
2023-10-15 06:38:15 +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
|
|
|
{
|
2023-10-15 06:38:15 +00:00
|
|
|
const bool start = GetController()->GetCmdByte(4) & 0x01;
|
|
|
|
const bool load = GetController()->GetCmdByte(4) & 0x02;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
|
|
|
if (!start) {
|
|
|
|
if (load) {
|
2022-12-05 17:58:23 +00:00
|
|
|
GetController()->ScheduleShutdown(AbstractController::piscsi_shutdown_mode::STOP_PI);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-12-05 17:58:23 +00:00
|
|
|
GetController()->ScheduleShutdown(AbstractController::piscsi_shutdown_mode::STOP_PISCSI);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
2022-10-04 15:23:42 +00:00
|
|
|
}
|
|
|
|
else if (load) {
|
2022-12-05 17:58:23 +00:00
|
|
|
GetController()->ScheduleShutdown(AbstractController::piscsi_shutdown_mode::RESTART_PI);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
2022-02-23 03:08:22 +00:00
|
|
|
else {
|
2023-10-15 06:38:15 +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
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
int HostServices::ModeSense6(cdb_t 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)) {
|
2023-10-15 06:38:15 +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;
|
|
|
|
}
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
int HostServices::ModeSense10(cdb_t 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)) {
|
2023-10-15 06:38:15 +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) {
|
2023-10-15 06:38:15 +00:00
|
|
|
const auto now = system_clock::now();
|
|
|
|
const time_t t = system_clock::to_time_t(now);
|
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
|
|
|
}
|