diff --git a/Cargo.lock b/Cargo.lock index 24bff133..068b7ddc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -721,7 +721,6 @@ dependencies = [ "libk-util", "log", "vfs", - "ygg_driver_block", "yggdrasil-abi", ] @@ -1507,15 +1506,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "uuid" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" -dependencies = [ - "bytemuck", -] - [[package]] name = "vcpkg" version = "0.2.15" @@ -1531,7 +1521,6 @@ dependencies = [ "libk-thread", "libk-util", "log", - "ygg_driver_block", "yggdrasil-abi", ] @@ -1761,25 +1750,10 @@ dependencies = [ "static_assertions", "tock-registers 0.8.1", "vfs", - "ygg_driver_block", "ygg_driver_pci", "yggdrasil-abi", ] -[[package]] -name = "ygg_driver_block" -version = "0.1.0" -dependencies = [ - "bytemuck", - "futures-util", - "libk-mm", - "libk-util", - "log", - "static_assertions", - "uuid", - "yggdrasil-abi", -] - [[package]] name = "ygg_driver_input" version = "0.1.0" @@ -1834,7 +1808,6 @@ dependencies = [ "static_assertions", "tock-registers 0.8.1", "vfs", - "ygg_driver_block", "ygg_driver_pci", "yggdrasil-abi", ] @@ -1974,7 +1947,6 @@ dependencies = [ "vmalloc", "yboot-proto", "ygg_driver_ahci", - "ygg_driver_block", "ygg_driver_input", "ygg_driver_net_core", "ygg_driver_net_loopback", diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index a05b4749..a16d5895 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -29,7 +29,6 @@ kernel-arch = { path = "arch" } # Drivers ygg_driver_pci = { path = "driver/bus/pci" } ygg_driver_usb = { path = "driver/bus/usb" } -ygg_driver_block = { path = "driver/block/core" } ygg_driver_net_core = { path = "driver/net/core" } ygg_driver_net_loopback = { path = "driver/net/loopback" } ygg_driver_virtio_net = { path = "driver/virtio/net", features = ["pci"] } diff --git a/kernel/driver/block/ahci/Cargo.toml b/kernel/driver/block/ahci/Cargo.toml index 7639a8ba..0a64c0de 100644 --- a/kernel/driver/block/ahci/Cargo.toml +++ b/kernel/driver/block/ahci/Cargo.toml @@ -13,7 +13,6 @@ device-api = { path = "../../../lib/device-api", features = ["derive"] } vfs = { path = "../../../lib/vfs" } ygg_driver_pci = { path = "../../bus/pci" } -ygg_driver_block = { path = "../../block/core" } kernel-fs = { path = "../../fs/kernel-fs" } log = "0.4.20" diff --git a/kernel/driver/block/ahci/src/lib.rs b/kernel/driver/block/ahci/src/lib.rs index 397ffeea..8ec6b7a4 100644 --- a/kernel/driver/block/ahci/src/lib.rs +++ b/kernel/driver/block/ahci/src/lib.rs @@ -19,7 +19,7 @@ use libk_util::{sync::IrqSafeSpinlock, OneTimeInit}; use port::AhciPort; use regs::{PortRegs, Regs}; use tock_registers::interfaces::{ReadWriteable, Readable, Writeable}; -use ygg_driver_block::{probe_partitions, NgBlockDeviceWrapper}; +use vfs::block::{probe_partitions, NgBlockDeviceWrapper}; use ygg_driver_pci::{ device::{PciDeviceInfo, PreferredInterruptMode}, PciCommandRegister, PciConfigurationSpace, diff --git a/kernel/driver/block/ahci/src/port.rs b/kernel/driver/block/ahci/src/port.rs index 14afc1d9..9603dad1 100644 --- a/kernel/driver/block/ahci/src/port.rs +++ b/kernel/driver/block/ahci/src/port.rs @@ -10,7 +10,7 @@ use futures_util::{task::AtomicWaker, Future}; use libk_mm::{address::AsPhysicalAddress, device::DeviceMemoryIo, PageBox}; use libk_util::{sync::IrqSafeSpinlock, waker::QueueWaker, OneTimeInit}; use tock_registers::interfaces::{Readable, Writeable}; -use ygg_driver_block::{IoOperation, IoRequest, IoSubmissionId, NgBlockDevice}; +use vfs::block::{IoOperation, IoRequest, IoSubmissionId, NgBlockDevice}; use yggdrasil_abi::error::Error; use crate::{ diff --git a/kernel/driver/block/core/Cargo.toml b/kernel/driver/block/core/Cargo.toml deleted file mode 100644 index ce2e9537..00000000 --- a/kernel/driver/block/core/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "ygg_driver_block" -version = "0.1.0" -edition = "2021" -authors = ["Mark Poliakov "] - -[dependencies] -yggdrasil-abi = { path = "../../../../lib/abi" } -libk-util = { path = "../../../libk/libk-util" } -libk-mm = { path = "../../../libk/libk-mm" } - -log = "0.4.20" -futures-util = { version = "0.3.28", default-features = false, features = ["alloc", "async-await"] } -bytemuck = { version = "1.14.0", features = ["derive"] } -static_assertions = "1.1.0" -uuid = { version = "1.6.1", default-features = false, features = ["bytemuck"] } diff --git a/kernel/driver/block/core/src/partition.rs b/kernel/driver/block/core/src/partition.rs deleted file mode 100644 index 148d3ad7..00000000 --- a/kernel/driver/block/core/src/partition.rs +++ /dev/null @@ -1,137 +0,0 @@ -use core::mem::{size_of, MaybeUninit}; - -use alloc::{vec, vec::Vec}; -use bytemuck::{Pod, Zeroable}; -use libk::mem::PageBox; -use static_assertions::const_assert_eq; -use uuid::Uuid; -use yggdrasil_abi::{error::Error, io::DeviceRequest}; - -use crate::{BlockDevice, NgBlockDevice, NgBlockDeviceWrapper}; - -pub struct Partition<'a, D: NgBlockDevice + 'a> { - pub device: &'a NgBlockDeviceWrapper<'a, D>, - pub lba_start: u64, - pub lba_end: u64, -} - -#[derive(Clone, Copy)] -#[repr(C)] -struct GptHeader { - signature: [u8; 8], - revision: u32, - header_size: u32, - crc32: u32, - _0: u32, - header_lba: u64, - alternate_header_lba: u64, - first_usable_lba: u64, - last_usable_lba: u64, - guid: [u8; 16], - partition_table_lba: u64, - partition_table_len: u32, - partition_table_entry_size: u32, - partition_table_crc32: u32, - _1: [u8; 420], -} - -#[derive(Clone, Copy, Zeroable, Pod)] -#[repr(C)] -struct GptEntry { - type_guid: Uuid, - part_guid: Uuid, - lba_start: u64, - lba_end: u64, - attrs: u64, -} - -const_assert_eq!(size_of::(), 512); - -impl<'a, D: NgBlockDevice + 'a> Partition<'a, D> { - fn end_byte(&self) -> u64 { - self.lba_end * self.device.block_size - } - - fn start_byte(&self) -> u64 { - self.lba_start * self.device.block_size - } -} - -impl<'a, D: NgBlockDevice + 'a> BlockDevice for Partition<'a, D> { - fn read(&'static self, pos: u64, buf: &mut [u8]) -> Result { - if pos >= self.end_byte() { - return Ok(0); - } - - let start = self.start_byte() + pos; - let end = core::cmp::min(start + buf.len() as u64, self.end_byte()); - let count = (end - start) as usize; - - self.device.read(start, &mut buf[..count]) - } - - fn write(&'static self, pos: u64, buf: &[u8]) -> Result { - if pos >= self.end_byte() { - return Ok(0); - } - - let start = self.start_byte() + pos; - let end = core::cmp::min(start + buf.len() as u64, self.end_byte()); - let count = (end - start) as usize; - - self.device.write(start, &buf[..count]) - } - - fn size(&self) -> Result { - Ok((self.lba_end - self.lba_start) * self.device.block_size) - } - - fn device_request(&self, req: &mut DeviceRequest) -> Result<(), Error> { - self.device.device_request(req) - } -} - -unsafe fn read_struct_lba(dev: &'static dyn BlockDevice, lba: u64) -> Result { - assert_eq!(size_of::(), 512); - let mut data = MaybeUninit::::uninit(); - let buffer = core::slice::from_raw_parts_mut(data.as_mut_ptr() as *mut u8, 512); - dev.read_exact(lba * 512, buffer)?; - Ok(data.assume_init()) -} - -pub(crate) fn probe_gpt( - dev: &'static NgBlockDeviceWrapper<'static, D>, -) -> Result>>, Error> { - let header = unsafe { read_struct_lba::(dev, 1) }?; - - if &header.signature != b"EFI PART" { - // Not a GPT partition table - return Ok(None); - } - - let pt_entsize = header.partition_table_entry_size as usize; - let pt_len = header.partition_table_len as usize; - let mut pt_data = PageBox::new_slice(0, pt_len * pt_entsize)?; - - assert!(size_of::() <= pt_entsize); - - dev.read_exact(header.partition_table_lba * 512, &mut pt_data)?; - - let mut partitions = vec![]; - for i in 0..pt_len { - let pt_entry_data = &pt_data[i * pt_entsize..i * pt_entsize + size_of::()]; - let pt_entry: &GptEntry = bytemuck::from_bytes(pt_entry_data); - - if pt_entry.type_guid.is_nil() { - continue; - } - - partitions.push(Partition { - device: dev, - lba_start: pt_entry.lba_start, - lba_end: pt_entry.lba_end, - }); - } - - Ok(Some(partitions)) -} diff --git a/kernel/driver/block/nvme/Cargo.toml b/kernel/driver/block/nvme/Cargo.toml index 63f7cf58..896866dc 100644 --- a/kernel/driver/block/nvme/Cargo.toml +++ b/kernel/driver/block/nvme/Cargo.toml @@ -13,7 +13,6 @@ device-api = { path = "../../../lib/device-api", features = ["derive"] } vfs = { path = "../../../lib/vfs" } ygg_driver_pci = { path = "../../bus/pci" } -ygg_driver_block = { path = "../../block/core" } kernel-fs = { path = "../../fs/kernel-fs" } log = "0.4.20" diff --git a/kernel/driver/block/nvme/src/drive.rs b/kernel/driver/block/nvme/src/drive.rs index 49bb6443..8aca3c7f 100644 --- a/kernel/driver/block/nvme/src/drive.rs +++ b/kernel/driver/block/nvme/src/drive.rs @@ -5,7 +5,7 @@ use kernel_fs::devfs; use libk_mm::address::AsPhysicalAddress; use libk_thread::cpu_index; use libk_util::waker::QueueWaker; -use ygg_driver_block::{ +use vfs::block::{ probe_partitions, IoOperation, IoRequest, IoSubmissionId, NgBlockDevice, NgBlockDeviceWrapper, }; use yggdrasil_abi::error::Error; diff --git a/kernel/driver/fs/kernel-fs/Cargo.toml b/kernel/driver/fs/kernel-fs/Cargo.toml index cc8d1245..42dd73cd 100644 --- a/kernel/driver/fs/kernel-fs/Cargo.toml +++ b/kernel/driver/fs/kernel-fs/Cargo.toml @@ -9,6 +9,4 @@ yggdrasil-abi = { path = "../../../../lib/abi" } vfs = { path = "../../../lib/vfs" } libk-util = { path = "../../../libk/libk-util" } -ygg_driver_block = { path = "../../block/core" } - log = "0.4.20" diff --git a/kernel/driver/fs/kernel-fs/src/devfs.rs b/kernel/driver/fs/kernel-fs/src/devfs.rs index 63c8e814..7d67e1a6 100644 --- a/kernel/driver/fs/kernel-fs/src/devfs.rs +++ b/kernel/driver/fs/kernel-fs/src/devfs.rs @@ -3,8 +3,7 @@ use core::sync::atomic::{AtomicUsize, Ordering}; use alloc::{format, string::String}; use libk_util::OneTimeInit; -use vfs::{impls::MemoryDirectory, CharDevice, Node, NodeFlags, NodeRef}; -use ygg_driver_block::BlockDevice; +use vfs::{block::BlockDevice, impls::MemoryDirectory, CharDevice, Node, NodeFlags, NodeRef}; use yggdrasil_abi::error::Error; /// Describes the kind of a character device diff --git a/kernel/lib/vfs/Cargo.toml b/kernel/lib/vfs/Cargo.toml index 2e55150c..1fcb6ec8 100644 --- a/kernel/lib/vfs/Cargo.toml +++ b/kernel/lib/vfs/Cargo.toml @@ -12,7 +12,5 @@ libk-mm = { path = "../../libk/libk-mm" } libk-util = { path = "../../libk/libk-util" } libk-thread = { path = "../../libk/libk-thread" } -ygg_driver_block = { path = "../../driver/block/core" } - log = "0.4.20" futures-util = { version = "0.3.28", default-features = false, features = ["alloc", "async-await"] } diff --git a/kernel/driver/block/core/src/device.rs b/kernel/lib/vfs/src/block/device.rs similarity index 99% rename from kernel/driver/block/core/src/device.rs rename to kernel/lib/vfs/src/block/device.rs index d924baca..f79d4066 100644 --- a/kernel/driver/block/core/src/device.rs +++ b/kernel/lib/vfs/src/block/device.rs @@ -12,7 +12,7 @@ use libk_mm::{address::PhysicalAddress, table::MapAttributes, PageBox, PageProvi use libk_util::waker::QueueWaker; use yggdrasil_abi::{error::Error, io::DeviceRequest}; -use crate::{ +use crate::block::{ request::{IoOperation, IoRequest, IoSubmissionId}, BlockDevice, }; diff --git a/kernel/driver/block/core/src/lib.rs b/kernel/lib/vfs/src/block/mod.rs similarity index 99% rename from kernel/driver/block/core/src/lib.rs rename to kernel/lib/vfs/src/block/mod.rs index 80d7f1e0..e35af962 100644 --- a/kernel/driver/block/core/src/lib.rs +++ b/kernel/lib/vfs/src/block/mod.rs @@ -1,6 +1,4 @@ -#![no_std] - -extern crate alloc; +#![allow(missing_docs)] use core::task::{Context, Poll}; diff --git a/kernel/driver/block/core/src/request.rs b/kernel/lib/vfs/src/block/request.rs similarity index 100% rename from kernel/driver/block/core/src/request.rs rename to kernel/lib/vfs/src/block/request.rs diff --git a/kernel/lib/vfs/src/device.rs b/kernel/lib/vfs/src/device.rs index c45a291e..99a76ab1 100644 --- a/kernel/lib/vfs/src/device.rs +++ b/kernel/lib/vfs/src/device.rs @@ -1,7 +1,7 @@ -use ygg_driver_block::BlockDevice; use yggdrasil_abi::{error::Error, io::DeviceRequest}; use crate::{ + block::BlockDevice, node::{CommonImpl, NodeRef}, traits::FileReadiness, }; diff --git a/kernel/lib/vfs/src/lib.rs b/kernel/lib/vfs/src/lib.rs index 03bac5a7..42e1abd0 100644 --- a/kernel/lib/vfs/src/lib.rs +++ b/kernel/lib/vfs/src/lib.rs @@ -15,6 +15,8 @@ extern crate alloc; +pub mod block; + pub(crate) mod channel; pub(crate) mod device; pub(crate) mod file; diff --git a/kernel/lib/vfs/src/node/mod.rs b/kernel/lib/vfs/src/node/mod.rs index b8e7b091..d33d52e7 100644 --- a/kernel/lib/vfs/src/node/mod.rs +++ b/kernel/lib/vfs/src/node/mod.rs @@ -2,7 +2,6 @@ use core::{any::Any, fmt}; use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec}; use libk_util::sync::IrqSafeSpinlock; -use ygg_driver_block::BlockDevice; use yggdrasil_abi::{ bitflags, error::Error, @@ -21,6 +20,7 @@ pub use access::AccessToken; pub use traits::{CommonImpl, DirectoryImpl, RegularImpl, SymlinkImpl}; use crate::{ + block::BlockDevice, device::{BlockDeviceWrapper, CharDevice, CharDeviceWrapper}, PseudoTerminalMaster, PseudoTerminalSlave, }; diff --git a/kernel/src/device/display/linear_fb.rs b/kernel/src/device/display/linear_fb.rs index 6cb50184..3b583f10 100644 --- a/kernel/src/device/display/linear_fb.rs +++ b/kernel/src/device/display/linear_fb.rs @@ -15,7 +15,7 @@ use libk_mm::{ }; use libk_thread::thread::Thread; use libk_util::sync::IrqSafeSpinlock; -use ygg_driver_block::BlockDevice; +use vfs::block::BlockDevice; use crate::arch::L3;