EBRPartitionTable.cpp 7.2 KB

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