1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

Kernel: Merge unnecessary DiskDevice class into BlockDevice

This commit is contained in:
Andreas Kling 2020-02-08 02:17:26 +01:00
parent 6be880bd10
commit 88ea152b24
27 changed files with 98 additions and 212 deletions

View file

@ -47,6 +47,8 @@ private:
virtual bool can_write(const FileDescription&) const override { return true; }
virtual ssize_t read(FileDescription&, u8*, ssize_t) override { return -EINVAL; }
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override { return -EINVAL; }
virtual bool read_blocks(unsigned, u16, u8*) override { return false; }
virtual bool write_blocks(unsigned, u16, const u8*) override { return false; }
void set_register(u16 index, u16 value);
u32 find_framebuffer_address();

View file

@ -29,3 +29,33 @@
BlockDevice::~BlockDevice()
{
}
bool BlockDevice::read_block(unsigned index, u8* buffer) const
{
return const_cast<BlockDevice*>(this)->read_blocks(index, 1, buffer);
}
bool BlockDevice::write_block(unsigned index, const u8* data)
{
return write_blocks(index, 1, data);
}
bool BlockDevice::read_raw(u32 offset, unsigned length, u8* out) const
{
ASSERT((offset % block_size()) == 0);
ASSERT((length % block_size()) == 0);
u32 first_block = offset / block_size();
u32 end_block = (offset + length) / block_size();
return const_cast<BlockDevice*>(this)->read_blocks(first_block, end_block - first_block, out);
}
bool BlockDevice::write_raw(u32 offset, unsigned length, const u8* in)
{
ASSERT((offset % block_size()) == 0);
ASSERT((length % block_size()) == 0);
u32 first_block = offset / block_size();
u32 end_block = (offset + length) / block_size();
ASSERT(first_block <= 0xffffffff);
ASSERT(end_block <= 0xffffffff);
return write_blocks(first_block, end_block - first_block, in);
}

View file

@ -35,6 +35,14 @@ public:
size_t block_size() const { return m_block_size; }
virtual bool is_seekable() const override { return true; }
bool read_block(unsigned index, u8*) const;
bool write_block(unsigned index, const u8*);
bool read_raw(u32 offset, unsigned length, u8*) const;
bool write_raw(u32 offset, unsigned length, const u8*);
virtual bool read_blocks(unsigned index, u16 count, u8*) = 0;
virtual bool write_blocks(unsigned index, u16 count, const u8*) = 0;
protected:
BlockDevice(unsigned major, unsigned minor, size_t block_size = PAGE_SIZE)
: Device(major, minor)

View file

@ -1,56 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Kernel/Devices/DiskDevice.h>
DiskDevice::DiskDevice(int major, int minor, size_t block_size)
: BlockDevice(major, minor, block_size)
{
}
DiskDevice::~DiskDevice()
{
}
bool DiskDevice::read(DiskOffset offset, unsigned length, u8* out) const
{
ASSERT((offset % block_size()) == 0);
ASSERT((length % block_size()) == 0);
u32 first_block = offset / block_size();
u32 end_block = (offset + length) / block_size();
return const_cast<DiskDevice*>(this)->read_blocks(first_block, end_block - first_block, out);
}
bool DiskDevice::write(DiskOffset offset, unsigned length, const u8* in)
{
ASSERT((offset % block_size()) == 0);
ASSERT((length % block_size()) == 0);
u32 first_block = offset / block_size();
u32 end_block = (offset + length) / block_size();
ASSERT(first_block <= 0xffffffff);
ASSERT(end_block <= 0xffffffff);
return write_blocks(first_block, end_block - first_block, in);
}

View file

