More ATA interface cleanup.

This commit is contained in:
Maxim Poliakovski 2022-12-09 00:52:39 +01:00
parent 2537751fa7
commit daf3ecde4e
7 changed files with 137 additions and 25 deletions

View File

@ -111,6 +111,10 @@ void AtaBaseDevice::write(const uint8_t reg_addr, const uint16_t value) {
}
break;
case IDE_Reg::DEV_CTRL:
if (!(this->r_dev_ctrl & ATA_CTRL_SRST) && (value & ATA_CTRL_SRST)) {
LOG_F(INFO, "%s: soft reset triggered", this->name.c_str());
this->r_status |= IDE_Status::BSY;
}
this->r_dev_ctrl = value;
break;
default:

View File

@ -40,6 +40,8 @@ public:
virtual int perform_command() = 0;
int get_device_id() { return this->my_dev_id; };
void device_reset();
private:

View File

@ -45,7 +45,7 @@ enum IDE_Reg : int {
TIME_CONFIG = 0x20
};
/** Status Register Bits */
/** Status register bits. */
enum IDE_Status : int {
ERR = 0x1,
IDX = 0x2,
@ -57,7 +57,7 @@ enum IDE_Status : int {
BSY = 0x80
};
/** Error Register Bits */
/** Error register bits. */
enum IDE_Error : int {
ANMF = 0x1,
TK0NF = 0x2,
@ -69,20 +69,48 @@ enum IDE_Error : int {
BBK = 0x80
};
<<<<<<< HEAD
/** Bit definition for the device control register. */
enum {
ATA_CTRL_nIEN = 1 << 1,
ATA_CTRL_SRST = 1 << 2,
};
/** ATA commands. */
=======
/* ATA Signals */
enum IDE_Signal : int {
PDIAG = 0x22,
DASP = 0x27
};
/* ATA commands. */
>>>>>>> 1e9ec5d... Start ATA command support
enum IDE_Cmd : int {
NOP = 0x00,
RESET_ATAPI = 0x08,
RECALIBRATE = 0x10,
READ_SECTOR = 0x20,
READ_LONG = 0x22,
WRITE_SECTOR = 0x30,
WRITE_LONG = 0x32,
WRITE_VERIFY = 0x40,
FORMAT_TRACKS = 0x50,
DIAGNOSTICS = 0x90,
READ_DMA = 0xC8,
WRITE_DMA = 0xCA,
NOP = 0x00,
RESET_ATAPI = 0x08,
RECALIBRATE = 0x10,
READ_SECTOR = 0x20,
READ_SECTOR_NR = 0x21,
READ_LONG = 0x22,
READ_SECTOR_EXT = 0x24,
WRITE_SECTOR = 0x30,
WRITE_SECTOR_NR = 0x21,
WRITE_LONG = 0x32,
READ_VERIFY = 0x40,
FORMAT_TRACKS = 0x50,
IDE_SEEK = 0x70,
DIAGNOSTICS = 0x90,
INIT_DEV_PARAM = 0x91,
PACKET = 0xA0,
IDFY_PKT_DEV = 0xA1,
READ_MULTIPLE = 0xC4,
WRITE_MULTIPLE = 0xC5,
READ_DMA = 0xC8,
WRITE_DMA = 0xCA,
WRITE_BUFFER_DMA = 0xE9,
READ_BUFFER_DMA = 0xEB,
IDENTIFY_DEVICE = 0xEC,
};
}; // namespace ata_interface
@ -94,6 +122,8 @@ public:
virtual ~AtaInterface() = default;
virtual uint16_t read(const uint8_t reg_addr) = 0;
virtual void write(const uint8_t reg_addr, const uint16_t val) = 0;
virtual int get_device_id() = 0;
};
/** Dummy ATA device. */
@ -112,6 +142,9 @@ public:
};
void write(const uint8_t reg_addr, const uint16_t val) {};
// invalid device ID means no real device is present
int get_device_id() { return -1; };
};
#endif // ATA_INTERFACE_H

View File

