Skip to content

Commit

Permalink
Add binaries (#350)
Browse files Browse the repository at this point in the history
* Remove linker args

* Add binaries

* Read /bin for autocomplete

* Add /bin path to commands

* Remove api bin

* Remove usr reboot
  • Loading branch information
vinc authored Jun 12, 2022
1 parent cd2e016 commit 6e3f3af
Show file tree
Hide file tree
Showing 20 changed files with 128 additions and 62 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ user-rust:
touch dsk/bin/{}
basename -s .rs src/bin/*.rs | xargs -I {} \
cargo rustc --release --bin {} -- \
-C linker-flavor=ld \
-C link-args="-Ttext=200 -Trodata=2000" \
-C relocation-model=static
basename -s .rs src/bin/*.rs | xargs -I {} \
cp target/x86_64-moros/release/{} dsk/bin/{}
Expand Down
Binary file added dsk/bin/clear
Binary file not shown.
Binary file added dsk/bin/halt
Binary file not shown.
Binary file modified dsk/bin/hello
Binary file not shown.
Binary file added dsk/bin/reboot
Binary file not shown.
Binary file modified dsk/bin/sleep
Binary file not shown.
16 changes: 16 additions & 0 deletions dsk/src/bin/clear.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[bits 64]

section .data
msg: db "Not implemented", 10

global _start
section .text
_start:
mov rax, 4 ; syscall number for WRITE
mov rdi, 1 ; standard output
mov rsi, msg ; addr of string
mov rdx, 16 ; size of string
int 0x80
mov rax, 1 ; syscall number for EXIT
mov rdi, 0 ; no error
int 0x80
16 changes: 16 additions & 0 deletions dsk/src/bin/halt.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[bits 64]

section .data
msg: db "Not implemented", 10

global _start
section .text
_start:
mov rax, 4 ; syscall number for WRITE
mov rdi, 1 ; standard output
mov rsi, msg ; addr of string
mov rdx, 16 ; size of string
int 0x80
mov rax, 1 ; syscall number for EXIT
mov rdi, 0 ; no error
int 0x80
16 changes: 16 additions & 0 deletions dsk/src/bin/reboot.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[bits 64]

section .data
msg: db "Not implemented", 10

global _start
section .text
_start:
mov rax, 4 ; syscall number for WRITE
mov rdi, 1 ; standard output
mov rsi, msg ; addr of string
mov rdx, 16 ; size of string
int 0x80
mov rax, 1 ; syscall number for EXIT
mov rdi, 0 ; no error
int 0x80
10 changes: 0 additions & 10 deletions dsk/src/bin/sleep.s

This file was deleted.

6 changes: 3 additions & 3 deletions src/bin/sleep.rs → src/bin/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
syscall::write(1, b"An exception occured!\n");
loop {}
}

#[no_mangle]
pub unsafe extern "sysv64" fn _start() -> ! {
syscall::sleep(5.0);
pub unsafe extern "sysv64" fn _start() {
syscall::write(1, b"\x1b[2J\x1b[1;1H"); // Clear screen and move cursor to top
syscall::exit(0);
unreachable!();
}
22 changes: 22 additions & 0 deletions src/bin/halt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_std]
#![no_main]

use moros::api::syscall;

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
syscall::write(1, b"An exception occured!\n");
loop {}
}

#[no_mangle]
pub unsafe extern "sysv64" fn _start() {
syscall::write(1, b"\x1b[93m"); // Yellow
syscall::write(1, b"MOROS has reached its fate, the system is now halting.\n");
syscall::write(1, b"\x1b[0m"); // Reset
syscall::sleep(0.5);
syscall::halt();
loop { syscall::sleep(1.0) }
}
4 changes: 2 additions & 2 deletions src/bin/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
syscall::write(1, b"An exception occured!\n");
loop {}
}

#[no_mangle]
pub unsafe extern "sysv64" fn _start() -> ! {
pub unsafe extern "sysv64" fn _start() {
syscall::write(1, b"Hello, World!\n");
syscall::exit(0);
unreachable!();
}
22 changes: 22 additions & 0 deletions src/bin/reboot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_std]
#![no_main]

use moros::api::syscall;

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
syscall::write(1, b"An exception occured!\n");
loop {}
}

#[no_mangle]
pub unsafe extern "sysv64" fn _start() {
syscall::write(1, b"\x1b[93m"); // Yellow
syscall::write(1, b"MOROS has reached its fate, the system is now rebooting.\n");
syscall::write(1, b"\x1b[0m"); // Reset
syscall::sleep(0.5);
syscall::reboot();
loop { syscall::sleep(1.0) }
}
6 changes: 0 additions & 6 deletions src/usr/clear.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/usr/halt.rs

This file was deleted.

4 changes: 3 additions & 1 deletion src/usr/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ pub fn copy_files(verbose: bool) {
create_dir("/usr", verbose); // User directories
create_dir("/var", verbose); // Variables

copy_file("/bin/clear", include_bytes!("../../dsk/bin/clear"), verbose);
copy_file("/bin/halt", include_bytes!("../../dsk/bin/halt"), verbose);
copy_file("/bin/hello", include_bytes!("../../dsk/bin/hello"), verbose);
copy_file("/bin/sleep", include_bytes!("../../dsk/bin/sleep"), verbose);
copy_file("/bin/reboot", include_bytes!("../../dsk/bin/reboot"), verbose);

create_dir("/dev/clk", verbose); // Clocks
create_dev("/dev/clk/uptime", DeviceType::Uptime, verbose);
Expand Down
3 changes: 0 additions & 3 deletions src/usr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod base64;
pub mod beep;
pub mod calc;
pub mod chess;
pub mod clear;
pub mod colors;
pub mod copy;
pub mod date;
Expand All @@ -14,7 +13,6 @@ pub mod elf;
pub mod env;
pub mod find;
pub mod geotime;
pub mod halt;
pub mod help;
pub mod hex;
pub mod host;
Expand All @@ -31,7 +29,6 @@ pub mod pow;
pub mod print;
pub mod r#move;
pub mod read;
pub mod reboot;
pub mod shell;
pub mod sleep;
pub mod socket;
Expand Down
12 changes: 0 additions & 12 deletions src/usr/reboot.rs

This file was deleted.

39 changes: 28 additions & 11 deletions src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use alloc::vec::Vec;
use alloc::string::{String, ToString};

// TODO: Scan /bin
const AUTOCOMPLETE_COMMANDS: [&str; 40] = [
"2048", "base64", "calc", "clear", "colors", "copy", "date", "delete", "dhcp", "disk", "edit",
"env", "exit", "geotime", "goto", "halt", "help", "hex", "host", "http", "httpd", "install",
"keyboard", "lisp", "list", "memory", "move", "net", "pci", "print", "read", "reboot", "shell",
"sleep", "socket", "tcp", "time", "user", "vga", "write"
const AUTOCOMPLETE_COMMANDS: [&str; 37] = [
"2048", "base64", "calc", "colors", "copy", "date", "delete", "dhcp", "disk", "edit",
"env", "exit", "geotime", "goto", "help", "hex", "host", "http", "httpd", "install",
"keyboard", "lisp", "list", "memory", "move", "net", "pci", "print", "read",
"shell", "sleep", "socket", "tcp", "time", "user", "vga", "write"
];

#[repr(u8)]
Expand All @@ -28,13 +28,27 @@ pub enum ExitCode {
ShellExit = 255,
}

fn autocomplete_commands() -> Vec<String> {
let mut res = Vec::new();
for cmd in AUTOCOMPLETE_COMMANDS {
res.push(cmd.to_string());
}
if let Ok(files) = fs::read_dir("/bin") {
for file in files {
res.push(file.name());
}
}
res.sort();
res
}

fn shell_completer(line: &str) -> Vec<String> {
let mut entries = Vec::new();

let args = split_args(line);
let i = args.len() - 1;
if args.len() == 1 && !args[0].starts_with('/') { // Autocomplete command
for &cmd in &AUTOCOMPLETE_COMMANDS {
for cmd in autocomplete_commands() {
if let Some(entry) = cmd.strip_prefix(args[i]) {
entries.push(entry.into());
}
Expand Down Expand Up @@ -276,13 +290,10 @@ pub fn exec(cmd: &str, env: &mut BTreeMap<String, String>) -> ExitCode {
"vga" => usr::vga::main(&args),
"sh" | "shell" => usr::shell::main(&args),
"sleep" => usr::sleep::main(&args),
"clear" => usr::clear::main(&args),
"calc" => usr::calc::main(&args),
"base64" => usr::base64::main(&args),
"date" => usr::date::main(&args),
"env" => usr::env::main(&args),
"halt" => usr::halt::main(&args),
"reboot" => usr::reboot::main(&args),
"hex" => usr::hex::main(&args),
"net" => usr::net::main(&args),
"dhcp" => usr::dhcp::main(&args),
Expand Down Expand Up @@ -318,15 +329,21 @@ pub fn exec(cmd: &str, env: &mut BTreeMap<String, String>) -> ExitCode {
}
Some(FileType::File) => {
if api::process::spawn(&path).is_ok() {
// TODO: get exit code
ExitCode::CommandSuccessful
} else {
error!("'{}' is not executable", path);
ExitCode::CommandError
}
}
_ => {
error!("Could not execute '{}'", cmd);
ExitCode::CommandUnknown
// TODO: add aliases
if api::process::spawn(&format!("/bin/{}", args[0])).is_ok() {
ExitCode::CommandSuccessful
} else {
error!("Could not execute '{}'", cmd);
ExitCode::CommandUnknown
}
}
}
}
Expand Down

0 comments on commit 6e3f3af

Please sign in to comment.