2022-10-08 23:51:54 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
|
|
|
Copyright (C) 2018-22 divingkatae and maximum
|
|
|
|
(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
|
|
|
|
2022-10-25 00:10:54 +00:00
|
|
|
#include <devices/common/scsi/scsi.h>
|
2022-10-08 23:51:54 +00:00
|
|
|
#include <devices/common/scsi/scsi_hd.h>
|
2022-11-10 17:22:44 +00:00
|
|
|
#include <devices/deviceregistry.h>
|
2022-10-08 23:51:54 +00:00
|
|
|
#include <machines/machinebase.h>
|
|
|
|
#include <machines/machineproperties.h>
|
2022-10-22 21:41:19 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
2022-10-27 12:19:58 +00:00
|
|
|
#include <cstring>
|
2022-10-22 21:41:19 +00:00
|
|
|
#include <stdio.h>
|
2022-11-07 11:15:08 +00:00
|
|
|
#include <sys/stat.h>
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2022-11-10 17:22:44 +00:00
|
|
|
#define HDD_SECTOR_SIZE 512
|
2022-10-08 23:51:54 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2022-10-30 22:38:09 +00:00
|
|
|
ScsiHardDisk::ScsiHardDisk(int my_id) : ScsiDevice(my_id) {
|
2022-10-08 23:51:54 +00:00
|
|
|
supports_types(HWCompType::SCSI_DEV);
|
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.
|
2022-10-31 22:48:19 +00:00
|
|
|
this->hdd_img.open(filename, ios::out | ios::in | ios::binary);
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2022-11-07 11:15:08 +00:00
|
|
|
struct stat stat_buf;
|
|
|
|
int rc = stat(filename.c_str(), &stat_buf);
|
|
|
|
if (!rc) {
|
|
|
|
this->img_size = stat_buf.st_size;
|
|
|
|
} else {
|
|
|
|
ABORT_F("ScsiHardDisk: could not determine file size using stat()");
|
|
|
|
}
|
2022-10-08 23:51:54 +00:00
|
|
|
this->hdd_img.seekg(0, std::ios_base::beg);
|
|
|
|
}
|
|
|
|
|
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 param_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;
|
|
|
|
|
2022-10-27 12:07:20 +00:00
|
|
|
uint8_t* cmd = this->cmd_buf;
|
|
|
|
|
2022-11-07 23:32:45 +00:00
|
|
|
if (cmd[0] != 0 && cmd[0] != 8 && cmd[0] != 0xA && cmd[0] != 0x28 && cmd[0] != 0x2A) {
|
2022-11-07 20:56:01 +00:00
|
|
|
ABORT_F("SCSI-HD: untested command 0x%X", cmd[0]);
|
|
|
|
}
|
2022-11-07 11:24:02 +00:00
|
|
|
|
2022-10-27 12:07:20 +00:00
|
|
|
switch (cmd[0]) {
|
2022-10-27 12:19:58 +00:00
|
|
|
case ScsiCommand::TEST_UNIT_READY:
|
|
|
|
test_unit_ready();
|
|
|
|
break;
|
|
|
|
case ScsiCommand::REWIND:
|
|
|
|
rewind();
|
|
|
|
break;
|
|
|
|
case ScsiCommand::REQ_SENSE:
|
|
|
|
alloc_len = cmd[4];
|
|
|
|
req_sense(alloc_len);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::INQUIRY:
|
|
|
|
alloc_len = (cmd[3] << 8) + cmd[4];
|
|
|
|
inquiry(alloc_len);
|
|
|
|
break;
|
|
|
|
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:
|
|
|
|
lba = ((cmd[1] & 0x1F) << 16) + (cmd[2] << 8) + cmd[3];
|
|
|
|
seek(lba);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::MODE_SELECT_6:
|
|
|
|
param_len = cmd[4];
|
|
|
|
mode_select_6(param_len);
|
|
|
|
break;
|
|
|
|
case ScsiCommand::MODE_SENSE_6:
|
|
|
|
page_code = cmd[2] & 0x1F;
|
|
|
|
subpage_code = cmd[3];
|
|
|
|
alloc_len = cmd[4];
|
|
|
|
mode_sense_6(page_code, subpage_code, alloc_len);
|
|
|
|
break;
|
2022-11-13 23:52:53 +00:00
|
|
|
case ScsiCommand::READ_CAPACITY_10:
|
2022-10-27 12:19:58 +00:00
|
|
|
read_capacity_10();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_F(WARNING, "SCSI_HD: unrecognized command: %x", cmd[0]);
|
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:
|
2022-11-07 11:24:02 +00:00
|
|
|
this->data_ptr = (uint8_t*)this->img_buffer;
|
|
|
|
this->data_size = this->cur_buf_cnt;
|
2022-11-02 20:27:18 +00:00
|
|
|
break;
|
2022-11-07 20:56:01 +00:00
|
|
|
case ScsiPhase::DATA_OUT:
|
|
|
|
this->data_ptr = (uint8_t*)this->img_buffer;
|
|
|
|
this->data_size = 0;
|
|
|
|
break;
|
2022-11-02 20:27:18 +00:00
|
|
|
case ScsiPhase::STATUS:
|
|
|
|
if (!error) {
|
|
|
|
this->img_buffer[0] = ScsiStatus::GOOD;
|
|
|
|
} else {
|
|
|
|
this->img_buffer[0] = ScsiStatus::CHECK_CONDITION;
|
|
|
|
}
|
|
|
|
this->cur_buf_cnt = 1;
|
|
|
|
break;
|
|
|
|
case ScsiPhase::MESSAGE_IN:
|
|
|
|
this->img_buffer[0] = this->msg_code;
|
|
|
|
this->cur_buf_cnt = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_F(WARNING, "SCSI_HD: unexpected phase in prepare_data");
|
|
|
|
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) {
|
2022-10-23 23:45:58 +00:00
|
|
|
LOG_F(WARNING, "Inappropriate Allocation Length: %d", 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
|
|
|
}
|
|
|
|
|
2022-10-26 16:30:05 +00:00
|
|
|
void ScsiHardDisk::inquiry(uint16_t alloc_len) {
|
|
|
|
if (alloc_len >= 48) {
|
|
|
|
uint8_t empty_filler[1 << 17] = {0x0};
|
2022-10-27 12:19:58 +00:00
|
|
|
std::memcpy(img_buffer, empty_filler, (1 << 17));
|
2022-10-26 16:30:05 +00:00
|
|
|
img_buffer[2] = 0x1;
|
|
|
|
img_buffer[3] = 0x2;
|
|
|
|
img_buffer[4] = 0x31;
|
|
|
|
img_buffer[7] = 0x1C;
|
2022-10-27 12:19:58 +00:00
|
|
|
std::memcpy(img_buffer + 8, vendor_info, 8);
|
|
|
|
std::memcpy(img_buffer + 16, prod_info, 16);
|
|
|
|
std::memcpy(img_buffer + 32, rev_info, 8);
|
|
|
|
std::memcpy(img_buffer + 40, serial_info, 8);
|
2022-10-27 12:07:20 +00:00
|
|
|
}
|
2022-10-26 16:30:05 +00:00
|
|
|
else {
|
|
|
|
LOG_F(WARNING, "Inappropriate Allocation Length: %d", alloc_len);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:18:32 +00:00
|
|
|
int ScsiHardDisk::mode_select_6(uint8_t param_len) {
|
|
|
|
if (param_len == 0) {
|
|
|
|
return 0x0;
|
2022-10-27 12:07:20 +00:00
|
|
|
}
|
2022-10-26 16:18:32 +00:00
|
|
|
else {
|
|
|
|
LOG_F(WARNING, "Mode Select calling for param length of: %d", param_len);
|
|
|
|
return param_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiHardDisk::mode_sense_6(uint8_t page_code, uint8_t subpage_code, uint8_t alloc_len) {
|
|
|
|
LOG_F(WARNING, "Page Code %d; Subpage Code: %d", page_code, subpage_code);
|
2022-10-22 18:41:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 23:45:58 +00:00
|
|
|
void ScsiHardDisk::read_capacity_10() {
|
2022-10-26 16:18:32 +00:00
|
|
|
uint32_t sec_limit = (this->img_size >> 9);
|
2022-10-27 12:19:58 +00:00
|
|
|
|
|
|
|
std::memset(img_buffer, 0, sizeof(img_buffer));
|
2022-10-23 23:45:58 +00:00
|
|
|
|
|
|
|
img_buffer[0] = (sec_limit >> 24) & 0xFF;
|
|
|
|
img_buffer[1] = (sec_limit >> 16) & 0xFF;
|
|
|
|
img_buffer[2] = (sec_limit >> 8) & 0xFF;
|
|
|
|
img_buffer[3] = sec_limit & 0xFF;
|
|
|
|
|
|
|
|
img_buffer[6] = 2;
|
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() {
|
|
|
|
}
|
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;
|
|
|
|
|
2022-10-27 12:19:58 +00:00
|
|
|
std::memset(img_buffer, 0, sizeof(img_buffer));
|
|
|
|
|
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
|
|
|
|
2022-11-10 17:22:44 +00:00
|
|
|
transfer_size *= HDD_SECTOR_SIZE;
|
|
|
|
uint64_t device_offset = lba * HDD_SECTOR_SIZE;
|
2022-10-08 23:51:54 +00:00
|
|
|
|
|
|
|
this->hdd_img.seekg(device_offset, this->hdd_img.beg);
|
|
|
|
this->hdd_img.read(img_buffer, transfer_size);
|
2022-10-31 22:17:08 +00:00
|
|
|
|
|
|
|
this->cur_buf_cnt = transfer_size;
|
2022-11-07 11:24:02 +00:00
|
|
|
this->msg_buf[0] = ScsiMessage::COMMAND_COMPLETE;
|
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
|
|
|
|
2022-11-10 17:22:44 +00:00
|
|
|
transfer_size *= HDD_SECTOR_SIZE;
|
|
|
|
uint64_t device_offset = lba * HDD_SECTOR_SIZE;
|
2022-10-08 23:51:54 +00:00
|
|
|
|
2022-11-07 20:56:01 +00:00
|
|
|
this->incoming_size = transfer_size;
|
|
|
|
|
2022-10-08 23:51:54 +00:00
|
|
|
this->hdd_img.seekg(device_offset, this->hdd_img.beg);
|
2022-11-07 20:56:01 +00:00
|
|
|
|
|
|
|
this->post_xfer_action = [this]() {
|
|
|
|
this->hdd_img.write(this->img_buffer, this->incoming_size);
|
|
|
|
};
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 18:41:52 +00:00
|
|
|
void ScsiHardDisk::seek(uint32_t lba) {
|
2022-11-10 17:22:44 +00:00
|
|
|
uint64_t device_offset = lba * HDD_SECTOR_SIZE;
|
2022-10-26 16:18:32 +00:00
|
|
|
this->hdd_img.seekg(device_offset, this->hdd_img.beg);
|
2022-10-08 23:51:54 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 18:41:52 +00:00
|
|
|
void ScsiHardDisk::rewind() {
|
2022-10-08 23:51:54 +00:00
|
|
|
this->hdd_img.seekg(0, this->hdd_img.beg);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|