EBRPartitionTable.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPartition/EBRPartitionTable.h>
  7. namespace Partition {
  8. #ifdef KERNEL
  9. ErrorOr<NonnullOwnPtr<EBRPartitionTable>> EBRPartitionTable::try_to_initialize(Kernel::StorageDevice const& device)
  10. {
  11. auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) EBRPartitionTable(device)));
  12. #else
  13. ErrorOr<NonnullOwnPtr<EBRPartitionTable>> EBRPartitionTable::try_to_initialize(NonnullRefPtr<Core::File> device_file)
  14. {
  15. auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) EBRPartitionTable(move(device_file))));
  16. #endif
  17. if (table->is_protective_mbr())
  18. return Error::from_errno(ENOTSUP);
  19. if (!table->is_valid())
  20. return Error::from_errno(EINVAL);
  21. return table;
  22. }
  23. #ifdef KERNEL
  24. void EBRPartitionTable::search_extended_partition(Kernel::StorageDevice const& device, MBRPartitionTable& checked_ebr, u64 current_block_offset, size_t limit)
  25. #else
  26. void EBRPartitionTable::search_extended_partition(NonnullRefPtr<Core::File> device, MBRPartitionTable& checked_ebr, u64 current_block_offset, size_t limit)
  27. #endif
  28. {
  29. if (limit == 0)
  30. return;
  31. // EBRs should not carry more than 2 partitions (because they need to form a linked list)
  32. VERIFY(checked_ebr.partitions_count() <= 2);
  33. auto checked_logical_partition = checked_ebr.partition(0);
  34. // If we are pointed to an invalid logical partition, something is seriously wrong.
  35. VERIFY(checked_logical_partition.has_value());
  36. m_partitions.append(checked_logical_partition.value().offset(current_block_offset));
  37. if (!checked_ebr.contains_ebr())
  38. return;
  39. current_block_offset += checked_ebr.partition(1).value().start_block();
  40. auto next_ebr = MBRPartitionTable::try_to_initialize(device, current_block_offset);
  41. if (!next_ebr)
  42. return;
  43. search_extended_partition(device, *next_ebr, current_block_offset, (limit - 1));
  44. }
  45. #ifdef KERNEL
  46. EBRPartitionTable::EBRPartitionTable(Kernel::StorageDevice const& device)
  47. #else
  48. EBRPartitionTable::EBRPartitionTable(NonnullRefPtr<Core::File> device)
  49. #endif
  50. : MBRPartitionTable(device)
  51. {
  52. if (!is_header_valid())
  53. return;
  54. m_valid = true;
  55. VERIFY(partitions_count() == 0);
  56. auto& header = this->header();
  57. for (size_t index = 0; index < 4; index++) {
  58. auto& entry = header.entry[index];
  59. // Start enumerating all logical partitions
  60. if (entry.type == 0xf) {
  61. auto checked_ebr = MBRPartitionTable::try_to_initialize(device, entry.offset);
  62. if (!checked_ebr)
  63. continue;
  64. // It's quite unlikely to see that amount of partitions, so stop at 128 partitions.
  65. search_extended_partition(device, *checked_ebr, entry.offset, 128);
  66. continue;
  67. }
  68. if (entry.offset == 0x00) {
  69. continue;
  70. }
  71. MUST(m_partitions.try_empend(entry.offset, (entry.offset + entry.length), entry.type));
  72. }
  73. }
  74. EBRPartitionTable::~EBRPartitionTable() = default;
  75. }