mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:57:36 +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:
parent
b5a043cf42
commit
de49714f36
10 changed files with 273 additions and 13 deletions
|
@ -2,15 +2,16 @@
|
||||||
|
|
||||||
// #define OFFD_DEBUG
|
// #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())
|
: DiskDevice(100, 0, device.block_size())
|
||||||
, m_device(device)
|
, m_device(device)
|
||||||
, m_block_offset(block_offset)
|
, m_block_offset(block_offset)
|
||||||
|
, m_block_limit(block_limit)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
class DiskPartition final : public DiskDevice {
|
class DiskPartition final : public DiskDevice {
|
||||||
public:
|
public:
|
||||||
static NonnullRefPtr<DiskPartition> create(DiskDevice&, unsigned block_offset);
|
static NonnullRefPtr<DiskPartition> create(DiskDevice&, unsigned block_offset, unsigned block_limit);
|
||||||
virtual ~DiskPartition();
|
virtual ~DiskPartition();
|
||||||
|
|
||||||
virtual bool read_block(unsigned index, u8* out) const override;
|
virtual bool read_block(unsigned index, u8* out) const override;
|
||||||
|
@ -22,8 +22,9 @@ public:
|
||||||
private:
|
private:
|
||||||
virtual const char* class_name() const override;
|
virtual const char* class_name() const override;
|
||||||
|
|
||||||
DiskPartition(DiskDevice&, unsigned block_offset);
|
DiskPartition(DiskDevice&, unsigned block_offset, unsigned block_limit);
|
||||||
|
|
||||||
NonnullRefPtr<DiskDevice> m_device;
|
NonnullRefPtr<DiskDevice> m_device;
|
||||||
unsigned m_block_offset;
|
unsigned m_block_offset;
|
||||||
|
unsigned m_block_limit;
|
||||||
};
|
};
|
||||||
|
|
75
Kernel/Devices/GPTPartitionTable.cpp
Normal file
75
Kernel/Devices/GPTPartitionTable.cpp
Normal 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]);
|
||||||
|
}
|
60
Kernel/Devices/GPTPartitionTable.h
Normal file
60
Kernel/Devices/GPTPartitionTable.h
Normal 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];
|
||||||
|
};
|
|
@ -37,6 +37,11 @@ bool MBRPartitionTable::initialize()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool MBRPartitionTable::is_protective_mbr() const
|
||||||
|
{
|
||||||
|
return header().entry[0].type == MBR_PROTECTIVE;
|
||||||
|
}
|
||||||
|
|
||||||
RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
|
RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
|
||||||
{
|
{
|
||||||
ASSERT(index >= 1 && index <= 4);
|
ASSERT(index >= 1 && index <= 4);
|
||||||
|
@ -55,7 +60,7 @@ RefPtr<DiskPartition> MBRPartitionTable::partition(unsigned index)
|
||||||
|
|
||||||
if (entry.offset == 0x00) {
|
if (entry.offset == 0x00) {
|
||||||
#ifdef MBR_DEBUG
|
#ifdef MBR_DEBUG
|
||||||
kprintf("MBRPartitionTable::partition: missing partition requested index=%d\n", index);
|
kprintf("MBRPartitionTable::partition: missing partition requested index=%d\n", index);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return nullptr;
|
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);
|
kprintf("MBRPartitionTable::partition: found partition index=%d type=%x\n", index, entry.type);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return DiskPartition::create(m_device, entry.offset);
|
return DiskPartition::create(m_device, entry.offset, (entry.offset + entry.length));
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <Kernel/Devices/DiskPartition.h>
|
#include <Kernel/Devices/DiskPartition.h>
|
||||||
|
|
||||||
#define MBR_SIGNATURE 0xaa55
|
#define MBR_SIGNATURE 0xaa55
|
||||||
|
#define MBR_PROTECTIVE 0xEE
|
||||||
|
|
||||||
struct MBRPartitionEntry {
|
struct MBRPartitionEntry {
|
||||||
u8 status;
|
u8 status;
|
||||||
|
@ -35,6 +36,7 @@ public:
|
||||||
~MBRPartitionTable();
|
~MBRPartitionTable();
|
||||||
|
|
||||||
bool initialize();
|
bool initialize();
|
||||||
|
bool is_protective_mbr() const;
|
||||||
RefPtr<DiskPartition> partition(unsigned index);
|
RefPtr<DiskPartition> partition(unsigned index);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -81,6 +81,7 @@ VFS_OBJS = \
|
||||||
Devices/DebugLogDevice.o \
|
Devices/DebugLogDevice.o \
|
||||||
Devices/DiskPartition.o \
|
Devices/DiskPartition.o \
|
||||||
Devices/MBRPartitionTable.o \
|
Devices/MBRPartitionTable.o \
|
||||||
|
Devices/GPTPartitionTable.o \
|
||||||
FileSystem/InodeWatcher.o \
|
FileSystem/InodeWatcher.o \
|
||||||
FileSystem/FileSystem.o \
|
FileSystem/FileSystem.o \
|
||||||
FileSystem/DiskBackedFileSystem.o \
|
FileSystem/DiskBackedFileSystem.o \
|
||||||
|
|
87
Kernel/build-gpt-image-grub.sh
Executable file
87
Kernel/build-gpt-image-grub.sh
Executable file
|
@ -0,0 +1,87 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
die() {
|
||||||
|
echo "die: $@"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ $(id -u) != 0 ]; then
|
||||||
|
die "this script needs to run as root"
|
||||||
|
fi
|
||||||
|
|
||||||
|
grub=$(which grub-install 2>/dev/null) || true
|
||||||
|
if [[ -z "$grub" ]]; then
|
||||||
|
grub=$(which grub2-install 2>/dev/null) || true
|
||||||
|
fi
|
||||||
|
if [ -z "$grub" ]; then
|
||||||
|
echo "can't find a grub-install or grub2-install binary, oh no"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "using grub-install at ${grub}"
|
||||||
|
|
||||||
|
echo "setting up disk image..."
|
||||||
|
dd if=/dev/zero of=_disk_image bs=1M count=${DISK_SIZE:-701} status=none || die "couldn't create disk image"
|
||||||
|
chown 1000:1000 _disk_image || die "couldn't adjust permissions on disk image"
|
||||||
|
echo "done"
|
||||||
|
|
||||||
|
echo -n "creating loopback device... "
|
||||||
|
dev=$(losetup --find --partscan --show _disk_image)
|
||||||
|
if [ -z $dev ]; then
|
||||||
|
die "couldn't mount loopback device"
|
||||||
|
fi
|
||||||
|
echo "loopback device is at ${dev}"
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
if [ -d mnt ]; then
|
||||||
|
echo -n "unmounting filesystem... "
|
||||||
|
umount mnt || ( sleep 1 && sync && umount mnt )
|
||||||
|
rm -rf mnt
|
||||||
|
echo "done"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -e ${dev} ]; then
|
||||||
|
echo -n "cleaning up loopback device... "
|
||||||
|
losetup -d ${dev}
|
||||||
|
echo "done"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
echo -n "creating partition table... "
|
||||||
|
parted -s ${dev} mklabel gpt mkpart BIOSBOOT ext3 1MiB 8MiB mkpart OS ext2 8MiB 700MiB set 1 bios_grub || die "couldn't partition disk"
|
||||||
|
echo "done"
|
||||||
|
|
||||||
|
echo -n "destroying old filesystem... "
|
||||||
|
dd if=/dev/zero of=${dev}p2 bs=1M count=1 status=none || die "couldn't destroy old filesystem"
|
||||||
|
echo "done"
|
||||||
|
|
||||||
|
echo -n "creating new filesystem... "
|
||||||
|
mke2fs -q ${dev}p2 || die "couldn't create filesystem"
|
||||||
|
echo "done"
|
||||||
|
|
||||||
|
echo -n "mounting filesystem... "
|
||||||
|
mkdir -p mnt
|
||||||
|
mount ${dev}p2 mnt/ || die "couldn't mount filesystem"
|
||||||
|
echo "done"
|
||||||
|
|
||||||
|
./build-root-filesystem.sh
|
||||||
|
|
||||||
|
echo -n "creating /boot... "
|
||||||
|
mkdir -p mnt/boot
|
||||||
|
echo "done"
|
||||||
|
|
||||||
|
echo "installing grub using $grub..."
|
||||||
|
$grub --boot-directory=mnt/boot --target=i386-pc --modules="ext2 part_msdos part_gpt" ${dev}
|
||||||
|
|
||||||
|
if [ -d mnt/boot/grub2 ]; then
|
||||||
|
cp grub_gpt.cfg mnt/boot/grub2/grub.cfg
|
||||||
|
else
|
||||||
|
cp grub_gpt.cfg mnt/boot/grub/grub.cfg
|
||||||
|
fi
|
||||||
|
echo "done"
|
||||||
|
|
||||||
|
echo -n "installing kernel in /boot... "
|
||||||
|
cp kernel mnt/boot
|
||||||
|
echo "done"
|
11
Kernel/grub_gpt.cfg
Normal file
11
Kernel/grub_gpt.cfg
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
timeout=1
|
||||||
|
|
||||||
|
menuentry 'Serenity (normal)' {
|
||||||
|
root=hd0,2
|
||||||
|
multiboot /boot/kernel root=/dev/hda2
|
||||||
|
}
|
||||||
|
|
||||||
|
menuentry 'Serenity (with serial debug)' {
|
||||||
|
root=hd0,2
|
||||||
|
multiboot /boot/kernel serial_debug root=/dev/hda2
|
||||||
|
}
|
|
@ -14,6 +14,7 @@
|
||||||
#include <Kernel/Devices/DiskPartition.h>
|
#include <Kernel/Devices/DiskPartition.h>
|
||||||
#include <Kernel/Devices/FloppyDiskDevice.h>
|
#include <Kernel/Devices/FloppyDiskDevice.h>
|
||||||
#include <Kernel/Devices/FullDevice.h>
|
#include <Kernel/Devices/FullDevice.h>
|
||||||
|
#include <Kernel/Devices/GPTPartitionTable.h>
|
||||||
#include <Kernel/Devices/KeyboardDevice.h>
|
#include <Kernel/Devices/KeyboardDevice.h>
|
||||||
#include <Kernel/Devices/MBRPartitionTable.h>
|
#include <Kernel/Devices/MBRPartitionTable.h>
|
||||||
#include <Kernel/Devices/MBVGADevice.h>
|
#include <Kernel/Devices/MBVGADevice.h>
|
||||||
|
@ -96,18 +97,34 @@ VFS* vfs;
|
||||||
}
|
}
|
||||||
|
|
||||||
MBRPartitionTable mbr(root_dev);
|
MBRPartitionTable mbr(root_dev);
|
||||||
|
|
||||||
if (!mbr.initialize()) {
|
if (!mbr.initialize()) {
|
||||||
kprintf("init_stage2: couldn't read MBR from disk\n");
|
kprintf("init_stage2: couldn't read MBR from disk\n");
|
||||||
hang();
|
hang();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto partition = mbr.partition(partition_number);
|
if (mbr.is_protective_mbr()) {
|
||||||
if (!partition) {
|
dbgprintf("GPT Partitioned Storage Detected!\n");
|
||||||
kprintf("init_stage2: couldn't get partition %d\n", partition_number);
|
GPTPartitionTable gpt(root_dev);
|
||||||
hang();
|
if (!gpt.initialize()) {
|
||||||
|
kprintf("init_stage2: couldn't read GPT from disk\n");
|
||||||
|
hang();
|
||||||
|
}
|
||||||
|
auto partition = gpt.partition(partition_number);
|
||||||
|
if (!partition) {
|
||||||
|
kprintf("init_stage2: couldn't get partition %d\n", partition_number);
|
||||||
|
hang();
|
||||||
|
}
|
||||||
|
root_dev = *partition;
|
||||||
|
} else {
|
||||||
|
dbgprintf("MBR Partitioned Storage Detected!\n");
|
||||||
|
auto partition = mbr.partition(partition_number);
|
||||||
|
if (!partition) {
|
||||||
|
kprintf("init_stage2: couldn't get partition %d\n", partition_number);
|
||||||
|
hang();
|
||||||
|
}
|
||||||
|
root_dev = *partition;
|
||||||
}
|
}
|
||||||
|
|
||||||
root_dev = *partition;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto e2fs = Ext2FS::create(root_dev);
|
auto e2fs = Ext2FS::create(root_dev);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue