MBRPartitionTable.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.offset == 0x00) {
  43. #ifdef MBR_DEBUG
  44. kprintf("MBRPartitionTable::partition: missing partition requested index=%d\n", index);
  45. #endif
  46. return nullptr;
  47. }
  48. #ifdef MBR_DEBUG
  49. kprintf("MBRPartitionTable::partition: found partition index=%d type=%x\n", index, entry.type);
  50. #endif
  51. return DiskPartition::create(m_device.copy_ref(), entry.offset);
  52. }