mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
Kernel: Introduce a new partitioning subsystem
The partitioning code was very outdated, and required a full refactor. The new subsystem removes duplicated code and uses more AK containers. The most important change is that all implementations of the PartitionTable class conform to one interface, which made it possible to remove unnecessary code in the EBRPartitionTable class. Finding partitions is now done in the StorageManagement singleton, instead of doing so in init.cpp. Also, now we don't try to find partitions on demand - the kernel will try to detect if a StorageDevice is partitioned, and if so, will check what is the partition table, which could be MBR, GUID or EBR. Then, it will create DiskPartitionMetadata object for each partition that is available in the partition table. This object will be used by the partition enumeration code to create a DiskPartition with the correct minor number.
This commit is contained in:
parent
bc18712adf
commit
72b1998f0d
20 changed files with 639 additions and 561 deletions
|
@ -25,8 +25,12 @@
|
|||
*/
|
||||
|
||||
#include <Kernel/Devices/BlockDevice.h>
|
||||
#include <Kernel/FileSystem/Ext2FileSystem.h>
|
||||
#include <Kernel/PCI/Access.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/StorageManagement.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
@ -35,10 +39,82 @@ static StorageManagement* s_the;
|
|||
|
||||
StorageManagement::StorageManagement(String root_device, bool force_pio)
|
||||
: m_controllers(enumerate_controllers(force_pio))
|
||||
, m_storage_devices(enumerate_storage_devices())
|
||||
, m_disk_partitions(enumerate_disk_partitions())
|
||||
, m_boot_device(determine_boot_device(root_device))
|
||||
, m_boot_block_device(determine_boot_block_device(root_device))
|
||||
{
|
||||
}
|
||||
|
||||
NonnullRefPtrVector<StorageController> StorageManagement::enumerate_controllers(bool force_pio) const
|
||||
{
|
||||
NonnullRefPtrVector<StorageController> controllers;
|
||||
PCI::enumerate([&](const PCI::Address& address, PCI::ID) {
|
||||
if (PCI::get_class(address) == 0x1 && PCI::get_subclass(address) == 0x1) {
|
||||
controllers.append(IDEController::initialize(address, force_pio));
|
||||
}
|
||||
});
|
||||
return controllers;
|
||||
}
|
||||
|
||||
NonnullRefPtrVector<StorageDevice> StorageManagement::enumerate_storage_devices() const
|
||||
{
|
||||
ASSERT(!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;
|
||||
}
|
||||
|
||||
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 nullptr;
|
||||
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 nullptr;
|
||||
return move(ebr_table_or_result.value());
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NonnullRefPtrVector<DiskPartition> StorageManagement::enumerate_disk_partitions() const
|
||||
{
|
||||
ASSERT(!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;
|
||||
}
|
||||
|
||||
NonnullRefPtr<StorageDevice> StorageManagement::determine_boot_device(String root_device) const
|
||||
{
|
||||
ASSERT(!m_controllers.is_empty());
|
||||
|
@ -54,37 +130,48 @@ NonnullRefPtr<StorageDevice> StorageManagement::determine_boot_device(String roo
|
|||
}
|
||||
|
||||
size_t drive_index = (u8)drive_letter - (u8)'a';
|
||||
auto devices = storage_devices();
|
||||
if (drive_index >= devices.size()) {
|
||||
if (drive_index >= m_storage_devices.size()) {
|
||||
klog() << "init_stage2: invalid selection of hard drive.";
|
||||
Processor::halt();
|
||||
}
|
||||
return devices[drive_index];
|
||||
return m_storage_devices[drive_index];
|
||||
}
|
||||
|
||||
NonnullRefPtrVector<StorageController> StorageManagement::enumerate_controllers(bool force_pio) const
|
||||
NonnullRefPtr<BlockDevice> StorageManagement::determine_boot_block_device(String root_device) const
|
||||
{
|
||||
NonnullRefPtrVector<StorageController> controllers;
|
||||
PCI::enumerate([&](const PCI::Address& address, PCI::ID) {
|
||||
if (PCI::get_class(address) == 0x1 && PCI::get_subclass(address) == 0x1) {
|
||||
controllers.append(IDEController::initialize(address, force_pio));
|
||||
}
|
||||
});
|
||||
return controllers;
|
||||
}
|
||||
auto determined_boot_device = m_boot_device;
|
||||
root_device = root_device.substring(strlen("/dev/hda"), root_device.length() - strlen("/dev/hda"));
|
||||
if (!root_device.length())
|
||||
return determined_boot_device;
|
||||
|
||||
NonnullRefPtrVector<StorageDevice> StorageManagement::storage_devices() const
|
||||
{
|
||||
NonnullRefPtrVector<StorageDevice> devices;
|
||||
for (auto& controller : m_controllers) {
|
||||
for (size_t index = 0; index < controller.devices_count(); index++) {
|
||||
auto device = controller.device(index);
|
||||
if (device.is_null())
|
||||
continue;
|
||||
devices.append(device.release_nonnull());
|
||||
}
|
||||
auto partition_number = root_device.to_uint();
|
||||
|
||||
if (!partition_number.has_value()) {
|
||||
klog() << "init_stage2: couldn't parse partition number from root kernel parameter";
|
||||
Processor::halt();
|
||||
}
|
||||
return devices;
|
||||
|
||||
if (partition_number.value() > m_boot_device->m_partitions.size()) {
|
||||
klog() << "init_stage2: invalid partition number!";
|
||||
Processor::halt();
|
||||
}
|
||||
|
||||
return m_boot_device->m_partitions[partition_number.value() - 1];
|
||||
}
|
||||
|
||||
NonnullRefPtr<BlockDevice> StorageManagement::boot_block_device() const
|
||||
{
|
||||
return m_boot_block_device;
|
||||
}
|
||||
|
||||
NonnullRefPtr<FS> StorageManagement::root_filesystem() const
|
||||
{
|
||||
auto e2fs = Ext2FS::create(*FileDescription::create(boot_block_device()));
|
||||
if (!e2fs->initialize()) {
|
||||
klog() << "init_stage2: couldn't open root filesystem";
|
||||
Processor::halt();
|
||||
}
|
||||
return e2fs;
|
||||
}
|
||||
|
||||
bool StorageManagement::initialized()
|
||||
|
@ -103,10 +190,6 @@ StorageManagement& StorageManagement::the()
|
|||
return *s_the;
|
||||
}
|
||||
|
||||
NonnullRefPtr<StorageDevice> StorageManagement::boot_device() const
|
||||
{
|
||||
return m_boot_device;
|
||||
}
|
||||
NonnullRefPtrVector<StorageController> StorageManagement::ide_controllers() const
|
||||
{
|
||||
NonnullRefPtrVector<StorageController> ide_controllers;
|
||||
|
@ -116,5 +199,4 @@ NonnullRefPtrVector<StorageController> StorageManagement::ide_controllers() cons
|
|||
}
|
||||
return ide_controllers;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue