Kernel: Move Partition out of StorageDevice and rename functions

This commit is contained in:
Bananymous 2023-03-29 13:23:01 +03:00
parent 9c7670847e
commit 1fb8c211f0
10 changed files with 51 additions and 48 deletions

View File

@ -65,7 +65,7 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override { return BAN::Error::from_errno(EISDIR); } virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override { return BAN::Error::from_errno(EISDIR); }
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override { return BAN::Error::from_errno(EINVAL); }; virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override { return BAN::Error::from_errno(EINVAL); };
virtual Type type() const override { return Type::DeviceManager; } virtual InodeType type() const override { return InodeType::DeviceManager; }
virtual bool operator==(const Inode&) const override { return false; } virtual bool operator==(const Inode&) const override { return false; }
virtual BAN::RefPtr<Inode> root_inode() override { return this; } virtual BAN::RefPtr<Inode> root_inode() override { return this; }

View File

@ -142,7 +142,7 @@ namespace Kernel
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override; virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override;
virtual Type type() const override { return Type::Ext2; } virtual InodeType type() const override { return InodeType::Ext2; }
virtual bool operator==(const Inode& other) const override; virtual bool operator==(const Inode& other) const override;
protected: protected:
@ -175,12 +175,12 @@ namespace Kernel
class Ext2FS : public FileSystem class Ext2FS : public FileSystem
{ {
public: public:
static BAN::ErrorOr<Ext2FS*> create(StorageDevice::Partition&); static BAN::ErrorOr<Ext2FS*> create(Partition&);
virtual BAN::RefPtr<Inode> root_inode() override { return m_root_inode; } virtual BAN::RefPtr<Inode> root_inode() override { return m_root_inode; }
private: private:
Ext2FS(StorageDevice::Partition& partition) Ext2FS(Partition& partition)
: m_partition(partition) : m_partition(partition)
{} {}
@ -207,7 +207,7 @@ namespace Kernel
uint32_t block_size() const { return 1024 << superblock().log_block_size; } uint32_t block_size() const { return 1024 << superblock().log_block_size; }
private: private:
StorageDevice::Partition& m_partition; Partition& m_partition;
BAN::RefPtr<Inode> m_root_inode; BAN::RefPtr<Inode> m_root_inode;

View File

@ -33,7 +33,7 @@ namespace Kernel
IFREG = 0x8000, IFREG = 0x8000,
}; };
enum class Type enum class InodeType
{ {
DeviceManager, DeviceManager,
Device, Device,
@ -66,7 +66,7 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) = 0; virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) = 0;
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) = 0; virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) = 0;
virtual Type type() const = 0; virtual InodeType type() const = 0;
virtual bool operator==(const Inode&) const = 0; virtual bool operator==(const Inode&) const = 0;
protected: protected:

View File

@ -76,7 +76,7 @@ namespace Kernel::Input
virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override; virtual BAN::ErrorOr<size_t> read(size_t, void*, size_t) override;
virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override { return BAN::Error::from_errno(ENOTDIR); } virtual BAN::ErrorOr<void> create_file(BAN::StringView, mode_t) override { return BAN::Error::from_errno(ENOTDIR); }
virtual Type type() const override { return Type::Device; } virtual InodeType type() const override { return InodeType::Device; }
virtual bool operator==(const Inode&) const override { return false; } virtual bool operator==(const Inode&) const override { return false; }
protected: protected:

View File

@ -9,7 +9,7 @@ namespace Kernel
struct ATABus; struct ATABus;
class ATAController; class ATAController;
class ATADevice : public StorageDevice class ATADevice final : public StorageDevice
{ {
public: public:
virtual BAN::ErrorOr<void> read_sectors(uint64_t, uint8_t, uint8_t*) override; virtual BAN::ErrorOr<void> read_sectors(uint64_t, uint8_t, uint8_t*) override;
@ -18,14 +18,14 @@ namespace Kernel
virtual uint64_t total_size() const override { return lba_count * sector_size(); } virtual uint64_t total_size() const override { return lba_count * sector_size(); }
private: private:
enum class Type enum class DeviceType
{ {
Unknown, Unknown,
ATA, ATA,
ATAPI, ATAPI,
}; };
Type type; DeviceType type;
uint8_t slave_bit; // 0x00 for master, 0x10 for slave uint8_t slave_bit; // 0x00 for master, 0x10 for slave
uint16_t signature; uint16_t signature;
uint16_t capabilities; uint16_t capabilities;

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <BAN/Vector.h> #include <BAN/Vector.h>
#include <kernel/Device.h>
namespace Kernel namespace Kernel
{ {
@ -13,35 +14,37 @@ namespace Kernel
uint8_t data4[8]; uint8_t data4[8];
}; };
class StorageDevice class StorageDevice;
class Partition
{ {
public: public:
struct Partition Partition(StorageDevice&, 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& partition_type() const { return m_type; }
const GUID& guid() const { return m_guid; } const GUID& partition_guid() const { return m_guid; }
uint64_t lba_start() const { return m_lba_start; } uint64_t lba_start() const { return m_lba_start; }
uint64_t lba_end() const { return m_lba_end; } uint64_t lba_end() const { return m_lba_end; }
uint64_t attributes() const { return m_attributes; } uint64_t attributes() const { return m_attributes; }
const char* name() const { return m_name; } const char* label() const { return m_label; }
const StorageDevice& device() const { return m_device; } const StorageDevice& device() const { return m_device; }
BAN::ErrorOr<void> read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer); BAN::ErrorOr<void> read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer);
BAN::ErrorOr<void> write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer); BAN::ErrorOr<void> write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer);
bool is_used() const { uint8_t zero[16] {}; return memcmp(&m_type, zero, 16); } bool is_used() const { uint8_t zero[16] {}; return memcmp(&m_type, zero, 16); }
private: private:
StorageDevice& m_device; StorageDevice& m_device;
const GUID m_type; const GUID m_type;
const GUID m_guid; const GUID m_guid;
const uint64_t m_lba_start; const uint64_t m_lba_start;
const uint64_t m_lba_end; const uint64_t m_lba_end;
const uint64_t m_attributes; const uint64_t m_attributes;
char m_name[36 * 4 + 1]; char m_label[36 * 4 + 1];
}; };
class StorageDevice
{
public: public:
virtual ~StorageDevice() {} virtual ~StorageDevice() {}

View File

@ -444,7 +444,7 @@ namespace Kernel
return index() == ext2_other.index(); return index() == ext2_other.index();
} }
BAN::ErrorOr<Ext2FS*> Ext2FS::create(StorageDevice::Partition& partition) BAN::ErrorOr<Ext2FS*> Ext2FS::create(Partition& partition)
{ {
Ext2FS* ext2fs = new Ext2FS(partition); Ext2FS* ext2fs = new Ext2FS(partition);
if (ext2fs == nullptr) if (ext2fs == nullptr)

View File

@ -92,7 +92,7 @@ namespace Kernel
for (auto& partition : device->partitions()) for (auto& partition : device->partitions())
{ {
if (partition.name() == "banan-root"sv) if (partition.label() == "banan-root"sv)
{ {
if (root_inode()) if (root_inode())
dwarnln("multiple root partitions found"); dwarnln("multiple root partitions found");
@ -127,7 +127,7 @@ namespace Kernel
{ {
for (auto& partition : device->partitions()) for (auto& partition : device->partitions())
{ {
if (partition.name() == "mount-test"sv) if (partition.label() == "mount-test"sv)
{ {
auto ext2fs = TRY(Ext2FS::create(partition)); auto ext2fs = TRY(Ext2FS::create(partition));
TRY(mount(ext2fs, "/mnt"sv)); TRY(mount(ext2fs, "/mnt"sv));

View File

@ -89,7 +89,7 @@ namespace Kernel
ATABus& bus = m_buses[bus_index]; ATABus& bus = m_buses[bus_index];
ATADevice& device = bus.devices[device_index]; ATADevice& device = bus.devices[device_index];
device.type = ATADevice::Type::Unknown; device.type = ATADevice::DeviceType::Unknown;
device.slave_bit = device_index << 4; device.slave_bit = device_index << 4;
device.bus = &bus; device.bus = &bus;
device.controller = this; device.controller = this;
@ -113,7 +113,7 @@ namespace Kernel
break; break;
} }
auto type = ATADevice::Type::ATA; auto type = ATADevice::DeviceType::ATA;
// Not a ATA device, maybe ATAPI // Not a ATA device, maybe ATAPI
if (status & ATA_STATUS_ERR) if (status & ATA_STATUS_ERR)
@ -122,9 +122,9 @@ namespace Kernel
uint8_t lba2 = bus.read(ATA_PORT_LBA2); uint8_t lba2 = bus.read(ATA_PORT_LBA2);
if (lba1 == 0x14 && lba2 == 0xEB) if (lba1 == 0x14 && lba2 == 0xEB)
type = ATADevice::Type::ATAPI; type = ATADevice::DeviceType::ATAPI;
else if (lba1 == 0x69 && lba2 == 0x96) else if (lba1 == 0x69 && lba2 == 0x96)
type = ATADevice::Type::ATAPI; type = ATADevice::DeviceType::ATAPI;
else else
continue; continue;
@ -171,12 +171,12 @@ namespace Kernel
for (uint32_t j = 0; j < 2; j++) for (uint32_t j = 0; j < 2; j++)
{ {
ATADevice& device = m_buses[i].devices[j]; ATADevice& device = m_buses[i].devices[j];
if (device.type == ATADevice::Type::Unknown) if (device.type == ATADevice::DeviceType::Unknown)
continue; continue;
constexpr uint32_t words_per_mib = 1024 * 1024 / 2; constexpr uint32_t words_per_mib = 1024 * 1024 / 2;
const char* device_type = const char* device_type =
device.type == ATADevice::Type::ATA ? "ATA" : device.type == ATADevice::DeviceType::ATA ? "ATA" :
device.type == ATADevice::Type::ATAPI ? "ATAPI" : device.type == ATADevice::DeviceType::ATAPI ? "ATAPI" :
"Unknown"; "Unknown";
dprintln("Found {} Drive ({} MiB) model {}", device_type, device.lba_count * device.sector_words / words_per_mib, device.model); dprintln("Found {} Drive ({} MiB) model {}", device_type, device.lba_count * device.sector_words / words_per_mib, device.model);
} }

View File

@ -185,7 +185,7 @@ namespace Kernel
return {}; return {};
} }
StorageDevice::Partition::Partition(StorageDevice& device, const GUID& type, const GUID& guid, uint64_t start, uint64_t end, uint64_t attr, const char* name) Partition::Partition(StorageDevice& device, const GUID& type, const GUID& guid, uint64_t start, uint64_t end, uint64_t attr, const char* label)
: m_device(device) : m_device(device)
, m_type(type) , m_type(type)
, m_guid(guid) , m_guid(guid)
@ -193,10 +193,10 @@ namespace Kernel
, m_lba_end(end) , m_lba_end(end)
, m_attributes(attr) , m_attributes(attr)
{ {
memcpy(m_name, name, sizeof(m_name)); memcpy(m_label, label, sizeof(m_label));
} }
BAN::ErrorOr<void> StorageDevice::Partition::read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer) BAN::ErrorOr<void> Partition::read_sectors(uint64_t lba, uint8_t sector_count, uint8_t* buffer)
{ {
const uint32_t sectors_in_partition = m_lba_end - m_lba_start; const uint32_t sectors_in_partition = m_lba_end - m_lba_start;
if (lba + sector_count > sectors_in_partition) if (lba + sector_count > sectors_in_partition)
@ -205,7 +205,7 @@ namespace Kernel
return {}; return {};
} }
BAN::ErrorOr<void> StorageDevice::Partition::write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer) BAN::ErrorOr<void> Partition::write_sectors(uint64_t lba, uint8_t sector_count, const uint8_t* buffer)
{ {
const uint32_t sectors_in_partition = m_lba_end - m_lba_start; const uint32_t sectors_in_partition = m_lba_end - m_lba_start;
if (lba + sector_count > sectors_in_partition) if (lba + sector_count > sectors_in_partition)