--- nono/vm/scsidev.cpp 2026/04/29 17:04:29 1.1.1.1 +++ nono/vm/scsidev.cpp 2026/04/29 17:04:39 1.1.1.4 @@ -1,16 +1,15 @@ // // nono -// Copyright (C) 2020 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // // SCSI デバイス共通部分 #include "scsidev.h" -#include "configfile.h" +#include "config.h" +#include "mainapp.h" #include "mystring.h" -#include -#include -#include // // SCSI デバイス @@ -44,19 +43,42 @@ SCSIHostDevice::~SCSIHostDevice() { } +// 設定ファイルに使われるこのホストデバイスを示す接頭語を設定する +// (継承クラス側のコンストラクタで config_name を設定後に呼ぶこと) +void +SCSIHostDevice::SetConfigName(const char *name_) +{ + config_name = name_; +} + bool SCSIHostDevice::Create() { // 設定を読み込む。 for (int id = 0; id < 8; id++) { - const std::string key = string_format("%s_id%d_devtype", + const std::string key = string_format("%s-id%d-image", GetConfigName(), id); - int devtype = gConfig->ReadInt(key); - switch (devtype) { - case SCSI::HD: + const ConfigItem& item = gConfig->Find(key); + const std::string& image = item.AsString(); + + // 空ならデバイスなし + if (image.length() == 0) { + continue; + } + + // ',' より前がデバイス種別 (ここではパスは不要) + auto pos = image.find(','); + if (pos == std::string::npos) { + item.Err("no device type specified"); + return false; + } + std::string type = string_tolower(image.substr(0, pos)); + + if (type == "hd") { target[id].reset(new SCSIHD(this, id)); scsibus->Attach(target[id].get(), id); - break; + } else { + item.Err("invaild device type '%s'", type.c_str()); } } @@ -74,9 +96,9 @@ SCSIHostDevice::Init() std::string typestr; if (target[id]) { switch (target[id]->GetDevType()) { - case SCSI::None: + case SCSI::DevType::None: break; - case SCSI::HD: + case SCSI::DevType::HD: typestr = "HD"; break; } @@ -144,28 +166,26 @@ class SCSICmd // 以下いずれも戻り値は遷移先のフェーズ。 // コマンドフェーズを処理する。 - // コマンドが確定したところで呼ばれる。遷移先フェーズを返すこと。 + // コマンドが確定したところで呼ばれる。戻り値に遷移先フェーズを返すこと。 + // また、コマンド開始時に各自で ClearSense() を呼ぶこと。センスというか + // CA 状態は次のコマンド実行でクリアされるが (ネクサスはとりあえず無視)、 + // RequestSense の場合は現在保持しているセンス情報をホストに返すので、 + // つまり RequestSense 以外のコマンドが全員各自でクリアする必要がある。 virtual SCSI::XferPhase Command(std::vector& cmdseq) { + parent->ClearSense(); return SCSI::XferPhase::Status; } - // データインフェーズを開始する。 - virtual void BeginDataIn() { } - // データインフェーズを完了する。 virtual SCSI::XferPhase DoneDataIn() { return SCSI::XferPhase::Status; } - // データアウトフェーズを開始する。 - virtual void BeginDataOut() { } - // データアウトフェーズを完了する。 virtual SCSI::XferPhase DoneDataOut() { return SCSI::XferPhase::Status; } // ステータスフェーズを開始する。 - // デフォルトでは Good を返す実装を用意しておく。 - virtual void BeginStatus() { + void BeginStatus() { buf.clear(); - buf.push_back((uint8)SCSI::StatusByte::Good); + buf.push_back(status_byte); } // ステータスフェーズを完了する。 @@ -190,6 +210,9 @@ class SCSICmd protected: // このコマンドを実行しているターゲットデバイス。 SCSITarget *parent; + + // ステータスバイト (デフォルトは 0 = Good) + uint8 status_byte {}; }; // @@ -197,39 +220,68 @@ class SCSICmd // class SCSICmdTestUnitReady : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdTestUnitReady(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdTestUnitReady() { } }; // +// SCSI コマンド 0x01: RezeroUnit (共通) +// +class SCSICmdRezeroUnit : public SCSICmd +{ + using inherited = SCSICmd; + public: + SCSICmdRezeroUnit(SCSITarget *p) : inherited(p) { } + virtual ~SCSICmdRezeroUnit() { } + + // とりあえず黙って成功するだけでいいか +}; + +// // SCSI コマンド 0x03: RequestSense (共通) // class SCSICmdRequestSense : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdRequestSense(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdRequestSense() { } virtual SCSI::XferPhase Command(std::vector& cmdseq) { - // この内容は全く不正。でっちあげである。 - // まず 8バイトのバッファに応答を作成 + // 応答のセンスデータは SCSI-2 形式(最小18バイト) を実装。 + // 今の所、センスキー、ASC、ASCQ だけで返せる内容にだけ対応している。 + // これ以外のフィールドを返す必要が出来たらまた考えること。 + buf.clear(); - buf.resize(8); - buf[0] = 0xf0; // Valid, エラークラス7, エラーコード0 - buf[1] = 0; - buf[2] = 0; // No Sense - buf[3] = 0; - buf[4] = 0; - buf[5] = 0; - buf[6] = 0; - buf[7] = 0; // 追加センスデータ長 0 + buf.resize(17); + buf[0] = 0xf0; // Valid、エラーコード 0x70 + buf[1] = 0; // セグメント番号(?) + buf[2] = parent->sensekey; // センスキー (上位4ビットは今の所 0) + buf[3] = 0; // Information + buf[4] = 0; // Information + buf[5] = 0; // Information + buf[6] = 0; // Information + buf[7] = 0; // 追加センスデータ長 0 + buf[8] = 0; // Command specific Information + buf[9] = 0; // Command specific Information + buf[10] = 0; // Command specific Information + buf[11] = 0; // Command specific Information + buf[12] = parent->senseasc >> 8; + buf[13] = parent->senseasc & 0xff; + buf[14] = 0; // FRU + buf[15] = 0; // センスキー固有情報 + buf[16] = 0; // センスキー固有情報 + buf[17] = 0; // センスキー固有情報 + // 追加センスデータなし // それからバッファを指定の長さに変更 buf.resize((uint)cmdseq[4]); + // 転送した後にセンスデータをクリア + parent->ClearSense(); + return SCSI::XferPhase::DataIn; } }; @@ -239,7 +291,7 @@ class SCSICmdRequestSense : public SCSIC // class SCSICmdRead6 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdRead6(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdRead6() { } @@ -249,20 +301,32 @@ class SCSICmdRead6 : public SCSICmd uint32 lba; uint32 blks; + // センスデータをクリア + parent->ClearSense(); + lba = (cmdseq[1] & 0x1f) << 16; lba |= cmdseq[2] << 8; lba |= cmdseq[3]; blks = cmdseq[4]; - // blks == 0 は 256 ブロック転送を意味する。 if (blks == 0) { blks = 256; } - uint8 *start = hdparent->mem + lba * hdparent->blocksize; - uint8 *end = start + blks * hdparent->blocksize; + parent->putlog(1, "READ6 LBA=0x%x blks=0x%x", lba, blks); + + if (((uint64)lba + (uint64)blks) * parent->GetBlocksize() > + hdparent->GetSize()) + { + parent->sensekey = SCSI::SenseKey::IllegalRequest; + parent->senseasc = SCSI::ASC::InvalidFieldInCDB; + status_byte = SCSI::StatusByte::CheckCondition; + return SCSI::XferPhase::Status; + } + + uint8 *start = hdparent->mem + lba * parent->GetBlocksize(); + uint8 *end = start + blks * parent->GetBlocksize(); buf.assign(start, end); - parent->putlog(1, "READ6 LBA=0x%x blks=0x%x", lba, blks); return SCSI::XferPhase::DataIn; } }; @@ -272,7 +336,7 @@ class SCSICmdRead6 : public SCSICmd // class SCSICmdWrite6 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdWrite6(SCSITarget *p) : inherited(p) { hdparent = dynamic_cast(parent); @@ -280,24 +344,35 @@ class SCSICmdWrite6 : public SCSICmd virtual ~SCSICmdWrite6() { } virtual SCSI::XferPhase Command(std::vector& cmdseq) { + // センスデータをクリア + parent->ClearSense(); + lba = (cmdseq[1] & 0x1f) << 16; lba |= cmdseq[2] << 8; lba |= cmdseq[3]; blks = cmdseq[4]; - // blks == 0 は 256 ブロック転送を意味する。 if (blks == 0) { blks = 256; } - recvbytes = blks * hdparent->blocksize; + if (((uint64)lba + (uint64)blks) * parent->GetBlocksize() > + hdparent->GetSize()) + { + parent->sensekey = SCSI::SenseKey::IllegalRequest; + parent->senseasc = SCSI::ASC::InvalidFieldInCDB; + status_byte = SCSI::StatusByte::CheckCondition; + return SCSI::XferPhase::Status; + } + + recvbytes = blks * parent->GetBlocksize(); return SCSI::XferPhase::DataOut; } virtual SCSI::XferPhase DoneDataOut() { parent->putlog(1, "WRITE6 LBA=0x%x blks=0x%x", lba, blks); if (hdparent->writeprotect == false) { - memcpy(hdparent->mem + (lba * hdparent->blocksize), + memcpy(hdparent->mem + (lba * parent->GetBlocksize()), buf.data(), buf.size()); } return SCSI::XferPhase::Status; @@ -314,12 +389,15 @@ class SCSICmdWrite6 : public SCSICmd // class SCSICmdInquiry : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdInquiry(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdInquiry() { } virtual SCSI::XferPhase Command(std::vector& cmdseq) { + // センスデータをクリア + parent->ClearSense(); + // 応答データはターゲットデバイスが持っている buf = parent->inquiry_data; @@ -337,12 +415,15 @@ class SCSICmdInquiry : public SCSICmd // class SCSICmdModeSelect6 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdModeSelect6(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdModeSelect6() { } virtual SCSI::XferPhase Command(std::vector& cmdseq) { + // センスデータをクリア + parent->ClearSense(); + recvbytes = cmdseq[4]; return SCSI::XferPhase::DataOut; } @@ -368,18 +449,64 @@ class SCSICmdModeSelect6 : public SCSICm blocksize |= (desc->block_size[2]); // ブロック長を書き戻す - parent->blocksize = blocksize; + parent->SetBlocksize(blocksize); } return SCSI::XferPhase::Status; } }; // +// SCSI コマンド 0x1a ModeSense(6) (共通) +// +class SCSICmdModeSense6 : public SCSICmd +{ + using inherited = SCSICmd; + public: + SCSICmdModeSense6(SCSITarget *p) : inherited(p) { } + virtual ~SCSICmdModeSense6() { } + + virtual SCSI::XferPhase Command(std::vector& cmdseq) { + uint8 pagecode = (cmdseq[2] & 0x3f); + + // センスデータをクリア + parent->ClearSense(); + + // 未実装 (XXX センスデータは適当) + parent->putlog(1, "ModeSense(6) PageCode $%02x 未実装", pagecode); + parent->sensekey = SCSI::SenseKey::IllegalRequest; + parent->senseasc = SCSI::ASC::InvalidFieldInCDB; + status_byte = SCSI::StatusByte::CheckCondition; + + return SCSI::XferPhase::Status; + } +}; + +// +// SCSI コマンド 0x1b StartStopUnit (ダイレクトアクセスデバイス) +// +class SCSICmdStartStopUnit : public SCSICmd +{ + using inherited = SCSICmd; + public: + SCSICmdStartStopUnit(SCSITarget *p) : inherited(p) { } + virtual ~SCSICmdStartStopUnit() { } + + // スピンアップ/ダウンやメディアのロード/アンロードを指示する。 + // ので今の所エミュレーション上は特にすることはない。 + // XXX それっぽい時間待つくらいはしてもいいかも知れない + + virtual SCSI::XferPhase Command(std::vector& cmdseq) { + parent->putlog(1, "StartStopUnit"); + return inherited::Command(cmdseq); + } +}; + +// // SCSI コマンド 0x25 ReadCapacity (ダイレクトアクセスデバイス) // class SCSICmdReadCapacity : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdReadCapacity(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdReadCapacity() { } @@ -387,6 +514,9 @@ class SCSICmdReadCapacity : public SCSIC virtual SCSI::XferPhase Command(std::vector& cmdseq) { SCSIHD *hdparent = dynamic_cast(parent); + // センスデータをクリア + parent->ClearSense(); + // PMI=1 は未サポート if ((cmdseq[8] & 0x01) != 0) { parent->putmsg("SCSI ReadCapacity PMI=1 not supported"); @@ -395,8 +525,8 @@ class SCSICmdReadCapacity : public SCSIC // 応答データは // +00.L 最終論理ブロックアドレス // +04.L ブロック長 - uint32 blksize = hdparent->blocksize; - uint32 lastblk = (hdparent->memlen / blksize) - 1; + uint32 blksize = parent->GetBlocksize(); + uint32 lastblk = (hdparent->GetSize() / blksize) - 1; buf.resize(8); buf[0] = lastblk >> 24; buf[1] = lastblk >> 16; @@ -416,7 +546,7 @@ class SCSICmdReadCapacity : public SCSIC // class SCSICmdRead10 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdRead10(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdRead10() { } @@ -426,6 +556,9 @@ class SCSICmdRead10 : public SCSICmd uint32 lba; uint32 blks; + // センスデータをクリア + parent->ClearSense(); + if ((cmdseq[1] & 0x01)) { parent->putmsg("SCSI Read(10) RelAdr not supported"); } @@ -436,13 +569,21 @@ class SCSICmdRead10 : public SCSICmd lba |= cmdseq[5]; blks = cmdseq[7] << 8; blks |= cmdseq[8]; + parent->putlog(1, "READ10 LBA=0x%x blks=0x%x", lba, blks); + + if (((uint64)lba + (uint64)blks) * parent->GetBlocksize() > + hdparent->GetSize()) + { + parent->sensekey = SCSI::SenseKey::IllegalRequest; + parent->senseasc = SCSI::ASC::InvalidFieldInCDB; + status_byte = SCSI::StatusByte::CheckCondition; + return SCSI::XferPhase::Status; + } - // XXX 範囲チェック - uint8 *start = hdparent->mem + lba * hdparent->blocksize; - uint8 *end = start + blks * hdparent->blocksize; + uint8 *start = hdparent->mem + lba * parent->GetBlocksize(); + uint8 *end = start + blks * parent->GetBlocksize(); buf.assign(start, end); - parent->putlog(1, "READ10 LBA=0x%x blks=0x%x", lba, blks); return SCSI::XferPhase::DataIn; } }; @@ -452,16 +593,17 @@ class SCSICmdRead10 : public SCSICmd // class SCSICmdNotSupportedCommand : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdNotSupportedCommand(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdNotSupportedCommand() { } - // Command は何もせずステータスに移るだけなので基底のやつでいい - - virtual void BeginStatus() { - buf.clear(); - buf.push_back((uint8)SCSI::StatusByte::CheckCondition); + virtual SCSI::XferPhase Command(std::vector& cmdseq) { + // XXX センスキーよく分からんけどとりあえず + parent->sensekey = SCSI::SenseKey::IllegalRequest; + parent->senseasc = SCSI::ASC::InvalidCommandOperationCode; + status_byte = SCSI::StatusByte::CheckCondition; + return SCSI::XferPhase::Status; } }; @@ -487,6 +629,14 @@ SCSITarget::~SCSITarget() { } +// バスリセットされた (SCSIBus から呼ばれる) +void +SCSITarget::BusResetCB() +{ + // センスキーをクリア + ClearSense(); +} + // セレクションで選択された (SCSIBus から呼ばれる) void SCSITarget::SelectionCB() @@ -664,13 +814,11 @@ SCSITarget::TransferAckCB() // 悩ましい。 switch (xfer) { case SCSI::XferPhase::DataOut: - cmd->BeginDataOut(); cmd->buf.clear(); bytecount = cmd->recvbytes; putlog(3, "DataOut begin (%d bytes)", bytecount); break; case SCSI::XferPhase::DataIn: - cmd->BeginDataIn(); bytecount = 0; putlog(3, "DataIn begin (%d bytes)", (int)cmd->buf.size()); break; @@ -715,6 +863,10 @@ SCSITarget::DispatchCmd() cmd.reset(new SCSICmdTestUnitReady(this)); break; + case SCSI::Command::RezeroUnit: // 0x01 + cmd.reset(new SCSICmdRezeroUnit(this)); + break; + case SCSI::Command::RequestSense: // 0x03 cmd.reset(new SCSICmdRequestSense(this)); break; @@ -727,6 +879,10 @@ SCSITarget::DispatchCmd() cmd.reset(new SCSICmdModeSelect6(this)); break; + case SCSI::Command::ModeSense6: // 0x1a + cmd.reset(new SCSICmdModeSense6(this)); + break; + case SCSI::Command::ReadCapacity: // 0x25 cmd.reset(new SCSICmdTestUnitReady(this)); break; @@ -755,67 +911,45 @@ SCSIHD::SCSIHD(SCSIHostDevice *h, int id : inherited(h, id, inquiry_data) { devname = "SCSIHD"; - devtype = SCSI::HD; - fd = -1; - mem = NULL; + devtype = SCSI::DevType::HD; } // デストラクタ SCSIHD::~SCSIHD() { - if (mem) { - munmap(mem, memlen); - mem = NULL; - } - if (fd >= 0) { - close(fd); - fd = -1; - } } // 初期化 bool SCSIHD::Init() { - struct stat st; - const std::string myname = string_format("%s:ID%d", host->GetConfigName(), myid); - const std::string keybody = string_format("%s_id%d_", + const std::string keybody = string_format("%s-id%d-", host->GetConfigName(), myid); const std::string imgkey = keybody + "image"; const std::string wpkey = keybody + "writeprotect"; - // devtype があって image がないのは異常状態なのでエラー - const std::string& filename = gConfig->ReadStr(imgkey); - if (filename == "") { - gConfig->Err(imgkey, "not specified"); - return false; - } - - const std::string path = gConfig->GetConfigDir() + filename; - fd = open(path.c_str(), O_RDWR); - if (fd == -1) { - warn("%s \"%s\" open failed", myname.c_str(), path.c_str()); - return false; - } - - if (fstat(fd, &st) == -1) { - warn("%s \"%s\" fstat failed", myname.c_str(), path.c_str()); - close(fd); + const ConfigItem& itemimg = gConfig->Find(imgkey); + const std::string& image = itemimg.AsString(); + // ここに来る時にチェックしているので ',' はあるはず + auto pos = image.find(',') + 1; + std::string filename = string_ltrim(image.substr(pos, image.size() - pos)); + if (filename.length() == 0) { + itemimg.Err("imagefile not specified"); return false; } - memlen = st.st_size; - mem = (uint8 *)mmap(NULL, memlen, PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0); - if (mem == MAP_FAILED) { - warn("%s \"%s\" mmap failed", myname.c_str(), path.c_str()); - close(fd); + // ファイルをオープン + const std::string path = gMainApp.GetVMDir() + filename; + file.SetFilename(path); + file.SetDispname(string_format("%s \"%s\"", myname.c_str(), path.c_str())); + mem = file.OpenFile(); + if (mem == NULL) { return false; } - writeprotect = (bool)gConfig->ReadInt(wpkey); + writeprotect = gConfig->Find(wpkey).AsInt(); if (writeprotect) { putmsg(0, "%s: write protected", myname.c_str()); } @@ -836,6 +970,10 @@ SCSIHD::DispatchCmd() cmd.reset(new SCSICmdWrite6(this)); break; + case SCSI::Command::StartStopUnit: // 0x1b + cmd.reset(new SCSICmdStartStopUnit(this)); + break; + case SCSI::Command::ReadCapacity: // 0x25 cmd.reset(new SCSICmdReadCapacity(this)); break; @@ -851,6 +989,21 @@ SCSIHD::DispatchCmd() } } +// 指定の場所を自由に読み出す +// (エミュレータ特権アクセス) +bool +SCSIHD::PeekImage(void *buf, uint32 start, uint32 len) const +{ + if (mem == NULL) { + return false; + } + if ((uint64)start + (uint64)len > GetSize()) { + return false; + } + memcpy(buf, mem + start, len); + return true; +} + // HD の inquiry データ /*static*/ std::vector SCSIHD::inquiry_data = { @@ -865,5 +1018,5 @@ SCSIHD::inquiry_data = { 'N', 'O', 'N', 'O', ' ', ' ', ' ', ' ', 'S', 'C', 'S', 'I', 'H', 'D', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - 'r', 'e', 'v', '0', + '0', ' ', ' ', ' ', };