mirror of
https://github.com/dingusdev/dingusppc.git
synced 2026-12-01 16:27:48 -10:00
The 7300 ROM tries to read from an empty CD-ROM drive during boot. We would try to transfer data from an unallocated buffer and crash. Return NOT READY / MEDIUM NOT PRESENT for reads, capacity requests, and TOC requests. Also covers the separate ATAPI READ CD path. This matches the behavior described in ATAPI 2.6 section 8.5 and the per-command error tables: https://www.grix.it/UserFiles/marco.c/INF-8020.pdf
295 lines
8.8 KiB
C++
295 lines
8.8 KiB
C++
/*
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
|
Copyright (C) 2018-26 The DingusPPC Development Team
|
|
(See CREDITS.MD for more details)
|
|
|
|
(You may also contact divingkxt or powermax2286 on Discord)
|
|
|
|
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/>.
|
|
*/
|
|
|
|
#include <core/memaccess.h>
|
|
#include <devices/common/scsi/scsiblockcmds.h>
|
|
#include <loguru.hpp>
|
|
|
|
ScsiBlockCmds::ScsiBlockCmds() {
|
|
this->enable_cmd(ScsiCommand::READ_6);
|
|
this->enable_cmd(ScsiCommand::READ_10);
|
|
this->enable_cmd(ScsiCommand::READ_12);
|
|
this->enable_cmd(ScsiCommand::WRITE_6);
|
|
this->enable_cmd(ScsiCommand::WRITE_10);
|
|
this->enable_cmd(ScsiCommand::WRITE_12);
|
|
this->enable_cmd(ScsiCommand::START_STOP_UNIT);
|
|
this->enable_cmd(ScsiCommand::PREVENT_ALLOW_MEDIUM_REMOVAL);
|
|
this->enable_cmd(ScsiCommand::READ_CAPACITY);
|
|
|
|
this->add_page_getter(this, ModePage::ERROR_RECOVERY,
|
|
&ScsiBlockCmds::get_error_recovery_page);
|
|
}
|
|
|
|
void ScsiBlockCmds::init_block_device(uint8_t medium_type, uint8_t dev_flags,
|
|
uint8_t density_code, bool is_apple_compliant)
|
|
{
|
|
this->medium_type = medium_type;
|
|
this->device_flags = dev_flags;
|
|
|
|
phy_impl->set_read_more_data_cb(
|
|
[this](int* dsize, uint8_t** dptr) -> bool {
|
|
if (this->blk_dev->get_remaining_size()) {
|
|
*dsize = this->blk_dev->read_more();
|
|
*dptr = this->blk_dev->get_cache_ptr();
|
|
return true;
|
|
} else
|
|
return false;
|
|
}
|
|
);
|
|
|
|
if (this->blk_dev->medium_writable()) {
|
|
phy_impl->set_write_more_data_cb(
|
|
[this](int* dsize, uint8_t** dptr) -> bool {
|
|
if (this->blk_dev->get_remaining_size()) {
|
|
*dsize = this->blk_dev->write_more();
|
|
*dptr = this->blk_dev->get_cache_ptr();
|
|
return true;
|
|
} else
|
|
return false;
|
|
}
|
|
);
|
|
} else {
|
|
phy_impl->set_write_more_data_cb(
|
|
[this](int* dsize, uint8_t** dptr) {
|
|
return false;
|
|
}
|
|
);
|
|
}
|
|
|
|
if (is_apple_compliant)
|
|
this->add_page_getter(this, 0x30, &ScsiBlockCmds::get_apple_copyright_page);
|
|
}
|
|
|
|
void ScsiBlockCmds::process_command() {
|
|
if (!this->ready_for_command()) {
|
|
phy_impl->switch_phase(ScsiPhase::STATUS);
|
|
return;
|
|
}
|
|
|
|
int next_phase;
|
|
|
|
// use non-disk buffer by default
|
|
phy_impl->set_buffer(this->buf_ptr);
|
|
|
|
switch(this->cdb_ptr[0]) {
|
|
case ScsiCommand::READ_6:
|
|
case ScsiCommand::READ_10:
|
|
case ScsiCommand::READ_12:
|
|
next_phase = this->read();
|
|
break;
|
|
case ScsiCommand::WRITE_6:
|
|
case ScsiCommand::WRITE_10:
|
|
case ScsiCommand::WRITE_12:
|
|
next_phase = this->write();
|
|
break;
|
|
case ScsiCommand::START_STOP_UNIT:
|
|
next_phase = this->start_stop_unit();
|
|
break;
|
|
case ScsiCommand::PREVENT_ALLOW_MEDIUM_REMOVAL:
|
|
phy_impl->set_lock_state((this->cdb_ptr[4] & 1) != 0);
|
|
next_phase = ScsiPhase::STATUS;
|
|
break;
|
|
case ScsiCommand::READ_CAPACITY:
|
|
next_phase = this->read_capacity();
|
|
break;
|
|
default:
|
|
ScsiCommonCmds::process_command();
|
|
return;
|
|
}
|
|
|
|
phy_impl->switch_phase(next_phase);
|
|
}
|
|
|
|
int ScsiBlockCmds::read() {
|
|
int nblocks = this->get_xfer_len();
|
|
|
|
// special case: zero transfer length means 256 blocks for READ_6
|
|
if (this->cdb_ptr[0] == ScsiCommand::READ_6) {
|
|
if (!nblocks)
|
|
nblocks = 256;
|
|
} else {
|
|
if ((this->cdb_ptr[1] & 1) && !this->linked_cmds_supported()) {
|
|
LOG_F(WARNING, "READ: RelAdr bit set but it should not be");
|
|
this->set_field_pointer(1);
|
|
this->set_bit_pointer(0);
|
|
this->invalid_cdb();
|
|
return ScsiPhase::STATUS;
|
|
}
|
|
}
|
|
|
|
// go directly to the STATUS phase if nblocks = 0
|
|
if (!nblocks) {
|
|
LOG_F(WARNING, "READ: skip data transfer because nblocks = 0");
|
|
return ScsiPhase::STATUS;
|
|
}
|
|
|
|
this->blk_dev->set_fpos(this->get_lba());
|
|
phy_impl->set_xfer_len(this->blk_dev->read_begin(nblocks));
|
|
phy_impl->set_buffer(this->blk_dev->get_cache_ptr());
|
|
|
|
return ScsiPhase::DATA_IN;
|
|
}
|
|
|
|
int ScsiBlockCmds::write() {
|
|
int nblocks = this->get_xfer_len();
|
|
|
|
// special case: zero transfer length means 256 blocks for WRITE_6
|
|
if (this->cdb_ptr[0] == ScsiCommand::WRITE_6) {
|
|
if (!nblocks)
|
|
nblocks = 256;
|
|
} else {
|
|
if ((this->cdb_ptr[1] & 1) && !this->linked_cmds_supported()) {
|
|
LOG_F(WARNING, "WRITE: RelAdr bit set but it should not be");
|
|
this->set_field_pointer(1);
|
|
this->set_bit_pointer(0);
|
|
this->invalid_cdb();
|
|
return ScsiPhase::STATUS;
|
|
}
|
|
}
|
|
|
|
// go directly to the STATUS phase if nblocks = 0
|
|
if (!nblocks) {
|
|
LOG_F(WARNING, "WRITE: skip data transfer because nblocks = 0");
|
|
return ScsiPhase::STATUS;
|
|
}
|
|
|
|
this->blk_dev->set_fpos(this->get_lba());
|
|
phy_impl->set_xfer_len(this->blk_dev->write_begin(nblocks));
|
|
phy_impl->set_buffer(this->blk_dev->get_cache_ptr());
|
|
|
|
phy_impl->set_post_xfer_action([this]() {
|
|
this->blk_dev->write_cache();
|
|
}
|
|
);
|
|
|
|
return ScsiPhase::DATA_OUT;
|
|
}
|
|
|
|
int ScsiBlockCmds::start_stop_unit() {
|
|
if (this->cdb_ptr[4] & ScsiStartStopUnit::START) {
|
|
if (this->cdb_ptr[4] & ScsiStartStopUnit::LOAD_EJECT)
|
|
LOG_F(INFO, "START_STOP_UNIT: medium load requested");
|
|
} else {
|
|
if (this->cdb_ptr[4] & ScsiStartStopUnit::LOAD_EJECT)
|
|
this->eject_medium();
|
|
}
|
|
return ScsiPhase::STATUS;
|
|
}
|
|
|
|
void ScsiBlockCmds::eject_medium() {
|
|
LOG_F(INFO, "START_STOP_UNIT: medium eject requested");
|
|
}
|
|
|
|
int ScsiBlockCmds::read_capacity() {
|
|
uint32_t lba = this->get_lba();
|
|
|
|
if ((this->cdb_ptr[1] & 1) && !this->linked_cmds_supported()) {
|
|
LOG_F(WARNING, "READ_CAPACITY: RelAdr bit set but it should not be");
|
|
this->set_field_pointer(1);
|
|
this->set_bit_pointer(0);
|
|
this->invalid_cdb();
|
|
return ScsiPhase::STATUS;
|
|
}
|
|
|
|
if (!(this->cdb_ptr[8] & 1) && lba) {
|
|
LOG_F(ERROR, "READ_CAPACITY: non-zero LBA for PMI=0");
|
|
this->set_field_pointer(2);
|
|
this->invalid_cdb();
|
|
return ScsiPhase::STATUS;
|
|
}
|
|
|
|
uint32_t last_lba = (uint32_t)std::min(
|
|
this->blk_dev->get_size_in_blocks() - 1, (uint64_t)0xFFFFFFFFU);
|
|
uint32_t blk_len = this->blk_dev->get_block_size();
|
|
|
|
WRITE_DWORD_BE_A(&this->buf_ptr[0], last_lba);
|
|
WRITE_DWORD_BE_A(&this->buf_ptr[4], blk_len);
|
|
|
|
phy_impl->set_xfer_len(8);
|
|
|
|
return ScsiPhase::DATA_IN;
|
|
}
|
|
|
|
int ScsiBlockCmds::format_block_descriptors(uint8_t* out_ptr) {
|
|
uint32_t nblocks = (uint32_t)std::min(
|
|
this->blk_dev->get_size_in_blocks(), (uint64_t)0xFFFFFFFFU);
|
|
|
|
WRITE_DWORD_BE_A(&out_ptr[0], nblocks);
|
|
WRITE_DWORD_BE_A(&out_ptr[4], (this->density_code << 24) |
|
|
(this->blk_dev->get_block_size() & 0xFFFFFF));
|
|
|
|
return 8;
|
|
}
|
|
|
|
int ScsiBlockCmds::get_error_recovery_page(uint8_t ctrl, uint8_t subpage,
|
|
uint8_t *out_ptr, int avail_len)
|
|
{
|
|
if (subpage && subpage != 0xFFU)
|
|
return FORMAT_ERR_BAD_SUBPAGE;
|
|
|
|
if (ctrl == 3)
|
|
return FORMAT_ERR_BAD_CONTROL;
|
|
|
|
int page_size = 6;
|
|
|
|
if (page_size > avail_len)
|
|
return FORMAT_ERR_DATA_TOO_BIG;
|
|
|
|
std::memset(out_ptr, 0, page_size);
|
|
|
|
return page_size;
|
|
}
|
|
|
|
uint32_t ScsiBlockCmds::get_lba() {
|
|
int cmd_len = this->get_cdb_length(this->cdb_ptr[0]);
|
|
|
|
switch(cmd_len) {
|
|
case 6:
|
|
return ((this->cdb_ptr[1] & 0x1F) << 16) | READ_WORD_BE_A(&this->cdb_ptr[2]);
|
|
case 10:
|
|
case 12:
|
|
return READ_DWORD_BE_U(&this->cdb_ptr[2]);
|
|
default:
|
|
ABORT_F("unsupported command length %d in get_lba", cmd_len);
|
|
}
|
|
}
|
|
|
|
static char Apple_Copyright_Page_Data[] = "APPLE COMPUTER, INC ";
|
|
|
|
int ScsiBlockCmds::get_apple_copyright_page(uint8_t ctrl, uint8_t subpage, uint8_t *out_ptr,
|
|
int avail_len)
|
|
{
|
|
if (subpage && subpage != 0xFFU)
|
|
return FORMAT_ERR_BAD_SUBPAGE;
|
|
|
|
if (ctrl == 3)
|
|
return FORMAT_ERR_BAD_CONTROL;
|
|
|
|
int page_size = 22;
|
|
|
|
if (page_size > avail_len)
|
|
return FORMAT_ERR_DATA_TOO_BIG;
|
|
|
|
std::memcpy(out_ptr, Apple_Copyright_Page_Data, page_size);
|
|
|
|
return page_size;
|
|
}
|