MBRPartitionTable.cpp 4.9 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/ByteBuffer.h>
  27. #include <Kernel/Debug.h>
  28. #include <Kernel/Storage/Partition/MBRPartitionTable.h>
  29. namespace Kernel {
  30. #define MBR_SIGNATURE 0xaa55
  31. #define MBR_PROTECTIVE 0xEE
  32. #define EBR_CHS_CONTAINER 0x05
  33. #define EBR_LBA_CONTAINER 0x0F
  34. Result<NonnullOwnPtr<MBRPartitionTable>, PartitionTable::Error> MBRPartitionTable::try_to_initialize(const StorageDevice& device)
  35. {
  36. auto table = make<MBRPartitionTable>(device);
  37. if (table->contains_ebr())
  38. return { PartitionTable::Error::ConatinsEBR };
  39. if (table->is_protective_mbr())
  40. return { PartitionTable::Error::MBRProtective };
  41. if (!table->is_valid())
  42. return { PartitionTable::Error::Invalid };
  43. return table;
  44. }
  45. OwnPtr<MBRPartitionTable> MBRPartitionTable::try_to_initialize(const StorageDevice& device, u32 start_lba)
  46. {
  47. auto table = make<MBRPartitionTable>(device, start_lba);
  48. if (!table->is_valid())
  49. return {};
  50. return table;
  51. }
  52. bool MBRPartitionTable::read_boot_record()
  53. {
  54. auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data());
  55. if (!m_device->read_block(m_start_lba, buffer))
  56. return false;
  57. m_header_valid = true;
  58. return m_header_valid;
  59. }
  60. MBRPartitionTable::MBRPartitionTable(const StorageDevice& device, u32 start_lba)
  61. : PartitionTable(device)
  62. , m_start_lba(start_lba)
  63. , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()))
  64. {
  65. if (!read_boot_record() || !initialize())
  66. return;
  67. m_header_valid = true;
  68. auto& header = this->header();
  69. for (size_t index = 0; index < 4; index++) {
  70. auto& entry = header.entry[index];
  71. if (entry.offset == 0x00) {
  72. continue;
  73. }
  74. auto partition_type = ByteBuffer::create_zeroed(sizeof(u8));
  75. partition_type.data()[0] = entry.type;
  76. m_partitions.append(DiskPartitionMetadata({ entry.offset, (entry.offset + entry.length), partition_type }));
  77. }
  78. m_valid = true;
  79. }
  80. MBRPartitionTable::MBRPartitionTable(const StorageDevice& device)
  81. : PartitionTable(device)
  82. , m_start_lba(0)
  83. , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()))
  84. {
  85. if (!read_boot_record() || contains_ebr() || is_protective_mbr() || !initialize())
  86. return;
  87. auto& header = this->header();
  88. for (size_t index = 0; index < 4; index++) {
  89. auto& entry = header.entry[index];
  90. if (entry.offset == 0x00) {
  91. continue;
  92. }
  93. auto partition_type = ByteBuffer::create_zeroed(sizeof(u8));
  94. partition_type.data()[0] = entry.type;
  95. m_partitions.append(DiskPartitionMetadata({ entry.offset, (entry.offset + entry.length), partition_type }));
  96. }
  97. m_valid = true;
  98. }
  99. MBRPartitionTable::~MBRPartitionTable()
  100. {
  101. }
  102. const MBRPartitionTable::Header& MBRPartitionTable::header() const
  103. {
  104. return *(const MBRPartitionTable::Header*)m_cached_header.data();
  105. }
  106. bool MBRPartitionTable::initialize()
  107. {
  108. auto& header = this->header();
  109. #if MBR_DEBUG
  110. klog() << "Master Boot Record: mbr_signature=0x" << String::format("%x", header.mbr_signature);
  111. #endif
  112. if (header.mbr_signature != MBR_SIGNATURE) {
  113. klog() << "Master Boot Record: invalid signature";
  114. return false;
  115. }
  116. return true;
  117. }
  118. bool MBRPartitionTable::contains_ebr() const
  119. {
  120. for (int i = 0; i < 4; i++) {
  121. if (header().entry[i].type == EBR_CHS_CONTAINER || header().entry[i].type == EBR_LBA_CONTAINER)
  122. return true;
  123. }
  124. return false;
  125. }
  126. bool MBRPartitionTable::is_protective_mbr() const
  127. {
  128. return header().entry[0].type == MBR_PROTECTIVE;
  129. }
  130. }