EBRPartitionTable.cpp 3.1 KB

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