MBRPartitionTable.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/MBRPartitionTable.h>
  28. #define MBR_DEBUG
  29. namespace Kernel {
  30. MBRPartitionTable::MBRPartitionTable(NonnullRefPtr<BlockDevice> device)
  31. : m_device(move(device))
  32. {
  33. }
  34. MBRPartitionTable::~MBRPartitionTable()
  35. {
  36. }
  37. const MBRPartitionHeader& MBRPartitionTable::header() const
  38. {
  39. return *reinterpret_cast<const MBRPartitionHeader*>(m_cached_header);
  40. }
  41. bool MBRPartitionTable::initialize()
  42. {
  43. if (!m_device->read_block(0, m_cached_header)) {
  44. return false;
  45. }
  46. auto& header = this->header();
  47. #ifdef MBR_DEBUG
  48. klog() << "MBRPartitionTable::initialize: mbr_signature=0x" << String::format("%x", header.mbr_signature);
  49. #endif
  50. if (header.mbr_signature != MBR_SIGNATURE) {
  51. klog() << "MBRPartitionTable::initialize: bad mbr signature 0x" << String::format("%x", header.mbr_signature);
  52. return false;
  53. }
  54. return true;
  55. }
  56. bool MBRPartitionTable::contains_ebr() const
  57. {
  58. for (int i = 0; i < 4; i++) {
  59. if (header().entry[i].type == EBR_CHS_CONTAINER || header().entry[i].type == EBR_LBA_CONTAINER)
  60. return true;
  61. }
  62. return false;
  63. }
  64. bool MBRPartitionTable::is_protective_mbr() const
  65. {
  66. return header().entry[0].type == MBR_PROTECTIVE;
  67. }
  68. RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
  69. {
  70. ASSERT(index >= 1 && index <= 4);
  71. auto& header = this->header();
  72. auto& entry = header.entry[index - 1];
  73. if (header.mbr_signature != MBR_SIGNATURE) {
  74. klog() << "MBRPartitionTable::initialize: bad mbr signature - not initalized? 0x" << String::format("%x", header.mbr_signature);
  75. return nullptr;
  76. }
  77. #ifdef MBR_DEBUG
  78. klog() << "MBRPartitionTable::partition: status=0x" << String::format("%x", entry.status) << " offset=0x" << String::format("%x", entry.offset);
  79. #endif
  80. if (entry.offset == 0x00) {
  81. #ifdef MBR_DEBUG
  82. klog() << "MBRPartitionTable::partition: missing partition requested index=" << index;
  83. #endif
  84. return nullptr;
  85. }
  86. #ifdef MBR_DEBUG
  87. klog() << "MBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.type);
  88. #endif
  89. return DiskPartition::create(m_device, entry.offset, (entry.offset + entry.length));
  90. }
  91. }