summaryrefslogtreecommitdiff
path: root/Kernel/Storage/StorageManagement.cpp
blob: 9b53b991e535ce3ad978e80c2bc80b9d149518b2 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/UUID.h>
#include <Kernel/Bus/PCI/Access.h>
#include <Kernel/CommandLine.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/FileSystem/Ext2FileSystem.h>
#include <Kernel/Panic.h>
#include <Kernel/Storage/AHCIController.h>
#include <Kernel/Storage/IDEController.h>
#include <Kernel/Storage/Partition/EBRPartitionTable.h>
#include <Kernel/Storage/Partition/GUIDPartitionTable.h>
#include <Kernel/Storage/Partition/MBRPartitionTable.h>
#include <Kernel/Storage/RamdiskController.h>
#include <Kernel/Storage/StorageManagement.h>

namespace Kernel {

static StorageManagement* s_the;
static size_t s_device_minor_number;

UNMAP_AFTER_INIT StorageManagement::StorageManagement(String boot_argument, bool force_pio)
    : m_boot_argument(boot_argument)
    , m_controllers(enumerate_controllers(force_pio))
    , m_storage_devices(enumerate_storage_devices())
    , m_disk_partitions(enumerate_disk_partitions())
{
    s_device_minor_number = 0;
    if (!boot_argument_contains_partition_uuid()) {
        determine_boot_device();
        return;
    }
    determine_boot_device_with_partition_uuid();
}

bool StorageManagement::boot_argument_contains_partition_uuid()
{
    return m_boot_argument.starts_with("PARTUUID=");
}

UNMAP_AFTER_INIT NonnullRefPtrVector<StorageController> StorageManagement::enumerate_controllers(bool force_pio) const
{
    NonnullRefPtrVector<StorageController> controllers;
    if (!kernel_command_line().disable_physical_storage()) {
        if (kernel_command_line().is_ide_enabled()) {
            PCI::enumerate([&](const PCI::Address& address, PCI::ID) {
                if (PCI::get_class(address) == PCI_MASS_STORAGE_CLASS_ID && PCI::get_subclass(address) == PCI_IDE_CTRL_SUBCLASS_ID) {
                    controllers.append(IDEController::initialize(address, force_pio));
                }
            });
        }
        PCI::enumerate([&](const PCI::Address& address, PCI::ID) {
            if (PCI::get_class(address) == PCI_MASS_STORAGE_CLASS_ID && PCI::get_subclass(address) == PCI_SATA_CTRL_SUBCLASS_ID && PCI::get_programming_interface(address) == PCI_AHCI_IF_PROGIF) {
                controllers.append(AHCIController::initialize(address));
            }
        });
    }
    controllers.append(RamdiskController::initialize());
    return controllers;
}

UNMAP_AFTER_INIT NonnullRefPtrVector<StorageDevice> StorageManagement::enumerate_storage_devices() const
{
    VERIFY(!m_controllers.is_empty());
    NonnullRefPtrVector<StorageDevice> devices;
    for (auto& controller : m_controllers) {
        for (size_t device_index = 0; device_index < controller.devices_count(); device_index++) {
            auto device = controller.device(device_index);
            if (device.is_null())
                continue;
            devices.append(device.release_nonnull());
        }
    }
    return devices;
}

UNMAP_AFTER_INIT OwnPtr<PartitionTable> StorageManagement::try_to_initialize_partition_table(const StorageDevice& device) const
{
    auto mbr_table_or_result = MBRPartitionTable::try_to_initialize(device);
    if (!mbr_table_or_result.is_error())
        return move(mbr_table_or_result.value());
    if (mbr_table_or_result.error() == PartitionTable::Error::MBRProtective) {
        auto gpt_table_or_result = GUIDPartitionTable::try_to_initialize(device);
        if (gpt_table_or_result.is_error())
            return {};
        return move(gpt_table_or_result.value());
    }
    if (mbr_table_or_result.error() == PartitionTable::Error::ConatinsEBR) {
        auto ebr_table_or_result = EBRPartitionTable::try_to_initialize(device);
        if (ebr_table_or_result.is_error())
            return {};
        return move(ebr_table_or_result.value());
    }
    return {};
}

UNMAP_AFTER_INIT NonnullRefPtrVector<DiskPartition> StorageManagement::enumerate_disk_partitions() const
{
    VERIFY(!m_storage_devices.is_empty());
    NonnullRefPtrVector<DiskPartition> partitions;
    size_t device_index = 0;
    for (auto& device : m_storage_devices) {
        auto partition_table = try_to_initialize_partition_table(device);
        if (!partition_table)
            continue;
        for (size_t partition_index = 0; partition_index < partition_table->partitions_count(); partition_index++) {
            auto partition_metadata = partition_table->partition(partition_index);
            if (!partition_metadata.has_value())
                continue;
            // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
            auto disk_partition = DiskPartition::create(const_cast<StorageDevice&>(device), (partition_index + (16 * device_index)), partition_metadata.value());
            partitions.append(disk_partition);
            const_cast<StorageDevice&>(device).m_partitions.append(disk_partition);
        }
        device_index++;
    }
    return partitions;
}

UNMAP_AFTER_INIT void StorageManagement::determine_boot_device()
{
    VERIFY(!m_controllers.is_empty());
    if (m_boot_argument.starts_with("/dev/")) {
        StringView device_name = m_boot_argument.substring_view(5);
        Device::for_each([&](Device& device) {
            if (device.is_block_device()) {
                auto& block_device = static_cast<BlockDevice&>(device);
                if (device.device_name() == device_name) {
                    m_boot_block_device = block_device;
                }
            }
        });
    }

    if (m_boot_block_device.is_null()) {
        PANIC("StorageManagement: boot device {} not found", m_boot_argument);
    }
}

UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_partition_uuid()
{
    VERIFY(!m_disk_partitions.is_empty());
    VERIFY(m_boot_argument.starts_with("PARTUUID="));

    auto partition_uuid = UUID(m_boot_argument.substring_view(strlen("PARTUUID=")));

    if (partition_uuid.to_string().length() != 36) {
        PANIC("StorageManagement: Specified partition UUID is not valid");
    }

    for (auto& partition : m_disk_partitions) {
        if (partition.metadata().unique_guid().is_zero())
            continue;
        if (partition.metadata().unique_guid() == partition_uuid) {
            m_boot_block_device = partition;
            break;
        }
    }
}

RefPtr<BlockDevice> StorageManagement::boot_block_device() const
{
    return m_boot_block_device;
}

int StorageManagement::major_number()
{
    return 3;
}
int StorageManagement::minor_number()
{
    return s_device_minor_number++;
}

NonnullRefPtr<FileSystem> StorageManagement::root_filesystem() const
{
    auto boot_device_description = boot_block_device();
    if (!boot_device_description) {
        PANIC("StorageManagement: Couldn't find a suitable device to boot from");
    }
    auto e2fs = Ext2FS::create(FileDescription::try_create(boot_device_description.release_nonnull()).value());
    if (auto result = e2fs->initialize(); result.is_error()) {
        PANIC("StorageManagement: Couldn't open root filesystem: {}", result);
    }
    return e2fs;
}

bool StorageManagement::initialized()
{
    return (s_the != nullptr);
}

UNMAP_AFTER_INIT void StorageManagement::initialize(String root_device, bool force_pio)
{
    VERIFY(!StorageManagement::initialized());
    s_the = new StorageManagement(root_device, force_pio);
}

StorageManagement& StorageManagement::the()
{
    return *s_the;
}

}