mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
Kernel/Storage: Introduce new boot device addressing modes
Before of this patch, we supported two methods to address a boot device: 1. Specifying root=/dev/hdXY, where X is a-z letter which corresponds to a boot device, and Y as number from 1 to 16, to indicate the partition number, which can be omitted to instruct the kernel to use a raw device rather than a partition on a raw device. 2. Specifying root=PARTUUID: with a GUID string of a GUID partition. In case of existing storage device with GPT partitions, this is most likely the safest option to ensure booting from persistent storage. While option 2 is more advanced and reliable, the first option has 2 caveats: 1. The string prefix "/dev/hd" doesn't mean anything beside a convention on Linux installations, that was taken into use in Serenity. In Serenity we don't mount DevTmpFS before we mount the boot device on /, so the kernel doesn't really access /dev anyway, so this convention is only a big misleading relic that can easily make the user to assume we access /dev early on boot. 2. This convention although resemble the simple linux convention, is quite limited in specifying a correct boot device across hardware setup changes, so option 2 was recommended to ensure the system is always bootable. With these caveats in mind, this commit tries to fix the problem with adding more addressing options as well as to remove the first option being mentioned above of addressing. To sum it up, there are 4 addressing options: 1. Hardware relative address - Each instance of StorageController is assigned with a index number relative to the type of hardware it handles which makes it possible to address storage devices with a prefix of the commandset ("ata" for ATA, "nvme" for NVMe, "ramdisk" for Plain memory), and then the number for the parent controller relative hardware index, another number LUN target_id, and a third number for LUN disk_id. 2. LUN address - Similar to the previous option, but instead we rely on the parent controller absolute index for the first number. 3. Block device major and minor numbers - by specifying the major and minor numbers, the kernel can simply try to get the corresponding block device and use it as the boot device. 4. GUID string, in the same fashion like before, so the user use the "PARTUUID:" string prefix and add the GUID of the GPT partition. For the new address modes 1 and 2, the user can choose to also specify a partition out of the selected boot device. To do that, the user needs to append the semicolon character and then add the string "partX" where X is to be changed for the partition number. We start counting from 0, and therefore the first partition number is 0 and not 1 in the kernel boot argument.
This commit is contained in:
parent
13c8695523
commit
2c84466ad8
22 changed files with 360 additions and 101 deletions
|
@ -14,6 +14,7 @@
|
|||
#include <Kernel/Bus/PCI/Controller/VolumeManagementDevice.h>
|
||||
#include <Kernel/CommandLine.h>
|
||||
#include <Kernel/Devices/BlockDevice.h>
|
||||
#include <Kernel/Devices/DeviceManagement.h>
|
||||
#include <Kernel/FileSystem/Ext2FileSystem.h>
|
||||
#include <Kernel/Panic.h>
|
||||
#include <Kernel/Storage/ATA/AHCI/Controller.h>
|
||||
|
@ -34,22 +35,41 @@ static Atomic<u32> s_storage_device_minor_number;
|
|||
static Atomic<u32> s_partition_device_minor_number;
|
||||
static Atomic<u32> s_controller_id;
|
||||
|
||||
static Atomic<u32> s_relative_ata_controller_id;
|
||||
static Atomic<u32> s_relative_nvme_controller_id;
|
||||
|
||||
static constexpr StringView partition_uuid_prefix = "PARTUUID:"sv;
|
||||
|
||||
static constexpr StringView partition_number_prefix = "part"sv;
|
||||
static constexpr StringView block_device_prefix = "block"sv;
|
||||
|
||||
static constexpr StringView ata_device_prefix = "ata"sv;
|
||||
static constexpr StringView nvme_device_prefix = "nvme"sv;
|
||||
static constexpr StringView ramdisk_device_prefix = "ramdisk"sv;
|
||||
static constexpr StringView logical_unit_number_device_prefix = "lun"sv;
|
||||
|
||||
UNMAP_AFTER_INIT StorageManagement::StorageManagement()
|
||||
{
|
||||
}
|
||||
|
||||
u32 StorageManagement::generate_relative_nvme_controller_id(Badge<NVMeController>)
|
||||
{
|
||||
auto controller_id = s_relative_nvme_controller_id.load();
|
||||
s_relative_nvme_controller_id++;
|
||||
return controller_id;
|
||||
}
|
||||
u32 StorageManagement::generate_relative_ata_controller_id(Badge<ATAController>)
|
||||
{
|
||||
auto controller_id = s_relative_ata_controller_id.load();
|
||||
s_relative_ata_controller_id++;
|
||||
return controller_id;
|
||||
}
|
||||
|
||||
void StorageManagement::remove_device(StorageDevice& device)
|
||||
{
|
||||
m_storage_devices.remove(device);
|
||||
}
|
||||
|
||||
bool StorageManagement::boot_argument_contains_partition_uuid()
|
||||
{
|
||||
return m_boot_argument.starts_with(partition_uuid_prefix);
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::enumerate_pci_controllers(bool force_pio, bool nvme_poll)
|
||||
{
|
||||
VERIFY(m_controllers.is_empty());
|
||||
|
@ -120,12 +140,12 @@ UNMAP_AFTER_INIT void StorageManagement::dump_storage_devices_and_partitions() c
|
|||
for (auto const& storage_device : m_storage_devices) {
|
||||
auto const& partitions = storage_device.partitions();
|
||||
if (partitions.is_empty()) {
|
||||
dbgln(" Device: {} (no partitions)", storage_device.early_storage_name());
|
||||
dbgln(" Device: block{}:{} (no partitions)", storage_device.major(), storage_device.minor());
|
||||
} else {
|
||||
dbgln(" Device: {} ({} partitions)", storage_device.early_storage_name(), partitions.size());
|
||||
dbgln(" Device: block{}:{} ({} partitions)", storage_device.major(), storage_device.minor(), partitions.size());
|
||||
unsigned partition_number = 1;
|
||||
for (auto const& partition : partitions) {
|
||||
dbgln(" Partition: {} (UUID {})", partition_number, partition.metadata().unique_guid().to_string());
|
||||
dbgln(" Partition: {}, block{}:{} (UUID {})", partition_number, partition.major(), partition.minor(), partition.metadata().unique_guid().to_string());
|
||||
partition_number++;
|
||||
}
|
||||
}
|
||||
|
@ -164,51 +184,184 @@ UNMAP_AFTER_INIT void StorageManagement::enumerate_disk_partitions()
|
|||
}
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_boot_device()
|
||||
UNMAP_AFTER_INIT Optional<unsigned> StorageManagement::extract_boot_device_partition_number_parameter(StringView device_prefix)
|
||||
{
|
||||
VERIFY(!m_controllers.is_empty());
|
||||
if (m_boot_argument.starts_with("/dev/"sv)) {
|
||||
StringView storage_name = m_boot_argument.substring_view(5);
|
||||
for (auto& storage_device : m_storage_devices) {
|
||||
if (storage_device.early_storage_name() == storage_name) {
|
||||
m_boot_block_device = storage_device;
|
||||
break;
|
||||
}
|
||||
VERIFY(m_boot_argument.starts_with(device_prefix));
|
||||
VERIFY(!m_boot_argument.starts_with(partition_uuid_prefix));
|
||||
auto storage_device_relative_address_view = m_boot_argument.substring_view(device_prefix.length());
|
||||
auto parameter_view = storage_device_relative_address_view.find_last_split_view(';');
|
||||
if (parameter_view == storage_device_relative_address_view)
|
||||
return {};
|
||||
if (!parameter_view.starts_with(partition_number_prefix)) {
|
||||
PANIC("StorageManagement: Invalid root boot parameter.");
|
||||
}
|
||||
|
||||
// If the early storage name's last character is a digit (e.g. in the case of NVMe where the last
|
||||
// number in the device name indicates the node, e.g. /dev/nvme0n1 we need to append a "p" character
|
||||
// so that we can properly distinguish the partition index from the device itself
|
||||
char storage_name_last_char = *(storage_device.early_storage_name().end() - 1);
|
||||
OwnPtr<KString> normalized_name;
|
||||
StringView early_storage_name;
|
||||
if (storage_name_last_char >= '0' && storage_name_last_char <= '9') {
|
||||
normalized_name = MUST(KString::formatted("{}p", storage_device.early_storage_name()));
|
||||
early_storage_name = normalized_name->view();
|
||||
} else {
|
||||
early_storage_name = storage_device.early_storage_name();
|
||||
}
|
||||
auto parameter_number = parameter_view.substring_view(partition_number_prefix.length()).to_uint<unsigned>();
|
||||
if (!parameter_number.has_value()) {
|
||||
PANIC("StorageManagement: Invalid root boot parameter.");
|
||||
}
|
||||
|
||||
auto start_storage_name = storage_name.substring_view(0, min(early_storage_name.length(), storage_name.length()));
|
||||
return parameter_number.value();
|
||||
}
|
||||
|
||||
if (early_storage_name.starts_with(start_storage_name)) {
|
||||
StringView partition_sign = storage_name.substring_view(start_storage_name.length());
|
||||
auto possible_partition_number = partition_sign.to_uint<size_t>();
|
||||
if (!possible_partition_number.has_value())
|
||||
break;
|
||||
if (possible_partition_number.value() == 0)
|
||||
break;
|
||||
if (storage_device.partitions().size() < possible_partition_number.value())
|
||||
break;
|
||||
m_boot_block_device = storage_device.partitions()[possible_partition_number.value() - 1];
|
||||
break;
|
||||
}
|
||||
UNMAP_AFTER_INIT Array<unsigned, 3> StorageManagement::extract_boot_device_address_parameters(StringView device_prefix)
|
||||
{
|
||||
VERIFY(!m_boot_argument.starts_with(partition_uuid_prefix));
|
||||
Array<unsigned, 3> address_parameters;
|
||||
auto parameters_view = m_boot_argument.substring_view(device_prefix.length()).find_first_split_view(';');
|
||||
size_t parts_count = 0;
|
||||
bool parse_failure = false;
|
||||
parameters_view.for_each_split_view(':', false, [&](StringView parameter_view) {
|
||||
if (parse_failure)
|
||||
return;
|
||||
if (parts_count > 2)
|
||||
return;
|
||||
auto parameter_number = parameter_view.to_uint<unsigned>();
|
||||
if (!parameter_number.has_value()) {
|
||||
parse_failure = true;
|
||||
return;
|
||||
}
|
||||
address_parameters[parts_count] = parameter_number.value();
|
||||
parts_count++;
|
||||
});
|
||||
|
||||
if (parts_count > 3) {
|
||||
dbgln("StorageManagement: Detected {} parts in boot device parameter.", parts_count);
|
||||
PANIC("StorageManagement: Invalid root boot parameter.");
|
||||
}
|
||||
if (parse_failure) {
|
||||
PANIC("StorageManagement: Invalid root boot parameter.");
|
||||
}
|
||||
|
||||
return address_parameters;
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::resolve_partition_from_boot_device_parameter(StorageDevice const& chosen_storage_device, StringView boot_device_prefix)
|
||||
{
|
||||
auto possible_partition_number = extract_boot_device_partition_number_parameter(boot_device_prefix);
|
||||
if (!possible_partition_number.has_value())
|
||||
return;
|
||||
|
||||
auto partition_number = possible_partition_number.value();
|
||||
if (chosen_storage_device.partitions().size() <= partition_number)
|
||||
PANIC("StorageManagement: Invalid partition number parameter.");
|
||||
m_boot_block_device = chosen_storage_device.partitions()[partition_number];
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_hardware_relative_boot_device(StringView relative_hardware_prefix, Function<bool(StorageDevice const&)> filter_device_callback)
|
||||
{
|
||||
VERIFY(m_boot_argument.starts_with(relative_hardware_prefix));
|
||||
auto address_parameters = extract_boot_device_address_parameters(relative_hardware_prefix);
|
||||
|
||||
RefPtr<StorageDevice> chosen_storage_device;
|
||||
|
||||
for (auto& storage_device : m_storage_devices) {
|
||||
if (!filter_device_callback(storage_device))
|
||||
continue;
|
||||
auto storage_device_lun = storage_device.logical_unit_number_address();
|
||||
if (storage_device.parent_controller_hardware_relative_id() == address_parameters[0]
|
||||
&& storage_device_lun.target_id == address_parameters[1]
|
||||
&& storage_device_lun.disk_id == address_parameters[2]) {
|
||||
m_boot_block_device = storage_device;
|
||||
chosen_storage_device = storage_device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_boot_block_device.is_null()) {
|
||||
dump_storage_devices_and_partitions();
|
||||
PANIC("StorageManagement: boot device {} not found", m_boot_argument);
|
||||
if (chosen_storage_device)
|
||||
resolve_partition_from_boot_device_parameter(*chosen_storage_device, relative_hardware_prefix);
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_ata_boot_device()
|
||||
{
|
||||
determine_hardware_relative_boot_device(ata_device_prefix, [](StorageDevice const& device) -> bool {
|
||||
return device.command_set() == StorageDevice::CommandSet::ATA;
|
||||
});
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_nvme_boot_device()
|
||||
{
|
||||
determine_hardware_relative_boot_device(nvme_device_prefix, [](StorageDevice const& device) -> bool {
|
||||
return device.command_set() == StorageDevice::CommandSet::NVMe;
|
||||
});
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_ramdisk_boot_device()
|
||||
{
|
||||
determine_hardware_relative_boot_device(ramdisk_device_prefix, [](StorageDevice const& device) -> bool {
|
||||
return device.command_set() == StorageDevice::CommandSet::PlainMemory;
|
||||
});
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_block_boot_device()
|
||||
{
|
||||
VERIFY(m_boot_argument.starts_with(block_device_prefix));
|
||||
auto parameters_view = extract_boot_device_address_parameters(block_device_prefix);
|
||||
|
||||
// Note: We simply fetch the corresponding BlockDevice with the major and minor parameters.
|
||||
// We don't try to accept and resolve a partition number as it will make this code much more
|
||||
// complicated. This rule is also explained in the boot_device_addressing(7) manual page.
|
||||
LockRefPtr<Device> device = DeviceManagement::the().get_device(parameters_view[0], parameters_view[1]);
|
||||
if (device && device->is_block_device())
|
||||
m_boot_block_device = static_ptr_cast<BlockDevice>(device);
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_logical_unit_number()
|
||||
{
|
||||
VERIFY(m_boot_argument.starts_with(logical_unit_number_device_prefix));
|
||||
auto address_parameters = extract_boot_device_address_parameters(logical_unit_number_device_prefix);
|
||||
|
||||
RefPtr<StorageDevice> chosen_storage_device;
|
||||
|
||||
for (auto& storage_device : m_storage_devices) {
|
||||
auto storage_device_lun = storage_device.logical_unit_number_address();
|
||||
if (storage_device_lun.controller_id == address_parameters[0]
|
||||
&& storage_device_lun.target_id == address_parameters[1]
|
||||
&& storage_device_lun.disk_id == address_parameters[2]) {
|
||||
m_boot_block_device = storage_device;
|
||||
chosen_storage_device = storage_device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (chosen_storage_device)
|
||||
resolve_partition_from_boot_device_parameter(*chosen_storage_device, logical_unit_number_device_prefix);
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_boot_device()
|
||||
{
|
||||
VERIFY(!m_controllers.is_empty());
|
||||
|
||||
if (m_boot_argument.starts_with(block_device_prefix)) {
|
||||
determine_block_boot_device();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_boot_argument.starts_with(partition_uuid_prefix)) {
|
||||
determine_boot_device_with_partition_uuid();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_boot_argument.starts_with(logical_unit_number_device_prefix)) {
|
||||
determine_boot_device_with_logical_unit_number();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_boot_argument.starts_with(ata_device_prefix)) {
|
||||
determine_ata_boot_device();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_boot_argument.starts_with(ramdisk_device_prefix)) {
|
||||
determine_ramdisk_boot_device();
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_boot_argument.starts_with(nvme_device_prefix)) {
|
||||
determine_nvme_boot_device();
|
||||
return;
|
||||
}
|
||||
PANIC("StorageManagement: Invalid root boot parameter.");
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_partition_uuid()
|
||||
|
@ -289,11 +442,12 @@ UNMAP_AFTER_INIT void StorageManagement::initialize(StringView root_device, bool
|
|||
m_controllers.append(RamdiskController::initialize());
|
||||
enumerate_storage_devices();
|
||||
enumerate_disk_partitions();
|
||||
if (!boot_argument_contains_partition_uuid()) {
|
||||
determine_boot_device();
|
||||
return;
|
||||
|
||||
determine_boot_device();
|
||||
if (m_boot_block_device.is_null()) {
|
||||
dump_storage_devices_and_partitions();
|
||||
PANIC("StorageManagement: boot device {} not found", m_boot_argument);
|
||||
}
|
||||
determine_boot_device_with_partition_uuid();
|
||||
}
|
||||
|
||||
StorageManagement& StorageManagement::the()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue