2022-10-08 23:51:54 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-11-01 15:18:48 +00:00
|
|
|
Copyright (C) 2018-23 divingkatae and maximum
|
2022-10-08 23:51:54 +00:00
|
|
|
(theweirdo) spatium
|
|
|
|
|
|
|
|
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-10-22 21:41:19 +00:00
|
|
|
/** @file Generic SCSI Hard Disk emulation. */
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2023-11-09 11:30:07 +00:00
|
|
|
#include <core/timermanager.h>
|
2022-10-25 00:10:54 +00:00
|
|
|
#include <devices/common/scsi/scsi.h>
|
2022-12-14 15:14:37 +00:00
|
|
|
#include <devices/common/scsi/scsihd.h>
|
2022-11-10 17:22:44 +00:00
|
|
|
#include <devices/deviceregistry.h>
|
2022-12-14 15:14:37 +00:00
|
|
|
#include <loguru.hpp>
|
2022-10-08 23:51:54 +00:00
|
|
|
#include <machines/machineproperties.h>
|
2022-12-14 15:14:37 +00:00
|
|
|
#include <memaccess.h>
|
2024-03-14 04:17:04 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <cstring>
|
2022-10-22 21:41:19 +00:00
|
|
|
|
2022-10-08 23:51:54 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2023-12-10 16:58:12 +00:00
|
|
|
ScsiHardDisk::ScsiHardDisk(std::string name, int my_id) : ScsiDevice(name, my_id) {
|
2022-10-30 22:38:09 +00:00
|
|
|
}
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2022-10-31 22:48:19 +00:00
|
|
|
void ScsiHardDisk::insert_image(std::string filename) {
|
2022-10-08 23:51:54 +00:00
|
|
|
//We don't want to store everything in memory, but
|
|
|
|
//we want to keep the hard disk available.
|
2023-10-31 09:49:23 +00:00
|
|
|
if (!this->disk_img.open(filename))
|
2023-12-04 21:41:01 +00:00
|
|
|
ABORT_F("%s: could not open image file %s", this->name.c_str(), filename.c_str());
|
2023-09-16 21:17:27 +00:00
|
|
|
|
2023-10-31 09:49:23 +00:00
|
|
|
this->img_size = this->disk_img.size();
|
2024-03-10 07:49:34 +00:00
|
|
|
uint64_t tb = (this->img_size + this->sector_size - 1) / this->sector_size;
|
2023-06-02 08:23:21 +00:00
|
|
|
this->total_blocks = static_cast<int>(tb);
|
|
|
|
if (this->total_blocks < 0 || tb != this->total_blocks) {
|
|
|
|
ABORT_F("ScsiHardDisk: file size is too large");
|
|
|
|
}
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 12:07:20 +00:00
|
|
|
void ScsiHardDisk::process_command() {
|
2022-10-26 16:18:32 +00:00
|
|
|
uint32_t lba = 0;
|
|
|
|
uint16_t transfer_len = 0;
|
|
|
|
uint16_t alloc_len = 0;
|
|
|
|
|
|
|
|
uint8_t page_code = 0;
|
|
|
|
uint8_t subpage_code = 0;
|
|
|
|
|
2022-11-07 20:56:01 +00:00
|
|
|
this->pre_xfer_action = nullptr;
|
|
|
|
this->post_xfer_action = nullptr;
|
|
|
|
|
2022-11-07 11:24:02 +00:00
|
|
|
// assume successful command execution
|
|
|
|
this->status = ScsiStatus::GOOD;
|
2023-11-01 15:18:48 +00:00
|
|
|
this->msg_buf[0] = ScsiMessage::COMMAND_COMPLETE;
|
2022-11-07 11:24:02 +00:00
|
|
|
|
2022-10-27 12:07:20 +00:00
|
|
|
uint8_t* cmd = this->cmd_buf;
|
|
|
|
|
|
|
|
switch (cmd[0]) {
|
2022-10-27 12:19:58 +00:00
|
|
|
case ScsiCommand::TEST_UNIT_READY:
|
|
|
|
test_unit_ready();
|
|
|
|
break;
|
|
|
|
case ScsiCommand::REWIND:
|
2024-03-10 07:37:31 +00:00
|
|
|
this->illegal_command(cmd);
|
2022-10-27 12:19:58 +00:00
|
|
|
break;
|
|
|
|
case ScsiCommand::REQ_SENSE:
|
|
|
|
alloc_len = cmd[4];
|
|
|
|
req_sense(alloc_len);
|
|
|
|
break;
|
2023-11-09 11:30:07 +00:00
|
|
|
case ScsiCommand::FORMAT_UNIT:
|
|
|
|
this->format();
|
|
|
|
break;
|
2022-10-27 12:19:58 +00:00
|
|
|
case ScsiCommand::INQUIRY:
|
2023-08-02 14:53:19 +00:00
|
|
|
this->inquiry();
|
2022-10-27 12:19:58 +00:00
|
|
|
break;
|
2024-03-10 05:44:12 +00:00
|
|
|
case ScsiCommand::READ_BLK_LIMITS:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
2022-10-27 12:19:58 +00:00
|
|
|
case ScsiCommand::READ_6:
|
|
|
|
lba = ((cmd[1] & 0x1F) << 16) + (cmd[2] << 8) + cmd[3];
|
|
|
|
transfer_len = cmd[4];
|
|
|
|
read(lba, transfer_len, 6);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::READ_10:
|
|
|
|
lba = (cmd[2] << 24) + (cmd[3] << 16) + (cmd[4] << 8) + cmd[5];
|
|
|
|
transfer_len = (cmd[7] << 8) + cmd[8];
|
|
|
|
read(lba, transfer_len, 10);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::WRITE_6:
|
|
|
|
lba = ((cmd[1] & 0x1F) << 16) + (cmd[2] << 8) + cmd[3];
|
|
|
|
transfer_len = cmd[4];
|
|
|
|
write(lba, transfer_len, 6);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::WRITE_10:
|
|
|
|
lba = (cmd[2] << 24) + (cmd[3] << 16) + (cmd[4] << 8) + cmd[5];
|
|
|
|
transfer_len = (cmd[7] << 8) + cmd[8];
|
|
|
|
write(lba, transfer_len, 10);
|
2022-11-07 20:56:01 +00:00
|
|
|
this->switch_phase(ScsiPhase::DATA_OUT);
|
2022-10-27 12:19:58 +00:00
|
|
|
break;
|
|
|
|
case ScsiCommand::SEEK_6:
|
2024-03-10 07:37:31 +00:00
|
|
|
this->illegal_command(cmd);
|
2022-10-27 12:19:58 +00:00
|
|
|
break;
|
2024-03-10 05:44:12 +00:00
|
|
|
case ScsiCommand::VERIFY_6:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
2022-10-27 12:19:58 +00:00
|
|
|
case ScsiCommand::MODE_SELECT_6:
|
2023-11-09 11:30:07 +00:00
|
|
|
mode_select_6(cmd[4]);
|
2022-10-27 12:19:58 +00:00
|
|
|
break;
|
2024-03-10 05:44:12 +00:00
|
|
|
case ScsiCommand::RELEASE_UNIT:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::ERASE_6:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
2022-10-27 12:19:58 +00:00
|
|
|
case ScsiCommand::MODE_SENSE_6:
|
2023-08-02 14:53:19 +00:00
|
|
|
this->mode_sense_6();
|
2022-10-27 12:19:58 +00:00
|
|
|
break;
|
2024-03-10 05:44:12 +00:00
|
|
|
case ScsiCommand::START_STOP_UNIT:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::DIAG_RESULTS:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::SEND_DIAGS:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
2022-11-13 23:52:53 +00:00
|
|
|
case ScsiCommand::READ_CAPACITY_10:
|
2023-08-02 14:53:19 +00:00
|
|
|
this->read_capacity_10();
|
2022-10-27 12:19:58 +00:00
|
|
|
break;
|
2024-03-10 05:44:12 +00:00
|
|
|
case ScsiCommand::VERIFY_10:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
2023-11-01 15:18:48 +00:00
|
|
|
case ScsiCommand::READ_BUFFER:
|
|
|
|
read_buffer();
|
|
|
|
break;
|
2024-03-10 05:44:12 +00:00
|
|
|
case ScsiCommand::MODE_SENSE_10:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::READ_12:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// CD-ROM specific commands
|
|
|
|
case ScsiCommand::READ_TOC:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::SET_CD_SPEED:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::READ_CD:
|
|
|
|
this->illegal_command(cmd);
|
|
|
|
break;
|
2022-10-27 12:19:58 +00:00
|
|
|
default:
|
2024-03-10 05:44:12 +00:00
|
|
|
this->illegal_command(cmd);
|
2022-10-26 16:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 20:27:18 +00:00
|
|
|
bool ScsiHardDisk::prepare_data() {
|
|
|
|
switch (this->cur_phase) {
|
|
|
|
case ScsiPhase::DATA_IN:
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_ptr = (uint8_t*)this->data_buf;
|
2023-11-01 15:18:48 +00:00
|
|
|
this->data_size = this->bytes_out;
|
2022-11-02 20:27:18 +00:00
|
|
|
break;
|
2022-11-07 20:56:01 +00:00
|
|
|
case ScsiPhase::DATA_OUT:
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_ptr = (uint8_t*)this->data_buf;
|
2022-11-07 20:56:01 +00:00
|
|
|
this->data_size = 0;
|
|
|
|
break;
|
2022-11-02 20:27:18 +00:00
|
|
|
case ScsiPhase::STATUS:
|
|
|
|
if (!error) {
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[0] = ScsiStatus::GOOD;
|
2022-11-02 20:27:18 +00:00
|
|
|
} else {
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[0] = ScsiStatus::CHECK_CONDITION;
|
2022-11-02 20:27:18 +00:00
|
|
|
}
|
2023-11-01 15:18:48 +00:00
|
|
|
this->data_size = 1;
|
2022-11-02 20:27:18 +00:00
|
|
|
break;
|
|
|
|
case ScsiPhase::MESSAGE_IN:
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[0] = this->msg_code;
|
2023-11-01 15:18:48 +00:00
|
|
|
this->data_size = 1;
|
2022-11-02 20:27:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-12-04 21:41:01 +00:00
|
|
|
LOG_F(WARNING, "%s: unexpected phase in prepare_data", this->name.c_str());
|
2022-11-02 20:27:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-22 18:41:52 +00:00
|
|
|
int ScsiHardDisk::test_unit_ready() {
|
2022-11-07 23:32:45 +00:00
|
|
|
this->switch_phase(ScsiPhase::STATUS);
|
|
|
|
return ScsiError::NO_ERROR;
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:18:32 +00:00
|
|
|
int ScsiHardDisk::req_sense(uint16_t alloc_len) {
|
2022-10-08 23:51:54 +00:00
|
|
|
if (alloc_len != 252) {
|
2023-12-04 21:41:01 +00:00
|
|
|
LOG_F(WARNING, "%s: inappropriate Allocation Length: %d", this->name.c_str(),
|
|
|
|
alloc_len);
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
2022-10-22 18:41:52 +00:00
|
|
|
return ScsiError::NO_ERROR; // placeholder - no sense
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 14:53:19 +00:00
|
|
|
void ScsiHardDisk::inquiry() {
|
|
|
|
int page_num = cmd_buf[2];
|
|
|
|
int alloc_len = cmd_buf[4];
|
|
|
|
|
|
|
|
if (page_num) {
|
2023-12-04 21:41:01 +00:00
|
|
|
ABORT_F("%s: invalid page number in INQUIRY", this->name.c_str());
|
2023-08-02 14:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (alloc_len >= 36) {
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[0] = 0; // device type: Direct-access block device
|
|
|
|
this->data_buf[1] = 0; // non-removable media
|
|
|
|
this->data_buf[2] = 2; // ANSI version: SCSI-2
|
|
|
|
this->data_buf[3] = 1; // response data format
|
|
|
|
this->data_buf[4] = 0; // additional length
|
|
|
|
this->data_buf[5] = 0;
|
|
|
|
this->data_buf[6] = 0;
|
|
|
|
this->data_buf[7] = 0x18; // supports synchronous xfers and linked commands
|
|
|
|
std::memcpy(data_buf + 8, vendor_info, 8);
|
|
|
|
std::memcpy(data_buf + 16, prod_info, 16);
|
|
|
|
std::memcpy(data_buf + 32, rev_info, 4);
|
2023-08-02 14:53:19 +00:00
|
|
|
|
|
|
|
this->bytes_out = 36;
|
|
|
|
|
|
|
|
this->switch_phase(ScsiPhase::DATA_IN);
|
2022-10-27 12:07:20 +00:00
|
|
|
}
|
2022-10-26 16:30:05 +00:00
|
|
|
else {
|
2023-12-04 21:41:01 +00:00
|
|
|
LOG_F(WARNING, "%s: allocation length too small: %d", this->name.c_str(),
|
|
|
|
alloc_len);
|
2022-10-26 16:30:05 +00:00
|
|
|
}
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 18:41:52 +00:00
|
|
|
int ScsiHardDisk::send_diagnostic() {
|
2022-10-08 23:51:54 +00:00
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
2023-11-09 11:30:07 +00:00
|
|
|
void ScsiHardDisk::mode_select_6(uint8_t param_len) {
|
|
|
|
if (!param_len) {
|
|
|
|
this->switch_phase(ScsiPhase::STATUS);
|
|
|
|
return;
|
2022-10-26 16:18:32 +00:00
|
|
|
}
|
2023-11-09 11:30:07 +00:00
|
|
|
|
|
|
|
this->incoming_size = param_len;
|
|
|
|
|
2024-03-10 07:49:34 +00:00
|
|
|
std::memset(&this->data_buf[0], 0xDD, this->sector_size);
|
2023-11-09 11:30:07 +00:00
|
|
|
|
|
|
|
this->post_xfer_action = [this]() {
|
|
|
|
// TODO: parse the received mode parameter list here
|
|
|
|
};
|
|
|
|
|
|
|
|
this->switch_phase(ScsiPhase::DATA_OUT);
|
2022-10-26 16:18:32 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 15:18:48 +00:00
|
|
|
static char Apple_Copyright_Page_Data[] = "APPLE COMPUTER, INC ";
|
|
|
|
|
2023-08-02 14:53:19 +00:00
|
|
|
void ScsiHardDisk::mode_sense_6() {
|
2023-11-01 15:18:48 +00:00
|
|
|
uint8_t page_code = this->cmd_buf[2] & 0x3F;
|
|
|
|
uint8_t page_ctrl = this->cmd_buf[2] >> 6;
|
2023-08-02 14:53:19 +00:00
|
|
|
uint8_t alloc_len = this->cmd_buf[4];
|
2023-11-01 15:18:48 +00:00
|
|
|
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[ 0] = 13; // initial data size
|
|
|
|
this->data_buf[ 1] = 0; // medium type
|
|
|
|
this->data_buf[ 2] = 0; // medium is write enabled
|
|
|
|
this->data_buf[ 3] = 8; // block description length
|
2023-11-01 15:18:48 +00:00
|
|
|
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[ 4] = 0; // density code
|
|
|
|
this->data_buf[ 5] = (this->total_blocks >> 16) & 0xFFU;
|
|
|
|
this->data_buf[ 6] = (this->total_blocks >> 8) & 0xFFU;
|
|
|
|
this->data_buf[ 7] = (this->total_blocks ) & 0xFFU;
|
2024-03-10 07:49:34 +00:00
|
|
|
WRITE_DWORD_BE_A(&this->data_buf[8], this->sector_size);
|
2023-11-01 15:18:48 +00:00
|
|
|
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[12] = page_code;
|
2023-11-01 15:18:48 +00:00
|
|
|
|
|
|
|
switch(page_code) {
|
2023-11-09 11:30:07 +00:00
|
|
|
case 1: // read-write error recovery page
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[13] = 6; // data size - 1
|
|
|
|
std::memset(&this->data_buf[14], 0, 6);
|
2023-11-09 11:30:07 +00:00
|
|
|
break;
|
2023-11-01 15:18:48 +00:00
|
|
|
case 3: // Format device page
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[13] = 22; // data size - 1
|
|
|
|
std::memset(&this->data_buf[14], 0, 22);
|
2023-11-01 15:18:48 +00:00
|
|
|
// default values taken from Empire 540/1080S manual
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[15] = 6; // tracks per defect zone
|
|
|
|
this->data_buf[17] = 1; // alternate sectors per zone
|
|
|
|
this->data_buf[23] = 92; // sectors per track in the outermost zone
|
|
|
|
this->data_buf[27] = 1; // interleave factor
|
|
|
|
this->data_buf[29] = 19; // track skew factor
|
|
|
|
this->data_buf[31] = 25; // cylinder skew factor
|
|
|
|
this->data_buf[32] = 0x80; // SSEC=1, HSEC=0, RMB=0, SURF=0, INS=0
|
|
|
|
WRITE_WORD_BE_U(&this->data_buf[24], 512); // bytes per sector
|
2023-11-01 15:18:48 +00:00
|
|
|
break;
|
|
|
|
case 0x30: // Copyright page for Apple certified drives
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[13] = 22; // data size - 1
|
|
|
|
std::memcpy(&this->data_buf[14], Apple_Copyright_Page_Data, 22);
|
2023-11-01 15:18:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-12-04 21:41:01 +00:00
|
|
|
ABORT_F("%s: unsupported page %d in MODE_SENSE_6", this->name.c_str(), page_code);
|
2023-11-01 15:18:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// adjust for overall mode sense data length
|
2024-03-10 05:17:20 +00:00
|
|
|
this->data_buf[0] += this->data_buf[13] + 1;
|
2023-11-01 15:18:48 +00:00
|
|
|
|
2024-03-10 05:17:20 +00:00
|
|
|
this->bytes_out = std::min(alloc_len, (uint8_t)this->data_buf[0]);
|
2023-11-01 15:18:48 +00:00
|
|
|
|
|
|
|
this->switch_phase(ScsiPhase::DATA_IN);
|
2022-10-22 18:41:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 23:45:58 +00:00
|
|
|
void ScsiHardDisk::read_capacity_10() {
|
2022-12-14 15:14:37 +00:00
|
|
|
uint32_t lba = READ_DWORD_BE_U(&this->cmd_buf[2]);
|
2022-10-27 12:19:58 +00:00
|
|
|
|
2022-12-14 15:14:37 +00:00
|
|
|
if (this->cmd_buf[1] & 1) {
|
2023-12-04 21:41:01 +00:00
|
|
|
ABORT_F("%s: RelAdr bit set in READ_CAPACITY_10", this->name.c_str());
|
2022-12-14 15:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(this->cmd_buf[8] & 1) && lba) {
|
2023-12-04 21:41:01 +00:00
|
|
|
LOG_F(ERROR, "%s: non-zero LBA for PMI=0", this->name.c_str());
|
2022-12-14 15:14:37 +00:00
|
|
|
this->status = ScsiStatus::CHECK_CONDITION;
|
|
|
|
this->sense = ScsiSense::ILLEGAL_REQ;
|
|
|
|
this->switch_phase(ScsiPhase::STATUS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t last_lba = this->total_blocks - 1;
|
2024-03-10 07:49:34 +00:00
|
|
|
uint32_t blk_len = this->sector_size;
|
2022-10-23 23:45:58 +00:00
|
|
|
|
2024-03-10 05:17:20 +00:00
|
|
|
WRITE_DWORD_BE_A(&data_buf[0], last_lba);
|
|
|
|
WRITE_DWORD_BE_A(&data_buf[4], blk_len);
|
2022-10-23 23:45:58 +00:00
|
|
|
|
2023-11-01 15:18:48 +00:00
|
|
|
this->bytes_out = 8;
|
2022-12-14 15:14:37 +00:00
|
|
|
|
|
|
|
this->switch_phase(ScsiPhase::DATA_IN);
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 23:45:58 +00:00
|
|
|
|
2022-10-22 18:41:52 +00:00
|
|
|
void ScsiHardDisk::format() {
|
2023-12-04 21:41:01 +00:00
|
|
|
LOG_F(WARNING, "%s: attempt to format the disk!", this->name.c_str());
|
2023-11-09 11:30:07 +00:00
|
|
|
|
|
|
|
if (this->cmd_buf[1] & 0x10)
|
2023-12-04 21:41:01 +00:00
|
|
|
ABORT_F("%s: defect list isn't supported yet", this->name.c_str());
|
2023-11-09 11:30:07 +00:00
|
|
|
|
|
|
|
TimerManager::get_instance()->add_oneshot_timer(NS_PER_SEC, [this]() {
|
|
|
|
this->switch_phase(ScsiPhase::STATUS);
|
|
|
|
});
|
2022-10-22 18:41:52 +00:00
|
|
|
}
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2022-10-26 16:18:32 +00:00
|
|
|
void ScsiHardDisk::read(uint32_t lba, uint16_t transfer_len, uint8_t cmd_len) {
|
|
|
|
uint32_t transfer_size = transfer_len;
|
|
|
|
|
2024-03-10 05:17:20 +00:00
|
|
|
std::memset(data_buf, 0, sizeof(data_buf));
|
2022-10-27 12:19:58 +00:00
|
|
|
|
2022-11-10 17:22:44 +00:00
|
|
|
if (cmd_len == 6 && transfer_len == 0) {
|
|
|
|
transfer_size = 256;
|
|
|
|
}
|
2022-10-26 16:18:32 +00:00
|
|
|
|
2024-03-10 07:49:34 +00:00
|
|
|
transfer_size *= this->sector_size;
|
2024-03-10 09:52:57 +00:00
|
|
|
uint64_t device_offset = (uint64_t)lba * this->sector_size;
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2024-03-10 05:17:20 +00:00
|
|
|
this->disk_img.read(data_buf, device_offset, transfer_size);
|
2022-10-31 22:17:08 +00:00
|
|
|
|
2023-11-01 15:18:48 +00:00
|
|
|
this->bytes_out = transfer_size;
|
2022-11-07 20:56:01 +00:00
|
|
|
|
|
|
|
this->switch_phase(ScsiPhase::DATA_IN);
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:18:32 +00:00
|
|
|
void ScsiHardDisk::write(uint32_t lba, uint16_t transfer_len, uint8_t cmd_len) {
|
|
|
|
uint32_t transfer_size = transfer_len;
|
|
|
|
|
2022-11-10 17:22:44 +00:00
|
|
|
if (cmd_len == 6 && transfer_len == 0) {
|
|
|
|
transfer_size = 256;
|
|
|
|
}
|
2022-10-26 16:18:32 +00:00
|
|
|
|
2024-03-10 07:49:34 +00:00
|
|
|
transfer_size *= this->sector_size;
|
2024-03-10 09:52:57 +00:00
|
|
|
uint64_t device_offset = (uint64_t)lba * this->sector_size;
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2022-11-07 20:56:01 +00:00
|
|
|
this->incoming_size = transfer_size;
|
|
|
|
|
2023-09-16 21:17:27 +00:00
|
|
|
this->post_xfer_action = [this, device_offset]() {
|
2024-03-10 05:17:20 +00:00
|
|
|
this->disk_img.write(this->data_buf, device_offset, this->incoming_size);
|
2022-11-07 20:56:01 +00:00
|
|
|
};
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 15:18:48 +00:00
|
|
|
void ScsiHardDisk::read_buffer() {
|
|
|
|
uint8_t mode = this->cmd_buf[1];
|
|
|
|
uint32_t alloc_len = (this->cmd_buf[6] << 24) | (this->cmd_buf[7] << 16) |
|
|
|
|
this->cmd_buf[8];
|
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case 0: // Combined header and data mode
|
2024-03-10 05:17:20 +00:00
|
|
|
WRITE_DWORD_BE_A(&this->data_buf[0], 0x10000); // report buffer size of 64K
|
2023-11-01 15:18:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-12-04 21:41:01 +00:00
|
|
|
ABORT_F("%s: unsupported mode %d in READ_BUFFER", this->name.c_str(), mode);
|
2023-11-01 15:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->bytes_out = alloc_len;
|
|
|
|
|
|
|
|
this->switch_phase(ScsiPhase::DATA_IN);
|
|
|
|
}
|
|
|
|
|
2022-10-08 23:51:54 +00:00
|
|
|
static const PropMap SCSI_HD_Properties = {
|
|
|
|
{"hdd_img", new StrProperty("")},
|
|
|
|
{"hdd_wr_prot", new BinProperty(0)},
|
2022-10-25 00:41:58 +00:00
|
|
|
};
|
2022-10-30 22:38:09 +00:00
|
|
|
|
2022-10-31 22:48:19 +00:00
|
|
|
static const DeviceDescription SCSI_HD_Descriptor =
|
2022-10-30 22:38:09 +00:00
|
|
|
{ScsiHardDisk::create, {}, SCSI_HD_Properties};
|
|
|
|
|
2022-10-31 22:48:19 +00:00
|
|
|
REGISTER_DEVICE(ScsiHD, SCSI_HD_Descriptor);
|