1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 00:37:35 +00:00

PartitionTable: Initial GPT Support, Adding Block Limit

Also added a script to handle creation of GPT partitioned disk (with
GRUB config file). Block limit will be used to disallow potential access
to other partitions.
This commit is contained in:
supercomputer7 2019-10-07 03:12:37 +03:00 committed by Andreas Kling
parent b5a043cf42
commit de49714f36
10 changed files with 273 additions and 13 deletions

View file

@ -2,15 +2,16 @@
// #define OFFD_DEBUG
NonnullRefPtr<DiskPartition> DiskPartition::create(DiskDevice& device, unsigned block_offset)
NonnullRefPtr<DiskPartition> DiskPartition::create(DiskDevice& device, unsigned block_offset, unsigned block_limit)
{
return adopt(*new DiskPartition(device, block_offset));
return adopt(*new DiskPartition(device, block_offset, block_limit));
}
DiskPartition::DiskPartition(DiskDevice& device, unsigned block_offset)
DiskPartition::DiskPartition(DiskDevice& device, unsigned block_offset, unsigned block_limit)
: DiskDevice(100, 0, device.block_size())
, m_device(device)
, m_block_offset(block_offset)
, m_block_limit(block_limit)
{
}

View file

@ -5,7 +5,7 @@
class DiskPartition final : public DiskDevice {
public:
static NonnullRefPtr<DiskPartition> create(DiskDevice&, unsigned block_offset);
static NonnullRefPtr<DiskPartition> create(DiskDevice&, unsigned block_offset, unsigned block_limit);
virtual ~DiskPartition();
virtual bool read_block(unsigned index, u8* out) const override;
@ -22,8 +22,9 @@ public:
private:
virtual const char* class_name() const override;
DiskPartition(DiskDevice&, unsigned block_offset);
DiskPartition(DiskDevice&, unsigned block_offset, unsigned block_limit);
NonnullRefPtr<DiskDevice> m_device;
unsigned m_block_offset;
unsigned m_block_limit;
};

View file

@ -0,0 +1,75 @@
#include <AK/ByteBuffer.h>
#include <Kernel/Devices/GPTPartitionTable.h>
#define GPT_DEBUG
GPTPartitionTable::GPTPartitionTable(DiskDevice& device)
: m_device(move(device))
{
}
GPTPartitionTable::~GPTPartitionTable()
{
}
const GPTPartitionHeader& GPTPartitionTable::header() const
{
return *reinterpret_cast<const GPTPartitionHeader*>(m_cached_header);
}
bool GPTPartitionTable::initialize()
{
if (!m_device->read_block(1, m_cached_header)) {
return false;
}
auto& header = this->header();
#ifdef GPT_DEBUG
kprintf("GPTPartitionTable::initialize: gpt_signature=%#x%x\n", header.sig[1], header.sig[0]);
#endif
if (header.sig[0] != GPT_SIGNATURE && header.sig[1] != GPT_SIGNATURE2) {
kprintf("GPTPartitionTable::initialize: bad GPT signature %#x%x\n", header.sig[1], 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) {
kprintf("GPTPartitionTable::initialize: bad gpt signature - not initalized? %#x\n", header.sig);
return nullptr;
}
u8 entries_per_sector = BytesPerSector / header.partition_entry_size;
GPTPartitionEntry entries[entries_per_sector];
this->m_device->read_blocks(lba, 1, (u8*)&entries);
GPTPartitionEntry& entry = entries[((index - 1) % entries_per_sector)];
#ifdef GPT_DEBUG
kprintf("GPTPartitionTable::partition %d\n", index);
kprintf("GPTPartitionTable - offset = %d%d\n", entry.first_lba[1], entry.first_lba[0]);
#endif
if (entry.first_lba[0] == 0x00) {
#ifdef GPT_DEBUG
kprintf("GPTPartitionTable::partition: missing partition requested index=%d\n", index);
#endif
return nullptr;
}
#ifdef GPT_DEBUG
kprintf("GPTPartitionTable::partition: found partition index=%d type=%x-%x-%x-%x\n", index, entry.partition_guid[3], entry.partition_guid[2], entry.partition_guid[1], entry.partition_guid[0]);
#endif
return DiskPartition::create(m_device, entry.first_lba[0], entry.last_lba[0]);
}

View file

@ -0,0 +1,60 @@
#pragma once
#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <Kernel/Devices/DiskDevice.h>
#include <Kernel/Devices/DiskPartition.h>
#define GPT_SIGNATURE2 0x54524150
#define GPT_SIGNATURE 0x20494645
#define BytesPerSector 512
struct GPTPartitionEntry {
u32 partition_guid[4];
u32 unique_guid[4];
u32 first_lba[2];
u32 last_lba[2];
u64 attributes;
u8 partition_name[72];
} __attribute__((packed));
struct 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;
} __attribute__((packed));
class GPTPartitionTable {
public:
explicit GPTPartitionTable(DiskDevice&);
~GPTPartitionTable();
bool initialize();
RefPtr<DiskPartition> partition(unsigned index);
private:
NonnullRefPtr<DiskDevice> m_device;
ByteBuffer read_header() const;
const GPTPartitionHeader& header() const;
u8 m_cached_header[512];
};

View file

@ -37,6 +37,11 @@ bool MBRPartitionTable::initialize()
return true;
}
bool MBRPartitionTable::is_protective_mbr() const
{
return header().entry[0].type == MBR_PROTECTIVE;
}
RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
{
ASSERT(index >= 1 && index <= 4);
@ -55,7 +60,7 @@ RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
if (entry.offset == 0x00) {
#ifdef MBR_DEBUG
kprintf("MBRPartitionTable::partition: missing partition requested index=%d\n", index);
kprintf("MBRPartitionTable::partition: missing partition requested index=%d\n", index);
#endif
return nullptr;
@ -65,5 +70,5 @@ RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
kprintf("MBRPartitionTable::partition: found partition index=%d type=%x\n", index, entry.type);
#endif
return DiskPartition::create(m_device, entry.offset);
return DiskPartition::create(m_device, entry.offset, (entry.offset + entry.length));
}

View file

@ -6,6 +6,7 @@
#include <Kernel/Devices/DiskPartition.h>
#define MBR_SIGNATURE 0xaa55
#define MBR_PROTECTIVE 0xEE
struct MBRPartitionEntry {
u8 status;
@ -35,6 +36,7 @@ public:
~MBRPartitionTable();
bool initialize();
bool is_protective_mbr() const;
RefPtr<DiskPartition> partition(unsigned index);
private: