mirror of
https://github.com/akuker/RASCSI.git
synced 2025-01-10 17:30:47 +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
229 lines
5.6 KiB
C++
229 lines
5.6 KiB
C++
//---------------------------------------------------------------------------
|
||
//
|
||
// SCSI Target Emulator RaSCSI (*^..^*)
|
||
// for Raspberry Pi
|
||
//
|
||
// Copyright (C) 2001-2006 PI.(ytanaka@ipc-tokai.or.jp)
|
||
// Copyright (C) 2014-2020 GIMONS
|
||
// Copyright (C) akuker
|
||
//
|
||
// Licensed under the BSD 3-Clause License.
|
||
// See LICENSE file in the project root folder.
|
||
//
|
||
// [ SCSI hard disk ]
|
||
//
|
||
//---------------------------------------------------------------------------
|
||
#include "scsihd.h"
|
||
#include "fileio.h"
|
||
#include "exceptions.h"
|
||
#include <sstream>
|
||
|
||
#define DEFAULT_PRODUCT "SCSI HD"
|
||
|
||
//===========================================================================
|
||
//
|
||
// SCSI Hard Disk
|
||
//
|
||
//===========================================================================
|
||
|
||
SCSIHD::SCSIHD(const set<uint32_t>& sector_sizes, bool removable) : Disk(removable ? "SCRM" : "SCHD")
|
||
{
|
||
SetSectorSizes(sector_sizes);
|
||
}
|
||
|
||
void SCSIHD::FinalizeSetup(const Filepath &path, off_t size)
|
||
{
|
||
// 2TB is the current maximum
|
||
if (size > 2LL * 1024 * 1024 * 1024 * 1024) {
|
||
throw io_exception("File size must not exceed 2 TiB");
|
||
}
|
||
|
||
// For non-removable media drives set the default product name based on the drive capacity
|
||
if (!IsRemovable()) {
|
||
uint64_t capacity = GetBlockCount() * GetSectorSizeInBytes();
|
||
string unit;
|
||
if (capacity >= 1000000) {
|
||
capacity /= 1000000;
|
||
unit = "MB";
|
||
}
|
||
else {
|
||
capacity /= 1000;
|
||
unit = "KB";
|
||
}
|
||
stringstream product;
|
||
product << DEFAULT_PRODUCT << " " << capacity << " " << unit;
|
||
SetProduct(product.str(), false);
|
||
}
|
||
|
||
SetReadOnly(false);
|
||
SetProtectable(true);
|
||
SetProtected(false);
|
||
|
||
Disk::Open(path);
|
||
FileSupport::SetPath(path);
|
||
}
|
||
|
||
void SCSIHD::Reset()
|
||
{
|
||
// Unlock and release attention
|
||
SetLocked(false);
|
||
SetAttn(false);
|
||
|
||
// No reset, clear code
|
||
SetReset(false);
|
||
SetStatusCode(STATUS_NOERROR);
|
||
}
|
||
|
||
void SCSIHD::Open(const Filepath& path)
|
||
{
|
||
assert(!IsReady());
|
||
|
||
// Open as read-only
|
||
Fileio fio;
|
||
if (!fio.Open(path, Fileio::ReadOnly)) {
|
||
throw file_not_found_exception("Can't open SCSI hard disk file");
|
||
}
|
||
|
||
// Get file size
|
||
off_t size = fio.GetFileSize();
|
||
fio.Close();
|
||
|
||
// Sector size (default 512 bytes) and number of blocks
|
||
SetSectorSizeInBytes(GetConfiguredSectorSize() ? GetConfiguredSectorSize() : 512, false);
|
||
SetBlockCount((DWORD)(size >> GetSectorSizeShiftCount()));
|
||
|
||
// Effective size must be a multiple of the sector size
|
||
size = (size / GetSectorSizeInBytes()) * GetSectorSizeInBytes();
|
||
|
||
FinalizeSetup(path, size);
|
||
}
|
||
|
||
int SCSIHD::Inquiry(const DWORD *cdb, BYTE *buf)
|
||
{
|
||
// EVPD check
|
||
if (cdb[1] & 0x01) {
|
||
SetStatusCode(STATUS_INVALIDCDB);
|
||
return 0;
|
||
}
|
||
|
||
// Basic data
|
||
// buf[0] ... Direct Access Device
|
||
// buf[1] ... Bit 7 set means removable
|
||
// buf[2] ... SCSI-2 compliant command system
|
||
// buf[3] ... SCSI-2 compliant Inquiry response
|
||
// buf[4] ... Inquiry additional data
|
||
memset(buf, 0, 8);
|
||
buf[1] = IsRemovable() ? 0x80 : 0x00;
|
||
buf[2] = 0x02;
|
||
buf[3] = 0x02;
|
||
buf[4] = 0x1F;
|
||
|
||
// Padded vendor, product, revision
|
||
memcpy(&buf[8], GetPaddedName().c_str(), 28);
|
||
|
||
// Size of data that can be returned
|
||
int size = (buf[4] + 5);
|
||
|
||
// Limit if the other buffer is small
|
||
if (size > (int)cdb[4]) {
|
||
size = (int)cdb[4];
|
||
}
|
||
|
||
return size;
|
||
}
|
||
|
||
bool SCSIHD::ModeSelect(const DWORD *cdb, const BYTE *buf, int length)
|
||
{
|
||
assert(length >= 0);
|
||
|
||
int size;
|
||
|
||
// PF
|
||
if (cdb[1] & 0x10) {
|
||
// Mode Parameter header
|
||
if (length >= 12) {
|
||
// Check the block length bytes
|
||
size = 1 << GetSectorSizeShiftCount();
|
||
if (buf[9] != (BYTE)(size >> 16) ||
|
||
buf[10] != (BYTE)(size >> 8) ||
|
||
buf[11] != (BYTE)size) {
|
||
// currently does not allow changing sector length
|
||
SetStatusCode(STATUS_INVALIDPRM);
|
||
return false;
|
||
}
|
||
buf += 12;
|
||
length -= 12;
|
||
}
|
||
|
||
// Parsing the page
|
||
while (length > 0) {
|
||
// Get page
|
||
BYTE page = buf[0];
|
||
|
||
switch (page) {
|
||
// format device
|
||
case 0x03:
|
||
// check the number of bytes in the physical sector
|
||
size = 1 << GetSectorSizeShiftCount();
|
||
if (buf[0xc] != (BYTE)(size >> 8) ||
|
||
buf[0xd] != (BYTE)size) {
|
||
// currently does not allow changing sector length
|
||
SetStatusCode(STATUS_INVALIDPRM);
|
||
return false;
|
||
}
|
||
break;
|
||
|
||
// CD-ROM Parameters
|
||
// TODO Move to scsicd.cpp
|
||
// According to the SONY CDU-541 manual, Page code 8 is supposed
|
||
// to set the Logical Block Adress Format, as well as the
|
||
// inactivity timer multiplier
|
||
case 0x08:
|
||
// Debug code for Issue #2:
|
||
// https://github.com/akuker/RASCSI/issues/2
|
||
LOGWARN("[Unhandled page code] Received mode page code 8 with total length %d\n ", length);
|
||
for (int i = 0; i<length; i++)
|
||
{
|
||
printf("%02X ", buf[i]);
|
||
}
|
||
printf("\n");
|
||
break;
|
||
// Other page
|
||
default:
|
||
printf("Unknown Mode Select page code received: %02X\n",page);
|
||
break;
|
||
}
|
||
|
||
// Advance to the next page
|
||
size = buf[1] + 2;
|
||
length -= size;
|
||
buf += size;
|
||
}
|
||
}
|
||
|
||
// Do not generate an error for the time being (MINIX)
|
||
return true;
|
||
}
|
||
|
||
//---------------------------------------------------------------------------
|
||
//
|
||
// Add Vendor special page to make drive Apple compatible
|
||
//
|
||
//---------------------------------------------------------------------------
|
||
void SCSIHD::AddVendorPage(map<int, vector<BYTE>>& pages, int page, bool changeable) const
|
||
{
|
||
// Page code 48
|
||
if (page != 0x30 && page != 0x3f) {
|
||
return;
|
||
}
|
||
|
||
vector<BYTE> buf(30);
|
||
|
||
// No changeable area
|
||
if (!changeable) {
|
||
memcpy(&buf.data()[0xa], "APPLE COMPUTER, INC.", 20);
|
||
}
|
||
|
||
pages[48] = buf;
|
||
}
|