MBRPartitionTable.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <AK/ByteBuffer.h>
  2. #include <Kernel/Devices/MBRPartitionTable.h>
  3. #define MBR_DEBUG
  4. MBRPartitionTable::MBRPartitionTable(Retained<DiskDevice>&& device)
  5. : m_device(move(device))
  6. {
  7. }
  8. MBRPartitionTable::~MBRPartitionTable()
  9. {
  10. }
  11. const MBRPartitionHeader& MBRPartitionTable::header() const
  12. {
  13. return *reinterpret_cast<const MBRPartitionHeader*>(m_cached_header);
  14. }
  15. bool MBRPartitionTable::initialize()
  16. {
  17. if (!m_device->read_block(0, m_cached_header)) {
  18. return false;
  19. }
  20. auto& header = this->header();
  21. #ifdef MBR_DEBUG
  22. kprintf("MBRPartitionTable::initialize: mbr_signature=%#x\n", header.mbr_signature);
  23. #endif
  24. if (header.mbr_signature != MBR_SIGNATURE) {
  25. kprintf("MBRPartitionTable::initialize: bad mbr signature %#x\n", header.mbr_signature);
  26. return false;
  27. }
  28. return true;
  29. }
  30. RetainPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
  31. {
  32. ASSERT(index >= 1 && index <= 4);
  33. auto& header = this->header();
  34. auto& entry = header.entry[index - 1];
  35. if (header.mbr_signature != MBR_SIGNATURE) {
  36. kprintf("MBRPartitionTable::initialize: bad mbr signature - not initalized? %#x\n", header.mbr_signature);
  37. return nullptr;
  38. }
  39. #ifdef MBR_DEBUG
  40. kprintf("MBRPartitionTable::partition: status=%#x offset=%#x\n", entry.status, entry.offset);
  41. #endif
  42. if (entry.status == 0x00) {
  43. return nullptr;
  44. }
  45. return DiskPartition::create(m_device.copy_ref(), entry.offset);
  46. }