GUIDPartitionTable.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/AllOf.h>
  27. #include <AK/Array.h>
  28. #include <Kernel/Storage/Partition/GUIDPartitionTable.h>
  29. #ifndef GPT_DEBUG
  30. # define GPT_DEBUG
  31. #endif
  32. namespace Kernel {
  33. #define GPT_SIGNATURE2 0x54524150
  34. #define GPT_SIGNATURE 0x20494645
  35. #define BytesPerSector 512
  36. struct [[gnu::packed]] GPTPartitionEntry {
  37. u8 partition_guid[16];
  38. u8 unique_guid[16];
  39. u64 first_lba;
  40. u64 last_lba;
  41. u64 attributes;
  42. char partition_name[72];
  43. };
  44. struct [[gnu::packed]] GUIDPartitionHeader {
  45. u32 sig[2];
  46. u32 revision;
  47. u32 header_size;
  48. u32 crc32_header;
  49. u32 reserved;
  50. u64 current_lba;
  51. u64 backup_lba;
  52. u64 first_usable_lba;
  53. u64 last_usable_lba;
  54. u64 disk_guid1[2];
  55. u64 partition_array_start_lba;
  56. u32 entries_count;
  57. u32 partition_entry_size;
  58. u32 crc32_entries_array;
  59. };
  60. Result<NonnullOwnPtr<GUIDPartitionTable>, PartitionTable::Error> GUIDPartitionTable::try_to_initialize(const StorageDevice& device)
  61. {
  62. auto table = make<GUIDPartitionTable>(device);
  63. if (!table->is_valid())
  64. return { PartitionTable::Error::Invalid };
  65. return table;
  66. }
  67. GUIDPartitionTable::GUIDPartitionTable(const StorageDevice& device)
  68. : MBRPartitionTable(device)
  69. {
  70. m_cached_header = ByteBuffer::create_zeroed(m_device->block_size());
  71. ASSERT(partitions_count() == 0);
  72. if (!initialize())
  73. m_valid = false;
  74. }
  75. const GUIDPartitionHeader& GUIDPartitionTable::header() const
  76. {
  77. return *(const GUIDPartitionHeader*)m_cached_header.data();
  78. }
  79. bool GUIDPartitionTable::initialize()
  80. {
  81. ASSERT(m_cached_header.data() != nullptr);
  82. auto first_gpt_block = (m_device->block_size() == 512) ? 1 : 0;
  83. auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data());
  84. if (!m_device->read_block(first_gpt_block, buffer)) {
  85. return false;
  86. }
  87. #ifdef GPT_DEBUG
  88. klog() << "GUIDPartitionTable: signature - 0x" << String::format("%x", header().sig[1]) << String::format("%x", header().sig[0]);
  89. #endif
  90. if (header().sig[0] != GPT_SIGNATURE && header().sig[1] != GPT_SIGNATURE2) {
  91. klog() << "GUIDPartitionTable: bad signature 0x" << String::format("%x", header().sig[1]) << String::format("%x", header().sig[0]);
  92. return false;
  93. }
  94. auto entries_buffer = ByteBuffer::create_zeroed(m_device->block_size());
  95. auto raw_entries_buffer = UserOrKernelBuffer::for_kernel_buffer(entries_buffer.data());
  96. size_t raw_byte_index = header().partition_array_start_lba * m_device->block_size();
  97. for (size_t entry_index = 0; entry_index < header().entries_count; entry_index++) {
  98. if (!m_device->read_block((raw_byte_index / m_device->block_size()), raw_entries_buffer)) {
  99. return false;
  100. }
  101. auto* entries = (const GPTPartitionEntry*)entries_buffer.data();
  102. auto& entry = entries[entry_index % (m_device->block_size() / (size_t)header().partition_entry_size)];
  103. Array<u8, 16> partition_type {};
  104. partition_type.span().overwrite(0, entry.partition_guid, partition_type.size());
  105. if (is_unused_entry(partition_type)) {
  106. raw_byte_index += header().partition_entry_size;
  107. continue;
  108. }
  109. Array<u8, 16> unique_guid {};
  110. unique_guid.span().overwrite(0, entry.unique_guid, unique_guid.size());
  111. String name = entry.partition_name;
  112. dbgln("Detected GPT partition (entry={}), offset={}, limit={}", entry_index, entry.first_lba, entry.last_lba);
  113. m_partitions.append({ entry.first_lba, entry.last_lba, partition_type, unique_guid, entry.attributes, "" });
  114. raw_byte_index += header().partition_entry_size;
  115. }
  116. return true;
  117. }
  118. bool GUIDPartitionTable::is_unused_entry(Array<u8, 16> partition_type) const
  119. {
  120. return all_of(partition_type.begin(), partition_type.end(), [](const auto octet) { return octet == 0; });
  121. }
  122. }