forked from Bananymous/banan-os
Kernel: Cleaner partition parsing errors
This commit is contained in:
parent
e322826347
commit
ebfd092075
|
@ -43,7 +43,7 @@ namespace Kernel
|
||||||
{
|
{
|
||||||
s_instance->add_device(device);
|
s_instance->add_device(device);
|
||||||
if (auto res = device->initialize_partitions(); res.is_error())
|
if (auto res = device->initialize_partitions(); res.is_error())
|
||||||
dprintln("{}: {}", device->name(), res.error());
|
dprintln("{}", res.error());
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (auto* partition : device->partitions())
|
for (auto* partition : device->partitions())
|
||||||
|
|
|
@ -146,23 +146,29 @@ namespace Kernel
|
||||||
|
|
||||||
BAN::ErrorOr<void> StorageDevice::initialize_partitions()
|
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());
|
BAN::Vector<uint8_t> lba1(sector_size());
|
||||||
TRY(read_sectors(1, 1, lba1.data()));
|
TRY(read_sectors(1, 1, lba1.data()));
|
||||||
|
|
||||||
const GPTHeader& header = *(const GPTHeader*)lba1.data();
|
const GPTHeader& header = *(const GPTHeader*)lba1.data();
|
||||||
if (!is_valid_gpt_header(header, sector_size()))
|
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;
|
uint32_t size = header.partition_entry_count * header.partition_entry_size;
|
||||||
if (uint32_t remainder = size % sector_size())
|
if (uint32_t remainder = size % sector_size())
|
||||||
size += sector_size() - remainder;
|
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;
|
BAN::Vector<uint8_t> entry_array;
|
||||||
TRY(entry_array.resize(size));
|
TRY(entry_array.resize(size));
|
||||||
TRY(read_sectors(header.partition_entry_lba, size / sector_size(), entry_array.data()));
|
TRY(read_sectors(header.partition_entry_lba, size / sector_size(), entry_array.data()));
|
||||||
|
|
||||||
if (!is_valid_gpt_crc32(header, lba1, entry_array))
|
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++)
|
for (uint32_t i = 0; i < header.partition_entry_count; i++)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue