Kernel: Cleaner partition parsing errors

This commit is contained in:
Bananymous 2023-04-03 09:55:49 +03:00
parent e322826347
commit ebfd092075
2 changed files with 9 additions and 3 deletions

View File

@ -43,7 +43,7 @@ namespace Kernel
{
s_instance->add_device(device);
if (auto res = device->initialize_partitions(); res.is_error())
dprintln("{}: {}", device->name(), res.error());
dprintln("{}", res.error());
else
{
for (auto* partition : device->partitions())

View File

@ -146,23 +146,29 @@ namespace Kernel
BAN::ErrorOr<void> StorageDevice::initialize_partitions()
{
if (total_size() < sizeof(GPTHeader))
return BAN::Error::from_format("Disk {} does not have enough space for GPT header", name());
BAN::Vector<uint8_t> lba1(sector_size());
TRY(read_sectors(1, 1, lba1.data()));
const GPTHeader& header = *(const GPTHeader*)lba1.data();
if (!is_valid_gpt_header(header, sector_size()))
return BAN::Error::from_c_string("Invalid GPT header");
return BAN::Error::from_format("Disk {} has invalid GPT header", name());
uint32_t size = header.partition_entry_count * header.partition_entry_size;
if (uint32_t remainder = size % sector_size())
size += sector_size() - remainder;
if (total_size() < header.partition_entry_lba * sector_size() + size)
return BAN::Error::from_format("Disk {} has invalid GPT header", name());
BAN::Vector<uint8_t> entry_array;
TRY(entry_array.resize(size));
TRY(read_sectors(header.partition_entry_lba, size / sector_size(), entry_array.data()));
if (!is_valid_gpt_crc32(header, lba1, entry_array))
return BAN::Error::from_c_string("Invalid crc3 in the GPT header");
return BAN::Error::from_format("Disk {} has invalid crc3 in the GPT header", name());
for (uint32_t i = 0; i < header.partition_entry_count; i++)
{