Kernel: Rewrite basically all current disk io stuff
This is a big commit that was kinda hard to split to smaller ones. Essentially we now look at all the mass storage devices from PCI and initialize (P)ATA devices. This doesn't provide any more functionality but better abstractions and everything doesn't have to be on its old default port that might be different for modern computers.
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Vector.h>
|
||||
#include <kernel/DiskIO.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class ATADevice : public DiskDevice
|
||||
{
|
||||
public:
|
||||
static ATADevice* create(uint16_t io_base, uint16_t ctl_base, uint8_t slave_bit);
|
||||
|
||||
virtual ~ATADevice() {}
|
||||
|
||||
uint16_t io_base() const { return m_io_base; }
|
||||
uint16_t ctl_base() const { return m_ctl_base; }
|
||||
uint8_t slave_bit() const { return m_slave_bit; }
|
||||
virtual const char* type() const = 0;
|
||||
|
||||
protected:
|
||||
ATADevice(uint16_t io_base, uint16_t ctl_base, uint8_t slave_bit)
|
||||
: m_io_base(io_base)
|
||||
, m_ctl_base(ctl_base)
|
||||
, m_slave_bit(slave_bit)
|
||||
{}
|
||||
|
||||
private:
|
||||
const uint16_t m_io_base;
|
||||
const uint16_t m_ctl_base;
|
||||
const uint8_t m_slave_bit;
|
||||
};
|
||||
|
||||
class PATADevice final : public ATADevice
|
||||
{
|
||||
public:
|
||||
PATADevice(uint16_t io_base, uint16_t ctl_base, uint8_t slave_bit)
|
||||
: ATADevice(io_base, ctl_base, slave_bit)
|
||||
{}
|
||||
|
||||
virtual uint32_t sector_size() const override { return m_sector_words * 2; }
|
||||
virtual const char* type() const override { return "PATA"; }
|
||||
|
||||
virtual bool read_sectors(uint32_t lba, uint32_t sector_count, uint8_t* buffer) override;
|
||||
|
||||
protected:
|
||||
virtual bool initialize() override;
|
||||
|
||||
private:
|
||||
bool read_lba28(uint32_t lba, uint8_t sector_count, uint8_t* buffer);
|
||||
bool wait_while_buzy();
|
||||
bool wait_for_transfer();
|
||||
void flush();
|
||||
|
||||
private:
|
||||
bool m_lba_48 = false;
|
||||
uint32_t m_sector_words = 256;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/String.h>
|
||||
#include <kernel/DiskIO.h>
|
||||
#include <kernel/Storage/StorageDevice.h>
|
||||
#include <kernel/FS/FileSystem.h>
|
||||
|
||||
namespace Kernel
|
||||
@@ -156,12 +156,12 @@ namespace Kernel
|
||||
class Ext2FS : public FileSystem
|
||||
{
|
||||
public:
|
||||
static BAN::ErrorOr<Ext2FS*> create(DiskDevice::Partition&);
|
||||
static BAN::ErrorOr<Ext2FS*> create(StorageDevice::Partition&);
|
||||
|
||||
virtual const BAN::RefCounted<Inode> root_inode() const override { return m_root_inode; }
|
||||
|
||||
private:
|
||||
Ext2FS(DiskDevice::Partition& partition)
|
||||
Ext2FS(StorageDevice::Partition& partition)
|
||||
: m_partition(partition)
|
||||
{}
|
||||
|
||||
@@ -177,7 +177,7 @@ namespace Kernel
|
||||
const Ext2::Inode& ext2_root_inode() const;
|
||||
|
||||
private:
|
||||
DiskDevice::Partition& m_partition;
|
||||
StorageDevice::Partition& m_partition;
|
||||
|
||||
BAN::RefCounted<Inode> m_root_inode;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <kernel/FS/FileSystem.h>
|
||||
#include <kernel/Storage/StorageController.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
@@ -8,7 +9,7 @@ namespace Kernel
|
||||
class VirtualFileSystem : public FileSystem
|
||||
{
|
||||
public:
|
||||
static void initialize(BAN::RefCounted<Inode> root_inode);
|
||||
static BAN::ErrorOr<void> initialize();
|
||||
static VirtualFileSystem& get();
|
||||
static bool is_initialized();
|
||||
|
||||
@@ -17,12 +18,13 @@ namespace Kernel
|
||||
BAN::ErrorOr<BAN::RefCounted<Inode>> from_absolute_path(BAN::StringView);
|
||||
|
||||
private:
|
||||
VirtualFileSystem(BAN::RefCounted<Inode> root_inode)
|
||||
: m_root_inode(root_inode)
|
||||
{}
|
||||
VirtualFileSystem() = default;
|
||||
BAN::ErrorOr<void> initialize_impl();
|
||||
|
||||
private:
|
||||
BAN::RefCounted<Inode> m_root_inode;
|
||||
|
||||
BAN::Vector<StorageController*> m_storage_controllers;
|
||||
};
|
||||
|
||||
}
|
||||
67
kernel/include/kernel/Storage/ATAController.h
Normal file
67
kernel/include/kernel/Storage/ATAController.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Errors.h>
|
||||
#include <kernel/Storage/StorageController.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
struct ATABus;
|
||||
|
||||
class ATADevice : public StorageDevice
|
||||
{
|
||||
public:
|
||||
virtual BAN::ErrorOr<void> read_sectors(uint64_t, uint8_t, uint8_t*) override;
|
||||
virtual uint32_t sector_size() const override { return sector_words * 2; }
|
||||
virtual uint64_t total_size() const override { return lba_count * sector_size(); }
|
||||
|
||||
private:
|
||||
enum class Type
|
||||
{
|
||||
Unknown,
|
||||
ATA,
|
||||
ATAPI,
|
||||
};
|
||||
|
||||
Type type;
|
||||
uint8_t slave_bit; // 0x00 for master, 0x10 for slave
|
||||
uint16_t signature;
|
||||
uint16_t capabilities;
|
||||
uint32_t command_set;
|
||||
uint32_t sector_words;
|
||||
uint64_t lba_count;
|
||||
char model[41];
|
||||
|
||||
ATABus* bus;
|
||||
|
||||
friend class ATAController;
|
||||
};
|
||||
|
||||
struct ATABus
|
||||
{
|
||||
uint16_t base;
|
||||
uint16_t ctrl;
|
||||
ATADevice devices[2];
|
||||
|
||||
uint8_t read(uint16_t);
|
||||
void read_buffer(uint16_t, uint16_t*, size_t);
|
||||
void write(uint16_t, uint8_t);
|
||||
BAN::ErrorOr<void> wait(bool);
|
||||
BAN::Error error();
|
||||
};
|
||||
|
||||
class ATAController : public StorageController
|
||||
{
|
||||
public:
|
||||
static BAN::ErrorOr<ATAController*> create(const PCIDevice&);
|
||||
|
||||
private:
|
||||
ATAController(const PCIDevice& device) : m_pci_device(device) {}
|
||||
BAN::ErrorOr<void> initialize();
|
||||
|
||||
private:
|
||||
ATABus m_buses[2];
|
||||
const PCIDevice& m_pci_device;
|
||||
};
|
||||
|
||||
}
|
||||
19
kernel/include/kernel/Storage/StorageController.h
Normal file
19
kernel/include/kernel/Storage/StorageController.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <kernel/PCI.h>
|
||||
#include <kernel/Storage/StorageDevice.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
class StorageController
|
||||
{
|
||||
public:
|
||||
BAN::Vector<StorageDevice*>& devices() { return m_devices; }
|
||||
const BAN::Vector<StorageDevice*>& devices() const { return m_devices; }
|
||||
|
||||
protected:
|
||||
BAN::Vector<StorageDevice*> m_devices;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -13,12 +13,12 @@ namespace Kernel
|
||||
uint8_t data4[8];
|
||||
};
|
||||
|
||||
class DiskDevice
|
||||
class StorageDevice
|
||||
{
|
||||
public:
|
||||
struct Partition
|
||||
{
|
||||
Partition(DiskDevice&, const GUID&, const GUID&, uint64_t, uint64_t, uint64_t, const char*);
|
||||
Partition(StorageDevice&, const GUID&, const GUID&, uint64_t, uint64_t, uint64_t, const char*);
|
||||
|
||||
const GUID& type() const { return m_type; }
|
||||
const GUID& guid() const { return m_guid; }
|
||||
@@ -26,29 +26,29 @@ namespace Kernel
|
||||
uint64_t lba_end() const { return m_lba_end; }
|
||||
uint64_t attributes() const { return m_attributes; }
|
||||
const char* name() const { return m_name; }
|
||||
const DiskDevice& device() const { return m_device; }
|
||||
const StorageDevice& device() const { return m_device; }
|
||||
|
||||
bool read_sectors(uint32_t lba, uint32_t sector_count, uint8_t* buffer);
|
||||
BAN::ErrorOr<void> read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer);
|
||||
bool is_used() const { uint8_t zero[16] {}; return memcmp(&m_type, zero, 16); }
|
||||
|
||||
private:
|
||||
DiskDevice& m_device;
|
||||
StorageDevice& m_device;
|
||||
const GUID m_type;
|
||||
const GUID m_guid;
|
||||
const uint64_t m_lba_start;
|
||||
const uint64_t m_lba_end;
|
||||
const uint64_t m_attributes;
|
||||
char m_name[72];
|
||||
char m_name[36 * 3 + 1];
|
||||
};
|
||||
|
||||
public:
|
||||
virtual ~DiskDevice() {}
|
||||
virtual ~StorageDevice() {}
|
||||
|
||||
virtual bool initialize() = 0;
|
||||
bool initialize_partitions();
|
||||
BAN::ErrorOr<void> initialize_partitions();
|
||||
|
||||
virtual bool read_sectors(uint32_t lba, uint32_t sector_count, uint8_t* buffer) = 0;
|
||||
virtual BAN::ErrorOr<void> read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer) = 0;
|
||||
virtual uint32_t sector_size() const = 0;
|
||||
virtual uint64_t total_size() const = 0;
|
||||
|
||||
BAN::Vector<Partition>& partitions() { return m_partitions; }
|
||||
|
||||
@@ -56,18 +56,4 @@ namespace Kernel
|
||||
BAN::Vector<Partition> m_partitions;
|
||||
};
|
||||
|
||||
class DiskIO
|
||||
{
|
||||
public:
|
||||
static bool initialize();
|
||||
static DiskIO& get();
|
||||
|
||||
private:
|
||||
DiskIO();
|
||||
void try_add_device(DiskDevice*);
|
||||
|
||||
private:
|
||||
BAN::Vector<DiskDevice*> m_devices;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user