EBRPartitionTable.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/Devices/EBRPartitionTable.h>
  28. #ifndef EBR_DEBUG
  29. # define EBR_DEBUG
  30. #endif
  31. namespace Kernel {
  32. EBRPartitionTable::EBRPartitionTable(NonnullRefPtr<BlockDevice> device)
  33. : m_device(move(device))
  34. {
  35. }
  36. EBRPartitionTable::~EBRPartitionTable()
  37. {
  38. }
  39. const MBRPartitionHeader& EBRPartitionTable::header() const
  40. {
  41. return *reinterpret_cast<const MBRPartitionHeader*>(m_cached_mbr_header);
  42. }
  43. const EBRPartitionExtension& EBRPartitionTable::ebr_extension() const
  44. {
  45. return *reinterpret_cast<const EBRPartitionExtension*>(m_cached_ebr_header);
  46. }
  47. int EBRPartitionTable::index_of_ebr_container() const
  48. {
  49. for (int i = 0; i < 4; i++) {
  50. if (header().entry[i].type == EBR_CHS_CONTAINER || header().entry[i].type == EBR_LBA_CONTAINER)
  51. return i;
  52. }
  53. ASSERT_NOT_REACHED();
  54. }
  55. bool EBRPartitionTable::initialize()
  56. {
  57. auto mbr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_mbr_header);
  58. if (!m_device->read_block(0, mbr_header_buffer)) {
  59. return false;
  60. }
  61. auto& header = this->header();
  62. m_ebr_container_id = index_of_ebr_container() + 1;
  63. #ifdef EBR_DEBUG
  64. klog() << "EBRPartitionTable::initialize: MBR_signature=0x" << String::format("%x", header.mbr_signature);
  65. #endif
  66. if (header.mbr_signature != MBR_SIGNATURE) {
  67. klog() << "EBRPartitionTable::initialize: bad MBR signature 0x" << String::format("%x", header.mbr_signature);
  68. return false;
  69. }
  70. auto& ebr_entry = header.entry[m_ebr_container_id - 1];
  71. auto ebr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_ebr_header);
  72. if (!m_device->read_block(ebr_entry.offset, ebr_header_buffer)) {
  73. return false;
  74. }
  75. size_t index = 1;
  76. while (index < 128) { // Unlikely to encounter a disk with 128 partitions in this configuration...
  77. if (ebr_extension().next_chained_ebr_extension.offset == 0 && ebr_extension().next_chained_ebr_extension.type == 0) {
  78. break;
  79. }
  80. index++;
  81. if (!m_device->read_block(ebr_extension().next_chained_ebr_extension.offset, ebr_header_buffer)) {
  82. return false;
  83. }
  84. }
  85. m_ebr_chained_extensions_count = index;
  86. klog() << "EBRPartitionTable::initialize: Extended partitions count - " << m_ebr_chained_extensions_count;
  87. return true;
  88. }
  89. RefPtr<DiskPartition> EBRPartitionTable::get_non_extended_partition(unsigned index)
  90. {
  91. auto& header = this->header();
  92. auto& entry = header.entry[index - 1];
  93. #ifdef EBR_DEBUG
  94. klog() << "EBRPartitionTable::partition: status=0x" << String::format("%x", entry.status) << " offset=0x" << String::format("%x", entry.offset);
  95. #endif
  96. if (entry.offset == 0x00) {
  97. #ifdef EBR_DEBUG
  98. klog() << "EBRPartitionTable::partition: missing partition requested index=" << index;
  99. #endif
  100. return nullptr;
  101. }
  102. #ifdef EBR_DEBUG
  103. klog() << "EBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.type);
  104. #endif
  105. return DiskPartition::create(m_device, entry.offset, (entry.offset + entry.length));
  106. }
  107. RefPtr<DiskPartition> EBRPartitionTable::get_extended_partition(unsigned index)
  108. {
  109. unsigned relative_index = index - m_ebr_container_id;
  110. auto& header = this->header();
  111. #ifdef EBR_DEBUG
  112. klog() << "EBRPartitionTable::partition: relative index " << relative_index;
  113. #endif
  114. auto& ebr_entry = header.entry[m_ebr_container_id - 1];
  115. #ifdef EBR_DEBUG
  116. klog() << "EBRPartitionTable::partition: Extended partition, offset 0x" << String::format("%x", ebr_entry.offset) << ", type " << String::format("%x", ebr_entry.type);
  117. #endif
  118. auto ebr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_ebr_header);
  119. if (!m_device->read_block(ebr_entry.offset, ebr_header_buffer)) {
  120. return nullptr;
  121. }
  122. size_t i = 0;
  123. while (i < relative_index) {
  124. #ifdef EBR_DEBUG
  125. klog() << "EBRPartitionTable::partition: logical partition, relative offset 0x" << String::format("%x", ebr_extension().entry.offset) << ", type " << String::format("%x", ebr_extension().entry.type);
  126. klog() << "EBRPartitionTable::partition: next logical partition, relative offset 0x" << String::format("%x", ebr_extension().next_chained_ebr_extension.offset) << ", type " << String::format("%x", ebr_extension().next_chained_ebr_extension.type);
  127. #endif
  128. if (ebr_extension().next_chained_ebr_extension.offset == 0 && ebr_extension().next_chained_ebr_extension.type == 0) {
  129. break;
  130. }
  131. i++;
  132. if (!m_device->read_block(ebr_extension().next_chained_ebr_extension.offset, ebr_header_buffer)) {
  133. return nullptr;
  134. }
  135. }
  136. #ifdef EBR_DEBUG
  137. klog() << "EBRPartitionTable::partition: status=" << String::format("%x", ebr_extension().entry.status) << " offset=" << String::format("%x", ebr_extension().entry.offset + ebr_entry.offset);
  138. #endif
  139. if (ebr_extension().entry.offset == 0x00) {
  140. #ifdef EBR_DEBUG
  141. klog() << "EBRPartitionTable::partition: missing partition requested index=" << index;
  142. #endif
  143. return nullptr;
  144. }
  145. #ifdef EBR_DEBUG
  146. klog() << "EBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", ebr_extension().entry.type);
  147. #endif
  148. return DiskPartition::create(m_device, ebr_extension().entry.offset + ebr_entry.offset, (ebr_extension().entry.offset + ebr_entry.offset + ebr_extension().entry.length));
  149. }
  150. bool EBRPartitionTable::index_is_extended_partition(unsigned index) const
  151. {
  152. return !(m_ebr_container_id > index || index > (m_ebr_container_id + m_ebr_chained_extensions_count));
  153. }
  154. RefPtr<DiskPartition> EBRPartitionTable::partition(unsigned index)
  155. {
  156. ASSERT(index >= 1 && index <= m_ebr_chained_extensions_count + 4);
  157. auto& header = this->header();
  158. if (header.mbr_signature != MBR_SIGNATURE) {
  159. klog() << "EBRPartitionTable::initialize: bad MBR signature - not initialized? 0x" << String::format("%x", header.mbr_signature);
  160. return nullptr;
  161. }
  162. if (index_is_extended_partition(index))
  163. return get_extended_partition(index);
  164. if (index > 4)
  165. return get_non_extended_partition(index - m_ebr_chained_extensions_count);
  166. return get_non_extended_partition(index);
  167. }
  168. }