MBRPartitionTable.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <Kernel/Debug.h>
  8. #include <Kernel/Storage/Partition/MBRPartitionTable.h>
  9. namespace Kernel {
  10. #define MBR_SIGNATURE 0xaa55
  11. #define MBR_PROTECTIVE 0xEE
  12. #define EBR_CHS_CONTAINER 0x05
  13. #define EBR_LBA_CONTAINER 0x0F
  14. Result<NonnullOwnPtr<MBRPartitionTable>, PartitionTable::Error> MBRPartitionTable::try_to_initialize(const StorageDevice& device)
  15. {
  16. auto table = make<MBRPartitionTable>(device);
  17. if (table->contains_ebr())
  18. return { PartitionTable::Error::ConatinsEBR };
  19. if (table->is_protective_mbr())
  20. return { PartitionTable::Error::MBRProtective };
  21. if (!table->is_valid())
  22. return { PartitionTable::Error::Invalid };
  23. return table;
  24. }
  25. OwnPtr<MBRPartitionTable> MBRPartitionTable::try_to_initialize(const StorageDevice& device, u32 start_lba)
  26. {
  27. auto table = make<MBRPartitionTable>(device, start_lba);
  28. if (!table->is_valid())
  29. return {};
  30. return table;
  31. }
  32. bool MBRPartitionTable::read_boot_record()
  33. {
  34. auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data());
  35. if (!m_device->read_block(m_start_lba, buffer))
  36. return false;
  37. m_header_valid = true;
  38. return m_header_valid;
  39. }
  40. MBRPartitionTable::MBRPartitionTable(const StorageDevice& device, u32 start_lba)
  41. : PartitionTable(device)
  42. , m_start_lba(start_lba)
  43. , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value()) // FIXME: Do something sensible if this fails because of OOM.
  44. {
  45. if (!read_boot_record() || !initialize())
  46. return;
  47. m_header_valid = true;
  48. auto& header = this->header();
  49. for (size_t index = 0; index < 4; index++) {
  50. auto& entry = header.entry[index];
  51. if (entry.offset == 0x00) {
  52. continue;
  53. }
  54. m_partitions.empend(entry.offset, (entry.offset + entry.length), entry.type);
  55. }
  56. m_valid = true;
  57. }
  58. MBRPartitionTable::MBRPartitionTable(const StorageDevice& device)
  59. : PartitionTable(device)
  60. , m_start_lba(0)
  61. , m_cached_header(ByteBuffer::create_zeroed(m_device->block_size()).release_value()) // FIXME: Do something sensible if this fails because of OOM.
  62. {
  63. if (!read_boot_record() || contains_ebr() || is_protective_mbr() || !initialize())
  64. return;
  65. auto& header = this->header();
  66. for (size_t index = 0; index < 4; index++) {
  67. auto& entry = header.entry[index];
  68. if (entry.offset == 0x00) {
  69. continue;
  70. }
  71. m_partitions.empend(entry.offset, (entry.offset + entry.length), entry.type);
  72. }
  73. m_valid = true;
  74. }
  75. MBRPartitionTable::~MBRPartitionTable()
  76. {
  77. }
  78. const MBRPartitionTable::Header& MBRPartitionTable::header() const
  79. {
  80. return *(const MBRPartitionTable::Header*)m_cached_header.data();
  81. }
  82. bool MBRPartitionTable::initialize()
  83. {
  84. auto& header = this->header();
  85. dbgln_if(MBR_DEBUG, "Master Boot Record: mbr_signature={:#08x}", header.mbr_signature);
  86. if (header.mbr_signature != MBR_SIGNATURE) {
  87. dbgln("Master Boot Record: invalid signature");
  88. return false;
  89. }
  90. return true;
  91. }
  92. bool MBRPartitionTable::contains_ebr() const
  93. {
  94. for (int i = 0; i < 4; i++) {
  95. if (header().entry[i].type == EBR_CHS_CONTAINER || header().entry[i].type == EBR_LBA_CONTAINER)
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool MBRPartitionTable::is_protective_mbr() const
  101. {
  102. return header().entry[0].type == MBR_PROTECTIVE;
  103. }
  104. }