Selaa lähdekoodia

LibPartition: Make MBRPartitionTable kernel/userland agnostic

Samuel Bowman 3 vuotta sitten
vanhempi
commit
7e45c3b687

+ 30 - 2
Userland/Libraries/LibPartition/MBRPartitionTable.cpp

@@ -14,9 +14,15 @@ namespace Partition {
 #define EBR_CHS_CONTAINER 0x05
 #define EBR_LBA_CONTAINER 0x0F
 
+#ifdef KERNEL
 ErrorOr<NonnullOwnPtr<MBRPartitionTable>> MBRPartitionTable::try_to_initialize(Kernel::StorageDevice const& 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())
         return Error::from_errno(ENOTSUP);
     if (table->is_protective_mbr())
@@ -26,9 +32,15 @@ ErrorOr<NonnullOwnPtr<MBRPartitionTable>> MBRPartitionTable::try_to_initialize(K
     return table;
 }
 
+#ifdef KERNEL
 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();
+#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())
         return {};
     return table;
@@ -36,17 +48,28 @@ OwnPtr<MBRPartitionTable> 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<Core::File> 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<Core::File> 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;

+ 7 - 0
Userland/Libraries/LibPartition/MBRPartitionTable.h

@@ -38,10 +38,17 @@ public:
 public:
     ~MBRPartitionTable();
 
+#ifdef KERNEL
     static ErrorOr<NonnullOwnPtr<MBRPartitionTable>> try_to_initialize(Kernel::StorageDevice const&);
     static OwnPtr<MBRPartitionTable> try_to_initialize(Kernel::StorageDevice const&, u32 start_lba);
     explicit MBRPartitionTable(Kernel::StorageDevice const&);
     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 contains_ebr() const;