GPTPartitionTable.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/ByteBuffer.h>
  27. #include <Kernel/Devices/GPTPartitionTable.h>
  28. #ifndef GPT_DEBUG
  29. # define GPT_DEBUG
  30. #endif
  31. namespace Kernel {
  32. GPTPartitionTable::GPTPartitionTable(BlockDevice& device)
  33. : m_device(move(device))
  34. {
  35. }
  36. GPTPartitionTable::~GPTPartitionTable()
  37. {
  38. }
  39. const GPTPartitionHeader& GPTPartitionTable::header() const
  40. {
  41. return *reinterpret_cast<const GPTPartitionHeader*>(m_cached_header);
  42. }
  43. bool GPTPartitionTable::initialize()
  44. {
  45. auto header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header);
  46. if (!m_device->read_block(1, header_buffer)) {
  47. return false;
  48. }
  49. auto& header = this->header();
  50. #ifdef GPT_DEBUG
  51. klog() << "GPTPartitionTable::initialize: gpt_signature=0x" << String::format("%x", header.sig[1]) << String::format("%x", header.sig[0]);
  52. #endif
  53. if (header.sig[0] != GPT_SIGNATURE && header.sig[1] != GPT_SIGNATURE2) {
  54. klog() << "GPTPartitionTable::initialize: bad GPT signature 0x" << String::format("%x", header.sig[1]) << String::format("%x", header.sig[0]);
  55. return false;
  56. }
  57. return true;
  58. }
  59. RefPtr<DiskPartition> GPTPartitionTable::partition(unsigned index)
  60. {
  61. ASSERT(index >= 1 && index <= 4294967294);
  62. auto& header = this->header();
  63. unsigned lba = header.partition_array_start_lba + (((index - 1) * header.partition_entry_size) / BytesPerSector);
  64. if (header.sig[0] != GPT_SIGNATURE) {
  65. klog() << "GPTPartitionTable::initialize: bad gpt signature - not initialized? 0x" << String::format("%x", header.sig);
  66. return nullptr;
  67. }
  68. u8 entries_per_sector = BytesPerSector / header.partition_entry_size;
  69. GPTPartitionEntry entries[entries_per_sector];
  70. auto entries_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&entries);
  71. this->m_device->read_blocks(lba, 1, entries_buffer);
  72. GPTPartitionEntry& entry = entries[((index - 1) % entries_per_sector)];
  73. #ifdef GPT_DEBUG
  74. klog() << "GPTPartitionTable::partition " << index;
  75. klog() << "GPTPartitionTable - offset = " << entry.first_lba[1] << entry.first_lba[0];
  76. #endif
  77. if (entry.first_lba[0] == 0x00) {
  78. #ifdef GPT_DEBUG
  79. klog() << "GPTPartitionTable::partition: missing partition requested index=" << index;
  80. #endif
  81. return nullptr;
  82. }
  83. #ifdef GPT_DEBUG
  84. klog() << "GPTPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.partition_guid[3]) << "-" << String::format("%x", entry.partition_guid[2]) << "-" << String::format("%x", entry.partition_guid[1]) << "-" << String::format("%x", entry.partition_guid[0]);
  85. #endif
  86. return DiskPartition::create(m_device, entry.first_lba[0], entry.last_lba[0]);
  87. }
  88. }