mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:07:35 +00:00
Kernel: Move Partition code files to the Storage folder
This folder is more appropriate for these files.
This commit is contained in:
parent
247517cd4a
commit
3a19e18d1e
10 changed files with 17 additions and 17 deletions
106
Kernel/Storage/Partition/DiskPartition.cpp
Normal file
106
Kernel/Storage/Partition/DiskPartition.cpp
Normal file
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <Kernel/FileSystem/FileDescription.h>
|
||||
#include <Kernel/Storage/Partition/DiskPartition.h>
|
||||
|
||||
// #define OFFD_DEBUG
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned block_offset, unsigned block_limit)
|
||||
{
|
||||
return adopt(*new DiskPartition(device, block_offset, block_limit));
|
||||
}
|
||||
|
||||
DiskPartition::DiskPartition(BlockDevice& device, unsigned block_offset, unsigned block_limit)
|
||||
: BlockDevice(100, 0, device.block_size())
|
||||
, m_device(device)
|
||||
, m_block_offset(block_offset)
|
||||
, m_block_limit(block_limit)
|
||||
{
|
||||
}
|
||||
|
||||
DiskPartition::~DiskPartition()
|
||||
{
|
||||
}
|
||||
|
||||
void DiskPartition::start_request(AsyncBlockDeviceRequest& request)
|
||||
{
|
||||
request.add_sub_request(m_device->make_request<AsyncBlockDeviceRequest>(request.request_type(),
|
||||
request.block_index() + m_block_offset, request.block_count(), request.buffer(), request.buffer_size()));
|
||||
}
|
||||
|
||||
KResultOr<size_t> DiskPartition::read(FileDescription& fd, size_t offset, UserOrKernelBuffer& outbuf, size_t len)
|
||||
{
|
||||
unsigned adjust = m_block_offset * block_size();
|
||||
|
||||
#ifdef OFFD_DEBUG
|
||||
klog() << "DiskPartition::read offset=" << fd.offset() << " adjust=" << adjust << " len=" << len;
|
||||
#endif
|
||||
|
||||
return m_device->read(fd, offset + adjust, outbuf, len);
|
||||
}
|
||||
|
||||
bool DiskPartition::can_read(const FileDescription& fd, size_t offset) const
|
||||
{
|
||||
unsigned adjust = m_block_offset * block_size();
|
||||
|
||||
#ifdef OFFD_DEBUG
|
||||
klog() << "DiskPartition::can_read offset=" << offset << " adjust=" << adjust;
|
||||
#endif
|
||||
|
||||
return m_device->can_read(fd, offset + adjust);
|
||||
}
|
||||
|
||||
KResultOr<size_t> DiskPartition::write(FileDescription& fd, size_t offset, const UserOrKernelBuffer& inbuf, size_t len)
|
||||
{
|
||||
unsigned adjust = m_block_offset * block_size();
|
||||
|
||||
#ifdef OFFD_DEBUG
|
||||
klog() << "DiskPartition::write offset=" << offset << " adjust=" << adjust << " len=" << len;
|
||||
#endif
|
||||
|
||||
return m_device->write(fd, offset + adjust, inbuf, len);
|
||||
}
|
||||
|
||||
bool DiskPartition::can_write(const FileDescription& fd, size_t offset) const
|
||||
{
|
||||
unsigned adjust = m_block_offset * block_size();
|
||||
|
||||
#ifdef OFFD_DEBUG
|
||||
klog() << "DiskPartition::can_write offset=" << offset << " adjust=" << adjust;
|
||||
#endif
|
||||
|
||||
return m_device->can_write(fd, offset + adjust);
|
||||
}
|
||||
|
||||
const char* DiskPartition::class_name() const
|
||||
{
|
||||
return "DiskPartition";
|
||||
}
|
||||
|
||||
}
|
60
Kernel/Storage/Partition/DiskPartition.h
Normal file
60
Kernel/Storage/Partition/DiskPartition.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefPtr.h>
|
||||
#include <Kernel/Devices/BlockDevice.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
class DiskPartition final : public BlockDevice {
|
||||
public:
|
||||
static NonnullRefPtr<DiskPartition> create(BlockDevice&, unsigned block_offset, unsigned block_limit);
|
||||
virtual ~DiskPartition();
|
||||
|
||||
virtual void start_request(AsyncBlockDeviceRequest&) override;
|
||||
|
||||
// ^BlockDevice
|
||||
virtual KResultOr<size_t> read(FileDescription&, size_t, UserOrKernelBuffer&, size_t) override;
|
||||
virtual bool can_read(const FileDescription&, size_t) const override;
|
||||
virtual KResultOr<size_t> write(FileDescription&, size_t, const UserOrKernelBuffer&, size_t) override;
|
||||
virtual bool can_write(const FileDescription&, size_t) const override;
|
||||
|
||||
// ^Device
|
||||
virtual mode_t required_mode() const override { return 0600; }
|
||||
|
||||
private:
|
||||
virtual const char* class_name() const override;
|
||||
|
||||
DiskPartition(BlockDevice&, unsigned block_offset, unsigned block_limit);
|
||||
|
||||
NonnullRefPtr<BlockDevice> m_device;
|
||||
unsigned m_block_offset;
|
||||
unsigned m_block_limit;
|
||||
};
|
||||
|
||||
}
|
208
Kernel/Storage/Partition/EBRPartitionTable.cpp
Normal file
208
Kernel/Storage/Partition/EBRPartitionTable.cpp
Normal file
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <Kernel/Storage/Partition/EBRPartitionTable.h>
|
||||
|
||||
#ifndef EBR_DEBUG
|
||||
# define EBR_DEBUG
|
||||
#endif
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
EBRPartitionTable::EBRPartitionTable(NonnullRefPtr<BlockDevice> device)
|
||||
: m_device(move(device))
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
71
Kernel/Storage/Partition/EBRPartitionTable.h
Normal file
71
Kernel/Storage/Partition/EBRPartitionTable.h
Normal file
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <Kernel/Storage/Partition/DiskPartition.h>
|
||||
#include <Kernel/Storage/Partition/MBRPartitionTable.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
struct [[gnu::packed]] EBRPartitionExtension
|
||||
{
|
||||
u8 unused_area[446];
|
||||
MBRPartitionEntry entry;
|
||||
MBRPartitionEntry next_chained_ebr_extension;
|
||||
MBRPartitionEntry unused[2];
|
||||
u16 mbr_signature;
|
||||
};
|
||||
|
||||
class EBRPartitionTable {
|
||||
|
||||
public:
|
||||
explicit EBRPartitionTable(NonnullRefPtr<BlockDevice>);
|
||||
~EBRPartitionTable();
|
||||
|
||||
bool initialize();
|
||||
RefPtr<DiskPartition> partition(unsigned index);
|
||||
|
||||
private:
|
||||
int index_of_ebr_container() const;
|
||||
NonnullRefPtr<BlockDevice> m_device;
|
||||
|
||||
const MBRPartitionHeader& header() const;
|
||||
const EBRPartitionExtension& ebr_extension() const;
|
||||
|
||||
bool index_is_extended_partition(unsigned index) const;
|
||||
|
||||
RefPtr<DiskPartition> get_extended_partition(unsigned index);
|
||||
RefPtr<DiskPartition> get_non_extended_partition(unsigned index);
|
||||
u8 m_ebr_container_id { 0 };
|
||||
size_t m_ebr_chained_extensions_count { 0 };
|
||||
u8 m_cached_mbr_header[512];
|
||||
u8 m_cached_ebr_header[512];
|
||||
};
|
||||
|
||||
}
|
109
Kernel/Storage/Partition/GPTPartitionTable.cpp
Normal file
109
Kernel/Storage/Partition/GPTPartitionTable.cpp
Normal file
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <Kernel/Storage/Partition/GPTPartitionTable.h>
|
||||
|
||||
#ifndef GPT_DEBUG
|
||||
# define GPT_DEBUG
|
||||
#endif
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
GPTPartitionTable::GPTPartitionTable(BlockDevice& device)
|
||||
: m_device(move(device))
|
||||
{
|
||||
}
|
||||
|
||||
GPTPartitionTable::~GPTPartitionTable()
|
||||
{
|
||||
}
|
||||
|
||||
const GPTPartitionHeader& GPTPartitionTable::header() const
|
||||
{
|
||||
return *reinterpret_cast<const GPTPartitionHeader*>(m_cached_header);
|
||||
}
|
||||
|
||||
bool GPTPartitionTable::initialize()
|
||||
{
|
||||
auto header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header);
|
||||
if (!m_device->read_block(1, header_buffer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& header = this->header();
|
||||
|
||||
#ifdef GPT_DEBUG
|
||||
klog() << "GPTPartitionTable::initialize: gpt_signature=0x" << String::format("%x", header.sig[1]) << String::format("%x", header.sig[0]);
|
||||
#endif
|
||||
|
||||
if (header.sig[0] != GPT_SIGNATURE && header.sig[1] != GPT_SIGNATURE2) {
|
||||
klog() << "GPTPartitionTable::initialize: bad GPT signature 0x" << String::format("%x", header.sig[1]) << String::format("%x", header.sig[0]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RefPtr<DiskPartition> GPTPartitionTable::partition(unsigned index)
|
||||
{
|
||||
ASSERT(index >= 1 && index <= 4294967294);
|
||||
|
||||
auto& header = this->header();
|
||||
unsigned lba = header.partition_array_start_lba + (((index - 1) * header.partition_entry_size) / BytesPerSector);
|
||||
|
||||
if (header.sig[0] != GPT_SIGNATURE) {
|
||||
klog() << "GPTPartitionTable::initialize: bad gpt signature - not initialized? 0x" << String::format("%x", header.sig);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
u8 entries_per_sector = BytesPerSector / header.partition_entry_size;
|
||||
|
||||
GPTPartitionEntry entries[entries_per_sector];
|
||||
auto entries_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&entries);
|
||||
this->m_device->read_block(lba, entries_buffer);
|
||||
GPTPartitionEntry& entry = entries[((index - 1) % entries_per_sector)];
|
||||
|
||||
#ifdef GPT_DEBUG
|
||||
klog() << "GPTPartitionTable::partition " << index;
|
||||
klog() << "GPTPartitionTable - offset = " << entry.first_lba[1] << entry.first_lba[0];
|
||||
#endif
|
||||
|
||||
if (entry.first_lba[0] == 0x00) {
|
||||
#ifdef GPT_DEBUG
|
||||
klog() << "GPTPartitionTable::partition: missing partition requested index=" << index;
|
||||
#endif
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#ifdef GPT_DEBUG
|
||||
klog() << "GPTPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.partition_guid[3]) << "-" << String::format("%x", entry.partition_guid[2]) << "-" << String::format("%x", entry.partition_guid[1]) << "-" << String::format("%x", entry.partition_guid[0]);
|
||||
#endif
|
||||
return DiskPartition::create(m_device, entry.first_lba[0], entry.last_lba[0]);
|
||||
}
|
||||
|
||||
}
|
91
Kernel/Storage/Partition/GPTPartitionTable.h
Normal file
91
Kernel/Storage/Partition/GPTPartitionTable.h
Normal file
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <Kernel/Storage/Partition/DiskPartition.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
#define GPT_SIGNATURE2 0x54524150
|
||||
#define GPT_SIGNATURE 0x20494645
|
||||
#define BytesPerSector 512
|
||||
|
||||
struct [[gnu::packed]] GPTPartitionEntry
|
||||
{
|
||||
u32 partition_guid[4];
|
||||
u32 unique_guid[4];
|
||||
|
||||
u32 first_lba[2];
|
||||
u32 last_lba[2];
|
||||
|
||||
u64 attributes;
|
||||
u8 partition_name[72];
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] GPTPartitionHeader
|
||||
{
|
||||
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;
|
||||
};
|
||||
|
||||
class GPTPartitionTable {
|
||||
|
||||
public:
|
||||
explicit GPTPartitionTable(BlockDevice&);
|
||||
~GPTPartitionTable();
|
||||
|
||||
bool initialize();
|
||||
RefPtr<DiskPartition> partition(unsigned index);
|
||||
|
||||
private:
|
||||
NonnullRefPtr<BlockDevice> m_device;
|
||||
|
||||
const GPTPartitionHeader& header() const;
|
||||
|
||||
u8 m_cached_header[512];
|
||||
};
|
||||
|
||||
}
|
116
Kernel/Storage/Partition/MBRPartitionTable.cpp
Normal file
116
Kernel/Storage/Partition/MBRPartitionTable.cpp
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <Kernel/Storage/Partition/MBRPartitionTable.h>
|
||||
|
||||
#ifndef MBR_DEBUG
|
||||
# define MBR_DEBUG
|
||||
#endif
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
MBRPartitionTable::MBRPartitionTable(NonnullRefPtr<BlockDevice> device)
|
||||
: m_device(move(device))
|
||||
{
|
||||
}
|
||||
|
||||
MBRPartitionTable::~MBRPartitionTable()
|
||||
{
|
||||
}
|
||||
|
||||
const MBRPartitionHeader& MBRPartitionTable::header() const
|
||||
{
|
||||
return *reinterpret_cast<const MBRPartitionHeader*>(m_cached_header);
|
||||
}
|
||||
|
||||
bool MBRPartitionTable::initialize()
|
||||
{
|
||||
auto header_buffer = UserOrKernelBuffer::for_kernel_buffer(m_cached_header);
|
||||
if (!m_device->read_block(0, header_buffer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& header = this->header();
|
||||
|
||||
#ifdef MBR_DEBUG
|
||||
klog() << "MBRPartitionTable::initialize: mbr_signature=0x" << String::format("%x", header.mbr_signature);
|
||||
#endif
|
||||
|
||||
if (header.mbr_signature != MBR_SIGNATURE) {
|
||||
klog() << "MBRPartitionTable::initialize: bad mbr signature 0x" << String::format("%x", header.mbr_signature);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MBRPartitionTable::contains_ebr() const
|
||||
{
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (header().entry[i].type == EBR_CHS_CONTAINER || header().entry[i].type == EBR_LBA_CONTAINER)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MBRPartitionTable::is_protective_mbr() const
|
||||
{
|
||||
return header().entry[0].type == MBR_PROTECTIVE;
|
||||
}
|
||||
|
||||
RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
|
||||
{
|
||||
ASSERT(index >= 1 && index <= 4);
|
||||
|
||||
auto& header = this->header();
|
||||
auto& entry = header.entry[index - 1];
|
||||
|
||||
if (header.mbr_signature != MBR_SIGNATURE) {
|
||||
klog() << "MBRPartitionTable::initialize: bad mbr signature - not initialized? 0x" << String::format("%x", header.mbr_signature);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#ifdef MBR_DEBUG
|
||||
klog() << "MBRPartitionTable::partition: status=0x" << String::format("%x", entry.status) << " offset=0x" << String::format("%x", entry.offset);
|
||||
#endif
|
||||
|
||||
if (entry.offset == 0x00) {
|
||||
#ifdef MBR_DEBUG
|
||||
klog() << "MBRPartitionTable::partition: missing partition requested index=" << index;
|
||||
#endif
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#ifdef MBR_DEBUG
|
||||
klog() << "MBRPartitionTable::partition: found partition index=" << index << " type=" << String::format("%x", entry.type);
|
||||
#endif
|
||||
|
||||
return DiskPartition::create(m_device, entry.offset, (entry.offset + entry.length));
|
||||
}
|
||||
|
||||
}
|
82
Kernel/Storage/Partition/MBRPartitionTable.h
Normal file
82
Kernel/Storage/Partition/MBRPartitionTable.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <Kernel/Storage/Partition/DiskPartition.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
#define MBR_SIGNATURE 0xaa55
|
||||
#define MBR_PROTECTIVE 0xEE
|
||||
#define EBR_CHS_CONTAINER 0x05
|
||||
#define EBR_LBA_CONTAINER 0x0F
|
||||
|
||||
struct [[gnu::packed]] MBRPartitionEntry
|
||||
{
|
||||
u8 status;
|
||||
u8 chs1[3];
|
||||
u8 type;
|
||||
u8 chs2[3];
|
||||
u32 offset;
|
||||
u32 length;
|
||||
};
|
||||
|
||||
struct [[gnu::packed]] MBRPartitionHeader
|
||||
{
|
||||
u8 code1[218];
|
||||
u16 ts_zero;
|
||||
u8 ts_drive, ts_seconds, ts_minutes, ts_hours;
|
||||
u8 code2[216];
|
||||
u32 disk_signature;
|
||||
u16 disk_signature_zero;
|
||||
MBRPartitionEntry entry[4];
|
||||
u16 mbr_signature;
|
||||
};
|
||||
|
||||
class MBRPartitionTable {
|
||||
AK_MAKE_ETERNAL
|
||||
|
||||
public:
|
||||
explicit MBRPartitionTable(NonnullRefPtr<BlockDevice>);
|
||||
~MBRPartitionTable();
|
||||
|
||||
bool initialize();
|
||||
bool is_protective_mbr() const;
|
||||
bool contains_ebr() const;
|
||||
RefPtr<DiskPartition> partition(unsigned index);
|
||||
|
||||
private:
|
||||
NonnullRefPtr<BlockDevice> m_device;
|
||||
|
||||
const MBRPartitionHeader& header() const;
|
||||
|
||||
u8 m_cached_header[512];
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue