summaryrefslogtreecommitdiff
path: root/Kernel/Devices/MBRPartitionTable.cpp
blob: b194855b3e00c1277e9278a45b6eb12b79e3bee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <AK/ByteBuffer.h>
#include <Kernel/Devices/MBRPartitionTable.h>

#define MBR_DEBUG

MBRPartitionTable::MBRPartitionTable(Retained<DiskDevice>&& device)
    : m_device(move(device))
{
}

MBRPartitionTable::~MBRPartitionTable()
{
}

const MBRPartitionHeader& MBRPartitionTable::header() const
{
    return *reinterpret_cast<const MBRPartitionHeader*>(m_cached_header);
}

bool MBRPartitionTable::initialize()
{
    if (!m_device->read_block(0, m_cached_header)) {
        return false;
    }

    auto& header = this->header();

#ifdef MBR_DEBUG
    kprintf("MBRPartitionTable::initialize: mbr_signature=%#x\n", header.mbr_signature);
#endif

    if (header.mbr_signature != MBR_SIGNATURE) {
        kprintf("MBRPartitionTable::initialize: bad mbr signature %#x\n", header.mbr_signature);
        return false;
    }

    return true;
}

RetainPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
{
    ASSERT(index >= 1 && index <= 4);

    auto& header = this->header();
    auto& entry = header.entry[index - 1];

    if (header.mbr_signature != MBR_SIGNATURE) {
        kprintf("MBRPartitionTable::initialize: bad mbr signature - not initalized? %#x\n", header.mbr_signature);
        return nullptr;
    }

#ifdef MBR_DEBUG
    kprintf("MBRPartitionTable::partition: status=%#x offset=%#x\n", entry.status, entry.offset);
#endif

    if (entry.offset == 0x00) {
#ifdef MBR_DEBUG
    kprintf("MBRPartitionTable::partition: missing partition requested index=%d\n", index);
#endif

        return nullptr;
    }

#ifdef MBR_DEBUG
    kprintf("MBRPartitionTable::partition: found partition index=%d type=%x\n", index, entry.type);
#endif

    return DiskPartition::create(m_device.copy_ref(), entry.offset);
}