From 7e45c3b6870fda784da32af9abba8dba1bdc59eb Mon Sep 17 00:00:00 2001 From: Samuel Bowman Date: Sun, 20 Mar 2022 20:35:10 -0400 Subject: [PATCH] LibPartition: Make MBRPartitionTable kernel/userland agnostic --- .../LibPartition/MBRPartitionTable.cpp | 32 +++++++++++++++++-- .../LibPartition/MBRPartitionTable.h | 7 ++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibPartition/MBRPartitionTable.cpp b/Userland/Libraries/LibPartition/MBRPartitionTable.cpp index ef6abff930..2504ee1457 100644 --- a/Userland/Libraries/LibPartition/MBRPartitionTable.cpp +++ b/Userland/Libraries/LibPartition/MBRPartitionTable.cpp @@ -14,9 +14,15 @@ namespace Partition { #define EBR_CHS_CONTAINER 0x05 #define EBR_LBA_CONTAINER 0x0F +#ifdef KERNEL ErrorOr> MBRPartitionTable::try_to_initialize(Kernel::StorageDevice const& device) { auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(device))); +#else +ErrorOr> MBRPartitionTable::try_to_initialize(NonnullRefPtr device_file) +{ + auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(move(device_file)))); +#endif if (table->contains_ebr()) return Error::from_errno(ENOTSUP); if (table->is_protective_mbr()) @@ -26,9 +32,15 @@ ErrorOr> MBRPartitionTable::try_to_initialize(K return table; } +#ifdef KERNEL OwnPtr MBRPartitionTable::try_to_initialize(Kernel::StorageDevice const& device, u32 start_lba) { auto table = adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(device, start_lba)).release_value_but_fixme_should_propagate_errors(); +#else +OwnPtr MBRPartitionTable::try_to_initialize(NonnullRefPtr device_file, u32 start_lba) +{ + auto table = adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(move(device_file), start_lba)).release_value_but_fixme_should_propagate_errors(); +#endif if (!table->is_valid()) return {}; return table; @@ -36,17 +48,28 @@ OwnPtr MBRPartitionTable::try_to_initialize(Kernel::StorageDe bool MBRPartitionTable::read_boot_record() { +#ifdef KERNEL auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data()); if (!m_device->read_block(m_start_lba, buffer)) return false; +#else + m_device_file->seek(m_start_lba * m_block_size); + if (m_device_file->read(m_cached_header.data(), m_cached_header.size()) != 512) + return false; +#endif m_header_valid = true; return m_header_valid; } +#ifdef KERNEL MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device, u32 start_lba) : PartitionTable(device) +#else +MBRPartitionTable::MBRPartitionTable(NonnullRefPtr device_file, u32 start_lba) + : PartitionTable(move(device_file)) +#endif , m_start_lba(start_lba) - , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM. + , m_cached_header(ByteBuffer::create_zeroed(m_block_size).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM. { if (!read_boot_record() || !initialize()) return; @@ -64,10 +87,15 @@ MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device, u32 st m_valid = true; } +#ifdef KERNEL MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device) : PartitionTable(device) +#else +MBRPartitionTable::MBRPartitionTable(NonnullRefPtr device_file) + : PartitionTable(move(device_file)) +#endif , m_start_lba(0) - , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM. + , m_cached_header(ByteBuffer::create_zeroed(m_block_size).release_value_but_fixme_should_propagate_errors()) // FIXME: Do something sensible if this fails because of OOM. { if (!read_boot_record() || contains_ebr() || is_protective_mbr() || !initialize()) return; diff --git a/Userland/Libraries/LibPartition/MBRPartitionTable.h b/Userland/Libraries/LibPartition/MBRPartitionTable.h index 454d30117d..62c126fc79 100644 --- a/Userland/Libraries/LibPartition/MBRPartitionTable.h +++ b/Userland/Libraries/LibPartition/MBRPartitionTable.h @@ -38,10 +38,17 @@ public: public: ~MBRPartitionTable(); +#ifdef KERNEL static ErrorOr> try_to_initialize(Kernel::StorageDevice const&); static OwnPtr try_to_initialize(Kernel::StorageDevice const&, u32 start_lba); explicit MBRPartitionTable(Kernel::StorageDevice const&); MBRPartitionTable(Kernel::StorageDevice const&, u32 start_lba); +#else + static ErrorOr> try_to_initialize(NonnullRefPtr); + static OwnPtr try_to_initialize(NonnullRefPtr, u32 start_lba); + explicit MBRPartitionTable(NonnullRefPtr); + MBRPartitionTable(NonnullRefPtr, u32 start_lba); +#endif bool is_protective_mbr() const; bool contains_ebr() const;