GUIDPartitionTable.cpp 4.0 KB

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