--- nono/vm/scsidev.cpp 2026/04/29 17:04:29 1.1 +++ nono/vm/scsidev.cpp 2026/04/29 17:04:32 1.1.1.2 @@ -6,7 +6,8 @@ // SCSI デバイス共通部分 #include "scsidev.h" -#include "configfile.h" +#include "config.h" +#include "mainapp.h" #include "mystring.h" #include #include @@ -44,19 +45,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->Get(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()); } } @@ -149,15 +173,9 @@ class SCSICmd 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; } @@ -204,6 +222,19 @@ class SCSICmdTestUnitReady : public SCSI }; // +// SCSI コマンド 0x01: RezeroUnit (共通) +// +class SCSICmdRezeroUnit : public SCSICmd +{ + typedef SCSICmd inherited; + public: + SCSICmdRezeroUnit(SCSITarget *p) : inherited(p) { } + virtual ~SCSICmdRezeroUnit() { } + + // とりあえず黙って成功するだけでいいか +}; + +// // SCSI コマンド 0x03: RequestSense (共通) // class SCSICmdRequestSense : public SCSICmd @@ -258,8 +289,8 @@ class SCSICmdRead6 : public SCSICmd if (blks == 0) { blks = 256; } - 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, "READ6 LBA=0x%x blks=0x%x", lba, blks); @@ -289,7 +320,7 @@ class SCSICmdWrite6 : public SCSICmd if (blks == 0) { blks = 256; } - recvbytes = blks * hdparent->blocksize; + recvbytes = blks * parent->GetBlocksize(); return SCSI::XferPhase::DataOut; } @@ -297,7 +328,7 @@ class SCSICmdWrite6 : public SCSICmd 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; @@ -368,13 +399,36 @@ 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 +{ + typedef SCSICmd inherited; + public: + SCSICmdModeSense6(SCSITarget *p) : inherited(p) { } + virtual ~SCSICmdModeSense6() { } + + virtual SCSI::XferPhase Command(std::vector& cmdseq) { + uint8 pagecode = (cmdseq[2] & 0x3f); + // 未実装 + parent->putlog(1, "ModeSense(6) PageCode $%02x 未実装", pagecode); + return SCSI::XferPhase::Status; + } + + virtual void BeginStatus() { + buf.clear(); + buf.push_back((uint8)SCSI::StatusByte::CheckCondition); + } +}; + +// // SCSI コマンド 0x25 ReadCapacity (ダイレクトアクセスデバイス) // class SCSICmdReadCapacity : public SCSICmd @@ -395,7 +449,7 @@ class SCSICmdReadCapacity : public SCSIC // 応答データは // +00.L 最終論理ブロックアドレス // +04.L ブロック長 - uint32 blksize = hdparent->blocksize; + uint32 blksize = parent->GetBlocksize(); uint32 lastblk = (hdparent->memlen / blksize) - 1; buf.resize(8); buf[0] = lastblk >> 24; @@ -438,8 +492,8 @@ class SCSICmdRead10 : public SCSICmd blks |= cmdseq[8]; // 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); @@ -664,13 +718,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 +767,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 +783,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; @@ -781,19 +841,22 @@ SCSIHD::Init() 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"); + const ConfigItem& itemimg = gConfig->Get(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; } - const std::string path = gConfig->GetConfigDir() + filename; + const std::string path = gMainApp.GetVMDir() + filename; fd = open(path.c_str(), O_RDWR); if (fd == -1) { warn("%s \"%s\" open failed", myname.c_str(), path.c_str()); @@ -815,7 +878,7 @@ SCSIHD::Init() return false; } - writeprotect = (bool)gConfig->ReadInt(wpkey); + writeprotect = gConfig->Get(wpkey).AsInt(); if (writeprotect) { putmsg(0, "%s: write protected", myname.c_str()); } @@ -851,6 +914,21 @@ SCSIHD::DispatchCmd() } } +// 指定の場所を自由に読み出す +// (エミュレータ特権アクセス) +bool +SCSIHD::PeekImage(void *buf, uint32 start, uint32 len) const +{ + if (mem == NULL) { + return false; + } + if ((uint64)start + (uint64)len > memlen) { + return false; + } + memcpy(buf, mem + start, len); + return true; +} + // HD の inquiry データ /*static*/ std::vector SCSIHD::inquiry_data = { @@ -865,5 +943,5 @@ SCSIHD::inquiry_data = { 'N', 'O', 'N', 'O', ' ', ' ', ' ', ' ', 'S', 'C', 'S', 'I', 'H', 'D', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - 'r', 'e', 'v', '0', + '0', ' ', ' ', ' ', };