From 554872c8facfd0b6a17632d487bb04bd21c9ee70 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 21 Feb 2018 13:28:59 +0100 Subject: [PATCH] added console.rs to group commands, refactored the command buffer and command matching code --- kernel-rs/src/console.rs | 41 +++++++++++++++++++++++++ kernel-rs/src/cpuio.rs | 4 +++ kernel-rs/src/lib.rs | 60 +++++++------------------------------ kernel-rs/src/vga/buffer.rs | 53 +++++++++++++------------------- 4 files changed, 75 insertions(+), 83 deletions(-) create mode 100644 kernel-rs/src/console.rs diff --git a/kernel-rs/src/console.rs b/kernel-rs/src/console.rs new file mode 100644 index 00000000..c5f8a075 --- /dev/null +++ b/kernel-rs/src/console.rs @@ -0,0 +1,41 @@ +use cpuio; + +//TODO implement ACPI to have such functionality +/// Reboot the kernel +/// +/// If reboot failed, will loop on a halt cmd +/// +pub fn reboot() { + //TODO disable interrupt here something like : asm volatile ("cli"); + + // I will now clear the keyboard buffer + let mut buffer: u8 = 0x02; + while buffer == 0x02 { + buffer = cpuio::inb(0x64); + } + cpuio::outb(0x64, 0xFE);//Send reset value to CPU //TODO doesn't work + println!("Reicv reboot command. System cannot reboot yet, he is now halt\n"); + cpuio::halt(); +} + +/// Shutdown the kernel +/// +/// # Pre-requist: +/// Seems that he have to use following line command : +/// `-device isa-debug-exit,iobase=0xf4,iosize=0x04` +/// +/// If shutdown failed, will loop on a halt cmd +/// +pub fn shutdown() -> ! { + cpuio::outb(0xf4, 0x00);//TODO doesn't work :( + println!("Reicv shutdown command. System cannot shutdown properly yet, he is now halt\n"); + cpuio::halt(); +} + +/// Print the kernel stack +/// +pub fn print_kernel_stack() { + println!("It's a stack print"); + +} + diff --git a/kernel-rs/src/cpuio.rs b/kernel-rs/src/cpuio.rs index 5ce962a2..30821f39 100644 --- a/kernel-rs/src/cpuio.rs +++ b/kernel-rs/src/cpuio.rs @@ -1,4 +1,8 @@ //! Rust wrappers around the x86-family I/O instructions. +//! this cmodule uses inline assembly so you need to add +//! `#![feature(asm)]` to yourcrate attributes + +#![allow(dead_code)] /// Read a `u8`-sized value from `port`. pub fn inb(port: u16) -> u8 { diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index d9bbf662..b47e56e6 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -1,64 +1,24 @@ +#![no_std] + #![feature(lang_items)] #![feature(const_fn)] #![feature(ptr_internals)] -#![feature(asm)] -#![no_std] +#![feature(asm)] //needed by cpuio for inline asm -extern crate spin; -extern crate volatile; +// extern crate spin; +// extern crate volatile; extern crate rlibc; #[macro_use] -mod vga; - -mod context; -mod keyboard; +mod vga; // 80x25 screen and terminal +mod context; // kernel init and environment +mod keyboard; // PS/2 detection and processing +mod console; // vga terminal commands +mod cpuio; // asm wrapper to cpu i/o use context::CONTEXT; use vga::{Color, ColorCode}; -#[allow(dead_code)] -mod cpuio; - -//TODO implement ACPI to have such functionality -/// Reboot the kernel -/// -/// If reboot failed, will loop on a halt cmd -/// -fn reboot() { - //TODO disable interrupt here something like : asm volatile ("cli"); - - // I will now clear the keyboard buffer - let mut buffer: u8 = 0x02; - while buffer == 0x02 { - buffer = cpuio::inb(0x64); - } - cpuio::outb(0x64, 0xFE);//Send reset value to CPU //TODO doesn't work - println!("Reicv reboot command. System cannot reboot yet, he is now halt\n"); - cpuio::halt(); -} - -/// Shutdown the kernel -/// -/// # Pre-requist: -/// Seems that he have to use following line command : -/// `-device isa-debug-exit,iobase=0xf4,iosize=0x04` -/// -/// If shutdown failed, will loop on a halt cmd -/// -fn shutdown() -> ! { - cpuio::outb(0xf4, 0x00);//TODO doesn't work :( - println!("Reicv shutdown command. System cannot shutdown properly yet, he is now halt\n"); - cpuio::halt(); -} - -/// Print the kernel stack -/// -fn print_kernel_stack() { - println!("It's a stack print"); - -} - #[no_mangle] pub extern fn kmain() -> ! { unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Cyan); } diff --git a/kernel-rs/src/vga/buffer.rs b/kernel-rs/src/vga/buffer.rs index b454fc1d..65724e7d 100644 --- a/kernel-rs/src/vga/buffer.rs +++ b/kernel-rs/src/vga/buffer.rs @@ -10,6 +10,7 @@ use super::{Color, ColorCode}; use ::context::CONTEXT; use cpuio; +use ::console; #[derive(Debug, Clone, Copy)] #[repr(C)] @@ -20,8 +21,8 @@ struct ScreenChar { macro_rules! print { ($($arg:tt)*) => ({ - $crate::vga::buffer::print(format_args!($($arg)*)); - }); + $crate::vga::buffer::print(format_args!($($arg)*)); + }); } macro_rules! println { @@ -44,26 +45,17 @@ pub struct Writer { pub buffer_pos: usize, pub color_code: ColorCode, buffer: [u8; BUFFER_ROWS * BUFFER_COLS], - command: [char; 10], + command: [u8; 10], command_len: usize, } -// enum shell_command { -// "reboot" => super::reboot(); -// } - -const NULL: [char; 10] = ['\0'; 10]; -const REBOOT: [char; 10] = ['r', 'e', 'b', 'o', 'o', 't', '\0', '\0', '\0', '\0']; -const HALT: [char; 10] = ['h', 'a', 'l', 't', '\0', '\0', '\0', '\0', '\0', '\0']; -const SHUTDOWN: [char; 10] = ['s', 'h', 'u', 't', 'd', 'o', 'w', 'n', '\0', '\0']; -const STACK: [char; 10] = ['s', 't', 'a', 'c', 'k', '\0', '\0', '\0', '\0', '\0']; impl Writer { pub const fn new() -> Writer { Writer { buffer_pos: 0, color_code: ColorCode::new(Color::White, Color::Black), buffer: [0; BUFFER_ROWS * BUFFER_COLS], - command: NULL, + command: [b'\0'; 10], command_len: 0, } } @@ -78,7 +70,6 @@ impl Writer { pub fn backspace(&mut self) { if self.command_len > 0 { self.command_len -= 1; - self.command[self.command_len] = '\0'; self.erase_byte(); } } @@ -86,33 +77,28 @@ impl Writer { pub fn keypress(&mut self, ascii: u8) { match ascii { b'\n' => { - self.command_len = 0; self.write_byte(b'\n'); - // println!("{:?}", self.command.iter()); - match self.command { - SHUTDOWN | HALT => { - super::super::shutdown(); - } - REBOOT => { - super::super::reboot(); - } - STACK => { - super::super::print_kernel_stack(); - } - _ => { - let color_code_save = self.color_code; - self.color_code = ColorCode::new(Color::Red, Color::Black); - println!("Command unknown !"); - self.color_code = color_code_save; + { + let command: &str = &core::str::from_utf8(&self.command).unwrap()[..self.command_len]; + match command { + "shutdown" | "halt" => console::shutdown(), + "reboot" => console::reboot(), + "stack" => console::print_kernel_stack(), + _ => { + let color_code_save = self.color_code; + self.color_code = ColorCode::new(Color::Red, Color::Black); + println!("`{}': Command unknown ", command); + self.color_code = color_code_save; + } } } - self.command = NULL; + self.command_len = 0; self.prompt(); } byte => { if self.command_len >= 10 { return }; - self.command[self.command_len] = byte as char; + self.command[self.command_len] = byte; self.write_byte(byte); self.command_len += 1; } @@ -190,6 +176,7 @@ impl Writer { } } +// trait needed by formatting macros use core::fmt; impl fmt::Write for Writer { fn write_str(&mut self, s: &str) -> ::core::fmt::Result {