diff options
author | Samuel Bowman <sam@sambowman.tech> | 2022-02-12 14:21:28 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-21 20:13:44 +0100 |
commit | be1c5c6b9f2d69074e076efdee4816dd22a2ad70 (patch) | |
tree | 74cb9e632826fd48898355f87ed7df57089c7d87 /Kernel | |
parent | 27861477740873c74f660cbfe8862e634c02861f (diff) | |
download | serenity-be1c5c6b9f2d69074e076efdee4816dd22a2ad70.zip |
Kernel+LibPartition: Move DiskPartitionMetadata into LibPartition
This commit creates a new library LibPartition which will contain
partition related code sharable between Kernel and Userland and
includes DiskPartitionMetadata as the first shared class.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/CMakeLists.txt | 6 | ||||
-rw-r--r-- | Kernel/Storage/Partition/DiskPartition.cpp | 6 | ||||
-rw-r--r-- | Kernel/Storage/Partition/DiskPartition.h | 10 | ||||
-rw-r--r-- | Kernel/Storage/Partition/DiskPartitionMetadata.cpp | 99 | ||||
-rw-r--r-- | Kernel/Storage/Partition/DiskPartitionMetadata.h | 54 | ||||
-rw-r--r-- | Kernel/Storage/Partition/PartitionTable.cpp | 2 | ||||
-rw-r--r-- | Kernel/Storage/Partition/PartitionTable.h | 8 |
7 files changed, 18 insertions, 167 deletions
diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index a61aa1282d..9e3a6991da 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -104,7 +104,6 @@ set(KERNEL_SOURCES Storage/ATA/ATAPIDiscDevice.cpp Storage/ATA/ATAPort.cpp Storage/Partition/DiskPartition.cpp - Storage/Partition/DiskPartitionMetadata.cpp Storage/Partition/EBRPartitionTable.cpp Storage/Partition/GUIDPartitionTable.cpp Storage/Partition/MBRPartitionTable.cpp @@ -405,6 +404,10 @@ set(CRYPTO_SOURCES ../Userland/Libraries/LibCrypto/Hash/SHA2.cpp ) +set(PARTITION_SOURCES + ../Userland/Libraries/LibPartition/DiskPartitionMetadata.cpp +) + set(SOURCES ${AK_SOURCES} ) @@ -416,6 +419,7 @@ if (NOT "${SERENITY_ARCH}" STREQUAL "aarch64") ${ELF_SOURCES} ${VT_SOURCES} ${CRYPTO_SOURCES} + ${PARTITION_SOURCES} ) else() set(RPI_SOURCES diff --git a/Kernel/Storage/Partition/DiskPartition.cpp b/Kernel/Storage/Partition/DiskPartition.cpp index 4bf2a5eca1..185f4f52f3 100644 --- a/Kernel/Storage/Partition/DiskPartition.cpp +++ b/Kernel/Storage/Partition/DiskPartition.cpp @@ -11,7 +11,7 @@ namespace Kernel { -NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata) +NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned minor_number, Partition::DiskPartitionMetadata metadata) { auto partition_or_error = DeviceManagement::try_create_device<DiskPartition>(device, minor_number, metadata); // FIXME: Find a way to propagate errors @@ -19,7 +19,7 @@ NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned return partition_or_error.release_value(); } -DiskPartition::DiskPartition(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata) +DiskPartition::DiskPartition(BlockDevice& device, unsigned minor_number, Partition::DiskPartitionMetadata metadata) : BlockDevice(100, minor_number, device.block_size()) , m_device(device) , m_metadata(metadata) @@ -28,7 +28,7 @@ DiskPartition::DiskPartition(BlockDevice& device, unsigned minor_number, DiskPar DiskPartition::~DiskPartition() = default; -DiskPartitionMetadata const& DiskPartition::metadata() const +Partition::DiskPartitionMetadata const& DiskPartition::metadata() const { return m_metadata; } diff --git a/Kernel/Storage/Partition/DiskPartition.h b/Kernel/Storage/Partition/DiskPartition.h index a6a17797a9..f67d2cb32f 100644 --- a/Kernel/Storage/Partition/DiskPartition.h +++ b/Kernel/Storage/Partition/DiskPartition.h @@ -9,7 +9,7 @@ #include <AK/RefPtr.h> #include <AK/WeakPtr.h> #include <Kernel/Devices/BlockDevice.h> -#include <Kernel/Storage/Partition/DiskPartitionMetadata.h> +#include <LibPartition/DiskPartitionMetadata.h> namespace Kernel { @@ -17,7 +17,7 @@ class DiskPartition final : public BlockDevice { friend class DeviceManagement; public: - static NonnullRefPtr<DiskPartition> create(BlockDevice&, unsigned, DiskPartitionMetadata); + static NonnullRefPtr<DiskPartition> create(BlockDevice&, unsigned, Partition::DiskPartitionMetadata); virtual ~DiskPartition(); virtual void start_request(AsyncBlockDeviceRequest&) override; @@ -28,14 +28,14 @@ public: virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override; virtual bool can_write(OpenFileDescription const&, u64) const override; - DiskPartitionMetadata const& metadata() const; + Partition::DiskPartitionMetadata const& metadata() const; private: - DiskPartition(BlockDevice&, unsigned, DiskPartitionMetadata); + DiskPartition(BlockDevice&, unsigned, Partition::DiskPartitionMetadata); virtual StringView class_name() const override; WeakPtr<BlockDevice> m_device; - DiskPartitionMetadata m_metadata; + Partition::DiskPartitionMetadata m_metadata; }; } diff --git a/Kernel/Storage/Partition/DiskPartitionMetadata.cpp b/Kernel/Storage/Partition/DiskPartitionMetadata.cpp deleted file mode 100644 index 903f713efd..0000000000 --- a/Kernel/Storage/Partition/DiskPartitionMetadata.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il> - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include <AK/AllOf.h> -#include <Kernel/Storage/Partition/DiskPartitionMetadata.h> - -namespace Kernel { - -DiskPartitionMetadata::PartitionType::PartitionType(u8 partition_type) -{ - m_partition_type[0] = partition_type; -} -DiskPartitionMetadata::PartitionType::PartitionType(Array<u8, 16> partition_type) - : m_partition_type_is_uuid(true) -{ - m_partition_type.span().overwrite(0, partition_type.data(), partition_type.size()); -} -UUID DiskPartitionMetadata::PartitionType::to_uuid() const -{ - VERIFY(is_uuid()); - return m_partition_type; -} -u8 DiskPartitionMetadata::PartitionType::to_byte_indicator() const -{ - VERIFY(!is_uuid()); - return m_partition_type[0]; -} -bool DiskPartitionMetadata::PartitionType::is_uuid() const -{ - return m_partition_type_is_uuid; -} -bool DiskPartitionMetadata::PartitionType::is_valid() const -{ - return !all_of(m_partition_type, [](auto const octet) { return octet == 0; }); -} - -DiskPartitionMetadata::DiskPartitionMetadata(u64 start_block, u64 end_block, u8 partition_type) - : m_start_block(start_block) - , m_end_block(end_block) - , m_type(partition_type) -{ - VERIFY(m_type.is_valid()); -} - -DiskPartitionMetadata::DiskPartitionMetadata(u64 start_block, u64 end_block, Array<u8, 16> partition_type) - : m_start_block(start_block) - , m_end_block(end_block) - , m_type(partition_type) -{ - VERIFY(m_type.is_valid()); -} - -DiskPartitionMetadata::DiskPartitionMetadata(u64 start_block, u64 end_block, Array<u8, 16> partition_type, UUID unique_guid, u64 special_attributes) - : m_start_block(start_block) - , m_end_block(end_block) - , m_type(partition_type) - , m_unique_guid(unique_guid) - , m_attributes(special_attributes) -{ - VERIFY(m_type.is_valid()); - VERIFY(!m_unique_guid.is_zero()); -} - -DiskPartitionMetadata DiskPartitionMetadata::offset(u64 blocks_count) const -{ - return { blocks_count + m_start_block, blocks_count + m_end_block, m_type.m_partition_type }; -} - -u64 DiskPartitionMetadata::start_block() const -{ - return m_start_block; -} - -u64 DiskPartitionMetadata::end_block() const -{ - return m_end_block; -} - -Optional<u64> DiskPartitionMetadata::special_attributes() const -{ - if (m_attributes == 0) - return {}; - return m_attributes; -} - -DiskPartitionMetadata::PartitionType const& DiskPartitionMetadata::type() const -{ - return m_type; -} - -const UUID& DiskPartitionMetadata::unique_guid() const -{ - return m_unique_guid; -} - -} diff --git a/Kernel/Storage/Partition/DiskPartitionMetadata.h b/Kernel/Storage/Partition/DiskPartitionMetadata.h deleted file mode 100644 index 2511b127c0..0000000000 --- a/Kernel/Storage/Partition/DiskPartitionMetadata.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il> - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include <AK/RefPtr.h> -#include <AK/UUID.h> -#include <Kernel/Devices/BlockDevice.h> - -namespace Kernel { - -class DiskPartitionMetadata { -private: - class PartitionType { - friend class DiskPartitionMetadata; - - public: - explicit PartitionType(u8 partition_type); - explicit PartitionType(Array<u8, 16> partition_type); - UUID to_uuid() const; - u8 to_byte_indicator() const; - bool is_uuid() const; - bool is_valid() const; - - private: - Array<u8, 16> m_partition_type {}; - bool m_partition_type_is_uuid { false }; - }; - -public: - DiskPartitionMetadata(u64 block_offset, u64 block_limit, u8 partition_type); - DiskPartitionMetadata(u64 start_block, u64 end_block, Array<u8, 16> partition_type); - DiskPartitionMetadata(u64 block_offset, u64 block_limit, Array<u8, 16> partition_type, UUID unique_guid, u64 special_attributes); - u64 start_block() const; - u64 end_block() const; - - DiskPartitionMetadata offset(u64 blocks_count) const; - - Optional<u64> special_attributes() const; - PartitionType const& type() const; - const UUID& unique_guid() const; - -private: - u64 m_start_block; - u64 m_end_block; - PartitionType m_type; - UUID m_unique_guid {}; - u64 m_attributes { 0 }; -}; - -} diff --git a/Kernel/Storage/Partition/PartitionTable.cpp b/Kernel/Storage/Partition/PartitionTable.cpp index 1731ef63b4..75fe0e56cf 100644 --- a/Kernel/Storage/Partition/PartitionTable.cpp +++ b/Kernel/Storage/Partition/PartitionTable.cpp @@ -12,7 +12,7 @@ PartitionTable::PartitionTable(StorageDevice const& device) { } -Optional<DiskPartitionMetadata> PartitionTable::partition(unsigned index) +Optional<Partition::DiskPartitionMetadata> PartitionTable::partition(unsigned index) { if (index > partitions_count()) return {}; diff --git a/Kernel/Storage/Partition/PartitionTable.h b/Kernel/Storage/Partition/PartitionTable.h index f9f4441f66..e21f5d61ff 100644 --- a/Kernel/Storage/Partition/PartitionTable.h +++ b/Kernel/Storage/Partition/PartitionTable.h @@ -9,25 +9,25 @@ #include <AK/RefPtr.h> #include <AK/Vector.h> #include <Kernel/Storage/Partition/DiskPartition.h> -#include <Kernel/Storage/Partition/DiskPartitionMetadata.h> #include <Kernel/Storage/StorageDevice.h> +#include <LibPartition/DiskPartitionMetadata.h> namespace Kernel { class PartitionTable { public: - Optional<DiskPartitionMetadata> partition(unsigned index); + Optional<Partition::DiskPartitionMetadata> partition(unsigned index); size_t partitions_count() const { return m_partitions.size(); } virtual ~PartitionTable() = default; virtual bool is_valid() const = 0; - Vector<DiskPartitionMetadata> partitions() const { return m_partitions; } + Vector<Partition::DiskPartitionMetadata> partitions() const { return m_partitions; } protected: explicit PartitionTable(StorageDevice const&); NonnullRefPtr<StorageDevice> m_device; - Vector<DiskPartitionMetadata> m_partitions; + Vector<Partition::DiskPartitionMetadata> m_partitions; }; } |