From 96863494761779c303963c50d3da53630f9630fd Mon Sep 17 00:00:00 2001 From: Able Date: Fri, 13 Sep 2024 18:11:23 -0500 Subject: [PATCH] add support for the device tree --- .../{device_tree.rs => device_tree/mod.rs} | 39 ++++---- kernel/src/holeybytes/ecah.rs | 7 ++ .../kernel_services/dt_msg_handler.rs | 92 +++++++++++++++++++ kernel/src/holeybytes/kernel_services/mod.rs | 1 + sysdata/libraries/dt_api/README.md | 1 + sysdata/libraries/dt_api/src/lib.hb | 8 ++ sysdata/libraries/horizon_api/src/lib.hb | 4 +- sysdata/programs/dt_buffer_test/README.md | 1 + sysdata/programs/dt_buffer_test/meta.toml | 11 +++ sysdata/programs/dt_buffer_test/src/main.hb | 8 ++ sysdata/system_config.toml | 4 + 11 files changed, 155 insertions(+), 21 deletions(-) rename kernel/src/{device_tree.rs => device_tree/mod.rs} (76%) create mode 100644 kernel/src/holeybytes/kernel_services/dt_msg_handler.rs create mode 100644 sysdata/libraries/dt_api/README.md create mode 100644 sysdata/libraries/dt_api/src/lib.hb create mode 100644 sysdata/programs/dt_buffer_test/README.md create mode 100644 sysdata/programs/dt_buffer_test/meta.toml create mode 100644 sysdata/programs/dt_buffer_test/src/main.hb diff --git a/kernel/src/device_tree.rs b/kernel/src/device_tree/mod.rs similarity index 76% rename from kernel/src/device_tree.rs rename to kernel/src/device_tree/mod.rs index 0ec81d1..8d1c64c 100644 --- a/kernel/src/device_tree.rs +++ b/kernel/src/device_tree/mod.rs @@ -6,6 +6,7 @@ use { core::fmt, hashbrown::HashMap, }; + /// A device object. /// TODO define device pub type Device = xml::XMLElement; @@ -23,27 +24,29 @@ impl DeviceTree { let mut dt = Self { devices: HashMap::new(), }; - device_tree!(dt, [ - "Mice", - "Keyboards", - "Controllers", - "Generic HIDs", - "Disk Drives", - "CD Drives", - "Batteries", - "Monitors", - "GPUs", - "CPUs", - "USB", - "Serial Ports", - "Cameras", - "Biometric Devices", - ]); + device_tree!( + dt, + [ + "Mice", + "Keyboards", + "Controllers", + "Generic HIDs", + "Disk Drives", + "CD Drives", + "Batteries", + "Monitors", + "GPUs", + "CPUs", + "USB", + "Serial Ports", + "Cameras", + "Biometric Devices", + ] + ); dt } } -use crate::{utils::TAB, device_tree}; -use crate::tab; +use crate::{device_tree, tab, utils::TAB}; impl fmt::Display for DeviceTree { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f)?; diff --git a/kernel/src/holeybytes/ecah.rs b/kernel/src/holeybytes/ecah.rs index 0f488cc..2a69e80 100644 --- a/kernel/src/holeybytes/ecah.rs +++ b/kernel/src/holeybytes/ecah.rs @@ -6,6 +6,7 @@ use crate::{ allocator, holeybytes::kernel_services::{ block_read, + dt_msg_handler::dt_msg_handler, service_definition_service::{sds_msg_handler, SERVICES}, }, }; @@ -134,6 +135,12 @@ pub fn handler(vm: &mut Vm) { } #[cfg(not(target_arch = "x86_64"))] 3 => unimplemented!("TODO: implement whatever buffer 3 does for no x86_64"), + + 5 => match dt_msg_handler(vm, mem_addr, length) { + Ok(()) => {} + Err(err) => log::error!("Improper dt query"), + }, + buffer_id => { let mut buffs = IPC_BUFFERS.lock(); match buffs.get_mut(&buffer_id) { diff --git a/kernel/src/holeybytes/kernel_services/dt_msg_handler.rs b/kernel/src/holeybytes/kernel_services/dt_msg_handler.rs new file mode 100644 index 0000000..bcd34e0 --- /dev/null +++ b/kernel/src/holeybytes/kernel_services/dt_msg_handler.rs @@ -0,0 +1,92 @@ +use { + crate::holeybytes::{kernel_services::block_read, Vm}, + alloc::{ + borrow::ToOwned, + string::{String, ToString}, + vec::Vec, + }, + log::debug, +}; +pub enum DtError { + QueryFailure, +} + +pub fn dt_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), DtError> { + let mut msg_vec = block_read(mem_addr, length); + let mut bytes: Vec = Vec::new(); + for byte in msg_vec { + if byte == 0 { + break; + } + bytes.push(byte) + } + let query_string = String::from_utf8(bytes).unwrap(); + log::trace!("Query {}", query_string); + + let ret = query_parse(query_string); + log::trace!("Query response {}", ret); + + vm.registers[1] = hbvm::value::Value(ret); + + Ok(()) +} + +fn query_parse(query_string: String) -> u64 { + let qt_parse_step_one = query_string.split("/"); + let mut qt_parse_step_two: Vec = Vec::new(); + for a in qt_parse_step_one { + qt_parse_step_two.push(a.to_string()); + } + + let first_fragment: &str = &qt_parse_step_two[0]; + let ret = match first_fragment { + "framebuffer" => framebuffer_parse(qt_parse_step_two), + "cpu" => cpu_parse(qt_parse_step_two), + + _ => 0, + }; + + return ret; +} + +fn cpu_parse(qt_parse_step_two: Vec) -> u64 { + let second_fragment: &str = &qt_parse_step_two[1]; + match second_fragment { + // "architecture" => { + // return 0; + // } + _ => { + return 0; + } + }; +} + +fn framebuffer_parse(qt_parse_step_two: Vec) -> u64 { + use { + crate::kmain::FB_REQ, + limine::{Framebuffer, NonNullPtr}, + }; + let fbs = &FB_REQ.get_response().get().unwrap().framebuffers(); + + let second_fragment: &str = &qt_parse_step_two[1]; + match second_fragment { + "fb0" => { + let fb_front = &fbs[0]; + let third_fragment: &str = &qt_parse_step_two[2]; + let ret = match third_fragment { + "ptr" => { + let ptr = fb_front.address.as_ptr().unwrap(); + ptr as usize as u64 + } + "width" => fb_front.width, + "height" => fb_front.height, + + _ => 0, + }; + return ret; + } + _ => { + return 0; + } + }; +} diff --git a/kernel/src/holeybytes/kernel_services/mod.rs b/kernel/src/holeybytes/kernel_services/mod.rs index 23908ef..9d0b9fc 100644 --- a/kernel/src/holeybytes/kernel_services/mod.rs +++ b/kernel/src/holeybytes/kernel_services/mod.rs @@ -1,5 +1,6 @@ use alloc::{vec, vec::Vec}; +pub mod dt_msg_handler; pub mod mem_serve; pub mod service_definition_service; diff --git a/sysdata/libraries/dt_api/README.md b/sysdata/libraries/dt_api/README.md new file mode 100644 index 0000000..61d4547 --- /dev/null +++ b/sysdata/libraries/dt_api/README.md @@ -0,0 +1 @@ +# dt_api \ No newline at end of file diff --git a/sysdata/libraries/dt_api/src/lib.hb b/sysdata/libraries/dt_api/src/lib.hb new file mode 100644 index 0000000..bf3e800 --- /dev/null +++ b/sysdata/libraries/dt_api/src/lib.hb @@ -0,0 +1,8 @@ +stn := @use("rel:../../stn/src/lib.hb"); +.{string, memory, buffer} := stn + +dt_get := fn(query: ^u8): int { + message_length := string.length(query) + + return @eca(int, 3, 5, query, message_length) +} \ No newline at end of file diff --git a/sysdata/libraries/horizon_api/src/lib.hb b/sysdata/libraries/horizon_api/src/lib.hb index 4611dc4..543bd14 100644 --- a/sysdata/libraries/horizon_api/src/lib.hb +++ b/sysdata/libraries/horizon_api/src/lib.hb @@ -1,4 +1,4 @@ -stn := @use("rel:../../stn/src/lib.hb") +stn := @use("rel:../../stn/src/lib.hb"); .{string, memory, buffer} := stn WindowID := struct { @@ -13,8 +13,6 @@ create_window := fn(channel: int): void { windowing_system_buffer := buffer.search("XHorizon\0") - - if windowing_system_buffer == 0 { } else { msg := "\{01}\0" diff --git a/sysdata/programs/dt_buffer_test/README.md b/sysdata/programs/dt_buffer_test/README.md new file mode 100644 index 0000000..c01a7c5 --- /dev/null +++ b/sysdata/programs/dt_buffer_test/README.md @@ -0,0 +1 @@ +# dt_buffer_test \ No newline at end of file diff --git a/sysdata/programs/dt_buffer_test/meta.toml b/sysdata/programs/dt_buffer_test/meta.toml new file mode 100644 index 0000000..f25002e --- /dev/null +++ b/sysdata/programs/dt_buffer_test/meta.toml @@ -0,0 +1,11 @@ +[package] +name = "dt_buffer_test" +authors = ["able"] + +[dependants.libraries] + +[dependants.binaries] +hblang.version = "1.0.0" + +[build] +command = "hblang src/main.hb" diff --git a/sysdata/programs/dt_buffer_test/src/main.hb b/sysdata/programs/dt_buffer_test/src/main.hb new file mode 100644 index 0000000..dbc27b4 --- /dev/null +++ b/sysdata/programs/dt_buffer_test/src/main.hb @@ -0,0 +1,8 @@ +dt_api := @use("../../../libraries/dt_api/src/lib.hb") + +main := fn(): int { + dt_api.dt_get("framebuffer/fb0/width\0") + dt_api.dt_get("cpu/architecture\0") + + return 0 +} \ No newline at end of file diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index cc80cab..90f4ee3 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -39,3 +39,7 @@ path = "boot:///horizon.hbf" [boot.limine.ableos.modules.horizon_testing_program] path = "boot:///horizon_testing_program.hbf" + + +[boot.limine.ableos.modules.dt_buffer_test] +path = "boot:///dt_buffer_test.hbf"