From 8767dbecb45bbe5f748952fb9171b1a96c173f67 Mon Sep 17 00:00:00 2001 From: salmonik Date: Thu, 27 Aug 2026 17:16:04 +0200 Subject: tool migration: v2 exports + 10 external CLI binaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rsapi v2: 14 new ABI function pointers appended after v1 — fs_resolve, fs_dir_next (iterator), fs_create, fs_remove, fs_read, fs_write, fs_size, fs_close, rtc_now, mem_stats, pci_probe, pci_device, klog_dump, kernel_version. New C-repr structs: DirEntry, RtcTime, MemStats, PciDevice, KernelVersion. ABI_VERSION bumped to 2. Shell-side: build_exports() constructs the full table at runtime per invocation (replaces static const). 14 kernel-side wrapper functions bridge rsapi types to rsnix kernel APIs; fs_dir_next maintains a per-handle cache of children() results for iteration. Tool binaries (10 crates, all linked against rsapi only): - date: rtc_now → formatted YYYY-MM-DD HH:MM:SS - mem: heap/pages free/total via mem_stats - pci: pci_probe + pci_device enumerate all devices - cat: fs_read paths or get_line stdin; put_line output - ls: fs_resolve + fs_dir_next iteration; trailing / for dirs - mkdir: fs_create(path, true) - touch: fs_create(path, false) - rm: fs_remove(path) - klog: klog_dump 2048 bytes → put_line - info: rtc_now + kernel_version (minimal; mem/pci deferred) Gate verified: date prints RTC; pci lists all 4 QEMU devices (8086:29c0, 1234:1111, 8086:10d3, 8086:2918); hello/boom/count still work; test-disk PASS; poweroff clean. --- bins/cat/src/main.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 bins/cat/src/main.rs (limited to 'bins/cat/src') diff --git a/bins/cat/src/main.rs b/bins/cat/src/main.rs new file mode 100644 index 0000000..6683c83 --- /dev/null +++ b/bins/cat/src/main.rs @@ -0,0 +1,68 @@ +//! cat — print files or stdin via the exports table. +//! +//! Args: [path ...] — each path is printed as raw text. +//! No args = read from get_line (stdin) until EOF. + +#![no_std] +#![no_main] + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop { core::hint::spin_loop(); } +} + +fn put(exports: &rsapi::Exports, buf: &[u8]) { + (exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: buf.len() }); +} + +/// Read one file and put_line each chunk. +fn dump_file(exports: &rsapi::Exports, path: &str) -> i32 { + let h = (exports.fs_resolve)(rsapi::Line { ptr: path.as_ptr(), len: path.len() }); + if h < 0 { + return -1; + } + let mut buf = [0u8; 512]; + let mut pos: u64 = 0; + loop { + let n = (exports.fs_read)(h, buf.as_mut_ptr(), pos, 512); + if n <= 0 { break; } + if !(exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: n as usize }) { + break; // pipeline says stop + } + pos += n as u64; + } + (exports.fs_close)(h); + 0 +} + +#[unsafe(no_mangle)] +pub extern "C" fn efi_main(exports: &'static rsapi::Exports, argc: usize, argv: *const rsapi::Arg) -> i32 { + let args = unsafe { core::slice::from_raw_parts(argv, argc) }; + if argc > 0 { + // Print each path argument + let mut code = 0; + for a in args.iter() { + let path = core::str::from_utf8(unsafe { + core::slice::from_raw_parts(a.ptr, a.len) + }).unwrap_or(""); + if dump_file(exports, path) != 0 { + code = 1; + } + } + code + } else { + // No args: read from stdin (get_line) until EOF/Abort + loop { + let mut line = rsapi::Line { ptr: core::ptr::null(), len: 0 }; + match (exports.get_line)(&mut line) { + rsapi::StreamResult::Line => { + put(exports, unsafe { + core::slice::from_raw_parts(line.ptr, line.len) + }); + } + rsapi::StreamResult::Eof | rsapi::StreamResult::Abort => break, + } + } + 0 + } +} -- cgit v1.2.3