--- nono/vm/scsidev.cpp 2026/04/29 17:04:32 1.1.1.2 +++ nono/vm/scsidev.cpp 2026/04/29 17:04:36 1.1.1.3 @@ -1,6 +1,7 @@ // // nono -// Copyright (C) 2020 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // // SCSI デバイス共通部分 @@ -9,9 +10,6 @@ #include "config.h" #include "mainapp.h" #include "mystring.h" -#include -#include -#include // // SCSI デバイス @@ -60,7 +58,7 @@ SCSIHostDevice::Create() for (int id = 0; id < 8; id++) { const std::string key = string_format("%s-id%d-image", GetConfigName(), id); - const ConfigItem& item = gConfig->Get(key); + const ConfigItem& item = gConfig->Find(key); const std::string& image = item.AsString(); // 空ならデバイスなし @@ -215,7 +213,7 @@ class SCSICmd // class SCSICmdTestUnitReady : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdTestUnitReady(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdTestUnitReady() { } @@ -226,7 +224,7 @@ class SCSICmdTestUnitReady : public SCSI // class SCSICmdRezeroUnit : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdRezeroUnit(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdRezeroUnit() { } @@ -239,7 +237,7 @@ class SCSICmdRezeroUnit : public SCSICmd // class SCSICmdRequestSense : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdRequestSense(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdRequestSense() { } @@ -270,7 +268,7 @@ class SCSICmdRequestSense : public SCSIC // class SCSICmdRead6 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdRead6(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdRead6() { } @@ -303,7 +301,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); @@ -345,7 +343,7 @@ class SCSICmdWrite6 : public SCSICmd // class SCSICmdInquiry : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdInquiry(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdInquiry() { } @@ -368,7 +366,7 @@ class SCSICmdInquiry : public SCSICmd // class SCSICmdModeSelect6 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdModeSelect6(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdModeSelect6() { } @@ -410,7 +408,7 @@ class SCSICmdModeSelect6 : public SCSICm // class SCSICmdModeSense6 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdModeSense6(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdModeSense6() { } @@ -433,7 +431,7 @@ class SCSICmdModeSense6 : public SCSICmd // class SCSICmdReadCapacity : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdReadCapacity(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdReadCapacity() { } @@ -450,7 +448,7 @@ class SCSICmdReadCapacity : public SCSIC // +00.L 最終論理ブロックアドレス // +04.L ブロック長 uint32 blksize = parent->GetBlocksize(); - uint32 lastblk = (hdparent->memlen / blksize) - 1; + uint32 lastblk = (hdparent->GetSize() / blksize) - 1; buf.resize(8); buf[0] = lastblk >> 24; buf[1] = lastblk >> 16; @@ -470,7 +468,7 @@ class SCSICmdReadCapacity : public SCSIC // class SCSICmdRead10 : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdRead10(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdRead10() { } @@ -506,7 +504,7 @@ class SCSICmdRead10 : public SCSICmd // class SCSICmdNotSupportedCommand : public SCSICmd { - typedef SCSICmd inherited; + using inherited = SCSICmd; public: SCSICmdNotSupportedCommand(SCSITarget *p) : inherited(p) { } virtual ~SCSICmdNotSupportedCommand() { } @@ -816,29 +814,17 @@ SCSIHD::SCSIHD(SCSIHostDevice *h, int id { devname = "SCSIHD"; devtype = SCSI::HD; - fd = -1; - mem = NULL; } // デストラクタ 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-", @@ -846,7 +832,7 @@ SCSIHD::Init() const std::string imgkey = keybody + "image"; const std::string wpkey = keybody + "writeprotect"; - const ConfigItem& itemimg = gConfig->Get(imgkey); + const ConfigItem& itemimg = gConfig->Find(imgkey); const std::string& image = itemimg.AsString(); // ここに来る時にチェックしているので ',' はあるはず auto pos = image.find(',') + 1; @@ -856,29 +842,16 @@ SCSIHD::Init() return false; } + // ファイルをオープン 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()); - return false; - } - - if (fstat(fd, &st) == -1) { - warn("%s \"%s\" fstat failed", myname.c_str(), path.c_str()); - close(fd); - 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); + file.SetFilename(path); + file.SetDispname(string_format("%s \"%s\"", myname.c_str(), path.c_str())); + mem = file.OpenFile(); + if (mem == NULL) { return false; } - writeprotect = gConfig->Get(wpkey).AsInt(); + writeprotect = gConfig->Find(wpkey).AsInt(); if (writeprotect) { putmsg(0, "%s: write protected", myname.c_str()); } @@ -922,7 +895,7 @@ SCSIHD::PeekImage(void *buf, uint32 star if (mem == NULL) { return false; } - if ((uint64)start + (uint64)len > memlen) { + if ((uint64)start + (uint64)len > GetSize()) { return false; } memcpy(buf, mem + start, len);