2020-12-26 17:53:30 +03:00
|
|
|
/*
|
2022-04-25 19:54:06 +03:00
|
|
|
* Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il>
|
2020-12-26 17:53:30 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-26 17:53:30 +03:00
|
|
|
*/
|
|
|
|
|
2022-03-02 04:21:35 +03:00
|
|
|
#include <AK/Debug.h>
|
|
|
|
#include <LibPartition/GUIDPartitionTable.h>
|
2020-12-26 17:53:30 +03:00
|
|
|
|
2022-03-02 04:21:35 +03:00
|
|
|
namespace Partition {
|
2020-12-26 17:53:30 +03:00
|
|
|
|
|
|
|
#define GPT_SIGNATURE2 0x54524150
|
|
|
|
#define GPT_SIGNATURE 0x20494645
|
|
|
|
|
2020-12-31 00:44:54 +03:00
|
|
|
struct [[gnu::packed]] GPTPartitionEntry {
|
2020-12-26 17:53:30 +03:00
|
|
|
u8 partition_guid[16];
|
|
|
|
u8 unique_guid[16];
|
|
|
|
|
|
|
|
u64 first_lba;
|
|
|
|
u64 last_lba;
|
|
|
|
|
|
|
|
u64 attributes;
|
|
|
|
char partition_name[72];
|
|
|
|
};
|
|
|
|
|
2020-12-31 00:44:54 +03:00
|
|
|
struct [[gnu::packed]] GUIDPartitionHeader {
|
2020-12-26 17:53:30 +03:00
|
|
|
u32 sig[2];
|
|
|
|
u32 revision;
|
|
|
|
u32 header_size;
|
|
|
|
u32 crc32_header;
|
|
|
|
u32 reserved;
|
|
|
|
u64 current_lba;
|
|
|
|
u64 backup_lba;
|
|
|
|
|
|
|
|
u64 first_usable_lba;
|
|
|
|
u64 last_usable_lba;
|
|
|
|
|
|
|
|
u64 disk_guid1[2];
|
|
|
|
|
|
|
|
u64 partition_array_start_lba;
|
|
|
|
|
|
|
|
u32 entries_count;
|
|
|
|
u32 partition_entry_size;
|
|
|
|
u32 crc32_entries_array;
|
|
|
|
};
|
|
|
|
|
2023-05-21 21:33:09 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<GUIDPartitionTable>> GUIDPartitionTable::try_to_initialize(PartitionableDevice device)
|
2022-06-16 05:57:05 +03:00
|
|
|
{
|
2023-05-21 21:33:09 +03:00
|
|
|
auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GUIDPartitionTable(move(device))));
|
2020-12-26 17:53:30 +03:00
|
|
|
if (!table->is_valid())
|
2022-04-25 19:54:06 +03:00
|
|
|
return Error::from_errno(EINVAL);
|
2020-12-26 17:53:30 +03:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2023-05-21 21:33:09 +03:00
|
|
|
GUIDPartitionTable::GUIDPartitionTable(PartitionableDevice device)
|
|
|
|
: MBRPartitionTable(move(device))
|
2020-12-26 17:53:30 +03:00
|
|
|
{
|
2021-09-06 01:59:52 +03:00
|
|
|
// FIXME: Handle OOM failure here.
|
2023-05-21 21:33:09 +03:00
|
|
|
m_cached_header = ByteBuffer::create_zeroed(block_size()).release_value_but_fixme_should_propagate_errors();
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(partitions_count() == 0);
|
2020-12-26 17:53:30 +03:00
|
|
|
if (!initialize())
|
|
|
|
m_valid = false;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
GUIDPartitionHeader const& GUIDPartitionTable::header() const
|
2020-12-26 17:53:30 +03:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
return *(GUIDPartitionHeader const*)m_cached_header.data();
|
2020-12-26 17:53:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GUIDPartitionTable::initialize()
|
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_cached_header.data() != nullptr);
|
2020-12-26 17:53:30 +03:00
|
|
|
|
2023-05-21 21:33:09 +03:00
|
|
|
auto first_gpt_block = (block_size() == 512) ? 1 : 0;
|
2020-12-26 17:53:30 +03:00
|
|
|
|
2023-05-21 21:33:09 +03:00
|
|
|
auto maybe_error = m_device.read_block(first_gpt_block, m_cached_header.bytes());
|
|
|
|
if (maybe_error.is_error())
|
2022-06-16 05:57:05 +03:00
|
|
|
return false;
|
2020-12-26 17:53:30 +03:00
|
|
|
|
2021-03-12 16:02:17 +03:00
|
|
|
dbgln_if(GPT_DEBUG, "GUIDPartitionTable: signature - {:#08x} {:#08x}", header().sig[1], header().sig[0]);
|
2020-12-26 17:53:30 +03:00
|
|
|
|
|
|
|
if (header().sig[0] != GPT_SIGNATURE && header().sig[1] != GPT_SIGNATURE2) {
|
2021-03-12 16:02:17 +03:00
|
|
|
dbgln("GUIDPartitionTable: bad signature {:#08x} {:#08x}", header().sig[1], header().sig[0]);
|
2020-12-26 17:53:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-21 21:33:09 +03:00
|
|
|
auto entries_buffer_result = ByteBuffer::create_zeroed(block_size());
|
2022-01-20 20:47:39 +03:00
|
|
|
if (entries_buffer_result.is_error()) {
|
2022-06-16 05:57:05 +03:00
|
|
|
dbgln("GUIDPartitionTable: not enough memory for entries buffer");
|
2021-09-06 01:59:52 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto entries_buffer = entries_buffer_result.release_value();
|
2023-05-21 21:33:09 +03:00
|
|
|
size_t raw_byte_index = header().partition_array_start_lba * block_size();
|
2020-12-26 17:53:30 +03:00
|
|
|
for (size_t entry_index = 0; entry_index < header().entries_count; entry_index++) {
|
2023-05-21 21:33:09 +03:00
|
|
|
maybe_error = m_device.read_block(raw_byte_index / block_size(), entries_buffer.bytes());
|
|
|
|
if (maybe_error.is_error())
|
2022-06-16 05:57:05 +03:00
|
|
|
return false;
|
2022-04-01 20:58:27 +03:00
|
|
|
auto* entries = (GPTPartitionEntry const*)entries_buffer.data();
|
2023-05-21 21:33:09 +03:00
|
|
|
auto& entry = entries[entry_index % (block_size() / header().partition_entry_size)];
|
2020-12-31 14:17:03 +03:00
|
|
|
Array<u8, 16> partition_type {};
|
|
|
|
partition_type.span().overwrite(0, entry.partition_guid, partition_type.size());
|
2020-12-26 17:53:30 +03:00
|
|
|
|
|
|
|
if (is_unused_entry(partition_type)) {
|
|
|
|
raw_byte_index += header().partition_entry_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-31 14:17:03 +03:00
|
|
|
Array<u8, 16> unique_guid {};
|
|
|
|
unique_guid.span().overwrite(0, entry.unique_guid, unique_guid.size());
|
2021-01-10 17:43:09 +03:00
|
|
|
dbgln("Detected GPT partition (entry={}), offset={}, limit={}", entry_index, entry.first_lba, entry.last_lba);
|
2021-10-03 02:39:33 +03:00
|
|
|
m_partitions.append({ entry.first_lba, entry.last_lba, partition_type, unique_guid, entry.attributes });
|
2020-12-26 17:53:30 +03:00
|
|
|
raw_byte_index += header().partition_entry_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-31 14:17:03 +03:00
|
|
|
bool GUIDPartitionTable::is_unused_entry(Array<u8, 16> partition_type) const
|
2020-12-26 17:53:30 +03:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
return all_of(partition_type, [](auto const octet) { return octet == 0; });
|
2020-12-26 17:53:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|