From 9de4b392a2ff3b05b8475f09bae8dc0f38248723 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 4 Jun 2023 11:25:16 +0200 Subject: [PATCH] kernel: Make the MBR code read from a device instead of an inode --- kernel/src/arch/x86_64/disk/ATA.cpp | 14 ++++++++------ kernel/src/fs/MBR.cpp | 8 ++++---- kernel/src/fs/MBR.h | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/kernel/src/arch/x86_64/disk/ATA.cpp b/kernel/src/arch/x86_64/disk/ATA.cpp index 1bcf86bf..4a6a6690 100644 --- a/kernel/src/arch/x86_64/disk/ATA.cpp +++ b/kernel/src/arch/x86_64/disk/ATA.cpp @@ -332,15 +332,16 @@ namespace ATA return false; } - if (ATADevice::create(m_drives[drive]).has_error()) + auto rc = ATADevice::create(m_drives[drive]); + + if (rc.has_error()) { kwarnln("ata: Failed to register ATA drive %d:%d in DeviceRegistry", m_channel_index, drive); continue; } - // FIXME: Do not hardcode the path like this. - /* auto inode = VFS::resolve_path("/dev/cdrom", Credentials {}).value(); - MBR::identify(inode); */ + auto device = rc.release_value(); + MBR::identify(device); } } @@ -728,11 +729,12 @@ namespace ATA static u32 next_minor = 0; -Result ATADevice::create(SharedPtr drive) +Result> ATADevice::create(SharedPtr drive) { auto device = TRY(adopt_shared_if_nonnull(new (std::nothrow) ATADevice())); device->m_drive = drive; - return DeviceRegistry::register_special_device(DeviceRegistry::Disk, next_minor++, device, "cdrom", 0400); + TRY(DeviceRegistry::register_special_device(DeviceRegistry::Disk, next_minor++, device, "cdrom", 0400)); + return (SharedPtr)device; } Result ATADevice::read(u8* buf, usize offset, usize size) const diff --git a/kernel/src/fs/MBR.cpp b/kernel/src/fs/MBR.cpp index 21bce837..c3d5da52 100644 --- a/kernel/src/fs/MBR.cpp +++ b/kernel/src/fs/MBR.cpp @@ -3,13 +3,13 @@ namespace MBR { - Result identify(SharedPtr inode) + Result identify(SharedPtr device) { - // Cannot read a partition table from a pipe/socket/character device! Who is even coming up with this silliness? - if (!VFS::is_seekable(inode)) return false; + // Cannot read a partition table from a character device! Who is even coming up with this silliness? + if (!device->is_block_device()) return false; DiskHeader hdr; - usize nread = TRY(inode->read((u8*)&hdr, 0, sizeof(hdr))); + usize nread = TRY(device->read((u8*)&hdr, 0, sizeof(hdr))); check(nread == 512); if (hdr.signature[0] != MBR_SIGNATURE_1 || hdr.signature[1] != MBR_SIGNATURE_2) return false; diff --git a/kernel/src/fs/MBR.h b/kernel/src/fs/MBR.h index d447cdef..b361ea78 100644 --- a/kernel/src/fs/MBR.h +++ b/kernel/src/fs/MBR.h @@ -1,6 +1,6 @@ #pragma once -#include "fs/VFS.h" +#include "fs/devices/DeviceRegistry.h" #include #define MBR_BOOTABLE 0x80 @@ -31,5 +31,5 @@ namespace MBR static_assert(sizeof(DiskHeader) == 512ul); - Result identify(SharedPtr inode); + Result identify(SharedPtr device); };