mirror of
https://github.com/akuker/RASCSI.git
synced 2024-06-01 04:41:38 +00:00
0e8d89e827
* Replace member functions * Fixed TODO * Added TODOs * Added TODOs * Removed duplicate code * Fixed return value * CD-ROM mode pages are provided by the CD-ROM implementation * MO mode pages are provided by the MO implementation * Comment update * Removed duplicate code * Removed more duplicate code * Optimization * Updated mode page size handling * Addec TODO * Started more flexible mode page handling * Map mode pages * Page 0 must be last * Error handling update * Updated size handling * Updated map handling * Use map references * Move superclass call * Added comment * Host services also support mode page 0x3f (all pages) * Updated handling of page 0 * Removed duplicate code * Updated buffer size handling * Code cleanup * Removed duplicate code * Use calloc() * Removed duplicate code * Comment update * Fixed buffer offset * Fixed TODO * Added buffer size check * Comment udpate * Logging update * Updated logging * Avoid potential memory leak * Updated handling of page 0 * Added TODO * Comment update * Fixed error message * Use vector instead of byte array * Optimization * More optimizations * Removed duplicate code * Do not try to add more pages when buffer is full * Updated error message * Comment update, fixed host services message length handling * Code cleanup, optimizations * Updated payload handling for page 0 * Fixed TODO * Updated handling of PS field * Fixed offsets * Updated handling for page 0 * Code cleanup * More cleanup * Updated block descriptor handling * Result of REPORT LUNS must not depend on whether a device is ready or not * Printer uses a dynamically allocated buffer * Use realloc * Updated memory handling * Added assertion * Comment update * Fixed initialization * Reset byte transfer flag * Updated usage of realloc * Reverted some changes * Re-added buffer size check * Renaming * Inquiry for hard disk must also work when drive is not ready * Primary device checks EVPD * Added page code check to Inquiry * Explicitly set response level format * Added comment * Removed useless cast * Fixed inconsistencies in setting the additional length * Logging uodate * Updated logging * Made methods const * Moved code * Added TODO * Added vendor page * Reduced visibility * Code cleanup * Mark override * Removed duplicate cast * Synchronized host services mode page handling with other code * Added TODO * Signature update * Moved code * Removed duplicate code * Fixed TODO * Removed duplicate code * Added buffer size check * Improved buffer size check * Code cleanup * Removed useless assertions * Cleanup * Renaming * Added overrides * Removed unnecessary casts * Cleanup * Added TODO * Removed obsolete memset * Removed duplicate code * Logging update * Logging update * Assertion update * Removed useless comments * Code cleanup * Removed obsolete casts * User super typedef * Updated log messages * Fixed #712 * Updated error handling * Removed useless assertions * Reduced casts to Disk* * Updated sector size list argument * Removed obsolete casts * Removed comment
179 lines
4.3 KiB
C++
179 lines
4.3 KiB
C++
//---------------------------------------------------------------------------
|
|
//
|
|
// 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;
|
|
using namespace scsi_defs;
|
|
|
|
ModePageDevice::ModePageDevice(const string& id) : PrimaryDevice(id)
|
|
{
|
|
dispatcher.AddCommand(eCmdModeSense6, "ModeSense6", &ModePageDevice::ModeSense6);
|
|
dispatcher.AddCommand(eCmdModeSense10, "ModeSense10", &ModePageDevice::ModeSense10);
|
|
dispatcher.AddCommand(eCmdModeSelect6, "ModeSelect6", &ModePageDevice::ModeSelect6);
|
|
dispatcher.AddCommand(eCmdModeSelect10, "ModeSelect10", &ModePageDevice::ModeSelect10);
|
|
}
|
|
|
|
bool ModePageDevice::Dispatch(SCSIDEV *controller)
|
|
{
|
|
// The superclass class handles the less specific commands
|
|
return dispatcher.Dispatch(this, controller) ? true : super::Dispatch(controller);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
int size = 0;
|
|
|
|
vector<BYTE> page0;
|
|
for (auto const& page : pages) {
|
|
if (size + (int)page.second.size() > max_length) {
|
|
LOGWARN("Mode page data size exceeds reserved buffer size");
|
|
|
|
page0.clear();
|
|
|
|
break;
|
|
}
|
|
else {
|
|
// The specification mandates that page 0 must be returned after all others
|
|
if (page.first) {
|
|
// Page data
|
|
memcpy(&buf[size], page.second.data(), page.second.size());
|
|
// Page code, PS bit may already have been set
|
|
buf[size] |= page.first;
|
|
// Page payload size
|
|
buf[size + 1] = page.second.size() - 2;
|
|
|
|
size += page.second.size();
|
|
}
|
|
else {
|
|
page0 = page.second;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Page 0 must be last
|
|
if (!page0.empty()) {
|
|
memcpy(&buf[size], page0.data(), page0.size());
|
|
// Page payload size
|
|
buf[size + 1] = page0.size() - 2;
|
|
size += page0.size();
|
|
}
|
|
|
|
return size;
|
|
}
|
|
|
|
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)
|
|
{
|
|
ctrl->length = ModeSense10(ctrl->cmd, ctrl->buffer, ctrl->bufsize);
|
|
if (ctrl->length <= 0) {
|
|
controller->Error();
|
|
return;
|
|
}
|
|
|
|
controller->DataIn();
|
|
}
|
|
|
|
bool ModePageDevice::ModeSelect(const DWORD*, const BYTE *, int)
|
|
{
|
|
// Cannot be set
|
|
SetStatusCode(STATUS_INVALIDPRM);
|
|
|
|
return false;
|
|
}
|
|
|
|
void ModePageDevice::ModeSelect6(SASIDEV *controller)
|
|
{
|
|
LOGTRACE("%s Unsupported mode page $%02X", __PRETTY_FUNCTION__, ctrl->buffer[0]);
|
|
|
|
ctrl->length = ModeSelectCheck6();
|
|
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]);
|
|
|
|
ctrl->length = ModeSelectCheck10();
|
|
if (ctrl->length <= 0) {
|
|
controller->Error();
|
|
return;
|
|
}
|
|
|
|
controller->DataOut();
|
|
}
|
|
|
|
int ModePageDevice::ModeSelectCheck(int length)
|
|
{
|
|
// Error if save parameters are set for other types than of SCHD or SCRM
|
|
// TODO The assumption above is not correct, and this code should be located elsewhere
|
|
if (!IsSCSIHD() && (ctrl->cmd[1] & 0x01)) {
|
|
SetStatusCode(STATUS_INVALIDCDB);
|
|
return 0;
|
|
}
|
|
|
|
return length;
|
|
}
|
|
|
|
int ModePageDevice::ModeSelectCheck6()
|
|
{
|
|
// Receive the data specified by the parameter length
|
|
return ModeSelectCheck(ctrl->cmd[4]);
|
|
}
|
|
|
|
int ModePageDevice::ModeSelectCheck10()
|
|
{
|
|
// Receive the data specified by the parameter length
|
|
int length = ctrl->cmd[7];
|
|
length <<= 8;
|
|
length |= ctrl->cmd[8];
|
|
if (length > ctrl->bufsize) {
|
|
length = ctrl->bufsize;
|
|
}
|
|
|
|
return ModeSelectCheck(length);
|
|
}
|