added console.rs to group commands, refactored the command buffer and command matching code

This commit is contained in:
Jack Halford 2018-02-21 13:28:59 +01:00
parent 0e7b73e2fa
commit 554872c8fa
4 changed files with 75 additions and 83 deletions

41
kernel-rs/src/console.rs Normal file
View file

@ -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");
}

View file

@ -1,4 +1,8 @@
//! Rust wrappers around the x86-family I/O instructions. //! 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`. /// Read a `u8`-sized value from `port`.
pub fn inb(port: u16) -> u8 { pub fn inb(port: u16) -> u8 {

View file

@ -1,64 +1,24 @@
#![no_std]
#![feature(lang_items)] #![feature(lang_items)]
#![feature(const_fn)] #![feature(const_fn)]
#![feature(ptr_internals)] #![feature(ptr_internals)]
#![feature(asm)] #![feature(asm)] //needed by cpuio for inline asm
#![no_std]
extern crate spin; // extern crate spin;
extern crate volatile; // extern crate volatile;
extern crate rlibc; extern crate rlibc;
#[macro_use] #[macro_use]
mod vga; mod vga; // 80x25 screen and terminal
mod context; // kernel init and environment
mod context; mod keyboard; // PS/2 detection and processing
mod keyboard; mod console; // vga terminal commands
mod cpuio; // asm wrapper to cpu i/o
use context::CONTEXT; use context::CONTEXT;
use vga::{Color, ColorCode}; 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] #[no_mangle]
pub extern fn kmain() -> ! { pub extern fn kmain() -> ! {
unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Cyan); } unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Cyan); }

View file

@ -10,6 +10,7 @@
use super::{Color, ColorCode}; use super::{Color, ColorCode};
use ::context::CONTEXT; use ::context::CONTEXT;
use cpuio; use cpuio;
use ::console;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(C)] #[repr(C)]
@ -20,8 +21,8 @@ struct ScreenChar {
macro_rules! print { macro_rules! print {
($($arg:tt)*) => ({ ($($arg:tt)*) => ({
$crate::vga::buffer::print(format_args!($($arg)*)); $crate::vga::buffer::print(format_args!($($arg)*));
}); });
} }
macro_rules! println { macro_rules! println {
@ -44,26 +45,17 @@ pub struct Writer {
pub buffer_pos: usize, pub buffer_pos: usize,
pub color_code: ColorCode, pub color_code: ColorCode,
buffer: [u8; BUFFER_ROWS * BUFFER_COLS], buffer: [u8; BUFFER_ROWS * BUFFER_COLS],
command: [char; 10], command: [u8; 10],
command_len: usize, 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 { impl Writer {
pub const fn new() -> Writer { pub const fn new() -> Writer {
Writer { Writer {
buffer_pos: 0, buffer_pos: 0,
color_code: ColorCode::new(Color::White, Color::Black), color_code: ColorCode::new(Color::White, Color::Black),
buffer: [0; BUFFER_ROWS * BUFFER_COLS], buffer: [0; BUFFER_ROWS * BUFFER_COLS],
command: NULL, command: [b'\0'; 10],
command_len: 0, command_len: 0,
} }
} }
@ -78,7 +70,6 @@ impl Writer {
pub fn backspace(&mut self) { pub fn backspace(&mut self) {
if self.command_len > 0 { if self.command_len > 0 {
self.command_len -= 1; self.command_len -= 1;
self.command[self.command_len] = '\0';
self.erase_byte(); self.erase_byte();
} }
} }
@ -86,33 +77,28 @@ impl Writer {
pub fn keypress(&mut self, ascii: u8) { pub fn keypress(&mut self, ascii: u8) {
match ascii { match ascii {
b'\n' => { b'\n' => {
self.command_len = 0;
self.write_byte(b'\n'); self.write_byte(b'\n');
// println!("{:?}", self.command.iter()); {
match self.command { let command: &str = &core::str::from_utf8(&self.command).unwrap()[..self.command_len];
SHUTDOWN | HALT => { match command {
super::super::shutdown(); "shutdown" | "halt" => console::shutdown(),
} "reboot" => console::reboot(),
REBOOT => { "stack" => console::print_kernel_stack(),
super::super::reboot(); _ => {
} let color_code_save = self.color_code;
STACK => { self.color_code = ColorCode::new(Color::Red, Color::Black);
super::super::print_kernel_stack(); println!("`{}': Command unknown ", command);
} self.color_code = color_code_save;
_ => { }
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;
} }
} }
self.command = NULL; self.command_len = 0;
self.prompt(); self.prompt();
} }
byte => { byte => {
if self.command_len >= 10 { return }; 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.write_byte(byte);
self.command_len += 1; self.command_len += 1;
} }
@ -190,6 +176,7 @@ impl Writer {
} }
} }
// trait needed by formatting macros
use core::fmt; use core::fmt;
impl fmt::Write for Writer { impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> ::core::fmt::Result { fn write_str(&mut self, s: &str) -> ::core::fmt::Result {