GUIDPartitionTable.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibPartition/GUIDPartitionTable.h>
  8. namespace Partition {
  9. #define GPT_SIGNATURE2 0x54524150
  10. #define GPT_SIGNATURE 0x20494645
  11. struct [[gnu::packed]] GPTPartitionEntry {
  12. u8 partition_guid[16];
  13. u8 unique_guid[16];
  14. u64 first_lba;
  15. u64 last_lba;
  16. u64 attributes;
  17. char partition_name[72];
  18. };
  19. struct [[gnu::packed]] GUIDPartitionHeader {
  20. u32 sig[2];
  21. u32 revision;
  22. u32 header_size;
  23. u32 crc32_header;
  24. u32 reserved;
  25. u64 current_lba;
  26. u64 backup_lba;
  27. u64 first_usable_lba;
  28. u64 last_usable_lba;
  29. u64 disk_guid1[2];
  30. u64 partition_array_start_lba;
  31. u32 entries_count;
  32. u32 partition_entry_size;
  33. u32 crc32_entries_array;
  34. };
  35. ErrorOr<NonnullOwnPtr<GUIDPartitionTable>> GUIDPartitionTable::try_to_initialize(PartitionableDevice device)
  36. {
  37. auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GUIDPartitionTable(move(device))));
  38. if (!table->is_valid())
  39. return Error::from_errno(EINVAL);
  40. return table;
  41. }
  42. GUIDPartitionTable::GUIDPartitionTable(PartitionableDevice device)
  43. : MBRPartitionTable(move(device))
  44. {
  45. // FIXME: Handle OOM failure here.
  46. m_cached_header = ByteBuffer::create_zeroed(block_size()).release_value_but_fixme_should_propagate_errors();
  47. VERIFY(partitions_count() == 0);
  48. if (!initialize())
  49. m_valid = false;
  50. }
  51. GUIDPartitionHeader const& GUIDPartitionTable::header() const
  52. {
  53. return *(GUIDPartitionHeader const*)m_cached_header.data();
  54. }
  55. bool GUIDPartitionTable::initialize()
  56. {
  57. VERIFY(m_cached_header.data() != nullptr);
  58. auto first_gpt_block = (block_size() == 512) ? 1 : 0;
  59. auto maybe_error = m_device.read_block(first_gpt_block, m_cached_header.bytes());
  60. if (maybe_error.is_error())
  61. return false;
  62. dbgln_if(GPT_DEBUG, "GUIDPartitionTable: signature - {:#08x} {:#08x}", header().sig[1], header().sig[0]);
  63. if (header().sig[0] != GPT_SIGNATURE && header().sig[1] != GPT_SIGNATURE2) {
  64. dbgln("GUIDPartitionTable: bad signature {:#08x} {:#08x}", header().sig[1], header().sig[0]);
  65. return false;
  66. }
  67. auto entries_buffer_result = ByteBuffer::create_zeroed(block_size());
  68. if (entries_buffer_result.is_error()) {
  69. dbgln("GUIDPartitionTable: not enough memory for entries buffer");
  70. return false;
  71. }
  72. auto entries_buffer = entries_buffer_result.release_value();
  73. size_t raw_byte_index = header().partition_array_start_lba * block_size();
  74. for (size_t entry_index = 0; entry_index < header().entries_count; entry_index++) {
  75. maybe_error = m_device.read_block(raw_byte_index / block_size(), entries_buffer.bytes());
  76. if (maybe_error.is_error())
  77. return false;
  78. auto* entries = (GPTPartitionEntry const*)entries_buffer.data();
  79. auto& entry = entries[entry_index % (block_size() / header().partition_entry_size)];
  80. Array<u8, 16> partition_type {};
  81. partition_type.span().overwrite(0, entry.partition_guid, partition_type.size());
  82. if (is_unused_entry(partition_type)) {
  83. raw_byte_index += header().partition_entry_size;
  84. continue;
  85. }
  86. Array<u8, 16> unique_guid {};
  87. unique_guid.span().overwrite(0, entry.unique_guid, unique_guid.size());
  88. dbgln("Detected GPT partition (entry={}), offset={}, limit={}", entry_index, entry.first_lba, entry.last_lba);
  89. m_partitions.append({ entry.first_lba, entry.last_lba, partition_type, unique_guid, entry.attributes });
  90. raw_byte_index += header().partition_entry_size;
  91. }
  92. return true;
  93. }
  94. bool GUIDPartitionTable::is_unused_entry(Array<u8, 16> partition_type) const
  95. {
  96. return all_of(partition_type, [](auto const octet) { return octet == 0; });
  97. }
  98. }