2022-02-10 18:54:48 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SCSI Target Emulator RaSCSI (*^..^*)
|
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
// A basic device with mode page support, to be used for subclassing
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "controllers/scsidev_ctrl.h"
|
|
|
|
#include "mode_page_device.h"
|
|
|
|
|
|
|
|
using namespace std;
|
2022-02-13 19:30:02 +00:00
|
|
|
using namespace scsi_defs;
|
2022-02-10 18:54:48 +00:00
|
|
|
|
2022-02-13 19:30:02 +00:00
|
|
|
ModePageDevice::ModePageDevice(const string& id) : PrimaryDevice(id)
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-02-13 19:30:02 +00:00
|
|
|
dispatcher.AddCommand(eCmdModeSense6, "ModeSense6", &ModePageDevice::ModeSense6);
|
|
|
|
dispatcher.AddCommand(eCmdModeSense10, "ModeSense10", &ModePageDevice::ModeSense10);
|
|
|
|
dispatcher.AddCommand(eCmdModeSelect6, "ModeSelect6", &ModePageDevice::ModeSelect6);
|
|
|
|
dispatcher.AddCommand(eCmdModeSelect10, "ModeSelect10", &ModePageDevice::ModeSelect10);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ModePageDevice::Dispatch(SCSIDEV *controller)
|
|
|
|
{
|
2022-02-13 19:30:02 +00:00
|
|
|
// The superclass class handles the less specific commands
|
|
|
|
return dispatcher.Dispatch(this, controller) ? true : super::Dispatch(controller);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
int ModePageDevice::AddModePages(const DWORD *cdb, BYTE *buf, int max_length)
|
|
|
|
{
|
|
|
|
bool changeable = (cdb[2] & 0xc0) == 0x40;
|
|
|
|
|
|
|
|
// Get page code (0x3f means all pages)
|
|
|
|
int page = cdb[2] & 0x3f;
|
|
|
|
|
|
|
|
LOGTRACE("%s Requesting mode page $%02X", __PRETTY_FUNCTION__, page);
|
|
|
|
|
|
|
|
// Mode page data mapped to the respective page numbers, C++ maps are ordered by key
|
|
|
|
map<int, vector<BYTE>> pages;
|
|
|
|
AddModePages(pages, page, changeable);
|
|
|
|
|
|
|
|
// If no mode data were added at all something must be wrong
|
|
|
|
if (pages.empty()) {
|
|
|
|
LOGTRACE("%s Unsupported mode page $%02X", __PRETTY_FUNCTION__, page);
|
|
|
|
SetStatusCode(STATUS_INVALIDCDB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-20 00:34:31 +00:00
|
|
|
// Holds all mode page data
|
|
|
|
vector<BYTE> result;
|
2022-02-27 21:58:01 +00:00
|
|
|
|
|
|
|
vector<BYTE> page0;
|
|
|
|
for (auto const& page : pages) {
|
2022-08-20 00:34:31 +00:00
|
|
|
// The specification mandates that page 0 must be returned after all others
|
|
|
|
if (page.first) {
|
|
|
|
size_t offset = result.size();
|
|
|
|
|
|
|
|
// Page data
|
|
|
|
result.insert(result.end(), page.second.begin(), page.second.end());
|
|
|
|
// Page code, PS bit may already have been set
|
|
|
|
result[offset] |= page.first;
|
|
|
|
// Page payload size
|
|
|
|
result[offset + 1] = page.second.size() - 2;
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-08-20 00:34:31 +00:00
|
|
|
page0 = page.second;
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page 0 must be last
|
|
|
|
if (!page0.empty()) {
|
2022-08-20 00:34:31 +00:00
|
|
|
size_t offset = result.size();
|
|
|
|
|
|
|
|
// Page data
|
|
|
|
result.insert(result.end(), page0.begin(), page0.end());
|
2022-02-27 21:58:01 +00:00
|
|
|
// Page payload size
|
2022-08-20 00:34:31 +00:00
|
|
|
result[offset + 1] = page0.size() - 2;
|
2022-02-27 21:58:01 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 00:34:31 +00:00
|
|
|
// Do not return more than the requested number of bytes
|
|
|
|
size_t size = (size_t)max_length < result.size() ? max_length : result.size();
|
|
|
|
memcpy(buf, result.data(), size);
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:54:48 +00:00
|
|
|
void ModePageDevice::ModeSense6(SASIDEV *controller)
|
|
|
|
{
|
|
|
|
ctrl->length = ModeSense6(ctrl->cmd, ctrl->buffer);
|
|
|
|
if (ctrl->length <= 0) {
|
|
|
|
controller->Error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller->DataIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModePageDevice::ModeSense10(SASIDEV *controller)
|
|
|
|
{
|
2022-02-27 21:58:01 +00:00
|
|
|
ctrl->length = ModeSense10(ctrl->cmd, ctrl->buffer, ctrl->bufsize);
|
2022-02-10 18:54:48 +00:00
|
|
|
if (ctrl->length <= 0) {
|
|
|
|
controller->Error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller->DataIn();
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
bool ModePageDevice::ModeSelect(const DWORD*, const BYTE *, int)
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
2022-02-27 21:58:01 +00:00
|
|
|
// Cannot be set
|
2022-02-10 18:54:48 +00:00
|
|
|
SetStatusCode(STATUS_INVALIDPRM);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModePageDevice::ModeSelect6(SASIDEV *controller)
|
|
|
|
{
|
|
|
|
LOGTRACE("%s Unsupported mode page $%02X", __PRETTY_FUNCTION__, ctrl->buffer[0]);
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
ctrl->length = ModeSelectCheck6();
|
2022-02-10 18:54:48 +00:00
|
|
|
if (ctrl->length <= 0) {
|
|
|
|
controller->Error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller->DataOut();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModePageDevice::ModeSelect10(SASIDEV *controller)
|
|
|
|
{
|
|
|
|
LOGTRACE("%s Unsupported mode page $%02X", __PRETTY_FUNCTION__, ctrl->buffer[0]);
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
ctrl->length = ModeSelectCheck10();
|
2022-02-10 18:54:48 +00:00
|
|
|
if (ctrl->length <= 0) {
|
|
|
|
controller->Error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller->DataOut();
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
int ModePageDevice::ModeSelectCheck(int length)
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
|
|
|
// Error if save parameters are set for other types than of SCHD or SCRM
|
2022-02-27 21:58:01 +00:00
|
|
|
// TODO The assumption above is not correct, and this code should be located elsewhere
|
|
|
|
if (!IsSCSIHD() && (ctrl->cmd[1] & 0x01)) {
|
2022-02-10 18:54:48 +00:00
|
|
|
SetStatusCode(STATUS_INVALIDCDB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
int ModePageDevice::ModeSelectCheck6()
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
|
|
|
// Receive the data specified by the parameter length
|
2022-02-27 21:58:01 +00:00
|
|
|
return ModeSelectCheck(ctrl->cmd[4]);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
int ModePageDevice::ModeSelectCheck10()
|
2022-02-10 18:54:48 +00:00
|
|
|
{
|
|
|
|
// Receive the data specified by the parameter length
|
2022-02-27 21:58:01 +00:00
|
|
|
int length = ctrl->cmd[7];
|
2022-02-10 18:54:48 +00:00
|
|
|
length <<= 8;
|
2022-02-27 21:58:01 +00:00
|
|
|
length |= ctrl->cmd[8];
|
|
|
|
if (length > ctrl->bufsize) {
|
|
|
|
length = ctrl->bufsize;
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 21:58:01 +00:00
|
|
|
return ModeSelectCheck(length);
|
2022-02-10 18:54:48 +00:00
|
|
|
}
|