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/Cargo.toml | 8 ++++++ bins/cat/src/main.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ bins/date/Cargo.toml | 8 ++++++ bins/date/src/main.rs | 38 ++++++++++++++++++++++++++++ bins/info/Cargo.toml | 8 ++++++ bins/info/src/main.rs | 22 ++++++++++++++++ bins/klog/Cargo.toml | 8 ++++++ bins/klog/src/main.rs | 19 ++++++++++++++ bins/ls/Cargo.toml | 8 ++++++ bins/ls/src/main.rs | 47 ++++++++++++++++++++++++++++++++++ bins/mem/Cargo.toml | 8 ++++++ bins/mem/src/main.rs | 47 ++++++++++++++++++++++++++++++++++ bins/mkdir/Cargo.toml | 8 ++++++ bins/mkdir/src/main.rs | 18 +++++++++++++ bins/pci/Cargo.toml | 8 ++++++ bins/pci/src/main.rs | 53 +++++++++++++++++++++++++++++++++++++++ bins/rm/Cargo.toml | 8 ++++++ bins/rm/src/main.rs | 17 +++++++++++++ bins/touch/Cargo.toml | 8 ++++++ bins/touch/src/main.rs | 17 +++++++++++++ 20 files changed, 426 insertions(+) create mode 100644 bins/cat/Cargo.toml create mode 100644 bins/cat/src/main.rs create mode 100644 bins/date/Cargo.toml create mode 100644 bins/date/src/main.rs create mode 100644 bins/info/Cargo.toml create mode 100644 bins/info/src/main.rs create mode 100644 bins/klog/Cargo.toml create mode 100644 bins/klog/src/main.rs create mode 100644 bins/ls/Cargo.toml create mode 100644 bins/ls/src/main.rs create mode 100644 bins/mem/Cargo.toml create mode 100644 bins/mem/src/main.rs create mode 100644 bins/mkdir/Cargo.toml create mode 100644 bins/mkdir/src/main.rs create mode 100644 bins/pci/Cargo.toml create mode 100644 bins/pci/src/main.rs create mode 100644 bins/rm/Cargo.toml create mode 100644 bins/rm/src/main.rs create mode 100644 bins/touch/Cargo.toml create mode 100644 bins/touch/src/main.rs (limited to 'bins') diff --git a/bins/cat/Cargo.toml b/bins/cat/Cargo.toml new file mode 100644 index 0000000..c5990d5 --- /dev/null +++ b/bins/cat/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "cat" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } 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 + } +} diff --git a/bins/date/Cargo.toml b/bins/date/Cargo.toml new file mode 100644 index 0000000..795cc6c --- /dev/null +++ b/bins/date/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "date" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/date/src/main.rs b/bins/date/src/main.rs new file mode 100644 index 0000000..880833e --- /dev/null +++ b/bins/date/src/main.rs @@ -0,0 +1,38 @@ +//! date — show current RTC time via the exports table. + +#![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() }); +} + +fn dig(b: u8) -> [u8; 2] { [b'0' + b / 10, b'0' + b % 10] } + +#[unsafe(no_mangle)] +pub extern "C" fn efi_main(exports: &'static rsapi::Exports, _argc: usize, _argv: *const rsapi::Arg) -> i32 { + let mut t = rsapi::RtcTime { year: 0, month: 0, day: 0, hour: 0, minute: 0, second: 0, _pad: 0 }; + (exports.rtc_now)(&mut t); + // "YYYY-MM-DD HH:MM:SS" = 19 bytes + let mut buf = [0u8; 19]; + let y = t.year; + buf[0] = b'0' + (y / 1000) as u8; buf[1] = b'0' + (y / 100) as u8 % 10; + buf[2] = b'0' + (y / 10) as u8 % 10; buf[3] = b'0' + y as u8 % 10; + buf[4] = b'-'; + let m = dig(t.month); buf[5] = m[0]; buf[6] = m[1]; + buf[7] = b'-'; + let d = dig(t.day); buf[8] = d[0]; buf[9] = d[1]; + buf[10] = b' '; + let h = dig(t.hour); buf[11] = h[0]; buf[12] = h[1]; + buf[13] = b':'; + let mi = dig(t.minute); buf[14] = mi[0]; buf[15] = mi[1]; + buf[16] = b':'; + let s = dig(t.second); buf[17] = s[0]; buf[18] = s[1]; + put(exports, &buf); + 0 +} diff --git a/bins/info/Cargo.toml b/bins/info/Cargo.toml new file mode 100644 index 0000000..b22a534 --- /dev/null +++ b/bins/info/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "info" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/info/src/main.rs b/bins/info/src/main.rs new file mode 100644 index 0000000..cf1bc9f --- /dev/null +++ b/bins/info/src/main.rs @@ -0,0 +1,22 @@ +//! info — show system information via the exports table. + +#![no_std] +#![no_main] + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop { core::hint::spin_loop(); } +} + +fn put(exports: &rsapi::Exports, s: &str) { + (exports.put_line)(rsapi::Line { ptr: s.as_ptr(), len: s.len() }); +} + +#[unsafe(no_mangle)] +pub extern "C" fn efi_main(exports: &'static rsapi::Exports, _argc: usize, _argv: *const rsapi::Arg) -> i32 { + let mut t = rsapi::RtcTime { year: 0, month: 0, day: 0, hour: 0, minute: 0, second: 0, _pad: 0 }; + (exports.rtc_now)(&mut t); + put(exports, "rs-dos v0.6.0 - external info"); + put(exports, "kernel: rsnix 0.6.0"); + 0 +} diff --git a/bins/klog/Cargo.toml b/bins/klog/Cargo.toml new file mode 100644 index 0000000..73957ec --- /dev/null +++ b/bins/klog/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "klog" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/klog/src/main.rs b/bins/klog/src/main.rs new file mode 100644 index 0000000..fcd3512 --- /dev/null +++ b/bins/klog/src/main.rs @@ -0,0 +1,19 @@ +//! klog — dump kernel log via the exports table. + +#![no_std] +#![no_main] + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop { core::hint::spin_loop(); } +} + +#[unsafe(no_mangle)] +pub extern "C" fn efi_main(exports: &'static rsapi::Exports, _argc: usize, _argv: *const rsapi::Arg) -> i32 { + let mut buf = [0u8; 2048]; + let n = (exports.klog_dump)(buf.as_mut_ptr(), buf.len() as u64) as usize; + if n > 0 { + (exports.put_line)(rsapi::Line { ptr: buf.as_ptr(), len: n }); + } + 0 +} diff --git a/bins/ls/Cargo.toml b/bins/ls/Cargo.toml new file mode 100644 index 0000000..78497bd --- /dev/null +++ b/bins/ls/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ls" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/ls/src/main.rs b/bins/ls/src/main.rs new file mode 100644 index 0000000..956bf18 --- /dev/null +++ b/bins/ls/src/main.rs @@ -0,0 +1,47 @@ +//! ls — list directory contents via the exports table. +//! +//! Args: [path] — defaults to "." (root). + +#![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() }); +} + +#[unsafe(no_mangle)] +pub extern "C" fn efi_main(exports: &'static rsapi::Exports, argc: usize, argv: *const rsapi::Arg) -> i32 { + let path_str = if argc > 0 { + let a = unsafe { &*argv }; + unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts(a.ptr, a.len)) } + } else { + "/" + }; + let h = (exports.fs_resolve)(rsapi::Line { ptr: path_str.as_ptr(), len: path_str.len() }); + if h < 0 { return 1; } + let mut entry = rsapi::DirEntry { + name: rsapi::Line { ptr: core::ptr::null(), len: 0 }, + is_dir: false, + _pad: 0, + size: 0, + }; + while (exports.fs_dir_next)(h, &mut entry) { + let name = unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts(entry.name.ptr, entry.name.len)) }; + if entry.is_dir { + let mut buf = [0u8; 64]; + let nb = name.as_bytes(); + buf[..nb.len()].copy_from_slice(nb); + buf[nb.len()] = b'/'; + put(exports, &buf[..nb.len() + 1]); + } else { + put(exports, name.as_bytes()); + } + } + (exports.fs_close)(h); + 0 +} diff --git a/bins/mem/Cargo.toml b/bins/mem/Cargo.toml new file mode 100644 index 0000000..2306fc7 --- /dev/null +++ b/bins/mem/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "mem" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/mem/src/main.rs b/bins/mem/src/main.rs new file mode 100644 index 0000000..fd99f47 --- /dev/null +++ b/bins/mem/src/main.rs @@ -0,0 +1,47 @@ +//! mem — show memory statistics via the exports table. + +#![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() }); +} + +fn fmt_kib(exports: &rsapi::Exports, v: u64) { + let mut buf = [0u8; 32]; + let kb = v / 1024; + let mut i = buf.len(); + if kb == 0 { i -= 1; buf[i] = b'0'; } + else { let mut n = kb; while n > 0 { i -= 1; buf[i] = b'0' + (n % 10) as u8; n /= 10; } } + let s = b" KiB"; + buf[i..i+s.len()].copy_from_slice(s); + put(exports, &buf[i..i+s.len()]); +} + +#[unsafe(no_mangle)] +pub extern "C" fn efi_main(exports: &'static rsapi::Exports, _argc: usize, _argv: *const rsapi::Arg) -> i32 { + let mut s = rsapi::MemStats { heap_total: 0, heap_free: 0, pages_total: 0, pages_free: 0 }; + (exports.mem_stats)(&mut s); + let msg = b"heap:"; + put(exports, msg); + put(exports, b" free"); fmt_kib(exports, s.heap_free); + put(exports, b" / total"); fmt_kib(exports, s.heap_total); + let msg2 = b"pages:"; + put(exports, msg2); + put(exports, b" free"); + let mut fb = [0u8; 16]; + let mut i = fb.len(); + let n = s.pages_free; if n==0 { i-=1; fb[i]=b'0'; } else { let mut m=n; while m>0 { i-=1; fb[i]=b'0'+(m%10) as u8; m/=10; } } + put(exports, &fb[i..]); + put(exports, b" / "); + let mut tb = [0u8; 16]; + let mut j = tb.len(); + let t = s.pages_total; if t==0 { j-=1; tb[j]=b'0'; } else { let mut m=t; while m>0 { j-=1; tb[j]=b'0'+(m%10) as u8; m/=10; } } + put(exports, &tb[j..]); + 0 +} diff --git a/bins/mkdir/Cargo.toml b/bins/mkdir/Cargo.toml new file mode 100644 index 0000000..ca97f6c --- /dev/null +++ b/bins/mkdir/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "mkdir" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/mkdir/src/main.rs b/bins/mkdir/src/main.rs new file mode 100644 index 0000000..f6f7e66 --- /dev/null +++ b/bins/mkdir/src/main.rs @@ -0,0 +1,18 @@ +//! mkdir — create directories via the exports table. + +#![no_std] +#![no_main] + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop { core::hint::spin_loop(); } +} + +#[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 args.is_empty() { return 1; } + let path = unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts(args[0].ptr, args[0].len)) }; + let rc = (exports.fs_create)(rsapi::Line { ptr: path.as_ptr(), len: path.len() }, true); + rc +} diff --git a/bins/pci/Cargo.toml b/bins/pci/Cargo.toml new file mode 100644 index 0000000..85590f8 --- /dev/null +++ b/bins/pci/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "pci" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/pci/src/main.rs b/bins/pci/src/main.rs new file mode 100644 index 0000000..1182d83 --- /dev/null +++ b/bins/pci/src/main.rs @@ -0,0 +1,53 @@ +//! pci — list PCI devices via the exports table. + +#![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() }); +} + +fn hex2(b: u8, out: &mut [u8]) { + let hi = b >> 4; let lo = b & 0xf; + out[0] = if hi < 10 { b'0' + hi } else { b'a' + hi - 10 }; + out[1] = if lo < 10 { b'0' + lo } else { b'a' + lo - 10 }; +} + +#[unsafe(no_mangle)] +pub extern "C" fn efi_main(exports: &'static rsapi::Exports, _argc: usize, _argv: *const rsapi::Arg) -> i32 { + let count = (exports.pci_probe)(); + let mut i = 0u32; + while i < count { + let mut d = rsapi::PciDevice { bus: 0, device: 0, function: 0, class: 0, subclass: 0, vendor: 0, device_id: 0 }; + if (exports.pci_device)(i, &mut d) { + // "XX:XX.F class=XX:XX ven=XXXX dev=XXXX" = 42 bytes max + let mut buf = [0u8; 42]; + hex2(d.bus, &mut buf[0..2]); buf[2] = b':'; + hex2(d.device, &mut buf[3..5]); buf[5] = b'.'; + buf[6] = b'0' + d.function; + let class_lbl = b" class="; + buf[7..7+class_lbl.len()].copy_from_slice(class_lbl); + let o = 7 + class_lbl.len(); + hex2(d.class, &mut buf[o..o+2]); buf[o+2] = b':'; + hex2(d.subclass, &mut buf[o+3..o+5]); + let ven_lbl = b" ven="; + buf[o+5..o+5+ven_lbl.len()].copy_from_slice(ven_lbl); + let v = o + 5 + ven_lbl.len(); + hex2((d.vendor >> 8) as u8, &mut buf[v..v+2]); + hex2((d.vendor & 0xff) as u8, &mut buf[v+2..v+4]); + let dev_lbl = b" dev="; + buf[v+4..v+4+dev_lbl.len()].copy_from_slice(dev_lbl); + let dd = v + 4 + dev_lbl.len(); + hex2((d.device_id >> 8) as u8, &mut buf[dd..dd+2]); + hex2((d.device_id & 0xff) as u8, &mut buf[dd+2..dd+4]); + put(exports, &buf[..dd+4]); + } + i += 1; + } + 0 +} diff --git a/bins/rm/Cargo.toml b/bins/rm/Cargo.toml new file mode 100644 index 0000000..b49abac --- /dev/null +++ b/bins/rm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rm" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/rm/src/main.rs b/bins/rm/src/main.rs new file mode 100644 index 0000000..3407563 --- /dev/null +++ b/bins/rm/src/main.rs @@ -0,0 +1,17 @@ +//! rm — remove a file or empty directory via the exports table. + +#![no_std] +#![no_main] + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop { core::hint::spin_loop(); } +} + +#[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 args.is_empty() { return 1; } + let path = unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts(args[0].ptr, args[0].len)) }; + (exports.fs_remove)(rsapi::Line { ptr: path.as_ptr(), len: path.len() }) +} diff --git a/bins/touch/Cargo.toml b/bins/touch/Cargo.toml new file mode 100644 index 0000000..54824e7 --- /dev/null +++ b/bins/touch/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "touch" +version.workspace = true +edition.workspace = true +[lints] +workspace = true +[dependencies] +rsapi = { path = "../../rsapi" } diff --git a/bins/touch/src/main.rs b/bins/touch/src/main.rs new file mode 100644 index 0000000..5cd61e3 --- /dev/null +++ b/bins/touch/src/main.rs @@ -0,0 +1,17 @@ +//! touch — create an empty file via the exports table. + +#![no_std] +#![no_main] + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop { core::hint::spin_loop(); } +} + +#[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 args.is_empty() { return 1; } + let path = unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts(args[0].ptr, args[0].len)) }; + (exports.fs_create)(rsapi::Line { ptr: path.as_ptr(), len: path.len() }, false) +} -- cgit v1.2.3