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