From 5412334168990aa42b1b3d3d0d30aba55346d006 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 5 Mar 2018 13:12:02 +0100 Subject: [PATCH] it runs --- kernel-rs/Cargo.toml | 4 --- kernel-rs/src/console.rs | 17 ++++++----- kernel-rs/src/context.rs | 62 +++++++++++++++++++++++++++------------ kernel-rs/src/keyboard.rs | 10 +++---- kernel-rs/src/lib.rs | 27 ++--------------- kernel-rs/src/vga/mod.rs | 18 ++++++------ 6 files changed, 70 insertions(+), 68 deletions(-) diff --git a/kernel-rs/Cargo.toml b/kernel-rs/Cargo.toml index f7771995..f854812c 100644 --- a/kernel-rs/Cargo.toml +++ b/kernel-rs/Cargo.toml @@ -11,7 +11,3 @@ rlibc = "1.0" bitflags = "1.0.1" # spin = "0.4.5" multiboot2 = { path = "multiboot2-elf64" } - -# [dependencies.lazy_static] -# version = "0.2.4" -# features = ["spin_no_std"] diff --git a/kernel-rs/src/console.rs b/kernel-rs/src/console.rs index 42566a12..d8e9fefc 100644 --- a/kernel-rs/src/console.rs +++ b/kernel-rs/src/console.rs @@ -1,10 +1,11 @@ extern crate core; -extern crate multiboot2; +// extern crate multiboot2; use acpi; use cpuio; +use context; +// use multiboot2; use core::char; -use context::*; use vga::*; fn dispatch(command: &str) -> Result <(), &'static str> { @@ -35,10 +36,10 @@ fn help() -> Result <(), &'static str> { print!("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n", "acpi => Return acpi state (ENABLED|DISABLE)", "help | h => Print this help", - "memory => lolilol", // TODO - "multiboot => lolilol", // TODO + "memory => print memory areas", // TODO + "multiboot => print multiboot information", // TODO "reboot => reboot", - "sections => lolilol", // TODO + "sections => print elf sections", // TODO "shutdown | halt | q => Kill a kitten, then shutdown", "stack => Print kernel stack in a fancy way"); Ok(()) @@ -120,7 +121,7 @@ fn print_stack() -> Result <(), &'static str> { } fn mb2_memory() -> Result <(), &'static str> { - let boot_info = &context().boot_info; + let boot_info = context::boot_info(); let memory_map_tag = boot_info.memory_map_tag() .expect("Memory map tag required"); @@ -134,7 +135,7 @@ fn mb2_memory() -> Result <(), &'static str> { } fn mb2_sections() -> Result <(), &'static str> { - let boot_info = &context().boot_info; + let boot_info = context::boot_info(); let elf_sections_tag = boot_info.elf_sections_tag() .expect("Elf-sections tag required"); @@ -148,7 +149,7 @@ fn mb2_sections() -> Result <(), &'static str> { } fn mb2_info() -> Result <(), &'static str> { - let boot_info = &context().boot_info; + let boot_info = context::boot_info(); let command_line_tag = boot_info.command_line_tag() .expect("Elf-sections tag required"); diff --git a/kernel-rs/src/context.rs b/kernel-rs/src/context.rs index 6569012d..64eea470 100644 --- a/kernel-rs/src/context.rs +++ b/kernel-rs/src/context.rs @@ -52,29 +52,51 @@ impl Context } } - pub fn init_screen(&mut self) { - self.vga1.prompt(); - self.vga2.prompt(); - self.vga1.flush(); - } +} - pub fn switch_term(&mut self) { - self.current_term = { - if self.current_term == 0 { 1 } - else { 0 } - }; - } +pub fn init_screen() { + set_color!(White, Cyan); + print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}", + format_args!("{: ^80}", r#" ,--, "#), + format_args!("{: ^80}", r#" ,--.'| ,----, "#), + format_args!("{: ^80}", r#" ,--, | : .' .' \ "#), + format_args!("{: ^80}", r#",---.'| : ' ,----,' | "#), + format_args!("{: ^80}", r#"; : | | ; | : . ; "#), + format_args!("{: ^80}", r#"| | : _' | ; |.' / "#), + format_args!("{: ^80}", r#": : |.' | `----'/ ; "#), + format_args!("{: ^80}", r#"| ' ' ; : / ; / "#), + format_args!("{: ^80}", r#"\ \ .'. | ; / /-, "#), + format_args!("{: ^80}", r#" `---`: | ' / / /.`| "#), + format_args!("{: ^80}", r#" ' ; |./__; : "#), + format_args!("{: ^80}", r#" | : ;| : .' "#), + format_args!("{: ^80}", r#" ' ,/ ; | .' "#), + format_args!("{: ^80}", r#" '--' `---' "#)); + set_color!(); + context().vga1.prompt(); + context().vga2.prompt(); + context().vga1.flush(); +} - pub fn current_term(&mut self) -> &mut vga::Writer{ - if self.current_term == 0 { - &mut self.vga1 - } else { - &mut self.vga2 - } +pub fn switch_term() { + context().current_term = { + if context().current_term == 0 { 1 } + else { 0 } + }; +} + +pub fn current_term() -> &'static mut vga::Writer{ + if context().current_term == 0 { + &mut context().vga1 + } else { + &mut context().vga2 } } -pub fn context() -> &'static mut Context { +pub fn boot_info() -> &'static multiboot2::BootInformation { + &context().boot_info +} + +fn context() -> &'static mut Context { unsafe { match CONTEXT { Some(ref mut x) => &mut *x, @@ -82,3 +104,7 @@ pub fn context() -> &'static mut Context { } } } + +pub fn init(multiboot_info_addr: usize) { + unsafe { CONTEXT = Some(Context::new(multiboot_info_addr)) }; +} diff --git a/kernel-rs/src/keyboard.rs b/kernel-rs/src/keyboard.rs index 7084495a..a29f14b3 100644 --- a/kernel-rs/src/keyboard.rs +++ b/kernel-rs/src/keyboard.rs @@ -1,7 +1,7 @@ extern crate core; use cpuio; -use context::*; +use context; const MAX_KEYS: usize = 59; const KEYMAP_US: [[u8;2]; MAX_KEYS] = [ @@ -93,18 +93,18 @@ pub fn kbd_callback() { 0x38 => {ALT = !is_release}, 0x1D => {CTRL = !is_release}, 0x0F if !is_release => { - context().switch_term(); - context().current_term().flush(); + context::switch_term(); + context::current_term().flush(); }, 0x0E if !is_release => { - context().current_term().backspace(); + context::current_term().backspace(); } _ => {} } }, Some(ascii) if !is_release => { let sym = if SHIFT { ascii[1] } else { ascii[0] }; - context().current_term().keypress(sym); + context::current_term().keypress(sym); }, Some(_) => {}, None =>{}, diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 89dca7fa..93c08533 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -25,34 +25,13 @@ pub mod acpi; /// simple area frame allocator implementation pub mod memory; -use context::*; -use memory::*; - -use vga::{Color, ColorCode}; +// use vga::{Color, ColorCode}; #[no_mangle] pub extern fn kmain(multiboot_info_addr: usize) -> ! { - unsafe { CONTEXT = Some(Context::new(multiboot_info_addr)) }; + context::init(multiboot_info_addr); + context::init_screen(); acpi::init().unwrap(); - context().init_screen(); - - set_color!(White, Cyan); - print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}", - format_args!("{: ^80}", r#" ,--, "#), - format_args!("{: ^80}", r#" ,--.'| ,----, "#), - format_args!("{: ^80}", r#" ,--, | : .' .' \ "#), - format_args!("{: ^80}", r#",---.'| : ' ,----,' | "#), - format_args!("{: ^80}", r#"; : | | ; | : . ; "#), - format_args!("{: ^80}", r#"| | : _' | ; |.' / "#), - format_args!("{: ^80}", r#": : |.' | `----'/ ; "#), - format_args!("{: ^80}", r#"| ' ' ; : / ; / "#), - format_args!("{: ^80}", r#"\ \ .'. | ; / /-, "#), - format_args!("{: ^80}", r#" `---`: | ' / / /.`| "#), - format_args!("{: ^80}", r#" ' ; |./__; : "#), - format_args!("{: ^80}", r#" | : ;| : .' "#), - format_args!("{: ^80}", r#" ' ,/ ; | .' "#), - format_args!("{: ^80}", r#" '--' `---' "#)); - set_color!(); loop { keyboard::kbd_callback(); } } diff --git a/kernel-rs/src/vga/mod.rs b/kernel-rs/src/vga/mod.rs index fa2efba8..8bc3ebf6 100644 --- a/kernel-rs/src/vga/mod.rs +++ b/kernel-rs/src/vga/mod.rs @@ -2,7 +2,7 @@ pub mod color; pub use self::color::{Color, ColorCode}; -use context::*; +use context; use cpuio; use console; @@ -25,21 +25,21 @@ macro_rules! println { } macro_rules! flush { - () => (context().current_term().flush()); + () => (context::current_term().flush()); } macro_rules! set_color { - () => (context().current_term().color_code = - ColorCode::new(Color::White, Color::Black)); - ($fg:ident) => (context().current_term().color_code = - ColorCode::new(Color::$fg, Color::Black)); - ($fg:ident, $bg:ident) => (context().current_term().color_code = - ColorCode::new(Color::$fg, Color::$bg)); + () => ($crate::context::current_term().color_code = + $crate::vga::ColorCode::new($crate::vga::Color::White, $crate::vga::Color::Black)); + ($fg:ident) => ($crate::context::current_term().color_code = + $crate::vga::ColorCode::new($crate::vga::Color::$fg, $crate::vga::Color::Black)); + ($fg:ident, $bg:ident) => ($crate::context::current_term().color_code = + $crate::vga::ColorCode::new($crate::vga::Color::$fg, $crate::vga::Color::$bg)); } pub fn print(args: fmt::Arguments) { use core::fmt::Write; - context().current_term().write_fmt(args).unwrap(); + context::current_term().write_fmt(args).unwrap(); } extern crate core;