GUIDPartitionTable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(Kernel::StorageDevice const& device)
  36. {
  37. auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GUIDPartitionTable(device)));
  38. if (!table->is_valid())
  39. return Error::from_errno(EINVAL);
  40. return table;
  41. }
  42. GUIDPartitionTable::GUIDPartitionTable(Kernel::StorageDevice const& device)
  43. : MBRPartitionTable(device)
  44. {
  45. // FIXME: Handle OOM failure here.
  46. m_cached_header = ByteBuffer::create_zeroed(m_device->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 = (m_device->block_size() == 512) ? 1 : 0;
  59. auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data());
  60. if (!m_device->read_block(first_gpt_block, buffer)) {
  61. return false;
  62. }
  63. dbgln_if(GPT_DEBUG, "GUIDPartitionTable: signature - {:#08x} {:#08x}", header().sig[1], header().sig[0]);
  64. if (header().sig[0] != GPT_SIGNATURE && header().sig[1] != GPT_SIGNATURE2) {
  65. dbgln("GUIDPartitionTable: bad signature {:#08x} {:#08x}", header().sig[1], header().sig[0]);
  66. return false;
  67. }
  68. auto entries_buffer_result = ByteBuffer::create_zeroed(m_device->block_size());
  69. if (entries_buffer_result.is_error()) {
  70. dbgln("GUIPartitionTable: not enough memory for entries buffer");
  71. return false;
  72. }
  73. auto entries_buffer = entries_buffer_result.release_value();
  74. auto raw_entries_buffer = UserOrKernelBuffer::for_kernel_buffer(entries_buffer.data());
  75. size_t raw_byte_index = header().partition_array_start_lba * m_device->block_size();
  76. for (size_t entry_index = 0; entry_index < header().entries_count; entry_index++) {
  77. if (!m_device->read_block((raw_byte_index / m_device->block_size()), raw_entries_buffer)) {
  78. return false;
  79. }
  80. auto* entries = (GPTPartitionEntry const*)entries_buffer.data();
  81. auto& entry = entries[entry_index % (m_device->block_size() / (size_t)header().partition_entry_size)];
  82. Array<u8, 16> partition_type {};
  83. partition_type.span().overwrite(0, entry.partition_guid, partition_type.size());
  84. if (is_unused_entry(partition_type)) {
  85. raw_byte_index += header().partition_entry_size;
  86. continue;
  87. }
  88. Array<u8, 16> unique_guid {};
  89. unique_guid.span().overwrite(0, entry.unique_guid, unique_guid.size());
  90. dbgln("Detected GPT partition (entry={}), offset={}, limit={}", entry_index, entry.first_lba, entry.last_lba);
  91. m_partitions.append({ entry.first_lba, entry.last_lba, partition_type, unique_guid, entry.attributes });
  92. raw_byte_index += header().partition_entry_size;
  93. }
  94. return true;
  95. }
  96. bool GUIDPartitionTable::is_unused_entry(Array<u8, 16> partition_type) const
  97. {
  98. return all_of(partition_type, [](auto const octet) { return octet == 0; });
  99. }
  100. }