@ -22,14 +22,58 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file ATA hard disk emulation. */
#include <devices/common/ata/atahd.h>
#include <sys/stat.h>
#include <fstream>
#include <string>
#include <loguru.hpp>
using namespace ata_interface;
AtaHardDisk::AtaHardDisk() : AtaBaseDevice("ATA-HD")
{
}
void AtaHardDisk::insert_image(std::string filename) {
this->hdd_img.open(filename, std::fstream::out | std::fstream::in | std::fstream::binary);
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()");
}
this->hdd_img.seekg(0, std::ios_base::beg);
}
int AtaHardDisk::perform_command()
{
LOG_F(INFO, "%s: command 0x%x requested", this->name.c_str(), this->r_command);
this->r_status |= BSY;
switch (this->r_command) {
case NOP:
break;
case RESET_ATAPI: {
device_reset();
break;
}
case READ_SECTOR:
case READ_SECTOR_NR: {
uint16_t sec_count = (this->r_sect_count == 0) ? 256 : this->r_sect_count;
uint32_t sector = (r_sect_num << 16);
sector |= ((this->r_cylinder_lo) << 8) + (this->r_cylinder_hi);
uint64_t offset = sector * 512;
hdd_img.seekg(offset, std::ios::beg);
hdd_img.read(buffer, sec_count * 512);
break;
}
case DIAGNOSTICS:
break;
default:
LOG_F(INFO, "Unknown ATA command 0x%x", this->r_command);
this->r_status |= ERR;
}
this->r_status &= ~(BSY);
this->r_status |= DRDY;
return -1;
}

View File

@ -25,6 +25,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#define ATA_HARD_DISK_H
#include <devices/common/ata/atabasedevice.h>
#include <string>
#include <fstream>
class AtaHardDisk : public AtaBaseDevice
{
@ -32,7 +34,13 @@ public:
AtaHardDisk();
~AtaHardDisk() = default;
void insert_image(std::string filename);
int perform_command();
private:
std::fstream hdd_img;
uint64_t img_size;
char * buffer = new char[1 <<17];
};
#endif // ATA_HARD_DISK_H

View File

@ -46,21 +46,35 @@ IdeChannel::IdeChannel(const std::string name)
this->devices[1] = std::unique_ptr<AtaNullDevice>(new AtaNullDevice());
}
uint16_t IdeChannel::read(const uint8_t reg_addr, const int size)
uint32_t IdeChannel::read(const uint8_t reg_addr, const int size)
{
return this->devices[this->cur_dev]->read(reg_addr);
if (reg_addr == IDE_Reg::TIME_CONFIG) {
if (size != 4) {
LOG_F(WARNING, "%s: non-DWORD read from the channel config", this->name.c_str());
}
return this->ch_config;
} else {
return this->devices[this->cur_dev]->read(reg_addr);
}
}
void IdeChannel::write(const uint8_t reg_addr, const uint16_t val, const int size)
void IdeChannel::write(const uint8_t reg_addr, const uint32_t val, const int size)
{
// keep track of the currently selected device
if (reg_addr == IDE_Reg::DEVICE_HEAD) {
this->cur_dev = (val >> 4) & 1;
}
if (reg_addr == IDE_Reg::TIME_CONFIG) {
if (size != 4) {
LOG_F(WARNING, "%s: non-DWORD write to the channel config", this->name.c_str());
}
this->ch_config = val;
} else {
// keep track of the currently selected device
if (reg_addr == IDE_Reg::DEVICE_HEAD) {
this->cur_dev = (val >> 4) & 1;
}
// redirect register writes to both devices
for (auto& dev : this->devices) {
dev->write(reg_addr, val);
// redirect register writes to both devices
for (auto& dev : this->devices) {
dev->write(reg_addr, val);
}
}
}

View File

@ -45,11 +45,18 @@ public:
return std::unique_ptr<IdeChannel>(new IdeChannel("IDE1"));
}
<<<<<<< HEAD
uint32_t read(const uint8_t reg_addr, const int size);
void write(const uint8_t reg_addr, const uint32_t val, const int size);
=======
uint16_t read(const uint8_t reg_addr, const int size);
void write(const uint8_t reg_addr, const uint16_t val, const int size);
//void talk_to_channel(std::string name)
>>>>>>> 1e9ec5d... Start ATA command support
private:
int cur_dev = 0;
int cur_dev = 0;
uint32_t ch_config = 0; // timing configuration for this channel
std::unique_ptr<AtaInterface> devices[2];
};