1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +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:
Liav A 2020-12-26 16:53:30 +02:00 committed by Andreas Kling
parent bc18712adf
commit 72b1998f0d
20 changed files with 639 additions and 561 deletions

View file

@ -33,176 +33,69 @@
namespace Kernel {
EBRPartitionTable::EBRPartitionTable(NonnullRefPtr<BlockDevice> device)
: m_device(move(device))
Result<NonnullOwnPtr<EBRPartitionTable>, PartitionTable::Error> EBRPartitionTable::try_to_initialize(const StorageDevice& device)
{
auto table = make<EBRPartitionTable>(device);
if (table->is_protective_mbr())
return { PartitionTable::Error::MBRProtective };
if (!table->is_valid())
return { PartitionTable::Error::Invalid };
return table;
}
void EBRPartitionTable::search_extended_partition(const StorageDevice& device, MBRPartitionTable& checked_ebr, u64 current_block_offset, size_t limit)
{
if (limit == 0)
return;
// EBRs should not carry more than 2 partitions (because they need to form a linked list)
ASSERT(checked_ebr.partitions_count() <= 2);
auto checked_logical_partition = checked_ebr.partition(0);
// If we are pointed to an invalid logical partition, something is seriously wrong.
ASSERT(checked_logical_partition.has_value());
m_partitions.append(checked_logical_partition.value().offset(current_block_offset));
if (!checked_ebr.contains_ebr())
return;
current_block_offset += checked_ebr.partition(1).value().start_block();
auto next_ebr = MBRPartitionTable::try_to_initialize(device, current_block_offset);
if (!next_ebr)
return;
search_extended_partition(device, *next_ebr, current_block_offset, (limit - 1));
}
EBRPartitionTable::EBRPartitionTable(const StorageDevice& device)
: MBRPartitionTable(device)
{
if (!is_header_valid())
return;
m_valid = true;
ASSERT(partitions_count() == 0);
auto& header = this->header();
for (size_t index = 0; index < 4; index++) {
auto& entry = header.entry[index];
// Start enumerating all logical partitions
if (entry.type == 0xf) {
auto checked_ebr = MBRPartitionTable::try_to_initialize(device, entry.offset);
if (!checked_ebr)
continue;
// It's quite unlikely to see that amount of partitions, so stop at 128 partitions.
search_extended_partition(device, *checked_ebr, entry.offset, 128);
continue;
}
if (entry.offset == 0x00) {
continue;
}
auto partition_type = ByteBuffer::create_zeroed(sizeof(u8));
partition_type.data()[0] = entry.type;
m_partitions.append(DiskPartitionMetadata({ entry.offset, (entry.offset + entry.length), partition_type }));
}
}
EBRPartitionTable::~EBRPartitionTable()
{
}
const MBRPartitionHeader& EBRPartitionTable::header() const
{
return *reinterpret_cast<const MBRPartitionHeader*>(m_cached_mbr_header);
}
const EBRPartitionExtension& EBRPartitionTable::ebr_extension() const
{
return *reinterpret_cast<const EBRPartitionExtension*>(m_cached_ebr_header);
}
int EBRPartitionTable::index_of_ebr_container() const
{
for (int i = 0; i < 4; i++) {
if (header().entry[i].type == EBR_CHS_CONTAINER || header().entry[i].type == EBR_LBA_CONTAINER)
return i;
}
ASSERT_NOT_REACHED();
}
bool EBRPartitionTable::initialize()
{
auto mbr_header_request = m_device->make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read,
0, 1, UserOrKernelBuffer::for_kernel_buffer(m_cached_mbr_header), sizeof(m_cached_mbr_header));
auto mbr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_mbr_header);
if (!m_device->read_block(0, mbr_header_buffer)) {
return false;
}
auto& header = this->header();
m_ebr_container_id = index_of_ebr_container() + 1;
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::initialize: MBR_signature=0x" << String::format("%x", header.mbr_signature);
#endif
if (header.mbr_signature != MBR_SIGNATURE) {
klog() << "EBRPartitionTable::initialize: bad MBR signature 0x" << String::format("%x", header.mbr_signature);
return false;
}
auto& ebr_entry = header.entry[m_ebr_container_id - 1];
auto ebr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_ebr_header);
if (!m_device->read_block(ebr_entry.offset, ebr_header_buffer)) {
return false;
}
size_t index = 1;
while (index < 128) { // Unlikely to encounter a disk with 128 partitions in this configuration...
if (ebr_extension().next_chained_ebr_extension.offset == 0 && ebr_extension().next_chained_ebr_extension.type == 0) {
break;
}
index++;
if (!m_device->read_block(ebr_extension().next_chained_ebr_extension.offset, ebr_header_buffer)) {
return false;
}
}
m_ebr_chained_extensions_count = index;
klog() << "EBRPartitionTable::initialize: Extended partitions count - " << m_ebr_chained_extensions_count;
return true;
}
RefPtr<DiskPartition> EBRPartitionTable::get_non_extended_partition(unsigned index)
{
auto& header = this->header();
auto& entry = header.entry[index - 1];
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: status=0x" << String::format("%x", entry.status) << " offset=0x" << String::format("%x", entry.offset);
#endif
if (entry.offset == 0x00) {
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: missing partition requested index=" << index;
#endif
return nullptr;
}
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.type);
#endif
return DiskPartition::create(m_device, entry.offset, (entry.offset + entry.length));
}
RefPtr<DiskPartition> EBRPartitionTable::get_extended_partition(unsigned index)
{
unsigned relative_index = index - m_ebr_container_id;
auto& header = this->header();
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: relative index " << relative_index;
#endif
auto& ebr_entry = header.entry[m_ebr_container_id - 1];
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: Extended partition, offset 0x" << String::format("%x", ebr_entry.offset) << ", type " << String::format("%x", ebr_entry.type);
#endif
auto ebr_header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_ebr_header);
if (!m_device->read_block(ebr_entry.offset, ebr_header_buffer)) {
return nullptr;
}
size_t i = 0;
while (i < relative_index) {
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: logical partition, relative offset 0x" << String::format("%x", ebr_extension().entry.offset) << ", type " << String::format("%x", ebr_extension().entry.type);
klog() << "EBRPartitionTable::partition: next logical partition, relative offset 0x" << String::format("%x", ebr_extension().next_chained_ebr_extension.offset) << ", type " << String::format("%x", ebr_extension().next_chained_ebr_extension.type);
#endif
if (ebr_extension().next_chained_ebr_extension.offset == 0 && ebr_extension().next_chained_ebr_extension.type == 0) {
break;
}
i++;
if (!m_device->read_block(ebr_extension().next_chained_ebr_extension.offset, ebr_header_buffer)) {
return nullptr;
}
}
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: status=" << String::format("%x", ebr_extension().entry.status) << " offset=" << String::format("%x", ebr_extension().entry.offset + ebr_entry.offset);
#endif
if (ebr_extension().entry.offset == 0x00) {
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: missing partition requested index=" << index;
#endif
return nullptr;
}
#ifdef EBR_DEBUG
klog() << "EBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", ebr_extension().entry.type);
#endif
return DiskPartition::create(m_device, ebr_extension().entry.offset + ebr_entry.offset, (ebr_extension().entry.offset + ebr_entry.offset + ebr_extension().entry.length));
}
bool EBRPartitionTable::index_is_extended_partition(unsigned index) const
{
return !(m_ebr_container_id > index || index > (m_ebr_container_id + m_ebr_chained_extensions_count));
}
RefPtr<DiskPartition> EBRPartitionTable::partition(unsigned index)
{
ASSERT(index >= 1 && index <= m_ebr_chained_extensions_count + 4);
auto& header = this->header();
if (header.mbr_signature != MBR_SIGNATURE) {
klog() << "EBRPartitionTable::initialize: bad MBR signature - not initialized? 0x" << String::format("%x", header.mbr_signature);
return nullptr;
}
if (index_is_extended_partition(index))
return get_extended_partition(index);
if (index > 4)
return get_non_extended_partition(index - m_ebr_chained_extensions_count);
return get_non_extended_partition(index);
}
}