1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 13:58:11 +00:00

LibPartition: Make MBRPartitionTable kernel/userland agnostic

This commit is contained in:
Samuel Bowman 2022-03-20 20:35:10 -04:00 committed by Linus Groh
parent 6a1c85aa61
commit 7e45c3b687
2 changed files with 37 additions and 2 deletions

View file

@ -14,9 +14,15 @@ namespace Partition {
#define EBR_CHS_CONTAINER 0x05 #define EBR_CHS_CONTAINER 0x05
#define EBR_LBA_CONTAINER 0x0F #define EBR_LBA_CONTAINER 0x0F
#ifdef KERNEL
ErrorOr<NonnullOwnPtr<MBRPartitionTable>> MBRPartitionTable::try_to_initialize(Kernel::StorageDevice const& device) ErrorOr<NonnullOwnPtr<MBRPartitionTable>> MBRPartitionTable::try_to_initialize(Kernel::StorageDevice const& device)
{ {
auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(device))); auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(device)));
#else
ErrorOr<NonnullOwnPtr<MBRPartitionTable>> MBRPartitionTable::try_to_initialize(NonnullRefPtr<Core::File> device_file)
{
auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(move(device_file))));
#endif
if (table->contains_ebr()) if (table->contains_ebr())
return Error::from_errno(ENOTSUP); return Error::from_errno(ENOTSUP);
if (table->is_protective_mbr()) if (table->is_protective_mbr())
@ -26,9 +32,15 @@ ErrorOr<NonnullOwnPtr<MBRPartitionTable>> MBRPartitionTable::try_to_initialize(K
return table; return table;
} }
#ifdef KERNEL
OwnPtr<MBRPartitionTable> MBRPartitionTable::try_to_initialize(Kernel::StorageDevice const& device, u32 start_lba) OwnPtr<MBRPartitionTable> 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(); auto table = adopt_nonnull_own_or_enomem(new (nothrow) MBRPartitionTable(device, start_lba)).release_value_but_fixme_should_propagate_errors();
#else
OwnPtr<MBRPartitionTable> MBRPartitionTable::try_to_initialize(NonnullRefPtr<Core::File> 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()) if (!table->is_valid())
return {}; return {};
return table; return table;
@ -36,17 +48,28 @@ OwnPtr<MBRPartitionTable> MBRPartitionTable::try_to_initialize(Kernel::StorageDe
bool MBRPartitionTable::read_boot_record() bool MBRPartitionTable::read_boot_record()
{ {
#ifdef KERNEL
auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data()); auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data());
if (!m_device->read_block(m_start_lba, buffer)) if (!m_device->read_block(m_start_lba, buffer))
return false; 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; m_header_valid = true;
return m_header_valid; return m_header_valid;
} }
#ifdef KERNEL
MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device, u32 start_lba) MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device, u32 start_lba)
: PartitionTable(device) : PartitionTable(device)
#else
MBRPartitionTable::MBRPartitionTable(NonnullRefPtr<Core::File> device_file, u32 start_lba)
: PartitionTable(move(device_file))
#endif
, m_start_lba(start_lba) , 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()) if (!read_boot_record() || !initialize())
return; return;
@ -64,10 +87,15 @@ MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device, u32 st
m_valid = true; m_valid = true;
} }
#ifdef KERNEL
MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device) MBRPartitionTable::MBRPartitionTable(Kernel::StorageDevice const& device)
: PartitionTable(device) : PartitionTable(device)
#else
MBRPartitionTable::MBRPartitionTable(NonnullRefPtr<Core::File> device_file)
: PartitionTable(move(device_file))
#endif
, m_start_lba(0) , 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()) if (!read_boot_record() || contains_ebr() || is_protective_mbr() || !initialize())
return; return;

View file

@ -38,10 +38,17 @@ public:
public: public:
~MBRPartitionTable(); ~MBRPartitionTable();
#ifdef KERNEL
static ErrorOr<NonnullOwnPtr<MBRPartitionTable>> try_to_initialize(Kernel::StorageDevice const&); static ErrorOr<NonnullOwnPtr<MBRPartitionTable>> try_to_initialize(Kernel::StorageDevice const&);
static OwnPtr<MBRPartitionTable> try_to_initialize(Kernel::StorageDevice const&, u32 start_lba); static OwnPtr<MBRPartitionTable> try_to_initialize(Kernel::StorageDevice const&, u32 start_lba);
explicit MBRPartitionTable(Kernel::StorageDevice const&); explicit MBRPartitionTable(Kernel::StorageDevice const&);
MBRPartitionTable(Kernel::StorageDevice const&, u32 start_lba); MBRPartitionTable(Kernel::StorageDevice const&, u32 start_lba);
#else
static ErrorOr<NonnullOwnPtr<MBRPartitionTable>> try_to_initialize(NonnullRefPtr<Core::File>);
static OwnPtr<MBRPartitionTable> try_to_initialize(NonnullRefPtr<Core::File>, u32 start_lba);
explicit MBRPartitionTable(NonnullRefPtr<Core::File>);
MBRPartitionTable(NonnullRefPtr<Core::File>, u32 start_lba);
#endif
bool is_protective_mbr() const; bool is_protective_mbr() const;
bool contains_ebr() const; bool contains_ebr() const;