Serenity Operating System
at master 153 lines 4.9 kB view raw
1/* 2 * Copyright (c) 2020-2022, Liav A. <liavalb@hotmail.co.il> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/Debug.h> 8#include <LibPartition/GUIDPartitionTable.h> 9 10#ifndef KERNEL 11# include <LibCore/DeprecatedFile.h> 12#endif 13 14namespace Partition { 15 16#define GPT_SIGNATURE2 0x54524150 17#define GPT_SIGNATURE 0x20494645 18 19struct [[gnu::packed]] GPTPartitionEntry { 20 u8 partition_guid[16]; 21 u8 unique_guid[16]; 22 23 u64 first_lba; 24 u64 last_lba; 25 26 u64 attributes; 27 char partition_name[72]; 28}; 29 30struct [[gnu::packed]] GUIDPartitionHeader { 31 u32 sig[2]; 32 u32 revision; 33 u32 header_size; 34 u32 crc32_header; 35 u32 reserved; 36 u64 current_lba; 37 u64 backup_lba; 38 39 u64 first_usable_lba; 40 u64 last_usable_lba; 41 42 u64 disk_guid1[2]; 43 44 u64 partition_array_start_lba; 45 46 u32 entries_count; 47 u32 partition_entry_size; 48 u32 crc32_entries_array; 49}; 50 51#ifdef KERNEL 52ErrorOr<NonnullOwnPtr<GUIDPartitionTable>> GUIDPartitionTable::try_to_initialize(Kernel::StorageDevice& device) 53{ 54 auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GUIDPartitionTable(device))); 55#else 56ErrorOr<NonnullOwnPtr<GUIDPartitionTable>> GUIDPartitionTable::try_to_initialize(NonnullRefPtr<Core::DeprecatedFile> device_file) 57{ 58 auto table = TRY(adopt_nonnull_own_or_enomem(new (nothrow) GUIDPartitionTable(move(device_file)))); 59#endif 60 if (!table->is_valid()) 61 return Error::from_errno(EINVAL); 62 return table; 63} 64 65#ifdef KERNEL 66GUIDPartitionTable::GUIDPartitionTable(Kernel::StorageDevice& device) 67 : MBRPartitionTable(device) 68#else 69GUIDPartitionTable::GUIDPartitionTable(NonnullRefPtr<Core::DeprecatedFile> device_file) 70 : MBRPartitionTable(move(device_file)) 71#endif 72{ 73 // FIXME: Handle OOM failure here. 74 m_cached_header = ByteBuffer::create_zeroed(m_block_size).release_value_but_fixme_should_propagate_errors(); 75 VERIFY(partitions_count() == 0); 76 if (!initialize()) 77 m_valid = false; 78} 79 80GUIDPartitionHeader const& GUIDPartitionTable::header() const 81{ 82 return *(GUIDPartitionHeader const*)m_cached_header.data(); 83} 84 85bool GUIDPartitionTable::initialize() 86{ 87 VERIFY(m_cached_header.data() != nullptr); 88 89 auto first_gpt_block = (m_block_size == 512) ? 1 : 0; 90 91#ifdef KERNEL 92 auto buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header.data()); 93 if (!m_device->read_block(first_gpt_block, buffer)) 94 return false; 95#else 96 m_device_file->seek(first_gpt_block * m_block_size); 97 if (m_device_file->read(m_cached_header.data(), m_cached_header.size()) != (int)m_block_size) 98 return false; 99#endif 100 101 dbgln_if(GPT_DEBUG, "GUIDPartitionTable: signature - {:#08x} {:#08x}", header().sig[1], header().sig[0]); 102 103 if (header().sig[0] != GPT_SIGNATURE && header().sig[1] != GPT_SIGNATURE2) { 104 dbgln("GUIDPartitionTable: bad signature {:#08x} {:#08x}", header().sig[1], header().sig[0]); 105 return false; 106 } 107 108 auto entries_buffer_result = ByteBuffer::create_zeroed(m_block_size); 109 if (entries_buffer_result.is_error()) { 110 dbgln("GUIDPartitionTable: not enough memory for entries buffer"); 111 return false; 112 } 113 auto entries_buffer = entries_buffer_result.release_value(); 114#ifdef KERNEL 115 auto raw_entries_buffer = UserOrKernelBuffer::for_kernel_buffer(entries_buffer.data()); 116#endif 117 size_t raw_byte_index = header().partition_array_start_lba * m_block_size; 118 for (size_t entry_index = 0; entry_index < header().entries_count; entry_index++) { 119 120#ifdef KERNEL 121 if (!m_device->read_block((raw_byte_index / m_block_size), raw_entries_buffer)) 122 return false; 123#else 124 m_device_file->seek(raw_byte_index); 125 if (m_device_file->read(entries_buffer.data(), entries_buffer.size()) != (int)m_block_size) 126 return false; 127#endif 128 auto* entries = (GPTPartitionEntry const*)entries_buffer.data(); 129 auto& entry = entries[entry_index % (m_block_size / (size_t)header().partition_entry_size)]; 130 Array<u8, 16> partition_type {}; 131 partition_type.span().overwrite(0, entry.partition_guid, partition_type.size()); 132 133 if (is_unused_entry(partition_type)) { 134 raw_byte_index += header().partition_entry_size; 135 continue; 136 } 137 138 Array<u8, 16> unique_guid {}; 139 unique_guid.span().overwrite(0, entry.unique_guid, unique_guid.size()); 140 dbgln("Detected GPT partition (entry={}), offset={}, limit={}", entry_index, entry.first_lba, entry.last_lba); 141 m_partitions.append({ entry.first_lba, entry.last_lba, partition_type, unique_guid, entry.attributes }); 142 raw_byte_index += header().partition_entry_size; 143 } 144 145 return true; 146} 147 148bool GUIDPartitionTable::is_unused_entry(Array<u8, 16> partition_type) const 149{ 150 return all_of(partition_type, [](auto const octet) { return octet == 0; }); 151} 152 153}