EBRPartitionTable.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/Storage/Partition/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_request = m_device->make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read,
  58. 0, 1, UserOrKernelBuffer::for_kernel_buffer(m_cached_mbr_header), sizeof(m_cached_mbr_header));
  59. auto mbr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_mbr_header);
  60. if (!m_device->read_block(0, mbr_header_buffer)) {
  61. return false;
  62. }
  63. auto& header = this->header();
  64. m_ebr_container_id = index_of_ebr_container() + 1;
  65. #ifdef EBR_DEBUG
  66. klog() << "EBRPartitionTable::initialize: MBR_signature=0x" << String::format("%x", header.mbr_signature);
  67. #endif
  68. if (header.mbr_signature != MBR_SIGNATURE) {
  69. klog() << "EBRPartitionTable::initialize: bad MBR signature 0x" << String::format("%x", header.mbr_signature);
  70. return false;
  71. }
  72. auto& ebr_entry = header.entry[m_ebr_container_id - 1];
  73. auto ebr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_ebr_header);
  74. if (!m_device->read_block(ebr_entry.offset, ebr_header_buffer)) {
  75. return false;
  76. }
  77. size_t index = 1;
  78. while (index < 128) { // Unlikely to encounter a disk with 128 partitions in this configuration...
  79. if (ebr_extension().next_chained_ebr_extension.offset == 0 && ebr_extension().next_chained_ebr_extension.type == 0) {
  80. break;
  81. }
  82. index++;
  83. if (!m_device->read_block(ebr_extension().next_chained_ebr_extension.offset, ebr_header_buffer)) {
  84. return false;
  85. }
  86. }
  87. m_ebr_chained_extensions_count = index;
  88. klog() << "EBRPartitionTable::initialize: Extended partitions count - " << m_ebr_chained_extensions_count;
  89. return true;
  90. }
  91. RefPtr<DiskPartition> EBRPartitionTable::get_non_extended_partition(unsigned index)
  92. {
  93. auto& header = this->header();
  94. auto& entry = header.entry[index - 1];
  95. #ifdef EBR_DEBUG
  96. klog() << "EBRPartitionTable::partition: status=0x" << String::format("%x", entry.status) << " offset=0x" << String::format("%x", entry.offset);
  97. #endif
  98. if (entry.offset == 0x00) {
  99. #ifdef EBR_DEBUG
  100. klog() << "EBRPartitionTable::partition: missing partition requested index=" << index;
  101. #endif
  102. return nullptr;
  103. }
  104. #ifdef EBR_DEBUG
  105. klog() << "EBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.type);
  106. #endif
  107. return DiskPartition::create(m_device, entry.offset, (entry.offset + entry.length));
  108. }
  109. RefPtr<DiskPartition> EBRPartitionTable::get_extended_partition(unsigned index)
  110. {
  111. unsigned relative_index = index - m_ebr_container_id;
  112. auto& header = this->header();
  113. #ifdef EBR_DEBUG
  114. klog() << "EBRPartitionTable::partition: relative index " << relative_index;
  115. #endif
  116. auto& ebr_entry = header.entry[m_ebr_container_id - 1];
  117. #ifdef EBR_DEBUG
  118. klog() << "EBRPartitionTable::partition: Extended partition, offset 0x" << String::format("%x", ebr_entry.offset) << ", type " << String::format("%x", ebr_entry.type);
  119. #endif
  120. auto ebr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_ebr_header);
  121. if (!m_device->read_block(ebr_entry.offset, ebr_header_buffer)) {
  122. return nullptr;
  123. }
  124. size_t i = 0;
  125. while (i < relative_index) {
  126. #ifdef EBR_DEBUG
  127. klog() << "EBRPartitionTable::partition: logical partition, relative offset 0x" << String::format("%x", ebr_extension().entry.offset) << ", type " << String::format("%x", ebr_extension().entry.type);
  128. 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);
  129. #endif
  130. if (ebr_extension().next_chained_ebr_extension.offset == 0 && ebr_extension().next_chained_ebr_extension.type == 0) {
  131. break;
  132. }
  133. i++;
  134. if (!m_device->read_block(ebr_extension().next_chained_ebr_extension.offset, ebr_header_buffer)) {
  135. return nullptr;
  136. }
  137. }
  138. #ifdef EBR_DEBUG
  139. klog() << "EBRPartitionTable::partition: status=" << String::format("%x", ebr_extension().entry.status) << " offset=" << String::format("%x", ebr_extension().entry.offset + ebr_entry.offset);
  140. #endif
  141. if (ebr_extension().entry.offset == 0x00) {
  142. #ifdef EBR_DEBUG
  143. klog() << "EBRPartitionTable::partition: missing partition requested index=" << index;
  144. #endif
  145. return nullptr;
  146. }
  147. #ifdef EBR_DEBUG
  148. klog() << "EBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", ebr_extension().entry.type);
  149. #endif
  150. return DiskPartition::create(m_device, ebr_extension().entry.offset + ebr_entry.offset, (ebr_extension().entry.offset + ebr_entry.offset + ebr_extension().entry.length));
  151. }
  152. bool EBRPartitionTable::index_is_extended_partition(unsigned index) const
  153. {
  154. return !(m_ebr_container_id > index || index > (m_ebr_container_id + m_ebr_chained_extensions_count));
  155. }
  156. RefPtr<DiskPartition> EBRPartitionTable::partition(unsigned index)
  157. {
  158. ASSERT(index >= 1 && index <= m_ebr_chained_extensions_count + 4);
  159. auto& header = this->header();
  160. if (header.mbr_signature != MBR_SIGNATURE) {
  161. klog() << "EBRPartitionTable::initialize: bad MBR signature - not initialized? 0x" << String::format("%x", header.mbr_signature);
  162. return nullptr;
  163. }
  164. if (index_is_extended_partition(index))
  165. return get_extended_partition(index);
  166. if (index > 4)
  167. return get_non_extended_partition(index - m_ebr_chained_extensions_count);
  168. return get_non_extended_partition(index);
  169. }
  170. }