@ -1,52 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/RefCounted.h>
#include <AK/Types.h>
#include <Kernel/Devices/BlockDevice.h>
// FIXME: Support 64-bit DiskOffset
typedef u32 DiskOffset;
class DiskDevice : public BlockDevice {
public:
virtual ~DiskDevice() override;
virtual bool read_block(unsigned index, u8*) const = 0;
virtual bool write_block(unsigned index, const u8*) = 0;
bool read(DiskOffset, unsigned length, u8*) const;
bool write(DiskOffset, unsigned length, const u8*);
virtual bool read_blocks(unsigned index, u16 count, u8*) = 0;
virtual bool write_blocks(unsigned index, u16 count, const u8*) = 0;
virtual bool is_disk_device() const override { return true; };
protected:
DiskDevice(int major, int minor, size_t block_size = 512);
};

View file

@ -28,13 +28,13 @@
// #define OFFD_DEBUG
NonnullRefPtr<DiskPartition> DiskPartition::create(DiskDevice& device, unsigned block_offset, unsigned block_limit)
NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned block_offset, unsigned block_limit)
{
return adopt(*new DiskPartition(device, block_offset, block_limit));
}
DiskPartition::DiskPartition(DiskDevice& device, unsigned block_offset, unsigned block_limit)
: DiskDevice(100, 0, device.block_size())
DiskPartition::DiskPartition(BlockDevice& device, unsigned block_offset, unsigned block_limit)
: BlockDevice(100, 0, device.block_size())
, m_device(device)
, m_block_offset(block_offset)
, m_block_limit(block_limit)
@ -45,24 +45,6 @@ DiskPartition::~DiskPartition()
{
}
bool DiskPartition::read_block(unsigned index, u8* out) const
{
#ifdef OFFD_DEBUG
kprintf("DiskPartition::read_block %u (really: %u)\n", index, m_block_offset + index);
#endif
return m_device->read_block(m_block_offset + index, out);
}
bool DiskPartition::write_block(unsigned index, const u8* data)
{
#ifdef OFFD_DEBUG
kprintf("DiskPartition::write_block %u (really: %u)\n", index, m_block_offset + index);
#endif
return m_device->write_block(m_block_offset + index, data);
}
bool DiskPartition::read_blocks(unsigned index, u16 count, u8* out)
{
#ifdef OFFD_DEBUG

View file

@ -27,15 +27,13 @@
#pragma once
#include <AK/RefPtr.h>
#include <Kernel/Devices/DiskDevice.h>
#include <Kernel/Devices/BlockDevice.h>
class DiskPartition final : public DiskDevice {
class DiskPartition final : public BlockDevice {
public:
static NonnullRefPtr<DiskPartition> create(DiskDevice&, unsigned block_offset, unsigned block_limit);
static NonnullRefPtr<DiskPartition> create(BlockDevice&, unsigned block_offset, unsigned block_limit);
virtual ~DiskPartition();
virtual bool read_block(unsigned index, u8* out) const override;
virtual bool write_block(unsigned index, const u8*) override;
virtual bool read_blocks(unsigned index, u16 count, u8*) override;
virtual bool write_blocks(unsigned index, u16 count, const u8*) override;
@ -48,9 +46,9 @@ public:
private:
virtual const char* class_name() const override;
DiskPartition(DiskDevice&, unsigned block_offset, unsigned block_limit);
DiskPartition(BlockDevice&, unsigned block_offset, unsigned block_limit);
NonnullRefPtr<DiskDevice> m_device;
NonnullRefPtr<BlockDevice> m_device;
unsigned m_block_offset;
unsigned m_block_limit;
};

View file

@ -29,7 +29,7 @@
#define EBR_DEBUG
EBRPartitionTable::EBRPartitionTable(NonnullRefPtr<DiskDevice> device)
EBRPartitionTable::EBRPartitionTable(NonnullRefPtr<BlockDevice> device)
: m_device(move(device))
{
}

View file

@ -28,7 +28,6 @@
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <Kernel/Devices/DiskDevice.h>
#include <Kernel/Devices/DiskPartition.h>
#include <Kernel/Devices/MBRPartitionTable.h>
@ -44,7 +43,7 @@ struct [[gnu::packed]] EBRPartitionExtension
class EBRPartitionTable {
public:
explicit EBRPartitionTable(NonnullRefPtr<DiskDevice>);
explicit EBRPartitionTable(NonnullRefPtr<BlockDevice>);
~EBRPartitionTable();
bool initialize();
@ -52,7 +51,7 @@ public:
private:
int index_of_ebr_container() const;
NonnullRefPtr<DiskDevice> m_device;
NonnullRefPtr<BlockDevice> m_device;
const MBRPartitionHeader& header() const;
const EBRPartitionExtension& ebr_extension() const;

View file

@ -110,7 +110,7 @@ const char* FloppyDiskDevice::class_name() const
FloppyDiskDevice::FloppyDiskDevice(FloppyDiskDevice::DriveType type)
: IRQHandler(IRQ_FLOPPY_DRIVE)
, DiskDevice(89, (type == FloppyDiskDevice::DriveType::Master) ? 0 : 1, BYTES_PER_SECTOR)
, BlockDevice(89, (type == FloppyDiskDevice::DriveType::Master) ? 0 : 1, BYTES_PER_SECTOR)
, m_io_base_addr((type == FloppyDiskDevice::DriveType::Master) ? 0x3F0 : 0x370)
{
initialize();
@ -120,16 +120,6 @@ FloppyDiskDevice::~FloppyDiskDevice()
{
}
bool FloppyDiskDevice::read_block(unsigned index, u8* data) const
{
return const_cast<FloppyDiskDevice*>(this)->read_blocks(index, 1, data);
}
bool FloppyDiskDevice::write_block(unsigned index, const u8* data)
{
return write_sectors_with_dma(index, 1, data);
}
bool FloppyDiskDevice::read_blocks(unsigned index, u16 count, u8* data)
{
return read_sectors_with_dma(index, count, data);

View file

@ -98,7 +98,7 @@
#pragma once
#include <AK/RefPtr.h>
#include <Kernel/Devices/DiskDevice.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/IRQHandler.h>
#include <Kernel/Lock.h>
#include <Kernel/VM/PhysicalAddress.h>
@ -120,8 +120,9 @@ struct FloppyControllerCommand {
// uses the Intel 82077A controller. More about this controller can
// be found here: http://www.buchty.net/casio/files/82077.pdf
//
class FloppyDiskDevice final : public IRQHandler
, public DiskDevice {
class FloppyDiskDevice final
: public IRQHandler
, public BlockDevice {
AK_MAKE_ETERNAL
static constexpr u8 SECTORS_PER_CYLINDER = 18;
@ -160,8 +161,6 @@ public:
virtual ~FloppyDiskDevice() override;
// ^DiskDevice
virtual bool read_block(unsigned index, u8*) const override;
virtual bool write_block(unsigned index, const u8*) override;
virtual bool read_blocks(unsigned index, u16 count, u8*) override;
virtual bool write_blocks(unsigned index, u16 count, const u8*) override;

View file

@ -29,7 +29,7 @@
#define GPT_DEBUG
GPTPartitionTable::GPTPartitionTable(DiskDevice& device)
GPTPartitionTable::GPTPartitionTable(BlockDevice& device)
: m_device(move(device))
{
}

View file

@ -29,7 +29,6 @@
#include <AK/RefPtr.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <Kernel/Devices/DiskDevice.h>
#include <Kernel/Devices/DiskPartition.h>
#define GPT_SIGNATURE2 0x54524150
@ -73,14 +72,14 @@ struct [[gnu::packed]] GPTPartitionHeader
class GPTPartitionTable {
public:
explicit GPTPartitionTable(DiskDevice&);
explicit GPTPartitionTable(BlockDevice&);
~GPTPartitionTable();
bool initialize();
RefPtr<DiskPartition> partition(unsigned index);
private:
NonnullRefPtr<DiskDevice> m_device;
NonnullRefPtr<BlockDevice> m_device;
const GPTPartitionHeader& header() const;

View file

@ -29,7 +29,7 @@
#define MBR_DEBUG
MBRPartitionTable::MBRPartitionTable(NonnullRefPtr<DiskDevice> device)
MBRPartitionTable::MBRPartitionTable(NonnullRefPtr<BlockDevice> device)
: m_device(move(device))
{
}

View file

@ -28,7 +28,6 @@
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <Kernel/Devices/DiskDevice.h>
#include <Kernel/Devices/DiskPartition.h>
#define MBR_SIGNATURE 0xaa55
@ -62,7 +61,7 @@ class MBRPartitionTable {
AK_MAKE_ETERNAL
public:
explicit MBRPartitionTable(NonnullRefPtr<DiskDevice>);
explicit MBRPartitionTable(NonnullRefPtr<BlockDevice>);
~MBRPartitionTable();
bool initialize();
@ -71,7 +70,7 @@ public:
RefPtr<DiskPartition> partition(unsigned index);
private:
NonnullRefPtr<DiskDevice> m_device;
NonnullRefPtr<BlockDevice> m_device;
const MBRPartitionHeader& header() const;

View file

@ -47,6 +47,8 @@ private:
virtual bool can_write(const FileDescription&) const override { return true; }
virtual ssize_t read(FileDescription&, u8*, ssize_t) override { return -EINVAL; }
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override { return -EINVAL; }
virtual bool read_blocks(unsigned, u16, u8*) override { return false; }
virtual bool write_blocks(unsigned, u16, const u8*) override { return false; }
size_t framebuffer_size_in_bytes() const { return m_framebuffer_pitch * m_framebuffer_height; }

View file

@ -33,7 +33,7 @@ NonnullRefPtr<PATADiskDevice> PATADiskDevice::create(PATAChannel& channel, Drive
}
PATADiskDevice::PATADiskDevice(PATAChannel& channel, DriveType type, int major, int minor)
: DiskDevice(major, minor)
: BlockDevice(major, minor, 512)
, m_drive_type(type)
, m_channel(channel)
{
@ -55,11 +55,6 @@ bool PATADiskDevice::read_blocks(unsigned index, u16 count, u8* out)
return read_sectors(index, count, out);
}
bool PATADiskDevice::read_block(unsigned index, u8* out) const
{
return const_cast<PATADiskDevice*>(this)->read_blocks(index, 1, out);
}
bool PATADiskDevice::write_blocks(unsigned index, u16 count, const u8* data)
{
if (m_channel.m_bus_master_base && m_channel.m_dma_enabled.resource())
@ -71,11 +66,6 @@ bool PATADiskDevice::write_blocks(unsigned index, u16 count, const u8* data)
return true;
}
bool PATADiskDevice::write_block(unsigned index, const u8* data)
{
return write_blocks(index, 1, data);
}
void PATADiskDevice::set_drive_geometry(u16 cyls, u16 heads, u16 spt)
{
m_cylinders = cyls;

View file

@ -30,13 +30,13 @@
//
#pragma once
#include <Kernel/Devices/DiskDevice.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/IRQHandler.h>
#include <Kernel/Lock.h>
class PATAChannel;
class PATADiskDevice final : public DiskDevice {
class PATADiskDevice final : public BlockDevice {
AK_MAKE_ETERNAL
public:
// Type of drive this IDEDiskDevice is on the ATA channel.
@ -53,8 +53,6 @@ public:
virtual ~PATADiskDevice() override;
// ^DiskDevice
virtual bool read_block(unsigned index, u8*) const override;
virtual bool write_block(unsigned index, const u8*) override;
virtual bool read_blocks(unsigned index, u16 count, u8*) override;
virtual bool write_blocks(unsigned index, u16 count, const u8*) override;