From 1ea751f93120f1206a4780da0e35d6f9aaaa8225 Mon Sep 17 00:00:00 2001 From: rajc6h6 Date: Tue, 10 Mar 2026 22:59:34 +0530 Subject: [PATCH 1/3] Add filesystem syscall 70 wrapper and fs_demo example --- examples/fs_demo.rs | 25 +++++++++++++++++++++++++ src/fs.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 examples/fs_demo.rs create mode 100644 src/fs.rs diff --git a/examples/fs_demo.rs b/examples/fs_demo.rs new file mode 100644 index 0000000..076f496 --- /dev/null +++ b/examples/fs_demo.rs @@ -0,0 +1,25 @@ +#![no_std] +#![no_main] + +use kos::fs::{FileInfo, fs_read}; + +static FILENAME: &[u8] = b"/rd/1/readme.txt\0"; +static mut BUFFER: [u8; 256] = [0u8; 256]; + +#[no_mangle] +pub extern "C" fn _start() -> ! { + unsafe { + let info = FileInfo { + func: 0, + param1: 0, + param2: 0, + param3: 256, + buf: BUFFER.as_mut_ptr(), + name: FILENAME.as_ptr(), + }; + + let (_result, _bytes_read) = fs_read(&info as *const FileInfo); + } + + loop {} +} diff --git a/src/fs.rs b/src/fs.rs new file mode 100644 index 0000000..4b47d6f --- /dev/null +++ b/src/fs.rs @@ -0,0 +1,31 @@ +use core::arch::asm; + +/// KolibriOS Function 70 — filesystem operations +/// Passes a pointer to a FileInfo structure via ebx +/// int 0x40 with eax=70 is the KolibriOS FS syscall + +#[repr(C, packed)] +pub struct FileInfo { + pub func: u32, // 0 = read, 1 = read folder, 2 = create/write + pub param1: u32, // offset (low) + pub param2: u32, // offset (high) or flags + pub param3: u32, // bytes to read/write + pub buf: *mut u8, // pointer to buffer + pub name: *const u8, // pointer to filename (ASCIIZ) +} + +/// Opens/reads a file on KolibriOS using syscall eax=70 +/// Returns (eax_result, ebx_bytes_read) +pub unsafe fn fs_read(info: *const FileInfo) -> (u32, u32) { + let eax_out: u32; + let ebx_out: u32; + unsafe { + asm!( + "int 0x40", + inlateout("eax") 70u32 => eax_out, + inlateout("ebx") info as u32 => ebx_out, + options(nostack) + ); + } + (eax_out, ebx_out) +} -- 2.54.0 From 922c05ceb64481e49ae82410534e47de83bbc620 Mon Sep 17 00:00:00 2001 From: rajc6h6 Date: Sat, 21 Mar 2026 12:43:38 +0530 Subject: [PATCH 2/3] refactor: move raw syscall to sys.rs, safe fs_read API, c-string fix, named struct fields --- Cargo.toml | 3 +++ examples/fs_demo.rs | 23 +++++------------------ src/fs.rs | 41 ++++++++++++----------------------------- src/lib.rs | 1 + src/sys.rs | 23 +++++++++++++++++++++++ 5 files changed, 44 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bc59867..b7d3a6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,6 @@ path = "examples/dark.rs" [profile.release] opt-level = "z" lto = "thin" + +[[example]] +name = "fs_demo" diff --git a/examples/fs_demo.rs b/examples/fs_demo.rs index 076f496..41a03b5 100644 --- a/examples/fs_demo.rs +++ b/examples/fs_demo.rs @@ -1,25 +1,12 @@ -#![no_std] +#![no_std] #![no_main] -use kos::fs::{FileInfo, fs_read}; - -static FILENAME: &[u8] = b"/rd/1/readme.txt\0"; -static mut BUFFER: [u8; 256] = [0u8; 256]; +use kos::fs::fs_read; #[no_mangle] pub extern "C" fn _start() -> ! { - unsafe { - let info = FileInfo { - func: 0, - param1: 0, - param2: 0, - param3: 256, - buf: BUFFER.as_mut_ptr(), - name: FILENAME.as_ptr(), - }; - - let (_result, _bytes_read) = fs_read(&info as *const FileInfo); - } - + let path = c"/rd/1/readme.txt"; + let mut buf = [0u8; 512]; + let (_status, _bytes) = fs_read(path, &mut buf); loop {} } diff --git a/src/fs.rs b/src/fs.rs index 4b47d6f..ed6b23c 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1,31 +1,14 @@ -use core::arch::asm; +use core::ffi::CStr; +use crate::sys::{syscall_fs, FileSystemRequest}; -/// KolibriOS Function 70 — filesystem operations -/// Passes a pointer to a FileInfo structure via ebx -/// int 0x40 with eax=70 is the KolibriOS FS syscall - -#[repr(C, packed)] -pub struct FileInfo { - pub func: u32, // 0 = read, 1 = read folder, 2 = create/write - pub param1: u32, // offset (low) - pub param2: u32, // offset (high) or flags - pub param3: u32, // bytes to read/write - pub buf: *mut u8, // pointer to buffer - pub name: *const u8, // pointer to filename (ASCIIZ) -} - -/// Opens/reads a file on KolibriOS using syscall eax=70 -/// Returns (eax_result, ebx_bytes_read) -pub unsafe fn fs_read(info: *const FileInfo) -> (u32, u32) { - let eax_out: u32; - let ebx_out: u32; - unsafe { - asm!( - "int 0x40", - inlateout("eax") 70u32 => eax_out, - inlateout("ebx") info as u32 => ebx_out, - options(nostack) - ); - } - (eax_out, ebx_out) +pub fn fs_read(path: &CStr, buf: &mut [u8]) -> (u32, u32) { + let req = FileSystemRequest { + operation: 0, + offset_low: 0, + offset_high: 0, + byte_count: buf.len() as u32, + buffer: buf.as_mut_ptr() as u32, + path: path.as_ptr() as u32, + }; + unsafe { syscall_fs(&req) } } diff --git a/src/lib.rs b/src/lib.rs index 296cdf8..063bc76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ mod nanolibc; pub mod allocation; pub mod dll; pub mod sys; +pub mod fs; pub use modules::*; diff --git a/src/sys.rs b/src/sys.rs index 41188e8..eb10ed2 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -68,3 +68,26 @@ extern "C" { #[link_name = "_load_dll"] pub fn load_dll(name: *const u8) -> *const u32; } + +#[repr(C)] +pub struct FileSystemRequest { + pub operation: u32, + pub offset_low: u32, + pub offset_high: u32, + pub byte_count: u32, + pub buffer: u32, + pub path: u32, +} + +pub unsafe fn syscall_fs(req: *const FileSystemRequest) -> (u32, u32) { + let eax_result: u32; + let ebx_result: u32; + core::arch::asm!( + "int 0x40", + inout("eax") 70u32 => eax_result, + in("ebx") req, + out("ebx") ebx_result, + options(nostack) + ); + (eax_result, ebx_result) +} -- 2.54.0 From 809021d748bf30faaa75dba08068c418968d607c Mon Sep 17 00:00:00 2001 From: rajc6h6 Date: Sat, 21 Mar 2026 13:29:04 +0530 Subject: [PATCH 3/3] feat: add remaining console.obj functions (closes #1) --- src/dll/console.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/dll/console.rs b/src/dll/console.rs index f5a5a08..ed4b50a 100644 --- a/src/dll/console.rs +++ b/src/dll/console.rs @@ -6,10 +6,23 @@ pub struct Console { con_init: extern "stdcall" fn(u32, u32, u32, u32, *const u8), con_write_string: extern "stdcall" fn(*const u8, u32), con_exit: extern "stdcall" fn(bool), + con_set_title: extern "stdcall" fn(*const u8), + con_get_flags: extern "stdcall" fn() -> u32, + con_set_flags: extern "stdcall" fn(u32) -> u32, + con_get_font_height: extern "stdcall" fn() -> u32, + con_get_cursor_height: extern "stdcall" fn() -> u32, + con_set_cursor_height: extern "stdcall" fn(u32) -> u32, + con_getch: extern "stdcall" fn() -> u32, + con_getch2: extern "stdcall" fn() -> u16, + con_kbhit: extern "stdcall" fn() -> u32, + con_gets: extern "stdcall" fn(*mut u8, u32) -> *mut u8, + con_cls: extern "stdcall" fn(), + con_get_cursor_pos: extern "stdcall" fn(*mut u32, *mut u32), + con_set_cursor_pos: extern "stdcall" fn(u32, u32), } impl Console { - pub fn import(path: Option<&CStr>) -> Result { + pub fn import(path: Option<&CStr>) -> Result { let lib = DLL::load_dll(path.unwrap_or(c"/sys/lib/console.obj")); match lib { Err(e) => return Err(e), @@ -18,6 +31,19 @@ impl Console { con_init: transmute(dll.get_func(c"con_init").ok().unwrap()), con_write_string: transmute(dll.get_func(c"con_write_string").ok().unwrap()), con_exit: transmute(dll.get_func(c"con_exit").ok().unwrap()), + con_set_title: transmute(dll.get_func(c"con_set_title").ok().unwrap()), + con_get_flags: transmute(dll.get_func(c"con_get_flags").ok().unwrap()), + con_set_flags: transmute(dll.get_func(c"con_set_flags").ok().unwrap()), + con_get_font_height: transmute(dll.get_func(c"con_get_font_height").ok().unwrap()), + con_get_cursor_height: transmute(dll.get_func(c"con_get_cursor_height").ok().unwrap()), + con_set_cursor_height: transmute(dll.get_func(c"con_set_cursor_height").ok().unwrap()), + con_getch: transmute(dll.get_func(c"con_getch").ok().unwrap()), + con_getch2: transmute(dll.get_func(c"con_getch2").ok().unwrap()), + con_kbhit: transmute(dll.get_func(c"con_kbhit").ok().unwrap()), + con_gets: transmute(dll.get_func(c"con_gets").ok().unwrap()), + con_cls: transmute(dll.get_func(c"con_cls").ok().unwrap()), + con_get_cursor_pos: transmute(dll.get_func(c"con_get_cursor_pos").ok().unwrap()), + con_set_cursor_pos: transmute(dll.get_func(c"con_set_cursor_pos").ok().unwrap()), }) }, } @@ -34,4 +60,60 @@ impl Console { pub fn exit(self, close_window: bool) { (self.con_exit)(close_window); } + + pub fn set_title(&self, title: &CStr) { + (self.con_set_title)(title.as_ptr() as *const u8); + } + + pub fn get_flags(&self) -> u32 { + (self.con_get_flags)() + } + + pub fn set_flags(&self, flags: u32) -> u32 { + (self.con_set_flags)(flags) + } + + pub fn get_font_height(&self) -> u32 { + (self.con_get_font_height)() + } + + pub fn get_cursor_height(&self) -> u32 { + (self.con_get_cursor_height)() + } + + pub fn set_cursor_height(&self, height: u32) -> u32 { + (self.con_set_cursor_height)(height) + } + + pub fn getch(&self) -> u32 { + (self.con_getch)() + } + + pub fn getch2(&self) -> u16 { + (self.con_getch2)() + } + + pub fn kbhit(&self) -> u32 { + (self.con_kbhit)() + } + + pub fn gets<'a>(&self, buf: &'a mut [u8]) -> Option<&'a [u8]> { + let ptr = (self.con_gets)(buf.as_mut_ptr(), buf.len() as u32); + if ptr.is_null() { None } else { Some(buf) } + } + + pub fn cls(&self) { + (self.con_cls)(); + } + + pub fn get_cursor_pos(&self) -> (u32, u32) { + let mut x = 0u32; + let mut y = 0u32; + (self.con_get_cursor_pos)(&mut x, &mut y); + (x, y) + } + + pub fn set_cursor_pos(&self, x: u32, y: u32) { + (self.con_set_cursor_pos)(x, y); + } } -- 2.54.0