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/GPTPartitionTable.h>
29
30#define GPT_DEBUG
31
32namespace Kernel {
33
34GPTPartitionTable::GPTPartitionTable(BlockDevice& device)
35 : m_device(move(device))
36{
37}
38
39GPTPartitionTable::~GPTPartitionTable()
40{
41}
42
43const GPTPartitionHeader& GPTPartitionTable::header() const
44{
45 return *reinterpret_cast<const GPTPartitionHeader*>(m_cached_header);
46}
47
48bool GPTPartitionTable::initialize()
49{
50 if (!m_device->read_block(1, m_cached_header)) {
51 return false;
52 }
53
54 auto& header = this->header();
55
56#ifdef GPT_DEBUG
57 klog() << "GPTPartitionTable::initialize: gpt_signature=0x" << String::format("%x", header.sig[1]) << String::format("%x", header.sig[0]);
58#endif
59
60 if (header.sig[0] != GPT_SIGNATURE && header.sig[1] != GPT_SIGNATURE2) {
61 klog() << "GPTPartitionTable::initialize: bad GPT signature 0x" << String::format("%x", header.sig[1]) << String::format("%x", header.sig[0]);
62 return false;
63 }
64
65 return true;
66}
67
68RefPtr<DiskPartition> GPTPartitionTable::partition(unsigned index)
69{
70 ASSERT(index >= 1 && index <= 4294967294);
71
72 auto& header = this->header();
73 unsigned lba = header.partition_array_start_lba + (((index - 1) * header.partition_entry_size) / BytesPerSector);
74
75 if (header.sig[0] != GPT_SIGNATURE) {
76 klog() << "GPTPartitionTable::initialize: bad gpt signature - not initalized? 0x" << String::format("%x", header.sig);
77 return nullptr;
78 }
79
80 u8 entries_per_sector = BytesPerSector / header.partition_entry_size;
81
82 GPTPartitionEntry entries[entries_per_sector];
83 this->m_device->read_blocks(lba, 1, (u8*)&entries);
84 GPTPartitionEntry& entry = entries[((index - 1) % entries_per_sector)];
85
86#ifdef GPT_DEBUG
87 klog() << "GPTPartitionTable::partition " << index;
88 klog() << "GPTPartitionTable - offset = " << entry.first_lba[1] << entry.first_lba[0];
89#endif
90
91 if (entry.first_lba[0] == 0x00) {
92#ifdef GPT_DEBUG
93 klog() << "GPTPartitionTable::partition: missing partition requested index=" << index;
94#endif
95
96 return nullptr;
97 }
98
99#ifdef GPT_DEBUG
100 klog() << "GPTPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.partition_guid[3]) << "-" << String::format("%x", entry.partition_guid[2]) << "-" << String::format("%x", entry.partition_guid[1]) << "-" << String::format("%x", entry.partition_guid[0]);
101#endif
102 return DiskPartition::create(m_device, entry.first_lba[0], entry.last_lba[0]);
103}
104
105}